test_tcp_oos.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049
  1. #include "test_tcp_oos.h"
  2. #include "lwip/priv/tcp_priv.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. #if !TCP_QUEUE_OOSEQ
  9. #error "This tests needs TCP_QUEUE_OOSEQ enabled"
  10. #endif
  11. /** CHECK_SEGMENTS_ON_OOSEQ:
  12. * 1: check count, seqno and len of segments on pcb->ooseq (strict)
  13. * 0: only check that bytes are received in correct order (less strict) */
  14. #define CHECK_SEGMENTS_ON_OOSEQ 1
  15. #if CHECK_SEGMENTS_ON_OOSEQ
  16. #define EXPECT_OOSEQ(x) EXPECT(x)
  17. #else
  18. #define EXPECT_OOSEQ(x)
  19. #endif
  20. /* helper functions */
  21. /** Get the numbers of segments on the ooseq list */
  22. static int tcp_oos_count(struct tcp_pcb* pcb)
  23. {
  24. int num = 0;
  25. struct tcp_seg* seg = pcb->ooseq;
  26. while(seg != NULL) {
  27. num++;
  28. seg = seg->next;
  29. }
  30. return num;
  31. }
  32. #if TCP_OOSEQ_MAX_PBUFS && (TCP_OOSEQ_MAX_PBUFS < ((TCP_WND / TCP_MSS) + 1)) && (PBUF_POOL_BUFSIZE >= (TCP_MSS + PBUF_LINK_ENCAPSULATION_HLEN + PBUF_LINK_HLEN + PBUF_IP_HLEN + PBUF_TRANSPORT_HLEN))
  33. /** Get the numbers of pbufs on the ooseq list */
  34. static int tcp_oos_pbuf_count(struct tcp_pcb* pcb)
  35. {
  36. int num = 0;
  37. struct tcp_seg* seg = pcb->ooseq;
  38. while(seg != NULL) {
  39. num += pbuf_clen(seg->p);
  40. seg = seg->next;
  41. }
  42. return num;
  43. }
  44. #endif
  45. /** Get the seqno of a segment (by index) on the ooseq list
  46. *
  47. * @param pcb the pcb to check for ooseq segments
  48. * @param seg_index index of the segment on the ooseq list
  49. * @return seqno of the segment
  50. */
  51. static u32_t
  52. tcp_oos_seg_seqno(struct tcp_pcb* pcb, int seg_index)
  53. {
  54. int num = 0;
  55. struct tcp_seg* seg = pcb->ooseq;
  56. /* then check the actual segment */
  57. while(seg != NULL) {
  58. if(num == seg_index) {
  59. return seg->tcphdr->seqno;
  60. }
  61. num++;
  62. seg = seg->next;
  63. }
  64. fail();
  65. return 0;
  66. }
  67. /** Get the tcplen (datalen + SYN/FIN) of a segment (by index) on the ooseq list
  68. *
  69. * @param pcb the pcb to check for ooseq segments
  70. * @param seg_index index of the segment on the ooseq list
  71. * @return tcplen of the segment
  72. */
  73. static int
  74. tcp_oos_seg_tcplen(struct tcp_pcb* pcb, int seg_index)
  75. {
  76. int num = 0;
  77. struct tcp_seg* seg = pcb->ooseq;
  78. /* then check the actual segment */
  79. while(seg != NULL) {
  80. if(num == seg_index) {
  81. return TCP_TCPLEN(seg);
  82. }
  83. num++;
  84. seg = seg->next;
  85. }
  86. fail();
  87. return -1;
  88. }
  89. /** Get the tcplen (datalen + SYN/FIN) of all segments on the ooseq list
  90. *
  91. * @param pcb the pcb to check for ooseq segments
  92. * @return tcplen of all segment
  93. */
  94. static int
  95. tcp_oos_tcplen(struct tcp_pcb* pcb)
  96. {
  97. int len = 0;
  98. struct tcp_seg* seg = pcb->ooseq;
  99. /* then check the actual segment */
  100. while(seg != NULL) {
  101. len += TCP_TCPLEN(seg);
  102. seg = seg->next;
  103. }
  104. return len;
  105. }
  106. /* Setup/teardown functions */
  107. static void
  108. tcp_oos_setup(void)
  109. {
  110. tcp_remove_all();
  111. }
  112. static void
  113. tcp_oos_teardown(void)
  114. {
  115. tcp_remove_all();
  116. netif_list = NULL;
  117. netif_default = NULL;
  118. }
  119. /* Test functions */
  120. /** create multiple segments and pass them to tcp_input in a wrong
  121. * order to see if ooseq-caching works correctly
  122. * FIN is received in out-of-sequence segments only */
  123. START_TEST(test_tcp_recv_ooseq_FIN_OOSEQ)
  124. {
  125. struct test_tcp_counters counters;
  126. struct tcp_pcb* pcb;
  127. struct pbuf *p_8_9, *p_4_8, *p_4_10, *p_2_14, *p_fin, *pinseq;
  128. char data[] = {
  129. 1, 2, 3, 4,
  130. 5, 6, 7, 8,
  131. 9, 10, 11, 12,
  132. 13, 14, 15, 16};
  133. ip_addr_t remote_ip, local_ip, netmask;
  134. u16_t data_len;
  135. u16_t remote_port = 0x100, local_port = 0x101;
  136. struct netif netif;
  137. LWIP_UNUSED_ARG(_i);
  138. /* initialize local vars */
  139. memset(&netif, 0, sizeof(netif));
  140. IP_ADDR4(&local_ip, 192, 168, 1, 1);
  141. IP_ADDR4(&remote_ip, 192, 168, 1, 2);
  142. IP_ADDR4(&netmask, 255, 255, 255, 0);
  143. test_tcp_init_netif(&netif, NULL, &local_ip, &netmask);
  144. data_len = sizeof(data);
  145. /* initialize counter struct */
  146. memset(&counters, 0, sizeof(counters));
  147. counters.expected_data_len = data_len;
  148. counters.expected_data = data;
  149. /* create and initialize the pcb */
  150. pcb = test_tcp_new_counters_pcb(&counters);
  151. EXPECT_RET(pcb != NULL);
  152. tcp_set_state(pcb, ESTABLISHED, &local_ip, &remote_ip, local_port, remote_port);
  153. /* create segments */
  154. /* pinseq is sent as last segment! */
  155. pinseq = tcp_create_rx_segment(pcb, &data[0], 4, 0, 0, TCP_ACK);
  156. /* p1: 8 bytes before FIN */
  157. /* seqno: 8..16 */
  158. p_8_9 = tcp_create_rx_segment(pcb, &data[8], 8, 8, 0, TCP_ACK|TCP_FIN);
  159. /* p2: 4 bytes before p1, including the first 4 bytes of p1 (partly duplicate) */
  160. /* seqno: 4..11 */
  161. p_4_8 = tcp_create_rx_segment(pcb, &data[4], 8, 4, 0, TCP_ACK);
  162. /* p3: same as p2 but 2 bytes longer */
  163. /* seqno: 4..13 */
  164. p_4_10 = tcp_create_rx_segment(pcb, &data[4], 10, 4, 0, TCP_ACK);
  165. /* p4: 14 bytes before FIN, includes data from p1 and p2, plus partly from pinseq */
  166. /* seqno: 2..15 */
  167. p_2_14 = tcp_create_rx_segment(pcb, &data[2], 14, 2, 0, TCP_ACK);
  168. /* FIN, seqno 16 */
  169. p_fin = tcp_create_rx_segment(pcb, NULL, 0,16, 0, TCP_ACK|TCP_FIN);
  170. EXPECT(pinseq != NULL);
  171. EXPECT(p_8_9 != NULL);
  172. EXPECT(p_4_8 != NULL);
  173. EXPECT(p_4_10 != NULL);
  174. EXPECT(p_2_14 != NULL);
  175. EXPECT(p_fin != NULL);
  176. if ((pinseq != NULL) && (p_8_9 != NULL) && (p_4_8 != NULL) && (p_4_10 != NULL) && (p_2_14 != NULL) && (p_fin != NULL)) {
  177. /* pass the segment to tcp_input */
  178. test_tcp_input(p_8_9, &netif);
  179. /* check if counters are as expected */
  180. EXPECT(counters.close_calls == 0);
  181. EXPECT(counters.recv_calls == 0);
  182. EXPECT(counters.recved_bytes == 0);
  183. EXPECT(counters.err_calls == 0);
  184. /* check ooseq queue */
  185. EXPECT_OOSEQ(tcp_oos_count(pcb) == 1);
  186. EXPECT_OOSEQ(tcp_oos_seg_seqno(pcb, 0) == 8);
  187. EXPECT_OOSEQ(tcp_oos_seg_tcplen(pcb, 0) == 9); /* includes FIN */
  188. /* pass the segment to tcp_input */
  189. test_tcp_input(p_4_8, &netif);
  190. /* check if counters are as expected */
  191. EXPECT(counters.close_calls == 0);
  192. EXPECT(counters.recv_calls == 0);
  193. EXPECT(counters.recved_bytes == 0);
  194. EXPECT(counters.err_calls == 0);
  195. /* check ooseq queue */
  196. EXPECT_OOSEQ(tcp_oos_count(pcb) == 2);
  197. EXPECT_OOSEQ(tcp_oos_seg_seqno(pcb, 0) == 4);
  198. EXPECT_OOSEQ(tcp_oos_seg_tcplen(pcb, 0) == 4);
  199. EXPECT_OOSEQ(tcp_oos_seg_seqno(pcb, 1) == 8);
  200. EXPECT_OOSEQ(tcp_oos_seg_tcplen(pcb, 1) == 9); /* includes FIN */
  201. /* pass the segment to tcp_input */
  202. test_tcp_input(p_4_10, &netif);
  203. /* check if counters are as expected */
  204. EXPECT(counters.close_calls == 0);
  205. EXPECT(counters.recv_calls == 0);
  206. EXPECT(counters.recved_bytes == 0);
  207. EXPECT(counters.err_calls == 0);
  208. /* ooseq queue: unchanged */
  209. EXPECT_OOSEQ(tcp_oos_count(pcb) == 2);
  210. EXPECT_OOSEQ(tcp_oos_seg_seqno(pcb, 0) == 4);
  211. EXPECT_OOSEQ(tcp_oos_seg_tcplen(pcb, 0) == 4);
  212. EXPECT_OOSEQ(tcp_oos_seg_seqno(pcb, 1) == 8);
  213. EXPECT_OOSEQ(tcp_oos_seg_tcplen(pcb, 1) == 9); /* includes FIN */
  214. /* pass the segment to tcp_input */
  215. test_tcp_input(p_2_14, &netif);
  216. /* check if counters are as expected */
  217. EXPECT(counters.close_calls == 0);
  218. EXPECT(counters.recv_calls == 0);
  219. EXPECT(counters.recved_bytes == 0);
  220. EXPECT(counters.err_calls == 0);
  221. /* check ooseq queue */
  222. EXPECT_OOSEQ(tcp_oos_count(pcb) == 1);
  223. EXPECT_OOSEQ(tcp_oos_seg_seqno(pcb, 0) == 2);
  224. EXPECT_OOSEQ(tcp_oos_seg_tcplen(pcb, 0) == 15); /* includes FIN */
  225. /* pass the segment to tcp_input */
  226. test_tcp_input(p_fin, &netif);
  227. /* check if counters are as expected */
  228. EXPECT(counters.close_calls == 0);
  229. EXPECT(counters.recv_calls == 0);
  230. EXPECT(counters.recved_bytes == 0);
  231. EXPECT(counters.err_calls == 0);
  232. /* ooseq queue: unchanged */
  233. EXPECT_OOSEQ(tcp_oos_count(pcb) == 1);
  234. EXPECT_OOSEQ(tcp_oos_seg_seqno(pcb, 0) == 2);
  235. EXPECT_OOSEQ(tcp_oos_seg_tcplen(pcb, 0) == 15); /* includes FIN */
  236. /* pass the segment to tcp_input */
  237. test_tcp_input(pinseq, &netif);
  238. /* check if counters are as expected */
  239. EXPECT(counters.close_calls == 1);
  240. EXPECT(counters.recv_calls == 1);
  241. EXPECT(counters.recved_bytes == data_len);
  242. EXPECT(counters.err_calls == 0);
  243. EXPECT(pcb->ooseq == NULL);
  244. }
  245. /* make sure the pcb is freed */
  246. EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
  247. tcp_abort(pcb);
  248. EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
  249. }
  250. END_TEST
  251. /** create multiple segments and pass them to tcp_input in a wrong
  252. * order to see if ooseq-caching works correctly
  253. * FIN is received IN-SEQUENCE at the end */
  254. START_TEST(test_tcp_recv_ooseq_FIN_INSEQ)
  255. {
  256. struct test_tcp_counters counters;
  257. struct tcp_pcb* pcb;
  258. struct pbuf *p_1_2, *p_4_8, *p_3_11, *p_2_12, *p_15_1, *p_15_1a, *pinseq, *pinseqFIN;
  259. char data[] = {
  260. 1, 2, 3, 4,
  261. 5, 6, 7, 8,
  262. 9, 10, 11, 12,
  263. 13, 14, 15, 16};
  264. ip_addr_t remote_ip, local_ip, netmask;
  265. u16_t data_len;
  266. u16_t remote_port = 0x100, local_port = 0x101;
  267. struct netif netif;
  268. LWIP_UNUSED_ARG(_i);
  269. /* initialize local vars */
  270. memset(&netif, 0, sizeof(netif));
  271. IP_ADDR4(&local_ip, 192, 168, 1, 1);
  272. IP_ADDR4(&remote_ip, 192, 168, 1, 2);
  273. IP_ADDR4(&netmask, 255, 255, 255, 0);
  274. test_tcp_init_netif(&netif, NULL, &local_ip, &netmask);
  275. data_len = sizeof(data);
  276. /* initialize counter struct */
  277. memset(&counters, 0, sizeof(counters));
  278. counters.expected_data_len = data_len;
  279. counters.expected_data = data;
  280. /* create and initialize the pcb */
  281. pcb = test_tcp_new_counters_pcb(&counters);
  282. EXPECT_RET(pcb != NULL);
  283. tcp_set_state(pcb, ESTABLISHED, &local_ip, &remote_ip, local_port, remote_port);
  284. /* create segments */
  285. /* p1: 7 bytes - 2 before FIN */
  286. /* seqno: 1..2 */
  287. p_1_2 = tcp_create_rx_segment(pcb, &data[1], 2, 1, 0, TCP_ACK);
  288. /* p2: 4 bytes before p1, including the first 4 bytes of p1 (partly duplicate) */
  289. /* seqno: 4..11 */
  290. p_4_8 = tcp_create_rx_segment(pcb, &data[4], 8, 4, 0, TCP_ACK);
  291. /* p3: same as p2 but 2 bytes longer and one byte more at the front */
  292. /* seqno: 3..13 */
  293. p_3_11 = tcp_create_rx_segment(pcb, &data[3], 11, 3, 0, TCP_ACK);
  294. /* p4: 13 bytes - 2 before FIN - should be ignored as contained in p1 and p3 */
  295. /* seqno: 2..13 */
  296. p_2_12 = tcp_create_rx_segment(pcb, &data[2], 12, 2, 0, TCP_ACK);
  297. /* pinseq is the first segment that is held back to create ooseq! */
  298. /* seqno: 0..3 */
  299. pinseq = tcp_create_rx_segment(pcb, &data[0], 4, 0, 0, TCP_ACK);
  300. /* p5: last byte before FIN */
  301. /* seqno: 15 */
  302. p_15_1 = tcp_create_rx_segment(pcb, &data[15], 1, 15, 0, TCP_ACK);
  303. /* p6: same as p5, should be ignored */
  304. p_15_1a= tcp_create_rx_segment(pcb, &data[15], 1, 15, 0, TCP_ACK);
  305. /* pinseqFIN: last 2 bytes plus FIN */
  306. /* only segment containing seqno 14 and FIN */
  307. pinseqFIN = tcp_create_rx_segment(pcb, &data[14], 2, 14, 0, TCP_ACK|TCP_FIN);
  308. EXPECT(pinseq != NULL);
  309. EXPECT(p_1_2 != NULL);
  310. EXPECT(p_4_8 != NULL);
  311. EXPECT(p_3_11 != NULL);
  312. EXPECT(p_2_12 != NULL);
  313. EXPECT(p_15_1 != NULL);
  314. EXPECT(p_15_1a != NULL);
  315. EXPECT(pinseqFIN != NULL);
  316. if ((pinseq != NULL) && (p_1_2 != NULL) && (p_4_8 != NULL) && (p_3_11 != NULL) && (p_2_12 != NULL)
  317. && (p_15_1 != NULL) && (p_15_1a != NULL) && (pinseqFIN != NULL)) {
  318. /* pass the segment to tcp_input */
  319. test_tcp_input(p_1_2, &netif);
  320. /* check if counters are as expected */
  321. EXPECT(counters.close_calls == 0);
  322. EXPECT(counters.recv_calls == 0);
  323. EXPECT(counters.recved_bytes == 0);
  324. EXPECT(counters.err_calls == 0);
  325. /* check ooseq queue */
  326. EXPECT_OOSEQ(tcp_oos_count(pcb) == 1);
  327. EXPECT_OOSEQ(tcp_oos_seg_seqno(pcb, 0) == 1);
  328. EXPECT_OOSEQ(tcp_oos_seg_tcplen(pcb, 0) == 2);
  329. /* pass the segment to tcp_input */
  330. test_tcp_input(p_4_8, &netif);
  331. /* check if counters are as expected */
  332. EXPECT(counters.close_calls == 0);
  333. EXPECT(counters.recv_calls == 0);
  334. EXPECT(counters.recved_bytes == 0);
  335. EXPECT(counters.err_calls == 0);
  336. /* check ooseq queue */
  337. EXPECT_OOSEQ(tcp_oos_count(pcb) == 2);
  338. EXPECT_OOSEQ(tcp_oos_seg_seqno(pcb, 0) == 1);
  339. EXPECT_OOSEQ(tcp_oos_seg_tcplen(pcb, 0) == 2);
  340. EXPECT_OOSEQ(tcp_oos_seg_seqno(pcb, 1) == 4);
  341. EXPECT_OOSEQ(tcp_oos_seg_tcplen(pcb, 1) == 8);
  342. /* pass the segment to tcp_input */
  343. test_tcp_input(p_3_11, &netif);
  344. /* check if counters are as expected */
  345. EXPECT(counters.close_calls == 0);
  346. EXPECT(counters.recv_calls == 0);
  347. EXPECT(counters.recved_bytes == 0);
  348. EXPECT(counters.err_calls == 0);
  349. /* check ooseq queue */
  350. EXPECT_OOSEQ(tcp_oos_count(pcb) == 2);
  351. EXPECT_OOSEQ(tcp_oos_seg_seqno(pcb, 0) == 1);
  352. EXPECT_OOSEQ(tcp_oos_seg_tcplen(pcb, 0) == 2);
  353. /* p_3_11 has removed p_4_8 from ooseq */
  354. EXPECT_OOSEQ(tcp_oos_seg_seqno(pcb, 1) == 3);
  355. EXPECT_OOSEQ(tcp_oos_seg_tcplen(pcb, 1) == 11);
  356. /* pass the segment to tcp_input */
  357. test_tcp_input(p_2_12, &netif);
  358. /* check if counters are as expected */
  359. EXPECT(counters.close_calls == 0);
  360. EXPECT(counters.recv_calls == 0);
  361. EXPECT(counters.recved_bytes == 0);
  362. EXPECT(counters.err_calls == 0);
  363. /* check ooseq queue */
  364. EXPECT_OOSEQ(tcp_oos_count(pcb) == 2);
  365. EXPECT_OOSEQ(tcp_oos_seg_seqno(pcb, 0) == 1);
  366. EXPECT_OOSEQ(tcp_oos_seg_tcplen(pcb, 0) == 1);
  367. EXPECT_OOSEQ(tcp_oos_seg_seqno(pcb, 1) == 2);
  368. EXPECT_OOSEQ(tcp_oos_seg_tcplen(pcb, 1) == 12);
  369. /* pass the segment to tcp_input */
  370. test_tcp_input(pinseq, &netif);
  371. /* check if counters are as expected */
  372. EXPECT(counters.close_calls == 0);
  373. EXPECT(counters.recv_calls == 1);
  374. EXPECT(counters.recved_bytes == 14);
  375. EXPECT(counters.err_calls == 0);
  376. EXPECT(pcb->ooseq == NULL);
  377. /* pass the segment to tcp_input */
  378. test_tcp_input(p_15_1, &netif);
  379. /* check if counters are as expected */
  380. EXPECT(counters.close_calls == 0);
  381. EXPECT(counters.recv_calls == 1);
  382. EXPECT(counters.recved_bytes == 14);
  383. EXPECT(counters.err_calls == 0);
  384. /* check ooseq queue */
  385. EXPECT_OOSEQ(tcp_oos_count(pcb) == 1);
  386. EXPECT_OOSEQ(tcp_oos_seg_seqno(pcb, 0) == 15);
  387. EXPECT_OOSEQ(tcp_oos_seg_tcplen(pcb, 0) == 1);
  388. /* pass the segment to tcp_input */
  389. test_tcp_input(p_15_1a, &netif);
  390. /* check if counters are as expected */
  391. EXPECT(counters.close_calls == 0);
  392. EXPECT(counters.recv_calls == 1);
  393. EXPECT(counters.recved_bytes == 14);
  394. EXPECT(counters.err_calls == 0);
  395. /* check ooseq queue: unchanged */
  396. EXPECT_OOSEQ(tcp_oos_count(pcb) == 1);
  397. EXPECT_OOSEQ(tcp_oos_seg_seqno(pcb, 0) == 15);
  398. EXPECT_OOSEQ(tcp_oos_seg_tcplen(pcb, 0) == 1);
  399. /* pass the segment to tcp_input */
  400. test_tcp_input(pinseqFIN, &netif);
  401. /* check if counters are as expected */
  402. EXPECT(counters.close_calls == 1);
  403. EXPECT(counters.recv_calls == 2);
  404. EXPECT(counters.recved_bytes == data_len);
  405. EXPECT(counters.err_calls == 0);
  406. EXPECT(pcb->ooseq == NULL);
  407. }
  408. /* make sure the pcb is freed */
  409. EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
  410. tcp_abort(pcb);
  411. EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
  412. }
  413. END_TEST
  414. static char data_full_wnd[TCP_WND + TCP_MSS];
  415. /** create multiple segments and pass them to tcp_input with the first segment missing
  416. * to simulate overruning the rxwin with ooseq queueing enabled */
  417. START_TEST(test_tcp_recv_ooseq_overrun_rxwin)
  418. {
  419. #if !TCP_OOSEQ_MAX_BYTES && !TCP_OOSEQ_MAX_PBUFS
  420. int i, k;
  421. struct test_tcp_counters counters;
  422. struct tcp_pcb* pcb;
  423. struct pbuf *pinseq, *p_ovr;
  424. ip_addr_t remote_ip, local_ip, netmask;
  425. u16_t remote_port = 0x100, local_port = 0x101;
  426. struct netif netif;
  427. int datalen = 0;
  428. int datalen2;
  429. for(i = 0; i < (int)sizeof(data_full_wnd); i++) {
  430. data_full_wnd[i] = (char)i;
  431. }
  432. /* initialize local vars */
  433. memset(&netif, 0, sizeof(netif));
  434. IP_ADDR4(&local_ip, 192, 168, 1, 1);
  435. IP_ADDR4(&remote_ip, 192, 168, 1, 2);
  436. IP_ADDR4(&netmask, 255, 255, 255, 0);
  437. test_tcp_init_netif(&netif, NULL, &local_ip, &netmask);
  438. /* initialize counter struct */
  439. memset(&counters, 0, sizeof(counters));
  440. counters.expected_data_len = TCP_WND;
  441. counters.expected_data = data_full_wnd;
  442. /* create and initialize the pcb */
  443. pcb = test_tcp_new_counters_pcb(&counters);
  444. EXPECT_RET(pcb != NULL);
  445. tcp_set_state(pcb, ESTABLISHED, &local_ip, &remote_ip, local_port, remote_port);
  446. pcb->rcv_nxt = 0x8000;
  447. /* create segments */
  448. /* pinseq is sent as last segment! */
  449. pinseq = tcp_create_rx_segment(pcb, &data_full_wnd[0], TCP_MSS, 0, 0, TCP_ACK);
  450. for(i = TCP_MSS, k = 0; i < TCP_WND; i += TCP_MSS, k++) {
  451. int count, expected_datalen;
  452. struct pbuf *p = tcp_create_rx_segment(pcb, &data_full_wnd[TCP_MSS*(k+1)],
  453. TCP_MSS, TCP_MSS*(k+1), 0, TCP_ACK);
  454. EXPECT_RET(p != NULL);
  455. /* pass the segment to tcp_input */
  456. test_tcp_input(p, &netif);
  457. /* check if counters are as expected */
  458. EXPECT(counters.close_calls == 0);
  459. EXPECT(counters.recv_calls == 0);
  460. EXPECT(counters.recved_bytes == 0);
  461. EXPECT(counters.err_calls == 0);
  462. /* check ooseq queue */
  463. count = tcp_oos_count(pcb);
  464. EXPECT_OOSEQ(count == k+1);
  465. datalen = tcp_oos_tcplen(pcb);
  466. if (i + TCP_MSS < TCP_WND) {
  467. expected_datalen = (k+1)*TCP_MSS;
  468. } else {
  469. expected_datalen = TCP_WND - TCP_MSS;
  470. }
  471. if (datalen != expected_datalen) {
  472. EXPECT_OOSEQ(datalen == expected_datalen);
  473. }
  474. }
  475. /* pass in one more segment, cleary overrunning the rxwin */
  476. p_ovr = tcp_create_rx_segment(pcb, &data_full_wnd[TCP_MSS*(k+1)], TCP_MSS, TCP_MSS*(k+1), 0, TCP_ACK);
  477. EXPECT_RET(p_ovr != NULL);
  478. /* pass the segment to tcp_input */
  479. test_tcp_input(p_ovr, &netif);
  480. /* check if counters are as expected */
  481. EXPECT(counters.close_calls == 0);
  482. EXPECT(counters.recv_calls == 0);
  483. EXPECT(counters.recved_bytes == 0);
  484. EXPECT(counters.err_calls == 0);
  485. /* check ooseq queue */
  486. EXPECT_OOSEQ(tcp_oos_count(pcb) == k);
  487. datalen2 = tcp_oos_tcplen(pcb);
  488. EXPECT_OOSEQ(datalen == datalen2);
  489. /* now pass inseq */
  490. test_tcp_input(pinseq, &netif);
  491. EXPECT(pcb->ooseq == NULL);
  492. /* make sure the pcb is freed */
  493. EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
  494. tcp_abort(pcb);
  495. EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
  496. #endif /* !TCP_OOSEQ_MAX_BYTES && !TCP_OOSEQ_MAX_PBUFS */
  497. LWIP_UNUSED_ARG(_i);
  498. }
  499. END_TEST
  500. /** similar to above test, except seqno starts near the max rxwin */
  501. START_TEST(test_tcp_recv_ooseq_overrun_rxwin_edge)
  502. {
  503. #if !TCP_OOSEQ_MAX_BYTES && !TCP_OOSEQ_MAX_PBUFS
  504. int i, k;
  505. struct test_tcp_counters counters;
  506. struct tcp_pcb* pcb;
  507. struct pbuf *pinseq, *p_ovr;
  508. ip_addr_t remote_ip, local_ip, netmask;
  509. u16_t remote_port = 0x100, local_port = 0x101;
  510. struct netif netif;
  511. int datalen = 0;
  512. int datalen2;
  513. for(i = 0; i < (int)sizeof(data_full_wnd); i++) {
  514. data_full_wnd[i] = (char)i;
  515. }
  516. /* initialize local vars */
  517. memset(&netif, 0, sizeof(netif));
  518. IP_ADDR4(&local_ip, 192, 168, 1, 1);
  519. IP_ADDR4(&remote_ip, 192, 168, 1, 2);
  520. IP_ADDR4(&netmask, 255, 255, 255, 0);
  521. test_tcp_init_netif(&netif, NULL, &local_ip, &netmask);
  522. /* initialize counter struct */
  523. memset(&counters, 0, sizeof(counters));
  524. counters.expected_data_len = TCP_WND;
  525. counters.expected_data = data_full_wnd;
  526. /* create and initialize the pcb */
  527. pcb = test_tcp_new_counters_pcb(&counters);
  528. EXPECT_RET(pcb != NULL);
  529. tcp_set_state(pcb, ESTABLISHED, &local_ip, &remote_ip, local_port, remote_port);
  530. pcb->rcv_nxt = 0xffffffff - (TCP_WND / 2);
  531. /* create segments */
  532. /* pinseq is sent as last segment! */
  533. pinseq = tcp_create_rx_segment(pcb, &data_full_wnd[0], TCP_MSS, 0, 0, TCP_ACK);
  534. for(i = TCP_MSS, k = 0; i < TCP_WND; i += TCP_MSS, k++) {
  535. int count, expected_datalen;
  536. struct pbuf *p = tcp_create_rx_segment(pcb, &data_full_wnd[TCP_MSS*(k+1)],
  537. TCP_MSS, TCP_MSS*(k+1), 0, TCP_ACK);
  538. EXPECT_RET(p != NULL);
  539. /* pass the segment to tcp_input */
  540. test_tcp_input(p, &netif);
  541. /* check if counters are as expected */
  542. EXPECT(counters.close_calls == 0);
  543. EXPECT(counters.recv_calls == 0);
  544. EXPECT(counters.recved_bytes == 0);
  545. EXPECT(counters.err_calls == 0);
  546. /* check ooseq queue */
  547. count = tcp_oos_count(pcb);
  548. EXPECT_OOSEQ(count == k+1);
  549. datalen = tcp_oos_tcplen(pcb);
  550. if (i + TCP_MSS < TCP_WND) {
  551. expected_datalen = (k+1)*TCP_MSS;
  552. } else {
  553. expected_datalen = TCP_WND - TCP_MSS;
  554. }
  555. if (datalen != expected_datalen) {
  556. EXPECT_OOSEQ(datalen == expected_datalen);
  557. }
  558. }
  559. /* pass in one more segment, cleary overrunning the rxwin */
  560. p_ovr = tcp_create_rx_segment(pcb, &data_full_wnd[TCP_MSS*(k+1)], TCP_MSS, TCP_MSS*(k+1), 0, TCP_ACK);
  561. EXPECT_RET(p_ovr != NULL);
  562. /* pass the segment to tcp_input */
  563. test_tcp_input(p_ovr, &netif);
  564. /* check if counters are as expected */
  565. EXPECT(counters.close_calls == 0);
  566. EXPECT(counters.recv_calls == 0);
  567. EXPECT(counters.recved_bytes == 0);
  568. EXPECT(counters.err_calls == 0);
  569. /* check ooseq queue */
  570. EXPECT_OOSEQ(tcp_oos_count(pcb) == k);
  571. datalen2 = tcp_oos_tcplen(pcb);
  572. EXPECT_OOSEQ(datalen == datalen2);
  573. /* now pass inseq */
  574. test_tcp_input(pinseq, &netif);
  575. EXPECT(pcb->ooseq == NULL);
  576. /* make sure the pcb is freed */
  577. EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
  578. tcp_abort(pcb);
  579. EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
  580. #endif /* !TCP_OOSEQ_MAX_BYTES && !TCP_OOSEQ_MAX_PBUFS */
  581. LWIP_UNUSED_ARG(_i);
  582. }
  583. END_TEST
  584. START_TEST(test_tcp_recv_ooseq_max_bytes)
  585. {
  586. #if TCP_OOSEQ_MAX_BYTES && (TCP_OOSEQ_MAX_BYTES < (TCP_WND + 1)) && (PBUF_POOL_BUFSIZE >= (TCP_MSS + PBUF_LINK_ENCAPSULATION_HLEN + PBUF_LINK_HLEN + PBUF_IP_HLEN + PBUF_TRANSPORT_HLEN))
  587. int i, k;
  588. struct test_tcp_counters counters;
  589. struct tcp_pcb* pcb;
  590. struct pbuf *p_ovr;
  591. ip_addr_t remote_ip, local_ip, netmask;
  592. u16_t remote_port = 0x100, local_port = 0x101;
  593. struct netif netif;
  594. int datalen = 0;
  595. int datalen2;
  596. for(i = 0; i < sizeof(data_full_wnd); i++) {
  597. data_full_wnd[i] = (char)i;
  598. }
  599. /* initialize local vars */
  600. memset(&netif, 0, sizeof(netif));
  601. IP_ADDR4(&local_ip, 192, 168, 1, 1);
  602. IP_ADDR4(&remote_ip, 192, 168, 1, 2);
  603. IP_ADDR4(&netmask, 255, 255, 255, 0);
  604. test_tcp_init_netif(&netif, NULL, &local_ip, &netmask);
  605. /* initialize counter struct */
  606. memset(&counters, 0, sizeof(counters));
  607. counters.expected_data_len = TCP_WND;
  608. counters.expected_data = data_full_wnd;
  609. /* create and initialize the pcb */
  610. pcb = test_tcp_new_counters_pcb(&counters);
  611. EXPECT_RET(pcb != NULL);
  612. tcp_set_state(pcb, ESTABLISHED, &local_ip, &remote_ip, local_port, remote_port);
  613. pcb->rcv_nxt = 0x8000;
  614. /* don't 'recv' the first segment (1 byte) so that all other segments will be ooseq */
  615. /* create segments and 'recv' them */
  616. for(k = 1, i = 1; k < TCP_OOSEQ_MAX_BYTES; k += TCP_MSS, i++) {
  617. int count;
  618. struct pbuf *p = tcp_create_rx_segment(pcb, &data_full_wnd[k],
  619. TCP_MSS, k, 0, TCP_ACK);
  620. EXPECT_RET(p != NULL);
  621. EXPECT_RET(p->next == NULL);
  622. /* pass the segment to tcp_input */
  623. test_tcp_input(p, &netif);
  624. /* check if counters are as expected */
  625. EXPECT(counters.close_calls == 0);
  626. EXPECT(counters.recv_calls == 0);
  627. EXPECT(counters.recved_bytes == 0);
  628. EXPECT(counters.err_calls == 0);
  629. /* check ooseq queue */
  630. count = tcp_oos_pbuf_count(pcb);
  631. EXPECT_OOSEQ(count == i);
  632. datalen = tcp_oos_tcplen(pcb);
  633. EXPECT_OOSEQ(datalen == (i * TCP_MSS));
  634. }
  635. /* pass in one more segment, overrunning the limit */
  636. p_ovr = tcp_create_rx_segment(pcb, &data_full_wnd[k+1], 1, k+1, 0, TCP_ACK);
  637. EXPECT_RET(p_ovr != NULL);
  638. /* pass the segment to tcp_input */
  639. test_tcp_input(p_ovr, &netif);
  640. /* check if counters are as expected */
  641. EXPECT(counters.close_calls == 0);
  642. EXPECT(counters.recv_calls == 0);
  643. EXPECT(counters.recved_bytes == 0);
  644. EXPECT(counters.err_calls == 0);
  645. /* check ooseq queue (ensure the new segment was not accepted) */
  646. EXPECT_OOSEQ(tcp_oos_count(pcb) == (i-1));
  647. datalen2 = tcp_oos_tcplen(pcb);
  648. EXPECT_OOSEQ(datalen2 == ((i-1) * TCP_MSS));
  649. /* make sure the pcb is freed */
  650. EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
  651. tcp_abort(pcb);
  652. EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
  653. #endif /* TCP_OOSEQ_MAX_BYTES && (TCP_OOSEQ_MAX_BYTES < (TCP_WND + 1)) && (PBUF_POOL_BUFSIZE >= (TCP_MSS + PBUF_LINK_ENCAPSULATION_HLEN + PBUF_LINK_HLEN + PBUF_IP_HLEN + PBUF_TRANSPORT_HLEN)) */
  654. LWIP_UNUSED_ARG(_i);
  655. }
  656. END_TEST
  657. START_TEST(test_tcp_recv_ooseq_max_pbufs)
  658. {
  659. #if TCP_OOSEQ_MAX_PBUFS && (TCP_OOSEQ_MAX_PBUFS < ((TCP_WND / TCP_MSS) + 1)) && (PBUF_POOL_BUFSIZE >= (TCP_MSS + PBUF_LINK_ENCAPSULATION_HLEN + PBUF_LINK_HLEN + PBUF_IP_HLEN + PBUF_TRANSPORT_HLEN))
  660. int i;
  661. struct test_tcp_counters counters;
  662. struct tcp_pcb* pcb;
  663. struct pbuf *p_ovr;
  664. ip_addr_t remote_ip, local_ip, netmask;
  665. u16_t remote_port = 0x100, local_port = 0x101;
  666. struct netif netif;
  667. int datalen = 0;
  668. int datalen2;
  669. for(i = 0; i < sizeof(data_full_wnd); i++) {
  670. data_full_wnd[i] = (char)i;
  671. }
  672. /* initialize local vars */
  673. memset(&netif, 0, sizeof(netif));
  674. IP_ADDR4(&local_ip, 192, 168, 1, 1);
  675. IP_ADDR4(&remote_ip, 192, 168, 1, 2);
  676. IP_ADDR4(&netmask, 255, 255, 255, 0);
  677. test_tcp_init_netif(&netif, NULL, &local_ip, &netmask);
  678. /* initialize counter struct */
  679. memset(&counters, 0, sizeof(counters));
  680. counters.expected_data_len = TCP_WND;
  681. counters.expected_data = data_full_wnd;
  682. /* create and initialize the pcb */
  683. pcb = test_tcp_new_counters_pcb(&counters);
  684. EXPECT_RET(pcb != NULL);
  685. tcp_set_state(pcb, ESTABLISHED, &local_ip, &remote_ip, local_port, remote_port);
  686. pcb->rcv_nxt = 0x8000;
  687. /* don't 'recv' the first segment (1 byte) so that all other segments will be ooseq */
  688. /* create segments and 'recv' them */
  689. for(i = 1; i <= TCP_OOSEQ_MAX_PBUFS; i++) {
  690. int count;
  691. struct pbuf *p = tcp_create_rx_segment(pcb, &data_full_wnd[i],
  692. 1, i, 0, TCP_ACK);
  693. EXPECT_RET(p != NULL);
  694. EXPECT_RET(p->next == NULL);
  695. /* pass the segment to tcp_input */
  696. test_tcp_input(p, &netif);
  697. /* check if counters are as expected */
  698. EXPECT(counters.close_calls == 0);
  699. EXPECT(counters.recv_calls == 0);
  700. EXPECT(counters.recved_bytes == 0);
  701. EXPECT(counters.err_calls == 0);
  702. /* check ooseq queue */
  703. count = tcp_oos_pbuf_count(pcb);
  704. EXPECT_OOSEQ(count == i);
  705. datalen = tcp_oos_tcplen(pcb);
  706. EXPECT_OOSEQ(datalen == i);
  707. }
  708. /* pass in one more segment, overrunning the limit */
  709. p_ovr = tcp_create_rx_segment(pcb, &data_full_wnd[i+1], 1, i+1, 0, TCP_ACK);
  710. EXPECT_RET(p_ovr != NULL);
  711. /* pass the segment to tcp_input */
  712. test_tcp_input(p_ovr, &netif);
  713. /* check if counters are as expected */
  714. EXPECT(counters.close_calls == 0);
  715. EXPECT(counters.recv_calls == 0);
  716. EXPECT(counters.recved_bytes == 0);
  717. EXPECT(counters.err_calls == 0);
  718. /* check ooseq queue (ensure the new segment was not accepted) */
  719. EXPECT_OOSEQ(tcp_oos_count(pcb) == (i-1));
  720. datalen2 = tcp_oos_tcplen(pcb);
  721. EXPECT_OOSEQ(datalen2 == (i-1));
  722. /* make sure the pcb is freed */
  723. EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
  724. tcp_abort(pcb);
  725. EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
  726. #endif /* TCP_OOSEQ_MAX_PBUFS && (TCP_OOSEQ_MAX_BYTES < (TCP_WND + 1)) && (PBUF_POOL_BUFSIZE >= (TCP_MSS + PBUF_LINK_ENCAPSULATION_HLEN + PBUF_LINK_HLEN + PBUF_IP_HLEN + PBUF_TRANSPORT_HLEN)) */
  727. LWIP_UNUSED_ARG(_i);
  728. }
  729. END_TEST
  730. static void
  731. check_rx_counters(struct tcp_pcb *pcb, struct test_tcp_counters *counters, u32_t exp_close_calls, u32_t exp_rx_calls,
  732. u32_t exp_rx_bytes, u32_t exp_err_calls, int exp_oos_count, int exp_oos_len)
  733. {
  734. int oos_len;
  735. EXPECT(counters->close_calls == exp_close_calls);
  736. EXPECT(counters->recv_calls == exp_rx_calls);
  737. EXPECT(counters->recved_bytes == exp_rx_bytes);
  738. EXPECT(counters->err_calls == exp_err_calls);
  739. /* check that pbuf is queued in ooseq */
  740. EXPECT_OOSEQ(tcp_oos_count(pcb) == exp_oos_count);
  741. oos_len = tcp_oos_tcplen(pcb);
  742. EXPECT_OOSEQ(exp_oos_len == oos_len);
  743. }
  744. /* this test uses 4 packets:
  745. * - data (len=TCP_MSS)
  746. * - FIN
  747. * - data after FIN (len=1) (invalid)
  748. * - 2nd FIN (invalid)
  749. *
  750. * the parameter 'delay_packet' is a bitmask that choses which on these packets is ooseq
  751. */
  752. static void test_tcp_recv_ooseq_double_FINs(int delay_packet)
  753. {
  754. int i, k;
  755. struct test_tcp_counters counters;
  756. struct tcp_pcb* pcb;
  757. struct pbuf *p_normal_fin, *p_data_after_fin, *p, *p_2nd_fin_ooseq;
  758. ip_addr_t remote_ip, local_ip, netmask;
  759. u16_t remote_port = 0x100, local_port = 0x101;
  760. struct netif netif;
  761. u32_t exp_rx_calls = 0, exp_rx_bytes = 0, exp_close_calls = 0, exp_oos_pbufs = 0, exp_oos_tcplen = 0;
  762. int first_dropped = 0xff;
  763. for(i = 0; i < (int)sizeof(data_full_wnd); i++) {
  764. data_full_wnd[i] = (char)i;
  765. }
  766. /* initialize local vars */
  767. memset(&netif, 0, sizeof(netif));
  768. IP_ADDR4(&local_ip, 192, 168, 1, 1);
  769. IP_ADDR4(&remote_ip, 192, 168, 1, 2);
  770. IP_ADDR4(&netmask, 255, 255, 255, 0);
  771. test_tcp_init_netif(&netif, NULL, &local_ip, &netmask);
  772. /* initialize counter struct */
  773. memset(&counters, 0, sizeof(counters));
  774. counters.expected_data_len = TCP_WND;
  775. counters.expected_data = data_full_wnd;
  776. /* create and initialize the pcb */
  777. pcb = test_tcp_new_counters_pcb(&counters);
  778. EXPECT_RET(pcb != NULL);
  779. tcp_set_state(pcb, ESTABLISHED, &local_ip, &remote_ip, local_port, remote_port);
  780. pcb->rcv_nxt = 0x8000;
  781. /* create segments */
  782. p = tcp_create_rx_segment(pcb, &data_full_wnd[0], TCP_MSS, 0, 0, TCP_ACK);
  783. p_normal_fin = tcp_create_rx_segment(pcb, NULL, 0, TCP_MSS, 0, TCP_ACK|TCP_FIN);
  784. k = 1;
  785. p_data_after_fin = tcp_create_rx_segment(pcb, &data_full_wnd[TCP_MSS+1], k, TCP_MSS+1, 0, TCP_ACK);
  786. p_2nd_fin_ooseq = tcp_create_rx_segment(pcb, NULL, 0, TCP_MSS+1+k, 0, TCP_ACK|TCP_FIN);
  787. if(delay_packet & 1) {
  788. /* drop normal data */
  789. first_dropped = 1;
  790. } else {
  791. /* send normal data */
  792. test_tcp_input(p, &netif);
  793. exp_rx_calls++;
  794. exp_rx_bytes += TCP_MSS;
  795. }
  796. /* check if counters are as expected */
  797. check_rx_counters(pcb, &counters, exp_close_calls, exp_rx_calls, exp_rx_bytes, 0, exp_oos_pbufs, exp_oos_tcplen);
  798. if(delay_packet & 2) {
  799. /* drop FIN */
  800. if(first_dropped > 2) {
  801. first_dropped = 2;
  802. }
  803. } else {
  804. /* send FIN */
  805. test_tcp_input(p_normal_fin, &netif);
  806. if (first_dropped < 2) {
  807. /* already dropped packets, this one is ooseq */
  808. exp_oos_pbufs++;
  809. exp_oos_tcplen++;
  810. } else {
  811. /* inseq */
  812. exp_close_calls++;
  813. }
  814. }
  815. /* check if counters are as expected */
  816. check_rx_counters(pcb, &counters, exp_close_calls, exp_rx_calls, exp_rx_bytes, 0, exp_oos_pbufs, exp_oos_tcplen);
  817. if(delay_packet & 4) {
  818. /* drop data-after-FIN */
  819. if(first_dropped > 3) {
  820. first_dropped = 3;
  821. }
  822. } else {
  823. /* send data-after-FIN */
  824. test_tcp_input(p_data_after_fin, &netif);
  825. if (first_dropped < 3) {
  826. /* already dropped packets, this one is ooseq */
  827. if (delay_packet & 2) {
  828. /* correct FIN was ooseq */
  829. exp_oos_pbufs++;
  830. exp_oos_tcplen += k;
  831. }
  832. } else {
  833. /* inseq: no change */
  834. }
  835. }
  836. /* check if counters are as expected */
  837. check_rx_counters(pcb, &counters, exp_close_calls, exp_rx_calls, exp_rx_bytes, 0, exp_oos_pbufs, exp_oos_tcplen);
  838. if(delay_packet & 8) {
  839. /* drop 2nd-FIN */
  840. if(first_dropped > 4) {
  841. first_dropped = 4;
  842. }
  843. } else {
  844. /* send 2nd-FIN */
  845. test_tcp_input(p_2nd_fin_ooseq, &netif);
  846. if (first_dropped < 3) {
  847. /* already dropped packets, this one is ooseq */
  848. if (delay_packet & 2) {
  849. /* correct FIN was ooseq */
  850. exp_oos_pbufs++;
  851. exp_oos_tcplen++;
  852. }
  853. } else {
  854. /* inseq: no change */
  855. }
  856. }
  857. /* check if counters are as expected */
  858. check_rx_counters(pcb, &counters, exp_close_calls, exp_rx_calls, exp_rx_bytes, 0, exp_oos_pbufs, exp_oos_tcplen);
  859. if(delay_packet & 1) {
  860. /* dropped normal data before */
  861. test_tcp_input(p, &netif);
  862. exp_rx_calls++;
  863. exp_rx_bytes += TCP_MSS;
  864. if((delay_packet & 2) == 0) {
  865. /* normal FIN was NOT delayed */
  866. exp_close_calls++;
  867. exp_oos_pbufs = exp_oos_tcplen = 0;
  868. }
  869. }
  870. /* check if counters are as expected */
  871. check_rx_counters(pcb, &counters, exp_close_calls, exp_rx_calls, exp_rx_bytes, 0, exp_oos_pbufs, exp_oos_tcplen);
  872. if(delay_packet & 2) {
  873. /* dropped normal FIN before */
  874. test_tcp_input(p_normal_fin, &netif);
  875. exp_close_calls++;
  876. exp_oos_pbufs = exp_oos_tcplen = 0;
  877. }
  878. /* check if counters are as expected */
  879. check_rx_counters(pcb, &counters, exp_close_calls, exp_rx_calls, exp_rx_bytes, 0, exp_oos_pbufs, exp_oos_tcplen);
  880. if(delay_packet & 4) {
  881. /* dropped data-after-FIN before */
  882. test_tcp_input(p_data_after_fin, &netif);
  883. }
  884. /* check if counters are as expected */
  885. check_rx_counters(pcb, &counters, exp_close_calls, exp_rx_calls, exp_rx_bytes, 0, exp_oos_pbufs, exp_oos_tcplen);
  886. if(delay_packet & 8) {
  887. /* dropped 2nd-FIN before */
  888. test_tcp_input(p_2nd_fin_ooseq, &netif);
  889. }
  890. /* check if counters are as expected */
  891. check_rx_counters(pcb, &counters, exp_close_calls, exp_rx_calls, exp_rx_bytes, 0, exp_oos_pbufs, exp_oos_tcplen);
  892. /* check that ooseq data has been dumped */
  893. EXPECT(pcb->ooseq == NULL);
  894. /* make sure the pcb is freed */
  895. EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 1);
  896. tcp_abort(pcb);
  897. EXPECT(MEMP_STATS_GET(used, MEMP_TCP_PCB) == 0);
  898. }
  899. /** create multiple segments and pass them to tcp_input with the first segment missing
  900. * to simulate overruning the rxwin with ooseq queueing enabled */
  901. #define FIN_TEST(name, num) \
  902. START_TEST(name) \
  903. { \
  904. LWIP_UNUSED_ARG(_i); \
  905. test_tcp_recv_ooseq_double_FINs(num); \
  906. } \
  907. END_TEST
  908. FIN_TEST(test_tcp_recv_ooseq_double_FIN_0, 0)
  909. FIN_TEST(test_tcp_recv_ooseq_double_FIN_1, 1)
  910. FIN_TEST(test_tcp_recv_ooseq_double_FIN_2, 2)
  911. FIN_TEST(test_tcp_recv_ooseq_double_FIN_3, 3)
  912. FIN_TEST(test_tcp_recv_ooseq_double_FIN_4, 4)
  913. FIN_TEST(test_tcp_recv_ooseq_double_FIN_5, 5)
  914. FIN_TEST(test_tcp_recv_ooseq_double_FIN_6, 6)
  915. FIN_TEST(test_tcp_recv_ooseq_double_FIN_7, 7)
  916. FIN_TEST(test_tcp_recv_ooseq_double_FIN_8, 8)
  917. FIN_TEST(test_tcp_recv_ooseq_double_FIN_9, 9)
  918. FIN_TEST(test_tcp_recv_ooseq_double_FIN_10, 10)
  919. FIN_TEST(test_tcp_recv_ooseq_double_FIN_11, 11)
  920. FIN_TEST(test_tcp_recv_ooseq_double_FIN_12, 12)
  921. FIN_TEST(test_tcp_recv_ooseq_double_FIN_13, 13)
  922. FIN_TEST(test_tcp_recv_ooseq_double_FIN_14, 14)
  923. FIN_TEST(test_tcp_recv_ooseq_double_FIN_15, 15)
  924. /** Create the suite including all tests for this module */
  925. Suite *
  926. tcp_oos_suite(void)
  927. {
  928. testfunc tests[] = {
  929. TESTFUNC(test_tcp_recv_ooseq_FIN_OOSEQ),
  930. TESTFUNC(test_tcp_recv_ooseq_FIN_INSEQ),
  931. TESTFUNC(test_tcp_recv_ooseq_overrun_rxwin),
  932. TESTFUNC(test_tcp_recv_ooseq_overrun_rxwin_edge),
  933. TESTFUNC(test_tcp_recv_ooseq_max_bytes),
  934. TESTFUNC(test_tcp_recv_ooseq_max_pbufs),
  935. TESTFUNC(test_tcp_recv_ooseq_double_FIN_0),
  936. TESTFUNC(test_tcp_recv_ooseq_double_FIN_1),
  937. TESTFUNC(test_tcp_recv_ooseq_double_FIN_2),
  938. TESTFUNC(test_tcp_recv_ooseq_double_FIN_3),
  939. TESTFUNC(test_tcp_recv_ooseq_double_FIN_4),
  940. TESTFUNC(test_tcp_recv_ooseq_double_FIN_5),
  941. TESTFUNC(test_tcp_recv_ooseq_double_FIN_6),
  942. TESTFUNC(test_tcp_recv_ooseq_double_FIN_7),
  943. TESTFUNC(test_tcp_recv_ooseq_double_FIN_8),
  944. TESTFUNC(test_tcp_recv_ooseq_double_FIN_9),
  945. TESTFUNC(test_tcp_recv_ooseq_double_FIN_10),
  946. TESTFUNC(test_tcp_recv_ooseq_double_FIN_11),
  947. TESTFUNC(test_tcp_recv_ooseq_double_FIN_12),
  948. TESTFUNC(test_tcp_recv_ooseq_double_FIN_13),
  949. TESTFUNC(test_tcp_recv_ooseq_double_FIN_14),
  950. TESTFUNC(test_tcp_recv_ooseq_double_FIN_15)
  951. };
  952. return create_suite("TCP_OOS", tests, sizeof(tests)/sizeof(testfunc), tcp_oos_setup, tcp_oos_teardown);
  953. }