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 #include "FreeRTOSIPConfig.h"
41 
42 /* This must come after list.h is included (in this case, indirectly
43  * by mock_list.h). */
44 #include "mock_queue.h"
45 #include "mock_event_groups.h"
46 
47 #include "FreeRTOS_Routing.h"
48 
49 #include "catch_assert.h"
50 
51 /* Default IPv4 address is 192.168.123.223, which is 0xDF7BA8C0. */
52 #define IPV4_DEFAULT_ADDRESS       ( 0xDF7BA8C0 )
53 /* Default IPv4 netmask is 255.255.255.0, which is 0x00FFFFFF. */
54 #define IPV4_DEFAULT_NETMASK       ( 0x00FFFFFF )
55 /* Default IPv4 netmask is 192.168.123.254, which is 0xFE7BA8C0. */
56 #define IPV4_DEFAULT_GATEWAY       ( 0xFE7BA8C0 )
57 /* Default IPv4 netmask is 192.168.123.1, which is 0x017BA8C0. */
58 #define IPV4_DEFAULT_DNS_SERVER    ( 0x017BA8C0 )
59 
60 /* ===========================  EXTERN VARIABLES  =========================== */
61 
62 const uint8_t ucDefaultIPAddress_IPv4[ ipIP_ADDRESS_LENGTH_BYTES ] = { 192, 168, 123, 223 };
63 const uint8_t ucDefaultNetMask_IPv4[ ipIP_ADDRESS_LENGTH_BYTES ] = { 255, 255, 255, 0 };
64 const uint8_t ucDefaultGatewayAddress_IPv4[ ipIP_ADDRESS_LENGTH_BYTES ] = { 192, 168, 123, 254 };
65 const uint8_t ucDefaultDNSServerAddress_IPv4[ ipIP_ADDRESS_LENGTH_BYTES ] = { 192, 168, 123, 1 };
66 const uint8_t ucDefaultMACAddress_IPv4[ ipMAC_ADDRESS_LENGTH_BYTES ] = { 0xab, 0xcd, 0xef, 0x11, 0x22, 0x33 };
67 
68 /* ============================  Unity Fixtures  ============================ */
69 
70 /*! called before each test case */
setUp(void)71 void setUp( void )
72 {
73     pxNetworkEndPoints = NULL;
74     pxNetworkInterfaces = NULL;
75 }
76 
77 /* =============================== Test Cases =============================== */
78 
79 /**
80  * @brief The purpose of this test is to verify FreeRTOS_FillEndPoint when all input parameters
81  * are valid IPv4 default setting.
82  *
83  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
84  *
85  * Test step:
86  *  - Call FreeRTOS_FillEndPoint to fill IP address, netmask, gateway address,
87  *    DNS server address, and MAC address into endpoint.
88  *  - Check if pxNetworkEndPoints is same as input endpoint.
89  *  - Check if all setting are correctly stored in endpoint.
90  */
test_FreeRTOS_FillEndPoint_HappyPath(void)91 void test_FreeRTOS_FillEndPoint_HappyPath( void )
92 {
93     NetworkInterface_t xInterfaces;
94     NetworkEndPoint_t xEndPoint;
95 
96     memset( &xInterfaces, 0, sizeof( NetworkInterface_t ) );
97     memset( &xEndPoint, 0, sizeof( xEndPoint ) );
98 
99     FreeRTOS_FillEndPoint( &xInterfaces,
100                            &xEndPoint,
101                            ucDefaultIPAddress_IPv4,
102                            ucDefaultNetMask_IPv4,
103                            ucDefaultGatewayAddress_IPv4,
104                            ucDefaultDNSServerAddress_IPv4,
105                            ucDefaultMACAddress_IPv4 );
106 
107     TEST_ASSERT_EQUAL( &xEndPoint, pxNetworkEndPoints );
108     TEST_ASSERT_EQUAL( &xEndPoint, xInterfaces.pxEndPoint );
109     TEST_ASSERT_EQUAL( IPV4_DEFAULT_ADDRESS, xEndPoint.ipv4_defaults.ulIPAddress );
110     TEST_ASSERT_EQUAL( IPV4_DEFAULT_NETMASK, xEndPoint.ipv4_defaults.ulNetMask );
111     TEST_ASSERT_EQUAL( IPV4_DEFAULT_GATEWAY, xEndPoint.ipv4_defaults.ulGatewayAddress );
112     TEST_ASSERT_EQUAL( IPV4_DEFAULT_DNS_SERVER, xEndPoint.ipv4_defaults.ulDNSServerAddresses[ 0 ] );
113     TEST_ASSERT_EQUAL_MEMORY( xEndPoint.xMACAddress.ucBytes, ucDefaultMACAddress_IPv4, ipMAC_ADDRESS_LENGTH_BYTES );
114     TEST_ASSERT_EQUAL( pdFALSE_UNSIGNED, xEndPoint.bits.bIPv6 );
115 }
116 
117 /**
118  * @brief The purpose of this test is to verify FreeRTOS_FillEndPoint when network interface is NULL.
119  *
120  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
121  *
122  * Test step:
123  *  - Call FreeRTOS_FillEndPoint to fill IP address, netmask, gateway address,
124  *    DNS server address, and MAC address into endpoint with NULL network interface.
125  *  - Check if pxNetworkEndPoints is NULL.
126  */
test_FreeRTOS_FillEndPoint_NullInterface(void)127 void test_FreeRTOS_FillEndPoint_NullInterface( void )
128 {
129     NetworkEndPoint_t xEndPoint;
130 
131     memset( &xEndPoint, 0, sizeof( xEndPoint ) );
132 
133     FreeRTOS_FillEndPoint( NULL,
134                            &xEndPoint,
135                            ucDefaultIPAddress_IPv4,
136                            ucDefaultNetMask_IPv4,
137                            ucDefaultGatewayAddress_IPv4,
138                            ucDefaultDNSServerAddress_IPv4,
139                            ucDefaultMACAddress_IPv4 );
140 
141     TEST_ASSERT_EQUAL( NULL, pxNetworkEndPoints );
142 }
143 
144 /**
145  * @brief The purpose of this test is to verify FreeRTOS_FillEndPoint when endpoint is NULL.
146  *
147  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
148  *
149  * Test step:
150  *  - Call FreeRTOS_FillEndPoint to fill IP address, netmask, gateway address,
151  *    DNS server address, and MAC address into endpoint with NULL endpoint.
152  *  - Check if pxNetworkEndPoints is NULL.
153  */
test_FreeRTOS_FillEndPoint_NullEndpoint(void)154 void test_FreeRTOS_FillEndPoint_NullEndpoint( void )
155 {
156     NetworkInterface_t xInterfaces;
157 
158     memset( &xInterfaces, 0, sizeof( NetworkInterface_t ) );
159 
160     FreeRTOS_FillEndPoint( &xInterfaces,
161                            NULL,
162                            ucDefaultIPAddress_IPv4,
163                            ucDefaultNetMask_IPv4,
164                            ucDefaultGatewayAddress_IPv4,
165                            ucDefaultDNSServerAddress_IPv4,
166                            ucDefaultMACAddress_IPv4 );
167 
168     TEST_ASSERT_EQUAL( NULL, pxNetworkEndPoints );
169 }
170 
171 /**
172  * @brief The purpose of this test is to verify FreeRTOS_FillEndPoint when all input parameters
173  * are valid IPv4 default setting.
174  *
175  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
176  *
177  * Test step:
178  *  - Call FreeRTOS_FillEndPoint to fill IP address, netmask, gateway address,
179  *    DNS server address, and MAC address into endpoint.
180  *  - Check if pxNetworkEndPoints is same as input endpoint.
181  *  - Check if all setting are correctly stored in endpoint.
182  *  - Loop steps up here three times.
183  */
test_FreeRTOS_FillEndPoint_MultipleEndpoints(void)184 void test_FreeRTOS_FillEndPoint_MultipleEndpoints( void )
185 {
186     NetworkInterface_t xInterfaces;
187     NetworkEndPoint_t xEndPoint[ 2 ];
188 
189     memset( &xInterfaces, 0, sizeof( NetworkInterface_t ) );
190     memset( &xEndPoint[ 0 ], 0, sizeof( NetworkEndPoint_t ) );
191     memset( &xEndPoint[ 1 ], 0, sizeof( NetworkEndPoint_t ) );
192 
193     FreeRTOS_FillEndPoint( &xInterfaces,
194                            &xEndPoint[ 0 ],
195                            ucDefaultIPAddress_IPv4,
196                            ucDefaultNetMask_IPv4,
197                            ucDefaultGatewayAddress_IPv4,
198                            ucDefaultDNSServerAddress_IPv4,
199                            ucDefaultMACAddress_IPv4 );
200 
201     TEST_ASSERT_EQUAL( &xEndPoint[ 0 ], pxNetworkEndPoints );
202     TEST_ASSERT_EQUAL( &xEndPoint[ 0 ], xInterfaces.pxEndPoint );
203     TEST_ASSERT_EQUAL( IPV4_DEFAULT_ADDRESS, xEndPoint[ 0 ].ipv4_defaults.ulIPAddress );
204     TEST_ASSERT_EQUAL( IPV4_DEFAULT_NETMASK, xEndPoint[ 0 ].ipv4_defaults.ulNetMask );
205     TEST_ASSERT_EQUAL( IPV4_DEFAULT_GATEWAY, xEndPoint[ 0 ].ipv4_defaults.ulGatewayAddress );
206     TEST_ASSERT_EQUAL( IPV4_DEFAULT_DNS_SERVER, xEndPoint[ 0 ].ipv4_defaults.ulDNSServerAddresses[ 0 ] );
207     TEST_ASSERT_EQUAL_MEMORY( xEndPoint[ 0 ].xMACAddress.ucBytes, ucDefaultMACAddress_IPv4, ipMAC_ADDRESS_LENGTH_BYTES );
208     TEST_ASSERT_EQUAL( pdFALSE_UNSIGNED, xEndPoint[ 0 ].bits.bIPv6 );
209     memset( &xEndPoint[ 0 ], 0, sizeof( NetworkEndPoint_t ) );
210 
211     /* Assertion triggered due to trying to add second endpoint with backward compatible enabled. */
212     catch_assert( FreeRTOS_FillEndPoint( &xInterfaces,
213                                          &xEndPoint[ 1 ],
214                                          ucDefaultGatewayAddress_IPv4,
215                                          ucDefaultNetMask_IPv4,
216                                          ucDefaultGatewayAddress_IPv4,
217                                          ucDefaultDNSServerAddress_IPv4,
218                                          ucDefaultMACAddress_IPv4 ) );
219 }
220 
221 /**
222  * @brief The purpose of this test is to verify FreeRTOS_FillEndPoint when all input parameters
223  * are valid IPv6 default setting.
224  *
225  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
226  *
227  * Test step:
228  *  - Call FreeRTOS_FillEndPoint to fill IP address, netmask, gateway address,
229  *    DNS server address, and MAC address into endpoint.
230  *  - Check if pxNetworkEndPoints is same as input endpoint.
231  *  - Check if all setting are correctly stored in endpoint.
232  *  - Call FreeRTOS_FillEndPoint to fill with same endpoint.
233  *  - Check if endpoint is not attached.
234  */
test_FreeRTOS_FillEndPoint_SameEndpoint(void)235 void test_FreeRTOS_FillEndPoint_SameEndpoint( void )
236 {
237     NetworkInterface_t xInterfaces;
238     NetworkEndPoint_t xEndPoint;
239 
240     memset( &xInterfaces, 0, sizeof( NetworkInterface_t ) );
241     memset( &xEndPoint, 0, sizeof( xEndPoint ) );
242 
243     FreeRTOS_FillEndPoint( &xInterfaces,
244                            &xEndPoint,
245                            ucDefaultIPAddress_IPv4,
246                            ucDefaultNetMask_IPv4,
247                            ucDefaultGatewayAddress_IPv4,
248                            ucDefaultDNSServerAddress_IPv4,
249                            ucDefaultMACAddress_IPv4 );
250 
251     TEST_ASSERT_EQUAL( &xEndPoint, pxNetworkEndPoints );
252     TEST_ASSERT_EQUAL( &xEndPoint, xInterfaces.pxEndPoint );
253     TEST_ASSERT_EQUAL( IPV4_DEFAULT_ADDRESS, xEndPoint.ipv4_defaults.ulIPAddress );
254     TEST_ASSERT_EQUAL( IPV4_DEFAULT_NETMASK, xEndPoint.ipv4_defaults.ulNetMask );
255     TEST_ASSERT_EQUAL( IPV4_DEFAULT_GATEWAY, xEndPoint.ipv4_defaults.ulGatewayAddress );
256     TEST_ASSERT_EQUAL( IPV4_DEFAULT_DNS_SERVER, xEndPoint.ipv4_defaults.ulDNSServerAddresses[ 0 ] );
257     TEST_ASSERT_EQUAL_MEMORY( xEndPoint.xMACAddress.ucBytes, ucDefaultMACAddress_IPv4, ipMAC_ADDRESS_LENGTH_BYTES );
258     TEST_ASSERT_EQUAL( pdFALSE_UNSIGNED, xEndPoint.bits.bIPv6 );
259 
260     /* Assertion triggered due to trying to add second endpoint with backward compatible enabled. */
261     catch_assert( FreeRTOS_FillEndPoint( &xInterfaces,
262                                          &xEndPoint,
263                                          ucDefaultIPAddress_IPv4,
264                                          ucDefaultNetMask_IPv4,
265                                          ucDefaultGatewayAddress_IPv4,
266                                          ucDefaultDNSServerAddress_IPv4,
267                                          ucDefaultMACAddress_IPv4 ) );
268 }
269 
270 /**
271  * @brief The purpose of this test is to verify FreeRTOS_AddNetworkInterface when input parameter
272  * is not NULL.
273  *
274  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
275  *
276  * Test step:
277  *  - Call FreeRTOS_AddNetworkInterface with one valid network interface.
278  *  - Check if the input network interface is stored into pxNetworkInterfaces.
279  */
test_FreeRTOS_AddNetworkInterface_HappyPath(void)280 void test_FreeRTOS_AddNetworkInterface_HappyPath( void )
281 {
282     NetworkInterface_t xNetworkInterface;
283 
284     memset( &xNetworkInterface, 0, sizeof( NetworkInterface_t ) );
285 
286     ( void ) FreeRTOS_AddNetworkInterface( &xNetworkInterface );
287 
288     TEST_ASSERT_EQUAL( &xNetworkInterface, pxNetworkInterfaces );
289     TEST_ASSERT_EQUAL( NULL, pxNetworkInterfaces->pxNext );
290 }
291 
292 /**
293  * @brief The purpose of this test is to verify FreeRTOS_AddNetworkInterface two times with
294  * different valid input parameters.
295  *
296  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
297  *
298  * Test step:
299  *  - Call FreeRTOS_AddNetworkInterface two times with three different network interfaces.
300  *  - Check if assertion triggered at the second time.
301  */
test_FreeRTOS_AddNetworkInterface_TwoInARow(void)302 void test_FreeRTOS_AddNetworkInterface_TwoInARow( void )
303 {
304     NetworkInterface_t xNetworkInterface[ 2 ];
305     NetworkInterface_t * pxNetworkInterface = NULL;
306     int i = 0;
307 
308     for( i = 0; i < 2; i++ )
309     {
310         memset( &( xNetworkInterface[ i ] ), 0, sizeof( NetworkInterface_t ) );
311     }
312 
313     ( void ) FreeRTOS_AddNetworkInterface( &( xNetworkInterface[ 0 ] ) );
314     TEST_ASSERT_EQUAL( &( xNetworkInterface[ 0 ] ), pxNetworkInterfaces );
315 
316     /* In backward compatible, we only support 1 interface */
317     catch_assert( FreeRTOS_AddNetworkInterface( &( xNetworkInterface[ 1 ] ) ) );
318 }
319 
320 /**
321  * @brief The purpose of this test is to verify FreeRTOS_AddNetworkInterface when input parameter
322  * is NULL.
323  *
324  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
325  *
326  * Test step:
327  *  - Call FreeRTOS_AddNetworkInterface with input NULL.
328  *  - Check if pxNetworkInterfaces is still NULL.
329  */
test_FreeRTOS_AddNetworkInterface_Null(void)330 void test_FreeRTOS_AddNetworkInterface_Null( void )
331 {
332     ( void ) FreeRTOS_AddNetworkInterface( NULL );
333     TEST_ASSERT_EQUAL( NULL, pxNetworkInterfaces );
334 }
335 
336 /**
337  * @brief FreeRTOS_FirstNetworkInterface should be able to find the first network interface.
338  *
339  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
340  *
341  * Test step:
342  *  - Assign a network interface into pxNetworkInterfaces.
343  *  - Call FreeRTOS_FirstNetworkInterface to get first network interface.
344  *  - Check if the return is same as the input.
345  */
test_FreeRTOS_FirstNetworkInterface_HappyPath(void)346 void test_FreeRTOS_FirstNetworkInterface_HappyPath( void )
347 {
348     NetworkInterface_t xNetworkInterface;
349     NetworkInterface_t * pxNetworkInterface = NULL;
350 
351     memset( &xNetworkInterface, 0, sizeof( NetworkInterface_t ) );
352     pxNetworkInterfaces = &xNetworkInterface;
353 
354     pxNetworkInterface = FreeRTOS_FirstNetworkInterface();
355 
356     TEST_ASSERT_EQUAL( &xNetworkInterface, pxNetworkInterface );
357 }
358 
359 /**
360  * @brief FreeRTOS_FirstNetworkInterface should be able to return NULL if there is no network interface available.
361  *
362  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
363  *
364  * Test step:
365  *  - Call FreeRTOS_FirstNetworkInterface to get first network interface.
366  *  - Check if the return is NULL.
367  */
test_FreeRTOS_FirstNetworkInterface_Null(void)368 void test_FreeRTOS_FirstNetworkInterface_Null( void )
369 {
370     NetworkInterface_t * pxNetworkInterface = NULL;
371 
372     pxNetworkInterface = FreeRTOS_FirstNetworkInterface();
373 
374     TEST_ASSERT_EQUAL( NULL, pxNetworkInterface );
375 }
376 
377 /**
378  * @brief FreeRTOS_NextNetworkInterface returns next network interface correctly.
379  *
380  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
381  *
382  * Test step:
383  *  - Create 1 network interface and attach them into pxNetworkInterfaces.
384  *  - Check if pxNetworkInterfaces is same as first input.
385  *  - Check if we can query next network interface and get NULL correctly by calling FreeRTOS_NextNetworkInterface.
386  */
test_FreeRTOS_NextNetworkInterface_HappyPath(void)387 void test_FreeRTOS_NextNetworkInterface_HappyPath( void )
388 {
389     NetworkInterface_t xNetworkInterface;
390     NetworkInterface_t * pxNetworkInterface = NULL;
391 
392     memset( &xNetworkInterface, 0, sizeof( NetworkInterface_t ) );
393     pxNetworkInterfaces = &xNetworkInterface;
394 
395     pxNetworkInterface = FreeRTOS_NextNetworkInterface( &xNetworkInterface );
396 
397     TEST_ASSERT_EQUAL( NULL, pxNetworkInterface );
398 }
399 
400 /**
401  * @brief FreeRTOS_FirstEndPoint should return endpoint attached on the input network interface.
402  *
403  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
404  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
405  *
406  * Test step:
407  *  - Set a network interface into pxNetworkInterfaces.
408  *  - Attach an endpoint to the network interface.
409  *  - Call FreeRTOS_FirstEndPoint to get attached endpoint.
410  *  - Check if returned endpoint is same as attached one.
411  */
test_FreeRTOS_FirstEndPoint_HappyPath(void)412 void test_FreeRTOS_FirstEndPoint_HappyPath( void )
413 {
414     NetworkInterface_t xNetworkInterface;
415     NetworkInterface_t * pxNetworkInterface = NULL;
416     NetworkEndPoint_t xEndPoint;
417     NetworkEndPoint_t * pxEndPoint = NULL;
418 
419     memset( &xNetworkInterface, 0, sizeof( NetworkInterface_t ) );
420     pxNetworkInterfaces = &xNetworkInterface;
421     memset( &xEndPoint, 0, sizeof( NetworkEndPoint_t ) );
422 
423     xEndPoint.pxNetworkInterface = pxNetworkInterfaces;
424     pxNetworkEndPoints = &xEndPoint;
425 
426     pxEndPoint = FreeRTOS_FirstEndPoint( pxNetworkInterfaces );
427 
428     TEST_ASSERT_EQUAL( &xEndPoint, pxEndPoint );
429 }
430 
431 /**
432  * @brief FreeRTOS_FirstEndPoint should return first endpoint in the list if input is NULL.
433  *
434  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
435  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
436  *
437  * Test step:
438  *  - Set a network interface into pxNetworkInterfaces.
439  *  - Attach an endpoint to the network interface.
440  *  - Call FreeRTOS_FirstEndPoint to get attached endpoint with NULL input.
441  *  - Check if returned endpoint is same as attached one.
442  */
test_FreeRTOS_FirstEndPoint_Null(void)443 void test_FreeRTOS_FirstEndPoint_Null( void )
444 {
445     NetworkInterface_t xNetworkInterface;
446     NetworkInterface_t * pxNetworkInterface = NULL;
447     NetworkEndPoint_t xEndPoint;
448     NetworkEndPoint_t * pxEndPoint = NULL;
449 
450     memset( &xNetworkInterface, 0, sizeof( NetworkInterface_t ) );
451     pxNetworkInterfaces = &xNetworkInterface;
452     memset( &xEndPoint, 0, sizeof( NetworkEndPoint_t ) );
453 
454     xEndPoint.pxNetworkInterface = pxNetworkInterfaces;
455     pxNetworkEndPoints = &xEndPoint;
456 
457     pxEndPoint = FreeRTOS_FirstEndPoint( NULL );
458 
459     TEST_ASSERT_EQUAL( &xEndPoint, pxEndPoint );
460 }
461 
462 /**
463  * @brief FreeRTOS_FirstEndPoint should return NULL if no endpoint available in the list.
464  *
465  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
466  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
467  *
468  * Test step:
469  *  - Set a network interface into pxNetworkInterfaces.
470  *  - Attach an endpoint to the network interface.
471  *  - Call FreeRTOS_FirstEndPoint to get attached endpoint with NULL input.
472  *  - Check if returned endpoint is same as attached one.
473  */
test_FreeRTOS_FirstEndPoint_NoEndpoints(void)474 void test_FreeRTOS_FirstEndPoint_NoEndpoints( void )
475 {
476     NetworkInterface_t xNetworkInterface;
477     NetworkInterface_t * pxNetworkInterface = NULL;
478     NetworkEndPoint_t xEndPoint;
479     NetworkEndPoint_t * pxEndPoint = NULL;
480 
481     memset( &xNetworkInterface, 0, sizeof( NetworkInterface_t ) );
482     pxNetworkInterfaces = &xNetworkInterface;
483     memset( &xEndPoint, 0, sizeof( NetworkEndPoint_t ) );
484 
485     xEndPoint.pxNetworkInterface = pxNetworkInterfaces;
486 
487     pxEndPoint = FreeRTOS_FirstEndPoint( &xNetworkInterface );
488 
489     TEST_ASSERT_EQUAL( NULL, pxEndPoint );
490 }
491 
492 /**
493  * @brief FreeRTOS_FirstEndPoint should return first endpoint with specified interface.
494  *
495  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
496  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
497  *
498  * Test step:
499  *  - Create 3 network interfaces and 3 endpoints.
500  *  - Attach one endpoint each network interface.
501  *  - Put interfaces & endpoints into the list.
502  *  - Loop to call FreeRTOS_FirstEndPoint to get first endpoint with each network interface.
503  *  - Check if returned endpoint is same as first endpoint.
504  */
test_FreeRTOS_FirstEndPoint_AnotherInterface(void)505 void test_FreeRTOS_FirstEndPoint_AnotherInterface( void )
506 {
507     /* Attach one endpoint to one network interface. Check if we can get correct endpoint by API. */
508     NetworkInterface_t xNetworkInterface[ 3 ];
509     NetworkInterface_t * pxNetworkInterface = NULL;
510     NetworkEndPoint_t xEndPoint[ 3 ];
511     NetworkEndPoint_t * pxEndPoint = NULL;
512     int i = 0;
513 
514     for( i = 0; i < 3; i++ )
515     {
516         memset( &( xNetworkInterface[ i ] ), 0, sizeof( NetworkInterface_t ) );
517         memset( &( xEndPoint[ i ] ), 0, sizeof( NetworkEndPoint_t ) );
518 
519         if( pxNetworkInterfaces == NULL )
520         {
521             pxNetworkInterfaces = &( xNetworkInterface[ i ] );
522             pxNetworkInterface = pxNetworkInterfaces;
523 
524             pxNetworkEndPoints = &( xEndPoint[ i ] );
525             pxEndPoint = pxNetworkEndPoints;
526         }
527         else
528         {
529             pxNetworkInterface->pxNext = &( xNetworkInterface[ i ] );
530             pxNetworkInterface = pxNetworkInterface->pxNext;
531 
532             pxEndPoint->pxNext = &( xEndPoint[ i ] );
533             pxEndPoint = pxEndPoint->pxNext;
534         }
535 
536         xEndPoint[ i ].pxNetworkInterface = pxNetworkInterface;
537     }
538 
539     for( i = 0; i < 3; i++ )
540     {
541         pxEndPoint = FreeRTOS_FirstEndPoint( &( xNetworkInterface[ i ] ) );
542         /* In backward compatible, stack only supports one endpoint. */
543         TEST_ASSERT_EQUAL( &( xEndPoint[ 0 ] ), pxEndPoint );
544     }
545 }
546 
547 /**
548  * @brief FreeRTOS_FirstEndPoint_IPv6 should return an IPv6 endpoint attached on the input network interface.
549  *
550  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
551  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
552  *
553  * Test step:
554  *  - Set a network interface into pxNetworkInterfaces.
555  *  - Attach an endpoint to the network interface.
556  *     - Set IPv6 bit in endpoint.
557  *  - Call FreeRTOS_FirstEndPoint to get attached endpoint.
558  *  - Check if returned endpoint is same as attached one.
559  */
test_FreeRTOS_FirstEndPoint_IPv6_HappyPath(void)560 void test_FreeRTOS_FirstEndPoint_IPv6_HappyPath( void )
561 {
562     NetworkInterface_t xNetworkInterface;
563     NetworkInterface_t * pxNetworkInterface = NULL;
564     NetworkEndPoint_t xEndPoint;
565     NetworkEndPoint_t * pxEndPoint = NULL;
566 
567     memset( &xNetworkInterface, 0, sizeof( NetworkInterface_t ) );
568     pxNetworkInterfaces = &xNetworkInterface;
569 
570     memset( &xEndPoint, 0, sizeof( NetworkEndPoint_t ) );
571     xEndPoint.bits.bIPv6 = pdTRUE;
572     xEndPoint.pxNetworkInterface = pxNetworkInterfaces;
573     pxNetworkEndPoints = &xEndPoint;
574 
575     pxEndPoint = FreeRTOS_FirstEndPoint_IPv6( pxNetworkInterfaces );
576 
577     TEST_ASSERT_EQUAL( &xEndPoint, pxEndPoint );
578 }
579 
580 /**
581  * @brief FreeRTOS_FirstEndPoint_IPv6 should return first endpoint in the list if input is NULL.
582  *
583  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
584  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
585  *
586  * Test step:
587  *  - Set a network interface into pxNetworkInterfaces.
588  *  - Attach an endpoint to the network interface.
589  *     - Set IPv6 bit in endpoint.
590  *  - Call FreeRTOS_FirstEndPoint_IPv6 to get attached endpoint with NULL input.
591  *  - Check if returned endpoint is same as attached one.
592  */
test_FreeRTOS_FirstEndPoint_IPv6_Null(void)593 void test_FreeRTOS_FirstEndPoint_IPv6_Null( void )
594 {
595     NetworkInterface_t xNetworkInterface;
596     NetworkInterface_t * pxNetworkInterface = NULL;
597     NetworkEndPoint_t xEndPoint;
598     NetworkEndPoint_t * pxEndPoint = NULL;
599 
600     memset( &xNetworkInterface, 0, sizeof( NetworkInterface_t ) );
601     pxNetworkInterfaces = &xNetworkInterface;
602 
603     memset( &xEndPoint, 0, sizeof( NetworkEndPoint_t ) );
604     xEndPoint.bits.bIPv6 = pdTRUE;
605     xEndPoint.pxNetworkInterface = pxNetworkInterfaces;
606     pxNetworkEndPoints = &xEndPoint;
607 
608     pxEndPoint = FreeRTOS_FirstEndPoint_IPv6( NULL );
609 
610     TEST_ASSERT_EQUAL( &xEndPoint, pxEndPoint );
611 }
612 
613 /**
614  * @brief FreeRTOS_FirstEndPoint_IPv6 should return first endpoint with specified interface.
615  *
616  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
617  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
618  *
619  * Test step:
620  *  - Create 3 network interfaces and 6 endpoints (e0~e5).
621  *  - Attach 2 endpoints each network interface.
622  *     - One endpoint with IPv6 bit set.
623  *     - The other endpoint with IPv6 bit clear.
624  *     - The other endpoint with IPv6 bit clear.
625  *       - Network interface 0: endpoint e0(IPv4)/e3(IPv6)
626  *       - Network interface 1: endpoint e1(IPv4)/e4(IPv6)
627  *       - Network interface 2: endpoint e2(IPv4)/e5(IPv6)
628  *  - Put interfaces & endpoints into the list.
629  *  - Loop to call FreeRTOS_FirstEndPoint to get e0 with each network interface.
630  *  - Check if returned endpoint is same as e0.
631  */
test_FreeRTOS_FirstEndPoint_IPv6_AnotherInterface(void)632 void test_FreeRTOS_FirstEndPoint_IPv6_AnotherInterface( void )
633 {
634     /* Attach one endpoint to one network interface. Check if we can get correct endpoint by API. */
635     NetworkInterface_t xNetworkInterface[ 3 ];
636     NetworkInterface_t * pxNetworkInterface = NULL;
637     NetworkEndPoint_t xEndPoint[ 6 ];
638     NetworkEndPoint_t * pxEndPoint = NULL;
639     int i = 0;
640 
641     for( i = 0; i < 3; i++ )
642     {
643         memset( &( xNetworkInterface[ i ] ), 0, sizeof( NetworkInterface_t ) );
644 
645         if( pxNetworkInterfaces == NULL )
646         {
647             pxNetworkInterfaces = &( xNetworkInterface[ i ] );
648             pxNetworkInterface = pxNetworkInterfaces;
649         }
650         else
651         {
652             pxNetworkInterface->pxNext = &( xNetworkInterface[ i ] );
653             pxNetworkInterface = pxNetworkInterface->pxNext;
654         }
655     }
656 
657     for( i = 0; i < 6; i++ )
658     {
659         bool bShouldBeIPv6 = i >= 3 ? true : false;
660         memset( &( xEndPoint[ i ] ), 0, sizeof( NetworkEndPoint_t ) );
661 
662         if( pxNetworkEndPoints == NULL )
663         {
664             pxNetworkEndPoints = &( xEndPoint[ i ] );
665             pxEndPoint = pxNetworkEndPoints;
666         }
667         else
668         {
669             pxEndPoint->pxNext = &( xEndPoint[ i ] );
670             pxEndPoint = pxEndPoint->pxNext;
671         }
672 
673         if( bShouldBeIPv6 )
674         {
675             xEndPoint[ i ].pxNetworkInterface = &( xNetworkInterface[ i - 3 ] );
676             xEndPoint[ i ].bits.bIPv6 = pdTRUE;
677         }
678         else
679         {
680             xEndPoint[ i ].pxNetworkInterface = &( xNetworkInterface[ i ] );
681             xEndPoint[ i ].bits.bIPv6 = pdFALSE;
682         }
683     }
684 
685     for( i = 0; i < 3; i++ )
686     {
687         pxEndPoint = FreeRTOS_FirstEndPoint_IPv6( &( xNetworkInterface[ i ] ) );
688         TEST_ASSERT_EQUAL( &( xEndPoint[ 0 ] ), pxEndPoint );
689     }
690 }
691 
692 /**
693  * @brief FreeRTOS_NextEndPoint always returns NULL when backward compatible enabled.
694  *
695  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
696  *
697  * Test step:
698  *  - Create 1 endpoints and stored then into pxNetworkEndPoints.
699  *  - Loop to call FreeRTOS_NextEndPoint to get endpoints.
700  *  - Check if returned endpoint is NULL.
701  */
test_FreeRTOS_NextEndPoint_HappyPath(void)702 void test_FreeRTOS_NextEndPoint_HappyPath( void )
703 {
704     NetworkEndPoint_t xEndPoint;
705     NetworkEndPoint_t * pxEndPoint = NULL;
706 
707     memset( &xEndPoint, 0, sizeof( NetworkEndPoint_t ) );
708     pxNetworkEndPoints = &xEndPoint;
709     pxEndPoint = pxNetworkEndPoints;
710 
711     pxEndPoint = FreeRTOS_NextEndPoint( NULL, pxEndPoint );
712     TEST_ASSERT_EQUAL( NULL, pxEndPoint );
713 }
714 
715 /**
716  * @brief FreeRTOS_FindEndPointOnIP_IPv4 should return the endpoint with specified IPv4 address.
717  *
718  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
719  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
720  *
721  * Test step:
722  *  - Create 1 endpoint and add it to the list.
723  *  - Call FreeRTOS_FindEndPointOnIP_IPv4 to query with same IP address stored in endpoint.
724  *  - Check if returned endpoint is same.
725  */
test_FreeRTOS_FindEndPointOnIP_IPv4_HappyPath(void)726 void test_FreeRTOS_FindEndPointOnIP_IPv4_HappyPath( void )
727 {
728     NetworkEndPoint_t xEndPoint;
729     NetworkEndPoint_t * pxEndPoint = NULL;
730 
731     memset( &xEndPoint, 0, sizeof( NetworkEndPoint_t ) );
732     xEndPoint.ipv4_settings.ulIPAddress = IPV4_DEFAULT_ADDRESS;
733 
734     pxNetworkEndPoints = &xEndPoint;
735 
736     pxEndPoint = FreeRTOS_FindEndPointOnIP_IPv4( IPV4_DEFAULT_ADDRESS, 0 );
737     TEST_ASSERT_EQUAL( &xEndPoint, pxEndPoint );
738 }
739 
740 /**
741  * @brief FreeRTOS_FindEndPointOnIP_IPv4 should return NULL if no matched endpoint found.
742  *
743  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
744  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
745  *
746  * Test step:
747  *  - Create 1 endpoint and add it to the list.
748  *     - Set e0 IP address to 192.168.123.223 (IPV4_DEFAULT_ADDRESS).
749  *  - Call FreeRTOS_FindEndPointOnIP_IPv4 to query different IP address.
750  *  - Check if returned endpoint is NULL.
751  */
test_FreeRTOS_FindEndPointOnIP_IPv4_NotFound(void)752 void test_FreeRTOS_FindEndPointOnIP_IPv4_NotFound( void )
753 {
754     NetworkEndPoint_t xEndPoint;
755     NetworkEndPoint_t * pxEndPoint = NULL;
756 
757     /* Initialize e0. */
758     memset( &xEndPoint, 0, sizeof( NetworkEndPoint_t ) );
759     xEndPoint.ipv4_settings.ulIPAddress = IPV4_DEFAULT_ADDRESS;
760     pxNetworkEndPoints = &xEndPoint;
761 
762     pxEndPoint = FreeRTOS_FindEndPointOnIP_IPv4( IPV4_DEFAULT_GATEWAY, 0 );
763     TEST_ASSERT_EQUAL( NULL, pxEndPoint );
764 }
765 
766 /**
767  * @brief FreeRTOS_FindEndPointOnIP_IPv4 should return first endpoint in the list when query IP address is 0.
768  *
769  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
770  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
771  *
772  * Test step:
773  *  - Create 1 endpoint and add it to the list.
774  *  - Call FreeRTOS_FindEndPointOnIP_IPv4 to query with 0 address.
775  *  - Check if returned endpoint is the first endpoint.
776  */
test_FreeRTOS_FindEndPointOnIP_IPv4_AnyEndpoint(void)777 void test_FreeRTOS_FindEndPointOnIP_IPv4_AnyEndpoint( void )
778 {
779     NetworkEndPoint_t xEndPoint;
780     NetworkEndPoint_t * pxEndPoint = NULL;
781 
782     memset( &xEndPoint, 0, sizeof( NetworkEndPoint_t ) );
783     xEndPoint.ipv4_settings.ulIPAddress = IPV4_DEFAULT_ADDRESS;
784     pxNetworkEndPoints = &xEndPoint;
785 
786     pxEndPoint = FreeRTOS_FindEndPointOnIP_IPv4( 0, 0 );
787     TEST_ASSERT_EQUAL( &xEndPoint, pxEndPoint );
788 }
789 
790 /**
791  * @brief FreeRTOS_FindEndPointOnMAC should return the endpoint with specified MAC address & network interface.
792  *
793  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
794  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
795  *
796  * Test step:
797  *  - Create 1 network interface and add it to the list.
798  *  - Create 1 endpoint, attach to the interface, and add it to the list.
799  *  - Call FreeRTOS_FindEndPointOnMAC to query with same MAC address stored in endpoint.
800  *  - Check if returned endpoint is same.
801  */
test_FreeRTOS_FindEndPointOnMAC_HappyPath(void)802 void test_FreeRTOS_FindEndPointOnMAC_HappyPath( void )
803 {
804     NetworkEndPoint_t xEndPoint;
805     NetworkEndPoint_t * pxEndPoint = NULL;
806     /* MAC address:  */
807     const MACAddress_t xMACAddress = { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc };
808     NetworkInterface_t xNetworkInterface;
809 
810     /* Initialize network interface and add it to the list. */
811     memset( &xNetworkInterface, 0, sizeof( NetworkInterface_t ) );
812     pxNetworkInterfaces = &xNetworkInterface;
813 
814     /* Initialize network endpoint and add it to the list. */
815     memset( &xEndPoint, 0, sizeof( NetworkEndPoint_t ) );
816     memcpy( xEndPoint.xMACAddress.ucBytes, xMACAddress.ucBytes, sizeof( MACAddress_t ) );
817     xEndPoint.pxNetworkInterface = &xNetworkInterface;
818     pxNetworkEndPoints = &xEndPoint;
819 
820     pxEndPoint = FreeRTOS_FindEndPointOnMAC( &xMACAddress, &xNetworkInterface );
821     TEST_ASSERT_EQUAL( &xEndPoint, pxEndPoint );
822 }
823 
824 /**
825  * @brief FreeRTOS_FindEndPointOnMAC should return the endpoint with NULL pointer as MAC address.
826  *
827  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
828  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
829  *
830  * Test step:
831  *  - Create 1 endpoint and add it to the list.
832  *  - Call FreeRTOS_FindEndPointOnMAC to query with NULL pointer as MAC address.
833  *  - Check if returned endpoint is NULL.
834  */
test_FreeRTOS_FindEndPointOnMAC_NullMAC(void)835 void test_FreeRTOS_FindEndPointOnMAC_NullMAC( void )
836 {
837     NetworkEndPoint_t xEndPoint;
838     NetworkEndPoint_t * pxEndPoint = NULL;
839     /* MAC address:  */
840     const MACAddress_t xMACAddress = { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc };
841 
842     /* Initialize network endpoint and add it to the list. */
843     memset( &xEndPoint, 0, sizeof( NetworkEndPoint_t ) );
844     memcpy( xEndPoint.xMACAddress.ucBytes, xMACAddress.ucBytes, sizeof( MACAddress_t ) );
845     pxNetworkEndPoints = &xEndPoint;
846 
847     pxEndPoint = FreeRTOS_FindEndPointOnMAC( NULL, NULL );
848     TEST_ASSERT_EQUAL( NULL, pxEndPoint );
849 }
850 
851 /**
852  * @brief FreeRTOS_FindEndPointOnMAC should return NULL if no endpoint matches.
853  *
854  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
855  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
856  *
857  * Test step:
858  *  - Create 1 network interface and add it to the list.
859  *  - Create 1 endpoint, attach to the interface, and add it to the list.
860  *     - Use ucDefaultMACAddress_IPv4 as MAC address.
861  *  - Call FreeRTOS_FindEndPointOnMAC to query with different MAC address.
862  *  - Check if returned endpoint is NULL.
863  */
test_FreeRTOS_FindEndPointOnMAC_NotFound(void)864 void test_FreeRTOS_FindEndPointOnMAC_NotFound( void )
865 {
866     NetworkEndPoint_t xEndPoint;
867     NetworkEndPoint_t * pxEndPoint = NULL;
868     /* MAC address:  */
869     const MACAddress_t xMACAddress = { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc };
870     NetworkInterface_t xNetworkInterface;
871     MACAddress_t * pxQueryMACAddress = NULL;
872 
873     /* Initialize network interface and add it to the list. */
874     memset( &xNetworkInterface, 0, sizeof( NetworkInterface_t ) );
875     pxNetworkInterfaces = &xNetworkInterface;
876 
877     /* Initialize network endpoint and add it to the list. */
878     memset( &xEndPoint, 0, sizeof( NetworkEndPoint_t ) );
879     memcpy( xEndPoint.xMACAddress.ucBytes, xMACAddress.ucBytes, sizeof( MACAddress_t ) );
880     xEndPoint.pxNetworkInterface = &xNetworkInterface;
881     pxNetworkEndPoints = &xEndPoint;
882 
883     pxQueryMACAddress = ( MACAddress_t * ) ( &ucDefaultMACAddress_IPv4 );
884     pxEndPoint = FreeRTOS_FindEndPointOnMAC( pxQueryMACAddress, &xNetworkInterface );
885     TEST_ASSERT_EQUAL( NULL, pxEndPoint );
886 }
887 
888 /**
889  * @brief FreeRTOS_FindEndPointOnNetMask should be able to find the endpoint within same network region.
890  *
891  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
892  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
893  *
894  * Test step:
895  *  - Create 1 endpoint and add it to the list.
896  *     - Set the IP address to 192.168.123.223.
897  *     - Set the netmask to 255.255.255.0.
898  *  - Call FreeRTOS_FindEndPointOnNetMask to query for IP address 192.168.123.1.
899  *  - Check if returned endpoint is same.
900  */
test_FreeRTOS_FindEndPointOnNetMask_HappyPath(void)901 void test_FreeRTOS_FindEndPointOnNetMask_HappyPath( void )
902 {
903     NetworkEndPoint_t xEndPoint;
904     NetworkEndPoint_t * pxEndPoint = NULL;
905 
906     /* Initialize network endpoint and add it to the list. */
907     memset( &xEndPoint, 0, sizeof( NetworkEndPoint_t ) );
908     xEndPoint.ipv4_settings.ulIPAddress = IPV4_DEFAULT_ADDRESS;
909     xEndPoint.ipv4_settings.ulNetMask = IPV4_DEFAULT_NETMASK;
910     pxNetworkEndPoints = &xEndPoint;
911 
912     /* IPV4_DEFAULT_DNS_SERVER is 192.168.123.1 within the network region. */
913     pxEndPoint = FreeRTOS_FindEndPointOnNetMask( IPV4_DEFAULT_DNS_SERVER, 0 );
914     TEST_ASSERT_EQUAL( &xEndPoint, pxEndPoint );
915 }
916 
917 /**
918  * @brief FreeRTOS_FindEndPointOnNetMask should be able to return NULL if no endpoint is in same network region.
919  *
920  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
921  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
922  *
923  * Test step:
924  *  - Create 1 endpoint and add it to the list.
925  *     - Set the netmask to 255.255.255.0.
926  *     - Set the IP address to 192.168.123.223.
927  *  - Call FreeRTOS_FindEndPointOnNetMask to query for IP address 192.168.1.1.
928  *  - Check if returned endpoint is NULL.
929  */
test_FreeRTOS_FindEndPointOnNetMask_NotFound(void)930 void test_FreeRTOS_FindEndPointOnNetMask_NotFound( void )
931 {
932     NetworkEndPoint_t xEndPoint;
933     NetworkEndPoint_t * pxEndPoint = NULL;
934 
935     /* Initialize network endpoint and add it to the list. */
936     memset( &xEndPoint, 0, sizeof( NetworkEndPoint_t ) );
937     xEndPoint.ipv4_settings.ulIPAddress = IPV4_DEFAULT_ADDRESS;
938     xEndPoint.ipv4_settings.ulNetMask = IPV4_DEFAULT_NETMASK;
939     pxNetworkEndPoints = &xEndPoint;
940 
941     /* 192.168.1.1 is 0x0101A8C0. */
942     pxEndPoint = FreeRTOS_FindEndPointOnNetMask( 0x0101A8C0, 0 );
943     TEST_ASSERT_EQUAL( NULL, pxEndPoint );
944 }
945 
946 /**
947  * @brief FreeRTOS_InterfaceEndPointOnNetMask should be able to find the endpoint within same network region.
948  *
949  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
950  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
951  *
952  * Test step:
953  *  - Create 1 endpoint and add it to the list.
954  *     - Set the IP address to 192.168.123.223 (IPV4_DEFAULT_ADDRESS).
955  *     - Set the netmask to 255.255.255.0.
956  *  - Call FreeRTOS_InterfaceEndPointOnNetMask to query for IP address 192.168.123.1.
957  *  - Check if returned endpoint is same.
958  */
test_FreeRTOS_InterfaceEndPointOnNetMask_HappyPath(void)959 void test_FreeRTOS_InterfaceEndPointOnNetMask_HappyPath( void )
960 {
961     NetworkEndPoint_t xEndPoint;
962     NetworkEndPoint_t * pxEndPoint = NULL;
963     NetworkInterface_t xNetworkInterface;
964 
965     /* Initialize network interface and add it to the list. */
966     memset( &xNetworkInterface, 0, sizeof( NetworkInterface_t ) );
967     pxNetworkInterfaces = &xNetworkInterface;
968 
969     /* Initialize network endpoint and add it to the list. */
970     memset( &xEndPoint, 0, sizeof( NetworkEndPoint_t ) );
971     xEndPoint.ipv4_settings.ulIPAddress = IPV4_DEFAULT_ADDRESS;
972     xEndPoint.ipv4_settings.ulNetMask = IPV4_DEFAULT_NETMASK;
973     xEndPoint.pxNetworkInterface = &xNetworkInterface;
974     pxNetworkEndPoints = &xEndPoint;
975 
976     /* IPV4_DEFAULT_ADDRESS is 192.168.123.1 within the network region. */
977     pxEndPoint = FreeRTOS_InterfaceEndPointOnNetMask( &xNetworkInterface, IPV4_DEFAULT_DNS_SERVER, 0 );
978     TEST_ASSERT_EQUAL( &xEndPoint, pxEndPoint );
979 }
980 
981 /**
982  * @brief FreeRTOS_InterfaceEndPointOnNetMask returns NULL when there is no endpoint matches.
983  *
984  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
985  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
986  *
987  * Test step:
988  *  - Create 1 IPv4 endpoint and add it to the list.
989  *     - Set the IP address to 192.168.123.223 (IPV4_DEFAULT_ADDRESS).
990  *     - Set the netmask to 255.255.255.0.
991  *  - Call FreeRTOS_InterfaceEndPointOnNetMask to query for IP address 192.168.122.1.
992  *  - Check if returned endpoint is same.
993  */
test_FreeRTOS_InterfaceEndPointOnNetMask_NotFound(void)994 void test_FreeRTOS_InterfaceEndPointOnNetMask_NotFound( void )
995 {
996     NetworkEndPoint_t xEndPoint;
997     NetworkEndPoint_t * pxEndPoint = NULL;
998     NetworkInterface_t xNetworkInterface;
999 
1000     /* Initialize network interface and add it to the list. */
1001     memset( &xNetworkInterface, 0, sizeof( NetworkInterface_t ) );
1002     pxNetworkInterfaces = &xNetworkInterface;
1003 
1004     /* Initialize network endpoint and add it to the list. */
1005     memset( &xEndPoint, 0, sizeof( NetworkEndPoint_t ) );
1006     xEndPoint.ipv4_settings.ulIPAddress = IPV4_DEFAULT_ADDRESS;
1007     xEndPoint.ipv4_settings.ulNetMask = IPV4_DEFAULT_NETMASK;
1008     xEndPoint.pxNetworkInterface = &xNetworkInterface;
1009     pxNetworkEndPoints = &xEndPoint;
1010 
1011     /* 192.168.122.1 is not in the network region. */
1012     pxEndPoint = FreeRTOS_InterfaceEndPointOnNetMask( &xNetworkInterface, 0x017AA8C0, 0 );
1013     TEST_ASSERT_EQUAL( NULL, pxEndPoint );
1014 }
1015 
1016 /**
1017  * @brief FreeRTOS_FindGateWay should be able to find the endpoint with valid IPv4 gateway address.
1018  *
1019  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
1020  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
1021  *
1022  * Test step:
1023  *  - Create 1 IPv4 endpoint and add it to the list.
1024  *     - Set the gateway address to 192.168.123.254 (IPV4_DEFAULT_GATEWAY).
1025  *  - Call FreeRTOS_FindGateWay with ipTYPE_IPv4.
1026  *  - Check if returned endpoint is same.
1027  */
test_FreeRTOS_FindGateWay_IPv4HappyPath(void)1028 void test_FreeRTOS_FindGateWay_IPv4HappyPath( void )
1029 {
1030     NetworkEndPoint_t xEndPoint;
1031     NetworkEndPoint_t * pxEndPoint = NULL;
1032 
1033     /* Initialize network endpoint and add it to the list. */
1034     memset( &xEndPoint, 0, sizeof( NetworkEndPoint_t ) );
1035     xEndPoint.ipv4_settings.ulGatewayAddress = IPV4_DEFAULT_GATEWAY;
1036     pxNetworkEndPoints = &xEndPoint;
1037 
1038     pxEndPoint = FreeRTOS_FindGateWay( ipTYPE_IPv4 );
1039     TEST_ASSERT_EQUAL( &xEndPoint, pxEndPoint );
1040 }
1041 
1042 /**
1043  * @brief FreeRTOS_FindGateWay should be able to return NULL if no valid IPv4 gateway address.
1044  *
1045  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
1046  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
1047  *
1048  * Test step:
1049  *  - Create 1 IPv4 endpoint and add it to the list.
1050  *     - Set the gateway address to 0 (invalid address).
1051  *  - Call FreeRTOS_FindGateWay with ipTYPE_IPv4.
1052  *  - Check if returned endpoint is same.
1053  */
test_FreeRTOS_FindGateWay_IPv4NotFound(void)1054 void test_FreeRTOS_FindGateWay_IPv4NotFound( void )
1055 {
1056     NetworkEndPoint_t xEndPoint;
1057     NetworkEndPoint_t * pxEndPoint = NULL;
1058 
1059     /* Initialize network endpoint and add it to the list. */
1060     memset( &xEndPoint, 0, sizeof( NetworkEndPoint_t ) );
1061     pxNetworkEndPoints = &xEndPoint;
1062 
1063     pxEndPoint = FreeRTOS_FindGateWay( ipTYPE_IPv4 );
1064     TEST_ASSERT_EQUAL( NULL, pxEndPoint );
1065 }
1066 
1067 /**
1068  * @brief FreeRTOS_FindGateWay should be able to return NULL if no endpoint stored in the list.
1069  *
1070  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
1071  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
1072  *
1073  * Test step:
1074  *  - Call FreeRTOS_FindGateWay with ipTYPE_IPv4.
1075  *  - Check if returned endpoint is same.
1076  */
test_FreeRTOS_FindGateWay_IPv4EmptyList(void)1077 void test_FreeRTOS_FindGateWay_IPv4EmptyList( void )
1078 {
1079     NetworkEndPoint_t * pxEndPoint = NULL;
1080 
1081     pxEndPoint = FreeRTOS_FindGateWay( ipTYPE_IPv4 );
1082     TEST_ASSERT_EQUAL( NULL, pxEndPoint );
1083 }
1084 
1085 /**
1086  * @brief When backward compatible enabled, FreeRTOS_MatchingEndpoint always return the only endpoint stored in the list.
1087  *
1088  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
1089  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
1090  *
1091  * Test step:
1092  *  - Create 1 interface and 1 endpoint.
1093  *  - Put interface & endpoint into the list.
1094  *     - Assign 192.168.123.223 (IPV4_DEFAULT_ADDRESS) to the endpoint.
1095  *     - Assign ab:cd:ef:11:22:33 (ucDefaultMACAddress_IPv4) to the endpoint.
1096  *     - Attach endpoint to interface.
1097  *  - Call FreeRTOS_MatchingEndpoint and check if returned endpoint is same.
1098  */
test_FreeRTOS_MatchingEndpoint_MatchIPv4Address()1099 void test_FreeRTOS_MatchingEndpoint_MatchIPv4Address()
1100 {
1101     NetworkInterface_t xNetworkInterface;
1102     NetworkEndPoint_t xEndPoint;
1103     NetworkEndPoint_t * pxEndPoint = NULL;
1104 
1105     /* Initialize network interface. */
1106     memset( &xNetworkInterface, 0, sizeof( NetworkInterface_t ) );
1107     pxNetworkInterfaces = &xNetworkInterface;
1108 
1109     /* Initialize endpoint. */
1110     memset( &xEndPoint, 0, sizeof( NetworkEndPoint_t ) );
1111     xEndPoint.ipv4_settings.ulIPAddress = IPV4_DEFAULT_ADDRESS;
1112     memcpy( xEndPoint.xMACAddress.ucBytes, ucDefaultMACAddress_IPv4, sizeof( ucDefaultMACAddress_IPv4 ) );
1113     xEndPoint.pxNetworkInterface = &xNetworkInterface;
1114     pxNetworkEndPoints = &xEndPoint;
1115 
1116     pxEndPoint = FreeRTOS_MatchingEndpoint( NULL, NULL );
1117     TEST_ASSERT_EQUAL( &xEndPoint, pxEndPoint );
1118 }
1119 
1120 /**
1121  * @brief When backward compatible enabled, FreeRTOS_FindEndPointOnIP_IPv6 always return the only endpoint stored in the list.
1122  *
1123  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
1124  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
1125  *
1126  * Test step:
1127  *  - Create 1 interface and 1 endpoint.
1128  *  - Put interface & endpoint into the list.
1129  *     - Assign 192.168.123.223 (IPV4_DEFAULT_ADDRESS) to the endpoint.
1130  *     - Assign ab:cd:ef:11:22:33 (ucDefaultMACAddress_IPv4) to the endpoint.
1131  *     - Attach endpoint to interface.
1132  *  - Call FreeRTOS_FindEndPointOnIP_IPv6 and check if returned endpoint is same.
1133  */
test_FreeRTOS_FindEndPointOnIP_IPv6_HappyPath()1134 void test_FreeRTOS_FindEndPointOnIP_IPv6_HappyPath()
1135 {
1136     NetworkInterface_t xNetworkInterface;
1137     NetworkEndPoint_t xEndPoint;
1138     NetworkEndPoint_t * pxEndPoint = NULL;
1139 
1140     /* Initialize network interface. */
1141     memset( &xNetworkInterface, 0, sizeof( NetworkInterface_t ) );
1142     pxNetworkInterfaces = &xNetworkInterface;
1143 
1144     /* Initialize endpoint. */
1145     memset( &xEndPoint, 0, sizeof( NetworkEndPoint_t ) );
1146     xEndPoint.ipv4_settings.ulIPAddress = IPV4_DEFAULT_ADDRESS;
1147     memcpy( xEndPoint.xMACAddress.ucBytes, ucDefaultMACAddress_IPv4, sizeof( ucDefaultMACAddress_IPv4 ) );
1148     xEndPoint.pxNetworkInterface = &xNetworkInterface;
1149     pxNetworkEndPoints = &xEndPoint;
1150 
1151     pxEndPoint = FreeRTOS_FindEndPointOnIP_IPv6( NULL );
1152     TEST_ASSERT_EQUAL( &xEndPoint, pxEndPoint );
1153 }
1154 
1155 /**
1156  * @brief When backward compatible enabled, FreeRTOS_FindEndPointOnNetMask_IPv6 always return the only endpoint stored in the list.
1157  *
1158  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
1159  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
1160  *
1161  * Test step:
1162  *  - Create 1 interface and 1 endpoint.
1163  *  - Put interface & endpoint into the list.
1164  *     - Assign 192.168.123.223 (IPV4_DEFAULT_ADDRESS) to the endpoint.
1165  *     - Assign ab:cd:ef:11:22:33 (ucDefaultMACAddress_IPv4) to the endpoint.
1166  *     - Attach endpoint to interface.
1167  *  - Call FreeRTOS_FindEndPointOnNetMask_IPv6 and check if returned endpoint is same.
1168  */
test_FreeRTOS_FindEndPointOnNetMask_IPv6_HappyPath()1169 void test_FreeRTOS_FindEndPointOnNetMask_IPv6_HappyPath()
1170 {
1171     NetworkInterface_t xNetworkInterface;
1172     NetworkEndPoint_t xEndPoint;
1173     NetworkEndPoint_t * pxEndPoint = NULL;
1174 
1175     /* Initialize network interface. */
1176     memset( &xNetworkInterface, 0, sizeof( NetworkInterface_t ) );
1177     pxNetworkInterfaces = &xNetworkInterface;
1178 
1179     /* Initialize endpoint. */
1180     memset( &xEndPoint, 0, sizeof( NetworkEndPoint_t ) );
1181     xEndPoint.ipv4_settings.ulIPAddress = IPV4_DEFAULT_ADDRESS;
1182     memcpy( xEndPoint.xMACAddress.ucBytes, ucDefaultMACAddress_IPv4, sizeof( ucDefaultMACAddress_IPv4 ) );
1183     xEndPoint.pxNetworkInterface = &xNetworkInterface;
1184     pxNetworkEndPoints = &xEndPoint;
1185 
1186     pxEndPoint = FreeRTOS_FindEndPointOnNetMask_IPv6( NULL );
1187     TEST_ASSERT_EQUAL( &xEndPoint, pxEndPoint );
1188 }
1189