test_etharp.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 = ETHARP_HWADDR_LEN;
  82. etharphdr->protolen = sizeof(ip_addr_t);
  83. etharphdr->opcode = htons(ARP_REPLY);
  84. SMEMCPY(&etharphdr->sipaddr, adr, sizeof(ip_addr_t));
  85. SMEMCPY(&etharphdr->dipaddr, &test_ipaddr, sizeof(ip_addr_t));
  86. k = 6;
  87. while(k > 0) {
  88. k--;
  89. /* Write the ARP MAC-Addresses */
  90. etharphdr->shwaddr.addr[k] = test_ethaddr2.addr[k];
  91. etharphdr->dhwaddr.addr[k] = test_ethaddr.addr[k];
  92. /* Write the Ethernet MAC-Addresses */
  93. ethhdr->dest.addr[k] = test_ethaddr.addr[k];
  94. ethhdr->src.addr[k] = test_ethaddr2.addr[k];
  95. }
  96. ethernet_input(p, &test_netif);
  97. }
  98. /* Setups/teardown functions */
  99. static void
  100. etharp_setup(void)
  101. {
  102. etharp_remove_all();
  103. default_netif_add();
  104. }
  105. static void
  106. etharp_teardown(void)
  107. {
  108. etharp_remove_all();
  109. default_netif_remove();
  110. }
  111. /* Test functions */
  112. START_TEST(test_etharp_table)
  113. {
  114. #if ETHARP_SUPPORT_STATIC_ENTRIES
  115. err_t err;
  116. #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
  117. s8_t idx;
  118. ip_addr_t *unused_ipaddr;
  119. struct eth_addr *unused_ethaddr;
  120. struct udp_pcb* pcb;
  121. LWIP_UNUSED_ARG(_i);
  122. if (netif_default != &test_netif) {
  123. fail("This test needs a default netif");
  124. }
  125. linkoutput_ctr = 0;
  126. pcb = udp_new();
  127. fail_unless(pcb != NULL);
  128. if (pcb != NULL) {
  129. ip_addr_t adrs[ARP_TABLE_SIZE + 2];
  130. int i;
  131. for(i = 0; i < ARP_TABLE_SIZE + 2; i++) {
  132. IP4_ADDR(&adrs[i], 192,168,0,i+2);
  133. }
  134. /* fill ARP-table with dynamic entries */
  135. for(i = 0; i < ARP_TABLE_SIZE; i++) {
  136. struct pbuf *p = pbuf_alloc(PBUF_TRANSPORT, 10, PBUF_RAM);
  137. fail_unless(p != NULL);
  138. if (p != NULL) {
  139. err_t err = udp_sendto(pcb, p, &adrs[i], 123);
  140. fail_unless(err == ERR_OK);
  141. /* etharp request sent? */
  142. fail_unless(linkoutput_ctr == (2*i) + 1);
  143. pbuf_free(p);
  144. /* create an ARP response */
  145. create_arp_response(&adrs[i]);
  146. /* queued UDP packet sent? */
  147. fail_unless(linkoutput_ctr == (2*i) + 2);
  148. idx = etharp_find_addr(NULL, &adrs[i], &unused_ethaddr, &unused_ipaddr);
  149. fail_unless(idx == i);
  150. etharp_tmr();
  151. }
  152. }
  153. linkoutput_ctr = 0;
  154. #if ETHARP_SUPPORT_STATIC_ENTRIES
  155. /* create one static entry */
  156. err = etharp_add_static_entry(&adrs[ARP_TABLE_SIZE], &test_ethaddr3);
  157. fail_unless(err == ERR_OK);
  158. idx = etharp_find_addr(NULL, &adrs[ARP_TABLE_SIZE], &unused_ethaddr, &unused_ipaddr);
  159. fail_unless(idx == 0);
  160. fail_unless(linkoutput_ctr == 0);
  161. #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
  162. linkoutput_ctr = 0;
  163. /* fill ARP-table with dynamic entries */
  164. for(i = 0; i < ARP_TABLE_SIZE; i++) {
  165. struct pbuf *p = pbuf_alloc(PBUF_TRANSPORT, 10, PBUF_RAM);
  166. fail_unless(p != NULL);
  167. if (p != NULL) {
  168. err_t err = udp_sendto(pcb, p, &adrs[i], 123);
  169. fail_unless(err == ERR_OK);
  170. /* etharp request sent? */
  171. fail_unless(linkoutput_ctr == (2*i) + 1);
  172. pbuf_free(p);
  173. /* create an ARP response */
  174. create_arp_response(&adrs[i]);
  175. /* queued UDP packet sent? */
  176. fail_unless(linkoutput_ctr == (2*i) + 2);
  177. idx = etharp_find_addr(NULL, &adrs[i], &unused_ethaddr, &unused_ipaddr);
  178. if (i < ARP_TABLE_SIZE - 1) {
  179. fail_unless(idx == i+1);
  180. } else {
  181. /* the last entry must not overwrite the static entry! */
  182. fail_unless(idx == 1);
  183. }
  184. etharp_tmr();
  185. }
  186. }
  187. #if ETHARP_SUPPORT_STATIC_ENTRIES
  188. /* create a second static entry */
  189. err = etharp_add_static_entry(&adrs[ARP_TABLE_SIZE+1], &test_ethaddr4);
  190. fail_unless(err == ERR_OK);
  191. idx = etharp_find_addr(NULL, &adrs[ARP_TABLE_SIZE], &unused_ethaddr, &unused_ipaddr);
  192. fail_unless(idx == 0);
  193. idx = etharp_find_addr(NULL, &adrs[ARP_TABLE_SIZE+1], &unused_ethaddr, &unused_ipaddr);
  194. fail_unless(idx == 2);
  195. /* and remove it again */
  196. err = etharp_remove_static_entry(&adrs[ARP_TABLE_SIZE+1]);
  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 == -1);
  202. #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
  203. /* check that static entries don't time out */
  204. etharp_remove_all();
  205. idx = etharp_find_addr(NULL, &adrs[ARP_TABLE_SIZE], &unused_ethaddr, &unused_ipaddr);
  206. fail_unless(idx == 0);
  207. #if ETHARP_SUPPORT_STATIC_ENTRIES
  208. /* remove the first static entry */
  209. err = etharp_remove_static_entry(&adrs[ARP_TABLE_SIZE]);
  210. fail_unless(err == ERR_OK);
  211. idx = etharp_find_addr(NULL, &adrs[ARP_TABLE_SIZE], &unused_ethaddr, &unused_ipaddr);
  212. fail_unless(idx == -1);
  213. idx = etharp_find_addr(NULL, &adrs[ARP_TABLE_SIZE+1], &unused_ethaddr, &unused_ipaddr);
  214. fail_unless(idx == -1);
  215. #endif /* ETHARP_SUPPORT_STATIC_ENTRIES */
  216. udp_remove(pcb);
  217. }
  218. }
  219. END_TEST
  220. /** Create the suite including all tests for this module */
  221. Suite *
  222. etharp_suite(void)
  223. {
  224. TFun tests[] = {
  225. test_etharp_table
  226. };
  227. return create_suite("ETHARP", tests, sizeof(tests)/sizeof(TFun), etharp_setup, etharp_teardown);
  228. }