Debian Codegraph @

src/vendor/azure-uamqp-c/deps/azure-c-shared-utility/tests/xio_ut/xio_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 <cstdlib>
6 #else
7 #include <stdlib.h>
8 #endif
9
10 #include "azure_macro_utils/macro_utils.h"
11 #include "testrunnerswitcher.h"
12
13 static unsigned int g_fail_alloc_calls;
14
15 static void* my_gballoc_malloc(size_t size)
16 {
17 void* result = NULL;
18 if (g_fail_alloc_calls == 0)
19 {
20 result = malloc(size);
21 }
22 return result;
23 }
24
25 static void my_gballoc_free(void* ptr)
26 {
27 free(ptr);
28 }
29
30 #include "umock_c/umock_c.h"
31 #include "umock_c/umocktypes_charptr.h"
32 #include "umock_c/umock_c_negative_tests.h"
33
34 #define ENABLE_MOCKS
35 #include "azure_c_shared_utility/gballoc.h"
36 #include "azure_c_shared_utility/optionhandler.h"
37 #undef ENABLE_MOCKS
38
39 #include "azure_c_shared_utility/xio.h"
40 static CONCRETE_IO_HANDLE TEST_CONCRETE_IO_HANDLE = (CONCRETE_IO_HANDLE)0x4242;
41
42 #define ENABLE_MOCKS
43 MOCK_FUNCTION_WITH_CODE(, CONCRETE_IO_HANDLE, test_xio_create, void*, xio_create_parameters)
44 MOCK_FUNCTION_END(TEST_CONCRETE_IO_HANDLE)
45 MOCK_FUNCTION_WITH_CODE(, void, test_xio_destroy, CONCRETE_IO_HANDLE, handle)
46 MOCK_FUNCTION_END()
47 MOCK_FUNCTION_WITH_CODE(, int, test_xio_open, CONCRETE_IO_HANDLE, handle, ON_IO_OPEN_COMPLETE, on_io_open_complete, void*, on_io_open_complete_context, ON_BYTES_RECEIVED, on_bytes_received, void*, on_bytes_received_context, ON_IO_ERROR, on_io_error, void*, on_io_error_context)
48 MOCK_FUNCTION_END(0)
49 MOCK_FUNCTION_WITH_CODE(, int, test_xio_close, CONCRETE_IO_HANDLE, handle, ON_IO_CLOSE_COMPLETE, on_io_close_complete, void*, callback_context)
50 MOCK_FUNCTION_END(0)
51 MOCK_FUNCTION_WITH_CODE(, int, test_xio_send, CONCRETE_IO_HANDLE, handle, const void*, buffer, size_t, size, ON_SEND_COMPLETE, on_send_complete, void*, callback_context)
52 MOCK_FUNCTION_END(0)
53 MOCK_FUNCTION_WITH_CODE(, void, test_xio_dowork, CONCRETE_IO_HANDLE, handle)
54 MOCK_FUNCTION_END()
55 MOCK_FUNCTION_WITH_CODE(, int, test_xio_setoption, CONCRETE_IO_HANDLE, handle, const char*, optionName, const void*, value)
56 MOCK_FUNCTION_END(0)
57
58 #include "umock_c/umock_c_prod.h"
59 /*this function will clone an option given by name and value*/
60 MOCKABLE_FUNCTION(,void*, test_xio_CloneOption, const char*, name, const void*, value);
61
62 MOCKABLE_FUNCTION(, void, test_xio_DestroyOption, const char*, name, const void*, value);
63
64 MOCKABLE_FUNCTION(, OPTIONHANDLER_HANDLE, test_xio_retrieveoptions, CONCRETE_IO_HANDLE, handle);
65
66 #undef ENABLE_MOCKS
67
68 #ifdef __cplusplus
69 extern "C" {
70 #endif
71 void test_on_bytes_received(void* context, const unsigned char* buffer, size_t size)
72 {
73 (void)context;
74 (void)buffer;
75 (void)size;
76 }
77
78 void test_on_io_open_complete(void* context, IO_OPEN_RESULT open_result)
79 {
80 (void)context;
81 (void)open_result;
82 }
83
84 void test_on_io_close_complete(void* context)
85 {
86 (void)context;
87 }
88
89 void test_on_io_error(void* context)
90 {
91 (void)context;
92 }
93
94 void test_on_send_complete(void* context, IO_SEND_RESULT send_result)
95 {
96 (void)context;
97 (void)send_result;
98 }
99 #ifdef __cplusplus
100 }
101 #endif
102
103
104
105 const IO_INTERFACE_DESCRIPTION test_io_description =
106 {
107 test_xio_retrieveoptions,
108 test_xio_create,
109 test_xio_destroy,
110 test_xio_open,
111 test_xio_close,
112 test_xio_send,
113 test_xio_dowork,
114 test_xio_setoption
115 };
116
117 static TEST_MUTEX_HANDLE g_testByTest;
118
119 MU_DEFINE_ENUM_STRINGS(UMOCK_C_ERROR_CODE, UMOCK_C_ERROR_CODE_VALUES)
120
121 static void on_umock_c_error(UMOCK_C_ERROR_CODE error_code)
122 {
123 ASSERT_FAIL("umock_c reported error :%" PRI_MU_ENUM "", MU_ENUM_VALUE(UMOCK_C_ERROR_CODE, error_code));
124 }
125
126 static OPTIONHANDLER_HANDLE my_OptionHandler_Create(pfCloneOption cloneOption, pfDestroyOption destroyOption, pfSetOption setOption)
127 {
128 (void)cloneOption, (void)destroyOption, (void)setOption;
129 return (OPTIONHANDLER_HANDLE)my_gballoc_malloc(1);
130 }
131
132 static OPTIONHANDLER_HANDLE my_test_xio_retrieveoptions(CONCRETE_IO_HANDLE handle)
133 {
134 (void)(handle);
135 return (OPTIONHANDLER_HANDLE)my_gballoc_malloc(2);
136 }
137
138 static OPTIONHANDLER_RESULT my_OptionHandler_AddOption(OPTIONHANDLER_HANDLE handle, const char* name, const void* value)
139 {
140 (void)name, (void)handle, (void)value;
141 return OPTIONHANDLER_OK;
142 }
143
144 static void my_OptionHandler_Destroy(OPTIONHANDLER_HANDLE handle)
145 {
146 my_gballoc_free((void*)handle);
147 }
148
149
150 BEGIN_TEST_SUITE(xio_unittests)
151
152 TEST_SUITE_INITIALIZE(suite_init)
153 {
154 int result;
155
156 g_testByTest = TEST_MUTEX_CREATE();
157 ASSERT_IS_NOT_NULL(g_testByTest);
158
159 umock_c_init(on_umock_c_error);
160
161 result = umocktypes_charptr_register_types();
162 ASSERT_ARE_EQUAL(int, 0, result);
163
164 REGISTER_UMOCK_ALIAS_TYPE(CONCRETE_IO_HANDLE, void*);
165 REGISTER_UMOCK_ALIAS_TYPE(XIO_HANDLE, void*);
166 REGISTER_UMOCK_ALIAS_TYPE(ON_SEND_COMPLETE, void*);
167 REGISTER_UMOCK_ALIAS_TYPE(ON_IO_CLOSE_COMPLETE, void*);
168 REGISTER_UMOCK_ALIAS_TYPE(ON_IO_OPEN_COMPLETE, void*);
169 REGISTER_UMOCK_ALIAS_TYPE(ON_BYTES_RECEIVED, void*);
170 REGISTER_UMOCK_ALIAS_TYPE(ON_IO_ERROR, void*);
171
172 REGISTER_UMOCK_ALIAS_TYPE(pfCloneOption, void*);
173 REGISTER_UMOCK_ALIAS_TYPE(pfDestroyOption, void*);
174 REGISTER_UMOCK_ALIAS_TYPE(pfSetOption, void*);
175 REGISTER_UMOCK_ALIAS_TYPE(OPTIONHANDLER_HANDLE, void*);
176
177 REGISTER_GLOBAL_MOCK_HOOK(gballoc_malloc, my_gballoc_malloc);
178 REGISTER_GLOBAL_MOCK_HOOK(gballoc_free, my_gballoc_free);
179
180 REGISTER_GLOBAL_MOCK_HOOK(OptionHandler_Create, my_OptionHandler_Create);
181 REGISTER_GLOBAL_MOCK_FAIL_RETURN(OptionHandler_Create, (OPTIONHANDLER_HANDLE)NULL);
182
183 REGISTER_GLOBAL_MOCK_HOOK(test_xio_retrieveoptions, my_test_xio_retrieveoptions);
184 REGISTER_GLOBAL_MOCK_FAIL_RETURN(test_xio_retrieveoptions, (OPTIONHANDLER_HANDLE)NULL);
185
186 REGISTER_GLOBAL_MOCK_HOOK(OptionHandler_AddOption, my_OptionHandler_AddOption);
187 REGISTER_GLOBAL_MOCK_FAIL_RETURN(OptionHandler_AddOption, OPTIONHANDLER_ERROR);
188
189 REGISTER_GLOBAL_MOCK_HOOK(OptionHandler_Destroy, my_OptionHandler_Destroy);
190 }
191
192 TEST_SUITE_CLEANUP(suite_cleanup)
193 {
194 umock_c_deinit();
195
196 TEST_MUTEX_DESTROY(g_testByTest);
197 }
198
199 TEST_FUNCTION_INITIALIZE(method_init)
200 {
201 if (TEST_MUTEX_ACQUIRE(g_testByTest))
202 {
203 ASSERT_FAIL("Could not acquire test serialization mutex.");
204 }
205 g_fail_alloc_calls = 0;
206
207 umock_c_reset_all_calls();
208 }
209
210 TEST_FUNCTION_CLEANUP(method_cleanup)
211 {
212 TEST_MUTEX_RELEASE(g_testByTest);
213 }
214
215 /* xio_create */
216
217 /* Tests_SRS_XIO_01_001: [xio_create shall return on success a non-NULL handle to a new IO interface.] */
218 /* Tests_SRS_XIO_01_002: [In order to instantiate the concrete IO implementation the function concrete_io_create from the io_interface_description shall be called, passing the xio_create_parameters argument.] */
219 TEST_FUNCTION(xio_create_with_all_args_except_interface_description_NULL_succeeds)
220 {
221 // arrange
222 XIO_HANDLE result;
223 EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG));
224 STRICT_EXPECTED_CALL(test_xio_create(NULL));
225
226 // act
227 result = xio_create(&test_io_description, NULL);
228
229 // assert
230 ASSERT_IS_NOT_NULL(result);
231 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
232
233 // cleanup
234 xio_destroy(result);
235 }
236
237 /* Tests_SRS_XIO_01_002: [In order to instantiate the concrete IO implementation the function concrete_io_create from the io_interface_description shall be called, passing the xio_create_parameters argument.] */
238 TEST_FUNCTION(xio_create_passes_the_args_to_the_concrete_io_implementation)
239 {
240 // arrange
241 XIO_HANDLE result;
242 EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG));
243 STRICT_EXPECTED_CALL(test_xio_create((void*)0x4243));
244
245 // act
246 result = xio_create(&test_io_description, (void*)0x4243);
247
248 // assert
249 ASSERT_IS_NOT_NULL(result);
250 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
251
252 // cleanup
253 xio_destroy(result);
254 }
255
256 /* Tests_SRS_XIO_01_016: [If the underlying concrete_xxio_create call fails, xxio_create shall return NULL.] */
257 TEST_FUNCTION(when_concrete_xio_create_fails_then_xio_create_fails)
258 {
259 // arrange
260 XIO_HANDLE result;
261 STRICT_EXPECTED_CALL(test_xio_create(NULL))
262 .SetReturn((CONCRETE_IO_HANDLE)NULL);
263 EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG));
264 EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG));
265
266 // act
267 result = xio_create(&test_io_description, NULL);
268
269 // assert
270 ASSERT_IS_NULL(result);
271 }
272
273 /* Tests_SRS_XIO_01_003: [If the argument io_interface_description is NULL, xio_create shall return NULL.] */
274 TEST_FUNCTION(when_io_interface_description_is_NULL_then_xio_create_fails)
275 {
276 // arrange
277
278 // act
279 XIO_HANDLE result = xio_create(NULL, NULL);
280
281 // assert
282 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
283 ASSERT_IS_NULL(result);
284 }
285
286 /* Tests_SRS_XIO_01_004: [If any io_interface_description member is NULL, xio_create shall return NULL.] */
287 TEST_FUNCTION(when_concrete_xio_retrieveoptions_is_NULL_then_xio_create_fails)
288 {
289 // arrange
290 const IO_INTERFACE_DESCRIPTION io_description_null =
291 {
292 NULL,
293 test_xio_create,
294 test_xio_destroy,
295 test_xio_open,
296 test_xio_close,
297 test_xio_send,
298 test_xio_dowork,
299 test_xio_setoption
300 };
301
302 // act
303 XIO_HANDLE result = xio_create(&io_description_null, NULL);
304
305 // assert
306 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
307 ASSERT_IS_NULL(result);
308 }
309
310 /* Tests_SRS_XIO_01_004: [If any io_interface_description member is NULL, xio_create shall return NULL.] */
311 TEST_FUNCTION(when_concrete_xio_create_is_NULL_then_xio_create_fails)
312 {
313 // arrange
314 const IO_INTERFACE_DESCRIPTION io_description_null =
315 {
316 test_xio_retrieveoptions,
317 NULL,
318 test_xio_destroy,
319 test_xio_open,
320 test_xio_close,
321 test_xio_send,
322 test_xio_dowork,
323 test_xio_setoption
324 };
325
326 // act
327 XIO_HANDLE result = xio_create(&io_description_null, NULL);
328
329 // assert
330 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
331 ASSERT_IS_NULL(result);
332 }
333
334 /* Tests_SRS_XIO_01_004: [If any io_interface_description member is NULL, xio_create shall return NULL.] */
335 TEST_FUNCTION(when_concrete_xio_destroy_is_NULL_then_xio_create_fails)
336 {
337 // arrange
338 const IO_INTERFACE_DESCRIPTION io_description_null =
339 {
340 test_xio_retrieveoptions,
341 test_xio_create,
342 NULL,
343 test_xio_open,
344 test_xio_close,
345 test_xio_send,
346 test_xio_dowork,
347 test_xio_setoption
348 };
349
350 // act
351 XIO_HANDLE result = xio_create(&io_description_null, NULL);
352
353 // assert
354 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
355 ASSERT_IS_NULL(result);
356 }
357
358 /* Tests_SRS_XIO_01_004: [If any io_interface_description member is NULL, xio_create shall return NULL.] */
359 TEST_FUNCTION(when_concrete_xio_open_is_NULL_then_xio_create_fails)
360 {
361 // arrange
362 const IO_INTERFACE_DESCRIPTION io_description_null =
363 {
364 test_xio_retrieveoptions,
365 test_xio_create,
366 test_xio_destroy,
367 NULL,
368 test_xio_close,
369 test_xio_send,
370 test_xio_dowork,
371 test_xio_setoption
372 };
373
374 // act
375 XIO_HANDLE result = xio_create(&io_description_null, NULL);
376
377 // assert
378 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
379 ASSERT_IS_NULL(result);
380 }
381
382 /* Tests_SRS_XIO_01_004: [If any io_interface_description member is NULL, xio_create shall return NULL.] */
383 TEST_FUNCTION(when_concrete_xio_close_is_NULL_then_xio_create_fails)
384 {
385 // arrange
386 const IO_INTERFACE_DESCRIPTION io_description_null =
387 {
388 test_xio_retrieveoptions,
389 test_xio_create,
390 test_xio_destroy,
391 test_xio_open,
392 NULL,
393 test_xio_send,
394 test_xio_dowork,
395 test_xio_setoption
396 };
397
398 // act
399 XIO_HANDLE result = xio_create(&io_description_null, NULL);
400
401 // assert
402 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
403 ASSERT_IS_NULL(result);
404 }
405
406 /* Tests_SRS_XIO_01_004: [If any io_interface_description member is NULL, xio_create shall return NULL.] */
407 TEST_FUNCTION(when_concrete_xio_send_is_NULL_then_xio_create_fails)
408 {
409 // arrange
410 const IO_INTERFACE_DESCRIPTION io_description_null =
411 {
412 test_xio_retrieveoptions,
413 test_xio_create,
414 test_xio_destroy,
415 test_xio_open,
416 test_xio_close,
417 NULL,
418 test_xio_dowork,
419 test_xio_setoption
420 };
421
422 // act
423 XIO_HANDLE result = xio_create(&io_description_null, NULL);
424
425 // assert
426 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
427 ASSERT_IS_NULL(result);
428 }
429
430 /* Tests_SRS_XIO_01_004: [If any io_interface_description member is NULL, xio_create shall return NULL.] */
431 TEST_FUNCTION(when_concrete_xio_dowork_is_NULL_then_xio_create_fails)
432 {
433 // arrange
434 const IO_INTERFACE_DESCRIPTION io_description_null =
435 {
436 test_xio_retrieveoptions,
437 test_xio_create,
438 test_xio_destroy,
439 test_xio_open,
440 test_xio_close,
441 test_xio_send,
442 NULL,
443 test_xio_setoption
444 };
445
446 // act
447 XIO_HANDLE result = xio_create(&io_description_null, NULL);
448
449 // assert
450 ASSERT_IS_NULL(result);
451 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
452 }
453
454 /* Tests_SRS_XIO_01_004: [If any io_interface_description member is NULL, xio_create shall return NULL.] */
455 TEST_FUNCTION(when_concrete_xio_setoption_is_NULL_then_xio_create_fails)
456 {
457 // arrange
458 const IO_INTERFACE_DESCRIPTION io_description_null =
459 {
460 test_xio_retrieveoptions,
461 test_xio_create,
462 test_xio_destroy,
463 test_xio_open,
464 test_xio_close,
465 test_xio_send,
466 test_xio_dowork,
467 NULL
468 };
469
470 // act
471 XIO_HANDLE result = xio_create(&io_description_null, NULL);
472
473 // assert
474 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
475 ASSERT_IS_NULL(result);
476 }
477
478 /* Tests_SRS_XIO_01_017: [If allocating the memory needed for the IO interface fails then xio_create shall return NULL.] */
479 TEST_FUNCTION(when_allocating_memory_Fails_then_xio_create_fails)
480 {
481 // arrange
482 XIO_HANDLE result;
483 g_fail_alloc_calls = 1;
484
485 EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG))
486 .SetReturn((void*)NULL);
487
488 // act
489 result = xio_create(&test_io_description, NULL);
490
491 // assert
492 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
493 ASSERT_IS_NULL(result);
494 }
495
496 /* xio_destroy */
497
498 /* Tests_SRS_XIO_01_005: [xio_destroy shall free all resources associated with the IO handle.] */
499 /* Tests_SRS_XIO_01_006: [xio_destroy shall also call the concrete_xio_destroy function that is member of the io_interface_description argument passed to xio_create, while passing as argument to concrete_xio_destroy the result of the underlying concrete_xio_create handle that was called as part of the xio_create call.] */
500 TEST_FUNCTION(xio_destroy_calls_concrete_xio_destroy_and_frees_memory)
501 {
502 // arrange
503 XIO_HANDLE handle = xio_create(&test_io_description, NULL);
504 umock_c_reset_all_calls();
505
506 STRICT_EXPECTED_CALL(test_xio_destroy(TEST_CONCRETE_IO_HANDLE));
507 EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG));
508
509 // act
510 xio_destroy(handle);
511
512 // assert
513 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
514 }
515
516 /* Tests_SRS_XIO_01_007: [If handle is NULL, xio_destroy shall do nothing.] */
517 TEST_FUNCTION(xio_destroy_with_null_handle_does_nothing)
518 {
519 // arrange
520
521 // act
522 xio_destroy(NULL);
523
524 // assert
525 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
526 }
527
528 /* xio_open */
529
530 /* Tests_SRS_XIO_01_019: SRS_XIO_01_019: [xio_open shall call the specific concrete_xio_open function specified in xio_create, passing callback function and context arguments for three events: open completed, bytes received, and IO error.] */
531 /* Tests_SRS_XIO_01_020: [On success, xio_open shall return 0.] */
532 TEST_FUNCTION(xio_open_calls_the_underlying_concrete_xio_open_and_succeeds)
533 {
534 // arrange
535 int result;
536 XIO_HANDLE handle = xio_create(&test_io_description, NULL);
537 umock_c_reset_all_calls();
538
539 STRICT_EXPECTED_CALL(test_xio_open(TEST_CONCRETE_IO_HANDLE, test_on_io_open_complete, (void*)1, test_on_bytes_received, (void*)2, test_on_io_error, (void*)3));
540
541 // act
542 result = xio_open(handle, test_on_io_open_complete, (void*)1, test_on_bytes_received, (void*)2, test_on_io_error, (void*)3);
543
544 // assert
545 ASSERT_ARE_EQUAL(int, 0, result);
546 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
547
548 // cleanup
549 xio_destroy(handle);
550 }
551
552 /* Tests_SRS_XIO_01_021: [If handle is NULL, xio_open shall return a non-zero value.] */
553 TEST_FUNCTION(xio_open_with_NULL_handle_fails)
554 {
555 // arrange
556
557 // act
558 int result = xio_open(NULL, test_on_io_open_complete, (void*)1, test_on_bytes_received, (void*)2, test_on_io_error, (void*)3);
559
560 // assert
561 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
562 ASSERT_ARE_NOT_EQUAL(int, 0, result);
563 }
564
565 /* Tests_SRS_XIO_01_022: [If the underlying concrete_xio_open fails, xio_open shall return a non-zero value.] */
566 TEST_FUNCTION(when_the_concrete_xio_open_fails_then_xio_open_fails)
567 {
568 // arrange
569 int result;
570 XIO_HANDLE handle = xio_create(&test_io_description, NULL);
571 umock_c_reset_all_calls();
572
573 STRICT_EXPECTED_CALL(test_xio_open(TEST_CONCRETE_IO_HANDLE, test_on_io_open_complete, (void*)1, test_on_bytes_received, (void*)2, test_on_io_error, (void*)3))
574 .SetReturn(1);
575
576 // act
577 result = xio_open(handle, test_on_io_open_complete, (void*)1, test_on_bytes_received, (void*)2, test_on_io_error, (void*)3);
578
579 // assert
580 ASSERT_ARE_NOT_EQUAL(int, 0, result);
581 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
582
583 // cleanup
584 xio_destroy(handle);
585 }
586
587 /* xio_close */
588
589 /* Tests_SRS_XIO_01_023: [xio_close shall call the specific concrete_xio_close function specified in xio_create.] */
590 /* Tests_SRS_XIO_01_024: [On success, xio_close shall return 0.] */
591 TEST_FUNCTION(xio_close_calls_the_underlying_concrete_xio_close_and_succeeds)
592 {
593 // arrange
594 int result;
595 XIO_HANDLE handle = xio_create(&test_io_description, NULL);
596 umock_c_reset_all_calls();
597
598 STRICT_EXPECTED_CALL(test_xio_close(TEST_CONCRETE_IO_HANDLE, test_on_io_close_complete, (void*)0x4242));
599
600 // act
601 result = xio_close(handle, test_on_io_close_complete, (void*)0x4242);
602
603 // assert
604 ASSERT_ARE_EQUAL(int, 0, result);
605 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
606
607 // cleanup
608 xio_destroy(handle);
609 }
610
611 /* Tests_SRS_XIO_01_025: [If handle is NULL, xio_close shall return a non-zero value.] */
612 TEST_FUNCTION(xio_close_with_NULL_handle_fails)
613 {
614 // arrange
615
616 // act
617 int result = xio_close(NULL, test_on_io_close_complete, (void*)0x4242);
618
619 // assert
620 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
621 ASSERT_ARE_NOT_EQUAL(int, 0, result);
622 }
623
624 /* Tests_SRS_XIO_01_026: [If the underlying concrete_xio_close fails, xio_close shall return a non-zero value.] */
625 TEST_FUNCTION(when_the_concrete_xio_close_fails_then_xio_close_fails)
626 {
627 // arrange
628 int result;
629 XIO_HANDLE handle = xio_create(&test_io_description, NULL);
630 umock_c_reset_all_calls();
631
632 STRICT_EXPECTED_CALL(test_xio_close(TEST_CONCRETE_IO_HANDLE, test_on_io_close_complete, (void*)0x4242))
633 .SetReturn(1);
634
635 // act
636 result = xio_close(handle, test_on_io_close_complete, (void*)0x4242);
637
638 // assert
639 ASSERT_ARE_NOT_EQUAL(int, 0, result);
640 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
641
642 // cleanup
643 xio_destroy(handle);
644 }
645
646 /* xio_send */
647
648 /* Tests_SRS_XIO_01_008: [xio_send shall pass the sequence of bytes pointed to by buffer to the concrete IO implementation specified in xio_create, by calling the concrete_xio_send function while passing down the buffer and size arguments to it.] */
649 /* Tests_SRS_XIO_01_009: [On success, xio_send shall return 0.] */
650 TEST_FUNCTION(xio_send_calls_the_underlying_concrete_xio_send_and_succeeds)
651 {
652 // arrange
653 int result;
654 unsigned char send_data[] = { 0x42, 43 };
655 XIO_HANDLE handle = xio_create(&test_io_description, NULL);
656 umock_c_reset_all_calls();
657
658 STRICT_EXPECTED_CALL(test_xio_send(TEST_CONCRETE_IO_HANDLE, send_data, sizeof(send_data), test_on_send_complete, (void*)0x4242));
659
660 // act
661 result = xio_send(handle, send_data, sizeof(send_data), test_on_send_complete, (void*)0x4242);
662
663 // assert
664 ASSERT_ARE_EQUAL(int, 0, result);
665 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
666
667 // cleanup
668 xio_destroy(handle);
669 }
670
671 /* Tests_SRS_XIO_01_010: [If handle is NULL, xio_send shall return a non-zero value.] */
672 TEST_FUNCTION(xio_send_with_NULL_handle_fails)
673 {
674 // arrange
675 int result;
676 unsigned char send_data[] = { 0x42, 43 };
677 umock_c_reset_all_calls();
678
679 // act
680 result = xio_send(NULL, send_data, sizeof(send_data), test_on_send_complete, (void*)0x4242);
681
682 // assert
683 ASSERT_ARE_NOT_EQUAL(int, 0, result);
684 }
685
686 /* Tests_SRS_XIO_01_015: [If the underlying concrete_xio_send fails, xio_send shall return a non-zero value.] */
687 TEST_FUNCTION(when_the_concrete_xio_send_fails_then_xio_send_fails)
688 {
689 // arrange
690 int result;
691 unsigned char send_data[] = { 0x42, 43 };
692 XIO_HANDLE handle = xio_create(&test_io_description, NULL);
693 umock_c_reset_all_calls();
694
695 STRICT_EXPECTED_CALL(test_xio_send(TEST_CONCRETE_IO_HANDLE, send_data, sizeof(send_data), test_on_send_complete, (void*)0x4242))
696 .SetReturn(42);
697
698 // act
699 result = xio_send(handle, send_data, sizeof(send_data), test_on_send_complete, (void*)0x4242);
700
701 // assert
702 ASSERT_ARE_NOT_EQUAL(int, 0, result);
703 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
704
705 // cleanup
706 xio_destroy(handle);
707 }
708
709 /* Tests_SRS_XIO_01_011: [No error check shall be performed on buffer and size.] */
710 TEST_FUNCTION(xio_send_with_NULL_buffer_and_nonzero_length_passes_the_args_down_and_succeeds)
711 {
712 // arrange
713 int result;
714 XIO_HANDLE handle = xio_create(&test_io_description, NULL);
715 umock_c_reset_all_calls();
716
717 STRICT_EXPECTED_CALL(test_xio_send(TEST_CONCRETE_IO_HANDLE, NULL, 1, test_on_send_complete, (void*)0x4242));
718
719 // act
720 result = xio_send(handle, NULL, 1, test_on_send_complete, (void*)0x4242);
721
722 // assert
723 ASSERT_ARE_EQUAL(int, 0, result);
724 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
725
726 // cleanup
727 xio_destroy(handle);
728 }
729
730 /* Tests_SRS_XIO_01_011: [No error check shall be performed on buffer and size.] */
731 TEST_FUNCTION(xio_send_with_NULL_buffer_and_zero_length_passes_the_args_down_and_succeeds)
732 {
733 // arrange
734 int result;
735 XIO_HANDLE handle = xio_create(&test_io_description, NULL);
736 umock_c_reset_all_calls();
737
738 STRICT_EXPECTED_CALL(test_xio_send(TEST_CONCRETE_IO_HANDLE, NULL, 0, test_on_send_complete, (void*)0x4242));
739
740 // act
741 result = xio_send(handle, NULL, 0, test_on_send_complete, (void*)0x4242);
742
743 // assert
744 ASSERT_ARE_EQUAL(int, 0, result);
745 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
746
747 // cleanup
748 xio_destroy(handle);
749 }
750
751 /* Tests_SRS_XIO_01_011: [No error check shall be performed on buffer and size.] */
752 TEST_FUNCTION(xio_send_with_non_NULL_buffer_and_zero_length_passes_the_args_down_and_succeeds)
753 {
754 // arrange
755 int result;
756 unsigned char send_data[] = { 0x42, 43 };
757 XIO_HANDLE handle = xio_create(&test_io_description, NULL);
758 umock_c_reset_all_calls();
759
760 STRICT_EXPECTED_CALL(test_xio_send(TEST_CONCRETE_IO_HANDLE, send_data, 0, test_on_send_complete, (void*)0x4242));
761
762 // act
763 result = xio_send(handle, send_data, 0, test_on_send_complete, (void*)0x4242);
764
765 // assert
766 ASSERT_ARE_EQUAL(int, 0, result);
767 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
768
769 // cleanup
770 xio_destroy(handle);
771 }
772
773 /* xio_dowork */
774
775 /* Tests_SRS_XIO_01_012: [xio_dowork shall call the concrete IO implementation specified in xio_create, by calling the concrete_xio_dowork function.] */
776 TEST_FUNCTION(xio_dowork_calls_the_concrete_dowork_and_succeeds)
777 {
778 // arrange
779 XIO_HANDLE handle = xio_create(&test_io_description, NULL);
780 umock_c_reset_all_calls();
781
782 STRICT_EXPECTED_CALL(test_xio_dowork(TEST_CONCRETE_IO_HANDLE));
783
784 // act
785 xio_dowork(handle);
786
787 // assert
788 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
789
790 // cleanup
791 xio_destroy(handle);
792 }
793
794 /* Tests_SRS_XIO_01_018: [When the handle argument is NULL, xio_dowork shall do nothing.] */
795 TEST_FUNCTION(xio_dowork_with_NULL_handle_does_nothing)
796 {
797 // arrange
798
799 // act
800 xio_dowork(NULL);
801
802 // assert
803 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
804 }
805
806 /* Tests_SRS_XIO_03_030: [If the xio argument or the optionName argument is NULL, xio_setoption shall return a non-zero value.] */
807 TEST_FUNCTION(xio_setoption_with_NULL_handle_fails)
808 {
809 // arrange
810 int result;
811 const char* optionName = "TheOptionName";
812 const void* optionValue = (void*)1;
813
814 umock_c_reset_all_calls();
815
816 // act
817 result = xio_setoption(NULL, optionName, optionValue);
818
819 // assert
820 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
821 ASSERT_ARE_NOT_EQUAL(int, 0, result);
822 }
823
824 /* Tests_SRS_XIO_03_030: [If the xio argument or the optionName argument is NULL, xio_setoption shall return a non-zero value.] */
825 TEST_FUNCTION(xio_setoption_with_NULL_optionName_fails)
826 {
827 // arrange
828 const void* optionValue = (void*)1;
829 int result;
830 XIO_HANDLE handle = xio_create(&test_io_description, NULL);
831
832 umock_c_reset_all_calls();
833
834 // act
835 result = xio_setoption(handle, NULL, optionValue);
836
837 // assert
838 ASSERT_ARE_NOT_EQUAL(int, 0, result);
839 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
840
841 // cleanup
842 xio_destroy(handle);
843 }
844
845 /* Tests_SRS_XIO_003_028: [xio_setoption shall pass the optionName and value to the concrete IO implementation specified in xio_create by invoking the concrete_xio_setoption function.] */
846 /* Tests_SRS_XIO_03_029: [xio_setoption shall return 0 upon success.] */
847 TEST_FUNCTION(xio_setoption_with_valid_args_passes_the_args_down_and_succeeds)
848 {
849 // arrange
850 int result;
851 const char* optionName = "TheOptionName";
852 const void* optionValue = (void*)1;
853 XIO_HANDLE handle = xio_create(&test_io_description, NULL);
854
855 umock_c_reset_all_calls();
856
857 STRICT_EXPECTED_CALL(test_xio_setoption(TEST_CONCRETE_IO_HANDLE, optionName, optionValue));
858
859 // act
860 result = xio_setoption(handle, optionName, optionValue);
861
862 // assert
863 ASSERT_ARE_EQUAL(int, 0, result);
864 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
865
866 // cleanup
867 xio_destroy(handle);
868 }
869
870 /* Tests_SRS_XIO_03_031: [If the underlying concrete_xio_setoption fails, xio_setOption shall return a non-zero value.] */
871 TEST_FUNCTION(xio_setoption_fails_when_concrete_xio_setoption_fails)
872 {
873 // arrange
874 int result;
875 const char* optionName = "TheOptionName";
876 const void* optionValue = (void*)1;
877 XIO_HANDLE handle = xio_create(&test_io_description, NULL);
878
879 umock_c_reset_all_calls();
880
881 STRICT_EXPECTED_CALL(test_xio_setoption(TEST_CONCRETE_IO_HANDLE, optionName, optionValue))
882 .SetReturn(42);
883
884 // act
885 result = xio_setoption(handle, optionName, optionValue);
886
887 // assert
888 ASSERT_ARE_NOT_EQUAL(int, 0, result);
889 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
890
891 // cleanup
892 xio_destroy(handle);
893 }
894
895 /*Tests_SRS_XIO_02_001: [ If argument xio is NULL then xio_retrieveoptions shall fail and return NULL. ]*/
896 TEST_FUNCTION(xio_retrieveoptions_with_NULL_xio_fails)
897 {
898 ///arrange
899
900 ///act
901 OPTIONHANDLER_HANDLE h = xio_retrieveoptions(NULL);
902
903 ///assert
904 ASSERT_IS_NULL(h);
905
906 ///cleanup
907 }
908
909 /*this function exists for the purpose of sharing code between happy and unhappy paths*/
910 static void xio_retrieveoptions_inert_path(void)
911 {
912 STRICT_EXPECTED_CALL(OptionHandler_Create(IGNORED_PTR_ARG, IGNORED_PTR_ARG, (pfSetOption)xio_setoption));
913
914 STRICT_EXPECTED_CALL(test_xio_retrieveoptions(IGNORED_PTR_ARG));
915
916 STRICT_EXPECTED_CALL(OptionHandler_AddOption(IGNORED_PTR_ARG, "concreteOptions", IGNORED_PTR_ARG));
917
918 STRICT_EXPECTED_CALL(OptionHandler_Destroy(IGNORED_PTR_ARG));
919
920 }
921
922 /*Tests_SRS_XIO_02_002: [ xio_retrieveoptions shall create a OPTIONHANDLER_HANDLE by calling OptionHandler_Create passing xio_setoption as setOption argument and xio_CloneOption and xio_DestroyOption for cloneOption and destroyOption. ]*/
923 /*Tests_SRS_XIO_02_003: [ xio_retrieveoptions shall retrieve the concrete handle's options by a call to concrete_io_retrieveoptions. ]*/
924 /*Tests_SRS_XIO_02_004: [ xio_retrieveoptions shall add a hardcoded option named concreteOptions having the same content as the concrete handle's options. ]*/
925 /*Tests_SRS_XIO_02_006: [ Otherwise, xio_retrieveoptions shall succeed and return a non-NULL handle. ]*/
926 TEST_FUNCTION(xio_retrieveoptions_happypath)
927 {
928 ///arrange
929 OPTIONHANDLER_HANDLE h;
930 XIO_HANDLE x = xio_create(&test_io_description, NULL);
931 umock_c_reset_all_calls();
932
933 xio_retrieveoptions_inert_path();
934
935 ///act
936 h = xio_retrieveoptions(x);
937
938 ///assert
939 ASSERT_IS_NOT_NULL(h);
940 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
941
942 ///cleanup
943 OptionHandler_Destroy(h);
944 xio_destroy(x);
945 }
946
947 /*Tests_SRS_XIO_02_005: [ If any operation fails, then xio_retrieveoptions shall fail and return NULL. ]*/
948
949 TEST_FUNCTION(xio_retrieveoptions_unhappypaths)
950 {
951 ///arrange
952 XIO_HANDLE x;
953 size_t i;
954 int negativeTestsInitResult = umock_c_negative_tests_init();
955 ASSERT_ARE_EQUAL(int, 0, negativeTestsInitResult);
956
957 x = xio_create(&test_io_description, NULL);
958 umock_c_reset_all_calls();
959
960 xio_retrieveoptions_inert_path();
961
962 umock_c_negative_tests_snapshot();
963
964 ///act
965 for (i = 0; i < umock_c_negative_tests_call_count(); i++)
966 {
967 if (!umock_c_negative_tests_can_call_fail(i)) continue;
968
969 char temp_str[128];
970 OPTIONHANDLER_HANDLE h;
971
972 umock_c_negative_tests_reset();
973 umock_c_negative_tests_fail_call(i);
974
975 ///act
976 (void)sprintf(temp_str, "On failed call %lu", (unsigned long)i);
977
978 ///act
979 h = xio_retrieveoptions(x);
980
981 ///assert
982 ASSERT_IS_NULL(h, temp_str);
983 }
984
985 ///cleanup
986 xio_destroy(x);
987 umock_c_negative_tests_deinit();
988 }
989
990
991 END_TEST_SUITE(xio_unittests)