-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtransport_interface_test.c
1688 lines (1411 loc) · 72 KB
/
transport_interface_test.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 Integration Toolkit preview
* Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* 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.
*/
/**
* @file transport_interface_test.c
* @brief Integration tests for the transport interface test implementation.
*/
#include "test_execution_config.h"
#if ( TRANSPORT_INTERFACE_TEST_ENABLED == 1 )
/* Standard header includes. */
#include <string.h>
#include <stdbool.h>
/* Include for init and de-init functions. */
#include "transport_interface_test.h"
/* Include for test parameter and execution configs. */
#include "test_param_config.h"
/* Include for Unity framework. */
#include "unity.h"
#include "unity_fixture.h"
/*-----------------------------------------------------------*/
/**
* @brief Transport Interface test buffer length.
*
* Length of the test buffer used in tests. Guard buffers are placed before and
* after the topic buffer to verify that transport interface APIs do not write
* out of bounds. The memory layout is:
*
* +--------------+-------------------------------+------------+
* | Guard | Writable Topic Buffer | Guard |
* +--------------+-------------------------------+------------+
*
* Both guard buffers are filled with a known pattern before each test and are
* verified to remain unchanged after each test.
*/
#define TRANSPORT_TEST_BUFFER_PREFIX_GUARD_LENGTH ( 32U )
#define TRANSPORT_TEST_BUFFER_WRITABLE_LENGTH ( 2048U )
#define TRANSPORT_TEST_BUFFER_SUFFIX_GUARD_LENGTH ( 32U )
#define TRANSPORT_TEST_BUFFER_TOTAL_LENGTH \
( TRANSPORT_TEST_BUFFER_PREFIX_GUARD_LENGTH + \
TRANSPORT_TEST_BUFFER_WRITABLE_LENGTH + \
TRANSPORT_TEST_BUFFER_SUFFIX_GUARD_LENGTH )
/**
* @brief Transport Interface test buffer guard pattern.
*/
#define TRANSPORT_TEST_BUFFER_GUARD_PATTERN ( 0xA5 )
/**
* @brief Transport Interface send receive retry count.
*/
#define TRANSPORT_TEST_SEND_RECEIVE_RETRY_COUNT ( 50U )
/**
* @brief Transport Interface writev retry count.
*/
#define TRANSPORT_TEST_WRITEV_RETRY_COUNT ( 50U )
/**
* @brief Transport Interface delay in milliseconds.
*
* The Delay time for the transport interface test to wait for the data echo-ed
* back from transport network.
*/
#define TRANSPORT_TEST_DELAY_MS ( 200U )
/**
* @brief Echo server disconnect command.
*
* The command to inform echo server to disconnect the test connection.
*/
#define TRANSPORT_TEST_DISCONNECT_COMMAND "DISCONNECT"
/**
* @brief Transport Interface disconnect delay in milliseconds.
*
* The Delay time for the transport interface test to wait for the send operation
* and response from the network. The delay time should be long enough to cover different
* network environment.
*/
#define TRANSPORT_TEST_NETWORK_DELAY_MS ( 3000U )
/**
* @brief Number of simultaneous tasks for multithreaded tests.
*/
#define TRANSPORT_TEST_MULTI_THREAD_TASK_COUNT ( 2U )
/**
* @brief The thread parameter index used in non-multithreaded test.
*/
#define TRANSPORT_TEST_INDEX ( 0U )
/**
* @brief The second thread parameter index used in multithreaded test.
*/
#define TRANSPORT_TEST_SECOND_INDEX ( 1U )
/**
* @brief Timeout to wait thread function finish the test.
*/
#ifndef TRANSPORT_TEST_WAIT_THREAD_TIMEOUT_MS
#define TRANSPORT_TEST_WAIT_THREAD_TIMEOUT_MS ( 1000000U )
#endif
/**
* @brief Timeout to wait no data to receive test thread.
*/
#define TRANSPORT_TEST_WAIT_THREAD_RECEIVE_TIMEOUT_MS ( 180000U )
/**
* @brief Print progress in SendRecvCompare test.
*
* This debug function should be disabled if testing with IDT.
*/
#ifndef TRANSPORT_TEST_PRINT_DEBUG_PROGRESS
#define TRANSPORT_TEST_PRINT_DEBUG_PROGRESS ( 0 )
#endif
/**
* @brief Print debug error message when test fail in multi-threaded test.
*
* Use UnityPrint for older version of Unity.
*/
#ifndef TEST_MESSAGE
#define TEST_MESSAGE( x ) UnityPrint( x )
#endif
/*-----------------------------------------------------------*/
typedef struct threadParameter
{
NetworkContext_t * pNetworkContext;
uint8_t transportTestBuffer[ TRANSPORT_TEST_BUFFER_TOTAL_LENGTH ];
bool xNetworkConnected;
bool stopFlag;
bool xResult;
} threadParameter_t;
/*-----------------------------------------------------------*/
#if ( TRANSPORT_INTERFACE_TEST_ENABLED == 1 )
#ifndef ECHO_SERVER_ENDPOINT
#error "Please define ECHO_SERVER_ENDPOINT"
#endif
#ifndef ECHO_SERVER_PORT
#error "Please define ECHO_SERVER_PORT"
#endif
#endif /* if ( TRANSPORT_INTERFACE_TEST_ENABLED == 1 ) */
/*-----------------------------------------------------------*/
/**
* @brief Struct of test parameters filled in by user.
*/
static TransportTestParam_t testParam = { 0 };
/**
* @brief Info of the server to connect to for tests.
*/
static TestHostInfo_t testHostInfo = { 0 };
/**
* @brief Transport Interface used in test cases.
*/
static TransportInterface_t * pTestTransport = NULL;
/**
* @brief Thread paramter to hold the network context and test buffer.
*/
static threadParameter_t threadParameter[ TRANSPORT_TEST_MULTI_THREAD_TASK_COUNT ];
/**
* @brief Test group for transport interface test.
*/
TEST_GROUP( Full_TransportInterfaceTest );
/*-----------------------------------------------------------*/
/**
* @brief Initialize the test data with 0,1,...,255,0,1,...
*/
static void prvInitializeTestData( uint8_t * pTransportTestBuffer,
uint32_t testSize )
{
uint32_t i;
for( i = 0U; i < testSize; i++ )
{
pTransportTestBuffer[ i ] = ( uint8_t ) i;
}
}
/*-----------------------------------------------------------*/
/**
* @brief Verify the data received and buffer after testSize should remain unchanged.
*
* The received data is verified with the pattern initialized in prvInitializeTestData.
* The test buffer after testSize should remain unchanged.
*/
static bool prvVerifyTestData( uint8_t * pTransportTestBuffer,
uint32_t testSize,
uint32_t maxBufferSize )
{
uint32_t i;
bool retValue = true;
/* Check the data received is correct. */
for( i = 0U; i < testSize; i++ )
{
if( ( ( uint8_t ) i ) != pTransportTestBuffer[ i ] )
{
TEST_MESSAGE( "Received data is not the same as expected." );
retValue = false;
break;
}
}
/* Check the buffer after testSize is unchanged. */
if( ( retValue == true ) && ( testSize < maxBufferSize ) )
{
for( i = 0U; i < ( maxBufferSize - testSize ); i++ )
{
if( pTransportTestBuffer[ testSize + i ] != TRANSPORT_TEST_BUFFER_GUARD_PATTERN )
{
TEST_MESSAGE( "Buffer after testSize should not be altered." );
retValue = false;
break;
}
}
}
return retValue;
}
/*-----------------------------------------------------------*/
/**
* @brief Send the data over transport network with retry.
*
* The send API may return less bytes then requested. If the transport send function
* returns zero, it should represent the send operation can be retired by calling
* the API function. The retry operation is handled in this function.
*/
static bool prvTransportSendData( TransportInterface_t * pTransport,
NetworkContext_t * pNetworkContext,
uint8_t * pTransportTestBuffer,
uint32_t sendSize )
{
uint32_t transferTotal = 0U;
int32_t transportResult = 0;
uint32_t i;
bool retValue = true;
for( i = 0U; i < TRANSPORT_TEST_SEND_RECEIVE_RETRY_COUNT; i++ )
{
transportResult = pTransport->send( pNetworkContext,
&pTransportTestBuffer[ transferTotal ],
sendSize - transferTotal );
/* Send should not have any error. */
if( transportResult < 0 )
{
TEST_MESSAGE( "Transport send data should not have any error." );
retValue = false;
break;
}
if( ( sendSize - transferTotal ) < ( uint32_t ) transportResult )
{
TEST_MESSAGE( "More data is sent than expected." );
retValue = false;
break;
}
transferTotal = transferTotal + ( uint32_t ) transportResult;
if( sendSize == transferTotal )
{
/* All the data is sent. Break the retry loop. */
break;
}
}
/* Check if all the data is sent. */
if( sendSize != transferTotal )
{
TEST_MESSAGE( "Fail to send all the data expected." );
retValue = false;
}
return retValue;
}
/*-----------------------------------------------------------*/
#ifdef TRANSPORT_TEST_EXECUTE_WRITEV_TESTS
/**
* @brief Send the data over transport network using writev with retry.
*
* The writev API may return less bytes then requested. If the transport writev function
* returns zero, it should represent that the writev operation can be retried by calling
* the API function. The retry operation is handled in this function.
*/
static bool prvTransportWritevData( TransportInterface_t * pTransport,
NetworkContext_t * pNetworkContext,
TransportOutVector_t * pTransportTestVectorArray,
size_t ioVecCount )
{
int32_t transportResult = 0;
uint32_t i;
bool retValue = true;
int32_t bytesToSend = 0;
int32_t bytesSentOrError = 0;
TransportOutVector_t * pIoVectIterator;
size_t vectorsToBeSent = ioVecCount;
/* Count the total number of bytes to be sent as outlined in the vector. */
for( pIoVectIterator = pTransportTestVectorArray; pIoVectIterator <= &( pTransportTestVectorArray[ ioVecCount - 1U ] ); pIoVectIterator++ )
{
bytesToSend += ( int32_t ) pIoVectIterator->iov_len;
}
/* Reset the iterator to point to the first entry in the array. */
pIoVectIterator = pTransportTestVectorArray;
for( i = 0U; i < TRANSPORT_TEST_WRITEV_RETRY_COUNT; i++ )
{
uint32_t bytesSentThisVector = 0U;
transportResult = pTransport->writev( pNetworkContext,
pIoVectIterator,
vectorsToBeSent );
/* Send should not have any error. */
if( transportResult < 0 )
{
TEST_MESSAGE( "Transport send data should not have any error." );
retValue = false;
break;
}
if( ( bytesToSend - bytesSentOrError ) < transportResult )
{
TEST_MESSAGE( "More data is sent than expected." );
retValue = false;
break;
}
bytesSentOrError += transportResult;
bytesSentThisVector += transportResult;
if( bytesToSend == bytesSentOrError )
{
/* All the data is sent. Break the retry loop. */
break;
}
/* Increment the vector iterator based on the number of bytes sent. */
while( ( pIoVectIterator <= &( pTransportTestVectorArray[ ioVecCount - 1U ] ) ) &&
( bytesSentThisVector >= pIoVectIterator->iov_len ) )
{
bytesSentThisVector -= ( uint32_t ) pIoVectIterator->iov_len;
pIoVectIterator++;
/* Update the number of vector which are yet to be sent. */
vectorsToBeSent--;
}
/* Some of the bytes from the current vector pointed to by the vector iterator were sent
* as well, update the length and the pointer to data in this vector. */
if( ( bytesSentThisVector > 0U ) &&
( pIoVectIterator <= &( pTransportTestVectorArray[ ioVecCount - 1U ] ) ) )
{
/* Update the pointer to data to point to the first byte which needs to be sent in the next iteration. */
pIoVectIterator->iov_base = ( const void * ) &( ( ( const uint8_t * ) pIoVectIterator->iov_base )[ bytesSentThisVector ] );
pIoVectIterator->iov_len -= bytesSentThisVector;
}
}
/* Check if all the data is sent. */
if( bytesToSend != bytesSentOrError )
{
TEST_MESSAGE( "Fail to send all the data expected." );
retValue = false;
}
return retValue;
}
#endif
/*-----------------------------------------------------------*/
/**
* @brief Receive the data from transport network with retry.
*
* The receive API may return less btyes then requested. If the transport receive
* function returns zero, it should represent the read operation can be retried
* by calling the API function. The retry operation is handled in this function.
*/
static bool prvTransportRecvData( TransportInterface_t * pTransport,
NetworkContext_t * pNetworkContext,
uint8_t * pTransportTestBuffer,
uint32_t recvSize )
{
uint32_t transferTotal = 0U;
int32_t transportResult = 0;
uint32_t i;
bool retValue = true;
/* Initialize the receive buffer with TRANSPORT_TEST_BUFFER_GUARD_PATTERN. */
memset( &( pTransportTestBuffer[ 0 ] ), TRANSPORT_TEST_BUFFER_GUARD_PATTERN, recvSize );
for( i = 0U; i < TRANSPORT_TEST_SEND_RECEIVE_RETRY_COUNT; i++ )
{
transportResult = pTransport->recv( pNetworkContext,
&pTransportTestBuffer[ transferTotal ],
recvSize - transferTotal );
/* Receive should not have any error. */
if( transportResult < 0 )
{
TEST_MESSAGE( "Transport receive data should not have any error." );
retValue = false;
break;
}
if( ( recvSize - transferTotal ) < ( uint32_t ) transportResult )
{
TEST_MESSAGE( "More data is received than expected." );
retValue = false;
break;
}
transferTotal = transferTotal + transportResult;
if( recvSize > transferTotal )
{
retValue = prvVerifyTestData( &pTransportTestBuffer[ transferTotal ], 0, ( recvSize - transferTotal ) );
}
else
{
/* This is test testSize == transferTotal case. All the data is received.
* Break the retry loop. */
break;
}
/* Delay to wait for the test data from the transport network. */
FRTest_TimeDelay( TRANSPORT_TEST_DELAY_MS );
}
/* Check if all the data is recevied. */
if( recvSize != transferTotal )
{
retValue = false;
TEST_MESSAGE( "Fail to receive all the data expected." );
}
return retValue;
}
/*-----------------------------------------------------------*/
/**
* @brief Verify the buffer guard of the test buffer.
*/
static void prvVerifyTestBufferGuard( uint8_t * pTransportTestBuffer )
{
/* Prefix and Suffix guard buffers must never change. */
TEST_ASSERT_EACH_EQUAL_HEX8_MESSAGE( TRANSPORT_TEST_BUFFER_GUARD_PATTERN,
pTransportTestBuffer,
TRANSPORT_TEST_BUFFER_PREFIX_GUARD_LENGTH,
"transportTestBuffer prefix guard should not be altered." );
TEST_ASSERT_EACH_EQUAL_HEX8_MESSAGE( TRANSPORT_TEST_BUFFER_GUARD_PATTERN,
&( pTransportTestBuffer[ TRANSPORT_TEST_BUFFER_PREFIX_GUARD_LENGTH +
TRANSPORT_TEST_BUFFER_WRITABLE_LENGTH ] ),
TRANSPORT_TEST_BUFFER_SUFFIX_GUARD_LENGTH,
"transportTestBuffer suffix guard should not be altered." );
}
/*-----------------------------------------------------------*/
/**
* @brief Thread function of the send/receive/compare test.
*/
static void prvSendRecvCompareFunc( void * pParam )
{
threadParameter_t * pThreadParameter = pParam;
uint8_t * pTransportTestBufferStart =
&( pThreadParameter->transportTestBuffer[ TRANSPORT_TEST_BUFFER_PREFIX_GUARD_LENGTH ] );
uint32_t testBufferSize;
NetworkContext_t * pNetworkContext = pThreadParameter->pNetworkContext;
pThreadParameter->xResult = true;
for( testBufferSize = 1U; testBufferSize <= TRANSPORT_TEST_BUFFER_WRITABLE_LENGTH; testBufferSize = testBufferSize + 1 )
{
/* Initialize the test data buffer. */
prvInitializeTestData( pTransportTestBufferStart, testBufferSize );
/* Send the test data to the server. */
if( pThreadParameter->xResult == true )
{
pThreadParameter->xResult = prvTransportSendData( pTestTransport,
pNetworkContext,
pTransportTestBufferStart,
testBufferSize );
}
/* Receive the test data from server. */
if( pThreadParameter->xResult == true )
{
pThreadParameter->xResult = prvTransportRecvData( pTestTransport,
pNetworkContext,
pTransportTestBufferStart,
testBufferSize );
}
/* Compare the test data received from server. */
if( pThreadParameter->xResult == true )
{
pThreadParameter->xResult = prvVerifyTestData( pTransportTestBufferStart,
testBufferSize,
TRANSPORT_TEST_BUFFER_WRITABLE_LENGTH );
}
if( ( pThreadParameter->stopFlag == true ) || ( pThreadParameter->xResult == false ) )
{
break;
}
#if ( TRANSPORT_TEST_PRINT_DEBUG_PROGRESS == 1 )
/* Output information to indicate the test is running. */
UNITY_OUTPUT_CHAR( '.' );
#endif
}
}
/*-----------------------------------------------------------*/
#ifdef TRANSPORT_TEST_EXECUTE_WRITEV_TESTS
/**
* @brief Thread function of the writev/receive/compare test.
*/
static void prvWritevRecvCompareFunc( void * pParam )
{
threadParameter_t * pThreadParameter = pParam;
uint8_t * pTransportTestBufferStart =
&( pThreadParameter->transportTestBuffer[ TRANSPORT_TEST_BUFFER_PREFIX_GUARD_LENGTH ] );
uint32_t testBufferSize;
NetworkContext_t * pNetworkContext = pThreadParameter->pNetworkContext;
TransportOutVector_t pTransportTestVectorArray[ 5U ];
size_t ioVecCount;
pThreadParameter->xResult = true;
for( testBufferSize = 1U; testBufferSize <= TRANSPORT_TEST_BUFFER_WRITABLE_LENGTH; testBufferSize = testBufferSize + 1 )
{
if( testBufferSize % 5 == 0 )
{
ioVecCount = 5U;
}
else if( testBufferSize % 3 == 0 )
{
ioVecCount = 3U;
}
else if( testBufferSize % 2 == 0 )
{
ioVecCount = 2U;
}
else
{
ioVecCount = 1U;
}
for( size_t index = 0U; index < ioVecCount; index ++ )
{
pTransportTestVectorArray[ index ].iov_base = &pTransportTestBufferStart[ index * ( testBufferSize / ioVecCount ) ];;
pTransportTestVectorArray[ index ].iov_len = testBufferSize / ioVecCount;
}
/* Initialize the test data buffer. */
prvInitializeTestData( pTransportTestBufferStart, testBufferSize );
/* Send the test data to the server. */
if( pThreadParameter->xResult == true )
{
pThreadParameter->xResult = prvTransportWritevData( pTestTransport, pNetworkContext, pTransportTestVectorArray,
ioVecCount );
}
/* Receive the test data from server. */
if( pThreadParameter->xResult == true )
{
pThreadParameter->xResult = prvTransportRecvData( pTestTransport, pNetworkContext, pTransportTestBufferStart,
testBufferSize );
}
/* Compare the test data received from server. */
if( pThreadParameter->xResult == true )
{
pThreadParameter->xResult = prvVerifyTestData( pTransportTestBufferStart, testBufferSize,
TRANSPORT_TEST_BUFFER_WRITABLE_LENGTH );
}
if( ( pThreadParameter->stopFlag == true ) || ( pThreadParameter->xResult == false ) )
{
break;
}
#if ( TRANSPORT_TEST_PRINT_DEBUG_PROGRESS == 1 )
/* Output information to indicate the test is running. */
UNITY_OUTPUT_CHAR( '.' );
#endif
}
}
#endif
/*-----------------------------------------------------------*/
static void prvRetunZeroRetryFunc( void * pParam )
{
threadParameter_t * pThreadParameter = pParam;
int32_t transportResult = 0;
/* Pointer of writable test buffer. */
uint8_t * pTransportTestBufferStart =
&( pThreadParameter->transportTestBuffer[ TRANSPORT_TEST_BUFFER_PREFIX_GUARD_LENGTH ] );
NetworkContext_t * pNetworkContext = pThreadParameter->pNetworkContext;
pThreadParameter->xResult = true;
/* Receive from remote server. No data will be recevied since no data is sent. */
transportResult = pTestTransport->recv( pNetworkContext,
pTransportTestBufferStart,
TRANSPORT_TEST_BUFFER_WRITABLE_LENGTH );
if( transportResult != 0 )
{
TEST_MESSAGE( "No data to receive should return 0." );
pThreadParameter->xResult = false;
}
/* The buffer should remain unchanged when zero returned. */
if( pThreadParameter->xResult == true )
{
pThreadParameter->xResult = prvVerifyTestData( pTransportTestBufferStart, 0, TRANSPORT_TEST_BUFFER_WRITABLE_LENGTH );
}
/* Send some data to echo server then retry the receive operation. The receive
* operation should be able to receive all the data. */
/* Initialize the test data. */
prvInitializeTestData( pTransportTestBufferStart, TRANSPORT_TEST_BUFFER_WRITABLE_LENGTH );
/* Send the test data to the server. */
if( pThreadParameter->xResult == true )
{
pThreadParameter->xResult = prvTransportSendData( pTestTransport, pNetworkContext,
pTransportTestBufferStart, TRANSPORT_TEST_BUFFER_WRITABLE_LENGTH );
}
/* Receive the test data from server. */
if( pThreadParameter->xResult == true )
{
pThreadParameter->xResult = prvTransportRecvData( pTestTransport, pNetworkContext,
pTransportTestBufferStart, TRANSPORT_TEST_BUFFER_WRITABLE_LENGTH );
}
/* Compare the test data received from server. */
if( pThreadParameter->xResult == true )
{
pThreadParameter->xResult = prvVerifyTestData( pTransportTestBufferStart,
TRANSPORT_TEST_BUFFER_WRITABLE_LENGTH, TRANSPORT_TEST_BUFFER_WRITABLE_LENGTH );
}
}
/*-----------------------------------------------------------*/
static void prvNoDataToReceiveFunc( void * pParam )
{
threadParameter_t * pThreadParameter = pParam;
int32_t transportResult = 0;
/* Pointer of writable test buffer. */
uint8_t * pTransportTestBufferStart =
&( pThreadParameter->transportTestBuffer[ TRANSPORT_TEST_BUFFER_PREFIX_GUARD_LENGTH ] );
NetworkContext_t * pNetworkContext = pThreadParameter->pNetworkContext;
pThreadParameter->xResult = true;
/* Receive from remote server. No data will be recevied since no data is sent. */
transportResult = pTestTransport->recv( pNetworkContext,
pTransportTestBufferStart,
TRANSPORT_TEST_BUFFER_WRITABLE_LENGTH );
if( transportResult != 0 )
{
TEST_MESSAGE( "No data to receive should return 0." );
pThreadParameter->xResult = false;
}
/* The buffer should remain unchanged when zero returned. */
if( pThreadParameter->xResult == true )
{
pThreadParameter->xResult = prvVerifyTestData( pTransportTestBufferStart, 0, TRANSPORT_TEST_BUFFER_WRITABLE_LENGTH );
}
}
/*-----------------------------------------------------------*/
/**
* @brief Test setup function for transport interface test.
*/
TEST_SETUP( Full_TransportInterfaceTest )
{
NetworkConnectStatus_t networkConnectResult = NETWORK_CONNECT_SUCCESS;
uint8_t * pTransportTestBuffer = threadParameter[ TRANSPORT_TEST_INDEX ].transportTestBuffer;
NetworkContext_t * pNetworkContext = threadParameter[ TRANSPORT_TEST_INDEX ].pNetworkContext;
uint32_t threadIndex;
/* Reset the threadParameter status. */
for( threadIndex = 0U; threadIndex < TRANSPORT_TEST_MULTI_THREAD_TASK_COUNT; threadIndex++ )
{
threadParameter[ threadIndex ].xNetworkConnected = false;
threadParameter[ threadIndex ].xResult = false;
threadParameter[ threadIndex ].stopFlag = false;
}
/* Ensure the test parameter is valid. testParam.pNetworkCredentials is not checked here. */
TEST_ASSERT_NOT_NULL_MESSAGE( testParam.pTransport, "testParam.pTransport should not be NULL." );
TEST_ASSERT_NOT_NULL_MESSAGE( testParam.pTransport->send, "testParam.pTransport->send should not be NULL." );
TEST_ASSERT_NOT_NULL_MESSAGE( testParam.pTransport->recv, "testParam.pTransport->recv should not be NULL." );
#ifdef TRANSPORT_TEST_EXECUTE_WRITEV_TESTS
TEST_ASSERT_NOT_NULL_MESSAGE( testParam.pTransport->writev, "testParam.pTransport->writev should not be NULL." );
#endif
TEST_ASSERT_NOT_NULL_MESSAGE( testParam.pNetworkContext, "testParam.pNetworkContext should not be NULL." );
TEST_ASSERT_NOT_NULL_MESSAGE( testParam.pSecondNetworkContext, "testParam.pSecondNetworkContext should not be NULL." );
TEST_ASSERT_NOT_NULL_MESSAGE( testParam.pNetworkConnect, "testParam.pNetworkConnect should not be NULL." );
TEST_ASSERT_NOT_NULL_MESSAGE( testParam.pNetworkDisconnect, "testParam.pNetworkDisconnect should not be NULL." );
/* Setup the trasnport structure to use the primary network context. */
pTestTransport = testParam.pTransport;
/* Initialize the transport test buffer with TRANSPORT_TEST_BUFFER_GUARD_PATTERN. */
memset( pTransportTestBuffer, TRANSPORT_TEST_BUFFER_GUARD_PATTERN, TRANSPORT_TEST_BUFFER_TOTAL_LENGTH );
/* Call the hook function implemented by the application to initialize the transport interface. */
networkConnectResult = testParam.pNetworkConnect( pNetworkContext,
&testHostInfo, testParam.pNetworkCredentials );
TEST_ASSERT_EQUAL_INT32_MESSAGE( NETWORK_CONNECT_SUCCESS, networkConnectResult, "Network connect failed." );
threadParameter[ TRANSPORT_TEST_INDEX ].xNetworkConnected = true;
}
/*-----------------------------------------------------------*/
/**
* @brief Test tear down function for transport interface test.
*/
TEST_TEAR_DOWN( Full_TransportInterfaceTest )
{
uint8_t * pTransportTestBuffer = threadParameter[ TRANSPORT_TEST_INDEX ].transportTestBuffer;
NetworkContext_t * pNetworkContext = threadParameter[ TRANSPORT_TEST_INDEX ].pNetworkContext;
prvVerifyTestBufferGuard( pTransportTestBuffer );
/* Call the hook function implemented by the application to de-initialize the transport interface. */
if( threadParameter[ TRANSPORT_TEST_INDEX ].xNetworkConnected == true )
{
testParam.pNetworkDisconnect( pNetworkContext );
threadParameter[ TRANSPORT_TEST_INDEX ].xNetworkConnected = false;
}
}
/*-----------------------------------------------------------*/
/**
* @brief Test transport interface send with NULL network context pointer handling.
*/
TEST( Full_TransportInterfaceTest, TransportSend_NetworkContextNullPtr )
{
int32_t sendResult = 0;
uint8_t * pTransportTestBuffer = threadParameter[ TRANSPORT_TEST_INDEX ].transportTestBuffer;
/* Send with NULL network context pointer should return negative value. */
sendResult = pTestTransport->send( NULL,
&( pTransportTestBuffer[ TRANSPORT_TEST_BUFFER_PREFIX_GUARD_LENGTH ] ),
TRANSPORT_TEST_BUFFER_WRITABLE_LENGTH );
TEST_ASSERT_LESS_THAN_INT32_MESSAGE( 0, sendResult, "Transport interface send with NULL NetworkContext_t "
"pointer should return negative value." );
}
/*-----------------------------------------------------------*/
/**
* @brief Test transport interface send with NULL buffer pointer handling.
*/
TEST( Full_TransportInterfaceTest, TransportSend_BufferNullPtr )
{
int32_t sendResult = 0;
NetworkContext_t * pNetworkContext = threadParameter[ TRANSPORT_TEST_INDEX ].pNetworkContext;
/* Send with NULL buffer pointer should return negative value. */
sendResult = pTestTransport->send( pNetworkContext, NULL, 1 );
TEST_ASSERT_LESS_THAN_INT32_MESSAGE( 0, sendResult, "Transport interface send with NULL buffer "
"pointer should return negative value." );
}
/*-----------------------------------------------------------*/
/**
* @brief Test transport interface send with zero byte to send handling.
*/
TEST( Full_TransportInterfaceTest, TransportSend_ZeroByteToSend )
{
int32_t sendResult = 0;
uint8_t * pTransportTestBuffer = threadParameter[ TRANSPORT_TEST_INDEX ].transportTestBuffer;
NetworkContext_t * pNetworkContext = threadParameter[ TRANSPORT_TEST_INDEX ].pNetworkContext;
/* Send with zero byte to send should return negative value. */
sendResult = pTestTransport->send( pNetworkContext,
&( pTransportTestBuffer[ TRANSPORT_TEST_BUFFER_PREFIX_GUARD_LENGTH ] ),
0 );
TEST_ASSERT_LESS_THAN_INT32_MESSAGE( 0, sendResult, "Transport interface send with zero byte "
"to send should return negative value." );
}
/*-----------------------------------------------------------*/
/**
* @brief Test transport interface recv with NULL network context pointer handling.
*/
TEST( Full_TransportInterfaceTest, TransportRecv_NetworkContextNullPtr )
{
int32_t recvResult = 0;
uint8_t * pTransportTestBuffer = threadParameter[ TRANSPORT_TEST_INDEX ].transportTestBuffer;
/* Receive with NULL network context pointer should return negative value. */
recvResult = pTestTransport->recv( NULL,
&( pTransportTestBuffer[ TRANSPORT_TEST_BUFFER_PREFIX_GUARD_LENGTH ] ),
TRANSPORT_TEST_BUFFER_WRITABLE_LENGTH );
TEST_ASSERT_LESS_THAN_INT32_MESSAGE( 0, recvResult, "Transport interface recv with NULL network "
"context pointer should return negative value." );
TEST_ASSERT_EACH_EQUAL_HEX8_MESSAGE( TRANSPORT_TEST_BUFFER_GUARD_PATTERN,
&( pTransportTestBuffer[ TRANSPORT_TEST_BUFFER_PREFIX_GUARD_LENGTH ] ),
TRANSPORT_TEST_BUFFER_WRITABLE_LENGTH,
"transportTestBuffer should not be altered." );
}
/*-----------------------------------------------------------*/
/**
* @brief Test transport interface recv with NULL buffer pointer handling.
*/
TEST( Full_TransportInterfaceTest, TransportRecv_BufferNullPtr )
{
int32_t recvResult = 0;
NetworkContext_t * pNetworkContext = threadParameter[ TRANSPORT_TEST_INDEX ].pNetworkContext;
/* Receive with NULL buffer pointer should return negative value. */
recvResult = pTestTransport->recv( pNetworkContext, NULL, TRANSPORT_TEST_BUFFER_WRITABLE_LENGTH );
TEST_ASSERT_LESS_THAN_INT32_MESSAGE( 0, recvResult, "Transport interface recv with NULL buffer "
"pointer should return negative value." );
}
/*-----------------------------------------------------------*/
/**
* @brief Test transport interface recv zero byte to receive handling.
*/
TEST( Full_TransportInterfaceTest, TransportRecv_ZeroByteToRecv )
{
int32_t recvResult = 0;
uint8_t * pTransportTestBuffer = threadParameter[ TRANSPORT_TEST_INDEX ].transportTestBuffer;
NetworkContext_t * pNetworkContext = threadParameter[ TRANSPORT_TEST_INDEX ].pNetworkContext;
/* Receive with zero byte to recv should return negative value. */
recvResult = pTestTransport->recv( pNetworkContext,
&( pTransportTestBuffer[ TRANSPORT_TEST_BUFFER_PREFIX_GUARD_LENGTH ] ),
0 );
TEST_ASSERT_LESS_THAN_INT32_MESSAGE( 0, recvResult, "Transport interface recv with zero "
"byte to recv should return negative value." );
TEST_ASSERT_EACH_EQUAL_HEX8_MESSAGE( TRANSPORT_TEST_BUFFER_GUARD_PATTERN,
&( pTransportTestBuffer[ TRANSPORT_TEST_BUFFER_PREFIX_GUARD_LENGTH ] ),
TRANSPORT_TEST_BUFFER_WRITABLE_LENGTH,
"transportTestBuffer should not be altered." );
}
/*-----------------------------------------------------------*/
/**
* @brief Test transport interface with send one byte, receive and compare
*
* Test send receive beharivor in the following order.
* Send : 1 byte
* Send : ( TRANSPORT_TEST_BUFFER_WRITABLE_LENGTH - 1 ) bytes
* Receive : TRANSPORT_TEST_BUFFER_WRITABLE_LENGTH bytes
*/
TEST( Full_TransportInterfaceTest, Transport_SendOneByteRecvCompare )
{
/* Pointer of writable test buffer. */
uint8_t * pTransportTestBufferStart =
&( threadParameter[ TRANSPORT_TEST_INDEX ].transportTestBuffer[ TRANSPORT_TEST_BUFFER_PREFIX_GUARD_LENGTH ] );
NetworkContext_t * pNetworkContext = threadParameter[ TRANSPORT_TEST_INDEX ].pNetworkContext;
bool retValue;
/* Initialize the test data buffer. */
prvInitializeTestData( pTransportTestBufferStart, TRANSPORT_TEST_BUFFER_WRITABLE_LENGTH );
/* Send the test data to the server. */
retValue = prvTransportSendData( pTestTransport, pNetworkContext, pTransportTestBufferStart, 1U );
TEST_ASSERT_MESSAGE( ( retValue == true ), "Send test data failed." );
/* Send the rest test data to the server. */
retValue = prvTransportSendData( pTestTransport, pNetworkContext, &pTransportTestBufferStart[ 1 ],
( TRANSPORT_TEST_BUFFER_WRITABLE_LENGTH - 1U ) );
TEST_ASSERT_MESSAGE( ( retValue == true ), "Send test data failed." );
/* Receive the test data from server. */
retValue = prvTransportRecvData( pTestTransport, pNetworkContext, pTransportTestBufferStart,
TRANSPORT_TEST_BUFFER_WRITABLE_LENGTH );
TEST_ASSERT_MESSAGE( ( retValue == true ), "Receive test data failed." );
/* Compare the test data received from server. */
retValue = prvVerifyTestData( pTransportTestBufferStart, TRANSPORT_TEST_BUFFER_WRITABLE_LENGTH,
TRANSPORT_TEST_BUFFER_WRITABLE_LENGTH );
TEST_ASSERT_MESSAGE( ( retValue == true ), "Verify test data failed." );
}
/*-----------------------------------------------------------*/
/**
* @brief Test transport interface with send, receive one byte and compare.
*
* Test send receive beharivor in the following order.
* Send : TRANSPORT_TEST_BUFFER_WRITABLE_LENGTH bytes
* Receive : 1 byte
* Receive : ( TRANSPORT_TEST_BUFFER_WRITABLE_LENGTH - 1 ) bytes
*/
TEST( Full_TransportInterfaceTest, Transport_SendRecvOneByteCompare )
{
/* Pointer of writable test buffer. */
uint8_t * pTransportTestBufferStart =
&( threadParameter[ TRANSPORT_TEST_INDEX ].transportTestBuffer[ TRANSPORT_TEST_BUFFER_PREFIX_GUARD_LENGTH ] );
NetworkContext_t * pNetworkContext = threadParameter[ TRANSPORT_TEST_INDEX ].pNetworkContext;
bool retValue;
/* Initialize the test data buffer. */
prvInitializeTestData( pTransportTestBufferStart, TRANSPORT_TEST_BUFFER_WRITABLE_LENGTH );
/* Send the test data to the server. */
retValue = prvTransportSendData( pTestTransport, pNetworkContext, pTransportTestBufferStart,
TRANSPORT_TEST_BUFFER_WRITABLE_LENGTH );
TEST_ASSERT_MESSAGE( ( retValue == true ), "Send test data failed." );
/* Reset the buffer. The buffer after received data is verified in prvVerifyTestData. */
memset( pTransportTestBufferStart, TRANSPORT_TEST_BUFFER_GUARD_PATTERN, TRANSPORT_TEST_BUFFER_WRITABLE_LENGTH );
/* Receive one byte test data from server. */
retValue = prvTransportRecvData( pTestTransport, pNetworkContext, pTransportTestBufferStart, 1U );
TEST_ASSERT_MESSAGE( ( retValue == true ), "Receive test data failed." );
/* Compare the test data received from server. */
retValue = prvVerifyTestData( pTransportTestBufferStart, 1U, TRANSPORT_TEST_BUFFER_WRITABLE_LENGTH );
TEST_ASSERT_MESSAGE( ( retValue == true ), "Verify test data failed." );
/* Receive remain test data from server. */
retValue = prvTransportRecvData( pTestTransport, pNetworkContext, &pTransportTestBufferStart[ 1U ],
( TRANSPORT_TEST_BUFFER_WRITABLE_LENGTH - 1U ) );
TEST_ASSERT_MESSAGE( ( retValue == true ), "Receive test data failed." );
/* Compare remain test data received from server. */
retValue = prvVerifyTestData( pTransportTestBufferStart, TRANSPORT_TEST_BUFFER_WRITABLE_LENGTH,
TRANSPORT_TEST_BUFFER_WRITABLE_LENGTH );
TEST_ASSERT_MESSAGE( ( retValue == true ), "Verify test data failed." );