diff --git a/sys/include/net/ng_sixlowpan/ctx.h b/sys/include/net/ng_sixlowpan/ctx.h index 40e1544ebf..275408e08b 100644 --- a/sys/include/net/ng_sixlowpan/ctx.h +++ b/sys/include/net/ng_sixlowpan/ctx.h @@ -37,6 +37,9 @@ extern "C" { #define NG_SIXLOWPAN_CTX_SIZE (16) /**< maximum number of entries in * context buffer */ +/** + * @brief Entry in the 6LoWPAN context buffer. + */ typedef struct { ng_ipv6_addr_t prefix; /**< The prefix associated to this context. */ uint8_t prefix_len; /**< Length of ng_sixlowpan_ctx_t::prefix in bit. */ @@ -95,6 +98,13 @@ ng_sixlowpan_ctx_t *ng_sixlowpan_ctx_update(uint8_t id, const ng_ipv6_addr_t *pr uint8_t prefix_len, uint16_t ltime); +#ifdef TEST_SUITES +/** + * @brief Resets the whole context buffer. + */ +void ng_sixlowpan_ctx_reset(void); +#endif + #ifdef __cplusplus } #endif diff --git a/sys/net/network_layer/ng_sixlowpan/ctx/ng_sixlowpan_ctx.c b/sys/net/network_layer/ng_sixlowpan/ctx/ng_sixlowpan_ctx.c index 3bd57f68ef..7d4752c41a 100644 --- a/sys/net/network_layer/ng_sixlowpan/ctx/ng_sixlowpan_ctx.c +++ b/sys/net/network_layer/ng_sixlowpan/ctx/ng_sixlowpan_ctx.c @@ -52,7 +52,7 @@ ng_sixlowpan_ctx_t *ng_sixlowpan_ctx_lookup_addr(const ng_ipv6_addr_t *addr) if ((_ctxs[id].prefix_len <= match) && (match > best)) { best = match; - res = _ctxs + id; + res = &(_ctxs[id]); } } } @@ -84,7 +84,7 @@ ng_sixlowpan_ctx_t *ng_sixlowpan_ctx_lookup_id(uint8_t id) ng_ipv6_addr_to_str(ipv6str, &_ctxs[id].prefix, sizeof(ipv6str)), _ctxs[id].prefix_len); mutex_unlock(&_ctx_mutex); - return _ctxs + id; + return &(_ctxs[id]); } mutex_unlock(&_ctx_mutex); @@ -140,7 +140,7 @@ ng_sixlowpan_ctx_t *ng_sixlowpan_ctx_update(uint8_t id, const ng_ipv6_addr_t *pr mutex_unlock(&_ctx_mutex); - return _ctxs + id; + return &(_ctxs[id]); } static uint32_t _current_minute(void) @@ -169,4 +169,13 @@ static void _update_lifetime(unsigned int id) } } +#ifdef TEST_SUITES +#include + +void ng_sixlowpan_ctx_reset(void) +{ + memset(_ctxs, 0, sizeof(_ctxs)); +} +#endif + /** @} */ diff --git a/tests/unittests/tests-sixlowpan_ctx/Makefile b/tests/unittests/tests-sixlowpan_ctx/Makefile new file mode 100644 index 0000000000..48422e909a --- /dev/null +++ b/tests/unittests/tests-sixlowpan_ctx/Makefile @@ -0,0 +1 @@ +include $(RIOTBASE)/Makefile.base diff --git a/tests/unittests/tests-sixlowpan_ctx/Makefile.include b/tests/unittests/tests-sixlowpan_ctx/Makefile.include new file mode 100644 index 0000000000..4aff25f608 --- /dev/null +++ b/tests/unittests/tests-sixlowpan_ctx/Makefile.include @@ -0,0 +1 @@ +USEMODULE += ng_sixlowpan_ctx diff --git a/tests/unittests/tests-sixlowpan_ctx/tests-sixlowpan_ctx.c b/tests/unittests/tests-sixlowpan_ctx/tests-sixlowpan_ctx.c new file mode 100644 index 0000000000..3f8a384761 --- /dev/null +++ b/tests/unittests/tests-sixlowpan_ctx/tests-sixlowpan_ctx.c @@ -0,0 +1,211 @@ +/* + * Copyright (C) 2014 Martine Lenders + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @{ + * + * @file + */ +#include +#include + +#include "embUnit.h" + +#include "net/ng_ipv6/addr.h" +#include "net/ng_sixlowpan/ctx.h" + +#include "unittests-constants.h" +#include "tests-sixlowpan_ctx.h" + +#define DEFAULT_TEST_ID (5) +#define DEFAULT_TEST_PREFIX { { \ + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, \ + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f \ + } \ + } +#define DEFAULT_TEST_PREFIX_LEN (63) + +#define OTHER_TEST_ID (12) +#define OTHER_TEST_PREFIX { { \ + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x06, \ + 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f \ + } \ + } +#define WRONG_TEST_PREFIX { { \ + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x05, \ + 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f \ + } \ + } + +static void tear_down(void) +{ + ng_sixlowpan_ctx_reset(); +} + +static void test_sixlowpan_ctx_update__success(void) +{ + ng_ipv6_addr_t addr = DEFAULT_TEST_PREFIX; + + TEST_ASSERT_NOT_NULL(ng_sixlowpan_ctx_update(DEFAULT_TEST_ID, &addr, + DEFAULT_TEST_PREFIX_LEN, + TEST_UINT16)); + TEST_ASSERT_NOT_NULL(ng_sixlowpan_ctx_lookup_id(DEFAULT_TEST_ID)); +} + +static void test_sixlowpan_ctx_update__wrong_id1(void) +{ + ng_ipv6_addr_t addr = DEFAULT_TEST_PREFIX; + + /* add context DEFAULT_TEST_PREFIX to DEFAULT_TEST_ID */ + test_sixlowpan_ctx_update__success(); + /* NG_SIXLOWPAN_CTX_SIZE out of bound so neither context update nor lookup + * should not be possible */ + TEST_ASSERT_NULL(ng_sixlowpan_ctx_update(NG_SIXLOWPAN_CTX_SIZE, &addr, + DEFAULT_TEST_PREFIX_LEN, + TEST_UINT16)); + TEST_ASSERT_NULL(ng_sixlowpan_ctx_lookup_id(NG_SIXLOWPAN_CTX_SIZE)); + + TEST_ASSERT_NOT_NULL(ng_sixlowpan_ctx_update(DEFAULT_TEST_ID, &addr, + DEFAULT_TEST_PREFIX_LEN, + TEST_UINT16)); + TEST_ASSERT_NOT_NULL(ng_sixlowpan_ctx_lookup_id(DEFAULT_TEST_ID)); +} + +static void test_sixlowpan_ctx_update__wrong_id2(void) +{ + ng_ipv6_addr_t addr = DEFAULT_TEST_PREFIX; + + /* add context DEFAULT_TEST_PREFIX to DEFAULT_TEST_ID */ + test_sixlowpan_ctx_update__success(); + /* UINT8_MAX out of bound so neither context update nor lookup should not + * be possible */ + TEST_ASSERT_NULL(ng_sixlowpan_ctx_update(UINT8_MAX, &addr, + DEFAULT_TEST_PREFIX_LEN, + TEST_UINT16)); + TEST_ASSERT_NULL(ng_sixlowpan_ctx_lookup_id(UINT8_MAX)); + + TEST_ASSERT_NOT_NULL(ng_sixlowpan_ctx_update(DEFAULT_TEST_ID, &addr, + DEFAULT_TEST_PREFIX_LEN, + TEST_UINT16)); + TEST_ASSERT_NOT_NULL(ng_sixlowpan_ctx_lookup_id(DEFAULT_TEST_ID)); +} + +static void test_sixlowpan_ctx_update__wrong_prefix_len(void) +{ + ng_ipv6_addr_t addr = DEFAULT_TEST_PREFIX; + + TEST_ASSERT_NULL(ng_sixlowpan_ctx_update(DEFAULT_TEST_ID, &addr, + 0, TEST_UINT16)); + TEST_ASSERT_NULL(ng_sixlowpan_ctx_lookup_id(DEFAULT_TEST_ID)); +} + +static void test_sixlowpan_ctx_update__ltime0(void) +{ + test_sixlowpan_ctx_update__success(); /* add context to DEFAULT_TEST_ID */ + TEST_ASSERT_NULL(ng_sixlowpan_ctx_update(DEFAULT_TEST_ID, NULL, 0, 0)); + TEST_ASSERT_NULL(ng_sixlowpan_ctx_lookup_id(DEFAULT_TEST_ID)); +} + +static void test_sixlowpan_ctx_lookup_addr__empty(void) +{ + ng_ipv6_addr_t addr = DEFAULT_TEST_PREFIX; + + TEST_ASSERT_NULL(ng_sixlowpan_ctx_lookup_addr(&addr)); +} + +static void test_sixlowpan_ctx_lookup_addr__same_addr(void) +{ + ng_ipv6_addr_t addr = DEFAULT_TEST_PREFIX; + ng_sixlowpan_ctx_t *ctx; + + /* add context DEFAULT_TEST_PREFIX to DEFAULT_TEST_ID */ + test_sixlowpan_ctx_update__success(); + TEST_ASSERT_NOT_NULL((ctx = ng_sixlowpan_ctx_lookup_addr(&addr))); + TEST_ASSERT_EQUAL_INT(DEFAULT_TEST_ID, ctx->id); + TEST_ASSERT_EQUAL_INT(DEFAULT_TEST_PREFIX_LEN, ctx->prefix_len); + TEST_ASSERT(TEST_UINT16 >= ctx->ltime); + TEST_ASSERT(DEFAULT_TEST_PREFIX_LEN >= ng_ipv6_addr_match_prefix(&addr, &ctx->prefix)); +} + +static void test_sixlowpan_ctx_lookup_addr__other_addr_same_prefix(void) +{ + ng_ipv6_addr_t addr1 = DEFAULT_TEST_PREFIX; + ng_ipv6_addr_t addr2 = OTHER_TEST_PREFIX; + ng_sixlowpan_ctx_t *ctx; + + /* add context DEFAULT_TEST_PREFIX to DEFAULT_TEST_ID */ + test_sixlowpan_ctx_update__success(); + TEST_ASSERT_NOT_NULL((ctx = ng_sixlowpan_ctx_lookup_addr(&addr2))); + TEST_ASSERT_EQUAL_INT(DEFAULT_TEST_ID, ctx->id); + TEST_ASSERT_EQUAL_INT(DEFAULT_TEST_PREFIX_LEN, ctx->prefix_len); + TEST_ASSERT(TEST_UINT16 >= ctx->ltime); + TEST_ASSERT(DEFAULT_TEST_PREFIX_LEN >= ng_ipv6_addr_match_prefix(&addr1, &ctx->prefix)); +} + +static void test_sixlowpan_ctx_lookup_addr__other_addr_other_prefix(void) +{ + ng_ipv6_addr_t addr = WRONG_TEST_PREFIX; + + /* add context DEFAULT_TEST_PREFIX to DEFAULT_TEST_ID */ + test_sixlowpan_ctx_update__success(); + TEST_ASSERT_NULL(ng_sixlowpan_ctx_lookup_addr(&addr)); +} + +static void test_sixlowpan_ctx_lookup_id__empty(void) +{ + TEST_ASSERT_NULL(ng_sixlowpan_ctx_lookup_id(DEFAULT_TEST_ID)); +} + +static void test_sixlowpan_ctx_lookup_id__wrong_id(void) +{ + /* add context DEFAULT_TEST_PREFIX to DEFAULT_TEST_ID */ + test_sixlowpan_ctx_update__success(); + TEST_ASSERT_NULL(ng_sixlowpan_ctx_lookup_id(OTHER_TEST_ID)); +} + +static void test_sixlowpan_ctx_lookup_id__success(void) +{ + ng_ipv6_addr_t addr = DEFAULT_TEST_PREFIX; + ng_sixlowpan_ctx_t *ctx; + + /* add context DEFAULT_TEST_PREFIX to DEFAULT_TEST_ID */ + test_sixlowpan_ctx_update__success(); + TEST_ASSERT_NOT_NULL((ctx = ng_sixlowpan_ctx_lookup_id(DEFAULT_TEST_ID))); + TEST_ASSERT_EQUAL_INT(DEFAULT_TEST_ID, ctx->id); + TEST_ASSERT_EQUAL_INT(DEFAULT_TEST_PREFIX_LEN, ctx->prefix_len); + TEST_ASSERT(TEST_UINT16 >= ctx->ltime); + TEST_ASSERT(DEFAULT_TEST_PREFIX_LEN >= ng_ipv6_addr_match_prefix(&addr, &ctx->prefix)); +} + +Test *tests_sixlowpan_ctx_tests(void) +{ + EMB_UNIT_TESTFIXTURES(fixtures) { + new_TestFixture(test_sixlowpan_ctx_update__wrong_id1), + new_TestFixture(test_sixlowpan_ctx_update__wrong_id2), + new_TestFixture(test_sixlowpan_ctx_update__wrong_prefix_len), + new_TestFixture(test_sixlowpan_ctx_update__success), + new_TestFixture(test_sixlowpan_ctx_update__ltime0), + new_TestFixture(test_sixlowpan_ctx_lookup_addr__empty), + new_TestFixture(test_sixlowpan_ctx_lookup_addr__same_addr), + new_TestFixture(test_sixlowpan_ctx_lookup_addr__other_addr_same_prefix), + new_TestFixture(test_sixlowpan_ctx_lookup_addr__other_addr_other_prefix), + new_TestFixture(test_sixlowpan_ctx_lookup_id__empty), + new_TestFixture(test_sixlowpan_ctx_lookup_id__wrong_id), + new_TestFixture(test_sixlowpan_ctx_lookup_id__success), + }; + + EMB_UNIT_TESTCALLER(sixlowpan_ctx_tests, NULL, tear_down, fixtures); + + return (Test *)&sixlowpan_ctx_tests; +} + +void tests_sixlowpan_ctx(void) +{ + TESTS_RUN(tests_sixlowpan_ctx_tests()); +} +/** @} */ diff --git a/tests/unittests/tests-sixlowpan_ctx/tests-sixlowpan_ctx.h b/tests/unittests/tests-sixlowpan_ctx/tests-sixlowpan_ctx.h new file mode 100644 index 0000000000..ad96e58e49 --- /dev/null +++ b/tests/unittests/tests-sixlowpan_ctx/tests-sixlowpan_ctx.h @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2015 Martine Lenders + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @addtogroup unittests + * @{ + * + * @file tests-sixlowpan_ctx.h + * @brief Unittests for the ``sixlowpan_ctx`` module + * + * @author Martine Lenders + */ +#ifndef TESTS_SIXLOWPAN_CTX_H_ +#define TESTS_SIXLOWPAN_CTX_H_ + +#include "embUnit.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief The entry point of this test suite. + */ +void tests_sixlowpan_ctx(void); + +#ifdef __cplusplus +} +#endif + +#endif /* TESTS_SIXLOWPAN_CTX_H_ */ +/** @} */