test_ip6.c 9.1 KB

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