test_netif.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. #include "test_netif.h"
  2. #include "lwip/netif.h"
  3. #include "lwip/stats.h"
  4. #include "lwip/etharp.h"
  5. #include "netif/ethernet.h"
  6. #if !LWIP_NETIF_EXT_STATUS_CALLBACK
  7. #error "This tests needs LWIP_NETIF_EXT_STATUS_CALLBACK enabled"
  8. #endif
  9. static struct netif net_test;
  10. /* Setups/teardown functions */
  11. static void
  12. netif_setup(void)
  13. {
  14. lwip_check_ensure_no_alloc(SKIP_POOL(MEMP_SYS_TIMEOUT));
  15. }
  16. static void
  17. netif_teardown(void)
  18. {
  19. lwip_check_ensure_no_alloc(SKIP_POOL(MEMP_SYS_TIMEOUT));
  20. }
  21. /* test helper functions */
  22. static err_t
  23. testif_tx_func(struct netif *netif, struct pbuf *p)
  24. {
  25. LWIP_UNUSED_ARG(netif);
  26. LWIP_UNUSED_ARG(p);
  27. return ERR_OK;
  28. }
  29. static err_t
  30. testif_init(struct netif *netif)
  31. {
  32. netif->name[0] = 'c';
  33. netif->name[1] = 'h';
  34. netif->output = etharp_output;
  35. netif->linkoutput = testif_tx_func;
  36. netif->mtu = 1500;
  37. netif->hwaddr_len = 6;
  38. netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_ETHERNET | NETIF_FLAG_IGMP | NETIF_FLAG_MLD6;
  39. netif->hwaddr[0] = 0x02;
  40. netif->hwaddr[1] = 0x03;
  41. netif->hwaddr[2] = 0x04;
  42. netif->hwaddr[3] = 0x05;
  43. netif->hwaddr[4] = 0x06;
  44. netif->hwaddr[5] = 0x07;
  45. return ERR_OK;
  46. }
  47. #define MAX_NSC_REASON_IDX 10
  48. static netif_nsc_reason_t expected_reasons;
  49. static int callback_ctr;
  50. static int dummy_active;
  51. static void
  52. test_netif_ext_callback_dummy(struct netif* netif, netif_nsc_reason_t reason, const netif_ext_callback_args_t* args)
  53. {
  54. LWIP_UNUSED_ARG(netif);
  55. LWIP_UNUSED_ARG(reason);
  56. LWIP_UNUSED_ARG(args);
  57. fail_unless(dummy_active);
  58. }
  59. static void
  60. test_netif_ext_callback(struct netif* netif, netif_nsc_reason_t reason, const netif_ext_callback_args_t* args)
  61. {
  62. LWIP_UNUSED_ARG(args); /* @todo */
  63. callback_ctr++;
  64. fail_unless(netif == &net_test);
  65. fail_unless(expected_reasons == reason);
  66. }
  67. /* Test functions */
  68. NETIF_DECLARE_EXT_CALLBACK(netif_callback_1)
  69. NETIF_DECLARE_EXT_CALLBACK(netif_callback_2)
  70. NETIF_DECLARE_EXT_CALLBACK(netif_callback_3)
  71. START_TEST(test_netif_extcallbacks)
  72. {
  73. ip4_addr_t addr;
  74. ip4_addr_t netmask;
  75. ip4_addr_t gw;
  76. LWIP_UNUSED_ARG(_i);
  77. IP4_ADDR(&addr, 0, 0, 0, 0);
  78. IP4_ADDR(&netmask, 0, 0, 0, 0);
  79. IP4_ADDR(&gw, 0, 0, 0, 0);
  80. netif_add_ext_callback(&netif_callback_3, test_netif_ext_callback_dummy);
  81. netif_add_ext_callback(&netif_callback_2, test_netif_ext_callback);
  82. netif_add_ext_callback(&netif_callback_1, test_netif_ext_callback_dummy);
  83. dummy_active = 1;
  84. /* positive tests: check that single events come as expected */
  85. expected_reasons = LWIP_NSC_NETIF_ADDED;
  86. callback_ctr = 0;
  87. netif_add(&net_test, &addr, &netmask, &gw, &net_test, testif_init, ethernet_input);
  88. fail_unless(callback_ctr == 1);
  89. expected_reasons = LWIP_NSC_LINK_CHANGED;
  90. callback_ctr = 0;
  91. netif_set_link_up(&net_test);
  92. fail_unless(callback_ctr == 1);
  93. expected_reasons = LWIP_NSC_STATUS_CHANGED;
  94. callback_ctr = 0;
  95. netif_set_up(&net_test);
  96. fail_unless(callback_ctr == 1);
  97. IP4_ADDR(&addr, 1, 2, 3, 4);
  98. expected_reasons = LWIP_NSC_IPV4_ADDRESS_CHANGED;
  99. callback_ctr = 0;
  100. netif_set_ipaddr(&net_test, &addr);
  101. fail_unless(callback_ctr == 1);
  102. IP4_ADDR(&netmask, 255, 255, 255, 0);
  103. expected_reasons = LWIP_NSC_IPV4_NETMASK_CHANGED;
  104. callback_ctr = 0;
  105. netif_set_netmask(&net_test, &netmask);
  106. fail_unless(callback_ctr == 1);
  107. IP4_ADDR(&gw, 1, 2, 3, 254);
  108. expected_reasons = LWIP_NSC_IPV4_GATEWAY_CHANGED;
  109. callback_ctr = 0;
  110. netif_set_gw(&net_test, &gw);
  111. fail_unless(callback_ctr == 1);
  112. IP4_ADDR(&addr, 0, 0, 0, 0);
  113. expected_reasons = LWIP_NSC_IPV4_ADDRESS_CHANGED;
  114. callback_ctr = 0;
  115. netif_set_ipaddr(&net_test, &addr);
  116. fail_unless(callback_ctr == 1);
  117. IP4_ADDR(&netmask, 0, 0, 0, 0);
  118. expected_reasons = LWIP_NSC_IPV4_NETMASK_CHANGED;
  119. callback_ctr = 0;
  120. netif_set_netmask(&net_test, &netmask);
  121. fail_unless(callback_ctr == 1);
  122. IP4_ADDR(&gw, 0, 0, 0, 0);
  123. expected_reasons = LWIP_NSC_IPV4_GATEWAY_CHANGED;
  124. callback_ctr = 0;
  125. netif_set_gw(&net_test, &gw);
  126. fail_unless(callback_ctr == 1);
  127. /* check for multi-events (only one combined callback expected) */
  128. IP4_ADDR(&addr, 1, 2, 3, 4);
  129. IP4_ADDR(&netmask, 255, 255, 255, 0);
  130. IP4_ADDR(&gw, 1, 2, 3, 254);
  131. expected_reasons = (netif_nsc_reason_t)(LWIP_NSC_IPV4_ADDRESS_CHANGED | LWIP_NSC_IPV4_NETMASK_CHANGED |
  132. LWIP_NSC_IPV4_GATEWAY_CHANGED | LWIP_NSC_IPV4_SETTINGS_CHANGED |
  133. LWIP_NSC_IPV4_ADDR_VALID);
  134. callback_ctr = 0;
  135. netif_set_addr(&net_test, &addr, &netmask, &gw);
  136. fail_unless(callback_ctr == 1);
  137. /* check that for no-change, no callback is expected */
  138. expected_reasons = LWIP_NSC_NONE;
  139. callback_ctr = 0;
  140. netif_set_ipaddr(&net_test, &addr);
  141. fail_unless(callback_ctr == 0);
  142. netif_set_netmask(&net_test, &netmask);
  143. callback_ctr = 0;
  144. fail_unless(callback_ctr == 0);
  145. callback_ctr = 0;
  146. netif_set_gw(&net_test, &gw);
  147. fail_unless(callback_ctr == 0);
  148. /* netif_set_addr() always issues at least LWIP_NSC_IPV4_ADDR_VALID */
  149. expected_reasons = LWIP_NSC_IPV4_ADDR_VALID;
  150. callback_ctr = 0;
  151. netif_set_addr(&net_test, &addr, &netmask, &gw);
  152. fail_unless(callback_ctr == 1);
  153. /* check for single-events */
  154. IP4_ADDR(&addr, 1, 2, 3, 5);
  155. expected_reasons = (netif_nsc_reason_t)(LWIP_NSC_IPV4_ADDRESS_CHANGED | LWIP_NSC_IPV4_SETTINGS_CHANGED |
  156. LWIP_NSC_IPV4_ADDR_VALID);
  157. callback_ctr = 0;
  158. netif_set_addr(&net_test, &addr, &netmask, &gw);
  159. fail_unless(callback_ctr == 1);
  160. expected_reasons = LWIP_NSC_STATUS_CHANGED;
  161. callback_ctr = 0;
  162. netif_set_down(&net_test);
  163. fail_unless(callback_ctr == 1);
  164. expected_reasons = LWIP_NSC_NETIF_REMOVED;
  165. callback_ctr = 0;
  166. netif_remove(&net_test);
  167. fail_unless(callback_ctr == 1);
  168. expected_reasons = LWIP_NSC_NONE;
  169. netif_remove_ext_callback(&netif_callback_2);
  170. netif_remove_ext_callback(&netif_callback_3);
  171. netif_remove_ext_callback(&netif_callback_1);
  172. dummy_active = 0;
  173. }
  174. END_TEST
  175. START_TEST(test_netif_flag_set)
  176. {
  177. ip4_addr_t addr;
  178. ip4_addr_t netmask;
  179. ip4_addr_t gw;
  180. LWIP_UNUSED_ARG(_i);
  181. IP4_ADDR(&addr, 0, 0, 0, 0);
  182. IP4_ADDR(&netmask, 0, 0, 0, 0);
  183. IP4_ADDR(&gw, 0, 0, 0, 0);
  184. netif_add(&net_test, &addr, &netmask, &gw, &net_test, testif_init, ethernet_input);
  185. fail_if(netif_is_flag_set(&net_test, NETIF_FLAG_UP));
  186. fail_unless(netif_is_flag_set(&net_test, NETIF_FLAG_BROADCAST));
  187. fail_if(netif_is_flag_set(&net_test, NETIF_FLAG_LINK_UP));
  188. fail_unless(netif_is_flag_set(&net_test, NETIF_FLAG_ETHARP));
  189. fail_unless(netif_is_flag_set(&net_test, NETIF_FLAG_ETHERNET));
  190. fail_unless(netif_is_flag_set(&net_test, NETIF_FLAG_IGMP));
  191. fail_unless(netif_is_flag_set(&net_test, NETIF_FLAG_MLD6));
  192. netif_remove(&net_test);
  193. }
  194. END_TEST
  195. START_TEST(test_netif_find)
  196. {
  197. struct netif net0;
  198. struct netif net1;
  199. LWIP_UNUSED_ARG(_i);
  200. /* No netifs available */
  201. fail_unless(netif_find("ch0") == NULL);
  202. /* Add netifs with known names */
  203. fail_unless(netif_add_noaddr(&net0, NULL, testif_init, ethernet_input) == &net0);
  204. net0.num = 0;
  205. fail_unless(netif_add_noaddr(&net1, NULL, testif_init, ethernet_input) == &net1);
  206. net1.num = 1;
  207. fail_unless(netif_find("ch0") == &net0);
  208. fail_unless(netif_find("CH0") == NULL);
  209. fail_unless(netif_find("ch1") == &net1);
  210. fail_unless(netif_find("ch3") == NULL);
  211. /* atoi failure is not treated as zero */
  212. fail_unless(netif_find("chX") == NULL);
  213. fail_unless(netif_find("ab0") == NULL);
  214. netif_remove(&net0);
  215. netif_remove(&net1);
  216. }
  217. END_TEST
  218. /** Create the suite including all tests for this module */
  219. Suite *
  220. netif_suite(void)
  221. {
  222. testfunc tests[] = {
  223. TESTFUNC(test_netif_extcallbacks),
  224. TESTFUNC(test_netif_flag_set),
  225. TESTFUNC(test_netif_find)
  226. };
  227. return create_suite("NETIF", tests, sizeof(tests)/sizeof(testfunc), netif_setup, netif_teardown);
  228. }