xref: /nrf52832-nimble/rt-thread/components/net/lwip-2.1.0/test/unit/tcp/test_tcp.c (revision 104654410c56c573564690304ae786df310c91fc)
1 #include "test_tcp.h"
2 
3 #include "lwip/priv/tcp_priv.h"
4 #include "lwip/stats.h"
5 #include "tcp_helper.h"
6 #include "lwip/inet_chksum.h"
7 
8 #ifdef _MSC_VER
9 #pragma warning(disable: 4307) /* we explicitly wrap around TCP seqnos */
10 #endif
11 
12 #if !LWIP_STATS || !TCP_STATS || !MEMP_STATS
13 #error "This tests needs TCP- and MEMP-statistics enabled"
14 #endif
15 #if TCP_SND_BUF <= TCP_WND
16 #error "This tests needs TCP_SND_BUF to be > TCP_WND"
17 #endif
18 
19 /* used with check_seqnos() */
20 #define SEQNO1 (0xFFFFFF00 - TCP_MSS)
21 #define ISS    6510
22 static u32_t seqnos[] = {
23     SEQNO1,
24     SEQNO1 + (1 * TCP_MSS),
25     SEQNO1 + (2 * TCP_MSS),
26     SEQNO1 + (3 * TCP_MSS),
27     SEQNO1 + (4 * TCP_MSS),
28     SEQNO1 + (5 * TCP_MSS) };
29 
30 static u8_t test_tcp_timer;
31 
32 /* our own version of tcp_tmr so we can reset fast/slow timer state */
33 static void
test_tcp_tmr(void)34 test_tcp_tmr(void)
35 {
36   tcp_fasttmr();
37   if (++test_tcp_timer & 1) {
38     tcp_slowtmr();
39   }
40 }
41 
42 /* Setups/teardown functions */
43 static struct netif *old_netif_list;
44 static struct netif *old_netif_default;
45 
46 static void
tcp_setup(void)47 tcp_setup(void)
48 {
49   struct tcp_pcb dummy_pcb; /* we need this for tcp_next_iss() only */
50 
51   old_netif_list = netif_list;
52   old_netif_default = netif_default;
53   netif_list = NULL;
54   netif_default = NULL;
55   /* reset iss to default (6510) */
56   tcp_ticks = 0;
57   tcp_ticks = 0 - (tcp_next_iss(&dummy_pcb) - 6510);
58   tcp_next_iss(&dummy_pcb);
59   tcp_ticks = 0;
60 
61   test_tcp_timer = 0;
62   tcp_remove_all();
63   lwip_check_ensure_no_alloc(SKIP_POOL(MEMP_SYS_TIMEOUT));
64 }
65 
66 static void
tcp_teardown(void)67 tcp_teardown(void)
68 {
69   netif_list = NULL;
70   netif_default = NULL;
71   tcp_remove_all();
72   /* restore netif_list for next tests (e.g. loopif) */
73   netif_list = old_netif_list;
74   netif_default = old_netif_default;
75   lwip_check_ensure_no_alloc(SKIP_POOL(MEMP_SYS_TIMEOUT));
76 }
77 
78 
79 /* Test functions */
80 
81 /** Call tcp_new() and tcp_abort() and test memp stats */
START_TEST(test_tcp_new_abort)82 START_TEST(test_tcp_new_abort)
83 {
84   struct tcp_pcb* pcb;
85   LWIP_UNUSED_ARG(_i);
86 
87   fail_unless(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
88 
89   pcb = tcp_new();
90   fail_unless(pcb != NULL);
91   if (pcb != NULL) {
92     fail_unless(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
93     tcp_abort(pcb);
94     fail_unless(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
95   }
96 }
97 END_TEST
98 
99 /** Call tcp_new() and tcp_abort() and test memp stats */
START_TEST(test_tcp_listen_passive_open)100 START_TEST(test_tcp_listen_passive_open)
101 {
102   struct tcp_pcb *pcb, *pcbl;
103   struct tcp_pcb_listen *lpcb;
104   struct netif netif;
105   struct test_tcp_txcounters txcounters;
106   struct test_tcp_counters counters;
107   struct pbuf *p;
108   ip_addr_t src_addr;
109   err_t err;
110   LWIP_UNUSED_ARG(_i);
111 
112   fail_unless(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
113 
114   test_tcp_init_netif(&netif, &txcounters, &test_local_ip, &test_netmask);
115   /* initialize counter struct */
116   memset(&counters, 0, sizeof(counters));
117 
118   pcb = tcp_new();
119   EXPECT_RET(pcb != NULL);
120   err = tcp_bind(pcb, &netif.ip_addr, 1234);
121   EXPECT(err == ERR_OK);
122   pcbl = tcp_listen(pcb);
123   EXPECT_RET(pcbl != NULL);
124   EXPECT_RET(pcbl != pcb);
125   lpcb = (struct tcp_pcb_listen *)pcbl;
126 
127   ip_addr_set_ip4_u32_val(src_addr, lwip_htonl(lwip_ntohl(ip_addr_get_ip4_u32(&lpcb->local_ip)) + 1));
128 
129   /* check correct syn packet */
130   p = tcp_create_segment(&src_addr, &lpcb->local_ip, 12345,
131     lpcb->local_port, NULL, 0, 12345, 54321, TCP_SYN);
132   EXPECT(p != NULL);
133   if (p != NULL) {
134     /* pass the segment to tcp_input */
135     test_tcp_input(p, &netif);
136     /* check if counters are as expected */
137     EXPECT(txcounters.num_tx_calls == 1);
138   }
139 
140   /* check syn packet with short length */
141   p = tcp_create_segment(&src_addr, &lpcb->local_ip, 12345,
142     lpcb->local_port, NULL, 0, 12345, 54321, TCP_SYN);
143   EXPECT(p != NULL);
144   EXPECT(p->next == NULL);
145   if ((p != NULL) && (p->next == NULL)) {
146     p->len -= 2;
147     p->tot_len -= 2;
148     /* pass the segment to tcp_input */
149     test_tcp_input(p, &netif);
150     /* check if counters are as expected */
151     EXPECT(txcounters.num_tx_calls == 1);
152   }
153 
154   tcp_close(pcbl);
155 }
156 END_TEST
157 
158 /** Create an ESTABLISHED pcb and check if receive callback is called */
START_TEST(test_tcp_recv_inseq)159 START_TEST(test_tcp_recv_inseq)
160 {
161   struct test_tcp_counters counters;
162   struct tcp_pcb* pcb;
163   struct pbuf* p;
164   char data[] = {1, 2, 3, 4};
165   u16_t data_len;
166   struct netif netif;
167   struct test_tcp_txcounters txcounters;
168   LWIP_UNUSED_ARG(_i);
169 
170   /* initialize local vars */
171   test_tcp_init_netif(&netif, &txcounters, &test_local_ip, &test_netmask);
172   data_len = sizeof(data);
173   /* initialize counter struct */
174   memset(&counters, 0, sizeof(counters));
175   counters.expected_data_len = data_len;
176   counters.expected_data = data;
177 
178   /* create and initialize the pcb */
179   pcb = test_tcp_new_counters_pcb(&counters);
180   EXPECT_RET(pcb != NULL);
181   tcp_set_state(pcb, ESTABLISHED, &test_local_ip, &test_remote_ip, TEST_LOCAL_PORT, TEST_REMOTE_PORT);
182 
183   /* create a segment */
184   p = tcp_create_rx_segment(pcb, counters.expected_data, data_len, 0, 0, 0);
185   EXPECT(p != NULL);
186   if (p != NULL) {
187     /* pass the segment to tcp_input */
188     test_tcp_input(p, &netif);
189     /* check if counters are as expected */
190     EXPECT(counters.close_calls == 0);
191     EXPECT(counters.recv_calls == 1);
192     EXPECT(counters.recved_bytes == data_len);
193     EXPECT(counters.err_calls == 0);
194   }
195 
196   /* make sure the pcb is freed */
197   EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
198   tcp_abort(pcb);
199   EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
200 }
201 END_TEST
202 
203 /** Create an ESTABLISHED pcb and check if receive callback is called if a segment
204  * overlapping rcv_nxt is received */
START_TEST(test_tcp_recv_inseq_trim)205 START_TEST(test_tcp_recv_inseq_trim)
206 {
207   struct test_tcp_counters counters;
208   struct tcp_pcb* pcb;
209   struct pbuf* p;
210   char data[PBUF_POOL_BUFSIZE*2];
211   u16_t data_len;
212   struct netif netif;
213   struct test_tcp_txcounters txcounters;
214   const u32_t new_data_len = 40;
215   LWIP_UNUSED_ARG(_i);
216 
217   /* initialize local vars */
218   test_tcp_init_netif(&netif, &txcounters, &test_local_ip, &test_netmask);
219   data_len = sizeof(data);
220   memset(data, 0, sizeof(data));
221   /* initialize counter struct */
222   memset(&counters, 0, sizeof(counters));
223   counters.expected_data_len = data_len;
224   counters.expected_data = data;
225 
226   /* create and initialize the pcb */
227   pcb = test_tcp_new_counters_pcb(&counters);
228   EXPECT_RET(pcb != NULL);
229   tcp_set_state(pcb, ESTABLISHED, &test_local_ip, &test_remote_ip, TEST_LOCAL_PORT, TEST_REMOTE_PORT);
230 
231   /* create a segment (with an overlapping/old seqno so that the new data begins in the 2nd pbuf) */
232   p = tcp_create_rx_segment(pcb, counters.expected_data, data_len, (u32_t)(0-(data_len-new_data_len)), 0, 0);
233   EXPECT(p != NULL);
234   if (p != NULL) {
235     EXPECT(p->next != NULL);
236     if (p->next != NULL) {
237       EXPECT(p->next->next != NULL);
238     }
239   }
240   if ((p != NULL) && (p->next != NULL) && (p->next->next != NULL)) {
241     /* pass the segment to tcp_input */
242     test_tcp_input(p, &netif);
243     /* check if counters are as expected */
244     EXPECT(counters.close_calls == 0);
245     EXPECT(counters.recv_calls == 1);
246     EXPECT(counters.recved_bytes == new_data_len);
247     EXPECT(counters.err_calls == 0);
248   }
249 
250   /* make sure the pcb is freed */
251   EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
252   tcp_abort(pcb);
253   EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
254 }
255 END_TEST
256 
257 static err_t test_tcp_recv_expect1byte(void* arg, struct tcp_pcb* pcb, struct pbuf* p, err_t err);
258 
259 static err_t
test_tcp_recv_expectclose(void * arg,struct tcp_pcb * pcb,struct pbuf * p,err_t err)260 test_tcp_recv_expectclose(void* arg, struct tcp_pcb* pcb, struct pbuf* p, err_t err)
261 {
262   EXPECT_RETX(pcb != NULL, ERR_OK);
263   EXPECT_RETX(err == ERR_OK, ERR_OK);
264   LWIP_UNUSED_ARG(arg);
265 
266   if (p != NULL) {
267     fail();
268   } else {
269     /* correct: FIN received; close our end, too */
270     err_t err2 = tcp_close(pcb);
271     fail_unless(err2 == ERR_OK);
272     /* set back to some other rx function, just to not get here again */
273     tcp_recv(pcb, test_tcp_recv_expect1byte);
274   }
275   return ERR_OK;
276 }
277 
278 static err_t
test_tcp_recv_expect1byte(void * arg,struct tcp_pcb * pcb,struct pbuf * p,err_t err)279 test_tcp_recv_expect1byte(void* arg, struct tcp_pcb* pcb, struct pbuf* p, err_t err)
280 {
281   EXPECT_RETX(pcb != NULL, ERR_OK);
282   EXPECT_RETX(err == ERR_OK, ERR_OK);
283   LWIP_UNUSED_ARG(arg);
284 
285   if (p != NULL) {
286     if ((p->len == 1) && (p->tot_len == 1)) {
287       tcp_recv(pcb, test_tcp_recv_expectclose);
288     } else {
289       fail();
290     }
291     pbuf_free(p);
292   } else {
293     fail();
294   }
295   return ERR_OK;
296 }
297 
START_TEST(test_tcp_passive_close)298 START_TEST(test_tcp_passive_close)
299 {
300   struct test_tcp_counters counters;
301   struct tcp_pcb* pcb;
302   struct pbuf* p;
303   char data = 0x0f;
304   struct netif netif;
305   struct test_tcp_txcounters txcounters;
306   LWIP_UNUSED_ARG(_i);
307 
308   /* initialize local vars */
309   test_tcp_init_netif(&netif, &txcounters, &test_local_ip, &test_netmask);
310 
311   /* initialize counter struct */
312   memset(&counters, 0, sizeof(counters));
313   counters.expected_data_len = 1;
314   counters.expected_data = &data;
315 
316   /* create and initialize the pcb */
317   pcb = test_tcp_new_counters_pcb(&counters);
318   EXPECT_RET(pcb != NULL);
319   tcp_set_state(pcb, ESTABLISHED, &test_local_ip, &test_remote_ip, TEST_LOCAL_PORT, TEST_REMOTE_PORT);
320 
321   /* create a segment without data */
322   p = tcp_create_rx_segment(pcb, &data, 1, 0, 0, TCP_FIN);
323   EXPECT(p != NULL);
324   if (p != NULL) {
325     tcp_recv(pcb, test_tcp_recv_expect1byte);
326     /* pass the segment to tcp_input */
327     test_tcp_input(p, &netif);
328   }
329   /* don't free the pcb here (part of the test!) */
330 }
331 END_TEST
332 
333 /** Check that we handle malformed tcp headers, and discard the pbuf(s) */
START_TEST(test_tcp_malformed_header)334 START_TEST(test_tcp_malformed_header)
335 {
336   struct test_tcp_counters counters;
337   struct tcp_pcb* pcb;
338   struct pbuf* p;
339   char data[] = {1, 2, 3, 4};
340   u16_t data_len, chksum;
341   struct netif netif;
342   struct test_tcp_txcounters txcounters;
343   struct tcp_hdr *hdr;
344   LWIP_UNUSED_ARG(_i);
345 
346   /* initialize local vars */
347   test_tcp_init_netif(&netif, &txcounters, &test_local_ip, &test_netmask);
348   data_len = sizeof(data);
349   /* initialize counter struct */
350   memset(&counters, 0, sizeof(counters));
351   counters.expected_data_len = data_len;
352   counters.expected_data = data;
353 
354   /* create and initialize the pcb */
355   pcb = test_tcp_new_counters_pcb(&counters);
356   EXPECT_RET(pcb != NULL);
357   tcp_set_state(pcb, ESTABLISHED, &test_local_ip, &test_remote_ip, TEST_LOCAL_PORT, TEST_REMOTE_PORT);
358 
359   /* create a segment */
360   p = tcp_create_rx_segment(pcb, counters.expected_data, data_len, 0, 0, 0);
361 
362   pbuf_header(p, -(s16_t)sizeof(struct ip_hdr));
363 
364   hdr = (struct tcp_hdr *)p->payload;
365   TCPH_HDRLEN_FLAGS_SET(hdr, 15, 0x3d1);
366 
367   hdr->chksum = 0;
368 
369   chksum = ip_chksum_pseudo(p, IP_PROTO_TCP, p->tot_len,
370                              &test_remote_ip, &test_local_ip);
371 
372   hdr->chksum = chksum;
373 
374   pbuf_header(p, sizeof(struct ip_hdr));
375 
376   EXPECT(p != NULL);
377   EXPECT(p->next == NULL);
378   if (p != NULL) {
379     /* pass the segment to tcp_input */
380     test_tcp_input(p, &netif);
381     /* check if counters are as expected */
382     EXPECT(counters.close_calls == 0);
383     EXPECT(counters.recv_calls == 0);
384     EXPECT(counters.recved_bytes == 0);
385     EXPECT(counters.err_calls == 0);
386   }
387 
388   /* make sure the pcb is freed */
389   EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
390   tcp_abort(pcb);
391   EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
392 }
393 END_TEST
394 
395 
396 /** Provoke fast retransmission by duplicate ACKs and then recover by ACKing all sent data.
397  * At the end, send more data. */
START_TEST(test_tcp_fast_retx_recover)398 START_TEST(test_tcp_fast_retx_recover)
399 {
400   struct netif netif;
401   struct test_tcp_txcounters txcounters;
402   struct test_tcp_counters counters;
403   struct tcp_pcb* pcb;
404   struct pbuf* p;
405   char data1[] = { 1,  2,  3,  4};
406   char data2[] = { 5,  6,  7,  8};
407   char data3[] = { 9, 10, 11, 12};
408   char data4[] = {13, 14, 15, 16};
409   char data5[] = {17, 18, 19, 20};
410   char data6[TCP_MSS] = {21, 22, 23, 24};
411   err_t err;
412   LWIP_UNUSED_ARG(_i);
413 
414   /* initialize local vars */
415   test_tcp_init_netif(&netif, &txcounters, &test_local_ip, &test_netmask);
416   memset(&counters, 0, sizeof(counters));
417 
418   /* create and initialize the pcb */
419   pcb = test_tcp_new_counters_pcb(&counters);
420   EXPECT_RET(pcb != NULL);
421   tcp_set_state(pcb, ESTABLISHED, &test_local_ip, &test_remote_ip, TEST_LOCAL_PORT, TEST_REMOTE_PORT);
422   pcb->mss = TCP_MSS;
423   /* disable initial congestion window (we don't send a SYN here...) */
424   pcb->cwnd = pcb->snd_wnd;
425 
426   /* send data1 */
427   err = tcp_write(pcb, data1, sizeof(data1), TCP_WRITE_FLAG_COPY);
428   EXPECT_RET(err == ERR_OK);
429   err = tcp_output(pcb);
430   EXPECT_RET(err == ERR_OK);
431   EXPECT_RET(txcounters.num_tx_calls == 1);
432   EXPECT_RET(txcounters.num_tx_bytes == sizeof(data1) + sizeof(struct tcp_hdr) + sizeof(struct ip_hdr));
433   memset(&txcounters, 0, sizeof(txcounters));
434  /* "recv" ACK for data1 */
435   p = tcp_create_rx_segment(pcb, NULL, 0, 0, 4, TCP_ACK);
436   EXPECT_RET(p != NULL);
437   test_tcp_input(p, &netif);
438   EXPECT_RET(txcounters.num_tx_calls == 0);
439   EXPECT_RET(pcb->unacked == NULL);
440   /* send data2 */
441   err = tcp_write(pcb, data2, sizeof(data2), TCP_WRITE_FLAG_COPY);
442   EXPECT_RET(err == ERR_OK);
443   err = tcp_output(pcb);
444   EXPECT_RET(err == ERR_OK);
445   EXPECT_RET(txcounters.num_tx_calls == 1);
446   EXPECT_RET(txcounters.num_tx_bytes == sizeof(data2) + sizeof(struct tcp_hdr) + sizeof(struct ip_hdr));
447   memset(&txcounters, 0, sizeof(txcounters));
448   /* duplicate ACK for data1 (data2 is lost) */
449   p = tcp_create_rx_segment(pcb, NULL, 0, 0, 0, TCP_ACK);
450   EXPECT_RET(p != NULL);
451   test_tcp_input(p, &netif);
452   EXPECT_RET(txcounters.num_tx_calls == 0);
453   EXPECT_RET(pcb->dupacks == 1);
454   /* send data3 */
455   err = tcp_write(pcb, data3, sizeof(data3), TCP_WRITE_FLAG_COPY);
456   EXPECT_RET(err == ERR_OK);
457   err = tcp_output(pcb);
458   EXPECT_RET(err == ERR_OK);
459   /* nagle enabled, no tx calls */
460   EXPECT_RET(txcounters.num_tx_calls == 0);
461   EXPECT_RET(txcounters.num_tx_bytes == 0);
462   memset(&txcounters, 0, sizeof(txcounters));
463   /* 2nd duplicate ACK for data1 (data2 and data3 are lost) */
464   p = tcp_create_rx_segment(pcb, NULL, 0, 0, 0, TCP_ACK);
465   EXPECT_RET(p != NULL);
466   test_tcp_input(p, &netif);
467   EXPECT_RET(txcounters.num_tx_calls == 0);
468   EXPECT_RET(pcb->dupacks == 2);
469   /* queue data4, don't send it (unsent-oversize is != 0) */
470   err = tcp_write(pcb, data4, sizeof(data4), TCP_WRITE_FLAG_COPY);
471   EXPECT_RET(err == ERR_OK);
472   /* 3nd duplicate ACK for data1 (data2 and data3 are lost) -> fast retransmission */
473   p = tcp_create_rx_segment(pcb, NULL, 0, 0, 0, TCP_ACK);
474   EXPECT_RET(p != NULL);
475   test_tcp_input(p, &netif);
476   /*EXPECT_RET(txcounters.num_tx_calls == 1);*/
477   EXPECT_RET(pcb->dupacks == 3);
478   memset(&txcounters, 0, sizeof(txcounters));
479   /* @todo: check expected data?*/
480 
481   /* send data5, not output yet */
482   err = tcp_write(pcb, data5, sizeof(data5), TCP_WRITE_FLAG_COPY);
483   EXPECT_RET(err == ERR_OK);
484   /*err = tcp_output(pcb);
485   EXPECT_RET(err == ERR_OK);*/
486   EXPECT_RET(txcounters.num_tx_calls == 0);
487   EXPECT_RET(txcounters.num_tx_bytes == 0);
488   memset(&txcounters, 0, sizeof(txcounters));
489   {
490     int i = 0;
491     do
492     {
493       err = tcp_write(pcb, data6, TCP_MSS, TCP_WRITE_FLAG_COPY);
494       i++;
495     }while(err == ERR_OK);
496     EXPECT_RET(err != ERR_OK);
497   }
498   err = tcp_output(pcb);
499   EXPECT_RET(err == ERR_OK);
500   /*EXPECT_RET(txcounters.num_tx_calls == 0);
501   EXPECT_RET(txcounters.num_tx_bytes == 0);*/
502   memset(&txcounters, 0, sizeof(txcounters));
503 
504   /* send even more data */
505   err = tcp_write(pcb, data5, sizeof(data5), TCP_WRITE_FLAG_COPY);
506   EXPECT_RET(err == ERR_OK);
507   err = tcp_output(pcb);
508   EXPECT_RET(err == ERR_OK);
509   /* ...and even more data */
510   err = tcp_write(pcb, data5, sizeof(data5), TCP_WRITE_FLAG_COPY);
511   EXPECT_RET(err == ERR_OK);
512   err = tcp_output(pcb);
513   EXPECT_RET(err == ERR_OK);
514   /* ...and even more data */
515   err = tcp_write(pcb, data5, sizeof(data5), TCP_WRITE_FLAG_COPY);
516   EXPECT_RET(err == ERR_OK);
517   err = tcp_output(pcb);
518   EXPECT_RET(err == ERR_OK);
519   /* ...and even more data */
520   err = tcp_write(pcb, data5, sizeof(data5), TCP_WRITE_FLAG_COPY);
521   EXPECT_RET(err == ERR_OK);
522   err = tcp_output(pcb);
523   EXPECT_RET(err == ERR_OK);
524 
525   /* send ACKs for data2 and data3 */
526   p = tcp_create_rx_segment(pcb, NULL, 0, 0, 12, TCP_ACK);
527   EXPECT_RET(p != NULL);
528   test_tcp_input(p, &netif);
529   /*EXPECT_RET(txcounters.num_tx_calls == 0);*/
530 
531   /* ...and even more data */
532   err = tcp_write(pcb, data5, sizeof(data5), TCP_WRITE_FLAG_COPY);
533   EXPECT_RET(err == ERR_OK);
534   err = tcp_output(pcb);
535   EXPECT_RET(err == ERR_OK);
536   /* ...and even more data */
537   err = tcp_write(pcb, data5, sizeof(data5), TCP_WRITE_FLAG_COPY);
538   EXPECT_RET(err == ERR_OK);
539   err = tcp_output(pcb);
540   EXPECT_RET(err == ERR_OK);
541 
542 #if 0
543   /* create expected segment */
544   p1 = tcp_create_rx_segment(pcb, counters.expected_data, data_len, 0, 0, 0);
545   EXPECT_RET(p != NULL);
546   if (p != NULL) {
547     /* pass the segment to tcp_input */
548     test_tcp_input(p, &netif);
549     /* check if counters are as expected */
550     EXPECT_RET(counters.close_calls == 0);
551     EXPECT_RET(counters.recv_calls == 1);
552     EXPECT_RET(counters.recved_bytes == data_len);
553     EXPECT_RET(counters.err_calls == 0);
554   }
555 #endif
556   /* make sure the pcb is freed */
557   EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
558   tcp_abort(pcb);
559   EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
560 }
561 END_TEST
562 
563 static u8_t tx_data[TCP_WND*2];
564 
565 static void
check_seqnos(struct tcp_seg * segs,int num_expected,u32_t * seqnos_expected)566 check_seqnos(struct tcp_seg *segs, int num_expected, u32_t *seqnos_expected)
567 {
568   struct tcp_seg *s = segs;
569   int i;
570   for (i = 0; i < num_expected; i++, s = s->next) {
571     EXPECT_RET(s != NULL);
572     EXPECT(s->tcphdr->seqno == htonl(seqnos_expected[i]));
573   }
574   EXPECT(s == NULL);
575 }
576 
577 /** Send data with sequence numbers that wrap around the u32_t range.
578  * Then, provoke fast retransmission by duplicate ACKs and check that all
579  * segment lists are still properly sorted. */
START_TEST(test_tcp_fast_rexmit_wraparound)580 START_TEST(test_tcp_fast_rexmit_wraparound)
581 {
582   struct netif netif;
583   struct test_tcp_txcounters txcounters;
584   struct test_tcp_counters counters;
585   struct tcp_pcb* pcb;
586   struct pbuf* p;
587   err_t err;
588   size_t i;
589   u16_t sent_total = 0;
590   LWIP_UNUSED_ARG(_i);
591 
592   for (i = 0; i < sizeof(tx_data); i++) {
593     tx_data[i] = (u8_t)i;
594   }
595 
596   /* initialize local vars */
597   test_tcp_init_netif(&netif, &txcounters, &test_local_ip, &test_netmask);
598   memset(&counters, 0, sizeof(counters));
599 
600   /* create and initialize the pcb */
601   tcp_ticks = SEQNO1 - ISS;
602   pcb = test_tcp_new_counters_pcb(&counters);
603   EXPECT_RET(pcb != NULL);
604   tcp_set_state(pcb, ESTABLISHED, &test_local_ip, &test_remote_ip, TEST_LOCAL_PORT, TEST_REMOTE_PORT);
605   pcb->mss = TCP_MSS;
606   /* disable initial congestion window (we don't send a SYN here...) */
607   pcb->cwnd = 2*TCP_MSS;
608   /* start in congestion advoidance */
609   pcb->ssthresh = pcb->cwnd;
610 
611   /* send 6 mss-sized segments */
612   for (i = 0; i < 6; i++) {
613     err = tcp_write(pcb, &tx_data[sent_total], TCP_MSS, TCP_WRITE_FLAG_COPY);
614     EXPECT_RET(err == ERR_OK);
615     sent_total += TCP_MSS;
616   }
617   check_seqnos(pcb->unsent, 6, seqnos);
618   EXPECT(pcb->unacked == NULL);
619   err = tcp_output(pcb);
620   EXPECT(txcounters.num_tx_calls == 2);
621   EXPECT(txcounters.num_tx_bytes == 2 * (TCP_MSS + 40U));
622   memset(&txcounters, 0, sizeof(txcounters));
623 
624   check_seqnos(pcb->unacked, 2, seqnos);
625   check_seqnos(pcb->unsent, 4, &seqnos[2]);
626 
627   /* ACK the first segment */
628   p = tcp_create_rx_segment(pcb, NULL, 0, 0, TCP_MSS, TCP_ACK);
629   test_tcp_input(p, &netif);
630   /* ensure this didn't trigger a retransmission. Only one
631   segment should be transmitted because cwnd opened up by
632   TCP_MSS and a fraction since we are in congestion avoidance */
633   EXPECT(txcounters.num_tx_calls == 1);
634   EXPECT(txcounters.num_tx_bytes == TCP_MSS + 40U);
635   memset(&txcounters, 0, sizeof(txcounters));
636   check_seqnos(pcb->unacked, 2, &seqnos[1]);
637   check_seqnos(pcb->unsent, 3, &seqnos[3]);
638 
639   /* 3 dupacks */
640   EXPECT(pcb->dupacks == 0);
641   p = tcp_create_rx_segment(pcb, NULL, 0, 0, 0, TCP_ACK);
642   test_tcp_input(p, &netif);
643   EXPECT(txcounters.num_tx_calls == 0);
644   EXPECT(pcb->dupacks == 1);
645   p = tcp_create_rx_segment(pcb, NULL, 0, 0, 0, TCP_ACK);
646   test_tcp_input(p, &netif);
647   EXPECT(txcounters.num_tx_calls == 0);
648   EXPECT(pcb->dupacks == 2);
649   /* 3rd dupack -> fast rexmit */
650   p = tcp_create_rx_segment(pcb, NULL, 0, 0, 0, TCP_ACK);
651   test_tcp_input(p, &netif);
652   EXPECT(pcb->dupacks == 3);
653   EXPECT(txcounters.num_tx_calls == 4);
654   memset(&txcounters, 0, sizeof(txcounters));
655   EXPECT(pcb->unsent == NULL);
656   check_seqnos(pcb->unacked, 5, &seqnos[1]);
657 
658   /* make sure the pcb is freed */
659   EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
660   tcp_abort(pcb);
661   EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
662 }
663 END_TEST
664 
665 /** Send data with sequence numbers that wrap around the u32_t range.
666  * Then, provoke RTO retransmission and check that all
667  * segment lists are still properly sorted. */
START_TEST(test_tcp_rto_rexmit_wraparound)668 START_TEST(test_tcp_rto_rexmit_wraparound)
669 {
670   struct netif netif;
671   struct test_tcp_txcounters txcounters;
672   struct test_tcp_counters counters;
673   struct tcp_pcb* pcb;
674   struct tcp_pcb dummy_pcb_for_iss; /* we need this for tcp_next_iss() only */
675   err_t err;
676   size_t i;
677   u16_t sent_total = 0;
678   LWIP_UNUSED_ARG(_i);
679 
680   for (i = 0; i < sizeof(tx_data); i++) {
681     tx_data[i] = (u8_t)i;
682   }
683 
684   /* initialize local vars */
685   test_tcp_init_netif(&netif, &txcounters, &test_local_ip, &test_netmask);
686   memset(&counters, 0, sizeof(counters));
687 
688   /* create and initialize the pcb */
689   tcp_ticks = 0;
690   tcp_ticks = 0 - tcp_next_iss(&dummy_pcb_for_iss);
691   tcp_ticks = SEQNO1 - tcp_next_iss(&dummy_pcb_for_iss);
692   pcb = test_tcp_new_counters_pcb(&counters);
693   EXPECT_RET(pcb != NULL);
694   tcp_set_state(pcb, ESTABLISHED, &test_local_ip, &test_remote_ip, TEST_LOCAL_PORT, TEST_REMOTE_PORT);
695   pcb->mss = TCP_MSS;
696   /* disable initial congestion window (we don't send a SYN here...) */
697   pcb->cwnd = 2*TCP_MSS;
698 
699   /* send 6 mss-sized segments */
700   for (i = 0; i < 6; i++) {
701     err = tcp_write(pcb, &tx_data[sent_total], TCP_MSS, TCP_WRITE_FLAG_COPY);
702     EXPECT_RET(err == ERR_OK);
703     sent_total += TCP_MSS;
704   }
705   check_seqnos(pcb->unsent, 6, seqnos);
706   EXPECT(pcb->unacked == NULL);
707   err = tcp_output(pcb);
708   EXPECT(txcounters.num_tx_calls == 2);
709   EXPECT(txcounters.num_tx_bytes == 2 * (TCP_MSS + 40U));
710   memset(&txcounters, 0, sizeof(txcounters));
711 
712   check_seqnos(pcb->unacked, 2, seqnos);
713   check_seqnos(pcb->unsent, 4, &seqnos[2]);
714 
715   /* call the tcp timer some times */
716   for (i = 0; i < 10; i++) {
717     test_tcp_tmr();
718     EXPECT(txcounters.num_tx_calls == 0);
719   }
720   /* 11th call to tcp_tmr: RTO rexmit fires */
721   test_tcp_tmr();
722   EXPECT(txcounters.num_tx_calls == 1);
723   check_seqnos(pcb->unacked, 1, seqnos);
724   check_seqnos(pcb->unsent, 5, &seqnos[1]);
725 
726   /* fake greater cwnd */
727   pcb->cwnd = pcb->snd_wnd;
728   /* send more data */
729   err = tcp_output(pcb);
730   EXPECT(err == ERR_OK);
731   /* check queues are sorted */
732   EXPECT(pcb->unsent == NULL);
733   check_seqnos(pcb->unacked, 6, seqnos);
734 
735   /* make sure the pcb is freed */
736   EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
737   tcp_abort(pcb);
738   EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
739 }
740 END_TEST
741 
742 /** Provoke fast retransmission by duplicate ACKs and then recover by ACKing all sent data.
743  * At the end, send more data. */
test_tcp_tx_full_window_lost(u8_t zero_window_probe_from_unsent)744 static void test_tcp_tx_full_window_lost(u8_t zero_window_probe_from_unsent)
745 {
746   struct netif netif;
747   struct test_tcp_txcounters txcounters;
748   struct test_tcp_counters counters;
749   struct tcp_pcb* pcb;
750   struct pbuf *p;
751   err_t err;
752   size_t i;
753   u16_t sent_total;
754   u8_t expected = 0xFE;
755 
756   for (i = 0; i < sizeof(tx_data); i++) {
757     u8_t d = (u8_t)i;
758     if (d == 0xFE) {
759       d = 0xF0;
760     }
761     tx_data[i] = d;
762   }
763   if (zero_window_probe_from_unsent) {
764     tx_data[TCP_WND] = expected;
765   } else {
766     tx_data[0] = expected;
767   }
768 
769   /* initialize local vars */
770   test_tcp_init_netif(&netif, &txcounters, &test_local_ip, &test_netmask);
771   memset(&counters, 0, sizeof(counters));
772 
773   /* create and initialize the pcb */
774   pcb = test_tcp_new_counters_pcb(&counters);
775   EXPECT_RET(pcb != NULL);
776   tcp_set_state(pcb, ESTABLISHED, &test_local_ip, &test_remote_ip, TEST_LOCAL_PORT, TEST_REMOTE_PORT);
777   pcb->mss = TCP_MSS;
778   /* disable initial congestion window (we don't send a SYN here...) */
779   pcb->cwnd = pcb->snd_wnd;
780 
781   /* send a full window (minus 1 packets) of TCP data in MSS-sized chunks */
782   sent_total = 0;
783   if ((TCP_WND - TCP_MSS) % TCP_MSS != 0) {
784     u16_t initial_data_len = (TCP_WND - TCP_MSS) % TCP_MSS;
785     err = tcp_write(pcb, &tx_data[sent_total], initial_data_len, TCP_WRITE_FLAG_COPY);
786     EXPECT_RET(err == ERR_OK);
787     err = tcp_output(pcb);
788     EXPECT_RET(err == ERR_OK);
789     EXPECT(txcounters.num_tx_calls == 1);
790     EXPECT(txcounters.num_tx_bytes == initial_data_len + 40U);
791     memset(&txcounters, 0, sizeof(txcounters));
792     sent_total += initial_data_len;
793   }
794   for (; sent_total < (TCP_WND - TCP_MSS); sent_total += TCP_MSS) {
795     err = tcp_write(pcb, &tx_data[sent_total], TCP_MSS, TCP_WRITE_FLAG_COPY);
796     EXPECT_RET(err == ERR_OK);
797     err = tcp_output(pcb);
798     EXPECT_RET(err == ERR_OK);
799     EXPECT(txcounters.num_tx_calls == 1);
800     EXPECT(txcounters.num_tx_bytes == TCP_MSS + 40U);
801     memset(&txcounters, 0, sizeof(txcounters));
802   }
803   EXPECT(sent_total == (TCP_WND - TCP_MSS));
804 
805   /* now ACK the packet before the first */
806   p = tcp_create_rx_segment(pcb, NULL, 0, 0, 0, TCP_ACK);
807   test_tcp_input(p, &netif);
808   /* ensure this didn't trigger a retransmission */
809   EXPECT(txcounters.num_tx_calls == 0);
810   EXPECT(txcounters.num_tx_bytes == 0);
811 
812   EXPECT(pcb->persist_backoff == 0);
813   /* send the last packet, now a complete window has been sent */
814   err = tcp_write(pcb, &tx_data[sent_total], TCP_MSS, TCP_WRITE_FLAG_COPY);
815   sent_total += TCP_MSS;
816   EXPECT_RET(err == ERR_OK);
817   err = tcp_output(pcb);
818   EXPECT_RET(err == ERR_OK);
819   EXPECT(txcounters.num_tx_calls == 1);
820   EXPECT(txcounters.num_tx_bytes == TCP_MSS + 40U);
821   memset(&txcounters, 0, sizeof(txcounters));
822   EXPECT(pcb->persist_backoff == 0);
823 
824   if (zero_window_probe_from_unsent) {
825     /* ACK all data but close the TX window */
826     p = tcp_create_rx_segment_wnd(pcb, NULL, 0, 0, TCP_WND, TCP_ACK, 0);
827     test_tcp_input(p, &netif);
828     /* ensure this didn't trigger any transmission */
829     EXPECT(txcounters.num_tx_calls == 0);
830     EXPECT(txcounters.num_tx_bytes == 0);
831     /* window is completely full, but persist timer is off since send buffer is empty */
832     EXPECT(pcb->snd_wnd == 0);
833     EXPECT(pcb->persist_backoff == 0);
834   }
835 
836   /* send one byte more (out of window) -> persist timer starts */
837   err = tcp_write(pcb, &tx_data[sent_total], 1, TCP_WRITE_FLAG_COPY);
838   EXPECT_RET(err == ERR_OK);
839   err = tcp_output(pcb);
840   EXPECT_RET(err == ERR_OK);
841   EXPECT(txcounters.num_tx_calls == 0);
842   EXPECT(txcounters.num_tx_bytes == 0);
843   memset(&txcounters, 0, sizeof(txcounters));
844   if (!zero_window_probe_from_unsent) {
845     /* no persist timer unless a zero window announcement has been received */
846     EXPECT(pcb->persist_backoff == 0);
847   } else {
848     EXPECT(pcb->persist_backoff == 1);
849 
850     /* call tcp_timer some more times to let persist timer count up */
851     for (i = 0; i < 4; i++) {
852       test_tcp_tmr();
853       EXPECT(txcounters.num_tx_calls == 0);
854       EXPECT(txcounters.num_tx_bytes == 0);
855     }
856 
857     /* this should trigger the zero-window-probe */
858     txcounters.copy_tx_packets = 1;
859     test_tcp_tmr();
860     txcounters.copy_tx_packets = 0;
861     EXPECT(txcounters.num_tx_calls == 1);
862     EXPECT(txcounters.num_tx_bytes == 1 + 40U);
863     EXPECT(txcounters.tx_packets != NULL);
864     if (txcounters.tx_packets != NULL) {
865       u8_t sent;
866       u16_t ret;
867       ret = pbuf_copy_partial(txcounters.tx_packets, &sent, 1, 40U);
868       EXPECT(ret == 1);
869       EXPECT(sent == expected);
870     }
871     if (txcounters.tx_packets != NULL) {
872       pbuf_free(txcounters.tx_packets);
873       txcounters.tx_packets = NULL;
874     }
875   }
876 
877   /* make sure the pcb is freed */
878   EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
879   tcp_abort(pcb);
880   EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
881 }
882 
START_TEST(test_tcp_tx_full_window_lost_from_unsent)883 START_TEST(test_tcp_tx_full_window_lost_from_unsent)
884 {
885   LWIP_UNUSED_ARG(_i);
886   test_tcp_tx_full_window_lost(1);
887 }
888 END_TEST
889 
START_TEST(test_tcp_tx_full_window_lost_from_unacked)890 START_TEST(test_tcp_tx_full_window_lost_from_unacked)
891 {
892   LWIP_UNUSED_ARG(_i);
893   test_tcp_tx_full_window_lost(0);
894 }
895 END_TEST
896 
897 /** Send data, provoke retransmission and then add data to a segment
898  * that already has been sent before. */
START_TEST(test_tcp_retx_add_to_sent)899 START_TEST(test_tcp_retx_add_to_sent)
900 {
901   struct netif netif;
902   struct test_tcp_txcounters txcounters;
903   struct test_tcp_counters counters;
904   struct tcp_pcb* pcb;
905   struct pbuf* p;
906   char data1a[] = {  1,  2,  3};
907   char data1b[] = {  4};
908   char data2a[] = {  5,  6,  7,  8};
909   char data2b[] = {  5,  6,  7};
910   char data3[] = {  9, 10, 11, 12, 12};
911   char data4[] = { 13, 14, 15, 16,17};
912   err_t err;
913   int i;
914   LWIP_UNUSED_ARG(_i);
915 
916   /* initialize local vars */
917   test_tcp_init_netif(&netif, &txcounters, &test_local_ip, &test_netmask);
918   memset(&counters, 0, sizeof(counters));
919 
920   /* create and initialize the pcb */
921   pcb = test_tcp_new_counters_pcb(&counters);
922   EXPECT_RET(pcb != NULL);
923   tcp_set_state(pcb, ESTABLISHED, &test_local_ip, &test_remote_ip, TEST_LOCAL_PORT, TEST_REMOTE_PORT);
924   pcb->mss = TCP_MSS;
925   /* disable initial congestion window (we don't send a SYN here...) */
926   pcb->cwnd = pcb->snd_wnd;
927 
928   /* send data1 */
929   err = tcp_write(pcb, data1a, sizeof(data1a), TCP_WRITE_FLAG_COPY);
930   EXPECT_RET(err == ERR_OK);
931   err = tcp_write(pcb, data1b, sizeof(data1b), TCP_WRITE_FLAG_COPY);
932   EXPECT_RET(err == ERR_OK);
933   err = tcp_output(pcb);
934   EXPECT_RET(err == ERR_OK);
935   EXPECT_RET(txcounters.num_tx_calls == 1);
936   EXPECT_RET(txcounters.num_tx_bytes == sizeof(data1a) + sizeof(data1b) + sizeof(struct tcp_hdr) + sizeof(struct ip_hdr));
937   memset(&txcounters, 0, sizeof(txcounters));
938  /* "recv" ACK for data1 */
939   p = tcp_create_rx_segment(pcb, NULL, 0, 0, 4, TCP_ACK);
940   EXPECT_RET(p != NULL);
941   test_tcp_input(p, &netif);
942   EXPECT_RET(txcounters.num_tx_calls == 0);
943   EXPECT_RET(pcb->unacked == NULL);
944   /* send data2 */
945   err = tcp_write(pcb, data2a, sizeof(data2a), TCP_WRITE_FLAG_COPY);
946   EXPECT_RET(err == ERR_OK);
947   err = tcp_write(pcb, data2b, sizeof(data2b), TCP_WRITE_FLAG_COPY);
948   EXPECT_RET(err == ERR_OK);
949   err = tcp_output(pcb);
950   EXPECT_RET(err == ERR_OK);
951   EXPECT_RET(txcounters.num_tx_calls == 1);
952   EXPECT_RET(txcounters.num_tx_bytes == sizeof(data2a) + sizeof(data2b) + sizeof(struct tcp_hdr) + sizeof(struct ip_hdr));
953   memset(&txcounters, 0, sizeof(txcounters));
954   /* send data3 */
955   err = tcp_write(pcb, data3, sizeof(data3), TCP_WRITE_FLAG_COPY);
956   EXPECT_RET(err == ERR_OK);
957   err = tcp_output(pcb);
958   EXPECT_RET(err == ERR_OK);
959   EXPECT_RET(txcounters.num_tx_calls == 0);
960   EXPECT_RET(txcounters.num_tx_bytes == 0);
961   memset(&txcounters, 0, sizeof(txcounters));
962 
963   /* data3 not sent yet (nagle) */
964   EXPECT_RET(pcb->unacked != NULL);
965   EXPECT_RET(pcb->unsent != NULL);
966 
967   /* disable nagle for this test so data to sent segment can be added below... */
968   tcp_nagle_disable(pcb);
969 
970   /* call the tcp timer some times */
971   for (i = 0; i < 20; i++) {
972     test_tcp_tmr();
973     if (txcounters.num_tx_calls != 0) {
974       break;
975     }
976   }
977   /* data3 sent */
978   EXPECT_RET(txcounters.num_tx_calls == 1);
979   EXPECT_RET(txcounters.num_tx_bytes == sizeof(data3) + sizeof(struct tcp_hdr) + sizeof(struct ip_hdr));
980   EXPECT_RET(pcb->unacked != NULL);
981   EXPECT_RET(pcb->unsent == NULL);
982   memset(&txcounters, 0, sizeof(txcounters));
983 
984   tcp_nagle_enable(pcb);
985 
986   /* call the tcp timer some times */
987   for (i = 0; i < 20; i++) {
988     test_tcp_tmr();
989     if (txcounters.num_tx_calls != 0) {
990       break;
991     }
992   }
993   /* RTO: rexmit of data2 */
994   EXPECT_RET(txcounters.num_tx_calls == 1);
995   EXPECT_RET(txcounters.num_tx_bytes == sizeof(data2a) + sizeof(data2b) + sizeof(struct tcp_hdr) + sizeof(struct ip_hdr));
996   EXPECT_RET(pcb->unacked != NULL);
997   EXPECT_RET(pcb->unsent != NULL);
998   memset(&txcounters, 0, sizeof(txcounters));
999 
1000   /* send data4 */
1001   err = tcp_write(pcb, data4, sizeof(data4), TCP_WRITE_FLAG_COPY);
1002   EXPECT_RET(err == ERR_OK);
1003   /* disable nagle for this test so data to transmit without further ACKs... */
1004   tcp_nagle_disable(pcb);
1005   err = tcp_output(pcb);
1006   EXPECT_RET(err == ERR_OK);
1007   /* nagle enabled, no tx calls */
1008   EXPECT_RET(txcounters.num_tx_calls == 1);
1009   EXPECT_RET(txcounters.num_tx_bytes == sizeof(data3) + sizeof(data4) + sizeof(struct tcp_hdr) + sizeof(struct ip_hdr));
1010   memset(&txcounters, 0, sizeof(txcounters));
1011   /* make sure the pcb is freed */
1012   EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
1013   tcp_abort(pcb);
1014   EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
1015 }
1016 END_TEST
1017 
START_TEST(test_tcp_rto_tracking)1018 START_TEST(test_tcp_rto_tracking)
1019 {
1020   struct netif netif;
1021   struct test_tcp_txcounters txcounters;
1022   struct test_tcp_counters counters;
1023   struct tcp_pcb* pcb;
1024   struct pbuf* p;
1025   err_t err;
1026   size_t i;
1027   u16_t sent_total = 0;
1028   LWIP_UNUSED_ARG(_i);
1029 
1030   for (i = 0; i < sizeof(tx_data); i++) {
1031     tx_data[i] = (u8_t)i;
1032   }
1033 
1034   /* initialize local vars */
1035   test_tcp_init_netif(&netif, &txcounters, &test_local_ip, &test_netmask);
1036   memset(&counters, 0, sizeof(counters));
1037 
1038   /* create and initialize the pcb */
1039   tcp_ticks = SEQNO1 - ISS;
1040   pcb = test_tcp_new_counters_pcb(&counters);
1041   EXPECT_RET(pcb != NULL);
1042   tcp_set_state(pcb, ESTABLISHED, &test_local_ip, &test_remote_ip, TEST_LOCAL_PORT, TEST_REMOTE_PORT);
1043   pcb->mss = TCP_MSS;
1044   /* Set congestion window large enough to send all our segments */
1045   pcb->cwnd = 5*TCP_MSS;
1046 
1047   /* send 5 mss-sized segments */
1048   for (i = 0; i < 5; i++) {
1049     err = tcp_write(pcb, &tx_data[sent_total], TCP_MSS, TCP_WRITE_FLAG_COPY);
1050     EXPECT_RET(err == ERR_OK);
1051     sent_total += TCP_MSS;
1052   }
1053   check_seqnos(pcb->unsent, 5, seqnos);
1054   EXPECT(pcb->unacked == NULL);
1055   err = tcp_output(pcb);
1056   EXPECT(txcounters.num_tx_calls == 5);
1057   EXPECT(txcounters.num_tx_bytes == 5 * (TCP_MSS + 40U));
1058   memset(&txcounters, 0, sizeof(txcounters));
1059   /* Check all 5 are in-flight */
1060   EXPECT(pcb->unsent == NULL);
1061   check_seqnos(pcb->unacked, 5, seqnos);
1062 
1063   /* Force us into retransmisson timeout */
1064   while (!(pcb->flags & TF_RTO)) {
1065     test_tcp_tmr();
1066   }
1067   /* Ensure 4 remaining segments are back on unsent, ready for retransmission */
1068   check_seqnos(pcb->unsent, 4, &seqnos[1]);
1069   /* Ensure 1st segment is on unacked (already retransmitted) */
1070   check_seqnos(pcb->unacked, 1, seqnos);
1071   EXPECT(txcounters.num_tx_calls == 1);
1072   EXPECT(txcounters.num_tx_bytes == TCP_MSS + 40U);
1073   memset(&txcounters, 0, sizeof(txcounters));
1074   /* Ensure rto_end points to next byte */
1075   EXPECT(pcb->rto_end == seqnos[5]);
1076   EXPECT(pcb->rto_end == pcb->snd_nxt);
1077   /* Check cwnd was reset */
1078   EXPECT(pcb->cwnd == pcb->mss);
1079 
1080   /* Add another segment to send buffer which is outside of RTO */
1081   err = tcp_write(pcb, &tx_data[sent_total], TCP_MSS, TCP_WRITE_FLAG_COPY);
1082   EXPECT_RET(err == ERR_OK);
1083   sent_total += TCP_MSS;
1084   check_seqnos(pcb->unsent, 5, &seqnos[1]);
1085   /* Ensure no new data was sent */
1086   EXPECT(txcounters.num_tx_calls == 0);
1087   EXPECT(txcounters.num_tx_bytes == 0);
1088   EXPECT(pcb->rto_end == pcb->snd_nxt);
1089 
1090   /* ACK first segment */
1091   p = tcp_create_rx_segment(pcb, NULL, 0, 0, TCP_MSS, TCP_ACK);
1092   test_tcp_input(p, &netif);
1093   /* Next two retranmissions should go out, due to cwnd in slow start */
1094   EXPECT(txcounters.num_tx_calls == 2);
1095   EXPECT(txcounters.num_tx_bytes == 2 * (TCP_MSS + 40U));
1096   memset(&txcounters, 0, sizeof(txcounters));
1097   check_seqnos(pcb->unacked, 2, &seqnos[1]);
1098   check_seqnos(pcb->unsent, 3, &seqnos[3]);
1099   /* RTO should still be marked */
1100   EXPECT(pcb->flags & TF_RTO);
1101   /* cwnd should have only grown by 1 MSS */
1102   EXPECT(pcb->cwnd == (tcpwnd_size_t)(2 * pcb->mss));
1103   /* Ensure no new data was sent */
1104   EXPECT(pcb->rto_end == pcb->snd_nxt);
1105 
1106   /* ACK the next two segments */
1107   p = tcp_create_rx_segment(pcb, NULL, 0, 0, 2*TCP_MSS, TCP_ACK);
1108   test_tcp_input(p, &netif);
1109   /* Final 2 retransmissions and 1 new data should go out */
1110   EXPECT(txcounters.num_tx_calls == 3);
1111   EXPECT(txcounters.num_tx_bytes == 3 * (TCP_MSS + 40U));
1112   memset(&txcounters, 0, sizeof(txcounters));
1113   check_seqnos(pcb->unacked, 3, &seqnos[3]);
1114   EXPECT(pcb->unsent == NULL);
1115   /* RTO should still be marked */
1116   EXPECT(pcb->flags & TF_RTO);
1117   /* cwnd should have only grown by 1 MSS */
1118   EXPECT(pcb->cwnd == (tcpwnd_size_t)(3 * pcb->mss));
1119   /* snd_nxt should have been advanced past rto_end */
1120   EXPECT(TCP_SEQ_GT(pcb->snd_nxt, pcb->rto_end));
1121 
1122   /* ACK the next two segments, finishing our RTO, leaving new segment unacked */
1123   p = tcp_create_rx_segment(pcb, NULL, 0, 0, 2*TCP_MSS, TCP_ACK);
1124   test_tcp_input(p, &netif);
1125   EXPECT(!(pcb->flags & TF_RTO));
1126   check_seqnos(pcb->unacked, 1, &seqnos[5]);
1127   /* We should be in ABC congestion avoidance, so no change in cwnd */
1128   EXPECT(pcb->cwnd == (tcpwnd_size_t)(3 * pcb->mss));
1129   EXPECT(pcb->cwnd >= pcb->ssthresh);
1130   /* Ensure ABC congestion avoidance is tracking bytes acked */
1131   EXPECT(pcb->bytes_acked == (tcpwnd_size_t)(2 * pcb->mss));
1132 
1133   /* make sure the pcb is freed */
1134   EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
1135   tcp_abort(pcb);
1136   EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
1137 }
1138 END_TEST
1139 
test_tcp_rto_timeout_impl(int link_down)1140 static void test_tcp_rto_timeout_impl(int link_down)
1141 {
1142   struct netif netif;
1143   struct test_tcp_txcounters txcounters;
1144   struct test_tcp_counters counters;
1145   struct tcp_pcb *pcb, *cur;
1146   err_t err;
1147   size_t i;
1148   const size_t max_wait_ctr = 1024 * 1024;
1149 
1150   /* Setup data for a single segment */
1151   for (i = 0; i < TCP_MSS; i++) {
1152     tx_data[i] = (u8_t)i;
1153   }
1154 
1155   /* initialize local vars */
1156   test_tcp_init_netif(&netif, &txcounters, &test_local_ip, &test_netmask);
1157   memset(&counters, 0, sizeof(counters));
1158 
1159   /* create and initialize the pcb */
1160   tcp_ticks = SEQNO1 - ISS;
1161   pcb = test_tcp_new_counters_pcb(&counters);
1162   EXPECT_RET(pcb != NULL);
1163   tcp_set_state(pcb, ESTABLISHED, &test_local_ip, &test_remote_ip, TEST_LOCAL_PORT, TEST_REMOTE_PORT);
1164   pcb->mss = TCP_MSS;
1165   pcb->cwnd = TCP_MSS;
1166 
1167   /* send our segment */
1168   err = tcp_write(pcb, &tx_data[0], TCP_MSS, TCP_WRITE_FLAG_COPY);
1169   EXPECT_RET(err == ERR_OK);
1170   err = tcp_output(pcb);
1171   EXPECT(txcounters.num_tx_calls == 1);
1172   EXPECT(txcounters.num_tx_bytes == 1 * (TCP_MSS + 40U));
1173   memset(&txcounters, 0, sizeof(txcounters));
1174 
1175   /* ensure no errors have been recorded */
1176   EXPECT(counters.err_calls == 0);
1177   EXPECT(counters.last_err == ERR_OK);
1178 
1179   /* Force us into retransmisson timeout */
1180    for (i = 0; !(pcb->flags & TF_RTO) && i < max_wait_ctr; i++) {
1181     test_tcp_tmr();
1182   }
1183    EXPECT(i < max_wait_ctr);
1184 
1185   /* check first rexmit */
1186   EXPECT(pcb->nrtx == 1);
1187   EXPECT(txcounters.num_tx_calls == 1);
1188   EXPECT(txcounters.num_tx_bytes == 1 * (TCP_MSS + 40U));
1189 
1190   /* still no error expected */
1191   EXPECT(counters.err_calls == 0);
1192   EXPECT(counters.last_err == ERR_OK);
1193 
1194   if (link_down) {
1195     netif_set_link_down(&netif);
1196   }
1197 
1198   /* keep running the timer till we hit our maximum RTO */
1199   for (i = 0;  counters.last_err == ERR_OK && i < max_wait_ctr; i++) {
1200     test_tcp_tmr();
1201   }
1202   EXPECT(i < max_wait_ctr);
1203 
1204   /* check number of retransmissions */
1205   if (link_down) {
1206     EXPECT(txcounters.num_tx_calls == 1);
1207     EXPECT(txcounters.num_tx_bytes == 1 * (TCP_MSS + 40U));
1208   } else {
1209     EXPECT(txcounters.num_tx_calls == TCP_MAXRTX);
1210     EXPECT(txcounters.num_tx_bytes == TCP_MAXRTX * (TCP_MSS + 40U));
1211   }
1212 
1213   /* check the connection (pcb) has been aborted */
1214   EXPECT(counters.err_calls == 1);
1215   EXPECT(counters.last_err == ERR_ABRT);
1216   /* check our pcb is no longer active */
1217   for (cur = tcp_active_pcbs; cur != NULL; cur = cur->next) {
1218     EXPECT(cur != pcb);
1219   }
1220   EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
1221 }
1222 
START_TEST(test_tcp_rto_timeout)1223 START_TEST(test_tcp_rto_timeout)
1224 {
1225   LWIP_UNUSED_ARG(_i);
1226   test_tcp_rto_timeout_impl(0);
1227 }
1228 END_TEST
1229 
START_TEST(test_tcp_rto_timeout_link_down)1230 START_TEST(test_tcp_rto_timeout_link_down)
1231 {
1232   LWIP_UNUSED_ARG(_i);
1233   test_tcp_rto_timeout_impl(1);
1234 }
1235 END_TEST
1236 
test_tcp_rto_timeout_syn_sent_impl(int link_down)1237 static void test_tcp_rto_timeout_syn_sent_impl(int link_down)
1238 {
1239   struct netif netif;
1240   struct test_tcp_txcounters txcounters;
1241   struct test_tcp_counters counters;
1242   struct tcp_pcb *pcb, *cur;
1243   err_t err;
1244   size_t i;
1245   const size_t max_wait_ctr = 1024 * 1024;
1246   const u16_t tcp_syn_opts_len = LWIP_TCP_OPT_LENGTH(TF_SEG_OPTS_MSS|TF_SEG_OPTS_WND_SCALE|TF_SEG_OPTS_SACK_PERM|TF_SEG_OPTS_TS);
1247 
1248   /* Setup data for a single segment */
1249   for (i = 0; i < TCP_MSS; i++) {
1250     tx_data[i] = (u8_t)i;
1251   }
1252 
1253   /* initialize local vars */
1254   test_tcp_init_netif(&netif, &txcounters, &test_local_ip, &test_netmask);
1255   memset(&counters, 0, sizeof(counters));
1256 
1257   /* create and initialize the pcb */
1258   tcp_ticks = SEQNO1 - ISS;
1259   pcb = test_tcp_new_counters_pcb(&counters);
1260   EXPECT_RET(pcb != NULL);
1261   err = tcp_connect(pcb, &netif.gw, 123, NULL);
1262   EXPECT_RET(err == ERR_OK);
1263   EXPECT_RET(pcb->state == SYN_SENT);
1264   EXPECT(txcounters.num_tx_calls == 1);
1265   EXPECT(txcounters.num_tx_bytes == 40U + tcp_syn_opts_len);
1266 
1267   /* ensure no errors have been recorded */
1268   EXPECT(counters.err_calls == 0);
1269   EXPECT(counters.last_err == ERR_OK);
1270 
1271   txcounters.num_tx_calls = 0;
1272   txcounters.num_tx_bytes = 0;
1273 
1274   /* Force us into retransmisson timeout */
1275   for (i = 0; !(pcb->flags & TF_RTO) && i < max_wait_ctr; i++) {
1276     test_tcp_tmr();
1277   }
1278   EXPECT(i < max_wait_ctr);
1279 
1280   /* check first rexmit */
1281   EXPECT(pcb->nrtx == 1);
1282   EXPECT(txcounters.num_tx_calls == 1);
1283   EXPECT(txcounters.num_tx_bytes == 40U + tcp_syn_opts_len); /* 40: headers; >=: options */
1284 
1285   /* still no error expected */
1286   EXPECT(counters.err_calls == 0);
1287   EXPECT(counters.last_err == ERR_OK);
1288 
1289   if (link_down) {
1290     /* set link down and check what happens to the RTO counter */
1291     netif_set_link_down(&netif);
1292   }
1293 
1294   /* keep running the timer till we hit our maximum RTO */
1295   for (i = 0;  counters.last_err == ERR_OK && i < max_wait_ctr; i++) {
1296     test_tcp_tmr();
1297   }
1298   EXPECT(i < max_wait_ctr);
1299 
1300   /* check number of retransmissions */
1301   if (link_down) {
1302     EXPECT(txcounters.num_tx_calls == 1);
1303     EXPECT(txcounters.num_tx_bytes == 40U + tcp_syn_opts_len);
1304   } else {
1305     EXPECT(txcounters.num_tx_calls == TCP_SYNMAXRTX);
1306     EXPECT(txcounters.num_tx_bytes == TCP_SYNMAXRTX * (tcp_syn_opts_len + 40U));
1307   }
1308 
1309   /* check the connection (pcb) has been aborted */
1310   EXPECT(counters.err_calls == 1);
1311   EXPECT(counters.last_err == ERR_ABRT);
1312   /* check our pcb is no longer active */
1313   for (cur = tcp_active_pcbs; cur != NULL; cur = cur->next) {
1314     EXPECT(cur != pcb);
1315   }
1316   EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
1317 }
1318 
START_TEST(test_tcp_rto_timeout_syn_sent)1319 START_TEST(test_tcp_rto_timeout_syn_sent)
1320 {
1321   LWIP_UNUSED_ARG(_i);
1322   test_tcp_rto_timeout_syn_sent_impl(0);
1323 }
1324 END_TEST
1325 
START_TEST(test_tcp_rto_timeout_syn_sent_link_down)1326 START_TEST(test_tcp_rto_timeout_syn_sent_link_down)
1327 {
1328   LWIP_UNUSED_ARG(_i);
1329   test_tcp_rto_timeout_syn_sent_impl(1);
1330 }
1331 END_TEST
1332 
test_tcp_zwp_timeout_impl(int link_down)1333 static void test_tcp_zwp_timeout_impl(int link_down)
1334 {
1335   struct netif netif;
1336   struct test_tcp_txcounters txcounters;
1337   struct test_tcp_counters counters;
1338   struct tcp_pcb *pcb, *cur;
1339   struct pbuf* p;
1340   err_t err;
1341   size_t i;
1342 
1343   /* Setup data for two segments */
1344   for (i = 0; i < 2*TCP_MSS; i++) {
1345     tx_data[i] = (u8_t)i;
1346   }
1347 
1348   /* initialize local vars */
1349   test_tcp_init_netif(&netif, &txcounters, &test_local_ip, &test_netmask);
1350   memset(&counters, 0, sizeof(counters));
1351 
1352   /* create and initialize the pcb */
1353   tcp_ticks = SEQNO1 - ISS;
1354   pcb = test_tcp_new_counters_pcb(&counters);
1355   EXPECT_RET(pcb != NULL);
1356   tcp_set_state(pcb, ESTABLISHED, &test_local_ip, &test_remote_ip, TEST_LOCAL_PORT, TEST_REMOTE_PORT);
1357   pcb->mss = TCP_MSS;
1358   pcb->cwnd = TCP_MSS;
1359 
1360   /* send first segment */
1361   err = tcp_write(pcb, &tx_data[0], TCP_MSS, TCP_WRITE_FLAG_COPY);
1362   EXPECT(err == ERR_OK);
1363   err = tcp_output(pcb);
1364   EXPECT(err == ERR_OK);
1365 
1366   /* verify segment is in-flight */
1367   EXPECT(pcb->unsent == NULL);
1368   check_seqnos(pcb->unacked, 1, seqnos);
1369   EXPECT(txcounters.num_tx_calls == 1);
1370   EXPECT(txcounters.num_tx_bytes == 1 * (TCP_MSS + 40U));
1371   memset(&txcounters, 0, sizeof(txcounters));
1372 
1373   /* ACK the segment and close the TX window */
1374   p = tcp_create_rx_segment_wnd(pcb, NULL, 0, 0, TCP_MSS, TCP_ACK, 0);
1375   test_tcp_input(p, &netif);
1376   EXPECT(pcb->unacked == NULL);
1377   EXPECT(pcb->unsent == NULL);
1378   /* send buffer empty, persist should be off */
1379   EXPECT(pcb->persist_backoff == 0);
1380   EXPECT(pcb->snd_wnd == 0);
1381 
1382   /* send second segment, should be buffered */
1383   err = tcp_write(pcb, &tx_data[TCP_MSS], TCP_MSS, TCP_WRITE_FLAG_COPY);
1384   EXPECT(err == ERR_OK);
1385   err = tcp_output(pcb);
1386   EXPECT(err == ERR_OK);
1387 
1388   /* ensure it is buffered and persist timer started */
1389   EXPECT(pcb->unacked == NULL);
1390   check_seqnos(pcb->unsent, 1, &seqnos[1]);
1391   EXPECT(txcounters.num_tx_calls == 0);
1392   EXPECT(txcounters.num_tx_bytes == 0);
1393   EXPECT(pcb->persist_backoff == 1);
1394 
1395   /* ensure no errors have been recorded */
1396   EXPECT(counters.err_calls == 0);
1397   EXPECT(counters.last_err == ERR_OK);
1398 
1399   /* run timer till first probe */
1400   EXPECT(pcb->persist_probe == 0);
1401   while (pcb->persist_probe == 0) {
1402     test_tcp_tmr();
1403   }
1404   EXPECT(txcounters.num_tx_calls == 1);
1405   EXPECT(txcounters.num_tx_bytes == (1 + 40U));
1406   memset(&txcounters, 0, sizeof(txcounters));
1407 
1408   /* respond to probe with remote's current SEQ, ACK, and zero-window */
1409   p = tcp_create_rx_segment_wnd(pcb, NULL, 0, 0, 0, TCP_ACK, 0);
1410   test_tcp_input(p, &netif);
1411   /* ensure zero-window is still active, but probe count reset */
1412   EXPECT(pcb->persist_backoff > 1);
1413   EXPECT(pcb->persist_probe == 0);
1414   EXPECT(pcb->snd_wnd == 0);
1415 
1416   /* ensure no errors have been recorded */
1417   EXPECT(counters.err_calls == 0);
1418   EXPECT(counters.last_err == ERR_OK);
1419 
1420   if (link_down) {
1421     netif_set_link_down(&netif);
1422   }
1423 
1424   /* now run the timer till we hit our maximum probe count */
1425   while (counters.last_err == ERR_OK) {
1426     test_tcp_tmr();
1427   }
1428 
1429   if (link_down) {
1430     EXPECT(txcounters.num_tx_calls == 0);
1431     EXPECT(txcounters.num_tx_bytes == 0);
1432   } else {
1433     /* check maximum number of 1 byte probes were sent */
1434     EXPECT(txcounters.num_tx_calls == TCP_MAXRTX);
1435     EXPECT(txcounters.num_tx_bytes == TCP_MAXRTX * (1 + 40U));
1436   }
1437 
1438   /* check the connection (pcb) has been aborted */
1439   EXPECT(counters.err_calls == 1);
1440   EXPECT(counters.last_err == ERR_ABRT);
1441   /* check our pcb is no longer active */
1442   for (cur = tcp_active_pcbs; cur != NULL; cur = cur->next) {
1443     EXPECT(cur != pcb);
1444   }
1445   EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
1446 }
1447 
START_TEST(test_tcp_zwp_timeout)1448 START_TEST(test_tcp_zwp_timeout)
1449 {
1450   LWIP_UNUSED_ARG(_i);
1451   test_tcp_zwp_timeout_impl(0);
1452 }
1453 END_TEST
1454 
START_TEST(test_tcp_zwp_timeout_link_down)1455 START_TEST(test_tcp_zwp_timeout_link_down)
1456 {
1457   LWIP_UNUSED_ARG(_i);
1458   test_tcp_zwp_timeout_impl(1);
1459 }
1460 END_TEST
1461 
START_TEST(test_tcp_persist_split)1462 START_TEST(test_tcp_persist_split)
1463 {
1464   struct netif netif;
1465   struct test_tcp_txcounters txcounters;
1466   struct test_tcp_counters counters;
1467   struct tcp_pcb *pcb;
1468   struct pbuf* p;
1469   err_t err;
1470   size_t i;
1471   LWIP_UNUSED_ARG(_i);
1472 
1473   /* Setup data for four segments */
1474   for (i = 0; i < 4 * TCP_MSS; i++) {
1475     tx_data[i] = (u8_t)i;
1476   }
1477 
1478   /* initialize local vars */
1479   test_tcp_init_netif(&netif, &txcounters, &test_local_ip, &test_netmask);
1480   memset(&counters, 0, sizeof(counters));
1481 
1482   /* create and initialize the pcb */
1483   tcp_ticks = SEQNO1 - ISS;
1484   pcb = test_tcp_new_counters_pcb(&counters);
1485   EXPECT_RET(pcb != NULL);
1486   tcp_set_state(pcb, ESTABLISHED, &test_local_ip, &test_remote_ip, TEST_LOCAL_PORT, TEST_REMOTE_PORT);
1487   pcb->mss = TCP_MSS;
1488   /* set window to three segments */
1489   pcb->cwnd = 3 * TCP_MSS;
1490   pcb->snd_wnd = 3 * TCP_MSS;
1491   pcb->snd_wnd_max = 3 * TCP_MSS;
1492 
1493   /* send four segments. Fourth should stay buffered and is a 3/4 MSS segment to
1494      get coverage on the oversized segment case */
1495   err = tcp_write(pcb, &tx_data[0], (3 * TCP_MSS) + (TCP_MSS - (TCP_MSS / 4)), TCP_WRITE_FLAG_COPY);
1496   EXPECT(err == ERR_OK);
1497   err = tcp_output(pcb);
1498   EXPECT(err == ERR_OK);
1499 
1500   /* verify 3 segments are in-flight */
1501   EXPECT(pcb->unacked != NULL);
1502   check_seqnos(pcb->unacked, 3, seqnos);
1503   EXPECT(txcounters.num_tx_calls == 3);
1504   EXPECT(txcounters.num_tx_bytes == 3 * (TCP_MSS + 40U));
1505   memset(&txcounters, 0, sizeof(txcounters));
1506   /* verify 4th segment is on unsent */
1507   EXPECT(pcb->unsent != NULL);
1508   EXPECT(pcb->unsent->len == TCP_MSS - (TCP_MSS / 4));
1509   check_seqnos(pcb->unsent, 1, &seqnos[3]);
1510 #if TCP_OVERSIZE
1511   EXPECT(pcb->unsent_oversize == TCP_MSS / 4);
1512 #if TCP_OVERSIZE_DBGCHECK
1513   EXPECT(pcb->unsent->oversize_left == pcb->unsent_oversize);
1514 #endif /* TCP_OVERSIZE_DBGCHECK */
1515 #endif /* TCP_OVERSIZE */
1516 
1517   /* ACK the 3 segments and update the window to only 1/2 TCP_MSS.
1518      4th segment should stay on unsent because it's bigger than 1/2 MSS */
1519   p = tcp_create_rx_segment_wnd(pcb, NULL, 0, 0, 3 * TCP_MSS, TCP_ACK, TCP_MSS / 2);
1520   test_tcp_input(p, &netif);
1521   EXPECT(pcb->unacked == NULL);
1522   EXPECT(pcb->snd_wnd == TCP_MSS / 2);
1523   EXPECT(pcb->unsent != NULL);
1524   check_seqnos(pcb->unsent, 1, &seqnos[3]);
1525   EXPECT(txcounters.num_tx_calls == 0);
1526   EXPECT(txcounters.num_tx_bytes == 0);
1527   /* persist timer should be started since 4th segment is stuck waiting on snd_wnd */
1528   EXPECT(pcb->persist_backoff == 1);
1529 
1530   /* ensure no errors have been recorded */
1531   EXPECT(counters.err_calls == 0);
1532   EXPECT(counters.last_err == ERR_OK);
1533 
1534   /* call tcp_timer some more times to let persist timer count up */
1535     for (i = 0; i < 4; i++) {
1536       test_tcp_tmr();
1537       EXPECT(txcounters.num_tx_calls == 0);
1538       EXPECT(txcounters.num_tx_bytes == 0);
1539     }
1540 
1541   /* this should be the first timer shot, which should split the
1542    * segment and send a runt (of the remaining window size) */
1543   txcounters.copy_tx_packets = 1;
1544   test_tcp_tmr();
1545   txcounters.copy_tx_packets = 0;
1546   /* persist will be disabled as RTO timer takes over */
1547   EXPECT(pcb->persist_backoff == 0);
1548   EXPECT(txcounters.num_tx_calls == 1);
1549   EXPECT(txcounters.num_tx_bytes == ((TCP_MSS /2) + 40U));
1550   /* verify 1/2 MSS segment sent, 1/4 MSS still buffered */
1551   EXPECT(pcb->unsent != NULL);
1552   EXPECT(pcb->unsent->len == TCP_MSS / 4);
1553   EXPECT(pcb->unacked != NULL);
1554   EXPECT(pcb->unacked->len == TCP_MSS / 2);
1555 #if TCP_OVERSIZE
1556   /* verify there is no oversized remaining since during the
1557      segment split, the remainder pbuf is always the exact length */
1558   EXPECT(pcb->unsent_oversize == 0);
1559 #if TCP_OVERSIZE_DBGCHECK
1560   /* Split segment already transmitted, should be at 0 */
1561   EXPECT(pcb->unacked->oversize_left == 0);
1562   /* Remainder segement should match pcb value (which is 0) */
1563   EXPECT(pcb->unsent->oversize_left == pcb->unsent_oversize);
1564 #endif /* TCP_OVERSIZE_DBGCHECK */
1565 #endif /* TCP_OVERSIZE */
1566 
1567   /* verify first half segment */
1568   EXPECT(txcounters.tx_packets != NULL);
1569   if (txcounters.tx_packets != NULL) {
1570     u8_t sent[TCP_MSS / 2];
1571     u16_t ret;
1572     ret = pbuf_copy_partial(txcounters.tx_packets, &sent, TCP_MSS / 2, 40U);
1573     EXPECT(ret == TCP_MSS / 2);
1574     EXPECT(memcmp(sent, &tx_data[3 * TCP_MSS], TCP_MSS / 2) == 0);
1575   }
1576   if (txcounters.tx_packets != NULL) {
1577     pbuf_free(txcounters.tx_packets);
1578     txcounters.tx_packets = NULL;
1579   }
1580   memset(&txcounters, 0, sizeof(txcounters));
1581 
1582   /* ACK the half segment, leave window at half segment */
1583   p = tcp_create_rx_segment_wnd(pcb, NULL, 0, 0, TCP_MSS / 2, TCP_ACK, TCP_MSS / 2);
1584   txcounters.copy_tx_packets = 1;
1585   test_tcp_input(p, &netif);
1586   txcounters.copy_tx_packets = 0;
1587   /* ensure remaining segment was sent */
1588   EXPECT(txcounters.num_tx_calls == 1);
1589   EXPECT(txcounters.num_tx_bytes == ((TCP_MSS / 4) + 40U));
1590   EXPECT(pcb->unsent == NULL);
1591   EXPECT(pcb->unacked != NULL);
1592   EXPECT(pcb->unacked->len == TCP_MSS / 4);
1593   EXPECT(pcb->snd_wnd == TCP_MSS / 2);
1594 
1595   /* verify remainder segment */
1596   EXPECT(txcounters.tx_packets != NULL);
1597   if (txcounters.tx_packets != NULL) {
1598     u8_t sent[TCP_MSS / 4];
1599     u16_t ret;
1600     ret = pbuf_copy_partial(txcounters.tx_packets, &sent, TCP_MSS / 4, 40U);
1601     EXPECT(ret == TCP_MSS / 4);
1602     EXPECT(memcmp(sent, &tx_data[(3 * TCP_MSS) + TCP_MSS / 2], TCP_MSS / 4) == 0);
1603   }
1604   if (txcounters.tx_packets != NULL) {
1605     pbuf_free(txcounters.tx_packets);
1606     txcounters.tx_packets = NULL;
1607   }
1608 
1609   /* ensure no errors have been recorded */
1610   EXPECT(counters.err_calls == 0);
1611   EXPECT(counters.last_err == ERR_OK);
1612 
1613   /* make sure the pcb is freed */
1614   EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
1615   tcp_abort(pcb);
1616   EXPECT_RET(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
1617 }
1618 END_TEST
1619 
1620 /** Create the suite including all tests for this module */
1621 Suite *
tcp_suite(void)1622 tcp_suite(void)
1623 {
1624   testfunc tests[] = {
1625     TESTFUNC(test_tcp_new_abort),
1626     TESTFUNC(test_tcp_listen_passive_open),
1627     TESTFUNC(test_tcp_recv_inseq),
1628     TESTFUNC(test_tcp_recv_inseq_trim),
1629     TESTFUNC(test_tcp_passive_close),
1630     TESTFUNC(test_tcp_malformed_header),
1631     TESTFUNC(test_tcp_fast_retx_recover),
1632     TESTFUNC(test_tcp_fast_rexmit_wraparound),
1633     TESTFUNC(test_tcp_rto_rexmit_wraparound),
1634     TESTFUNC(test_tcp_tx_full_window_lost_from_unacked),
1635     TESTFUNC(test_tcp_tx_full_window_lost_from_unsent),
1636     TESTFUNC(test_tcp_retx_add_to_sent),
1637     TESTFUNC(test_tcp_rto_tracking),
1638     TESTFUNC(test_tcp_rto_timeout),
1639     TESTFUNC(test_tcp_rto_timeout_link_down),
1640     TESTFUNC(test_tcp_rto_timeout_syn_sent),
1641     TESTFUNC(test_tcp_rto_timeout_syn_sent_link_down),
1642     TESTFUNC(test_tcp_zwp_timeout),
1643     TESTFUNC(test_tcp_zwp_timeout_link_down),
1644     TESTFUNC(test_tcp_persist_split)
1645   };
1646   return create_suite("TCP", tests, sizeof(tests)/sizeof(testfunc), tcp_setup, tcp_teardown);
1647 }
1648