Debian Codegraph @

src/vendor/azure-uamqp-c/deps/azure-c-shared-utility/tests/httpapicompact_ut/httpapi_compact_ut.c

raw
1 // Copyright (c) Microsoft. All rights reserved.
2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
4 #ifdef __cplusplus
5 #include <cstddef>
6 #include <ctime>
7 #include <cstdlib>
8 #else
9 #include <stdlib.h>
10 #include <stddef.h>
11 #include <time.h>
12 #endif
13
14 #include "azure_macro_utils/macro_utils.h"
15 #include "testrunnerswitcher.h"
16 #include "umock_c/umock_c.h"
17 #include "umock_c/umocktypes_charptr.h"
18
19 static int currentmalloc_call = 0;
20 static int whenShallmalloc_fail = 0;
21
22 void* my_gballoc_malloc(size_t size)
23 {
24 void* result;
25 currentmalloc_call++;
26 if (whenShallmalloc_fail > 0)
27 {
28 if (currentmalloc_call >= whenShallmalloc_fail)
29 {
30 currentmalloc_call--;
31 result = NULL;
32 }
33 else
34 {
35 result = malloc(size);
36 }
37 }
38 else
39 {
40 result = malloc(size);
41 }
42 return result;
43 }
44
45 void* my_gballoc_realloc(void* ptr, size_t size)
46 {
47 void* newptr = realloc(ptr, size);
48
49 if (ptr == NULL)
50 {
51 currentmalloc_call++;
52 }
53
54 return newptr;
55 }
56
57 void my_gballoc_free(void* ptr)
58 {
59 currentmalloc_call--;
60 free(ptr);
61 }
62
63 int my_mallocAndStrcpy_s(char** destination, const char* source)
64 {
65 *destination = (char*)malloc(strlen(source) + 1);
66 (void)strcpy(*destination, source);
67 currentmalloc_call++;
68 return 0;
69 }
70
71 #define MAX_RECEIVE_BUFFER_SIZES 3
72 #define HUGE_RELATIVE_PATH_SIZE 10000
73
74 #define TEST_CREATE_CONNECTION_HOST_NAME (const char*)"https://test.azure-devices.net"
75 #define TEST_EXECUTE_REQUEST_RELATIVE_PATH (const char*)"/devices/Huzzah_w_DHT22/messages/events?api-version=2016-11-14"
76 #define TEST_EXECUTE_REQUEST_CONTENT (const unsigned char*)"{\"ObjectType\":\"DeviceInfo\", \"Version\":\"1.0\", \"IsSimulatedDevice\":false, \"DeviceProperties\":{\"DeviceID\":\"Huzzah_w_DHT22\", \"HubEnabledState\":true}, \"Commands\":[{ \"Name\":\"SetHumidity\", \"Parameters\":[{\"Name\":\"humidity\",\"Type\":\"int\"}]},{ \"Name\":\"SetTemperature\", \"Parameters\":[{\"Name\":\"temperature\",\"Type\":\"int\"}]}]}"
77 #define TEST_EXECUTE_REQUEST_CONTENT_LENGTH (size_t)320
78 #define TEST_SETOPTIONS_CERTIFICATE (const unsigned char*)"blah!blah!blah!"
79 #define TEST_SETOPTIONS_X509CLIENTCERT (const unsigned char*)"ADMITONE"
80 #define TEST_SETOPTIONS_X509PRIVATEKEY (const unsigned char*)"SPEAKFRIENDANDENTER"
81 #define TEST_GET_HEADER_HEAD_COUNT (size_t)2
82
83
84 #define ENABLE_MOCKS
85 #include "azure_c_shared_utility/gballoc.h"
86 #include "azure_c_shared_utility/crt_abstractions.h"
87 #include "azure_c_shared_utility/optimize_size.h"
88 #include "azure_c_shared_utility/xio.h"
89 #include "azure_c_shared_utility/httpheaders.h"
90 #include "azure_c_shared_utility/threadapi.h"
91 #include "azure_c_shared_utility/platform.h"
92 #include "azure_c_shared_utility/buffer_.h"
93 #include "azure_c_shared_utility/http_proxy_io.h"
94 #undef ENABLE_MOCKS
95 #include "azure_c_shared_utility/httpapi.h"
96 #include "azure_c_shared_utility/shared_util_options.h"
97
98 static bool current_xioCreate_must_fail = false;
99 XIO_HANDLE my_xio_create(const IO_INTERFACE_DESCRIPTION* io_interface_description, const void* xio_create_parameters)
100 {
101 XIO_HANDLE result;
102 (void)io_interface_description;
103 (void)xio_create_parameters;
104 if (current_xioCreate_must_fail)
105 {
106 result = NULL;
107 }
108 else
109 {
110 result = (XIO_HANDLE)malloc(sizeof(XIO_HANDLE));
111 }
112 return result;
113 }
114
115 void my_xio_destroy(XIO_HANDLE xio)
116 {
117 if (xio != NULL)
118 {
119 free(xio);
120 }
121 }
122
123 static int xio_setoption_shallReturn;
124 int my_xio_setoption(XIO_HANDLE xio, const char* optionName, const void* value)
125 {
126 int result;
127 if ((xio == NULL) || (optionName == NULL) || (value == NULL))
128 {
129 result = MU_FAILURE;
130 }
131 else
132 {
133 result = xio_setoption_shallReturn;
134 }
135 return result;
136 }
137
138 static int xio_open_shallReturn;
139 static int xio_close_shallReturn;
140 static const int* xio_send_shallReturn;
141 static int xio_send_shallReturn_counter;
142 static char xio_send_transmited_buffer[1024];
143 static int xio_send_transmited_buffer_target = 0;
144
145 typedef enum xio_dowork_job_tag
146 {
147 XIO_DOWORK_JOB_NONE,
148 XIO_DOWORK_JOB_OPEN,
149 XIO_DOWORK_JOB_SEND,
150 XIO_DOWORK_JOB_RECEIVED,
151 XIO_DOWORK_JOB_CLOSE,
152 XIO_DOWORK_JOB_ERROR,
153 XIO_DOWORK_JOB_END
154 } xio_dowork_job;
155
156 static const int xio_send_0[10] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
157 static const int xio_send_e[4] = { 123, 123, 123, 123 };
158 static const int xio_send_0_e[4] = { 0, 123, 0, 0 };
159 static const int xio_send_00_e[4] = { 0, 0, 123, 0 };
160 static const int xio_send_7x0[7] = { 0, 0, 0, 0, 0, 0, 0 };
161 static const int xio_send_6x0_e[7] = { 0, 0, 0, 0, 0, 0, 123 };
162 static const xio_dowork_job doworkjob_end[1] = { XIO_DOWORK_JOB_END };
163 static const xio_dowork_job doworkjob_oe[2] = { XIO_DOWORK_JOB_OPEN, XIO_DOWORK_JOB_END };
164 static const xio_dowork_job doworkjob_4none_oe[6] = { XIO_DOWORK_JOB_NONE, XIO_DOWORK_JOB_NONE, XIO_DOWORK_JOB_NONE, XIO_DOWORK_JOB_NONE, XIO_DOWORK_JOB_OPEN, XIO_DOWORK_JOB_END };
165 static const xio_dowork_job doworkjob_4none_ee[6] = { XIO_DOWORK_JOB_NONE, XIO_DOWORK_JOB_NONE, XIO_DOWORK_JOB_NONE, XIO_DOWORK_JOB_NONE, XIO_DOWORK_JOB_ERROR, XIO_DOWORK_JOB_END };
166 static const xio_dowork_job doworkjob_o_3none_ee[6] = { XIO_DOWORK_JOB_OPEN, XIO_DOWORK_JOB_NONE, XIO_DOWORK_JOB_NONE, XIO_DOWORK_JOB_NONE, XIO_DOWORK_JOB_ERROR, XIO_DOWORK_JOB_END };
167 static const xio_dowork_job doworkjob_oee[3] = { XIO_DOWORK_JOB_OPEN, XIO_DOWORK_JOB_ERROR, XIO_DOWORK_JOB_END };
168 static const xio_dowork_job doworkjob_ose[3] = { XIO_DOWORK_JOB_OPEN, XIO_DOWORK_JOB_SEND, XIO_DOWORK_JOB_END };
169 static const xio_dowork_job doworkjob_ee[2] = { XIO_DOWORK_JOB_ERROR, XIO_DOWORK_JOB_END };
170 static const xio_dowork_job doworkjob_o_re[3] = { XIO_DOWORK_JOB_OPEN, XIO_DOWORK_JOB_RECEIVED, XIO_DOWORK_JOB_END };
171 static const xio_dowork_job doworkjob_o_rce[8] = { XIO_DOWORK_JOB_OPEN, XIO_DOWORK_JOB_RECEIVED, XIO_DOWORK_JOB_RECEIVED, XIO_DOWORK_JOB_RECEIVED, XIO_DOWORK_JOB_RECEIVED, XIO_DOWORK_JOB_RECEIVED, XIO_DOWORK_JOB_CLOSE, XIO_DOWORK_JOB_END };
172 static const xio_dowork_job doworkjob_o_rc_error[9] = { XIO_DOWORK_JOB_OPEN, XIO_DOWORK_JOB_RECEIVED, XIO_DOWORK_JOB_RECEIVED, XIO_DOWORK_JOB_RECEIVED, XIO_DOWORK_JOB_RECEIVED, XIO_DOWORK_JOB_RECEIVED, XIO_DOWORK_JOB_CLOSE, XIO_DOWORK_JOB_ERROR, XIO_DOWORK_JOB_END };
173 static const xio_dowork_job doworkjob_o_rre[4] = { XIO_DOWORK_JOB_OPEN, XIO_DOWORK_JOB_RECEIVED, XIO_DOWORK_JOB_RECEIVED, XIO_DOWORK_JOB_END };
174 static const xio_dowork_job doworkjob_o_sre[15] = { XIO_DOWORK_JOB_OPEN,
175 XIO_DOWORK_JOB_SEND, XIO_DOWORK_JOB_SEND, XIO_DOWORK_JOB_SEND, XIO_DOWORK_JOB_SEND, XIO_DOWORK_JOB_SEND, XIO_DOWORK_JOB_SEND, XIO_DOWORK_JOB_SEND,
176 XIO_DOWORK_JOB_RECEIVED, XIO_DOWORK_JOB_RECEIVED, XIO_DOWORK_JOB_RECEIVED, XIO_DOWORK_JOB_RECEIVED, XIO_DOWORK_JOB_RECEIVED, XIO_DOWORK_JOB_CLOSE, XIO_DOWORK_JOB_END };
177
178 static const IO_OPEN_RESULT openresult_ok[1] = { IO_OPEN_OK };
179 static const IO_OPEN_RESULT openresult_error[1] = { IO_OPEN_ERROR };
180
181 static const IO_SEND_RESULT sendresult_error[1] = { IO_SEND_ERROR };
182 static const IO_SEND_RESULT sendresult_o_3error[4] = { IO_SEND_OK, IO_SEND_ERROR, IO_SEND_ERROR, IO_SEND_ERROR };
183 static const IO_SEND_RESULT sendresult_7ok[7] = {
184 IO_SEND_OK,
185 IO_SEND_OK,
186 IO_SEND_OK,
187 IO_SEND_OK,
188 IO_SEND_OK,
189 IO_SEND_OK,
190 IO_SEND_OK
191 };
192 static const IO_SEND_RESULT sendresult_6ok_error[7] = {
193 IO_SEND_OK,
194 IO_SEND_OK,
195 IO_SEND_OK,
196 IO_SEND_OK,
197 IO_SEND_OK,
198 IO_SEND_OK,
199 IO_SEND_ERROR
200 };
201
202
203 static const xio_dowork_job* DoworkJobs = (const xio_dowork_job*)doworkjob_end;
204 static const IO_OPEN_RESULT* DoworkJobsOpenResult;
205 static int SkipDoworkJobsOpenResult;
206 static const IO_SEND_RESULT* DoworkJobsSendResult;
207 static int SkipDoworkJobsSendResult;
208
209 static bool DoworkJobsCloseSuccess;
210 static int SkipDoworkJobsCloseResult;
211 static bool call_on_io_close_complete_in_xio_close;
212
213 static ON_IO_OPEN_COMPLETE my_on_io_open_complete;
214 static void* my_on_io_open_complete_context;
215
216 static ON_IO_CLOSE_COMPLETE my_on_io_close_complete;
217 static void* my_on_io_close_complete_context;
218
219 static ON_SEND_COMPLETE my_on_send_complete;
220 static void* my_on_send_complete_context;
221 static bool call_on_send_complete_in_xio_send;
222
223 static ON_BYTES_RECEIVED my_on_bytes_received;
224 static void* my_on_bytes_received_context;
225 static const unsigned char* DoworkJobsReceivedBuffer;
226 static size_t DoworkJobsReceivedBuffer_size[MAX_RECEIVE_BUFFER_SIZES];
227 static int DoworkJobsReceivedBuffer_counter;
228
229 static ON_IO_ERROR my_on_io_error;
230 static void* my_on_io_error_context;
231
232 int my_xio_open(XIO_HANDLE xio,
233 ON_IO_OPEN_COMPLETE on_io_open_complete, void* on_io_open_complete_context,
234 ON_BYTES_RECEIVED on_bytes_received, void* on_bytes_received_context,
235 ON_IO_ERROR on_io_error, void* on_io_error_context)
236 {
237 int result;
238 if ((xio == NULL) ||
239 (on_io_open_complete == NULL) || (on_io_open_complete_context == NULL) ||
240 (on_bytes_received == NULL) || (on_bytes_received_context == NULL) ||
241 (on_io_error == NULL) || (on_io_error_context == NULL))
242 {
243 result = MU_FAILURE;
244 }
245 else
246 {
247 my_on_io_open_complete = on_io_open_complete;
248 my_on_io_open_complete_context = on_io_open_complete_context;
249 my_on_bytes_received = on_bytes_received;
250 my_on_bytes_received_context = on_bytes_received_context;
251 my_on_io_error = on_io_error;
252 my_on_io_error_context = on_io_error_context;
253
254 result = xio_open_shallReturn;
255 }
256
257 if (result == 0)
258 {
259 xio_dowork(xio);
260 }
261 return result;
262 }
263
264 int my_xio_close(XIO_HANDLE xio, ON_IO_CLOSE_COMPLETE on_io_close_complete, void* on_io_close_complete_context)
265 {
266 int result;
267 if ((xio == NULL) ||
268 (on_io_close_complete == NULL) || (on_io_close_complete_context == NULL))
269 {
270 result = MU_FAILURE;
271 }
272 else
273 {
274 my_on_io_close_complete = on_io_close_complete;
275 my_on_io_close_complete_context = on_io_close_complete_context;
276 result = xio_close_shallReturn;
277 if (call_on_io_close_complete_in_xio_close)
278 {
279 my_on_io_close_complete(my_on_io_close_complete_context);
280 }
281 }
282 return result;
283 }
284
285 int my_xio_send(XIO_HANDLE xio, const void* buffer, size_t size, ON_SEND_COMPLETE on_send_complete, void* callback_context)
286 {
287 int result;
288 if ((xio == NULL) ||
289 (buffer == NULL) || (size == 0))
290 {
291 result = MU_FAILURE;
292
293 if (call_on_send_complete_in_xio_send)
294 {
295 on_send_complete(callback_context, IO_SEND_ERROR);
296 }
297 }
298 else
299 {
300 my_on_send_complete = on_send_complete;
301 my_on_send_complete_context = callback_context;
302
303 if (xio_send_transmited_buffer_target > 0)
304 {
305 xio_send_transmited_buffer_target--;
306 if (xio_send_transmited_buffer_target == 0)
307 {
308 (void)memcpy(xio_send_transmited_buffer, buffer, size);
309 }
310 }
311 result = xio_send_shallReturn[xio_send_shallReturn_counter];
312 xio_send_shallReturn_counter++;
313
314 if (call_on_send_complete_in_xio_send)
315 {
316 on_send_complete(callback_context, IO_SEND_OK);
317 }
318 }
319 return result;
320 }
321
322 void my_xio_dowork(XIO_HANDLE xio)
323 {
324 if (xio != NULL)
325 {
326 switch (*DoworkJobs)
327 {
328 case XIO_DOWORK_JOB_NONE:
329 DoworkJobs++;
330 break;
331 case XIO_DOWORK_JOB_OPEN:
332 if ((SkipDoworkJobsOpenResult--) <= 0)
333 {
334 if (my_on_io_open_complete != NULL)
335 {
336 my_on_io_open_complete(my_on_io_open_complete_context, (*DoworkJobsOpenResult));
337 }
338 DoworkJobs++;
339 DoworkJobsOpenResult++;
340 SkipDoworkJobsOpenResult = 0;
341 }
342 break;
343 case XIO_DOWORK_JOB_SEND:
344 if ((SkipDoworkJobsSendResult--) <= 0)
345 {
346 if (my_on_send_complete != NULL)
347 {
348 my_on_send_complete(my_on_send_complete_context, (*DoworkJobsSendResult));
349 }
350 DoworkJobs++;
351 DoworkJobsSendResult++;
352 SkipDoworkJobsSendResult = 0;
353 }
354 break;
355 case XIO_DOWORK_JOB_RECEIVED:
356 if (my_on_bytes_received != NULL)
357 {
358 my_on_bytes_received(my_on_bytes_received_context, DoworkJobsReceivedBuffer, DoworkJobsReceivedBuffer_size[DoworkJobsReceivedBuffer_counter]);
359 }
360 DoworkJobs++;
361 if (DoworkJobsReceivedBuffer_counter < MAX_RECEIVE_BUFFER_SIZES-1)
362 {
363 DoworkJobsReceivedBuffer_counter++;
364 }
365 break;
366 case XIO_DOWORK_JOB_CLOSE:
367 if ((SkipDoworkJobsCloseResult--) <= 0)
368 {
369 if(DoworkJobsCloseSuccess)
370 {
371 my_on_io_close_complete(my_on_io_close_complete_context);
372 }
373 DoworkJobs++;
374 SkipDoworkJobsCloseResult = 0;
375 }
376 break;
377 case XIO_DOWORK_JOB_ERROR:
378 if (my_on_io_error != NULL)
379 {
380 my_on_io_error(my_on_io_error_context);
381 }
382 DoworkJobs++;
383 break;
384 default:
385 break;
386 }
387
388 }
389 }
390
391 HTTP_HEADERS_HANDLE my_HTTPHeaders_Alloc(void)
392 {
393 return (HTTP_HEADERS_HANDLE)malloc(1);
394 }
395
396 void my_HTTPHeaders_Free(HTTP_HEADERS_HANDLE handle)
397 {
398 free(handle);
399 }
400
401 static BUFFER_HANDLE TestBufferHandle;
402
403 BUFFER_HANDLE my_BUFFER_new(void)
404 {
405 return (BUFFER_HANDLE)malloc(1);
406 }
407
408 void my_BUFFER_delete(BUFFER_HANDLE handle)
409 {
410 free(handle);
411 }
412
413 static HTTP_HEADERS_RESULT HTTPHeaders_GetHeaderCount_shallReturn;
414 HTTP_HEADERS_RESULT my_HTTPHeaders_GetHeaderCount(HTTP_HEADERS_HANDLE handle, size_t* headerCount)
415 {
416 HTTP_HEADERS_RESULT result;
417
418 if (handle == NULL)
419 {
420 result = HTTP_HEADERS_ERROR;
421 }
422 else
423 {
424 (*headerCount) = TEST_GET_HEADER_HEAD_COUNT;
425 result = HTTPHeaders_GetHeaderCount_shallReturn;
426 }
427
428 return result;
429 }
430
431 static HTTP_HEADERS_RESULT HTTPHeaders_GetHeader_shallReturn;
432 HTTP_HEADERS_RESULT my_HTTPHeaders_GetHeader(HTTP_HEADERS_HANDLE handle, size_t index, char** destination)
433 {
434 HTTP_HEADERS_RESULT result;
435
436 if ((handle == NULL) || (destination == NULL) || (index > TEST_GET_HEADER_HEAD_COUNT))
437 {
438 result = HTTP_HEADERS_INVALID_ARG;
439 }
440 else
441 {
442 *destination = (char*)malloc(11 * sizeof(char));
443 strcpy(*destination, "0123456789");
444 result = HTTPHeaders_GetHeaderCount_shallReturn;
445 }
446
447 return result;
448 }
449
450 static const IO_INTERFACE_DESCRIPTION default_tlsio = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL };
451 const IO_INTERFACE_DESCRIPTION* my_platform_get_default_tlsio(void)
452 {
453 return &default_tlsio;
454 }
455
456
457 static void createHttpObjects(HTTP_HEADERS_HANDLE* requestHttpHeaders, HTTP_HEADERS_HANDLE* responseHttpHeaders)
458 {
459 STRICT_EXPECTED_CALL(HTTPHeaders_Alloc());
460 STRICT_EXPECTED_CALL(gballoc_malloc(1));
461 STRICT_EXPECTED_CALL(HTTPHeaders_Alloc());
462 STRICT_EXPECTED_CALL(gballoc_malloc(1));
463
464 /*assumed to never fail*/
465 *requestHttpHeaders = HTTPHeaders_Alloc();
466 *responseHttpHeaders = HTTPHeaders_Alloc();
467 if (
468 (*requestHttpHeaders == NULL) ||
469 (*responseHttpHeaders == NULL)
470 )
471 {
472 ASSERT_FAIL("unable to build test prerequisites");
473 }
474 }
475
476 static void destroyHttpObjects(HTTP_HEADERS_HANDLE* requestHttpHeaders, HTTP_HEADERS_HANDLE* responseHttpHeaders)
477 {
478 STRICT_EXPECTED_CALL(HTTPHeaders_Free(IGNORED_PTR_ARG));
479 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG));
480 STRICT_EXPECTED_CALL(HTTPHeaders_Free(IGNORED_PTR_ARG));
481 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG));
482
483 HTTPHeaders_Free(*requestHttpHeaders);
484 *requestHttpHeaders = NULL;
485 HTTPHeaders_Free(*responseHttpHeaders);
486 *responseHttpHeaders = NULL;
487 }
488
489 static HTTP_HANDLE createHttpConnection(void)
490 {
491 xio_open_shallReturn = 0;
492 xio_send_shallReturn_counter = 0;
493 xio_send_shallReturn = (const int*)xio_send_0;
494
495 my_on_io_open_complete = NULL;
496 my_on_io_open_complete_context = NULL;
497
498 my_on_io_close_complete = NULL;
499 my_on_io_close_complete_context = NULL;
500
501 my_on_send_complete = NULL;
502 my_on_send_complete_context = NULL;
503
504 my_on_bytes_received = NULL;
505 my_on_bytes_received_context = NULL;
506
507 my_on_io_error = NULL;
508 my_on_io_error_context = NULL;
509
510 HTTPHeaders_GetHeaderCount_shallReturn = HTTP_HEADERS_OK;
511 xio_setoption_shallReturn = 0;
512
513 current_xioCreate_must_fail = false;
514
515 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG)).IgnoreArgument(1);
516 STRICT_EXPECTED_CALL(mallocAndStrcpy_s(IGNORED_PTR_ARG, IGNORED_PTR_ARG));
517 STRICT_EXPECTED_CALL(platform_get_default_tlsio());
518 STRICT_EXPECTED_CALL(xio_create(&default_tlsio, IGNORED_PTR_ARG)).IgnoreArgument(2);
519 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG)).IgnoreArgument(1);
520
521 HTTPAPI_Init();
522 return HTTPAPI_CreateConnection(TEST_CREATE_CONNECTION_HOST_NAME); /* currentmalloc_call += 2 */
523 }
524
525 static void setHttpCertificate(HTTP_HANDLE httpHandle)
526 {
527 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG))
528 .IgnoreArgument(1);
529 /*Tests_SRS_HTTPAPI_COMPACT_21_056: [ The HTTPAPI_SetOption shall change the HTTP options. ]*/
530 /*Tests_SRS_HTTPAPI_COMPACT_21_057: [ The HTTPAPI_SetOption shall receive a handle that identiry the HTTP connection. ]*/
531 /*Tests_SRS_HTTPAPI_COMPACT_21_058: [ The HTTPAPI_SetOption shall receive the option as a pair optionName/value. ]*/
532 HTTPAPI_SetOption(httpHandle, "TrustedCerts", TEST_SETOPTIONS_CERTIFICATE); /* currentmalloc_call += 1 */
533 }
534
535 static void setHttpx509ClientCertificateAndKey(HTTP_HANDLE httpHandle)
536 {
537 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG))
538 .IgnoreArgument(1);
539 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG))
540 .IgnoreArgument(1);
541 /*Tests_SRS_HTTPAPI_COMPACT_21_056: [ The HTTPAPI_SetOption shall change the HTTP options. ]*/
542 /*Tests_SRS_HTTPAPI_COMPACT_21_057: [ The HTTPAPI_SetOption shall receive a handle that identiry the HTTP connection. ]*/
543 /*Tests_SRS_HTTPAPI_COMPACT_21_058: [ The HTTPAPI_SetOption shall receive the option as a pair optionName/value. ]*/
544 HTTPAPI_SetOption(httpHandle, SU_OPTION_X509_CERT, TEST_SETOPTIONS_X509CLIENTCERT); /* currentmalloc_call += 1 */
545 HTTPAPI_SetOption(httpHandle, SU_OPTION_X509_PRIVATE_KEY, TEST_SETOPTIONS_X509PRIVATEKEY); /* currentmalloc_call += 1 */
546 }
547
548 static void setupAllCallBeforeOpenHTTPsequence(HTTP_HEADERS_HANDLE requestHttpHeaders, int numberOfDoWork, bool useClientCert)
549 {
550 int i;
551
552 STRICT_EXPECTED_CALL(HTTPHeaders_GetHeaderCount(requestHttpHeaders, IGNORED_PTR_ARG))
553 .IgnoreArgument(2);
554 STRICT_EXPECTED_CALL(xio_setoption(IGNORED_PTR_ARG, "TrustedCerts", TEST_SETOPTIONS_CERTIFICATE))
555 .IgnoreArgument(1)
556 .IgnoreArgument(3);
557 if (useClientCert == true)
558 {
559 STRICT_EXPECTED_CALL(xio_setoption(IGNORED_PTR_ARG, SU_OPTION_X509_CERT, TEST_SETOPTIONS_X509CLIENTCERT))
560 .IgnoreArgument(1)
561 .IgnoreArgument(3);
562 STRICT_EXPECTED_CALL(xio_setoption(IGNORED_PTR_ARG, SU_OPTION_X509_PRIVATE_KEY, TEST_SETOPTIONS_X509PRIVATEKEY))
563 .IgnoreArgument(1)
564 .IgnoreArgument(3);
565 }
566 STRICT_EXPECTED_CALL(xio_open(IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
567 .IgnoreAllArguments();
568 for (i = 0; i < numberOfDoWork; i++)
569 {
570 STRICT_EXPECTED_CALL(xio_dowork(IGNORED_NUM_ARG))
571 .IgnoreArgument(1);
572 if (i > 0)
573 {
574 STRICT_EXPECTED_CALL(ThreadAPI_Sleep(100));
575 }
576 }
577 }
578
579 #define TEST_RECEIVED_ANSWER (const unsigned char*)"HTTP/111.222 433 555\r\ncontent-length:10\r\ntransfer-encoding:\r\n\r\n0123456789\r\n\r\n"
580 static void setupAllCallBeforeReceiveHTTPsequenceWithSuccess()
581 {
582 STRICT_EXPECTED_CALL(xio_dowork(IGNORED_NUM_ARG))
583 .IgnoreArgument(1);
584 STRICT_EXPECTED_CALL(gballoc_realloc(IGNORED_NUM_ARG, DoworkJobsReceivedBuffer_size[0])).IgnoreArgument(1);
585
586 STRICT_EXPECTED_CALL(xio_dowork(IGNORED_NUM_ARG))
587 .IgnoreArgument(1);
588 STRICT_EXPECTED_CALL(gballoc_realloc(IGNORED_NUM_ARG, IGNORED_NUM_ARG)).IgnoreAllArguments();
589 STRICT_EXPECTED_CALL(HTTPHeaders_AddHeaderNameValuePair(IGNORED_PTR_ARG, "content-length", "10")).IgnoreArgument(1);
590
591 STRICT_EXPECTED_CALL(xio_dowork(IGNORED_NUM_ARG))
592 .IgnoreArgument(1);
593 STRICT_EXPECTED_CALL(gballoc_realloc(IGNORED_NUM_ARG, IGNORED_NUM_ARG)).IgnoreAllArguments();
594 STRICT_EXPECTED_CALL(HTTPHeaders_AddHeaderNameValuePair(IGNORED_PTR_ARG, "transfer-encoding", "")).IgnoreArgument(1);
595
596 STRICT_EXPECTED_CALL(xio_dowork(IGNORED_NUM_ARG))
597 .IgnoreArgument(1);
598 STRICT_EXPECTED_CALL(gballoc_realloc(IGNORED_NUM_ARG, IGNORED_NUM_ARG)).IgnoreAllArguments();
599 STRICT_EXPECTED_CALL(xio_dowork(IGNORED_NUM_ARG))
600 .IgnoreArgument(1);
601
602 STRICT_EXPECTED_CALL(gballoc_realloc(IGNORED_NUM_ARG, IGNORED_NUM_ARG)).IgnoreAllArguments();
603 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG))
604 .IgnoreArgument(1);
605 }
606
607 static void setupAllCallBeforeSendHTTPsequenceWithSuccess(HTTP_HEADERS_HANDLE requestHttpHeaders)
608 {
609 STRICT_EXPECTED_CALL(xio_send(IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_NUM_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
610 .IgnoreAllArguments();
611 STRICT_EXPECTED_CALL(HTTPHeaders_GetHeader(requestHttpHeaders, IGNORED_NUM_ARG, IGNORED_PTR_ARG))
612 .IgnoreArgument(2).IgnoreArgument(3);
613 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG)).IgnoreArgument(1);
614 STRICT_EXPECTED_CALL(xio_send(IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_NUM_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
615 .IgnoreAllArguments();
616 STRICT_EXPECTED_CALL(xio_send(IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_NUM_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
617 .IgnoreAllArguments();
618 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG))
619 .IgnoreArgument(1);
620 STRICT_EXPECTED_CALL(HTTPHeaders_GetHeader(requestHttpHeaders, IGNORED_NUM_ARG, IGNORED_PTR_ARG))
621 .IgnoreArgument(2).IgnoreArgument(3);
622 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG)).IgnoreArgument(1);
623 STRICT_EXPECTED_CALL(xio_send(IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_NUM_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
624 .IgnoreAllArguments();
625 STRICT_EXPECTED_CALL(xio_send(IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_NUM_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
626 .IgnoreAllArguments();
627 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG))
628 .IgnoreArgument(1);
629 STRICT_EXPECTED_CALL(xio_send(IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_NUM_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
630 .IgnoreAllArguments();
631
632 STRICT_EXPECTED_CALL(xio_send(IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_NUM_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
633 .IgnoreAllArguments();
634 }
635
636 #define TEST_HEAD_RECEIVED_ANSWER (const unsigned char*)"HTTP/111.222 433 555\r\ncontent-length:10\r\ntransfer-encoding:\r\n\r\n"
637 static void setupAllCallBeforeReceiveHTTPHeadsequenceWithSuccess()
638 {
639 STRICT_EXPECTED_CALL(xio_dowork(IGNORED_NUM_ARG));
640
641 STRICT_EXPECTED_CALL(gballoc_realloc(IGNORED_NUM_ARG, DoworkJobsReceivedBuffer_size[0]));
642
643 STRICT_EXPECTED_CALL(xio_dowork(IGNORED_NUM_ARG));
644 STRICT_EXPECTED_CALL(gballoc_realloc(IGNORED_NUM_ARG, IGNORED_NUM_ARG));
645 STRICT_EXPECTED_CALL(HTTPHeaders_AddHeaderNameValuePair(IGNORED_PTR_ARG, "content-length", "10"));
646
647 STRICT_EXPECTED_CALL(xio_dowork(IGNORED_NUM_ARG));
648 STRICT_EXPECTED_CALL(gballoc_realloc(IGNORED_NUM_ARG, IGNORED_NUM_ARG));
649 STRICT_EXPECTED_CALL(HTTPHeaders_AddHeaderNameValuePair(IGNORED_PTR_ARG, "transfer-encoding", ""));
650
651 STRICT_EXPECTED_CALL(xio_dowork(IGNORED_NUM_ARG));
652 STRICT_EXPECTED_CALL(gballoc_realloc(IGNORED_NUM_ARG, IGNORED_NUM_ARG));
653
654 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG));
655 }
656
657 static const IO_OPEN_RESULT* DoworkJobsOpenResult_ReceiveHead = (const IO_OPEN_RESULT*)openresult_ok;
658 static const IO_SEND_RESULT* DoworkJobsSendResult_ReceiveHead = (const IO_SEND_RESULT*) sendresult_7ok;
659
660 static void PrepareReceiveHead(HTTP_HEADERS_HANDLE requestHttpHeaders, size_t bufferSize[], int doworkReduction[], int countSizes)
661 {
662 int countBuffer;
663 DoworkJobsOpenResult = DoworkJobsOpenResult_ReceiveHead;
664 DoworkJobsSendResult = DoworkJobsSendResult_ReceiveHead;
665
666 setupAllCallBeforeOpenHTTPsequence(requestHttpHeaders, 1, false);
667 setupAllCallBeforeSendHTTPsequenceWithSuccess(requestHttpHeaders);
668
669 for (countBuffer = 0; countBuffer < countSizes; countBuffer++)
670 {
671 int countChar;
672 if (countBuffer > 0)
673 {
674 STRICT_EXPECTED_CALL(ThreadAPI_Sleep(100));
675 }
676 STRICT_EXPECTED_CALL(xio_dowork(IGNORED_NUM_ARG))
677 .IgnoreArgument(1);
678 STRICT_EXPECTED_CALL(gballoc_realloc(IGNORED_NUM_ARG, bufferSize[countBuffer])).IgnoreArgument(1);
679 for (countChar = 0; countChar < doworkReduction[countBuffer]; countChar++)
680 {
681 STRICT_EXPECTED_CALL(xio_dowork(IGNORED_NUM_ARG))
682 .IgnoreArgument(1);
683 }
684 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG))
685 .IgnoreArgument(1);
686 }
687
688 HTTPHeaders_GetHeader_shallReturn = HTTP_HEADERS_OK;
689 }
690
691 IMPLEMENT_UMOCK_C_ENUM_TYPE(HTTP_HEADERS_RESULT, HTTP_HEADERS_RESULT_VALUES);
692
693 static TEST_MUTEX_HANDLE g_testByTest;
694
695 MU_DEFINE_ENUM_STRINGS(UMOCK_C_ERROR_CODE, UMOCK_C_ERROR_CODE_VALUES)
696
697 static void on_umock_c_error(UMOCK_C_ERROR_CODE error_code)
698 {
699 ASSERT_FAIL("umock_c reported error :%" PRI_MU_ENUM "", MU_ENUM_VALUE(UMOCK_C_ERROR_CODE, error_code));
700 }
701
702 BEGIN_TEST_SUITE(httpapicompact_ut)
703
704 TEST_SUITE_INITIALIZE(setsBufferTempSize)
705 {
706 int result;
707
708 g_testByTest = TEST_MUTEX_CREATE();
709 ASSERT_IS_NOT_NULL(g_testByTest);
710
711 umock_c_init(on_umock_c_error);
712
713 result = umocktypes_charptr_register_types();
714 ASSERT_ARE_EQUAL(int, 0, result);
715
716 TestBufferHandle = BUFFER_new();
717 ASSERT_IS_NULL(TestBufferHandle);
718
719 REGISTER_TYPE(HTTP_HEADERS_RESULT, HTTP_HEADERS_RESULT);
720
721 REGISTER_UMOCK_ALIAS_TYPE(HTTP_HEADERS_HANDLE, void*);
722 REGISTER_UMOCK_ALIAS_TYPE(XIO_HANDLE, void*);
723 REGISTER_UMOCK_ALIAS_TYPE(ON_SEND_COMPLETE, void*);
724 REGISTER_UMOCK_ALIAS_TYPE(ON_IO_CLOSE_COMPLETE, void*);
725 REGISTER_UMOCK_ALIAS_TYPE(ON_IO_OPEN_COMPLETE, void*);
726 REGISTER_UMOCK_ALIAS_TYPE(ON_BYTES_RECEIVED, void*);
727 REGISTER_UMOCK_ALIAS_TYPE(ON_IO_ERROR, void*);
728 REGISTER_UMOCK_ALIAS_TYPE(BUFFER_HANDLE, void*);
729
730 REGISTER_GLOBAL_MOCK_HOOK(gballoc_malloc, my_gballoc_malloc);
731 REGISTER_GLOBAL_MOCK_HOOK(gballoc_realloc, my_gballoc_realloc);
732 REGISTER_GLOBAL_MOCK_HOOK(gballoc_free, my_gballoc_free);
733 REGISTER_GLOBAL_MOCK_HOOK(xio_create, my_xio_create);
734 REGISTER_GLOBAL_MOCK_HOOK(xio_destroy, my_xio_destroy);
735 REGISTER_GLOBAL_MOCK_HOOK(xio_setoption, my_xio_setoption);
736 REGISTER_GLOBAL_MOCK_HOOK(xio_open, my_xio_open);
737 REGISTER_GLOBAL_MOCK_HOOK(xio_close, my_xio_close);
738 REGISTER_GLOBAL_MOCK_HOOK(xio_send, my_xio_send);
739 REGISTER_GLOBAL_MOCK_HOOK(xio_dowork, my_xio_dowork);
740
741 REGISTER_GLOBAL_MOCK_RETURN(HTTPHeaders_AddHeaderNameValuePair, HTTP_HEADERS_OK);
742 REGISTER_GLOBAL_MOCK_HOOK(HTTPHeaders_Alloc, my_HTTPHeaders_Alloc);
743 REGISTER_GLOBAL_MOCK_HOOK(HTTPHeaders_Free, my_HTTPHeaders_Free);
744 REGISTER_GLOBAL_MOCK_HOOK(BUFFER_new, my_BUFFER_new);
745 REGISTER_GLOBAL_MOCK_HOOK(BUFFER_delete, my_BUFFER_delete);
746 REGISTER_GLOBAL_MOCK_HOOK(HTTPHeaders_GetHeaderCount, my_HTTPHeaders_GetHeaderCount);
747 REGISTER_GLOBAL_MOCK_HOOK(HTTPHeaders_GetHeader, my_HTTPHeaders_GetHeader);
748
749 REGISTER_GLOBAL_MOCK_HOOK(platform_get_default_tlsio, my_platform_get_default_tlsio);
750 REGISTER_GLOBAL_MOCK_HOOK(mallocAndStrcpy_s, my_mallocAndStrcpy_s);
751 }
752
753 TEST_SUITE_CLEANUP(TestClassCleanup)
754 {
755 BUFFER_delete(TestBufferHandle);
756
757 umock_c_deinit();
758
759 TEST_MUTEX_DESTROY(g_testByTest);
760 }
761
762 TEST_FUNCTION_INITIALIZE(f)
763 {
764 if (TEST_MUTEX_ACQUIRE(g_testByTest))
765 {
766 ASSERT_FAIL("our mutex is ABANDONED. Failure in test framework");
767 }
768
769 umock_c_reset_all_calls();
770
771 currentmalloc_call = 0;
772 whenShallmalloc_fail = 0;
773
774 xio_send_transmited_buffer[0] = '\0';
775
776 call_on_send_complete_in_xio_send = true;
777 SkipDoworkJobsOpenResult = 0;
778 SkipDoworkJobsCloseResult = 0;
779 SkipDoworkJobsSendResult = 0;
780
781 xio_close_shallReturn = 0;
782 DoworkJobsCloseSuccess = true;
783 call_on_io_close_complete_in_xio_close = true;
784 }
785
786 TEST_FUNCTION_CLEANUP(cleans)
787 {
788 TEST_MUTEX_RELEASE(g_testByTest);
789 }
790
791
792 /* HTTPAPI_Init */
793
794 /*Tests_SRS_HTTPAPI_COMPACT_21_001: [ The httpapi_compact shall implement the methods defined by the httpapi.h. ]*/
795 /*Tests_SRS_HTTPAPI_COMPACT_21_002: [ The httpapi_compact shall support the http requests. ]*/
796 /*Tests_SRS_HTTPAPI_COMPACT_21_003: [ The httpapi_compact shall return error codes defined by HTTPAPI_RESULT. ]*/
797 /*Tests_SRS_HTTPAPI_COMPACT_21_004: [ The HTTPAPI_Init shall allocate all memory to control the http protocol. ]*/
798 /*Tests_SRS_HTTPAPI_COMPACT_21_007: [ If there is not enough memory to control the http protocol, the HTTPAPI_Init shall return HTTPAPI_ALLOC_FAILED. ]*/
799 /*Tests_SRS_HTTPAPI_COMPACT_21_006: [ If HTTPAPI_Init succeed allocating all the needed memory, it shall return HTTPAPI_OK. ]*/
800 TEST_FUNCTION(HTTPAPI_Init__always_return_HTTPAPI_OK_Succeed)
801 {
802 /// arrange
803 int result;
804
805 /// act
806 result = HTTPAPI_Init();
807
808 /// assert
809 ASSERT_ARE_EQUAL(int, HTTPAPI_OK, result);
810
811 /// cleanup
812 //none
813 }
814
815
816 /* HTTPAPI_Deinit */
817
818 /*Tests_SRS_HTTPAPI_COMPACT_21_009: [ The HTTPAPI_Init shall release all memory allocated by the httpapi_compact. ]*/
819 TEST_FUNCTION(HTTPAPI_Deinit__just_call_Succeed)
820 {
821 /// arrange
822 HTTPAPI_Init();
823
824 /// act
825 HTTPAPI_Deinit();
826
827 /// assert
828 //none
829
830 /// cleanup
831 //none
832 }
833
834
835 /* HTTPAPI_CreateConnection */
836
837 /*Tests_SRS_HTTPAPI_COMPACT_21_014: [ If the hostName is NULL, the HTTPAPI_CreateConnection shall return NULL as the handle. ]*/
838 TEST_FUNCTION(HTTPAPI_CreateConnection__hostName_NULL_Failed)
839 {
840 /// arrange
841 HTTP_HANDLE httpHandle;
842 HTTPAPI_Init();
843 current_xioCreate_must_fail = false;
844
845 /// act
846 httpHandle = HTTPAPI_CreateConnection(NULL); /* currentmalloc_call += 0 */
847
848 /// assert
849 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
850 ASSERT_ARE_EQUAL(int, 0, currentmalloc_call);
851 ASSERT_IS_NULL(httpHandle);
852
853 /// cleanup
854 HTTPAPI_Deinit();
855 }
856
857 /*Tests_SRS_HTTPAPI_COMPACT_21_015: [ If the hostName is empty, the HTTPAPI_CreateConnection shall return NULL as the handle. ]*/
858 TEST_FUNCTION(HTTPAPI_CreateConnection__empty_hostName_Failed)
859 {
860 /// arrange
861 const char* hostName = "";
862 HTTP_HANDLE httpHandle;
863 HTTPAPI_Init();
864 current_xioCreate_must_fail = false;
865
866 /// act
867 httpHandle = HTTPAPI_CreateConnection(hostName); /* currentmalloc_call += 0 */
868
869 /// assert
870 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
871 ASSERT_ARE_EQUAL(int, 0, currentmalloc_call);
872 ASSERT_IS_NULL(httpHandle);
873
874 /// cleanup
875 HTTPAPI_Deinit();
876 }
877
878 /*Tests_SRS_HTTPAPI_COMPACT_21_011: [ The HTTPAPI_CreateConnection shall create an http connection to the host specified by the hostName parameter. ]*/
879 /*Tests_SRS_HTTPAPI_COMPACT_21_012: [ The HTTPAPI_CreateConnection shall return a non-NULL handle on success. ]*/
880 TEST_FUNCTION(HTTPAPI_CreateConnection__valid_hostName_Succeed)
881 {
882 /// arrange
883 HTTP_HANDLE httpHandle;
884 HTTPAPI_Init();
885 current_xioCreate_must_fail = false;
886 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG));
887 STRICT_EXPECTED_CALL(mallocAndStrcpy_s(IGNORED_NUM_ARG, IGNORED_NUM_ARG));
888 STRICT_EXPECTED_CALL(platform_get_default_tlsio());
889 STRICT_EXPECTED_CALL(xio_create(&default_tlsio, IGNORED_PTR_ARG)).IgnoreArgument(2);
890 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG)).IgnoreArgument(1);
891
892 /// act
893 httpHandle = HTTPAPI_CreateConnection(TEST_CREATE_CONNECTION_HOST_NAME); /* currentmalloc_call += 2 */
894
895 /// assert
896 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
897 ASSERT_ARE_EQUAL(int, 3, currentmalloc_call);
898 ASSERT_IS_NOT_NULL(httpHandle);
899
900 /// cleanup
901 HTTPAPI_CloseConnection(httpHandle); /* currentmalloc_call -= 2 */
902 HTTPAPI_Deinit();
903 }
904
905 /*Tests_SRS_HTTPAPI_COMPACT_21_013: [ If there is not enough memory to control the http connection, the HTTPAPI_CreateConnection shall return NULL as the handle. ]*/
906 TEST_FUNCTION(HTTPAPI_CreateConnection__no_enough_memory_failed)
907 {
908 /// arrange
909 HTTP_HANDLE httpHandle;
910 current_xioCreate_must_fail = false;
911 whenShallmalloc_fail = 1;
912 HTTPAPI_Init();
913 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG)).IgnoreArgument(1);
914
915 /// act
916 httpHandle = HTTPAPI_CreateConnection(TEST_CREATE_CONNECTION_HOST_NAME); /* currentmalloc_call += 2 */
917
918 /// assert
919 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
920 ASSERT_ARE_EQUAL(int, 0, currentmalloc_call);
921 ASSERT_IS_NULL(httpHandle);
922
923 /// cleanup
924 HTTPAPI_Deinit();
925 }
926
927 /*Tests_SRS_HTTPAPI_COMPACT_21_016: [ If the HTTPAPI_CreateConnection failed to create the connection, it shall return NULL as the handle. ]*/
928 TEST_FUNCTION(HTTPAPI_CreateConnection__create_xio_connection_failed)
929 {
930 /// arrange
931 HTTP_HANDLE httpHandle;
932 current_xioCreate_must_fail = true;
933 HTTPAPI_Init();
934
935 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG)).IgnoreArgument(1);
936 STRICT_EXPECTED_CALL(mallocAndStrcpy_s(IGNORED_NUM_ARG, IGNORED_NUM_ARG));
937 STRICT_EXPECTED_CALL(platform_get_default_tlsio());
938 STRICT_EXPECTED_CALL(xio_create(&default_tlsio, IGNORED_PTR_ARG)).IgnoreArgument(2);
939 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG)); // hostname
940 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_NUM_ARG)).IgnoreArgument(1);
941
942 /// act
943 httpHandle = HTTPAPI_CreateConnection(TEST_CREATE_CONNECTION_HOST_NAME); /* currentmalloc_call += 2 */
944
945 /// assert
946 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
947 ASSERT_ARE_EQUAL(int, 0, currentmalloc_call);
948 ASSERT_IS_NULL(httpHandle);
949
950 /// cleanup
951 HTTPAPI_Deinit();
952 }
953
954 /* HTTPAPI_CloseConnection */
955
956 /*Tests_SRS_HTTPAPI_COMPACT_21_017: [ The HTTPAPI_CloseConnection shall close the connection previously created in HTTPAPI_ExecuteRequest. ]*/
957 /*Tests_SRS_HTTPAPI_COMPACT_21_076: [ After close the connection, The HTTPAPI_CloseConnection shall destroy the connection previously created in HTTPAPI_CreateConnection. ]*/
958 TEST_FUNCTION(HTTPAPI_CloseConnection__valid_hostName_Succeed)
959 {
960 /// arrange
961 HTTP_HANDLE httpHandle = createHttpConnection();
962 STRICT_EXPECTED_CALL(xio_close(IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
963 .IgnoreAllArguments();
964 STRICT_EXPECTED_CALL(xio_destroy(IGNORED_PTR_ARG))
965 .IgnoreArgument(1);
966 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG))
967 .IgnoreArgument(1);
968 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG))
969 .IgnoreArgument(1);
970 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG));
971
972 /// act
973 HTTPAPI_CloseConnection(httpHandle); /* currentmalloc_call -= 2 */
974
975 /// assert
976 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
977 ASSERT_ARE_EQUAL(int, 0, currentmalloc_call);
978
979 /// cleanup
980 HTTPAPI_Deinit();
981 }
982
983 /*Tests_SRS_HTTPAPI_COMPACT_21_020: [ If the connection handle is NULL, the HTTPAPI_CloseConnection shall not do anything. ]*/
984 TEST_FUNCTION(HTTPAPI_CloseConnection__handle_NULL_Succeed)
985 {
986 /// arrange
987 HTTP_HANDLE httpHandle = NULL;
988 HTTPAPI_Init();
989
990 /// act
991 HTTPAPI_CloseConnection(httpHandle); /* currentmalloc_call -= 0 */
992
993 /// assert
994 ASSERT_ARE_EQUAL(int, 0, currentmalloc_call);
995
996 /// cleanup
997 HTTPAPI_Deinit();
998 }
999
1000 /*Tests_SRS_HTTPAPI_COMPACT_21_018: [ If there is a certificate associated to this connection, the HTTPAPI_CloseConnection shall free all allocated memory for the certificate. ]*/
1001 TEST_FUNCTION(HTTPAPI_CloseConnection__free_certificate_memory_succeed)
1002 {
1003 /// arrange
1004 HTTP_HANDLE httpHandle = createHttpConnection();
1005 setHttpCertificate(httpHandle);
1006 STRICT_EXPECTED_CALL(xio_close(IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
1007 .IgnoreAllArguments();
1008 STRICT_EXPECTED_CALL(xio_destroy(IGNORED_PTR_ARG))
1009 .IgnoreArgument(1);
1010 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG))
1011 .IgnoreArgument(1);
1012 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG))
1013 .IgnoreArgument(1);
1014 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG))
1015 .IgnoreArgument(1);
1016 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG)); // hostname
1017
1018 /// act
1019 HTTPAPI_CloseConnection(httpHandle); /* currentmalloc_call -= 3 */
1020
1021 /// assert
1022 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1023 ASSERT_ARE_EQUAL(int, 0, currentmalloc_call);
1024
1025 /// cleanup
1026 HTTPAPI_Deinit();
1027 }
1028
1029 /*Tests_SRS_HTTPAPI_COMPACT_06_001: [ If there is a x509 client certificate associated to this connection, the HTTAPI_CloseConnection shall free all allocated memory for the certificate. ]*/
1030 /*Tests_SRS_HTTPAPI_COMPACT_06_002: [ If there is a x509 client private key associated to this connection, then HTTP_CloseConnection shall free all the allocated memory for the private key. ]*/
1031 TEST_FUNCTION(HTTPAPI_CloseConnection__free_x509client_memory_succeed)
1032 {
1033 /// arrange
1034 HTTP_HANDLE httpHandle = createHttpConnection();
1035 setHttpx509ClientCertificateAndKey(httpHandle);
1036 umock_c_reset_all_calls();
1037 STRICT_EXPECTED_CALL(xio_close(IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
1038 .IgnoreAllArguments();
1039 STRICT_EXPECTED_CALL(xio_destroy(IGNORED_PTR_ARG))
1040 .IgnoreArgument(1);
1041 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG)) // From the xio destroy
1042 .IgnoreArgument(1);
1043 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG)) // the cert
1044 .IgnoreArgument(1);
1045 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG)) // the key
1046 .IgnoreArgument(1);
1047 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG)) // the instance.
1048 .IgnoreArgument(1);
1049 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG)); // hostname
1050
1051 /// act
1052 HTTPAPI_CloseConnection(httpHandle); /* currentmalloc_call -= 3 */
1053
1054 /// assert
1055 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1056 ASSERT_ARE_EQUAL(int, 0, currentmalloc_call);
1057
1058 /// cleanup
1059 HTTPAPI_Deinit();
1060 }
1061
1062 /*Tests_SRS_HTTPAPI_COMPACT_21_087: [ If the xio return anything different than 0, the HTTPAPI_CloseConnection shall destroy the connection anyway. ]*/
1063 TEST_FUNCTION(HTTPAPI_CloseConnection__return_LINE_failed)
1064 {
1065 /// arrange
1066 HTTP_HANDLE httpHandle = createHttpConnection();
1067
1068 xio_close_shallReturn = MU_FAILURE;
1069 DoworkJobsCloseSuccess = true;
1070 SkipDoworkJobsCloseResult = 0;
1071 call_on_io_close_complete_in_xio_close = true;
1072
1073 STRICT_EXPECTED_CALL(xio_close(IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
1074 .IgnoreAllArguments();
1075 STRICT_EXPECTED_CALL(xio_destroy(IGNORED_PTR_ARG))
1076 .IgnoreArgument(1);
1077 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG))
1078 .IgnoreArgument(1);
1079 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG))
1080 .IgnoreArgument(1);
1081 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG)); // hostname
1082
1083 /// act
1084 HTTPAPI_CloseConnection(httpHandle); /* currentmalloc_call -= 2 */
1085
1086 /// assert
1087 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1088 ASSERT_ARE_EQUAL(int, 0, currentmalloc_call);
1089
1090 /// cleanup
1091 HTTPAPI_Deinit();
1092 }
1093
1094 /*Tests_SRS_HTTPAPI_COMPACT_21_084: [ The HTTPAPI_CloseConnection shall wait, at least, 10 seconds for the SSL close process. ]*/
1095 /*Tests_SRS_HTTPAPI_COMPACT_21_086: [ The HTTPAPI_CloseConnection shall wait, at least, 100 milliseconds between retries. ]*/
1096 TEST_FUNCTION(HTTPAPI_CloseConnection__close_on_dowork_succeed)
1097 {
1098 /// arrange
1099 unsigned int statusCode;
1100 HTTPAPI_RESULT result;
1101 HTTP_HEADERS_HANDLE requestHttpHeaders;
1102 HTTP_HEADERS_HANDLE responseHttpHeaders;
1103 HTTP_HANDLE httpHandle = createHttpConnection();
1104 createHttpObjects(&requestHttpHeaders, &responseHttpHeaders);
1105 setHttpCertificate(httpHandle);
1106
1107 DoworkJobsReceivedBuffer = TEST_RECEIVED_ANSWER;
1108 DoworkJobsReceivedBuffer_size[0] = strlen((const char*)DoworkJobsReceivedBuffer);
1109 DoworkJobsReceivedBuffer_counter = 0;
1110 DoworkJobs = (const xio_dowork_job*)doworkjob_o_rce;
1111 DoworkJobsOpenResult = DoworkJobsOpenResult_ReceiveHead;
1112 DoworkJobsSendResult = DoworkJobsSendResult_ReceiveHead;
1113
1114 setupAllCallBeforeOpenHTTPsequence(requestHttpHeaders, 1, false);
1115 setupAllCallBeforeSendHTTPsequenceWithSuccess(requestHttpHeaders);
1116 setupAllCallBeforeReceiveHTTPsequenceWithSuccess();
1117
1118 HTTPHeaders_GetHeader_shallReturn = HTTP_HEADERS_OK;
1119
1120 xio_close_shallReturn = 0;
1121 DoworkJobsCloseSuccess = true;
1122 SkipDoworkJobsCloseResult = 0;
1123 call_on_io_close_complete_in_xio_close = false;
1124
1125 STRICT_EXPECTED_CALL(xio_close(IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
1126 .IgnoreAllArguments();
1127 STRICT_EXPECTED_CALL(xio_dowork(IGNORED_PTR_ARG))
1128 .IgnoreArgument(1);
1129 STRICT_EXPECTED_CALL(xio_destroy(IGNORED_PTR_ARG))
1130 .IgnoreArgument(1);
1131 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG))
1132 .IgnoreArgument(1);
1133 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG))
1134 .IgnoreArgument(1);
1135 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG))
1136 .IgnoreArgument(1);
1137 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG)); // hostname
1138
1139 result = HTTPAPI_ExecuteRequest(
1140 httpHandle,
1141 HTTPAPI_REQUEST_GET,
1142 TEST_EXECUTE_REQUEST_RELATIVE_PATH,
1143 requestHttpHeaders,
1144 TEST_EXECUTE_REQUEST_CONTENT,
1145 TEST_EXECUTE_REQUEST_CONTENT_LENGTH,
1146 &statusCode,
1147 responseHttpHeaders,
1148 TestBufferHandle);
1149 ASSERT_ARE_EQUAL(int, HTTPAPI_OK, result);
1150
1151 /// act
1152 HTTPAPI_CloseConnection(httpHandle); /* currentmalloc_call -= 3 */
1153
1154 /// assert
1155 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1156 ASSERT_ARE_EQUAL(int, 2, currentmalloc_call);
1157
1158 /// cleanup
1159 destroyHttpObjects(&requestHttpHeaders, &responseHttpHeaders); /* currentmalloc_call -= 2 */
1160 HTTPAPI_Deinit();
1161 }
1162
1163 /*Tests_SRS_HTTPAPI_COMPACT_21_084: [ The HTTPAPI_CloseConnection shall wait, at least, 10 seconds for the SSL close process. ]*/
1164 TEST_FUNCTION(HTTPAPI_CloseConnection__close_on_dowork_retry_n_succeed)
1165 {
1166 /// arrange
1167 int i;
1168 unsigned int statusCode;
1169 HTTPAPI_RESULT result;
1170 HTTP_HEADERS_HANDLE requestHttpHeaders;
1171 HTTP_HEADERS_HANDLE responseHttpHeaders;
1172 HTTP_HANDLE httpHandle = createHttpConnection();
1173 createHttpObjects(&requestHttpHeaders, &responseHttpHeaders);
1174 setHttpCertificate(httpHandle);
1175
1176 DoworkJobsReceivedBuffer = TEST_RECEIVED_ANSWER;
1177 DoworkJobsReceivedBuffer_size[0] = strlen((const char*)DoworkJobsReceivedBuffer);
1178 DoworkJobsReceivedBuffer_counter = 0;
1179 DoworkJobs = (const xio_dowork_job*)doworkjob_o_rce;
1180 DoworkJobsOpenResult = DoworkJobsOpenResult_ReceiveHead;
1181 DoworkJobsSendResult = DoworkJobsSendResult_ReceiveHead;
1182
1183 setupAllCallBeforeOpenHTTPsequence(requestHttpHeaders, 1, false);
1184 setupAllCallBeforeSendHTTPsequenceWithSuccess(requestHttpHeaders);
1185 setupAllCallBeforeReceiveHTTPsequenceWithSuccess();
1186
1187 HTTPHeaders_GetHeader_shallReturn = HTTP_HEADERS_OK;
1188
1189 xio_close_shallReturn = 0;
1190 DoworkJobsCloseSuccess = true;
1191 SkipDoworkJobsCloseResult = 90;
1192 call_on_io_close_complete_in_xio_close = false;
1193
1194 STRICT_EXPECTED_CALL(xio_close(IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
1195 .IgnoreAllArguments();
1196 for (i = 0; i < SkipDoworkJobsCloseResult; i++)
1197 {
1198 STRICT_EXPECTED_CALL(xio_dowork(IGNORED_PTR_ARG))
1199 .IgnoreArgument(1);
1200 STRICT_EXPECTED_CALL(ThreadAPI_Sleep(100));
1201 }
1202 STRICT_EXPECTED_CALL(xio_dowork(IGNORED_PTR_ARG))
1203 .IgnoreArgument(1);
1204 STRICT_EXPECTED_CALL(xio_destroy(IGNORED_PTR_ARG))
1205 .IgnoreArgument(1);
1206 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG))
1207 .IgnoreArgument(1);
1208 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG))
1209 .IgnoreArgument(1);
1210 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG))
1211 .IgnoreArgument(1);
1212 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG)); // hostname
1213
1214 result = HTTPAPI_ExecuteRequest(
1215 httpHandle,
1216 HTTPAPI_REQUEST_GET,
1217 TEST_EXECUTE_REQUEST_RELATIVE_PATH,
1218 requestHttpHeaders,
1219 TEST_EXECUTE_REQUEST_CONTENT,
1220 TEST_EXECUTE_REQUEST_CONTENT_LENGTH,
1221 &statusCode,
1222 responseHttpHeaders,
1223 TestBufferHandle);
1224 ASSERT_ARE_EQUAL(int, HTTPAPI_OK, result);
1225
1226 /// act
1227 HTTPAPI_CloseConnection(httpHandle); /* currentmalloc_call -= 3 */
1228
1229 /// assert
1230 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1231 ASSERT_ARE_EQUAL(int, 2, currentmalloc_call);
1232
1233 /// cleanup
1234 destroyHttpObjects(&requestHttpHeaders, &responseHttpHeaders); /* currentmalloc_call -= 2 */
1235 HTTPAPI_Deinit();
1236 }
1237
1238 /*Tests_SRS_HTTPAPI_COMPACT_21_084: [ The HTTPAPI_CloseConnection shall wait, at least, 10 seconds for the SSL close process. ]*/
1239 TEST_FUNCTION(HTTPAPI_CloseConnection__close_on_dowork_retry_n_failed)
1240 {
1241 /// arrange
1242 int i;
1243 unsigned int statusCode;
1244 HTTPAPI_RESULT result;
1245 HTTP_HEADERS_HANDLE requestHttpHeaders;
1246 HTTP_HEADERS_HANDLE responseHttpHeaders;
1247 HTTP_HANDLE httpHandle = createHttpConnection();
1248 createHttpObjects(&requestHttpHeaders, &responseHttpHeaders);
1249 setHttpCertificate(httpHandle);
1250
1251 DoworkJobsReceivedBuffer = TEST_RECEIVED_ANSWER;
1252 DoworkJobsReceivedBuffer_size[0] = strlen((const char*)DoworkJobsReceivedBuffer);
1253 DoworkJobsReceivedBuffer_counter = 0;
1254 DoworkJobs = (const xio_dowork_job*)doworkjob_o_rc_error;
1255 DoworkJobsOpenResult = DoworkJobsOpenResult_ReceiveHead;
1256 DoworkJobsSendResult = DoworkJobsSendResult_ReceiveHead;
1257
1258 setupAllCallBeforeOpenHTTPsequence(requestHttpHeaders, 1, false);
1259 setupAllCallBeforeSendHTTPsequenceWithSuccess(requestHttpHeaders);
1260 setupAllCallBeforeReceiveHTTPsequenceWithSuccess();
1261
1262 HTTPHeaders_GetHeader_shallReturn = HTTP_HEADERS_OK;
1263
1264 xio_close_shallReturn = 0;
1265 DoworkJobsCloseSuccess = false;
1266 SkipDoworkJobsCloseResult = 90;
1267 call_on_io_close_complete_in_xio_close = false;
1268
1269 STRICT_EXPECTED_CALL(xio_close(IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
1270 .IgnoreAllArguments();
1271 for (i = 0; i < SkipDoworkJobsCloseResult + 1; i++)
1272 {
1273 STRICT_EXPECTED_CALL(xio_dowork(IGNORED_PTR_ARG))
1274 .IgnoreArgument(1);
1275 STRICT_EXPECTED_CALL(ThreadAPI_Sleep(100));
1276 }
1277 STRICT_EXPECTED_CALL(xio_dowork(IGNORED_PTR_ARG))
1278 .IgnoreArgument(1);
1279 STRICT_EXPECTED_CALL(xio_destroy(IGNORED_PTR_ARG))
1280 .IgnoreArgument(1);
1281 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG))
1282 .IgnoreArgument(1);
1283 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG))
1284 .IgnoreArgument(1);
1285 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG))
1286 .IgnoreArgument(1);
1287 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG));
1288
1289 result = HTTPAPI_ExecuteRequest(
1290 httpHandle,
1291 HTTPAPI_REQUEST_GET,
1292 TEST_EXECUTE_REQUEST_RELATIVE_PATH,
1293 requestHttpHeaders,
1294 TEST_EXECUTE_REQUEST_CONTENT,
1295 TEST_EXECUTE_REQUEST_CONTENT_LENGTH,
1296 &statusCode,
1297 responseHttpHeaders,
1298 TestBufferHandle);
1299 ASSERT_ARE_EQUAL(int, HTTPAPI_OK, result);
1300
1301 /// act
1302 HTTPAPI_CloseConnection(httpHandle); /* currentmalloc_call -= 3 */
1303
1304 /// assert
1305 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1306 ASSERT_ARE_EQUAL(int, 2, currentmalloc_call);
1307
1308 /// cleanup
1309 destroyHttpObjects(&requestHttpHeaders, &responseHttpHeaders); /* currentmalloc_call -= 2 */
1310 HTTPAPI_Deinit();
1311 }
1312
1313 /*Tests_SRS_HTTPAPI_COMPACT_21_085: [ If the HTTPAPI_CloseConnection retries 10 seconds to close the connection without success, it shall destroy the connection anyway. ]*/
1314 TEST_FUNCTION(HTTPAPI_CloseConnection__close_timeout_failed)
1315 {
1316 /// arrange
1317 int i;
1318 unsigned int statusCode;
1319 HTTPAPI_RESULT result;
1320 HTTP_HEADERS_HANDLE requestHttpHeaders;
1321 HTTP_HEADERS_HANDLE responseHttpHeaders;
1322 HTTP_HANDLE httpHandle = createHttpConnection();
1323 createHttpObjects(&requestHttpHeaders, &responseHttpHeaders);
1324 setHttpCertificate(httpHandle);
1325
1326 DoworkJobsReceivedBuffer = TEST_RECEIVED_ANSWER;
1327 DoworkJobsReceivedBuffer_size[0] = strlen((const char*)DoworkJobsReceivedBuffer);
1328 DoworkJobsReceivedBuffer_counter = 0;
1329 DoworkJobs = (const xio_dowork_job*)doworkjob_o_rce;
1330 DoworkJobsOpenResult = DoworkJobsOpenResult_ReceiveHead;
1331 DoworkJobsSendResult = DoworkJobsSendResult_ReceiveHead;
1332
1333 setupAllCallBeforeOpenHTTPsequence(requestHttpHeaders, 1, false);
1334 setupAllCallBeforeSendHTTPsequenceWithSuccess(requestHttpHeaders);
1335 setupAllCallBeforeReceiveHTTPsequenceWithSuccess();
1336
1337 HTTPHeaders_GetHeader_shallReturn = HTTP_HEADERS_OK;
1338
1339 xio_close_shallReturn = 0;
1340 DoworkJobsCloseSuccess = true;
1341 SkipDoworkJobsCloseResult = 101;
1342 call_on_io_close_complete_in_xio_close = false;
1343
1344 STRICT_EXPECTED_CALL(xio_close(IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
1345 .IgnoreAllArguments();
1346 for (i = 0; i < SkipDoworkJobsCloseResult; i++)
1347 {
1348 STRICT_EXPECTED_CALL(xio_dowork(IGNORED_PTR_ARG))
1349 .IgnoreArgument(1);
1350 STRICT_EXPECTED_CALL(ThreadAPI_Sleep(100));
1351 }
1352 STRICT_EXPECTED_CALL(xio_dowork(IGNORED_PTR_ARG))
1353 .IgnoreArgument(1);
1354 STRICT_EXPECTED_CALL(xio_destroy(IGNORED_PTR_ARG))
1355 .IgnoreArgument(1);
1356 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG))
1357 .IgnoreArgument(1);
1358 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG))
1359 .IgnoreArgument(1);
1360 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG))
1361 .IgnoreArgument(1);
1362 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG)); // hostname
1363
1364 result = HTTPAPI_ExecuteRequest(
1365 httpHandle,
1366 HTTPAPI_REQUEST_GET,
1367 TEST_EXECUTE_REQUEST_RELATIVE_PATH,
1368 requestHttpHeaders,
1369 TEST_EXECUTE_REQUEST_CONTENT,
1370 TEST_EXECUTE_REQUEST_CONTENT_LENGTH,
1371 &statusCode,
1372 responseHttpHeaders,
1373 TestBufferHandle);
1374 ASSERT_ARE_EQUAL(int, HTTPAPI_OK, result);
1375
1376 /// act
1377 HTTPAPI_CloseConnection(httpHandle); /* currentmalloc_call -= 3 */
1378
1379 /// assert
1380 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1381 ASSERT_ARE_EQUAL(int, 2, currentmalloc_call);
1382
1383 /// cleanup
1384 destroyHttpObjects(&requestHttpHeaders, &responseHttpHeaders); /* currentmalloc_call -= 2 */
1385 HTTPAPI_Deinit();
1386 }
1387
1388 /* HTTPAPI_ExecuteRequest */
1389
1390 /*Tests_SRS_HTTPAPI_COMPACT_21_034: [ If there is no previous connection, the HTTPAPI_ExecuteRequest shall return HTTPAPI_INVALID_ARG. ]*/
1391 TEST_FUNCTION(HTTPAPI_ExecuteRequest__NULL_handle_failed)
1392 {
1393 /// arrange
1394 unsigned int statusCode;
1395 HTTPAPI_RESULT result;
1396 HTTP_HEADERS_HANDLE requestHttpHeaders;
1397 HTTP_HEADERS_HANDLE responseHttpHeaders;
1398 createHttpObjects(&requestHttpHeaders, &responseHttpHeaders);
1399
1400 /// act
1401 result = HTTPAPI_ExecuteRequest(
1402 (HTTP_HANDLE)NULL,
1403 HTTPAPI_REQUEST_GET,
1404 TEST_EXECUTE_REQUEST_RELATIVE_PATH,
1405 requestHttpHeaders,
1406 TEST_EXECUTE_REQUEST_CONTENT,
1407 TEST_EXECUTE_REQUEST_CONTENT_LENGTH,
1408 &statusCode,
1409 responseHttpHeaders,
1410 TestBufferHandle);
1411
1412 /// assert
1413 ASSERT_ARE_EQUAL(int, HTTPAPI_INVALID_ARG, result);
1414 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1415 ASSERT_ARE_EQUAL(int, 2, currentmalloc_call);
1416
1417 /// cleanup
1418 destroyHttpObjects(&requestHttpHeaders, &responseHttpHeaders); /* currentmalloc_call -= 2 */
1419 HTTPAPI_Deinit();
1420 }
1421
1422 /*Tests_SRS_HTTPAPI_COMPACT_21_037: [ If the request type is unknown, the HTTPAPI_ExecuteRequest shall return HTTPAPI_INVALID_ARG. ]*/
1423 TEST_FUNCTION(HTTPAPI_ExecuteRequest__invalid_request_type_failed)
1424 {
1425 /// arrange
1426 unsigned int statusCode;
1427 HTTPAPI_RESULT result;
1428 HTTP_HEADERS_HANDLE requestHttpHeaders;
1429 HTTP_HEADERS_HANDLE responseHttpHeaders;
1430 HTTP_HANDLE httpHandle = createHttpConnection();
1431 createHttpObjects(&requestHttpHeaders, &responseHttpHeaders);
1432
1433 /// act
1434 result = HTTPAPI_ExecuteRequest(
1435 httpHandle,
1436 (HTTPAPI_REQUEST_TYPE)MU_ENUM_VALUE_COUNT(HTTPAPI_REQUEST_TYPE_VALUES),
1437 TEST_EXECUTE_REQUEST_RELATIVE_PATH,
1438 requestHttpHeaders,
1439 TEST_EXECUTE_REQUEST_CONTENT,
1440 TEST_EXECUTE_REQUEST_CONTENT_LENGTH,
1441 &statusCode,
1442 responseHttpHeaders,
1443 TestBufferHandle);
1444
1445 /// assert
1446 ASSERT_ARE_EQUAL(int, HTTPAPI_INVALID_ARG, result);
1447 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1448 ASSERT_ARE_EQUAL(int, 5, currentmalloc_call);
1449
1450 /// cleanup
1451 destroyHttpObjects(&requestHttpHeaders, &responseHttpHeaders); /* currentmalloc_call -= 2 */
1452 HTTPAPI_CloseConnection(httpHandle); /* currentmalloc_call -= 2 */
1453 HTTPAPI_Deinit();
1454 }
1455
1456 /*Tests_SRS_HTTPAPI_COMPACT_21_039: [ If the relativePath is NULL or invalid, the HTTPAPI_ExecuteRequest shall return HTTPAPI_INVALID_ARG. ]*/
1457 TEST_FUNCTION(HTTPAPI_ExecuteRequest__NULL_relative_path_failed)
1458 {
1459 /// arrange
1460 unsigned int statusCode;
1461 HTTPAPI_RESULT result;
1462 HTTP_HEADERS_HANDLE requestHttpHeaders;
1463 HTTP_HEADERS_HANDLE responseHttpHeaders;
1464 HTTP_HANDLE httpHandle = createHttpConnection();
1465 createHttpObjects(&requestHttpHeaders, &responseHttpHeaders);
1466
1467 /// act
1468 result = HTTPAPI_ExecuteRequest(
1469 httpHandle,
1470 HTTPAPI_REQUEST_GET,
1471 NULL,
1472 requestHttpHeaders,
1473 TEST_EXECUTE_REQUEST_CONTENT,
1474 TEST_EXECUTE_REQUEST_CONTENT_LENGTH,
1475 &statusCode,
1476 responseHttpHeaders,
1477 TestBufferHandle);
1478
1479 /// assert
1480 ASSERT_ARE_EQUAL(int, HTTPAPI_INVALID_ARG, result);
1481 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1482 ASSERT_ARE_EQUAL(int, 5, currentmalloc_call);
1483
1484 /// cleanup
1485 destroyHttpObjects(&requestHttpHeaders, &responseHttpHeaders); /* currentmalloc_call -= 2 */
1486 HTTPAPI_CloseConnection(httpHandle); /* currentmalloc_call -= 2 */
1487 HTTPAPI_Deinit();
1488 }
1489
1490 /*Tests_SRS_HTTPAPI_COMPACT_21_041: [ If the httpHeadersHandle is NULL or invalid, the HTTPAPI_ExecuteRequest shall return HTTPAPI_INVALID_ARG. ]*/
1491 TEST_FUNCTION(HTTPAPI_ExecuteRequest__NULL_http_headers_handle_failed)
1492 {
1493 /// arrange
1494 unsigned int statusCode;
1495 HTTPAPI_RESULT result;
1496 HTTP_HEADERS_HANDLE requestHttpHeaders;
1497 HTTP_HEADERS_HANDLE responseHttpHeaders;
1498 HTTP_HANDLE httpHandle = createHttpConnection();
1499 createHttpObjects(&requestHttpHeaders, &responseHttpHeaders);
1500
1501 /// act
1502 result = HTTPAPI_ExecuteRequest(
1503 httpHandle,
1504 HTTPAPI_REQUEST_GET,
1505 TEST_EXECUTE_REQUEST_RELATIVE_PATH,
1506 (HTTP_HEADERS_HANDLE)NULL,
1507 TEST_EXECUTE_REQUEST_CONTENT,
1508 TEST_EXECUTE_REQUEST_CONTENT_LENGTH,
1509 &statusCode,
1510 responseHttpHeaders,
1511 TestBufferHandle);
1512
1513 /// assert
1514 ASSERT_ARE_EQUAL(int, HTTPAPI_INVALID_ARG, result);
1515 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1516 ASSERT_ARE_EQUAL(int, 5, currentmalloc_call);
1517
1518 /// cleanup
1519 destroyHttpObjects(&requestHttpHeaders, &responseHttpHeaders); /* currentmalloc_call -= 2 */
1520 HTTPAPI_CloseConnection(httpHandle); /* currentmalloc_call -= 2 */
1521 HTTPAPI_Deinit();
1522 }
1523
1524 /*Tests_SRS_HTTPAPI_COMPACT_21_041: [ If the httpHeadersHandle is NULL or invalid, the HTTPAPI_ExecuteRequest shall return HTTPAPI_INVALID_ARG. ]*/
1525 TEST_FUNCTION(HTTPAPI_ExecuteRequest__invalid_http_headers_handle_failed)
1526 {
1527 /// arrange
1528 unsigned int statusCode;
1529 HTTPAPI_RESULT result;
1530 HTTP_HEADERS_HANDLE requestHttpHeaders;
1531 HTTP_HEADERS_HANDLE responseHttpHeaders;
1532 HTTP_HANDLE httpHandle = createHttpConnection();
1533 createHttpObjects(&requestHttpHeaders, &responseHttpHeaders);
1534 HTTPHeaders_GetHeaderCount_shallReturn = HTTP_HEADERS_INVALID_ARG;
1535
1536 STRICT_EXPECTED_CALL(HTTPHeaders_GetHeaderCount(requestHttpHeaders, IGNORED_PTR_ARG))
1537 .IgnoreArgument(2);
1538
1539 /// act
1540 result = HTTPAPI_ExecuteRequest(
1541 httpHandle,
1542 HTTPAPI_REQUEST_GET,
1543 TEST_EXECUTE_REQUEST_RELATIVE_PATH,
1544 requestHttpHeaders,
1545 TEST_EXECUTE_REQUEST_CONTENT,
1546 TEST_EXECUTE_REQUEST_CONTENT_LENGTH,
1547 &statusCode,
1548 responseHttpHeaders,
1549 TestBufferHandle);
1550
1551 /// assert
1552 ASSERT_ARE_EQUAL(int, HTTPAPI_INVALID_ARG, result);
1553 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1554 ASSERT_ARE_EQUAL(int, 5, currentmalloc_call);
1555
1556 /// cleanup
1557 destroyHttpObjects(&requestHttpHeaders, &responseHttpHeaders); /* currentmalloc_call -= 2 */
1558 HTTPAPI_CloseConnection(httpHandle); /* currentmalloc_call -= 2 */
1559 HTTPAPI_Deinit();
1560 }
1561
1562 /*Tests_SRS_HTTPAPI_COMPACT_21_054: [ If Http header maker cannot provide the number of headers, the HTTPAPI_ExecuteRequest shall return HTTPAPI_INVALID_ARG. ]*/
1563 TEST_FUNCTION(HTTPAPI_ExecuteRequest__http_headers_handle_failed)
1564 {
1565 /// arrange
1566 unsigned int statusCode;
1567 HTTPAPI_RESULT result;
1568 HTTP_HEADERS_HANDLE requestHttpHeaders;
1569 HTTP_HEADERS_HANDLE responseHttpHeaders;
1570 HTTP_HANDLE httpHandle = createHttpConnection();
1571 createHttpObjects(&requestHttpHeaders, &responseHttpHeaders);
1572 HTTPHeaders_GetHeaderCount_shallReturn = HTTP_HEADERS_ERROR;
1573
1574 STRICT_EXPECTED_CALL(HTTPHeaders_GetHeaderCount(requestHttpHeaders, IGNORED_PTR_ARG))
1575 .IgnoreArgument(2);
1576
1577 /// act
1578 result = HTTPAPI_ExecuteRequest(
1579 httpHandle,
1580 HTTPAPI_REQUEST_GET,
1581 TEST_EXECUTE_REQUEST_RELATIVE_PATH,
1582 requestHttpHeaders,
1583 TEST_EXECUTE_REQUEST_CONTENT,
1584 TEST_EXECUTE_REQUEST_CONTENT_LENGTH,
1585 &statusCode,
1586 responseHttpHeaders,
1587 TestBufferHandle);
1588
1589 /// assert
1590 ASSERT_ARE_EQUAL(int, HTTPAPI_INVALID_ARG, result);
1591 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1592 ASSERT_ARE_EQUAL(int, 5, currentmalloc_call);
1593
1594 /// cleanup
1595 destroyHttpObjects(&requestHttpHeaders, &responseHttpHeaders); /* currentmalloc_call -= 2 */
1596 HTTPAPI_CloseConnection(httpHandle); /* currentmalloc_call -= 2 */
1597 HTTPAPI_Deinit();
1598 }
1599
1600 /*Tests_SRS_HTTPAPI_COMPACT_21_023: [ If the transport failed setting the Certificate, the HTTPAPI_ExecuteRequest shall not send any request and return HTTPAPI_SET_OPTION_FAILED. ]*/
1601 TEST_FUNCTION(HTTPAPI_ExecuteRequest__certificate_failed)
1602 {
1603 /// arrange
1604 unsigned int statusCode;
1605 HTTPAPI_RESULT result;
1606 HTTP_HEADERS_HANDLE requestHttpHeaders;
1607 HTTP_HEADERS_HANDLE responseHttpHeaders;
1608 HTTP_HANDLE httpHandle = createHttpConnection();
1609
1610 createHttpObjects(&requestHttpHeaders, &responseHttpHeaders);
1611 setHttpCertificate(httpHandle);
1612 xio_setoption_shallReturn = MU_FAILURE;
1613
1614 STRICT_EXPECTED_CALL(HTTPHeaders_GetHeaderCount(requestHttpHeaders, IGNORED_PTR_ARG))
1615 .IgnoreArgument(2);
1616 STRICT_EXPECTED_CALL(xio_setoption(IGNORED_PTR_ARG, "TrustedCerts", TEST_SETOPTIONS_CERTIFICATE))
1617 .IgnoreArgument(1)
1618 .IgnoreArgument(3);
1619
1620 /// act
1621 result = HTTPAPI_ExecuteRequest(
1622 httpHandle,
1623 HTTPAPI_REQUEST_GET,
1624 TEST_EXECUTE_REQUEST_RELATIVE_PATH,
1625 requestHttpHeaders,
1626 TEST_EXECUTE_REQUEST_CONTENT,
1627 TEST_EXECUTE_REQUEST_CONTENT_LENGTH,
1628 &statusCode,
1629 responseHttpHeaders,
1630 TestBufferHandle);
1631
1632 /// assert
1633 ASSERT_ARE_EQUAL(int, HTTPAPI_SET_OPTION_FAILED, result);
1634 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1635 ASSERT_ARE_EQUAL(int, 6, currentmalloc_call);
1636
1637 /// cleanup
1638 destroyHttpObjects(&requestHttpHeaders, &responseHttpHeaders); /* currentmalloc_call -= 2 */
1639 HTTPAPI_CloseConnection(httpHandle); /* currentmalloc_call -= 3 */
1640 HTTPAPI_Deinit();
1641 }
1642
1643 /*Tests_SRS_HTTPAPI_COMPACT_06_005: [ If the transport failed setting the client certificate, the HTTPAPI_ExecuteRequest shall not send any request and return HTTPAPI_SET_OPTION_FAILED. ]*/
1644 TEST_FUNCTION(HTTPAPI_ExecuteRequest__x509client_certificate_failed)
1645 {
1646 /// arrange
1647 unsigned int statusCode;
1648 HTTPAPI_RESULT result;
1649 HTTP_HEADERS_HANDLE requestHttpHeaders;
1650 HTTP_HEADERS_HANDLE responseHttpHeaders;
1651 HTTP_HANDLE httpHandle = createHttpConnection();
1652
1653 createHttpObjects(&requestHttpHeaders, &responseHttpHeaders);
1654 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG))
1655 .IgnoreArgument(1);
1656 HTTPAPI_SetOption(httpHandle, SU_OPTION_X509_CERT, TEST_SETOPTIONS_X509CLIENTCERT); /* currentmalloc_call += 1 */
1657
1658 xio_setoption_shallReturn = MU_FAILURE;
1659
1660 STRICT_EXPECTED_CALL(HTTPHeaders_GetHeaderCount(requestHttpHeaders, IGNORED_PTR_ARG))
1661 .IgnoreArgument(2);
1662 STRICT_EXPECTED_CALL(xio_setoption(IGNORED_PTR_ARG, SU_OPTION_X509_CERT, TEST_SETOPTIONS_X509CLIENTCERT))
1663 .IgnoreArgument(1)
1664 .IgnoreArgument(3);
1665
1666 /// act
1667 result = HTTPAPI_ExecuteRequest(
1668 httpHandle,
1669 HTTPAPI_REQUEST_GET,
1670 TEST_EXECUTE_REQUEST_RELATIVE_PATH,
1671 requestHttpHeaders,
1672 TEST_EXECUTE_REQUEST_CONTENT,
1673 TEST_EXECUTE_REQUEST_CONTENT_LENGTH,
1674 &statusCode,
1675 responseHttpHeaders,
1676 TestBufferHandle);
1677
1678 /// assert
1679 ASSERT_ARE_EQUAL(int, HTTPAPI_SET_OPTION_FAILED, result);
1680 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1681 ASSERT_ARE_EQUAL(int, 6, currentmalloc_call);
1682
1683 /// cleanup
1684 destroyHttpObjects(&requestHttpHeaders, &responseHttpHeaders); /* currentmalloc_call -= 2 */
1685 HTTPAPI_CloseConnection(httpHandle); /* currentmalloc_call -= 3 */
1686 HTTPAPI_Deinit();
1687 }
1688
1689 /*Tests_SRS_HTTPAPI_COMPACT_06_006: [ If the transport failed setting the client certificate private key, the HTTPAPI_ExecuteRequest shall not send any request and return HTTPAPI_SET_OPTION_FAILED. ] */
1690 TEST_FUNCTION(HTTPAPI_ExecuteRequest__x509client_privatekey_failed)
1691 {
1692 /// arrange
1693 unsigned int statusCode;
1694 HTTPAPI_RESULT result;
1695 HTTP_HEADERS_HANDLE requestHttpHeaders;
1696 HTTP_HEADERS_HANDLE responseHttpHeaders;
1697 HTTP_HANDLE httpHandle = createHttpConnection();
1698
1699 createHttpObjects(&requestHttpHeaders, &responseHttpHeaders);
1700 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG))
1701 .IgnoreArgument(1);
1702 HTTPAPI_SetOption(httpHandle, SU_OPTION_X509_PRIVATE_KEY, TEST_SETOPTIONS_X509PRIVATEKEY); /* currentmalloc_call += 1 */
1703
1704 xio_setoption_shallReturn = MU_FAILURE;
1705
1706 STRICT_EXPECTED_CALL(HTTPHeaders_GetHeaderCount(requestHttpHeaders, IGNORED_PTR_ARG))
1707 .IgnoreArgument(2);
1708 STRICT_EXPECTED_CALL(xio_setoption(IGNORED_PTR_ARG, SU_OPTION_X509_PRIVATE_KEY, TEST_SETOPTIONS_X509PRIVATEKEY))
1709 .IgnoreArgument(1)
1710 .IgnoreArgument(3);
1711
1712 /// act
1713 result = HTTPAPI_ExecuteRequest(
1714 httpHandle,
1715 HTTPAPI_REQUEST_GET,
1716 TEST_EXECUTE_REQUEST_RELATIVE_PATH,
1717 requestHttpHeaders,
1718 TEST_EXECUTE_REQUEST_CONTENT,
1719 TEST_EXECUTE_REQUEST_CONTENT_LENGTH,
1720 &statusCode,
1721 responseHttpHeaders,
1722 TestBufferHandle);
1723
1724 /// assert
1725 ASSERT_ARE_EQUAL(int, HTTPAPI_SET_OPTION_FAILED, result);
1726 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1727 ASSERT_ARE_EQUAL(int, 6, currentmalloc_call);
1728
1729 /// cleanup
1730 destroyHttpObjects(&requestHttpHeaders, &responseHttpHeaders); /* currentmalloc_call -= 2 */
1731 HTTPAPI_CloseConnection(httpHandle); /* currentmalloc_call -= 3 */
1732 HTTPAPI_Deinit();
1733 }
1734
1735 /*Tests_SRS_HTTPAPI_COMPACT_21_052: [ If any memory allocation get fail, the HTTPAPI_ExecuteRequest shall return HTTPAPI_ALLOC_FAILED. ]*/
1736 /*Tests_SRS_HTTPAPI_COMPACT_21_062: [ If any memory allocation get fail, the HTTPAPI_SetOption shall return HTTPAPI_ALLOC_FAILED. ]*/
1737 TEST_FUNCTION(HTTPAPI_ExecuteRequest__certificate_out_of_memory_failed)
1738 {
1739 /// arrange
1740 HTTPAPI_RESULT result;
1741 HTTP_HANDLE httpHandle = createHttpConnection();
1742 xio_setoption_shallReturn = 0;
1743 whenShallmalloc_fail = 1;
1744
1745 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG))
1746 .IgnoreArgument(1);
1747
1748 /// act
1749 result = HTTPAPI_SetOption(httpHandle, "TrustedCerts", TEST_SETOPTIONS_CERTIFICATE);
1750
1751 /// assert
1752 ASSERT_ARE_EQUAL(int, HTTPAPI_ALLOC_FAILED, result);
1753 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1754 ASSERT_ARE_EQUAL(int, 3, currentmalloc_call);
1755
1756 /// cleanup
1757 HTTPAPI_CloseConnection(httpHandle); /* currentmalloc_call -= 2 */
1758 HTTPAPI_Deinit();
1759 }
1760
1761 /*Tests_SRS_HTTPAPI_COMPACT_21_064: [ If the HTTPAPI_SetOption get success setting the option, it shall return HTTPAPI_OK. ]*/
1762 TEST_FUNCTION(HTTPAPI_ExecuteRequest__certificate_succeed)
1763 {
1764 /// arrange
1765 HTTPAPI_RESULT result;
1766 HTTP_HANDLE httpHandle = createHttpConnection();
1767 xio_setoption_shallReturn = 0;
1768
1769 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG))
1770 .IgnoreArgument(1);
1771
1772 /// act
1773 result = HTTPAPI_SetOption(httpHandle, "TrustedCerts", TEST_SETOPTIONS_CERTIFICATE);
1774
1775 /// assert
1776 ASSERT_ARE_EQUAL(int, HTTPAPI_OK, result);
1777 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1778 ASSERT_ARE_EQUAL(int, 4, currentmalloc_call);
1779
1780 /// cleanup
1781 HTTPAPI_CloseConnection(httpHandle); /* currentmalloc_call -= 3 */
1782 HTTPAPI_Deinit();
1783 }
1784
1785
1786
1787 /*Tests_SRS_HTTPAPI_COMPACT_21_059: [ If the handle is NULL, the HTTPAPI_SetOption shall return HTTPAPI_INVALID_ARG. ]*/
1788 TEST_FUNCTION(HTTPAPI_ExecuteRequest__certificate_NULL_handle_failed)
1789 {
1790 /// arrange
1791 HTTPAPI_RESULT result;
1792
1793 /// act
1794 result = HTTPAPI_SetOption(NULL, "TrustedCerts", TEST_SETOPTIONS_CERTIFICATE);
1795
1796 /// assert
1797 ASSERT_ARE_EQUAL(int, HTTPAPI_INVALID_ARG, result);
1798
1799 /// cleanup
1800 }
1801
1802 /*Tests_SRS_HTTPAPI_COMPACT_21_060: [ If the optionName is NULL, the HTTPAPI_SetOption shall return HTTPAPI_INVALID_ARG. ]*/
1803 TEST_FUNCTION(HTTPAPI_ExecuteRequest__certificate_NULL_optionName_failed)
1804 {
1805 /// arrange
1806 HTTPAPI_RESULT result;
1807 HTTP_HANDLE httpHandle = createHttpConnection();
1808 xio_setoption_shallReturn = 0;
1809
1810 /// act
1811 result = HTTPAPI_SetOption(httpHandle, NULL, TEST_SETOPTIONS_CERTIFICATE);
1812
1813 /// assert
1814 ASSERT_ARE_EQUAL(int, HTTPAPI_INVALID_ARG, result);
1815 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1816 ASSERT_ARE_EQUAL(int, 3, currentmalloc_call);
1817
1818 /// cleanup
1819 HTTPAPI_CloseConnection(httpHandle); /* currentmalloc_call -= 2 */
1820 HTTPAPI_Deinit();
1821 }
1822
1823 /*Tests_SRS_HTTPAPI_COMPACT_21_063: [ If the HTTP do not support the optionName, the HTTPAPI_SetOption shall return HTTPAPI_INVALID_ARG. ]*/
1824 TEST_FUNCTION(HTTPAPI_ExecuteRequest__certificate_invalid_optionName_failed)
1825 {
1826 /// arrange
1827 HTTPAPI_RESULT result;
1828 HTTP_HANDLE httpHandle = createHttpConnection();
1829 xio_setoption_shallReturn = 0;
1830
1831 /// act
1832 result = HTTPAPI_SetOption(httpHandle, "InvalidOptionName", TEST_SETOPTIONS_CERTIFICATE);
1833
1834 /// assert
1835 ASSERT_ARE_EQUAL(int, HTTPAPI_INVALID_ARG, result);
1836 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1837 ASSERT_ARE_EQUAL(int, 3, currentmalloc_call);
1838
1839 /// cleanup
1840 HTTPAPI_CloseConnection(httpHandle); /* currentmalloc_call -= 2 */
1841 HTTPAPI_Deinit();
1842 }
1843
1844 /*Tests_SRS_HTTPAPI_COMPACT_21_061: [ If the value is NULL, the HTTPAPI_SetOption shall return HTTPAPI_INVALID_ARG. ]*/
1845 TEST_FUNCTION(HTTPAPI_ExecuteRequest__certificate_NULL_value_failed)
1846 {
1847 /// arrange
1848 HTTPAPI_RESULT result;
1849 HTTP_HANDLE httpHandle = createHttpConnection();
1850 xio_setoption_shallReturn = 0;
1851
1852 /// act
1853 result = HTTPAPI_SetOption(httpHandle, "TrustedCerts", NULL);
1854
1855 /// assert
1856 ASSERT_ARE_EQUAL(int, HTTPAPI_INVALID_ARG, result);
1857 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1858 ASSERT_ARE_EQUAL(int, 3, currentmalloc_call);
1859
1860 /// cleanup
1861 HTTPAPI_CloseConnection(httpHandle); /* currentmalloc_call -= 2 */
1862 HTTPAPI_Deinit();
1863 }
1864
1865 /*Tests_SRS_HTTPAPI_COMPACT_21_052: [ If any memory allocation get fail, the HTTPAPI_ExecuteRequest shall return HTTPAPI_ALLOC_FAILED. ]*/
1866 /*Tests_SRS_HTTPAPI_COMPACT_21_070: [ If any memory allocation get fail, the HTTPAPI_CloneOption shall return HTTPAPI_ALLOC_FAILED. ]*/
1867 TEST_FUNCTION(HTTPAPI_ExecuteRequest__clone_certificate_out_of_memory_failed)
1868 {
1869 /// arrange
1870 HTTPAPI_RESULT result;
1871 unsigned char* cloneCertificate;
1872 whenShallmalloc_fail = 1;
1873
1874 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG))
1875 .IgnoreArgument(1);
1876
1877 /// act
1878 result = HTTPAPI_CloneOption("TrustedCerts", TEST_SETOPTIONS_CERTIFICATE, (const void**)&cloneCertificate);
1879
1880 /// assert
1881 ASSERT_ARE_EQUAL(int, HTTPAPI_ALLOC_FAILED, result);
1882 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1883 ASSERT_ARE_EQUAL(int, 0, currentmalloc_call);
1884
1885 /// cleanup
1886 }
1887
1888 /*Tests_SRS_HTTPAPI_COMPACT_21_065: [ The HTTPAPI_CloneOption shall provide the means to clone the HTTP option. ]*/
1889 /*Tests_SRS_HTTPAPI_COMPACT_21_066: [ The HTTPAPI_CloneOption shall return a clone of the value identified by the optionName. ]*/
1890 /*Tests_SRS_HTTPAPI_COMPACT_21_072: [ If the HTTPAPI_CloneOption get success setting the option, it shall return HTTPAPI_OK. ]*/
1891 TEST_FUNCTION(HTTPAPI_ExecuteRequest__clone_certificate_succeed)
1892 {
1893 /// arrange
1894 HTTPAPI_RESULT result;
1895 unsigned char* cloneCertificate;
1896
1897 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG))
1898 .IgnoreArgument(1);
1899
1900 /// act
1901 result = HTTPAPI_CloneOption("TrustedCerts", TEST_SETOPTIONS_CERTIFICATE, (const void**)&cloneCertificate);
1902
1903 /// assert
1904 ASSERT_ARE_EQUAL(int, HTTPAPI_OK, result);
1905 ASSERT_ARE_EQUAL(char_ptr, (char*)TEST_SETOPTIONS_CERTIFICATE, (char*)cloneCertificate);
1906 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1907 ASSERT_ARE_EQUAL(int, 1, currentmalloc_call);
1908
1909 /// cleanup
1910 free((void*)cloneCertificate);
1911 }
1912
1913 /*Tests_SRS_HTTPAPI_COMPACT_21_067: [ If the optionName is NULL, the HTTPAPI_CloneOption shall return HTTPAPI_INVALID_ARG. ]*/
1914 TEST_FUNCTION(HTTPAPI_ExecuteRequest__clone_certificate_NULL_optionName_failed)
1915 {
1916 /// arrange
1917 HTTPAPI_RESULT result;
1918 unsigned char* cloneCertificate;
1919
1920 /// act
1921 result = HTTPAPI_CloneOption(NULL, TEST_SETOPTIONS_CERTIFICATE, (const void**)&cloneCertificate);
1922
1923 /// assert
1924 ASSERT_ARE_EQUAL(int, HTTPAPI_INVALID_ARG, result);
1925 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1926 ASSERT_ARE_EQUAL(int, 0, currentmalloc_call);
1927
1928 /// cleanup
1929 }
1930
1931 /*Tests_SRS_HTTPAPI_COMPACT_21_068: [ If the value is NULL, the HTTPAPI_CloneOption shall return HTTPAPI_INVALID_ARG. ]*/
1932 TEST_FUNCTION(HTTPAPI_ExecuteRequest__clone_certificate_NULL_value_failed)
1933 {
1934 /// arrange
1935 HTTPAPI_RESULT result;
1936 unsigned char* cloneCertificate;
1937
1938 /// act
1939 result = HTTPAPI_CloneOption("TrustedCerts", NULL, (const void**)&cloneCertificate);
1940
1941 /// assert
1942 ASSERT_ARE_EQUAL(int, HTTPAPI_INVALID_ARG, result);
1943 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1944 ASSERT_ARE_EQUAL(int, 0, currentmalloc_call);
1945
1946 /// cleanup
1947 }
1948
1949 /*Tests_SRS_HTTPAPI_COMPACT_21_069: [ If the savedValue is NULL, the HTTPAPI_CloneOption shall return HTTPAPI_INVALID_ARG. ]*/
1950 TEST_FUNCTION(HTTPAPI_ExecuteRequest__clone_certificate_NULL_savadValue_failed)
1951 {
1952 /// arrange
1953 HTTPAPI_RESULT result;
1954
1955 /// act
1956 result = HTTPAPI_CloneOption("TrustedCerts", TEST_SETOPTIONS_CERTIFICATE, NULL);
1957
1958 /// assert
1959 ASSERT_ARE_EQUAL(int, HTTPAPI_INVALID_ARG, result);
1960 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1961 ASSERT_ARE_EQUAL(int, 0, currentmalloc_call);
1962
1963 /// cleanup
1964 }
1965
1966 /*Tests_SRS_HTTPAPI_COMPACT_21_071: [ If the HTTP do not support the optionName, the HTTPAPI_CloneOption shall return HTTPAPI_INVALID_ARG. ]*/
1967 TEST_FUNCTION(HTTPAPI_ExecuteRequest__clone_certificate_invalid_optionName_failed)
1968 {
1969 /// arrange
1970 HTTPAPI_RESULT result;
1971 unsigned char* cloneCertificate;
1972
1973 /// act
1974 result = HTTPAPI_CloneOption("InvalidOptionName", TEST_SETOPTIONS_CERTIFICATE, (const void**)&cloneCertificate);
1975
1976 /// assert
1977 ASSERT_ARE_EQUAL(int, HTTPAPI_INVALID_ARG, result);
1978 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1979 ASSERT_ARE_EQUAL(int, 0, currentmalloc_call);
1980
1981 /// cleanup
1982 }
1983
1984 /*Tests_SRS_HTTPAPI_COMPACT_21_025: [ If the open process failed, the HTTPAPI_ExecuteRequest shall not send any request and return HTTPAPI_OPEN_REQUEST_FAILED. ]*/
1985 TEST_FUNCTION(HTTPAPI_ExecuteRequest__xoi_open_returns_LINE_failed)
1986 {
1987 /// arrange
1988 unsigned int statusCode;
1989 HTTPAPI_RESULT result;
1990 HTTP_HEADERS_HANDLE requestHttpHeaders;
1991 HTTP_HEADERS_HANDLE responseHttpHeaders;
1992 HTTP_HANDLE httpHandle = createHttpConnection();
1993 createHttpObjects(&requestHttpHeaders, &responseHttpHeaders);
1994 setHttpCertificate(httpHandle);
1995 xio_open_shallReturn = MU_FAILURE;
1996 setupAllCallBeforeOpenHTTPsequence(requestHttpHeaders, 0, false);
1997
1998 /// act
1999 result = HTTPAPI_ExecuteRequest(
2000 httpHandle,
2001 HTTPAPI_REQUEST_GET,
2002 TEST_EXECUTE_REQUEST_RELATIVE_PATH,
2003 requestHttpHeaders,
2004 TEST_EXECUTE_REQUEST_CONTENT,
2005 TEST_EXECUTE_REQUEST_CONTENT_LENGTH,
2006 &statusCode,
2007 responseHttpHeaders,
2008 TestBufferHandle);
2009
2010 /// assert
2011 ASSERT_ARE_EQUAL(int, HTTPAPI_OPEN_REQUEST_FAILED, result);
2012 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2013 ASSERT_ARE_EQUAL(int, 6, currentmalloc_call);
2014
2015 /// cleanup
2016 destroyHttpObjects(&requestHttpHeaders, &responseHttpHeaders); /* currentmalloc_call -= 2 */
2017 HTTPAPI_CloseConnection(httpHandle); /* currentmalloc_call -= 3 */
2018 HTTPAPI_Deinit();
2019 }
2020
2021 /*Tests_SRS_HTTPAPI_COMPACT_21_025: [ If the open process failed, the HTTPAPI_ExecuteRequest shall not send any request and return HTTPAPI_OPEN_REQUEST_FAILED. ]*/
2022 TEST_FUNCTION(HTTPAPI_ExecuteRequest__on_io_open_complete_with_error_on_openning_failed)
2023 {
2024 /// arrange
2025 HTTPAPI_RESULT result;
2026 HTTP_HEADERS_HANDLE requestHttpHeaders;
2027 HTTP_HEADERS_HANDLE responseHttpHeaders;
2028 HTTP_HANDLE httpHandle;
2029 unsigned int statusCode;
2030 createHttpObjects(&requestHttpHeaders, &responseHttpHeaders);
2031 httpHandle = createHttpConnection();
2032 setHttpCertificate(httpHandle);
2033
2034 DoworkJobs = (const xio_dowork_job*)doworkjob_oe;
2035 DoworkJobsOpenResult = (const IO_OPEN_RESULT*)openresult_error;
2036
2037 setupAllCallBeforeOpenHTTPsequence(requestHttpHeaders, 1, false);
2038
2039 /// act
2040 result = HTTPAPI_ExecuteRequest(
2041 httpHandle,
2042 HTTPAPI_REQUEST_GET,
2043 TEST_EXECUTE_REQUEST_RELATIVE_PATH,
2044 requestHttpHeaders,
2045 TEST_EXECUTE_REQUEST_CONTENT,
2046 TEST_EXECUTE_REQUEST_CONTENT_LENGTH,
2047 &statusCode,
2048 responseHttpHeaders,
2049 TestBufferHandle);
2050
2051 /// assert
2052 ASSERT_ARE_EQUAL(int, HTTPAPI_OPEN_REQUEST_FAILED, result);
2053 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2054 ASSERT_ARE_EQUAL(int, 6, currentmalloc_call);
2055
2056 /// cleanup
2057 destroyHttpObjects(&requestHttpHeaders, &responseHttpHeaders); /* currentmalloc_call -= 2 */
2058 HTTPAPI_CloseConnection(httpHandle); /* currentmalloc_call -= 3 */
2059 HTTPAPI_Deinit();
2060 }
2061
2062 /*Tests_SRS_HTTPAPI_COMPACT_21_025: [ If the open process failed, the HTTPAPI_ExecuteRequest shall not send any request and return HTTPAPI_OPEN_REQUEST_FAILED. ]*/
2063 TEST_FUNCTION(HTTPAPI_ExecuteRequest__on_io_open_complete_with_error_on_working_failed)
2064 {
2065 /// arrange
2066 unsigned int statusCode;
2067 HTTPAPI_RESULT result;
2068 HTTP_HEADERS_HANDLE requestHttpHeaders;
2069 HTTP_HEADERS_HANDLE responseHttpHeaders;
2070 HTTP_HANDLE httpHandle = createHttpConnection();
2071 createHttpObjects(&requestHttpHeaders, &responseHttpHeaders);
2072 setHttpCertificate(httpHandle);
2073
2074 DoworkJobs = (const xio_dowork_job*) doworkjob_4none_oe;
2075 DoworkJobsOpenResult = (const IO_OPEN_RESULT*)openresult_error;
2076
2077 setupAllCallBeforeOpenHTTPsequence(requestHttpHeaders, 5, false);
2078
2079 /// act
2080 result = HTTPAPI_ExecuteRequest(
2081 httpHandle,
2082 HTTPAPI_REQUEST_GET,
2083 TEST_EXECUTE_REQUEST_RELATIVE_PATH,
2084 requestHttpHeaders,
2085 TEST_EXECUTE_REQUEST_CONTENT,
2086 TEST_EXECUTE_REQUEST_CONTENT_LENGTH,
2087 &statusCode,
2088 responseHttpHeaders,
2089 TestBufferHandle);
2090
2091 /// assert
2092 ASSERT_ARE_EQUAL(int, HTTPAPI_OPEN_REQUEST_FAILED, result);
2093 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2094 ASSERT_ARE_EQUAL(int, 6, currentmalloc_call);
2095
2096 /// cleanup
2097 destroyHttpObjects(&requestHttpHeaders, &responseHttpHeaders); /* currentmalloc_call -= 2 */
2098 HTTPAPI_CloseConnection(httpHandle); /* currentmalloc_call -= 3 */
2099 HTTPAPI_Deinit();
2100 }
2101
2102 /*Tests_SRS_HTTPAPI_COMPACT_21_077: [ The HTTPAPI_ExecuteRequest shall wait, at least, 10 seconds for the SSL open process. ]*/
2103 /*Tests_SRS_HTTPAPI_COMPACT_21_078: [ If the HTTPAPI_ExecuteRequest cannot open the connection in 10 seconds, it shall fail and return HTTPAPI_OPEN_REQUEST_FAILED. ]*/
2104 TEST_FUNCTION(HTTPAPI_ExecuteRequest__on_io_open_complete_with_error_after_n_retry_failed)
2105 {
2106 /// arrange
2107 unsigned int statusCode;
2108 HTTPAPI_RESULT result;
2109 HTTP_HEADERS_HANDLE requestHttpHeaders;
2110 HTTP_HEADERS_HANDLE responseHttpHeaders;
2111 HTTP_HANDLE httpHandle = createHttpConnection();
2112 createHttpObjects(&requestHttpHeaders, &responseHttpHeaders);
2113 setHttpCertificate(httpHandle);
2114
2115 DoworkJobs = (const xio_dowork_job*)doworkjob_4none_oe;
2116 DoworkJobsOpenResult = (const IO_OPEN_RESULT*)openresult_error;
2117
2118 SkipDoworkJobsOpenResult = 5;
2119 setupAllCallBeforeOpenHTTPsequence(requestHttpHeaders, SkipDoworkJobsOpenResult+5, false);
2120
2121 /// act
2122 result = HTTPAPI_ExecuteRequest(
2123 httpHandle,
2124 HTTPAPI_REQUEST_GET,
2125 TEST_EXECUTE_REQUEST_RELATIVE_PATH,
2126 requestHttpHeaders,
2127 TEST_EXECUTE_REQUEST_CONTENT,
2128 TEST_EXECUTE_REQUEST_CONTENT_LENGTH,
2129 &statusCode,
2130 responseHttpHeaders,
2131 TestBufferHandle);
2132
2133 /// assert
2134 ASSERT_ARE_EQUAL(int, HTTPAPI_OPEN_REQUEST_FAILED, result);
2135 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2136 ASSERT_ARE_EQUAL(int, 6, currentmalloc_call);
2137
2138 /// cleanup
2139 destroyHttpObjects(&requestHttpHeaders, &responseHttpHeaders); /* currentmalloc_call -= 2 */
2140 HTTPAPI_CloseConnection(httpHandle); /* currentmalloc_call -= 3 */
2141 HTTPAPI_Deinit();
2142 }
2143
2144 /*Tests_SRS_HTTPAPI_COMPACT_21_077: [ The HTTPAPI_ExecuteRequest shall wait, at least, 10 seconds for the SSL open process. ]*/
2145 /*Tests_SRS_HTTPAPI_COMPACT_21_078: [ If the HTTPAPI_ExecuteRequest cannot open the connection in 10 seconds, it shall fail and return HTTPAPI_OPEN_REQUEST_FAILED. ]*/
2146 TEST_FUNCTION(HTTPAPI_ExecuteRequest__on_io_open_complete_with_timeout_failed)
2147 {
2148 /// arrange
2149 unsigned int statusCode;
2150 HTTPAPI_RESULT result;
2151 HTTP_HEADERS_HANDLE requestHttpHeaders;
2152 HTTP_HEADERS_HANDLE responseHttpHeaders;
2153 HTTP_HANDLE httpHandle = createHttpConnection();
2154 createHttpObjects(&requestHttpHeaders, &responseHttpHeaders);
2155 setHttpCertificate(httpHandle);
2156
2157 DoworkJobs = (const xio_dowork_job*)doworkjob_4none_oe;
2158 DoworkJobsOpenResult = (const IO_OPEN_RESULT*)openresult_ok;
2159
2160 SkipDoworkJobsOpenResult = 98;
2161 setupAllCallBeforeOpenHTTPsequence(requestHttpHeaders, SkipDoworkJobsOpenResult + 4, false);
2162 STRICT_EXPECTED_CALL(xio_dowork(IGNORED_NUM_ARG))
2163 .IgnoreArgument(1);
2164
2165 /// act
2166 result = HTTPAPI_ExecuteRequest(
2167 httpHandle,
2168 HTTPAPI_REQUEST_GET,
2169 TEST_EXECUTE_REQUEST_RELATIVE_PATH,
2170 requestHttpHeaders,
2171 TEST_EXECUTE_REQUEST_CONTENT,
2172 TEST_EXECUTE_REQUEST_CONTENT_LENGTH,
2173 &statusCode,
2174 responseHttpHeaders,
2175 TestBufferHandle);
2176
2177 /// assert
2178 ASSERT_ARE_EQUAL(int, HTTPAPI_OPEN_REQUEST_FAILED, result);
2179 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2180 ASSERT_ARE_EQUAL(int, 6, currentmalloc_call);
2181
2182 /// cleanup
2183 destroyHttpObjects(&requestHttpHeaders, &responseHttpHeaders); /* currentmalloc_call -= 2 */
2184 HTTPAPI_CloseConnection(httpHandle); /* currentmalloc_call -= 3 */
2185 HTTPAPI_Deinit();
2186 }
2187
2188 /*Tests_SRS_HTTPAPI_COMPACT_21_025: [ If the open process failed, the HTTPAPI_ExecuteRequest shall not send any request and return HTTPAPI_OPEN_REQUEST_FAILED. ]*/
2189 TEST_FUNCTION(HTTPAPI_ExecuteRequest__on_io_error_on_openning_failed)
2190 {
2191 /// arrange
2192 unsigned int statusCode;
2193 HTTPAPI_RESULT result;
2194 HTTP_HEADERS_HANDLE requestHttpHeaders;
2195 HTTP_HEADERS_HANDLE responseHttpHeaders;
2196 HTTP_HANDLE httpHandle = createHttpConnection();
2197 createHttpObjects(&requestHttpHeaders, &responseHttpHeaders);
2198 setHttpCertificate(httpHandle);
2199
2200 DoworkJobs = (const xio_dowork_job*)doworkjob_ee;
2201
2202 setupAllCallBeforeOpenHTTPsequence(requestHttpHeaders, 1, false);
2203
2204 /// act
2205 result = HTTPAPI_ExecuteRequest(
2206 httpHandle,
2207 HTTPAPI_REQUEST_GET,
2208 TEST_EXECUTE_REQUEST_RELATIVE_PATH,
2209 requestHttpHeaders,
2210 TEST_EXECUTE_REQUEST_CONTENT,
2211 TEST_EXECUTE_REQUEST_CONTENT_LENGTH,
2212 &statusCode,
2213 responseHttpHeaders,
2214 TestBufferHandle);
2215
2216 /// assert
2217 ASSERT_ARE_EQUAL(int, HTTPAPI_OPEN_REQUEST_FAILED, result);
2218 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2219 ASSERT_ARE_EQUAL(int, 6, currentmalloc_call);
2220
2221 /// cleanup
2222 destroyHttpObjects(&requestHttpHeaders, &responseHttpHeaders); /* currentmalloc_call -= 2 */
2223 HTTPAPI_CloseConnection(httpHandle); /* currentmalloc_call -= 3 */
2224 HTTPAPI_Deinit();
2225 }
2226
2227 /*Tests_SRS_HTTPAPI_COMPACT_21_025: [ If the open process failed, the HTTPAPI_ExecuteRequest shall not send any request and return HTTPAPI_OPEN_REQUEST_FAILED. ]*/
2228 TEST_FUNCTION(HTTPAPI_ExecuteRequest__on_io_error_on_working_failed)
2229 {
2230 /// arrange
2231 unsigned int statusCode;
2232 HTTPAPI_RESULT result;
2233 HTTP_HEADERS_HANDLE requestHttpHeaders;
2234 HTTP_HEADERS_HANDLE responseHttpHeaders;
2235 HTTP_HANDLE httpHandle = createHttpConnection();
2236 createHttpObjects(&requestHttpHeaders, &responseHttpHeaders);
2237 setHttpCertificate(httpHandle);
2238
2239 DoworkJobs = (const xio_dowork_job*)doworkjob_4none_ee;
2240
2241 setupAllCallBeforeOpenHTTPsequence(requestHttpHeaders, 5, false);
2242
2243 /// act
2244 result = HTTPAPI_ExecuteRequest(
2245 httpHandle,
2246 HTTPAPI_REQUEST_GET,
2247 TEST_EXECUTE_REQUEST_RELATIVE_PATH,
2248 requestHttpHeaders,
2249 TEST_EXECUTE_REQUEST_CONTENT,
2250 TEST_EXECUTE_REQUEST_CONTENT_LENGTH,
2251 &statusCode,
2252 responseHttpHeaders,
2253 TestBufferHandle);
2254
2255 /// assert
2256 ASSERT_ARE_EQUAL(int, HTTPAPI_OPEN_REQUEST_FAILED, result);
2257 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2258 ASSERT_ARE_EQUAL(int, 6, currentmalloc_call);
2259
2260 /// cleanup
2261 destroyHttpObjects(&requestHttpHeaders, &responseHttpHeaders); /* currentmalloc_call -= 2 */
2262 HTTPAPI_CloseConnection(httpHandle); /* currentmalloc_call -= 3 */
2263 HTTPAPI_Deinit();
2264 }
2265
2266 /*Tests_SRS_HTTPAPI_COMPACT_21_027: [ If the HTTPAPI_ExecuteRequest cannot create a buffer to send the request, it shall not send any request and return HTTPAPI_STRING_PROCESSING_ERROR. ]*/
2267 TEST_FUNCTION(HTTPAPI_ExecuteRequest__huge_relative_path_failed)
2268 {
2269 /// arrange
2270 unsigned int statusCode;
2271 HTTPAPI_RESULT result;
2272 HTTP_HEADERS_HANDLE requestHttpHeaders;
2273 HTTP_HEADERS_HANDLE responseHttpHeaders;
2274 HTTP_HANDLE httpHandle = createHttpConnection();
2275 size_t i;
2276 char hugeRelativePath[HUGE_RELATIVE_PATH_SIZE];
2277 createHttpObjects(&requestHttpHeaders, &responseHttpHeaders);
2278
2279 DoworkJobs = (const xio_dowork_job*)doworkjob_oe;
2280 DoworkJobsOpenResult = (const IO_OPEN_RESULT*)openresult_ok;
2281
2282 setHttpCertificate(httpHandle);
2283 setupAllCallBeforeOpenHTTPsequence(requestHttpHeaders, 1, false);
2284
2285 for (i = 0; i < HUGE_RELATIVE_PATH_SIZE; i++)
2286 {
2287 hugeRelativePath[i] = 'a';
2288 }
2289 hugeRelativePath[HUGE_RELATIVE_PATH_SIZE - 1] = '\0';
2290
2291 /// act
2292 result = HTTPAPI_ExecuteRequest(
2293 httpHandle,
2294 HTTPAPI_REQUEST_GET,
2295 hugeRelativePath,
2296 requestHttpHeaders,
2297 TEST_EXECUTE_REQUEST_CONTENT,
2298 TEST_EXECUTE_REQUEST_CONTENT_LENGTH,
2299 &statusCode,
2300 responseHttpHeaders,
2301 TestBufferHandle);
2302
2303 /// assert
2304 ASSERT_ARE_EQUAL(int, HTTPAPI_STRING_PROCESSING_ERROR, result);
2305 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2306 ASSERT_ARE_EQUAL(int, 6, currentmalloc_call);
2307
2308 /// cleanup
2309 destroyHttpObjects(&requestHttpHeaders, &responseHttpHeaders); /* currentmalloc_call -= 2 */
2310 HTTPAPI_CloseConnection(httpHandle); /* currentmalloc_call -= 3 */
2311 HTTPAPI_Deinit();
2312 }
2313
2314 /*Tests_SRS_HTTPAPI_COMPACT_21_026: [ If the open process succeed, the HTTPAPI_ExecuteRequest shall send the request message to the host. ]*/
2315 /*Tests_SRS_HTTPAPI_COMPACT_21_024: [ The HTTPAPI_ExecuteRequest shall open the transport connection with the host to send the request. ]*/
2316 /*Tests_SRS_HTTPAPI_COMPACT_21_022: [ If a Certificate was provided, the HTTPAPI_ExecuteRequest shall set this option on the transport layer. ]*/
2317 /*Tests_SRS_HTTPAPI_COMPACT_06_003: [ If the x509 client certificate is provided, the HTTPAPI_ExecuteRequest shall set this option on the transport layer. ]*/
2318 /*Tests_SRS_HTTPAPI_COMPACT_06_004: [ If the x509 client certificate private key is provided, the HTTPAPI_ExecuteRequest shall set this optionon the transport layer. ]*/
2319 /*Tests_SRS_HTTPAPI_COMPACT_21_028: [ If the HTTPAPI_ExecuteRequest cannot send the request header, it shall return HTTPAPI_SEND_REQUEST_FAILED. ]*/
2320 TEST_FUNCTION(HTTPAPI_ExecuteRequest__io_send_header_return_error_failed)
2321 {
2322 /// arrange
2323 unsigned int statusCode;
2324 HTTPAPI_RESULT result;
2325 HTTP_HEADERS_HANDLE requestHttpHeaders;
2326 HTTP_HEADERS_HANDLE responseHttpHeaders;
2327 HTTP_HANDLE httpHandle = createHttpConnection();
2328 createHttpObjects(&requestHttpHeaders, &responseHttpHeaders);
2329
2330 DoworkJobs = (const xio_dowork_job*)doworkjob_oe;
2331 DoworkJobsOpenResult = (const IO_OPEN_RESULT*)openresult_ok;
2332
2333 setHttpCertificate(httpHandle);
2334 setHttpx509ClientCertificateAndKey(httpHandle);
2335 setupAllCallBeforeOpenHTTPsequence(requestHttpHeaders, 1, true);
2336 xio_send_shallReturn = (const int*)xio_send_e;
2337 STRICT_EXPECTED_CALL(xio_send(IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_NUM_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
2338 .IgnoreAllArguments();
2339
2340 /// act
2341 result = HTTPAPI_ExecuteRequest(
2342 httpHandle,
2343 HTTPAPI_REQUEST_GET,
2344 TEST_EXECUTE_REQUEST_RELATIVE_PATH,
2345 requestHttpHeaders,
2346 TEST_EXECUTE_REQUEST_CONTENT,
2347 TEST_EXECUTE_REQUEST_CONTENT_LENGTH,
2348 &statusCode,
2349 responseHttpHeaders,
2350 TestBufferHandle);
2351
2352 /// assert
2353 ASSERT_ARE_EQUAL(int, HTTPAPI_SEND_REQUEST_FAILED, result);
2354 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2355 ASSERT_ARE_EQUAL(int, 8, currentmalloc_call);
2356
2357 /// cleanup
2358 destroyHttpObjects(&requestHttpHeaders, &responseHttpHeaders); /* currentmalloc_call -= 2 */
2359 HTTPAPI_CloseConnection(httpHandle); /* currentmalloc_call -= 3 */
2360 HTTPAPI_Deinit();
2361 }
2362
2363 /*Tests_SRS_HTTPAPI_COMPACT_21_028: [ If the HTTPAPI_ExecuteRequest cannot send the request header, it shall return HTTPAPI_SEND_REQUEST_FAILED. ]*/
2364 TEST_FUNCTION(HTTPAPI_ExecuteRequest__on_send_header_complete_with_success_before_error_failed)
2365 {
2366 /// arrange
2367 unsigned int statusCode;
2368 HTTPAPI_RESULT result;
2369 HTTP_HEADERS_HANDLE requestHttpHeaders;
2370 HTTP_HEADERS_HANDLE responseHttpHeaders;
2371 HTTP_HANDLE httpHandle = createHttpConnection();
2372 createHttpObjects(&requestHttpHeaders, &responseHttpHeaders);
2373 setHttpCertificate(httpHandle);
2374
2375 DoworkJobs = (const xio_dowork_job*)doworkjob_oe;
2376 DoworkJobsOpenResult = (const IO_OPEN_RESULT*)openresult_ok;
2377 DoworkJobsSendResult = (const IO_SEND_RESULT*)sendresult_o_3error;
2378 xio_send_shallReturn = (const int*)xio_send_0_e;
2379
2380 setupAllCallBeforeOpenHTTPsequence(requestHttpHeaders, 1, false);
2381 STRICT_EXPECTED_CALL(xio_send(IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_NUM_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
2382 .IgnoreAllArguments();
2383 STRICT_EXPECTED_CALL(HTTPHeaders_GetHeader(requestHttpHeaders, IGNORED_NUM_ARG, IGNORED_PTR_ARG))
2384 .IgnoreArgument(2).IgnoreArgument(3);
2385 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG)).IgnoreArgument(1);
2386 STRICT_EXPECTED_CALL(xio_send(IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_NUM_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
2387 .IgnoreAllArguments();
2388 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG))
2389 .IgnoreArgument(1);
2390
2391 HTTPHeaders_GetHeader_shallReturn = HTTP_HEADERS_OK;
2392
2393 /// act
2394 result = HTTPAPI_ExecuteRequest(
2395 httpHandle,
2396 HTTPAPI_REQUEST_GET,
2397 TEST_EXECUTE_REQUEST_RELATIVE_PATH,
2398 requestHttpHeaders,
2399 TEST_EXECUTE_REQUEST_CONTENT,
2400 TEST_EXECUTE_REQUEST_CONTENT_LENGTH,
2401 &statusCode,
2402 responseHttpHeaders,
2403 TestBufferHandle);
2404
2405 /// assert
2406 ASSERT_ARE_EQUAL(int, HTTPAPI_SEND_REQUEST_FAILED, result);
2407 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2408 ASSERT_ARE_EQUAL(int, 6, currentmalloc_call);
2409
2410 /// cleanup
2411 destroyHttpObjects(&requestHttpHeaders, &responseHttpHeaders); /* currentmalloc_call -= 2 */
2412 HTTPAPI_CloseConnection(httpHandle); /* currentmalloc_call -= 3 */
2413 HTTPAPI_Deinit();
2414 }
2415
2416 /*Tests_SRS_HTTPAPI_COMPACT_21_028: [ If the HTTPAPI_ExecuteRequest cannot send the request header, it shall return HTTPAPI_SEND_REQUEST_FAILED. ]*/
2417 TEST_FUNCTION(HTTPAPI_ExecuteRequest__on_send_header_complete_with_2_success_before_error_failed)
2418 {
2419 /// arrange
2420 unsigned int statusCode;
2421 HTTPAPI_RESULT result;
2422 HTTP_HEADERS_HANDLE requestHttpHeaders;
2423 HTTP_HEADERS_HANDLE responseHttpHeaders;
2424 HTTP_HANDLE httpHandle = createHttpConnection();
2425 createHttpObjects(&requestHttpHeaders, &responseHttpHeaders);
2426 setHttpCertificate(httpHandle);
2427
2428 DoworkJobs = (const xio_dowork_job*)doworkjob_oe;
2429 DoworkJobsOpenResult = (const IO_OPEN_RESULT*)openresult_ok;
2430 DoworkJobsSendResult = (const IO_SEND_RESULT*)sendresult_o_3error;
2431 xio_send_shallReturn = (const int*)xio_send_00_e;
2432
2433 setupAllCallBeforeOpenHTTPsequence(requestHttpHeaders, 1, false);
2434 STRICT_EXPECTED_CALL(xio_send(IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_NUM_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
2435 .IgnoreAllArguments();
2436 STRICT_EXPECTED_CALL(HTTPHeaders_GetHeader(requestHttpHeaders, IGNORED_NUM_ARG, IGNORED_PTR_ARG))
2437 .IgnoreArgument(2).IgnoreArgument(3);
2438 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG)).IgnoreArgument(1);
2439 STRICT_EXPECTED_CALL(xio_send(IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_NUM_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
2440 .IgnoreAllArguments();
2441 STRICT_EXPECTED_CALL(xio_send(IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_NUM_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
2442 .IgnoreAllArguments();
2443 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG))
2444 .IgnoreArgument(1);
2445
2446 HTTPHeaders_GetHeader_shallReturn = HTTP_HEADERS_OK;
2447
2448 /// act
2449 result = HTTPAPI_ExecuteRequest(
2450 httpHandle,
2451 HTTPAPI_REQUEST_GET,
2452 TEST_EXECUTE_REQUEST_RELATIVE_PATH,
2453 requestHttpHeaders,
2454 TEST_EXECUTE_REQUEST_CONTENT,
2455 TEST_EXECUTE_REQUEST_CONTENT_LENGTH,
2456 &statusCode,
2457 responseHttpHeaders,
2458 TestBufferHandle);
2459
2460 /// assert
2461 ASSERT_ARE_EQUAL(int, HTTPAPI_SEND_REQUEST_FAILED, result);
2462 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2463 ASSERT_ARE_EQUAL(int, 6, currentmalloc_call);
2464
2465 /// cleanup
2466 destroyHttpObjects(&requestHttpHeaders, &responseHttpHeaders); /* currentmalloc_call -= 2 */
2467 HTTPAPI_CloseConnection(httpHandle); /* currentmalloc_call -= 3 */
2468 HTTPAPI_Deinit();
2469 }
2470
2471 /*Tests_SRS_HTTPAPI_COMPACT_21_079: [ The HTTPAPI_ExecuteRequest shall wait, at least, 20 seconds to send a buffer using the SSL connection. ]*/
2472 /*Tests_SRS_HTTPAPI_COMPACT_21_080: [ If the HTTPAPI_ExecuteRequest retries to send the message for 20 seconds without success, it shall fail and return HTTPAPI_SEND_REQUEST_FAILED. ]*/
2473 TEST_FUNCTION(HTTPAPI_ExecuteRequest__on_send_header_complete_timeout_failed)
2474 {
2475 /// arrange
2476 int i;
2477 unsigned int statusCode;
2478 HTTPAPI_RESULT result;
2479 HTTP_HEADERS_HANDLE requestHttpHeaders;
2480 HTTP_HEADERS_HANDLE responseHttpHeaders;
2481 HTTP_HANDLE httpHandle = createHttpConnection();
2482 createHttpObjects(&requestHttpHeaders, &responseHttpHeaders);
2483 setHttpCertificate(httpHandle);
2484
2485 DoworkJobs = (const xio_dowork_job*)doworkjob_ose;
2486 DoworkJobsOpenResult = (const IO_OPEN_RESULT*)openresult_ok;
2487 DoworkJobsSendResult = (const IO_SEND_RESULT*)sendresult_o_3error;
2488 xio_send_shallReturn = (const int*)xio_send_00_e;
2489 call_on_send_complete_in_xio_send = false;
2490
2491 setupAllCallBeforeOpenHTTPsequence(requestHttpHeaders, 1, false);
2492 STRICT_EXPECTED_CALL(xio_send(IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_NUM_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
2493 .IgnoreAllArguments();
2494 SkipDoworkJobsSendResult = 200;
2495 for (i = 0; i < SkipDoworkJobsSendResult; i++)
2496 {
2497 STRICT_EXPECTED_CALL(xio_dowork(IGNORED_NUM_ARG))
2498 .IgnoreArgument(1);
2499 STRICT_EXPECTED_CALL(ThreadAPI_Sleep(100));
2500 }
2501 STRICT_EXPECTED_CALL(xio_dowork(IGNORED_NUM_ARG))
2502 .IgnoreArgument(1);
2503
2504 HTTPHeaders_GetHeader_shallReturn = HTTP_HEADERS_OK;
2505
2506 /// act
2507 result = HTTPAPI_ExecuteRequest(
2508 httpHandle,
2509 HTTPAPI_REQUEST_GET,
2510 TEST_EXECUTE_REQUEST_RELATIVE_PATH,
2511 requestHttpHeaders,
2512 TEST_EXECUTE_REQUEST_CONTENT,
2513 TEST_EXECUTE_REQUEST_CONTENT_LENGTH,
2514 &statusCode,
2515 responseHttpHeaders,
2516 TestBufferHandle);
2517
2518 /// assert
2519 ASSERT_ARE_EQUAL(int, HTTPAPI_SEND_REQUEST_FAILED, result);
2520 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2521 ASSERT_ARE_EQUAL(int, 6, currentmalloc_call);
2522
2523 /// cleanup
2524 destroyHttpObjects(&requestHttpHeaders, &responseHttpHeaders); /* currentmalloc_call -= 2 */
2525 HTTPAPI_CloseConnection(httpHandle); /* currentmalloc_call -= 3 */
2526 HTTPAPI_Deinit();
2527 }
2528
2529 /*Tests_SRS_HTTPAPI_COMPACT_21_079: [ The HTTPAPI_ExecuteRequest shall wait, at least, 20 seconds to send a buffer using the SSL connection. ]*/
2530 /*Tests_SRS_HTTPAPI_COMPACT_21_080: [ If the HTTPAPI_ExecuteRequest retries to send the message for 20 seconds without success, it shall fail and return HTTPAPI_SEND_REQUEST_FAILED. ]*/
2531 TEST_FUNCTION(HTTPAPI_ExecuteRequest__on_send_header_complete_retry_n_and_failed)
2532 {
2533 /// arrange
2534 int i;
2535 unsigned int statusCode;
2536 HTTPAPI_RESULT result;
2537 HTTP_HEADERS_HANDLE requestHttpHeaders;
2538 HTTP_HEADERS_HANDLE responseHttpHeaders;
2539 HTTP_HANDLE httpHandle = createHttpConnection();
2540 createHttpObjects(&requestHttpHeaders, &responseHttpHeaders);
2541 setHttpCertificate(httpHandle);
2542
2543 DoworkJobs = (const xio_dowork_job*)doworkjob_ose;
2544 DoworkJobsOpenResult = (const IO_OPEN_RESULT*)openresult_ok;
2545 DoworkJobsSendResult = (const IO_SEND_RESULT*)sendresult_error;
2546 xio_send_shallReturn = (const int*)xio_send_00_e;
2547 call_on_send_complete_in_xio_send = false;
2548
2549 setupAllCallBeforeOpenHTTPsequence(requestHttpHeaders, 1, false);
2550 STRICT_EXPECTED_CALL(xio_send(IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_NUM_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
2551 .IgnoreAllArguments();
2552 SkipDoworkJobsSendResult = 10;
2553 for (i = 0; i < SkipDoworkJobsSendResult; i++)
2554 {
2555 STRICT_EXPECTED_CALL(xio_dowork(IGNORED_NUM_ARG))
2556 .IgnoreArgument(1);
2557 STRICT_EXPECTED_CALL(ThreadAPI_Sleep(100));
2558 }
2559 STRICT_EXPECTED_CALL(xio_dowork(IGNORED_NUM_ARG))
2560 .IgnoreArgument(1);
2561
2562 HTTPHeaders_GetHeader_shallReturn = HTTP_HEADERS_OK;
2563
2564 /// act
2565 result = HTTPAPI_ExecuteRequest(
2566 httpHandle,
2567 HTTPAPI_REQUEST_GET,
2568 TEST_EXECUTE_REQUEST_RELATIVE_PATH,
2569 requestHttpHeaders,
2570 TEST_EXECUTE_REQUEST_CONTENT,
2571 TEST_EXECUTE_REQUEST_CONTENT_LENGTH,
2572 &statusCode,
2573 responseHttpHeaders,
2574 TestBufferHandle);
2575
2576 /// assert
2577 ASSERT_ARE_EQUAL(int, HTTPAPI_SEND_REQUEST_FAILED, result);
2578 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2579 ASSERT_ARE_EQUAL(int, 6, currentmalloc_call);
2580
2581 /// cleanup
2582 destroyHttpObjects(&requestHttpHeaders, &responseHttpHeaders); /* currentmalloc_call -= 2 */
2583 HTTPAPI_CloseConnection(httpHandle); /* currentmalloc_call -= 3 */
2584 HTTPAPI_Deinit();
2585 }
2586
2587 /*Tests_SRS_HTTPAPI_COMPACT_21_029: [ If the HTTPAPI_ExecuteRequest cannot send the buffer with the request, it shall return HTTPAPI_SEND_REQUEST_FAILED. ]*/
2588 TEST_FUNCTION(HTTPAPI_ExecuteRequest__on_send_buffer_complete_with_error_failed)
2589 {
2590 /// arrange
2591 unsigned int statusCode;
2592 HTTPAPI_RESULT result;
2593 HTTP_HEADERS_HANDLE requestHttpHeaders;
2594 HTTP_HEADERS_HANDLE responseHttpHeaders;
2595 HTTP_HANDLE httpHandle = createHttpConnection();
2596 createHttpObjects(&requestHttpHeaders, &responseHttpHeaders);
2597 setHttpCertificate(httpHandle);
2598
2599 DoworkJobs = (const xio_dowork_job*)doworkjob_oe;
2600 DoworkJobsOpenResult = (const IO_OPEN_RESULT*)openresult_ok;
2601 DoworkJobsSendResult = (const IO_SEND_RESULT*)sendresult_6ok_error;
2602 xio_send_shallReturn = (const int*)xio_send_6x0_e;
2603
2604 setupAllCallBeforeOpenHTTPsequence(requestHttpHeaders, 1, false);
2605
2606 STRICT_EXPECTED_CALL(xio_send(IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_NUM_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
2607 .IgnoreAllArguments();
2608 STRICT_EXPECTED_CALL(HTTPHeaders_GetHeader(requestHttpHeaders, IGNORED_NUM_ARG, IGNORED_PTR_ARG))
2609 .IgnoreArgument(2).IgnoreArgument(3);
2610 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG)).IgnoreArgument(1);
2611 STRICT_EXPECTED_CALL(xio_send(IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_NUM_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
2612 .IgnoreAllArguments();
2613 STRICT_EXPECTED_CALL(xio_send(IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_NUM_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
2614 .IgnoreAllArguments();
2615 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG))
2616 .IgnoreArgument(1);
2617 STRICT_EXPECTED_CALL(HTTPHeaders_GetHeader(requestHttpHeaders, IGNORED_NUM_ARG, IGNORED_PTR_ARG))
2618 .IgnoreArgument(2).IgnoreArgument(3);
2619 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG)).IgnoreArgument(1);
2620 STRICT_EXPECTED_CALL(xio_send(IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_NUM_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
2621 .IgnoreAllArguments();
2622 STRICT_EXPECTED_CALL(xio_send(IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_NUM_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
2623 .IgnoreAllArguments();
2624 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG))
2625 .IgnoreArgument(1);
2626 STRICT_EXPECTED_CALL(xio_send(IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_NUM_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
2627 .IgnoreAllArguments();
2628
2629 STRICT_EXPECTED_CALL(xio_send(IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_NUM_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
2630 .IgnoreAllArguments();
2631
2632 HTTPHeaders_GetHeader_shallReturn = HTTP_HEADERS_OK;
2633
2634 /// act
2635 result = HTTPAPI_ExecuteRequest(
2636 httpHandle,
2637 HTTPAPI_REQUEST_GET,
2638 TEST_EXECUTE_REQUEST_RELATIVE_PATH,
2639 requestHttpHeaders,
2640 TEST_EXECUTE_REQUEST_CONTENT,
2641 TEST_EXECUTE_REQUEST_CONTENT_LENGTH,
2642 &statusCode,
2643 responseHttpHeaders,
2644 TestBufferHandle);
2645
2646 /// assert
2647 ASSERT_ARE_EQUAL(int, HTTPAPI_SEND_REQUEST_FAILED, result);
2648 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2649 ASSERT_ARE_EQUAL(int, 6, currentmalloc_call);
2650
2651 /// cleanup
2652 destroyHttpObjects(&requestHttpHeaders, &responseHttpHeaders); /* currentmalloc_call -= 2 */
2653 HTTPAPI_CloseConnection(httpHandle); /* currentmalloc_call -= 3 */
2654 HTTPAPI_Deinit();
2655 }
2656
2657 /*Tests_SRS_HTTPAPI_COMPACT_21_030: [ At the end of the transmission, the HTTPAPI_ExecuteRequest shall receive the response from the host. ]*/
2658 /*Tests_SRS_HTTPAPI_COMPACT_21_032: [ If the HTTPAPI_ExecuteRequest cannot read the message with the request result, it shall return HTTPAPI_READ_DATA_FAILED. ]*/
2659 TEST_FUNCTION(HTTPAPI_ExecuteRequest__on_read_header_failed_failed)
2660 {
2661 /// arrange
2662 unsigned int statusCode;
2663 HTTPAPI_RESULT result;
2664 HTTP_HEADERS_HANDLE requestHttpHeaders;
2665 HTTP_HEADERS_HANDLE responseHttpHeaders;
2666 HTTP_HANDLE httpHandle = createHttpConnection();
2667 createHttpObjects(&requestHttpHeaders, &responseHttpHeaders);
2668 setHttpCertificate(httpHandle);
2669
2670 DoworkJobs = (const xio_dowork_job*)doworkjob_oee;
2671 DoworkJobsOpenResult = (const IO_OPEN_RESULT*)openresult_ok;
2672 DoworkJobsSendResult = (const IO_SEND_RESULT*)sendresult_7ok;
2673
2674 setupAllCallBeforeOpenHTTPsequence(requestHttpHeaders, 1, false);
2675 setupAllCallBeforeSendHTTPsequenceWithSuccess(requestHttpHeaders);
2676
2677 STRICT_EXPECTED_CALL(xio_dowork(IGNORED_NUM_ARG))
2678 .IgnoreArgument(1);
2679
2680 HTTPHeaders_GetHeader_shallReturn = HTTP_HEADERS_OK;
2681
2682 /// act
2683 result = HTTPAPI_ExecuteRequest(
2684 httpHandle,
2685 HTTPAPI_REQUEST_GET,
2686 TEST_EXECUTE_REQUEST_RELATIVE_PATH,
2687 requestHttpHeaders,
2688 TEST_EXECUTE_REQUEST_CONTENT,
2689 TEST_EXECUTE_REQUEST_CONTENT_LENGTH,
2690 &statusCode,
2691 responseHttpHeaders,
2692 TestBufferHandle);
2693
2694 /// assert
2695 ASSERT_ARE_EQUAL(int, HTTPAPI_READ_DATA_FAILED, result);
2696 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2697 ASSERT_ARE_EQUAL(int, 6, currentmalloc_call);
2698
2699 /// cleanup
2700 destroyHttpObjects(&requestHttpHeaders, &responseHttpHeaders); /* currentmalloc_call -= 2 */
2701 HTTPAPI_CloseConnection(httpHandle); /* currentmalloc_call -= 3 */
2702 HTTPAPI_Deinit();
2703 }
2704
2705 /*Tests_SRS_HTTPAPI_COMPACT_21_032: [ If the HTTPAPI_ExecuteRequest cannot read the message with the request result, it shall return HTTPAPI_READ_DATA_FAILED. ]*/
2706 TEST_FUNCTION(HTTPAPI_ExecuteRequest__on_read_NULL_header_failed)
2707 {
2708 /// arrange
2709 unsigned int statusCode;
2710 HTTPAPI_RESULT result;
2711 HTTP_HEADERS_HANDLE requestHttpHeaders;
2712 HTTP_HEADERS_HANDLE responseHttpHeaders;
2713 HTTP_HANDLE httpHandle = createHttpConnection();
2714 createHttpObjects(&requestHttpHeaders, &responseHttpHeaders);
2715 setHttpCertificate(httpHandle);
2716
2717 DoworkJobsReceivedBuffer = NULL;
2718 DoworkJobsReceivedBuffer_size[0] = 10;
2719 DoworkJobsReceivedBuffer_counter = 0;
2720 DoworkJobs = (const xio_dowork_job*)doworkjob_o_re;
2721 DoworkJobsOpenResult = DoworkJobsOpenResult_ReceiveHead;
2722 DoworkJobsSendResult = DoworkJobsSendResult_ReceiveHead;
2723
2724 setupAllCallBeforeOpenHTTPsequence(requestHttpHeaders, 1, false);
2725 setupAllCallBeforeSendHTTPsequenceWithSuccess(requestHttpHeaders);
2726
2727 STRICT_EXPECTED_CALL(xio_dowork(IGNORED_NUM_ARG))
2728 .IgnoreArgument(1);
2729
2730 HTTPHeaders_GetHeader_shallReturn = HTTP_HEADERS_OK;
2731
2732 /// act
2733 result = HTTPAPI_ExecuteRequest(
2734 httpHandle,
2735 HTTPAPI_REQUEST_GET,
2736 TEST_EXECUTE_REQUEST_RELATIVE_PATH,
2737 requestHttpHeaders,
2738 TEST_EXECUTE_REQUEST_CONTENT,
2739 TEST_EXECUTE_REQUEST_CONTENT_LENGTH,
2740 &statusCode,
2741 responseHttpHeaders,
2742 TestBufferHandle);
2743
2744 /// assert
2745 ASSERT_ARE_EQUAL(int, HTTPAPI_READ_DATA_FAILED, result);
2746 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2747 ASSERT_ARE_EQUAL(int, 6, currentmalloc_call);
2748
2749 /// cleanup
2750 destroyHttpObjects(&requestHttpHeaders, &responseHttpHeaders); /* currentmalloc_call -= 2 */
2751 HTTPAPI_CloseConnection(httpHandle); /* currentmalloc_call -= 3 */
2752 HTTPAPI_Deinit();
2753 }
2754
2755 /*Tests_SRS_HTTPAPI_COMPACT_21_055: [ If the HTTPAPI_ExecuteRequest cannot parser the received message, it shall return HTTPAPI_RECEIVE_RESPONSE_FAILED. ]*/
2756 TEST_FUNCTION(HTTPAPI_ExecuteRequest__on_read_not_HTTP_header_failed)
2757 {
2758 /// arrange
2759 unsigned int statusCode;
2760 HTTPAPI_RESULT result;
2761 HTTP_HEADERS_HANDLE requestHttpHeaders;
2762 HTTP_HEADERS_HANDLE responseHttpHeaders;
2763 HTTP_HANDLE httpHandle = createHttpConnection();
2764 createHttpObjects(&requestHttpHeaders, &responseHttpHeaders);
2765 setHttpCertificate(httpHandle);
2766
2767 DoworkJobsReceivedBuffer = (const unsigned char*)"HTTPS/111.222 433 555\r\n";
2768 DoworkJobsReceivedBuffer_size[0] = strlen((const char*)DoworkJobsReceivedBuffer);
2769 DoworkJobsReceivedBuffer_counter = 0;
2770 DoworkJobs = (const xio_dowork_job*)doworkjob_o_re;
2771 DoworkJobsOpenResult = DoworkJobsOpenResult_ReceiveHead;
2772 DoworkJobsSendResult = DoworkJobsSendResult_ReceiveHead;
2773
2774 setupAllCallBeforeOpenHTTPsequence(requestHttpHeaders, 1, false);
2775 setupAllCallBeforeSendHTTPsequenceWithSuccess(requestHttpHeaders);
2776
2777 STRICT_EXPECTED_CALL(xio_dowork(IGNORED_NUM_ARG))
2778 .IgnoreArgument(1);
2779 STRICT_EXPECTED_CALL(gballoc_realloc(IGNORED_NUM_ARG, DoworkJobsReceivedBuffer_size[0])).IgnoreArgument(1);
2780 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG))
2781 .IgnoreArgument(1);
2782
2783 HTTPHeaders_GetHeader_shallReturn = HTTP_HEADERS_OK;
2784
2785 /// act
2786 result = HTTPAPI_ExecuteRequest(
2787 httpHandle,
2788 HTTPAPI_REQUEST_GET,
2789 TEST_EXECUTE_REQUEST_RELATIVE_PATH,
2790 requestHttpHeaders,
2791 TEST_EXECUTE_REQUEST_CONTENT,
2792 TEST_EXECUTE_REQUEST_CONTENT_LENGTH,
2793 &statusCode,
2794 responseHttpHeaders,
2795 TestBufferHandle);
2796
2797 /// assert
2798 ASSERT_ARE_EQUAL(int, HTTPAPI_RECEIVE_RESPONSE_FAILED, result);
2799 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2800 ASSERT_ARE_EQUAL(int, 6, currentmalloc_call);
2801
2802 /// cleanup
2803 destroyHttpObjects(&requestHttpHeaders, &responseHttpHeaders); /* currentmalloc_call -= 2 */
2804 HTTPAPI_CloseConnection(httpHandle); /* currentmalloc_call -= 3 */
2805 HTTPAPI_Deinit();
2806 }
2807
2808 /*Tests_SRS_HTTPAPI_COMPACT_21_055: [ If the HTTPAPI_ExecuteRequest cannot parser the received message, it shall return HTTPAPI_RECEIVE_RESPONSE_FAILED. ]*/
2809 TEST_FUNCTION(HTTPAPI_ExecuteRequest__on_read_wrong_URL_header_failed)
2810 {
2811 /// arrange
2812 unsigned int statusCode;
2813 HTTPAPI_RESULT result;
2814 HTTP_HEADERS_HANDLE requestHttpHeaders;
2815 HTTP_HEADERS_HANDLE responseHttpHeaders;
2816 int doworkReduction[1] = { 0 };
2817 HTTP_HANDLE httpHandle = createHttpConnection();
2818 createHttpObjects(&requestHttpHeaders, &responseHttpHeaders);
2819 setHttpCertificate(httpHandle);
2820
2821 DoworkJobsReceivedBuffer = (const unsigned char*)"HTTP/111222 433 555\r\n";
2822 DoworkJobsReceivedBuffer_size[0] = strlen((const char*)DoworkJobsReceivedBuffer);
2823 DoworkJobsReceivedBuffer_counter = 0;
2824 PrepareReceiveHead(requestHttpHeaders, DoworkJobsReceivedBuffer_size, doworkReduction, 1);
2825 DoworkJobs = (const xio_dowork_job*)doworkjob_o_re;
2826
2827 /// act
2828 result = HTTPAPI_ExecuteRequest(
2829 httpHandle,
2830 HTTPAPI_REQUEST_GET,
2831 TEST_EXECUTE_REQUEST_RELATIVE_PATH,
2832 requestHttpHeaders,
2833 TEST_EXECUTE_REQUEST_CONTENT,
2834 TEST_EXECUTE_REQUEST_CONTENT_LENGTH,
2835 &statusCode,
2836 responseHttpHeaders,
2837 TestBufferHandle);
2838
2839 /// assert
2840 ASSERT_ARE_EQUAL(int, HTTPAPI_RECEIVE_RESPONSE_FAILED, result);
2841 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2842 ASSERT_ARE_EQUAL(int, 6, currentmalloc_call);
2843
2844 /// cleanup
2845 destroyHttpObjects(&requestHttpHeaders, &responseHttpHeaders); /* currentmalloc_call -= 2 */
2846 HTTPAPI_CloseConnection(httpHandle); /* currentmalloc_call -= 3 */
2847 HTTPAPI_Deinit();
2848 }
2849
2850 /*Tests_SRS_HTTPAPI_COMPACT_21_055: [ If the HTTPAPI_ExecuteRequest cannot parser the received message, it shall return HTTPAPI_RECEIVE_RESPONSE_FAILED. ]*/
2851 TEST_FUNCTION(HTTPAPI_ExecuteRequest__on_read_header_with_no_statusCode_failed)
2852 {
2853 /// arrange
2854 unsigned int statusCode;
2855 HTTPAPI_RESULT result;
2856 HTTP_HEADERS_HANDLE requestHttpHeaders;
2857 HTTP_HEADERS_HANDLE responseHttpHeaders;
2858 int doworkReduction[1] = { 0 };
2859 HTTP_HANDLE httpHandle = createHttpConnection();
2860 createHttpObjects(&requestHttpHeaders, &responseHttpHeaders);
2861 setHttpCertificate(httpHandle);
2862
2863 DoworkJobsReceivedBuffer = (const unsigned char*)"HTTP/111.222\r\n";
2864 DoworkJobsReceivedBuffer_size[0] = strlen((const char*)DoworkJobsReceivedBuffer);
2865 DoworkJobsReceivedBuffer_counter = 0;
2866 PrepareReceiveHead(requestHttpHeaders, DoworkJobsReceivedBuffer_size, doworkReduction, 1);
2867 DoworkJobs = (const xio_dowork_job*)doworkjob_o_re;
2868
2869 /// act
2870 result = HTTPAPI_ExecuteRequest(
2871 httpHandle,
2872 HTTPAPI_REQUEST_GET,
2873 TEST_EXECUTE_REQUEST_RELATIVE_PATH,
2874 requestHttpHeaders,
2875 TEST_EXECUTE_REQUEST_CONTENT,
2876 TEST_EXECUTE_REQUEST_CONTENT_LENGTH,
2877 &statusCode,
2878 responseHttpHeaders,
2879 TestBufferHandle);
2880
2881 /// assert
2882 ASSERT_ARE_EQUAL(int, HTTPAPI_RECEIVE_RESPONSE_FAILED, result);
2883 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2884 ASSERT_ARE_EQUAL(int, 6, currentmalloc_call);
2885
2886 /// cleanup
2887 destroyHttpObjects(&requestHttpHeaders, &responseHttpHeaders); /* currentmalloc_call -= 2 */
2888 HTTPAPI_CloseConnection(httpHandle); /* currentmalloc_call -= 3 */
2889 HTTPAPI_Deinit();
2890 }
2891
2892 /*Tests_SRS_HTTPAPI_COMPACT_21_055: [ If the HTTPAPI_ExecuteRequest cannot parser the received message, it shall return HTTPAPI_RECEIVE_RESPONSE_FAILED. ]*/
2893 TEST_FUNCTION(HTTPAPI_ExecuteRequest__on_read_header_incomplete_failed)
2894 {
2895 /// arrange
2896 unsigned int statusCode;
2897 HTTPAPI_RESULT result;
2898 int doworkReduction[1] = { 0 };
2899 HTTP_HEADERS_HANDLE requestHttpHeaders;
2900 HTTP_HEADERS_HANDLE responseHttpHeaders;
2901 HTTP_HANDLE httpHandle = createHttpConnection();
2902 createHttpObjects(&requestHttpHeaders, &responseHttpHeaders);
2903 setHttpCertificate(httpHandle);
2904
2905 DoworkJobsReceivedBuffer = (const unsigned char*)"HTTP/111\r\n";
2906 DoworkJobsReceivedBuffer_size[0] = strlen((const char*)DoworkJobsReceivedBuffer);
2907 DoworkJobsReceivedBuffer_counter = 0;
2908 PrepareReceiveHead(requestHttpHeaders, DoworkJobsReceivedBuffer_size, doworkReduction, 1);
2909 DoworkJobs = (const xio_dowork_job*)doworkjob_o_re;
2910
2911 /// act
2912 result = HTTPAPI_ExecuteRequest(
2913 httpHandle,
2914 HTTPAPI_REQUEST_GET,
2915 TEST_EXECUTE_REQUEST_RELATIVE_PATH,
2916 requestHttpHeaders,
2917 TEST_EXECUTE_REQUEST_CONTENT,
2918 TEST_EXECUTE_REQUEST_CONTENT_LENGTH,
2919 &statusCode,
2920 responseHttpHeaders,
2921 TestBufferHandle);
2922
2923 /// assert
2924 ASSERT_ARE_EQUAL(int, HTTPAPI_RECEIVE_RESPONSE_FAILED, result);
2925 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2926 ASSERT_ARE_EQUAL(int, 6, currentmalloc_call);
2927
2928 /// cleanup
2929 destroyHttpObjects(&requestHttpHeaders, &responseHttpHeaders); /* currentmalloc_call -= 2 */
2930 HTTPAPI_CloseConnection(httpHandle); /* currentmalloc_call -= 3 */
2931 HTTPAPI_Deinit();
2932 }
2933
2934 /*Tests_SRS_HTTPAPI_COMPACT_21_055: [ If the HTTPAPI_ExecuteRequest cannot parser the received message, it shall return HTTPAPI_RECEIVE_RESPONSE_FAILED. ]*/
2935 TEST_FUNCTION(HTTPAPI_ExecuteRequest__on_read_multi_header_with_size_0_and_error_failed)
2936 {
2937 /// arrange
2938 unsigned int statusCode;
2939 HTTPAPI_RESULT result;
2940 int doworkReduction[2] = { 0, 0 };
2941 HTTP_HEADERS_HANDLE requestHttpHeaders;
2942 HTTP_HEADERS_HANDLE responseHttpHeaders;
2943 HTTP_HANDLE httpHandle = createHttpConnection();
2944 createHttpObjects(&requestHttpHeaders, &responseHttpHeaders);
2945 setHttpCertificate(httpHandle);
2946
2947 DoworkJobsReceivedBuffer = (const unsigned char*)"HTTP/111\r\n";
2948 DoworkJobsReceivedBuffer_size[0] = 0;
2949 DoworkJobsReceivedBuffer_size[1] = strlen((const char*)DoworkJobsReceivedBuffer);
2950 DoworkJobsReceivedBuffer_counter = 0;
2951 PrepareReceiveHead(requestHttpHeaders, DoworkJobsReceivedBuffer_size, doworkReduction, 2);
2952 DoworkJobs = (const xio_dowork_job*)doworkjob_o_rre;
2953
2954 /// act
2955 result = HTTPAPI_ExecuteRequest(
2956 httpHandle,
2957 HTTPAPI_REQUEST_GET,
2958 TEST_EXECUTE_REQUEST_RELATIVE_PATH,
2959 requestHttpHeaders,
2960 TEST_EXECUTE_REQUEST_CONTENT,
2961 TEST_EXECUTE_REQUEST_CONTENT_LENGTH,
2962 &statusCode,
2963 responseHttpHeaders,
2964 TestBufferHandle);
2965
2966 /// assert
2967 ASSERT_ARE_EQUAL(int, HTTPAPI_RECEIVE_RESPONSE_FAILED, result);
2968 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2969 ASSERT_ARE_EQUAL(int, 6, currentmalloc_call);
2970
2971 /// cleanup
2972 destroyHttpObjects(&requestHttpHeaders, &responseHttpHeaders); /* currentmalloc_call -= 2 */
2973 HTTPAPI_CloseConnection(httpHandle); /* currentmalloc_call -= 3 */
2974 HTTPAPI_Deinit();
2975 }
2976
2977 /*Tests_SRS_HTTPAPI_COMPACT_21_032: [ If the HTTPAPI_ExecuteRequest cannot read the message with the request result, it shall return HTTPAPI_READ_DATA_FAILED. ]*/
2978 TEST_FUNCTION(HTTPAPI_ExecuteRequest__read_huge_header_failed)
2979 {
2980 /// arrange
2981 HTTP_HANDLE httpHandle;
2982 unsigned int statusCode;
2983 HTTPAPI_RESULT result;
2984 int doworkReduction[1] = { 0 };
2985 HTTP_HEADERS_HANDLE requestHttpHeaders;
2986 HTTP_HEADERS_HANDLE responseHttpHeaders;
2987 size_t i;
2988 unsigned char hugeBuffer[3000] = "HTTP/111.";
2989 createHttpObjects(&requestHttpHeaders, &responseHttpHeaders);
2990 for (i = strlen((const char*)hugeBuffer); i < 3000; i++)
2991 {
2992 hugeBuffer[i] = 'a';
2993 }
2994 hugeBuffer[2995] = '\r';
2995 hugeBuffer[2996] = '\n';
2996 hugeBuffer[2997] = '\r';
2997 hugeBuffer[2998] = '\n';
2998 hugeBuffer[2999] = '\0';
2999
3000 httpHandle = createHttpConnection();
3001 setHttpCertificate(httpHandle);
3002
3003 DoworkJobsReceivedBuffer = (const unsigned char*)hugeBuffer;
3004 DoworkJobsReceivedBuffer_size[0] = strlen((const char*)DoworkJobsReceivedBuffer);
3005 DoworkJobsReceivedBuffer_counter = 0;
3006 PrepareReceiveHead(requestHttpHeaders, DoworkJobsReceivedBuffer_size, doworkReduction, 1);
3007 DoworkJobs = (const xio_dowork_job*)doworkjob_o_re;
3008
3009 /// act
3010 result = HTTPAPI_ExecuteRequest(
3011 httpHandle,
3012 HTTPAPI_REQUEST_GET,
3013 TEST_EXECUTE_REQUEST_RELATIVE_PATH,
3014 requestHttpHeaders,
3015 TEST_EXECUTE_REQUEST_CONTENT,
3016 TEST_EXECUTE_REQUEST_CONTENT_LENGTH,
3017 &statusCode,
3018 responseHttpHeaders,
3019 TestBufferHandle);
3020
3021 /// assert
3022 ASSERT_ARE_EQUAL(int, HTTPAPI_READ_DATA_FAILED, result);
3023 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
3024 ASSERT_ARE_EQUAL(int, 6, currentmalloc_call);
3025
3026 /// cleanup
3027 destroyHttpObjects(&requestHttpHeaders, &responseHttpHeaders); /* currentmalloc_call -= 2 */
3028 HTTPAPI_CloseConnection(httpHandle); /* currentmalloc_call -= 3 */
3029 HTTPAPI_Deinit();
3030 }
3031
3032 /*Tests_SRS_HTTPAPI_COMPACT_21_032: [ If the HTTPAPI_ExecuteRequest cannot read the message with the request result, it shall return HTTPAPI_READ_DATA_FAILED. ]*/
3033 TEST_FUNCTION(HTTPAPI_ExecuteRequest__content_length_without_value_failed)
3034 {
3035 /// arrange
3036 unsigned int statusCode;
3037 HTTPAPI_RESULT result;
3038 int doworkReduction[1] = { 1 };
3039 HTTP_HEADERS_HANDLE requestHttpHeaders;
3040 HTTP_HEADERS_HANDLE responseHttpHeaders;
3041 HTTP_HANDLE httpHandle = createHttpConnection();
3042 createHttpObjects(&requestHttpHeaders, &responseHttpHeaders);
3043 setHttpCertificate(httpHandle);
3044
3045 DoworkJobsReceivedBuffer = (const unsigned char*)"HTTP/111.222 433 555\r\ncontent-length:\r\n\r\n";
3046 DoworkJobsReceivedBuffer_size[0] = strlen((const char*)DoworkJobsReceivedBuffer);
3047 PrepareReceiveHead(requestHttpHeaders, DoworkJobsReceivedBuffer_size, doworkReduction, 1);
3048 DoworkJobsReceivedBuffer_counter = 0;
3049 DoworkJobs = (const xio_dowork_job*)doworkjob_o_re;
3050
3051 /// act
3052 result = HTTPAPI_ExecuteRequest(
3053 httpHandle,
3054 HTTPAPI_REQUEST_GET,
3055 TEST_EXECUTE_REQUEST_RELATIVE_PATH,
3056 requestHttpHeaders,
3057 TEST_EXECUTE_REQUEST_CONTENT,
3058 TEST_EXECUTE_REQUEST_CONTENT_LENGTH,
3059 &statusCode,
3060 responseHttpHeaders,
3061 TestBufferHandle);
3062
3063 /// assert
3064 ASSERT_ARE_EQUAL(int, HTTPAPI_READ_DATA_FAILED, result);
3065 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
3066 ASSERT_ARE_EQUAL(int, 6, currentmalloc_call);
3067
3068 /// cleanup
3069 destroyHttpObjects(&requestHttpHeaders, &responseHttpHeaders); /* currentmalloc_call -= 2 */
3070 HTTPAPI_CloseConnection(httpHandle); /* currentmalloc_call -= 3 */
3071 HTTPAPI_Deinit();
3072 }
3073
3074 /*Tests_SRS_HTTPAPI_COMPACT_21_031: [ After receive the response, the HTTPAPI_ExecuteRequest shall close the transport connection with the host. ]*/
3075 /*Tests_SRS_HTTPAPI_COMPACT_21_033: [ If the whole process succeed, the HTTPAPI_ExecuteRequest shall retur HTTPAPI_OK. ]*/
3076 /*Tests_SRS_HTTPAPI_COMPACT_21_021: [ The HTTPAPI_ExecuteRequest shall execute the http communtication with the provided host, sending a request and reciving the response. ]*/
3077 /*Tests_SRS_HTTPAPI_COMPACT_21_040: [ The request shall contain the http header provided in httpHeadersHandle parameter. ]*/
3078 /*Tests_SRS_HTTPAPI_COMPACT_21_042: [ The request can contain the a content message, provided in content parameter. ]*/
3079 /*Tests_SRS_HTTPAPI_COMPACT_21_046: [ The HTTPAPI_ExecuteRequest shall return the http status reported by the host in the received response. ]*/
3080 /*Tests_SRS_HTTPAPI_COMPACT_21_047: [ The HTTPAPI_ExecuteRequest shall report the status in the statusCode parameter. ]*/
3081 /*Tests_SRS_HTTPAPI_COMPACT_21_050: [ If there is a content in the response, the HTTPAPI_ExecuteRequest shall copy it in the responseContent buffer. ]*/
3082 /*Tests_SRS_HTTPAPI_COMPACT_21_073: [ The message received by the HTTPAPI_ExecuteRequest shall starts with a valid header. ]*/
3083 /*Tests_SRS_HTTPAPI_COMPACT_21_074: [ After the header, the message received by the HTTPAPI_ExecuteRequest can contain addition information about the content. ]*/
3084 /*Tests_SRS_HTTPAPI_COMPACT_21_053: [ The HTTPAPI_ExecuteRequest shall produce a set of http header to send to the host. ]*/
3085 TEST_FUNCTION(HTTPAPI_ExecuteRequest__Execute_request_succeed)
3086 {
3087 /// arrange
3088 unsigned int statusCode;
3089 HTTPAPI_RESULT result;
3090 HTTP_HEADERS_HANDLE requestHttpHeaders;
3091 HTTP_HEADERS_HANDLE responseHttpHeaders;
3092 HTTP_HANDLE httpHandle = createHttpConnection();
3093 createHttpObjects(&requestHttpHeaders, &responseHttpHeaders);
3094 setHttpCertificate(httpHandle);
3095
3096 DoworkJobsReceivedBuffer = TEST_RECEIVED_ANSWER;
3097 DoworkJobsReceivedBuffer_size[0] = strlen((const char*)DoworkJobsReceivedBuffer);
3098 DoworkJobsReceivedBuffer_counter = 0;
3099 DoworkJobs = (const xio_dowork_job*)doworkjob_o_rce;
3100 DoworkJobsOpenResult = DoworkJobsOpenResult_ReceiveHead;
3101 DoworkJobsSendResult = DoworkJobsSendResult_ReceiveHead;
3102
3103 setupAllCallBeforeOpenHTTPsequence(requestHttpHeaders, 1, false);
3104 setupAllCallBeforeSendHTTPsequenceWithSuccess(requestHttpHeaders);
3105 setupAllCallBeforeReceiveHTTPsequenceWithSuccess();
3106
3107 HTTPHeaders_GetHeader_shallReturn = HTTP_HEADERS_OK;
3108
3109 /// act
3110 result = HTTPAPI_ExecuteRequest(
3111 httpHandle,
3112 HTTPAPI_REQUEST_GET,
3113 TEST_EXECUTE_REQUEST_RELATIVE_PATH,
3114 requestHttpHeaders,
3115 TEST_EXECUTE_REQUEST_CONTENT,
3116 TEST_EXECUTE_REQUEST_CONTENT_LENGTH,
3117 &statusCode,
3118 responseHttpHeaders,
3119 TestBufferHandle);
3120
3121 /// assert
3122 ASSERT_ARE_EQUAL(int, HTTPAPI_OK, result);
3123 ASSERT_ARE_EQUAL(int, 433, statusCode);
3124 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
3125 ASSERT_ARE_EQUAL(int, 6, currentmalloc_call);
3126
3127 /// cleanup
3128 destroyHttpObjects(&requestHttpHeaders, &responseHttpHeaders); /* currentmalloc_call -= 2 */
3129 HTTPAPI_CloseConnection(httpHandle); /* currentmalloc_call -= 3 */
3130 HTTPAPI_Deinit();
3131 }
3132
3133 /*Tests_SRS_HTTPAPI_COMPACT_21_077: [ The HTTPAPI_ExecuteRequest shall wait, at least, 10 seconds for the SSL open process. ]*/
3134 TEST_FUNCTION(HTTPAPI_ExecuteRequest__Execute_request_retry_open_succeed)
3135 {
3136 /// arrange
3137 unsigned int statusCode;
3138 HTTPAPI_RESULT result;
3139 HTTP_HEADERS_HANDLE requestHttpHeaders;
3140 HTTP_HEADERS_HANDLE responseHttpHeaders;
3141 HTTP_HANDLE httpHandle = createHttpConnection();
3142 createHttpObjects(&requestHttpHeaders, &responseHttpHeaders);
3143 setHttpCertificate(httpHandle);
3144
3145 DoworkJobsReceivedBuffer = TEST_RECEIVED_ANSWER;
3146 DoworkJobsReceivedBuffer_size[0] = strlen((const char*)DoworkJobsReceivedBuffer);
3147 DoworkJobsReceivedBuffer_counter = 0;
3148 DoworkJobs = (const xio_dowork_job*)doworkjob_o_rce;
3149 DoworkJobsOpenResult = DoworkJobsOpenResult_ReceiveHead;
3150 DoworkJobsSendResult = DoworkJobsSendResult_ReceiveHead;
3151
3152 SkipDoworkJobsOpenResult = 97;
3153 setupAllCallBeforeOpenHTTPsequence(requestHttpHeaders, SkipDoworkJobsOpenResult + 1, false);
3154
3155 setupAllCallBeforeSendHTTPsequenceWithSuccess(requestHttpHeaders);
3156 setupAllCallBeforeReceiveHTTPsequenceWithSuccess();
3157
3158 HTTPHeaders_GetHeader_shallReturn = HTTP_HEADERS_OK;
3159
3160 /// act
3161 result = HTTPAPI_ExecuteRequest(
3162 httpHandle,
3163 HTTPAPI_REQUEST_GET,
3164 TEST_EXECUTE_REQUEST_RELATIVE_PATH,
3165 requestHttpHeaders,
3166 TEST_EXECUTE_REQUEST_CONTENT,
3167 TEST_EXECUTE_REQUEST_CONTENT_LENGTH,
3168 &statusCode,
3169 responseHttpHeaders,
3170 TestBufferHandle);
3171
3172 /// assert
3173 ASSERT_ARE_EQUAL(int, HTTPAPI_OK, result);
3174 ASSERT_ARE_EQUAL(int, 433, statusCode);
3175 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
3176 ASSERT_ARE_EQUAL(int, 6, currentmalloc_call);
3177
3178 /// cleanup
3179 destroyHttpObjects(&requestHttpHeaders, &responseHttpHeaders); /* currentmalloc_call -= 2 */
3180 HTTPAPI_CloseConnection(httpHandle); /* currentmalloc_call -= 3 */
3181 HTTPAPI_Deinit();
3182 }
3183
3184 /*Tests_SRS_HTTPAPI_COMPACT_21_079: [ The HTTPAPI_ExecuteRequest shall wait, at least, 20 seconds to send a buffer using the SSL connection. ]*/
3185 TEST_FUNCTION(HTTPAPI_ExecuteRequest__Execute_request_retry_send_succeed)
3186 {
3187 /// arrange
3188 unsigned int statusCode;
3189 int i;
3190 HTTPAPI_RESULT result;
3191 HTTP_HEADERS_HANDLE requestHttpHeaders;
3192 HTTP_HEADERS_HANDLE responseHttpHeaders;
3193 HTTP_HANDLE httpHandle = createHttpConnection();
3194 createHttpObjects(&requestHttpHeaders, &responseHttpHeaders);
3195 setHttpCertificate(httpHandle);
3196
3197 DoworkJobsReceivedBuffer = TEST_RECEIVED_ANSWER;
3198 DoworkJobsReceivedBuffer_size[0] = strlen((const char*)DoworkJobsReceivedBuffer);
3199 DoworkJobsReceivedBuffer_counter = 0;
3200 DoworkJobs = (const xio_dowork_job*)doworkjob_o_sre;
3201 DoworkJobsOpenResult = DoworkJobsOpenResult_ReceiveHead;
3202 DoworkJobsSendResult = DoworkJobsSendResult_ReceiveHead;
3203 call_on_send_complete_in_xio_send = false;
3204
3205 setupAllCallBeforeOpenHTTPsequence(requestHttpHeaders, 1, false);
3206
3207 STRICT_EXPECTED_CALL(xio_send(IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_NUM_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
3208 .IgnoreAllArguments();
3209 SkipDoworkJobsSendResult = 199;
3210 for (i = 0; i < SkipDoworkJobsSendResult+1; i++)
3211 {
3212 STRICT_EXPECTED_CALL(xio_dowork(IGNORED_NUM_ARG))
3213 .IgnoreArgument(1);
3214 STRICT_EXPECTED_CALL(ThreadAPI_Sleep(100));
3215 }
3216 STRICT_EXPECTED_CALL(HTTPHeaders_GetHeader(requestHttpHeaders, IGNORED_NUM_ARG, IGNORED_PTR_ARG))
3217 .IgnoreArgument(2).IgnoreArgument(3);
3218 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG)).IgnoreArgument(1);
3219 STRICT_EXPECTED_CALL(xio_send(IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_NUM_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
3220 .IgnoreAllArguments();
3221 STRICT_EXPECTED_CALL(xio_dowork(IGNORED_NUM_ARG))
3222 .IgnoreArgument(1);
3223 STRICT_EXPECTED_CALL(ThreadAPI_Sleep(100));
3224 STRICT_EXPECTED_CALL(xio_send(IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_NUM_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
3225 .IgnoreAllArguments();
3226 STRICT_EXPECTED_CALL(xio_dowork(IGNORED_NUM_ARG))
3227 .IgnoreArgument(1);
3228 STRICT_EXPECTED_CALL(ThreadAPI_Sleep(100));
3229 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG))
3230 .IgnoreArgument(1);
3231 STRICT_EXPECTED_CALL(HTTPHeaders_GetHeader(requestHttpHeaders, IGNORED_NUM_ARG, IGNORED_PTR_ARG))
3232 .IgnoreArgument(2).IgnoreArgument(3);
3233 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG)).IgnoreArgument(1);
3234 STRICT_EXPECTED_CALL(xio_send(IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_NUM_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
3235 .IgnoreAllArguments();
3236 STRICT_EXPECTED_CALL(xio_dowork(IGNORED_NUM_ARG))
3237 .IgnoreArgument(1);
3238 STRICT_EXPECTED_CALL(ThreadAPI_Sleep(100));
3239 STRICT_EXPECTED_CALL(xio_send(IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_NUM_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
3240 .IgnoreAllArguments();
3241 STRICT_EXPECTED_CALL(xio_dowork(IGNORED_NUM_ARG))
3242 .IgnoreArgument(1);
3243 STRICT_EXPECTED_CALL(ThreadAPI_Sleep(100));
3244 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG))
3245 .IgnoreArgument(1);
3246 STRICT_EXPECTED_CALL(xio_send(IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_NUM_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
3247 .IgnoreAllArguments();
3248 STRICT_EXPECTED_CALL(xio_dowork(IGNORED_NUM_ARG))
3249 .IgnoreArgument(1);
3250 STRICT_EXPECTED_CALL(ThreadAPI_Sleep(100));
3251
3252 STRICT_EXPECTED_CALL(xio_send(IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_NUM_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
3253 .IgnoreAllArguments();
3254 STRICT_EXPECTED_CALL(xio_dowork(IGNORED_NUM_ARG))
3255 .IgnoreArgument(1);
3256 STRICT_EXPECTED_CALL(ThreadAPI_Sleep(100));
3257
3258 setupAllCallBeforeReceiveHTTPsequenceWithSuccess();
3259
3260 HTTPHeaders_GetHeader_shallReturn = HTTP_HEADERS_OK;
3261
3262 /// act
3263 result = HTTPAPI_ExecuteRequest(
3264 httpHandle,
3265 HTTPAPI_REQUEST_GET,
3266 TEST_EXECUTE_REQUEST_RELATIVE_PATH,
3267 requestHttpHeaders,
3268 TEST_EXECUTE_REQUEST_CONTENT,
3269 TEST_EXECUTE_REQUEST_CONTENT_LENGTH,
3270 &statusCode,
3271 responseHttpHeaders,
3272 TestBufferHandle);
3273
3274 /// assert
3275 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
3276 ASSERT_ARE_EQUAL(int, HTTPAPI_OK, result);
3277 ASSERT_ARE_EQUAL(int, 433, statusCode);
3278 ASSERT_ARE_EQUAL(int, 6, currentmalloc_call);
3279
3280 /// cleanup
3281 destroyHttpObjects(&requestHttpHeaders, &responseHttpHeaders); /* currentmalloc_call -= 2 */
3282 HTTPAPI_CloseConnection(httpHandle); /* currentmalloc_call -= 3 */
3283 HTTPAPI_Deinit();
3284 }
3285
3286 /*Tests_SRS_HTTPAPI_COMPACT_21_035: [ The HTTPAPI_ExecuteRequest shall execute resquest for types GET, POST, PUT, DELETE, PATCH, HEAD. ]*/
3287 /*Tests_SRS_HTTPAPI_COMPACT_21_036: [ The request type shall be provided in the parameter requestType. ]*/
3288 TEST_FUNCTION(HTTPAPI_ExecuteRequest__request_get_succeed)
3289 {
3290 /// arrange
3291 unsigned int statusCode;
3292 HTTPAPI_RESULT result;
3293 HTTP_HEADERS_HANDLE requestHttpHeaders;
3294 HTTP_HEADERS_HANDLE responseHttpHeaders;
3295 HTTP_HANDLE httpHandle = createHttpConnection();
3296 createHttpObjects(&requestHttpHeaders, &responseHttpHeaders);
3297 setHttpCertificate(httpHandle);
3298
3299 DoworkJobsReceivedBuffer = TEST_RECEIVED_ANSWER;
3300 DoworkJobsReceivedBuffer_size[0] = strlen((const char*)DoworkJobsReceivedBuffer);
3301 DoworkJobsReceivedBuffer_counter = 0;
3302 DoworkJobs = (const xio_dowork_job*)doworkjob_o_rce;
3303 DoworkJobsOpenResult = DoworkJobsOpenResult_ReceiveHead;
3304 DoworkJobsSendResult = DoworkJobsSendResult_ReceiveHead;
3305
3306 setupAllCallBeforeOpenHTTPsequence(requestHttpHeaders, 1, false);
3307 setupAllCallBeforeSendHTTPsequenceWithSuccess(requestHttpHeaders);
3308 setupAllCallBeforeReceiveHTTPsequenceWithSuccess();
3309
3310 HTTPHeaders_GetHeader_shallReturn = HTTP_HEADERS_OK;
3311 xio_send_transmited_buffer_target = 1;
3312
3313 /// act
3314 result = HTTPAPI_ExecuteRequest(
3315 httpHandle,
3316 HTTPAPI_REQUEST_GET,
3317 TEST_EXECUTE_REQUEST_RELATIVE_PATH,
3318 requestHttpHeaders,
3319 TEST_EXECUTE_REQUEST_CONTENT,
3320 TEST_EXECUTE_REQUEST_CONTENT_LENGTH,
3321 &statusCode,
3322 responseHttpHeaders,
3323 TestBufferHandle);
3324
3325 /// assert
3326 ASSERT_ARE_EQUAL(int, HTTPAPI_OK, result);
3327 ASSERT_ARE_EQUAL(int, 433, statusCode);
3328 xio_send_transmited_buffer[3] = '\0';
3329 ASSERT_ARE_EQUAL(char_ptr, "GET", xio_send_transmited_buffer);
3330 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
3331 ASSERT_ARE_EQUAL(int, 6, currentmalloc_call);
3332
3333 /// cleanup
3334 destroyHttpObjects(&requestHttpHeaders, &responseHttpHeaders); /* currentmalloc_call -= 2 */
3335 HTTPAPI_CloseConnection(httpHandle); /* currentmalloc_call -= 3 */
3336 HTTPAPI_Deinit();
3337 }
3338
3339 /*Tests_SRS_HTTPAPI_COMPACT_21_035: [ The HTTPAPI_ExecuteRequest shall execute resquest for types GET, POST, PUT, DELETE, PATCH, HEAD. ]*/
3340 TEST_FUNCTION(HTTPAPI_ExecuteRequest__request_post_succeed)
3341 {
3342 /// arrange
3343 unsigned int statusCode;
3344 HTTPAPI_RESULT result;
3345 HTTP_HEADERS_HANDLE requestHttpHeaders;
3346 HTTP_HEADERS_HANDLE responseHttpHeaders;
3347 HTTP_HANDLE httpHandle = createHttpConnection();
3348 createHttpObjects(&requestHttpHeaders, &responseHttpHeaders);
3349
3350 setHttpCertificate(httpHandle);
3351 DoworkJobsReceivedBuffer = TEST_RECEIVED_ANSWER;
3352 DoworkJobsReceivedBuffer_size[0] = strlen((const char*)DoworkJobsReceivedBuffer);
3353 DoworkJobsReceivedBuffer_counter = 0;
3354 DoworkJobs = (const xio_dowork_job*)doworkjob_o_rce;
3355 DoworkJobsOpenResult = DoworkJobsOpenResult_ReceiveHead;
3356 DoworkJobsSendResult = DoworkJobsSendResult_ReceiveHead;
3357
3358 setupAllCallBeforeOpenHTTPsequence(requestHttpHeaders, 1, false);
3359 setupAllCallBeforeSendHTTPsequenceWithSuccess(requestHttpHeaders);
3360 setupAllCallBeforeReceiveHTTPsequenceWithSuccess();
3361
3362 HTTPHeaders_GetHeader_shallReturn = HTTP_HEADERS_OK;
3363 xio_send_transmited_buffer_target = 1;
3364
3365 /// act
3366 result = HTTPAPI_ExecuteRequest(
3367 httpHandle,
3368 HTTPAPI_REQUEST_POST,
3369 TEST_EXECUTE_REQUEST_RELATIVE_PATH,
3370 requestHttpHeaders,
3371 TEST_EXECUTE_REQUEST_CONTENT,
3372 TEST_EXECUTE_REQUEST_CONTENT_LENGTH,
3373 &statusCode,
3374 responseHttpHeaders,
3375 TestBufferHandle);
3376
3377 /// assert
3378 ASSERT_ARE_EQUAL(int, HTTPAPI_OK, result);
3379 ASSERT_ARE_EQUAL(int, 433, statusCode);
3380 xio_send_transmited_buffer[4] = '\0';
3381 ASSERT_ARE_EQUAL(char_ptr, "POST", xio_send_transmited_buffer);
3382 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
3383 ASSERT_ARE_EQUAL(int, 6, currentmalloc_call);
3384
3385 /// cleanup
3386 destroyHttpObjects(&requestHttpHeaders, &responseHttpHeaders); /* currentmalloc_call -= 2 */
3387 HTTPAPI_CloseConnection(httpHandle); /* currentmalloc_call -= 3 */
3388 HTTPAPI_Deinit();
3389 }
3390
3391 /*Tests_SRS_HTTPAPI_COMPACT_21_035: [ The HTTPAPI_ExecuteRequest shall execute resquest for types GET, POST, PUT, DELETE, PATCH, HEAD. ]*/
3392 TEST_FUNCTION(HTTPAPI_ExecuteRequest__request_put_succeed)
3393 {
3394 /// arrange
3395 unsigned int statusCode;
3396 HTTPAPI_RESULT result;
3397 HTTP_HEADERS_HANDLE requestHttpHeaders;
3398 HTTP_HEADERS_HANDLE responseHttpHeaders;
3399 HTTP_HANDLE httpHandle = createHttpConnection();
3400 createHttpObjects(&requestHttpHeaders, &responseHttpHeaders);
3401
3402 setHttpCertificate(httpHandle);
3403 DoworkJobsReceivedBuffer = TEST_RECEIVED_ANSWER;
3404 DoworkJobsReceivedBuffer_size[0] = strlen((const char*)DoworkJobsReceivedBuffer);
3405 DoworkJobsReceivedBuffer_counter = 0;
3406 DoworkJobs = (const xio_dowork_job*)doworkjob_o_rce;
3407 DoworkJobsOpenResult = DoworkJobsOpenResult_ReceiveHead;
3408 DoworkJobsSendResult = DoworkJobsSendResult_ReceiveHead;
3409
3410 setupAllCallBeforeOpenHTTPsequence(requestHttpHeaders, 1, false);
3411 setupAllCallBeforeSendHTTPsequenceWithSuccess(requestHttpHeaders);
3412 setupAllCallBeforeReceiveHTTPsequenceWithSuccess();
3413
3414 HTTPHeaders_GetHeader_shallReturn = HTTP_HEADERS_OK;
3415 xio_send_transmited_buffer_target = 1;
3416
3417 /// act
3418 result = HTTPAPI_ExecuteRequest(
3419 httpHandle,
3420 HTTPAPI_REQUEST_PUT,
3421 TEST_EXECUTE_REQUEST_RELATIVE_PATH,
3422 requestHttpHeaders,
3423 TEST_EXECUTE_REQUEST_CONTENT,
3424 TEST_EXECUTE_REQUEST_CONTENT_LENGTH,
3425 &statusCode,
3426 responseHttpHeaders,
3427 TestBufferHandle);
3428
3429 /// assert
3430 ASSERT_ARE_EQUAL(int, HTTPAPI_OK, result);
3431 ASSERT_ARE_EQUAL(int, 433, statusCode);
3432 xio_send_transmited_buffer[3] = '\0';
3433 ASSERT_ARE_EQUAL(char_ptr, "PUT", xio_send_transmited_buffer);
3434 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
3435 ASSERT_ARE_EQUAL(int, 6, currentmalloc_call);
3436
3437 /// cleanup
3438 destroyHttpObjects(&requestHttpHeaders, &responseHttpHeaders); /* currentmalloc_call -= 2 */
3439 HTTPAPI_CloseConnection(httpHandle); /* currentmalloc_call -= 3 */
3440 HTTPAPI_Deinit();
3441 }
3442
3443 /*Tests_SRS_HTTPAPI_COMPACT_21_035: [ The HTTPAPI_ExecuteRequest shall execute resquest for types GET, POST, PUT, DELETE, PATCH, HEAD. ]*/
3444 TEST_FUNCTION(HTTPAPI_ExecuteRequest__request_delete_succeed)
3445 {
3446 /// arrange
3447 unsigned int statusCode;
3448 HTTPAPI_RESULT result;
3449 HTTP_HEADERS_HANDLE requestHttpHeaders;
3450 HTTP_HEADERS_HANDLE responseHttpHeaders;
3451 HTTP_HANDLE httpHandle = createHttpConnection();
3452 createHttpObjects(&requestHttpHeaders, &responseHttpHeaders);
3453
3454 setHttpCertificate(httpHandle);
3455 DoworkJobsReceivedBuffer = TEST_RECEIVED_ANSWER;
3456 DoworkJobsReceivedBuffer_size[0] = strlen((const char*)DoworkJobsReceivedBuffer);
3457 DoworkJobsReceivedBuffer_counter = 0;
3458 DoworkJobs = (const xio_dowork_job*)doworkjob_o_rce;
3459 DoworkJobsOpenResult = DoworkJobsOpenResult_ReceiveHead;
3460 DoworkJobsSendResult = DoworkJobsSendResult_ReceiveHead;
3461
3462 setupAllCallBeforeOpenHTTPsequence(requestHttpHeaders, 1, false);
3463 setupAllCallBeforeSendHTTPsequenceWithSuccess(requestHttpHeaders);
3464 setupAllCallBeforeReceiveHTTPsequenceWithSuccess();
3465
3466 HTTPHeaders_GetHeader_shallReturn = HTTP_HEADERS_OK;
3467 xio_send_transmited_buffer_target = 1;
3468
3469 /// act
3470 result = HTTPAPI_ExecuteRequest(
3471 httpHandle,
3472 HTTPAPI_REQUEST_DELETE,
3473 TEST_EXECUTE_REQUEST_RELATIVE_PATH,
3474 requestHttpHeaders,
3475 TEST_EXECUTE_REQUEST_CONTENT,
3476 TEST_EXECUTE_REQUEST_CONTENT_LENGTH,
3477 &statusCode,
3478 responseHttpHeaders,
3479 TestBufferHandle);
3480
3481 /// assert
3482 ASSERT_ARE_EQUAL(int, HTTPAPI_OK, result);
3483 ASSERT_ARE_EQUAL(int, 433, statusCode);
3484 xio_send_transmited_buffer[6] = '\0';
3485 ASSERT_ARE_EQUAL(char_ptr, "DELETE", xio_send_transmited_buffer);
3486 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
3487 ASSERT_ARE_EQUAL(int, 6, currentmalloc_call);
3488
3489 /// cleanup
3490 destroyHttpObjects(&requestHttpHeaders, &responseHttpHeaders); /* currentmalloc_call -= 2 */
3491 HTTPAPI_CloseConnection(httpHandle); /* currentmalloc_call -= 3 */
3492 HTTPAPI_Deinit();
3493 }
3494
3495 /*Tests_SRS_HTTPAPI_COMPACT_21_035: [ The HTTPAPI_ExecuteRequest shall execute resquest for types GET, POST, PUT, DELETE, PATCH, HEAD. ]*/
3496 TEST_FUNCTION(HTTPAPI_ExecuteRequest__request_patch_succeed)
3497 {
3498 /// arrange
3499 unsigned int statusCode;
3500 HTTPAPI_RESULT result;
3501 HTTP_HEADERS_HANDLE requestHttpHeaders;
3502 HTTP_HEADERS_HANDLE responseHttpHeaders;
3503 HTTP_HANDLE httpHandle = createHttpConnection();
3504 createHttpObjects(&requestHttpHeaders, &responseHttpHeaders);
3505
3506 setHttpCertificate(httpHandle);
3507 DoworkJobsReceivedBuffer = TEST_RECEIVED_ANSWER;
3508 DoworkJobsReceivedBuffer_size[0] = strlen((const char*)DoworkJobsReceivedBuffer);
3509 DoworkJobsReceivedBuffer_counter = 0;
3510 DoworkJobs = (const xio_dowork_job*)doworkjob_o_rce;
3511 DoworkJobsOpenResult = DoworkJobsOpenResult_ReceiveHead;
3512 DoworkJobsSendResult = DoworkJobsSendResult_ReceiveHead;
3513
3514 setupAllCallBeforeOpenHTTPsequence(requestHttpHeaders, 1, false);
3515 setupAllCallBeforeSendHTTPsequenceWithSuccess(requestHttpHeaders);
3516 setupAllCallBeforeReceiveHTTPsequenceWithSuccess();
3517
3518 HTTPHeaders_GetHeader_shallReturn = HTTP_HEADERS_OK;
3519 xio_send_transmited_buffer_target = 1;
3520
3521 /// act
3522 result = HTTPAPI_ExecuteRequest(
3523 httpHandle,
3524 HTTPAPI_REQUEST_PATCH,
3525 TEST_EXECUTE_REQUEST_RELATIVE_PATH,
3526 requestHttpHeaders,
3527 TEST_EXECUTE_REQUEST_CONTENT,
3528 TEST_EXECUTE_REQUEST_CONTENT_LENGTH,
3529 &statusCode,
3530 responseHttpHeaders,
3531 TestBufferHandle);
3532
3533 /// assert
3534 ASSERT_ARE_EQUAL(int, HTTPAPI_OK, result);
3535 ASSERT_ARE_EQUAL(int, 433, statusCode);
3536 xio_send_transmited_buffer[5] = '\0';
3537 ASSERT_ARE_EQUAL(char_ptr, "PATCH", xio_send_transmited_buffer);
3538 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
3539 ASSERT_ARE_EQUAL(int, 6, currentmalloc_call);
3540
3541 /// cleanup
3542 destroyHttpObjects(&requestHttpHeaders, &responseHttpHeaders); /* currentmalloc_call -= 2 */
3543 HTTPAPI_CloseConnection(httpHandle); /* currentmalloc_call -= 3 */
3544 HTTPAPI_Deinit();
3545 }
3546
3547 /*Tests_SRS_HTTPAPI_COMPACT_21_035: [ The HTTPAPI_ExecuteRequest shall execute resquest for types GET, POST, PUT, DELETE, PATCH, HEAD. ]*/
3548 TEST_FUNCTION(HTTPAPI_ExecuteRequest__request_head_succeed)
3549 {
3550 /// arrange
3551 unsigned int statusCode;
3552 HTTPAPI_RESULT result;
3553 HTTP_HEADERS_HANDLE requestHttpHeaders;
3554 HTTP_HEADERS_HANDLE responseHttpHeaders;
3555 HTTP_HANDLE httpHandle = createHttpConnection();
3556 createHttpObjects(&requestHttpHeaders, &responseHttpHeaders);
3557
3558 setHttpCertificate(httpHandle);
3559 DoworkJobsReceivedBuffer = TEST_RECEIVED_ANSWER;
3560 DoworkJobsReceivedBuffer_size[0] = strlen((const char*)DoworkJobsReceivedBuffer);
3561 DoworkJobsReceivedBuffer_counter = 0;
3562 DoworkJobs = (const xio_dowork_job*)doworkjob_o_rce;
3563 DoworkJobsOpenResult = DoworkJobsOpenResult_ReceiveHead;
3564 DoworkJobsSendResult = DoworkJobsSendResult_ReceiveHead;
3565
3566 setupAllCallBeforeOpenHTTPsequence(requestHttpHeaders, 1, false);
3567 setupAllCallBeforeSendHTTPsequenceWithSuccess(requestHttpHeaders);
3568 setupAllCallBeforeReceiveHTTPHeadsequenceWithSuccess();
3569
3570 HTTPHeaders_GetHeader_shallReturn = HTTP_HEADERS_OK;
3571 xio_send_transmited_buffer_target = 1;
3572
3573 /// act
3574 result = HTTPAPI_ExecuteRequest(
3575 httpHandle,
3576 HTTPAPI_REQUEST_HEAD,
3577 TEST_EXECUTE_REQUEST_RELATIVE_PATH,
3578 requestHttpHeaders,
3579 TEST_EXECUTE_REQUEST_CONTENT,
3580 TEST_EXECUTE_REQUEST_CONTENT_LENGTH,
3581 &statusCode,
3582 responseHttpHeaders,
3583 TestBufferHandle);
3584
3585 /// assert
3586 ASSERT_ARE_EQUAL(int, HTTPAPI_OK, result);
3587 ASSERT_ARE_EQUAL(int, 433, statusCode);
3588 xio_send_transmited_buffer[4] = '\0';
3589 ASSERT_ARE_EQUAL(char_ptr, "HEAD", xio_send_transmited_buffer);
3590 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
3591 ASSERT_ARE_EQUAL(int, 6, currentmalloc_call);
3592
3593 /// cleanup
3594 destroyHttpObjects(&requestHttpHeaders, &responseHttpHeaders); /* currentmalloc_call -= 2 */
3595 HTTPAPI_CloseConnection(httpHandle); /* currentmalloc_call -= 3 */
3596 HTTPAPI_Deinit();
3597 }
3598
3599 /*Tests_SRS_HTTPAPI_COMPACT_21_038: [ The HTTPAPI_ExecuteRequest shall execute the resquest for the path in relativePath parameter. ]*/
3600 TEST_FUNCTION(HTTPAPI_ExecuteRequest__request_relative_path_succeed)
3601 {
3602 /// arrange
3603 unsigned int statusCode;
3604 HTTPAPI_RESULT result;
3605 HTTP_HEADERS_HANDLE requestHttpHeaders;
3606 HTTP_HEADERS_HANDLE responseHttpHeaders;
3607 HTTP_HANDLE httpHandle = createHttpConnection();
3608 createHttpObjects(&requestHttpHeaders, &responseHttpHeaders);
3609
3610 setHttpCertificate(httpHandle);
3611 DoworkJobsReceivedBuffer = TEST_RECEIVED_ANSWER;
3612 DoworkJobsReceivedBuffer_size[0] = strlen((const char*)DoworkJobsReceivedBuffer);
3613 DoworkJobsReceivedBuffer_counter = 0;
3614 DoworkJobs = (const xio_dowork_job*)doworkjob_o_rce;
3615 DoworkJobsOpenResult = DoworkJobsOpenResult_ReceiveHead;
3616 DoworkJobsSendResult = DoworkJobsSendResult_ReceiveHead;
3617
3618 setupAllCallBeforeOpenHTTPsequence(requestHttpHeaders, 1, false);
3619 setupAllCallBeforeSendHTTPsequenceWithSuccess(requestHttpHeaders);
3620 setupAllCallBeforeReceiveHTTPsequenceWithSuccess();
3621
3622 HTTPHeaders_GetHeader_shallReturn = HTTP_HEADERS_OK;
3623 xio_send_transmited_buffer_target = 1;
3624
3625 /// act
3626 result = HTTPAPI_ExecuteRequest(
3627 httpHandle,
3628 HTTPAPI_REQUEST_GET,
3629 TEST_EXECUTE_REQUEST_RELATIVE_PATH,
3630 requestHttpHeaders,
3631 TEST_EXECUTE_REQUEST_CONTENT,
3632 TEST_EXECUTE_REQUEST_CONTENT_LENGTH,
3633 &statusCode,
3634 responseHttpHeaders,
3635 TestBufferHandle);
3636
3637 /// assert
3638 ASSERT_ARE_EQUAL(int, HTTPAPI_OK, result);
3639 ASSERT_ARE_EQUAL(int, 433, statusCode);
3640 xio_send_transmited_buffer[66] = '\0';
3641 ASSERT_ARE_EQUAL(char_ptr, TEST_EXECUTE_REQUEST_RELATIVE_PATH, &(xio_send_transmited_buffer[4]));
3642 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
3643 ASSERT_ARE_EQUAL(int, 6, currentmalloc_call);
3644
3645 /// cleanup
3646 destroyHttpObjects(&requestHttpHeaders, &responseHttpHeaders); /* currentmalloc_call -= 2 */
3647 HTTPAPI_CloseConnection(httpHandle); /* currentmalloc_call -= 3 */
3648 HTTPAPI_Deinit();
3649 }
3650
3651 /*Tests_SRS_HTTPAPI_COMPACT_21_043: [ If the content is NULL, the HTTPAPI_ExecuteRequest shall send the request without content. ]*/
3652 /*Tests_SRS_HTTPAPI_COMPACT_21_044: [ If the content is not NULL, the number of bytes in the content shall be provided in contentLength parameter. ]*/
3653 /*Tests_SRS_HTTPAPI_COMPACT_21_075: [ The message received by the HTTPAPI_ExecuteRequest can contain a body with the message content. ]*/
3654 TEST_FUNCTION(HTTPAPI_ExecuteRequest__request_with_content_succeed)
3655 {
3656 /// arrange
3657 unsigned int statusCode;
3658 HTTPAPI_RESULT result;
3659 HTTP_HEADERS_HANDLE requestHttpHeaders;
3660 HTTP_HEADERS_HANDLE responseHttpHeaders;
3661 HTTP_HANDLE httpHandle = createHttpConnection();
3662 createHttpObjects(&requestHttpHeaders, &responseHttpHeaders);
3663
3664 setHttpCertificate(httpHandle);
3665 DoworkJobsReceivedBuffer = TEST_RECEIVED_ANSWER;
3666 DoworkJobsReceivedBuffer_size[0] = strlen((const char*)DoworkJobsReceivedBuffer);
3667 DoworkJobsReceivedBuffer_counter = 0;
3668 DoworkJobs = (const xio_dowork_job*)doworkjob_o_rce;
3669 DoworkJobsOpenResult = DoworkJobsOpenResult_ReceiveHead;
3670 DoworkJobsSendResult = DoworkJobsSendResult_ReceiveHead;
3671
3672 setupAllCallBeforeOpenHTTPsequence(requestHttpHeaders, 1, false);
3673 setupAllCallBeforeSendHTTPsequenceWithSuccess(requestHttpHeaders);
3674 setupAllCallBeforeReceiveHTTPsequenceWithSuccess();
3675
3676 HTTPHeaders_GetHeader_shallReturn = HTTP_HEADERS_OK;
3677 xio_send_transmited_buffer_target = 7;
3678
3679 /// act
3680 result = HTTPAPI_ExecuteRequest(
3681 httpHandle,
3682 HTTPAPI_REQUEST_GET,
3683 TEST_EXECUTE_REQUEST_RELATIVE_PATH,
3684 requestHttpHeaders,
3685 TEST_EXECUTE_REQUEST_CONTENT,
3686 TEST_EXECUTE_REQUEST_CONTENT_LENGTH,
3687 &statusCode,
3688 responseHttpHeaders,
3689 TestBufferHandle);
3690
3691 /// assert
3692 ASSERT_ARE_EQUAL(int, HTTPAPI_OK, result);
3693 ASSERT_ARE_EQUAL(int, 433, statusCode);
3694 ASSERT_ARE_EQUAL(char_ptr, (const char*)TEST_EXECUTE_REQUEST_CONTENT, xio_send_transmited_buffer);
3695 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
3696 ASSERT_ARE_EQUAL(int, 6, currentmalloc_call);
3697
3698 /// cleanup
3699 destroyHttpObjects(&requestHttpHeaders, &responseHttpHeaders); /* currentmalloc_call -= 2 */
3700 HTTPAPI_CloseConnection(httpHandle); /* currentmalloc_call -= 3 */
3701 HTTPAPI_Deinit();
3702 }
3703
3704 /*Tests_SRS_HTTPAPI_COMPACT_21_043: [ If the content is NULL, the HTTPAPI_ExecuteRequest shall send the request without content. ]*/
3705 TEST_FUNCTION(HTTPAPI_ExecuteRequest__request_NULL_content_succeed)
3706 {
3707 /// arrange
3708 unsigned int statusCode;
3709 HTTPAPI_RESULT result;
3710 HTTP_HEADERS_HANDLE requestHttpHeaders;
3711 HTTP_HEADERS_HANDLE responseHttpHeaders;
3712 HTTP_HANDLE httpHandle = createHttpConnection();
3713 createHttpObjects(&requestHttpHeaders, &responseHttpHeaders);
3714
3715 setHttpCertificate(httpHandle);
3716 DoworkJobsReceivedBuffer = TEST_RECEIVED_ANSWER;
3717 DoworkJobsReceivedBuffer_size[0] = strlen((const char*)DoworkJobsReceivedBuffer);
3718 DoworkJobsReceivedBuffer_counter = 0;
3719 DoworkJobs = (const xio_dowork_job*)doworkjob_o_rce;
3720 DoworkJobsOpenResult = DoworkJobsOpenResult_ReceiveHead;
3721 DoworkJobsSendResult = DoworkJobsSendResult_ReceiveHead;
3722
3723 setupAllCallBeforeOpenHTTPsequence(requestHttpHeaders, 1, false);
3724 STRICT_EXPECTED_CALL(xio_send(IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_NUM_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
3725 .IgnoreAllArguments();
3726 STRICT_EXPECTED_CALL(HTTPHeaders_GetHeader(requestHttpHeaders, IGNORED_NUM_ARG, IGNORED_PTR_ARG))
3727 .IgnoreArgument(2).IgnoreArgument(3);
3728 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG)).IgnoreArgument(1);
3729 STRICT_EXPECTED_CALL(xio_send(IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_NUM_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
3730 .IgnoreAllArguments();
3731 STRICT_EXPECTED_CALL(xio_send(IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_NUM_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
3732 .IgnoreAllArguments();
3733 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG))
3734 .IgnoreArgument(1);
3735 STRICT_EXPECTED_CALL(HTTPHeaders_GetHeader(requestHttpHeaders, IGNORED_NUM_ARG, IGNORED_PTR_ARG))
3736 .IgnoreArgument(2).IgnoreArgument(3);
3737 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG)).IgnoreArgument(1);
3738 STRICT_EXPECTED_CALL(xio_send(IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_NUM_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
3739 .IgnoreAllArguments();
3740 STRICT_EXPECTED_CALL(xio_send(IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_NUM_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
3741 .IgnoreAllArguments();
3742 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG))
3743 .IgnoreArgument(1);
3744 STRICT_EXPECTED_CALL(xio_send(IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_NUM_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
3745 .IgnoreAllArguments();
3746 setupAllCallBeforeReceiveHTTPsequenceWithSuccess();
3747
3748 HTTPHeaders_GetHeader_shallReturn = HTTP_HEADERS_OK;
3749 xio_send_transmited_buffer_target = 7;
3750
3751 /// act
3752 result = HTTPAPI_ExecuteRequest(
3753 httpHandle,
3754 HTTPAPI_REQUEST_GET,
3755 TEST_EXECUTE_REQUEST_RELATIVE_PATH,
3756 requestHttpHeaders,
3757 NULL,
3758 TEST_EXECUTE_REQUEST_CONTENT_LENGTH,
3759 &statusCode,
3760 responseHttpHeaders,
3761 TestBufferHandle);
3762
3763 /// assert
3764 ASSERT_ARE_EQUAL(int, HTTPAPI_OK, result);
3765 ASSERT_ARE_EQUAL(int, 433, statusCode);
3766 ASSERT_ARE_EQUAL(char_ptr, (const char*)"", xio_send_transmited_buffer);
3767 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
3768 ASSERT_ARE_EQUAL(int, 6, currentmalloc_call);
3769
3770 /// cleanup
3771 destroyHttpObjects(&requestHttpHeaders, &responseHttpHeaders); /* currentmalloc_call -= 2 */
3772 HTTPAPI_CloseConnection(httpHandle); /* currentmalloc_call -= 3 */
3773 HTTPAPI_Deinit();
3774 }
3775
3776 /*Tests_SRS_HTTPAPI_COMPACT_21_045: [ If the contentLength is lower than one, the HTTPAPI_ExecuteRequest shall send the request without content. ]*/
3777 TEST_FUNCTION(HTTPAPI_ExecuteRequest__request_content_size_0_succeed)
3778 {
3779 /// arrange
3780 unsigned int statusCode;
3781 HTTPAPI_RESULT result;
3782 HTTP_HEADERS_HANDLE requestHttpHeaders;
3783 HTTP_HEADERS_HANDLE responseHttpHeaders;
3784 HTTP_HANDLE httpHandle = createHttpConnection();
3785 createHttpObjects(&requestHttpHeaders, &responseHttpHeaders);
3786
3787 setHttpCertificate(httpHandle);
3788 DoworkJobsReceivedBuffer = TEST_RECEIVED_ANSWER;
3789 DoworkJobsReceivedBuffer_size[0] = strlen((const char*)DoworkJobsReceivedBuffer);
3790 DoworkJobsReceivedBuffer_counter = 0;
3791 DoworkJobs = (const xio_dowork_job*)doworkjob_o_rce;
3792 DoworkJobsOpenResult = DoworkJobsOpenResult_ReceiveHead;
3793 DoworkJobsSendResult = DoworkJobsSendResult_ReceiveHead;
3794
3795 setupAllCallBeforeOpenHTTPsequence(requestHttpHeaders, 1, false);
3796
3797 STRICT_EXPECTED_CALL(xio_send(IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_NUM_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
3798 .IgnoreAllArguments();
3799 STRICT_EXPECTED_CALL(HTTPHeaders_GetHeader(requestHttpHeaders, IGNORED_NUM_ARG, IGNORED_PTR_ARG))
3800 .IgnoreArgument(2).IgnoreArgument(3);
3801 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG)).IgnoreArgument(1);
3802 STRICT_EXPECTED_CALL(xio_send(IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_NUM_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
3803 .IgnoreAllArguments();
3804 STRICT_EXPECTED_CALL(xio_send(IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_NUM_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
3805 .IgnoreAllArguments();
3806 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG))
3807 .IgnoreArgument(1);
3808 STRICT_EXPECTED_CALL(HTTPHeaders_GetHeader(requestHttpHeaders, IGNORED_NUM_ARG, IGNORED_PTR_ARG))
3809 .IgnoreArgument(2).IgnoreArgument(3);
3810 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG)).IgnoreArgument(1);
3811 STRICT_EXPECTED_CALL(xio_send(IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_NUM_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
3812 .IgnoreAllArguments();
3813 STRICT_EXPECTED_CALL(xio_send(IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_NUM_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
3814 .IgnoreAllArguments();
3815 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG))
3816 .IgnoreArgument(1);
3817 STRICT_EXPECTED_CALL(xio_send(IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_NUM_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
3818 .IgnoreAllArguments();
3819 setupAllCallBeforeReceiveHTTPsequenceWithSuccess();
3820
3821 HTTPHeaders_GetHeader_shallReturn = HTTP_HEADERS_OK;
3822 xio_send_transmited_buffer_target = 7;
3823
3824 /// act
3825 result = HTTPAPI_ExecuteRequest(
3826 httpHandle,
3827 HTTPAPI_REQUEST_GET,
3828 TEST_EXECUTE_REQUEST_RELATIVE_PATH,
3829 requestHttpHeaders,
3830 TEST_EXECUTE_REQUEST_CONTENT,
3831 0,
3832 &statusCode,
3833 responseHttpHeaders,
3834 TestBufferHandle);
3835
3836 /// assert
3837 ASSERT_ARE_EQUAL(int, HTTPAPI_OK, result);
3838 ASSERT_ARE_EQUAL(int, 433, statusCode);
3839 ASSERT_ARE_EQUAL(char_ptr, (const char*)"", xio_send_transmited_buffer);
3840 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
3841 ASSERT_ARE_EQUAL(int, 6, currentmalloc_call);
3842
3843 /// cleanup
3844 destroyHttpObjects(&requestHttpHeaders, &responseHttpHeaders); /* currentmalloc_call -= 2 */
3845 HTTPAPI_CloseConnection(httpHandle); /* currentmalloc_call -= 3 */
3846 HTTPAPI_Deinit();
3847 }
3848
3849 /*Tests_SRS_HTTPAPI_COMPACT_21_048: [ If the statusCode is NULL, the HTTPAPI_ExecuteRequest shall report not report any status. ]*/
3850 TEST_FUNCTION(HTTPAPI_ExecuteRequest__Execute_request_no_statusCode_succeed)
3851 {
3852 /// arrange
3853 HTTPAPI_RESULT result;
3854 HTTP_HEADERS_HANDLE requestHttpHeaders;
3855 HTTP_HEADERS_HANDLE responseHttpHeaders;
3856 HTTP_HANDLE httpHandle = createHttpConnection();
3857 createHttpObjects(&requestHttpHeaders, &responseHttpHeaders);
3858
3859 setHttpCertificate(httpHandle);
3860 DoworkJobsReceivedBuffer = TEST_RECEIVED_ANSWER;
3861 DoworkJobsReceivedBuffer_size[0] = strlen((const char*)DoworkJobsReceivedBuffer);
3862 DoworkJobsReceivedBuffer_counter = 0;
3863 DoworkJobs = (const xio_dowork_job*)doworkjob_o_rce;
3864 DoworkJobsOpenResult = DoworkJobsOpenResult_ReceiveHead;
3865 DoworkJobsSendResult = DoworkJobsSendResult_ReceiveHead;
3866
3867 setupAllCallBeforeOpenHTTPsequence(requestHttpHeaders, 1, false);
3868 setupAllCallBeforeSendHTTPsequenceWithSuccess(requestHttpHeaders);
3869 setupAllCallBeforeReceiveHTTPsequenceWithSuccess();
3870
3871 HTTPHeaders_GetHeader_shallReturn = HTTP_HEADERS_OK;
3872
3873 /// act
3874 result = HTTPAPI_ExecuteRequest(
3875 httpHandle,
3876 HTTPAPI_REQUEST_GET,
3877 TEST_EXECUTE_REQUEST_RELATIVE_PATH,
3878 requestHttpHeaders,
3879 TEST_EXECUTE_REQUEST_CONTENT,
3880 TEST_EXECUTE_REQUEST_CONTENT_LENGTH,
3881 NULL,
3882 responseHttpHeaders,
3883 TestBufferHandle);
3884
3885 /// assert
3886 ASSERT_ARE_EQUAL(int, HTTPAPI_OK, result);
3887 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
3888 ASSERT_ARE_EQUAL(int, 6, currentmalloc_call);
3889
3890 /// cleanup
3891 destroyHttpObjects(&requestHttpHeaders, &responseHttpHeaders); /* currentmalloc_call -= 2 */
3892 HTTPAPI_CloseConnection(httpHandle); /* currentmalloc_call -= 3 */
3893 HTTPAPI_Deinit();
3894 }
3895
3896 /*Tests_SRS_HTTPAPI_COMPACT_21_049: [ If responseHeadersHandle is provide, the HTTPAPI_ExecuteRequest shall prepare a Response Header usign the HTTPHeaders_AddHeaderNameValuePair. ]*/
3897 TEST_FUNCTION(HTTPAPI_ExecuteRequest__Execute_request_no_responseHeadersHandle_succeed)
3898 {
3899 /// arrange
3900 unsigned int statusCode;
3901 HTTPAPI_RESULT result;
3902 HTTP_HEADERS_HANDLE requestHttpHeaders;
3903 HTTP_HEADERS_HANDLE responseHttpHeaders;
3904 HTTP_HANDLE httpHandle = createHttpConnection();
3905 createHttpObjects(&requestHttpHeaders, &responseHttpHeaders);
3906
3907 setHttpCertificate(httpHandle);
3908 DoworkJobsReceivedBuffer = TEST_RECEIVED_ANSWER;
3909 DoworkJobsReceivedBuffer_size[0] = strlen((const char*)DoworkJobsReceivedBuffer);
3910 DoworkJobsReceivedBuffer_counter = 0;
3911 DoworkJobs = (const xio_dowork_job*)doworkjob_o_re;
3912 DoworkJobsOpenResult = DoworkJobsOpenResult_ReceiveHead;
3913 DoworkJobsSendResult = DoworkJobsSendResult_ReceiveHead;
3914
3915 setupAllCallBeforeOpenHTTPsequence(requestHttpHeaders, 1, false);
3916
3917 STRICT_EXPECTED_CALL(xio_send(IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_NUM_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
3918 .IgnoreAllArguments();
3919 STRICT_EXPECTED_CALL(HTTPHeaders_GetHeader(requestHttpHeaders, IGNORED_NUM_ARG, IGNORED_PTR_ARG))
3920 .IgnoreArgument(2).IgnoreArgument(3);
3921 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG)).IgnoreArgument(1);
3922 STRICT_EXPECTED_CALL(xio_send(IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_NUM_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
3923 .IgnoreAllArguments();
3924 STRICT_EXPECTED_CALL(xio_send(IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_NUM_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
3925 .IgnoreAllArguments();
3926 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG))
3927 .IgnoreArgument(1);
3928 STRICT_EXPECTED_CALL(HTTPHeaders_GetHeader(requestHttpHeaders, IGNORED_NUM_ARG, IGNORED_PTR_ARG))
3929 .IgnoreArgument(2).IgnoreArgument(3);
3930 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG)).IgnoreArgument(1);
3931 STRICT_EXPECTED_CALL(xio_send(IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_NUM_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
3932 .IgnoreAllArguments();
3933 STRICT_EXPECTED_CALL(xio_send(IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_NUM_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
3934 .IgnoreAllArguments();
3935 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG))
3936 .IgnoreArgument(1);
3937 STRICT_EXPECTED_CALL(xio_send(IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_NUM_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
3938 .IgnoreAllArguments();
3939 STRICT_EXPECTED_CALL(xio_send(IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_NUM_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
3940 .IgnoreAllArguments();
3941 STRICT_EXPECTED_CALL(xio_dowork(IGNORED_NUM_ARG))
3942 .IgnoreArgument(1);
3943 STRICT_EXPECTED_CALL(gballoc_realloc(IGNORED_NUM_ARG, DoworkJobsReceivedBuffer_size[0])).IgnoreArgument(1);
3944
3945 STRICT_EXPECTED_CALL(xio_dowork(IGNORED_NUM_ARG))
3946 .IgnoreArgument(1);
3947 STRICT_EXPECTED_CALL(xio_dowork(IGNORED_NUM_ARG))
3948 .IgnoreArgument(1);
3949 STRICT_EXPECTED_CALL(xio_dowork(IGNORED_NUM_ARG))
3950 .IgnoreArgument(1);
3951 STRICT_EXPECTED_CALL(xio_dowork(IGNORED_NUM_ARG))
3952 .IgnoreArgument(1);
3953
3954 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG))
3955 .IgnoreArgument(1);
3956
3957 HTTPHeaders_GetHeader_shallReturn = HTTP_HEADERS_OK;
3958
3959 /// act
3960 result = HTTPAPI_ExecuteRequest(
3961 httpHandle,
3962 HTTPAPI_REQUEST_GET,
3963 TEST_EXECUTE_REQUEST_RELATIVE_PATH,
3964 requestHttpHeaders,
3965 TEST_EXECUTE_REQUEST_CONTENT,
3966 TEST_EXECUTE_REQUEST_CONTENT_LENGTH,
3967 &statusCode,
3968 NULL,
3969 TestBufferHandle);
3970
3971 /// assert
3972 ASSERT_ARE_EQUAL(int, HTTPAPI_OK, result);
3973 ASSERT_ARE_EQUAL(int, 433, statusCode);
3974 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
3975 ASSERT_ARE_EQUAL(int, 6, currentmalloc_call);
3976
3977 /// cleanup
3978 destroyHttpObjects(&requestHttpHeaders, &responseHttpHeaders); /* currentmalloc_call -= 2 */
3979 HTTPAPI_CloseConnection(httpHandle); /* currentmalloc_call -= 3 */
3980 HTTPAPI_Deinit();
3981 }
3982
3983 /*Tests_SRS_HTTPAPI_COMPACT_21_051: [ If the responseContent is NULL, the HTTPAPI_ExecuteRequest shall ignore any content in the response. ]*/
3984 TEST_FUNCTION(HTTPAPI_ExecuteRequest__Execute_request_responseContent_NULL_succeed)
3985 {
3986 /// arrange
3987 unsigned int statusCode;
3988 HTTPAPI_RESULT result;
3989 HTTP_HEADERS_HANDLE requestHttpHeaders;
3990 HTTP_HEADERS_HANDLE responseHttpHeaders;
3991 HTTP_HANDLE httpHandle = createHttpConnection();
3992 createHttpObjects(&requestHttpHeaders, &responseHttpHeaders);
3993
3994 setHttpCertificate(httpHandle);
3995 DoworkJobsReceivedBuffer = TEST_RECEIVED_ANSWER;
3996 DoworkJobsReceivedBuffer_size[0] = strlen((const char*)DoworkJobsReceivedBuffer);
3997 DoworkJobsReceivedBuffer_counter = 0;
3998 DoworkJobs = (const xio_dowork_job*)doworkjob_o_rce;
3999 DoworkJobsOpenResult = DoworkJobsOpenResult_ReceiveHead;
4000 DoworkJobsSendResult = DoworkJobsSendResult_ReceiveHead;
4001
4002 setupAllCallBeforeOpenHTTPsequence(requestHttpHeaders, 1, false);
4003 setupAllCallBeforeSendHTTPsequenceWithSuccess(requestHttpHeaders);
4004 setupAllCallBeforeReceiveHTTPsequenceWithSuccess();
4005
4006 HTTPHeaders_GetHeader_shallReturn = HTTP_HEADERS_OK;
4007
4008 /// act
4009 result = HTTPAPI_ExecuteRequest(
4010 httpHandle,
4011 HTTPAPI_REQUEST_GET,
4012 TEST_EXECUTE_REQUEST_RELATIVE_PATH,
4013 requestHttpHeaders,
4014 TEST_EXECUTE_REQUEST_CONTENT,
4015 TEST_EXECUTE_REQUEST_CONTENT_LENGTH,
4016 &statusCode,
4017 responseHttpHeaders,
4018 NULL);
4019
4020 /// assert
4021 ASSERT_ARE_EQUAL(int, HTTPAPI_OK, result);
4022 ASSERT_ARE_EQUAL(int, 433, statusCode);
4023 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
4024 ASSERT_ARE_EQUAL(int, 6, currentmalloc_call);
4025
4026 /// cleanup
4027 destroyHttpObjects(&requestHttpHeaders, &responseHttpHeaders); /* currentmalloc_call -= 2 */
4028 HTTPAPI_CloseConnection(httpHandle); /* currentmalloc_call -= 3 */
4029 HTTPAPI_Deinit();
4030 }
4031
4032 /*Tests_SRS_HTTPAPI_COMPACT_21_081: [ The HTTPAPI_ExecuteRequest shall try to read the message with the response up to 20 seconds. ]*/
4033 /*Tests_SRS_HTTPAPI_COMPACT_21_082: [ If the HTTPAPI_ExecuteRequest retries 20 seconds to receive the message without success, it shall fail and return HTTPAPI_READ_DATA_FAILED. ]*/
4034 /*Tests_SRS_HTTPAPI_COMPACT_21_083: [ The HTTPAPI_ExecuteRequest shall wait, at least, 100 milliseconds between retries. ]*/
4035 TEST_FUNCTION(HTTPAPI_ExecuteRequest__Execute_request_with_truncated_content_failed)
4036 {
4037 /// arrange
4038 int i;
4039 unsigned int statusCode;
4040 HTTPAPI_RESULT result;
4041 HTTP_HEADERS_HANDLE requestHttpHeaders;
4042 HTTP_HEADERS_HANDLE responseHttpHeaders;
4043 HTTP_HANDLE httpHandle = createHttpConnection();
4044 createHttpObjects(&requestHttpHeaders, &responseHttpHeaders);
4045 setHttpCertificate(httpHandle);
4046
4047 DoworkJobsReceivedBuffer = (const unsigned char*)"HTTP/111.222 433 555\r\ncontent-length:10\r\ntransfer-encoding:\r\n\r\n0123";
4048 DoworkJobsReceivedBuffer_size[0] = strlen((const char*)DoworkJobsReceivedBuffer);
4049 DoworkJobsReceivedBuffer_counter = 0;
4050 DoworkJobs = (const xio_dowork_job*)doworkjob_o_re;
4051 DoworkJobsOpenResult = DoworkJobsOpenResult_ReceiveHead;
4052 DoworkJobsSendResult = DoworkJobsSendResult_ReceiveHead;
4053
4054 setupAllCallBeforeOpenHTTPsequence(requestHttpHeaders, 1, false);
4055 setupAllCallBeforeSendHTTPsequenceWithSuccess(requestHttpHeaders);
4056
4057 STRICT_EXPECTED_CALL(xio_dowork(IGNORED_NUM_ARG))
4058 .IgnoreArgument(1);
4059 STRICT_EXPECTED_CALL(gballoc_realloc(IGNORED_NUM_ARG, DoworkJobsReceivedBuffer_size[0])).IgnoreArgument(1);
4060
4061 STRICT_EXPECTED_CALL(xio_dowork(IGNORED_NUM_ARG))
4062 .IgnoreArgument(1);
4063 STRICT_EXPECTED_CALL(HTTPHeaders_AddHeaderNameValuePair(IGNORED_PTR_ARG, "content-length", "10")).IgnoreArgument(1);
4064
4065 STRICT_EXPECTED_CALL(xio_dowork(IGNORED_NUM_ARG))
4066 .IgnoreArgument(1);
4067 STRICT_EXPECTED_CALL(HTTPHeaders_AddHeaderNameValuePair(IGNORED_PTR_ARG, "transfer-encoding", "")).IgnoreArgument(1);
4068
4069 STRICT_EXPECTED_CALL(xio_dowork(IGNORED_NUM_ARG))
4070 .IgnoreArgument(1);
4071 STRICT_EXPECTED_CALL(xio_dowork(IGNORED_NUM_ARG))
4072 .IgnoreArgument(1);
4073
4074 for (i = 0; i < 200; i++)
4075 {
4076 STRICT_EXPECTED_CALL(ThreadAPI_Sleep(100));
4077 STRICT_EXPECTED_CALL(xio_dowork(IGNORED_NUM_ARG))
4078 .IgnoreArgument(1);
4079 }
4080
4081 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG))
4082 .IgnoreArgument(1);
4083
4084 HTTPHeaders_GetHeader_shallReturn = HTTP_HEADERS_OK;
4085
4086 /// act
4087 result = HTTPAPI_ExecuteRequest(
4088 httpHandle,
4089 HTTPAPI_REQUEST_GET,
4090 TEST_EXECUTE_REQUEST_RELATIVE_PATH,
4091 requestHttpHeaders,
4092 TEST_EXECUTE_REQUEST_CONTENT,
4093 TEST_EXECUTE_REQUEST_CONTENT_LENGTH,
4094 &statusCode,
4095 responseHttpHeaders,
4096 TestBufferHandle);
4097
4098 /// assert
4099 ASSERT_ARE_EQUAL(int, HTTPAPI_READ_DATA_FAILED, result);
4100 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
4101 ASSERT_ARE_EQUAL(int, 6, currentmalloc_call);
4102
4103 /// cleanup
4104 destroyHttpObjects(&requestHttpHeaders, &responseHttpHeaders); /* currentmalloc_call -= 2 */
4105 HTTPAPI_CloseConnection(httpHandle); /* currentmalloc_call -= 3 */
4106 HTTPAPI_Deinit();
4107 }
4108
4109 /*Tests_SRS_HTTPAPI_COMPACT_21_081: [ The HTTPAPI_ExecuteRequest shall try to read the message with the response up to 20 seconds. ]*/
4110 /*Tests_SRS_HTTPAPI_COMPACT_21_082: [ If the HTTPAPI_ExecuteRequest retries 20 seconds to receive the message without success, it shall fail and return HTTPAPI_READ_DATA_FAILED. ]*/
4111 /*Tests_SRS_HTTPAPI_COMPACT_21_083: [ The HTTPAPI_ExecuteRequest shall wait, at least, 100 milliseconds between retries. ]*/
4112 TEST_FUNCTION(HTTPAPI_ExecuteRequest__Execute_request_with_truncated_parameter_failed)
4113 {
4114 /// arrange
4115 int i;
4116 unsigned int statusCode;
4117 HTTPAPI_RESULT result;
4118 HTTP_HEADERS_HANDLE requestHttpHeaders;
4119 HTTP_HEADERS_HANDLE responseHttpHeaders;
4120 HTTP_HANDLE httpHandle = createHttpConnection();
4121 createHttpObjects(&requestHttpHeaders, &responseHttpHeaders);
4122 setHttpCertificate(httpHandle);
4123
4124 DoworkJobsReceivedBuffer = (const unsigned char*)"HTTP/111.222 433 555\r\ncontent-length:10\r\ntransfer-enc";
4125 DoworkJobsReceivedBuffer_size[0] = strlen((const char*)DoworkJobsReceivedBuffer);
4126 DoworkJobsReceivedBuffer_counter = 0;
4127 DoworkJobs = (const xio_dowork_job*)doworkjob_o_re;
4128 DoworkJobsOpenResult = DoworkJobsOpenResult_ReceiveHead;
4129 DoworkJobsSendResult = DoworkJobsSendResult_ReceiveHead;
4130
4131 setupAllCallBeforeOpenHTTPsequence(requestHttpHeaders, 1, false);
4132 setupAllCallBeforeSendHTTPsequenceWithSuccess(requestHttpHeaders);
4133
4134 STRICT_EXPECTED_CALL(xio_dowork(IGNORED_NUM_ARG))
4135 .IgnoreArgument(1);
4136 STRICT_EXPECTED_CALL(gballoc_realloc(IGNORED_NUM_ARG, DoworkJobsReceivedBuffer_size[0])).IgnoreArgument(1);
4137
4138 STRICT_EXPECTED_CALL(xio_dowork(IGNORED_NUM_ARG))
4139 .IgnoreArgument(1);
4140 STRICT_EXPECTED_CALL(HTTPHeaders_AddHeaderNameValuePair(IGNORED_PTR_ARG, "content-length", "10")).IgnoreArgument(1);
4141
4142 STRICT_EXPECTED_CALL(xio_dowork(IGNORED_NUM_ARG))
4143 .IgnoreArgument(1);
4144 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG))
4145 .IgnoreArgument(1);
4146
4147 for (i = 0; i < 200; i++)
4148 {
4149 STRICT_EXPECTED_CALL(ThreadAPI_Sleep(100));
4150 STRICT_EXPECTED_CALL(xio_dowork(IGNORED_NUM_ARG))
4151 .IgnoreArgument(1);
4152 }
4153
4154
4155 HTTPHeaders_GetHeader_shallReturn = HTTP_HEADERS_OK;
4156
4157 /// act
4158 result = HTTPAPI_ExecuteRequest(
4159 httpHandle,
4160 HTTPAPI_REQUEST_GET,
4161 TEST_EXECUTE_REQUEST_RELATIVE_PATH,
4162 requestHttpHeaders,
4163 TEST_EXECUTE_REQUEST_CONTENT,
4164 TEST_EXECUTE_REQUEST_CONTENT_LENGTH,
4165 &statusCode,
4166 responseHttpHeaders,
4167 TestBufferHandle);
4168
4169 /// assert
4170 ASSERT_ARE_EQUAL(int, HTTPAPI_READ_DATA_FAILED, result);
4171 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
4172 ASSERT_ARE_EQUAL(int, 6, currentmalloc_call);
4173
4174 /// cleanup
4175 destroyHttpObjects(&requestHttpHeaders, &responseHttpHeaders); /* currentmalloc_call -= 2 */
4176 HTTPAPI_CloseConnection(httpHandle); /* currentmalloc_call -= 3 */
4177 HTTPAPI_Deinit();
4178 }
4179
4180 /*Tests_SRS_HTTPAPI_COMPACT_21_081: [ The HTTPAPI_ExecuteRequest shall try to read the message with the response up to 20 seconds. ]*/
4181 /*Tests_SRS_HTTPAPI_COMPACT_21_082: [ If the HTTPAPI_ExecuteRequest retries 20 seconds to receive the message without success, it shall fail and return HTTPAPI_READ_DATA_FAILED. ]*/
4182 /*Tests_SRS_HTTPAPI_COMPACT_21_083: [ The HTTPAPI_ExecuteRequest shall wait, at least, 100 milliseconds between retries. ]*/
4183 TEST_FUNCTION(HTTPAPI_ExecuteRequest__Execute_request_with_truncated_header_failed)
4184 {
4185 /// arrange
4186 int i;
4187 unsigned int statusCode;
4188 HTTPAPI_RESULT result;
4189 HTTP_HEADERS_HANDLE requestHttpHeaders;
4190 HTTP_HEADERS_HANDLE responseHttpHeaders;
4191 HTTP_HANDLE httpHandle = createHttpConnection();
4192 createHttpObjects(&requestHttpHeaders, &responseHttpHeaders);
4193 setHttpCertificate(httpHandle);
4194
4195 DoworkJobsReceivedBuffer = (const unsigned char*)"HTTP/111.222 ";
4196 DoworkJobsReceivedBuffer_size[0] = strlen((const char*)DoworkJobsReceivedBuffer);
4197 DoworkJobsReceivedBuffer_counter = 0;
4198 DoworkJobs = (const xio_dowork_job*)doworkjob_o_re;
4199 DoworkJobsOpenResult = DoworkJobsOpenResult_ReceiveHead;
4200 DoworkJobsSendResult = DoworkJobsSendResult_ReceiveHead;
4201
4202 setupAllCallBeforeOpenHTTPsequence(requestHttpHeaders, 1, false);
4203 setupAllCallBeforeSendHTTPsequenceWithSuccess(requestHttpHeaders);
4204
4205 STRICT_EXPECTED_CALL(xio_dowork(IGNORED_NUM_ARG))
4206 .IgnoreArgument(1);
4207 STRICT_EXPECTED_CALL(gballoc_realloc(IGNORED_NUM_ARG, DoworkJobsReceivedBuffer_size[0])).IgnoreArgument(1);
4208
4209 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG))
4210 .IgnoreArgument(1);
4211
4212 for (i = 0; i < 200; i++)
4213 {
4214 STRICT_EXPECTED_CALL(ThreadAPI_Sleep(100));
4215 STRICT_EXPECTED_CALL(xio_dowork(IGNORED_NUM_ARG))
4216 .IgnoreArgument(1);
4217 }
4218
4219
4220 HTTPHeaders_GetHeader_shallReturn = HTTP_HEADERS_OK;
4221
4222 /// act
4223 result = HTTPAPI_ExecuteRequest(
4224 httpHandle,
4225 HTTPAPI_REQUEST_GET,
4226 TEST_EXECUTE_REQUEST_RELATIVE_PATH,
4227 requestHttpHeaders,
4228 TEST_EXECUTE_REQUEST_CONTENT,
4229 TEST_EXECUTE_REQUEST_CONTENT_LENGTH,
4230 &statusCode,
4231 responseHttpHeaders,
4232 TestBufferHandle);
4233
4234 /// assert
4235 ASSERT_ARE_EQUAL(int, HTTPAPI_READ_DATA_FAILED, result);
4236 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
4237 ASSERT_ARE_EQUAL(int, 6, currentmalloc_call);
4238
4239 /// cleanup
4240 destroyHttpObjects(&requestHttpHeaders, &responseHttpHeaders); /* currentmalloc_call -= 2 */
4241 HTTPAPI_CloseConnection(httpHandle); /* currentmalloc_call -= 3 */
4242 HTTPAPI_Deinit();
4243 }
4244
4245 /*Tests_SRS_HTTPAPI_COMPACT_42_088: [ The message received by the HTTPAPI_ExecuteRequest should not contain http body. ]*/
4246 TEST_FUNCTION(HTTPAPI_ExecuteRequest__request_with_no_content_succeed)
4247 {
4248 /// arrange
4249 unsigned int statusCode;
4250 HTTPAPI_RESULT result;
4251 HTTP_HEADERS_HANDLE requestHttpHeaders;
4252 HTTP_HEADERS_HANDLE responseHttpHeaders;
4253 HTTP_HANDLE httpHandle = createHttpConnection();
4254 createHttpObjects(&requestHttpHeaders, &responseHttpHeaders);
4255
4256 setHttpCertificate(httpHandle);
4257 DoworkJobsReceivedBuffer = TEST_HEAD_RECEIVED_ANSWER;
4258 DoworkJobsReceivedBuffer_size[0] = strlen((const char*)DoworkJobsReceivedBuffer);
4259 DoworkJobsReceivedBuffer_counter = 0;
4260 DoworkJobs = (const xio_dowork_job*)doworkjob_o_rce;
4261 DoworkJobsOpenResult = DoworkJobsOpenResult_ReceiveHead;
4262 DoworkJobsSendResult = DoworkJobsSendResult_ReceiveHead;
4263
4264 setupAllCallBeforeOpenHTTPsequence(requestHttpHeaders, 1, false);
4265 setupAllCallBeforeSendHTTPsequenceWithSuccess(requestHttpHeaders);
4266 setupAllCallBeforeReceiveHTTPHeadsequenceWithSuccess();
4267
4268 HTTPHeaders_GetHeader_shallReturn = HTTP_HEADERS_OK;
4269 xio_send_transmited_buffer_target = 7;
4270
4271 /// act
4272 result = HTTPAPI_ExecuteRequest(
4273 httpHandle,
4274 HTTPAPI_REQUEST_HEAD,
4275 TEST_EXECUTE_REQUEST_RELATIVE_PATH,
4276 requestHttpHeaders,
4277 TEST_EXECUTE_REQUEST_CONTENT,
4278 TEST_EXECUTE_REQUEST_CONTENT_LENGTH,
4279 &statusCode,
4280 responseHttpHeaders,
4281 TestBufferHandle);
4282
4283 /// assert
4284 ASSERT_ARE_EQUAL(int, HTTPAPI_OK, result);
4285 ASSERT_ARE_EQUAL(int, 433, statusCode);
4286 ASSERT_ARE_EQUAL(char_ptr, (const char*)TEST_EXECUTE_REQUEST_CONTENT, xio_send_transmited_buffer);
4287 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
4288 ASSERT_ARE_EQUAL(int, 6, currentmalloc_call);
4289
4290 /// cleanup
4291 destroyHttpObjects(&requestHttpHeaders, &responseHttpHeaders); /* currentmalloc_call -= 2 */
4292 HTTPAPI_CloseConnection(httpHandle); /* currentmalloc_call -= 3 */
4293 HTTPAPI_Deinit();
4294 }
4295
4296 END_TEST_SUITE(httpapicompact_ut)