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_list.h"
39 
40 /* This must come after list.h is included (in this case, indirectly
41  * by mock_list.h). */
42 #include "mock_Sockets_DiffConfig1_list_macros.h"
43 #include "mock_queue.h"
44 #include "mock_event_groups.h"
45 #include "mock_portable.h"
46 
47 #include "FreeRTOSIPConfig.h"
48 
49 #include "mock_FreeRTOS_IP.h"
50 #include "mock_FreeRTOS_ARP.h"
51 #include "mock_NetworkBufferManagement.h"
52 #include "mock_NetworkInterface.h"
53 #include "mock_FreeRTOS_DHCP.h"
54 #include "mock_FreeRTOS_DNS.h"
55 #include "mock_FreeRTOS_Stream_Buffer.h"
56 #include "mock_FreeRTOS_TCP_WIN.h"
57 
58 #include "FreeRTOS_Sockets.h"
59 #include "FreeRTOS_IP_Private.h"
60 
61 #include "catch_assert.h"
62 
63 extern List_t xBoundUDPSocketsList;
64 extern List_t xBoundTCPSocketsList;
65 
66 BaseType_t prvValidSocket( const FreeRTOS_Socket_t * pxSocket,
67                            BaseType_t xProtocol,
68                            BaseType_t xIsBound );
69 
70 uint8_t ucASCIIToHex( char cChar );
71 
72 BaseType_t bMayConnect( FreeRTOS_Socket_t const * pxSocket );
73 
74 static uint32_t xRandomNumberToReturn;
75 static BaseType_t xRNGStatus;
76 static UBaseType_t uxGlobalCallbackCount;
77 static BaseType_t xLocalReceiveCallback_Return;
78 static uint8_t xLocalReceiveCallback_Called = 0;
79 
80 static FreeRTOS_Socket_t xGlobalSocket;
81 
vUserCallbackLocal(FreeRTOS_Socket_t * xSocket)82 static void vUserCallbackLocal( FreeRTOS_Socket_t * xSocket )
83 {
84     uxGlobalCallbackCount++;
85 }
86 
xStubApplicationGetRandomNumber(uint32_t * xRndNumber,int count)87 static BaseType_t xStubApplicationGetRandomNumber( uint32_t * xRndNumber,
88                                                    int count )
89 {
90     ( void ) count;
91     *xRndNumber = xRandomNumberToReturn;
92     return xRNGStatus;
93 }
94 
vpxListFindListItemWithValue_NotFound(void)95 static void vpxListFindListItemWithValue_NotFound( void )
96 {
97     xIPIsNetworkTaskReady_ExpectAndReturn( pdFALSE );
98 }
99 
vpxListFindListItemWithValue_Found(const List_t * pxList,TickType_t xWantedItemValue,const ListItem_t * pxReturn)100 static void vpxListFindListItemWithValue_Found( const List_t * pxList,
101                                                 TickType_t xWantedItemValue,
102                                                 const ListItem_t * pxReturn )
103 {
104     xIPIsNetworkTaskReady_ExpectAndReturn( pdTRUE );
105 
106     listGET_NEXT_ExpectAndReturn( &( pxList->xListEnd ), pxReturn );
107 
108     listGET_LIST_ITEM_VALUE_ExpectAndReturn( pxReturn, xWantedItemValue );
109 }
110 
xStubForEventGroupWaitBits(EventGroupHandle_t xEventGroup,const EventBits_t uxBitsToWaitFor,const BaseType_t xClearOnExit,const BaseType_t xWaitForAllBits,TickType_t xTicksToWait,int CallbackCount)111 static BaseType_t xStubForEventGroupWaitBits( EventGroupHandle_t xEventGroup,
112                                               const EventBits_t uxBitsToWaitFor,
113                                               const BaseType_t xClearOnExit,
114                                               const BaseType_t xWaitForAllBits,
115                                               TickType_t xTicksToWait,
116                                               int CallbackCount )
117 {
118     xGlobalSocket.u.xTCP.eTCPState = eESTABLISHED;
119 }
120 
xLocalReceiveCallback(Socket_t xSocket,void * pvData,size_t xLength)121 static BaseType_t xLocalReceiveCallback( Socket_t xSocket,
122                                          void * pvData,
123                                          size_t xLength )
124 {
125     xLocalReceiveCallback_Called++;
126     return xLocalReceiveCallback_Return;
127 }
128 
129 /*
130  * @brief Binding successful.
131  */
test_vSocketBind_TCP(void)132 void test_vSocketBind_TCP( void )
133 {
134     BaseType_t xReturn;
135     FreeRTOS_Socket_t xSocket;
136     struct freertos_sockaddr xBindAddress;
137     size_t uxAddressLength;
138     BaseType_t xInternal = pdFALSE;
139 
140     memset( &xBindAddress, 0xFC, sizeof( xBindAddress ) );
141     memset( &xSocket, 0, sizeof( xSocket ) );
142 
143     xSocket.ucProtocol = ( uint8_t ) FREERTOS_IPPROTO_TCP;
144 
145     catch_assert( vSocketBind( &xSocket, NULL, uxAddressLength, xInternal ) );
146 }
147