test_etharp.c 7.6 KB

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