-
Notifications
You must be signed in to change notification settings - Fork 180
/
Copy pathFreeRTOS_DNS_Parser_utest.c
3843 lines (3084 loc) · 126 KB
/
FreeRTOS_DNS_Parser_utest.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* FreeRTOS+TCP <DEVELOPMENT BRANCH>
* Copyright (C) 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* SPDX-License-Identifier: MIT
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* http://aws.amazon.com/freertos
* http://www.FreeRTOS.org
*/
/* Include Unity header */
#include "unity.h"
/* Include standard libraries */
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include "FreeRTOS.h"
#include "FreeRTOS_IP.h"
#include "mock_FreeRTOS_Sockets.h"
#include "mock_FreeRTOS_Routing.h"
#include "mock_FreeRTOS_IP_Private.h"
#include "mock_task.h"
#include "mock_list.h"
#include "mock_queue.h"
#include "mock_FreeRTOS_DNS_Callback.h"
#include "mock_FreeRTOS_DNS_Cache.h"
#include "mock_FreeRTOS_DNS_Networking.h"
#include "mock_NetworkBufferManagement.h"
#include "mock_FreeRTOS_DNS.h"
#include "catch_assert.h"
#include "FreeRTOSIPConfig.h"
#include "FreeRTOS_DNS_Parser.h"
#define LLMNR_ADDRESS "freertos"
#define GOOD_ADDRESS "www.freertos.org"
#define BAD_ADDRESS "this is a bad address"
#define DOTTED_ADDRESS "192.268.0.1"
#define prvROUND_UP_TO( SIZE, ALIGNMENT ) ( ( ( SIZE ) + ( ALIGNMENT ) -1 ) / ( ALIGNMENT ) *( ALIGNMENT ) )
#define prvALIGNED_UDP_PAYLOAD_OFFSET_IPv4 prvROUND_UP_TO( ipUDP_PAYLOAD_OFFSET_IPv4, sizeof( void * ) )
typedef void (* FOnDNSEvent ) ( const char * /* pcName */,
void * /* pvSearchID */,
struct freertos_addrinfo * /* pxAddressInfo */ );
/* =========================== GLOBAL VARIABLES =========================== */
static int callback_called = 0;
static BaseType_t hook_return = pdFALSE;
static BaseType_t hook_called = pdFALSE;
/* =========================== STATIC FUNCTIONS =========================== */
static void dns_callback( const char * pcName,
void * pvSearchID,
struct freertos_addrinfo * pxAddressInfo )
{
callback_called = 1;
}
/* =========================== EXTERN VARIABLES =========================== */
extern BaseType_t xBufferAllocFixedSize;
extern struct freertos_addrinfo pucAddrBuffer[ 2 ];
extern struct freertos_sockaddr pucSockAddrBuffer[ 1 ];
/* ============================ TEST FIXTURES ============================= */
/**
* @brief calls at the beginning of each test case
*/
void setUp( void )
{
xBufferAllocFixedSize = pdFALSE;
callback_called = 0;
}
/**
* @brief calls at the end of each test case
*/
void tearDown( void )
{
hook_return = pdFALSE;
hook_called = pdFALSE;
}
/* ============================= TEST MACROS =============================== */
#define ASSERT_DNS_QUERY_HOOK_CALLED() \
do { \
TEST_ASSERT_EQUAL( pdTRUE, hook_called ); \
} while( 0 )
#define ASSERT_DNS_QUERY_HOOK_NOT_CALLED() \
do { \
TEST_ASSERT_EQUAL( pdFALSE, hook_called ); \
} while( 0 )
/* ============================= TEST CASES =============================== */
/**
* @brief ensures when there is no empty spots in the pucByte buffer zero is
* returned
*/
void test_DNS_ReadNameField_success_empty_uxRemainingBytes( void )
{
uint8_t pucByte[ 300 ];
size_t ret;
ParseSet_t xSet = { 0 };
size_t uxDestLen;
memset( pucByte, 0x00, 300 );
ret = DNS_ReadNameField( &xSet, uxDestLen );
TEST_ASSERT_EQUAL( 0, ret );
}
/**
* @brief ensures that if the dns name is dnsNAME_IS_OFFSET and the source
* length is less than 2 bytes zero is returned
*/
void test_DNS_ReadNameField_fail_offset_dns_name( void )
{
uint8_t pucByte[ 300 ] = { 0 };
size_t ret;
ParseSet_t xSet = { 0 };
size_t uxDestLen;
memset( pucByte, 0x00, 300 );
pucByte[ 0 ] = dnsNAME_IS_OFFSET;
ret = DNS_ReadNameField( &xSet, uxDestLen );
TEST_ASSERT_EQUAL( 0, ret );
}
/**
* @brief ensures that if the dns name is dnsNAME_IS_OFFSET and the source
* length is greater than 2 bytes 2 is returned (sizeof (uint16_t)
*/
void test_DNS_ReadNameField_success_fully_coded_gt_uint16( void )
{
uint8_t pucByte[ 300 ] = { 0 };
size_t ret;
ParseSet_t xSet = { 0 };
memset( pucByte, 0x00, 300 );
pucByte[ 0 ] = dnsNAME_IS_OFFSET;
xSet.pucByte = pucByte;
xSet.uxSourceBytesRemaining = 8;
ret = DNS_ReadNameField( &xSet, 234 );
TEST_ASSERT_EQUAL( sizeof( uint16_t ), ret );
}
/**
* @brief ensures that if the dns name is dnsNAME_IS_OFFSET and the source
* length is equal to 2 bytes 0 is returned
*/
void test_DNS_ReadNameField_success_half_coded_gt_uint16( void )
{
uint8_t pucByte[ 300 ] = { 0 };
size_t ret;
ParseSet_t xSet = { 0 };
memset( pucByte, 0x00, 300 );
pucByte[ 0 ] = dnsNAME_IS_OFFSET;
xSet.pucByte = pucByte;
xSet.uxSourceBytesRemaining = 2;
ret = DNS_ReadNameField( &xSet, 234 );
TEST_ASSERT_EQUAL( 0, ret );
}
/**
* @brief ensures when the next name field size is zero, zero is returned
*/
void test_DNS_ReadNameField_zero_size_walk_over_nothing_to_do( void )
{
uint8_t pucByte[ 300 ] = { 0 };
size_t ret;
ParseSet_t xSet = { 0 };
memset( pucByte, 0x00, 300 );
xSet.pucByte = pucByte;
xSet.uxSourceBytesRemaining = 300;
pucByte[ 0 ] = 0;
ret = DNS_ReadNameField( &xSet, 234 );
TEST_ASSERT_EQUAL( 0, ret );
}
/**
* @brief ensures when the name is 8 bytes 10 is returned . + '\0', and the name
* field is copied to pcName
*/
void test_DNS_ReadNameField_walk_over_copy_name( void )
{
uint8_t pucByte[ 300 ] = { 0 };
char pcName[ 300 ] = { 0 };
size_t ret;
ParseSet_t xSet = { 0 };
memset( pucByte, 0x00, 300 );
xSet.pucByte = pucByte;
xSet.uxSourceBytesRemaining = 300;
pucByte[ 0 ] = 8;
strcpy( pucByte + 1, "FreeRTOS" );
ret = DNS_ReadNameField( &xSet, 254 );
TEST_ASSERT_EQUAL( 10, ret );
TEST_ASSERT_EQUAL_STRING( "FreeRTOS", xSet.pcName );
}
/**
* @brief ensures when the name is 8 bytes and the size passed is smaller
* zero is returned and field id copied to pcName
*/
void test_DNS_ReadNameField_walk_over_exact_source_length( void )
{
uint8_t pucByte[ 300 ] = { 0 };
char pcName[ 300 ] = { 0 };
size_t ret;
ParseSet_t xSet = { 0 };
size_t uxDestLen;
memset( pucByte, 0x00, 300 );
memset( xSet.pcName, 0x00, sizeof( xSet.pcName ) );
xSet.pucByte = pucByte;
xSet.uxSourceBytesRemaining = 9;
pucByte[ 0 ] = 8;
strcpy( pucByte + 1, "FreeRTOS" );
ret = DNS_ReadNameField( &xSet, 254 );
TEST_ASSERT_EQUAL( 0, ret );
TEST_ASSERT_EQUAL_STRING( "FreeRTOS", xSet.pcName );
TEST_ASSERT_EQUAL( 0, xSet.pcName[ 8 ] );
TEST_ASSERT_EQUAL( 0, xSet.pcName[ 9 ] );
TEST_ASSERT_EQUAL( 0, xSet.pcName[ 10 ] );
}
/**
* @brief ensures when we have 2 names fields
* their total + 2(dot, and \0) are returned
* and the result string in pcName
*/
void test_DNS_ReadNameField_walk_over_copy_2_names( void )
{
uint8_t pucByte[ 300 ] = { 0 };
char pcName[ 300 ] = { 0 };
ParseSet_t xSet = { 0 };
size_t ret;
memset( pucByte, 0x00, 300 );
memset( xSet.pcName, 0x00, sizeof( xSet.pcName ) );
pucByte[ 0 ] = 8;
strcpy( pucByte + 1, "FreeRTOS" );
pucByte[ 9 ] = 7;
strcpy( pucByte + 10, "PlusTCP" );
xSet.pucByte = pucByte;
xSet.uxSourceBytesRemaining = 300;
ret = DNS_ReadNameField( &xSet, 254 );
TEST_ASSERT_EQUAL( 18, ret );
TEST_ASSERT_EQUAL_STRING( "FreeRTOS.PlusTCP", xSet.pcName );
}
/**
* @brief ensures that when the destination buffer (pcName) is shorter than the
* total string name field, zero is returned and the string is truncated i
* pcName
*/
void test_DNS_ReadNameField_short_destination( void )
{
uint8_t pucByte[ 300 ] = { 0 };
char pcName[ 300 ] = { 0 };
ParseSet_t xSet = { 0 };
memset( pucByte, 0x00, 300 );
memset( xSet.pcName, 0x00, sizeof( xSet.pcName ) );
pucByte[ 0 ] = 8;
strcpy( pucByte + 1, "FreeRTOS" );
pucByte[ 9 ] = 7;
strcpy( pucByte + 10, "PlusTCP" );
size_t ret;
xSet.pucByte = pucByte;
xSet.uxSourceBytesRemaining = 300;
ret = DNS_ReadNameField( &xSet, 12 );
TEST_ASSERT_EQUAL( 0, ret );
TEST_ASSERT_EQUAL_STRING( "FreeRTOS.", xSet.pcName );
}
/**
* @brief ensures that when the source buffer (pucByte) is shorter than the size
* of the name field, zero is returned, and the resulting string (pcName)
* is truncated
*/
void test_DNS_ReadNameField_short_source( void )
{
uint8_t pucByte[ 300 ] = { 0 };
char pcName[ 300 ] = { 0 };
ParseSet_t xSet = { 0 };
memset( pucByte, 0x00, 300 );
memset( xSet.pcName, 0x00, sizeof( xSet.pcName ) );
pucByte[ 0 ] = 8;
strcpy( pucByte + 1, "FreeRTOS" );
pucByte[ 9 ] = 7;
strcpy( pucByte + 10, "PlusTCP" );
size_t ret;
xSet.pucByte = pucByte;
xSet.uxSourceBytesRemaining = 10;
ret = DNS_ReadNameField( &xSet, 254 );
TEST_ASSERT_EQUAL( 0, ret );
TEST_ASSERT_EQUAL_STRING( "FreeRTOS.", xSet.pcName );
}
/**
* @brief ensures when the second name does not fit the destination zero is
* returned and only the first string is returned
*/
void test_DNS_ReadNameField_fail_name_len_gt_destlen( void )
{
uint8_t pucByte[ 15 ] = { 0 };
char pcName[ 10 ] = { 0 };
ParseSet_t xSet = { 0 };
size_t uxDestLen;
memset( pucByte, 0x00, 15 );
pucByte[ 0 ] = 8;
strcpy( pucByte + 1, "FreeRTOS" );
pucByte[ 9 ] = 1;
size_t ret;
xSet.pucByte = pucByte;
xSet.uxSourceBytesRemaining = 15;
ret = DNS_ReadNameField( &xSet, 10 );
TEST_ASSERT_EQUAL( 0, ret );
TEST_ASSERT_EQUAL_STRING( "FreeRTOS.", xSet.pcName );
}
/* ==================== Testing DNS_SkipNameField ===========================*/
/**
* @brief ensures that if pucByte has a size of zero, zero is returned
*/
void test_DNS_SkipNameField_failed_zero_length()
{
size_t ret;
uint8_t pucByte[ 20 ];
memset( pucByte, 0x00, 20 );
ret = DNS_SkipNameField( pucByte, 0 );
TEST_ASSERT_EQUAL( 0, ret );
}
/**
* @brief ensures that the appropriate size is returned + dot, dot, and \0
*/
void test_DNS_SkipNameField_failed_()
{
uint8_t pucByte[ 300 ] = { 0 };
size_t ret;
memset( pucByte, 0x00, 300 );
pucByte[ 0 ] = 8;
strcpy( pucByte + 1, "FreeRTOS" );
pucByte[ 9 ] = 7;
strcpy( pucByte + 10, "PlusTCP" );
ret = DNS_SkipNameField( pucByte, 300 );
TEST_ASSERT_EQUAL( 18, ret );
}
/**
* @brief ensures that when it is an offset packet and the size is less than 2
* bytes, zero is returned
*/
void test_DNS_SkipNameField_fail_fully_coded( void )
{
uint8_t pucByte[ 2 ] = { 0 };
size_t ret;
memset( pucByte, 0x00, 2 );
pucByte[ 0 ] = dnsNAME_IS_OFFSET;
ret = DNS_SkipNameField( pucByte, 2 );
TEST_ASSERT_EQUAL( 0, ret );
}
/**
* @brief ensures that when it is an offset packet and the size is greater
* than 2, 2 is returned
*/
void test_DNS_SkipNameField_success_fully_coded_gt_uint16( void )
{
uint8_t pucByte[ 300 ] = { 0 };
size_t ret;
memset( pucByte, 0x00, 300 );
pucByte[ 0 ] = dnsNAME_IS_OFFSET;
ret = DNS_SkipNameField( pucByte, 84 );
TEST_ASSERT_EQUAL( sizeof( uint16_t ), ret );
}
/**
* @brief ensures that when the source is shorter than the size markings
* zero is returned
*/
void test_DNS_SkipNameField_short_source( void )
{
uint8_t pucByte[ 300 ] = { 0 };
memset( pucByte, 0x00, 300 );
pucByte[ 0 ] = 8;
strcpy( pucByte + 1, "FreeRTOS" );
pucByte[ 9 ] = 7;
strcpy( pucByte + 10, "PlusTCP" );
size_t ret;
ret = DNS_SkipNameField( pucByte, 10 );
TEST_ASSERT_EQUAL( 0, ret );
}
/**
* @brief ensures that when the last name field is less than the buffer
* remaining size, zero is returned
*/
void test_DNS_SkipNameField_small_buffer( void )
{
uint8_t pucByte[ 300 ] = { 0 };
memset( pucByte, 0x00, 300 );
pucByte[ 0 ] = 8;
strcpy( pucByte + 1, "FreeRTOS" );
pucByte[ 9 ] = 7;
strcpy( pucByte + 10, "PlusTCP" );
size_t ret;
ret = DNS_SkipNameField( pucByte, 6 );
TEST_ASSERT_EQUAL( 0, ret );
}
/* =================== test prepare Reply DNS Message ======================= */
/**
* @brief Send a DNS message successfully : IPv4
*/
void test_prepareReplyDNSMessage_success( void )
{
NetworkBufferDescriptor_t pxNetworkBuffer = { 0 };
uint8_t ether_buffer[ 300 ] = { 0 };
size_t uxDataLength;
NetworkEndPoint_t xEndPoint = { 0 };
pxNetworkBuffer.pucEthernetBuffer = ether_buffer;
pxNetworkBuffer.xDataLength = 300;
pxNetworkBuffer.pxEndPoint = &xEndPoint;
BaseType_t lNetLength = 50;
UDPPacket_t * pxUDPPacket;
IPHeader_t * pxIPHeader;
UDPHeader_t * pxUDPHeader;
pxUDPPacket = ( ( UDPPacket_t * )
pxNetworkBuffer.pucEthernetBuffer );
pxIPHeader = &pxUDPPacket->xIPHeader;
pxIPHeader->ucVersionHeaderLength = 0x0;
pxUDPHeader = &pxUDPPacket->xUDPHeader;
IPPacket_t * xIPPacket = ( ( IPPacket_t * ) pxNetworkBuffer.pucEthernetBuffer );
pxIPHeader->ulSourceIPAddress = 1234;
uxIPHeaderSizePacket_IgnoreAndReturn( ipSIZE_OF_IPv4_HEADER );
usGenerateChecksum_ExpectAnyArgsAndReturn( 555 );
usGenerateProtocolChecksum_ExpectAnyArgsAndReturn( 444 );
prepareReplyDNSMessage( &pxNetworkBuffer,
lNetLength );
uxDataLength = ( ( size_t ) lNetLength ) + ipSIZE_OF_IPv4_HEADER +
ipSIZE_OF_UDP_HEADER + ipSIZE_OF_ETH_HEADER;
TEST_ASSERT_EQUAL( pxNetworkBuffer.xDataLength, uxDataLength );
TEST_ASSERT_EQUAL( pxIPHeader->ucTimeToLive, ipconfigUDP_TIME_TO_LIVE );
}
/**
* @brief Send a DNS message used in MDNS successfully : IPv4
*/
void test_prepareReplyDNSMessage_success_MDNS( void )
{
NetworkBufferDescriptor_t pxNetworkBuffer = { 0 };
uint8_t ether_buffer[ 300 ] = { 0 };
size_t uxDataLength;
NetworkEndPoint_t xEndPoint = { 0 };
pxNetworkBuffer.pucEthernetBuffer = ether_buffer;
pxNetworkBuffer.xDataLength = 300;
pxNetworkBuffer.pxEndPoint = &xEndPoint;
BaseType_t lNetLength = 50;
UDPPacket_t * pxUDPPacket;
IPHeader_t * pxIPHeader;
UDPHeader_t * pxUDPHeader;
pxUDPPacket = ( ( UDPPacket_t * )
pxNetworkBuffer.pucEthernetBuffer );
pxIPHeader = &pxUDPPacket->xIPHeader;
pxIPHeader->ucVersionHeaderLength = 0x0;
pxUDPHeader = &pxUDPPacket->xUDPHeader;
IPPacket_t * xIPPacket = ( ( IPPacket_t * ) pxNetworkBuffer.pucEthernetBuffer );
pxIPHeader->ulSourceIPAddress = 1234;
pxIPHeader->ulDestinationIPAddress = ipMDNS_IP_ADDRESS;
uxIPHeaderSizePacket_IgnoreAndReturn( ipSIZE_OF_IPv4_HEADER );
usGenerateChecksum_ExpectAnyArgsAndReturn( 555 );
usGenerateProtocolChecksum_ExpectAnyArgsAndReturn( 444 );
prepareReplyDNSMessage( &pxNetworkBuffer,
lNetLength );
uxDataLength = ( ( size_t ) lNetLength ) + ipSIZE_OF_IPv4_HEADER +
ipSIZE_OF_UDP_HEADER + ipSIZE_OF_ETH_HEADER;
TEST_ASSERT_EQUAL( pxNetworkBuffer.xDataLength, uxDataLength );
TEST_ASSERT_EQUAL( pxIPHeader->ucTimeToLive, ipMDNS_TIME_TO_LIVE );
}
/**
* @brief Send a DNS message , but end point on NetMask is NULL : IPv4
*/
void test_prepareReplyDNSMessage_NullEndPoint( void )
{
NetworkBufferDescriptor_t pxNetworkBuffer = { 0 };
uint8_t ether_buffer[ 300 ] = { 0 };
size_t uxDataLength;
NetworkEndPoint_t xEndPoint = { 0 };
pxNetworkBuffer.pucEthernetBuffer = ether_buffer;
pxNetworkBuffer.xDataLength = 300;
pxNetworkBuffer.pxEndPoint = &xEndPoint;
BaseType_t lNetLength = 50;
UDPPacket_t * pxUDPPacket;
IPHeader_t * pxIPHeader;
UDPHeader_t * pxUDPHeader;
pxUDPPacket = ( ( UDPPacket_t * )
pxNetworkBuffer.pucEthernetBuffer );
pxIPHeader = &pxUDPPacket->xIPHeader;
pxIPHeader->ucVersionHeaderLength = 0x0;
pxUDPHeader = &pxUDPPacket->xUDPHeader;
IPPacket_t * xIPPacket = ( ( IPPacket_t * ) pxNetworkBuffer.pucEthernetBuffer );
pxIPHeader->ulSourceIPAddress = 1234;
uxIPHeaderSizePacket_IgnoreAndReturn( ipSIZE_OF_IPv4_HEADER );
usGenerateChecksum_ExpectAnyArgsAndReturn( 555 );
usGenerateProtocolChecksum_ExpectAnyArgsAndReturn( 444 );
prepareReplyDNSMessage( &pxNetworkBuffer,
lNetLength );
uxDataLength = ( ( size_t ) lNetLength ) + ipSIZE_OF_IPv4_HEADER +
ipSIZE_OF_UDP_HEADER + ipSIZE_OF_ETH_HEADER;
TEST_ASSERT_EQUAL( pxNetworkBuffer.xDataLength, uxDataLength );
}
/**
* @brief Send a DNS message successfully , when frame type is IPv6
*/
void test_prepareReplyDNSMessage_IPv6success( void )
{
NetworkBufferDescriptor_t pxNetworkBuffer = { 0 };
uint8_t ether_buffer[ 300 ] = { 0 };
size_t uxDataLength;
NetworkEndPoint_t xEndPoint = { 0 };
pxNetworkBuffer.pucEthernetBuffer = ether_buffer;
pxNetworkBuffer.xDataLength = 300;
pxNetworkBuffer.pxEndPoint = &xEndPoint;
BaseType_t lNetLength = 50;
IPPacket_IPv6_t * xIPPacket_IPv6 = ( ( IPPacket_IPv6_t * ) pxNetworkBuffer.pucEthernetBuffer );
xIPPacket_IPv6->xEthernetHeader.usFrameType = ipIPv6_FRAME_TYPE;
UDPPacket_t * pxUDPPacket;
IPHeader_t * pxIPHeader;
UDPHeader_t * pxUDPHeader;
pxUDPPacket = ( ( UDPPacket_t * )
pxNetworkBuffer.pucEthernetBuffer );
pxIPHeader = &pxUDPPacket->xIPHeader;
pxUDPHeader = &pxUDPPacket->xUDPHeader;
IPPacket_t * xIPPacket = ( ( IPPacket_t * ) pxNetworkBuffer.pucEthernetBuffer );
pxIPHeader->ulSourceIPAddress = 1234;
pxIPHeader->ucVersionHeaderLength = 0x60U;
uxIPHeaderSizePacket_IgnoreAndReturn( ipSIZE_OF_IPv6_HEADER );
usGenerateProtocolChecksum_ExpectAnyArgsAndReturn( 444 );
prepareReplyDNSMessage( &pxNetworkBuffer,
lNetLength );
uxDataLength = ( ( size_t ) lNetLength ) + ipSIZE_OF_IPv6_HEADER +
ipSIZE_OF_UDP_HEADER + ipSIZE_OF_ETH_HEADER;
TEST_ASSERT_EQUAL( pxNetworkBuffer.xDataLength, uxDataLength );
}
/**
* @brief Send a DNS message fail , when frame type is IPv6
*/
void test_prepareReplyDNSMessage_IPv6Fail( void )
{
NetworkBufferDescriptor_t pxNetworkBuffer = { 0 };
uint8_t ether_buffer[ 300 ] = { 0 };
size_t uxDataLength;
NetworkEndPoint_t xEndPoint = { 0 };
pxNetworkBuffer.pucEthernetBuffer = ether_buffer;
pxNetworkBuffer.xDataLength = 300;
pxNetworkBuffer.pxEndPoint = &xEndPoint;
BaseType_t lNetLength = 50;
UDPPacket_t * pxUDPPacket;
IPHeader_t * pxIPHeader;
UDPHeader_t * pxUDPHeader;
pxUDPPacket = ( ( UDPPacket_t * )
pxNetworkBuffer.pucEthernetBuffer );
pxIPHeader = &pxUDPPacket->xIPHeader;
pxIPHeader->ucVersionHeaderLength = 0x0;
pxUDPHeader = &pxUDPPacket->xUDPHeader;
IPPacket_t * xIPPacket = ( ( IPPacket_t * ) pxNetworkBuffer.pucEthernetBuffer );
pxIPHeader->ulSourceIPAddress = 1234;
pxIPHeader->ucVersionHeaderLength = 0x0U;
uxIPHeaderSizePacket_IgnoreAndReturn( ipSIZE_OF_IPv6_HEADER );
usGenerateChecksum_ExpectAnyArgsAndReturn( 555 );
usGenerateProtocolChecksum_ExpectAnyArgsAndReturn( 444 );
prepareReplyDNSMessage( &pxNetworkBuffer,
lNetLength );
uxDataLength = ( ( size_t ) lNetLength ) + ipSIZE_OF_IPv6_HEADER +
ipSIZE_OF_UDP_HEADER + ipSIZE_OF_ETH_HEADER;
TEST_ASSERT_EQUAL( pxNetworkBuffer.xDataLength, uxDataLength );
TEST_ASSERT_EQUAL( pxIPHeader->ucTimeToLive, ipconfigUDP_TIME_TO_LIVE );
}
/* / * =========================== test DNS_TreatNBNS ========================== * / */
/**
* @brief ensures that when buffer with size less than 92 bytes is passed,
* vReturnEthernetFrame is not called
*/
void test_DNS_TreatNBNS_Fail_MinimumBufferSize( void )
{
uint8_t pucPayload[ 300 ] = { 0 };
size_t uxBufferLength;
uint32_t ulIPAddress;
struct xNetworkEndPoint xEndPoint = { 0 };
NetworkBufferDescriptor_t xNetworkBuffer;
uint16_t * pusFlags, offset_of_uc = offsetof( NBNSRequest_t, usFlags );
xNetworkBuffer.pxEndPoint = &xEndPoint;
DNS_TreatNBNS( pucPayload,
91, /*minimum buffer size: 92 bytes*/
1234 );
}
/**
* @brief ensures that when Payload of the message passed is NULL,
* vReturnEthernetFrame is not called
*/
void test_DNS_TreatNBNS_Fail_NullPayload( void )
{
uint8_t * pucPayload = NULL;
size_t uxBufferLength;
uint32_t ulIPAddress;
struct xNetworkEndPoint xEndPoint = { 0 };
NetworkBufferDescriptor_t xNetworkBuffer;
uint16_t * pusFlags, offset_of_uc = offsetof( NBNSRequest_t, usFlags );
xNetworkBuffer.pxEndPoint = &xEndPoint;
DNS_TreatNBNS( pucPayload,
300,
1234 );
}
/**
* @brief ensures that when a random payload is passed, vReturnEthernetFrame is
* not called
*/
void test_DNS_TreatNBNS_success( void )
{
uint8_t pucPayload[ 300 ] = { 0 };
size_t uxBufferLength;
uint32_t ulIPAddress;
struct xNetworkEndPoint xEndPoint = { 0 };
NetworkBufferDescriptor_t xNetworkBuffer;
uint16_t * pusFlags, offset_of_uc = offsetof( NBNSRequest_t, usFlags );
xNetworkBuffer.pxEndPoint = &xEndPoint;
uxIPHeaderSizePacket_IgnoreAndReturn( ipSIZE_OF_IPv4_HEADER );
pxUDPPayloadBuffer_to_NetworkBuffer_ExpectAnyArgsAndReturn( NULL );
/*
* usChar2u16_ExpectAnyArgsAndReturn( dnsNBNS_TYPE_NET_BIOS );
* usChar2u16_ExpectAnyArgsAndReturn( dnsNBNS_TYPE_NET_BIOS );
* usChar2u16_ExpectAnyArgsAndReturn( dnsNBNS_TYPE_NET_BIOS );
*/
DNS_TreatNBNS( pucPayload,
300,
1234 );
}
/**
* @brief ensures that when a NULL payload is passed, no API is called
*
*/
void test_DNS_TreatNBNS_FailNullPayload( void )
{
uint8_t * pucPayload = NULL;
size_t uxBufferLength;
uint32_t ulIPAddress;
struct xNetworkEndPoint xEndPoint = { 0 };
NetworkBufferDescriptor_t xNetworkBuffer;
uint16_t * pusFlags, offset_of_uc = offsetof( NBNSRequest_t, usFlags );
xNetworkBuffer.pxEndPoint = &xEndPoint;
/*
* usChar2u16_ExpectAnyArgsAndReturn( dnsNBNS_TYPE_NET_BIOS );
* usChar2u16_ExpectAnyArgsAndReturn( dnsNBNS_TYPE_NET_BIOS );
* usChar2u16_ExpectAnyArgsAndReturn( dnsNBNS_TYPE_NET_BIOS );
*/
DNS_TreatNBNS( pucPayload,
300,
1234 );
}
/**
* @brief ensures that when a Length of the buffer is lesser than uxBytesNeeded , no API is called
*
*/
void test_DNS_TreatNBNS_FailLessBufferSize( void )
{
uint8_t * pucPayload = NULL;
size_t uxBufferLength;
uint32_t ulIPAddress;
struct xNetworkEndPoint xEndPoint = { 0 };
NetworkBufferDescriptor_t xNetworkBuffer;
uint16_t * pusFlags, offset_of_uc = offsetof( NBNSRequest_t, usFlags );
xNetworkBuffer.pxEndPoint = &xEndPoint;
/*
* usChar2u16_ExpectAnyArgsAndReturn( dnsNBNS_TYPE_NET_BIOS );
* usChar2u16_ExpectAnyArgsAndReturn( dnsNBNS_TYPE_NET_BIOS );
* usChar2u16_ExpectAnyArgsAndReturn( dnsNBNS_TYPE_NET_BIOS );
*/
DNS_TreatNBNS( pucPayload,
( sizeof( UDPPacket_t ) + sizeof( NBNSRequest_t ) - 1 ),
1234 );
}
/**
* @brief ensures that when a random payload is passed, vReturnEthernetFrame is
* not called
*/
void test_DNS_TreatNBNS_success_nbns_mask( void )
{
uint8_t pucPayload[ 300 ] = { 0 };
size_t uxBufferLength;
uint32_t ulIPAddress;
NetworkBufferDescriptor_t pxNetworkBuffer = { 0 };
pxNetworkBuffer.pucEthernetBuffer = pucPayload;
pxUDPPayloadBuffer_to_NetworkBuffer_ExpectAnyArgsAndReturn( &pxNetworkBuffer );
usChar2u16_ExpectAnyArgsAndReturn( dnsNBNS_FLAGS_OPCODE_MASK );
DNS_TreatNBNS( pucPayload,
300,
1234 );
}
/**
* @brief ensures that when new buffer is NULL, vReturnEthernetFrame is not
* called
*/
void test_DNS_TreatNBNS_success_nbns_query_trailing_space( void )
{
uint8_t pucPayload[ 300 ] = { 0 };
uint8_t ether_buffer[ 300 ] = { 0 };
size_t uxBufferLength;
uint32_t ulIPAddress;
NetworkBufferDescriptor_t pxNetworkBuffer = { 0 };
pxNetworkBuffer.pucEthernetBuffer = ether_buffer;
hook_return = pdTRUE;
pxUDPPayloadBuffer_to_NetworkBuffer_ExpectAnyArgsAndReturn( &pxNetworkBuffer );
usChar2u16_ExpectAnyArgsAndReturn( dnsNBNS_FLAGS_OPCODE_QUERY );
usChar2u16_ExpectAnyArgsAndReturn( dnsNBNS_TYPE_NET_BIOS );
usChar2u16_ExpectAnyArgsAndReturn( dnsNBNS_FLAGS_OPCODE_QUERY );
DNS_TreatNBNS( pucPayload,
300,
1234 );
ASSERT_DNS_QUERY_HOOK_NOT_CALLED();
}
/**
* @brief ensures that when buffer is NULL, vReturnEthernetFrame is not
* called
*/
void test_DNS_TreatNBNS_success_nbns_query( void )
{
uint8_t pucPayload[ 300 ] = { 0 };
uint8_t ether_buffer[ 300 ] = { 0 };
size_t uxBufferLength;
uint32_t ulIPAddress;
NetworkBufferDescriptor_t pxNetworkBuffer = { 0 };
pxNetworkBuffer.pucEthernetBuffer = ether_buffer;
hook_return = pdTRUE;
pxUDPPayloadBuffer_to_NetworkBuffer_ExpectAnyArgsAndReturn( &pxNetworkBuffer );
usChar2u16_ExpectAnyArgsAndReturn( dnsNBNS_FLAGS_OPCODE_QUERY );
usChar2u16_ExpectAnyArgsAndReturn( dnsNBNS_TYPE_NET_BIOS );
usChar2u16_ExpectAnyArgsAndReturn( dnsNBNS_FLAGS_OPCODE_QUERY );
/* FreeRTOS_FindEndPointOnNetMask_ExpectAnyArgsAndReturn( NULL ); */
/* pxDuplicateNetworkBufferWithDescriptor_ExpectAnyArgsAndReturn( NULL ); */
DNS_TreatNBNS( pucPayload,
300,
1234 );
ASSERT_DNS_QUERY_HOOK_NOT_CALLED();
}
/**
* @brief ensures that when new buffer is NULL, vReturnEthernetFrame is not
* called
*/
void test_DNS_TreatNBNS_success_nbns_query_network_buffer_null( void )
{
uint8_t pucPayload[ 300 ] = { 0 };
size_t uxBufferLength = 300;
uint32_t ulIPAddress;
hook_return = pdTRUE;
pxUDPPayloadBuffer_to_NetworkBuffer_ExpectAnyArgsAndReturn( NULL );
/*
* usChar2u16_ExpectAnyArgsAndReturn( dnsNBNS_FLAGS_OPCODE_QUERY );
* usChar2u16_ExpectAnyArgsAndReturn( dnsNBNS_TYPE_NET_BIOS );
* usChar2u16_ExpectAnyArgsAndReturn( dnsNBNS_FLAGS_OPCODE_QUERY );
*/
DNS_TreatNBNS( pucPayload,
uxBufferLength,
1234 );
/*ASSERT_DNS_QUERY_HOOK_CALLED(); */
}
/**
* @brief success path, formed packet is sent over the network with
* vReturnEthernetFrame
*/
void test_DNS_TreatNBNS_success_nbns_non_fixed_size_buffer( void )
{
/* XXX */
uint8_t pucPayload[ 300 ] = { 0 };
size_t uxBufferLength = 300;
uint32_t ulIPAddress;
NetworkBufferDescriptor_t pxNetworkBuffer = { 0 };
struct xNetworkEndPoint xEndPoint = { 0 };
xEndPoint.ipv4_settings.ulIPAddress = 0xABCD1234;
pxNetworkBuffer.pxEndPoint = &xEndPoint;
pxNetworkBuffer.pucEthernetBuffer = pucPayload;
pxNetworkBuffer.xDataLength = 300;
NetworkBufferDescriptor_t pxNewBuffer = { 0 };
pxNewBuffer.pxEndPoint = &xEndPoint;
pxNewBuffer.pucEthernetBuffer = pucPayload;
pxNewBuffer.xDataLength = 300;
hook_return = pdTRUE;
pxUDPPayloadBuffer_to_NetworkBuffer_ExpectAnyArgsAndReturn( &pxNetworkBuffer );
uxIPHeaderSizePacket_IgnoreAndReturn( ipSIZE_OF_IPv4_HEADER );
usChar2u16_ExpectAnyArgsAndReturn( dnsNBNS_FLAGS_OPCODE_QUERY ); /* usFlags */
usChar2u16_ExpectAnyArgsAndReturn( dnsNBNS_TYPE_NET_BIOS ); /* usType */
usChar2u16_ExpectAnyArgsAndReturn( dnsNBNS_FLAGS_OPCODE_QUERY );
pxResizeNetworkBufferWithDescriptor_ExpectAnyArgsAndReturn( &pxNewBuffer );
usGenerateChecksum_ExpectAnyArgsAndReturn( 4 );
usGenerateProtocolChecksum_ExpectAnyArgsAndReturn( 4 );
vReturnEthernetFrame_Expect( &pxNetworkBuffer, pdFALSE ); /* goal */
DNS_TreatNBNS( pucPayload,
uxBufferLength,
1234 );
ASSERT_DNS_QUERY_HOOK_CALLED();
}
/**
* @brief success path, BufferAllocation_1.c is used, the Network Buffers can contain at least
* ipconfigNETWORK_MTU + ipSIZE_OF_ETH_HEADER.
*/
void test_DNS_TreatNBNS_Fail_BufferAllocation1( void )
{
/* XXX */
uint8_t pucPayload[ 300 ] = { 0 };
size_t uxBufferLength = 300;
uint32_t ulIPAddress;
NetworkBufferDescriptor_t pxNetworkBuffer = { 0 };
struct xNetworkEndPoint xEndPoint = { 0 };
xEndPoint.ipv4_settings.ulIPAddress = 0xABCD1234;
pxNetworkBuffer.pxEndPoint = &xEndPoint;
pxNetworkBuffer.pucEthernetBuffer = pucPayload;
pxNetworkBuffer.xDataLength = 3000;
hook_return = pdTRUE;
xBufferAllocFixedSize = pdTRUE;
pxUDPPayloadBuffer_to_NetworkBuffer_ExpectAnyArgsAndReturn( &pxNetworkBuffer );
uxIPHeaderSizePacket_IgnoreAndReturn( ipSIZE_OF_IPv4_HEADER );
usChar2u16_ExpectAnyArgsAndReturn( dnsNBNS_FLAGS_OPCODE_QUERY ); /* usFlags */
usChar2u16_ExpectAnyArgsAndReturn( dnsNBNS_TYPE_NET_BIOS ); /* usType */
usChar2u16_ExpectAnyArgsAndReturn( dnsNBNS_FLAGS_OPCODE_QUERY );
catch_assert( DNS_TreatNBNS( pucPayload, uxBufferLength, 1234 ) );