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 /* Include Unity header */
28 #include "unity.h"
29
30 /* Include standard libraries */
31 #include <stdlib.h>
32 #include <string.h>
33 #include <stdint.h>
34
35 #include "mock_FreeRTOS_IP.h"
36 #include "mock_FreeRTOS_Sockets.h"
37 #include "mock_FreeRTOS_IP_Private.h"
38 #include "mock_task.h"
39 #include "mock_list.h"
40 #include "mock_queue.h"
41
42 #include "mock_FreeRTOS_DNS_Callback.h"
43 /*#include "mock_FreeRTOS_DNS_Cache.h" */
44 #include "mock_FreeRTOS_DNS_Parser.h"
45 /* #include "mock_FreeRTOS_DNS_Networking.h"*/
46 #include "mock_NetworkBufferManagement.h"
47 #include "FreeRTOS_DNS.h"
48
49
50 #include "catch_assert.h"
51 #include "FreeRTOS_DNS_Networking.h"
52
53 #include "FreeRTOSIPConfig.h"
54
55 #define LLMNR_ADDRESS "freertos"
56 #define GOOD_ADDRESS "www.freertos.org"
57 #define BAD_ADDRESS "this is a bad address"
58 #define DOTTED_ADDRESS "192.268.0.1"
59
60 typedef void (* FOnDNSEvent ) ( const char * /* pcName */,
61 void * /* pvSearchID */,
62 struct freertos_addrinfo * /* pxAddressInfo */ );
63
64 /* =========================== GLOBAL VARIABLES =========================== */
65 static int callback_called = 0;
66
67
68 /* =========================== STATIC FUNCTIONS =========================== */
dns_callback(const char * pcName,void * pvSearchID,uint32_t ulIPAddress)69 static void dns_callback( const char * pcName,
70 void * pvSearchID,
71 uint32_t ulIPAddress )
72 {
73 callback_called = 1;
74 }
75
76
77 /* ============================ TEST FIXTURES ============================= */
78
79 /**
80 * @brief calls at the beginning of each test case
81 */
setUp(void)82 void setUp( void )
83 {
84 callback_called = 0;
85 }
86
87 /**
88 * @brief calls at the end of each test case
89 */
tearDown(void)90 void tearDown( void )
91 {
92 }
93
94
95 /* ============================= TEST CASES =============================== */
96
97 /**
98 * @brief Ensures that when the socket is invalid, null is returned
99 */
test_CreateSocket_fail_socket(void)100 void test_CreateSocket_fail_socket( void )
101 {
102 Socket_t s;
103
104 FreeRTOS_socket_ExpectAndReturn( FREERTOS_AF_INET,
105 FREERTOS_SOCK_DGRAM,
106 FREERTOS_IPPROTO_UDP,
107 NULL );
108 xSocketValid_ExpectAndReturn( NULL, pdFALSE );
109
110 s = DNS_CreateSocket( 235 );
111
112 TEST_ASSERT_EQUAL( NULL, s );
113 }
114
115 /**
116 * @brief Happy path!
117 */
test_CreateSocket_success(void)118 void test_CreateSocket_success( void )
119 {
120 Socket_t s;
121
122 FreeRTOS_socket_ExpectAndReturn( FREERTOS_AF_INET,
123 FREERTOS_SOCK_DGRAM,
124 FREERTOS_IPPROTO_UDP,
125 ( Socket_t ) 235 );
126 xSocketValid_ExpectAndReturn( ( Socket_t ) 235, pdTRUE );
127 FreeRTOS_setsockopt_ExpectAnyArgsAndReturn( 0 );
128 FreeRTOS_setsockopt_ExpectAnyArgsAndReturn( 0 );
129
130 s = DNS_CreateSocket( 235 );
131
132 TEST_ASSERT_EQUAL( ( Socket_t ) 235, s );
133 }
134
135 /**
136 * @brief Ensures that when the socket could not be created or could not be found, null is returned
137 */
test_BindSocket_fail(void)138 void test_BindSocket_fail( void )
139 {
140 struct freertos_sockaddr xAddress;
141 Socket_t xSocket;
142 uint16_t usPort;
143 uint32_t ret;
144
145 FreeRTOS_bind_ExpectAnyArgsAndReturn( 0 );
146
147 ret = DNS_BindSocket( xSocket, usPort );
148
149 TEST_ASSERT_EQUAL( 0, ret );
150 }
151
152 /**
153 * @brief Happy path!
154 */
test_BindSocket_success(void)155 void test_BindSocket_success( void )
156 {
157 struct freertos_sockaddr xAddress;
158 Socket_t xSocket;
159 uint16_t usPort;
160 uint32_t ret;
161
162 FreeRTOS_bind_ExpectAnyArgsAndReturn( 1 );
163
164 ret = DNS_BindSocket( xSocket, usPort );
165
166 TEST_ASSERT_EQUAL( 1, ret );
167 }
168
169 /**
170 * @brief Happy path!
171 */
test_SendRequest_success(void)172 void test_SendRequest_success( void )
173 {
174 Socket_t s = ( Socket_t ) 123;
175 uint32_t ret;
176 struct freertos_sockaddr xAddress;
177 struct xDNSBuffer pxDNSBuf;
178
179 pxDNSBuf.uxPayloadLength = 1024;
180
181 FreeRTOS_sendto_ExpectAnyArgsAndReturn( pxDNSBuf.uxPayloadLength );
182
183 ret = DNS_SendRequest( s, &xAddress, &pxDNSBuf );
184
185 TEST_ASSERT_EQUAL( pdTRUE, ret );
186 }
187
188 /**
189 * @brief Ensures that when SendTo fails false is returned
190 */
test_SendRequest_fail(void)191 void test_SendRequest_fail( void )
192 {
193 Socket_t s = ( Socket_t ) 123;
194 uint32_t ret;
195 struct freertos_sockaddr xAddress;
196 struct xDNSBuffer pxDNSBuf;
197
198 FreeRTOS_sendto_ExpectAnyArgsAndReturn( pdFALSE );
199
200 ret = DNS_SendRequest( s, &xAddress, &pxDNSBuf );
201
202 TEST_ASSERT_EQUAL( pdFALSE, ret );
203 }
204
205 /**
206 * @brief Happy path!
207 */
test_ReadReply_success(void)208 void test_ReadReply_success( void )
209 {
210 Socket_t s = ( Socket_t ) 123;
211 struct freertos_sockaddr xAddress;
212 struct xDNSBuffer pxDNSBuf;
213 BaseType_t xReturn;
214
215 FreeRTOS_recvfrom_ExpectAnyArgsAndReturn( 600 );
216
217 xReturn = DNS_ReadReply( s, &xAddress, &pxDNSBuf );
218
219 TEST_ASSERT_EQUAL( 600, xReturn );
220 }
221
222 /**
223 * @brief Happy path!
224 */
test_CloseSocket_success(void)225 void test_CloseSocket_success( void )
226 {
227 Socket_t s = ( Socket_t ) 123;
228
229 FreeRTOS_closesocket_ExpectAndReturn( s, pdTRUE );
230
231 DNS_CloseSocket( s );
232 }
233