Windows Unit Test Support (#629)

Window Unit Test Support
This commit is contained in:
Nick Banks
2016-09-20 18:11:19 -07:00
committed by Jonathan Hui
parent 4bd62977d4
commit 25cea9414b
19 changed files with 753 additions and 123 deletions
+9 -10
View File
@@ -57,7 +57,6 @@ AM_CPPFLAGS = \
COMMON_LDADD = \
$(top_builddir)/src/core/libopenthread.a \
$(top_builddir)/examples/platforms/posix/libopenthread-posix.a \
$(top_builddir)/third_party/mbedtls/libmbedcrypto.a \
-lpthread \
$(NULL)
@@ -109,31 +108,31 @@ TESTS_ENVIRONMENT = \
# Source, compiler, and linker options for test programs.
test_aes_LDADD = $(COMMON_LDADD)
test_aes_SOURCES = test_aes.cpp
test_aes_SOURCES = test_platform.cpp test_aes.cpp
test_hmac_sha256_LDADD = $(COMMON_LDADD)
test_hmac_sha256_SOURCES = test_hmac_sha256.cpp
test_hmac_sha256_SOURCES = test_platform.cpp test_hmac_sha256.cpp
test_link_quality_LDADD = $(COMMON_LDADD)
test_link_quality_SOURCES = test_link_quality.cpp
test_link_quality_SOURCES = test_platform.cpp test_link_quality.cpp
test_lowpan_LDADD = $(COMMON_LDADD)
test_lowpan_SOURCES = test_lowpan.cpp test_util.cpp test_vector.c
test_lowpan_SOURCES = test_platform.cpp test_lowpan.cpp test_util.cpp test_vector.c
test_mac_frame_LDADD = $(COMMON_LDADD)
test_mac_frame_SOURCES = test_mac_frame.cpp
test_mac_frame_SOURCES = test_platform.cpp test_mac_frame.cpp
test_message_LDADD = $(COMMON_LDADD)
test_message_SOURCES = test_message.cpp
test_message_SOURCES = test_platform.cpp test_message.cpp
test_ncp_buffer_LDADD = $(COMMON_LDADD)
test_ncp_buffer_SOURCES = test_ncp_buffer.cpp
test_ncp_buffer_SOURCES = test_platform.cpp test_ncp_buffer.cpp
test_timer_LDADD = $(COMMON_LDADD)
test_timer_SOURCES = test_timer.cpp
test_timer_SOURCES = test_platform.cpp test_timer.cpp
test_toolchain_LDADD = $(COMMON_LDADD)
test_toolchain_SOURCES = test_toolchain.cpp
test_toolchain_SOURCES = test_platform.cpp test_toolchain.cpp
if OPENTHREAD_ENABLE_DIAG
test_diag_LDADD = $(top_builddir)/src/diag/libopenthread-diag.a \
+2 -4
View File
@@ -35,10 +35,6 @@
static Thread::Crypto::MbedTls mbedtls;
extern"C" void otSignalTaskletPending(otInstance *)
{
}
/**
* Verifies test vectors from IEEE 802.15.4-2006 Annex C Section C.2.1
*/
@@ -233,6 +229,7 @@ void TestMacCommandFrame()
"TestMacCommandFrame decrypt failed\n");
}
#ifdef ENABLE_TEST_MAIN
int main(void)
{
TestMacBeaconFrame();
@@ -241,3 +238,4 @@ int main(void)
printf("All tests passed\n");
return 0;
}
#endif
+2
View File
@@ -165,9 +165,11 @@ void TestDiag()
}
}
#ifdef ENABLE_TEST_MAIN
int main(void)
{
TestDiag();
printf("All tests passed\n");
return 0;
}
#endif
+2 -4
View File
@@ -36,10 +36,6 @@
static Thread::Crypto::MbedTls mbedtls;
extern"C" void otSignalTaskletPending(otInstance *)
{
}
void TestHmacSha256(void)
{
static const struct
@@ -80,9 +76,11 @@ void TestHmacSha256(void)
}
}
#ifdef ENABLE_TEST_MAIN
int main(void)
{
TestHmacSha256();
printf("All tests passed\n");
return 0;
}
#endif
+2 -4
View File
@@ -34,10 +34,6 @@
namespace Thread {
extern"C" void otSignalTaskletPending(otInstance *)
{
}
enum
{
kMaxRssValue = 0,
@@ -342,6 +338,7 @@ void TestLinkQualityCalculations(void)
} // namespace Thread
#ifdef ENABLE_TEST_MAIN
int main(void)
{
Thread::TestRssAveraging();
@@ -349,3 +346,4 @@ int main(void)
printf("All tests passed\n");
return 0;
}
#endif
+2 -36
View File
@@ -40,41 +40,6 @@ using namespace Thread;
namespace Thread {
extern "C" void otSignalTaskletPending(otInstance *)
{
}
extern "C" bool otAreTaskletsPending(otInstance *)
{
return false;
}
extern "C" void otPlatUartSendDone(void)
{
}
extern "C" void otPlatUartReceived(const uint8_t *aBuf, uint16_t aBufLength)
{
(void)aBuf;
(void)aBufLength;
}
extern "C" void otPlatDiagAlarmFired(otInstance *)
{
}
extern "C" void otPlatDiagRadioTransmitDone(otInstance *, bool aRxPending, ThreadError aError)
{
(void)aRxPending;
(void)aError;
}
extern "C" void otPlatDiagRadioReceiveDone(otInstance *, RadioPacket *aFrame, ThreadError aError)
{
(void)aFrame;
(void)aError;
}
Ip6::Ip6 sIp6;
ThreadNetif sMockThreadNetif(sIp6);
Lowpan::Lowpan sMockLowpan(sMockThreadNetif);
@@ -162,7 +127,7 @@ void TestLowpanIphc(void)
} // namespace Thread
#ifdef ENABLE_TEST_MAIN
int main(void)
{
TestLowpanIphc();
@@ -170,3 +135,4 @@ int main(void)
printf("All tests passed\n");
return 0;
}
#endif
+2 -4
View File
@@ -34,10 +34,6 @@
namespace Thread {
extern"C" void otSignalTaskletPending(otInstance *)
{
}
void TestMacHeader(void)
{
static const struct
@@ -88,9 +84,11 @@ void TestMacHeader(void)
} // namespace Thread
#ifdef ENABLE_TEST_MAIN
int main(void)
{
Thread::TestMacHeader();
printf("All tests passed\n");
return 0;
}
#endif
+2 -8
View File
@@ -32,14 +32,6 @@
#include <common/message.hpp>
#include <string.h>
extern"C" void otSignalTaskletPending(otInstance *)
{
}
extern "C" void otPlatDiagAlarmFired(otInstance *)
{
}
void TestMessage(void)
{
Thread::MessagePool messagePool;
@@ -68,9 +60,11 @@ void TestMessage(void)
"Message::Free failed\n");
}
#ifdef ENABLE_TEST_MAIN
int main(void)
{
TestMessage();
printf("All tests passed\n");
return 0;
}
#endif
+2 -8
View File
@@ -36,14 +36,6 @@ namespace Thread {
// This module implements unit-test for NcpFrameBuffer class.
extern"C" void otSignalTaskletPending(otInstance *)
{
}
extern "C" void otPlatDiagAlarmFired(otInstance *)
{
}
enum
{
kTestBufferSize = 101, // Size of backed buffer for NcpFrameBuffer.
@@ -468,9 +460,11 @@ void TestNcpFrameBuffer(void)
} // namespace Thread
#ifdef ENABLE_TEST_MAIN
int main(void)
{
Thread::TestNcpFrameBuffer();
printf("\nAll tests passed.\n");
return 0;
}
#endif
+258
View File
@@ -0,0 +1,258 @@
/*
* Copyright (c) 2016, The OpenThread Authors.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#if _WIN32
#define _CRT_SECURE_NO_WARNINGS
#include <windows.h>
#endif
#include <openthread.h>
#include <common/code_utils.hpp>
#include <platform/alarm.h>
#include <platform/misc.h>
#include <platform/radio.h>
#include <platform/random.h>
#include <stdio.h>
#include <stdlib.h>
enum
{
kCallCountIndexAlarmStop = 0,
kCallCountIndexAlarmStart,
kCallCountIndexTimerHandler,
kCallCountIndexMax
};
uint32_t sNow;
uint32_t sPlatT0;
uint32_t sPlatDt;
bool sTimerOn;
uint32_t sCallCount[kCallCountIndexMax];
bool sDiagMode = false;
extern "C" {
void otSignalTaskletPending(otInstance *)
{
}
bool otAreTaskletsPending(otInstance *)
{
return false;
}
//
// Alarm
//
void otPlatAlarmStop(otInstance *)
{
sTimerOn = false;
sCallCount[kCallCountIndexAlarmStop]++;
}
void otPlatAlarmStartAt(otInstance *, uint32_t aT0, uint32_t aDt)
{
sTimerOn = true;
sCallCount[kCallCountIndexAlarmStart]++;
sPlatT0 = aT0;
sPlatDt = aDt;
}
uint32_t otPlatAlarmGetNow(void)
{
return sNow;
}
//
// Radio
//
void otPlatRadioGetIeeeEui64(otInstance *, uint8_t *)
{
}
ThreadError otPlatRadioSetPanId(otInstance *, uint16_t)
{
return kThreadError_None;
}
ThreadError otPlatRadioSetExtendedAddress(otInstance *, uint8_t *)
{
return kThreadError_None;
}
ThreadError otPlatRadioSetShortAddress(otInstance *, uint16_t)
{
return kThreadError_None;
}
void otPlatRadioSetPromiscuous(otInstance *, bool)
{
}
ThreadError otPlatRadioEnable(otInstance *)
{
return kThreadError_None;
}
ThreadError otPlatRadioDisable(otInstance *)
{
return kThreadError_None;
}
ThreadError otPlatRadioSleep(otInstance *)
{
return kThreadError_None;
}
ThreadError otPlatRadioReceive(otInstance *, uint8_t)
{
return kThreadError_None;
}
ThreadError otPlatRadioTransmit(otInstance *)
{
return kThreadError_None;
}
RadioPacket *otPlatRadioGetTransmitBuffer(otInstance *)
{
return (RadioPacket *)0;
}
int8_t otPlatRadioGetRssi(otInstance *)
{
return 0;
}
otRadioCaps otPlatRadioGetCaps(otInstance *)
{
return kRadioCapsNone;
}
bool otPlatRadioGetPromiscuous(otInstance *)
{
return false;
}
//
// Random
//
uint32_t otPlatRandomGet(void)
{
#if _WIN32
return (uint32_t)rand();
#else
return (uint32_t)random();
#endif
}
ThreadError otPlatRandomSecureGet(uint16_t aInputLength, uint8_t *aOutput, uint16_t *aOutputLength)
{
ThreadError error = kThreadError_None;
VerifyOrExit(aOutput && aOutputLength, error = kThreadError_InvalidArgs);
for (uint16_t length = 0; length < aInputLength; length++)
{
aOutput[length] = (uint8_t)otPlatRandomGet();
}
*aOutputLength = aInputLength;
exit:
return error;
}
//
// Diag
//
void otPlatDiagProcess(int argc, char *argv[], char *aOutput)
{
// no more diagnostics features for Posix platform
sprintf(aOutput, "diag feature '%s' is not supported\r\n", argv[0]);
(void)argc;
}
void otPlatDiagModeSet(bool aMode)
{
sDiagMode = aMode;
}
bool otPlatDiagModeGet()
{
return sDiagMode;
}
void otPlatDiagAlarmFired(otInstance *)
{
}
void otPlatDiagRadioTransmitDone(otInstance *, bool, ThreadError)
{
}
void otPlatDiagRadioReceiveDone(otInstance *, RadioPacket *, ThreadError)
{
}
//
// Uart
//
void otPlatUartSendDone(void)
{
}
void otPlatUartReceived(const uint8_t *, uint16_t)
{
}
//
// Misc
//
void otPlatReset(otInstance *aInstance)
{
(void)aInstance;
}
otPlatResetReason otPlatGetResetReason(otInstance *aInstance)
{
(void)aInstance;
return kPlatResetReason_PowerOn;
}
} // extern "C"
+7 -28
View File
@@ -43,34 +43,11 @@ enum
};
static Thread::TimerScheduler sTimerScheduler;
static uint32_t sNow;
static uint32_t sPlatT0;
static uint32_t sPlatDt;
static bool sTimerOn;
static uint32_t sCallCount[kCallCountIndexMax];
extern "C" {
void otPlatAlarmStop(otInstance *)
{
sTimerOn = false;
sCallCount[kCallCountIndexAlarmStop]++;
}
void otPlatAlarmStartAt(otInstance *, uint32_t aT0, uint32_t aDt)
{
sTimerOn = true;
sCallCount[kCallCountIndexAlarmStart]++;
sPlatT0 = aT0;
sPlatDt = aDt;
}
uint32_t otPlatAlarmGetNow(void)
{
return sNow;
}
} // extern "C"
extern uint32_t sNow;
extern uint32_t sPlatT0;
extern uint32_t sPlatDt;
extern bool sTimerOn;
extern uint32_t sCallCount[kCallCountIndexMax];
void InitCounters(void)
{
@@ -409,9 +386,11 @@ void RunTimerTests(void)
TestTenTimers();
}
#ifdef ENABLE_TEST_MAIN
int main(void)
{
RunTimerTests();
printf("All tests passed\n");
return 0;
}
#endif
+17 -6
View File
@@ -34,23 +34,29 @@
void test_packed1()
{
typedef OT_TOOL_PACKED_BEGIN struct
OT_TOOL_PACKED_BEGIN
struct packed_t
{
uint8_t mByte;
uint32_t mWord;
uint16_t mShort;
} OT_TOOL_PACKED_END packed_t;
} OT_TOOL_PACKED_END;
CompileTimeAssert(sizeof(packed_t) == 7, "packed_t should be packed to 7 bytes");
VerifyOrQuit(sizeof(packed_t) == 7, "Toolchain::OT_TOOL_PACKED failed 1\n");
}
void test_packed2()
{
typedef OT_TOOL_PACKED_BEGIN struct
OT_TOOL_PACKED_BEGIN
struct packed_t
{
uint8_t mBytes[3];
uint8_t mByte;
} OT_TOOL_PACKED_END packed_t;
} OT_TOOL_PACKED_END;
CompileTimeAssert(sizeof(packed_t) == 4, "packed_t should be packed to 4 bytes");
VerifyOrQuit(sizeof(packed_t) == 4, "Toolchain::OT_TOOL_PACKED failed 2\n");
}
@@ -62,7 +68,8 @@ void test_packed_union()
uint16_t mField;
} nested_t;
typedef OT_TOOL_PACKED_BEGIN struct
OT_TOOL_PACKED_BEGIN
struct packed_t
{
uint8_t mBytes[3];
union
@@ -70,7 +77,9 @@ void test_packed_union()
nested_t mNestedStruct;
uint8_t mByte;
} OT_TOOL_PACKED_FIELD;
} OT_TOOL_PACKED_END packed_t;
} OT_TOOL_PACKED_END;
CompileTimeAssert(sizeof(packed_t) == 5, "packed_t should be packed to 5 bytes");
VerifyOrQuit(sizeof(packed_t) == 5, "Toolchain::OT_TOOL_PACKED failed 3\n");
}
@@ -92,9 +101,11 @@ void TestToolchain(void)
test_packed_enum();
}
#ifdef ENABLE_TEST_MAIN
int main(void)
{
TestToolchain();
printf("All tests passed\n");
return 0;
}
#endif
+27
View File
@@ -34,10 +34,15 @@
#include <openthread-types.h>
#ifndef _WIN32
#ifdef __cplusplus
extern "C" {
#endif
// Enable main functions
#define ENABLE_TEST_MAIN
#define SuccessOrQuit(ERR, MSG) \
do { \
if ((ERR) != kThreadError_None) \
@@ -58,8 +63,30 @@ extern "C" {
} \
} while (0)
//#define CompileTimeAssert(COND, MSG) typedef char __C_ASSERT__[(COND)?1:-1]
// I would use the above definition for CompileTimeAssert, but I am getting the following errors
// when I run 'make -f examples/Makefile-posix distcheck':
//
// error: typedef __C_ASSERT__ locally defined but not used [-Werror=unused-local-typedefs]
//
#define CompileTimeAssert(COND, MSG)
#ifdef __cplusplus
}
#endif
#else
typedef void (*utAssertTrue)(bool condition, const wchar_t *message);
extern utAssertTrue s_AssertTrue;
#define SuccessOrQuit(ERR, MSG) s_AssertTrue((ERR) == kThreadError_None, L##MSG)
#define VerifyOrQuit(ERR, MSG) s_AssertTrue(ERR, L##MSG)
#define CompileTimeAssert(COND, MSG) static_assert(COND, MSG)
#endif
#endif
+126
View File
@@ -0,0 +1,126 @@
/*
* Copyright (c) 2016, The OpenThread Authors.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <SDKDDKVer.h>
#include "CppUnitTest.h"
#include "test_util.h"
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
#pragma region Test Declarations
// test_aes.cpp
void TestMacBeaconFrame();
void TestMacDataFrame();
void TestMacCommandFrame();
// test_hmac_sha256.cpp
void TestHmacSha256();
// test_link_quality.cpp
namespace Thread
{
void TestRssAveraging();
void TestLinkQualityCalculations();
}
// test_lowpan.cpp
namespace Thread
{
void TestLowpanIphc();
}
// test_mac_frame.cpp
namespace Thread
{
void TestMacHeader();
}
// test_message.cpp
void TestMessage();
// test_timer.cpp
int TestOneTimer();
int TestTenTimers();
// test_toolchain.cpp
void test_packed1();
void test_packed2();
void test_packed_union();
void test_packed_enum();
#pragma endregion
utAssertTrue s_AssertTrue;
namespace Thread
{
TEST_CLASS(UnitTests)
{
public:
UnitTests() { s_AssertTrue = AssertTrue; }
// Helper for openthread test code to call
static void AssertTrue(bool condition, const wchar_t* message)
{
Assert::IsTrue(condition, message);
}
// test_aes.cpp
TEST_METHOD(TestMacBeaconFrame) { ::TestMacBeaconFrame(); }
TEST_METHOD(TestMacDataFrame) { ::TestMacDataFrame(); }
TEST_METHOD(TestMacCommandFrame) { ::TestMacCommandFrame(); }
// test_hmac_sha256.cpp
TEST_METHOD(TestHmacSha256) { ::TestHmacSha256(); }
// test_link_quality.cpp
TEST_METHOD(TestRssAveraging) { Thread::TestRssAveraging(); }
TEST_METHOD(TestLinkQualityCalculations) { Thread::TestLinkQualityCalculations(); }
// test_lowpan.cpp
TEST_METHOD(TestLowpanIphc) { Thread::TestLowpanIphc(); }
// test_mac_frame.cpp
TEST_METHOD(TestMacHeader) { Thread::TestMacHeader(); }
// test_message.cpp
TEST_METHOD(TestMessage) { ::TestMessage(); }
// test_message.cpp
TEST_METHOD(TestOneTimer) { ::TestOneTimer(); }
TEST_METHOD(TestTenTimers) { ::TestTenTimers(); }
// test_toolchain.cpp
TEST_METHOD(test_packed1) { ::test_packed1(); }
TEST_METHOD(test_packed2) { ::test_packed2(); }
TEST_METHOD(test_packed_union) { ::test_packed_union(); }
TEST_METHOD(test_packed_enum) { ::test_packed_enum(); }
};
}