mirror of
https://github.com/espressif/openthread.git
synced 2026-08-09 04:07:47 +00:00
Initial commit
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
#
|
||||
# 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 $(abs_top_nlbuild_autotools_dir)/automake/pre.am
|
||||
|
||||
#
|
||||
# Local headers to build against and distribute but not to install
|
||||
# since they are not part of the package.
|
||||
#
|
||||
noinst_HEADERS = \
|
||||
$(NULL)
|
||||
|
||||
#
|
||||
# Other files we do want to distribute with the package.
|
||||
#
|
||||
EXTRA_DIST = \
|
||||
$(NULL)
|
||||
|
||||
if OPENTHREAD_BUILD_TESTS
|
||||
# C preprocessor option flags that will apply to all compiled objects in this
|
||||
# makefile.
|
||||
|
||||
AM_CPPFLAGS = \
|
||||
-I$(top_srcdir)/include \
|
||||
-I$(top_srcdir)/src \
|
||||
-I$(top_srcdir)/src/core \
|
||||
-I$(top_srcdir)/third_party/mbedtls/repo/include \
|
||||
$(NULL)
|
||||
|
||||
COMMON_LDADD = \
|
||||
$(top_srcdir)/src/core/libopenthread.a \
|
||||
$(top_srcdir)/examples/platform/posix/libopenthread-posix.a \
|
||||
$(top_srcdir)/third_party/mbedtls/libmbedcrypto.a \
|
||||
-lpthread \
|
||||
$(NULL)
|
||||
|
||||
# Test applications that should be run when the 'check' target is run.
|
||||
|
||||
check_PROGRAMS = \
|
||||
test-aes \
|
||||
test-hmac-sha256 \
|
||||
test-mac-frame \
|
||||
test-message \
|
||||
$(NULL)
|
||||
|
||||
# Test applications and scripts that should be built and run when the
|
||||
# 'check' target is run.
|
||||
|
||||
TESTS = \
|
||||
$(check_PROGRAMS) \
|
||||
$(NULL)
|
||||
|
||||
# The additional environment variables and their values that will be
|
||||
# made available to all programs and scripts in TESTS.
|
||||
|
||||
TESTS_ENVIRONMENT = \
|
||||
$(NULL)
|
||||
|
||||
# Source, compiler, and linker options for test programs.
|
||||
|
||||
test_aes_LDADD = $(COMMON_LDADD)
|
||||
test_aes_SOURCES = test_aes.cpp
|
||||
|
||||
test_hmac_sha256_LDADD = $(COMMON_LDADD)
|
||||
test_hmac_sha256_SOURCES = test_hmac_sha256.cpp
|
||||
|
||||
test_mac_frame_LDADD = $(COMMON_LDADD)
|
||||
test_mac_frame_SOURCES = test_mac_frame.cpp
|
||||
|
||||
test_message_LDADD = $(COMMON_LDADD)
|
||||
test_message_SOURCES = test_message.cpp
|
||||
|
||||
endif # OPENTHREAD_BUILD_TESTS
|
||||
|
||||
include $(abs_top_nlbuild_autotools_dir)/automake/post.am
|
||||
@@ -0,0 +1,239 @@
|
||||
/*
|
||||
* 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 <openthread.h>
|
||||
#include <common/debug.hpp>
|
||||
#include <crypto/aes_ccm.hpp>
|
||||
#include <string.h>
|
||||
|
||||
extern"C" void otSignalTaskletPending(void)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies test vectors from IEEE 802.15.4-2006 Annex C Section C.2.1
|
||||
*/
|
||||
void TestMacBeaconFrame(void)
|
||||
{
|
||||
uint8_t key[] =
|
||||
{
|
||||
0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
|
||||
0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
|
||||
};
|
||||
|
||||
uint8_t test[] =
|
||||
{
|
||||
0x08, 0xD0, 0x84, 0x21, 0x43, 0x01, 0x00, 0x00,
|
||||
0x00, 0x00, 0x48, 0xDE, 0xAC, 0x02, 0x05, 0x00,
|
||||
0x00, 0x00, 0x55, 0xCF, 0x00, 0x00, 0x51, 0x52,
|
||||
0x53, 0x54, 0x22, 0x3B, 0xC1, 0xEC, 0x84, 0x1A,
|
||||
0xB5, 0x53
|
||||
};
|
||||
|
||||
uint8_t encrypted[] =
|
||||
{
|
||||
0x08, 0xD0, 0x84, 0x21, 0x43, 0x01, 0x00, 0x00,
|
||||
0x00, 0x00, 0x48, 0xDE, 0xAC, 0x02, 0x05, 0x00,
|
||||
0x00, 0x00, 0x55, 0xCF, 0x00, 0x00, 0x51, 0x52,
|
||||
0x53, 0x54, 0x22, 0x3B, 0xC1, 0xEC, 0x84, 0x1A,
|
||||
0xB5, 0x53
|
||||
};
|
||||
|
||||
uint8_t decrypted[] =
|
||||
{
|
||||
0x08, 0xD0, 0x84, 0x21, 0x43, 0x01, 0x00, 0x00,
|
||||
0x00, 0x00, 0x48, 0xDE, 0xAC, 0x02, 0x05, 0x00,
|
||||
0x00, 0x00, 0x55, 0xCF, 0x00, 0x00, 0x51, 0x52,
|
||||
0x53, 0x54, 0x22, 0x3B, 0xC1, 0xEC, 0x84, 0x1A,
|
||||
0xB5, 0x53
|
||||
};
|
||||
|
||||
Thread::Crypto::AesCcm aesCcm;
|
||||
uint32_t headerLength = sizeof(test) - 8;
|
||||
uint32_t payloadLength = 0;
|
||||
uint8_t tagLength = 8;
|
||||
|
||||
uint8_t nonce[] =
|
||||
{
|
||||
0xAC, 0xDE, 0x48, 0x00, 0x00, 0x00, 0x00, 0x01,
|
||||
0x00, 0x00, 0x00, 0x05, 0x02,
|
||||
};
|
||||
|
||||
aesCcm.SetKey(key, sizeof(key));
|
||||
aesCcm.Init(headerLength, payloadLength, tagLength, nonce, sizeof(nonce));
|
||||
aesCcm.Header(test, headerLength);
|
||||
aesCcm.Finalize(test + headerLength, &tagLength);
|
||||
|
||||
VerifyOrQuit(memcmp(test, encrypted, sizeof(encrypted)) == 0,
|
||||
"TestMacBeaconFrame encrypt failed");
|
||||
|
||||
aesCcm.Init(headerLength, payloadLength, tagLength, nonce, sizeof(nonce));
|
||||
aesCcm.Header(test, headerLength);
|
||||
aesCcm.Finalize(test + headerLength, &tagLength);
|
||||
|
||||
VerifyOrQuit(memcmp(test, decrypted, sizeof(decrypted)) == 0,
|
||||
"TestMacBeaconFrame decrypt failed");
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies test vectors from IEEE 802.15.4-2006 Annex C Section C.2.1
|
||||
*/
|
||||
void TestMacDataFrame()
|
||||
{
|
||||
uint8_t key[] =
|
||||
{
|
||||
0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
|
||||
0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
|
||||
};
|
||||
|
||||
uint8_t test[] =
|
||||
{
|
||||
0x69, 0xDC, 0x84, 0x21, 0x43, 0x02, 0x00, 0x00,
|
||||
0x00, 0x00, 0x48, 0xDE, 0xAC, 0x01, 0x00, 0x00,
|
||||
0x00, 0x00, 0x48, 0xDE, 0xAC, 0x04, 0x05, 0x00,
|
||||
0x00, 0x00, 0x61, 0x62, 0x63, 0x64
|
||||
};
|
||||
|
||||
uint8_t encrypted[] =
|
||||
{
|
||||
0x69, 0xDC, 0x84, 0x21, 0x43, 0x02, 0x00, 0x00,
|
||||
0x00, 0x00, 0x48, 0xDE, 0xAC, 0x01, 0x00, 0x00,
|
||||
0x00, 0x00, 0x48, 0xDE, 0xAC, 0x04, 0x05, 0x00,
|
||||
0x00, 0x00, 0xD4, 0x3E, 0x02, 0x2B
|
||||
};
|
||||
|
||||
uint8_t decrypted[] =
|
||||
{
|
||||
0x69, 0xDC, 0x84, 0x21, 0x43, 0x02, 0x00, 0x00,
|
||||
0x00, 0x00, 0x48, 0xDE, 0xAC, 0x01, 0x00, 0x00,
|
||||
0x00, 0x00, 0x48, 0xDE, 0xAC, 0x04, 0x05, 0x00,
|
||||
0x00, 0x00, 0x61, 0x62, 0x63, 0x64
|
||||
};
|
||||
|
||||
Thread::Crypto::AesCcm aesCcm;
|
||||
uint32_t headerLength = sizeof(test) - 4;
|
||||
uint32_t payloadLength = 4;
|
||||
uint8_t tagLength = 0;
|
||||
|
||||
uint8_t nonce[] =
|
||||
{
|
||||
0xAC, 0xDE, 0x48, 0x00, 0x00, 0x00, 0x00, 0x01,
|
||||
0x00, 0x00, 0x00, 0x05, 0x04,
|
||||
};
|
||||
|
||||
aesCcm.SetKey(key, sizeof(key));
|
||||
aesCcm.Init(headerLength, payloadLength, tagLength, nonce, sizeof(nonce));
|
||||
aesCcm.Header(test, headerLength);
|
||||
aesCcm.Payload(test + headerLength, test + headerLength, payloadLength, true);
|
||||
aesCcm.Finalize(test + headerLength + payloadLength, &tagLength);
|
||||
|
||||
VerifyOrQuit(memcmp(test, encrypted, sizeof(encrypted)) == 0,
|
||||
"TestMacDataFrame encrypt failed");
|
||||
|
||||
aesCcm.Init(headerLength, payloadLength, tagLength, nonce, sizeof(nonce));
|
||||
aesCcm.Header(test, headerLength);
|
||||
aesCcm.Payload(test + headerLength, test + headerLength, payloadLength, false);
|
||||
aesCcm.Finalize(test + headerLength + payloadLength, &tagLength);
|
||||
|
||||
VerifyOrQuit(memcmp(test, decrypted, sizeof(decrypted)) == 0,
|
||||
"TestMacDataFrame decrypt failed");
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies test vectors from IEEE 802.15.4-2006 Annex C Section C.2.3
|
||||
*/
|
||||
void TestMacCommandFrame()
|
||||
{
|
||||
uint8_t key[] =
|
||||
{
|
||||
0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
|
||||
0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
|
||||
};
|
||||
|
||||
uint8_t test[] =
|
||||
{
|
||||
0x2B, 0xDC, 0x84, 0x21, 0x43, 0x02, 0x00, 0x00,
|
||||
0x00, 0x00, 0x48, 0xDE, 0xAC, 0xFF, 0xFF, 0x01,
|
||||
0x00, 0x00, 0x00, 0x00, 0x48, 0xDE, 0xAC, 0x06,
|
||||
0x05, 0x00, 0x00, 0x00, 0x01, 0xCE, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
};
|
||||
|
||||
uint32_t headerLength = 29, payloadLength = 1;
|
||||
uint8_t tagLength = 8;
|
||||
|
||||
uint8_t encrypted[] =
|
||||
{
|
||||
0x2B, 0xDC, 0x84, 0x21, 0x43, 0x02, 0x00, 0x00,
|
||||
0x00, 0x00, 0x48, 0xDE, 0xAC, 0xFF, 0xFF, 0x01,
|
||||
0x00, 0x00, 0x00, 0x00, 0x48, 0xDE, 0xAC, 0x06,
|
||||
0x05, 0x00, 0x00, 0x00, 0x01, 0xD8, 0x4F, 0xDE,
|
||||
0x52, 0x90, 0x61, 0xF9, 0xC6, 0xF1,
|
||||
};
|
||||
|
||||
uint8_t decrypted[] =
|
||||
{
|
||||
0x2B, 0xDC, 0x84, 0x21, 0x43, 0x02, 0x00, 0x00,
|
||||
0x00, 0x00, 0x48, 0xDE, 0xAC, 0xFF, 0xFF, 0x01,
|
||||
0x00, 0x00, 0x00, 0x00, 0x48, 0xDE, 0xAC, 0x06,
|
||||
0x05, 0x00, 0x00, 0x00, 0x01, 0xCE, 0x4F, 0xDE,
|
||||
0x52, 0x90, 0x61, 0xF9, 0xC6, 0xF1,
|
||||
};
|
||||
|
||||
uint8_t nonce[] =
|
||||
{
|
||||
0xAC, 0xDE, 0x48, 0x00, 0x00, 0x00, 0x00, 0x01,
|
||||
0x00, 0x00, 0x00, 0x05, 0x06,
|
||||
};
|
||||
|
||||
Thread::Crypto::AesCcm aesCcm;
|
||||
aesCcm.SetKey(key, sizeof(key));
|
||||
aesCcm.Init(headerLength, payloadLength, tagLength, nonce, sizeof(nonce));
|
||||
aesCcm.Header(test, headerLength);
|
||||
aesCcm.Payload(test + headerLength, test + headerLength, payloadLength, true);
|
||||
aesCcm.Finalize(test + headerLength + payloadLength, &tagLength);
|
||||
VerifyOrQuit(memcmp(test, encrypted, sizeof(encrypted)) == 0,
|
||||
"TestMacCommandFrame encrypt failed\n");
|
||||
|
||||
aesCcm.Init(headerLength, payloadLength, tagLength, nonce, sizeof(nonce));
|
||||
aesCcm.Header(test, headerLength);
|
||||
aesCcm.Payload(test + headerLength, test + headerLength, payloadLength, false);
|
||||
aesCcm.Finalize(test + headerLength + payloadLength, &tagLength);
|
||||
|
||||
VerifyOrQuit(memcmp(test, decrypted, sizeof(decrypted)) == 0,
|
||||
"TestMacCommandFrame decrypt failed\n");
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
TestMacBeaconFrame();
|
||||
TestMacDataFrame();
|
||||
TestMacCommandFrame();
|
||||
printf("All tests passed\n");
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
/*
|
||||
* 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 <openthread.h>
|
||||
#include <common/debug.hpp>
|
||||
#include <string.h>
|
||||
|
||||
#include <crypto/hmac_sha256.h>
|
||||
|
||||
extern"C" void otSignalTaskletPending(void)
|
||||
{
|
||||
}
|
||||
|
||||
void TestHmacSha256(void)
|
||||
{
|
||||
static const struct
|
||||
{
|
||||
const char *key;
|
||||
const char *data;
|
||||
uint8_t hash[otCryptoSha256Size];
|
||||
} tests[] =
|
||||
{
|
||||
{
|
||||
"\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
|
||||
"Hi There",
|
||||
{
|
||||
0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53,
|
||||
0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
|
||||
0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7,
|
||||
0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7,
|
||||
},
|
||||
},
|
||||
{
|
||||
NULL,
|
||||
NULL,
|
||||
{},
|
||||
},
|
||||
};
|
||||
|
||||
uint8_t hash[otCryptoSha256Size];
|
||||
|
||||
for (int i = 0; tests[i].key != NULL; i++)
|
||||
{
|
||||
otCryptoHmacSha256Start(tests[i].key, strlen(tests[i].key));
|
||||
otCryptoHmacSha256Update(tests[i].data, strlen(tests[i].data));
|
||||
otCryptoHmacSha256Finish(hash);
|
||||
|
||||
VerifyOrQuit(memcmp(hash, tests[i].hash, sizeof(tests[i].hash)) == 0,
|
||||
"HMAC-SHA-256 failed\n");
|
||||
}
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
TestHmacSha256();
|
||||
printf("All tests passed\n");
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
/*
|
||||
* 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 <openthread.h>
|
||||
#include <common/debug.hpp>
|
||||
#include <mac/mac_frame.hpp>
|
||||
#include <string.h>
|
||||
|
||||
namespace Thread {
|
||||
|
||||
extern"C" void otSignalTaskletPending(void)
|
||||
{
|
||||
}
|
||||
|
||||
void TestMacHeader(void)
|
||||
{
|
||||
static const struct
|
||||
{
|
||||
uint16_t fcf;
|
||||
uint8_t secCtl;
|
||||
uint8_t headerLength;
|
||||
} tests[] =
|
||||
{
|
||||
{ Mac::Frame::kFcfDstAddrNone | Mac::Frame::kFcfSrcAddrNone, 0, 3 },
|
||||
{ Mac::Frame::kFcfDstAddrNone | Mac::Frame::kFcfSrcAddrShort, 0, 7 },
|
||||
{ Mac::Frame::kFcfDstAddrNone | Mac::Frame::kFcfSrcAddrExt, 0, 13 },
|
||||
{ Mac::Frame::kFcfDstAddrShort | Mac::Frame::kFcfSrcAddrNone, 0, 7 },
|
||||
{ Mac::Frame::kFcfDstAddrExt | Mac::Frame::kFcfSrcAddrNone, 0, 13 },
|
||||
{ Mac::Frame::kFcfDstAddrShort | Mac::Frame::kFcfSrcAddrShort, 0, 11 },
|
||||
{ Mac::Frame::kFcfDstAddrShort | Mac::Frame::kFcfSrcAddrExt, 0, 17 },
|
||||
{ Mac::Frame::kFcfDstAddrExt | Mac::Frame::kFcfSrcAddrShort, 0, 17 },
|
||||
{ Mac::Frame::kFcfDstAddrExt | Mac::Frame::kFcfSrcAddrExt, 0, 23 },
|
||||
|
||||
{ Mac::Frame::kFcfDstAddrShort | Mac::Frame::kFcfSrcAddrShort | Mac::Frame::kFcfPanidCompression, 0, 9 },
|
||||
{ Mac::Frame::kFcfDstAddrShort | Mac::Frame::kFcfSrcAddrExt | Mac::Frame::kFcfPanidCompression, 0, 15 },
|
||||
{ Mac::Frame::kFcfDstAddrExt | Mac::Frame::kFcfSrcAddrShort | Mac::Frame::kFcfPanidCompression, 0, 15 },
|
||||
{ Mac::Frame::kFcfDstAddrExt | Mac::Frame::kFcfSrcAddrExt | Mac::Frame::kFcfPanidCompression, 0, 21 },
|
||||
|
||||
{
|
||||
Mac::Frame::kFcfDstAddrShort | Mac::Frame::kFcfSrcAddrShort | Mac::Frame::kFcfPanidCompression |
|
||||
Mac::Frame::kFcfSecurityEnabled, Mac::Frame::kSecMic32 | Mac::Frame::kKeyIdMode1, 15
|
||||
},
|
||||
{
|
||||
Mac::Frame::kFcfDstAddrShort | Mac::Frame::kFcfSrcAddrShort | Mac::Frame::kFcfPanidCompression |
|
||||
Mac::Frame::kFcfSecurityEnabled, Mac::Frame::kSecMic32 | Mac::Frame::kKeyIdMode2, 19
|
||||
},
|
||||
};
|
||||
|
||||
for (unsigned i = 0; i < sizeof(tests) / sizeof(tests[0]); i++)
|
||||
{
|
||||
Mac::Frame frame;
|
||||
frame.InitMacHeader(tests[i].fcf, tests[i].secCtl);
|
||||
printf("%d\n", frame.GetHeaderLength());
|
||||
VerifyOrQuit(frame.GetHeaderLength() == tests[i].headerLength,
|
||||
"MacHeader test failed\n");
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace Thread
|
||||
|
||||
int main(void)
|
||||
{
|
||||
Thread::TestMacHeader();
|
||||
printf("All tests passed\n");
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
/*
|
||||
* 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 <openthread.h>
|
||||
#include <common/debug.hpp>
|
||||
#include <common/message.hpp>
|
||||
#include <string.h>
|
||||
|
||||
extern"C" void otSignalTaskletPending(void)
|
||||
{
|
||||
}
|
||||
|
||||
void TestMessage(void)
|
||||
{
|
||||
Thread::Message *message;
|
||||
uint8_t writeBuffer[1024];
|
||||
uint8_t readBuffer[1024];
|
||||
|
||||
Thread::Message::Init();
|
||||
|
||||
for (unsigned i = 0; i < sizeof(writeBuffer); i++)
|
||||
{
|
||||
writeBuffer[i] = random();
|
||||
}
|
||||
|
||||
VerifyOrQuit((message = Thread::Message::New(Thread::Message::kTypeIp6, 0)) != NULL,
|
||||
"Message::New failed\n");
|
||||
SuccessOrQuit(message->SetLength(sizeof(writeBuffer)),
|
||||
"Message::SetLength failed\n");
|
||||
VerifyOrQuit(message->Write(0, sizeof(writeBuffer), writeBuffer) == sizeof(writeBuffer),
|
||||
"Message::Write failed\n");
|
||||
VerifyOrQuit(message->Read(0, sizeof(readBuffer), readBuffer) == sizeof(readBuffer),
|
||||
"Message::Read failed\n");
|
||||
VerifyOrQuit(memcmp(writeBuffer, readBuffer, sizeof(writeBuffer)) == 0,
|
||||
"Message compare failed\n");
|
||||
VerifyOrQuit(message->GetLength() == 1024,
|
||||
"Message::GetLength failed\n");
|
||||
SuccessOrQuit(Thread::Message::Free(*message),
|
||||
"Message::Free failed\n");
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
TestMessage();
|
||||
printf("All tests passed\n");
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* 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_H
|
||||
#define TEST_UTIL_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <openthread-types.h>
|
||||
|
||||
#define SuccessOrQuit(ERR, MSG) \
|
||||
do { \
|
||||
if ((ERR) != kThreadError_None) \
|
||||
{ \
|
||||
fprintf(stderr, "%s FAILED: ", __FUNCTION__); \
|
||||
fputs(MSG, stderr); \
|
||||
exit(-1); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
#define VerifyOrQuit(TST, MSG) \
|
||||
do { \
|
||||
if (!(TST)) \
|
||||
{ \
|
||||
fprintf(stderr, "%s FAILED: ", __FUNCTION__); \
|
||||
fputs(MSG, stderr); \
|
||||
exit(-1); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user