1 /*
2 * FreeRTOS+TCP V3.1.0
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 /*#include "mock_task.h" */
38 #include "mock_TCP_IP_DiffConfig_list_macros.h"
39
40 /* This must come after list.h is included (in this case, indirectly
41 * by mock_list.h). */
42 #include "mock_queue.h"
43 #include "mock_task.h"
44 #include "mock_event_groups.h"
45 #include "mock_list.h"
46
47 #include "mock_FreeRTOS_IP.h"
48 #include "mock_FreeRTOS_IP_Utils.h"
49 #include "mock_NetworkBufferManagement.h"
50 #include "mock_NetworkInterface.h"
51 #include "mock_FreeRTOS_Sockets.h"
52 #include "mock_FreeRTOS_Stream_Buffer.h"
53 #include "mock_FreeRTOS_TCP_WIN.h"
54 #include "mock_FreeRTOS_TCP_State_Handling.h"
55 #include "mock_FreeRTOS_UDP_IP.h"
56 #include "mock_FreeRTOS_TCP_Transmission.h"
57
58 #include "catch_assert.h"
59
60 #include "FreeRTOSIPConfig.h"
61
62 #include "FreeRTOS_TCP_IP.h"
63
64 FreeRTOS_Socket_t xSocket, * pxSocket;
65 NetworkBufferDescriptor_t xNetworkBuffer, * pxNetworkBuffer;
66
67 extern BaseType_t xTCPWindowLoggingLevel;
68 extern FreeRTOS_Socket_t * xSocketToListen;
69
70 uint8_t ucEthernetBuffer[ ipconfigNETWORK_MTU ] =
71 {
72 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x08, 0x00, 0x45, 0x00,
73 0x00, 0x34, 0x15, 0xc2, 0x40, 0x00, 0x40, 0x06, 0xa8, 0x8e, 0xc0, 0xa8, 0x00, 0x08, 0xac, 0xd9,
74 0x0e, 0xea, 0xea, 0xfe, 0x01, 0xbb, 0x8b, 0xaf, 0x8a, 0x24, 0xdc, 0x96, 0x95, 0x7a, 0x80, 0x10,
75 0x01, 0xf5, 0x7c, 0x9a, 0x00, 0x00, 0x01, 0x01, 0x08, 0x0a, 0xb8, 0x53, 0x57, 0x27, 0xb2, 0xce,
76 0xc3, 0x17
77 };
78
79 static Socket_t xHandleConnectedSocket;
80 static size_t xHandleConnectedLength;
HandleConnected(Socket_t xSocket,size_t xLength)81 static void HandleConnected( Socket_t xSocket,
82 size_t xLength )
83 {
84 TEST_ASSERT_EQUAL( xHandleConnectedSocket, xSocket );
85 TEST_ASSERT_EQUAL( xHandleConnectedLength, xLength );
86 }
87
88 /* test xTCPSocketCheck function */
test_xTCPSocketCheck_StateEstablished_TxStreamNonNull1(void)89 void test_xTCPSocketCheck_StateEstablished_TxStreamNonNull1( void )
90 {
91 BaseType_t xReturn, xToReturn = 0xAABBCCDD;
92 FreeRTOS_Socket_t xSocket;
93 TickType_t xDelayReturn = 0;
94
95 memset( &xSocket, 0, sizeof( xSocket ) );
96
97 xSocket.u.xTCP.eTCPState = eESTABLISHED;
98 xSocket.u.xTCP.txStream = ( void * ) &xSocket;
99 xSocket.u.xTCP.pxAckMessage = ( void * ) &xSocket;
100
101 prvTCPAddTxData_Expect( &xSocket );
102
103 prvTCPReturnPacket_Expect( &xSocket, xSocket.u.xTCP.pxAckMessage, ipSIZE_OF_IPv4_HEADER + ipSIZE_OF_TCP_HEADER, ipconfigZERO_COPY_TX_DRIVER );
104
105 xTCPWindowTxHasData_ExpectAnyArgsAndReturn( pdTRUE );
106 xTCPWindowTxHasData_ReturnThruPtr_pulDelay( &xDelayReturn );
107
108 prvTCPSendPacket_ExpectAndReturn( &xSocket, 0 );
109
110 prvTCPStatusAgeCheck_ExpectAndReturn( &xSocket, xToReturn );
111
112 xReturn = xTCPSocketCheck( &xSocket );
113
114 TEST_ASSERT_EQUAL( xToReturn, xReturn );
115 TEST_ASSERT_EQUAL( NULL, xSocket.u.xTCP.pxAckMessage );
116 TEST_ASSERT_EQUAL( 1U, xSocket.u.xTCP.usTimeout );
117 }
118
119 /* @brief Test vTCPStateChange function when the state to be reached is established and the
120 * current state is closed. Socket select bit is set to select write. Also, this socket
121 * is an orphan. Since parent socket is NULL and reuse bit is not set, it will lead to
122 * the socket being NULL. */
test_vTCPStateChange_ClosedToEstablishedState_SelectWrite_QueuedBitSet(void)123 void test_vTCPStateChange_ClosedToEstablishedState_SelectWrite_QueuedBitSet( void )
124 {
125 FreeRTOS_Socket_t xSocket;
126 enum eTCP_STATE eTCPState;
127 BaseType_t xTickCountAck = 0xAABBEEDD;
128 BaseType_t xTickCountAlive = 0xAABBEFDD;
129 BaseType_t xBackup;
130
131 memset( &xSocket, 0, sizeof( xSocket ) );
132 eTCPState = eESTABLISHED;
133 xSocket.u.xTCP.eTCPState = eCLOSED;
134
135 xSocket.u.xTCP.usTimeout = 100;
136 xSocket.xSelectBits = eSELECT_WRITE;
137 /* if bPassQueued is true, this socket is an orphan until it gets connected. */
138 xSocket.u.xTCP.bits.bPassQueued = pdTRUE_UNSIGNED;
139
140 prvTCPSocketIsActive_ExpectAndReturn( xSocket.u.xTCP.eTCPState, pdTRUE );
141
142 xTaskGetTickCount_ExpectAndReturn( xTickCountAck );
143 xTaskGetTickCount_ExpectAndReturn( xTickCountAlive );
144
145 vTCPStateChange( &xSocket, eTCPState );
146 }
147
148 /* @brief Test vTCPStateChange function when the state to be reached is closed wait
149 * and current state is equal to syn first. */
test_vTCPStateChange_ClosedWaitState_CurrentStateSynFirstNextStateCloseWait(void)150 void test_vTCPStateChange_ClosedWaitState_CurrentStateSynFirstNextStateCloseWait( void )
151 {
152 FreeRTOS_Socket_t xSocket;
153 enum eTCP_STATE eTCPState;
154 BaseType_t xTickCountAck = 0xAABBEEDD;
155 BaseType_t xTickCountAlive = 0xAABBEFDD;
156
157 memset( &xSocket, 0, sizeof( xSocket ) );
158 eTCPState = eCLOSE_WAIT;
159
160 xSocketToListen = NULL;
161
162 xSocket.u.xTCP.eTCPState = eSYN_FIRST;
163 xSocket.u.xTCP.bits.bReuseSocket = pdTRUE_UNSIGNED;
164
165 prvTCPSocketIsActive_ExpectAndReturn( xSocket.u.xTCP.eTCPState, pdTRUE );
166 xTaskGetTickCount_ExpectAndReturn( xTickCountAck );
167 xTaskGetTickCount_ExpectAndReturn( xTickCountAlive );
168
169 vSocketWakeUpUser_Expect( &xSocket );
170
171 vTCPStateChange( &xSocket, eTCPState );
172
173 TEST_ASSERT_EQUAL( eCLOSED, xSocket.u.xTCP.eTCPState );
174 TEST_ASSERT_EQUAL( xTickCountAck, xSocket.u.xTCP.xLastActTime );
175 TEST_ASSERT_EQUAL( &xSocket, xSocketToListen );
176 TEST_ASSERT_EQUAL( pdFALSE_UNSIGNED, xSocket.u.xTCP.bits.bWaitKeepAlive );
177 TEST_ASSERT_EQUAL( pdFALSE_UNSIGNED, xSocket.u.xTCP.bits.bSendKeepAlive );
178 TEST_ASSERT_EQUAL( 0, xSocket.u.xTCP.ucKeepRepCount );
179 TEST_ASSERT_EQUAL( xTickCountAlive, xSocket.u.xTCP.xLastAliveTime );
180 }
181