test_ip4.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. #include "test_ip4.h"
  2. #include "lwip/ip4.h"
  3. #include "lwip/etharp.h"
  4. #include "lwip/inet_chksum.h"
  5. #include "lwip/stats.h"
  6. #include "lwip/prot/ip.h"
  7. #include "lwip/prot/ip4.h"
  8. #include "lwip/tcpip.h"
  9. #if !LWIP_IPV4 || !IP_REASSEMBLY || !MIB2_STATS || !IPFRAG_STATS
  10. #error "This tests needs LWIP_IPV4, IP_REASSEMBLY; MIB2- and IPFRAG-statistics enabled"
  11. #endif
  12. static struct netif test_netif;
  13. static ip4_addr_t test_ipaddr, test_netmask, test_gw;
  14. static int linkoutput_ctr;
  15. static int linkoutput_byte_ctr;
  16. /* reference internal lwip variable in netif.c */
  17. static err_t
  18. test_netif_linkoutput(struct netif *netif, struct pbuf *p)
  19. {
  20. fail_unless(netif == &test_netif);
  21. fail_unless(p != NULL);
  22. linkoutput_ctr++;
  23. linkoutput_byte_ctr += p->tot_len;
  24. return ERR_OK;
  25. }
  26. static err_t
  27. test_netif_init(struct netif *netif)
  28. {
  29. fail_unless(netif != NULL);
  30. netif->linkoutput = test_netif_linkoutput;
  31. netif->output = etharp_output;
  32. netif->mtu = 1500;
  33. netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP;
  34. netif->hwaddr_len = ETHARP_HWADDR_LEN;
  35. return ERR_OK;
  36. }
  37. static void
  38. test_netif_add(void)
  39. {
  40. IP4_ADDR(&test_gw, 192,168,0,1);
  41. IP4_ADDR(&test_ipaddr, 192,168,0,1);
  42. IP4_ADDR(&test_netmask, 255,255,0,0);
  43. fail_unless(netif_default == NULL);
  44. netif_add(&test_netif, &test_ipaddr, &test_netmask, &test_gw,
  45. NULL, test_netif_init, NULL);
  46. netif_set_default(&test_netif);
  47. netif_set_up(&test_netif);
  48. }
  49. static void
  50. test_netif_remove(void)
  51. {
  52. if (netif_default == &test_netif) {
  53. netif_remove(&test_netif);
  54. }
  55. }
  56. /* Helper functions */
  57. static void
  58. create_ip4_input_fragment(u16_t ip_id, u16_t start, u16_t len, int last)
  59. {
  60. struct pbuf *p;
  61. struct netif *input_netif = netif_list; /* just use any netif */
  62. fail_unless((start & 7) == 0);
  63. fail_unless(((len & 7) == 0) || last);
  64. fail_unless(input_netif != NULL);
  65. p = pbuf_alloc(PBUF_RAW, len + sizeof(struct ip_hdr), PBUF_RAM);
  66. fail_unless(p != NULL);
  67. if (p != NULL) {
  68. err_t err;
  69. struct ip_hdr *iphdr = (struct ip_hdr *)p->payload;
  70. IPH_VHL_SET(iphdr, 4, sizeof(struct ip_hdr) / 4);
  71. IPH_TOS_SET(iphdr, 0);
  72. IPH_LEN_SET(iphdr, lwip_htons(p->tot_len));
  73. IPH_ID_SET(iphdr, lwip_htons(ip_id));
  74. if (last) {
  75. IPH_OFFSET_SET(iphdr, lwip_htons(start / 8));
  76. } else {
  77. IPH_OFFSET_SET(iphdr, lwip_htons((start / 8) | IP_MF));
  78. }
  79. IPH_TTL_SET(iphdr, 5);
  80. IPH_PROTO_SET(iphdr, IP_PROTO_UDP);
  81. IPH_CHKSUM_SET(iphdr, 0);
  82. ip4_addr_copy(iphdr->src, *netif_ip4_addr(input_netif));
  83. iphdr->src.addr = lwip_htonl(lwip_htonl(iphdr->src.addr) + 1);
  84. ip4_addr_copy(iphdr->dest, *netif_ip4_addr(input_netif));
  85. IPH_CHKSUM_SET(iphdr, inet_chksum(iphdr, sizeof(struct ip_hdr)));
  86. err = ip4_input(p, input_netif);
  87. if (err != ERR_OK) {
  88. pbuf_free(p);
  89. }
  90. fail_unless(err == ERR_OK);
  91. }
  92. }
  93. static err_t arpless_output(struct netif *netif, struct pbuf *p,
  94. const ip4_addr_t *ipaddr) {
  95. LWIP_UNUSED_ARG(ipaddr);
  96. return netif->linkoutput(netif, p);
  97. }
  98. /* Setups/teardown functions */
  99. static void
  100. ip4_setup(void)
  101. {
  102. lwip_check_ensure_no_alloc(SKIP_POOL(MEMP_SYS_TIMEOUT));
  103. }
  104. static void
  105. ip4_teardown(void)
  106. {
  107. if (netif_list->loop_first != NULL) {
  108. pbuf_free(netif_list->loop_first);
  109. netif_list->loop_first = NULL;
  110. }
  111. netif_list->loop_last = NULL;
  112. /* poll until all memory is released... */
  113. tcpip_thread_poll_one();
  114. lwip_check_ensure_no_alloc(SKIP_POOL(MEMP_SYS_TIMEOUT));
  115. test_netif_remove();
  116. netif_set_up(netif_get_loopif());
  117. }
  118. /* Test functions */
  119. START_TEST(test_ip4_frag)
  120. {
  121. struct pbuf *data = pbuf_alloc(PBUF_IP, 8000, PBUF_RAM);
  122. ip_addr_t peer_ip = IPADDR4_INIT_BYTES(192,168,0,5);
  123. err_t err;
  124. LWIP_UNUSED_ARG(_i);
  125. /* Verify that 8000 byte payload is split into six packets */
  126. fail_unless(data != NULL);
  127. test_netif_add();
  128. test_netif.output = arpless_output;
  129. err = ip4_output_if_src(data, &test_ipaddr, ip_2_ip4(&peer_ip),
  130. 16, 0, IP_PROTO_UDP, &test_netif);
  131. fail_unless(err == ERR_OK);
  132. fail_unless(linkoutput_ctr == 6);
  133. fail_unless(linkoutput_byte_ctr == (8000 + (6 * IP_HLEN)));
  134. pbuf_free(data);
  135. test_netif_remove();
  136. }
  137. END_TEST
  138. START_TEST(test_ip4_reass)
  139. {
  140. const u16_t ip_id = 128;
  141. LWIP_UNUSED_ARG(_i);
  142. memset(&lwip_stats.mib2, 0, sizeof(lwip_stats.mib2));
  143. create_ip4_input_fragment(ip_id, 8*200, 200, 1);
  144. fail_unless(lwip_stats.ip_frag.recv == 1);
  145. fail_unless(lwip_stats.ip_frag.err == 0);
  146. fail_unless(lwip_stats.ip_frag.memerr == 0);
  147. fail_unless(lwip_stats.ip_frag.drop == 0);
  148. fail_unless(lwip_stats.mib2.ipreasmoks == 0);
  149. create_ip4_input_fragment(ip_id, 0*200, 200, 0);
  150. fail_unless(lwip_stats.ip_frag.recv == 2);
  151. fail_unless(lwip_stats.ip_frag.err == 0);
  152. fail_unless(lwip_stats.ip_frag.memerr == 0);
  153. fail_unless(lwip_stats.ip_frag.drop == 0);
  154. fail_unless(lwip_stats.mib2.ipreasmoks == 0);
  155. create_ip4_input_fragment(ip_id, 1*200, 200, 0);
  156. fail_unless(lwip_stats.ip_frag.recv == 3);
  157. fail_unless(lwip_stats.ip_frag.err == 0);
  158. fail_unless(lwip_stats.ip_frag.memerr == 0);
  159. fail_unless(lwip_stats.ip_frag.drop == 0);
  160. fail_unless(lwip_stats.mib2.ipreasmoks == 0);
  161. create_ip4_input_fragment(ip_id, 2*200, 200, 0);
  162. fail_unless(lwip_stats.ip_frag.recv == 4);
  163. fail_unless(lwip_stats.ip_frag.err == 0);
  164. fail_unless(lwip_stats.ip_frag.memerr == 0);
  165. fail_unless(lwip_stats.ip_frag.drop == 0);
  166. fail_unless(lwip_stats.mib2.ipreasmoks == 0);
  167. create_ip4_input_fragment(ip_id, 3*200, 200, 0);
  168. fail_unless(lwip_stats.ip_frag.recv == 5);
  169. fail_unless(lwip_stats.ip_frag.err == 0);
  170. fail_unless(lwip_stats.ip_frag.memerr == 0);
  171. fail_unless(lwip_stats.ip_frag.drop == 0);
  172. fail_unless(lwip_stats.mib2.ipreasmoks == 0);
  173. create_ip4_input_fragment(ip_id, 4*200, 200, 0);
  174. fail_unless(lwip_stats.ip_frag.recv == 6);
  175. fail_unless(lwip_stats.ip_frag.err == 0);
  176. fail_unless(lwip_stats.ip_frag.memerr == 0);
  177. fail_unless(lwip_stats.ip_frag.drop == 0);
  178. fail_unless(lwip_stats.mib2.ipreasmoks == 0);
  179. create_ip4_input_fragment(ip_id, 7*200, 200, 0);
  180. fail_unless(lwip_stats.ip_frag.recv == 7);
  181. fail_unless(lwip_stats.ip_frag.err == 0);
  182. fail_unless(lwip_stats.ip_frag.memerr == 0);
  183. fail_unless(lwip_stats.ip_frag.drop == 0);
  184. fail_unless(lwip_stats.mib2.ipreasmoks == 0);
  185. create_ip4_input_fragment(ip_id, 6*200, 200, 0);
  186. fail_unless(lwip_stats.ip_frag.recv == 8);
  187. fail_unless(lwip_stats.ip_frag.err == 0);
  188. fail_unless(lwip_stats.ip_frag.memerr == 0);
  189. fail_unless(lwip_stats.ip_frag.drop == 0);
  190. fail_unless(lwip_stats.mib2.ipreasmoks == 0);
  191. create_ip4_input_fragment(ip_id, 5*200, 200, 0);
  192. fail_unless(lwip_stats.ip_frag.recv == 9);
  193. fail_unless(lwip_stats.ip_frag.err == 0);
  194. fail_unless(lwip_stats.ip_frag.memerr == 0);
  195. fail_unless(lwip_stats.ip_frag.drop == 0);
  196. fail_unless(lwip_stats.mib2.ipreasmoks == 1);
  197. }
  198. END_TEST
  199. /* packets to 127.0.0.1 shall not be sent out to netif_default */
  200. START_TEST(test_127_0_0_1)
  201. {
  202. ip4_addr_t localhost;
  203. struct pbuf* p;
  204. LWIP_UNUSED_ARG(_i);
  205. test_netif_add();
  206. netif_set_down(netif_get_loopif());
  207. IP4_ADDR(&localhost, 127, 0, 0, 1);
  208. p = pbuf_alloc(PBUF_IP, 10, PBUF_POOL);
  209. if(ip4_output(p, netif_ip4_addr(netif_default), &localhost, 0, 0, IP_PROTO_UDP) != ERR_OK) {
  210. pbuf_free(p);
  211. }
  212. fail_unless(linkoutput_ctr == 0);
  213. }
  214. END_TEST
  215. START_TEST(test_ip4addr_aton)
  216. {
  217. ip4_addr_t ip_addr;
  218. LWIP_UNUSED_ARG(_i);
  219. fail_unless(ip4addr_aton("192.168.0.1", &ip_addr) == 1);
  220. fail_unless(ip4addr_aton("192.168.0.0001", &ip_addr) == 1);
  221. fail_unless(ip4addr_aton("192.168.0.zzz", &ip_addr) == 0);
  222. fail_unless(ip4addr_aton("192.168.1", &ip_addr) == 1);
  223. fail_unless(ip4addr_aton("192.168.0xd3", &ip_addr) == 1);
  224. fail_unless(ip4addr_aton("192.168.0xz5", &ip_addr) == 0);
  225. fail_unless(ip4addr_aton("192.168.095", &ip_addr) == 0);
  226. }
  227. END_TEST
  228. /** Create the suite including all tests for this module */
  229. Suite *
  230. ip4_suite(void)
  231. {
  232. testfunc tests[] = {
  233. TESTFUNC(test_ip4_frag),
  234. TESTFUNC(test_ip4_reass),
  235. TESTFUNC(test_127_0_0_1),
  236. TESTFUNC(test_ip4addr_aton),
  237. };
  238. return create_suite("IPv4", tests, sizeof(tests)/sizeof(testfunc), ip4_setup, ip4_teardown);
  239. }