test_ip6.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. #include "test_ip6.h"
  2. #include "lwip/ethip6.h"
  3. #include "lwip/ip6.h"
  4. #include "lwip/icmp6.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. #include "lwip/tcpip.h"
  12. #if LWIP_IPV6 /* allow to build the unit tests without IPv6 support */
  13. static struct netif test_netif6;
  14. static int linkoutput_ctr;
  15. static int linkoutput_byte_ctr;
  16. static err_t
  17. default_netif_linkoutput(struct netif *netif, struct pbuf *p)
  18. {
  19. fail_unless(netif == &test_netif6);
  20. fail_unless(p != NULL);
  21. linkoutput_ctr++;
  22. linkoutput_byte_ctr += p->tot_len;
  23. return ERR_OK;
  24. }
  25. static err_t
  26. default_netif_init(struct netif *netif)
  27. {
  28. fail_unless(netif != NULL);
  29. netif->linkoutput = default_netif_linkoutput;
  30. netif->output_ip6 = ethip6_output;
  31. netif->mtu = 1500;
  32. netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHERNET | NETIF_FLAG_MLD6;
  33. netif->hwaddr_len = ETH_HWADDR_LEN;
  34. return ERR_OK;
  35. }
  36. static void
  37. default_netif_add(void)
  38. {
  39. struct netif *n;
  40. fail_unless(netif_default == NULL);
  41. n = netif_add_noaddr(&test_netif6, NULL, default_netif_init, NULL);
  42. fail_unless(n == &test_netif6);
  43. netif_set_default(&test_netif6);
  44. }
  45. static void
  46. default_netif_remove(void)
  47. {
  48. fail_unless(netif_default == &test_netif6);
  49. netif_remove(&test_netif6);
  50. }
  51. static void
  52. ip6_test_handle_timers(int count)
  53. {
  54. int i;
  55. for (i = 0; i < count; i++) {
  56. nd6_tmr();
  57. }
  58. }
  59. /* Helper functions */
  60. static void
  61. create_ip6_input_fragment(u32_t ip_id, u16_t start, u16_t len, int last, u8_t next_hdr)
  62. {
  63. struct pbuf* p;
  64. struct netif* input_netif = netif_list; /* just use any netif */
  65. fail_unless((start & 7) == 0);
  66. fail_unless(((len & 7) == 0) || last);
  67. fail_unless(input_netif != NULL);
  68. p = pbuf_alloc(PBUF_RAW, len + sizeof(struct ip6_frag_hdr) +
  69. sizeof(struct ip6_hdr), PBUF_RAM);
  70. fail_unless(p != NULL);
  71. if (p != NULL) {
  72. err_t err;
  73. struct ip6_frag_hdr* fraghdr;
  74. struct ip6_hdr* ip6hdr = (struct ip6_hdr*)p->payload;
  75. IP6H_VTCFL_SET(ip6hdr, 6, 0, 0);
  76. IP6H_PLEN_SET(ip6hdr, len + sizeof(struct ip6_frag_hdr));
  77. IP6H_NEXTH_SET(ip6hdr, IP6_NEXTH_FRAGMENT);
  78. IP6H_HOPLIM_SET(ip6hdr, 64);
  79. ip6_addr_copy_to_packed(ip6hdr->src, *netif_ip6_addr(input_netif, 0));
  80. ip6hdr->src.addr[3]++;
  81. ip6_addr_copy_to_packed(ip6hdr->dest, *netif_ip6_addr(input_netif, 0));
  82. fraghdr = (struct ip6_frag_hdr*)(ip6hdr + 1);
  83. fraghdr->_nexth = next_hdr;
  84. fraghdr->reserved = 0;
  85. if (last) {
  86. fraghdr->_fragment_offset = htons(start & ~7);
  87. } else {
  88. fraghdr->_fragment_offset = htons((start & ~7) | 1);
  89. }
  90. fraghdr->_identification = htonl(ip_id);
  91. err = ip6_input(p, input_netif);
  92. if (err != ERR_OK) {
  93. pbuf_free(p);
  94. }
  95. fail_unless(err == ERR_OK);
  96. }
  97. }
  98. /* Setups/teardown functions */
  99. static void
  100. ip6_setup(void)
  101. {
  102. default_netif_add();
  103. lwip_check_ensure_no_alloc(SKIP_POOL(MEMP_SYS_TIMEOUT));
  104. }
  105. static void
  106. ip6_teardown(void)
  107. {
  108. if (netif_list->loop_first != NULL) {
  109. pbuf_free(netif_list->loop_first);
  110. netif_list->loop_first = NULL;
  111. }
  112. netif_list->loop_last = NULL;
  113. /* poll until all memory is released... */
  114. tcpip_thread_poll_one();
  115. default_netif_remove();
  116. lwip_check_ensure_no_alloc(SKIP_POOL(MEMP_SYS_TIMEOUT));
  117. }
  118. /* Test functions */
  119. static void
  120. test_ip6_ll_addr_iter(int expected_ctr1, int expected_ctr2)
  121. {
  122. fail_unless(linkoutput_ctr == 0);
  123. /* test that nothing is sent with link uo but netif down */
  124. netif_set_link_up(&test_netif6);
  125. ip6_test_handle_timers(500);
  126. fail_unless(linkoutput_ctr == 0);
  127. netif_set_link_down(&test_netif6);
  128. fail_unless(linkoutput_ctr == 0);
  129. /* test that nothing is sent with link down but netif up */
  130. netif_set_up(&test_netif6);
  131. ip6_test_handle_timers(500);
  132. fail_unless(linkoutput_ctr == 0);
  133. netif_set_down(&test_netif6);
  134. fail_unless(linkoutput_ctr == 0);
  135. /* test what is sent with link up + netif up */
  136. netif_set_link_up(&test_netif6);
  137. netif_set_up(&test_netif6);
  138. ip6_test_handle_timers(500);
  139. fail_unless(linkoutput_ctr == expected_ctr1);
  140. netif_set_down(&test_netif6);
  141. netif_set_link_down(&test_netif6);
  142. fail_unless(linkoutput_ctr == expected_ctr1);
  143. linkoutput_ctr = 0;
  144. netif_set_up(&test_netif6);
  145. netif_set_link_up(&test_netif6);
  146. ip6_test_handle_timers(500);
  147. fail_unless(linkoutput_ctr == expected_ctr2);
  148. netif_set_link_down(&test_netif6);
  149. netif_set_down(&test_netif6);
  150. fail_unless(linkoutput_ctr == expected_ctr2);
  151. linkoutput_ctr = 0;
  152. }
  153. START_TEST(test_ip6_ll_addr)
  154. {
  155. LWIP_UNUSED_ARG(_i);
  156. linkoutput_ctr = 0;
  157. /* test without link-local address */
  158. test_ip6_ll_addr_iter(0, 0);
  159. /* test with link-local address */
  160. netif_create_ip6_linklocal_address(&test_netif6, 1);
  161. test_ip6_ll_addr_iter(3 + LWIP_IPV6_DUP_DETECT_ATTEMPTS + LWIP_IPV6_MLD, 3);
  162. }
  163. END_TEST
  164. START_TEST(test_ip6_aton_ipv4mapped)
  165. {
  166. int ret;
  167. ip_addr_t addr;
  168. ip6_addr_t addr6;
  169. const ip_addr_t addr_expected = IPADDR6_INIT_HOST(0, 0, 0xFFFF, 0xD4CC65D2);
  170. const char *full_ipv6_addr = "0:0:0:0:0:FFFF:D4CC:65D2";
  171. const char *shortened_ipv6_addr = "::FFFF:D4CC:65D2";
  172. const char *full_ipv4_mapped_addr = "0:0:0:0:0:FFFF:212.204.101.210";
  173. const char *shortened_ipv4_mapped_addr = "::FFFF:212.204.101.210";
  174. const char *bogus_ipv4_mapped_addr = "::FFFF:212.204.101.2101";
  175. LWIP_UNUSED_ARG(_i);
  176. /* check IPv6 representation */
  177. memset(&addr6, 0, sizeof(addr6));
  178. ret = ip6addr_aton(full_ipv6_addr, &addr6);
  179. fail_unless(ret == 1);
  180. fail_unless(memcmp(&addr6, &addr_expected, 16) == 0);
  181. memset(&addr, 0, sizeof(addr));
  182. ret = ipaddr_aton(full_ipv6_addr, &addr);
  183. fail_unless(ret == 1);
  184. fail_unless(memcmp(&addr, &addr_expected, 16) == 0);
  185. /* check shortened IPv6 representation */
  186. memset(&addr6, 0, sizeof(addr6));
  187. ret = ip6addr_aton(shortened_ipv6_addr, &addr6);
  188. fail_unless(ret == 1);
  189. fail_unless(memcmp(&addr6, &addr_expected, 16) == 0);
  190. memset(&addr, 0, sizeof(addr));
  191. ret = ipaddr_aton(shortened_ipv6_addr, &addr);
  192. fail_unless(ret == 1);
  193. fail_unless(memcmp(&addr, &addr_expected, 16) == 0);
  194. /* checked shortened mixed representation */
  195. memset(&addr6, 0, sizeof(addr6));
  196. ret = ip6addr_aton(shortened_ipv4_mapped_addr, &addr6);
  197. fail_unless(ret == 1);
  198. fail_unless(memcmp(&addr6, &addr_expected, 16) == 0);
  199. memset(&addr, 0, sizeof(addr));
  200. ret = ipaddr_aton(shortened_ipv4_mapped_addr, &addr);
  201. fail_unless(ret == 1);
  202. fail_unless(memcmp(&addr, &addr_expected, 16) == 0);
  203. /* checked mixed representation */
  204. memset(&addr6, 0, sizeof(addr6));
  205. ret = ip6addr_aton(full_ipv4_mapped_addr, &addr6);
  206. fail_unless(ret == 1);
  207. fail_unless(memcmp(&addr6, &addr_expected, 16) == 0);
  208. memset(&addr, 0, sizeof(addr));
  209. ret = ipaddr_aton(full_ipv4_mapped_addr, &addr);
  210. fail_unless(ret == 1);
  211. fail_unless(memcmp(&addr, &addr_expected, 16) == 0);
  212. /* checked bogus mixed representation */
  213. memset(&addr6, 0, sizeof(addr6));
  214. ret = ip6addr_aton(bogus_ipv4_mapped_addr, &addr6);
  215. fail_unless(ret == 0);
  216. memset(&addr, 0, sizeof(addr));
  217. ret = ipaddr_aton(bogus_ipv4_mapped_addr, &addr);
  218. fail_unless(ret == 0);
  219. }
  220. END_TEST
  221. START_TEST(test_ip6_ntoa_ipv4mapped)
  222. {
  223. const ip_addr_t addr = IPADDR6_INIT_HOST(0, 0, 0xFFFF, 0xD4CC65D2);
  224. char buf[128];
  225. char *str;
  226. LWIP_UNUSED_ARG(_i);
  227. str = ip6addr_ntoa_r(ip_2_ip6(&addr), buf, sizeof(buf));
  228. fail_unless(str == buf);
  229. fail_unless(!strcmp(str, "::FFFF:212.204.101.210"));
  230. }
  231. END_TEST
  232. struct test_addr_and_str {
  233. ip_addr_t addr;
  234. const char *str;
  235. };
  236. START_TEST(test_ip6_ntoa)
  237. {
  238. struct test_addr_and_str tests[] = {
  239. {IPADDR6_INIT_HOST(0xfe800000, 0x00000000, 0xb2a1a2ff, 0xfea3a4a5), "FE80::B2A1:A2FF:FEA3:A4A5"}, /* test shortened zeros */
  240. {IPADDR6_INIT_HOST(0xfe800000, 0xff000000, 0xb2a1a2ff, 0xfea3a4a5), "FE80:0:FF00:0:B2A1:A2FF:FEA3:A4A5"}, /* don't omit single zero blocks */
  241. {IPADDR6_INIT_HOST(0xfe800000, 0xff000000, 0xb2000000, 0x0000a4a5), "FE80:0:FF00:0:B200::A4A5"}, /* omit longest zero block */
  242. };
  243. char buf[128];
  244. char *str;
  245. size_t i;
  246. LWIP_UNUSED_ARG(_i);
  247. for (i = 0; i < LWIP_ARRAYSIZE(tests); i++) {
  248. str = ip6addr_ntoa_r(ip_2_ip6(&tests[i].addr), buf, sizeof(buf));
  249. fail_unless(str == buf);
  250. fail_unless(!strcmp(str, tests[i].str));
  251. }
  252. }
  253. END_TEST
  254. START_TEST(test_ip6_lladdr)
  255. {
  256. u8_t zeros[128];
  257. const u8_t test_mac_addr[6] = {0xb0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5};
  258. const u32_t expected_ip6_addr_1[4] = {PP_HTONL(0xfe800000), 0, PP_HTONL(0xb2a1a2ff), PP_HTONL(0xfea3a4a5)};
  259. const u32_t expected_ip6_addr_2[4] = {PP_HTONL(0xfe800000), 0, PP_HTONL(0x0000b0a1), PP_HTONL(0xa2a3a4a5)};
  260. LWIP_UNUSED_ARG(_i);
  261. memset(zeros, 0, sizeof(zeros));
  262. fail_unless(test_netif6.hwaddr_len == 6);
  263. fail_unless(!memcmp(test_netif6.hwaddr, zeros, 6));
  264. fail_unless(test_netif6.ip6_addr_state[0] == 0);
  265. fail_unless(!memcmp(netif_ip6_addr(&test_netif6, 0), zeros, sizeof(ip6_addr_t)));
  266. /* set specific mac addr */
  267. memcpy(test_netif6.hwaddr, test_mac_addr, 6);
  268. /* create link-local addr based on mac (EUI-48) */
  269. netif_create_ip6_linklocal_address(&test_netif6, 1);
  270. fail_unless(IP_IS_V6(&test_netif6.ip6_addr[0]));
  271. fail_unless(!memcmp(&netif_ip6_addr(&test_netif6, 0)->addr, expected_ip6_addr_1, 16));
  272. #if LWIP_IPV6_SCOPES
  273. fail_unless(netif_ip6_addr(&test_netif6, 0)->zone == (test_netif6.num + 1));
  274. #endif
  275. /* reset address */
  276. memset(&test_netif6.ip6_addr[0], 0, sizeof(ip6_addr_t));
  277. test_netif6.ip6_addr_state[0] = 0;
  278. /* create link-local addr based interface ID */
  279. netif_create_ip6_linklocal_address(&test_netif6, 0);
  280. fail_unless(IP_IS_V6(&test_netif6.ip6_addr[0]));
  281. fail_unless(!memcmp(&netif_ip6_addr(&test_netif6, 0)->addr, expected_ip6_addr_2, 16));
  282. #if LWIP_IPV6_SCOPES
  283. fail_unless(netif_ip6_addr(&test_netif6, 0)->zone == (test_netif6.num + 1));
  284. #endif
  285. /* reset address */
  286. memset(&test_netif6.ip6_addr[0], 0, sizeof(ip6_addr_t));
  287. test_netif6.ip6_addr_state[0] = 0;
  288. /* reset mac address */
  289. memset(&test_netif6.hwaddr, 0, sizeof(test_netif6.hwaddr));
  290. }
  291. END_TEST
  292. static struct pbuf *cloned_pbuf = NULL;
  293. static err_t clone_output(struct netif *netif, struct pbuf *p, const ip6_addr_t *addr) {
  294. LWIP_UNUSED_ARG(netif);
  295. LWIP_UNUSED_ARG(addr);
  296. cloned_pbuf = pbuf_clone(PBUF_RAW, PBUF_RAM, p);
  297. return ERR_OK;
  298. }
  299. /* Reproduces bug #58553 */
  300. START_TEST(test_ip6_dest_unreachable_chained_pbuf)
  301. {
  302. ip_addr_t my_addr = IPADDR6_INIT_HOST(0x20010db8, 0x0, 0x0, 0x1);
  303. ip_addr_t peer_addr = IPADDR6_INIT_HOST(0x20010db8, 0x0, 0x0, 0x4);
  304. /* Create chained pbuf with UDP data that will get destination unreachable */
  305. u8_t udp_hdr[] = {
  306. 0x60, 0x00, 0x27, 0x03, 0x00, 0x2d, 0x11, 0x40,
  307. 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
  308. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
  309. 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
  310. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  311. 0x01, 0xff, 0x03, 0xff, 0x00, 0x2d, 0x00, 0x00,
  312. };
  313. struct pbuf *header = pbuf_alloc(PBUF_RAW, sizeof(udp_hdr), PBUF_ROM);
  314. u8_t udp_payload[] = "abcdefghijklmnopqrstuvwxyz0123456789";
  315. struct pbuf *data = pbuf_alloc(PBUF_RAW, sizeof(udp_payload), PBUF_ROM);
  316. u8_t *icmpptr;
  317. struct ip6_hdr *outhdr;
  318. struct icmp6_hdr *icmp6hdr;
  319. LWIP_UNUSED_ARG(_i);
  320. fail_unless(header);
  321. header->payload = udp_hdr;
  322. fail_unless(data);
  323. data->payload = udp_payload;
  324. pbuf_cat(header, data);
  325. data = NULL;
  326. /* Configure and enable local address */
  327. netif_set_up(&test_netif6);
  328. netif_ip6_addr_set(&test_netif6, 0, ip_2_ip6(&my_addr));
  329. netif_ip6_addr_set_state(&test_netif6, 0, IP6_ADDR_VALID);
  330. test_netif6.output_ip6 = clone_output;
  331. /* Process packet and send ICMPv6 reply for unreachable UDP port */
  332. ip6_input(header, &test_netif6);
  333. header = NULL;
  334. /* Verify ICMP reply packet contents */
  335. fail_unless(cloned_pbuf);
  336. fail_unless(cloned_pbuf->len == IP6_HLEN + ICMP6_HLEN + sizeof(udp_hdr) + sizeof(udp_payload));
  337. outhdr = (struct ip6_hdr*) cloned_pbuf->payload;
  338. fail_unless(ip6_addr_packed_eq(ip_2_ip6(&my_addr), &outhdr->src, IP6_NO_ZONE));
  339. fail_unless(ip6_addr_packed_eq(ip_2_ip6(&peer_addr), &outhdr->dest, IP6_NO_ZONE));
  340. icmpptr = &((u8_t*)cloned_pbuf->payload)[IP6_HLEN];
  341. icmp6hdr = (struct icmp6_hdr*) icmpptr;
  342. fail_unless(icmp6hdr->type == ICMP6_TYPE_DUR);
  343. fail_unless(icmp6hdr->code == ICMP6_DUR_PORT);
  344. fail_unless(icmp6hdr->data == lwip_htonl(0));
  345. icmpptr += ICMP6_HLEN;
  346. fail_unless(memcmp(icmpptr, udp_hdr, sizeof(udp_hdr)) == 0, "mismatch in copied ip6/udp header");
  347. icmpptr += sizeof(udp_hdr);
  348. fail_unless(memcmp(icmpptr, udp_payload, sizeof(udp_payload)) == 0, "mismatch in copied udp payload");
  349. pbuf_free(cloned_pbuf);
  350. }
  351. END_TEST
  352. /* Reproduces bug #57374 */
  353. START_TEST(test_ip6_frag_pbuf_len_assert)
  354. {
  355. ip_addr_t my_addr = IPADDR6_INIT_HOST(0x20010db8, 0x0, 0x0, 0x1);
  356. ip_addr_t peer_addr = IPADDR6_INIT_HOST(0x20010db8, 0x0, 0x0, 0x4);
  357. struct pbuf *payload, *hdr;
  358. err_t err;
  359. int i;
  360. LWIP_UNUSED_ARG(_i);
  361. /* Configure and enable local address */
  362. test_netif6.mtu = 1500;
  363. netif_set_up(&test_netif6);
  364. netif_ip6_addr_set(&test_netif6, 0, ip_2_ip6(&my_addr));
  365. netif_ip6_addr_set_state(&test_netif6, 0, IP6_ADDR_VALID);
  366. /* Create packet with lots of small pbufs around mtu limit */
  367. payload = pbuf_alloc(PBUF_RAW, 1400, PBUF_POOL);
  368. fail_unless(payload != NULL);
  369. for (i = 0; i < 16; i++) {
  370. struct pbuf *p = pbuf_alloc(PBUF_RAW, 32, PBUF_RAM);
  371. fail_unless(p != NULL);
  372. pbuf_cat(payload, p);
  373. }
  374. /* Prefix with header like UDP would */
  375. hdr = pbuf_alloc(PBUF_IP, 8, PBUF_RAM);
  376. fail_unless(hdr != NULL);
  377. pbuf_chain(hdr, payload);
  378. /* Send it and don't crash while fragmenting */
  379. err = ip6_output_if_src(hdr, ip_2_ip6(&my_addr), ip_2_ip6(&peer_addr), 15, 0, IP_PROTO_UDP, &test_netif6);
  380. fail_unless(err == ERR_OK);
  381. pbuf_free(hdr);
  382. pbuf_free(payload);
  383. }
  384. END_TEST
  385. static err_t direct_output(struct netif *netif, struct pbuf *p, const ip6_addr_t *addr) {
  386. LWIP_UNUSED_ARG(addr);
  387. return netif->linkoutput(netif, p);
  388. }
  389. START_TEST(test_ip6_frag)
  390. {
  391. ip_addr_t my_addr = IPADDR6_INIT_HOST(0x20010db8, 0x0, 0x0, 0x1);
  392. ip_addr_t peer_addr = IPADDR6_INIT_HOST(0x20010db8, 0x0, 0x0, 0x4);
  393. struct pbuf *data;
  394. err_t err;
  395. LWIP_UNUSED_ARG(_i);
  396. /* Configure and enable local address */
  397. test_netif6.mtu = 1500;
  398. netif_set_up(&test_netif6);
  399. netif_ip6_addr_set(&test_netif6, 0, ip_2_ip6(&my_addr));
  400. netif_ip6_addr_set_state(&test_netif6, 0, IP6_ADDR_VALID);
  401. test_netif6.output_ip6 = direct_output;
  402. /* Reset counters after multicast traffic */
  403. linkoutput_ctr = 0;
  404. linkoutput_byte_ctr = 0;
  405. /* Verify that 8000 byte payload is split into six packets */
  406. data = pbuf_alloc(PBUF_IP, 8000, PBUF_RAM);
  407. fail_unless(data != NULL);
  408. err = ip6_output_if_src(data, ip_2_ip6(&my_addr), ip_2_ip6(&peer_addr),
  409. 15, 0, IP_PROTO_UDP, &test_netif6);
  410. fail_unless(err == ERR_OK);
  411. fail_unless(linkoutput_ctr == 6);
  412. fail_unless(linkoutput_byte_ctr == (8000 + (6 * (IP6_HLEN + IP6_FRAG_HLEN))));
  413. pbuf_free(data);
  414. }
  415. END_TEST
  416. static void test_ip6_reass_helper(u32_t ip_id, const u16_t *segments, size_t num_segs, u16_t seglen)
  417. {
  418. ip_addr_t my_addr = IPADDR6_INIT_HOST(0x20010db8, 0x0, 0x0, 0x1);
  419. size_t i;
  420. memset(&lwip_stats.mib2, 0, sizeof(lwip_stats.mib2));
  421. memset(&lwip_stats.ip6_frag, 0, sizeof(lwip_stats.ip6_frag));
  422. netif_set_up(&test_netif6);
  423. netif_ip6_addr_set(&test_netif6, 0, ip_2_ip6(&my_addr));
  424. netif_ip6_addr_set_state(&test_netif6, 0, IP6_ADDR_VALID);
  425. for (i = 0; i < num_segs; i++) {
  426. u16_t seg = segments[i];
  427. int last = seg + 1U == num_segs;
  428. create_ip6_input_fragment(ip_id, seg * seglen, seglen, last, IP6_NEXTH_UDP);
  429. fail_unless(lwip_stats.ip6_frag.recv == i + 1);
  430. fail_unless(lwip_stats.ip6_frag.err == 0);
  431. fail_unless(lwip_stats.ip6_frag.memerr == 0);
  432. fail_unless(lwip_stats.ip6_frag.drop == 0);
  433. if (i + 1 == num_segs) {
  434. fail_unless(lwip_stats.mib2.ip6reasmoks == 1);
  435. }
  436. else {
  437. fail_unless(lwip_stats.mib2.ip6reasmoks == 0);
  438. }
  439. }
  440. }
  441. START_TEST(test_ip6_reass)
  442. {
  443. #define NUM_SEGS 9
  444. const u16_t t1[NUM_SEGS] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
  445. const u16_t t2[NUM_SEGS] = { 8, 0, 1, 2, 3, 4, 7, 6, 5 };
  446. const u16_t t3[NUM_SEGS] = { 1, 2, 3, 4, 5, 6, 7, 8, 0 };
  447. const u16_t t4[NUM_SEGS] = { 8, 2, 4, 6, 7, 5, 3, 1, 0 };
  448. LWIP_UNUSED_ARG(_i);
  449. test_ip6_reass_helper(128, t1, NUM_SEGS, 200);
  450. test_ip6_reass_helper(129, t2, NUM_SEGS, 208);
  451. test_ip6_reass_helper(130, t3, NUM_SEGS, 8);
  452. test_ip6_reass_helper(130, t4, NUM_SEGS, 1448);
  453. }
  454. /** Create the suite including all tests for this module */
  455. Suite *
  456. ip6_suite(void)
  457. {
  458. testfunc tests[] = {
  459. TESTFUNC(test_ip6_ll_addr),
  460. TESTFUNC(test_ip6_aton_ipv4mapped),
  461. TESTFUNC(test_ip6_ntoa_ipv4mapped),
  462. TESTFUNC(test_ip6_ntoa),
  463. TESTFUNC(test_ip6_lladdr),
  464. TESTFUNC(test_ip6_dest_unreachable_chained_pbuf),
  465. TESTFUNC(test_ip6_frag_pbuf_len_assert),
  466. TESTFUNC(test_ip6_frag),
  467. TESTFUNC(test_ip6_reass)
  468. };
  469. return create_suite("IPv6", tests, sizeof(tests)/sizeof(testfunc), ip6_setup, ip6_teardown);
  470. }
  471. #else /* LWIP_IPV6 */
  472. /* allow to build the unit tests without IPv6 support */
  473. START_TEST(test_ip6_dummy)
  474. {
  475. LWIP_UNUSED_ARG(_i);
  476. }
  477. END_TEST
  478. Suite *
  479. ip6_suite(void)
  480. {
  481. testfunc tests[] = {
  482. TESTFUNC(test_ip6_dummy),
  483. };
  484. return create_suite("IPv6", tests, sizeof(tests)/sizeof(testfunc), NULL, NULL);
  485. }
  486. #endif /* LWIP_IPV6 */