Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers test_ip6.c Source File

test_ip6.c

00001 #include "test_ip6.h"
00002 
00003 #include "lwip/ethip6.h"
00004 #include "lwip/ip6.h"
00005 #include "lwip/inet_chksum.h"
00006 #include "lwip/nd6.h"
00007 #include "lwip/stats.h"
00008 #include "lwip/prot/ethernet.h"
00009 #include "lwip/prot/ip.h"
00010 #include "lwip/prot/ip6.h"
00011 
00012 #include "lwip/tcpip.h"
00013 
00014 #if LWIP_IPV6 /* allow to build the unit tests without IPv6 support */
00015 
00016 static struct netif test_netif6;
00017 static int linkoutput_ctr;
00018 
00019 static err_t
00020 default_netif_linkoutput(struct netif *netif, struct pbuf *p)
00021 {
00022   fail_unless(netif == &test_netif6);
00023   fail_unless(p != NULL);
00024   linkoutput_ctr++;
00025   return ERR_OK;
00026 }
00027 
00028 static err_t
00029 default_netif_init(struct netif *netif)
00030 {
00031   fail_unless(netif != NULL);
00032   netif->linkoutput = default_netif_linkoutput;
00033   netif->output_ip6 = ethip6_output;
00034   netif->mtu = 1500;
00035   netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHERNET | NETIF_FLAG_MLD6;
00036   netif->hwaddr_len = ETH_HWADDR_LEN;
00037   return ERR_OK;
00038 }
00039 
00040 static void
00041 default_netif_add(void)
00042 {
00043   struct netif *n;
00044   fail_unless(netif_default == NULL);
00045   n = netif_add_noaddr(&test_netif6, NULL, default_netif_init, NULL);
00046   fail_unless(n == &test_netif6);
00047   netif_set_default(&test_netif6);
00048 }
00049 
00050 static void
00051 default_netif_remove(void)
00052 {
00053   fail_unless(netif_default == &test_netif6);
00054   netif_remove(&test_netif6);
00055 }
00056 
00057 static void
00058 ip6_test_handle_timers(int count)
00059 {
00060   int i;
00061   for (i = 0; i < count; i++) {
00062     nd6_tmr();
00063   }
00064 }
00065 
00066 /* Setups/teardown functions */
00067 
00068 static void
00069 ip6_setup(void)
00070 {
00071   default_netif_add();
00072   lwip_check_ensure_no_alloc(SKIP_POOL(MEMP_SYS_TIMEOUT));
00073 }
00074 
00075 static void
00076 ip6_teardown(void)
00077 {
00078   if (netif_list->loop_first != NULL) {
00079     pbuf_free(netif_list->loop_first);
00080     netif_list->loop_first = NULL;
00081   }
00082   netif_list->loop_last = NULL;
00083   /* poll until all memory is released... */
00084   tcpip_thread_poll_one();
00085   default_netif_remove();
00086   lwip_check_ensure_no_alloc(SKIP_POOL(MEMP_SYS_TIMEOUT));
00087 }
00088 
00089 
00090 /* Test functions */
00091 
00092 static void
00093 test_ip6_ll_addr_iter(int expected_ctr1, int expected_ctr2)
00094 {
00095   fail_unless(linkoutput_ctr == 0);
00096 
00097   /* test that nothing is sent with link uo but netif down */
00098   netif_set_link_up(&test_netif6);
00099   ip6_test_handle_timers(500);
00100   fail_unless(linkoutput_ctr == 0);
00101   netif_set_link_down(&test_netif6);
00102   fail_unless(linkoutput_ctr == 0);
00103 
00104   /* test that nothing is sent with link down but netif up */
00105   netif_set_up(&test_netif6);
00106   ip6_test_handle_timers(500);
00107   fail_unless(linkoutput_ctr == 0);
00108   netif_set_down(&test_netif6);
00109   fail_unless(linkoutput_ctr == 0);
00110 
00111   /* test what is sent with link up + netif up */
00112   netif_set_link_up(&test_netif6);
00113   netif_set_up(&test_netif6);
00114   ip6_test_handle_timers(500);
00115   fail_unless(linkoutput_ctr == expected_ctr1);
00116   netif_set_down(&test_netif6);
00117   netif_set_link_down(&test_netif6);
00118   fail_unless(linkoutput_ctr == expected_ctr1);
00119   linkoutput_ctr = 0;
00120 
00121   netif_set_up(&test_netif6);
00122   netif_set_link_up(&test_netif6);
00123   ip6_test_handle_timers(500);
00124   fail_unless(linkoutput_ctr == expected_ctr2);
00125   netif_set_link_down(&test_netif6);
00126   netif_set_down(&test_netif6);
00127   fail_unless(linkoutput_ctr == expected_ctr2);
00128   linkoutput_ctr = 0;
00129 }
00130 
00131 START_TEST(test_ip6_ll_addr)
00132 {
00133   LWIP_UNUSED_ARG(_i);
00134 
00135   /* test without link-local address */
00136   test_ip6_ll_addr_iter(0, 0);
00137 
00138   /* test with link-local address */
00139   netif_create_ip6_linklocal_address(&test_netif6, 1);
00140   test_ip6_ll_addr_iter(3 + LWIP_IPV6_DUP_DETECT_ATTEMPTS + LWIP_IPV6_MLD, 3);
00141 }
00142 END_TEST
00143 
00144 START_TEST(test_ip6_aton_ipv4mapped)
00145 {
00146   int ret;
00147   ip_addr_t addr;
00148   ip6_addr_t addr6;
00149   const ip_addr_t addr_expected = IPADDR6_INIT_HOST(0, 0, 0xFFFF, 0xD4CC65D2);
00150   const char *full_ipv6_addr = "0:0:0:0:0:FFFF:D4CC:65D2";
00151   const char *shortened_ipv6_addr = "::FFFF:D4CC:65D2";
00152   const char *full_ipv4_mapped_addr = "0:0:0:0:0:FFFF:212.204.101.210";
00153   const char *shortened_ipv4_mapped_addr = "::FFFF:212.204.101.210";
00154   const char *bogus_ipv4_mapped_addr = "::FFFF:212.204.101.2101";
00155   LWIP_UNUSED_ARG(_i);
00156 
00157   /* check IPv6 representation */
00158   memset(&addr6, 0, sizeof(addr6));
00159   ret = ip6addr_aton(full_ipv6_addr, &addr6);
00160   fail_unless(ret == 1);
00161   fail_unless(memcmp(&addr6, &addr_expected, 16) == 0);
00162   memset(&addr, 0, sizeof(addr));
00163   ret = ipaddr_aton(full_ipv6_addr, &addr);
00164   fail_unless(ret == 1);
00165   fail_unless(memcmp(&addr, &addr_expected, 16) == 0);
00166 
00167   /* check shortened IPv6 representation */
00168   memset(&addr6, 0, sizeof(addr6));
00169   ret = ip6addr_aton(shortened_ipv6_addr, &addr6);
00170   fail_unless(ret == 1);
00171   fail_unless(memcmp(&addr6, &addr_expected, 16) == 0);
00172   memset(&addr, 0, sizeof(addr));
00173   ret = ipaddr_aton(shortened_ipv6_addr, &addr);
00174   fail_unless(ret == 1);
00175   fail_unless(memcmp(&addr, &addr_expected, 16) == 0);
00176 
00177   /* checked shortened mixed representation */
00178   memset(&addr6, 0, sizeof(addr6));
00179   ret = ip6addr_aton(shortened_ipv4_mapped_addr, &addr6);
00180   fail_unless(ret == 1);
00181   fail_unless(memcmp(&addr6, &addr_expected, 16) == 0);
00182   memset(&addr, 0, sizeof(addr));
00183   ret = ipaddr_aton(shortened_ipv4_mapped_addr, &addr);
00184   fail_unless(ret == 1);
00185   fail_unless(memcmp(&addr, &addr_expected, 16) == 0);
00186 
00187   /* checked mixed representation */
00188   memset(&addr6, 0, sizeof(addr6));
00189   ret = ip6addr_aton(full_ipv4_mapped_addr, &addr6);
00190   fail_unless(ret == 1);
00191   fail_unless(memcmp(&addr6, &addr_expected, 16) == 0);
00192   memset(&addr, 0, sizeof(addr));
00193   ret = ipaddr_aton(full_ipv4_mapped_addr, &addr);
00194   fail_unless(ret == 1);
00195   fail_unless(memcmp(&addr, &addr_expected, 16) == 0);
00196 
00197   /* checked bogus mixed representation */
00198   memset(&addr6, 0, sizeof(addr6));
00199   ret = ip6addr_aton(bogus_ipv4_mapped_addr, &addr6);
00200   fail_unless(ret == 0);
00201   memset(&addr, 0, sizeof(addr));
00202   ret = ipaddr_aton(bogus_ipv4_mapped_addr, &addr);
00203   fail_unless(ret == 0);
00204 }
00205 END_TEST
00206 
00207 START_TEST(test_ip6_ntoa_ipv4mapped)
00208 {
00209   const ip_addr_t addr = IPADDR6_INIT_HOST(0, 0, 0xFFFF, 0xD4CC65D2);
00210   char buf[128];
00211   char *str;
00212   LWIP_UNUSED_ARG(_i);
00213 
00214   str = ip6addr_ntoa_r(ip_2_ip6(&addr), buf, sizeof(buf));
00215   fail_unless(str == buf);
00216   fail_unless(!strcmp(str, "::FFFF:212.204.101.210"));
00217 }
00218 END_TEST
00219 
00220 struct test_addr_and_str {
00221   ip_addr_t addr;
00222   const char *str;
00223 };
00224 
00225 START_TEST(test_ip6_ntoa)
00226 {
00227   struct test_addr_and_str tests[] = {
00228     {IPADDR6_INIT_HOST(0xfe800000, 0x00000000, 0xb2a1a2ff, 0xfea3a4a5), "FE80::B2A1:A2FF:FEA3:A4A5"}, /* test shortened zeros */
00229     {IPADDR6_INIT_HOST(0xfe800000, 0xff000000, 0xb2a1a2ff, 0xfea3a4a5), "FE80:0:FF00:0:B2A1:A2FF:FEA3:A4A5"}, /* don't omit single zero blocks */
00230     {IPADDR6_INIT_HOST(0xfe800000, 0xff000000, 0xb2000000, 0x0000a4a5), "FE80:0:FF00:0:B200::A4A5"}, /* omit longest zero block */
00231   };
00232   char buf[128];
00233   char *str;
00234   size_t i;
00235   LWIP_UNUSED_ARG(_i);
00236 
00237   for (i = 0; i < LWIP_ARRAYSIZE(tests); i++) {
00238     str = ip6addr_ntoa_r(ip_2_ip6(&tests[i].addr), buf, sizeof(buf));
00239     fail_unless(str == buf);
00240     fail_unless(!strcmp(str, tests[i].str));
00241   }
00242 }
00243 END_TEST
00244 
00245 START_TEST(test_ip6_lladdr)
00246 {
00247   u8_t zeros[128];
00248   const u8_t test_mac_addr[6] = {0xb0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5};
00249   const u32_t expected_ip6_addr_1[4] = {PP_HTONL(0xfe800000), 0, PP_HTONL(0xb2a1a2ff), PP_HTONL(0xfea3a4a5)};
00250   const u32_t expected_ip6_addr_2[4] = {PP_HTONL(0xfe800000), 0, PP_HTONL(0x0000b0a1), PP_HTONL(0xa2a3a4a5)};
00251   LWIP_UNUSED_ARG(_i);
00252   memset(zeros, 0, sizeof(zeros));
00253 
00254   fail_unless(test_netif6.hwaddr_len == 6);
00255   fail_unless(!memcmp(test_netif6.hwaddr, zeros, 6));
00256 
00257   fail_unless(test_netif6.ip6_addr_state[0] == 0);
00258   fail_unless(!memcmp(netif_ip6_addr(&test_netif6, 0), zeros, sizeof(ip6_addr_t)));
00259 
00260   /* set specific mac addr */
00261   memcpy(test_netif6.hwaddr, test_mac_addr, 6);
00262 
00263   /* create link-local addr based on mac (EUI-48) */
00264   netif_create_ip6_linklocal_address(&test_netif6, 1);
00265   fail_unless(IP_IS_V6(&test_netif6.ip6_addr[0]));
00266   fail_unless(!memcmp(&netif_ip6_addr(&test_netif6, 0)->addr, expected_ip6_addr_1, 16));
00267 #if LWIP_IPV6_SCOPES
00268   fail_unless(netif_ip6_addr(&test_netif6, 0)->zone == (test_netif6.num + 1));
00269 #endif
00270   /* reset address */
00271   memset(&test_netif6.ip6_addr[0], 0, sizeof(ip6_addr_t));
00272   test_netif6.ip6_addr_state[0] = 0;
00273 
00274   /* create link-local addr based interface ID */
00275   netif_create_ip6_linklocal_address(&test_netif6, 0);
00276   fail_unless(IP_IS_V6(&test_netif6.ip6_addr[0]));
00277   fail_unless(!memcmp(&netif_ip6_addr(&test_netif6, 0)->addr, expected_ip6_addr_2, 16));
00278 #if LWIP_IPV6_SCOPES
00279   fail_unless(netif_ip6_addr(&test_netif6, 0)->zone == (test_netif6.num + 1));
00280 #endif
00281   /* reset address */
00282   memset(&test_netif6.ip6_addr[0], 0, sizeof(ip6_addr_t));
00283   test_netif6.ip6_addr_state[0] = 0;
00284 
00285   /* reset mac address */
00286   memset(&test_netif6.hwaddr, 0, sizeof(test_netif6.hwaddr));
00287 }
00288 END_TEST
00289 
00290 /** Create the suite including all tests for this module */
00291 Suite *
00292 ip6_suite(void)
00293 {
00294   testfunc tests[] = {
00295     TESTFUNC(test_ip6_ll_addr),
00296     TESTFUNC(test_ip6_aton_ipv4mapped),
00297     TESTFUNC(test_ip6_ntoa_ipv4mapped),
00298     TESTFUNC(test_ip6_ntoa),
00299     TESTFUNC(test_ip6_lladdr)
00300   };
00301   return create_suite("IPv6", tests, sizeof(tests)/sizeof(testfunc), ip6_setup, ip6_teardown);
00302 }
00303 
00304 #else /* LWIP_IPV6 */
00305 
00306 /* allow to build the unit tests without IPv6 support */
00307 START_TEST(test_ip6_dummy)
00308 {
00309   LWIP_UNUSED_ARG(_i);
00310 }
00311 END_TEST
00312 
00313 Suite *
00314 ip6_suite(void)
00315 {
00316   testfunc tests[] = {
00317     TESTFUNC(test_ip6_dummy),
00318   };
00319   return create_suite("IPv6", tests, sizeof(tests)/sizeof(testfunc), NULL, NULL);
00320 }
00321 #endif /* LWIP_IPV6 */