tests-sixlowpan_ctx.c
8.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
* Copyright (C) 2014 Martine Lenders <mlenders@inf.fu-berlin.de>
*
* 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 <inttypes.h>
#include <stdbool.h>
#include <stdlib.h>
#include "embUnit.h"
#include "net/ipv6/addr.h"
#include "net/gnrc/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)
{
gnrc_sixlowpan_ctx_reset();
}
static void test_sixlowpan_ctx_update__success(void)
{
ipv6_addr_t addr = DEFAULT_TEST_PREFIX;
TEST_ASSERT_NOT_NULL(gnrc_sixlowpan_ctx_update(DEFAULT_TEST_ID, &addr,
DEFAULT_TEST_PREFIX_LEN,
TEST_UINT16, true));
TEST_ASSERT_NOT_NULL(gnrc_sixlowpan_ctx_lookup_id(DEFAULT_TEST_ID));
}
static void test_sixlowpan_ctx_update__ltime0(void)
{
ipv6_addr_t addr = DEFAULT_TEST_PREFIX;
TEST_ASSERT_NOT_NULL(gnrc_sixlowpan_ctx_update(DEFAULT_TEST_ID, &addr,
DEFAULT_TEST_PREFIX_LEN,
0, true));
TEST_ASSERT_NOT_NULL(gnrc_sixlowpan_ctx_lookup_id(DEFAULT_TEST_ID));
TEST_ASSERT_EQUAL_INT(DEFAULT_TEST_ID, gnrc_sixlowpan_ctx_lookup_id(DEFAULT_TEST_ID)->flags_id);
}
static void test_sixlowpan_ctx_update__wrong_id1(void)
{
ipv6_addr_t addr = DEFAULT_TEST_PREFIX;
/* add context DEFAULT_TEST_PREFIX to DEFAULT_TEST_ID */
test_sixlowpan_ctx_update__success();
/* GNRC_SIXLOWPAN_CTX_SIZE out of bound so neither context update nor lookup
* should not be possible */
TEST_ASSERT_NULL(gnrc_sixlowpan_ctx_update(GNRC_SIXLOWPAN_CTX_SIZE, &addr,
DEFAULT_TEST_PREFIX_LEN, TEST_UINT16, true));
TEST_ASSERT_NULL(gnrc_sixlowpan_ctx_lookup_id(GNRC_SIXLOWPAN_CTX_SIZE));
TEST_ASSERT_NOT_NULL(gnrc_sixlowpan_ctx_update(DEFAULT_TEST_ID, &addr, DEFAULT_TEST_PREFIX_LEN,
TEST_UINT16, true));
TEST_ASSERT_NOT_NULL(gnrc_sixlowpan_ctx_lookup_id(DEFAULT_TEST_ID));
}
static void test_sixlowpan_ctx_update__wrong_id2(void)
{
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(gnrc_sixlowpan_ctx_update(UINT8_MAX, &addr, DEFAULT_TEST_PREFIX_LEN,
TEST_UINT16, true));
TEST_ASSERT_NULL(gnrc_sixlowpan_ctx_lookup_id(UINT8_MAX));
TEST_ASSERT_NOT_NULL(gnrc_sixlowpan_ctx_update(DEFAULT_TEST_ID, &addr, DEFAULT_TEST_PREFIX_LEN,
TEST_UINT16, true));
TEST_ASSERT_NOT_NULL(gnrc_sixlowpan_ctx_lookup_id(DEFAULT_TEST_ID));
}
static void test_sixlowpan_ctx_update__wrong_prefix_len(void)
{
ipv6_addr_t addr = DEFAULT_TEST_PREFIX;
TEST_ASSERT_NULL(gnrc_sixlowpan_ctx_update(DEFAULT_TEST_ID, &addr, 0, TEST_UINT16, true));
TEST_ASSERT_NULL(gnrc_sixlowpan_ctx_lookup_id(DEFAULT_TEST_ID));
}
static void test_sixlowpan_ctx_lookup_addr__empty(void)
{
ipv6_addr_t addr = DEFAULT_TEST_PREFIX;
TEST_ASSERT_NULL(gnrc_sixlowpan_ctx_lookup_addr(&addr));
}
static void test_sixlowpan_ctx_lookup_addr__same_addr(void)
{
ipv6_addr_t addr = DEFAULT_TEST_PREFIX;
gnrc_sixlowpan_ctx_t *ctx;
/* add context DEFAULT_TEST_PREFIX to DEFAULT_TEST_ID */
test_sixlowpan_ctx_update__success();
TEST_ASSERT_NOT_NULL((ctx = gnrc_sixlowpan_ctx_lookup_addr(&addr)));
TEST_ASSERT_EQUAL_INT(GNRC_SIXLOWPAN_CTX_FLAGS_COMP | DEFAULT_TEST_ID, ctx->flags_id);
TEST_ASSERT_EQUAL_INT(DEFAULT_TEST_PREFIX_LEN, ctx->prefix_len);
TEST_ASSERT(TEST_UINT16 >= ctx->ltime);
TEST_ASSERT(DEFAULT_TEST_PREFIX_LEN >= ipv6_addr_match_prefix(&addr, &ctx->prefix));
}
static void test_sixlowpan_ctx_lookup_addr__other_addr_same_prefix(void)
{
ipv6_addr_t addr1 = DEFAULT_TEST_PREFIX;
ipv6_addr_t addr2 = OTHER_TEST_PREFIX;
gnrc_sixlowpan_ctx_t *ctx;
/* add context DEFAULT_TEST_PREFIX to DEFAULT_TEST_ID */
test_sixlowpan_ctx_update__success();
TEST_ASSERT_NOT_NULL((ctx = gnrc_sixlowpan_ctx_lookup_addr(&addr2)));
TEST_ASSERT_EQUAL_INT(GNRC_SIXLOWPAN_CTX_FLAGS_COMP | DEFAULT_TEST_ID, ctx->flags_id);
TEST_ASSERT_EQUAL_INT(DEFAULT_TEST_PREFIX_LEN, ctx->prefix_len);
TEST_ASSERT(TEST_UINT16 >= ctx->ltime);
TEST_ASSERT(DEFAULT_TEST_PREFIX_LEN >= ipv6_addr_match_prefix(&addr1, &ctx->prefix));
}
static void test_sixlowpan_ctx_lookup_addr__other_addr_other_prefix(void)
{
ipv6_addr_t addr = WRONG_TEST_PREFIX;
/* add context DEFAULT_TEST_PREFIX to DEFAULT_TEST_ID */
test_sixlowpan_ctx_update__success();
TEST_ASSERT_NULL(gnrc_sixlowpan_ctx_lookup_addr(&addr));
}
static void test_sixlowpan_ctx_lookup_id__empty(void)
{
TEST_ASSERT_NULL(gnrc_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(gnrc_sixlowpan_ctx_lookup_id(OTHER_TEST_ID));
}
static void test_sixlowpan_ctx_lookup_id__success(void)
{
ipv6_addr_t addr = DEFAULT_TEST_PREFIX;
gnrc_sixlowpan_ctx_t *ctx;
/* add context DEFAULT_TEST_PREFIX to DEFAULT_TEST_ID */
test_sixlowpan_ctx_update__success();
TEST_ASSERT_NOT_NULL((ctx = gnrc_sixlowpan_ctx_lookup_id(DEFAULT_TEST_ID)));
TEST_ASSERT_EQUAL_INT(GNRC_SIXLOWPAN_CTX_FLAGS_COMP | DEFAULT_TEST_ID, ctx->flags_id);
TEST_ASSERT_EQUAL_INT(DEFAULT_TEST_PREFIX_LEN, ctx->prefix_len);
TEST_ASSERT(TEST_UINT16 >= ctx->ltime);
TEST_ASSERT(DEFAULT_TEST_PREFIX_LEN >= ipv6_addr_match_prefix(&addr, &ctx->prefix));
}
static void test_sixlowpan_ctx_remove(void)
{
ipv6_addr_t addr = DEFAULT_TEST_PREFIX;
/* add context DEFAULT_TEST_PREFIX to DEFAULT_TEST_ID */
test_sixlowpan_ctx_update__success();
TEST_ASSERT_NOT_NULL(gnrc_sixlowpan_ctx_lookup_id(DEFAULT_TEST_ID));
TEST_ASSERT_NOT_NULL(gnrc_sixlowpan_ctx_lookup_addr(&addr));
gnrc_sixlowpan_ctx_remove(DEFAULT_TEST_ID);
TEST_ASSERT_NULL(gnrc_sixlowpan_ctx_lookup_id(DEFAULT_TEST_ID));
TEST_ASSERT_NULL(gnrc_sixlowpan_ctx_lookup_addr(&addr));
}
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),
new_TestFixture(test_sixlowpan_ctx_remove),
};
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());
}
/** @} */