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 /*#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 uxIPHeaderSizeSocket_ExpectAnyArgsAndReturn( ipSIZE_OF_IPv4_HEADER );
103
104 prvTCPReturnPacket_Expect( &xSocket, xSocket.u.xTCP.pxAckMessage, ipSIZE_OF_IPv4_HEADER + ipSIZE_OF_TCP_HEADER, ipconfigZERO_COPY_TX_DRIVER );
105
106 xTCPWindowTxHasData_ExpectAnyArgsAndReturn( pdTRUE );
107 xTCPWindowTxHasData_ReturnThruPtr_pulDelay( &xDelayReturn );
108
109 prvTCPSendPacket_ExpectAndReturn( &xSocket, 0 );
110
111 prvTCPStatusAgeCheck_ExpectAndReturn( &xSocket, xToReturn );
112
113 xReturn = xTCPSocketCheck( &xSocket );
114
115 TEST_ASSERT_EQUAL( xToReturn, xReturn );
116 TEST_ASSERT_EQUAL( NULL, xSocket.u.xTCP.pxAckMessage );
117 TEST_ASSERT_EQUAL( 1U, xSocket.u.xTCP.usTimeout );
118 }
119
120 /* @brief Test vTCPStateChange function when the state to be reached is established and the
121 * current state is closed. Socket select bit is set to select write. Also, this socket
122 * is an orphan. Since parent socket is NULL and reuse bit is not set, it will lead to
123 * the socket being NULL. */
test_vTCPStateChange_ClosedToEstablishedState_SelectWrite_QueuedBitSet(void)124 void test_vTCPStateChange_ClosedToEstablishedState_SelectWrite_QueuedBitSet( void )
125 {
126 FreeRTOS_Socket_t xSocket;
127 enum eTCP_STATE eTCPState;
128 BaseType_t xTickCountAck = 0xAABBEEDD;
129 BaseType_t xTickCountAlive = 0xAABBEFDD;
130 BaseType_t xBackup;
131
132 memset( &xSocket, 0, sizeof( xSocket ) );
133 eTCPState = eESTABLISHED;
134 xSocket.u.xTCP.eTCPState = eCLOSED;
135
136 xSocket.u.xTCP.usTimeout = 100;
137 xSocket.xSelectBits = eSELECT_WRITE;
138 /* if bPassQueued is true, this socket is an orphan until it gets connected. */
139 xSocket.u.xTCP.bits.bPassQueued = pdTRUE_UNSIGNED;
140
141 prvTCPSocketIsActive_ExpectAndReturn( xSocket.u.xTCP.eTCPState, pdTRUE );
142
143 xTaskGetTickCount_ExpectAndReturn( xTickCountAck );
144 xTaskGetTickCount_ExpectAndReturn( xTickCountAlive );
145 FreeRTOS_inet_ntop_ExpectAnyArgsAndReturn( NULL );
146
147 vTCPStateChange( &xSocket, eTCPState );
148 }
149
150 /* @brief Test vTCPStateChange function when the state to be reached is closed wait
151 * and current state is equal to syn first. */
test_vTCPStateChange_ClosedWaitState_CurrentStateSynFirstNextStateCloseWait(void)152 void test_vTCPStateChange_ClosedWaitState_CurrentStateSynFirstNextStateCloseWait( void )
153 {
154 FreeRTOS_Socket_t xSocket;
155 enum eTCP_STATE eTCPState;
156 BaseType_t xTickCountAck = 0xAABBEEDD;
157 BaseType_t xTickCountAlive = 0xAABBEFDD;
158
159 memset( &xSocket, 0, sizeof( xSocket ) );
160 eTCPState = eCLOSE_WAIT;
161
162 xSocketToListen = NULL;
163
164 xSocket.u.xTCP.eTCPState = eSYN_FIRST;
165 xSocket.u.xTCP.bits.bReuseSocket = pdTRUE_UNSIGNED;
166
167 prvTCPSocketIsActive_ExpectAndReturn( xSocket.u.xTCP.eTCPState, pdTRUE );
168 vTaskSuspendAll_Expect();
169 xTaskResumeAll_ExpectAndReturn( 0 );
170 xTaskGetTickCount_ExpectAndReturn( xTickCountAck );
171 xTaskGetTickCount_ExpectAndReturn( xTickCountAlive );
172 FreeRTOS_inet_ntop_ExpectAnyArgsAndReturn( NULL );
173
174 vSocketWakeUpUser_Expect( &xSocket );
175
176 vTCPStateChange( &xSocket, eTCPState );
177
178 TEST_ASSERT_EQUAL( eCLOSED, xSocket.u.xTCP.eTCPState );
179 TEST_ASSERT_EQUAL( xTickCountAck, xSocket.u.xTCP.xLastActTime );
180 TEST_ASSERT_EQUAL( &xSocket, xSocketToListen );
181 TEST_ASSERT_EQUAL( pdFALSE_UNSIGNED, xSocket.u.xTCP.bits.bWaitKeepAlive );
182 TEST_ASSERT_EQUAL( pdFALSE_UNSIGNED, xSocket.u.xTCP.bits.bSendKeepAlive );
183 TEST_ASSERT_EQUAL( 0, xSocket.u.xTCP.ucKeepRepCount );
184 TEST_ASSERT_EQUAL( xTickCountAlive, xSocket.u.xTCP.xLastAliveTime );
185 }
186