test: Lowpan module unit tests (#151)

* Added initial unit tests for 6LoWPAN layer decompression. Extended unit test framework with helper functions for setting up ASCI hex test vectors.

* Added missing license headers. Cleanly broke C++ into .hpp vs .h.

* Added multicast test vectors to 6lo decompression unit tests.

* Make pretty.

* Added Lowpan::Compress unit test.

* Allow extended initializer lists for unit test build.

* Split out sTestVectorLowpan into pure C99 file as it uses struct { .mField = initializers; } which may upset stricter C++ compilers such as clang++.

* Fix make distcheck - add stubs to test_lowpan for unhandled driver callbacks.

* Resolve most comments besides otDump suggestion.
This commit is contained in:
turon
2016-06-21 13:05:31 -07:00
committed by Jonathan Hui
parent 4369a31411
commit 31bc49de32
7 changed files with 520 additions and 0 deletions
+6
View File
@@ -34,6 +34,8 @@ include $(abs_top_nlbuild_autotools_dir)/automake/pre.am
#
noinst_HEADERS = \
test_util.h \
test_util.hpp \
test_vector.h \
$(NULL)
#
@@ -65,6 +67,7 @@ COMMON_LDADD = \
check_PROGRAMS = \
test-aes \
test-hmac-sha256 \
test-lowpan \
test-mac-frame \
test-message \
test-timer \
@@ -92,6 +95,9 @@ test_aes_SOURCES = test_aes.cpp
test_hmac_sha256_LDADD = $(COMMON_LDADD)
test_hmac_sha256_SOURCES = test_hmac_sha256.cpp
test_lowpan_LDADD = $(COMMON_LDADD)
test_lowpan_SOURCES = test_lowpan.cpp test_util.cpp test_vector.c
test_mac_frame_LDADD = $(COMMON_LDADD)
test_mac_frame_SOURCES = test_mac_frame.cpp
+155
View File
@@ -0,0 +1,155 @@
/*
* Copyright (c) 2016, Nest Labs, Inc.
* 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 "test_util.hpp"
#include "test_vector.h"
#include <common/debug.hpp>
#include <string.h>
#include <openthread.h>
#include <mac/mac.hpp>
#include <thread/thread_netif.hpp>
#include <thread/lowpan.hpp>
using namespace Thread;
namespace Thread {
extern "C" void otSignalTaskletPending(void)
{
}
extern "C" bool otAreTaskletsPending(void)
{
return false;
}
extern "C" void otPlatSerialSendDone(void)
{
}
extern "C" void otPlatSerialReceived(const uint8_t *aBuf, uint16_t aBufLength)
{
}
ThreadNetif sMockThreadNetif;
Lowpan::Lowpan sMockLowpan(sMockThreadNetif);
void TestLowpanIphc(void)
{
Message *message = NULL;
uint8_t result[1500];
unsigned resultLength = 0;
Mac::Address macSource;
Mac::Address macDest;
test_lowpan_vector_t *tests = sTestVectorLowpan;
for (unsigned i = 0; i < sTestVectorLowpanLen; i++)
{
// Prepare next test vector
std::string ipString(tests[i].mRaw);
std::vector<uint8_t> ipVector;
otTestHexToVector(ipString, ipVector);
std::string iphcString(tests[i].mCompressed);
std::vector<uint8_t> iphcVector;
otTestHexToVector(iphcString, iphcVector);
printf("=== Packet #%d: %s ===\n", i, tests[i].mTest);
printf("6lo Packet:\n");
otTestPrintHex(iphcVector);
printf("Decompressed Reference:\n");
otTestPrintHex(ipVector);
// Test decompression
Mac::Frame frame;
frame.mPsdu = iphcVector.data();
frame.mLength = iphcVector.size();
frame.GetSrcAddr(macSource);
frame.GetDstAddr(macDest);
VerifyOrQuit((message = Ip6::Ip6::NewMessage(0)) != NULL,
"6lo: Ip6::NewMessage failed");
// ===> Test Lowpan::Decompress
int decompressedBytes =
sMockLowpan.Decompress(*message, macSource, macDest,
frame.GetPayload(),
frame.GetPayloadLength(), 0);
uint16_t ip6PayloadLength = frame.GetPayloadLength() -
decompressedBytes;
SuccessOrQuit(message->Append(frame.GetPayload() + decompressedBytes,
ip6PayloadLength),
"6lo: Message::Append failed");
ip6PayloadLength = HostSwap16(message->GetLength() -
sizeof(Ip6::Header));
message->Write(Ip6::Header::GetPayloadLengthOffset(),
sizeof(ip6PayloadLength), &ip6PayloadLength);
resultLength = message->GetLength();
message->Read(0, resultLength, result);
printf("Decompressed OpenThread:\n");
otTestPrintHex(result, resultLength);
VerifyOrQuit(memcmp(ipVector.data(), result, resultLength) == 0,
"6lo: Lowpan::Decompress failed");
// ===> Test Lowpan::Compress
int resultLength = sMockLowpan.Compress(*message, macSource, macDest,
result);
printf("Compressed OpenThread:\n");
otTestPrintHex(result, resultLength);
VerifyOrQuit(memcmp(frame.GetPayload(), result, resultLength) == 0,
"6lo: Lowpan::Compress failed");
SuccessOrQuit(Message::Free(*message), "6lo: Message:Free failed");
printf("PASS\n\n");
}
}
} // namespace Thread
int main(void)
{
Message::Init();
TestLowpanIphc();
printf("All tests passed\n");
return 0;
}
+74
View File
@@ -0,0 +1,74 @@
/*
* Copyright (c) 2016, Nest Labs, Inc.
* 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 "test_util.h"
#include <vector>
#include <string>
#include <sstream>
#include <iostream>
void otTestHexToVector(std::string &aHex, std::vector<uint8_t> &aOutBytes)
{
std::istringstream ss(aHex);
std::string word;
while (ss >> word)
{
uint8_t n = strtol(word.data(), NULL, 16);
aOutBytes.push_back(n);
}
}
void otTestPrintHex(uint8_t *aBuffer, int aLength)
{
int i;
for (i = 0; i < aLength; i++)
{
printf("%02x ", aBuffer[i]);
if (i % 16 == 7) { printf(" "); }
if (i % 16 == 15) { printf("\n"); }
}
printf("\n");
}
void otTestPrintHex(std::string &aString)
{
otTestPrintHex((uint8_t *)aString.data(), aString.size());
}
void otTestPrintHex(std::vector<uint8_t> &aBytes)
{
otTestPrintHex((uint8_t *)&aBytes[0], aBytes.size());
}
+8
View File
@@ -34,6 +34,10 @@
#include <openthread-types.h>
#ifdef __cplusplus
extern "C" {
#endif
#define SuccessOrQuit(ERR, MSG) \
do { \
if ((ERR) != kThreadError_None) \
@@ -54,4 +58,8 @@
} \
} while (0)
#ifdef __cplusplus
}
#endif
#endif
+46
View File
@@ -0,0 +1,46 @@
/*
* Copyright (c) 2016, Nest Labs, Inc.
* 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.
*/
#ifndef TEST_UTIL_HPP
#define TEST_UTIL_HPP
#include "test_util.h"
// STL is okay in unit tests.
#include <string>
#include <vector>
void otTestHexToVector(std::string &aHex, std::vector<uint8_t> &aOutBytes);
void otTestPrintHex(uint8_t *aBuffer, int aLength);
void otTestPrintHex(std::vector<uint8_t> &aBytes);
void otTestPrintHex(std::string &aString);
#endif
+157
View File
@@ -0,0 +1,157 @@
/*
* Copyright (c) 2016, Nest Labs, Inc.
* 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 "test_vector.h"
const test_lowpan_vector_t sTestVectorLowpan[] =
{
{
// I1_t1_AF_pass.pcap
.mTest = "LL64 unicast ICMP ping request",
.mCompressed =
"61 cc 1d ce fa 03 00 00 00 00 0a 6e 14 01 00 00 "
"00 00 0a 6e 14 7a 33 3a 80 00 30 be 00 00 00 00 "
"41 42 43 44 45 46 47 48 48 ff",
.mRaw =
"60 00 00 00 00 10 3a 40 fe 80 00 00 00 00 00 00 "
"16 6e 0a 00 00 00 00 01 fe 80 00 00 00 00 00 00 "
"16 6e 0a 00 00 00 00 03 80 00 30 be 00 00 00 00 "
"41 42 43 44 45 46 47 48",
.mMac =
{
.mSrc = "14 6e 0a 00 00 00 00 01",
.mDst = "14 6e 0a 00 00 00 00 03",
.mPanid = 0xFACE,
},
.mSrc = "fe80::166e:a00:0:1",
.mDst = "fe80::166e:a00:0:3",
.mHops = 64
},
{
// I1_t1_AF_pass.pcap
.mTest = "LL64 unicast ICMP ping reply",
.mCompressed =
"61 cc 07 ce fa 01 00 00 00 00 0a 6e 14 03 00 00 "
"00 00 0a 6e 14 7a 33 3a 81 00 2f be 00 00 00 00 "
"41 42 43 44 45 46 47 48 37 59",
.mRaw =
"60 00 00 00 00 10 3a 40 fe 80 00 00 00 00 00 00 "
"16 6e 0a 00 00 00 00 03 fe 80 00 00 00 00 00 00 "
"16 6e 0a 00 00 00 00 01 81 00 2f be 00 00 00 00 "
"41 42 43 44 45 46 47 48",
.mMac =
{
.mSrc = "14 6e 0a 00 00 00 00 03",
.mDst = "14 6e 0a 00 00 00 00 01",
.mPanid = 0xFACE,
},
},
{
// I1_t2_AF_AS_pass.pcap
.mTest = "LL16 unicast ICMP ping request",
.mCompressed =
"61 88 13 ce fa 00 10 00 00 7a 33 3a 80 00 63 9e "
"00 00 00 00 41 42 43 44 45 46 47 48 84 5f",
.mRaw =
"60 00 00 00 00 10 3a 40 fe 80 00 00 00 00 00 00 "
"00 00 00 ff fe 00 00 00 fe 80 00 00 00 00 00 00 "
"00 00 00 ff fe 00 10 00 80 00 63 9e 00 00 00 00 "
"41 42 43 44 45 46 47 48"
},
{
// I1_t2_AF_AS_pass.pcap
.mTest = "LL16 unicast ICMP ping reply",
.mCompressed =
"61 88 0f ce fa 00 00 00 10 7a 33 3a 81 00 62 9e "
"00 00 00 00 41 42 43 44 45 46 47 48 e0 35",
.mRaw =
"60 00 00 00 00 10 3a 40 fe 80 00 00 00 00 00 00 "
"00 00 00 ff fe 00 10 00 fe 80 00 00 00 00 00 00 "
"00 00 00 ff fe 00 00 00 81 00 62 9e 00 00 00 00 "
"41 42 43 44 45 46 47 48"
},
{
// I1_t3_SF_pass.pcap
.mTest = "LL64 multicast FF02::1 ICMP ping request",
.mCompressed =
"41 c8 99 ce fa ff ff 01 00 00 00 00 0a 6e 14 7a "
"3b 3a 01 80 00 54 b4 40 41 42 43 44 45 46 47 68 "
"44",
.mRaw =
"60 00 00 00 00 0c 3a 40 fe 80 00 00 00 00 00 00 "
"16 6e 0a 00 00 00 00 01 ff 02 00 00 00 00 00 00 "
"00 00 00 00 00 00 00 01 80 00 54 b4 40 41 42 43 "
"44 45 46 47 "
},
{
// I1_t3_SF_pass.pcap
.mTest = "LL64 multicast FF02::1 ICMP ping reply",
.mCompressed =
"61 cc fc ce fa 01 00 00 00 00 0a 6e 14 02 00 00 "
"00 00 0a 6e 14 7a 33 3a 81 00 33 c7 40 41 42 43 "
"44 45 46 47 1a 80",
.mRaw =
"60 00 00 00 00 0c 3a 40 fe 80 00 00 00 00 00 00 "
"16 6e 0a 00 00 00 00 02 fe 80 00 00 00 00 00 00 "
"16 6e 0a 00 00 00 00 01 81 00 33 c7 40 41 42 43 "
"44 45 46 47"
},
{
// I1_t4_FS_pass.pcap
.mTest = "LL16 multicast FF02::1 ICMP ping request",
.mCompressed =
"41 88 df ce fa ff ff 00 08 7a 3b 3a 01 80 00 76 "
"0e 00 01 00 04 50 50 50 50 50 50 50 50 50 50 50 "
"50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 "
"50 50 50 50 50 a7 d2",
.mRaw =
"60 00 00 00 00 28 3a 40 fe 80 00 00 00 00 00 00 "
"00 00 00 ff fe 00 08 00 ff 02 00 00 00 00 00 00 "
"00 00 00 00 00 00 00 01 80 00 76 0e 00 01 00 04 "
"50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 "
"50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 "
},
{
// I1_t4_FS_pass.pcap
.mTest = "LL16 multicast FF02::1 ICMP ping reply",
.mCompressed =
"61 c8 41 ce fa 00 08 03 00 00 00 00 0a 6e 14 7a "
"33 3a 81 00 55 20 00 01 00 04 50 50 50 50 50 50 "
"50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 "
"50 50 50 50 50 50 50 50 50 50 ab 56",
.mRaw =
"60 00 00 00 00 28 3a 40 fe 80 00 00 00 00 00 00 "
"16 6e 0a 00 00 00 00 03 fe 80 00 00 00 00 00 00 "
"00 00 00 ff fe 00 08 00 81 00 55 20 00 01 00 04 "
"50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 "
"50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 "
},
};
const unsigned sTestVectorLowpanLen =
sizeof(sTestVectorLowpan) / sizeof(sTestVectorLowpan[0]);
+74
View File
@@ -0,0 +1,74 @@
/*
* Copyright (c) 2016, Nest Labs, Inc.
* 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.
*/
#ifndef TEST_VECTOR_H
#define TEST_VECTOR_H
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef const struct
{
const char *mResult;
uint16_t mFcf;
uint16_t mSeq;
uint16_t mPanid; ///< default panid is destination panid
const char *mSrc;
const char *mDst;
} test_mac_vector_t;
typedef const struct
{
const char *mTest;
const char *mCompressed;
const char *mRaw;
const char *mPrefix;
test_mac_vector_t mMac;
uint16_t mTraffic;
uint16_t mFlow;
uint16_t mHops;
const char *mSrc;
const char *mDst;
} test_lowpan_vector_t;
extern const test_lowpan_vector_t sTestVectorLowpan[];
extern const unsigned sTestVectorLowpanLen;
#ifdef __cplusplus
}
#endif
#endif