forked from FreeRTOS/coreHTTP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore_http_send_utest.c
More file actions
1962 lines (1644 loc) · 80.3 KB
/
core_http_send_utest.c
File metadata and controls
1962 lines (1644 loc) · 80.3 KB
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
/*
* coreHTTP
* Copyright (C) 2024 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.
*/
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#include "unity.h"
/* Include paths for public enums, structures, and macros. */
#include "core_http_client.h"
/* Private includes for internal macros. */
#include "core_http_client_private.h"
#include "mock_llhttp.h"
/* Template HTTP request for a HEAD request. */
#define HTTP_TEST_REQUEST_HEAD_HEADERS \
"HEAD /somedir/somepage.html HTTP/1.1\r\n" \
"test-header0: test-value0\r\n" \
"test-header1: test-value1\r\n" \
"test-header2: test-value2\r\n" \
"test-header3: test-value0\r\n" \
"test-header4: test-value1\r\n" \
"test-header5: test-value2\r\n" \
"\r\n"
#define HTTP_TEST_REQUEST_HEAD_HEADERS_LENGTH ( sizeof( HTTP_TEST_REQUEST_HEAD_HEADERS ) - 1U )
/* Template HTTP request for a PUT request. */
#define HTTP_TEST_REQUEST_PUT_HEADERS \
"PUT /somedir/somepage.html HTTP/1.1\r\n" \
"test-header1: test-value1\r\n" \
"test-header2: test-value2\r\n" \
"test-header3: test-value0\r\n" \
"test-header4: test-value1\r\n" \
"test-header5: test-value2\r\n" \
"\r\n"
#define HTTP_TEST_REQUEST_PUT_HEADERS_LENGTH ( sizeof( HTTP_TEST_REQUEST_PUT_HEADERS ) - 1U )
#define HTTP_TEST_REQUEST_PUT_BODY "abcdefghijklmnopqrstuvwxyz"
#define HTTP_TEST_REQUEST_PUT_BODY_LENGTH ( sizeof( HTTP_TEST_REQUEST_PUT_BODY ) - 1U )
#define HTTP_TEST_REQUEST_PUT_CONTENT_LENGTH_EXPECTED "Content-Length: 26\r\n" HTTP_HEADER_LINE_SEPARATOR
#define HTTP_TEST_REQUEST_PUT_CONTENT_LENGTH_EXPECTED_LENGTH ( sizeof( HTTP_TEST_REQUEST_PUT_CONTENT_LENGTH_EXPECTED ) - 1U )
/* Template HTTP request for a GET request. */
#define HTTP_TEST_REQUEST_GET_HEADERS \
"GET /somedir/somepage.html HTTP/1.1\r\n" \
"test-header1: test-value1\r\n" \
"test-header2: test-value2\r\n" \
"test-header3: test-value0\r\n" \
"test-header4: test-value1\r\n" \
"test-header5: test-value2\r\n" \
"\r\n"
#define HTTP_TEST_REQUEST_GET_HEADERS_LENGTH ( sizeof( HTTP_TEST_REQUEST_GET_HEADERS ) - 1U )
/* HTTP OK Status-Line. */
#define HTTP_STATUS_LINE_OK "HTTP/1.1 200 OK\r\n"
#define HTTP_STATUS_LINE_NO_REASON_PHRASE "HTTP/1.1 200\r\n"
#define HTTP_STATUS_CODE_OK 200
/* Various header lines for test response templates. */
#define HTTP_TEST_CONTENT_LENGTH_HEADER_LINE "Content-Length: 43\r\n"
#define HTTP_TEST_DATE_HEADER_LINE "Date: Sun, 14 Jul 2019 06:07:52 GMT\r\n"
#define HTTP_TEST_ETAG_HEADER_LINE "ETag: \"3356-5233\"\r\n"
#define HTTP_TEST_VARY_HEADER_LINE "Vary: *\r\n"
#define HTTP_TEST_P3P_HEADER_LINE "P3P: CP=\"This is not a P3P policy\"\r\n"
#define HTTP_TEST_XSERVER_HEADER_LINE "xserver: www1021\r\n"
#define HTTP_TEST_CONNECTION_CLOSE_HEADER_LINE "Connection: close\r\n"
#define HTTP_TEST_CONNECTION_KEEP_ALIVE_HEADER_LINE "Connection: keep-alive\r\n"
#define HTTP_TEST_TRANSFER_ENCODING_CHUNKED_HEADER_LINE "Transfer-Encoding: chunked\r\n"
/* Partial header field and value for testing partial header field and value
* handling in parser callback. */
#define HTTP_TEST_CONTENT_LENGTH_PARTIAL_HEADER_FIELD "Content-Len"
#define HTTP_TEST_CONTENT_LENGTH_PARTIAL_HEADER_VALUE "Content-Length: 4"
/* Template HTTP HEAD response. */
#define HTTP_TEST_RESPONSE_HEAD \
HTTP_STATUS_LINE_OK \
HTTP_TEST_CONTENT_LENGTH_HEADER_LINE \
HTTP_TEST_CONNECTION_CLOSE_HEADER_LINE \
HTTP_TEST_DATE_HEADER_LINE \
HTTP_TEST_ETAG_HEADER_LINE \
HTTP_TEST_VARY_HEADER_LINE \
HTTP_TEST_P3P_HEADER_LINE \
HTTP_TEST_XSERVER_HEADER_LINE HTTP_HEADER_LINE_SEPARATOR
#define HTTP_TEST_RESPONSE_HEAD_LENGTH ( sizeof( HTTP_TEST_RESPONSE_HEAD ) - 1U )
#define HTTP_TEST_RESPONSE_HEAD_HEADER_COUNT 7
#define HTTP_TEST_RESPONSE_HEAD_CONTENT_LENGTH 43
#define HTTP_TEST_RESPONSE_HEAD_PARTIAL_HEADER_FIELD_LENGTH ( sizeof( HTTP_STATUS_LINE_OK ) + sizeof( HTTP_TEST_CONTENT_LENGTH_PARTIAL_HEADER_FIELD ) - 2U )
#define HTTP_TEST_RESPONSE_HEAD_PARTIAL_HEADER_VALUE_LENGTH ( sizeof( HTTP_STATUS_LINE_OK ) + sizeof( HTTP_TEST_CONTENT_LENGTH_PARTIAL_HEADER_VALUE ) - 2U )
#define HTTP_TEST_RESPONSE_HEAD_2 \
HTTP_STATUS_LINE_NO_REASON_PHRASE \
HTTP_TEST_CONTENT_LENGTH_HEADER_LINE \
HTTP_TEST_CONNECTION_CLOSE_HEADER_LINE \
HTTP_TEST_DATE_HEADER_LINE \
HTTP_TEST_ETAG_HEADER_LINE \
HTTP_TEST_VARY_HEADER_LINE \
HTTP_TEST_P3P_HEADER_LINE \
HTTP_TEST_XSERVER_HEADER_LINE HTTP_HEADER_LINE_SEPARATOR
#define HTTP_TEST_RESPONSE_HEAD_2_LENGTH ( sizeof( HTTP_TEST_RESPONSE_HEAD_2 ) - 1U )
#define HTTP_TEST_RESPONSE_HEAD_2_HEADER_COUNT 7
#define HTTP_TEST_RESPONSE_HEAD_2_CONTENT_LENGTH 43
#define HTTP_TEST_RESPONSE_HEAD_2_PARTIAL_HEADER_FIELD_LENGTH ( sizeof( HTTP_STATUS_LINE_NO_REASON_PHRASE ) + sizeof( HTTP_TEST_CONTENT_LENGTH_PARTIAL_HEADER_FIELD ) - 2U )
#define HTTP_TEST_RESPONSE_HEAD_2_PARTIAL_HEADER_VALUE_LENGTH ( sizeof( HTTP_STATUS_LINE_NO_REASON_PHRASE ) + sizeof( HTTP_TEST_CONTENT_LENGTH_PARTIAL_HEADER_VALUE ) - 2U )
/* Template HTTP PUT response. This has no body. */
#define HTTP_TEST_RESPONSE_PUT \
HTTP_STATUS_LINE_OK \
HTTP_TEST_CONNECTION_KEEP_ALIVE_HEADER_LINE \
HTTP_TEST_DATE_HEADER_LINE \
HTTP_TEST_ETAG_HEADER_LINE \
HTTP_TEST_VARY_HEADER_LINE \
HTTP_TEST_P3P_HEADER_LINE \
HTTP_TEST_XSERVER_HEADER_LINE HTTP_HEADER_LINE_SEPARATOR
#define HTTP_TEST_RESPONSE_PUT_LENGTH ( sizeof( HTTP_TEST_RESPONSE_PUT ) - 1U )
#define HTTP_TEST_RESPONSE_PUT_HEADER_COUNT 6
/* Template HTTP GET response. */
#define HTTP_TEST_RESPONSE_GET \
HTTP_TEST_RESPONSE_HEAD \
"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopq"
#define HTTP_TEST_RESPONSE_GET_LENGTH ( sizeof( HTTP_TEST_RESPONSE_GET ) - 1U )
#define HTTP_TEST_RESPONSE_GET_HEADER_COUNT HTTP_TEST_RESPONSE_HEAD_HEADER_COUNT
#define HTTP_TEST_RESPONSE_GET_HEADERS_LENGTH ( HTTP_TEST_RESPONSE_HEAD_LENGTH - ( sizeof( HTTP_STATUS_LINE_OK ) - 1U ) )
#define HTTP_TEST_RESPONSE_GET_BODY_LENGTH HTTP_TEST_RESPONSE_HEAD_CONTENT_LENGTH
#define HTTP_TEST_RESPONSE_GET_CONTENT_LENGTH HTTP_TEST_RESPONSE_HEAD_CONTENT_LENGTH
#define HTTP_TEST_RESPONSE_GET_PARTIAL_BODY_LENGTH ( HTTP_TEST_RESPONSE_GET_LENGTH - 13U )
/* Template HTTP transfer-encoding chunked response. */
#define HTTP_TEST_RESPONSE_CHUNKED \
HTTP_STATUS_LINE_OK \
HTTP_TEST_TRANSFER_ENCODING_CHUNKED_HEADER_LINE \
HTTP_TEST_CONNECTION_KEEP_ALIVE_HEADER_LINE \
HTTP_TEST_DATE_HEADER_LINE \
HTTP_TEST_ETAG_HEADER_LINE \
HTTP_TEST_VARY_HEADER_LINE \
HTTP_TEST_P3P_HEADER_LINE \
HTTP_TEST_XSERVER_HEADER_LINE HTTP_HEADER_LINE_SEPARATOR \
"b\r\n" \
"abcdefghijk\r\n" \
"c\r\n" \
"lmnopqrstuvw\r\n" \
"3\r\n" \
"xyz\r\n" \
"0\r\n" \
"\r\n"
#define HTTP_TEST_RESPONSE_CHUNKED_LENGTH ( sizeof( HTTP_TEST_RESPONSE_CHUNKED ) - 1U )
#define HTTP_TEST_RESPONSE_CHUNKED_HEADER_COUNT 7
#define HTTP_TEST_RESPONSE_CHUNKED_BODY_LENGTH 26
#define HTTP_TEST_RESPONSE_CHUNKED_HEADERS_LENGTH \
sizeof( HTTP_TEST_TRANSFER_ENCODING_CHUNKED_HEADER_LINE ) + \
sizeof( HTTP_TEST_CONNECTION_KEEP_ALIVE_HEADER_LINE ) + \
sizeof( HTTP_TEST_DATE_HEADER_LINE ) + \
sizeof( HTTP_TEST_ETAG_HEADER_LINE ) + \
sizeof( HTTP_TEST_VARY_HEADER_LINE ) + \
sizeof( HTTP_TEST_P3P_HEADER_LINE ) + \
sizeof( HTTP_TEST_XSERVER_HEADER_LINE ) + \
HTTP_HEADER_LINE_SEPARATOR_LEN - 7U
/* Template HTTP response with no headers. */
#define HTTP_TEST_RESPONSE_NO_HEADERS \
HTTP_STATUS_LINE_OK HTTP_HEADER_LINE_SEPARATOR
#define HTTP_TEST_RESPONSE_NO_HEADERS_LENGTH ( sizeof( HTTP_TEST_RESPONSE_NO_HEADERS ) - 1U )
/* Test buffer to share among the test. */
#define HTTP_TEST_BUFFER_LENGTH 1024
/* Mock a NetworkContext structure for the test. */
struct NetworkContext
{
int mocked;
};
static uint8_t httpBuffer[ HTTP_TEST_BUFFER_LENGTH ] = { 0 };
/* Tests are run sequentially. If a response has these variables, then they
* will be set during the onHeaderCallback(). */
static uint8_t hasConnectionClose = 0;
static uint8_t hasConnectionKeepAlive = 0;
static size_t contentLength = 0;
/* The count of times a test invoked the onHeaderCallback(). */
static uint8_t headerCallbackCount = 0;
/* The count of times a test invoked the transport send interface. */
static uint8_t sendCurrentCall = 0;
/* Set this to 1 to enable checking that the Content-Length header generated is correct. */
static uint8_t checkContentLength = 0;
/* The test sets this variable to indicate at which call count of transport send
* to return an error from. */
static uint8_t sendErrorCall = 0;
/* The test sets this variable to indicate at which call count of transport send
* to send less bytes than indicated. */
static uint8_t sendPartialCall = 0;
/* The tests set this variable to indicate at which call count of transport send
* to return zero from. */
static uint8_t sendTimeoutCall = 0;
/* The network data to receive. */
static uint8_t * pNetworkData = NULL;
/* The length of the network data to receive. */
static size_t networkDataLen = 0;
/* The number of bytes to send in the first call to the transport receive
* interface. */
static size_t firstPartBytes = 0;
/* The count of times a test invoked the transport receive interface. */
static uint8_t recvCurrentCall = 0;
/* The test sets this variable to indicate which call count count of transport
* receive to return zero from. */
static uint8_t recvTimeoutCall = 0;
/* The count of times a mocked llhttp_execute callback has been invoked. */
static uint8_t httpParserExecuteCallCount;
/* The error to set to the parsing context when the llhttp_execute_error
* callback is invoked. */
static enum llhttp_errno httpParsingErrno;
/* Response shared among the tests. */
static HTTPResponse_t response = { 0 };
/* Transport interface shared among the tests. */
static TransportInterface_t transportInterface = { 0 };
/* Request headers shared among the tests. */
static HTTPRequestHeaders_t requestHeaders = { 0 };
/* Header parsing callback shared among the tests. */
static HTTPClient_ResponseHeaderParsingCallback_t headerParsingCallback = { 0 };
/* Flag to indicate this callback is called. */
static int statusCompleteCallbackFlag = 0;
/* A mocked timer query function that increments on every call. */
static uint32_t getTestTime( void )
{
static uint32_t entryTime = 0;
return entryTime++;
}
/* Application callback for intercepting the headers during the parse of a new
* response from the mocked network interface. */
static void onHeaderCallback( void * pContext,
const char * fieldLoc,
size_t fieldLen,
const char * valueLoc,
size_t valueLen,
uint16_t statusCode )
{
( void ) pContext;
( void ) statusCode;
if( strncmp( fieldLoc, "Connection", fieldLen ) == 0 )
{
if( strncmp( valueLoc, "keep-alive", valueLen ) == 0 )
{
hasConnectionKeepAlive = 1;
}
else if( strncmp( valueLoc, "close", valueLen ) == 0 )
{
hasConnectionClose = 1;
}
}
else if( strncmp( fieldLoc, "Content-Length", fieldLen ) == 0 )
{
contentLength = strtoul( valueLoc, NULL, 10 );
}
headerCallbackCount++;
}
/* Successful application transport send interface. */
static int32_t transportSendSuccess( NetworkContext_t * pNetworkContext,
const void * pBuffer,
size_t bytesToWrite )
{
int32_t retVal = bytesToWrite;
( void ) pNetworkContext;
sendCurrentCall++;
if( sendTimeoutCall == sendCurrentCall )
{
return 0;
}
if( sendPartialCall == sendCurrentCall )
{
retVal -= 1;
}
if( checkContentLength == 1U )
{
if( sendCurrentCall == 1U )
{
size_t contentLengthAndHeaderEndLen = HTTP_TEST_REQUEST_PUT_CONTENT_LENGTH_EXPECTED_LENGTH;
char * pContentLengthStart = ( ( ( char * ) pBuffer ) + bytesToWrite ) - contentLengthAndHeaderEndLen;
TEST_ASSERT_GREATER_OR_EQUAL( contentLengthAndHeaderEndLen, bytesToWrite );
TEST_ASSERT_EQUAL_MEMORY( HTTP_TEST_REQUEST_PUT_CONTENT_LENGTH_EXPECTED,
pContentLengthStart,
HTTP_TEST_REQUEST_PUT_CONTENT_LENGTH_EXPECTED_LENGTH );
}
}
return retVal;
}
/* Application transport send interface that returns a network error depending
* on the call count. Set sendErrorCall to 0 to return an error on the
* first call. Set sendErrorCall to 1 to return an error on the second call. */
static int32_t transportSendNetworkError( NetworkContext_t * pNetworkContext,
const void * pBuffer,
size_t bytesToWrite )
{
int32_t retVal = bytesToWrite;
( void ) pNetworkContext;
( void ) pBuffer;
sendCurrentCall++;
if( sendErrorCall == sendCurrentCall )
{
retVal = -1;
}
return retVal;
}
/* Application transport receive interface that sends the bytes specified in
* firstPartBytes on the first call, then sends the rest of the response in the
* second call. The response to send is set in pNetworkData and the current
* call count is kept track of in recvCurrentCall. This function will return
* zero (timeout condition) when recvTimeoutCall matches recvCurrentCall. */
static int32_t transportRecvSuccess( NetworkContext_t * pNetworkContext,
void * pBuffer,
size_t bytesToRead )
{
( void ) pNetworkContext;
size_t bytesToCopy = 0;
/* Increment this call count. */
recvCurrentCall++;
/* To test stopping in the middle of a response message, check that the
* flags are set. */
if( recvTimeoutCall == recvCurrentCall )
{
return 0;
}
/* If this is the first call, then copy the specific first bytes. */
if( recvCurrentCall == 1 )
{
bytesToCopy = firstPartBytes;
}
/* Otherwise copy the rest of the network data. */
else
{
bytesToCopy = networkDataLen;
}
if( bytesToCopy > bytesToRead )
{
bytesToCopy = bytesToRead;
}
memcpy( pBuffer, pNetworkData, bytesToCopy );
pNetworkData += bytesToCopy;
networkDataLen -= bytesToCopy;
return bytesToCopy;
}
/* Application transport receive that return a network error. */
static int32_t transportRecvNetworkError( NetworkContext_t * pNetworkContext,
void * pBuffer,
size_t bytesToRead )
{
( void ) pNetworkContext;
( void ) pBuffer;
( void ) bytesToRead;
return -1;
}
/* llhttp_init callback that sets the parser settings field. */
static void llhttp_init_setup( llhttp_t * parser,
enum llhttp_type type,
const llhttp_settings_t * settings,
int cmock_num_calls )
{
( void ) cmock_num_calls;
parser->type = type;
/* Remove const qualifier. llhttp does this too. */
parser->settings = ( llhttp_settings_t * ) settings;
parser->error = HPE_OK;
}
/* llhttp_get_errno callback that returns the errno value. */
llhttp_errno_t llhttp_get_errno_cb( const llhttp_t * parser,
int cmock_num_calls )
{
( void ) cmock_num_calls;
return parser->error;
}
/* Mocked llhttp_execute callback that sets the internal errno. */
static llhttp_errno_t llhttp_execute_error( llhttp_t * pParser,
const char * pData,
size_t len,
int cmock_num_calls )
{
( void ) pData;
( void ) len;
( void ) cmock_num_calls;
pParser->error = httpParsingErrno;
return httpParsingErrno;
}
/* Mock helper that parses the status line starting from pNext. */
static void helper_parse_status_line( const char ** pNext,
llhttp_t * pParser,
const llhttp_settings_t * pSettings )
{
const char * pReasonPhraseStart = NULL;
const char * pNextLineStart = NULL;
size_t reasonPhraseStartLen = 0;
/* For purposes of unit testing the response is well formed in the non-error
* cases, so the reason-phrase is always after HTTP/1.1 and the three digit
* status code. strchr() is used only for unit testing where test input are
* always string literals. strchr() should not be used in application code. */
*pNext = strchr( *pNext, SPACE_CHARACTER ); /* Get the space before the status-code. */
*pNext += SPACE_CHARACTER_LEN;
/* pNext points to the status code now. */
pReasonPhraseStart = strchr( *pNext, SPACE_CHARACTER );
pReasonPhraseStart = &( pReasonPhraseStart[ SPACE_CHARACTER_LEN ] );
pNextLineStart = strstr( *pNext, HTTP_HEADER_LINE_SEPARATOR );
pNextLineStart = &( pNextLineStart[ HTTP_HEADER_LINE_SEPARATOR_LEN ] );
pParser->status_code = 200;
/* Check if the reason phrase exist in the header and call the corresponding callback.
* Reason phrase "OK" exists in the response "HTTP/1.1 200 OK\r\n". The callback
* on_status is called.
* Reason phrase doesn't exist in the response "HTTP/1.1 200\r\n". The callback
* on_status_complete is called. */
if( pNextLineStart > pReasonPhraseStart )
{
reasonPhraseStartLen = ( size_t ) ( pNextLineStart - pReasonPhraseStart );
reasonPhraseStartLen = reasonPhraseStartLen - HTTP_HEADER_LINE_SEPARATOR_LEN;
pSettings->on_status( pParser,
pReasonPhraseStart,
reasonPhraseStartLen );
*pNext = pNextLineStart;
}
else
{
statusCompleteCallbackFlag = 1;
pSettings->on_status_complete( pParser );
*pNext = pNextLineStart;
}
}
/* Mock helper that parses all of the headers starting from pNext. */
static void helper_parse_headers( const char ** pNext,
llhttp_t * pParser,
const llhttp_settings_t * pSettings )
{
const char * pHeaderFieldStart = NULL;
size_t headerFieldLen = 0;
const char * pHeaderValueStart = NULL;
size_t headerValueLen = 0;
while( **pNext != '\r' )
{
pHeaderFieldStart = *pNext;
*pNext = strstr( *pNext, HTTP_HEADER_FIELD_SEPARATOR );
headerFieldLen = ( size_t ) ( *pNext - pHeaderFieldStart );
pSettings->on_header_field( pParser, pHeaderFieldStart, headerFieldLen );
*pNext += HTTP_HEADER_FIELD_SEPARATOR_LEN;
pHeaderValueStart = *pNext;
*pNext = strstr( *pNext, HTTP_HEADER_LINE_SEPARATOR );
headerValueLen = ( size_t ) ( *pNext - pHeaderValueStart );
pSettings->on_header_value( pParser, pHeaderValueStart, headerValueLen );
*pNext += HTTP_HEADER_LINE_SEPARATOR_LEN;
}
}
/* Mock helper that parses the end of the headers starting from pNext. pNext
* will point to the start of the body after this is finished. */
static void helper_parse_headers_finish( const char ** pNext,
llhttp_t * pParser,
const llhttp_settings_t * pSettings,
uint8_t * isHeadResponse )
{
uint8_t isHeadResponseReturned = 0;
pParser->content_length = contentLength;
if( hasConnectionClose )
{
pParser->flags |= F_CONNECTION_CLOSE;
}
if( hasConnectionKeepAlive )
{
pParser->flags |= F_CONNECTION_KEEP_ALIVE;
}
isHeadResponseReturned = pSettings->on_headers_complete( pParser );
if( isHeadResponse != NULL )
{
*isHeadResponse = isHeadResponseReturned;
}
*pNext += HTTP_HEADER_LINE_SEPARATOR_LEN;
}
/* Mock helper that parses the response body starting from pNext. */
static void helper_parse_body( const char ** pNext,
llhttp_t * pParser,
const llhttp_settings_t * pSettings,
uint8_t isHeadResponse,
const char * pData,
size_t len )
{
const char * pBody = NULL;
size_t bodyLen = 0;
pBody = *pNext;
if( isHeadResponse == 0 )
{
bodyLen = ( size_t ) ( len - ( size_t ) ( pBody - pData ) );
if( bodyLen > 0 )
{
pSettings->on_body( pParser, pBody, bodyLen );
}
}
}
/* Mocked llhttp_execute callback that expects a whole response to be in
* the given data to parse. */
static llhttp_errno_t llhttp_execute_whole_response( llhttp_t * pParser,
const char * pData,
size_t len,
int cmock_num_calls )
{
( void ) cmock_num_calls;
const char * pNext = pData;
uint8_t isHeadResponse = 0;
llhttp_settings_t * pSettings = ( llhttp_settings_t * ) pParser->settings;
pSettings->on_message_begin( pParser );
helper_parse_status_line( &pNext, pParser, pSettings );
helper_parse_headers( &pNext, pParser, pSettings );
helper_parse_headers_finish( &pNext, pParser, pSettings, &isHeadResponse );
helper_parse_body( &pNext, pParser, pSettings, isHeadResponse, pData, len );
pSettings->on_message_complete( pParser );
httpParserExecuteCallCount++;
return HPE_OK;
}
/* Mocked llhttp_execute callback that expects upgrade header in HTTP response. */
static llhttp_errno_t llhttp_execute_paused_upgrade( llhttp_t * pParser,
const char * pData,
size_t len,
int cmock_num_calls )
{
( void ) cmock_num_calls;
llhttp_errno_t eReturn = HPE_OK;
if( httpParserExecuteCallCount == 0 )
{
eReturn = HPE_PAUSED_UPGRADE;
}
llhttp_execute_whole_response( pParser, pData, len, 0 );
return eReturn;
}
/* Mocked llhttp_execute callback that will be called the first time on the
* response message up to the middle of the first header field, then the second
* time on the response message from the middle of the first header field to the
* end. */
static llhttp_errno_t llhttp_execute_partial_header_field( llhttp_t * pParser,
const char * pData,
size_t len,
int cmock_num_calls )
{
( void ) cmock_num_calls;
const char * pNext = pData;
uint8_t isHeadResponse = 0;
const char * pHeaderFieldStart = NULL;
size_t headerFieldLen = 0;
llhttp_settings_t * pSettings = ( llhttp_settings_t * ) pParser->settings;
if( httpParserExecuteCallCount == 0 )
{
pSettings->on_message_begin( pParser );
helper_parse_status_line( &pNext, pParser, pSettings );
/* pNext now points to the start of the partial header field. */
pHeaderFieldStart = pNext;
headerFieldLen = len - ( size_t ) ( pHeaderFieldStart - pData );
pSettings->on_header_field( pParser, pHeaderFieldStart, headerFieldLen );
}
else
{
/* For testing of invoking llhttp_execute() with a parsing length
* of zero, when data had been previously parsed. */
if( len == 0 )
{
pParser->error = HPE_INVALID_EOF_STATE;
return HPE_INVALID_EOF_STATE;
}
helper_parse_headers( &pNext, pParser, pSettings );
helper_parse_headers_finish( &pNext, pParser, pSettings, &isHeadResponse );
helper_parse_body( &pNext, pParser, pSettings, isHeadResponse, pData, len );
pSettings->on_message_complete( pParser );
}
httpParserExecuteCallCount++;
return HPE_OK;
}
/* Mocked llhttp_execute callback that will be called the first time on the
* response message up to the middle of the first header value, then the second
* time on the response message from the middle of the first header value to the
* end. */
static llhttp_errno_t llhttp_execute_partial_header_value( llhttp_t * pParser,
const char * pData,
size_t len,
int cmock_num_calls )
{
( void ) cmock_num_calls;
const char * pNext = pData;
uint8_t isHeadResponse = 0;
const char * pHeaderFieldStart = NULL;
size_t headerFieldLen = 0;
const char * pHeaderValueStart = NULL;
size_t headerValueLen = 0;
llhttp_settings_t * pSettings = ( llhttp_settings_t * ) pParser->settings;
if( httpParserExecuteCallCount == 0 )
{
pSettings->on_message_begin( pParser );
helper_parse_status_line( &pNext, pParser, pSettings );
/* Get the first header field. */
pHeaderFieldStart = pNext;
pNext = strstr( pNext, HTTP_HEADER_FIELD_SEPARATOR );
headerFieldLen = ( size_t ) ( pNext - pHeaderFieldStart );
pSettings->on_header_field( pParser, pHeaderFieldStart, headerFieldLen );
pNext += HTTP_HEADER_FIELD_SEPARATOR_LEN;
/* pNext now points to the start of the partial header value. */
pHeaderValueStart = pNext;
headerValueLen = len - ( size_t ) ( pHeaderValueStart - pData );
pSettings->on_header_value( pParser, pHeaderValueStart, headerValueLen );
}
else
{
/* In this second call to llhttp_execute mock, pData now starts
* at the partial header value. */
pHeaderValueStart = pNext;
pNext = strstr( pNext, HTTP_HEADER_LINE_SEPARATOR );
headerValueLen = ( size_t ) ( pNext - pHeaderValueStart );
pSettings->on_header_value( pParser, pHeaderValueStart, headerValueLen );
pNext += HTTP_HEADER_FIELD_SEPARATOR_LEN;
helper_parse_headers( &pNext, pParser, pSettings );
helper_parse_headers_finish( &pNext, pParser, pSettings, &isHeadResponse );
helper_parse_body( &pNext, pParser, pSettings, isHeadResponse, pData, len );
pSettings->on_message_complete( pParser );
}
httpParserExecuteCallCount++;
return HPE_OK;
}
/* Mocked llhttp_execute callback that will be called the first time on the
* response message up to the middle of the body, then the second time on the
* response message from the middle of the body to the end. */
static llhttp_errno_t llhttp_execute_partial_body( llhttp_t * pParser,
const char * pData,
size_t len,
int cmock_num_calls )
{
( void ) cmock_num_calls;
const char * pNext = pData;
llhttp_settings_t * pSettings = ( llhttp_settings_t * ) pParser->settings;
if( httpParserExecuteCallCount == 0 )
{
pSettings->on_message_begin( pParser );
helper_parse_status_line( &pNext, pParser, pSettings );
helper_parse_headers( &pNext, pParser, pSettings );
helper_parse_headers_finish( &pNext, pParser, pSettings, NULL );
helper_parse_body( &pNext, pParser, pSettings, 0, pData, len );
}
else
{
/* Parse the rest of the body. */
helper_parse_body( &pNext, pParser, pSettings, 0, pData, len );
pSettings->on_message_complete( pParser );
}
httpParserExecuteCallCount++;
return HPE_OK;
}
/* Mocked llhttp_execute callback that will be called when partial body has been
* received from the network. It returns HPE_PAUSED to mimic stopping parsing
* the body because user set HTTP_RESPONSE_DO_NOT_PARSE_BODY_FLAG. */
static llhttp_errno_t llhttp_execute_partial_body_do_not_parse( llhttp_t * pParser,
const char * pData,
size_t len,
int cmock_num_calls )
{
( void ) cmock_num_calls;
( void ) len;
const char * pNext = pData;
llhttp_settings_t * pSettings = ( llhttp_settings_t * ) pParser->settings;
pSettings->on_message_begin( pParser );
helper_parse_status_line( &pNext, pParser, pSettings );
helper_parse_headers( &pNext, pParser, pSettings );
helper_parse_headers_finish( &pNext, pParser, pSettings, NULL );
pParser->error_pos = pNext;
pParser->error = HPE_PAUSED;
return HPE_PAUSED;
}
/* Mocked llhttp_execute callback that will be on a response of type
* transfer-encoding chunked. */
static llhttp_errno_t llhttp_execute_chunked_body( llhttp_t * pParser,
const char * pData,
size_t len,
int cmock_num_calls )
{
( void ) cmock_num_calls;
( void ) len;
const char * pNext = pData;
uint8_t isHeadResponse = 0;
const char * pBody = NULL;
size_t bodyLen = 0;
const char * pChunkHeader = NULL;
llhttp_settings_t * pSettings = ( llhttp_settings_t * ) pParser->settings;
pSettings->on_message_begin( pParser );
helper_parse_status_line( &pNext, pParser, pSettings );
helper_parse_headers( &pNext, pParser, pSettings );
helper_parse_headers_finish( &pNext, pParser, pSettings, &isHeadResponse );
/* pNext now points to the start of the first chunk header. Loop until the
* last chunk header is detected. A "\r\n" follows the last chunk header
* (length 0 chunk header). */
while( *pNext != '\r' )
{
pChunkHeader = pNext;
bodyLen = ( size_t ) strtoul( pChunkHeader, NULL, 16 );
pNext = strstr( pNext, HTTP_HEADER_LINE_SEPARATOR );
pNext += HTTP_HEADER_LINE_SEPARATOR_LEN;
pBody = pNext;
if( bodyLen > 0 )
{
pSettings->on_body( pParser, pBody, bodyLen );
pNext = strstr( pNext, HTTP_HEADER_LINE_SEPARATOR );
pNext += HTTP_HEADER_LINE_SEPARATOR_LEN;
}
}
pSettings->on_message_complete( pParser );
httpParserExecuteCallCount++;
return HPE_OK;
}
/* ============================ UNITY FIXTURES ============================== */
/* Called before each test case. */
void setUp( void )
{
/* Setup global testing variables. */
hasConnectionClose = 0;
hasConnectionKeepAlive = 0;
contentLength = ULLONG_MAX;
headerCallbackCount = 0;
sendCurrentCall = 0;
sendErrorCall = 0;
sendPartialCall = 0;
sendTimeoutCall = 0;
checkContentLength = 0;
pNetworkData = ( uint8_t * ) HTTP_TEST_RESPONSE_HEAD;
networkDataLen = HTTP_TEST_RESPONSE_HEAD_LENGTH;
firstPartBytes = networkDataLen;
recvCurrentCall = 0;
recvTimeoutCall = UINT8_MAX;
httpParserExecuteCallCount = 0;
httpParsingErrno = HPE_OK;
transportInterface.recv = transportRecvSuccess;
transportInterface.send = transportSendSuccess;
transportInterface.pNetworkContext = NULL;
requestHeaders.pBuffer = httpBuffer;
requestHeaders.bufferLen = sizeof( httpBuffer );
memcpy( requestHeaders.pBuffer, HTTP_TEST_REQUEST_HEAD_HEADERS, HTTP_TEST_REQUEST_HEAD_HEADERS_LENGTH );
requestHeaders.headersLen = HTTP_TEST_REQUEST_HEAD_HEADERS_LENGTH;
memset( &response, 0, sizeof( HTTPResponse_t ) );
headerParsingCallback.onHeaderCallback = onHeaderCallback;
headerParsingCallback.pContext = NULL;
response.pBuffer = httpBuffer;
response.bufferLen = sizeof( httpBuffer );
response.pHeaderParsingCallback = &headerParsingCallback;
statusCompleteCallbackFlag = 0;
/* Ignore third-party init functions that return void. */
llhttp_init_Ignore();
llhttp_init_Stub( llhttp_init_setup );
llhttp_get_errno_Stub( llhttp_get_errno_cb );
llhttp_settings_init_Ignore();
llhttp_errno_name_IgnoreAndReturn( "Dummy" );
llhttp_get_error_reason_IgnoreAndReturn( "Dummy unit test print." );
}
/* ======================== Testing HTTPClient_Send ========================= */
/* Test successfully parsing a response to a HEAD request. The full response
* message is present in the response buffer on the first network read. */
void test_HTTPClient_Send_HEAD_request_parse_whole_response( void )
{
HTTPStatus_t returnStatus = HTTPSuccess;
llhttp_execute_Stub( llhttp_execute_whole_response );
returnStatus = HTTPClient_Send( &transportInterface,
&requestHeaders,
NULL,
0,
&response,
0 );
TEST_ASSERT_EQUAL( HTTPSuccess, returnStatus );
TEST_ASSERT_EQUAL( NULL, response.pBody );
TEST_ASSERT_EQUAL( 0U, response.bodyLen );
TEST_ASSERT_EQUAL( response.pBuffer + ( sizeof( HTTP_STATUS_LINE_OK ) - 1U ), response.pHeaders );
TEST_ASSERT_EQUAL( HTTP_TEST_RESPONSE_HEAD_LENGTH - ( sizeof( HTTP_STATUS_LINE_OK ) - 1U ) - HTTP_HEADER_END_INDICATOR_LEN,
response.headersLen );
TEST_ASSERT_EQUAL( HTTP_STATUS_CODE_OK, response.statusCode );
TEST_ASSERT_EQUAL( HTTP_TEST_RESPONSE_HEAD_CONTENT_LENGTH, response.contentLength );
TEST_ASSERT_EQUAL( HTTP_TEST_RESPONSE_HEAD_HEADER_COUNT, response.headerCount );
TEST_ASSERT_BITS_HIGH( HTTP_RESPONSE_CONNECTION_CLOSE_FLAG, response.respFlags );
TEST_ASSERT_BITS_LOW( HTTP_RESPONSE_CONNECTION_KEEP_ALIVE_FLAG, response.respFlags );
}
/*-----------------------------------------------------------*/
/* Test successfully parsing a response to a HEAD request. The full response
* message is present in the response buffer on the first network read. */
void test_HTTPClient_Send_HEAD_request_no_parse_body( void )
{
HTTPStatus_t returnStatus = HTTPSuccess;
llhttp_execute_Stub( llhttp_execute_whole_response );
response.respOptionFlags |= HTTP_RESPONSE_DO_NOT_PARSE_BODY_FLAG;
returnStatus = HTTPClient_Send( &transportInterface,
&requestHeaders,
NULL,
0,
&response,
0 );
TEST_ASSERT_EQUAL( HTTPSuccess, returnStatus );
TEST_ASSERT_EQUAL( NULL, response.pBody );
TEST_ASSERT_EQUAL( 0U, response.bodyLen );
TEST_ASSERT_EQUAL( response.pBuffer + ( sizeof( HTTP_STATUS_LINE_OK ) - 1U ), response.pHeaders );
TEST_ASSERT_EQUAL( HTTP_TEST_RESPONSE_HEAD_LENGTH - ( sizeof( HTTP_STATUS_LINE_OK ) - 1U ) - HTTP_HEADER_END_INDICATOR_LEN,
response.headersLen );
TEST_ASSERT_EQUAL( HTTP_STATUS_CODE_OK, response.statusCode );
TEST_ASSERT_EQUAL( HTTP_TEST_RESPONSE_HEAD_CONTENT_LENGTH, response.contentLength );
TEST_ASSERT_EQUAL( HTTP_TEST_RESPONSE_HEAD_HEADER_COUNT, response.headerCount );
TEST_ASSERT_BITS_HIGH( HTTP_RESPONSE_CONNECTION_CLOSE_FLAG, response.respFlags );
TEST_ASSERT_BITS_LOW( HTTP_RESPONSE_CONNECTION_KEEP_ALIVE_FLAG, response.respFlags );
}
/*-----------------------------------------------------------*/
/* Test successfully parsing a response to a HEAD request. The full response
* message is present in the response buffer on the first network read. The response
* contains a status code but without a reason string. The on_status_complete is called
* in this case. */
void test_HTTPClient_Send_HEAD_request_parse_whole_response_no_reason_string( void )
{
HTTPStatus_t returnStatus = HTTPSuccess;
pNetworkData = ( uint8_t * ) HTTP_TEST_RESPONSE_HEAD_2;
networkDataLen = HTTP_TEST_RESPONSE_HEAD_2_LENGTH;
llhttp_execute_Stub( llhttp_execute_whole_response );
returnStatus = HTTPClient_Send( &transportInterface,
&requestHeaders,
NULL,
0,
&response,
0 );
TEST_ASSERT_EQUAL( HTTPSuccess, returnStatus );
TEST_ASSERT_EQUAL( NULL, response.pBody );
TEST_ASSERT_EQUAL( 0U, response.bodyLen );
TEST_ASSERT_EQUAL( response.pBuffer + ( sizeof( HTTP_STATUS_LINE_NO_REASON_PHRASE ) - 1U ), response.pHeaders );
TEST_ASSERT_EQUAL( HTTP_TEST_RESPONSE_HEAD_2_LENGTH - ( sizeof( HTTP_STATUS_LINE_NO_REASON_PHRASE ) - 1U ) - HTTP_HEADER_END_INDICATOR_LEN,
response.headersLen );
TEST_ASSERT_EQUAL( HTTP_STATUS_CODE_OK, response.statusCode );
TEST_ASSERT_EQUAL( HTTP_TEST_RESPONSE_HEAD_2_CONTENT_LENGTH, response.contentLength );
TEST_ASSERT_EQUAL( HTTP_TEST_RESPONSE_HEAD_2_HEADER_COUNT, response.headerCount );
TEST_ASSERT_BITS_HIGH( HTTP_RESPONSE_CONNECTION_CLOSE_FLAG, response.respFlags );
TEST_ASSERT_BITS_LOW( HTTP_RESPONSE_CONNECTION_KEEP_ALIVE_FLAG, response.respFlags );
TEST_ASSERT_EQUAL( 1, statusCompleteCallbackFlag );
}
/*-----------------------------------------------------------*/
/* Test successfully parsing a response to a PUT request. The full response
* message is present in the response buffer on the first network read. */
void test_HTTPClient_Send_PUT_request_parse_whole_response( void )
{
HTTPStatus_t returnStatus = HTTPSuccess;
llhttp_execute_Stub( llhttp_execute_whole_response );
checkContentLength = 1;
memcpy( requestHeaders.pBuffer,
HTTP_TEST_REQUEST_PUT_HEADERS,
HTTP_TEST_REQUEST_PUT_HEADERS_LENGTH );
requestHeaders.headersLen = HTTP_TEST_REQUEST_PUT_HEADERS_LENGTH;
pNetworkData = ( uint8_t * ) HTTP_TEST_RESPONSE_PUT;
networkDataLen = HTTP_TEST_RESPONSE_PUT_LENGTH;
firstPartBytes = HTTP_TEST_RESPONSE_PUT_LENGTH;
returnStatus = HTTPClient_Send( &transportInterface,
&requestHeaders,