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_IP_DiffConfig2_list_macros.h"
43 #include "mock_queue.h"
44 #include "mock_event_groups.h"
45 
46 #include "mock_FreeRTOS_IP_Private.h"
47 #include "mock_FreeRTOS_IP_Utils.h"
48 #include "mock_NetworkBufferManagement.h"
49 #include "mock_FreeRTOS_Routing.h"
50 
51 #include "FreeRTOS_IP.h"
52 
53 /*#include "FreeRTOS_IP_stubs.c" */
54 #include "catch_assert.h"
55 
56 #include "FreeRTOSIPConfig.h"
57 
58 /* =========================== EXTERN VARIABLES =========================== */
59 
60 void prvIPTask( void * pvParameters );
61 void prvProcessIPEventsAndTimers( void );
62 eFrameProcessingResult_t prvProcessIPPacket( IPPacket_t * pxIPPacket,
63                                              NetworkBufferDescriptor_t * const pxNetworkBuffer );
64 void prvProcessEthernetPacket( NetworkBufferDescriptor_t * const pxNetworkBuffer );
65 
66 extern BaseType_t xIPTaskInitialised;
67 extern BaseType_t xNetworkDownEventPending;
68 extern BaseType_t xNetworkUp;
69 extern UBaseType_t uxQueueMinimumSpace;
70 
71 /* ============================ Unity Fixtures ============================ */
72 
73 /*! called before each test case */
setUp(void)74 void setUp( void )
75 {
76     pxNetworkEndPoints = NULL;
77     pxNetworkInterfaces = NULL;
78     xNetworkDownEventPending = pdFALSE;
79 }
80 
81 /*! called after each test case */
tearDown(void)82 void tearDown( void )
83 {
84 }
85 
86 /* ======================== Stub Callback Functions ========================= */
87 
88 TaskHandle_t IPInItHappyPath_xTaskHandleToSet = ( TaskHandle_t ) 0xCDBA9087;
StubxTaskCreate(TaskFunction_t pxTaskCode,const char * const pcName,const configSTACK_DEPTH_TYPE usStackDepth,void * const pvParameters,UBaseType_t uxPriority,TaskHandle_t * const pxCreatedTask)89 static BaseType_t StubxTaskCreate( TaskFunction_t pxTaskCode,
90                                    const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
91                                    const configSTACK_DEPTH_TYPE usStackDepth,
92                                    void * const pvParameters,
93                                    UBaseType_t uxPriority,
94                                    TaskHandle_t * const pxCreatedTask )
95 {
96     *pxCreatedTask = IPInItHappyPath_xTaskHandleToSet;
97     return pdPASS;
98 }
99 
100 /* ============================== Test Cases ============================== */
101 
102 /**
103  * @brief test_FreeRTOS_IPInit_HappyPathDHCP
104  * To validate if FreeRTOS_IPInit() works with DHCP enabled.
105  */
test_FreeRTOS_IPInit_HappyPathDHCP(void)106 void test_FreeRTOS_IPInit_HappyPathDHCP( void )
107 {
108     const uint8_t ucIPAddress[ ipIP_ADDRESS_LENGTH_BYTES ] = { 0xC0, 0xB0, 0xAB, 0x12 };
109     const uint8_t ucNetMask[ ipIP_ADDRESS_LENGTH_BYTES ] = { 0xC1, 0xB2, 0xAC, 0x13 };
110     const uint8_t ucGatewayAddress[ ipIP_ADDRESS_LENGTH_BYTES ] = { 0xC2, 0xB3, 0xAC, 0x14 };
111     const uint8_t ucDNSServerAddress[ ipIP_ADDRESS_LENGTH_BYTES ] = { 0xC3, 0xB4, 0xAD, 0x15 };
112     const uint8_t ucMACAddress[ ipMAC_ADDRESS_LENGTH_BYTES ] = { 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF };
113     BaseType_t xReturn;
114     QueueHandle_t ulPointerToQueue = ( QueueHandle_t ) 0x1234ABCD;
115 
116     /* Set the local IP to something other than 0. */
117     *ipLOCAL_IP_ADDRESS_POINTER = 0xABCD;
118 
119     FreeRTOS_FillEndPoint_Ignore();
120     FreeRTOS_FirstNetworkInterface_IgnoreAndReturn( pdTRUE );
121     pxFillInterfaceDescriptor_IgnoreAndReturn( pdTRUE );
122 
123     vPreCheckConfigs_Expect();
124 
125     xQueueGenericCreate_ExpectAndReturn( ipconfigEVENT_QUEUE_LENGTH, sizeof( IPStackEvent_t ), 0U, ulPointerToQueue );
126 
127     #if ( configQUEUE_REGISTRY_SIZE > 0 )
128         vQueueAddToRegistry_Expect( ulPointerToQueue, "NetEvnt" );
129     #endif
130 
131     xNetworkBuffersInitialise_ExpectAndReturn( pdPASS );
132 
133     vNetworkSocketsInit_Expect();
134 
135     xTaskCreate_Stub( StubxTaskCreate );
136 
137     xReturn = FreeRTOS_IPInit( ucIPAddress, ucNetMask, ucGatewayAddress, ucDNSServerAddress, ucMACAddress );
138 
139     TEST_ASSERT_EQUAL( pdPASS, xReturn );
140     TEST_ASSERT_EQUAL( IPInItHappyPath_xTaskHandleToSet, FreeRTOS_GetIPTaskHandle() );
141 }
142