1 /*
2  * FreeRTOS+TCP <DEVELOPMENT BRANCH>
3  * Copyright (C) 2022 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
4  *
5  * SPDX-License-Identifier: MIT
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy of
8  * this software and associated documentation files (the "Software"), to deal in
9  * the Software without restriction, including without limitation the rights to
10  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11  * the Software, and to permit persons to whom the Software is furnished to do so,
12  * subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in all
15  * copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  * http://aws.amazon.com/freertos
25  * http://www.FreeRTOS.org
26  */
27 
28 
29 /* Include Unity header */
30 #include "unity.h"
31 
32 /* Include standard libraries */
33 #include <stdlib.h>
34 #include <string.h>
35 #include <stdint.h>
36 
37 
38 #include "mock_FreeRTOS_IP.h"
39 #include "mock_FreeRTOS_IP_Private.h"
40 #include "mock_FreeRTOS_IP_Utils.h"
41 #include "mock_NetworkBufferManagement.h"
42 #include "mock_NetworkInterface.h"
43 #include "mock_FreeRTOS_Sockets.h"
44 #include "mock_FreeRTOS_Stream_Buffer.h"
45 #include "mock_FreeRTOS_TCP_WIN.h"
46 #include "mock_FreeRTOS_TCP_Transmission.h"
47 
48 #include "FreeRTOS_TCP_IP.h"
49 
50 #include "catch_assert.h"
51 
52 #include "FreeRTOSIPConfig.h"
53 
54 #include "FreeRTOS_TCP_Reception_stubs.c"
55 #include "FreeRTOS_TCP_Reception.h"
56 
57 
58 
59 BaseType_t prvCheckOptions( FreeRTOS_Socket_t * pxSocket,
60                             const NetworkBufferDescriptor_t * pxNetworkBuffer );
61 BaseType_t prvTCPSendReset( NetworkBufferDescriptor_t * pxNetworkBuffer );
62 
63 FreeRTOS_Socket_t xSocket, * pxSocket;
64 NetworkBufferDescriptor_t xNetworkBuffer, * pxNetworkBuffer;
65 uint8_t ucEthernetBuffer[ ipconfigNETWORK_MTU ] =
66 {
67     0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x08, 0x00, 0x45, 0x00,
68     0x00, 0x34, 0x15, 0xc2, 0x40, 0x00, 0x40, 0x06, 0xa8, 0x8e, 0xc0, 0xa8, 0x00, 0x08, 0xac, 0xd9,
69     0x0e, 0xea, 0xea, 0xfe, 0x01, 0xbb, 0x8b, 0xaf, 0x8a, 0x24, 0xdc, 0x96, 0x95, 0x7a, 0x80, 0x10,
70     0x01, 0xf5, 0x7c, 0x9a, 0x00, 0x00, 0x01, 0x01, 0x08, 0x0a, 0xb8, 0x53, 0x57, 0x27, 0xb2, 0xce,
71     0xc3, 0x17
72 };
73 
74 
75 const uint8_t ucTCPOptions_good_MSS_WSF[ ipSIZE_TCP_OPTIONS ] =
76 {
77     0x02, 0x04, 0x12, 0x34, /* MSS */
78     0x01,                   /* noop */
79     0x03, 0x03, 0x10,       /* WSF */
80     0x01,                   /* noop */
81     0x04, 0x02,             /* SACKP */
82     0x00                    /* EOL */
83 };
84 
85 const uint8_t ucTCPOptions_bad_MSS_WSF[ ipSIZE_TCP_OPTIONS ] =
86 {
87     0x02, 0x04, 0x12, 0x34, /* MSS */
88     0x01,                   /* noop */
89     0x03, 0x03, 0x10,       /* WSF */
90     0x01,                   /* noop */
91     0x08, 0x0a, 0x01        /* bad TS */
92 };
93 
94 const uint8_t ucTCPOptions_good_MSS_WSF_woEND[ ipSIZE_TCP_OPTIONS ] =
95 {
96     0x02, 0x04, 0x12, 0x34, /* MSS */
97     0x01,                   /* noop */
98     0x03, 0x03, 0x10,       /* WSF */
99     0x01,                   /* noop */
100     0x04, 0x02,             /* SACKP */
101     0x01                    /* noop */
102 };
103 
104 const uint8_t ucTCPOptions_good_SACK[ ipSIZE_TCP_OPTIONS ] =
105 {
106     0x05, 0x0A, 0x10, 0x00, 0x05, 0x00, 0x11, 0x11, 0x22, 0x22,
107     0x00,
108     0x00
109 };
110 
111 const uint8_t ucTCPOptions_good_TS[ ipSIZE_TCP_OPTIONS ] =
112 {
113     0x08, 0x0A, 0x12, 0x34, 0x56, 0x78, 0x11, 0x22, 0x33, 0x44,
114     0x00,
115     0x00
116 };
117 
118 /* Test for prvCheckOptions function. */
test_prvCheckOptions_No_Option(void)119 void test_prvCheckOptions_No_Option( void )
120 {
121     BaseType_t xReturn;
122 
123     /* Setup TCP option for tests */
124     pxSocket = &xSocket;
125     pxNetworkBuffer = &xNetworkBuffer;
126     pxNetworkBuffer->pucEthernetBuffer = ucEthernetBuffer;
127     size_t uxTCPHeaderOffset = ipSIZE_OF_ETH_HEADER + ipSIZE_OF_IPv4_HEADER;
128 
129     ProtocolHeaders_t * pxProtocolHeader = ( ( ProtocolHeaders_t * )
130                                              &( pxNetworkBuffer->pucEthernetBuffer[ ( size_t ) ipSIZE_OF_ETH_HEADER + ipSIZE_OF_IPv4_HEADER ] ) );
131     TCPHeader_t * pxTCPHeader = &( pxProtocolHeader->xTCPHeader );
132 
133     pxTCPHeader->ucTCPOffset = 64;
134 
135     uxIPHeaderSizePacket_ExpectAnyArgsAndReturn( ipSIZE_OF_IPv4_HEADER );
136 
137     xReturn = prvCheckOptions( pxSocket, pxNetworkBuffer );
138     TEST_ASSERT_EQUAL( pdPASS, xReturn );
139 }
140 
141 /* Test for prvCheckOptions function. */
test_prvCheckOptions_Invalid_Data_Length(void)142 void test_prvCheckOptions_Invalid_Data_Length( void )
143 {
144     BaseType_t xReturn;
145 
146     /* Setup TCP option for tests */
147     pxSocket = &xSocket;
148     pxNetworkBuffer = &xNetworkBuffer;
149     pxNetworkBuffer->pucEthernetBuffer = ucEthernetBuffer;
150     size_t uxTCPHeaderOffset = ipSIZE_OF_ETH_HEADER + ipSIZE_OF_IPv4_HEADER;
151 
152     ProtocolHeaders_t * pxProtocolHeader = ( ( ProtocolHeaders_t * )
153                                              &( pxNetworkBuffer->pucEthernetBuffer[ ( size_t ) ipSIZE_OF_ETH_HEADER + ipSIZE_OF_IPv4_HEADER ] ) );
154     TCPHeader_t * pxTCPHeader = &( pxProtocolHeader->xTCPHeader );
155 
156     pxTCPHeader->ucTCPOffset = 0x80;
157     pxNetworkBuffer->xDataLength = 50;
158 
159     uxIPHeaderSizePacket_ExpectAnyArgsAndReturn( ipSIZE_OF_IPv4_HEADER );
160 
161     xReturn = prvCheckOptions( pxSocket, pxNetworkBuffer );
162     TEST_ASSERT_EQUAL( pdPASS, xReturn );
163 }
164 
165 /* Test for prvCheckOptions function. */
test_prvCheckOptions_Invalid_Option_Length(void)166 void test_prvCheckOptions_Invalid_Option_Length( void )
167 {
168     BaseType_t xReturn;
169 
170     /* Setup TCP option for tests */
171     pxSocket = &xSocket;
172     pxNetworkBuffer = &xNetworkBuffer;
173     pxNetworkBuffer->pucEthernetBuffer = ucEthernetBuffer;
174     size_t uxTCPHeaderOffset = ipSIZE_OF_ETH_HEADER + ipSIZE_OF_IPv4_HEADER;
175 
176     ProtocolHeaders_t * pxProtocolHeader = ( ( ProtocolHeaders_t * )
177                                              &( pxNetworkBuffer->pucEthernetBuffer[ ( size_t ) ipSIZE_OF_ETH_HEADER + ipSIZE_OF_IPv4_HEADER ] ) );
178     TCPHeader_t * pxTCPHeader = &( pxProtocolHeader->xTCPHeader );
179 
180     pxTCPHeader->ucTCPOffset = 0x80;
181     pxNetworkBuffer->xDataLength = 60;
182 
183     uxIPHeaderSizePacket_ExpectAnyArgsAndReturn( ipSIZE_OF_IPv4_HEADER );
184 
185     xReturn = prvCheckOptions( pxSocket, pxNetworkBuffer );
186     TEST_ASSERT_EQUAL( pdPASS, xReturn );
187 }
188 
189 /* Test for prvCheckOptions function. */
test_prvCheckOptions_MSS_WSF(void)190 void test_prvCheckOptions_MSS_WSF( void )
191 {
192     BaseType_t xReturn;
193 
194     /* Setup TCP option for tests */
195     pxSocket = &xSocket;
196     pxNetworkBuffer = &xNetworkBuffer;
197     pxNetworkBuffer->pucEthernetBuffer = ucEthernetBuffer;
198     size_t uxTCPHeaderOffset = ipSIZE_OF_ETH_HEADER + ipSIZE_OF_IPv4_HEADER;
199 
200     ProtocolHeaders_t * pxProtocolHeader = ( ( ProtocolHeaders_t * )
201                                              &( pxNetworkBuffer->pucEthernetBuffer[ ( size_t ) ipSIZE_OF_ETH_HEADER + ipSIZE_OF_IPv4_HEADER ] ) );
202     TCPHeader_t * pxTCPHeader = &( pxProtocolHeader->xTCPHeader );
203 
204     pxTCPHeader->ucTCPOffset = 0x80;
205     pxNetworkBuffer->xDataLength = 0x50;
206     memcpy( ( void * ) pxTCPHeader->ucOptdata, ( void * ) ucTCPOptions_good_MSS_WSF, sizeof( ucTCPOptions_good_MSS_WSF ) );
207 
208     TCPWindow_t tcpWindow;
209     tcpWindow.usMSS = 536;
210     pxSocket->u.xTCP.usMSS = 1400;
211 
212     uxIPHeaderSizePacket_ExpectAnyArgsAndReturn( ipSIZE_OF_IPv4_HEADER );
213     usChar2u16_ExpectAnyArgsAndReturn( 500 );
214 
215     xReturn = prvCheckOptions( pxSocket, pxNetworkBuffer );
216     TEST_ASSERT_EQUAL( pdPASS, xReturn );
217 }
218 
219 /* Test for prvCheckOptions function. */
test_prvCheckOptions_MSS_WSF_Bad_Option(void)220 void test_prvCheckOptions_MSS_WSF_Bad_Option( void )
221 {
222     BaseType_t xReturn;
223 
224     /* Setup TCP option for tests */
225     pxSocket = &xSocket;
226     pxNetworkBuffer = &xNetworkBuffer;
227     pxNetworkBuffer->pucEthernetBuffer = ucEthernetBuffer;
228     size_t uxTCPHeaderOffset = ipSIZE_OF_ETH_HEADER + ipSIZE_OF_IPv4_HEADER;
229 
230     ProtocolHeaders_t * pxProtocolHeader = ( ( ProtocolHeaders_t * )
231                                              &( pxNetworkBuffer->pucEthernetBuffer[ ( size_t ) ipSIZE_OF_ETH_HEADER + ipSIZE_OF_IPv4_HEADER ] ) );
232     TCPHeader_t * pxTCPHeader = &( pxProtocolHeader->xTCPHeader );
233 
234     pxTCPHeader->ucTCPOffset = 0x80;
235     pxNetworkBuffer->xDataLength = 0x50;
236     memcpy( ( void * ) pxTCPHeader->ucOptdata, ( void * ) ucTCPOptions_bad_MSS_WSF, sizeof( ucTCPOptions_bad_MSS_WSF ) );
237 
238     TCPWindow_t tcpWindow;
239     tcpWindow.usMSS = 536;
240     pxSocket->u.xTCP.usMSS = 1400;
241 
242     uxIPHeaderSizePacket_ExpectAnyArgsAndReturn( ipSIZE_OF_IPv4_HEADER );
243     usChar2u16_ExpectAnyArgsAndReturn( 500 );
244 
245     xReturn = prvCheckOptions( pxSocket, pxNetworkBuffer );
246     TEST_ASSERT_EQUAL( pdFAIL, xReturn );
247 }
248 
249 /* Test for prvCheckOptions function. */
test_prvCheckOptions_MSS_WSF_Without_END(void)250 void test_prvCheckOptions_MSS_WSF_Without_END( void )
251 {
252     BaseType_t xReturn;
253 
254     /* Setup TCP option for tests */
255     pxSocket = &xSocket;
256     pxNetworkBuffer = &xNetworkBuffer;
257     pxNetworkBuffer->pucEthernetBuffer = ucEthernetBuffer;
258     size_t uxTCPHeaderOffset = ipSIZE_OF_ETH_HEADER + ipSIZE_OF_IPv4_HEADER;
259 
260     ProtocolHeaders_t * pxProtocolHeader = ( ( ProtocolHeaders_t * )
261                                              &( pxNetworkBuffer->pucEthernetBuffer[ ( size_t ) ipSIZE_OF_ETH_HEADER + ipSIZE_OF_IPv4_HEADER ] ) );
262     TCPHeader_t * pxTCPHeader = &( pxProtocolHeader->xTCPHeader );
263 
264     pxTCPHeader->ucTCPOffset = 0x80;
265     pxNetworkBuffer->xDataLength = 0x50;
266     memcpy( ( void * ) pxTCPHeader->ucOptdata, ( void * ) ucTCPOptions_good_MSS_WSF_woEND, sizeof( ucTCPOptions_good_MSS_WSF_woEND ) );
267 
268     TCPWindow_t tcpWindow;
269     tcpWindow.usMSS = 536;
270     pxSocket->u.xTCP.usMSS = 1400;
271     uxIPHeaderSizePacket_ExpectAnyArgsAndReturn( ipSIZE_OF_IPv4_HEADER );
272     usChar2u16_ExpectAnyArgsAndReturn( 500 );
273     xReturn = prvCheckOptions( pxSocket, pxNetworkBuffer );
274     TEST_ASSERT_EQUAL( pdPASS, xReturn );
275 }
276 
277 /* Test for prvCheckOptions function. */
test_prvCheckOptions_MSS_WSF_SYN_on(void)278 void test_prvCheckOptions_MSS_WSF_SYN_on( void )
279 {
280     BaseType_t xReturn;
281 
282     /* Setup TCP option for tests */
283     pxSocket = &xSocket;
284     pxNetworkBuffer = &xNetworkBuffer;
285     pxNetworkBuffer->pucEthernetBuffer = ucEthernetBuffer;
286     size_t uxTCPHeaderOffset = ipSIZE_OF_ETH_HEADER + ipSIZE_OF_IPv4_HEADER;
287 
288     ProtocolHeaders_t * pxProtocolHeader = ( ( ProtocolHeaders_t * )
289                                              &( pxNetworkBuffer->pucEthernetBuffer[ ( size_t ) ipSIZE_OF_ETH_HEADER + ipSIZE_OF_IPv4_HEADER ] ) );
290     TCPHeader_t * pxTCPHeader = &( pxProtocolHeader->xTCPHeader );
291 
292     pxTCPHeader->ucTCPOffset = 0x80;
293     pxNetworkBuffer->xDataLength = 0x50;
294     pxTCPHeader->ucTCPFlags |= tcpTCP_FLAG_SYN;
295     memcpy( ( void * ) pxTCPHeader->ucOptdata, ( void * ) ucTCPOptions_good_MSS_WSF, sizeof( ucTCPOptions_good_MSS_WSF ) );
296 
297     TCPWindow_t tcpWindow;
298     tcpWindow.usMSS = 536;
299     pxSocket->u.xTCP.usMSS = 1400;
300     uxIPHeaderSizePacket_ExpectAnyArgsAndReturn( ipSIZE_OF_IPv4_HEADER );
301     usChar2u16_ExpectAnyArgsAndReturn( 500 );
302     xReturn = prvCheckOptions( pxSocket, pxNetworkBuffer );
303     TEST_ASSERT_EQUAL( pdPASS, xReturn );
304 }
305 
306 /* Test for prvSingleStepTCPHeaderOptions function. */
test_prvSingleStepTCPHeaderOptions_SACK(void)307 void test_prvSingleStepTCPHeaderOptions_SACK( void )
308 {
309     int32_t result;
310 
311     /* Setup TCP option for tests */
312     pxNetworkBuffer = &xNetworkBuffer;
313     pxNetworkBuffer->pucEthernetBuffer = ucEthernetBuffer;
314     size_t uxTCPHeaderOffset = ipSIZE_OF_ETH_HEADER + ipSIZE_OF_IPv4_HEADER;
315     ProtocolHeaders_t * pxProtocolHeader = ( ( ProtocolHeaders_t * )
316                                              &( pxNetworkBuffer->pucEthernetBuffer[ uxTCPHeaderOffset ] ) );
317     TCPHeader_t * pxTCPHeader = &( pxProtocolHeader->xTCPHeader );
318 
319     pxTCPHeader->ucTCPOffset = 0x80;
320     const uint8_t ucTCPOptions_good_SACK[ ipSIZE_TCP_OPTIONS ] =
321     {
322         0x05, 0x0A, 0x10, 0x00, 0x05, 0x00, 0x11, 0x11, 0x22, 0x22
323     };
324     memcpy( ( void * ) pxTCPHeader->ucOptdata, ( void * ) ucTCPOptions_good_SACK, sizeof( ucTCPOptions_good_SACK ) );
325 
326     ulChar2u32_ExpectAnyArgsAndReturn( 0x12345678 );
327     ulChar2u32_ExpectAnyArgsAndReturn( 0x12345678 );
328     ulTCPWindowTxSack_ExpectAnyArgsAndReturn( 5 );
329     result = prvSingleStepTCPHeaderOptions(
330         pxTCPHeader->ucOptdata,
331         10,
332         pxSocket,
333         pdTRUE );
334 
335     TEST_ASSERT_EQUAL( 10, result );
336 }
337 
338 /* Test for prvSingleStepTCPHeaderOptions function. */
test_prvSingleStepTCPHeaderOptions_TS(void)339 void test_prvSingleStepTCPHeaderOptions_TS( void )
340 {
341     int32_t result;
342 
343     /* Setup TCP option for tests */
344     pxNetworkBuffer = &xNetworkBuffer;
345     pxNetworkBuffer->pucEthernetBuffer = ucEthernetBuffer;
346     size_t uxTCPHeaderOffset = ipSIZE_OF_ETH_HEADER + ipSIZE_OF_IPv4_HEADER;
347 
348     ProtocolHeaders_t * pxProtocolHeader = ( ( ProtocolHeaders_t * )
349                                              &( pxNetworkBuffer->pucEthernetBuffer[ uxTCPHeaderOffset ] ) );
350     TCPHeader_t * pxTCPHeader = &( pxProtocolHeader->xTCPHeader );
351 
352     pxTCPHeader->ucTCPOffset = 0x80;
353     pxNetworkBuffer->xDataLength = 0x50;
354     const uint8_t ucTCPOptions_good_SACK[ ipSIZE_TCP_OPTIONS ] =
355     {
356         0x08, 0x01, 0x10, 0x00, 0x05, 0x00, 0x11, 0x11, 0x22, 0x22
357     };
358     memcpy( ( void * ) pxTCPHeader->ucOptdata, ( void * ) ucTCPOptions_good_SACK, sizeof( ucTCPOptions_good_SACK ) );
359 
360 
361     result = prvSingleStepTCPHeaderOptions(
362         pxTCPHeader->ucOptdata,
363         10,
364         pxSocket,
365         pdTRUE );
366 
367     TEST_ASSERT_EQUAL( -1, result );
368 }
369 
370 /* Test for prvSingleStepTCPHeaderOptions function. */
test_prvSingleStepTCPHeaderOptions_END_NOOP(void)371 void test_prvSingleStepTCPHeaderOptions_END_NOOP( void )
372 {
373     int32_t result;
374 
375     /* Setup TCP option for tests */
376     pxNetworkBuffer = &xNetworkBuffer;
377     pxNetworkBuffer->pucEthernetBuffer = ucEthernetBuffer;
378 
379     ProtocolHeaders_t * pxProtocolHeader = ( ( ProtocolHeaders_t * )
380                                              &( pxNetworkBuffer->pucEthernetBuffer[ ( size_t ) ipSIZE_OF_ETH_HEADER + ipSIZE_OF_IPv4_HEADER ] ) );
381     TCPHeader_t * pxTCPHeader = &( pxProtocolHeader->xTCPHeader );
382 
383     pxTCPHeader->ucTCPOffset = 0x80;
384     pxNetworkBuffer->xDataLength = 0x50;
385     uint8_t ucTCPOptions = 0x00;
386     memcpy( ( void * ) pxTCPHeader->ucOptdata, ( void * ) &ucTCPOptions, sizeof( ucTCPOptions ) );
387 
388 
389     result = prvSingleStepTCPHeaderOptions(
390         ( const uint8_t * const ) pxTCPHeader->ucOptdata,
391         1,
392         pxSocket,
393         pdTRUE );
394 
395     TEST_ASSERT_EQUAL( 0, result );
396 
397     ucTCPOptions = 0x01;
398     memcpy( ( void * ) pxTCPHeader->ucOptdata, ( void * ) &ucTCPOptions, sizeof( ucTCPOptions ) );
399 
400 
401     result = prvSingleStepTCPHeaderOptions(
402         ( const uint8_t * const ) pxTCPHeader->ucOptdata,
403         1,
404         pxSocket,
405         pdTRUE );
406 
407     TEST_ASSERT_EQUAL( 1, result );
408 }
409 
410 /* Test for prvSingleStepTCPHeaderOptions function. */
test_prvSingleStepTCPHeaderOptions_MSS_Invalid_Length(void)411 void test_prvSingleStepTCPHeaderOptions_MSS_Invalid_Length( void )
412 {
413     int32_t result;
414 
415     /* Setup TCP option for tests */
416     pxNetworkBuffer = &xNetworkBuffer;
417     pxNetworkBuffer->pucEthernetBuffer = ucEthernetBuffer;
418     size_t uxTCPHeaderOffset = ipSIZE_OF_ETH_HEADER + ipSIZE_OF_IPv4_HEADER;
419 
420     ProtocolHeaders_t * pxProtocolHeader = ( ( ProtocolHeaders_t * )
421                                              &( pxNetworkBuffer->pucEthernetBuffer[ ( size_t ) ipSIZE_OF_ETH_HEADER + ipSIZE_OF_IPv4_HEADER ] ) );
422     TCPHeader_t * pxTCPHeader = &( pxProtocolHeader->xTCPHeader );
423 
424     pxTCPHeader->ucTCPOffset = 0x80;
425     pxNetworkBuffer->xDataLength = 0x50;
426     uint8_t ucTCPOptions[] = { 0x02, 0x04, 0x12, 0x34 };
427     memcpy( ( void * ) pxTCPHeader->ucOptdata, ( void * ) &ucTCPOptions, sizeof( ucTCPOptions ) );
428 
429 
430     result = prvSingleStepTCPHeaderOptions(
431         pxTCPHeader->ucOptdata,
432         1,
433         pxSocket,
434         pdTRUE );
435 
436     TEST_ASSERT_EQUAL( -1, result );
437 
438     result = prvSingleStepTCPHeaderOptions(
439         pxTCPHeader->ucOptdata,
440         3,
441         pxSocket,
442         pdTRUE );
443 
444     TEST_ASSERT_EQUAL( -1, result );
445 
446     ucTCPOptions[ 1 ] = 0x03;
447     memcpy( ( void * ) pxTCPHeader->ucOptdata, ( void * ) &ucTCPOptions, sizeof( ucTCPOptions ) );
448     result = prvSingleStepTCPHeaderOptions(
449         ( const uint8_t * const ) pxTCPHeader->ucOptdata,
450         4,
451         pxSocket,
452         pdTRUE );
453 
454     TEST_ASSERT_EQUAL( -1, result );
455 }
456 
457 /* Test for prvSingleStepTCPHeaderOptions function. */
test_prvSingleStepTCPHeaderOptions_Zero_Length_MSS(void)458 void test_prvSingleStepTCPHeaderOptions_Zero_Length_MSS( void )
459 {
460     int32_t result;
461 
462     /* Setup TCP option for tests */
463     pxNetworkBuffer = &xNetworkBuffer;
464     pxNetworkBuffer->pucEthernetBuffer = ucEthernetBuffer;
465     size_t uxTCPHeaderOffset = ipSIZE_OF_ETH_HEADER + ipSIZE_OF_IPv4_HEADER;
466 
467     ProtocolHeaders_t * pxProtocolHeader = ( ( ProtocolHeaders_t * )
468                                              &( pxNetworkBuffer->pucEthernetBuffer[ ( size_t ) ipSIZE_OF_ETH_HEADER + ipSIZE_OF_IPv4_HEADER ] ) );
469     TCPHeader_t * pxTCPHeader = &( pxProtocolHeader->xTCPHeader );
470 
471     pxTCPHeader->ucTCPOffset = 0x80;
472     pxNetworkBuffer->xDataLength = 0x50;
473     const uint8_t ucTCPOptions[] = { 0x02, 0x04, 0x0, 0x0 };
474     memcpy( ( void * ) pxTCPHeader->ucOptdata, ( void * ) &ucTCPOptions, sizeof( ucTCPOptions ) );
475     usChar2u16_ExpectAnyArgsAndReturn( 0 );
476 
477     result = prvSingleStepTCPHeaderOptions(
478         pxTCPHeader->ucOptdata,
479         4,
480         pxSocket,
481         pdTRUE );
482 
483     TEST_ASSERT_EQUAL( -1, result );
484 }
485 
486 /* Test for prvSingleStepTCPHeaderOptions function. */
test_prvSingleStepTCPHeaderOptions_Same_MSS(void)487 void test_prvSingleStepTCPHeaderOptions_Same_MSS( void )
488 {
489     int32_t result;
490 
491     /* Setup TCP option for tests */
492     pxNetworkBuffer = &xNetworkBuffer;
493     pxNetworkBuffer->pucEthernetBuffer = ucEthernetBuffer;
494     size_t uxTCPHeaderOffset = ipSIZE_OF_ETH_HEADER + ipSIZE_OF_IPv4_HEADER;
495     ProtocolHeaders_t * pxProtocolHeader = ( ( ProtocolHeaders_t * )
496                                              &( pxNetworkBuffer->pucEthernetBuffer[ ( size_t ) ipSIZE_OF_ETH_HEADER + ipSIZE_OF_IPv4_HEADER ] ) );
497     TCPHeader_t * pxTCPHeader = &( pxProtocolHeader->xTCPHeader );
498 
499     pxTCPHeader->ucTCPOffset = 0x80;
500     const uint8_t ucTCPOptions[] = { 0x02, 0x04, 0x0, 0x0 };
501     memcpy( ( void * ) pxTCPHeader->ucOptdata, ( void * ) &ucTCPOptions, sizeof( ucTCPOptions ) );
502 
503     TCPWindow_t tcpWindow;
504     tcpWindow.usMSS = 536;
505     pxSocket->u.xTCP.usMSS = 536;
506     usChar2u16_ExpectAnyArgsAndReturn( 536 );
507 
508     result = prvSingleStepTCPHeaderOptions(
509         pxTCPHeader->ucOptdata,
510         4,
511         pxSocket,
512         pdTRUE );
513 
514     TEST_ASSERT_EQUAL( 4, result );
515 }
516 
517 /* Test for prvSingleStepTCPHeaderOptions function. */
test_prvSingleStepTCPHeaderOptions_Invalid_Length_WS(void)518 void test_prvSingleStepTCPHeaderOptions_Invalid_Length_WS( void )
519 {
520     int32_t result;
521 
522     /* Setup TCP option for tests */
523     pxNetworkBuffer = &xNetworkBuffer;
524     pxNetworkBuffer->pucEthernetBuffer = ucEthernetBuffer;
525     size_t uxTCPHeaderOffset = ipSIZE_OF_ETH_HEADER + ipSIZE_OF_IPv4_HEADER;
526 
527     ProtocolHeaders_t * pxProtocolHeader = ( ( ProtocolHeaders_t * )
528                                              &( pxNetworkBuffer->pucEthernetBuffer[ ( size_t ) ipSIZE_OF_ETH_HEADER + ipSIZE_OF_IPv4_HEADER ] ) );
529     TCPHeader_t * pxTCPHeader = &( pxProtocolHeader->xTCPHeader );
530 
531     pxTCPHeader->ucTCPOffset = 0x80;
532     pxNetworkBuffer->xDataLength = 0x50;
533     uint8_t ucTCPOptions[] = { 0x03, 0x03, 0x10 };
534     memcpy( ( void * ) pxTCPHeader->ucOptdata, ( void * ) &ucTCPOptions, sizeof( ucTCPOptions ) );
535 
536 
537     result = prvSingleStepTCPHeaderOptions(
538         pxTCPHeader->ucOptdata,
539         2,
540         pxSocket,
541         pdTRUE );
542 
543     TEST_ASSERT_EQUAL( -1, result );
544 
545     ucTCPOptions[ 1 ] = 0x02;
546     memcpy( ( void * ) pxTCPHeader->ucOptdata, ( void * ) &ucTCPOptions, sizeof( ucTCPOptions ) );
547     result = prvSingleStepTCPHeaderOptions(
548         ( const uint8_t * const ) pxTCPHeader->ucOptdata,
549         3,
550         pxSocket,
551         pdTRUE );
552 
553     TEST_ASSERT_EQUAL( -1, result );
554 }
555 
556 static uint32_t ulCalled = 0;
xLocalFunctionPointer(Socket_t xSocket,size_t xLength)557 static void xLocalFunctionPointer( Socket_t xSocket,
558                                    size_t xLength )
559 {
560     ulCalled++;
561 }
562 
563 /* Test for prvReadSackOption function */
test_prvReadSackOption(void)564 void test_prvReadSackOption( void )
565 {
566     pxSocket = &xSocket;
567     ulCalled = 0;
568 
569     pxSocket->u.xTCP.txStream = ( StreamBuffer_t * ) 0x12345678;
570     ulChar2u32_ExpectAnyArgsAndReturn( 0x12345678 );
571     ulChar2u32_ExpectAnyArgsAndReturn( 0x12346678 );
572     ulTCPWindowTxSack_ExpectAnyArgsAndReturn( 0x1000 );
573     uxStreamBufferGet_ExpectAnyArgsAndReturn( 0x1000 );
574     pxSocket->xSelectBits |= eSELECT_WRITE;
575     pxSocket->u.xTCP.pxHandleSent = xLocalFunctionPointer;
576 
577     const uint8_t ucTCPOptions[] =
578     {
579         0x05, 0x0A, 0x10, 0x00, 0x05, 0x00, 0x11, 0x11, 0x22, 0x22
580     };
581 
582     prvReadSackOption( ( const uint8_t * const ) &ucTCPOptions, 2, pxSocket );
583 
584     TEST_ASSERT_NOT_EQUAL( pxSocket->xEventBits & ( ( ( EventBits_t ) eSELECT_WRITE ) << SOCKET_EVENT_BIT_COUNT ), 0 );
585     TEST_ASSERT_EQUAL( 1, ulCalled );
586 }
587 
588 /* Test for prvReadSackOption function */
test_prvReadSackOption_Zero_Length_Block(void)589 void test_prvReadSackOption_Zero_Length_Block( void )
590 {
591     pxSocket = &xSocket;
592 
593     pxSocket->u.xTCP.txStream = ( StreamBuffer_t * ) 0x12345678;
594     ulChar2u32_ExpectAnyArgsAndReturn( 0x12345678 );
595     ulChar2u32_ExpectAnyArgsAndReturn( 0x12346678 );
596     ulTCPWindowTxSack_ExpectAnyArgsAndReturn( 0 );
597     pxSocket->xEventBits = 0;
598 
599     const uint8_t ucTCPOptions[] =
600     {
601         0x05, 0x0A, 0x10, 0x00, 0x05, 0x00, 0x11, 0x11, 0x22, 0x22
602     };
603 
604     prvReadSackOption( ( const uint8_t * const ) &ucTCPOptions, 2, pxSocket );
605 
606     TEST_ASSERT_NOT_EQUAL( 0, pxSocket->xEventBits ^ ( EventBits_t ) eSOCKET_SEND );
607 }
608 
609 /* Test for prvReadSackOption function */
test_prvReadSackOption_Selectbits_On(void)610 void test_prvReadSackOption_Selectbits_On( void )
611 {
612     pxSocket = &xSocket;
613     ulCalled = 0;
614 
615     pxSocket->u.xTCP.txStream = ( StreamBuffer_t * ) 0x12345678;
616     ulChar2u32_ExpectAnyArgsAndReturn( 0x12345678 );
617     ulChar2u32_ExpectAnyArgsAndReturn( 0x12346678 );
618     ulTCPWindowTxSack_ExpectAnyArgsAndReturn( 0x1000 );
619     uxStreamBufferGet_ExpectAnyArgsAndReturn( 0x1000 );
620     pxSocket->xEventBits = 0;
621     pxSocket->xSelectBits = 0;
622     pxSocket->u.xTCP.pxHandleSent = xLocalFunctionPointer;
623 
624 
625     const uint8_t ucTCPOptions[] =
626     {
627         0x05, 0x0A, 0x10, 0x00, 0x05, 0x00, 0x11, 0x11, 0x22, 0x22
628     };
629 
630     prvReadSackOption( ( const uint8_t * const ) &ucTCPOptions, 2, pxSocket );
631 
632     TEST_ASSERT_EQUAL( 0, pxSocket->xEventBits & ( ( ( EventBits_t ) eSELECT_WRITE ) << SOCKET_EVENT_BIT_COUNT ) );
633 }
634 
635 
636 /* Test for prvReadSackOption function */
test_prvReadSackOption_No_Handler(void)637 void test_prvReadSackOption_No_Handler( void )
638 {
639     pxSocket = &xSocket;
640     ulCalled = 0;
641 
642     pxSocket->u.xTCP.txStream = ( StreamBuffer_t * ) 0x12345678;
643     ulChar2u32_ExpectAnyArgsAndReturn( 0x12345678 );
644     ulChar2u32_ExpectAnyArgsAndReturn( 0x12346678 );
645     ulTCPWindowTxSack_ExpectAnyArgsAndReturn( 0x1000 );
646     uxStreamBufferGet_ExpectAnyArgsAndReturn( 0x1000 );
647     pxSocket->xSelectBits = 0;
648     pxSocket->u.xTCP.pxHandleSent = NULL;
649 
650     const uint8_t ucTCPOptions[] =
651     {
652         0x05, 0x0A, 0x10, 0x00, 0x05, 0x00, 0x11, 0x11, 0x22, 0x22
653     };
654 
655     prvReadSackOption( ( const uint8_t * const ) &ucTCPOptions, 2, pxSocket );
656 
657     TEST_ASSERT_EQUAL( 0, pxSocket->xEventBits & ( ( ( EventBits_t ) eSELECT_WRITE ) << SOCKET_EVENT_BIT_COUNT ) );
658     TEST_ASSERT_EQUAL( 0, ulCalled );
659 }
660 
661 uint8_t EthernetBuffer[ ipconfigNETWORK_MTU ] =
662 {
663     0x8c, 0xdc, 0xd4, 0x4a, 0xea, 0x02, 0xa0, 0x40, 0xa0, 0x3a, 0x21, 0xea, 0x08, 0x00, 0x45, 0x20,
664     0x00, 0x5b, 0xd2, 0xe9, 0x00, 0x00, 0x39, 0x06, 0x32, 0x47, 0xac, 0xd9, 0x0e, 0xc3, 0xc0, 0xa8,
665     0x00, 0x08, 0x01, 0xbb, 0xdc, 0x44, 0xe2, 0x34, 0xd4, 0x84, 0xa7, 0xa9, 0xc1, 0xd8, 0x80, 0x18,
666     0x01, 0x15, 0x2c, 0xed, 0x00, 0x00, 0x01, 0x01, 0x08, 0x0a, 0x7c, 0x17, 0x05, 0xb6, 0x9e, 0x62,
667     0x6f, 0x27, 0x17, 0x03, 0x03, 0x00, 0x22, 0x1c, 0xeb, 0x68, 0x29, 0xea, 0x20, 0x2d, 0xb2, 0x6f,
668     0x97, 0xdf, 0x26, 0xf5, 0x70, 0x9c, 0x09, 0xe0, 0x0d, 0xda, 0xf5, 0xf9, 0xd5, 0x37, 0x92, 0x4f,
669     0x81, 0xe7, 0x65, 0x1e, 0xb1, 0x77, 0xcc, 0x72, 0x11
670 };
671 
672 /**
673  * @brief This function validates getting the TCP payload data
674  *        and check the length of this data when frame type is IPv4.
675  */
test_prvCheckRxData_IPv4(void)676 void test_prvCheckRxData_IPv4( void )
677 {
678     int32_t result;
679 
680     /* Setup TCP option for tests */
681     pxNetworkBuffer = &xNetworkBuffer;
682 
683     pxNetworkBuffer->pucEthernetBuffer = EthernetBuffer;
684     uint8_t * pData;
685 
686     uxIPHeaderSizePacket_ExpectAnyArgsAndReturn( ipSIZE_OF_IPv4_HEADER );
687     uxIPHeaderSizePacket_ExpectAnyArgsAndReturn( ipSIZE_OF_IPv4_HEADER );
688 
689     result = prvCheckRxData( pxNetworkBuffer, &pData );
690 
691     TEST_ASSERT_EQUAL( 14, result );
692 }
693 
694 /**
695  * @brief This function validates getting the TCP payload data
696  *        and check the length of this data when frame type is IPv6.
697  */
test_prvCheckRxData_IPv6(void)698 void test_prvCheckRxData_IPv6( void )
699 {
700     int32_t result;
701     uint8_t EthernetBuffer[ ipconfigNETWORK_MTU ];
702     EthernetHeader_t * px_ethHeader;
703     uint8_t * pData;
704     IPHeader_IPv6_t * pxIPHeader;
705 
706     /* Setup TCP option for tests */
707     memset( EthernetBuffer, 0, sizeof( EthernetBuffer ) );
708     pxNetworkBuffer = &xNetworkBuffer;
709 
710     pxNetworkBuffer->pucEthernetBuffer = EthernetBuffer;
711     pxNetworkBuffer->xDataLength = 0x50;
712     px_ethHeader = ( EthernetHeader_t * ) pxNetworkBuffer->pucEthernetBuffer;
713     px_ethHeader->usFrameType = ipIPv6_FRAME_TYPE;
714     pxIPHeader = ( IPHeader_IPv6_t * ) &( pxNetworkBuffer->pucEthernetBuffer[ ipSIZE_OF_ETH_HEADER ] );
715     pxIPHeader->usPayloadLength = FreeRTOS_htons( pxNetworkBuffer->xDataLength - ( ipSIZE_OF_ETH_HEADER + ipSIZE_OF_IPv6_HEADER ) );
716 
717     uxIPHeaderSizePacket_ExpectAnyArgsAndReturn( ipSIZE_OF_IPv6_HEADER );
718     uxIPHeaderSizePacket_ExpectAnyArgsAndReturn( ipSIZE_OF_IPv6_HEADER );
719 
720     result = prvCheckRxData( pxNetworkBuffer, &pData );
721 
722     TEST_ASSERT_EQUAL( ( pxNetworkBuffer->xDataLength - ( ipSIZE_OF_ETH_HEADER + ipSIZE_OF_IPv6_HEADER ) ), result );
723 }
724 
725 /**
726  * @brief This function validates getting the TCP payload data
727  *        and check the length of this data when frame type is incorrect.
728  */
test_prvCheckRxData_IncorrectFrameType(void)729 void test_prvCheckRxData_IncorrectFrameType( void )
730 {
731     int32_t result;
732     uint8_t EthernetBuffer[ ipconfigNETWORK_MTU ];
733     EthernetHeader_t * px_ethHeader;
734     uint8_t * pData;
735 
736     /* Setup TCP option for tests */
737     memset( EthernetBuffer, 0, sizeof( EthernetBuffer ) );
738     pxNetworkBuffer = &xNetworkBuffer;
739 
740     pxNetworkBuffer->pucEthernetBuffer = EthernetBuffer;
741     px_ethHeader = ( EthernetHeader_t * ) pxNetworkBuffer->pucEthernetBuffer;
742     px_ethHeader->usFrameType = 0;
743 
744     uxIPHeaderSizePacket_ExpectAnyArgsAndReturn( 0 );
745     uxIPHeaderSizePacket_ExpectAnyArgsAndReturn( 0 );
746 
747     result = prvCheckRxData( pxNetworkBuffer, &pData );
748 
749     TEST_ASSERT_EQUAL( 0, result );
750 }
751 
752 
753 /* Test for prvCheckRxData function. */
test_prvCheckRxData_URG_On(void)754 void test_prvCheckRxData_URG_On( void )
755 {
756     int32_t result;
757 
758     /* Setup TCP option for tests */
759     pxNetworkBuffer = &xNetworkBuffer;
760     /* Set TCP Flag URG */
761     uint8_t EthernetBuffer_urg[ ipconfigNETWORK_MTU ] =
762     {
763         0x8c, 0xdc, 0xd4, 0x4a, 0xea, 0x02, 0xa0, 0x40, 0xa0, 0x3a, 0x21, 0xea, 0x08, 0x00, 0x45, 0x20,
764         0x00, 0x5b, 0xd2, 0xe9, 0x00, 0x00, 0x39, 0x06, 0x32, 0x47, 0xac, 0xd9, 0x0e, 0xc3, 0xc0, 0xa8,
765         0x00, 0x08, 0x01, 0xbb, 0xdc, 0x44, 0xe2, 0x34, 0xd4, 0x84, 0xa7, 0xa9, 0xc1, 0xd8, 0x80, 0x38,
766         0x01, 0x15, 0x2c, 0xed, 0x00, 0x10, 0x01, 0x01, 0x08, 0x0a, 0x7c, 0x17, 0x05, 0xb6, 0x9e, 0x62,
767         0x6f, 0x27, 0x17, 0x03, 0x03, 0x00, 0x22, 0x1c, 0xeb, 0x68, 0x29, 0xea, 0x20, 0x2d, 0xb2, 0x6f,
768         0x97, 0xdf, 0x26, 0xf5, 0x70, 0x9c, 0x09, 0xe0, 0x0d, 0xda, 0xf5, 0xf9, 0xd5, 0x37, 0x92, 0x4f,
769         0x81, 0xe7, 0x65, 0x1e, 0xb1, 0x77, 0xcc, 0x72, 0x11
770     };
771 
772 
773     pxNetworkBuffer->pucEthernetBuffer = EthernetBuffer_urg;
774     uint8_t * pData;
775 
776     uxIPHeaderSizePacket_ExpectAnyArgsAndReturn( ipSIZE_OF_IPv4_HEADER );
777     uxIPHeaderSizePacket_ExpectAnyArgsAndReturn( ipSIZE_OF_IPv4_HEADER );
778     FreeRTOS_min_int32_ExpectAnyArgsAndReturn( 10 );
779 
780     result = prvCheckRxData( pxNetworkBuffer, &pData );
781 
782     TEST_ASSERT_EQUAL( 4, result );
783 }
784 
785 /* Test for prvStorexData function. */
test_prvStoreRxData_Happy_Path(void)786 void test_prvStoreRxData_Happy_Path( void )
787 {
788     int32_t result;
789 
790     /* Setup TCP option for tests */
791     pxNetworkBuffer = &xNetworkBuffer;
792 
793     pxNetworkBuffer->pucEthernetBuffer = EthernetBuffer;
794     uint8_t * pData;
795 
796     uxIPHeaderSizePacket_ExpectAnyArgsAndReturn( ipSIZE_OF_IPv4_HEADER );
797     uxIPHeaderSizePacket_ExpectAnyArgsAndReturn( ipSIZE_OF_IPv4_HEADER );
798 
799     result = prvCheckRxData( pxNetworkBuffer, &pData );
800 
801     TEST_ASSERT_EQUAL( 14, result );
802 
803     BaseType_t xResult = 0;
804     xSocket.u.xTCP.uxRxStreamSize = 39;
805     xSocket.u.xTCP.eTCPState = eESTABLISHED;
806     xSocket.u.xTCP.rxStream = ( StreamBuffer_t * ) 0x12345678;
807     pxSocket = &xSocket;
808     TCPWindow_t * pxTCPWindow = &pxSocket->u.xTCP.xTCPWindow;
809     pxTCPWindow->ulUserDataLength = 14;
810 
811     uxIPHeaderSizePacket_ExpectAnyArgsAndReturn( ipSIZE_OF_IPv4_HEADER );
812     uxStreamBufferGetSpace_ExpectAnyArgsAndReturn( 100 );
813     lTCPWindowRxCheck_ExpectAnyArgsAndReturn( 20 );
814     lTCPAddRxdata_ExpectAnyArgsAndReturn( 14 );
815     lTCPAddRxdata_ExpectAnyArgsAndReturn( 14 );
816 
817 
818     xResult = prvStoreRxData( pxSocket,
819                               pData,
820                               pxNetworkBuffer,
821                               14 );
822 
823     TEST_ASSERT_EQUAL( 0, xResult );
824 }
825 
826 /* Test for prvStorexData function. */
test_prvStoreRxData_Wrong_State(void)827 void test_prvStoreRxData_Wrong_State( void )
828 {
829     int32_t result;
830 
831     /* Setup TCP option for tests */
832     pxNetworkBuffer = &xNetworkBuffer;
833 
834     pxNetworkBuffer->pucEthernetBuffer = EthernetBuffer;
835     uint8_t * pData;
836 
837     uxIPHeaderSizePacket_ExpectAnyArgsAndReturn( ipSIZE_OF_IPv4_HEADER );
838     uxIPHeaderSizePacket_ExpectAnyArgsAndReturn( ipSIZE_OF_IPv4_HEADER );
839 
840     result = prvCheckRxData( pxNetworkBuffer, &pData );
841 
842     TEST_ASSERT_EQUAL( 14, result );
843 
844     BaseType_t xResult = 0;
845     xSocket.u.xTCP.eTCPState = eCONNECT_SYN;
846     pxSocket = &xSocket;
847 
848     uxIPHeaderSizePacket_ExpectAnyArgsAndReturn( ipSIZE_OF_IPv4_HEADER );
849 
850     xResult = prvStoreRxData( pxSocket,
851                               pData,
852                               pxNetworkBuffer,
853                               105 );
854 
855     TEST_ASSERT_EQUAL( 0, xResult );
856 }
857 
858 /* Test for prvStorexData function. */
test_prvStoreRxData_Zero_Length(void)859 void test_prvStoreRxData_Zero_Length( void )
860 {
861     int32_t result;
862 
863     /* Setup TCP option for tests */
864     pxNetworkBuffer = &xNetworkBuffer;
865     /* zero-length packet */
866     uint8_t EthernetBuffer_zl[ ipconfigNETWORK_MTU ] =
867     {
868         0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x08, 0x00, 0x45, 0x00,
869         0x00, 0x34, 0x15, 0xc2, 0x40, 0x00, 0x40, 0x06, 0xa8, 0x8e, 0xc0, 0xa8, 0x00, 0x08, 0xac, 0xd9,
870         0x0e, 0xea, 0xea, 0xfe, 0x01, 0xbb, 0x8b, 0xaf, 0x8a, 0x24, 0xdc, 0x96, 0x95, 0x7a, 0x80, 0x10,
871         0x01, 0xf5, 0x7c, 0x9a, 0x00, 0x00, 0x01, 0x01, 0x08, 0x0a, 0xb8, 0x53, 0x57, 0x27, 0xb2, 0xce,
872         0xc3, 0x17
873     };
874 
875     pxNetworkBuffer->pucEthernetBuffer = EthernetBuffer_zl;
876     uint8_t * pData;
877 
878     uxIPHeaderSizePacket_ExpectAnyArgsAndReturn( ipSIZE_OF_IPv4_HEADER );
879     uxIPHeaderSizePacket_ExpectAnyArgsAndReturn( ipSIZE_OF_IPv4_HEADER );
880 
881     result = prvCheckRxData( pxNetworkBuffer, &pData );
882 
883     TEST_ASSERT_EQUAL( 0, result );
884 
885     BaseType_t xResult = 0;
886     xSocket.u.xTCP.eTCPState = eESTABLISHED;
887     pxSocket = &xSocket;
888 
889     uxIPHeaderSizePacket_ExpectAnyArgsAndReturn( ipSIZE_OF_IPv4_HEADER );
890 
891     xResult = prvStoreRxData( pxSocket,
892                               pData,
893                               pxNetworkBuffer,
894                               0 );
895 
896     TEST_ASSERT_EQUAL( 0, xResult );
897 }
898 
899 
900 /* Test for prvStorexData function. */
test_prvStoreRxData_Null_RxStream(void)901 void test_prvStoreRxData_Null_RxStream( void )
902 {
903     int32_t result;
904 
905     /* Setup TCP option for tests */
906     pxNetworkBuffer = &xNetworkBuffer;
907 
908     pxNetworkBuffer->pucEthernetBuffer = EthernetBuffer;
909     uint8_t * pData;
910 
911     uxIPHeaderSizePacket_ExpectAnyArgsAndReturn( ipSIZE_OF_IPv4_HEADER );
912     uxIPHeaderSizePacket_ExpectAnyArgsAndReturn( ipSIZE_OF_IPv4_HEADER );
913 
914     result = prvCheckRxData( pxNetworkBuffer, &pData );
915 
916     TEST_ASSERT_EQUAL( 14, result );
917 
918     BaseType_t xResult = 0;
919     xSocket.u.xTCP.uxRxStreamSize = 39;
920     xSocket.u.xTCP.eTCPState = eESTABLISHED;
921     xSocket.u.xTCP.rxStream = NULL;
922     pxSocket = &xSocket;
923 
924     uxIPHeaderSizePacket_ExpectAnyArgsAndReturn( ipSIZE_OF_IPv4_HEADER );
925     lTCPWindowRxCheck_ExpectAnyArgsAndReturn( 20 );
926     lTCPAddRxdata_ExpectAnyArgsAndReturn( 14 );
927     prvTCPSendReset_IgnoreAndReturn( pdTRUE );
928 
929     xResult = prvStoreRxData( pxSocket,
930                               pData,
931                               pxNetworkBuffer,
932                               105 );
933 
934     TEST_ASSERT_EQUAL( -1, xResult );
935 }
936 
937 /* Test for prvStorexData function. */
test_prvStoreRxData_Negative_Offset(void)938 void test_prvStoreRxData_Negative_Offset( void )
939 {
940     int32_t result;
941 
942     /* Setup TCP option for tests */
943     pxNetworkBuffer = &xNetworkBuffer;
944 
945     pxNetworkBuffer->pucEthernetBuffer = EthernetBuffer;
946     uint8_t * pData;
947 
948     uxIPHeaderSizePacket_ExpectAnyArgsAndReturn( ipSIZE_OF_IPv4_HEADER );
949     uxIPHeaderSizePacket_ExpectAnyArgsAndReturn( ipSIZE_OF_IPv4_HEADER );
950 
951     result = prvCheckRxData( pxNetworkBuffer, &pData );
952 
953     TEST_ASSERT_EQUAL( 14, result );
954 
955     BaseType_t xResult = 0;
956     xSocket.u.xTCP.uxRxStreamSize = 39;
957     xSocket.u.xTCP.eTCPState = eESTABLISHED;
958     xSocket.u.xTCP.rxStream = NULL;
959     pxSocket = &xSocket;
960     TCPWindow_t * pxTCPWindow = &pxSocket->u.xTCP.xTCPWindow;
961     pxTCPWindow->ulUserDataLength = 0;
962 
963     uxIPHeaderSizePacket_ExpectAnyArgsAndReturn( ipSIZE_OF_IPv4_HEADER );
964     lTCPWindowRxCheck_ExpectAnyArgsAndReturn( -1 );
965 
966     xResult = prvStoreRxData( pxSocket,
967                               pData,
968                               pxNetworkBuffer,
969                               105 );
970 
971     TEST_ASSERT_EQUAL( 0, xResult );
972 }
973 
974 /* Test for prvStorexData function. */
test_prvStoreRxData_None_Zero_Skipcount(void)975 void test_prvStoreRxData_None_Zero_Skipcount( void )
976 {
977     int32_t result;
978 
979     /* Setup TCP option for tests */
980     pxNetworkBuffer = &xNetworkBuffer;
981     pxNetworkBuffer->pucEthernetBuffer = EthernetBuffer;
982 
983     ProtocolHeaders_t * pxProtocolHeaders = ( ( ProtocolHeaders_t * )
984                                               &( pxNetworkBuffer->pucEthernetBuffer[ ipSIZE_OF_ETH_HEADER + ipSIZE_OF_IPv4_HEADER ] ) );
985     TCPHeader_t * pxTCPHeader = &pxProtocolHeaders->xTCPHeader;
986     uint8_t * pData;
987 
988     uxIPHeaderSizePacket_ExpectAnyArgsAndReturn( ipSIZE_OF_IPv4_HEADER );
989     uxIPHeaderSizePacket_ExpectAnyArgsAndReturn( ipSIZE_OF_IPv4_HEADER );
990 
991     result = prvCheckRxData( pxNetworkBuffer, &pData );
992 
993     TEST_ASSERT_EQUAL( 14, result );
994 
995     BaseType_t xResult = 0;
996     xSocket.u.xTCP.uxRxStreamSize = 39;
997     xSocket.u.xTCP.eTCPState = eESTABLISHED;
998     xSocket.u.xTCP.rxStream = NULL;
999     pxSocket = &xSocket;
1000 
1001     TCPWindow_t * pxTCPWindow = &pxSocket->u.xTCP.xTCPWindow;
1002     pxTCPWindow->ulUserDataLength = 0;
1003     uint32_t SkipCount;
1004     uint32_t * pSkipCount = &SkipCount;
1005     *( pSkipCount ) = 10;
1006 
1007     uxIPHeaderSizePacket_ExpectAnyArgsAndReturn( ipSIZE_OF_IPv4_HEADER );
1008     lTCPWindowRxCheck_ExpectAndReturn(
1009         pxTCPWindow,
1010         FreeRTOS_ntohl( pxTCPHeader->ulSequenceNumber ),
1011         14,
1012         pxSocket->u.xTCP.uxRxStreamSize,
1013         NULL,
1014         20 );
1015     lTCPWindowRxCheck_IgnoreArg_pulSkipCount();
1016     lTCPWindowRxCheck_ReturnThruPtr_pulSkipCount( pSkipCount );
1017     lTCPAddRxdata_ExpectAnyArgsAndReturn( 4 );
1018     prvTCPSendReset_IgnoreAndReturn( pdTRUE );
1019 
1020     xResult = prvStoreRxData( pxSocket,
1021                               pData,
1022                               pxNetworkBuffer,
1023                               14 );
1024 
1025     TEST_ASSERT_EQUAL( 0, xResult );
1026 }
1027