test_udp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. #include "test_udp.h"
  2. #include "lwip/udp.h"
  3. #include "lwip/stats.h"
  4. #include "lwip/inet_chksum.h"
  5. #if !LWIP_STATS || !UDP_STATS || !MEMP_STATS
  6. #error "This tests needs UDP- and MEMP-statistics enabled"
  7. #endif
  8. struct test_udp_rxdata {
  9. u32_t rx_cnt;
  10. u32_t rx_bytes;
  11. struct udp_pcb *pcb;
  12. };
  13. static struct netif test_netif1, test_netif2;
  14. static ip4_addr_t test_gw1, test_ipaddr1, test_netmask1;
  15. static ip4_addr_t test_gw2, test_ipaddr2, test_netmask2;
  16. static int output_ctr, linkoutput_ctr;
  17. /* Helper functions */
  18. static void
  19. udp_remove_all(void)
  20. {
  21. struct udp_pcb *pcb = udp_pcbs;
  22. struct udp_pcb *pcb2;
  23. while(pcb != NULL) {
  24. pcb2 = pcb;
  25. pcb = pcb->next;
  26. udp_remove(pcb2);
  27. }
  28. fail_unless(MEMP_STATS_GET(used, MEMP_UDP_PCB) == 0);
  29. }
  30. static err_t
  31. default_netif_output(struct netif *netif, struct pbuf *p, const ip4_addr_t *ipaddr)
  32. {
  33. fail_unless((netif == &test_netif1) || (netif == &test_netif2));
  34. fail_unless(p != NULL);
  35. fail_unless(ipaddr != NULL);
  36. output_ctr++;
  37. return ERR_OK;
  38. }
  39. static err_t
  40. default_netif_linkoutput(struct netif *netif, struct pbuf *p)
  41. {
  42. fail_unless((netif == &test_netif1) || (netif == &test_netif2));
  43. fail_unless(p != NULL);
  44. linkoutput_ctr++;
  45. return ERR_OK;
  46. }
  47. static err_t
  48. default_netif_init(struct netif *netif)
  49. {
  50. fail_unless(netif != NULL);
  51. netif->output = default_netif_output;
  52. netif->linkoutput = default_netif_linkoutput;
  53. netif->mtu = 1500;
  54. netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP;
  55. netif->hwaddr_len = 6;
  56. return ERR_OK;
  57. }
  58. static void
  59. default_netif_add(void)
  60. {
  61. struct netif *n;
  62. #if LWIP_HAVE_LOOPIF
  63. fail_unless(netif_list != NULL); /* the loopif */
  64. fail_unless(netif_list->next == NULL);
  65. #else
  66. fail_unless(netif_list == NULL);
  67. #endif
  68. fail_unless(netif_default == NULL);
  69. IP4_ADDR(&test_ipaddr1, 192,168,0,1);
  70. IP4_ADDR(&test_netmask1, 255,255,255,0);
  71. IP4_ADDR(&test_gw1, 192,168,0,254);
  72. n = netif_add(&test_netif1, &test_ipaddr1, &test_netmask1,
  73. &test_gw1, NULL, default_netif_init, NULL);
  74. fail_unless(n == &test_netif1);
  75. IP4_ADDR(&test_ipaddr2, 192,168,1,1);
  76. IP4_ADDR(&test_netmask2, 255,255,255,0);
  77. IP4_ADDR(&test_gw2, 192,168,1,254);
  78. n = netif_add(&test_netif2, &test_ipaddr2, &test_netmask2,
  79. &test_gw2, NULL, default_netif_init, NULL);
  80. fail_unless(n == &test_netif2);
  81. netif_set_default(&test_netif1);
  82. netif_set_up(&test_netif1);
  83. netif_set_up(&test_netif2);
  84. }
  85. static void
  86. default_netif_remove(void)
  87. {
  88. fail_unless(netif_default == &test_netif1);
  89. netif_remove(&test_netif1);
  90. netif_remove(&test_netif2);
  91. fail_unless(netif_default == NULL);
  92. #if LWIP_HAVE_LOOPIF
  93. fail_unless(netif_list != NULL); /* the loopif */
  94. fail_unless(netif_list->next == NULL);
  95. #else
  96. fail_unless(netif_list == NULL);
  97. #endif
  98. }
  99. /* Setups/teardown functions */
  100. static void
  101. udp_setup(void)
  102. {
  103. udp_remove_all();
  104. default_netif_add();
  105. lwip_check_ensure_no_alloc(SKIP_POOL(MEMP_SYS_TIMEOUT));
  106. }
  107. static void
  108. udp_teardown(void)
  109. {
  110. udp_remove_all();
  111. default_netif_remove();
  112. lwip_check_ensure_no_alloc(SKIP_POOL(MEMP_SYS_TIMEOUT));
  113. }
  114. /* Test functions */
  115. START_TEST(test_udp_new_remove)
  116. {
  117. struct udp_pcb* pcb;
  118. LWIP_UNUSED_ARG(_i);
  119. fail_unless(MEMP_STATS_GET(used, MEMP_UDP_PCB) == 0);
  120. pcb = udp_new();
  121. fail_unless(pcb != NULL);
  122. if (pcb != NULL) {
  123. fail_unless(MEMP_STATS_GET(used, MEMP_UDP_PCB) == 1);
  124. udp_remove(pcb);
  125. fail_unless(MEMP_STATS_GET(used, MEMP_UDP_PCB) == 0);
  126. }
  127. }
  128. END_TEST
  129. static void test_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p,
  130. const ip_addr_t *addr, u16_t port)
  131. {
  132. struct test_udp_rxdata *ctr = (struct test_udp_rxdata *)arg;
  133. LWIP_UNUSED_ARG(addr);
  134. LWIP_UNUSED_ARG(port);
  135. fail_unless(arg != NULL);
  136. fail_unless(ctr->pcb == pcb);
  137. ctr->rx_cnt++;
  138. ctr->rx_bytes += p->tot_len;
  139. if (p != NULL) {
  140. pbuf_free(p);
  141. }
  142. }
  143. static struct pbuf *
  144. test_udp_create_test_packet(u16_t length, u16_t port, u32_t dst_addr)
  145. {
  146. err_t err;
  147. u8_t ret;
  148. struct udp_hdr *uh;
  149. struct ip_hdr *ih;
  150. struct pbuf *p;
  151. const u8_t test_data[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};
  152. p = pbuf_alloc(PBUF_TRANSPORT, length, PBUF_POOL);
  153. fail_unless(p != NULL);
  154. if (p == NULL) {
  155. return NULL;
  156. }
  157. fail_unless(p->next == NULL);
  158. err = pbuf_take(p, test_data, length);
  159. fail_unless(err == ERR_OK);
  160. /* add UDP header */
  161. ret = pbuf_add_header(p, sizeof(struct udp_hdr));
  162. fail_unless(!ret);
  163. uh = (struct udp_hdr *)p->payload;
  164. uh->chksum = 0;
  165. uh->dest = uh->src = lwip_htons(port);
  166. uh->len = lwip_htons(p->tot_len);
  167. /* add IPv4 header */
  168. ret = pbuf_add_header(p, sizeof(struct ip_hdr));
  169. fail_unless(!ret);
  170. ih = (struct ip_hdr *)p->payload;
  171. memset(ih, 0, sizeof(*ih));
  172. ih->dest.addr = dst_addr;
  173. ih->_len = lwip_htons(p->tot_len);
  174. ih->_ttl = 32;
  175. ih->_proto = IP_PROTO_UDP;
  176. IPH_VHL_SET(ih, 4, sizeof(struct ip_hdr) / 4);
  177. IPH_CHKSUM_SET(ih, inet_chksum(ih, sizeof(struct ip_hdr)));
  178. return p;
  179. }
  180. /* bind 2 pcbs to specific netif IP and test which one gets broadcasts */
  181. START_TEST(test_udp_broadcast_rx_with_2_netifs)
  182. {
  183. err_t err;
  184. struct udp_pcb *pcb1, *pcb2;
  185. const u16_t port = 12345;
  186. struct test_udp_rxdata ctr1, ctr2;
  187. struct pbuf *p;
  188. #if SO_REUSE
  189. struct udp_pcb *pcb_any;
  190. struct test_udp_rxdata ctr_any;
  191. #endif
  192. LWIP_UNUSED_ARG(_i);
  193. pcb1 = udp_new();
  194. fail_unless(pcb1 != NULL);
  195. pcb2 = udp_new();
  196. fail_unless(pcb2 != NULL);
  197. #if SO_REUSE
  198. pcb_any = udp_new();
  199. fail_unless(pcb_any != NULL);
  200. ip_set_option(pcb1, SOF_REUSEADDR);
  201. ip_set_option(pcb2, SOF_REUSEADDR);
  202. ip_set_option(pcb_any, SOF_REUSEADDR);
  203. err = udp_bind(pcb_any, NULL, port);
  204. fail_unless(err == ERR_OK);
  205. memset(&ctr_any, 0, sizeof(ctr_any));
  206. ctr_any.pcb = pcb_any;
  207. udp_recv(pcb_any, test_recv, &ctr_any);
  208. #endif
  209. err = udp_bind(pcb1, &test_netif1.ip_addr, port);
  210. fail_unless(err == ERR_OK);
  211. err = udp_bind(pcb2, &test_netif2.ip_addr, port);
  212. fail_unless(err == ERR_OK);
  213. memset(&ctr1, 0, sizeof(ctr1));
  214. ctr1.pcb = pcb1;
  215. memset(&ctr2, 0, sizeof(ctr2));
  216. ctr2.pcb = pcb2;
  217. udp_recv(pcb1, test_recv, &ctr1);
  218. udp_recv(pcb2, test_recv, &ctr2);
  219. /* unicast to netif1 */
  220. p = test_udp_create_test_packet(16, port, test_ipaddr1.addr);
  221. EXPECT_RET(p != NULL);
  222. err = ip4_input(p, &test_netif1);
  223. fail_unless(err == ERR_OK);
  224. fail_unless(ctr1.rx_cnt == 1);
  225. fail_unless(ctr1.rx_bytes == 16);
  226. fail_unless(ctr2.rx_cnt == 0);
  227. #if SO_REUSE
  228. fail_unless(ctr_any.rx_cnt == 0);
  229. #endif
  230. ctr1.rx_cnt = ctr1.rx_bytes = 0;
  231. /* unicast to netif2 */
  232. p = test_udp_create_test_packet(16, port, test_ipaddr2.addr);
  233. EXPECT_RET(p != NULL);
  234. err = ip4_input(p, &test_netif2);
  235. fail_unless(err == ERR_OK);
  236. fail_unless(ctr2.rx_cnt == 1);
  237. fail_unless(ctr2.rx_bytes == 16);
  238. fail_unless(ctr1.rx_cnt == 0);
  239. #if SO_REUSE
  240. fail_unless(ctr_any.rx_cnt == 0);
  241. #endif
  242. ctr2.rx_cnt = ctr2.rx_bytes = 0;
  243. /* broadcast to netif1-broadcast, input to netif2 */
  244. p = test_udp_create_test_packet(16, port, test_ipaddr1.addr | ~test_netmask1.addr);
  245. EXPECT_RET(p != NULL);
  246. err = ip4_input(p, &test_netif2);
  247. fail_unless(err == ERR_OK);
  248. fail_unless(ctr1.rx_cnt == 1);
  249. fail_unless(ctr1.rx_bytes == 16);
  250. fail_unless(ctr2.rx_cnt == 0);
  251. #if SO_REUSE
  252. fail_unless(ctr_any.rx_cnt == 0);
  253. #endif
  254. ctr1.rx_cnt = ctr1.rx_bytes = 0;
  255. /* broadcast to netif2-broadcast, input to netif1 */
  256. p = test_udp_create_test_packet(16, port, test_ipaddr2.addr | ~test_netmask2.addr);
  257. EXPECT_RET(p != NULL);
  258. err = ip4_input(p, &test_netif1);
  259. fail_unless(err == ERR_OK);
  260. fail_unless(ctr2.rx_cnt == 1);
  261. fail_unless(ctr2.rx_bytes == 16);
  262. fail_unless(ctr1.rx_cnt == 0);
  263. #if SO_REUSE
  264. fail_unless(ctr_any.rx_cnt == 0);
  265. #endif
  266. ctr2.rx_cnt = ctr2.rx_bytes = 0;
  267. /* broadcast to global-broadcast, input to netif1 */
  268. p = test_udp_create_test_packet(16, port, 0xffffffff);
  269. EXPECT_RET(p != NULL);
  270. err = ip4_input(p, &test_netif1);
  271. fail_unless(err == ERR_OK);
  272. fail_unless(ctr1.rx_cnt == 1);
  273. fail_unless(ctr1.rx_bytes == 16);
  274. fail_unless(ctr2.rx_cnt == 0);
  275. #if SO_REUSE
  276. fail_unless(ctr_any.rx_cnt == 0);
  277. #endif
  278. ctr1.rx_cnt = ctr1.rx_bytes = 0;
  279. /* broadcast to global-broadcast, input to netif2 */
  280. p = test_udp_create_test_packet(16, port, 0xffffffff);
  281. EXPECT_RET(p != NULL);
  282. err = ip4_input(p, &test_netif2);
  283. fail_unless(err == ERR_OK);
  284. fail_unless(ctr2.rx_cnt == 1);
  285. fail_unless(ctr2.rx_bytes == 16);
  286. fail_unless(ctr1.rx_cnt == 0);
  287. #if SO_REUSE
  288. fail_unless(ctr_any.rx_cnt == 0);
  289. #endif
  290. ctr2.rx_cnt = ctr2.rx_bytes = 0;
  291. }
  292. END_TEST
  293. START_TEST(test_udp_bind)
  294. {
  295. struct udp_pcb* pcb1;
  296. struct udp_pcb* pcb2;
  297. ip_addr_t ip1;
  298. ip_addr_t ip2;
  299. err_t err1;
  300. err_t err2;
  301. LWIP_UNUSED_ARG(_i);
  302. /* bind on same port using different IP address types */
  303. ip_addr_set_any_val(0, ip1);
  304. ip_addr_set_any_val(1, ip2);
  305. pcb1 = udp_new_ip_type(IPADDR_TYPE_V4);
  306. pcb2 = udp_new_ip_type(IPADDR_TYPE_V6);
  307. err1 = udp_bind(pcb1, &ip1, 2105);
  308. err2 = udp_bind(pcb2, &ip2, 2105);
  309. fail_unless(err1 == ERR_OK);
  310. fail_unless(err2 == ERR_OK);
  311. udp_remove(pcb1);
  312. udp_remove(pcb2);
  313. /* bind on same port using SAME IPv4 address type */
  314. ip_addr_set_any_val(0, ip1);
  315. ip_addr_set_any_val(0, ip2);
  316. pcb1 = udp_new_ip_type(IPADDR_TYPE_V4);
  317. pcb2 = udp_new_ip_type(IPADDR_TYPE_V4);
  318. err1 = udp_bind(pcb1, &ip1, 2105);
  319. err2 = udp_bind(pcb2, &ip2, 2105);
  320. fail_unless(err1 == ERR_OK);
  321. fail_unless(err2 == ERR_USE);
  322. udp_remove(pcb1);
  323. udp_remove(pcb2);
  324. /* bind on same port using SAME IPv6 address type */
  325. ip_addr_set_any_val(1, ip1);
  326. ip_addr_set_any_val(1, ip2);
  327. pcb1 = udp_new_ip_type(IPADDR_TYPE_V6);
  328. pcb2 = udp_new_ip_type(IPADDR_TYPE_V6);
  329. err1 = udp_bind(pcb1, &ip1, 2105);
  330. err2 = udp_bind(pcb2, &ip2, 2105);
  331. fail_unless(err1 == ERR_OK);
  332. fail_unless(err2 == ERR_USE);
  333. udp_remove(pcb1);
  334. udp_remove(pcb2);
  335. /* Bind with different IP address type */
  336. ip_addr_set_any_val(0, ip1);
  337. ip_addr_set_any_val(1, ip2);
  338. pcb1 = udp_new_ip_type(IPADDR_TYPE_V6);
  339. pcb2 = udp_new_ip_type(IPADDR_TYPE_V4);
  340. err1 = udp_bind(pcb1, &ip1, 2105);
  341. err2 = udp_bind(pcb2, &ip2, 2105);
  342. fail_unless(err1 == ERR_OK);
  343. fail_unless(err2 == ERR_OK);
  344. udp_remove(pcb1);
  345. udp_remove(pcb2);
  346. /* Bind with different IP numbers */
  347. IP_ADDR4(&ip1, 1, 2, 3, 4);
  348. IP_ADDR4(&ip2, 4, 3, 2, 1);
  349. pcb1 = udp_new_ip_type(IPADDR_TYPE_V6);
  350. pcb2 = udp_new_ip_type(IPADDR_TYPE_V4);
  351. err1 = udp_bind(pcb1, &ip1, 2105);
  352. err2 = udp_bind(pcb2, &ip2, 2105);
  353. fail_unless(err1 == ERR_OK);
  354. fail_unless(err2 == ERR_OK);
  355. udp_remove(pcb1);
  356. udp_remove(pcb2);
  357. /* Bind with same IP numbers */
  358. IP_ADDR4(&ip1, 1, 2, 3, 4);
  359. IP_ADDR4(&ip2, 1, 2, 3, 4);
  360. pcb1 = udp_new_ip_type(IPADDR_TYPE_V6);
  361. pcb2 = udp_new_ip_type(IPADDR_TYPE_V4);
  362. err1 = udp_bind(pcb1, &ip1, 2105);
  363. err2 = udp_bind(pcb2, &ip2, 2105);
  364. fail_unless(err1 == ERR_OK);
  365. fail_unless(err2 == ERR_USE);
  366. udp_remove(pcb1);
  367. udp_remove(pcb2);
  368. /* bind on same port using ANY + IPv4 */
  369. ip1 = *IP_ANY_TYPE;
  370. IP_ADDR4(&ip2, 1, 2, 3, 4);
  371. pcb1 = udp_new_ip_type(IPADDR_TYPE_ANY);
  372. pcb2 = udp_new_ip_type(IPADDR_TYPE_V4);
  373. err1 = udp_bind(pcb1, &ip1, 2105);
  374. err2 = udp_bind(pcb2, &ip2, 2105);
  375. fail_unless(err1 == ERR_OK);
  376. fail_unless(err2 == ERR_USE);
  377. udp_remove(pcb1);
  378. udp_remove(pcb2);
  379. }
  380. END_TEST
  381. /** Create the suite including all tests for this module */
  382. Suite *
  383. udp_suite(void)
  384. {
  385. testfunc tests[] = {
  386. TESTFUNC(test_udp_new_remove),
  387. TESTFUNC(test_udp_broadcast_rx_with_2_netifs),
  388. TESTFUNC(test_udp_bind)
  389. };
  390. return create_suite("UDP", tests, sizeof(tests)/sizeof(testfunc), udp_setup, udp_teardown);
  391. }