test_etharp.c 7.7 KB

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