1
0

test_tcp.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #include "test_tcp.h"
  2. #include "lwip/tcp_impl.h"
  3. #include "lwip/stats.h"
  4. #include "tcp_helper.h"
  5. #if !LWIP_STATS || !TCP_STATS || !MEMP_STATS
  6. #error "This tests needs TCP- and MEMP-statistics enabled"
  7. #endif
  8. /* Setups/teardown functions */
  9. static void
  10. tcp_setup(void)
  11. {
  12. tcp_remove_all();
  13. }
  14. static void
  15. tcp_teardown(void)
  16. {
  17. tcp_remove_all();
  18. }
  19. /* Test functions */
  20. /** Call tcp_new() and tcp_abort() and test memp stats */
  21. START_TEST(test_tcp_new_abort)
  22. {
  23. struct tcp_pcb* pcb;
  24. LWIP_UNUSED_ARG(_i);
  25. fail_unless(lwip_stats.memp[MEMP_TCP_PCB].used == 0);
  26. pcb = tcp_new();
  27. fail_unless(pcb != NULL);
  28. if (pcb != NULL) {
  29. fail_unless(lwip_stats.memp[MEMP_TCP_PCB].used == 1);
  30. tcp_abort(pcb);
  31. fail_unless(lwip_stats.memp[MEMP_TCP_PCB].used == 0);
  32. }
  33. }
  34. END_TEST
  35. /** Create an ESTABLISHED pcb and check if receive callback is called */
  36. START_TEST(test_tcp_recv_inseq)
  37. {
  38. struct test_tcp_counters counters;
  39. struct tcp_pcb* pcb;
  40. struct pbuf* p;
  41. char data[] = {1, 2, 3, 4};
  42. ip_addr_t remote_ip, local_ip;
  43. u16_t data_len;
  44. u16_t remote_port = 0x100, local_port = 0x101;
  45. struct netif netif;
  46. LWIP_UNUSED_ARG(_i);
  47. /* initialize local vars */
  48. memset(&netif, 0, sizeof(netif));
  49. IP4_ADDR(&local_ip, 192, 168, 1, 1);
  50. IP4_ADDR(&remote_ip, 192, 168, 1, 2);
  51. data_len = sizeof(data);
  52. /* initialize counter struct */
  53. memset(&counters, 0, sizeof(counters));
  54. counters.expected_data_len = data_len;
  55. counters.expected_data = data;
  56. /* create and initialize the pcb */
  57. pcb = test_tcp_new_counters_pcb(&counters);
  58. EXPECT_RET(pcb != NULL);
  59. tcp_set_state(pcb, ESTABLISHED, &local_ip, &remote_ip, local_port, remote_port);
  60. /* create a segment */
  61. p = tcp_create_rx_segment(pcb, counters.expected_data, data_len, 0, 0, 0);
  62. EXPECT(p != NULL);
  63. if (p != NULL) {
  64. /* pass the segment to tcp_input */
  65. test_tcp_input(p, &netif);
  66. /* check if counters are as expected */
  67. EXPECT(counters.close_calls == 0);
  68. EXPECT(counters.recv_calls == 1);
  69. EXPECT(counters.recved_bytes == data_len);
  70. EXPECT(counters.err_calls == 0);
  71. }
  72. /* make sure the pcb is freed */
  73. EXPECT(lwip_stats.memp[MEMP_TCP_PCB].used == 1);
  74. tcp_abort(pcb);
  75. EXPECT(lwip_stats.memp[MEMP_TCP_PCB].used == 0);
  76. }
  77. END_TEST
  78. /** Create the suite including all tests for this module */
  79. Suite *
  80. tcp_suite(void)
  81. {
  82. TFun tests[] = {
  83. test_tcp_new_abort,
  84. test_tcp_recv_inseq,
  85. };
  86. return create_suite("TCP", tests, sizeof(tests)/sizeof(TFun), tcp_setup, tcp_teardown);
  87. }