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