test_etharp.c 7.8 KB

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