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_IP_Utils_DiffConfig_list_macros.h"
43 #include "mock_queue.h"
44 #include "mock_event_groups.h"
45 
46 #include "FreeRTOSIPConfig.h"
47 
48 #include "mock_FreeRTOS_IP_Private.h"
49 #include "mock_FreeRTOS_IP.h"
50 #include "mock_FreeRTOS_IP_Timers.h"
51 #include "mock_FreeRTOS_TCP_IP.h"
52 #include "mock_FreeRTOS_ICMP.h"
53 #include "mock_FreeRTOS_ARP.h"
54 #include "mock_NetworkBufferManagement.h"
55 #include "mock_NetworkInterface.h"
56 #include "mock_FreeRTOS_DHCP.h"
57 #include "mock_FreeRTOS_Sockets.h"
58 #include "mock_FreeRTOS_DNS.h"
59 #include "mock_FreeRTOS_Stream_Buffer.h"
60 #include "mock_FreeRTOS_TCP_WIN.h"
61 #include "mock_FreeRTOS_UDP_IP.h"
62 
63 #include "FreeRTOS_IP_Utils.h"
64 
65 #include "catch_assert.h"
66 
67 #if ( ipconfigUSE_NETWORK_EVENT_HOOK == 1 )
68     extern BaseType_t xCallEventHook;
69 #endif
70 
71 extern UBaseType_t uxLastMinBufferCount;
72 extern size_t uxMinLastSize;
73 extern UBaseType_t uxLastMinQueueSpace;
74 
test_pxPacketBuffer_to_NetworkBuffer(void)75 void test_pxPacketBuffer_to_NetworkBuffer( void )
76 {
77     NetworkBufferDescriptor_t * pxReturn;
78 
79     pxReturn = pxPacketBuffer_to_NetworkBuffer( NULL );
80 
81     TEST_ASSERT_EQUAL( NULL, pxReturn );
82 }
83 
test_prvProcessNetworkDownEvent_Pass(void)84 void test_prvProcessNetworkDownEvent_Pass( void )
85 {
86     xCallEventHook = pdFALSE;
87 
88     vIPSetARPTimerEnableState_Expect( pdFALSE );
89 
90     FreeRTOS_ClearARP_Expect();
91 
92     xNetworkInterfaceInitialise_ExpectAndReturn( pdPASS );
93 
94     vIPNetworkUpCalls_Expect();
95 
96     prvProcessNetworkDownEvent();
97 }
98 
test_FreeRTOS_round_up(void)99 void test_FreeRTOS_round_up( void )
100 {
101     uint32_t ulReturn;
102     uint32_t a = 10;
103 
104     ulReturn = FreeRTOS_round_up( a, 0 );
105 
106     TEST_ASSERT_EQUAL( 10, ulReturn );
107 }
108 
test_FreeRTOS_round_down(void)109 void test_FreeRTOS_round_down( void )
110 {
111     uint32_t ulReturn;
112     uint32_t a = 10;
113 
114     ulReturn = FreeRTOS_round_down( a, 0 );
115 
116     TEST_ASSERT_EQUAL( 0, ulReturn );
117 }
118 
test_vPrintResourceStats_MinSizeIsBigger(void)119 void test_vPrintResourceStats_MinSizeIsBigger( void )
120 {
121     uxLastMinBufferCount = ipconfigNUM_NETWORK_BUFFER_DESCRIPTORS;
122     uxMinLastSize = 10u;
123     uxLastMinQueueSpace = 0;
124 
125     uxGetMinimumFreeNetworkBuffers_ExpectAndReturn( ipconfigNUM_NETWORK_BUFFER_DESCRIPTORS - 2 );
126     xPortGetMinimumEverFreeHeapSize_ExpectAndReturn( 1024U * 1025U );
127 
128     uxGetMinimumIPQueueSpace_ExpectAndReturn( 0 );
129 
130     vPrintResourceStats();
131 
132     TEST_ASSERT_EQUAL( 10, uxMinLastSize );
133     TEST_ASSERT_EQUAL( 0, uxLastMinQueueSpace );
134 }
135 
test_vPrintResourceStats_LastQueueNECurrentQueue(void)136 void test_vPrintResourceStats_LastQueueNECurrentQueue( void )
137 {
138     uxLastMinBufferCount = ipconfigNUM_NETWORK_BUFFER_DESCRIPTORS;
139     uxMinLastSize = 10u;
140     uxLastMinQueueSpace = 0;
141 
142     uxGetMinimumFreeNetworkBuffers_ExpectAndReturn( ipconfigNUM_NETWORK_BUFFER_DESCRIPTORS - 2 );
143     xPortGetMinimumEverFreeHeapSize_ExpectAndReturn( 1024U * 1025U );
144 
145     uxGetMinimumIPQueueSpace_ExpectAndReturn( 10 );
146 
147     vPrintResourceStats();
148 
149     TEST_ASSERT_EQUAL( 10, uxMinLastSize );
150     TEST_ASSERT_EQUAL( 10, uxLastMinQueueSpace );
151 }
152