source: azure_iot_hub_f767zi/trunk/asp_baseplatform/lwip/lwip-2.1.2/test/unit/ip6/test_ip6.c@ 457

Last change on this file since 457 was 457, checked in by coas-nagasima, 4 years ago

ファイルを追加

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc;charset=UTF-8
File size: 9.1 KB
Line 
1#include "test_ip6.h"
2
3#include "lwip/ethip6.h"
4#include "lwip/ip6.h"
5#include "lwip/inet_chksum.h"
6#include "lwip/nd6.h"
7#include "lwip/stats.h"
8#include "lwip/prot/ethernet.h"
9#include "lwip/prot/ip.h"
10#include "lwip/prot/ip6.h"
11
12#include "lwip/tcpip.h"
13
14#if LWIP_IPV6 /* allow to build the unit tests without IPv6 support */
15
16static struct netif test_netif6;
17static int linkoutput_ctr;
18
19static err_t
20default_netif_linkoutput(struct netif *netif, struct pbuf *p)
21{
22 fail_unless(netif == &test_netif6);
23 fail_unless(p != NULL);
24 linkoutput_ctr++;
25 return ERR_OK;
26}
27
28static err_t
29default_netif_init(struct netif *netif)
30{
31 fail_unless(netif != NULL);
32 netif->linkoutput = default_netif_linkoutput;
33 netif->output_ip6 = ethip6_output;
34 netif->mtu = 1500;
35 netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHERNET | NETIF_FLAG_MLD6;
36 netif->hwaddr_len = ETH_HWADDR_LEN;
37 return ERR_OK;
38}
39
40static void
41default_netif_add(void)
42{
43 struct netif *n;
44 fail_unless(netif_default == NULL);
45 n = netif_add_noaddr(&test_netif6, NULL, default_netif_init, NULL);
46 fail_unless(n == &test_netif6);
47 netif_set_default(&test_netif6);
48}
49
50static void
51default_netif_remove(void)
52{
53 fail_unless(netif_default == &test_netif6);
54 netif_remove(&test_netif6);
55}
56
57static void
58ip6_test_handle_timers(int count)
59{
60 int i;
61 for (i = 0; i < count; i++) {
62 nd6_tmr();
63 }
64}
65
66/* Setups/teardown functions */
67
68static void
69ip6_setup(void)
70{
71 default_netif_add();
72 lwip_check_ensure_no_alloc(SKIP_POOL(MEMP_SYS_TIMEOUT));
73}
74
75static void
76ip6_teardown(void)
77{
78 if (netif_list->loop_first != NULL) {
79 pbuf_free(netif_list->loop_first);
80 netif_list->loop_first = NULL;
81 }
82 netif_list->loop_last = NULL;
83 /* poll until all memory is released... */
84 tcpip_thread_poll_one();
85 default_netif_remove();
86 lwip_check_ensure_no_alloc(SKIP_POOL(MEMP_SYS_TIMEOUT));
87}
88
89
90/* Test functions */
91
92static void
93test_ip6_ll_addr_iter(int expected_ctr1, int expected_ctr2)
94{
95 fail_unless(linkoutput_ctr == 0);
96
97 /* test that nothing is sent with link uo but netif down */
98 netif_set_link_up(&test_netif6);
99 ip6_test_handle_timers(500);
100 fail_unless(linkoutput_ctr == 0);
101 netif_set_link_down(&test_netif6);
102 fail_unless(linkoutput_ctr == 0);
103
104 /* test that nothing is sent with link down but netif up */
105 netif_set_up(&test_netif6);
106 ip6_test_handle_timers(500);
107 fail_unless(linkoutput_ctr == 0);
108 netif_set_down(&test_netif6);
109 fail_unless(linkoutput_ctr == 0);
110
111 /* test what is sent with link up + netif up */
112 netif_set_link_up(&test_netif6);
113 netif_set_up(&test_netif6);
114 ip6_test_handle_timers(500);
115 fail_unless(linkoutput_ctr == expected_ctr1);
116 netif_set_down(&test_netif6);
117 netif_set_link_down(&test_netif6);
118 fail_unless(linkoutput_ctr == expected_ctr1);
119 linkoutput_ctr = 0;
120
121 netif_set_up(&test_netif6);
122 netif_set_link_up(&test_netif6);
123 ip6_test_handle_timers(500);
124 fail_unless(linkoutput_ctr == expected_ctr2);
125 netif_set_link_down(&test_netif6);
126 netif_set_down(&test_netif6);
127 fail_unless(linkoutput_ctr == expected_ctr2);
128 linkoutput_ctr = 0;
129}
130
131START_TEST(test_ip6_ll_addr)
132{
133 LWIP_UNUSED_ARG(_i);
134
135 /* test without link-local address */
136 test_ip6_ll_addr_iter(0, 0);
137
138 /* test with link-local address */
139 netif_create_ip6_linklocal_address(&test_netif6, 1);
140 test_ip6_ll_addr_iter(3 + LWIP_IPV6_DUP_DETECT_ATTEMPTS + LWIP_IPV6_MLD, 3);
141}
142END_TEST
143
144START_TEST(test_ip6_aton_ipv4mapped)
145{
146 int ret;
147 ip_addr_t addr;
148 ip6_addr_t addr6;
149 const ip_addr_t addr_expected = IPADDR6_INIT_HOST(0, 0, 0xFFFF, 0xD4CC65D2);
150 const char *full_ipv6_addr = "0:0:0:0:0:FFFF:D4CC:65D2";
151 const char *shortened_ipv6_addr = "::FFFF:D4CC:65D2";
152 const char *full_ipv4_mapped_addr = "0:0:0:0:0:FFFF:212.204.101.210";
153 const char *shortened_ipv4_mapped_addr = "::FFFF:212.204.101.210";
154 const char *bogus_ipv4_mapped_addr = "::FFFF:212.204.101.2101";
155 LWIP_UNUSED_ARG(_i);
156
157 /* check IPv6 representation */
158 memset(&addr6, 0, sizeof(addr6));
159 ret = ip6addr_aton(full_ipv6_addr, &addr6);
160 fail_unless(ret == 1);
161 fail_unless(memcmp(&addr6, &addr_expected, 16) == 0);
162 memset(&addr, 0, sizeof(addr));
163 ret = ipaddr_aton(full_ipv6_addr, &addr);
164 fail_unless(ret == 1);
165 fail_unless(memcmp(&addr, &addr_expected, 16) == 0);
166
167 /* check shortened IPv6 representation */
168 memset(&addr6, 0, sizeof(addr6));
169 ret = ip6addr_aton(shortened_ipv6_addr, &addr6);
170 fail_unless(ret == 1);
171 fail_unless(memcmp(&addr6, &addr_expected, 16) == 0);
172 memset(&addr, 0, sizeof(addr));
173 ret = ipaddr_aton(shortened_ipv6_addr, &addr);
174 fail_unless(ret == 1);
175 fail_unless(memcmp(&addr, &addr_expected, 16) == 0);
176
177 /* checked shortened mixed representation */
178 memset(&addr6, 0, sizeof(addr6));
179 ret = ip6addr_aton(shortened_ipv4_mapped_addr, &addr6);
180 fail_unless(ret == 1);
181 fail_unless(memcmp(&addr6, &addr_expected, 16) == 0);
182 memset(&addr, 0, sizeof(addr));
183 ret = ipaddr_aton(shortened_ipv4_mapped_addr, &addr);
184 fail_unless(ret == 1);
185 fail_unless(memcmp(&addr, &addr_expected, 16) == 0);
186
187 /* checked mixed representation */
188 memset(&addr6, 0, sizeof(addr6));
189 ret = ip6addr_aton(full_ipv4_mapped_addr, &addr6);
190 fail_unless(ret == 1);
191 fail_unless(memcmp(&addr6, &addr_expected, 16) == 0);
192 memset(&addr, 0, sizeof(addr));
193 ret = ipaddr_aton(full_ipv4_mapped_addr, &addr);
194 fail_unless(ret == 1);
195 fail_unless(memcmp(&addr, &addr_expected, 16) == 0);
196
197 /* checked bogus mixed representation */
198 memset(&addr6, 0, sizeof(addr6));
199 ret = ip6addr_aton(bogus_ipv4_mapped_addr, &addr6);
200 fail_unless(ret == 0);
201 memset(&addr, 0, sizeof(addr));
202 ret = ipaddr_aton(bogus_ipv4_mapped_addr, &addr);
203 fail_unless(ret == 0);
204}
205END_TEST
206
207START_TEST(test_ip6_ntoa_ipv4mapped)
208{
209 const ip_addr_t addr = IPADDR6_INIT_HOST(0, 0, 0xFFFF, 0xD4CC65D2);
210 char buf[128];
211 char *str;
212 LWIP_UNUSED_ARG(_i);
213
214 str = ip6addr_ntoa_r(ip_2_ip6(&addr), buf, sizeof(buf));
215 fail_unless(str == buf);
216 fail_unless(!strcmp(str, "::FFFF:212.204.101.210"));
217}
218END_TEST
219
220struct test_addr_and_str {
221 ip_addr_t addr;
222 const char *str;
223};
224
225START_TEST(test_ip6_ntoa)
226{
227 struct test_addr_and_str tests[] = {
228 {IPADDR6_INIT_HOST(0xfe800000, 0x00000000, 0xb2a1a2ff, 0xfea3a4a5), "FE80::B2A1:A2FF:FEA3:A4A5"}, /* test shortened zeros */
229 {IPADDR6_INIT_HOST(0xfe800000, 0xff000000, 0xb2a1a2ff, 0xfea3a4a5), "FE80:0:FF00:0:B2A1:A2FF:FEA3:A4A5"}, /* don't omit single zero blocks */
230 {IPADDR6_INIT_HOST(0xfe800000, 0xff000000, 0xb2000000, 0x0000a4a5), "FE80:0:FF00:0:B200::A4A5"}, /* omit longest zero block */
231 };
232 char buf[128];
233 char *str;
234 size_t i;
235 LWIP_UNUSED_ARG(_i);
236
237 for (i = 0; i < LWIP_ARRAYSIZE(tests); i++) {
238 str = ip6addr_ntoa_r(ip_2_ip6(&tests[i].addr), buf, sizeof(buf));
239 fail_unless(str == buf);
240 fail_unless(!strcmp(str, tests[i].str));
241 }
242}
243END_TEST
244
245START_TEST(test_ip6_lladdr)
246{
247 u8_t zeros[128];
248 const u8_t test_mac_addr[6] = {0xb0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5};
249 const u32_t expected_ip6_addr_1[4] = {PP_HTONL(0xfe800000), 0, PP_HTONL(0xb2a1a2ff), PP_HTONL(0xfea3a4a5)};
250 const u32_t expected_ip6_addr_2[4] = {PP_HTONL(0xfe800000), 0, PP_HTONL(0x0000b0a1), PP_HTONL(0xa2a3a4a5)};
251 LWIP_UNUSED_ARG(_i);
252 memset(zeros, 0, sizeof(zeros));
253
254 fail_unless(test_netif6.hwaddr_len == 6);
255 fail_unless(!memcmp(test_netif6.hwaddr, zeros, 6));
256
257 fail_unless(test_netif6.ip6_addr_state[0] == 0);
258 fail_unless(!memcmp(netif_ip6_addr(&test_netif6, 0), zeros, sizeof(ip6_addr_t)));
259
260 /* set specific mac addr */
261 memcpy(test_netif6.hwaddr, test_mac_addr, 6);
262
263 /* create link-local addr based on mac (EUI-48) */
264 netif_create_ip6_linklocal_address(&test_netif6, 1);
265 fail_unless(IP_IS_V6(&test_netif6.ip6_addr[0]));
266 fail_unless(!memcmp(&netif_ip6_addr(&test_netif6, 0)->addr, expected_ip6_addr_1, 16));
267#if LWIP_IPV6_SCOPES
268 fail_unless(netif_ip6_addr(&test_netif6, 0)->zone == (test_netif6.num + 1));
269#endif
270 /* reset address */
271 memset(&test_netif6.ip6_addr[0], 0, sizeof(ip6_addr_t));
272 test_netif6.ip6_addr_state[0] = 0;
273
274 /* create link-local addr based interface ID */
275 netif_create_ip6_linklocal_address(&test_netif6, 0);
276 fail_unless(IP_IS_V6(&test_netif6.ip6_addr[0]));
277 fail_unless(!memcmp(&netif_ip6_addr(&test_netif6, 0)->addr, expected_ip6_addr_2, 16));
278#if LWIP_IPV6_SCOPES
279 fail_unless(netif_ip6_addr(&test_netif6, 0)->zone == (test_netif6.num + 1));
280#endif
281 /* reset address */
282 memset(&test_netif6.ip6_addr[0], 0, sizeof(ip6_addr_t));
283 test_netif6.ip6_addr_state[0] = 0;
284
285 /* reset mac address */
286 memset(&test_netif6.hwaddr, 0, sizeof(test_netif6.hwaddr));
287}
288END_TEST
289
290/** Create the suite including all tests for this module */
291Suite *
292ip6_suite(void)
293{
294 testfunc tests[] = {
295 TESTFUNC(test_ip6_ll_addr),
296 TESTFUNC(test_ip6_aton_ipv4mapped),
297 TESTFUNC(test_ip6_ntoa_ipv4mapped),
298 TESTFUNC(test_ip6_ntoa),
299 TESTFUNC(test_ip6_lladdr)
300 };
301 return create_suite("IPv6", tests, sizeof(tests)/sizeof(testfunc), ip6_setup, ip6_teardown);
302}
303
304#else /* LWIP_IPV6 */
305
306/* allow to build the unit tests without IPv6 support */
307START_TEST(test_ip6_dummy)
308{
309 LWIP_UNUSED_ARG(_i);
310}
311END_TEST
312
313Suite *
314ip6_suite(void)
315{
316 testfunc tests[] = {
317 TESTFUNC(test_ip6_dummy),
318 };
319 return create_suite("IPv6", tests, sizeof(tests)/sizeof(testfunc), NULL, NULL);
320}
321#endif /* LWIP_IPV6 */
Note: See TracBrowser for help on using the repository browser.