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_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_DiffConfig_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_NetworkBufferManagement.h"
51 #include "mock_FreeRTOS_Stream_Buffer.h"
52 #include "mock_FreeRTOS_TCP_WIN.h"
53 #include "mock_FreeRTOS_Routing.h"
54 
55 #include "FreeRTOS_Sockets.h"
56 
57 #include "catch_assert.h"
58 
59 /* ============================== Test Cases ============================== */
60 
61 /**
62  * @brief Binding successful.
63  */
test_vSocketBind_TCP(void)64 void test_vSocketBind_TCP( void )
65 {
66     BaseType_t xReturn;
67     FreeRTOS_Socket_t xSocket;
68     struct freertos_sockaddr xBindAddress;
69     size_t uxAddressLength;
70     BaseType_t xInternal = pdFALSE;
71 
72     memset( &xBindAddress, 0xFC, sizeof( xBindAddress ) );
73     memset( &xSocket, 0, sizeof( xSocket ) );
74 
75     xSocket.ucProtocol = ( uint8_t ) FREERTOS_IPPROTO_TCP;
76 
77     xReturn = vSocketBind( &xSocket, NULL, uxAddressLength, xInternal );
78 
79     TEST_ASSERT_EQUAL( -pdFREERTOS_ERRNO_EADDRNOTAVAIL, xReturn );
80 }
81 
82 /**
83  * @brief Binding successful.
84  */
test_vSocketBind_TCP1(void)85 void test_vSocketBind_TCP1( void )
86 {
87     BaseType_t xReturn;
88     FreeRTOS_Socket_t xSocket;
89     struct freertos_sockaddr xBindAddress;
90     size_t uxAddressLength;
91     BaseType_t xInternal = pdFALSE;
92     NetworkEndPoint_t xEndPoint = { 0 };
93 
94     memset( &xBindAddress, 0xFC, sizeof( xBindAddress ) );
95     memset( &xSocket, 0, sizeof( xSocket ) );
96 
97     xSocket.ucProtocol = ( uint8_t ) FREERTOS_IPPROTO_TCP;
98 
99     xIPIsNetworkTaskReady_ExpectAndReturn( pdFALSE );
100 
101     listSET_LIST_ITEM_VALUE_Expect( &( xSocket.xBoundSocketListItem ), xBindAddress.sin_port );
102 
103     FreeRTOS_FindEndPointOnIP_IPv4_ExpectAnyArgsAndReturn( &xEndPoint );
104 
105     vListInsertEnd_Expect( NULL, &( xSocket.xBoundSocketListItem ) );
106     vListInsertEnd_IgnoreArg_pxList();
107 
108     xReturn = vSocketBind( &xSocket, &xBindAddress, uxAddressLength, xInternal );
109 
110     TEST_ASSERT_EQUAL( 0, xReturn );
111 }
112