test_etharp.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. #include "test_etharp.h"
  2. #include "lwip/udp.h"
  3. #include "netif/etharp.h"
  4. #include "lwip/stats.h"
  5. #if !LWIP_STATS || !UDP_STATS || !MEMP_STATS || !ETHARP_STATS
  6. #error "This tests needs UDP-, MEMP- and ETHARP-statistics enabled"
  7. #endif
  8. #if !ETHARP_SUPPORT_STATIC_ENTRIES
  9. #error "This test needs ETHARP_SUPPORT_STATIC_ENTRIES enabled"
  10. #endif
  11. static struct netif test_netif;
  12. static ip_addr_t test_ipaddr, test_netmask, test_gw;
  13. struct eth_addr test_ethaddr = {1,1,1,1,1,1};
  14. struct eth_addr test_ethaddr2 = {1,1,1,1,1,2};
  15. struct eth_addr test_ethaddr3 = {1,1,1,1,1,3};
  16. struct eth_addr test_ethaddr4 = {1,1,1,1,1,4};
  17. static int linkoutput_ctr;
  18. /* Helper functions */
  19. static void
  20. etharp_remove_all(void)
  21. {
  22. int i;
  23. /* call etharp_tmr often enough to have all entries cleaned */
  24. for(i = 0; i < 0xff; i++) {
  25. etharp_tmr();
  26. }
  27. }
  28. static err_t
  29. default_netif_linkoutput(struct netif *netif, struct pbuf *p)
  30. {
  31. fail_unless(netif == &test_netif);
  32. fail_unless(p != NULL);
  33. linkoutput_ctr++;
  34. return ERR_OK;
  35. }
  36. static err_t
  37. default_netif_init(struct netif *netif)
  38. {
  39. fail_unless(netif != NULL);
  40. netif->linkoutput = default_netif_linkoutput;
  41. netif->output = etharp_output;
  42. netif->mtu = 1500;
  43. netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP;
  44. netif->hwaddr_len = ETHARP_HWADDR_LEN;
  45. return ERR_OK;
  46. }
  47. static void
  48. default_netif_add(void)
  49. {
  50. IP4_ADDR(&test_gw, 192,168,0,1);
  51. IP4_ADDR(&test_ipaddr, 192,168,0,1);
  52. IP4_ADDR(&test_netmask, 255,255,0,0);
  53. fail_unless(netif_default == NULL);
  54. netif_set_default(netif_add(&test_netif, &test_ipaddr, &test_netmask,
  55. &test_gw, NULL, default_netif_init, NULL));
  56. netif_set_up(&test_netif);
  57. }
  58. static void
  59. default_netif_remove(void)
  60. {
  61. fail_unless(netif_default == &test_netif);
  62. netif_remove(&test_netif);
  63. }
  64. static void
  65. create_arp_response(ip_addr_t *adr)
  66. {
  67. int k;
  68. struct eth_hdr *ethhdr;
  69. struct etharp_hdr *etharphdr;
  70. struct pbuf *p = pbuf_alloc(PBUF_RAW, sizeof(struct eth_hdr) + sizeof(struct etharp_hdr), PBUF_RAM);
  71. if(p == NULL) {
  72. FAIL_RET();
  73. }
  74. ethhdr = (struct eth_hdr*)p->payload;
  75. etharphdr = (struct etharp_hdr*)(ethhdr + 1);
  76. ethhdr->dest = test_ethaddr;
  77. ethhdr->src = test_ethaddr2;
  78. ethhdr->type = htons(ETHTYPE_ARP);
  79. etharphdr->hwtype = htons(/*HWTYPE_ETHERNET*/ 1);
  80. etharphdr->proto = htons(ETHTYPE_IP);
  81. etharphdr->_hwlen_protolen = htons((ETHARP_HWADDR_LEN << 8) | sizeof(ip_addr_t));
  82. etharphdr->opcode = htons(ARP_REPLY);
  83. SMEMCPY(&etharphdr->sipaddr, adr, sizeof(ip_addr_t));
  84. SMEMCPY(&etharphdr->dipaddr, &test_ipaddr, sizeof(ip_addr_t));
  85. k = 6;
  86. while(k > 0) {
  87. k--;
  88. /* Write the ARP MAC-Addresses */
  89. etharphdr->shwaddr.addr[k] = test_ethaddr2.addr[k];
  90. etharphdr->dhwaddr.addr[k] = test_ethaddr.addr[k];
  91. /* Write the Ethernet MAC-Addresses */
  92. ethhdr->dest.addr[k] = test_ethaddr.addr[k];
  93. ethhdr->src.addr[k] = test_ethaddr2.addr[k];
  94. }
  95. ethernet_input(p, &test_netif);
  96. }
  97. /* Setups/teardown functions */
  98. static void
  99. etharp_setup(void)
  100. {
  101. etharp_remove_all();
  102. default_netif_add();
  103. }
  104. static void
  105. etharp_teardown(void)
  106. {
  107. etharp_remove_all();
  108. default_netif_remove();
  109. }
  110. /* Test functions */
  111. START_TEST(test_etharp_table)
  112. {
  113. #if ETHARP_SUPPORT_STATIC_ENTRIES
  114. err_t err;
  115. #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
  116. s8_t idx;
  117. ip_addr_t *unused_ipaddr;
  118. struct eth_addr *unused_ethaddr;
  119. struct udp_pcb* pcb;
  120. LWIP_UNUSED_ARG(_i);
  121. if (netif_default != &test_netif) {
  122. fail("This test needs a default netif");
  123. }
  124. linkoutput_ctr = 0;
  125. pcb = udp_new();
  126. fail_unless(pcb != NULL);
  127. if (pcb != NULL) {
  128. ip_addr_t adrs[ARP_TABLE_SIZE + 2];
  129. int i;
  130. for(i = 0; i < ARP_TABLE_SIZE + 2; i++) {
  131. IP4_ADDR(&adrs[i], 192,168,0,i+2);
  132. }
  133. /* fill ARP-table with dynamic entries */
  134. for(i = 0; i < ARP_TABLE_SIZE; i++) {
  135. struct pbuf *p = pbuf_alloc(PBUF_TRANSPORT, 10, PBUF_RAM);
  136. fail_unless(p != NULL);
  137. if (p != NULL) {
  138. err_t err = udp_sendto(pcb, p, &adrs[i], 123);
  139. fail_unless(err == ERR_OK);
  140. /* etharp request sent? */
  141. fail_unless(linkoutput_ctr == (2*i) + 1);
  142. pbuf_free(p);
  143. /* create an ARP response */
  144. create_arp_response(&adrs[i]);
  145. /* queued UDP packet sent? */
  146. fail_unless(linkoutput_ctr == (2*i) + 2);
  147. idx = etharp_find_addr(NULL, &adrs[i], &unused_ethaddr, &unused_ipaddr);
  148. fail_unless(idx == i);
  149. etharp_tmr();
  150. }
  151. }
  152. linkoutput_ctr = 0;
  153. #if ETHARP_SUPPORT_STATIC_ENTRIES
  154. /* create one static entry */
  155. err = etharp_add_static_entry(&adrs[ARP_TABLE_SIZE], &test_ethaddr3);
  156. fail_unless(err == ERR_OK);
  157. idx = etharp_find_addr(NULL, &adrs[ARP_TABLE_SIZE], &unused_ethaddr, &unused_ipaddr);
  158. fail_unless(idx == 0);
  159. fail_unless(linkoutput_ctr == 0);
  160. #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
  161. linkoutput_ctr = 0;
  162. /* fill ARP-table with dynamic entries */
  163. for(i = 0; i < ARP_TABLE_SIZE; i++) {
  164. struct pbuf *p = pbuf_alloc(PBUF_TRANSPORT, 10, PBUF_RAM);
  165. fail_unless(p != NULL);
  166. if (p != NULL) {
  167. err_t err = udp_sendto(pcb, p, &adrs[i], 123);
  168. fail_unless(err == ERR_OK);
  169. /* etharp request sent? */
  170. fail_unless(linkoutput_ctr == (2*i) + 1);
  171. pbuf_free(p);
  172. /* create an ARP response */
  173. create_arp_response(&adrs[i]);
  174. /* queued UDP packet sent? */
  175. fail_unless(linkoutput_ctr == (2*i) + 2);
  176. idx = etharp_find_addr(NULL, &adrs[i], &unused_ethaddr, &unused_ipaddr);
  177. if (i < ARP_TABLE_SIZE - 1) {
  178. fail_unless(idx == i+1);
  179. } else {
  180. /* the last entry must not overwrite the static entry! */
  181. fail_unless(idx == 1);
  182. }
  183. etharp_tmr();
  184. }
  185. }
  186. #if ETHARP_SUPPORT_STATIC_ENTRIES
  187. /* create a second static entry */
  188. err = etharp_add_static_entry(&adrs[ARP_TABLE_SIZE+1], &test_ethaddr4);
  189. fail_unless(err == ERR_OK);
  190. idx = etharp_find_addr(NULL, &adrs[ARP_TABLE_SIZE], &unused_ethaddr, &unused_ipaddr);
  191. fail_unless(idx == 0);
  192. idx = etharp_find_addr(NULL, &adrs[ARP_TABLE_SIZE+1], &unused_ethaddr, &unused_ipaddr);
  193. fail_unless(idx == 2);
  194. /* and remove it again */
  195. err = etharp_remove_static_entry(&adrs[ARP_TABLE_SIZE+1]);
  196. fail_unless(err == ERR_OK);
  197. idx = etharp_find_addr(NULL, &adrs[ARP_TABLE_SIZE], &unused_ethaddr, &unused_ipaddr);
  198. fail_unless(idx == 0);
  199. idx = etharp_find_addr(NULL, &adrs[ARP_TABLE_SIZE+1], &unused_ethaddr, &unused_ipaddr);
  200. fail_unless(idx == -1);
  201. #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
  202. /* check that static entries don't time out */
  203. etharp_remove_all();
  204. idx = etharp_find_addr(NULL, &adrs[ARP_TABLE_SIZE], &unused_ethaddr, &unused_ipaddr);
  205. fail_unless(idx == 0);
  206. #if ETHARP_SUPPORT_STATIC_ENTRIES
  207. /* remove the first static entry */
  208. err = etharp_remove_static_entry(&adrs[ARP_TABLE_SIZE]);
  209. fail_unless(err == ERR_OK);
  210. idx = etharp_find_addr(NULL, &adrs[ARP_TABLE_SIZE], &unused_ethaddr, &unused_ipaddr);
  211. fail_unless(idx == -1);
  212. idx = etharp_find_addr(NULL, &adrs[ARP_TABLE_SIZE+1], &unused_ethaddr, &unused_ipaddr);
  213. fail_unless(idx == -1);
  214. #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
  215. udp_remove(pcb);
  216. }
  217. }
  218. END_TEST
  219. /** Create the suite including all tests for this module */
  220. Suite *
  221. etharp_suite(void)
  222. {
  223. TFun tests[] = {
  224. test_etharp_table,
  225. };
  226. return create_suite("ETHARP", tests, sizeof(tests)/sizeof(TFun), etharp_setup, etharp_teardown);
  227. }