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_queue.h"
43 #include "mock_event_groups.h"
44 
45 #include "mock_FreeRTOS_IP.h"
46 #include "mock_FreeRTOS_IPv6.h"
47 #include "mock_FreeRTOS_Sockets.h"
48 
49 #include "FreeRTOS_Routing.h"
50 
51 #include "catch_assert.h"
52 
53 #include "FreeRTOSIPConfig.h"
54 
55 #include "FreeRTOS_Routing_stubs.c"
56 
57 /* ===========================  EXTERN VARIABLES  =========================== */
58 
59 /* Default IPv4 address is 192.168.123.223, which is 0xDF7BA8C0. */
60 #define IPV4_DEFAULT_ADDRESS       ( 0xDF7BA8C0 )
61 /* Default IPv4 netmask is 255.255.255.0, which is 0x00FFFFFF. */
62 #define IPV4_DEFAULT_NETMASK       ( 0x00FFFFFF )
63 /* Default IPv4 netmask is 192.168.123.254, which is 0xFE7BA8C0. */
64 #define IPV4_DEFAULT_GATEWAY       ( 0xFE7BA8C0 )
65 /* Default IPv4 netmask is 192.168.123.1, which is 0x017BA8C0. */
66 #define IPV4_DEFAULT_DNS_SERVER    ( 0x017BA8C0 )
67 
68 extern RoutingStats_t xRoutingStatistics;
69 const struct xIPv6_Address FreeRTOS_in6addr_any;
70 const struct xIPv6_Address FreeRTOS_in6addr_loopback;
71 
72 const uint8_t ucDefaultIPAddress_IPv4[ ipIP_ADDRESS_LENGTH_BYTES ] = { 192, 168, 123, 223 };
73 const uint8_t ucDefaultNetMask_IPv4[ ipIP_ADDRESS_LENGTH_BYTES ] = { 255, 255, 255, 0 };
74 const uint8_t ucDefaultGatewayAddress_IPv4[ ipIP_ADDRESS_LENGTH_BYTES ] = { 192, 168, 123, 254 };
75 const uint8_t ucDefaultDNSServerAddress_IPv4[ ipIP_ADDRESS_LENGTH_BYTES ] = { 192, 168, 123, 1 };
76 const uint8_t ucDefaultMACAddress_IPv4[ ipMAC_ADDRESS_LENGTH_BYTES ] = { 0xab, 0xcd, 0xef, 0x11, 0x22, 0x33 };
77 
78 /* Default IPv6 address is set to 2001::1 */
79 const IPv6_Address_t xDefaultIPAddress_IPv6 = { 0x20, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 };
80 /* Default IPv6 address is set to 2001:: */
81 const IPv6_Address_t xDefaultNetPrefix_IPv6 = { 0x20, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
82 const size_t xDefaultPrefixLength = 64U;
83 /* Default IPv6 address is set to 2001::fffe */
84 const IPv6_Address_t xDefaultGatewayAddress_IPv6 = { 0x20, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xfe };
85 /* Default IPv6 address is set to 2001::ffee */
86 const IPv6_Address_t xDefaultDNSServerAddress_IPv6 = { 0x20, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xee };
87 
88 const uint8_t ucDefaultMACAddress_IPv6[ ipMAC_ADDRESS_LENGTH_BYTES ] = { 0x11, 0x22, 0x33, 0xab, 0xcd, 0xef };
89 
90 /* ============================  Unity Fixtures  ============================ */
91 
92 /*! called before each test case */
setUp(void)93 void setUp( void )
94 {
95     pxNetworkEndPoints = NULL;
96     pxNetworkInterfaces = NULL;
97 }
98 
99 /* ==============================  Test Cases  ============================== */
100 
101 /**
102  * @brief The purpose of this test is to verify FreeRTOS_FillEndPoint when all input parameters
103  * are valid IPv4 default setting.
104  *
105  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
106  *
107  * Test step:
108  *  - Call FreeRTOS_FillEndPoint to fill IP address, netmask, gateway address,
109  *    DNS server address, and MAC address into endpoint.
110  *  - Check if pxNetworkEndPoints is same as input endpoint.
111  *  - Check if all setting are correctly stored in endpoint.
112  */
test_FreeRTOS_FillEndPoint_HappyPath(void)113 void test_FreeRTOS_FillEndPoint_HappyPath( void )
114 {
115     NetworkInterface_t xInterfaces;
116     NetworkEndPoint_t xEndPoint;
117 
118     memset( &xInterfaces, 0, sizeof( NetworkInterface_t ) );
119     memset( &xEndPoint, 0, sizeof( xEndPoint ) );
120 
121     printf( "test_FreeRTOS_FillEndPoint_happy_path: xEndPoint=%p\n", &xEndPoint );
122 
123     FreeRTOS_FillEndPoint( &xInterfaces,
124                            &xEndPoint,
125                            ucDefaultIPAddress_IPv4,
126                            ucDefaultNetMask_IPv4,
127                            ucDefaultGatewayAddress_IPv4,
128                            ucDefaultDNSServerAddress_IPv4,
129                            ucDefaultMACAddress_IPv4 );
130 
131     TEST_ASSERT_EQUAL( &xEndPoint, pxNetworkEndPoints );
132     TEST_ASSERT_EQUAL( &xEndPoint, xInterfaces.pxEndPoint );
133     TEST_ASSERT_EQUAL( IPV4_DEFAULT_ADDRESS, xEndPoint.ipv4_defaults.ulIPAddress );
134     TEST_ASSERT_EQUAL( IPV4_DEFAULT_NETMASK, xEndPoint.ipv4_defaults.ulNetMask );
135     TEST_ASSERT_EQUAL( IPV4_DEFAULT_GATEWAY, xEndPoint.ipv4_defaults.ulGatewayAddress );
136     TEST_ASSERT_EQUAL( IPV4_DEFAULT_DNS_SERVER, xEndPoint.ipv4_defaults.ulDNSServerAddresses[ 0 ] );
137     TEST_ASSERT_EQUAL_MEMORY( xEndPoint.xMACAddress.ucBytes, ucDefaultMACAddress_IPv4, ipMAC_ADDRESS_LENGTH_BYTES );
138     TEST_ASSERT_EQUAL( pdFALSE_UNSIGNED, xEndPoint.bits.bIPv6 );
139 }
140 
141 /**
142  * @brief The purpose of this test is to verify FreeRTOS_FillEndPoint when network interface is NULL.
143  *
144  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
145  *
146  * Test step:
147  *  - Call FreeRTOS_FillEndPoint to fill IP address, netmask, gateway address,
148  *    DNS server address, and MAC address into endpoint with NULL network interface.
149  *  - Check if pxNetworkEndPoints is NULL.
150  */
test_FreeRTOS_FillEndPoint_NullInterface(void)151 void test_FreeRTOS_FillEndPoint_NullInterface( void )
152 {
153     NetworkEndPoint_t xEndPoint;
154 
155     memset( &xEndPoint, 0, sizeof( xEndPoint ) );
156 
157     FreeRTOS_FillEndPoint( NULL,
158                            &xEndPoint,
159                            ucDefaultIPAddress_IPv4,
160                            ucDefaultNetMask_IPv4,
161                            ucDefaultGatewayAddress_IPv4,
162                            ucDefaultDNSServerAddress_IPv4,
163                            ucDefaultMACAddress_IPv4 );
164 
165     TEST_ASSERT_EQUAL( NULL, pxNetworkEndPoints );
166 }
167 
168 /**
169  * @brief The purpose of this test is to verify FreeRTOS_FillEndPoint when endpoint is NULL.
170  *
171  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
172  *
173  * Test step:
174  *  - Call FreeRTOS_FillEndPoint to fill IP address, netmask, gateway address,
175  *    DNS server address, and MAC address into endpoint with NULL endpoint.
176  *  - Check if pxNetworkEndPoints is NULL.
177  */
test_FreeRTOS_FillEndPoint_NullEndpoint(void)178 void test_FreeRTOS_FillEndPoint_NullEndpoint( void )
179 {
180     NetworkInterface_t xInterfaces;
181 
182     memset( &xInterfaces, 0, sizeof( NetworkInterface_t ) );
183 
184     FreeRTOS_FillEndPoint( &xInterfaces,
185                            NULL,
186                            ucDefaultIPAddress_IPv4,
187                            ucDefaultNetMask_IPv4,
188                            ucDefaultGatewayAddress_IPv4,
189                            ucDefaultDNSServerAddress_IPv4,
190                            ucDefaultMACAddress_IPv4 );
191 
192     TEST_ASSERT_EQUAL( NULL, pxNetworkEndPoints );
193 }
194 
195 /**
196  * @brief The purpose of this test is to verify FreeRTOS_FillEndPoint when all input parameters
197  * are valid IPv4 default setting.
198  *
199  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
200  *
201  * Test step:
202  *  - Call FreeRTOS_FillEndPoint to fill IP address, netmask, gateway address,
203  *    DNS server address, and MAC address into endpoint.
204  *  - Check if pxNetworkEndPoints is same as input endpoint.
205  *  - Check if all setting are correctly stored in endpoint.
206  *  - Loop steps up here three times.
207  */
test_FreeRTOS_FillEndPoint_MultipleEndpoints(void)208 void test_FreeRTOS_FillEndPoint_MultipleEndpoints( void )
209 {
210     NetworkInterface_t xInterfaces;
211     NetworkEndPoint_t xEndPoint[ 3 ];
212 
213     memset( &xInterfaces, 0, sizeof( NetworkInterface_t ) );
214     memset( &xEndPoint[ 0 ], 0, sizeof( NetworkEndPoint_t ) );
215     memset( &xEndPoint[ 1 ], 0, sizeof( NetworkEndPoint_t ) );
216     memset( &xEndPoint[ 2 ], 0, sizeof( NetworkEndPoint_t ) );
217 
218     FreeRTOS_FillEndPoint( &xInterfaces,
219                            &xEndPoint[ 0 ],
220                            ucDefaultIPAddress_IPv4,
221                            ucDefaultNetMask_IPv4,
222                            ucDefaultGatewayAddress_IPv4,
223                            ucDefaultDNSServerAddress_IPv4,
224                            ucDefaultMACAddress_IPv4 );
225 
226     TEST_ASSERT_EQUAL( &xEndPoint[ 0 ], pxNetworkEndPoints );
227     TEST_ASSERT_EQUAL( &xEndPoint[ 0 ], xInterfaces.pxEndPoint );
228     TEST_ASSERT_EQUAL( IPV4_DEFAULT_ADDRESS, xEndPoint[ 0 ].ipv4_defaults.ulIPAddress );
229     TEST_ASSERT_EQUAL( IPV4_DEFAULT_NETMASK, xEndPoint[ 0 ].ipv4_defaults.ulNetMask );
230     TEST_ASSERT_EQUAL( IPV4_DEFAULT_GATEWAY, xEndPoint[ 0 ].ipv4_defaults.ulGatewayAddress );
231     TEST_ASSERT_EQUAL( IPV4_DEFAULT_DNS_SERVER, xEndPoint[ 0 ].ipv4_defaults.ulDNSServerAddresses[ 0 ] );
232     TEST_ASSERT_EQUAL_MEMORY( xEndPoint[ 0 ].xMACAddress.ucBytes, ucDefaultMACAddress_IPv4, ipMAC_ADDRESS_LENGTH_BYTES );
233     TEST_ASSERT_EQUAL( pdFALSE_UNSIGNED, xEndPoint[ 0 ].bits.bIPv6 );
234     memset( &xEndPoint[ 0 ], 0, sizeof( NetworkEndPoint_t ) );
235 
236     FreeRTOS_FillEndPoint( &xInterfaces,
237                            &xEndPoint[ 1 ],
238                            ucDefaultGatewayAddress_IPv4,
239                            ucDefaultNetMask_IPv4,
240                            ucDefaultGatewayAddress_IPv4,
241                            ucDefaultDNSServerAddress_IPv4,
242                            ucDefaultMACAddress_IPv4 );
243 
244     TEST_ASSERT_EQUAL( &xEndPoint[ 1 ], pxNetworkEndPoints->pxNext );
245     TEST_ASSERT_EQUAL( &xEndPoint[ 1 ], xInterfaces.pxEndPoint->pxNext );
246     TEST_ASSERT_EQUAL( IPV4_DEFAULT_GATEWAY, xEndPoint[ 1 ].ipv4_defaults.ulIPAddress );
247     TEST_ASSERT_EQUAL( IPV4_DEFAULT_NETMASK, xEndPoint[ 1 ].ipv4_defaults.ulNetMask );
248     TEST_ASSERT_EQUAL( IPV4_DEFAULT_GATEWAY, xEndPoint[ 1 ].ipv4_defaults.ulGatewayAddress );
249     TEST_ASSERT_EQUAL( IPV4_DEFAULT_DNS_SERVER, xEndPoint[ 1 ].ipv4_defaults.ulDNSServerAddresses[ 0 ] );
250     TEST_ASSERT_EQUAL_MEMORY( xEndPoint[ 1 ].xMACAddress.ucBytes, ucDefaultMACAddress_IPv4, ipMAC_ADDRESS_LENGTH_BYTES );
251     TEST_ASSERT_EQUAL( pdFALSE_UNSIGNED, xEndPoint[ 1 ].bits.bIPv6 );
252 
253     FreeRTOS_FillEndPoint( &xInterfaces,
254                            &xEndPoint[ 2 ],
255                            ucDefaultGatewayAddress_IPv4,
256                            ucDefaultNetMask_IPv4,
257                            ucDefaultGatewayAddress_IPv4,
258                            ucDefaultDNSServerAddress_IPv4,
259                            ucDefaultMACAddress_IPv4 );
260 
261     TEST_ASSERT_EQUAL( &xEndPoint[ 2 ], pxNetworkEndPoints->pxNext->pxNext );
262     TEST_ASSERT_EQUAL( &xEndPoint[ 2 ], xInterfaces.pxEndPoint->pxNext->pxNext );
263     TEST_ASSERT_EQUAL( IPV4_DEFAULT_GATEWAY, xEndPoint[ 2 ].ipv4_defaults.ulIPAddress );
264     TEST_ASSERT_EQUAL( IPV4_DEFAULT_NETMASK, xEndPoint[ 2 ].ipv4_defaults.ulNetMask );
265     TEST_ASSERT_EQUAL( IPV4_DEFAULT_GATEWAY, xEndPoint[ 2 ].ipv4_defaults.ulGatewayAddress );
266     TEST_ASSERT_EQUAL( IPV4_DEFAULT_DNS_SERVER, xEndPoint[ 2 ].ipv4_defaults.ulDNSServerAddresses[ 0 ] );
267     TEST_ASSERT_EQUAL_MEMORY( xEndPoint[ 2 ].xMACAddress.ucBytes, ucDefaultMACAddress_IPv4, ipMAC_ADDRESS_LENGTH_BYTES );
268     TEST_ASSERT_EQUAL( pdFALSE_UNSIGNED, xEndPoint[ 2 ].bits.bIPv6 );
269 }
270 
271 /**
272  * @brief The purpose of this test is to verify FreeRTOS_FillEndPoint when all input parameters
273  * are valid IPv6 default setting.
274  *
275  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
276  *
277  * Test step:
278  *  - Call FreeRTOS_FillEndPoint to fill IP address, netmask, gateway address,
279  *    DNS server address, and MAC address into endpoint.
280  *  - Check if pxNetworkEndPoints is same as input endpoint.
281  *  - Check if all setting are correctly stored in endpoint.
282  *  - Call FreeRTOS_FillEndPoint to fill with same endpoint.
283  *  - Check if endpoint is not attached.
284  */
test_FreeRTOS_FillEndPoint_SameEndpoint(void)285 void test_FreeRTOS_FillEndPoint_SameEndpoint( void )
286 {
287     NetworkInterface_t xInterfaces;
288     NetworkEndPoint_t xEndPoint;
289 
290     memset( &xInterfaces, 0, sizeof( NetworkInterface_t ) );
291     memset( &xEndPoint, 0, sizeof( xEndPoint ) );
292 
293     FreeRTOS_FillEndPoint( &xInterfaces,
294                            &xEndPoint,
295                            ucDefaultIPAddress_IPv4,
296                            ucDefaultNetMask_IPv4,
297                            ucDefaultGatewayAddress_IPv4,
298                            ucDefaultDNSServerAddress_IPv4,
299                            ucDefaultMACAddress_IPv4 );
300 
301     TEST_ASSERT_EQUAL( &xEndPoint, pxNetworkEndPoints );
302     TEST_ASSERT_EQUAL( &xEndPoint, xInterfaces.pxEndPoint );
303     TEST_ASSERT_EQUAL( IPV4_DEFAULT_ADDRESS, xEndPoint.ipv4_defaults.ulIPAddress );
304     TEST_ASSERT_EQUAL( IPV4_DEFAULT_NETMASK, xEndPoint.ipv4_defaults.ulNetMask );
305     TEST_ASSERT_EQUAL( IPV4_DEFAULT_GATEWAY, xEndPoint.ipv4_defaults.ulGatewayAddress );
306     TEST_ASSERT_EQUAL( IPV4_DEFAULT_DNS_SERVER, xEndPoint.ipv4_defaults.ulDNSServerAddresses[ 0 ] );
307     TEST_ASSERT_EQUAL_MEMORY( xEndPoint.xMACAddress.ucBytes, ucDefaultMACAddress_IPv4, ipMAC_ADDRESS_LENGTH_BYTES );
308     TEST_ASSERT_EQUAL( pdFALSE_UNSIGNED, xEndPoint.bits.bIPv6 );
309 
310     FreeRTOS_FillEndPoint( &xInterfaces,
311                            &xEndPoint,
312                            ucDefaultIPAddress_IPv4,
313                            ucDefaultNetMask_IPv4,
314                            ucDefaultGatewayAddress_IPv4,
315                            ucDefaultDNSServerAddress_IPv4,
316                            ucDefaultMACAddress_IPv4 );
317     TEST_ASSERT_EQUAL( &xEndPoint, pxNetworkEndPoints );
318     TEST_ASSERT_EQUAL( NULL, pxNetworkEndPoints->pxNext );
319 }
320 
321 /**
322  * @brief The purpose of this test is to verify FreeRTOS_FillEndPoint_IPv6 when all input parameters
323  * are valid IPv6 default setting.
324  *
325  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
326  *
327  * Test step:
328  *  - Call FreeRTOS_FillEndPoint_IPv6 to fill IP address, netmask, gateway address,
329  *    DNS server address, and MAC address into endpoint.
330  *  - Check if pxNetworkEndPoints is same as input endpoint.
331  *  - Check if all setting are correctly stored in endpoint.
332  */
test_FreeRTOS_FillEndPoint_IPv6_HappyPath(void)333 void test_FreeRTOS_FillEndPoint_IPv6_HappyPath( void )
334 {
335     NetworkInterface_t xInterfaces;
336     NetworkEndPoint_t xEndPoint;
337 
338     memset( &xInterfaces, 0, sizeof( NetworkInterface_t ) );
339     memset( &xEndPoint, 0, sizeof( xEndPoint ) );
340 
341     FreeRTOS_FillEndPoint_IPv6( &xInterfaces,
342                                 &xEndPoint,
343                                 &xDefaultIPAddress_IPv6,
344                                 &xDefaultNetPrefix_IPv6,
345                                 xDefaultPrefixLength,
346                                 &xDefaultGatewayAddress_IPv6,
347                                 &xDefaultDNSServerAddress_IPv6,
348                                 ucDefaultMACAddress_IPv6 );
349 
350     TEST_ASSERT_EQUAL( &xEndPoint, pxNetworkEndPoints );
351     TEST_ASSERT_EQUAL( &xEndPoint, xInterfaces.pxEndPoint );
352     TEST_ASSERT_EQUAL_MEMORY( &xDefaultIPAddress_IPv6, xEndPoint.ipv6_defaults.xIPAddress.ucBytes, ipSIZE_OF_IPv6_ADDRESS );
353     TEST_ASSERT_EQUAL_MEMORY( &xDefaultNetPrefix_IPv6, xEndPoint.ipv6_defaults.xPrefix.ucBytes, ipSIZE_OF_IPv6_ADDRESS );
354     TEST_ASSERT_EQUAL( xDefaultPrefixLength, xEndPoint.ipv6_defaults.uxPrefixLength );
355     TEST_ASSERT_EQUAL_MEMORY( &xDefaultGatewayAddress_IPv6, xEndPoint.ipv6_defaults.xGatewayAddress.ucBytes, ipSIZE_OF_IPv6_ADDRESS );
356     TEST_ASSERT_EQUAL_MEMORY( &xDefaultDNSServerAddress_IPv6, xEndPoint.ipv6_defaults.xDNSServerAddresses[ 0 ].ucBytes, ipSIZE_OF_IPv6_ADDRESS );
357     TEST_ASSERT_EQUAL_MEMORY( xEndPoint.xMACAddress.ucBytes, ucDefaultMACAddress_IPv6, ipMAC_ADDRESS_LENGTH_BYTES );
358     TEST_ASSERT_EQUAL( pdTRUE_UNSIGNED, xEndPoint.bits.bIPv6 );
359 }
360 
361 /**
362  * @brief The purpose of this test is to verify FreeRTOS_FillEndPoint when network interface is NULL.
363  *
364  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
365  *
366  * Test step:
367  *  - Call FreeRTOS_FillEndPoint_IPv6 to fill IP address, netmask, gateway address,
368  *    DNS server address, and MAC address into endpoint with NULL network interface.
369  *  - Check if pxNetworkEndPoints is NULL.
370  */
test_FreeRTOS_FillEndPoint_IPv6_NullInterface(void)371 void test_FreeRTOS_FillEndPoint_IPv6_NullInterface( void )
372 {
373     NetworkEndPoint_t xEndPoint;
374 
375     memset( &xEndPoint, 0, sizeof( xEndPoint ) );
376 
377     FreeRTOS_FillEndPoint_IPv6( NULL,
378                                 &xEndPoint,
379                                 &xDefaultIPAddress_IPv6,
380                                 &xDefaultNetPrefix_IPv6,
381                                 xDefaultPrefixLength,
382                                 &xDefaultGatewayAddress_IPv6,
383                                 &xDefaultDNSServerAddress_IPv6,
384                                 ucDefaultMACAddress_IPv6 );
385 
386     TEST_ASSERT_EQUAL( NULL, pxNetworkEndPoints );
387 }
388 
389 /**
390  * @brief The purpose of this test is to verify FreeRTOS_FillEndPoint_IPv6 when endpoint is NULL.
391  *
392  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
393  *
394  * Test step:
395  *  - Call FreeRTOS_FillEndPoint_IPv6 to fill IP address, netmask, gateway address,
396  *    DNS server address, and MAC address into endpoint with NULL endpoint.
397  *  - Check if pxNetworkEndPoints is NULL.
398  */
test_FreeRTOS_FillEndPoint_IPv6_NullEndpoint(void)399 void test_FreeRTOS_FillEndPoint_IPv6_NullEndpoint( void )
400 {
401     NetworkInterface_t xInterfaces;
402 
403     memset( &xInterfaces, 0, sizeof( NetworkInterface_t ) );
404 
405     FreeRTOS_FillEndPoint_IPv6( &xInterfaces,
406                                 NULL,
407                                 &xDefaultIPAddress_IPv6,
408                                 &xDefaultNetPrefix_IPv6,
409                                 xDefaultPrefixLength,
410                                 &xDefaultGatewayAddress_IPv6,
411                                 &xDefaultDNSServerAddress_IPv6,
412                                 ucDefaultMACAddress_IPv6 );
413 
414     TEST_ASSERT_EQUAL( NULL, pxNetworkEndPoints );
415 }
416 
417 /**
418  * @brief The purpose of this test is to verify FreeRTOS_FillEndPoint_IPv6 when input IP address is NULL.
419  *
420  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
421  *
422  * Test step:
423  *  - Call FreeRTOS_FillEndPoint_IPv6 with NULL IP address pointer.
424  */
test_FreeRTOS_FillEndPoint_IPv6_NullIP(void)425 void test_FreeRTOS_FillEndPoint_IPv6_NullIP( void )
426 {
427     NetworkInterface_t xInterfaces;
428     NetworkEndPoint_t xEndPoint;
429 
430     memset( &xInterfaces, 0, sizeof( NetworkInterface_t ) );
431     memset( &xEndPoint, 0, sizeof( NetworkEndPoint_t ) );
432 
433     FreeRTOS_FillEndPoint_IPv6( &xInterfaces,
434                                 &xEndPoint,
435                                 NULL,
436                                 &xDefaultNetPrefix_IPv6,
437                                 xDefaultPrefixLength,
438                                 &xDefaultGatewayAddress_IPv6,
439                                 &xDefaultDNSServerAddress_IPv6,
440                                 ucDefaultMACAddress_IPv6 );
441 }
442 
443 /**
444  * @brief The purpose of this test is to verify FreeRTOS_FillEndPoint_IPv6 when input MAC address is NULL.
445  *
446  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
447  *
448  * Test step:
449  *  - Call FreeRTOS_FillEndPoint_IPv6 with NULL MAC address pointer.
450  */
test_FreeRTOS_FillEndPoint_IPv6_NullMAC(void)451 void test_FreeRTOS_FillEndPoint_IPv6_NullMAC( void )
452 {
453     NetworkInterface_t xInterfaces;
454     NetworkEndPoint_t xEndPoint;
455 
456     memset( &xInterfaces, 0, sizeof( NetworkInterface_t ) );
457     memset( &xEndPoint, 0, sizeof( NetworkEndPoint_t ) );
458 
459     FreeRTOS_FillEndPoint_IPv6( &xInterfaces,
460                                 &xEndPoint,
461                                 &xDefaultIPAddress_IPv6,
462                                 &xDefaultNetPrefix_IPv6,
463                                 xDefaultPrefixLength,
464                                 &xDefaultGatewayAddress_IPv6,
465                                 &xDefaultDNSServerAddress_IPv6,
466                                 NULL );
467 }
468 
469 /**
470  * @brief The purpose of this test is to verify FreeRTOS_FillEndPoint_IPv6 when some input parameters
471  * are NULL (gateway, DNS and prefix).
472  *
473  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
474  *
475  * Test step:
476  *  - Call FreeRTOS_FillEndPoint_IPv6 to fill IP address, netmask, gateway address,
477  *    DNS server address, and MAC address into endpoint.
478  *  - Check if pxNetworkEndPoints is same as input endpoint.
479  *  - Check if all setting are correctly stored in endpoint.
480  */
test_FreeRTOS_FillEndPoint_IPv6_NullGatewayDNSPrefix(void)481 void test_FreeRTOS_FillEndPoint_IPv6_NullGatewayDNSPrefix( void )
482 {
483     NetworkInterface_t xInterfaces;
484     NetworkEndPoint_t xEndPoint;
485 
486     memset( &xInterfaces, 0, sizeof( NetworkInterface_t ) );
487     memset( &xEndPoint, 0, sizeof( xEndPoint ) );
488 
489     FreeRTOS_FillEndPoint_IPv6( &xInterfaces,
490                                 &xEndPoint,
491                                 &xDefaultIPAddress_IPv6,
492                                 NULL,
493                                 0,
494                                 NULL,
495                                 NULL,
496                                 ucDefaultMACAddress_IPv6 );
497 
498     TEST_ASSERT_EQUAL( &xEndPoint, pxNetworkEndPoints );
499     TEST_ASSERT_EQUAL( &xEndPoint, xInterfaces.pxEndPoint );
500     TEST_ASSERT_EQUAL_MEMORY( &xDefaultIPAddress_IPv6, xEndPoint.ipv6_defaults.xIPAddress.ucBytes, ipSIZE_OF_IPv6_ADDRESS );
501     TEST_ASSERT_EQUAL( 0, xEndPoint.ipv6_defaults.uxPrefixLength );
502     TEST_ASSERT_EQUAL_MEMORY( xEndPoint.xMACAddress.ucBytes, ucDefaultMACAddress_IPv6, ipMAC_ADDRESS_LENGTH_BYTES );
503     TEST_ASSERT_EQUAL( pdTRUE_UNSIGNED, xEndPoint.bits.bIPv6 );
504 }
505 
506 /**
507  * @brief The purpose of this test is to verify FreeRTOS_FillEndPoint_IPv6 when all input parameters
508  * are valid IPv6 default setting.
509  *
510  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
511  *
512  * Test step:
513  *  - Call FreeRTOS_FillEndPoint_IPv6 to fill IP address, netmask, gateway address,
514  *    DNS server address, and MAC address into endpoint.
515  *  - Check if pxNetworkEndPoints is same as input endpoint.
516  *  - Check if all setting are correctly stored in endpoint.
517  *  - Loop steps up here three times.
518  */
test_FreeRTOS_FillEndPoint_IPv6_MultipleEndpoints(void)519 void test_FreeRTOS_FillEndPoint_IPv6_MultipleEndpoints( void )
520 {
521     NetworkInterface_t xInterfaces;
522     NetworkEndPoint_t xEndPoint[ 3 ];
523 
524     memset( &xInterfaces, 0, sizeof( NetworkInterface_t ) );
525     memset( &xEndPoint[ 0 ], 0, sizeof( NetworkEndPoint_t ) );
526     memset( &xEndPoint[ 1 ], 0, sizeof( NetworkEndPoint_t ) );
527     memset( &xEndPoint[ 2 ], 0, sizeof( NetworkEndPoint_t ) );
528 
529     FreeRTOS_FillEndPoint_IPv6( &xInterfaces,
530                                 &xEndPoint[ 0 ],
531                                 &xDefaultIPAddress_IPv6,
532                                 &xDefaultNetPrefix_IPv6,
533                                 xDefaultPrefixLength,
534                                 &xDefaultGatewayAddress_IPv6,
535                                 &xDefaultDNSServerAddress_IPv6,
536                                 ucDefaultMACAddress_IPv6 );
537 
538     TEST_ASSERT_EQUAL( &xEndPoint[ 0 ], pxNetworkEndPoints );
539     TEST_ASSERT_EQUAL( &xEndPoint[ 0 ], xInterfaces.pxEndPoint );
540     TEST_ASSERT_EQUAL_MEMORY( &xDefaultIPAddress_IPv6, xEndPoint[ 0 ].ipv6_defaults.xIPAddress.ucBytes, ipSIZE_OF_IPv6_ADDRESS );
541     TEST_ASSERT_EQUAL_MEMORY( &xDefaultNetPrefix_IPv6, xEndPoint[ 0 ].ipv6_defaults.xPrefix.ucBytes, ipSIZE_OF_IPv6_ADDRESS );
542     TEST_ASSERT_EQUAL( xDefaultPrefixLength, xEndPoint[ 0 ].ipv6_defaults.uxPrefixLength );
543     TEST_ASSERT_EQUAL_MEMORY( &xDefaultGatewayAddress_IPv6, xEndPoint[ 0 ].ipv6_defaults.xGatewayAddress.ucBytes, ipSIZE_OF_IPv6_ADDRESS );
544     TEST_ASSERT_EQUAL_MEMORY( &xDefaultDNSServerAddress_IPv6, xEndPoint[ 0 ].ipv6_defaults.xDNSServerAddresses[ 0 ].ucBytes, ipSIZE_OF_IPv6_ADDRESS );
545     TEST_ASSERT_EQUAL_MEMORY( xEndPoint[ 0 ].xMACAddress.ucBytes, ucDefaultMACAddress_IPv6, ipMAC_ADDRESS_LENGTH_BYTES );
546     TEST_ASSERT_EQUAL( pdTRUE_UNSIGNED, xEndPoint[ 0 ].bits.bIPv6 );
547 
548     FreeRTOS_FillEndPoint_IPv6( &xInterfaces,
549                                 &xEndPoint[ 1 ],
550                                 &xDefaultGatewayAddress_IPv6,
551                                 &xDefaultNetPrefix_IPv6,
552                                 xDefaultPrefixLength,
553                                 &xDefaultGatewayAddress_IPv6,
554                                 &xDefaultDNSServerAddress_IPv6,
555                                 ucDefaultMACAddress_IPv6 );
556 
557     TEST_ASSERT_EQUAL( &xEndPoint[ 1 ], pxNetworkEndPoints->pxNext );
558     TEST_ASSERT_EQUAL( &xEndPoint[ 1 ], xInterfaces.pxEndPoint->pxNext );
559     TEST_ASSERT_EQUAL_MEMORY( &xDefaultGatewayAddress_IPv6, xEndPoint[ 1 ].ipv6_defaults.xIPAddress.ucBytes, ipSIZE_OF_IPv6_ADDRESS );
560     TEST_ASSERT_EQUAL_MEMORY( &xDefaultNetPrefix_IPv6, xEndPoint[ 1 ].ipv6_defaults.xPrefix.ucBytes, ipSIZE_OF_IPv6_ADDRESS );
561     TEST_ASSERT_EQUAL( xDefaultPrefixLength, xEndPoint[ 1 ].ipv6_defaults.uxPrefixLength );
562     TEST_ASSERT_EQUAL_MEMORY( &xDefaultGatewayAddress_IPv6, xEndPoint[ 1 ].ipv6_defaults.xGatewayAddress.ucBytes, ipSIZE_OF_IPv6_ADDRESS );
563     TEST_ASSERT_EQUAL_MEMORY( &xDefaultDNSServerAddress_IPv6, xEndPoint[ 1 ].ipv6_defaults.xDNSServerAddresses[ 0 ].ucBytes, ipSIZE_OF_IPv6_ADDRESS );
564     TEST_ASSERT_EQUAL_MEMORY( xEndPoint[ 1 ].xMACAddress.ucBytes, ucDefaultMACAddress_IPv6, ipMAC_ADDRESS_LENGTH_BYTES );
565     TEST_ASSERT_EQUAL( pdTRUE_UNSIGNED, xEndPoint[ 1 ].bits.bIPv6 );
566 
567     FreeRTOS_FillEndPoint_IPv6( &xInterfaces,
568                                 &xEndPoint[ 2 ],
569                                 &xDefaultDNSServerAddress_IPv6,
570                                 &xDefaultNetPrefix_IPv6,
571                                 xDefaultPrefixLength,
572                                 &xDefaultGatewayAddress_IPv6,
573                                 &xDefaultDNSServerAddress_IPv6,
574                                 ucDefaultMACAddress_IPv6 );
575 
576     TEST_ASSERT_EQUAL( &xEndPoint[ 2 ], pxNetworkEndPoints->pxNext->pxNext );
577     TEST_ASSERT_EQUAL( &xEndPoint[ 2 ], xInterfaces.pxEndPoint->pxNext->pxNext );
578     TEST_ASSERT_EQUAL_MEMORY( &xDefaultDNSServerAddress_IPv6, xEndPoint[ 2 ].ipv6_defaults.xIPAddress.ucBytes, ipSIZE_OF_IPv6_ADDRESS );
579     TEST_ASSERT_EQUAL_MEMORY( &xDefaultNetPrefix_IPv6, xEndPoint[ 2 ].ipv6_defaults.xPrefix.ucBytes, ipSIZE_OF_IPv6_ADDRESS );
580     TEST_ASSERT_EQUAL( xDefaultPrefixLength, xEndPoint[ 2 ].ipv6_defaults.uxPrefixLength );
581     TEST_ASSERT_EQUAL_MEMORY( &xDefaultGatewayAddress_IPv6, xEndPoint[ 2 ].ipv6_defaults.xGatewayAddress.ucBytes, ipSIZE_OF_IPv6_ADDRESS );
582     TEST_ASSERT_EQUAL_MEMORY( &xDefaultDNSServerAddress_IPv6, xEndPoint[ 2 ].ipv6_defaults.xDNSServerAddresses[ 0 ].ucBytes, ipSIZE_OF_IPv6_ADDRESS );
583     TEST_ASSERT_EQUAL_MEMORY( xEndPoint[ 2 ].xMACAddress.ucBytes, ucDefaultMACAddress_IPv6, ipMAC_ADDRESS_LENGTH_BYTES );
584     TEST_ASSERT_EQUAL( pdTRUE_UNSIGNED, xEndPoint[ 2 ].bits.bIPv6 );
585 }
586 
587 /**
588  * @brief The purpose of this test is to verify FreeRTOS_FillEndPoint_IPv6 when all input parameters
589  * are valid IPv6 default setting.
590  *
591  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
592  *
593  * Test step:
594  *  - Call FreeRTOS_FillEndPoint_IPv6 to fill IP address, netmask, gateway address,
595  *    DNS server address, and MAC address into endpoint.
596  *  - Check if pxNetworkEndPoints is same as input endpoint.
597  *  - Check if all setting are correctly stored in endpoint.
598  *  - Call FreeRTOS_FillEndPoint_IPv6 to fill with same endpoint.
599  *  - Check if endpoint is not attached.
600  */
test_FreeRTOS_FillEndPoint_IPv6_SameEndpoint(void)601 void test_FreeRTOS_FillEndPoint_IPv6_SameEndpoint( void )
602 {
603     NetworkInterface_t xInterfaces;
604     NetworkEndPoint_t xEndPoint;
605 
606     memset( &xInterfaces, 0, sizeof( NetworkInterface_t ) );
607     memset( &xEndPoint, 0, sizeof( NetworkEndPoint_t ) );
608 
609     FreeRTOS_FillEndPoint_IPv6( &xInterfaces,
610                                 &xEndPoint,
611                                 &xDefaultIPAddress_IPv6,
612                                 &xDefaultNetPrefix_IPv6,
613                                 xDefaultPrefixLength,
614                                 &xDefaultGatewayAddress_IPv6,
615                                 &xDefaultDNSServerAddress_IPv6,
616                                 ucDefaultMACAddress_IPv6 );
617 
618     TEST_ASSERT_EQUAL( &xEndPoint, pxNetworkEndPoints );
619     TEST_ASSERT_EQUAL( &xEndPoint, xInterfaces.pxEndPoint );
620     TEST_ASSERT_EQUAL_MEMORY( &xDefaultIPAddress_IPv6, xEndPoint.ipv6_defaults.xIPAddress.ucBytes, ipSIZE_OF_IPv6_ADDRESS );
621     TEST_ASSERT_EQUAL_MEMORY( &xDefaultNetPrefix_IPv6, xEndPoint.ipv6_defaults.xPrefix.ucBytes, ipSIZE_OF_IPv6_ADDRESS );
622     TEST_ASSERT_EQUAL( xDefaultPrefixLength, xEndPoint.ipv6_defaults.uxPrefixLength );
623     TEST_ASSERT_EQUAL_MEMORY( &xDefaultGatewayAddress_IPv6, xEndPoint.ipv6_defaults.xGatewayAddress.ucBytes, ipSIZE_OF_IPv6_ADDRESS );
624     TEST_ASSERT_EQUAL_MEMORY( &xDefaultDNSServerAddress_IPv6, xEndPoint.ipv6_defaults.xDNSServerAddresses[ 0 ].ucBytes, ipSIZE_OF_IPv6_ADDRESS );
625     TEST_ASSERT_EQUAL_MEMORY( xEndPoint.xMACAddress.ucBytes, ucDefaultMACAddress_IPv6, ipMAC_ADDRESS_LENGTH_BYTES );
626     TEST_ASSERT_EQUAL( pdTRUE_UNSIGNED, xEndPoint.bits.bIPv6 );
627 
628     FreeRTOS_FillEndPoint_IPv6( &xInterfaces,
629                                 &xEndPoint,
630                                 &xDefaultGatewayAddress_IPv6,
631                                 &xDefaultNetPrefix_IPv6,
632                                 xDefaultPrefixLength,
633                                 &xDefaultGatewayAddress_IPv6,
634                                 &xDefaultDNSServerAddress_IPv6,
635                                 ucDefaultMACAddress_IPv6 );
636     TEST_ASSERT_EQUAL( &xEndPoint, pxNetworkEndPoints );
637     TEST_ASSERT_EQUAL( NULL, pxNetworkEndPoints->pxNext );
638 }
639 
640 /**
641  * @brief The purpose of this test is to verify FreeRTOS_AddNetworkInterface when input parameter
642  * is not NULL.
643  *
644  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
645  *
646  * Test step:
647  *  - Call FreeRTOS_AddNetworkInterface with one valid network interface.
648  *  - Check if the input network interface is stored into pxNetworkInterfaces.
649  */
test_FreeRTOS_AddNetworkInterface_HappyPath(void)650 void test_FreeRTOS_AddNetworkInterface_HappyPath( void )
651 {
652     NetworkInterface_t xNetworkInterface;
653 
654     memset( &xNetworkInterface, 0, sizeof( NetworkInterface_t ) );
655 
656     ( void ) FreeRTOS_AddNetworkInterface( &xNetworkInterface );
657 
658     TEST_ASSERT_EQUAL( &xNetworkInterface, pxNetworkInterfaces );
659     TEST_ASSERT_EQUAL( NULL, pxNetworkInterfaces->pxNext );
660 }
661 
662 /**
663  * @brief The purpose of this test is to verify FreeRTOS_AddNetworkInterface three times with
664  * different valid input parameters.
665  *
666  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
667  *
668  * Test step:
669  *  - Call FreeRTOS_AddNetworkInterface three times with three different network interfaces.
670  *  - Check if all input network interfaces are stored into pxNetworkInterfaces.
671  */
test_FreeRTOS_AddNetworkInterface_ThreeInARow(void)672 void test_FreeRTOS_AddNetworkInterface_ThreeInARow( void )
673 {
674     NetworkInterface_t xNetworkInterface[ 3 ];
675     NetworkInterface_t * pxNetworkInterface = NULL;
676     int i = 0;
677 
678     for( i = 0; i < 3; i++ )
679     {
680         memset( &( xNetworkInterface[ i ] ), 0, sizeof( NetworkInterface_t ) );
681 
682         ( void ) FreeRTOS_AddNetworkInterface( &( xNetworkInterface[ i ] ) );
683     }
684 
685     pxNetworkInterface = pxNetworkInterfaces;
686 
687     for( i = 0; i < 3; i++ )
688     {
689         TEST_ASSERT_EQUAL( &( xNetworkInterface[ i ] ), pxNetworkInterface );
690         pxNetworkInterface = pxNetworkInterface->pxNext;
691     }
692 
693     TEST_ASSERT_EQUAL( NULL, pxNetworkInterface );
694 }
695 
696 /**
697  * @brief The purpose of this test is to verify FreeRTOS_AddNetworkInterface when input parameter
698  * is NULL.
699  *
700  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
701  *
702  * Test step:
703  *  - Call FreeRTOS_AddNetworkInterface with input NULL.
704  *  - Check if pxNetworkInterfaces is still NULL.
705  */
test_FreeRTOS_AddNetworkInterface_Null(void)706 void test_FreeRTOS_AddNetworkInterface_Null( void )
707 {
708     ( void ) FreeRTOS_AddNetworkInterface( NULL );
709     TEST_ASSERT_EQUAL( NULL, pxNetworkInterfaces );
710 }
711 
712 /**
713  * @brief FreeRTOS_AddNetworkInterface should only add same interface once into
714  * the pxNetworkInterfaces.
715  *
716  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
717  *
718  * Test step:
719  *  - Call FreeRTOS_AddNetworkInterface with same input twice.
720  *  - Check if pxNetworkInterfaces is same as input.
721  *  - Check if pxNetworkInterfaces->pxNext is NULL.
722  */
test_FreeRTOS_AddNetworkInterface_DuplicateInterface(void)723 void test_FreeRTOS_AddNetworkInterface_DuplicateInterface( void )
724 {
725     NetworkInterface_t xNetworkInterface;
726 
727     memset( &xNetworkInterface, 0, sizeof( NetworkInterface_t ) );
728 
729     ( void ) FreeRTOS_AddNetworkInterface( &xNetworkInterface );
730     ( void ) FreeRTOS_AddNetworkInterface( &xNetworkInterface );
731 
732     TEST_ASSERT_EQUAL( &xNetworkInterface, pxNetworkInterfaces );
733     TEST_ASSERT_EQUAL( NULL, pxNetworkInterfaces->pxNext );
734 }
735 
736 /**
737  * @brief FreeRTOS_FirstNetworkInterface should be able to find the first network interface.
738  *
739  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
740  *
741  * Test step:
742  *  - Assign a network interface into pxNetworkInterfaces.
743  *  - Call FreeRTOS_FirstNetworkInterface to get first network interface.
744  *  - Check if the return is same as the input.
745  */
test_FreeRTOS_FirstNetworkInterface_HappyPath(void)746 void test_FreeRTOS_FirstNetworkInterface_HappyPath( void )
747 {
748     NetworkInterface_t xNetworkInterface;
749     NetworkInterface_t * pxNetworkInterface = NULL;
750 
751     memset( &xNetworkInterface, 0, sizeof( NetworkInterface_t ) );
752     pxNetworkInterfaces = &xNetworkInterface;
753 
754     pxNetworkInterface = FreeRTOS_FirstNetworkInterface();
755 
756     TEST_ASSERT_EQUAL( &xNetworkInterface, pxNetworkInterface );
757 }
758 
759 /**
760  * @brief FreeRTOS_FirstNetworkInterface should be able to return NULL if there is no network interface available.
761  *
762  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
763  *
764  * Test step:
765  *  - Call FreeRTOS_FirstNetworkInterface to get first network interface.
766  *  - Check if the return is NULL.
767  */
test_FreeRTOS_FirstNetworkInterface_Null(void)768 void test_FreeRTOS_FirstNetworkInterface_Null( void )
769 {
770     NetworkInterface_t * pxNetworkInterface = NULL;
771 
772     pxNetworkInterface = FreeRTOS_FirstNetworkInterface();
773 
774     TEST_ASSERT_EQUAL( NULL, pxNetworkInterface );
775 }
776 
777 /**
778  * @brief FreeRTOS_NextNetworkInterface returns next network interface correctly.
779  *
780  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
781  *
782  * Test step:
783  *  - Create 3 network interfaces and attach them into pxNetworkInterfaces.
784  *  - Check if pxNetworkInterfaces is same as first input.
785  *  - Check if we can query next two network interfaces correctly by calling FreeRTOS_NextNetworkInterface.
786  */
test_FreeRTOS_NextNetworkInterface_HappyPath(void)787 void test_FreeRTOS_NextNetworkInterface_HappyPath( void )
788 {
789     NetworkInterface_t xNetworkInterface[ 3 ];
790     NetworkInterface_t * pxNetworkInterface = NULL;
791     int i = 0;
792 
793     for( i = 0; i < 3; i++ )
794     {
795         memset( &( xNetworkInterface[ i ] ), 0, sizeof( NetworkInterface_t ) );
796 
797         if( pxNetworkInterfaces == NULL )
798         {
799             pxNetworkInterfaces = &( xNetworkInterface[ i ] );
800             pxNetworkInterface = pxNetworkInterfaces;
801         }
802         else
803         {
804             pxNetworkInterface->pxNext = &( xNetworkInterface[ i ] );
805             pxNetworkInterface = pxNetworkInterface->pxNext;
806         }
807     }
808 
809     pxNetworkInterface = pxNetworkInterfaces;
810 
811     for( i = 0; i < 3; i++ )
812     {
813         TEST_ASSERT_EQUAL( &( xNetworkInterface[ i ] ), pxNetworkInterface );
814         pxNetworkInterface = FreeRTOS_NextNetworkInterface( pxNetworkInterface );
815     }
816 
817     TEST_ASSERT_EQUAL( NULL, pxNetworkInterface );
818 }
819 
820 /**
821  * @brief FreeRTOS_NextNetworkInterface returns NULL if the input is NULL.
822  *
823  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
824  *
825  * Test step:
826  *  - Call FreeRTOS_NextNetworkInterface with NULL input.
827  *  - Check if return is NULL.
828  */
test_FreeRTOS_NextNetworkInterface_Null(void)829 void test_FreeRTOS_NextNetworkInterface_Null( void )
830 {
831     NetworkInterface_t * pxNetworkInterface = NULL;
832 
833     pxNetworkInterface = FreeRTOS_NextNetworkInterface( NULL );
834 
835     TEST_ASSERT_EQUAL( NULL, pxNetworkInterface );
836 }
837 
838 /**
839  * @brief FreeRTOS_FirstEndPoint should return endpoint attached on the input network interface.
840  *
841  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
842  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
843  *
844  * Test step:
845  *  - Set a network interface into pxNetworkInterfaces.
846  *  - Attach an endpoint to the network interface.
847  *  - Call FreeRTOS_FirstEndPoint to get attached endpoint.
848  *  - Check if returned endpoint is same as attached one.
849  */
test_FreeRTOS_FirstEndPoint_HappyPath(void)850 void test_FreeRTOS_FirstEndPoint_HappyPath( void )
851 {
852     NetworkInterface_t xNetworkInterface;
853     NetworkInterface_t * pxNetworkInterface = NULL;
854     NetworkEndPoint_t xEndPoint;
855     NetworkEndPoint_t * pxEndPoint = NULL;
856 
857     memset( &xNetworkInterface, 0, sizeof( NetworkInterface_t ) );
858     pxNetworkInterfaces = &xNetworkInterface;
859     memset( &xEndPoint, 0, sizeof( NetworkEndPoint_t ) );
860 
861     xEndPoint.pxNetworkInterface = pxNetworkInterfaces;
862     pxNetworkEndPoints = &xEndPoint;
863 
864     pxEndPoint = FreeRTOS_FirstEndPoint( pxNetworkInterfaces );
865 
866     TEST_ASSERT_EQUAL( &xEndPoint, pxEndPoint );
867 }
868 
869 /**
870  * @brief FreeRTOS_FirstEndPoint should return first endpoint in the list if input is NULL.
871  *
872  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
873  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
874  *
875  * Test step:
876  *  - Set a network interface into pxNetworkInterfaces.
877  *  - Attach an endpoint to the network interface.
878  *  - Call FreeRTOS_FirstEndPoint to get attached endpoint with NULL input.
879  *  - Check if returned endpoint is same as attached one.
880  */
test_FreeRTOS_FirstEndPoint_Null(void)881 void test_FreeRTOS_FirstEndPoint_Null( void )
882 {
883     NetworkInterface_t xNetworkInterface;
884     NetworkInterface_t * pxNetworkInterface = NULL;
885     NetworkEndPoint_t xEndPoint;
886     NetworkEndPoint_t * pxEndPoint = NULL;
887 
888     memset( &xNetworkInterface, 0, sizeof( NetworkInterface_t ) );
889     pxNetworkInterfaces = &xNetworkInterface;
890     memset( &xEndPoint, 0, sizeof( NetworkEndPoint_t ) );
891 
892     xEndPoint.pxNetworkInterface = pxNetworkInterfaces;
893     pxNetworkEndPoints = &xEndPoint;
894 
895     pxEndPoint = FreeRTOS_FirstEndPoint( NULL );
896 
897     TEST_ASSERT_EQUAL( &xEndPoint, pxEndPoint );
898 }
899 
900 /**
901  * @brief FreeRTOS_FirstEndPoint should return NULL if no endpoint available in the list.
902  *
903  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
904  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
905  *
906  * Test step:
907  *  - Set a network interface into pxNetworkInterfaces.
908  *  - Attach an endpoint to the network interface.
909  *  - Call FreeRTOS_FirstEndPoint to get attached endpoint with NULL input.
910  *  - Check if returned endpoint is same as attached one.
911  */
test_FreeRTOS_FirstEndPoint_NoEndpoints(void)912 void test_FreeRTOS_FirstEndPoint_NoEndpoints( void )
913 {
914     NetworkInterface_t xNetworkInterface;
915     NetworkInterface_t * pxNetworkInterface = NULL;
916     NetworkEndPoint_t xEndPoint;
917     NetworkEndPoint_t * pxEndPoint = NULL;
918 
919     memset( &xNetworkInterface, 0, sizeof( NetworkInterface_t ) );
920     pxNetworkInterfaces = &xNetworkInterface;
921     memset( &xEndPoint, 0, sizeof( NetworkEndPoint_t ) );
922 
923     xEndPoint.pxNetworkInterface = pxNetworkInterfaces;
924 
925     pxEndPoint = FreeRTOS_FirstEndPoint( &xNetworkInterface );
926 
927     TEST_ASSERT_EQUAL( NULL, pxEndPoint );
928 }
929 
930 /**
931  * @brief FreeRTOS_FirstEndPoint should return first endpoint with specified interface.
932  *
933  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
934  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
935  *
936  * Test step:
937  *  - Create 3 network interfaces and 3 endpoints.
938  *  - Attach one endpoint each network interface.
939  *  - Put interfaces & endpoints into the list.
940  *  - Loop to call FreeRTOS_FirstEndPoint to get attached endpoint with each network interface.
941  *  - Check if returned endpoint is same as attached one.
942  */
test_FreeRTOS_FirstEndPoint_AnotherInterface(void)943 void test_FreeRTOS_FirstEndPoint_AnotherInterface( void )
944 {
945     /* Attach one endpoint to one network interface. Check if we can get correct endpoint by API. */
946     NetworkInterface_t xNetworkInterface[ 3 ];
947     NetworkInterface_t * pxNetworkInterface = NULL;
948     NetworkEndPoint_t xEndPoint[ 3 ];
949     NetworkEndPoint_t * pxEndPoint = NULL;
950     int i = 0;
951 
952     for( i = 0; i < 3; i++ )
953     {
954         memset( &( xNetworkInterface[ i ] ), 0, sizeof( NetworkInterface_t ) );
955         memset( &( xEndPoint[ i ] ), 0, sizeof( NetworkEndPoint_t ) );
956 
957         if( pxNetworkInterfaces == NULL )
958         {
959             pxNetworkInterfaces = &( xNetworkInterface[ i ] );
960             pxNetworkInterface = pxNetworkInterfaces;
961 
962             pxNetworkEndPoints = &( xEndPoint[ i ] );
963             pxEndPoint = pxNetworkEndPoints;
964         }
965         else
966         {
967             pxNetworkInterface->pxNext = &( xNetworkInterface[ i ] );
968             pxNetworkInterface = pxNetworkInterface->pxNext;
969 
970             pxEndPoint->pxNext = &( xEndPoint[ i ] );
971             pxEndPoint = pxEndPoint->pxNext;
972         }
973 
974         xEndPoint[ i ].pxNetworkInterface = pxNetworkInterface;
975     }
976 
977     for( i = 0; i < 3; i++ )
978     {
979         pxEndPoint = FreeRTOS_FirstEndPoint( &( xNetworkInterface[ i ] ) );
980         TEST_ASSERT_EQUAL( &( xEndPoint[ i ] ), pxEndPoint );
981     }
982 }
983 
984 /**
985  * @brief FreeRTOS_FirstEndPoint_IPv6 should return an IPv6 endpoint attached on the input network interface.
986  *
987  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
988  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
989  *
990  * Test step:
991  *  - Set a network interface into pxNetworkInterfaces.
992  *  - Attach an endpoint to the network interface.
993  *     - Set IPv6 bit in endpoint.
994  *  - Call FreeRTOS_FirstEndPoint to get attached endpoint.
995  *  - Check if returned endpoint is same as attached one.
996  */
test_FreeRTOS_FirstEndPoint_IPv6_HappyPath(void)997 void test_FreeRTOS_FirstEndPoint_IPv6_HappyPath( void )
998 {
999     NetworkInterface_t xNetworkInterface;
1000     NetworkInterface_t * pxNetworkInterface = NULL;
1001     NetworkEndPoint_t xEndPoint;
1002     NetworkEndPoint_t * pxEndPoint = NULL;
1003 
1004     memset( &xNetworkInterface, 0, sizeof( NetworkInterface_t ) );
1005     pxNetworkInterfaces = &xNetworkInterface;
1006 
1007     memset( &xEndPoint, 0, sizeof( NetworkEndPoint_t ) );
1008     xEndPoint.bits.bIPv6 = pdTRUE;
1009     xEndPoint.pxNetworkInterface = pxNetworkInterfaces;
1010     pxNetworkEndPoints = &xEndPoint;
1011 
1012     pxEndPoint = FreeRTOS_FirstEndPoint_IPv6( pxNetworkInterfaces );
1013 
1014     TEST_ASSERT_EQUAL( &xEndPoint, pxEndPoint );
1015 }
1016 
1017 /**
1018  * @brief FreeRTOS_FirstEndPoint_IPv6 should return first endpoint in the list if input is NULL.
1019  *
1020  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
1021  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
1022  *
1023  * Test step:
1024  *  - Set a network interface into pxNetworkInterfaces.
1025  *  - Attach an endpoint to the network interface.
1026  *     - Set IPv6 bit in endpoint.
1027  *  - Call FreeRTOS_FirstEndPoint_IPv6 to get attached endpoint with NULL input.
1028  *  - Check if returned endpoint is same as attached one.
1029  */
test_FreeRTOS_FirstEndPoint_IPv6_Null(void)1030 void test_FreeRTOS_FirstEndPoint_IPv6_Null( void )
1031 {
1032     NetworkInterface_t xNetworkInterface;
1033     NetworkInterface_t * pxNetworkInterface = NULL;
1034     NetworkEndPoint_t xEndPoint;
1035     NetworkEndPoint_t * pxEndPoint = NULL;
1036 
1037     memset( &xNetworkInterface, 0, sizeof( NetworkInterface_t ) );
1038     pxNetworkInterfaces = &xNetworkInterface;
1039 
1040     memset( &xEndPoint, 0, sizeof( NetworkEndPoint_t ) );
1041     xEndPoint.bits.bIPv6 = pdTRUE;
1042     xEndPoint.pxNetworkInterface = pxNetworkInterfaces;
1043     pxNetworkEndPoints = &xEndPoint;
1044 
1045     pxEndPoint = FreeRTOS_FirstEndPoint_IPv6( NULL );
1046 
1047     TEST_ASSERT_EQUAL( &xEndPoint, pxEndPoint );
1048 }
1049 
1050 /**
1051  * @brief FreeRTOS_FirstEndPoint_IPv6 should return NULL if no IPv6 endpoint in the list.
1052  *
1053  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
1054  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
1055  *
1056  * Test step:
1057  *  - Set a network interface into pxNetworkInterfaces.
1058  *  - Attach an IPv4 endpoint to the network interface.
1059  *  - Call FreeRTOS_FirstEndPoint_IPv6 to get attached endpoint with NULL input.
1060  *  - Check if returned endpoint is same as attached one.
1061  */
test_FreeRTOS_FirstEndPoint_IPv6_NotFound(void)1062 void test_FreeRTOS_FirstEndPoint_IPv6_NotFound( void )
1063 {
1064     NetworkInterface_t xNetworkInterface;
1065     NetworkInterface_t * pxNetworkInterface = NULL;
1066     NetworkEndPoint_t xEndPoint;
1067     NetworkEndPoint_t * pxEndPoint = NULL;
1068 
1069     memset( &xNetworkInterface, 0, sizeof( NetworkInterface_t ) );
1070     pxNetworkInterfaces = &xNetworkInterface;
1071 
1072     memset( &xEndPoint, 0, sizeof( NetworkEndPoint_t ) );
1073     xEndPoint.pxNetworkInterface = pxNetworkInterfaces;
1074     pxNetworkEndPoints = &xEndPoint;
1075 
1076     pxEndPoint = FreeRTOS_FirstEndPoint_IPv6( &xNetworkInterface );
1077 
1078     TEST_ASSERT_EQUAL( NULL, pxEndPoint );
1079 }
1080 
1081 /**
1082  * @brief FreeRTOS_FirstEndPoint_IPv6 should return first endpoint with specified interface.
1083  *
1084  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
1085  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
1086  *
1087  * Test step:
1088  *  - Create 3 network interfaces and 6 endpoints (e0~e5).
1089  *  - Attach 2 endpoints each network interface.
1090  *     - One endpoint with IPv6 bit set.
1091  *     - The other endpoint with IPv6 bit clear.
1092  *     - The other endpoint with IPv6 bit clear.
1093  *       - Network interface 0: endpoint e0(IPv4)/e3(IPv6)
1094  *       - Network interface 1: endpoint e1(IPv4)/e4(IPv6)
1095  *       - Network interface 2: endpoint e2(IPv4)/e5(IPv6)
1096  *  - Put interfaces & endpoints into the list.
1097  *  - Loop to call FreeRTOS_FirstEndPoint_IPv6 to get attached IPv6 endpoint with each network interface.
1098  *  - Check if returned endpoint is same as attached one.
1099  */
test_FreeRTOS_FirstEndPoint_IPv6_AnotherInterface(void)1100 void test_FreeRTOS_FirstEndPoint_IPv6_AnotherInterface( void )
1101 {
1102     /* Attach one endpoint to one network interface. Check if we can get correct endpoint by API. */
1103     NetworkInterface_t xNetworkInterface[ 3 ];
1104     NetworkInterface_t * pxNetworkInterface = NULL;
1105     NetworkEndPoint_t xEndPoint[ 6 ];
1106     NetworkEndPoint_t * pxEndPoint = NULL;
1107     int i = 0;
1108 
1109     for( i = 0; i < 3; i++ )
1110     {
1111         memset( &( xNetworkInterface[ i ] ), 0, sizeof( NetworkInterface_t ) );
1112 
1113         if( pxNetworkInterfaces == NULL )
1114         {
1115             pxNetworkInterfaces = &( xNetworkInterface[ i ] );
1116             pxNetworkInterface = pxNetworkInterfaces;
1117         }
1118         else
1119         {
1120             pxNetworkInterface->pxNext = &( xNetworkInterface[ i ] );
1121             pxNetworkInterface = pxNetworkInterface->pxNext;
1122         }
1123     }
1124 
1125     for( i = 0; i < 6; i++ )
1126     {
1127         bool bShouldBeIPv6 = i >= 3 ? true : false;
1128         memset( &( xEndPoint[ i ] ), 0, sizeof( NetworkEndPoint_t ) );
1129 
1130         if( pxNetworkEndPoints == NULL )
1131         {
1132             pxNetworkEndPoints = &( xEndPoint[ i ] );
1133             pxEndPoint = pxNetworkEndPoints;
1134         }
1135         else
1136         {
1137             pxEndPoint->pxNext = &( xEndPoint[ i ] );
1138             pxEndPoint = pxEndPoint->pxNext;
1139         }
1140 
1141         if( bShouldBeIPv6 )
1142         {
1143             xEndPoint[ i ].pxNetworkInterface = &( xNetworkInterface[ i - 3 ] );
1144             xEndPoint[ i ].bits.bIPv6 = pdTRUE;
1145         }
1146         else
1147         {
1148             xEndPoint[ i ].pxNetworkInterface = &( xNetworkInterface[ i ] );
1149             xEndPoint[ i ].bits.bIPv6 = pdFALSE;
1150         }
1151     }
1152 
1153     for( i = 0; i < 3; i++ )
1154     {
1155         pxEndPoint = FreeRTOS_FirstEndPoint_IPv6( &( xNetworkInterface[ i ] ) );
1156         TEST_ASSERT_EQUAL( &( xEndPoint[ i + 3 ] ), pxEndPoint );
1157     }
1158 }
1159 
1160 /**
1161  * @brief FreeRTOS_NextEndPoint should return next endpoint with specified endpoint.
1162  *
1163  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
1164  *
1165  * Test step:
1166  *  - Create 3 endpoints and stored then into pxNetworkEndPoints.
1167  *  - Loop to call FreeRTOS_NextEndPoint to get endpoints.
1168  *  - Check if endpoints are returned in correct order.
1169  */
test_FreeRTOS_NextEndPoint_HappyPath(void)1170 void test_FreeRTOS_NextEndPoint_HappyPath( void )
1171 {
1172     NetworkEndPoint_t xEndPoint[ 3 ];
1173     NetworkEndPoint_t * pxEndPoint = NULL;
1174     int i = 0;
1175 
1176     for( i = 0; i < 3; i++ )
1177     {
1178         memset( &( xEndPoint[ i ] ), 0, sizeof( NetworkEndPoint_t ) );
1179 
1180         if( pxNetworkEndPoints == NULL )
1181         {
1182             pxNetworkEndPoints = &( xEndPoint[ i ] );
1183             pxEndPoint = pxNetworkEndPoints;
1184         }
1185         else
1186         {
1187             pxEndPoint->pxNext = &( xEndPoint[ i ] );
1188             pxEndPoint = pxEndPoint->pxNext;
1189         }
1190     }
1191 
1192     pxEndPoint = pxNetworkEndPoints;
1193 
1194     for( i = 0; i < 3; i++ )
1195     {
1196         TEST_ASSERT_EQUAL( &( xEndPoint[ i ] ), pxEndPoint );
1197         pxEndPoint = FreeRTOS_NextEndPoint( NULL, pxEndPoint );
1198     }
1199 
1200     TEST_ASSERT_EQUAL( NULL, pxEndPoint );
1201 }
1202 
1203 /**
1204  * @brief FreeRTOS_NextEndPoint should return next endpoint with specified interface.
1205  *
1206  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
1207  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
1208  *
1209  * Test step:
1210  *  - Create 3 interfaces and 9 endpoints (e0~e8).
1211  *  - Put interfaces & endpoints into the list.
1212  *     - Attach 3 endpoints into each network interface
1213  *       - Network interface 0: endpoint e0/e1/e2
1214  *       - Network interface 1: endpoint e3/e4/e5
1215  *       - Network interface 2: endpoint e6/e7/e8
1216  *     - Stored endpoints in below order.
1217  *       - e0,e3,e6,e1,e4,e7,e2,e5,e8
1218  *  - Loop to call FreeRTOS_NextEndPoint to get endpoints for interface 0.
1219  *  - Check if returned endpoints are e0 -> e1 -> e2.
1220  *  - Loop to call FreeRTOS_NextEndPoint to get endpoints for interface 1.
1221  *  - Check if returned endpoints are e3 -> e4 -> e5.
1222  *  - Loop to call FreeRTOS_NextEndPoint to get endpoints for interface 2.
1223  *  - Check if returned endpoints are e6 -> e7 -> e8.
1224  */
test_FreeRTOS_NextEndPoint_AnotherInterface(void)1225 void test_FreeRTOS_NextEndPoint_AnotherInterface( void )
1226 {
1227     NetworkInterface_t xNetworkInterface[ 3 ];
1228     NetworkInterface_t * pxNetworkInterface = NULL;
1229     NetworkEndPoint_t xEndPoint[ 9 ];
1230     NetworkEndPoint_t * pxEndPoint = NULL;
1231     int i = 0;
1232     const int specifiedEndpointOrder[ 9 ][ 2 ] = \
1233         /* {network_interface_index, endpoint_index} */
1234     {
1235         { 0, 0 },
1236         { 1, 3 },
1237         { 2, 6 },
1238         { 0, 1 },
1239         { 1, 4 },
1240         { 2, 7 },
1241         { 0, 2 },
1242         { 1, 5 },
1243         { 2, 8 }
1244     };
1245 
1246     /* Initialize network interface list. */
1247     for( i = 0; i < 3; i++ )
1248     {
1249         memset( &( xNetworkInterface[ i ] ), 0, sizeof( NetworkInterface_t ) );
1250 
1251         if( pxNetworkInterfaces == NULL )
1252         {
1253             pxNetworkInterfaces = &( xNetworkInterface[ i ] );
1254             pxNetworkInterface = pxNetworkInterfaces;
1255         }
1256         else
1257         {
1258             pxNetworkInterface->pxNext = &( xNetworkInterface[ i ] );
1259             pxNetworkInterface = pxNetworkInterface->pxNext;
1260         }
1261     }
1262 
1263     /* Initialize endpoint list. */
1264     for( i = 0; i < 9; i++ )
1265     {
1266         memset( &( xEndPoint[ specifiedEndpointOrder[ i ][ 1 ] ] ), 0, sizeof( NetworkEndPoint_t ) );
1267 
1268         if( pxNetworkEndPoints == NULL )
1269         {
1270             pxNetworkEndPoints = &( xEndPoint[ specifiedEndpointOrder[ i ][ 1 ] ] );
1271             pxEndPoint = pxNetworkEndPoints;
1272         }
1273         else
1274         {
1275             pxEndPoint->pxNext = &( xEndPoint[ specifiedEndpointOrder[ i ][ 1 ] ] );
1276             pxEndPoint = pxEndPoint->pxNext;
1277         }
1278 
1279         pxEndPoint->pxNetworkInterface = &( xNetworkInterface[ specifiedEndpointOrder[ i ][ 0 ] ] );
1280     }
1281 
1282     /* Loop to call FreeRTOS_NextEndPoint to get endpoints for interface 0. */
1283     /* Check if returned endpoints are e0 -> e1 -> e2. */
1284     pxEndPoint = NULL;
1285     pxEndPoint = FreeRTOS_NextEndPoint( &( xNetworkInterface[ 0 ] ), pxEndPoint );
1286     TEST_ASSERT_EQUAL( &( xEndPoint[ 0 ] ), pxEndPoint );
1287     pxEndPoint = FreeRTOS_NextEndPoint( &( xNetworkInterface[ 0 ] ), pxEndPoint );
1288     TEST_ASSERT_EQUAL( &( xEndPoint[ 1 ] ), pxEndPoint );
1289     pxEndPoint = FreeRTOS_NextEndPoint( &( xNetworkInterface[ 0 ] ), pxEndPoint );
1290     TEST_ASSERT_EQUAL( &( xEndPoint[ 2 ] ), pxEndPoint );
1291 
1292     /* Loop to call FreeRTOS_NextEndPoint to get endpoints for interface 1. */
1293     /* Check if returned endpoints are e3 -> e4 -> e5. */
1294     pxEndPoint = NULL;
1295     pxEndPoint = FreeRTOS_NextEndPoint( &( xNetworkInterface[ 1 ] ), pxEndPoint );
1296     TEST_ASSERT_EQUAL( &( xEndPoint[ 3 ] ), pxEndPoint );
1297     pxEndPoint = FreeRTOS_NextEndPoint( &( xNetworkInterface[ 1 ] ), pxEndPoint );
1298     TEST_ASSERT_EQUAL( &( xEndPoint[ 4 ] ), pxEndPoint );
1299     pxEndPoint = FreeRTOS_NextEndPoint( &( xNetworkInterface[ 1 ] ), pxEndPoint );
1300     TEST_ASSERT_EQUAL( &( xEndPoint[ 5 ] ), pxEndPoint );
1301 
1302     /* Loop to call FreeRTOS_NextEndPoint to get endpoints for interface 2. */
1303     /* Check if returned endpoints are e6 -> e7 -> e8. */
1304     pxEndPoint = NULL;
1305     pxEndPoint = FreeRTOS_NextEndPoint( &( xNetworkInterface[ 2 ] ), pxEndPoint );
1306     TEST_ASSERT_EQUAL( &( xEndPoint[ 6 ] ), pxEndPoint );
1307     pxEndPoint = FreeRTOS_NextEndPoint( &( xNetworkInterface[ 2 ] ), pxEndPoint );
1308     TEST_ASSERT_EQUAL( &( xEndPoint[ 7 ] ), pxEndPoint );
1309     pxEndPoint = FreeRTOS_NextEndPoint( &( xNetworkInterface[ 2 ] ), pxEndPoint );
1310     TEST_ASSERT_EQUAL( &( xEndPoint[ 8 ] ), pxEndPoint );
1311 }
1312 
1313 /**
1314  * @brief FreeRTOS_FindEndPointOnIP_IPv4 should return the endpoint with specified IPv4 address.
1315  *
1316  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
1317  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
1318  *
1319  * Test step:
1320  *  - Create 1 endpoint and add it to the list.
1321  *  - Call FreeRTOS_FindEndPointOnIP_IPv4 to query with same IP address stored in endpoint.
1322  *  - Check if returned endpoint is same.
1323  */
test_FreeRTOS_FindEndPointOnIP_IPv4_HappyPath(void)1324 void test_FreeRTOS_FindEndPointOnIP_IPv4_HappyPath( void )
1325 {
1326     NetworkEndPoint_t xEndPoint;
1327     NetworkEndPoint_t * pxEndPoint = NULL;
1328 
1329     memset( &xEndPoint, 0, sizeof( NetworkEndPoint_t ) );
1330     xEndPoint.ipv4_settings.ulIPAddress = IPV4_DEFAULT_ADDRESS;
1331 
1332     pxNetworkEndPoints = &xEndPoint;
1333 
1334     pxEndPoint = FreeRTOS_FindEndPointOnIP_IPv4( IPV4_DEFAULT_ADDRESS, 0 );
1335     TEST_ASSERT_EQUAL( &xEndPoint, pxEndPoint );
1336 }
1337 
1338 /**
1339  * @brief FreeRTOS_FindEndPointOnIP_IPv4 should return NULL if no matched endpoint found.
1340  *
1341  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
1342  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
1343  *
1344  * Test step:
1345  *  - Create 2 endpoints (e0, e1) and add them to the list.
1346  *     - Set e0 IP address to 192.168.123.223 (IPV4_DEFAULT_ADDRESS).
1347  *     - Set e1 IP address to 2001::1 (xDefaultIPAddress_IPv6).
1348  *  - Call FreeRTOS_FindEndPointOnIP_IPv4 to query different IP address.
1349  *  - Check if returned endpoint is NULL.
1350  */
test_FreeRTOS_FindEndPointOnIP_IPv4_NotFound(void)1351 void test_FreeRTOS_FindEndPointOnIP_IPv4_NotFound( void )
1352 {
1353     NetworkEndPoint_t xEndPoint[ 2 ];
1354     NetworkEndPoint_t * pxEndPoint = NULL;
1355     uint32_t ulWhere;
1356 
1357     /* Initialize e0. */
1358     memset( &xEndPoint[ 0 ], 0, sizeof( NetworkEndPoint_t ) );
1359     xEndPoint[ 0 ].ipv4_settings.ulIPAddress = IPV4_DEFAULT_ADDRESS;
1360     pxNetworkEndPoints = &xEndPoint[ 0 ];
1361 
1362     /* Initialize e1. */
1363     memset( &xEndPoint[ 1 ], 0, sizeof( NetworkEndPoint_t ) );
1364     memcpy( xEndPoint[ 1 ].ipv6_settings.xIPAddress.ucBytes, &xDefaultIPAddress_IPv6.ucBytes, sizeof( IPv6_Address_t ) );
1365     xEndPoint[ 1 ].bits.bIPv6 = pdTRUE;
1366     pxNetworkEndPoints->pxNext = &xEndPoint[ 1 ];
1367 
1368     /* Set ulWhere to boundary of array size to toggle corner case. */
1369     ulWhere = sizeof( xRoutingStatistics.ulLocationsIP ) / sizeof( xRoutingStatistics.ulLocationsIP[ 0 ] );
1370     pxEndPoint = FreeRTOS_FindEndPointOnIP_IPv4( IPV4_DEFAULT_GATEWAY, ulWhere );
1371     TEST_ASSERT_EQUAL( NULL, pxEndPoint );
1372 }
1373 
1374 /**
1375  * @brief FreeRTOS_FindEndPointOnIP_IPv4 should return the endpoint with specified IPv4 address.
1376  *
1377  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
1378  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
1379  *
1380  * Test step:
1381  *  - Create 2 endpoint and add it to the list.
1382  *  - Call FreeRTOS_FindEndPointOnIP_IPv4 to query with xDefaultIPAddress_IPv4 address stored in endpoint.
1383  *  - Check if returned endpoint is same.
1384  *  - Call FreeRTOS_FindEndPointOnIP_IPv4 to query with xDefaultGatewayAddress_IPv4 address stored in endpoint.
1385  *  - Check if returned endpoint is same.
1386  *  - Call FreeRTOS_FindEndPointOnIP_IPv4 to query with xDefaultDNSServerAddress_IPv4 address stored in endpoint.
1387  *  - Check if returned endpoint is NULL.
1388  */
test_FreeRTOS_FindEndPointOnIP_IPv4_MultipleEndpoints(void)1389 void test_FreeRTOS_FindEndPointOnIP_IPv4_MultipleEndpoints( void )
1390 {
1391     NetworkEndPoint_t xEndPoint[ 2 ];
1392     NetworkEndPoint_t * pxEndPoint = NULL;
1393 
1394     memset( &xEndPoint[ 0 ], 0, sizeof( NetworkEndPoint_t ) );
1395     xEndPoint[ 0 ].ipv4_settings.ulIPAddress = IPV4_DEFAULT_ADDRESS;
1396     pxNetworkEndPoints = &xEndPoint[ 0 ];
1397 
1398     memset( &xEndPoint[ 1 ], 0, sizeof( NetworkEndPoint_t ) );
1399     xEndPoint[ 1 ].ipv4_settings.ulIPAddress = IPV4_DEFAULT_GATEWAY;
1400     pxNetworkEndPoints->pxNext = &xEndPoint[ 1 ];
1401 
1402     pxEndPoint = FreeRTOS_FindEndPointOnIP_IPv4( IPV4_DEFAULT_ADDRESS, 0 );
1403     TEST_ASSERT_EQUAL( &xEndPoint[ 0 ], pxEndPoint );
1404 
1405     pxEndPoint = FreeRTOS_FindEndPointOnIP_IPv4( IPV4_DEFAULT_GATEWAY, 0 );
1406     TEST_ASSERT_EQUAL( &xEndPoint[ 1 ], pxEndPoint );
1407 
1408     pxEndPoint = FreeRTOS_FindEndPointOnIP_IPv4( IPV4_DEFAULT_DNS_SERVER, 0 );
1409     TEST_ASSERT_EQUAL( NULL, pxEndPoint );
1410 }
1411 
1412 /**
1413  * @brief FreeRTOS_FindEndPointOnIP_IPv4 should return first endpoint in the list when query IP address is 0.
1414  *
1415  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
1416  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
1417  *
1418  * Test step:
1419  *  - Create 2 endpoint and add it to the list.
1420  *  - Call FreeRTOS_FindEndPointOnIP_IPv4 to query with 0 address.
1421  *  - Check if returned endpoint is the first endpoint.
1422  */
test_FreeRTOS_FindEndPointOnIP_IPv4_AnyEndpoint(void)1423 void test_FreeRTOS_FindEndPointOnIP_IPv4_AnyEndpoint( void )
1424 {
1425     NetworkEndPoint_t xEndPoint[ 2 ];
1426     NetworkEndPoint_t * pxEndPoint = NULL;
1427 
1428     memset( &xEndPoint[ 0 ], 0, sizeof( NetworkEndPoint_t ) );
1429     xEndPoint[ 0 ].ipv4_settings.ulIPAddress = IPV4_DEFAULT_ADDRESS;
1430     pxNetworkEndPoints = &xEndPoint[ 0 ];
1431 
1432     memset( &xEndPoint[ 1 ], 0, sizeof( NetworkEndPoint_t ) );
1433     xEndPoint[ 1 ].ipv4_settings.ulIPAddress = IPV4_DEFAULT_GATEWAY;
1434     pxNetworkEndPoints->pxNext = &xEndPoint[ 1 ];
1435 
1436     pxEndPoint = FreeRTOS_FindEndPointOnIP_IPv4( 0, 0 );
1437     TEST_ASSERT_EQUAL( &xEndPoint[ 0 ], pxEndPoint );
1438 }
1439 
1440 /**
1441  * @brief FreeRTOS_FindEndPointOnIP_IPv4 should return first endpoint in the list when query IP address is 0.
1442  *
1443  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
1444  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
1445  *
1446  * Test step:
1447  *  - Create 1 endpoint with IP address 0 and add it to the list.
1448  *  - Call FreeRTOS_FindEndPointOnIP_IPv4 to query with IPV4_DEFAULT_ADDRESS.
1449  *  - Check if returned endpoint is same.
1450  *  - Call FreeRTOS_FindEndPointOnIP_IPv4 to query with IPV4_DEFAULT_GATEWAY.
1451  *  - Check if returned endpoint is same.
1452  */
test_FreeRTOS_FindEndPointOnIP_IPv4_ZeroAddressEndpoint(void)1453 void test_FreeRTOS_FindEndPointOnIP_IPv4_ZeroAddressEndpoint( void )
1454 {
1455     NetworkEndPoint_t xEndPoint;
1456     NetworkEndPoint_t * pxEndPoint = NULL;
1457 
1458     memset( &xEndPoint, 0, sizeof( NetworkEndPoint_t ) );
1459     xEndPoint.ipv4_settings.ulIPAddress = 0;
1460     pxNetworkEndPoints = &xEndPoint;
1461 
1462     pxEndPoint = FreeRTOS_FindEndPointOnIP_IPv4( IPV4_DEFAULT_ADDRESS, 0 );
1463     TEST_ASSERT_EQUAL( &xEndPoint, pxEndPoint );
1464     pxEndPoint = FreeRTOS_FindEndPointOnIP_IPv4( IPV4_DEFAULT_GATEWAY, 0 );
1465     TEST_ASSERT_EQUAL( &xEndPoint, pxEndPoint );
1466 }
1467 
1468 /**
1469  * @brief FreeRTOS_FindEndPointOnIP_IPv6 should return the endpoint with specified IPv6 address.
1470  *
1471  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
1472  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
1473  *
1474  * Test step:
1475  *  - Create 1 endpoint and add it to the list.
1476  *  - Call FreeRTOS_FindEndPointOnIP_IPv6 to query with same IP address stored in endpoint.
1477  *  - Check if returned endpoint is same.
1478  */
test_FreeRTOS_FindEndPointOnIP_IPv6_HappyPath(void)1479 void test_FreeRTOS_FindEndPointOnIP_IPv6_HappyPath( void )
1480 {
1481     NetworkEndPoint_t xEndPoint;
1482     NetworkEndPoint_t * pxEndPoint = NULL;
1483 
1484     memset( &xEndPoint, 0, sizeof( NetworkEndPoint_t ) );
1485     memcpy( xEndPoint.ipv6_settings.xIPAddress.ucBytes, &xDefaultIPAddress_IPv6.ucBytes, sizeof( IPv6_Address_t ) );
1486     xEndPoint.bits.bIPv6 = pdTRUE;
1487     xEndPoint.ipv6_settings.uxPrefixLength = 64;
1488 
1489     pxNetworkEndPoints = &xEndPoint;
1490 
1491     xCompareIPv6_Address_ExpectAndReturn( &( xEndPoint.ipv6_settings.xIPAddress ), &xDefaultIPAddress_IPv6, xEndPoint.ipv6_settings.uxPrefixLength, 0 );
1492 
1493     pxEndPoint = FreeRTOS_FindEndPointOnIP_IPv6( &xDefaultIPAddress_IPv6 );
1494     TEST_ASSERT_EQUAL( &xEndPoint, pxEndPoint );
1495 }
1496 
1497 /**
1498  * @brief FreeRTOS_FindEndPointOnIP_IPv6 should return the endpoint with specified IPv6 address.
1499  *
1500  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
1501  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
1502  *
1503  * Test step:
1504  *  - Create 2 endpoint and add it to the list.
1505  *  - Call FreeRTOS_FindEndPointOnIP_IPv6 to query with xDefaultIPAddress_IPv6 address stored in endpoint.
1506  *  - Check if returned endpoint is same.
1507  *  - Call FreeRTOS_FindEndPointOnIP_IPv6 to query with xDefaultGatewayAddress_IPv6 address stored in endpoint.
1508  *  - Check if returned endpoint is same.
1509  *  - Call FreeRTOS_FindEndPointOnIP_IPv6 to query with xDefaultDNSServerAddress_IPv6 address stored in endpoint.
1510  *  - Check if returned endpoint is NULL.
1511  */
test_FreeRTOS_FindEndPointOnIP_IPv6_MultipleEndpoints(void)1512 void test_FreeRTOS_FindEndPointOnIP_IPv6_MultipleEndpoints( void )
1513 {
1514     NetworkEndPoint_t xEndPoint[ 2 ];
1515     NetworkEndPoint_t * pxEndPoint = NULL;
1516 
1517     memset( &xEndPoint[ 0 ], 0, sizeof( NetworkEndPoint_t ) );
1518     memcpy( xEndPoint[ 0 ].ipv6_settings.xIPAddress.ucBytes, &xDefaultIPAddress_IPv6.ucBytes, sizeof( IPv6_Address_t ) );
1519     xEndPoint[ 0 ].bits.bIPv6 = pdTRUE;
1520     xEndPoint[ 0 ].ipv6_settings.uxPrefixLength = 64;
1521     pxNetworkEndPoints = &xEndPoint[ 0 ];
1522 
1523     memset( &xEndPoint[ 1 ], 0, sizeof( NetworkEndPoint_t ) );
1524     memcpy( xEndPoint[ 1 ].ipv6_settings.xIPAddress.ucBytes, &xDefaultGatewayAddress_IPv6.ucBytes, sizeof( IPv6_Address_t ) );
1525     xEndPoint[ 1 ].bits.bIPv6 = pdTRUE;
1526     xEndPoint[ 1 ].ipv6_settings.uxPrefixLength = 64;
1527     pxNetworkEndPoints->pxNext = &xEndPoint[ 1 ];
1528 
1529     xCompareIPv6_Address_ExpectAndReturn( &( xEndPoint[ 0 ].ipv6_settings.xIPAddress ), &xDefaultIPAddress_IPv6, xEndPoint[ 0 ].ipv6_settings.uxPrefixLength, 0 );
1530     pxEndPoint = FreeRTOS_FindEndPointOnIP_IPv6( &xDefaultIPAddress_IPv6 );
1531     TEST_ASSERT_EQUAL( &xEndPoint[ 0 ], pxEndPoint );
1532 
1533     xCompareIPv6_Address_ExpectAndReturn( &( xEndPoint[ 0 ].ipv6_settings.xIPAddress ), &xDefaultGatewayAddress_IPv6, xEndPoint[ 0 ].ipv6_settings.uxPrefixLength, 1 );
1534     xCompareIPv6_Address_ExpectAndReturn( &( xEndPoint[ 1 ].ipv6_settings.xIPAddress ), &xDefaultGatewayAddress_IPv6, xEndPoint[ 1 ].ipv6_settings.uxPrefixLength, 0 );
1535     pxEndPoint = FreeRTOS_FindEndPointOnIP_IPv6( &xDefaultGatewayAddress_IPv6 );
1536     TEST_ASSERT_EQUAL( &xEndPoint[ 1 ], pxEndPoint );
1537 
1538     xCompareIPv6_Address_ExpectAndReturn( &( xEndPoint[ 0 ].ipv6_settings.xIPAddress ), &xDefaultDNSServerAddress_IPv6, xEndPoint[ 0 ].ipv6_settings.uxPrefixLength, 1 );
1539     xCompareIPv6_Address_ExpectAndReturn( &( xEndPoint[ 1 ].ipv6_settings.xIPAddress ), &xDefaultDNSServerAddress_IPv6, xEndPoint[ 1 ].ipv6_settings.uxPrefixLength, 1 );
1540     pxEndPoint = FreeRTOS_FindEndPointOnIP_IPv6( &xDefaultDNSServerAddress_IPv6 );
1541     TEST_ASSERT_EQUAL( NULL, pxEndPoint );
1542 }
1543 
1544 /**
1545  * @brief FreeRTOS_FindEndPointOnIP_IPv6 should return NULL if no matched endpoint found.
1546  *
1547  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
1548  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
1549  *
1550  * Test step:
1551  *  - Create 2 endpoint and add it to the list.
1552  *     - 1 IPv4 endpoint.
1553  *     - 1 IPv6 endpoint, with xDefaultIPAddress_IPv6 address.
1554  *  - Call FreeRTOS_FindEndPointOnIP_IPv6 to query with xDefaultIPAddress_IPv6.
1555  *  - Check if returned endpoint is NULL.
1556  */
test_FreeRTOS_FindEndPointOnIP_IPv6_NotFound(void)1557 void test_FreeRTOS_FindEndPointOnIP_IPv6_NotFound( void )
1558 {
1559     NetworkEndPoint_t xEndPoint[ 2 ];
1560     NetworkEndPoint_t * pxEndPoint = NULL;
1561 
1562     memset( &xEndPoint[ 0 ], 0, sizeof( NetworkEndPoint_t ) );
1563     xEndPoint[ 0 ].ipv4_settings.ulIPAddress = IPV4_DEFAULT_ADDRESS;
1564     pxNetworkEndPoints = &xEndPoint[ 0 ];
1565 
1566     memset( &xEndPoint[ 1 ], 0, sizeof( NetworkEndPoint_t ) );
1567     memcpy( xEndPoint[ 1 ].ipv6_settings.xIPAddress.ucBytes, &xDefaultGatewayAddress_IPv6.ucBytes, sizeof( IPv6_Address_t ) );
1568     xEndPoint[ 1 ].bits.bIPv6 = pdTRUE;
1569     xEndPoint[ 1 ].ipv6_settings.uxPrefixLength = 64;
1570     pxNetworkEndPoints->pxNext = &xEndPoint[ 1 ];
1571 
1572     xCompareIPv6_Address_ExpectAndReturn( &( xEndPoint[ 1 ].ipv6_settings.xIPAddress ), &xDefaultGatewayAddress_IPv6, xEndPoint[ 1 ].ipv6_settings.uxPrefixLength, 1 );
1573 
1574     pxEndPoint = FreeRTOS_FindEndPointOnIP_IPv6( &xDefaultGatewayAddress_IPv6 );
1575     TEST_ASSERT_EQUAL( NULL, pxEndPoint );
1576 }
1577 
1578 /**
1579  * @brief FreeRTOS_FindEndPointOnMAC should return the endpoint with specified MAC address & network interface.
1580  *
1581  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
1582  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
1583  *
1584  * Test step:
1585  *  - Create 1 network interface and add it to the list.
1586  *  - Create 1 endpoint, attach to the interface, and add it to the list.
1587  *  - Call FreeRTOS_FindEndPointOnMAC to query with same MAC address stored in endpoint.
1588  *  - Check if returned endpoint is same.
1589  */
test_FreeRTOS_FindEndPointOnMAC_HappyPath(void)1590 void test_FreeRTOS_FindEndPointOnMAC_HappyPath( void )
1591 {
1592     NetworkEndPoint_t xEndPoint;
1593     NetworkEndPoint_t * pxEndPoint = NULL;
1594     /* MAC address:  */
1595     const MACAddress_t xMACAddress = { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc };
1596     NetworkInterface_t xNetworkInterface;
1597 
1598     /* Initialize network interface and add it to the list. */
1599     memset( &xNetworkInterface, 0, sizeof( NetworkInterface_t ) );
1600     pxNetworkInterfaces = &xNetworkInterface;
1601 
1602     /* Initialize network endpoint and add it to the list. */
1603     memset( &xEndPoint, 0, sizeof( NetworkEndPoint_t ) );
1604     memcpy( xEndPoint.xMACAddress.ucBytes, xMACAddress.ucBytes, sizeof( MACAddress_t ) );
1605     xEndPoint.pxNetworkInterface = &xNetworkInterface;
1606     pxNetworkEndPoints = &xEndPoint;
1607 
1608     pxEndPoint = FreeRTOS_FindEndPointOnMAC( &xMACAddress, &xNetworkInterface );
1609     TEST_ASSERT_EQUAL( &xEndPoint, pxEndPoint );
1610 }
1611 
1612 /**
1613  * @brief FreeRTOS_FindEndPointOnMAC should return the endpoint with specified MAC address.
1614  *
1615  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
1616  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
1617  *
1618  * Test step:
1619  *  - Create 1 endpoint and add it to the list.
1620  *  - Call FreeRTOS_FindEndPointOnMAC to query with same MAC address stored in endpoint.
1621  *  - Check if returned endpoint is same.
1622  */
test_FreeRTOS_FindEndPointOnMAC_NullInterface(void)1623 void test_FreeRTOS_FindEndPointOnMAC_NullInterface( void )
1624 {
1625     NetworkEndPoint_t xEndPoint;
1626     NetworkEndPoint_t * pxEndPoint = NULL;
1627     /* MAC address:  */
1628     const MACAddress_t xMACAddress = { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc };
1629 
1630     /* Initialize network endpoint and add it to the list. */
1631     memset( &xEndPoint, 0, sizeof( NetworkEndPoint_t ) );
1632     memcpy( xEndPoint.xMACAddress.ucBytes, xMACAddress.ucBytes, sizeof( MACAddress_t ) );
1633     pxNetworkEndPoints = &xEndPoint;
1634 
1635     pxEndPoint = FreeRTOS_FindEndPointOnMAC( &xMACAddress, NULL );
1636     TEST_ASSERT_EQUAL( &xEndPoint, pxEndPoint );
1637 }
1638 
1639 /**
1640  * @brief FreeRTOS_FindEndPointOnMAC should return the endpoint with NULL pointer as MAC address.
1641  *
1642  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
1643  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
1644  *
1645  * Test step:
1646  *  - Create 1 endpoint and add it to the list.
1647  *  - Call FreeRTOS_FindEndPointOnMAC to query with NULL pointer as MAC address.
1648  *  - Check if returned endpoint is NULL.
1649  */
test_FreeRTOS_FindEndPointOnMAC_NullMAC(void)1650 void test_FreeRTOS_FindEndPointOnMAC_NullMAC( void )
1651 {
1652     NetworkEndPoint_t xEndPoint;
1653     NetworkEndPoint_t * pxEndPoint = NULL;
1654     /* MAC address:  */
1655     const MACAddress_t xMACAddress = { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc };
1656 
1657     /* Initialize network endpoint and add it to the list. */
1658     memset( &xEndPoint, 0, sizeof( NetworkEndPoint_t ) );
1659     memcpy( xEndPoint.xMACAddress.ucBytes, xMACAddress.ucBytes, sizeof( MACAddress_t ) );
1660     pxNetworkEndPoints = &xEndPoint;
1661 
1662     pxEndPoint = FreeRTOS_FindEndPointOnMAC( NULL, NULL );
1663     TEST_ASSERT_EQUAL( NULL, pxEndPoint );
1664 }
1665 
1666 /**
1667  * @brief FreeRTOS_FindEndPointOnMAC should return the endpoint attached to the specified interface.
1668  *
1669  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
1670  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
1671  *
1672  * Test step:
1673  *  - Create 2 interfaces and add them to the list.
1674  *  - Create 2 endpoints with same MAC address, attach to each interface, and add them to the list.
1675  *  - Call FreeRTOS_FindEndPointOnMAC to query attached in first interface.
1676  *  - Check if returned endpoint is same as first endpoint.
1677  *  - Call FreeRTOS_FindEndPointOnMAC to query attached in second interface.
1678  *  - Check if returned endpoint is same as second endpoint.
1679  */
test_FreeRTOS_FindEndPointOnMAC_MultipleInterface(void)1680 void test_FreeRTOS_FindEndPointOnMAC_MultipleInterface( void )
1681 {
1682     NetworkEndPoint_t xEndPoint[ 2 ];
1683     NetworkEndPoint_t * pxEndPoint = NULL;
1684     NetworkInterface_t xNetworkInterface[ 2 ];
1685     NetworkInterface_t * pxNetworkInterface = NULL;
1686     /* MAC address:  */
1687     const MACAddress_t xMACAddress = { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc };
1688     int i = 0;
1689 
1690     /* Initialize network interfaces and add them to the list. */
1691     for( i = 0; i < 2; i++ )
1692     {
1693         memset( &( xNetworkInterface[ i ] ), 0, sizeof( NetworkInterface_t ) );
1694 
1695         if( pxNetworkInterfaces == NULL )
1696         {
1697             pxNetworkInterfaces = &( xNetworkInterface[ i ] );
1698             pxNetworkInterface = pxNetworkInterfaces;
1699         }
1700         else
1701         {
1702             pxNetworkInterface->pxNext = &( xNetworkInterface[ i ] );
1703             pxNetworkInterface = pxNetworkInterface->pxNext;
1704         }
1705     }
1706 
1707     /* Initialize network endpoints and add them to the list. */
1708     for( i = 0; i < 2; i++ )
1709     {
1710         memset( &( xEndPoint[ i ] ), 0, sizeof( NetworkEndPoint_t ) );
1711 
1712         if( pxNetworkEndPoints == NULL )
1713         {
1714             pxNetworkEndPoints = &( xEndPoint[ i ] );
1715             pxEndPoint = pxNetworkEndPoints;
1716         }
1717         else
1718         {
1719             pxEndPoint->pxNext = &( xEndPoint[ i ] );
1720             pxEndPoint = pxEndPoint->pxNext;
1721         }
1722 
1723         pxEndPoint->pxNetworkInterface = &xNetworkInterface[ i ];
1724         memcpy( pxEndPoint->xMACAddress.ucBytes, xMACAddress.ucBytes, sizeof( MACAddress_t ) );
1725     }
1726 
1727     /* Check if we can get correct endpoint by specified network interface. */
1728     for( i = 0; i < 2; i++ )
1729     {
1730         pxEndPoint = FreeRTOS_FindEndPointOnMAC( &xMACAddress, &xNetworkInterface[ i ] );
1731         TEST_ASSERT_EQUAL( &( xEndPoint[ i ] ), pxEndPoint );
1732     }
1733 }
1734 
1735 /**
1736  * @brief FreeRTOS_FindEndPointOnMAC should return NULL if no endpoint matches.
1737  *
1738  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
1739  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
1740  *
1741  * Test step:
1742  *  - Create 1 network interface and add it to the list.
1743  *  - Create 1 endpoint, attach to the interface, and add it to the list.
1744  *     - Use ucDefaultMACAddress_IPv4 as MAC address.
1745  *  - Call FreeRTOS_FindEndPointOnMAC to query with different MAC address.
1746  *  - Check if returned endpoint is NULL.
1747  */
test_FreeRTOS_FindEndPointOnMAC_NotFound(void)1748 void test_FreeRTOS_FindEndPointOnMAC_NotFound( void )
1749 {
1750     NetworkEndPoint_t xEndPoint;
1751     NetworkEndPoint_t * pxEndPoint = NULL;
1752     /* MAC address:  */
1753     const MACAddress_t xMACAddress = { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc };
1754     NetworkInterface_t xNetworkInterface;
1755     MACAddress_t * pxQueryMACAddress = NULL;
1756 
1757     /* Initialize network interface and add it to the list. */
1758     memset( &xNetworkInterface, 0, sizeof( NetworkInterface_t ) );
1759     pxNetworkInterfaces = &xNetworkInterface;
1760 
1761     /* Initialize network endpoint and add it to the list. */
1762     memset( &xEndPoint, 0, sizeof( NetworkEndPoint_t ) );
1763     memcpy( xEndPoint.xMACAddress.ucBytes, xMACAddress.ucBytes, sizeof( MACAddress_t ) );
1764     xEndPoint.pxNetworkInterface = &xNetworkInterface;
1765     pxNetworkEndPoints = &xEndPoint;
1766 
1767     pxQueryMACAddress = ( MACAddress_t * ) ( &ucDefaultMACAddress_IPv4 );
1768     pxEndPoint = FreeRTOS_FindEndPointOnMAC( pxQueryMACAddress, &xNetworkInterface );
1769     TEST_ASSERT_EQUAL( NULL, pxEndPoint );
1770 }
1771 
1772 /**
1773  * @brief FreeRTOS_FindEndPointOnNetMask should be able to find the endpoint within same network region.
1774  *
1775  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
1776  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
1777  *
1778  * Test step:
1779  *  - Create 1 endpoint and add it to the list.
1780  *     - Set the IP address to 192.168.123.223.
1781  *     - Set the netmask to 255.255.255.0.
1782  *  - Call FreeRTOS_FindEndPointOnNetMask to query for IP address 192.168.123.1.
1783  *  - Check if returned endpoint is same.
1784  */
test_FreeRTOS_FindEndPointOnNetMask_HappyPath(void)1785 void test_FreeRTOS_FindEndPointOnNetMask_HappyPath( void )
1786 {
1787     NetworkEndPoint_t xEndPoint;
1788     NetworkEndPoint_t * pxEndPoint = NULL;
1789 
1790     /* Initialize network endpoint and add it to the list. */
1791     memset( &xEndPoint, 0, sizeof( NetworkEndPoint_t ) );
1792     xEndPoint.ipv4_settings.ulIPAddress = IPV4_DEFAULT_ADDRESS;
1793     xEndPoint.ipv4_settings.ulNetMask = IPV4_DEFAULT_NETMASK;
1794     pxNetworkEndPoints = &xEndPoint;
1795 
1796     /* IPV4_DEFAULT_DNS_SERVER is 192.168.123.1 within the network region. */
1797     pxEndPoint = FreeRTOS_FindEndPointOnNetMask( IPV4_DEFAULT_DNS_SERVER, 0 );
1798     TEST_ASSERT_EQUAL( &xEndPoint, pxEndPoint );
1799 }
1800 
1801 /**
1802  * @brief FreeRTOS_FindEndPointOnNetMask should be able to return NULL if no endpoint is in same network region.
1803  *
1804  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
1805  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
1806  *
1807  * Test step:
1808  *  - Create 1 endpoint and add it to the list.
1809  *     - Set the netmask to 255.255.255.0.
1810  *     - Set the IP address to 192.168.123.223.
1811  *  - Call FreeRTOS_FindEndPointOnNetMask to query for IP address 192.168.1.1.
1812  *  - Check if returned endpoint is NULL.
1813  */
test_FreeRTOS_FindEndPointOnNetMask_NotFound(void)1814 void test_FreeRTOS_FindEndPointOnNetMask_NotFound( void )
1815 {
1816     NetworkEndPoint_t xEndPoint;
1817     NetworkEndPoint_t * pxEndPoint = NULL;
1818 
1819     /* Initialize network endpoint and add it to the list. */
1820     memset( &xEndPoint, 0, sizeof( NetworkEndPoint_t ) );
1821     xEndPoint.ipv4_settings.ulIPAddress = IPV4_DEFAULT_ADDRESS;
1822     xEndPoint.ipv4_settings.ulNetMask = IPV4_DEFAULT_NETMASK;
1823     pxNetworkEndPoints = &xEndPoint;
1824 
1825     /* 192.168.1.1 is 0x0101A8C0. */
1826     pxEndPoint = FreeRTOS_FindEndPointOnNetMask( 0x0101A8C0, 0 );
1827     TEST_ASSERT_EQUAL( NULL, pxEndPoint );
1828 }
1829 
1830 /**
1831  * @brief FreeRTOS_FindEndPointOnNetMask_IPv6 should be able to find the endpoint within same network region.
1832  *
1833  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
1834  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
1835  *
1836  * Test step:
1837  *  - Create 1 endpoint and add it to the list.
1838  *     - Set the IP address to 2001::1.
1839  *     - Set the prefix address to 2001::.
1840  *     - Set the prefix length to 64.
1841  *  - Call FreeRTOS_FindEndPointOnNetMask to query for IP address 2001::fffe.
1842  *  - Check if returned endpoint is same.
1843  */
test_FreeRTOS_FindEndPointOnNetMask_IPv6_HappyPath(void)1844 void test_FreeRTOS_FindEndPointOnNetMask_IPv6_HappyPath( void )
1845 {
1846     NetworkEndPoint_t xEndPoint;
1847     NetworkEndPoint_t * pxEndPoint = NULL;
1848 
1849     /* Initialize network endpoint and add it to the list. */
1850     memset( &xEndPoint, 0, sizeof( NetworkEndPoint_t ) );
1851     memcpy( xEndPoint.ipv6_settings.xIPAddress.ucBytes, &xDefaultIPAddress_IPv6.ucBytes, sizeof( IPv6_Address_t ) );
1852     memcpy( xEndPoint.ipv6_settings.xPrefix.ucBytes, &xDefaultNetPrefix_IPv6.ucBytes, sizeof( IPv6_Address_t ) );
1853     xEndPoint.ipv6_settings.uxPrefixLength = xDefaultPrefixLength;
1854     xEndPoint.bits.bIPv6 = pdTRUE;
1855     pxNetworkEndPoints = &xEndPoint;
1856 
1857     xCompareIPv6_Address_ExpectAndReturn( &( xEndPoint.ipv6_settings.xIPAddress ), &xDefaultGatewayAddress_IPv6, xDefaultPrefixLength, 0 );
1858 
1859     /* xDefaultGatewayAddress_IPv6 is 2001::fffe within the network region. */
1860     pxEndPoint = FreeRTOS_FindEndPointOnNetMask_IPv6( &xDefaultGatewayAddress_IPv6 );
1861     TEST_ASSERT_EQUAL( &xEndPoint, pxEndPoint );
1862 }
1863 
1864 /**
1865  * @brief FreeRTOS_FindEndPointOnNetMask_IPv6 should be able to return NULL if no endpoint is in same network region.
1866  *
1867  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
1868  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
1869  *
1870  * Test step:
1871  *  - Create 2 endpoint (e0, e1) and add it to the list.
1872  *     - Set e0 IP address to 2001::1.
1873  *     - Set e0 prefix address to 2001::.
1874  *     - Set e0 prefix length to 64.
1875  *     - Set e1 IP address to 192.168.124.223.
1876  *     - Set e1 netmask to 255.255.255.0.
1877  *  - Call FreeRTOS_FindEndPointOnNetMask to query for IP address 2002::fffe.
1878  *  - Check if returned endpoint is NULL.
1879  */
test_FreeRTOS_FindEndPointOnNetMask_IPv6_NotFound(void)1880 void test_FreeRTOS_FindEndPointOnNetMask_IPv6_NotFound( void )
1881 {
1882     NetworkEndPoint_t xEndPoint[ 2 ];
1883     NetworkEndPoint_t * pxEndPoint = NULL;
1884     const IPv6_Address_t xTargetAddress = { 0x20, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xfe };
1885 
1886     /* Initialize e0. */
1887     memset( &xEndPoint[ 0 ], 0, sizeof( NetworkEndPoint_t ) );
1888     memcpy( xEndPoint[ 0 ].ipv6_settings.xIPAddress.ucBytes, &xDefaultIPAddress_IPv6.ucBytes, sizeof( IPv6_Address_t ) );
1889     memcpy( xEndPoint[ 0 ].ipv6_settings.xPrefix.ucBytes, &xDefaultNetPrefix_IPv6.ucBytes, sizeof( IPv6_Address_t ) );
1890     xEndPoint[ 0 ].ipv6_settings.uxPrefixLength = xDefaultPrefixLength;
1891     xEndPoint[ 0 ].bits.bIPv6 = pdTRUE;
1892     pxNetworkEndPoints = &xEndPoint[ 0 ];
1893 
1894     /* Initialize e1. */
1895     memset( &xEndPoint[ 1 ], 0, sizeof( NetworkEndPoint_t ) );
1896     xEndPoint[ 1 ].ipv4_settings.ulIPAddress = IPV4_DEFAULT_ADDRESS;
1897     xEndPoint[ 1 ].ipv4_settings.ulNetMask = IPV4_DEFAULT_NETMASK;
1898     pxNetworkEndPoints->pxNext = &xEndPoint[ 1 ];
1899 
1900     xCompareIPv6_Address_ExpectAndReturn( &( xEndPoint[ 0 ].ipv6_settings.xIPAddress ), &xTargetAddress, xDefaultPrefixLength, 1 );
1901 
1902     pxEndPoint = FreeRTOS_FindEndPointOnNetMask_IPv6( &xTargetAddress );
1903     TEST_ASSERT_EQUAL( NULL, pxEndPoint );
1904 }
1905 
1906 /**
1907  * @brief FreeRTOS_InterfaceEndPointOnNetMask should be able to find the endpoint within same network region.
1908  *
1909  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
1910  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
1911  *
1912  * Test step:
1913  *  - Create 1 endpoint and add it to the list.
1914  *     - Set the IP address to 192.168.123.223 (IPV4_DEFAULT_ADDRESS).
1915  *     - Set the netmask to 255.255.255.0.
1916  *  - Call FreeRTOS_InterfaceEndPointOnNetMask to query for IP address 192.168.123.1.
1917  *  - Check if returned endpoint is same.
1918  */
test_FreeRTOS_InterfaceEndPointOnNetMask_HappyPath(void)1919 void test_FreeRTOS_InterfaceEndPointOnNetMask_HappyPath( void )
1920 {
1921     NetworkEndPoint_t xEndPoint;
1922     NetworkEndPoint_t * pxEndPoint = NULL;
1923     NetworkInterface_t xNetworkInterface;
1924     uint32_t ulWhere;
1925 
1926     /* Initialize network interface and add it to the list. */
1927     memset( &xNetworkInterface, 0, sizeof( NetworkInterface_t ) );
1928     pxNetworkInterfaces = &xNetworkInterface;
1929 
1930     /* Initialize network endpoint and add it to the list. */
1931     memset( &xEndPoint, 0, sizeof( NetworkEndPoint_t ) );
1932     xEndPoint.ipv4_settings.ulIPAddress = IPV4_DEFAULT_ADDRESS;
1933     xEndPoint.ipv4_settings.ulNetMask = IPV4_DEFAULT_NETMASK;
1934     xEndPoint.pxNetworkInterface = &xNetworkInterface;
1935     pxNetworkEndPoints = &xEndPoint;
1936 
1937     /* Set ulWhere to boundary of array size to toggle corner case. */
1938     ulWhere = sizeof( xRoutingStatistics.ulLocations ) / sizeof( xRoutingStatistics.ulLocations[ 0 ] );
1939     /* IPV4_DEFAULT_ADDRESS is 192.168.123.1 within the network region. */
1940     pxEndPoint = FreeRTOS_InterfaceEndPointOnNetMask( &xNetworkInterface, IPV4_DEFAULT_DNS_SERVER, ulWhere );
1941     TEST_ASSERT_EQUAL( &xEndPoint, pxEndPoint );
1942 }
1943 
1944 /**
1945  * @brief FreeRTOS_InterfaceEndPointOnNetMask should be able to find the endpoint within same network region.
1946  *
1947  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
1948  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
1949  *
1950  * Test step:
1951  *  - Create 2 network interfaces and add them to the list.
1952  *  - Create 2 endpoints (e0, e1).
1953  *     - Set e0 IP address to 192.168.123.223 (IPV4_DEFAULT_ADDRESS).
1954  *     - Set e0 netmask to 255.255.255.0.
1955  *     - Attach e0 to first interface.
1956  *     - Set e1 IP address to 192.168.124.223.
1957  *     - Set e1 netmask to 255.255.255.0.
1958  *     - Attach e0 to second interface.
1959  *  - Call FreeRTOS_InterfaceEndPointOnNetMask to query for IP address 192.168.124.1.
1960  *  - Check if returned endpoint is e1.
1961  */
test_FreeRTOS_InterfaceEndPointOnNetMask_DifferentInterface(void)1962 void test_FreeRTOS_InterfaceEndPointOnNetMask_DifferentInterface( void )
1963 {
1964     NetworkEndPoint_t xEndPoint[ 2 ];
1965     NetworkEndPoint_t * pxEndPoint = NULL;
1966     NetworkInterface_t xNetworkInterface[ 2 ];
1967     uint32_t ulWhere;
1968     /* 192.168.124.223 is 0xDF7CA8C0. */
1969     uint32_t ulE1IPAddress = 0xDF7CA8C0;
1970     /* 192.168.124.1 is 0x017CA8C0. */
1971     uint32_t ulQueryIPAddress = 0x017CA8C0;
1972 
1973     /* Create 2 network interfaces and add them to the list. */
1974     memset( &xNetworkInterface[ 0 ], 0, sizeof( NetworkInterface_t ) );
1975     pxNetworkInterfaces = &xNetworkInterface[ 0 ];
1976     memset( &xNetworkInterface[ 1 ], 0, sizeof( NetworkInterface_t ) );
1977     pxNetworkInterfaces->pxNext = &xNetworkInterface[ 1 ];
1978 
1979     /* Initialize e0. */
1980     memset( &xEndPoint[ 0 ], 0, sizeof( NetworkEndPoint_t ) );
1981     xEndPoint[ 0 ].ipv4_settings.ulIPAddress = IPV4_DEFAULT_ADDRESS;
1982     xEndPoint[ 0 ].ipv4_settings.ulNetMask = IPV4_DEFAULT_NETMASK;
1983     xEndPoint[ 0 ].pxNetworkInterface = &xNetworkInterface[ 0 ];
1984     pxNetworkEndPoints = &xEndPoint[ 0 ];
1985 
1986     /* Initialize e1. */
1987     memset( &xEndPoint[ 1 ], 0, sizeof( NetworkEndPoint_t ) );
1988     xEndPoint[ 1 ].ipv4_settings.ulIPAddress = ulE1IPAddress;
1989     xEndPoint[ 1 ].ipv4_settings.ulNetMask = IPV4_DEFAULT_NETMASK;
1990     xEndPoint[ 1 ].pxNetworkInterface = &xNetworkInterface[ 1 ];
1991     pxNetworkEndPoints->pxNext = &xEndPoint[ 1 ];
1992 
1993     /* Set ulWhere to boundary of array size to toggle corner case. */
1994     ulWhere = sizeof( xRoutingStatistics.ulLocations ) / sizeof( xRoutingStatistics.ulLocations[ 0 ] );
1995     /* IPV4_DEFAULT_ADDRESS is 192.168.123.1 within the network region. */
1996     pxEndPoint = FreeRTOS_InterfaceEndPointOnNetMask( &xNetworkInterface[ 1 ], ulQueryIPAddress, ulWhere );
1997     TEST_ASSERT_EQUAL( &xEndPoint[ 1 ], pxEndPoint );
1998 }
1999 
2000 /**
2001  * @brief FreeRTOS_InterfaceEndPointOnNetMask returns NULL when there is no endpoint matches.
2002  *
2003  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
2004  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
2005  *
2006  * Test step:
2007  *  - Create 1 IPv6 endpoint and add it to the list.
2008  *     - Set the IP address to 2001::1.
2009  *     - Set the prefix address to 2001::.
2010  *     - Set the prefix length to 64.
2011  *  - Call FreeRTOS_InterfaceEndPointOnNetMask to query for IP address 192.168.123.1.
2012  *  - Check if returned endpoint is same.
2013  */
test_FreeRTOS_InterfaceEndPointOnNetMask_NotFound(void)2014 void test_FreeRTOS_InterfaceEndPointOnNetMask_NotFound( void )
2015 {
2016     NetworkEndPoint_t xEndPoint;
2017     NetworkEndPoint_t * pxEndPoint = NULL;
2018     NetworkInterface_t xNetworkInterface;
2019 
2020     /* Initialize network interface and add it to the list. */
2021     memset( &xNetworkInterface, 0, sizeof( NetworkInterface_t ) );
2022     pxNetworkInterfaces = &xNetworkInterface;
2023 
2024     /* Initialize network endpoint and add it to the list. */
2025     memset( &xEndPoint, 0, sizeof( NetworkEndPoint_t ) );
2026     memcpy( xEndPoint.ipv6_settings.xIPAddress.ucBytes, &xDefaultIPAddress_IPv6.ucBytes, sizeof( IPv6_Address_t ) );
2027     memcpy( xEndPoint.ipv6_settings.xPrefix.ucBytes, &xDefaultNetPrefix_IPv6.ucBytes, sizeof( IPv6_Address_t ) );
2028     xEndPoint.ipv6_settings.uxPrefixLength = xDefaultPrefixLength;
2029     xEndPoint.bits.bIPv6 = pdTRUE;
2030     xEndPoint.pxNetworkInterface = &xNetworkInterface;
2031     pxNetworkEndPoints = &xEndPoint;
2032 
2033     /* IPV4_DEFAULT_ADDRESS is 192.168.123.1 within the network region. */
2034     pxEndPoint = FreeRTOS_InterfaceEndPointOnNetMask( &xNetworkInterface, IPV4_DEFAULT_DNS_SERVER, 0 );
2035     TEST_ASSERT_EQUAL( NULL, pxEndPoint );
2036 }
2037 
2038 /**
2039  * @brief FreeRTOS_InterfaceEndPointOnNetMask returns first IPv4 endpoint when querying with IP address 0xFFFFFFFF.
2040  *
2041  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
2042  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
2043  *
2044  * Test step:
2045  *  - Create 2 endpoint (e0, e1) and add them to the list.
2046  *     - Set e0 IP address to 2001::1 (xDefaultIPAddress_IPv6).
2047  *     - Set e0 prefix address to 2001::.
2048  *     - Set e0 prefix length to 64.
2049  *     - Set e1 IP address to 192.168.123.223 (IPV4_DEFAULT_ADDRESS).
2050  *     - Set e1 netmask to 255.255.255.0.
2051  *  - Call FreeRTOS_InterfaceEndPointOnNetMask to query for IP address 0xFFFFFFFF.
2052  *  - Check if returned endpoint is e1.
2053  */
test_FreeRTOS_InterfaceEndPointOnNetMask_Broadcast(void)2054 void test_FreeRTOS_InterfaceEndPointOnNetMask_Broadcast( void )
2055 {
2056     NetworkEndPoint_t xEndPoint[ 2 ];
2057     NetworkEndPoint_t * pxEndPoint = NULL;
2058     NetworkInterface_t xNetworkInterface;
2059 
2060     /* Initialize network interface and add it to the list. */
2061     memset( &xNetworkInterface, 0, sizeof( NetworkInterface_t ) );
2062     pxNetworkInterfaces = &xNetworkInterface;
2063 
2064     /* Initialize e0. */
2065     memset( &xEndPoint[ 0 ], 0, sizeof( NetworkEndPoint_t ) );
2066     memcpy( xEndPoint[ 0 ].ipv6_settings.xIPAddress.ucBytes, &xDefaultIPAddress_IPv6.ucBytes, sizeof( IPv6_Address_t ) );
2067     memcpy( xEndPoint[ 0 ].ipv6_settings.xPrefix.ucBytes, &xDefaultNetPrefix_IPv6.ucBytes, sizeof( IPv6_Address_t ) );
2068     xEndPoint[ 0 ].ipv6_settings.uxPrefixLength = xDefaultPrefixLength;
2069     xEndPoint[ 0 ].bits.bIPv6 = pdTRUE;
2070     xEndPoint[ 0 ].pxNetworkInterface = &xNetworkInterface;
2071     pxNetworkEndPoints = &xEndPoint[ 0 ];
2072 
2073     /* Initialize e1. */
2074     memset( &xEndPoint[ 1 ], 0, sizeof( NetworkEndPoint_t ) );
2075     xEndPoint[ 1 ].ipv4_settings.ulIPAddress = IPV4_DEFAULT_ADDRESS;
2076     xEndPoint[ 1 ].ipv4_settings.ulNetMask = IPV4_DEFAULT_NETMASK;
2077     xEndPoint[ 1 ].pxNetworkInterface = &xNetworkInterface;
2078     pxNetworkEndPoints->pxNext = &xEndPoint[ 1 ];
2079 
2080     /* IPV4_DEFAULT_ADDRESS is 192.168.123.1 within the network region. */
2081     pxEndPoint = FreeRTOS_InterfaceEndPointOnNetMask( &xNetworkInterface, 0xFFFFFFFF, 0 );
2082     TEST_ASSERT_EQUAL( &xEndPoint[ 1 ], pxEndPoint );
2083 }
2084 
2085 /**
2086  * @brief FreeRTOS_FindGateWay should be able to find the endpoint with valid IPv4 gateway address.
2087  *
2088  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
2089  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
2090  *
2091  * Test step:
2092  *  - Create 1 IPv4 endpoint and add it to the list.
2093  *     - Set the gateway address to 192.168.123.254 (IPV4_DEFAULT_GATEWAY).
2094  *  - Call FreeRTOS_FindGateWay with ipTYPE_IPv4.
2095  *  - Check if returned endpoint is same.
2096  */
test_FreeRTOS_FindGateWay_IPv4_HappyPath(void)2097 void test_FreeRTOS_FindGateWay_IPv4_HappyPath( void )
2098 {
2099     NetworkEndPoint_t xEndPoint;
2100     NetworkEndPoint_t * pxEndPoint = NULL;
2101 
2102     /* Initialize network endpoint and add it to the list. */
2103     memset( &xEndPoint, 0, sizeof( NetworkEndPoint_t ) );
2104     xEndPoint.ipv4_settings.ulGatewayAddress = IPV4_DEFAULT_GATEWAY;
2105     pxNetworkEndPoints = &xEndPoint;
2106 
2107     pxEndPoint = FreeRTOS_FindGateWay( ipTYPE_IPv4 );
2108     TEST_ASSERT_EQUAL( &xEndPoint, pxEndPoint );
2109 }
2110 
2111 /**
2112  * @brief FreeRTOS_FindGateWay should be able to return NULL if no valid IPv4 gateway address.
2113  *
2114  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
2115  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
2116  *
2117  * Test step:
2118  *  - Create 1 IPv6 endpoint and add it to the list.
2119  *     - Set the gateway address to 2001::fffe (xDefaultGatewayAddress_IPv6).
2120  *  - Create 1 IPv4 endpoint and add it to the list.
2121  *     - Set the gateway address to 0 (invalid address).
2122  *  - Call FreeRTOS_FindGateWay with ipTYPE_IPv4.
2123  *  - Check if returned endpoint is same.
2124  */
test_FreeRTOS_FindGateWay_IPv4_NotFound(void)2125 void test_FreeRTOS_FindGateWay_IPv4_NotFound( void )
2126 {
2127     NetworkEndPoint_t xEndPoint[ 2 ];
2128     NetworkEndPoint_t * pxEndPoint = NULL;
2129 
2130     /* Initialize network endpoint and add it to the list. */
2131     memset( &xEndPoint[ 0 ], 0, sizeof( NetworkEndPoint_t ) );
2132     memcpy( xEndPoint[ 0 ].ipv6_settings.xGatewayAddress.ucBytes, &xDefaultGatewayAddress_IPv6.ucBytes, sizeof( IPv6_Address_t ) );
2133     xEndPoint[ 0 ].bits.bIPv6 = pdTRUE;
2134     pxNetworkEndPoints = &xEndPoint[ 0 ];
2135 
2136     /* Initialize network endpoint and add it to the list. */
2137     memset( &xEndPoint[ 1 ], 0, sizeof( NetworkEndPoint_t ) );
2138     pxNetworkEndPoints->pxNext = &xEndPoint[ 1 ];
2139 
2140     pxEndPoint = FreeRTOS_FindGateWay( ipTYPE_IPv4 );
2141     TEST_ASSERT_EQUAL( NULL, pxEndPoint );
2142 }
2143 
2144 /**
2145  * @brief FreeRTOS_FindGateWay should be able to return the endpoint with valid IPv4 gateway address.
2146  *
2147  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
2148  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
2149  *
2150  * Test step:
2151  *  - Create 1 IPv6 endpoint and add it to the list.
2152  *     - Set the gateway address to 2001::fffe (xDefaultGatewayAddress_IPv6).
2153  *  - Create 1 IPv4 endpoint and add it to the list.
2154  *     - Set the gateway address to 192.168.123.254 (IPV4_DEFAULT_GATEWAY).
2155  *  - Call FreeRTOS_FindGateWay with ipTYPE_IPv4.
2156  *  - Check if returned endpoint is same as second endpoint.
2157  */
test_FreeRTOS_FindGateWay_IPv4_MultipleEndpoints(void)2158 void test_FreeRTOS_FindGateWay_IPv4_MultipleEndpoints( void )
2159 {
2160     NetworkEndPoint_t xEndPointV4;
2161     NetworkEndPoint_t xEndPointV6;
2162     NetworkEndPoint_t * pxEndPoint = NULL;
2163 
2164     /* Initialize IPv6 network endpoint and add it to the list. */
2165     memset( &xEndPointV6, 0, sizeof( NetworkEndPoint_t ) );
2166     memcpy( xEndPointV6.ipv6_settings.xGatewayAddress.ucBytes, &xDefaultGatewayAddress_IPv6.ucBytes, sizeof( IPv6_Address_t ) );
2167     xEndPointV6.bits.bIPv6 = pdTRUE;
2168     pxNetworkEndPoints = &xEndPointV6;
2169 
2170     /* Initialize IPv4 network endpoint and add it to the list. */
2171     memset( &xEndPointV4, 0, sizeof( NetworkEndPoint_t ) );
2172     xEndPointV4.ipv4_settings.ulGatewayAddress = IPV4_DEFAULT_GATEWAY;
2173     pxNetworkEndPoints->pxNext = &xEndPointV4;
2174 
2175     pxEndPoint = FreeRTOS_FindGateWay( ipTYPE_IPv4 );
2176     TEST_ASSERT_EQUAL( &xEndPointV4, pxEndPoint );
2177 }
2178 
2179 /**
2180  * @brief FreeRTOS_FindGateWay should be able to find the endpoint with valid IPv6 gateway address.
2181  *
2182  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
2183  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
2184  *
2185  * Test step:
2186  *  - Create 1 IPv6 endpoint and add it to the list.
2187  *     - Set the gateway address to 2001::fffe (xDefaultGatewayAddress_IPv6).
2188  *  - Call FreeRTOS_FindGateWay with ipTYPE_IPv6.
2189  *  - Check if returned endpoint is same.
2190  */
test_FreeRTOS_FindGateWay_IPv6_HappyPath(void)2191 void test_FreeRTOS_FindGateWay_IPv6_HappyPath( void )
2192 {
2193     NetworkEndPoint_t xEndPoint;
2194     NetworkEndPoint_t * pxEndPoint = NULL;
2195 
2196     /* Initialize network endpoint and add it to the list. */
2197     memset( &xEndPoint, 0, sizeof( NetworkEndPoint_t ) );
2198     memcpy( xEndPoint.ipv6_settings.xGatewayAddress.ucBytes, &xDefaultGatewayAddress_IPv6.ucBytes, sizeof( IPv6_Address_t ) );
2199     xEndPoint.bits.bIPv6 = pdTRUE;
2200     pxNetworkEndPoints = &xEndPoint;
2201 
2202     pxEndPoint = FreeRTOS_FindGateWay( ipTYPE_IPv6 );
2203     TEST_ASSERT_EQUAL( &xEndPoint, pxEndPoint );
2204 }
2205 
2206 /**
2207  * @brief FreeRTOS_FindGateWay should be able to return NULL if no valid IPv6 gateway address.
2208  *
2209  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
2210  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
2211  *
2212  * Test step:
2213  *  - Create 1 IPv4 endpoint and add it to the list.
2214  *     - Set the gateway address to 192.168.123.254 (IPV4_DEFAULT_GATEWAY).
2215  *  - Create 1 IPv6 endpoint and add it to the list.
2216  *     - Set the gateway address to :: (invalid address).
2217  *  - Call FreeRTOS_FindGateWay with ipTYPE_IPv6.
2218  *  - Check if returned endpoint is same.
2219  */
test_FreeRTOS_FindGateWay_IPv6_NotFound(void)2220 void test_FreeRTOS_FindGateWay_IPv6_NotFound( void )
2221 {
2222     NetworkEndPoint_t xEndPoint[ 2 ];
2223     NetworkEndPoint_t * pxEndPoint = NULL;
2224 
2225     /* Initialize network endpoint and add it to the list. */
2226     memset( &xEndPoint[ 0 ], 0, sizeof( NetworkEndPoint_t ) );
2227     xEndPoint[ 0 ].ipv4_settings.ulGatewayAddress = IPV4_DEFAULT_GATEWAY;
2228     pxNetworkEndPoints = &xEndPoint[ 0 ];
2229 
2230     /* Initialize network endpoint and add it to the list. */
2231     memset( &xEndPoint[ 1 ], 0, sizeof( NetworkEndPoint_t ) );
2232     xEndPoint[ 0 ].bits.bIPv6 = pdTRUE;
2233     pxNetworkEndPoints->pxNext = &xEndPoint[ 1 ];
2234 
2235     pxEndPoint = FreeRTOS_FindGateWay( ipTYPE_IPv6 );
2236     TEST_ASSERT_EQUAL( NULL, pxEndPoint );
2237 }
2238 
2239 /**
2240  * @brief FreeRTOS_FindGateWay should be able to return the endpoint with valid IPv6 gateway address.
2241  *
2242  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
2243  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
2244  *
2245  * Test step:
2246  *  - Create 1 IPv4 endpoint and add it to the list.
2247  *     - Set the gateway address to 192.168.123.254 (IPV4_DEFAULT_GATEWAY).
2248  *  - Create 1 IPv6 endpoint and add it to the list.
2249  *     - Set the gateway address to 2001::fffe (xDefaultGatewayAddress_IPv6).
2250  *  - Call FreeRTOS_FindGateWay with ipTYPE_IPv4.
2251  *  - Check if returned endpoint is same as second endpoint.
2252  */
test_FreeRTOS_FindGateWay_IPv6_MultipleEndpoints(void)2253 void test_FreeRTOS_FindGateWay_IPv6_MultipleEndpoints( void )
2254 {
2255     NetworkEndPoint_t xEndPointV4;
2256     NetworkEndPoint_t xEndPointV6;
2257     NetworkEndPoint_t * pxEndPoint = NULL;
2258 
2259     /* Initialize IPv4 network endpoint and add it to the list. */
2260     memset( &xEndPointV4, 0, sizeof( NetworkEndPoint_t ) );
2261     xEndPointV4.ipv4_settings.ulGatewayAddress = IPV4_DEFAULT_GATEWAY;
2262     pxNetworkEndPoints = &xEndPointV4;
2263 
2264     /* Initialize IPv6 network endpoint and add it to the list. */
2265     memset( &xEndPointV6, 0, sizeof( NetworkEndPoint_t ) );
2266     memcpy( xEndPointV6.ipv6_settings.xGatewayAddress.ucBytes, &xDefaultGatewayAddress_IPv6.ucBytes, sizeof( IPv6_Address_t ) );
2267     xEndPointV6.bits.bIPv6 = pdTRUE;
2268     pxNetworkEndPoints->pxNext = &xEndPointV6;
2269 
2270     pxEndPoint = FreeRTOS_FindGateWay( ipTYPE_IPv6 );
2271     TEST_ASSERT_EQUAL( &xEndPointV6, pxEndPoint );
2272 }
2273 
2274 /**
2275  * @brief pxGetSocketEndpoint should be able to return the endpoint attached in socket handler.
2276  *
2277  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
2278  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
2279  *
2280  * Test step:
2281  *  - Create 1 endpoint and add it to the list.
2282  *  - Create 1 socket handler.
2283  *     - Attach previous endpoint to socket handler.
2284  *  - Call pxGetSocketEndpoint with socket handler.
2285  *  - Check if returned endpoint is same.
2286  */
test_pxGetSocketEndpoint_HappyPath()2287 void test_pxGetSocketEndpoint_HappyPath()
2288 {
2289     NetworkEndPoint_t xEndPoint;
2290     NetworkEndPoint_t * pxEndPoint = NULL;
2291     FreeRTOS_Socket_t xSocket;
2292 
2293     /* Initialize network endpoint and add it to the list. */
2294     memset( &xEndPoint, 0, sizeof( NetworkEndPoint_t ) );
2295     pxNetworkEndPoints = &xEndPoint;
2296 
2297     /* Initialize socket handler. */
2298     memset( &xSocket, 0, sizeof( FreeRTOS_Socket_t ) );
2299     xSocket.pxEndPoint = &xEndPoint;
2300 
2301     pxEndPoint = pxGetSocketEndpoint( &xSocket );
2302     TEST_ASSERT_EQUAL( &xEndPoint, pxEndPoint );
2303 }
2304 
2305 /**
2306  * @brief pxGetSocketEndpoint should be able to return NULL if socket handler is also NULL.
2307  *
2308  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
2309  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
2310  *
2311  * Test step:
2312  *  - Call pxGetSocketEndpoint with NULL.
2313  *  - Check if returned endpoint is NULL.
2314  */
test_pxGetSocketEndpoint_Null()2315 void test_pxGetSocketEndpoint_Null()
2316 {
2317     NetworkEndPoint_t * pxEndPoint = NULL;
2318 
2319     pxEndPoint = pxGetSocketEndpoint( NULL );
2320     TEST_ASSERT_EQUAL( NULL, pxEndPoint );
2321 }
2322 
2323 /**
2324  * @brief vSetSocketEndpoint can set endpoint to socket handler successfully.
2325  *
2326  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
2327  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
2328  *
2329  * Test step:
2330  *  - Create 1 endpoint and add it to the list.
2331  *  - Create 1 socket handler.
2332  *  - Call vSetSocketEndpoint to attach previous endpoint to this socket handler.
2333  *  - Check if the endpoint in socket handler is same.
2334  */
test_vSetSocketEndpoint_HappyPath()2335 void test_vSetSocketEndpoint_HappyPath()
2336 {
2337     NetworkEndPoint_t xEndPoint;
2338     FreeRTOS_Socket_t xSocket;
2339 
2340     /* Initialize network endpoint and add it to the list. */
2341     memset( &xEndPoint, 0, sizeof( NetworkEndPoint_t ) );
2342     pxNetworkEndPoints = &xEndPoint;
2343 
2344     /* Initialize socket handler. */
2345     memset( &xSocket, 0, sizeof( FreeRTOS_Socket_t ) );
2346 
2347     vSetSocketEndpoint( &xSocket, &xEndPoint );
2348 
2349     TEST_ASSERT_EQUAL( &xEndPoint, xSocket.pxEndPoint );
2350 }
2351 
2352 /**
2353  * @brief vSetSocketEndpoint can return normally when socket handler is NULL.
2354  *
2355  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
2356  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
2357  *
2358  * Test step:
2359  *  - Call vSetSocketEndpoint with NULL socket handler.
2360  */
test_vSetSocketEndpoint_NullSocket()2361 void test_vSetSocketEndpoint_NullSocket()
2362 {
2363     vSetSocketEndpoint( NULL, NULL );
2364 }
2365 
2366 /**
2367  * @brief pcEndpointName can get IPv4 address in string from endpoint.
2368  *
2369  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
2370  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
2371  *
2372  * Test step:
2373  *  - Create 1 endpoint.
2374  *     - Set the IP address to 192.168.123.223 (IPV4_DEFAULT_ADDRESS).
2375  *  - Call pcEndpointName with enough buffer size.
2376  *  - Check if return buffer string is "192.168.123.223".
2377  */
test_pcEndpointName_IPv4_HappyPath()2378 void test_pcEndpointName_IPv4_HappyPath()
2379 {
2380     NetworkEndPoint_t xEndPoint;
2381     FreeRTOS_Socket_t xSocket;
2382     char cIPString[] = "192.168.123.223";
2383     int lNameSize = sizeof( cIPString ) + 1;
2384     char cName[ lNameSize ];
2385     const char * pcName = NULL;
2386 
2387     /* Initialize network endpoint and add it to the list. */
2388     memset( &xEndPoint, 0, sizeof( NetworkEndPoint_t ) );
2389     xEndPoint.ipv4_settings.ulIPAddress = IPV4_DEFAULT_ADDRESS;
2390 
2391     memset( &cName, 0, sizeof( cName ) );
2392 
2393     xStubFreeRTOS_inet_ntop_TargetFamily = FREERTOS_AF_INET4;
2394     pvStubFreeRTOS_inet_ntop_TargetSource = &( xEndPoint.ipv4_settings.ulIPAddress );
2395     pcStubFreeRTOS_inet_ntop_TargetDestination = cName;
2396     ulStubFreeRTOS_inet_ntop_TargetSize = lNameSize;
2397     pcStubFreeRTOS_inet_ntop_TargetCopySource = cIPString;
2398     ulStubFreeRTOS_inet_ntop_CopySize = lNameSize;
2399     FreeRTOS_inet_ntop_Stub( pcStubFreeRTOS_inet_ntop );
2400 
2401     pcName = pcEndpointName( &xEndPoint, cName, lNameSize );
2402     TEST_ASSERT_EQUAL_MEMORY( pcName, cIPString, lNameSize );
2403 }
2404 
2405 /**
2406  * @brief pcEndpointName can return normally without accessing null pointer.
2407  *
2408  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
2409  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
2410  *
2411  * Test step:
2412  *  - Call pcEndpointName with null buffer pointer.
2413  */
test_pcEndpointName_IPv4_NullBufferPointer()2414 void test_pcEndpointName_IPv4_NullBufferPointer()
2415 {
2416     NetworkEndPoint_t xEndPoint;
2417     FreeRTOS_Socket_t xSocket;
2418     char cIPString[] = "192.168.123.223";
2419     int lNameSize = sizeof( cIPString ) + 1;
2420     const char * pcName = NULL;
2421 
2422     /* Initialize network endpoint and add it to the list. */
2423     memset( &xEndPoint, 0, sizeof( NetworkEndPoint_t ) );
2424     xEndPoint.ipv4_settings.ulIPAddress = IPV4_DEFAULT_ADDRESS;
2425 
2426     xStubFreeRTOS_inet_ntop_TargetFamily = FREERTOS_AF_INET4;
2427     pvStubFreeRTOS_inet_ntop_TargetSource = &( xEndPoint.ipv4_settings.ulIPAddress );
2428     pcStubFreeRTOS_inet_ntop_TargetDestination = NULL;
2429     ulStubFreeRTOS_inet_ntop_TargetSize = lNameSize;
2430     pcStubFreeRTOS_inet_ntop_TargetCopySource = NULL;
2431     ulStubFreeRTOS_inet_ntop_CopySize = 0;
2432     FreeRTOS_inet_ntop_Stub( pcStubFreeRTOS_inet_ntop );
2433 
2434     pcName = pcEndpointName( &xEndPoint, NULL, lNameSize );
2435     TEST_ASSERT_EQUAL( NULL, pcName );
2436 }
2437 
2438 /**
2439  * @brief pcEndpointName can get partial IPv4 address in string from endpoint when the buffer size is less than necessary size.
2440  *
2441  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
2442  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
2443  *
2444  * Test step:
2445  *  - Create 1 endpoint.
2446  *     - Set the IP address to 192.168.123.223 (IPV4_DEFAULT_ADDRESS).
2447  *  - Call pcEndpointName with ( enough buffer size - 3 ).
2448  *  - Check if return buffer string is "192.168.123.".
2449  */
test_pcEndpointName_IPv4_TruncateBuffer()2450 void test_pcEndpointName_IPv4_TruncateBuffer()
2451 {
2452     NetworkEndPoint_t xEndPoint;
2453     FreeRTOS_Socket_t xSocket;
2454     const char cIPString[] = "192.168.123.223";
2455     int lNameSize = sizeof( cIPString ) - 3;
2456     char cName[ lNameSize ];
2457     const char * pcName = NULL;
2458 
2459     /* Initialize network endpoint and add it to the list. */
2460     memset( &xEndPoint, 0, sizeof( NetworkEndPoint_t ) );
2461     xEndPoint.ipv4_settings.ulIPAddress = IPV4_DEFAULT_ADDRESS;
2462 
2463     memset( &cName, 0, sizeof( cName ) );
2464 
2465     xStubFreeRTOS_inet_ntop_TargetFamily = FREERTOS_AF_INET4;
2466     pvStubFreeRTOS_inet_ntop_TargetSource = &( xEndPoint.ipv4_settings.ulIPAddress );
2467     pcStubFreeRTOS_inet_ntop_TargetDestination = cName;
2468     ulStubFreeRTOS_inet_ntop_TargetSize = lNameSize;
2469     pcStubFreeRTOS_inet_ntop_TargetCopySource = cIPString;
2470     ulStubFreeRTOS_inet_ntop_CopySize = lNameSize;
2471     FreeRTOS_inet_ntop_Stub( pcStubFreeRTOS_inet_ntop );
2472 
2473     pcName = pcEndpointName( &xEndPoint, cName, lNameSize );
2474     TEST_ASSERT_EQUAL_MEMORY( pcName, cIPString, lNameSize );
2475 }
2476 
2477 /**
2478  * @brief pcEndpointName can get string "NULL" when the endpoint is NULL pointer.
2479  *
2480  * Test step:
2481  *  - Call pcEndpointName with NULL endpoint.
2482  *  - Check if return buffer string is "NULL".
2483  */
test_pcEndpointName_NullEndpoint()2484 void test_pcEndpointName_NullEndpoint()
2485 {
2486     int lNameSize = 5;
2487     char cName[ lNameSize ];
2488     const char * pcName = NULL;
2489 
2490     pcName = pcEndpointName( NULL, cName, lNameSize );
2491     TEST_ASSERT_EQUAL_MEMORY( pcName, "NULL", lNameSize );
2492 }
2493 
2494 /**
2495  * @brief pcEndpointName can get IPv6 address in string from endpoint.
2496  *
2497  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
2498  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
2499  *
2500  * Test step:
2501  *  - Create 1 endpoint.
2502  *     - Set the IP address to 2001::1 (xDefaultIPAddress_IPv6).
2503  *  - Call pcEndpointName with enough buffer size.
2504  *  - Check if return buffer string is "2001::1".
2505  */
test_pcEndpointName_IPv6_HappyPath()2506 void test_pcEndpointName_IPv6_HappyPath()
2507 {
2508     NetworkEndPoint_t xEndPoint;
2509     FreeRTOS_Socket_t xSocket;
2510     const char cIPString[] = "2001::1";
2511     int lNameSize = sizeof( cIPString ) + 1;
2512     char cName[ lNameSize ];
2513     const char * pcName;
2514 
2515     /* Initialize network endpoint and add it to the list. */
2516     memset( &xEndPoint, 0, sizeof( NetworkEndPoint_t ) );
2517     xEndPoint.bits.bIPv6 = pdTRUE;
2518     memcpy( xEndPoint.ipv6_settings.xIPAddress.ucBytes, &xDefaultIPAddress_IPv6.ucBytes, sizeof( IPv6_Address_t ) );
2519 
2520     memset( &cName, 0, sizeof( cName ) );
2521 
2522     xStubFreeRTOS_inet_ntop_TargetFamily = FREERTOS_AF_INET6;
2523     pvStubFreeRTOS_inet_ntop_TargetSource = xEndPoint.ipv6_settings.xIPAddress.ucBytes;
2524     pcStubFreeRTOS_inet_ntop_TargetDestination = cName;
2525     ulStubFreeRTOS_inet_ntop_TargetSize = lNameSize;
2526     pcStubFreeRTOS_inet_ntop_TargetCopySource = cIPString;
2527     ulStubFreeRTOS_inet_ntop_CopySize = lNameSize;
2528     FreeRTOS_inet_ntop_Stub( pcStubFreeRTOS_inet_ntop );
2529 
2530     pcName = pcEndpointName( &xEndPoint, cName, lNameSize );
2531     TEST_ASSERT_EQUAL_MEMORY( pcName, cIPString, lNameSize );
2532 }
2533 
2534 /**
2535  * @brief pcEndpointName can get partial IPv6 address in string from endpoint when the buffer size is less than necessary size.
2536  *
2537  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
2538  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
2539  *
2540  * Test step:
2541  *  - Create 1 endpoint.
2542  *     - Set the IP address to 2001::1 (xDefaultIPAddress_IPv6).
2543  *  - Call pcEndpointName with ( enough buffer size - 3 ).
2544  *  - Check if return buffer string is "2001".
2545  */
test_pcEndpointName_IPv6_TruncateBuffer()2546 void test_pcEndpointName_IPv6_TruncateBuffer()
2547 {
2548     NetworkEndPoint_t xEndPoint;
2549     FreeRTOS_Socket_t xSocket;
2550     const char cIPString[] = "2001::1";
2551     int lNameSize = sizeof( cIPString ) - 3;
2552     char cName[ lNameSize ];
2553     const char * pcName;
2554 
2555     /* Initialize network endpoint and add it to the list. */
2556     memset( &xEndPoint, 0, sizeof( NetworkEndPoint_t ) );
2557     xEndPoint.bits.bIPv6 = pdTRUE;
2558     memcpy( xEndPoint.ipv6_settings.xIPAddress.ucBytes, &xDefaultIPAddress_IPv6.ucBytes, sizeof( IPv6_Address_t ) );
2559 
2560     memset( &cName, 0, sizeof( cName ) );
2561 
2562     xStubFreeRTOS_inet_ntop_TargetFamily = FREERTOS_AF_INET6;
2563     pvStubFreeRTOS_inet_ntop_TargetSource = xEndPoint.ipv6_settings.xIPAddress.ucBytes;
2564     pcStubFreeRTOS_inet_ntop_TargetDestination = cName;
2565     ulStubFreeRTOS_inet_ntop_TargetSize = lNameSize;
2566     pcStubFreeRTOS_inet_ntop_TargetCopySource = cIPString;
2567     ulStubFreeRTOS_inet_ntop_CopySize = lNameSize;
2568     FreeRTOS_inet_ntop_Stub( pcStubFreeRTOS_inet_ntop );
2569 
2570     pcName = pcEndpointName( &xEndPoint, cName, lNameSize );
2571     TEST_ASSERT_EQUAL_MEMORY( pcName, cIPString, lNameSize );
2572 }
2573 
2574 /**
2575  * @brief xIPv6_GetIPType returns eIPv6_Global if input address matches 2000::/3.
2576  *
2577  * Test step:
2578  *  - Create 1 IPv6 address.
2579  *     - Set the IP address to 2001::1 (xDefaultIPAddress_IPv6).
2580  *  - Call xIPv6_GetIPType to check IP type.
2581  *  - Check if it returns eIPv6_Global.
2582  */
test_xIPv6_GetIPType_Global()2583 void test_xIPv6_GetIPType_Global()
2584 {
2585     IPv6_Type_t xReturn;
2586 
2587     xReturn = xIPv6_GetIPType( &xDefaultIPAddress_IPv6 );
2588     TEST_ASSERT_EQUAL( eIPv6_Global, xReturn );
2589 }
2590 
2591 /**
2592  * @brief xIPv6_GetIPType returns eIPv6_LinkLocal if input address matches FE80::/10.
2593  *
2594  * Test step:
2595  *  - Create 1 IPv6 address.
2596  *     - Set the IP address to FE8F::1:2.
2597  *  - Call xIPv6_GetIPType to check IP type.
2598  *  - Check if it returns eIPv6_LinkLocal.
2599  */
test_xIPv6_GetIPType_LinkLocal()2600 void test_xIPv6_GetIPType_LinkLocal()
2601 {
2602     const IPv6_Address_t xIPv6Address = { 0xFE, 0x8F, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x01, 0x02 };
2603     IPv6_Type_t xReturn;
2604 
2605     xReturn = xIPv6_GetIPType( &xIPv6Address );
2606     TEST_ASSERT_EQUAL( eIPv6_LinkLocal, xReturn );
2607 }
2608 
2609 /**
2610  * @brief xIPv6_GetIPType returns eIPv6_SiteLocal if input address matches FEC0::/10.
2611  *
2612  * Test step:
2613  *  - Create 1 IPv6 address.
2614  *     - Set the IP address to FECF::1:2.
2615  *  - Call xIPv6_GetIPType to check IP type.
2616  *  - Check if it returns eIPv6_SiteLocal.
2617  */
test_xIPv6_GetIPType_SiteLocal()2618 void test_xIPv6_GetIPType_SiteLocal()
2619 {
2620     const IPv6_Address_t xIPv6Address = { 0xFE, 0xCF, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x01, 0x02 };
2621     IPv6_Type_t xReturn;
2622 
2623     xReturn = xIPv6_GetIPType( &xIPv6Address );
2624     TEST_ASSERT_EQUAL( eIPv6_SiteLocal, xReturn );
2625 }
2626 
2627 /**
2628  * @brief xIPv6_GetIPType returns eIPv6_Multicast if input address matches FF00::/8.
2629  *
2630  * Test step:
2631  *  - Create 1 IPv6 address.
2632  *     - Set the IP address to FFFF::1:2.
2633  *  - Call xIPv6_GetIPType to check IP type.
2634  *  - Check if it returns eIPv6_Multicast.
2635  */
test_xIPv6_GetIPType_Multicast()2636 void test_xIPv6_GetIPType_Multicast()
2637 {
2638     const IPv6_Address_t xIPv6Address = { 0xFF, 0xFF, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x01, 0x02 };
2639     IPv6_Type_t xReturn;
2640 
2641     xReturn = xIPv6_GetIPType( &xIPv6Address );
2642     TEST_ASSERT_EQUAL( eIPv6_Multicast, xReturn );
2643 }
2644 
2645 /**
2646  * @brief xIPv6_GetIPType returns eIPv6_Unknown if input address doesn't match any rule.
2647  *
2648  * Test step:
2649  *  - Create 1 IPv6 address.
2650  *     - Set the IP address to 1234::1:2.
2651  *  - Call xIPv6_GetIPType to check IP type.
2652  *  - Check if it returns eIPv6_Unknown.
2653  */
test_xIPv6_GetIPType_Unknown()2654 void test_xIPv6_GetIPType_Unknown()
2655 {
2656     const IPv6_Address_t xIPv6Address = { 0x12, 0x34, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x01, 0x02 };
2657     IPv6_Type_t xReturn;
2658 
2659     xReturn = xIPv6_GetIPType( &xIPv6Address );
2660     TEST_ASSERT_EQUAL( eIPv6_Unknown, xReturn );
2661 }
2662 
2663 /**
2664  * @brief xIPv6_GetIPType returns eIPv6_Unknown if input address is NULL.
2665  *
2666  * Test step:
2667  *  - Call xIPv6_GetIPType with NULL input.
2668  *  - Check if it returns eIPv6_Unknown.
2669  */
test_xIPv6_GetIPType_Null()2670 void test_xIPv6_GetIPType_Null()
2671 {
2672     IPv6_Type_t xReturn;
2673 
2674     xReturn = xIPv6_GetIPType( NULL );
2675     TEST_ASSERT_EQUAL( eIPv6_Unknown, xReturn );
2676 }
2677 
2678 /**
2679  * @brief FreeRTOS_MatchingEndpoint returns the endpoint with same IPv4 address.
2680  *
2681  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
2682  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
2683  *
2684  * Test step:
2685  *  - Create 1 interface and 1 endpoint.
2686  *  - Put interface & endpoint into the list.
2687  *     - Assign 192.168.123.223 (IPV4_DEFAULT_ADDRESS) to the endpoint.
2688  *     - Assign ab:cd:ef:11:22:33 (ucDefaultMACAddress_IPv4) to the endpoint.
2689  *     - Attach endpoint to interface.
2690  *  - Create a network buffer and set IPv4 packet with destination address (IPV4_DEFAULT_ADDRESS),
2691  *    but different MAC address.
2692  *  - Call FreeRTOS_MatchingEndpoint and check if returned endpoint is same.
2693  */
test_FreeRTOS_MatchingEndpoint_MatchIPv4Address()2694 void test_FreeRTOS_MatchingEndpoint_MatchIPv4Address()
2695 {
2696     NetworkInterface_t xNetworkInterface;
2697     NetworkEndPoint_t xEndPoint;
2698     NetworkEndPoint_t * pxEndPoint = NULL;
2699     uint8_t * pcNetworkBuffer[ sizeof( ProtocolPacket_t ) + 4 ] __attribute__( ( aligned( 32 ) ) );
2700     ProtocolPacket_t * pxProtocolPacket = ( ProtocolPacket_t * ) ( ( uintptr_t ) ( pcNetworkBuffer ) + 2U );
2701     const uint8_t ucLocalMACAddress_IPv4[ ipMAC_ADDRESS_LENGTH_BYTES ] = { 0x11, 0x11, 0x11, 0x11, 0x11, 0x11 };
2702 
2703     /* Initialize network interface. */
2704     memset( &xNetworkInterface, 0, sizeof( NetworkInterface_t ) );
2705     pxNetworkInterfaces = &xNetworkInterface;
2706 
2707     /* Initialize endpoint. */
2708     memset( &xEndPoint, 0, sizeof( NetworkEndPoint_t ) );
2709     xEndPoint.ipv4_settings.ulIPAddress = IPV4_DEFAULT_ADDRESS;
2710     memcpy( xEndPoint.xMACAddress.ucBytes, ucDefaultMACAddress_IPv4, sizeof( ucDefaultMACAddress_IPv4 ) );
2711     xEndPoint.pxNetworkInterface = &xNetworkInterface;
2712     pxNetworkEndPoints = &xEndPoint;
2713 
2714     /* Initialize network buffer. */
2715     memset( pcNetworkBuffer, 0, sizeof( pcNetworkBuffer ) );
2716     /* Ethernet part. */
2717     memcpy( pxProtocolPacket->xTCPPacket.xEthernetHeader.xDestinationAddress.ucBytes, ucLocalMACAddress_IPv4, sizeof( ucLocalMACAddress_IPv4 ) );
2718     memcpy( pxProtocolPacket->xTCPPacket.xEthernetHeader.xSourceAddress.ucBytes, ucLocalMACAddress_IPv4, sizeof( ucLocalMACAddress_IPv4 ) );
2719     pxProtocolPacket->xTCPPacket.xEthernetHeader.usFrameType = ipIPv4_FRAME_TYPE;
2720     /* IP part. */
2721     pxProtocolPacket->xTCPPacket.xIPHeader.ulSourceIPAddress = IPV4_DEFAULT_ADDRESS;
2722     pxProtocolPacket->xTCPPacket.xIPHeader.ulDestinationIPAddress = IPV4_DEFAULT_ADDRESS;
2723 
2724     pxEndPoint = FreeRTOS_MatchingEndpoint( &xNetworkInterface, ( const uint8_t * ) ( pxProtocolPacket ) );
2725     TEST_ASSERT_EQUAL( &xEndPoint, pxEndPoint );
2726 }
2727 
2728 /**
2729  * @brief FreeRTOS_MatchingEndpoint returns the endpoint with same MAC address.
2730  *
2731  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
2732  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
2733  *
2734  * Test step:
2735  *  - Create 1 interface and 1 endpoint.
2736  *  - Put interface & endpoint into the list.
2737  *     - Assign 192.168.123.223 (IPV4_DEFAULT_ADDRESS) to the endpoint.
2738  *     - Assign ab:cd:ef:11:22:33 (ucDefaultMACAddress_IPv4) to the endpoint.
2739  *     - Attach endpoint to interface.
2740  *  - Create a network buffer and set IPv4 packet with destination MAC address (ucDefaultMACAddress_IPv4),
2741  *    but different IP address.
2742  *  - Call FreeRTOS_MatchingEndpoint and check if returned endpoint is same.
2743  */
test_FreeRTOS_MatchingEndpoint_MatchMACAddress()2744 void test_FreeRTOS_MatchingEndpoint_MatchMACAddress()
2745 {
2746     NetworkInterface_t xNetworkInterface;
2747     NetworkEndPoint_t xEndPoint;
2748     NetworkEndPoint_t * pxEndPoint = NULL;
2749     uint8_t * pcNetworkBuffer[ sizeof( ProtocolPacket_t ) + 4 ] __attribute__( ( aligned( 32 ) ) );
2750     ProtocolPacket_t * pxProtocolPacket = ( ProtocolPacket_t * ) ( ( uintptr_t ) ( pcNetworkBuffer ) + 2U );
2751 
2752     /* Initialize network interface. */
2753     memset( &xNetworkInterface, 0, sizeof( NetworkInterface_t ) );
2754     pxNetworkInterfaces = &xNetworkInterface;
2755 
2756     /* Initialize endpoint. */
2757     memset( &xEndPoint, 0, sizeof( NetworkEndPoint_t ) );
2758     xEndPoint.ipv4_settings.ulIPAddress = IPV4_DEFAULT_ADDRESS;
2759     memcpy( xEndPoint.xMACAddress.ucBytes, ucDefaultMACAddress_IPv4, sizeof( ucDefaultMACAddress_IPv4 ) );
2760     xEndPoint.pxNetworkInterface = &xNetworkInterface;
2761     pxNetworkEndPoints = &xEndPoint;
2762 
2763     /* Initialize network buffer. */
2764     memset( pcNetworkBuffer, 0, sizeof( pcNetworkBuffer ) );
2765     /* Ethernet part. */
2766     memcpy( pxProtocolPacket->xTCPPacket.xEthernetHeader.xDestinationAddress.ucBytes, ucDefaultMACAddress_IPv4, sizeof( ucDefaultMACAddress_IPv4 ) );
2767     memcpy( pxProtocolPacket->xTCPPacket.xEthernetHeader.xSourceAddress.ucBytes, ucDefaultMACAddress_IPv4, sizeof( ucDefaultMACAddress_IPv4 ) );
2768     pxProtocolPacket->xTCPPacket.xEthernetHeader.usFrameType = ipIPv4_FRAME_TYPE;
2769     /* IP part. */
2770     pxProtocolPacket->xTCPPacket.xIPHeader.ulSourceIPAddress = IPV4_DEFAULT_GATEWAY;
2771     pxProtocolPacket->xTCPPacket.xIPHeader.ulDestinationIPAddress = IPV4_DEFAULT_GATEWAY;
2772 
2773     pxEndPoint = FreeRTOS_MatchingEndpoint( &xNetworkInterface, ( const uint8_t * ) ( pxProtocolPacket ) );
2774     TEST_ASSERT_EQUAL( &xEndPoint, pxEndPoint );
2775 }
2776 
2777 /**
2778  * @brief FreeRTOS_MatchingEndpoint returns the endpoint with same IPv4 address in ARP request packet.
2779  *
2780  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
2781  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
2782  *
2783  * Test step:
2784  *  - Create 1 interface and 1 endpoint.
2785  *  - Put interface & endpoint into the list.
2786  *     - Assign 192.168.123.223 (IPV4_DEFAULT_ADDRESS) to the endpoint.
2787  *     - Assign ab:cd:ef:11:22:33 (ucDefaultMACAddress_IPv4) to the endpoint.
2788  *     - Attach endpoint to interface.
2789  *  - Create a network buffer and set ARP request packet with destination IP address (IPV4_DEFAULT_ADDRESS),
2790  *    but different MAC address.
2791  *  - Call FreeRTOS_MatchingEndpoint and check if returned endpoint is same.
2792  */
test_FreeRTOS_MatchingEndpoint_ARPReqMatchIPv4Address()2793 void test_FreeRTOS_MatchingEndpoint_ARPReqMatchIPv4Address()
2794 {
2795     NetworkInterface_t xNetworkInterface;
2796     NetworkEndPoint_t xEndPoint;
2797     NetworkEndPoint_t * pxEndPoint = NULL;
2798     uint8_t * pcNetworkBuffer[ sizeof( ProtocolPacket_t ) + 4 ] __attribute__( ( aligned( 32 ) ) );
2799     ProtocolPacket_t * pxProtocolPacket = ( ProtocolPacket_t * ) ( ( uintptr_t ) ( pcNetworkBuffer ) + 2U );
2800     uint32_t ulIPAddress = IPV4_DEFAULT_GATEWAY;
2801     const uint8_t ucLocalMACAddress_IPv4[ ipMAC_ADDRESS_LENGTH_BYTES ] = { 0x11, 0x11, 0x11, 0x11, 0x11, 0x11 };
2802 
2803     /* Initialize network interface. */
2804     memset( &xNetworkInterface, 0, sizeof( NetworkInterface_t ) );
2805     pxNetworkInterfaces = &xNetworkInterface;
2806 
2807     /* Initialize endpoint. */
2808     memset( &xEndPoint, 0, sizeof( NetworkEndPoint_t ) );
2809     xEndPoint.ipv4_settings.ulIPAddress = IPV4_DEFAULT_ADDRESS;
2810     memcpy( xEndPoint.xMACAddress.ucBytes, ucDefaultMACAddress_IPv4, sizeof( ucDefaultMACAddress_IPv4 ) );
2811     xEndPoint.pxNetworkInterface = &xNetworkInterface;
2812     pxNetworkEndPoints = &xEndPoint;
2813 
2814     /* Initialize network buffer. */
2815     memset( pcNetworkBuffer, 0, sizeof( pcNetworkBuffer ) );
2816     /* Ethernet part. */
2817     memcpy( pxProtocolPacket->xARPPacket.xEthernetHeader.xDestinationAddress.ucBytes, ucLocalMACAddress_IPv4, sizeof( ucLocalMACAddress_IPv4 ) );
2818     memcpy( pxProtocolPacket->xARPPacket.xEthernetHeader.xSourceAddress.ucBytes, ucLocalMACAddress_IPv4, sizeof( ucLocalMACAddress_IPv4 ) );
2819     pxProtocolPacket->xARPPacket.xEthernetHeader.usFrameType = ipARP_FRAME_TYPE;
2820     /* IP part. */
2821     pxProtocolPacket->xARPPacket.xARPHeader.usOperation = ipARP_REQUEST;
2822     memcpy( pxProtocolPacket->xARPPacket.xARPHeader.ucSenderProtocolAddress, &ulIPAddress, sizeof( pxProtocolPacket->xARPPacket.xARPHeader.ucSenderProtocolAddress ) );
2823     pxProtocolPacket->xARPPacket.xARPHeader.ulTargetProtocolAddress = IPV4_DEFAULT_ADDRESS;
2824 
2825     pxEndPoint = FreeRTOS_MatchingEndpoint( &xNetworkInterface, ( const uint8_t * ) ( pxProtocolPacket ) );
2826     TEST_ASSERT_EQUAL( &xEndPoint, pxEndPoint );
2827 }
2828 
2829 /**
2830  * @brief FreeRTOS_MatchingEndpoint returns the endpoint with same MAC address in ARP request packet.
2831  *
2832  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
2833  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
2834  *
2835  * Test step:
2836  *  - Create 1 interface and 1 endpoint.
2837  *  - Put interface & endpoint into the list.
2838  *     - Assign 192.168.123.223 (IPV4_DEFAULT_ADDRESS) to the endpoint.
2839  *     - Assign ab:cd:ef:11:22:33 (ucDefaultMACAddress_IPv4) to the endpoint.
2840  *     - Attach endpoint to interface.
2841  *  - Create a network buffer and set ARP request packet with destination MAC address (ucDefaultMACAddress_IPv4),
2842  *    but different IP address.
2843  *  - Call FreeRTOS_MatchingEndpoint and check if returned endpoint is same.
2844  */
test_FreeRTOS_MatchingEndpoint_ARPReqMatchMACAddress()2845 void test_FreeRTOS_MatchingEndpoint_ARPReqMatchMACAddress()
2846 {
2847     NetworkInterface_t xNetworkInterface;
2848     NetworkEndPoint_t xEndPoint;
2849     NetworkEndPoint_t * pxEndPoint = NULL;
2850     uint8_t * pcNetworkBuffer[ sizeof( ProtocolPacket_t ) + 4 ] __attribute__( ( aligned( 32 ) ) );
2851     ProtocolPacket_t * pxProtocolPacket = ( ProtocolPacket_t * ) ( ( uintptr_t ) ( pcNetworkBuffer ) + 2U );
2852     uint32_t ulIPAddress = IPV4_DEFAULT_ADDRESS;
2853 
2854     /* Initialize network interface. */
2855     memset( &xNetworkInterface, 0, sizeof( NetworkInterface_t ) );
2856     pxNetworkInterfaces = &xNetworkInterface;
2857 
2858     /* Initialize endpoint. */
2859     memset( &xEndPoint, 0, sizeof( NetworkEndPoint_t ) );
2860     xEndPoint.ipv4_settings.ulIPAddress = IPV4_DEFAULT_ADDRESS;
2861     memcpy( xEndPoint.xMACAddress.ucBytes, ucDefaultMACAddress_IPv4, sizeof( ucDefaultMACAddress_IPv4 ) );
2862     xEndPoint.pxNetworkInterface = &xNetworkInterface;
2863     pxNetworkEndPoints = &xEndPoint;
2864 
2865     /* Initialize network buffer. */
2866     memset( pcNetworkBuffer, 0, sizeof( pcNetworkBuffer ) );
2867     /* Ethernet part. */
2868     memcpy( pxProtocolPacket->xARPPacket.xEthernetHeader.xDestinationAddress.ucBytes, ucDefaultMACAddress_IPv4, sizeof( ucDefaultMACAddress_IPv4 ) );
2869     memcpy( pxProtocolPacket->xARPPacket.xEthernetHeader.xSourceAddress.ucBytes, ucDefaultMACAddress_IPv4, sizeof( ucDefaultMACAddress_IPv4 ) );
2870     pxProtocolPacket->xARPPacket.xEthernetHeader.usFrameType = ipARP_FRAME_TYPE;
2871     /* IP part. */
2872     pxProtocolPacket->xARPPacket.xARPHeader.usOperation = ipARP_REQUEST;
2873     memcpy( pxProtocolPacket->xARPPacket.xARPHeader.ucSenderProtocolAddress, &ulIPAddress, sizeof( pxProtocolPacket->xARPPacket.xARPHeader.ucSenderProtocolAddress ) );
2874     pxProtocolPacket->xARPPacket.xARPHeader.ulTargetProtocolAddress = IPV4_DEFAULT_GATEWAY;
2875 
2876     pxEndPoint = FreeRTOS_MatchingEndpoint( &xNetworkInterface, ( const uint8_t * ) ( pxProtocolPacket ) );
2877     TEST_ASSERT_EQUAL( &xEndPoint, pxEndPoint );
2878 }
2879 
2880 /**
2881  * @brief FreeRTOS_MatchingEndpoint returns the endpoint with same IPv4 address in ARP reply packet.
2882  *
2883  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
2884  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
2885  *
2886  * Test step:
2887  *  - Create 1 interface and 1 endpoint.
2888  *  - Put interface & endpoint into the list.
2889  *     - Assign 192.168.123.223 (IPV4_DEFAULT_ADDRESS) to the endpoint.
2890  *     - Assign ab:cd:ef:11:22:33 (ucDefaultMACAddress_IPv4) to the endpoint.
2891  *     - Attach endpoint to interface.
2892  *  - Create a network buffer and set ARP reply packet with destination IP address (IPV4_DEFAULT_ADDRESS),
2893  *    but different MAC address.
2894  *  - Call FreeRTOS_MatchingEndpoint and check if returned endpoint is same.
2895  */
test_FreeRTOS_MatchingEndpoint_ARPReplyMatchIPv4Address()2896 void test_FreeRTOS_MatchingEndpoint_ARPReplyMatchIPv4Address()
2897 {
2898     NetworkInterface_t xNetworkInterface;
2899     NetworkEndPoint_t xEndPoint;
2900     NetworkEndPoint_t * pxEndPoint = NULL;
2901     uint8_t * pcNetworkBuffer[ sizeof( ProtocolPacket_t ) + 4 ] __attribute__( ( aligned( 32 ) ) );
2902     ProtocolPacket_t * pxProtocolPacket = ( ProtocolPacket_t * ) ( ( uintptr_t ) ( pcNetworkBuffer ) + 2U );
2903     uint32_t ulIPAddress = IPV4_DEFAULT_ADDRESS;
2904     const uint8_t ucLocalMACAddress_IPv4[ ipMAC_ADDRESS_LENGTH_BYTES ] = { 0x11, 0x11, 0x11, 0x11, 0x11, 0x11 };
2905 
2906     /* Initialize network interface. */
2907     memset( &xNetworkInterface, 0, sizeof( NetworkInterface_t ) );
2908     pxNetworkInterfaces = &xNetworkInterface;
2909 
2910     /* Initialize endpoint. */
2911     memset( &xEndPoint, 0, sizeof( NetworkEndPoint_t ) );
2912     xEndPoint.ipv4_settings.ulIPAddress = IPV4_DEFAULT_ADDRESS;
2913     memcpy( xEndPoint.xMACAddress.ucBytes, ucDefaultMACAddress_IPv4, sizeof( ucDefaultMACAddress_IPv4 ) );
2914     xEndPoint.pxNetworkInterface = &xNetworkInterface;
2915     pxNetworkEndPoints = &xEndPoint;
2916 
2917     /* Initialize network buffer. */
2918     memset( pcNetworkBuffer, 0, sizeof( pcNetworkBuffer ) );
2919     /* Ethernet part. */
2920     memcpy( pxProtocolPacket->xARPPacket.xEthernetHeader.xDestinationAddress.ucBytes, ucLocalMACAddress_IPv4, sizeof( ucLocalMACAddress_IPv4 ) );
2921     memcpy( pxProtocolPacket->xARPPacket.xEthernetHeader.xSourceAddress.ucBytes, ucLocalMACAddress_IPv4, sizeof( ucLocalMACAddress_IPv4 ) );
2922     pxProtocolPacket->xARPPacket.xEthernetHeader.usFrameType = ipARP_FRAME_TYPE;
2923     /* IP part. */
2924     pxProtocolPacket->xARPPacket.xARPHeader.usOperation = ipARP_REPLY;
2925     memcpy( pxProtocolPacket->xARPPacket.xARPHeader.ucSenderProtocolAddress, &ulIPAddress, sizeof( pxProtocolPacket->xARPPacket.xARPHeader.ucSenderProtocolAddress ) );
2926     pxProtocolPacket->xARPPacket.xARPHeader.ulTargetProtocolAddress = IPV4_DEFAULT_GATEWAY;
2927 
2928     pxEndPoint = FreeRTOS_MatchingEndpoint( &xNetworkInterface, ( const uint8_t * ) ( pxProtocolPacket ) );
2929     TEST_ASSERT_EQUAL( &xEndPoint, pxEndPoint );
2930 }
2931 
2932 /**
2933  * @brief FreeRTOS_MatchingEndpoint returns the endpoint with same MAC address in ARP reply packet.
2934  *
2935  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
2936  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
2937  *
2938  * Test step:
2939  *  - Create 1 interface and 1 endpoint.
2940  *  - Put interface & endpoint into the list.
2941  *     - Assign 192.168.123.223 (IPV4_DEFAULT_ADDRESS) to the endpoint.
2942  *     - Assign ab:cd:ef:11:22:33 (ucDefaultMACAddress_IPv4) to the endpoint.
2943  *     - Attach endpoint to interface.
2944  *  - Create a network buffer and set ARP reply packet with destination MAC address (ucDefaultMACAddress_IPv4),
2945  *    but different IP address.
2946  *  - Call FreeRTOS_MatchingEndpoint and check if returned endpoint is same.
2947  */
test_FreeRTOS_MatchingEndpoint_ARPReplyMatchMACAddress()2948 void test_FreeRTOS_MatchingEndpoint_ARPReplyMatchMACAddress()
2949 {
2950     NetworkInterface_t xNetworkInterface;
2951     NetworkEndPoint_t xEndPoint;
2952     NetworkEndPoint_t * pxEndPoint = NULL;
2953     uint8_t * pcNetworkBuffer[ sizeof( ProtocolPacket_t ) + 4 ] __attribute__( ( aligned( 32 ) ) );
2954     ProtocolPacket_t * pxProtocolPacket = ( ProtocolPacket_t * ) ( ( uintptr_t ) ( pcNetworkBuffer ) + 2U );
2955     uint32_t ulIPAddress = IPV4_DEFAULT_GATEWAY;
2956 
2957     /* Initialize network interface. */
2958     memset( &xNetworkInterface, 0, sizeof( NetworkInterface_t ) );
2959     pxNetworkInterfaces = &xNetworkInterface;
2960 
2961     /* Initialize endpoint. */
2962     memset( &xEndPoint, 0, sizeof( NetworkEndPoint_t ) );
2963     xEndPoint.ipv4_settings.ulIPAddress = IPV4_DEFAULT_ADDRESS;
2964     memcpy( xEndPoint.xMACAddress.ucBytes, ucDefaultMACAddress_IPv4, sizeof( ucDefaultMACAddress_IPv4 ) );
2965     xEndPoint.pxNetworkInterface = &xNetworkInterface;
2966     pxNetworkEndPoints = &xEndPoint;
2967 
2968     /* Initialize network buffer. */
2969     memset( pcNetworkBuffer, 0, sizeof( pcNetworkBuffer ) );
2970     /* Ethernet part. */
2971     memcpy( pxProtocolPacket->xARPPacket.xEthernetHeader.xDestinationAddress.ucBytes, ucDefaultMACAddress_IPv4, sizeof( ucDefaultMACAddress_IPv4 ) );
2972     memcpy( pxProtocolPacket->xARPPacket.xEthernetHeader.xSourceAddress.ucBytes, ucDefaultMACAddress_IPv4, sizeof( ucDefaultMACAddress_IPv4 ) );
2973     pxProtocolPacket->xARPPacket.xEthernetHeader.usFrameType = ipARP_FRAME_TYPE;
2974     /* IP part. */
2975     pxProtocolPacket->xARPPacket.xARPHeader.usOperation = ipARP_REPLY;
2976     memcpy( pxProtocolPacket->xARPPacket.xARPHeader.ucSenderProtocolAddress, &ulIPAddress, sizeof( pxProtocolPacket->xARPPacket.xARPHeader.ucSenderProtocolAddress ) );
2977     pxProtocolPacket->xARPPacket.xARPHeader.ulTargetProtocolAddress = IPV4_DEFAULT_ADDRESS;
2978 
2979     pxEndPoint = FreeRTOS_MatchingEndpoint( &xNetworkInterface, ( const uint8_t * ) ( pxProtocolPacket ) );
2980     TEST_ASSERT_EQUAL( &xEndPoint, pxEndPoint );
2981 }
2982 
2983 /**
2984  * @brief FreeRTOS_MatchingEndpoint returns the IPv4 endpoint while receiving in ARP packet with invalid option code.
2985  *
2986  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
2987  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
2988  *
2989  * Test step:
2990  *  - Create 1 interface and 1 endpoint.
2991  *  - Put interface & endpoint into the list.
2992  *     - Assign 192.168.123.223 (IPV4_DEFAULT_ADDRESS) to the endpoint.
2993  *     - Assign ab:cd:ef:11:22:33 (ucDefaultMACAddress_IPv4) to the endpoint.
2994  *     - Attach endpoint to interface.
2995  *  - Create a network buffer and set ARP packet with invalid option code and
2996  *    destination IP address (IPV4_DEFAULT_ADDRESS), but different MAC address.
2997  *  - Call FreeRTOS_MatchingEndpoint and check if returned endpoint is same.
2998  */
test_FreeRTOS_MatchingEndpoint_ARPWrongOption()2999 void test_FreeRTOS_MatchingEndpoint_ARPWrongOption()
3000 {
3001     NetworkInterface_t xNetworkInterface;
3002     NetworkEndPoint_t xEndPoint;
3003     NetworkEndPoint_t * pxEndPoint = NULL;
3004     uint8_t * pcNetworkBuffer[ sizeof( ProtocolPacket_t ) + 4 ] __attribute__( ( aligned( 32 ) ) );
3005     ProtocolPacket_t * pxProtocolPacket = ( ProtocolPacket_t * ) ( ( uintptr_t ) ( pcNetworkBuffer ) + 2U );
3006     uint32_t ulIPAddress = IPV4_DEFAULT_ADDRESS;
3007     const uint8_t ucLocalMACAddress_IPv4[ ipMAC_ADDRESS_LENGTH_BYTES ] = { 0x11, 0x11, 0x11, 0x11, 0x11, 0x11 };
3008 
3009     /* Initialize network interface. */
3010     memset( &xNetworkInterface, 0, sizeof( NetworkInterface_t ) );
3011     pxNetworkInterfaces = &xNetworkInterface;
3012 
3013     /* Initialize endpoint. */
3014     memset( &xEndPoint, 0, sizeof( NetworkEndPoint_t ) );
3015     xEndPoint.ipv4_settings.ulIPAddress = IPV4_DEFAULT_ADDRESS;
3016     memcpy( xEndPoint.xMACAddress.ucBytes, ucDefaultMACAddress_IPv4, sizeof( ucDefaultMACAddress_IPv4 ) );
3017     xEndPoint.pxNetworkInterface = &xNetworkInterface;
3018     pxNetworkEndPoints = &xEndPoint;
3019 
3020     /* Initialize network buffer. */
3021     memset( pcNetworkBuffer, 0, sizeof( pcNetworkBuffer ) );
3022     /* Ethernet part. */
3023     memcpy( pxProtocolPacket->xARPPacket.xEthernetHeader.xDestinationAddress.ucBytes, ucLocalMACAddress_IPv4, sizeof( ucLocalMACAddress_IPv4 ) );
3024     memcpy( pxProtocolPacket->xARPPacket.xEthernetHeader.xSourceAddress.ucBytes, ucLocalMACAddress_IPv4, sizeof( ucLocalMACAddress_IPv4 ) );
3025     pxProtocolPacket->xARPPacket.xEthernetHeader.usFrameType = ipARP_FRAME_TYPE;
3026     /* IP part. */
3027     pxProtocolPacket->xARPPacket.xARPHeader.usOperation = ipARP_PROTOCOL_TYPE;
3028     memcpy( pxProtocolPacket->xARPPacket.xARPHeader.ucSenderProtocolAddress, &ulIPAddress, sizeof( pxProtocolPacket->xARPPacket.xARPHeader.ucSenderProtocolAddress ) );
3029     pxProtocolPacket->xARPPacket.xARPHeader.ulTargetProtocolAddress = IPV4_DEFAULT_GATEWAY;
3030 
3031     pxEndPoint = FreeRTOS_MatchingEndpoint( &xNetworkInterface, ( const uint8_t * ) ( pxProtocolPacket ) );
3032     TEST_ASSERT_EQUAL( &xEndPoint, pxEndPoint );
3033 }
3034 
3035 /**
3036  * @brief FreeRTOS_MatchingEndpoint returns the endpoint with same IPv4 address.
3037  *
3038  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
3039  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
3040  *
3041  * Test step:
3042  *  - Create 1 interface and 2 endpoints (e0, e1).
3043  *  - Put interface & endpoint into the list.
3044  *     - Assign 192.168.123.223 (IPV4_DEFAULT_ADDRESS) to e0 endpoint.
3045  *     - Assign ab:cd:ef:11:22:33 (ucDefaultMACAddress_IPv4) to e0 endpoint.
3046  *     - Assign 192.168.123.254 (IPV4_DEFAULT_GATEWAY) to e1 endpoint.
3047  *     - Assign ab:cd:ef:11:22:33 (ucDefaultMACAddress_IPv4) to e1 endpoint.
3048  *     - Attach e0 and e1 to interface.
3049  *  - Create a network buffer and IPv4 packet with destination IPv4 address (IPV4_DEFAULT_ADDRESS).
3050  *  - Call FreeRTOS_MatchingEndpoint with check if returned endpoint is e0.
3051  *  - Create a network buffer and IPv4 packet with destination IPv4 address (IPV4_DEFAULT_GATEWAY).
3052  *  - Call FreeRTOS_MatchingEndpoint with check if returned endpoint is e1.
3053  */
test_FreeRTOS_MatchingEndpoint_OneMACOneIPv4()3054 void test_FreeRTOS_MatchingEndpoint_OneMACOneIPv4()
3055 {
3056     NetworkInterface_t xNetworkInterface;
3057     NetworkEndPoint_t xEndPoint[ 2 ];
3058     NetworkEndPoint_t * pxEndPoint = NULL;
3059     uint8_t * pcNetworkBuffer[ sizeof( ProtocolPacket_t ) + 4 ] __attribute__( ( aligned( 32 ) ) );
3060     ProtocolPacket_t * pxProtocolPacket = ( ProtocolPacket_t * ) ( ( uintptr_t ) ( pcNetworkBuffer ) + 2U );
3061 
3062     /* Initialize network interface. */
3063     memset( &xNetworkInterface, 0, sizeof( NetworkInterface_t ) );
3064     pxNetworkInterfaces = &xNetworkInterface;
3065 
3066     /* Initialize endpoint e0. */
3067     memset( &xEndPoint[ 0 ], 0, sizeof( NetworkEndPoint_t ) );
3068     xEndPoint[ 0 ].ipv4_settings.ulIPAddress = IPV4_DEFAULT_ADDRESS;
3069     memcpy( xEndPoint[ 0 ].xMACAddress.ucBytes, ucDefaultMACAddress_IPv4, sizeof( ucDefaultMACAddress_IPv4 ) );
3070     xEndPoint[ 0 ].pxNetworkInterface = &xNetworkInterface;
3071     pxNetworkEndPoints = &xEndPoint[ 0 ];
3072 
3073     /* Initialize endpoint e1. */
3074     memset( &xEndPoint[ 1 ], 0, sizeof( NetworkEndPoint_t ) );
3075     xEndPoint[ 1 ].ipv4_settings.ulIPAddress = IPV4_DEFAULT_GATEWAY;
3076     memcpy( xEndPoint[ 1 ].xMACAddress.ucBytes, ucDefaultMACAddress_IPv4, sizeof( ucDefaultMACAddress_IPv4 ) );
3077     xEndPoint[ 1 ].pxNetworkInterface = &xNetworkInterface;
3078     /* Attach endpoint to the end of list. */
3079     pxNetworkEndPoints->pxNext = &xEndPoint[ 1 ];
3080 
3081     /* Initialize e0 network buffer. */
3082     memset( pcNetworkBuffer, 0, sizeof( pcNetworkBuffer ) );
3083     /* Ethernet part. */
3084     memcpy( pxProtocolPacket->xTCPPacket.xEthernetHeader.xDestinationAddress.ucBytes, ucDefaultMACAddress_IPv4, sizeof( ucDefaultMACAddress_IPv4 ) );
3085     memcpy( pxProtocolPacket->xTCPPacket.xEthernetHeader.xSourceAddress.ucBytes, ucDefaultMACAddress_IPv4, sizeof( ucDefaultMACAddress_IPv4 ) );
3086     pxProtocolPacket->xTCPPacket.xEthernetHeader.usFrameType = ipIPv4_FRAME_TYPE;
3087     /* IP part. */
3088     pxProtocolPacket->xTCPPacket.xIPHeader.ulSourceIPAddress = IPV4_DEFAULT_GATEWAY;
3089     pxProtocolPacket->xTCPPacket.xIPHeader.ulDestinationIPAddress = IPV4_DEFAULT_ADDRESS;
3090 
3091     /* Query for e0. */
3092     pxEndPoint = FreeRTOS_MatchingEndpoint( &xNetworkInterface, ( const uint8_t * ) ( pxProtocolPacket ) );
3093     TEST_ASSERT_EQUAL( &xEndPoint[ 0 ], pxEndPoint );
3094 
3095     /* Initialize e1 network buffer. */
3096     memset( pcNetworkBuffer, 0, sizeof( pcNetworkBuffer ) );
3097     /* Ethernet part. */
3098     memcpy( pxProtocolPacket->xTCPPacket.xEthernetHeader.xDestinationAddress.ucBytes, ucDefaultMACAddress_IPv4, sizeof( ucDefaultMACAddress_IPv4 ) );
3099     memcpy( pxProtocolPacket->xTCPPacket.xEthernetHeader.xSourceAddress.ucBytes, ucDefaultMACAddress_IPv4, sizeof( ucDefaultMACAddress_IPv4 ) );
3100     pxProtocolPacket->xTCPPacket.xEthernetHeader.usFrameType = ipIPv4_FRAME_TYPE;
3101     /* IP part. */
3102     pxProtocolPacket->xTCPPacket.xIPHeader.ulSourceIPAddress = IPV4_DEFAULT_ADDRESS;
3103     pxProtocolPacket->xTCPPacket.xIPHeader.ulDestinationIPAddress = IPV4_DEFAULT_GATEWAY;
3104 
3105     /* Query for e1. */
3106     pxEndPoint = FreeRTOS_MatchingEndpoint( &xNetworkInterface, ( const uint8_t * ) ( pxProtocolPacket ) );
3107     TEST_ASSERT_EQUAL( &xEndPoint[ 1 ], pxEndPoint );
3108 }
3109 
3110 /**
3111  * @brief FreeRTOS_MatchingEndpoint returns NULL when input interface is NULL.
3112  *
3113  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
3114  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
3115  *
3116  * Test step:
3117  *  - Create 1 interface and 1 endpoint.
3118  *  - Put interface & endpoint into the list.
3119  *     - Assign 192.168.123.223 (IPV4_DEFAULT_ADDRESS) to the endpoint.
3120  *     - Assign ab:cd:ef:11:22:33 (ucDefaultMACAddress_IPv4) to the endpoint.
3121  *     - Attach endpoint to interface.
3122  *  - Create a network buffer and set IPv4 packet with destination IP address (IPV4_DEFAULT_ADDRESS).
3123  *  - Call FreeRTOS_MatchingEndpoint with check if returned endpoint is same.
3124  */
test_FreeRTOS_MatchingEndpoint_NullInterface()3125 void test_FreeRTOS_MatchingEndpoint_NullInterface()
3126 {
3127     NetworkInterface_t xNetworkInterface;
3128     NetworkEndPoint_t xEndPoint;
3129     NetworkEndPoint_t * pxEndPoint = NULL;
3130     uint8_t * pcNetworkBuffer[ sizeof( ProtocolPacket_t ) + 4 ] __attribute__( ( aligned( 32 ) ) );
3131     ProtocolPacket_t * pxProtocolPacket = ( ProtocolPacket_t * ) ( ( uintptr_t ) ( pcNetworkBuffer ) + 2U );
3132     const uint8_t ucLocalMACAddress_IPv4[ ipMAC_ADDRESS_LENGTH_BYTES ] = { 0x11, 0x11, 0x11, 0x11, 0x11, 0x11 };
3133 
3134     /* Initialize network interface. */
3135     memset( &xNetworkInterface, 0, sizeof( NetworkInterface_t ) );
3136     pxNetworkInterfaces = &xNetworkInterface;
3137 
3138     /* Initialize endpoint. */
3139     memset( &xEndPoint, 0, sizeof( NetworkEndPoint_t ) );
3140     xEndPoint.ipv4_settings.ulIPAddress = IPV4_DEFAULT_ADDRESS;
3141     memcpy( xEndPoint.xMACAddress.ucBytes, ucDefaultMACAddress_IPv4, sizeof( ucDefaultMACAddress_IPv4 ) );
3142     xEndPoint.pxNetworkInterface = &xNetworkInterface;
3143     pxNetworkEndPoints = &xEndPoint;
3144 
3145     /* Initialize network buffer. */
3146     memset( pcNetworkBuffer, 0, sizeof( pcNetworkBuffer ) );
3147     /* Ethernet part. */
3148     memcpy( pxProtocolPacket->xTCPPacket.xEthernetHeader.xDestinationAddress.ucBytes, ucLocalMACAddress_IPv4, sizeof( ucLocalMACAddress_IPv4 ) );
3149     memcpy( pxProtocolPacket->xTCPPacket.xEthernetHeader.xSourceAddress.ucBytes, ucLocalMACAddress_IPv4, sizeof( ucLocalMACAddress_IPv4 ) );
3150     pxProtocolPacket->xTCPPacket.xEthernetHeader.usFrameType = ipIPv4_FRAME_TYPE;
3151     /* IP part. */
3152     pxProtocolPacket->xTCPPacket.xIPHeader.ulSourceIPAddress = IPV4_DEFAULT_ADDRESS;
3153     pxProtocolPacket->xTCPPacket.xIPHeader.ulDestinationIPAddress = IPV4_DEFAULT_ADDRESS;
3154 
3155     pxEndPoint = FreeRTOS_MatchingEndpoint( NULL, ( const uint8_t * ) ( pxProtocolPacket ) );
3156     TEST_ASSERT_EQUAL( &xEndPoint, pxEndPoint );
3157 }
3158 
3159 /**
3160  * @brief FreeRTOS_MatchingEndpoint returns NULL when there is no matching IPv4 endpoint.
3161  *
3162  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
3163  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
3164  *
3165  * Test step:
3166  *  - Create 1 interface and 1 endpoint.
3167  *  - Put interface & endpoint into the list.
3168  *     - Assign 2001::1 (xDefaultIPAddress_IPv6) to the endpoint.
3169  *     - Assign 11:22:33:ab:cd:ef (ucDefaultMACAddress_IPv6) to the endpoint.
3170  *     - Attach endpoint to interface.
3171  *  - Create a network buffer and set IPv4 packet with different destination IPv4/MAC address.
3172  *  - Call FreeRTOS_MatchingEndpoint and check if returned endpoint is NULL.
3173  */
test_FreeRTOS_MatchingEndpoint_IPv4NotFound()3174 void test_FreeRTOS_MatchingEndpoint_IPv4NotFound()
3175 {
3176     NetworkInterface_t xNetworkInterface;
3177     NetworkEndPoint_t xEndPoint;
3178     NetworkEndPoint_t * pxEndPoint = NULL;
3179     uint8_t * pcNetworkBuffer[ sizeof( ProtocolPacket_t ) + 4 ] __attribute__( ( aligned( 32 ) ) );
3180     ProtocolPacket_t * pxProtocolPacket = ( ProtocolPacket_t * ) ( ( uintptr_t ) ( pcNetworkBuffer ) + 2U );
3181     const uint8_t ucLocalMACAddress_IPv4[ ipMAC_ADDRESS_LENGTH_BYTES ] = { 0x11, 0x11, 0x11, 0x11, 0x11, 0x11 };
3182 
3183     /* Initialize network interface. */
3184     memset( &xNetworkInterface, 0, sizeof( NetworkInterface_t ) );
3185     pxNetworkInterfaces = &xNetworkInterface;
3186 
3187     /* Initialize endpoint. */
3188     memset( &xEndPoint, 0, sizeof( NetworkEndPoint_t ) );
3189     xEndPoint.bits.bIPv6 = pdTRUE;
3190     memcpy( xEndPoint.ipv6_settings.xIPAddress.ucBytes, &xDefaultIPAddress_IPv6.ucBytes, sizeof( IPv6_Address_t ) );
3191     memcpy( xEndPoint.xMACAddress.ucBytes, ucDefaultMACAddress_IPv6, sizeof( ucDefaultMACAddress_IPv6 ) );
3192     xEndPoint.pxNetworkInterface = &xNetworkInterface;
3193     pxNetworkEndPoints = &xEndPoint;
3194 
3195     /* Initialize network buffer. */
3196     memset( pcNetworkBuffer, 0, sizeof( pcNetworkBuffer ) );
3197     /* Ethernet part. */
3198     memcpy( pxProtocolPacket->xTCPPacket.xEthernetHeader.xDestinationAddress.ucBytes, ucLocalMACAddress_IPv4, sizeof( ucLocalMACAddress_IPv4 ) );
3199     memcpy( pxProtocolPacket->xTCPPacket.xEthernetHeader.xSourceAddress.ucBytes, ucLocalMACAddress_IPv4, sizeof( ucLocalMACAddress_IPv4 ) );
3200     pxProtocolPacket->xTCPPacket.xEthernetHeader.usFrameType = ipIPv4_FRAME_TYPE;
3201     /* IP part. */
3202     pxProtocolPacket->xTCPPacket.xIPHeader.ulSourceIPAddress = IPV4_DEFAULT_ADDRESS;
3203     pxProtocolPacket->xTCPPacket.xIPHeader.ulDestinationIPAddress = IPV4_DEFAULT_ADDRESS;
3204 
3205     /* FreeRTOS_inet_ntop is used to print for debugging. Ignore it here. */
3206     FreeRTOS_inet_ntop_IgnoreAndReturn( NULL );
3207     FreeRTOS_inet_ntop_IgnoreAndReturn( NULL );
3208 
3209     pxEndPoint = FreeRTOS_MatchingEndpoint( &xNetworkInterface, ( const uint8_t * ) ( pxProtocolPacket ) );
3210     TEST_ASSERT_EQUAL( NULL, pxEndPoint );
3211 }
3212 
3213 /**
3214  * @brief FreeRTOS_MatchingEndpoint returns the endpoint with same IPv6 address.
3215  *
3216  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
3217  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
3218  *
3219  * Test step:
3220  *  - Create 1 interface and 1 endpoint.
3221  *  - Put interface & endpoint into the list.
3222  *     - Assign 2001::1 (xDefaultIPAddress_IPv6) to the endpoint.
3223  *     - Assign 11:22:33:ab:cd:ef (ucDefaultMACAddress_IPv6) to the endpoint.
3224  *     - Attach endpoint to interface.
3225  *  - Create a network buffer and set IPv6 packet with destination address (xDefaultIPAddress_IPv6),
3226  *    but different MAC address.
3227  *  - Call FreeRTOS_MatchingEndpoint and check if returned endpoint is same.
3228  */
test_FreeRTOS_MatchingEndpoint_MatchIPv6Address()3229 void test_FreeRTOS_MatchingEndpoint_MatchIPv6Address()
3230 {
3231     NetworkInterface_t xNetworkInterface;
3232     NetworkEndPoint_t xEndPoint;
3233     NetworkEndPoint_t * pxEndPoint = NULL;
3234     uint8_t * pcNetworkBuffer[ sizeof( TCPPacket_IPv6_t ) + 4 ] __attribute__( ( aligned( 32 ) ) );
3235     TCPPacket_IPv6_t * pxTCPPacket = ( TCPPacket_IPv6_t * ) ( ( uintptr_t ) ( pcNetworkBuffer ) + 2U );
3236     const uint8_t ucLocalMACAddress_IPv6[ ipMAC_ADDRESS_LENGTH_BYTES ] = { 0x11, 0x11, 0x11, 0x11, 0x11, 0x11 };
3237 
3238     /* Initialize network interface. */
3239     memset( &xNetworkInterface, 0, sizeof( NetworkInterface_t ) );
3240     pxNetworkInterfaces = &xNetworkInterface;
3241 
3242     /* Initialize endpoint. */
3243     memset( &xEndPoint, 0, sizeof( NetworkEndPoint_t ) );
3244     xEndPoint.bits.bIPv6 = pdTRUE;
3245     memcpy( xEndPoint.ipv6_settings.xIPAddress.ucBytes, xDefaultIPAddress_IPv6.ucBytes, sizeof( IPv6_Address_t ) );
3246     memcpy( xEndPoint.xMACAddress.ucBytes, ucDefaultMACAddress_IPv6, sizeof( ucDefaultMACAddress_IPv6 ) );
3247     xEndPoint.pxNetworkInterface = &xNetworkInterface;
3248     pxNetworkEndPoints = &xEndPoint;
3249 
3250     /* Initialize network buffer. */
3251     memset( pcNetworkBuffer, 0, sizeof( pcNetworkBuffer ) );
3252     /* Ethernet part. */
3253     memcpy( pxTCPPacket->xEthernetHeader.xDestinationAddress.ucBytes, ucLocalMACAddress_IPv6, sizeof( ucLocalMACAddress_IPv6 ) );
3254     memcpy( pxTCPPacket->xEthernetHeader.xSourceAddress.ucBytes, ucLocalMACAddress_IPv6, sizeof( ucLocalMACAddress_IPv6 ) );
3255     pxTCPPacket->xEthernetHeader.usFrameType = ipIPv6_FRAME_TYPE;
3256     /* IP part. */
3257     memcpy( pxTCPPacket->xIPHeader.xSourceAddress.ucBytes, xDefaultIPAddress_IPv6.ucBytes, sizeof( IPv6_Address_t ) );
3258     memcpy( pxTCPPacket->xIPHeader.xDestinationAddress.ucBytes, xDefaultIPAddress_IPv6.ucBytes, sizeof( IPv6_Address_t ) );
3259 
3260     pxEndPoint = FreeRTOS_MatchingEndpoint( &xNetworkInterface, ( const uint8_t * ) ( pxTCPPacket ) );
3261     TEST_ASSERT_EQUAL( &xEndPoint, pxEndPoint );
3262 }
3263 
3264 /**
3265  * @brief FreeRTOS_MatchingEndpoint returns NULL when there is no matching IPv6 endpoint.
3266  *
3267  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
3268  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
3269  *
3270  * Test step:
3271  *  - Create 1 interface and 1 endpoint.
3272  *  - Put interface & endpoint into the list.
3273  *     - Assign 192.168.123.223 (IPV4_DEFAULT_ADDRESS) to the endpoint.
3274  *     - Assign ab:cd:ef:11:22:33 (ucDefaultMACAddress_IPv4) to the endpoint.
3275  *     - Attach endpoint to interface.
3276  *  - Create a network buffer and IPv6 packet with destination IPv6/MAC address (xDefaultIPAddress_IPv6).
3277  *  - Call FreeRTOS_MatchingEndpoint with check if returned endpoint is NULL.
3278  */
test_FreeRTOS_MatchingEndpoint_IPv6NotFound()3279 void test_FreeRTOS_MatchingEndpoint_IPv6NotFound()
3280 {
3281     NetworkInterface_t xNetworkInterface;
3282     NetworkEndPoint_t xEndPoint;
3283     NetworkEndPoint_t * pxEndPoint = NULL;
3284     uint8_t * pcNetworkBuffer[ sizeof( TCPPacket_IPv6_t ) + 4 ] __attribute__( ( aligned( 32 ) ) );
3285     TCPPacket_IPv6_t * pxTCPPacket = ( TCPPacket_IPv6_t * ) ( ( uintptr_t ) ( pcNetworkBuffer ) + 2U );
3286     const uint8_t ucLocalMACAddress_IPv6[ ipMAC_ADDRESS_LENGTH_BYTES ] = { 0x11, 0x11, 0x11, 0x11, 0x11, 0x11 };
3287 
3288     /* Initialize network interface. */
3289     memset( &xNetworkInterface, 0, sizeof( NetworkInterface_t ) );
3290     pxNetworkInterfaces = &xNetworkInterface;
3291 
3292     /* Initialize endpoint. */
3293     memset( &xEndPoint, 0, sizeof( NetworkEndPoint_t ) );
3294     xEndPoint.ipv4_settings.ulIPAddress = IPV4_DEFAULT_ADDRESS;
3295     memcpy( xEndPoint.xMACAddress.ucBytes, ucDefaultMACAddress_IPv4, sizeof( ucDefaultMACAddress_IPv4 ) );
3296     xEndPoint.pxNetworkInterface = &xNetworkInterface;
3297     pxNetworkEndPoints = &xEndPoint;
3298 
3299     /* Initialize network buffer. */
3300     memset( pcNetworkBuffer, 0, sizeof( pcNetworkBuffer ) );
3301     /* Ethernet part. */
3302     memcpy( pxTCPPacket->xEthernetHeader.xDestinationAddress.ucBytes, ucDefaultMACAddress_IPv6, sizeof( ucDefaultMACAddress_IPv6 ) );
3303     memcpy( pxTCPPacket->xEthernetHeader.xSourceAddress.ucBytes, ucDefaultMACAddress_IPv6, sizeof( ucDefaultMACAddress_IPv6 ) );
3304     pxTCPPacket->xEthernetHeader.usFrameType = ipIPv6_FRAME_TYPE;
3305     /* IP part. */
3306     memcpy( pxTCPPacket->xIPHeader.xSourceAddress.ucBytes, xDefaultIPAddress_IPv6.ucBytes, sizeof( IPv6_Address_t ) );
3307     memcpy( pxTCPPacket->xIPHeader.xDestinationAddress.ucBytes, xDefaultIPAddress_IPv6.ucBytes, sizeof( IPv6_Address_t ) );
3308 
3309     /* FreeRTOS_inet_ntop is used to print for debugging. Ignore it here. */
3310     FreeRTOS_inet_ntop_IgnoreAndReturn( "2001::1" );
3311     FreeRTOS_inet_ntop_IgnoreAndReturn( "2001::1" );
3312 
3313     pxEndPoint = FreeRTOS_MatchingEndpoint( &xNetworkInterface, ( const uint8_t * ) ( pxTCPPacket ) );
3314     TEST_ASSERT_EQUAL( NULL, pxEndPoint );
3315 }
3316 
3317 /**
3318  * @brief FreeRTOS_MatchingEndpoint returns the endpoint with same IPv6 address.
3319  *
3320  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
3321  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
3322  *
3323  * Test step:
3324  *  - Create 1 interface and 2 endpoints (e0, e1).
3325  *  - Put interface & endpoint into the list.
3326  *     - Assign 2001::1 (xDefaultIPAddress_IPv6) to e0 endpoint.
3327  *     - Assign 11:22:33:ab:cd:ef (ucDefaultMACAddress_IPv6) to e0 endpoint.
3328  *     - Assign 2001::fffe (xDefaultGatewayAddress_IPv6) to e1 endpoint.
3329  *     - Assign 11:11:11:11:11:11 to e1 endpoint.
3330  *     - Attach e0 and e1 to interface.
3331  *  - Create a network buffer and IPv4 packet with destination IPv4 address (xDefaultIPAddress_IPv6).
3332  *  - Call FreeRTOS_MatchingEndpoint with check if returned endpoint is e0.
3333  *  - Create a network buffer and IPv4 packet with destination IPv4 address (xDefaultGatewayAddress_IPv6).
3334  *  - Call FreeRTOS_MatchingEndpoint with check if returned endpoint is e1.
3335  */
test_FreeRTOS_MatchingEndpoint_OneMACOneIPv6()3336 void test_FreeRTOS_MatchingEndpoint_OneMACOneIPv6()
3337 {
3338     NetworkInterface_t xNetworkInterface;
3339     NetworkEndPoint_t xEndPoint[ 2 ];
3340     NetworkEndPoint_t * pxEndPoint = NULL;
3341     uint8_t * pcNetworkBuffer[ sizeof( TCPPacket_IPv6_t ) + 4 ] __attribute__( ( aligned( 32 ) ) );
3342     TCPPacket_IPv6_t * pxTCPPacket = ( TCPPacket_IPv6_t * ) ( ( uintptr_t ) ( pcNetworkBuffer ) + 2U );
3343     const uint8_t ucLocalMACAddress_IPv6[ ipMAC_ADDRESS_LENGTH_BYTES ] = { 0x11, 0x11, 0x11, 0x11, 0x11, 0x11 };
3344 
3345     /* Initialize network interface. */
3346     memset( &xNetworkInterface, 0, sizeof( NetworkInterface_t ) );
3347     pxNetworkInterfaces = &xNetworkInterface;
3348 
3349     /* Initialize endpoint e0. */
3350     memset( &xEndPoint[ 0 ], 0, sizeof( NetworkEndPoint_t ) );
3351     xEndPoint[ 0 ].bits.bIPv6 = pdTRUE;
3352     memcpy( xEndPoint[ 0 ].ipv6_settings.xIPAddress.ucBytes, xDefaultIPAddress_IPv6.ucBytes, sizeof( IPv6_Address_t ) );
3353     memcpy( xEndPoint[ 0 ].xMACAddress.ucBytes, ucDefaultMACAddress_IPv6, sizeof( ucDefaultMACAddress_IPv6 ) );
3354     xEndPoint[ 0 ].pxNetworkInterface = &xNetworkInterface;
3355     pxNetworkEndPoints = &xEndPoint[ 0 ];
3356 
3357     /* Initialize endpoint e1. */
3358     memset( &xEndPoint[ 1 ], 0, sizeof( NetworkEndPoint_t ) );
3359     xEndPoint[ 1 ].bits.bIPv6 = pdTRUE;
3360     memcpy( xEndPoint[ 1 ].ipv6_settings.xIPAddress.ucBytes, xDefaultGatewayAddress_IPv6.ucBytes, sizeof( IPv6_Address_t ) );
3361     memcpy( xEndPoint[ 1 ].xMACAddress.ucBytes, ucDefaultMACAddress_IPv6, sizeof( ucDefaultMACAddress_IPv6 ) );
3362     xEndPoint[ 1 ].pxNetworkInterface = &xNetworkInterface;
3363     /* Attach endpoint to the end of list. */
3364     pxNetworkEndPoints->pxNext = &xEndPoint[ 1 ];
3365 
3366     /* Initialize e0 network buffer. */
3367     memset( pcNetworkBuffer, 0, sizeof( pcNetworkBuffer ) );
3368     /* Ethernet part. */
3369     memcpy( pxTCPPacket->xEthernetHeader.xDestinationAddress.ucBytes, ucLocalMACAddress_IPv6, sizeof( ucLocalMACAddress_IPv6 ) );
3370     memcpy( pxTCPPacket->xEthernetHeader.xSourceAddress.ucBytes, ucLocalMACAddress_IPv6, sizeof( ucLocalMACAddress_IPv6 ) );
3371     pxTCPPacket->xEthernetHeader.usFrameType = ipIPv6_FRAME_TYPE;
3372     /* IP part. */
3373     memcpy( pxTCPPacket->xIPHeader.xSourceAddress.ucBytes, xDefaultGatewayAddress_IPv6.ucBytes, sizeof( IPv6_Address_t ) );
3374     memcpy( pxTCPPacket->xIPHeader.xDestinationAddress.ucBytes, xDefaultIPAddress_IPv6.ucBytes, sizeof( IPv6_Address_t ) );
3375 
3376     /* Query for e0. */
3377     pxEndPoint = FreeRTOS_MatchingEndpoint( &xNetworkInterface, ( const uint8_t * ) ( pxTCPPacket ) );
3378     TEST_ASSERT_EQUAL( &xEndPoint[ 0 ], pxEndPoint );
3379 
3380     /* Initialize e1 network buffer. */
3381     memset( pcNetworkBuffer, 0, sizeof( pcNetworkBuffer ) );
3382     /* Ethernet part. */
3383     memcpy( pxTCPPacket->xEthernetHeader.xDestinationAddress.ucBytes, ucLocalMACAddress_IPv6, sizeof( ucLocalMACAddress_IPv6 ) );
3384     memcpy( pxTCPPacket->xEthernetHeader.xSourceAddress.ucBytes, ucLocalMACAddress_IPv6, sizeof( ucLocalMACAddress_IPv6 ) );
3385     pxTCPPacket->xEthernetHeader.usFrameType = ipIPv6_FRAME_TYPE;
3386     /* IP part. */
3387     memcpy( pxTCPPacket->xIPHeader.xSourceAddress.ucBytes, xDefaultIPAddress_IPv6.ucBytes, sizeof( IPv6_Address_t ) );
3388     memcpy( pxTCPPacket->xIPHeader.xDestinationAddress.ucBytes, xDefaultGatewayAddress_IPv6.ucBytes, sizeof( IPv6_Address_t ) );
3389 
3390     /* Query for e1. */
3391     pxEndPoint = FreeRTOS_MatchingEndpoint( &xNetworkInterface, ( const uint8_t * ) ( pxTCPPacket ) );
3392     TEST_ASSERT_EQUAL( &xEndPoint[ 1 ], pxEndPoint );
3393 }
3394 
3395 /**
3396  * @brief FreeRTOS_MatchingEndpoint returns the endpoint with type.
3397  *
3398  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
3399  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
3400  *
3401  * Test step:
3402  *  - Create 1 interface and 2 endpoints (e0, e1).
3403  *  - Put interface & endpoint into the list.
3404  *     - Assign 2001::1 (xDefaultIPAddress_IPv6) to e0 endpoint.
3405  *     - Assign 11:22:33:ab:cd:ef (ucDefaultMACAddress_IPv6) to e0 endpoint.
3406  *     - Assign 192.168.123.223 (IPV4_DEFAULT_ADDRESS) to e1 endpoint.
3407  *     - Assign ab:cd:ef:11:22:33 (ucDefaultMACAddress_IPv4) to e1 endpoint.
3408  *     - Attach e0 and e1 to interface.
3409  *  - Create a network buffer and IPv4 packet with different destination IPv6 address (xDefaultGatewayAddress_IPv6).
3410  *  - Call FreeRTOS_MatchingEndpoint with check if returned endpoint is e1. (Match by IP type.)
3411  *  - Create a network buffer and IPv4 packet with different destination IPv4 address (ucDefaultGatewayAddress_IPv4).
3412  *  - Call FreeRTOS_MatchingEndpoint with check if returned endpoint is e0. (Match by IP type.)
3413  */
test_FreeRTOS_MatchingEndpoint_Type()3414 void test_FreeRTOS_MatchingEndpoint_Type()
3415 {
3416     NetworkInterface_t xNetworkInterface;
3417     NetworkEndPoint_t xEndPoint[ 2 ];
3418     NetworkEndPoint_t * pxEndPoint = NULL;
3419     uint8_t * pcNetworkBuffer[ sizeof( TCPPacket_IPv6_t ) + 4 ] __attribute__( ( aligned( 32 ) ) );
3420     TCPPacket_IPv6_t * pxTCPPacket = ( TCPPacket_IPv6_t * ) ( ( uintptr_t ) ( pcNetworkBuffer ) + 2U );
3421     ProtocolPacket_t * pxProtocolPacket = ( ProtocolPacket_t * ) ( ( uintptr_t ) ( pcNetworkBuffer ) + 2U );
3422     const uint8_t ucLocalMACAddress[ ipMAC_ADDRESS_LENGTH_BYTES ] = { 0x11, 0x11, 0x11, 0x11, 0x11, 0x11 };
3423     /* Declare a non-global IPv6 address to test. */
3424     const IPv6_Address_t xNonGlobalIPAddress_IPv6 = { 0x12, 0x34, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 };
3425 
3426     /* Initialize network interface. */
3427     memset( &xNetworkInterface, 0, sizeof( NetworkInterface_t ) );
3428     pxNetworkInterfaces = &xNetworkInterface;
3429 
3430     /* Initialize endpoint e0. */
3431     memset( &xEndPoint[ 0 ], 0, sizeof( NetworkEndPoint_t ) );
3432     xEndPoint[ 0 ].bits.bIPv6 = pdTRUE;
3433     memcpy( xEndPoint[ 0 ].ipv6_settings.xIPAddress.ucBytes, xNonGlobalIPAddress_IPv6.ucBytes, sizeof( IPv6_Address_t ) );
3434     memcpy( xEndPoint[ 0 ].xMACAddress.ucBytes, ucDefaultMACAddress_IPv6, sizeof( ucDefaultMACAddress_IPv6 ) );
3435     xEndPoint[ 0 ].pxNetworkInterface = &xNetworkInterface;
3436     pxNetworkEndPoints = &xEndPoint[ 0 ];
3437 
3438     /* Initialize endpoint e1. */
3439     memset( &xEndPoint[ 1 ], 0, sizeof( NetworkEndPoint_t ) );
3440     xEndPoint[ 1 ].ipv4_settings.ulIPAddress = IPV4_DEFAULT_ADDRESS;
3441     memcpy( xEndPoint[ 1 ].xMACAddress.ucBytes, ucDefaultMACAddress_IPv4, sizeof( ucDefaultMACAddress_IPv4 ) );
3442     xEndPoint[ 1 ].pxNetworkInterface = &xNetworkInterface;
3443     /* Attach endpoint to the end of list. */
3444     pxNetworkEndPoints->pxNext = &xEndPoint[ 1 ];
3445 
3446     /* Initialize e0 network buffer. */
3447     memset( pcNetworkBuffer, 0, sizeof( pcNetworkBuffer ) );
3448     /* Ethernet part. */
3449     memcpy( pxTCPPacket->xEthernetHeader.xDestinationAddress.ucBytes, ucLocalMACAddress, sizeof( ucLocalMACAddress ) );
3450     memcpy( pxTCPPacket->xEthernetHeader.xSourceAddress.ucBytes, ucLocalMACAddress, sizeof( ucLocalMACAddress ) );
3451     pxTCPPacket->xEthernetHeader.usFrameType = ipIPv6_FRAME_TYPE;
3452     /* IP part. */
3453     memcpy( pxTCPPacket->xIPHeader.xSourceAddress.ucBytes, xDefaultIPAddress_IPv6.ucBytes, sizeof( IPv6_Address_t ) );
3454     memcpy( pxTCPPacket->xIPHeader.xDestinationAddress.ucBytes, xDefaultIPAddress_IPv6.ucBytes, sizeof( IPv6_Address_t ) );
3455 
3456     /* Query for e0. */
3457     pxEndPoint = FreeRTOS_MatchingEndpoint( &xNetworkInterface, ( const uint8_t * ) ( pxTCPPacket ) );
3458     TEST_ASSERT_EQUAL( &xEndPoint[ 0 ], pxEndPoint );
3459 
3460     /* Initialize e1 network buffer. */
3461     memset( pcNetworkBuffer, 0, sizeof( pcNetworkBuffer ) );
3462     /* Ethernet part. */
3463     memcpy( pxProtocolPacket->xTCPPacket.xEthernetHeader.xDestinationAddress.ucBytes, ucLocalMACAddress, sizeof( ucLocalMACAddress ) );
3464     memcpy( pxProtocolPacket->xTCPPacket.xEthernetHeader.xSourceAddress.ucBytes, ucLocalMACAddress, sizeof( ucLocalMACAddress ) );
3465     pxProtocolPacket->xTCPPacket.xEthernetHeader.usFrameType = ipIPv4_FRAME_TYPE;
3466     /* IP part. */
3467     pxProtocolPacket->xTCPPacket.xIPHeader.ulSourceIPAddress = IPV4_DEFAULT_GATEWAY;
3468     pxProtocolPacket->xTCPPacket.xIPHeader.ulDestinationIPAddress = IPV4_DEFAULT_GATEWAY;
3469 
3470     /* Query for e1. */
3471     pxEndPoint = FreeRTOS_MatchingEndpoint( &xNetworkInterface, ( const uint8_t * ) ( pxTCPPacket ) );
3472     TEST_ASSERT_EQUAL( &xEndPoint[ 1 ], pxEndPoint );
3473 }
3474 
3475 /**
3476  * @brief FreeRTOS_MatchingEndpoint returns the endpoint with default gateway address (FE80::1).
3477  *
3478  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
3479  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
3480  *
3481  * Test step:
3482  *  - Create 1 interface and 1 endpoint.
3483  *  - Put interface & endpoint into the list.
3484  *     - Assign FE70::1 to endpoint.
3485  *     - Assign 11:22:33:ab:cd:ef (ucDefaultMACAddress_IPv6) to endpoint.
3486  *     - Attach endpoint to interface.
3487  *  - Create a network buffer and set IPv6 packet with destination address FE80::1 (default gateway address).
3488  *  - Call FreeRTOS_MatchingEndpoint and check if returned endpoint is same (matched by IP type).
3489  */
test_FreeRTOS_MatchingEndpoint_IPv6DefaultGatewayNotFound()3490 void test_FreeRTOS_MatchingEndpoint_IPv6DefaultGatewayNotFound()
3491 {
3492     NetworkInterface_t xNetworkInterface;
3493     NetworkEndPoint_t xEndPoint;
3494     NetworkEndPoint_t * pxEndPoint = NULL;
3495     uint8_t * pcNetworkBuffer[ sizeof( TCPPacket_IPv6_t ) + 4 ] __attribute__( ( aligned( 32 ) ) );
3496     TCPPacket_IPv6_t * pxTCPPacket = ( TCPPacket_IPv6_t * ) ( ( uintptr_t ) ( pcNetworkBuffer ) + 2U );
3497     const uint8_t ucLocalMACAddress[ ipMAC_ADDRESS_LENGTH_BYTES ] = { 0x11, 0x11, 0x11, 0x11, 0x11, 0x11 };
3498     /* Declare a default gateway IPv6 address to test. */
3499     const IPv6_Address_t xDefaultGatewayIPAddress_IPv6 = { 0xFE, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 };
3500 
3501     /* Initialize network interface. */
3502     memset( &xNetworkInterface, 0, sizeof( NetworkInterface_t ) );
3503     pxNetworkInterfaces = &xNetworkInterface;
3504 
3505     /* Initialize endpoint e0. */
3506     memset( &xEndPoint, 0, sizeof( NetworkEndPoint_t ) );
3507     xEndPoint.bits.bIPv6 = pdTRUE;
3508     memcpy( xEndPoint.ipv6_settings.xIPAddress.ucBytes, xDefaultIPAddress_IPv6.ucBytes, sizeof( IPv6_Address_t ) );
3509     memcpy( xEndPoint.xMACAddress.ucBytes, ucDefaultMACAddress_IPv6, sizeof( ucDefaultMACAddress_IPv6 ) );
3510     xEndPoint.pxNetworkInterface = &xNetworkInterface;
3511     pxNetworkEndPoints = &xEndPoint;
3512 
3513     /* Initialize network buffer. */
3514     memset( pcNetworkBuffer, 0, sizeof( pcNetworkBuffer ) );
3515     /* Ethernet part. */
3516     memcpy( pxTCPPacket->xEthernetHeader.xDestinationAddress.ucBytes, ucLocalMACAddress, sizeof( ucLocalMACAddress ) );
3517     memcpy( pxTCPPacket->xEthernetHeader.xSourceAddress.ucBytes, ucLocalMACAddress, sizeof( ucLocalMACAddress ) );
3518     pxTCPPacket->xEthernetHeader.usFrameType = ipIPv6_FRAME_TYPE;
3519     /* IP part. */
3520     memcpy( pxTCPPacket->xIPHeader.xSourceAddress.ucBytes, xDefaultIPAddress_IPv6.ucBytes, sizeof( IPv6_Address_t ) );
3521     memcpy( pxTCPPacket->xIPHeader.xDestinationAddress.ucBytes, xDefaultGatewayIPAddress_IPv6.ucBytes, sizeof( IPv6_Address_t ) );
3522 
3523     pxEndPoint = FreeRTOS_MatchingEndpoint( &xNetworkInterface, ( const uint8_t * ) ( pxTCPPacket ) );
3524     TEST_ASSERT_EQUAL( &xEndPoint, pxEndPoint );
3525 }
3526 
3527 /**
3528  * @brief FreeRTOS_MatchingEndpoint returns the endpoint with non-global address (FE80::123).
3529  *
3530  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
3531  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
3532  *
3533  * Test step:
3534  *  - Create 1 interface and 1 endpoint.
3535  *  - Put interface & endpoint into the list.
3536  *     - Assign FE80::123 to endpoint.
3537  *     - Assign 11:22:33:ab:cd:ef (ucDefaultMACAddress_IPv6) to endpoint.
3538  *     - Attach endpoint to interface.
3539  *  - Create a network buffer and set IPv6 packet with destination address FE80::1 (default gateway address).
3540  *  - Call FreeRTOS_MatchingEndpoint and check if returned endpoint is same (matched by IP type).
3541  */
test_FreeRTOS_MatchingEndpoint_IPv6NonGlobal()3542 void test_FreeRTOS_MatchingEndpoint_IPv6NonGlobal()
3543 {
3544     NetworkInterface_t xNetworkInterface;
3545     NetworkEndPoint_t xEndPoint;
3546     NetworkEndPoint_t * pxEndPoint = NULL;
3547     uint8_t * pcNetworkBuffer[ sizeof( TCPPacket_IPv6_t ) + 4 ] __attribute__( ( aligned( 32 ) ) );
3548     TCPPacket_IPv6_t * pxTCPPacket = ( TCPPacket_IPv6_t * ) ( ( uintptr_t ) ( pcNetworkBuffer ) + 2U );
3549     const uint8_t ucLocalMACAddress[ ipMAC_ADDRESS_LENGTH_BYTES ] = { 0x11, 0x11, 0x11, 0x11, 0x11, 0x11 };
3550     /* Declare a non-global IPv6 address to test. */
3551     const IPv6_Address_t xLocalIPAddress_IPv6 = { 0xFE, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x23 };
3552 
3553     /* Initialize network interface. */
3554     memset( &xNetworkInterface, 0, sizeof( NetworkInterface_t ) );
3555     pxNetworkInterfaces = &xNetworkInterface;
3556 
3557     /* Initialize endpoint e0. */
3558     memset( &xEndPoint, 0, sizeof( NetworkEndPoint_t ) );
3559     xEndPoint.bits.bIPv6 = pdTRUE;
3560     memcpy( xEndPoint.ipv6_settings.xIPAddress.ucBytes, xLocalIPAddress_IPv6.ucBytes, sizeof( IPv6_Address_t ) );
3561     memcpy( xEndPoint.xMACAddress.ucBytes, ucDefaultMACAddress_IPv6, sizeof( ucDefaultMACAddress_IPv6 ) );
3562     xEndPoint.pxNetworkInterface = &xNetworkInterface;
3563     pxNetworkEndPoints = &xEndPoint;
3564 
3565     /* Initialize network buffer. */
3566     memset( pcNetworkBuffer, 0, sizeof( pcNetworkBuffer ) );
3567     /* Ethernet part. */
3568     memcpy( pxTCPPacket->xEthernetHeader.xDestinationAddress.ucBytes, ucLocalMACAddress, sizeof( ucLocalMACAddress ) );
3569     memcpy( pxTCPPacket->xEthernetHeader.xSourceAddress.ucBytes, ucLocalMACAddress, sizeof( ucLocalMACAddress ) );
3570     pxTCPPacket->xEthernetHeader.usFrameType = ipIPv6_FRAME_TYPE;
3571     /* IP part. */
3572     memcpy( pxTCPPacket->xIPHeader.xSourceAddress.ucBytes, xDefaultIPAddress_IPv6.ucBytes, sizeof( IPv6_Address_t ) );
3573     memcpy( pxTCPPacket->xIPHeader.xDestinationAddress.ucBytes, xLocalIPAddress_IPv6.ucBytes, sizeof( IPv6_Address_t ) );
3574 
3575     pxEndPoint = FreeRTOS_MatchingEndpoint( &xNetworkInterface, ( const uint8_t * ) ( pxTCPPacket ) );
3576     TEST_ASSERT_EQUAL( &xEndPoint, pxEndPoint );
3577 }
3578 
3579 /**
3580  * @brief FreeRTOS_MatchingEndpoint triggers assertion if buffer pointer is NULL.
3581  *
3582  * Test step:
3583  *  - Call FreeRTOS_MatchingEndpoint with NULL buffer pointer.
3584  */
test_FreeRTOS_MatchingEndpoint_NullBuffer()3585 void test_FreeRTOS_MatchingEndpoint_NullBuffer()
3586 {
3587     catch_assert( FreeRTOS_MatchingEndpoint( NULL, NULL ) );
3588 }
3589 
3590 /**
3591  * @brief FreeRTOS_MatchingEndpoint triggers assertion if buffer pointer is NULL.
3592  *
3593  * Test step:
3594  *  - Call FreeRTOS_MatchingEndpoint with buffer pointer with not aligned address.
3595  */
test_FreeRTOS_MatchingEndpoint_BufferAddressNotAligned()3596 void test_FreeRTOS_MatchingEndpoint_BufferAddressNotAligned()
3597 {
3598     uint8_t * pcNetworkBuffer[ sizeof( TCPPacket_IPv6_t ) ] __attribute__( ( aligned( 32 ) ) );
3599 
3600     catch_assert( FreeRTOS_MatchingEndpoint( NULL, ( const uint8_t * ) pcNetworkBuffer ) );
3601 }
3602 
3603 /**
3604  * @brief FreeRTOS_MatchingEndpoint returns NULL when receiving custom frame type
3605  * but ipconfigPROCESS_CUSTOM_ETHERNET_FRAMES is off.
3606  *
3607  * pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
3608  * pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
3609  *
3610  * Test step:
3611  *  - Create 1 interface and 1 endpoint.
3612  *  - Put interface & endpoint into the list.
3613  *     - Assign 192.168.123.223 (IPV4_DEFAULT_ADDRESS) to the endpoint.
3614  *     - Assign ab:cd:ef:11:22:33 (ucDefaultMACAddress_IPv4) to the endpoint.
3615  *     - Assign 0xFF as frame type to the endpoint.
3616  *     - Attach endpoint to interface.
3617  *  - Call FreeRTOS_MatchingEndpoint and check if returned endpoint is NULL.
3618  */
test_FreeRTOS_MatchingEndpoint_MatchCustomFrameType()3619 void test_FreeRTOS_MatchingEndpoint_MatchCustomFrameType()
3620 {
3621     NetworkInterface_t xNetworkInterface;
3622     NetworkEndPoint_t xEndPoint;
3623     NetworkEndPoint_t * pxEndPoint = NULL;
3624     uint8_t * pcNetworkBuffer[ sizeof( TCPPacket_IPv6_t ) + 4 ] __attribute__( ( aligned( 32 ) ) );
3625     ProtocolPacket_t * pxProtocolPacket = ( ProtocolPacket_t * ) ( ( uintptr_t ) ( pcNetworkBuffer ) + 2U );
3626 
3627     /* Initialize network interface. */
3628     memset( &xNetworkInterface, 0, sizeof( NetworkInterface_t ) );
3629     pxNetworkInterfaces = &xNetworkInterface;
3630 
3631     /* Initialize endpoint. */
3632     memset( &xEndPoint, 0, sizeof( NetworkEndPoint_t ) );
3633     xEndPoint.ipv4_settings.ulIPAddress = IPV4_DEFAULT_ADDRESS;
3634     memcpy( xEndPoint.xMACAddress.ucBytes, ucDefaultMACAddress_IPv4, sizeof( ucDefaultMACAddress_IPv4 ) );
3635     xEndPoint.pxNetworkInterface = &xNetworkInterface;
3636     pxNetworkEndPoints = &xEndPoint;
3637 
3638     /* Initialize network buffer. */
3639     memset( pcNetworkBuffer, 0, sizeof( pcNetworkBuffer ) );
3640     /* Ethernet part. */
3641     memcpy( pxProtocolPacket->xTCPPacket.xEthernetHeader.xDestinationAddress.ucBytes, ucDefaultMACAddress_IPv4, sizeof( ucDefaultMACAddress_IPv4 ) );
3642     memcpy( pxProtocolPacket->xTCPPacket.xEthernetHeader.xSourceAddress.ucBytes, ucDefaultMACAddress_IPv4, sizeof( ucDefaultMACAddress_IPv4 ) );
3643     pxProtocolPacket->xTCPPacket.xEthernetHeader.usFrameType = 0xFF;
3644     /* IP part. */
3645     pxProtocolPacket->xTCPPacket.xIPHeader.ulSourceIPAddress = IPV4_DEFAULT_ADDRESS;
3646     pxProtocolPacket->xTCPPacket.xIPHeader.ulDestinationIPAddress = IPV4_DEFAULT_ADDRESS;
3647 
3648     pxEndPoint = FreeRTOS_MatchingEndpoint( &xNetworkInterface, ( const uint8_t * ) ( pxProtocolPacket ) );
3649     TEST_ASSERT_EQUAL( NULL, pxEndPoint );
3650 }
3651