Debian Codegraph @

src/vendor/azure-uamqp-c/deps/azure-c-shared-utility/tests/wsio_ut/wsio_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 #include <cstddef>
7 #include <cstdint>
8 #else
9 #include <stdlib.h>
10 #include <stdbool.h>
11 #include <stddef.h>
12 #include <stdint.h>
13 #endif
14
15 #include "azure_macro_utils/macro_utils.h"
16 #include "testrunnerswitcher.h"
17 #include "umock_c/umock_c.h"
18 #include "umock_c/umocktypes_charptr.h"
19 #include "umock_c/umocktypes_bool.h"
20
21 #define ENABLE_MOCKS
22
23 #include "azure_c_shared_utility/xio.h"
24 #include "azure_c_shared_utility/crt_abstractions.h"
25 #include "azure_c_shared_utility/singlylinkedlist.h"
26 #include "azure_c_shared_utility/optionhandler.h"
27 #include "azure_c_shared_utility/uws_client.h"
28
29 static const void** list_items = NULL;
30 static size_t list_item_count = 0;
31 static const char* TEST_HOST_ADDRESS = "host_address.com";
32 static const char* TEST_RESOURCE_NAME = "/test_resource";
33 static const char* TEST_PROTOCOL = "test_proto";
34
35 static const SINGLYLINKEDLIST_HANDLE TEST_SINGLYLINKEDSINGLYLINKEDLIST_HANDLE = (SINGLYLINKEDLIST_HANDLE)0x4242;
36 static const LIST_ITEM_HANDLE TEST_LIST_ITEM_HANDLE = (LIST_ITEM_HANDLE)0x11;
37 static const UWS_CLIENT_HANDLE TEST_UWS_HANDLE = (UWS_CLIENT_HANDLE)0x4243;
38 static const XIO_HANDLE TEST_UNDERLYING_IO_HANDLE = (XIO_HANDLE)0x4244;
39 static const OPTIONHANDLER_HANDLE TEST_OPTIONHANDLER_HANDLE = (OPTIONHANDLER_HANDLE)0x4246;
40 static const OPTIONHANDLER_HANDLE TEST_UWS_CLIENT_OPTIONHANDLER_HANDLE = (OPTIONHANDLER_HANDLE)0x4247;
41 static void* TEST_UNDERLYING_IO_PARAMETERS = (void*)0x4248;
42 static const IO_INTERFACE_DESCRIPTION* TEST_UNDERLYING_IO_INTERFACE = (const IO_INTERFACE_DESCRIPTION*)0x4249;
43
44 IMPLEMENT_UMOCK_C_ENUM_TYPE(IO_OPEN_RESULT, IO_OPEN_RESULT_VALUES);
45 IMPLEMENT_UMOCK_C_ENUM_TYPE(WS_OPEN_RESULT, WS_OPEN_RESULT_VALUES);
46 IMPLEMENT_UMOCK_C_ENUM_TYPE(IO_SEND_RESULT, IO_SEND_RESULT_VALUES);
47 IMPLEMENT_UMOCK_C_ENUM_TYPE(WS_SEND_FRAME_RESULT, WS_SEND_FRAME_RESULT_VALUES);
48 IMPLEMENT_UMOCK_C_ENUM_TYPE(OPTIONHANDLER_RESULT, OPTIONHANDLER_RESULT_VALUES);
49
50 static size_t currentmalloc_call;
51 static size_t whenShallmalloc_fail;
52
53 static size_t currentcalloc_call;
54 static size_t whenShallcalloc_fail;
55
56 static void* my_gballoc_malloc(size_t size)
57 {
58 void* result;
59 currentmalloc_call++;
60 if (whenShallmalloc_fail > 0)
61 {
62 if (currentmalloc_call == whenShallmalloc_fail)
63 {
64 result = NULL;
65 }
66 else
67 {
68 result = malloc(size);
69 }
70 }
71 else
72 {
73 result = malloc(size);
74 }
75 return result;
76 }
77
78 static void* my_gballoc_calloc(size_t nmemb, size_t size)
79 {
80 void* result;
81 currentcalloc_call++;
82 if (whenShallcalloc_fail > 0)
83 {
84 if (currentcalloc_call == whenShallcalloc_fail)
85 {
86 result = NULL;
87 }
88 else
89 {
90 result = calloc(nmemb, size);
91 }
92 }
93 else
94 {
95 result = calloc(nmemb, size);
96 }
97 return result;
98 }
99
100 static void my_gballoc_free(void* ptr)
101 {
102 free(ptr);
103 }
104
105 static LIST_ITEM_HANDLE add_to_list(const void* item)
106 {
107 const void** items = (const void**)realloc((void*)list_items, (list_item_count + 1) * sizeof(item));
108 if (items != NULL)
109 {
110 list_items = items;
111 list_items[list_item_count++] = item;
112 }
113 return (LIST_ITEM_HANDLE)list_item_count;
114 }
115
116 static int singlylinkedlist_remove_result;
117
118 static int my_singlylinkedlist_remove(SINGLYLINKEDLIST_HANDLE list, LIST_ITEM_HANDLE item)
119 {
120 size_t index = (size_t)item - 1;
121 (void)list;
122 (void)memmove((void*)&list_items[index], &list_items[index + 1], sizeof(const void*) * (list_item_count - index - 1));
123 list_item_count--;
124 if (list_item_count == 0)
125 {
126 free((void*)list_items);
127 list_items = NULL;
128 }
129 return singlylinkedlist_remove_result;
130 }
131
132 static LIST_ITEM_HANDLE my_singlylinkedlist_get_head_item(SINGLYLINKEDLIST_HANDLE list)
133 {
134 LIST_ITEM_HANDLE list_item_handle = NULL;
135 (void)list;
136 if (list_item_count > 0)
137 {
138 list_item_handle = (LIST_ITEM_HANDLE)1;
139 }
140 else
141 {
142 list_item_handle = NULL;
143 }
144 return list_item_handle;
145 }
146
147 static LIST_ITEM_HANDLE my_singlylinkedlist_add(SINGLYLINKEDLIST_HANDLE list, const void* item)
148 {
149 (void)list;
150 return add_to_list(item);
151 }
152
153 static const void* my_singlylinkedlist_item_get_value(LIST_ITEM_HANDLE item_handle)
154 {
155 return (const void*)list_items[(size_t)item_handle - 1];
156 }
157
158 static LIST_ITEM_HANDLE my_singlylinkedlist_find(SINGLYLINKEDLIST_HANDLE handle, LIST_MATCH_FUNCTION match_function, const void* match_context)
159 {
160 size_t i;
161 const void* found_item = NULL;
162 (void)handle;
163 for (i = 0; i < list_item_count; i++)
164 {
165 if (match_function((LIST_ITEM_HANDLE)list_items[i], match_context))
166 {
167 found_item = list_items[i];
168 break;
169 }
170 }
171 return (LIST_ITEM_HANDLE)found_item;
172 }
173
174 int my_mallocAndStrcpy_s(char** destination, const char* source)
175 {
176 *destination = (char*)malloc(strlen(source) + 1);
177 (void)strcpy(*destination, source);
178 return 0;
179 }
180
181 static pfCloneOption g_clone_option;
182 static pfDestroyOption g_destroy_option;
183 static pfSetOption g_set_option;
184
185 static OPTIONHANDLER_HANDLE my_OptionHandler_Create(pfCloneOption cloneOption, pfDestroyOption destroyOption, pfSetOption setOption)
186 {
187 g_clone_option = cloneOption;
188 g_destroy_option = destroyOption;
189 g_set_option = setOption;
190
191 return TEST_OPTIONHANDLER_HANDLE;
192 }
193
194 #include "azure_c_shared_utility/gballoc.h"
195
196 #undef ENABLE_MOCKS
197
198 #include "azure_c_shared_utility/wsio.h"
199
200 // consumer mocks
201 MOCK_FUNCTION_WITH_CODE(, void, test_on_io_open_complete, void*, context, IO_OPEN_RESULT, io_open_result);
202 MOCK_FUNCTION_END()
203 MOCK_FUNCTION_WITH_CODE(, void, test_on_bytes_received, void*, context, const unsigned char*, buffer, size_t, size);
204 MOCK_FUNCTION_END()
205 MOCK_FUNCTION_WITH_CODE(, void, test_on_io_error, void*, context);
206 MOCK_FUNCTION_END()
207 MOCK_FUNCTION_WITH_CODE(, void, test_on_io_close_complete, void*, context);
208 MOCK_FUNCTION_END()
209 MOCK_FUNCTION_WITH_CODE(, void, test_on_send_complete, void*, context, IO_SEND_RESULT, send_result)
210 MOCK_FUNCTION_END()
211
212 static ON_WS_OPEN_COMPLETE g_on_ws_open_complete;
213 static void* g_on_ws_open_complete_context;
214 static ON_WS_SEND_FRAME_COMPLETE g_on_ws_send_frame_complete;
215 static void* g_on_ws_send_frame_complete_context;
216 static ON_WS_FRAME_RECEIVED g_on_ws_frame_received;
217 static void* g_on_ws_frame_received_context;
218 static ON_WS_PEER_CLOSED g_on_ws_peer_closed;
219 static void* g_on_ws_peer_closed_context;
220 static ON_WS_ERROR g_on_ws_error;
221 static void* g_on_ws_error_context;
222 static ON_WS_CLOSE_COMPLETE g_on_ws_close_complete;
223 static void* g_on_ws_close_complete_context;
224
225 static int my_uws_open_async(UWS_CLIENT_HANDLE uws, ON_WS_OPEN_COMPLETE on_ws_open_complete, void* on_ws_open_complete_context, ON_WS_FRAME_RECEIVED on_ws_frame_received, void* on_ws_frame_received_context, ON_WS_PEER_CLOSED on_ws_peer_closed, void* on_ws_peer_closed_context, ON_WS_ERROR on_ws_error, void* on_ws_error_context)
226 {
227 (void)uws;
228 g_on_ws_open_complete = on_ws_open_complete;
229 g_on_ws_open_complete_context = on_ws_open_complete_context;
230 g_on_ws_frame_received = on_ws_frame_received;
231 g_on_ws_frame_received_context = on_ws_frame_received_context;
232 g_on_ws_peer_closed = on_ws_peer_closed;
233 g_on_ws_peer_closed_context = on_ws_peer_closed_context;
234 g_on_ws_error = on_ws_error;
235 g_on_ws_error_context = on_ws_error_context;
236 return 0;
237 }
238
239 static int my_uws_close_async(UWS_CLIENT_HANDLE uws, ON_WS_CLOSE_COMPLETE on_ws_close_complete, void* on_ws_close_complete_context)
240 {
241 (void)uws;
242 g_on_ws_close_complete = on_ws_close_complete;
243 g_on_ws_close_complete_context = on_ws_close_complete_context;
244 return 0;
245 }
246
247 static int my_uws_send_frame_async(UWS_CLIENT_HANDLE uws, unsigned char frame_type, const unsigned char* buffer, size_t size, bool is_final, ON_WS_SEND_FRAME_COMPLETE on_ws_send_frame_complete, void* on_ws_send_frame_complete_context)
248 {
249 (void)uws;
250 (void)buffer;
251 (void)size;
252 (void)is_final;
253 (void)frame_type;
254 g_on_ws_send_frame_complete = on_ws_send_frame_complete;
255 g_on_ws_send_frame_complete_context = on_ws_send_frame_complete_context;
256 return 0;
257 }
258
259 static WSIO_CONFIG default_wsio_config;
260
261 static TEST_MUTEX_HANDLE g_testByTest;
262
263 MU_DEFINE_ENUM_STRINGS(UMOCK_C_ERROR_CODE, UMOCK_C_ERROR_CODE_VALUES)
264
265 static void on_umock_c_error(UMOCK_C_ERROR_CODE error_code)
266 {
267 ASSERT_FAIL("umock_c reported error :%" PRI_MU_ENUM "", MU_ENUM_VALUE(UMOCK_C_ERROR_CODE, error_code));
268 }
269
270 BEGIN_TEST_SUITE(wsio_ut)
271
272 TEST_SUITE_INITIALIZE(suite_init)
273 {
274 int result;
275 g_testByTest = TEST_MUTEX_CREATE();
276 ASSERT_IS_NOT_NULL(g_testByTest);
277
278 umock_c_init(on_umock_c_error);
279
280 result = umocktypes_charptr_register_types();
281 ASSERT_ARE_EQUAL(int, 0, result);
282
283 result = umocktypes_bool_register_types();
284 ASSERT_ARE_EQUAL(int, 0, result);
285
286 default_wsio_config.hostname = TEST_HOST_ADDRESS;
287 default_wsio_config.port = 443;
288 default_wsio_config.resource_name = TEST_RESOURCE_NAME;
289 default_wsio_config.protocol = TEST_PROTOCOL;
290 default_wsio_config.underlying_io_interface = TEST_UNDERLYING_IO_INTERFACE;
291 default_wsio_config.underlying_io_parameters = TEST_UNDERLYING_IO_PARAMETERS;
292
293 REGISTER_GLOBAL_MOCK_HOOK(gballoc_malloc, my_gballoc_malloc);
294 REGISTER_GLOBAL_MOCK_HOOK(gballoc_calloc, my_gballoc_calloc);
295 REGISTER_GLOBAL_MOCK_HOOK(gballoc_free, my_gballoc_free);
296 REGISTER_GLOBAL_MOCK_RETURN(singlylinkedlist_create, TEST_SINGLYLINKEDSINGLYLINKEDLIST_HANDLE);
297 REGISTER_GLOBAL_MOCK_HOOK(singlylinkedlist_remove, my_singlylinkedlist_remove);
298 REGISTER_GLOBAL_MOCK_HOOK(singlylinkedlist_get_head_item, my_singlylinkedlist_get_head_item);
299 REGISTER_GLOBAL_MOCK_HOOK(singlylinkedlist_add, my_singlylinkedlist_add);
300 REGISTER_GLOBAL_MOCK_HOOK(singlylinkedlist_item_get_value, my_singlylinkedlist_item_get_value);
301 REGISTER_GLOBAL_MOCK_HOOK(singlylinkedlist_find, my_singlylinkedlist_find);
302 REGISTER_GLOBAL_MOCK_HOOK(mallocAndStrcpy_s, my_mallocAndStrcpy_s);
303 REGISTER_GLOBAL_MOCK_HOOK(uws_client_open_async, my_uws_open_async);
304 REGISTER_GLOBAL_MOCK_HOOK(uws_client_close_async, my_uws_close_async);
305 REGISTER_GLOBAL_MOCK_HOOK(uws_client_send_frame_async, my_uws_send_frame_async);
306 REGISTER_GLOBAL_MOCK_HOOK(OptionHandler_Create, my_OptionHandler_Create);
307 REGISTER_GLOBAL_MOCK_RETURN(OptionHandler_FeedOptions, OPTIONHANDLER_OK);
308 REGISTER_GLOBAL_MOCK_RETURN(OptionHandler_AddOption, OPTIONHANDLER_OK);
309 REGISTER_GLOBAL_MOCK_RETURN(OptionHandler_Clone, TEST_OPTIONHANDLER_HANDLE);
310 REGISTER_GLOBAL_MOCK_RETURN(uws_client_create_with_io, TEST_UWS_HANDLE);
311 REGISTER_GLOBAL_MOCK_RETURN(uws_client_retrieve_options, TEST_UWS_CLIENT_OPTIONHANDLER_HANDLE);
312
313 REGISTER_TYPE(IO_OPEN_RESULT, IO_OPEN_RESULT);
314 REGISTER_TYPE(IO_SEND_RESULT, IO_SEND_RESULT);
315 REGISTER_TYPE(OPTIONHANDLER_RESULT, OPTIONHANDLER_RESULT);
316
317 REGISTER_UMOCK_ALIAS_TYPE(SINGLYLINKEDLIST_HANDLE, void*);
318 REGISTER_UMOCK_ALIAS_TYPE(LIST_ITEM_HANDLE, void*);
319 REGISTER_UMOCK_ALIAS_TYPE(XIO_HANDLE, void*);
320 REGISTER_UMOCK_ALIAS_TYPE(UWS_CLIENT_HANDLE, void*);
321 REGISTER_UMOCK_ALIAS_TYPE(ON_WS_OPEN_COMPLETE, void*);
322 REGISTER_UMOCK_ALIAS_TYPE(ON_WS_FRAME_RECEIVED, void*);
323 REGISTER_UMOCK_ALIAS_TYPE(ON_WS_ERROR, void*);
324 REGISTER_UMOCK_ALIAS_TYPE(ON_WS_CLOSE_COMPLETE, void*);
325 REGISTER_UMOCK_ALIAS_TYPE(ON_WS_SEND_FRAME_COMPLETE, void*);
326 REGISTER_UMOCK_ALIAS_TYPE(OPTIONHANDLER_HANDLE, void*);
327 REGISTER_UMOCK_ALIAS_TYPE(ON_WS_PEER_CLOSED, void*);
328 REGISTER_UMOCK_ALIAS_TYPE(pfCloneOption, void*);
329 REGISTER_UMOCK_ALIAS_TYPE(pfSetOption, void*);
330 REGISTER_UMOCK_ALIAS_TYPE(pfDestroyOption, void*);
331 }
332
333 TEST_SUITE_CLEANUP(suite_cleanup)
334 {
335 umock_c_deinit();
336
337 TEST_MUTEX_DESTROY(g_testByTest);
338 }
339
340 TEST_FUNCTION_INITIALIZE(method_init)
341 {
342 if (TEST_MUTEX_ACQUIRE(g_testByTest))
343 {
344 ASSERT_FAIL("Could not acquire test serialization mutex.");
345 }
346
347 umock_c_reset_all_calls();
348
349 currentmalloc_call = 0;
350 whenShallmalloc_fail = 0;
351 singlylinkedlist_remove_result = 0;
352 }
353
354 TEST_FUNCTION_CLEANUP(method_cleanup)
355 {
356 TEST_MUTEX_RELEASE(g_testByTest);
357 }
358
359 /* wsio_create */
360
361 /* Tests_SRS_WSIO_01_001: [wsio_create shall create an instance of wsio and return a non-NULL handle to it.]*/
362 /* Tests_SRS_WSIO_01_066: [ io_create_parameters shall be used as a WSIO_CONFIG* . ]*/
363 /* Tests_SRS_WSIO_01_070: [ The underlying uws instance shall be created by calling uws_client_create_with_io. ]*/
364 /* Tests_SRS_WSIO_01_071: [ The arguments for uws_client_create_with_io shall be: ]*/
365 /* Tests_SRS_WSIO_01_185: [ - underlying_io_interface shall be set to the underlying_io_interface field in the io_create_parameters passed to wsio_create. ]*/
366 /* Tests_SRS_WSIO_01_186: [ - underlying_io_parameters shall be set to the underlying_io_parameters field in the io_create_parameters passed to wsio_create. ]*/
367 /* Tests_SRS_WSIO_01_072: [ - hostname set to the hostname field in the io_create_parameters passed to wsio_create. ]*/
368 /* Tests_SRS_WSIO_01_130: [ - port set to the port field in the io_create_parameters passed to wsio_create. ]*/
369 /* Tests_SRS_WSIO_01_128: [ - resource_name set to the resource_name field in the io_create_parameters passed to wsio_create. ]*/
370 /* Tests_SRS_WSIO_01_129: [ - protocols shall be filled with only one structure, that shall have the protocol set to the value of the protocol field in the io_create_parameters passed to wsio_create. ]*/
371 /* Tests_SRS_WSIO_01_076: [ wsio_create shall create a pending send IO list that is to be used to queue send packets by calling singlylinkedlist_create. ]*/
372 TEST_FUNCTION(wsio_create_for_secure_connection_with_valid_args_succeeds)
373 {
374 // arrange
375 CONCRETE_IO_HANDLE wsio;
376
377 EXPECTED_CALL(gballoc_calloc(IGNORED_NUM_ARG, IGNORED_NUM_ARG));
378 STRICT_EXPECTED_CALL(uws_client_create_with_io(TEST_UNDERLYING_IO_INTERFACE, TEST_UNDERLYING_IO_PARAMETERS, TEST_HOST_ADDRESS, 443, TEST_RESOURCE_NAME, IGNORED_PTR_ARG, 1));
379 STRICT_EXPECTED_CALL(singlylinkedlist_create());
380
381 // act
382 wsio = wsio_get_interface_description()->concrete_io_create(&default_wsio_config);
383
384 // assert
385 ASSERT_IS_NOT_NULL(wsio);
386 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
387
388 // cleanup
389 wsio_get_interface_description()->concrete_io_destroy(wsio);
390 }
391
392 /* Tests_SRS_WSIO_01_065: [ If the argument io_create_parameters is NULL then wsio_create shall return NULL. ]*/
393 TEST_FUNCTION(wsio_create_with_NULL_create_arguments_fails)
394 {
395 // arrange
396
397 // act
398 CONCRETE_IO_HANDLE wsio = wsio_get_interface_description()->concrete_io_create(NULL);
399
400 // assert
401 ASSERT_IS_NULL(wsio);
402 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
403 }
404
405 /* Tests_SRS_WSIO_01_067: [ If any of the members hostname, resource_name or protocol is NULL in WSIO_CONFIG then wsio_create shall return NULL. ]*/
406 TEST_FUNCTION(wsio_create_with_NULL_hostname_field_fails)
407 {
408 // arrange
409 WSIO_CONFIG wsio_config;
410 CONCRETE_IO_HANDLE wsio;
411
412 wsio_config.hostname = NULL;
413 wsio_config.port = 443;
414 wsio_config.resource_name = TEST_RESOURCE_NAME;
415 wsio_config.protocol = TEST_PROTOCOL;
416 wsio_config.underlying_io_interface = NULL;
417 wsio_config.underlying_io_parameters = NULL;
418
419 // act
420 wsio = wsio_get_interface_description()->concrete_io_create(&wsio_config);
421
422 // assert
423 ASSERT_IS_NULL(wsio);
424 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
425 }
426
427 /* Tests_SRS_WSIO_01_067: [ If any of the members hostname, resource_name or protocol is NULL in WSIO_CONFIG then wsio_create shall return NULL. ]*/
428 TEST_FUNCTION(wsio_create_with_NULL_resource_name_field_fails)
429 {
430 // arrange
431 CONCRETE_IO_HANDLE wsio;
432 WSIO_CONFIG wsio_config;
433 wsio_config.hostname = TEST_HOST_ADDRESS;
434 wsio_config.port = 443;
435 wsio_config.resource_name = NULL;
436 wsio_config.protocol = TEST_PROTOCOL;
437 wsio_config.underlying_io_interface = NULL;
438 wsio_config.underlying_io_parameters = NULL;
439
440 // act
441 wsio = wsio_get_interface_description()->concrete_io_create(&wsio_config);
442
443 // assert
444 ASSERT_IS_NULL(wsio);
445 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
446 }
447
448 /* Tests_SRS_WSIO_01_067: [ If any of the members hostname, resource_name or protocol is NULL in WSIO_CONFIG then wsio_create shall return NULL. ]*/
449 TEST_FUNCTION(wsio_create_with_NULL_protocol_field_fails)
450 {
451 // arrange
452 WSIO_CONFIG wsio_config;
453 CONCRETE_IO_HANDLE wsio;
454
455 wsio_config.hostname = TEST_HOST_ADDRESS;
456 wsio_config.port = 443;
457 wsio_config.resource_name = TEST_RESOURCE_NAME;
458 wsio_config.protocol = NULL;
459 wsio_config.underlying_io_interface = NULL;
460 wsio_config.underlying_io_parameters = NULL;
461
462 // act
463 wsio = wsio_get_interface_description()->concrete_io_create(&wsio_config);
464
465 // assert
466 ASSERT_IS_NULL(wsio);
467 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
468 }
469
470 /* Tests_SRS_WSIO_01_068: [ If allocating memory for the new wsio instance fails then wsio_create shall return NULL. ]*/
471 TEST_FUNCTION(when_allocating_memory_fails_wsio_create_fails)
472 {
473 // arrange
474 CONCRETE_IO_HANDLE wsio;
475 EXPECTED_CALL(gballoc_calloc(IGNORED_NUM_ARG, IGNORED_NUM_ARG))
476 .SetReturn(NULL);
477
478 // act
479 wsio = wsio_get_interface_description()->concrete_io_create(&default_wsio_config);
480
481 // assert
482 ASSERT_IS_NULL(wsio);
483 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
484 }
485
486 /* Tests_SRS_WSIO_01_075: [ If uws_client_create_with_io fails, then wsio_create shall fail and return NULL. ]*/
487 TEST_FUNCTION(when_uws_create_fails_then_wsio_create_fails)
488 {
489 // arrange
490 CONCRETE_IO_HANDLE wsio;
491
492 EXPECTED_CALL(gballoc_calloc(IGNORED_NUM_ARG, IGNORED_NUM_ARG));
493 STRICT_EXPECTED_CALL(uws_client_create_with_io(TEST_UNDERLYING_IO_INTERFACE, TEST_UNDERLYING_IO_PARAMETERS, TEST_HOST_ADDRESS, 443, TEST_RESOURCE_NAME, IGNORED_PTR_ARG, 1))
494 .SetReturn(NULL);
495 EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG));
496
497 // act
498 wsio = wsio_get_interface_description()->concrete_io_create(&default_wsio_config);
499
500 // assert
501 ASSERT_IS_NULL(wsio);
502 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
503 }
504
505 /* Tests_SRS_WSIO_01_077: [ If singlylinkedlist_create fails then wsio_create shall fail and return NULL. ]*/
506 TEST_FUNCTION(when_singlylinkedlist_create_fails_then_wsio_create_fails)
507 {
508 // arrange
509 CONCRETE_IO_HANDLE wsio;
510
511 EXPECTED_CALL(gballoc_calloc(IGNORED_NUM_ARG, IGNORED_NUM_ARG));
512 STRICT_EXPECTED_CALL(uws_client_create_with_io(TEST_UNDERLYING_IO_INTERFACE, TEST_UNDERLYING_IO_PARAMETERS, TEST_HOST_ADDRESS, 443, TEST_RESOURCE_NAME, IGNORED_PTR_ARG, 1));
513 STRICT_EXPECTED_CALL(singlylinkedlist_create())
514 .SetReturn(NULL);
515 STRICT_EXPECTED_CALL(uws_client_destroy(TEST_UWS_HANDLE));
516 EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG));
517
518 // act
519 wsio = wsio_get_interface_description()->concrete_io_create(&default_wsio_config);
520
521 // assert
522 ASSERT_IS_NULL(wsio);
523 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
524 }
525
526 /* Tests_SRS_WSIO_01_071: [ The arguments for uws_client_create_with_io shall be: ]*/
527 /* Tests_SRS_WSIO_01_185: [ - underlying_io_interface shall be set to the underlying_io_interface field in the io_create_parameters passed to wsio_create. ]*/
528 /* Tests_SRS_WSIO_01_186: [ - underlying_io_parameters shall be set to the underlying_io_parameters field in the io_create_parameters passed to wsio_create. ]*/
529 /* Tests_SRS_WSIO_01_072: [ - hostname set to the hostname field in the io_create_parameters passed to wsio_create. ]*/
530 /* Tests_SRS_WSIO_01_130: [ - port set to the port field in the io_create_parameters passed to wsio_create. ]*/
531 /* Tests_SRS_WSIO_01_128: [ - resource_name set to the resource_name field in the io_create_parameters passed to wsio_create. ]*/
532 /* Tests_SRS_WSIO_01_129: [ - protocols shall be filled with only one structure, that shall have the protocol set to the value of the protocol field in the io_create_parameters passed to wsio_create. ]*/
533 TEST_FUNCTION(wsio_create_for_secure_connection_with_valid_args_succeeds_2)
534 {
535 // arrange
536 WSIO_CONFIG wsio_config;
537 CONCRETE_IO_HANDLE wsio;
538
539 wsio_config.hostname = "another.com";
540 wsio_config.port = 80;
541 wsio_config.resource_name = "haga";
542 wsio_config.protocol = "LeProtocol";
543 wsio_config.underlying_io_interface = TEST_UNDERLYING_IO_INTERFACE;
544 wsio_config.underlying_io_parameters = NULL;
545
546 EXPECTED_CALL(gballoc_calloc(IGNORED_NUM_ARG, IGNORED_NUM_ARG));
547 STRICT_EXPECTED_CALL(uws_client_create_with_io(TEST_UNDERLYING_IO_INTERFACE, NULL, "another.com", 80, "haga", IGNORED_PTR_ARG, 1));
548 STRICT_EXPECTED_CALL(singlylinkedlist_create());
549
550 // act
551 wsio = wsio_get_interface_description()->concrete_io_create(&wsio_config);
552
553 // assert
554 ASSERT_IS_NOT_NULL(wsio);
555 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
556
557 // cleanup
558 wsio_get_interface_description()->concrete_io_destroy(wsio);
559 }
560
561 /* wsio_destroy */
562
563 /* Tests_SRS_WSIO_01_078: [ wsio_destroy shall free all resources associated with the wsio instance. ]*/
564 /* Tests_SRS_WSIO_01_080: [ wsio_destroy shall destroy the uws instance created in wsio_create by calling uws_client_destroy. ]*/
565 /* Tests_SRS_WSIO_01_081: [ wsio_destroy shall free the list used to track the pending send IOs by calling singlylinkedlist_destroy. ]*/
566 TEST_FUNCTION(wsio_destroy_frees_all_resources)
567 {
568 // arrange
569 CONCRETE_IO_HANDLE wsio;
570 wsio = wsio_get_interface_description()->concrete_io_create(&default_wsio_config);
571 umock_c_reset_all_calls();
572
573 STRICT_EXPECTED_CALL(uws_client_destroy(TEST_UWS_HANDLE));
574 STRICT_EXPECTED_CALL(singlylinkedlist_destroy(TEST_SINGLYLINKEDSINGLYLINKEDLIST_HANDLE));
575 EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG));
576
577 // act
578 wsio_get_interface_description()->concrete_io_destroy(wsio);
579
580 // assert
581 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
582 }
583
584 /* Tests_SRS_WSIO_01_079: [ If ws_io is NULL, wsio_destroy shall do nothing. ]*/
585 TEST_FUNCTION(wsio_destroy_with_NULL_does_nothing)
586 {
587 // arrange
588
589 // act
590 wsio_get_interface_description()->concrete_io_destroy(NULL);
591
592 // assert
593 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
594 }
595
596 /* wsio_open */
597
598 /* Tests_SRS_WSIO_01_082: [ wsio_open shall open the underlying uws instance by calling uws_client_open_async and providing the uws handle created in wsio_create as argument. ]*/
599 /* Tests_SRS_WSIO_01_083: [ On success, wsio_open shall return 0. ]*/
600 TEST_FUNCTION(wsio_open_opens_the_underlying_uws_instance)
601 {
602 // arrange
603 CONCRETE_IO_HANDLE wsio;
604 int result;
605 wsio = wsio_get_interface_description()->concrete_io_create(&default_wsio_config);
606 umock_c_reset_all_calls();
607
608 STRICT_EXPECTED_CALL(uws_client_open_async(TEST_UWS_HANDLE, IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG));
609
610 // act
611 result = wsio_get_interface_description()->concrete_io_open(wsio, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
612
613 // assert
614 ASSERT_ARE_EQUAL(int, 0, result);
615 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
616
617 // cleanup
618 wsio_get_interface_description()->concrete_io_destroy(wsio);
619 }
620
621 /* Tests_SRS_WSIO_01_084: [ If opening the underlying uws instance fails then wsio_open shall fail and return a non-zero value. ]*/
622 TEST_FUNCTION(when_opening_the_uws_instance_fails_wsio_open_fails)
623 {
624 // arrange
625 CONCRETE_IO_HANDLE wsio;
626 int result;
627 wsio = wsio_get_interface_description()->concrete_io_create(&default_wsio_config);
628 umock_c_reset_all_calls();
629
630 STRICT_EXPECTED_CALL(uws_client_open_async(TEST_UWS_HANDLE, IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
631 .SetReturn(1);
632
633 // act
634 result = wsio_get_interface_description()->concrete_io_open(wsio, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
635
636 // assert
637 ASSERT_ARE_NOT_EQUAL(int, 0, result);
638 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
639
640 // cleanup
641 wsio_get_interface_description()->concrete_io_destroy(wsio);
642 }
643
644 /* Tests_SRS_WSIO_01_131: [ wsio_open when already OPEN or OPENING shall fail and return a non-zero value. ]*/
645 TEST_FUNCTION(wsio_open_when_already_opening_fails)
646 {
647 // arrange
648 CONCRETE_IO_HANDLE wsio;
649 int result;
650 wsio = wsio_get_interface_description()->concrete_io_create(&default_wsio_config);
651 (void)wsio_get_interface_description()->concrete_io_open(wsio, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
652 umock_c_reset_all_calls();
653
654 // act
655 result = wsio_get_interface_description()->concrete_io_open(wsio, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
656
657 // assert
658 ASSERT_ARE_NOT_EQUAL(int, 0, result);
659 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
660
661 // cleanup
662 wsio_get_interface_description()->concrete_io_destroy(wsio);
663 }
664
665 /* Tests_SRS_WSIO_01_131: [ wsio_open when already OPEN or OPENING shall fail and return a non-zero value. ]*/
666 TEST_FUNCTION(wsio_open_when_already_open_fails)
667 {
668 // arrange
669 CONCRETE_IO_HANDLE wsio;
670 int result;
671 wsio = wsio_get_interface_description()->concrete_io_create(&default_wsio_config);
672 (void)wsio_get_interface_description()->concrete_io_open(wsio, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
673 g_on_ws_open_complete(g_on_ws_open_complete_context, WS_OPEN_OK);
674 umock_c_reset_all_calls();
675
676 // act
677 result = wsio_get_interface_description()->concrete_io_open(wsio, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
678
679 // assert
680 ASSERT_ARE_NOT_EQUAL(int, 0, result);
681 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
682
683 // cleanup
684 wsio_get_interface_description()->concrete_io_destroy(wsio);
685 }
686
687 /* Tests_SRS_WSIO_01_132: [ If any of the arguments ws_io, on_io_open_complete, on_bytes_received, on_io_error is NULL, wsio_open shall fail and return a non-zero value. ]*/
688 TEST_FUNCTION(wsio_open_with_NULL_handle_fails)
689 {
690 // arrange
691 int result;
692
693 // act
694 result = wsio_get_interface_description()->concrete_io_open(NULL, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
695
696 // assert
697 ASSERT_ARE_NOT_EQUAL(int, 0, result);
698 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
699 }
700
701 /* Tests_SRS_WSIO_01_132: [ If any of the arguments ws_io, on_io_open_complete, on_bytes_received, on_io_error is NULL, wsio_open shall fail and return a non-zero value. ]*/
702 TEST_FUNCTION(wsio_open_with_NULL_on_open_complete_callback_fails)
703 {
704 // arrange
705 CONCRETE_IO_HANDLE wsio;
706 int result;
707 wsio = wsio_get_interface_description()->concrete_io_create(&default_wsio_config);
708 umock_c_reset_all_calls();
709
710 // act
711 result = wsio_get_interface_description()->concrete_io_open(wsio, NULL, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
712
713 // assert
714 ASSERT_ARE_NOT_EQUAL(int, 0, result);
715 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
716
717 // cleanup
718 wsio_get_interface_description()->concrete_io_destroy(wsio);
719 }
720
721 /* Tests_SRS_WSIO_01_132: [ If any of the arguments ws_io, on_io_open_complete, on_bytes_received, on_io_error is NULL, wsio_open shall fail and return a non-zero value. ]*/
722 TEST_FUNCTION(wsio_open_with_NULL_on_bytes_received_callback_fails)
723 {
724 // arrange
725 CONCRETE_IO_HANDLE wsio;
726 int result;
727 wsio = wsio_get_interface_description()->concrete_io_create(&default_wsio_config);
728 umock_c_reset_all_calls();
729
730 // act
731 result = wsio_get_interface_description()->concrete_io_open(wsio, test_on_io_open_complete, (void*)0x4242, NULL, (void*)0x4243, test_on_io_error, (void*)0x4244);
732
733 // assert
734 ASSERT_ARE_NOT_EQUAL(int, 0, result);
735 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
736
737 // cleanup
738 wsio_get_interface_description()->concrete_io_destroy(wsio);
739 }
740
741 /* Tests_SRS_WSIO_01_132: [ If any of the arguments ws_io, on_io_open_complete, on_bytes_received, on_io_error is NULL, wsio_open shall fail and return a non-zero value. ]*/
742 TEST_FUNCTION(wsio_open_with_NULL_on_io_error_callback_fails)
743 {
744 // arrange
745 CONCRETE_IO_HANDLE wsio;
746 int result;
747 wsio = wsio_get_interface_description()->concrete_io_create(&default_wsio_config);
748 umock_c_reset_all_calls();
749
750 // act
751 result = wsio_get_interface_description()->concrete_io_open(wsio, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, NULL, (void*)0x4244);
752
753 // assert
754 ASSERT_ARE_NOT_EQUAL(int, 0, result);
755 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
756
757 // cleanup
758 wsio_get_interface_description()->concrete_io_destroy(wsio);
759 }
760
761 /* Tests_SRS_WSIO_01_082: [ wsio_open shall open the underlying uws instance by calling uws_client_open_async and providing the uws handle created in wsio_create as argument. ]*/
762 /* Tests_SRS_WSIO_01_083: [ On success, wsio_open shall return 0. ]*/
763 TEST_FUNCTION(wsio_open_after_close_succeeds)
764 {
765 // arrange
766 CONCRETE_IO_HANDLE wsio;
767 int result;
768 wsio = wsio_get_interface_description()->concrete_io_create(&default_wsio_config);
769 (void)wsio_get_interface_description()->concrete_io_open(wsio, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
770 g_on_ws_open_complete(g_on_ws_open_complete_context, WS_OPEN_OK);
771 (void)wsio_get_interface_description()->concrete_io_close(wsio, test_on_io_close_complete, (void*)0x4245);
772 g_on_ws_close_complete(g_on_ws_close_complete_context);
773 umock_c_reset_all_calls();
774
775 STRICT_EXPECTED_CALL(uws_client_open_async(TEST_UWS_HANDLE, IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG));
776
777 // act
778 result = wsio_get_interface_description()->concrete_io_open(wsio, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
779
780 // assert
781 ASSERT_ARE_EQUAL(int, 0, result);
782 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
783
784 // cleanup
785 wsio_get_interface_description()->concrete_io_destroy(wsio);
786 }
787
788 /* wsio_close */
789
790 /* Tests_SRS_WSIO_01_085: [ wsio_close shall close the websockets IO if an open action is either pending or has completed successfully (if the IO is open). ]*/
791 /* Tests_SRS_WSIO_01_133: [ On success wsio_close shall return 0. ]*/
792 /* Tests_SRS_WSIO_01_091: [ wsio_close shall obtain all the pending IO items by repetitively querying for the head of the pending IO list and freeing that head item. ]*/
793 /* Tests_SRS_WSIO_01_087: [ wsio_close shall call uws_client_close_async while passing as argument the IO handle created in wsio_create. ]*/
794 /* Tests_SRS_WSIO_01_092: [ Obtaining the head of the pending IO list shall be done by calling singlylinkedlist_get_head_item. ]*/
795 /* Tests_SRS_WSIO_01_094: [ The callback context passed to the on_send_complete callback shall be the context given to wsio_send. ]*/
796 TEST_FUNCTION(wsio_close_when_IO_is_open_closes_the_uws)
797 {
798 // arrange
799 CONCRETE_IO_HANDLE wsio;
800 int result;
801 wsio = wsio_get_interface_description()->concrete_io_create(&default_wsio_config);
802 (void)wsio_get_interface_description()->concrete_io_open(wsio, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
803 g_on_ws_open_complete(g_on_ws_open_complete_context, WS_OPEN_OK);
804 umock_c_reset_all_calls();
805
806 STRICT_EXPECTED_CALL(uws_client_close_async(TEST_UWS_HANDLE, IGNORED_PTR_ARG, IGNORED_PTR_ARG));
807 STRICT_EXPECTED_CALL(singlylinkedlist_get_head_item(TEST_SINGLYLINKEDSINGLYLINKEDLIST_HANDLE));
808
809 // act
810 result = wsio_get_interface_description()->concrete_io_close(wsio, test_on_io_close_complete, (void*)0x4245);
811
812 // assert
813 ASSERT_ARE_EQUAL(int, 0, result);
814 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
815
816 // cleanup
817 wsio_get_interface_description()->concrete_io_destroy(wsio);
818 }
819
820 /* Tests_SRS_WSIO_01_086: [ if ws_io is NULL, wsio_close shall return a non-zero value. ]*/
821 TEST_FUNCTION(wsio_close_with_NULL_handle_fails)
822 {
823 // arrange
824 int result;
825
826 // act
827 result = wsio_get_interface_description()->concrete_io_close(NULL, test_on_io_close_complete, (void*)0x4245);
828
829 // assert
830 ASSERT_ARE_NOT_EQUAL(int, 0, result);
831 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
832 }
833
834 /* Tests_SRS_WSIO_01_088: [ wsio_close when no open action has been issued shall fail and return a non-zero value. ]*/
835 TEST_FUNCTION(when_IO_is_not_open_wsio_close_fails)
836 {
837 // arrange
838 CONCRETE_IO_HANDLE wsio;
839 int result;
840 wsio = wsio_get_interface_description()->concrete_io_create(&default_wsio_config);
841 umock_c_reset_all_calls();
842
843 // act
844 result = wsio_get_interface_description()->concrete_io_close(wsio, test_on_io_close_complete, (void*)0x4245);
845
846 // assert
847 ASSERT_ARE_NOT_EQUAL(int, 0, result);
848 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
849
850 // cleanup
851 wsio_get_interface_description()->concrete_io_destroy(wsio);
852 }
853
854 /* Tests_SRS_WSIO_01_089: [ wsio_close after a wsio_close shall fail and return a non-zero value. ]*/
855 TEST_FUNCTION(wsio_close_after_wsio_close_completed_fails)
856 {
857 // arrange
858 CONCRETE_IO_HANDLE wsio;
859 int result;
860 wsio = wsio_get_interface_description()->concrete_io_create(&default_wsio_config);
861 (void)wsio_get_interface_description()->concrete_io_open(wsio, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
862 g_on_ws_open_complete(g_on_ws_open_complete_context, WS_OPEN_OK);
863 (void)wsio_get_interface_description()->concrete_io_close(wsio, test_on_io_close_complete, (void*)0x4245);
864 g_on_ws_close_complete(g_on_ws_close_complete_context);
865 umock_c_reset_all_calls();
866
867 // act
868 result = wsio_get_interface_description()->concrete_io_close(wsio, test_on_io_close_complete, (void*)0x4245);
869
870 // assert
871 ASSERT_ARE_NOT_EQUAL(int, 0, result);
872 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
873
874 // cleanup
875 wsio_get_interface_description()->concrete_io_destroy(wsio);
876 }
877
878 /* Tests_SRS_WSIO_01_089: [ wsio_close after a wsio_close shall fail and return a non-zero value. ]*/
879 TEST_FUNCTION(wsio_close_after_wsio_close_and_in_closing_fails)
880 {
881 // arrange
882 CONCRETE_IO_HANDLE wsio;
883 int result;
884 wsio = wsio_get_interface_description()->concrete_io_create(&default_wsio_config);
885 (void)wsio_get_interface_description()->concrete_io_open(wsio, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
886 g_on_ws_open_complete(g_on_ws_open_complete_context, WS_OPEN_OK);
887 (void)wsio_get_interface_description()->concrete_io_close(wsio, test_on_io_close_complete, (void*)0x4245);
888 umock_c_reset_all_calls();
889
890 // act
891 result = wsio_get_interface_description()->concrete_io_close(wsio, test_on_io_close_complete, (void*)0x4245);
892
893 // assert
894 ASSERT_ARE_NOT_EQUAL(int, 0, result);
895 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
896
897 // cleanup
898 wsio_get_interface_description()->concrete_io_destroy(wsio);
899 }
900
901 /* Tests_SRS_WSIO_01_164: [ When uws_client_close fails, wsio_close shall fail and return a non-zero value. ]*/
902 TEST_FUNCTION(when_uws_close_fails_then_wsio_close_fails)
903 {
904 // arrange
905 CONCRETE_IO_HANDLE wsio;
906 int result;
907 wsio = wsio_get_interface_description()->concrete_io_create(&default_wsio_config);
908 (void)wsio_get_interface_description()->concrete_io_open(wsio, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
909 g_on_ws_open_complete(g_on_ws_open_complete_context, WS_OPEN_OK);
910 umock_c_reset_all_calls();
911
912 STRICT_EXPECTED_CALL(uws_client_close_async(TEST_UWS_HANDLE, IGNORED_PTR_ARG, IGNORED_PTR_ARG));
913 STRICT_EXPECTED_CALL(singlylinkedlist_get_head_item(TEST_SINGLYLINKEDSINGLYLINKEDLIST_HANDLE));
914
915 // act
916 result = wsio_get_interface_description()->concrete_io_close(wsio, test_on_io_close_complete, (void*)0x4245);
917
918 // assert
919 ASSERT_ARE_EQUAL(int, 0, result);
920 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
921
922 // cleanup
923 wsio_get_interface_description()->concrete_io_destroy(wsio);
924 }
925
926 /* Tests_SRS_WSIO_01_090: [ The argument on_io_close_complete shall be optional, if NULL is passed by the caller then no close complete callback shall be triggered. ]*/
927 TEST_FUNCTION(wsio_close_with_NULL_close_complete_callback_succeeds)
928 {
929 // arrange
930 CONCRETE_IO_HANDLE wsio;
931 int result;
932 wsio = wsio_get_interface_description()->concrete_io_create(&default_wsio_config);
933 (void)wsio_get_interface_description()->concrete_io_open(wsio, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
934 g_on_ws_open_complete(g_on_ws_open_complete_context, WS_OPEN_OK);
935 umock_c_reset_all_calls();
936
937 STRICT_EXPECTED_CALL(uws_client_close_async(TEST_UWS_HANDLE, IGNORED_PTR_ARG, IGNORED_PTR_ARG));
938 STRICT_EXPECTED_CALL(singlylinkedlist_get_head_item(TEST_SINGLYLINKEDSINGLYLINKEDLIST_HANDLE));
939
940 // act
941 result = wsio_get_interface_description()->concrete_io_close(wsio, NULL, NULL);
942
943 // assert
944 ASSERT_ARE_EQUAL(int, 0, result);
945 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
946
947 // cleanup
948 wsio_get_interface_description()->concrete_io_destroy(wsio);
949 }
950
951 /* Tests_SRS_WSIO_01_093: [ For each pending item the send complete callback shall be called with IO_SEND_CANCELLED.]*/
952 TEST_FUNCTION(wsio_close_indicates_a_pending_send_as_CANCELLED)
953 {
954 // arrange
955 CONCRETE_IO_HANDLE wsio;
956 int result;
957 unsigned char test_buffer[] = { 0x42 };
958
959 wsio = wsio_get_interface_description()->concrete_io_create(&default_wsio_config);
960 (void)wsio_get_interface_description()->concrete_io_open(wsio, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
961 g_on_ws_open_complete(g_on_ws_open_complete_context, WS_OPEN_OK);
962 (void)wsio_get_interface_description()->concrete_io_send(wsio, test_buffer, sizeof(test_buffer), test_on_send_complete, (void*)0x4343);
963 umock_c_reset_all_calls();
964
965 STRICT_EXPECTED_CALL(uws_client_close_async(TEST_UWS_HANDLE, IGNORED_PTR_ARG, IGNORED_PTR_ARG));
966 STRICT_EXPECTED_CALL(singlylinkedlist_get_head_item(TEST_SINGLYLINKEDSINGLYLINKEDLIST_HANDLE));
967 EXPECTED_CALL(singlylinkedlist_item_get_value(IGNORED_PTR_ARG));
968 STRICT_EXPECTED_CALL(singlylinkedlist_remove(TEST_SINGLYLINKEDSINGLYLINKEDLIST_HANDLE, IGNORED_PTR_ARG));
969 STRICT_EXPECTED_CALL(test_on_send_complete((void*)0x4343, IO_SEND_CANCELLED));
970 EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG));
971 STRICT_EXPECTED_CALL(singlylinkedlist_get_head_item(TEST_SINGLYLINKEDSINGLYLINKEDLIST_HANDLE));
972
973 // act
974 result = wsio_get_interface_description()->concrete_io_close(wsio, NULL, NULL);
975
976 // assert
977 ASSERT_ARE_EQUAL(int, 0, result);
978 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
979
980 // cleanup
981 wsio_get_interface_description()->concrete_io_destroy(wsio);
982 }
983
984 /* Tests_SRS_WSIO_01_093: [ For each pending item the send complete callback shall be called with IO_SEND_CANCELLED.]*/
985 TEST_FUNCTION(wsio_close_indicates_2_pending_sends_as_CANCELLED)
986 {
987 // arrange
988 CONCRETE_IO_HANDLE wsio;
989 int result;
990 unsigned char test_buffer[] = { 0x42 };
991
992 wsio = wsio_get_interface_description()->concrete_io_create(&default_wsio_config);
993 (void)wsio_get_interface_description()->concrete_io_open(wsio, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
994 g_on_ws_open_complete(g_on_ws_open_complete_context, WS_OPEN_OK);
995 (void)wsio_get_interface_description()->concrete_io_send(wsio, test_buffer, sizeof(test_buffer), test_on_send_complete, (void*)0x4343);
996 (void)wsio_get_interface_description()->concrete_io_send(wsio, test_buffer, sizeof(test_buffer), test_on_send_complete, (void*)0x4343);
997 umock_c_reset_all_calls();
998
999 STRICT_EXPECTED_CALL(uws_client_close_async(TEST_UWS_HANDLE, IGNORED_PTR_ARG, IGNORED_PTR_ARG));
1000 STRICT_EXPECTED_CALL(singlylinkedlist_get_head_item(TEST_SINGLYLINKEDSINGLYLINKEDLIST_HANDLE));
1001 EXPECTED_CALL(singlylinkedlist_item_get_value(IGNORED_PTR_ARG));
1002 STRICT_EXPECTED_CALL(singlylinkedlist_remove(TEST_SINGLYLINKEDSINGLYLINKEDLIST_HANDLE, IGNORED_PTR_ARG));
1003 STRICT_EXPECTED_CALL(test_on_send_complete((void*)0x4343, IO_SEND_CANCELLED));
1004 EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG));
1005 STRICT_EXPECTED_CALL(singlylinkedlist_get_head_item(TEST_SINGLYLINKEDSINGLYLINKEDLIST_HANDLE));
1006 EXPECTED_CALL(singlylinkedlist_item_get_value(IGNORED_PTR_ARG));
1007 STRICT_EXPECTED_CALL(singlylinkedlist_remove(TEST_SINGLYLINKEDSINGLYLINKEDLIST_HANDLE, IGNORED_PTR_ARG));
1008 STRICT_EXPECTED_CALL(test_on_send_complete((void*)0x4343, IO_SEND_CANCELLED));
1009 EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG));
1010 STRICT_EXPECTED_CALL(singlylinkedlist_get_head_item(TEST_SINGLYLINKEDSINGLYLINKEDLIST_HANDLE));
1011
1012 // act
1013 result = wsio_get_interface_description()->concrete_io_close(wsio, NULL, NULL);
1014
1015 // assert
1016 ASSERT_ARE_EQUAL(int, 0, result);
1017 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1018
1019 // cleanup
1020 wsio_get_interface_description()->concrete_io_destroy(wsio);
1021 }
1022
1023 /* wsio_send */
1024
1025 /* Tests_SRS_WSIO_01_095: [ wsio_send shall call uws_client_send_frame_async, passing the buffer and size arguments as they are: ]*/
1026 /* Tests_SRS_WSIO_01_097: [ The is_final argument shall be set to true. ]*/
1027 /* Tests_SRS_WSIO_01_098: [ On success, wsio_send shall return 0. ]*/
1028 /* Tests_SRS_WSIO_01_102: [ An entry shall be queued in the singly linked list by calling singlylinkedlist_add. ]*/
1029 /* Tests_SRS_WSIO_01_103: [ The entry shall contain the on_send_complete callback and its context. ]*/
1030 /* Tests_SRS_WSIO_01_096: [ The frame type used shall be WS_FRAME_TYPE_BINARY. ]*/
1031 TEST_FUNCTION(wsio_send_with_1_byte_calls_uws_send_frame)
1032 {
1033 // arrange
1034 CONCRETE_IO_HANDLE wsio;
1035 int result;
1036 unsigned char test_buffer[] = { 42 };
1037
1038 wsio = wsio_get_interface_description()->concrete_io_create(&default_wsio_config);
1039 (void)wsio_get_interface_description()->concrete_io_open(wsio, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
1040 g_on_ws_open_complete(g_on_ws_open_complete_context, WS_OPEN_OK);
1041 umock_c_reset_all_calls();
1042
1043 EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG));
1044 STRICT_EXPECTED_CALL(singlylinkedlist_add(TEST_SINGLYLINKEDSINGLYLINKEDLIST_HANDLE, IGNORED_PTR_ARG));
1045 STRICT_EXPECTED_CALL(uws_client_send_frame_async(TEST_UWS_HANDLE, WS_FRAME_TYPE_BINARY, IGNORED_PTR_ARG, sizeof(test_buffer), true, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
1046 .ValidateArgumentBuffer(3, test_buffer, sizeof(test_buffer));
1047
1048 // act
1049 result = wsio_get_interface_description()->concrete_io_send(wsio, test_buffer, sizeof(test_buffer), test_on_send_complete, (void*)0x4343);
1050
1051 // assert
1052 ASSERT_ARE_EQUAL(int, 0, result);
1053 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1054
1055 // cleanup
1056 wsio_get_interface_description()->concrete_io_destroy(wsio);
1057 }
1058
1059 /* Tests_SRS_WSIO_01_099: [ If the wsio is not OPEN (open has not been called or is still in progress) then wsio_send shall fail and return a non-zero value. ]*/
1060 TEST_FUNCTION(wsio_send_when_not_open_fails)
1061 {
1062 // arrange
1063 CONCRETE_IO_HANDLE wsio;
1064 int result;
1065 unsigned char test_buffer[] = { 42 };
1066
1067 wsio = wsio_get_interface_description()->concrete_io_create(&default_wsio_config);
1068 umock_c_reset_all_calls();
1069
1070 // act
1071 result = wsio_get_interface_description()->concrete_io_send(wsio, test_buffer, sizeof(test_buffer), test_on_send_complete, (void*)0x4343);
1072
1073 // assert
1074 ASSERT_ARE_NOT_EQUAL(int, 0, result);
1075 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1076
1077 // cleanup
1078 wsio_get_interface_description()->concrete_io_destroy(wsio);
1079 }
1080
1081 /* Tests_SRS_WSIO_01_099: [ If the wsio is not OPEN (open has not been called or is still in progress) then wsio_send shall fail and return a non-zero value. ]*/
1082 TEST_FUNCTION(wsio_send_when_opening_fails)
1083 {
1084 // arrange
1085 CONCRETE_IO_HANDLE wsio;
1086 int result;
1087 unsigned char test_buffer[] = { 42 };
1088
1089 wsio = wsio_get_interface_description()->concrete_io_create(&default_wsio_config);
1090 (void)wsio_get_interface_description()->concrete_io_open(wsio, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
1091 umock_c_reset_all_calls();
1092
1093 // act
1094 result = wsio_get_interface_description()->concrete_io_send(wsio, test_buffer, sizeof(test_buffer), test_on_send_complete, (void*)0x4343);
1095
1096 // assert
1097 ASSERT_ARE_NOT_EQUAL(int, 0, result);
1098 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1099
1100 // cleanup
1101 wsio_get_interface_description()->concrete_io_destroy(wsio);
1102 }
1103
1104 /* Tests_SRS_WSIO_01_099: [ If the wsio is not OPEN (open has not been called or is still in progress) then wsio_send shall fail and return a non-zero value. ]*/
1105 TEST_FUNCTION(wsio_send_after_io_is_closed_fails)
1106 {
1107 // arrange
1108 CONCRETE_IO_HANDLE wsio;
1109 int result;
1110 unsigned char test_buffer[] = { 42 };
1111
1112 wsio = wsio_get_interface_description()->concrete_io_create(&default_wsio_config);
1113 (void)wsio_get_interface_description()->concrete_io_open(wsio, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
1114 g_on_ws_open_complete(g_on_ws_open_complete_context, WS_OPEN_OK);
1115 (void)wsio_get_interface_description()->concrete_io_close(wsio, test_on_io_close_complete, (void*)0x4245);
1116 umock_c_reset_all_calls();
1117
1118 // act
1119 result = wsio_get_interface_description()->concrete_io_send(wsio, test_buffer, sizeof(test_buffer), test_on_send_complete, (void*)0x4343);
1120
1121 // assert
1122 ASSERT_ARE_NOT_EQUAL(int, 0, result);
1123 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1124
1125 // cleanup
1126 wsio_get_interface_description()->concrete_io_destroy(wsio);
1127 }
1128
1129 /* Tests_SRS_WSIO_01_100: [ If any of the arguments ws_io or buffer are NULL, wsio_send shall fail and return a non-zero value. ]*/
1130 TEST_FUNCTION(wsio_send_with_NULL_wsio_fails)
1131 {
1132 // arrange
1133 int result;
1134 unsigned char test_buffer[] = { 42 };
1135
1136 // act
1137 result = wsio_get_interface_description()->concrete_io_send(NULL, test_buffer, sizeof(test_buffer), test_on_send_complete, (void*)0x4343);
1138
1139 // assert
1140 ASSERT_ARE_NOT_EQUAL(int, 0, result);
1141 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1142 }
1143
1144 /* Tests_SRS_WSIO_01_100: [ If any of the arguments ws_io or buffer are NULL, wsio_send shall fail and return a non-zero value. ]*/
1145 TEST_FUNCTION(wsio_send_with_NULL_buffer_fails)
1146 {
1147 // arrange
1148 CONCRETE_IO_HANDLE wsio;
1149 int result;
1150
1151 wsio = wsio_get_interface_description()->concrete_io_create(&default_wsio_config);
1152 (void)wsio_get_interface_description()->concrete_io_open(wsio, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
1153 g_on_ws_open_complete(g_on_ws_open_complete_context, WS_OPEN_OK);
1154 (void)wsio_get_interface_description()->concrete_io_close(wsio, test_on_io_close_complete, (void*)0x4245);
1155 umock_c_reset_all_calls();
1156
1157 // act
1158 result = wsio_get_interface_description()->concrete_io_send(wsio, NULL, 1, test_on_send_complete, (void*)0x4343);
1159
1160 // assert
1161 ASSERT_ARE_NOT_EQUAL(int, 0, result);
1162 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1163
1164 // cleanup
1165 wsio_get_interface_description()->concrete_io_destroy(wsio);
1166 }
1167
1168 /* Tests_SRS_WSIO_01_101: [ If size is zero then wsio_send shall fail and return a non-zero value. ]*/
1169 TEST_FUNCTION(wsio_send_with_zero_size_fails)
1170 {
1171 // arrange
1172 CONCRETE_IO_HANDLE wsio;
1173 int result;
1174 unsigned char test_buffer[] = { 42 };
1175
1176 wsio = wsio_get_interface_description()->concrete_io_create(&default_wsio_config);
1177 (void)wsio_get_interface_description()->concrete_io_open(wsio, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
1178 g_on_ws_open_complete(g_on_ws_open_complete_context, WS_OPEN_OK);
1179 (void)wsio_get_interface_description()->concrete_io_close(wsio, test_on_io_close_complete, (void*)0x4245);
1180 umock_c_reset_all_calls();
1181
1182 // act
1183 result = wsio_get_interface_description()->concrete_io_send(wsio, test_buffer, 0, test_on_send_complete, (void*)0x4343);
1184
1185 // assert
1186 ASSERT_ARE_NOT_EQUAL(int, 0, result);
1187 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1188
1189 // cleanup
1190 wsio_get_interface_description()->concrete_io_destroy(wsio);
1191 }
1192
1193 /* Tests_SRS_WSIO_01_134: [ If allocating memory for the pending IO data fails, wsio_send shall fail and return a non-zero value. ]*/
1194 TEST_FUNCTION(when_allocating_memory_for_the_pending_send_fails_wsio_send_fails)
1195 {
1196 // arrange
1197 CONCRETE_IO_HANDLE wsio;
1198 int result;
1199 unsigned char test_buffer[] = { 42 };
1200
1201 wsio = wsio_get_interface_description()->concrete_io_create(&default_wsio_config);
1202 (void)wsio_get_interface_description()->concrete_io_open(wsio, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
1203 g_on_ws_open_complete(g_on_ws_open_complete_context, WS_OPEN_OK);
1204 umock_c_reset_all_calls();
1205
1206 EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG))
1207 .SetReturn(NULL);
1208
1209 // act
1210 result = wsio_get_interface_description()->concrete_io_send(wsio, test_buffer, sizeof(test_buffer), test_on_send_complete, (void*)0x4343);
1211
1212 // assert
1213 ASSERT_ARE_NOT_EQUAL(int, 0, result);
1214 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1215
1216 // cleanup
1217 wsio_get_interface_description()->concrete_io_destroy(wsio);
1218 }
1219
1220 /* Tests_SRS_WSIO_01_104: [ If singlylinkedlist_add fails, wsio_send shall fail and return a non-zero value. ]*/
1221 TEST_FUNCTION(when_adding_the_pending_item_to_the_list_fails_wsio_send_fails)
1222 {
1223 // arrange
1224 CONCRETE_IO_HANDLE wsio;
1225 int result;
1226 unsigned char test_buffer[] = { 42 };
1227
1228 wsio = wsio_get_interface_description()->concrete_io_create(&default_wsio_config);
1229 (void)wsio_get_interface_description()->concrete_io_open(wsio, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
1230 g_on_ws_open_complete(g_on_ws_open_complete_context, WS_OPEN_OK);
1231 umock_c_reset_all_calls();
1232
1233 EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG));
1234 STRICT_EXPECTED_CALL(singlylinkedlist_add(TEST_SINGLYLINKEDSINGLYLINKEDLIST_HANDLE, IGNORED_PTR_ARG))
1235 .SetReturn(NULL);
1236 EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG));
1237
1238 // act
1239 result = wsio_get_interface_description()->concrete_io_send(wsio, test_buffer, sizeof(test_buffer), test_on_send_complete, (void*)0x4343);
1240
1241 // assert
1242 ASSERT_ARE_NOT_EQUAL(int, 0, result);
1243 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1244
1245 // cleanup
1246 wsio_get_interface_description()->concrete_io_destroy(wsio);
1247 }
1248
1249 /* Tests_SRS_WSIO_01_105: [ The argument on_send_complete shall be optional, if NULL is passed by the caller then no send complete callback shall be triggered. ]*/
1250 TEST_FUNCTION(wsio_send_with_NULL_send_complete_callback_succeeds)
1251 {
1252 // arrange
1253 CONCRETE_IO_HANDLE wsio;
1254 int result;
1255 unsigned char test_buffer[] = { 42 };
1256
1257 wsio = wsio_get_interface_description()->concrete_io_create(&default_wsio_config);
1258 (void)wsio_get_interface_description()->concrete_io_open(wsio, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
1259 g_on_ws_open_complete(g_on_ws_open_complete_context, WS_OPEN_OK);
1260 umock_c_reset_all_calls();
1261
1262 EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG));
1263 STRICT_EXPECTED_CALL(singlylinkedlist_add(TEST_SINGLYLINKEDSINGLYLINKEDLIST_HANDLE, IGNORED_PTR_ARG));
1264 STRICT_EXPECTED_CALL(uws_client_send_frame_async(TEST_UWS_HANDLE, WS_FRAME_TYPE_BINARY, IGNORED_PTR_ARG, sizeof(test_buffer), true, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
1265 .ValidateArgumentBuffer(3, test_buffer, sizeof(test_buffer));
1266
1267 // act
1268 result = wsio_get_interface_description()->concrete_io_send(wsio, test_buffer, sizeof(test_buffer), NULL, (void*)0x4343);
1269
1270 // assert
1271 ASSERT_ARE_EQUAL(int, 0, result);
1272 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1273
1274 // cleanup
1275 wsio_get_interface_description()->concrete_io_destroy(wsio);
1276 }
1277
1278 /* wsio_dowork */
1279
1280 /* Tests_SRS_WSIO_01_106: [ wsio_dowork shall call uws_client_dowork with the uws handle created in wsio_create. ]*/
1281 TEST_FUNCTION(wsio_dowork_calls_the_underlying_uws_dowork)
1282 {
1283 // arrange
1284 CONCRETE_IO_HANDLE wsio;
1285
1286 wsio = wsio_get_interface_description()->concrete_io_create(&default_wsio_config);
1287 (void)wsio_get_interface_description()->concrete_io_open(wsio, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
1288 g_on_ws_open_complete(g_on_ws_open_complete_context, WS_OPEN_OK);
1289 umock_c_reset_all_calls();
1290
1291 STRICT_EXPECTED_CALL(uws_client_dowork(TEST_UWS_HANDLE));
1292
1293 // act
1294 wsio_get_interface_description()->concrete_io_dowork(wsio);
1295
1296 // assert
1297 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1298
1299 // cleanup
1300 wsio_get_interface_description()->concrete_io_destroy(wsio);
1301 }
1302
1303 /* Tests_SRS_WSIO_01_107: [ If the ws_io argument is NULL, wsio_dowork shall do nothing. ]*/
1304 TEST_FUNCTION(wsio_dowork_with_NULL_handle_does_nothing)
1305 {
1306 // arrange
1307
1308 // act
1309 wsio_get_interface_description()->concrete_io_dowork(NULL);
1310
1311 // assert
1312 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1313 }
1314
1315 /* Tests_SRS_WSIO_01_108: [ If the IO is not yet open, wsio_dowork shall do nothing. ]*/
1316 TEST_FUNCTION(wsio_dowork_when_not_open_dows_nothing)
1317 {
1318 // arrange
1319 CONCRETE_IO_HANDLE wsio;
1320
1321 wsio = wsio_get_interface_description()->concrete_io_create(&default_wsio_config);
1322 umock_c_reset_all_calls();
1323
1324 // act
1325 wsio_get_interface_description()->concrete_io_dowork(wsio);
1326
1327 // assert
1328 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1329
1330 // cleanup
1331 wsio_get_interface_description()->concrete_io_destroy(wsio);
1332 }
1333
1334 /* Tests_SRS_WSIO_01_108: [ If the IO is not yet open, wsio_dowork shall do nothing. ]*/
1335 TEST_FUNCTION(wsio_dowork_when_opening_calls_uws_dowork)
1336 {
1337 // arrange
1338 CONCRETE_IO_HANDLE wsio;
1339
1340 wsio = wsio_get_interface_description()->concrete_io_create(&default_wsio_config);
1341 (void)wsio_get_interface_description()->concrete_io_open(wsio, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
1342 umock_c_reset_all_calls();
1343
1344 STRICT_EXPECTED_CALL(uws_client_dowork(TEST_UWS_HANDLE));
1345
1346 // act
1347 wsio_get_interface_description()->concrete_io_dowork(wsio);
1348
1349 // assert
1350 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1351
1352 // cleanup
1353 wsio_get_interface_description()->concrete_io_destroy(wsio);
1354 }
1355
1356 /* Tests_SRS_WSIO_01_108: [ If the IO is not yet open, wsio_dowork shall do nothing. ]*/
1357 TEST_FUNCTION(wsio_dowork_after_close_does_nothing)
1358 {
1359 // arrange
1360 CONCRETE_IO_HANDLE wsio;
1361
1362 wsio = wsio_get_interface_description()->concrete_io_create(&default_wsio_config);
1363 (void)wsio_get_interface_description()->concrete_io_open(wsio, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
1364 g_on_ws_open_complete(g_on_ws_open_complete_context, WS_OPEN_OK);
1365 (void)wsio_get_interface_description()->concrete_io_close(wsio, test_on_io_close_complete, (void*)0x4245);
1366 g_on_ws_close_complete(g_on_ws_close_complete_context);
1367 umock_c_reset_all_calls();
1368
1369 // act
1370 wsio_get_interface_description()->concrete_io_dowork(wsio);
1371
1372 // assert
1373 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1374
1375 // cleanup
1376 wsio_get_interface_description()->concrete_io_destroy(wsio);
1377 }
1378
1379 /* Tests_SRS_WSIO_01_108: [ If the IO is not yet open, wsio_dowork shall do nothing. ]*/
1380 TEST_FUNCTION(wsio_dowork_in_closing_calls_uws_do_work)
1381 {
1382 // arrange
1383 CONCRETE_IO_HANDLE wsio;
1384
1385 wsio = wsio_get_interface_description()->concrete_io_create(&default_wsio_config);
1386 (void)wsio_get_interface_description()->concrete_io_open(wsio, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
1387 g_on_ws_open_complete(g_on_ws_open_complete_context, WS_OPEN_OK);
1388 (void)wsio_get_interface_description()->concrete_io_close(wsio, test_on_io_close_complete, (void*)0x4245);
1389 umock_c_reset_all_calls();
1390
1391 // act
1392 wsio_get_interface_description()->concrete_io_dowork(wsio);
1393
1394 // assert
1395 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1396
1397 // cleanup
1398 wsio_get_interface_description()->concrete_io_destroy(wsio);
1399 }
1400
1401 /* on_ws_error */
1402
1403 /* Tests_SRS_WSIO_01_121: [ When on_underlying_ws_error is called while the IO is OPEN the wsio instance shall be set to ERROR and an error shall be indicated via the on_io_error callback passed to wsio_open. ]*/
1404 /* Tests_SRS_WSIO_01_123: [ When calling on_io_error, the on_io_error_context argument given in wsio_open shall be passed to the callback on_io_error. ]*/
1405 TEST_FUNCTION(when_on_underlying_ws_error_is_called_in_open_the_error_is_reported_up)
1406 {
1407 // arrange
1408 CONCRETE_IO_HANDLE wsio;
1409
1410 wsio = wsio_get_interface_description()->concrete_io_create(&default_wsio_config);
1411 (void)wsio_get_interface_description()->concrete_io_open(wsio, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
1412 g_on_ws_open_complete(g_on_ws_open_complete_context, WS_OPEN_OK);
1413 umock_c_reset_all_calls();
1414
1415 STRICT_EXPECTED_CALL(test_on_io_error((void*)0x4244));
1416
1417 // act
1418 g_on_ws_error(g_on_ws_error_context, WS_ERROR_BAD_FRAME_RECEIVED);
1419
1420 // assert
1421 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1422
1423 // cleanup
1424 wsio_get_interface_description()->concrete_io_destroy(wsio);
1425 }
1426
1427 /* Tests_SRS_WSIO_01_135: [ When on_underlying_ws_error is called with a NULL context, it shall do nothing. ]*/
1428 TEST_FUNCTION(when_on_underlying_ws_error_with_NULL_context_does_nothing)
1429 {
1430 // arrange
1431 CONCRETE_IO_HANDLE wsio;
1432
1433 wsio = wsio_get_interface_description()->concrete_io_create(&default_wsio_config);
1434 (void)wsio_get_interface_description()->concrete_io_open(wsio, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
1435 g_on_ws_open_complete(g_on_ws_open_complete_context, WS_OPEN_OK);
1436 umock_c_reset_all_calls();
1437
1438 // act
1439 g_on_ws_error(NULL, WS_ERROR_BAD_FRAME_RECEIVED);
1440
1441 // assert
1442 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1443
1444 // cleanup
1445 wsio_get_interface_description()->concrete_io_destroy(wsio);
1446 }
1447
1448 /* Tests_SRS_WSIO_01_122: [ When on_underlying_ws_error is called while the IO is OPENING, the on_io_open_complete callback passed to wsio_open shall be called with IO_OPEN_ERROR. ]*/
1449 TEST_FUNCTION(when_on_underlying_ws_error_is_called_while_OPENING_calls_open_complete_with_ERROR)
1450 {
1451 // arrange
1452 CONCRETE_IO_HANDLE wsio;
1453
1454 wsio = wsio_get_interface_description()->concrete_io_create(&default_wsio_config);
1455 (void)wsio_get_interface_description()->concrete_io_open(wsio, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
1456 umock_c_reset_all_calls();
1457
1458 STRICT_EXPECTED_CALL(test_on_io_open_complete((void*)0x4242, IO_OPEN_ERROR));
1459
1460 // act
1461 g_on_ws_error(g_on_ws_error_context, WS_ERROR_BAD_FRAME_RECEIVED);
1462
1463 // assert
1464 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1465
1466 // cleanup
1467 wsio_get_interface_description()->concrete_io_destroy(wsio);
1468 }
1469
1470 /* on_underlying_ws_open_complete */
1471
1472 /* Tests_SRS_WSIO_01_136: [ When on_underlying_ws_open_complete is called with WS_OPEN_OK while the IO is opening, the callback on_io_open_complete shall be called with IO_OPEN_OK. ]*/
1473 TEST_FUNCTION(when_on_underlying_ws_open_complete_is_called_with_OK_while_OPENING_the_io_open_complete_callback_is_called_with_OPEN_OK)
1474 {
1475 // arrange
1476 CONCRETE_IO_HANDLE wsio;
1477
1478 wsio = wsio_get_interface_description()->concrete_io_create(&default_wsio_config);
1479 (void)wsio_get_interface_description()->concrete_io_open(wsio, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
1480 umock_c_reset_all_calls();
1481
1482 STRICT_EXPECTED_CALL(test_on_io_open_complete((void*)0x4242, IO_OPEN_OK));
1483
1484 // act
1485 g_on_ws_open_complete(g_on_ws_open_complete_context, WS_OPEN_OK);
1486
1487 // assert
1488 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1489
1490 // cleanup
1491 wsio_get_interface_description()->concrete_io_destroy(wsio);
1492 }
1493
1494 /* Tests_SRS_WSIO_01_149: [ When on_underlying_ws_open_complete is called with WS_OPEN_CANCELLED while the IO is opening, the callback on_io_open_complete shall be called with IO_OPEN_CANCELLED. ]*/
1495 TEST_FUNCTION(when_on_underlying_ws_open_complete_is_called_with_CANCELLED_while_OPENING_the_io_open_complete_callback_is_called_with_OPEN_CANCELLED)
1496 {
1497 // arrange
1498 CONCRETE_IO_HANDLE wsio;
1499
1500 wsio = wsio_get_interface_description()->concrete_io_create(&default_wsio_config);
1501 (void)wsio_get_interface_description()->concrete_io_open(wsio, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
1502 umock_c_reset_all_calls();
1503
1504 STRICT_EXPECTED_CALL(test_on_io_open_complete((void*)0x4242, IO_OPEN_CANCELLED));
1505
1506 // act
1507 g_on_ws_open_complete(g_on_ws_open_complete_context, WS_OPEN_CANCELLED);
1508
1509 // assert
1510 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1511
1512 // cleanup
1513 wsio_get_interface_description()->concrete_io_destroy(wsio);
1514 }
1515
1516 /* Tests_SRS_WSIO_01_137: [ When on_underlying_ws_open_complete is called with any other error code while the IO is opening, the callback on_io_open_complete shall be called with IO_OPEN_ERROR. ]*/
1517 TEST_FUNCTION(when_on_underlying_ws_open_complete_is_called_with_an_error_while_OPENING_the_io_open_complete_callback_is_called_with_OPEN_ERROR)
1518 {
1519 // arrange
1520 CONCRETE_IO_HANDLE wsio;
1521
1522 wsio = wsio_get_interface_description()->concrete_io_create(&default_wsio_config);
1523 (void)wsio_get_interface_description()->concrete_io_open(wsio, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
1524 umock_c_reset_all_calls();
1525
1526 STRICT_EXPECTED_CALL(test_on_io_open_complete((void*)0x4242, IO_OPEN_ERROR));
1527
1528 // act
1529 g_on_ws_open_complete(g_on_ws_open_complete_context, WS_OPEN_ERROR_CANNOT_SEND_UPGRADE_REQUEST);
1530
1531 // assert
1532 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1533
1534 // cleanup
1535 wsio_get_interface_description()->concrete_io_destroy(wsio);
1536 }
1537
1538 /* Tests_SRS_WSIO_01_138: [ When on_underlying_ws_open_complete is called with a NULL context, it shall do nothing. ]*/
1539 TEST_FUNCTION(on_underlying_ws_open_complete_with_NULL_context_does_nothing)
1540 {
1541 // arrange
1542 CONCRETE_IO_HANDLE wsio;
1543
1544 wsio = wsio_get_interface_description()->concrete_io_create(&default_wsio_config);
1545 (void)wsio_get_interface_description()->concrete_io_open(wsio, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
1546 umock_c_reset_all_calls();
1547
1548 // act
1549 g_on_ws_open_complete(NULL, WS_OPEN_ERROR_CANNOT_SEND_UPGRADE_REQUEST);
1550
1551 // assert
1552 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1553
1554 // cleanup
1555 wsio_get_interface_description()->concrete_io_destroy(wsio);
1556 }
1557
1558 /* Tests_SRS_WSIO_01_139: [ When on_underlying_ws_open_complete is called while in OPEN state it shall indicate an error by calling the on_io_error callback passed to wsio_open and switch to the ERROR state. ]*/
1559 /* Tests_SRS_WSIO_01_140: [ When calling on_io_error, the on_io_error_context argument given in wsio_open shall be passed to the callback on_io_error. ]*/
1560 TEST_FUNCTION(when_on_underlying_ws_open_complete_is_called_when_already_OPEN_an_error_is_indicated)
1561 {
1562 // arrange
1563 CONCRETE_IO_HANDLE wsio;
1564
1565 wsio = wsio_get_interface_description()->concrete_io_create(&default_wsio_config);
1566 (void)wsio_get_interface_description()->concrete_io_open(wsio, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
1567 g_on_ws_open_complete(g_on_ws_open_complete_context, WS_OPEN_OK);
1568 umock_c_reset_all_calls();
1569
1570 STRICT_EXPECTED_CALL(test_on_io_error((void*)0x4244));
1571
1572 // act
1573 g_on_ws_open_complete(g_on_ws_open_complete_context, WS_OPEN_OK);
1574
1575 // assert
1576 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1577
1578 // cleanup
1579 wsio_get_interface_description()->concrete_io_destroy(wsio);
1580 }
1581
1582 /* Tests_SRS_WSIO_01_141: [ When on_underlying_ws_open_complete is called while in the ERROR state it shall indicate an error by calling the on_io_error callback passed to wsio_open. ]*/
1583 /* Tests_SRS_WSIO_01_140: [ When calling on_io_error, the on_io_error_context argument given in wsio_open shall be passed to the callback on_io_error. ]*/
1584 TEST_FUNCTION(when_on_underlying_ws_open_complete_is_called_when_in_ERROR_an_error_is_indicated)
1585 {
1586 // arrange
1587 CONCRETE_IO_HANDLE wsio;
1588
1589 wsio = wsio_get_interface_description()->concrete_io_create(&default_wsio_config);
1590 (void)wsio_get_interface_description()->concrete_io_open(wsio, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
1591 g_on_ws_open_complete(g_on_ws_open_complete_context, WS_OPEN_OK);
1592 g_on_ws_open_complete(g_on_ws_open_complete_context, WS_OPEN_OK);
1593 umock_c_reset_all_calls();
1594
1595 STRICT_EXPECTED_CALL(test_on_io_error((void*)0x4244));
1596
1597 // act
1598 g_on_ws_open_complete(g_on_ws_open_complete_context, WS_OPEN_OK);
1599
1600 // assert
1601 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1602
1603 // cleanup
1604 wsio_get_interface_description()->concrete_io_destroy(wsio);
1605 }
1606
1607 /* Tests_SRS_WSIO_01_142: [ When on_underlying_ws_open_complete is called while in the CLOSING state an error shall be indicated by calling the on_io_error callback passed to wsio_open. ]*/
1608 TEST_FUNCTION(when_on_underlying_ws_open_complete_is_called_when_CLOSING_it_does_nothing)
1609 {
1610 // arrange
1611 CONCRETE_IO_HANDLE wsio;
1612
1613 wsio = wsio_get_interface_description()->concrete_io_create(&default_wsio_config);
1614 (void)wsio_get_interface_description()->concrete_io_open(wsio, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
1615 g_on_ws_open_complete(g_on_ws_open_complete_context, WS_OPEN_OK);
1616 wsio_get_interface_description()->concrete_io_close(wsio, test_on_io_close_complete, (void*)0x4245);
1617 umock_c_reset_all_calls();
1618
1619 STRICT_EXPECTED_CALL(test_on_io_error((void*)0x4244));
1620
1621 // act
1622 g_on_ws_open_complete(g_on_ws_open_complete_context, WS_OPEN_OK);
1623
1624 // assert
1625 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1626
1627 // cleanup
1628 wsio_get_interface_description()->concrete_io_destroy(wsio);
1629 }
1630
1631 /* on_ws_frame_received */
1632
1633 /* Tests_SRS_WSIO_01_124: [ When on_underlying_ws_frame_received is called the bytes in the frame shall be indicated by calling the on_bytes_received callback passed to wsio_open. ]*/
1634 /* Tests_SRS_WSIO_01_125: [ When calling on_bytes_received, the on_bytes_received_context argument given in wsio_open shall be passed to the callback on_bytes_received. ]*/
1635 TEST_FUNCTION(when_on_underlying_ws_frame_received_is_called_the_frame_content_is_indicated_up)
1636 {
1637 // arrange
1638 CONCRETE_IO_HANDLE wsio;
1639 const unsigned char test_buffer[] = { 0x42 };
1640
1641 wsio = wsio_get_interface_description()->concrete_io_create(&default_wsio_config);
1642 (void)wsio_get_interface_description()->concrete_io_open(wsio, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
1643 g_on_ws_open_complete(g_on_ws_open_complete_context, WS_OPEN_OK);
1644 umock_c_reset_all_calls();
1645
1646 STRICT_EXPECTED_CALL(test_on_bytes_received((void*)0x4243, IGNORED_PTR_ARG, sizeof(test_buffer)))
1647 .ValidateArgumentBuffer(2, test_buffer, sizeof(test_buffer));
1648
1649 // act
1650 g_on_ws_frame_received(g_on_ws_frame_received_context, WS_FRAME_TYPE_BINARY, test_buffer, sizeof(test_buffer));
1651
1652 // assert
1653 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1654
1655 // cleanup
1656 wsio_get_interface_description()->concrete_io_destroy(wsio);
1657 }
1658
1659 /* Tests_SRS_WSIO_01_125: [ When calling on_bytes_received, the on_bytes_received_context argument given in wsio_open shall be passed to the callback on_bytes_received. ]*/
1660 TEST_FUNCTION(when_on_underlying_ws_frame_received_is_called_the_frame_content_is_indicated_up_with_the_proper_context)
1661 {
1662 // arrange
1663 CONCRETE_IO_HANDLE wsio;
1664 const unsigned char test_buffer[] = { 0x42 };
1665
1666 wsio = wsio_get_interface_description()->concrete_io_create(&default_wsio_config);
1667 (void)wsio_get_interface_description()->concrete_io_open(wsio, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, NULL, test_on_io_error, (void*)0x4244);
1668 g_on_ws_open_complete(g_on_ws_open_complete_context, WS_OPEN_OK);
1669 umock_c_reset_all_calls();
1670
1671 STRICT_EXPECTED_CALL(test_on_bytes_received(NULL, IGNORED_PTR_ARG, sizeof(test_buffer)))
1672 .ValidateArgumentBuffer(2, test_buffer, sizeof(test_buffer));
1673
1674 // act
1675 g_on_ws_frame_received(g_on_ws_frame_received_context, WS_FRAME_TYPE_BINARY, test_buffer, sizeof(test_buffer));
1676
1677 // assert
1678 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1679
1680 // cleanup
1681 wsio_get_interface_description()->concrete_io_destroy(wsio);
1682 }
1683
1684 /* Tests_SRS_WSIO_01_126: [ If on_underlying_ws_frame_received is called while the IO is in any state other than OPEN, it shall do nothing. ]*/
1685 TEST_FUNCTION(when_on_underlying_ws_frame_received_is_called_while_opening_it_shall_do_nothing)
1686 {
1687 // arrange
1688 CONCRETE_IO_HANDLE wsio;
1689 const unsigned char test_buffer[] = { 0x42 };
1690
1691 wsio = wsio_get_interface_description()->concrete_io_create(&default_wsio_config);
1692 (void)wsio_get_interface_description()->concrete_io_open(wsio, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, NULL, test_on_io_error, (void*)0x4244);
1693 umock_c_reset_all_calls();
1694
1695 // act
1696 g_on_ws_frame_received(g_on_ws_frame_received_context, WS_FRAME_TYPE_BINARY, test_buffer, sizeof(test_buffer));
1697
1698 // assert
1699 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1700
1701 // cleanup
1702 wsio_get_interface_description()->concrete_io_destroy(wsio);
1703 }
1704
1705 /* Tests_SRS_WSIO_01_126: [ If on_underlying_ws_frame_received is called while the IO is in any state other than OPEN, it shall do nothing. ]*/
1706 TEST_FUNCTION(when_on_underlying_ws_frame_received_is_called_while_closing_it_shall_do_nothing)
1707 {
1708 // arrange
1709 CONCRETE_IO_HANDLE wsio;
1710 const unsigned char test_buffer[] = { 0x42 };
1711
1712 wsio = wsio_get_interface_description()->concrete_io_create(&default_wsio_config);
1713 (void)wsio_get_interface_description()->concrete_io_open(wsio, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, NULL, test_on_io_error, (void*)0x4244);
1714 g_on_ws_open_complete(g_on_ws_open_complete_context, WS_OPEN_OK);
1715 (void)wsio_get_interface_description()->concrete_io_close(wsio, test_on_io_close_complete, (void*)0x4245);
1716 umock_c_reset_all_calls();
1717
1718 // act
1719 g_on_ws_frame_received(g_on_ws_frame_received_context, WS_FRAME_TYPE_BINARY, test_buffer, sizeof(test_buffer));
1720
1721 // assert
1722 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1723
1724 // cleanup
1725 wsio_get_interface_description()->concrete_io_destroy(wsio);
1726 }
1727
1728 /* Tests_SRS_WSIO_01_150: [ If on_underlying_ws_frame_received is called with NULL context it shall do nothing. ]*/
1729 TEST_FUNCTION(when_on_underlying_ws_frame_received_with_NULL_context_does_nothing)
1730 {
1731 // arrange
1732 CONCRETE_IO_HANDLE wsio;
1733 const unsigned char test_buffer[] = { 0x42 };
1734
1735 wsio = wsio_get_interface_description()->concrete_io_create(&default_wsio_config);
1736 (void)wsio_get_interface_description()->concrete_io_open(wsio, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, NULL, test_on_io_error, (void*)0x4244);
1737 g_on_ws_open_complete(g_on_ws_open_complete_context, WS_OPEN_OK);
1738 umock_c_reset_all_calls();
1739
1740 // act
1741 g_on_ws_frame_received(NULL, WS_FRAME_TYPE_BINARY, test_buffer, sizeof(test_buffer));
1742
1743 // assert
1744 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1745
1746 // cleanup
1747 wsio_get_interface_description()->concrete_io_destroy(wsio);
1748 }
1749
1750 /* Tests_SRS_WSIO_01_151: [ If the WebSocket frame type is not binary then an error shall be indicated by calling the on_io_error callback passed to wsio_open. ]*/
1751 /* Tests_SRS_WSIO_01_152: [ When calling on_io_error, the on_io_error_context argument given in wsio_open shall be passed to the callback on_io_error. ]*/
1752 TEST_FUNCTION(when_on_underlying_ws_frame_received_is_called_with_a_text_frame_an_error_is_indicated)
1753 {
1754 // arrange
1755 CONCRETE_IO_HANDLE wsio;
1756 const unsigned char test_buffer[] = { 0x42 };
1757
1758 wsio = wsio_get_interface_description()->concrete_io_create(&default_wsio_config);
1759 (void)wsio_get_interface_description()->concrete_io_open(wsio, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, NULL, test_on_io_error, (void*)0x4244);
1760 g_on_ws_open_complete(g_on_ws_open_complete_context, WS_OPEN_OK);
1761 umock_c_reset_all_calls();
1762
1763 STRICT_EXPECTED_CALL(test_on_io_error((void*)0x4244));
1764
1765 // act
1766 g_on_ws_frame_received(g_on_ws_frame_received_context, WS_FRAME_TYPE_TEXT, test_buffer, sizeof(test_buffer));
1767
1768 // assert
1769 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1770
1771 // cleanup
1772 wsio_get_interface_description()->concrete_io_destroy(wsio);
1773 }
1774
1775 /* Tests_SRS_WSIO_01_153: [ When on_underlying_ws_frame_received is called with zero size, no bytes shall be indicated up as received. ]*/
1776 TEST_FUNCTION(when_on_underlying_ws_frame_received_is_called_with_zero_bytes_no_bytes_are_reported_as_received)
1777 {
1778 // arrange
1779 CONCRETE_IO_HANDLE wsio;
1780 const unsigned char test_buffer[] = { 0x42 };
1781
1782 wsio = wsio_get_interface_description()->concrete_io_create(&default_wsio_config);
1783 (void)wsio_get_interface_description()->concrete_io_open(wsio, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, NULL, test_on_io_error, (void*)0x4244);
1784 g_on_ws_open_complete(g_on_ws_open_complete_context, WS_OPEN_OK);
1785 umock_c_reset_all_calls();
1786
1787 // act
1788 g_on_ws_frame_received(g_on_ws_frame_received_context, WS_FRAME_TYPE_BINARY, test_buffer, 0);
1789
1790 // assert
1791 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1792
1793 // cleanup
1794 wsio_get_interface_description()->concrete_io_destroy(wsio);
1795 }
1796
1797 /* Tests_SRS_WSIO_01_154: [ When on_underlying_ws_frame_received is called with a positive size and a NULL buffer, an error shall be indicated by calling the on_io_error callback passed to wsio_open. ]*/
1798 TEST_FUNCTION(when_on_underlying_ws_frame_received_is_called_with_positive_size_and_NULL_buffer_an_error_is_indicated)
1799 {
1800 // arrange
1801 CONCRETE_IO_HANDLE wsio;
1802
1803 wsio = wsio_get_interface_description()->concrete_io_create(&default_wsio_config);
1804 (void)wsio_get_interface_description()->concrete_io_open(wsio, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, NULL, test_on_io_error, (void*)0x4244);
1805 g_on_ws_open_complete(g_on_ws_open_complete_context, WS_OPEN_OK);
1806 umock_c_reset_all_calls();
1807
1808 STRICT_EXPECTED_CALL(test_on_io_error((void*)0x4244));
1809
1810 // act
1811 g_on_ws_frame_received(g_on_ws_frame_received_context, WS_FRAME_TYPE_BINARY, NULL, 1);
1812
1813 // assert
1814 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1815
1816 // cleanup
1817 wsio_get_interface_description()->concrete_io_destroy(wsio);
1818 }
1819
1820 /* on_underlying_ws_send_frame_complete */
1821
1822 /* Tests_SRS_WSIO_01_143: [ When on_underlying_ws_send_frame_complete is called after sending a WebSocket frame, the pending IO shall be removed from the list. ]*/
1823 /* Tests_SRS_WSIO_01_145: [ Removing it from the list shall be done by calling singlylinkedlist_remove. ]*/
1824 /* Tests_SRS_WSIO_01_144: [ Also the pending IO data shall be freed. ]*/
1825 /* Tests_SRS_WSIO_01_146: [ When on_underlying_ws_send_frame_complete is called with WS_SEND_OK, the callback on_send_complete shall be called with IO_SEND_OK. ]*/
1826 TEST_FUNCTION(wsio_send_with_1_byte_completed_indicates_the_completion_up)
1827 {
1828 // arrange
1829 CONCRETE_IO_HANDLE wsio;
1830 unsigned char test_buffer[] = { 42 };
1831
1832 wsio = wsio_get_interface_description()->concrete_io_create(&default_wsio_config);
1833 (void)wsio_get_interface_description()->concrete_io_open(wsio, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
1834 g_on_ws_open_complete(g_on_ws_open_complete_context, WS_OPEN_OK);
1835 (void)wsio_get_interface_description()->concrete_io_send(wsio, test_buffer, sizeof(test_buffer), test_on_send_complete, (void*)0x4343);
1836 umock_c_reset_all_calls();
1837
1838 EXPECTED_CALL(singlylinkedlist_item_get_value(IGNORED_PTR_ARG));
1839 STRICT_EXPECTED_CALL(singlylinkedlist_remove(TEST_SINGLYLINKEDSINGLYLINKEDLIST_HANDLE, IGNORED_PTR_ARG));
1840 STRICT_EXPECTED_CALL(test_on_send_complete((void*)0x4343, IO_SEND_OK));
1841 EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG));
1842
1843 // act
1844 g_on_ws_send_frame_complete(g_on_ws_send_frame_complete_context, WS_SEND_FRAME_OK);
1845
1846 // assert
1847 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1848
1849 // cleanup
1850 wsio_get_interface_description()->concrete_io_destroy(wsio);
1851 }
1852
1853 /* Tests_SRS_WSIO_01_147: [ When on_underlying_ws_send_frame_complete is called with WS_SEND_CANCELLED, the callback on_send_complete shall be called with IO_SEND_CANCELLED. ]*/
1854 TEST_FUNCTION(wsio_send_with_1_byte_completed_with_CANCELLED_indicates_the_completion_up_as_CANCELLED)
1855 {
1856 // arrange
1857 CONCRETE_IO_HANDLE wsio;
1858 unsigned char test_buffer[] = { 42 };
1859
1860 wsio = wsio_get_interface_description()->concrete_io_create(&default_wsio_config);
1861 (void)wsio_get_interface_description()->concrete_io_open(wsio, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
1862 g_on_ws_open_complete(g_on_ws_open_complete_context, WS_OPEN_OK);
1863 (void)wsio_get_interface_description()->concrete_io_send(wsio, test_buffer, sizeof(test_buffer), test_on_send_complete, (void*)0x4343);
1864 umock_c_reset_all_calls();
1865
1866 EXPECTED_CALL(singlylinkedlist_item_get_value(IGNORED_PTR_ARG));
1867 STRICT_EXPECTED_CALL(singlylinkedlist_remove(TEST_SINGLYLINKEDSINGLYLINKEDLIST_HANDLE, IGNORED_PTR_ARG));
1868 STRICT_EXPECTED_CALL(test_on_send_complete((void*)0x4343, IO_SEND_CANCELLED));
1869 EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG));
1870
1871 // act
1872 g_on_ws_send_frame_complete(g_on_ws_send_frame_complete_context, WS_SEND_FRAME_CANCELLED);
1873
1874 // assert
1875 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1876
1877 // cleanup
1878 wsio_get_interface_description()->concrete_io_destroy(wsio);
1879 }
1880
1881 /* Tests_SRS_WSIO_01_148: [ When on_underlying_ws_send_frame_complete is called with any other error code, the callback on_send_complete shall be called with IO_SEND_ERROR. ]*/
1882 TEST_FUNCTION(wsio_send_with_1_byte_completed_with_ERROR_indicates_the_completion_up_as_ERROR)
1883 {
1884 // arrange
1885 CONCRETE_IO_HANDLE wsio;
1886 unsigned char test_buffer[] = { 42 };
1887
1888 wsio = wsio_get_interface_description()->concrete_io_create(&default_wsio_config);
1889 (void)wsio_get_interface_description()->concrete_io_open(wsio, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
1890 g_on_ws_open_complete(g_on_ws_open_complete_context, WS_OPEN_OK);
1891 (void)wsio_get_interface_description()->concrete_io_send(wsio, test_buffer, sizeof(test_buffer), test_on_send_complete, (void*)0x4343);
1892 umock_c_reset_all_calls();
1893
1894 EXPECTED_CALL(singlylinkedlist_item_get_value(IGNORED_PTR_ARG));
1895 STRICT_EXPECTED_CALL(singlylinkedlist_remove(TEST_SINGLYLINKEDSINGLYLINKEDLIST_HANDLE, IGNORED_PTR_ARG));
1896 STRICT_EXPECTED_CALL(test_on_send_complete((void*)0x4343, IO_SEND_ERROR));
1897 EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG));
1898
1899 // act
1900 g_on_ws_send_frame_complete(g_on_ws_send_frame_complete_context, WS_SEND_FRAME_ERROR);
1901
1902 // assert
1903 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1904
1905 // cleanup
1906 wsio_get_interface_description()->concrete_io_destroy(wsio);
1907 }
1908
1909 /* Tests_SRS_WSIO_01_155: [ When on_underlying_ws_send_frame_complete is called with a NULL context it shall do nothing. ]*/
1910 TEST_FUNCTION(on_underlying_ws_send_frame_complete_with_NULL_context_does_nothing)
1911 {
1912 // arrange
1913 CONCRETE_IO_HANDLE wsio;
1914 unsigned char test_buffer[] = { 42 };
1915
1916 wsio = wsio_get_interface_description()->concrete_io_create(&default_wsio_config);
1917 (void)wsio_get_interface_description()->concrete_io_open(wsio, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
1918 g_on_ws_open_complete(g_on_ws_open_complete_context, WS_OPEN_OK);
1919 (void)wsio_get_interface_description()->concrete_io_send(wsio, test_buffer, sizeof(test_buffer), test_on_send_complete, (void*)0x4343);
1920 umock_c_reset_all_calls();
1921
1922 // act
1923 g_on_ws_send_frame_complete(NULL, WS_SEND_FRAME_OK);
1924
1925 // assert
1926 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1927
1928 // cleanup
1929 wsio_get_interface_description()->concrete_io_destroy(wsio);
1930 }
1931
1932 /* wsio_setoption */
1933
1934 /* Tests_SRS_WSIO_01_109: [ If any of the arguments ws_io or option_name is NULL wsio_setoption shall return a non-zero value. ]*/
1935 TEST_FUNCTION(wsio_setoption_with_NULL_handle_fails)
1936 {
1937 // arrange
1938 int result;
1939
1940 // act
1941 result = wsio_get_interface_description()->concrete_io_setoption(NULL, "option1", (void*)0x4242);
1942
1943 // assert
1944 ASSERT_ARE_NOT_EQUAL(int, 0, result);
1945 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1946 }
1947
1948 /* Tests_SRS_WSIO_01_109: [ If any of the arguments ws_io or option_name is NULL wsio_setoption shall return a non-zero value. ]*/
1949 TEST_FUNCTION(wsio_setoption_with_NULL_option_fails)
1950 {
1951 // arrange
1952 CONCRETE_IO_HANDLE wsio;
1953 int result;
1954
1955 wsio = wsio_get_interface_description()->concrete_io_create(&default_wsio_config);
1956 umock_c_reset_all_calls();
1957
1958 // act
1959 result = wsio_get_interface_description()->concrete_io_setoption(wsio, NULL, (void*)0x4242);
1960
1961 // assert
1962 ASSERT_ARE_NOT_EQUAL(int, 0, result);
1963 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1964
1965 // cleanup
1966 wsio_get_interface_description()->concrete_io_destroy(wsio);
1967 }
1968
1969 /* Tests_SRS_WSIO_01_183: [ If the option name is WSIOOptions then wsio_setoption shall call OptionHandler_FeedOptions and pass to it the underlying IO handle and the value argument. ]*/
1970 /* Tests_SRS_WSIO_01_158: [ On success, wsio_setoption shall return 0. ]*/
1971 TEST_FUNCTION(wsio_setoption_with_WSIOOptions_feeds_the_options_to_the_underlying_layer)
1972 {
1973 // arrange
1974 CONCRETE_IO_HANDLE wsio;
1975 int result;
1976
1977 wsio = wsio_get_interface_description()->concrete_io_create(&default_wsio_config);
1978 umock_c_reset_all_calls();
1979
1980 STRICT_EXPECTED_CALL(OptionHandler_FeedOptions(TEST_OPTIONHANDLER_HANDLE, TEST_UWS_HANDLE));
1981
1982 // act
1983 result = wsio_get_interface_description()->concrete_io_setoption(wsio, "WSIOOptions", (void*)TEST_OPTIONHANDLER_HANDLE);
1984
1985 // assert
1986 ASSERT_ARE_EQUAL(int, 0, result);
1987 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1988
1989 // cleanup
1990 wsio_get_interface_description()->concrete_io_destroy(wsio);
1991 }
1992
1993 /* Tests_SRS_WSIO_01_184: [ If OptionHandler_FeedOptions fails, wsio_setoption shall fail and return a non-zero value. ]*/
1994 TEST_FUNCTION(when_OptionHandler_FeedOptions_fails_then_wsio_setoption_fails)
1995 {
1996 // arrange
1997 CONCRETE_IO_HANDLE wsio;
1998 int result;
1999
2000 wsio = wsio_get_interface_description()->concrete_io_create(&default_wsio_config);
2001 umock_c_reset_all_calls();
2002
2003 STRICT_EXPECTED_CALL(OptionHandler_FeedOptions(TEST_OPTIONHANDLER_HANDLE, TEST_UWS_HANDLE))
2004 .SetReturn(OPTIONHANDLER_ERROR);
2005
2006 // act
2007 result = wsio_get_interface_description()->concrete_io_setoption(wsio, "WSIOOptions", (void*)TEST_OPTIONHANDLER_HANDLE);
2008
2009 // assert
2010 ASSERT_ARE_NOT_EQUAL(int, 0, result);
2011 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2012
2013 // cleanup
2014 wsio_get_interface_description()->concrete_io_destroy(wsio);
2015 }
2016
2017 /* Tests_SRS_WSIO_01_156: [ Otherwise all options shall be passed as they are to uws by calling uws_client_set_option. ]*/
2018 /* Tests_SRS_WSIO_01_158: [ On success, wsio_setoption shall return 0. ]*/
2019 TEST_FUNCTION(wsio_setoption_passes_the_option_dows_to_uws)
2020 {
2021 // arrange
2022 CONCRETE_IO_HANDLE wsio;
2023 int result;
2024
2025 wsio = wsio_get_interface_description()->concrete_io_create(&default_wsio_config);
2026 umock_c_reset_all_calls();
2027
2028 STRICT_EXPECTED_CALL(uws_client_set_option(TEST_UWS_HANDLE, "option1", (void*)0x4242));
2029
2030 // act
2031 result = wsio_get_interface_description()->concrete_io_setoption(wsio, "option1", (void*)0x4242);
2032
2033 // assert
2034 ASSERT_ARE_EQUAL(int, 0, result);
2035 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2036
2037 // cleanup
2038 wsio_get_interface_description()->concrete_io_destroy(wsio);
2039 }
2040
2041 /* Tests_SRS_WSIO_01_157: [ If uws_client_set_option fails, wsio_setoption shall fail and return a non-zero value. ]*/
2042 TEST_FUNCTION(when_uws_set_option_fails_wsio_setoption_fails)
2043 {
2044 // arrange
2045 CONCRETE_IO_HANDLE wsio;
2046 int result;
2047
2048 wsio = wsio_get_interface_description()->concrete_io_create(&default_wsio_config);
2049 umock_c_reset_all_calls();
2050
2051 STRICT_EXPECTED_CALL(uws_client_set_option(TEST_UWS_HANDLE, "option1", (void*)0x4242))
2052 .SetReturn(1);
2053
2054 // act
2055 result = wsio_get_interface_description()->concrete_io_setoption(wsio, "option1", (void*)0x4242);
2056
2057 // assert
2058 ASSERT_ARE_NOT_EQUAL(int, 0, result);
2059 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2060
2061 // cleanup
2062 wsio_get_interface_description()->concrete_io_destroy(wsio);
2063 }
2064
2065 /* wsio_retrieveoptions */
2066
2067 /* Tests_SRS_WSIO_01_118: [ If parameter handle is NULL then wsio_retrieveoptions shall fail and return NULL. ]*/
2068 TEST_FUNCTION(wsio_retrieveoptions_with_NULL_handle_fails)
2069 {
2070 // arrange
2071 OPTIONHANDLER_HANDLE result;
2072
2073 // act
2074 result = wsio_get_interface_description()->concrete_io_retrieveoptions(NULL);
2075
2076 // assert
2077 ASSERT_IS_NULL(result);
2078 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2079 }
2080
2081 /* Tests_SRS_WSIO_01_119: [ wsio_retrieveoptions shall call OptionHandler_Create to produce an OPTIONHANDLER_HANDLE and on success return the new OPTIONHANDLER_HANDLE handle. ]*/
2082 /* Tests_SRS_WSIO_01_179: [ When calling uws_client_retrieve_options the uws client handle shall be passed to it. ]*/
2083 /* Tests_SRS_WSIO_01_178: [ uws_client_retrieve_options shall add to the option handler one option, whose name shall be uWSCLientOptions and the value shall be queried by calling uws_client_retrieve_options. ]*/
2084 /* Tests_SRS_WSIO_01_181: [ Adding the option shall be done by calling OptionHandler_AddOption. ]*/
2085 TEST_FUNCTION(wsio_retrieveoptions_creates_an_option_handler_with_the_value_obtained_from_the_underlying_retrieve_options)
2086 {
2087 // arrange
2088 OPTIONHANDLER_HANDLE result;
2089 CONCRETE_IO_HANDLE wsio;
2090
2091 wsio = wsio_get_interface_description()->concrete_io_create(&default_wsio_config);
2092 umock_c_reset_all_calls();
2093
2094 EXPECTED_CALL(OptionHandler_Create(IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG));
2095 STRICT_EXPECTED_CALL(uws_client_retrieve_options(TEST_UWS_HANDLE));
2096 STRICT_EXPECTED_CALL(OptionHandler_AddOption(TEST_OPTIONHANDLER_HANDLE, "WSIOOptions", TEST_UWS_CLIENT_OPTIONHANDLER_HANDLE));
2097 STRICT_EXPECTED_CALL(OptionHandler_Destroy(IGNORED_PTR_ARG));
2098
2099 // act
2100 result = wsio_get_interface_description()->concrete_io_retrieveoptions(wsio);
2101
2102 // assert
2103 ASSERT_IS_NOT_NULL(result);
2104 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2105
2106 // cleanup
2107 wsio_get_interface_description()->concrete_io_destroy(wsio);
2108 }
2109
2110 /* Tests_SRS_WSIO_01_120: [ If OptionHandler_Create fails then wsio_retrieveoptions shall fail and return NULL. ]*/
2111 TEST_FUNCTION(when_OptionHandler_Create_fails_then_wsio_retrieveoptions_fails)
2112 {
2113 // arrange
2114 OPTIONHANDLER_HANDLE result;
2115 CONCRETE_IO_HANDLE wsio;
2116
2117 wsio = wsio_get_interface_description()->concrete_io_create(&default_wsio_config);
2118 umock_c_reset_all_calls();
2119
2120 EXPECTED_CALL(OptionHandler_Create(IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
2121 .SetReturn(NULL);
2122
2123 // act
2124 result = wsio_get_interface_description()->concrete_io_retrieveoptions(wsio);
2125
2126 // assert
2127 ASSERT_IS_NULL(result);
2128 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2129
2130 // cleanup
2131 wsio_get_interface_description()->concrete_io_destroy(wsio);
2132 }
2133
2134 /* Tests_SRS_WSIO_01_180: [ If uws_client_retrieve_options fails, uws_client_retrieve_options shall fail and return NULL. ]*/
2135 TEST_FUNCTION(when_uws_client_retrieve_options_fails_then_wsio_retrieveoptions_fails)
2136 {
2137 // arrange
2138 OPTIONHANDLER_HANDLE result;
2139 CONCRETE_IO_HANDLE wsio;
2140
2141 wsio = wsio_get_interface_description()->concrete_io_create(&default_wsio_config);
2142 umock_c_reset_all_calls();
2143
2144 EXPECTED_CALL(OptionHandler_Create(IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG));
2145 STRICT_EXPECTED_CALL(uws_client_retrieve_options(TEST_UWS_HANDLE))
2146 .SetReturn(NULL);
2147 STRICT_EXPECTED_CALL(OptionHandler_Destroy(TEST_OPTIONHANDLER_HANDLE));
2148
2149 // act
2150 result = wsio_get_interface_description()->concrete_io_retrieveoptions(wsio);
2151
2152 // assert
2153 ASSERT_IS_NULL(result);
2154 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2155
2156 // cleanup
2157 wsio_get_interface_description()->concrete_io_destroy(wsio);
2158 }
2159
2160 /* Tests_SRS_WSIO_01_182: [ If OptionHandler_AddOption fails, uws_client_retrieve_options shall fail and return NULL. ]*/
2161 TEST_FUNCTION(when_OptionHandler_AddOption_fails_then_wsio_retrieveoptions_fails)
2162 {
2163 // arrange
2164 OPTIONHANDLER_HANDLE result;
2165 CONCRETE_IO_HANDLE wsio;
2166
2167 wsio = wsio_get_interface_description()->concrete_io_create(&default_wsio_config);
2168 umock_c_reset_all_calls();
2169
2170 EXPECTED_CALL(OptionHandler_Create(IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG));
2171 STRICT_EXPECTED_CALL(uws_client_retrieve_options(TEST_UWS_HANDLE));
2172 STRICT_EXPECTED_CALL(OptionHandler_AddOption(TEST_OPTIONHANDLER_HANDLE, "WSIOOptions", TEST_UWS_CLIENT_OPTIONHANDLER_HANDLE))
2173 .SetReturn(OPTIONHANDLER_ERROR);
2174 STRICT_EXPECTED_CALL(OptionHandler_Destroy(TEST_OPTIONHANDLER_HANDLE));
2175 STRICT_EXPECTED_CALL(OptionHandler_Destroy(TEST_UWS_CLIENT_OPTIONHANDLER_HANDLE));
2176
2177 // act
2178 result = wsio_get_interface_description()->concrete_io_retrieveoptions(wsio);
2179
2180 // assert
2181 ASSERT_IS_NULL(result);
2182 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2183
2184 // cleanup
2185 wsio_get_interface_description()->concrete_io_destroy(wsio);
2186 }
2187
2188 /* wsio_clone_option */
2189
2190 /* Tests_SRS_WSIO_01_174: [ If wsio_clone_option is called with NULL name or value it shall return NULL. ]*/
2191 TEST_FUNCTION(wsio_clone_option_with_NULL_name_fails)
2192 {
2193 // arrange
2194 CONCRETE_IO_HANDLE wsio;
2195 void* result;
2196
2197 wsio = wsio_get_interface_description()->concrete_io_create(&default_wsio_config);
2198 (void)wsio_get_interface_description()->concrete_io_retrieveoptions(wsio);
2199 umock_c_reset_all_calls();
2200
2201 // act
2202 result = g_clone_option(NULL, (void*)0x4243);
2203
2204 // assert
2205 ASSERT_IS_NULL(result);
2206 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2207
2208 // cleanup
2209 wsio_get_interface_description()->concrete_io_destroy(wsio);
2210 }
2211
2212 /* Tests_SRS_WSIO_01_174: [ If wsio_clone_option is called with NULL name or value it shall return NULL. ]*/
2213 TEST_FUNCTION(wsio_clone_option_with_NULL_value_fails)
2214 {
2215 // arrange
2216 CONCRETE_IO_HANDLE wsio;
2217 void* result;
2218
2219 wsio = wsio_get_interface_description()->concrete_io_create(&default_wsio_config);
2220 (void)wsio_get_interface_description()->concrete_io_retrieveoptions(wsio);
2221 umock_c_reset_all_calls();
2222
2223 // act
2224 result = g_clone_option("WSIOOptions", NULL);
2225
2226 // assert
2227 ASSERT_IS_NULL(result);
2228 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2229
2230 // cleanup
2231 wsio_get_interface_description()->concrete_io_destroy(wsio);
2232 }
2233
2234 /* Tests_SRS_WSIO_01_171: [** wsio_clone_option called with name being WSIOOptions shall return the same value. ]*/
2235 TEST_FUNCTION(wsio_clone_option_with_WSIOOptions_clones_the_option_handler)
2236 {
2237 // arrange
2238 CONCRETE_IO_HANDLE wsio;
2239 void* result;
2240
2241 wsio = wsio_get_interface_description()->concrete_io_create(&default_wsio_config);
2242 (void)wsio_get_interface_description()->concrete_io_retrieveoptions(wsio);
2243 umock_c_reset_all_calls();
2244 STRICT_EXPECTED_CALL(OptionHandler_Clone(IGNORED_PTR_ARG));
2245
2246 // act
2247 result = g_clone_option("WSIOOptions", (void*)0x4243);
2248
2249 // assert
2250 ASSERT_ARE_EQUAL(void_ptr, (void*)0x4246, result);
2251 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2252
2253 // cleanup
2254 wsio_get_interface_description()->concrete_io_destroy(wsio);
2255 }
2256
2257 /* Tests_SRS_WSIO_01_173: [ wsio_clone_option called with any other option name than WSIOOptions shall return NULL. ]*/
2258 TEST_FUNCTION(wsio_clone_option_with_an_unknown_option_name_fails)
2259 {
2260 // arrange
2261 CONCRETE_IO_HANDLE wsio;
2262 void* result;
2263
2264 wsio = wsio_get_interface_description()->concrete_io_create(&default_wsio_config);
2265 (void)wsio_get_interface_description()->concrete_io_retrieveoptions(wsio);
2266 umock_c_reset_all_calls();
2267
2268 // act
2269 result = g_clone_option("Cucu", (void*)0x4243);
2270
2271 // assert
2272 ASSERT_IS_NULL(result);
2273 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2274
2275 // cleanup
2276 wsio_get_interface_description()->concrete_io_destroy(wsio);
2277 }
2278
2279 /* wsio_destroy_option */
2280
2281 /* Tests_SRS_WSIO_01_175: [ wsio_destroy_option called with the option name being WSIOOptions shall destroy the value by calling OptionHandler_Destroy. ]*/
2282 TEST_FUNCTION(wsio_destroy_option_with_WSIOOptions_destroys_the_handler)
2283 {
2284 // arrange
2285 CONCRETE_IO_HANDLE wsio;
2286
2287 wsio = wsio_get_interface_description()->concrete_io_create(&default_wsio_config);
2288 (void)wsio_get_interface_description()->concrete_io_retrieveoptions(wsio);
2289 umock_c_reset_all_calls();
2290
2291 STRICT_EXPECTED_CALL(OptionHandler_Destroy(TEST_OPTIONHANDLER_HANDLE));
2292
2293 // act
2294 g_destroy_option("WSIOOptions", TEST_OPTIONHANDLER_HANDLE);
2295
2296 // assert
2297 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2298
2299 // cleanup
2300 wsio_get_interface_description()->concrete_io_destroy(wsio);
2301 }
2302
2303 /* Tests_SRS_WSIO_01_176: [ If wsio_destroy_option is called with any other name it shall do nothing. ]*/
2304 TEST_FUNCTION(wsio_destroy_option_with_an_unknown_option_does_no_destroy)
2305 {
2306 // arrange
2307 CONCRETE_IO_HANDLE wsio;
2308
2309 wsio = wsio_get_interface_description()->concrete_io_create(&default_wsio_config);
2310 (void)wsio_get_interface_description()->concrete_io_retrieveoptions(wsio);
2311 umock_c_reset_all_calls();
2312
2313 // act
2314 g_destroy_option("cucu", TEST_OPTIONHANDLER_HANDLE);
2315
2316 // assert
2317 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2318
2319 // cleanup
2320 wsio_get_interface_description()->concrete_io_destroy(wsio);
2321 }
2322
2323 /* Tests_SRS_WSIO_01_177: [ If wsio_destroy_option is called with NULL name or value it shall do nothing. ]*/
2324 TEST_FUNCTION(wsio_destroy_option_with_NULL_name_does_no_destroy)
2325 {
2326 // arrange
2327 CONCRETE_IO_HANDLE wsio;
2328
2329 wsio = wsio_get_interface_description()->concrete_io_create(&default_wsio_config);
2330 (void)wsio_get_interface_description()->concrete_io_retrieveoptions(wsio);
2331 umock_c_reset_all_calls();
2332
2333 // act
2334 g_destroy_option(NULL, TEST_OPTIONHANDLER_HANDLE);
2335
2336 // assert
2337 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2338
2339 // cleanup
2340 wsio_get_interface_description()->concrete_io_destroy(wsio);
2341 }
2342
2343 /* Tests_SRS_WSIO_01_177: [ If wsio_destroy_option is called with NULL name or value it shall do nothing. ]*/
2344 TEST_FUNCTION(wsio_destroy_option_with_NULL_value_does_no_destroy)
2345 {
2346 // arrange
2347 CONCRETE_IO_HANDLE wsio;
2348
2349 wsio = wsio_get_interface_description()->concrete_io_create(&default_wsio_config);
2350 (void)wsio_get_interface_description()->concrete_io_retrieveoptions(wsio);
2351 umock_c_reset_all_calls();
2352
2353 // act
2354 g_destroy_option("WSIOOptions", NULL);
2355
2356 // assert
2357 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2358
2359 // cleanup
2360 wsio_get_interface_description()->concrete_io_destroy(wsio);
2361 }
2362
2363 /* on_underlying_ws_close_complete */
2364
2365 /* Tests_SRS_WSIO_01_159: [ When on_underlying_ws_close_complete while the IO is closing (after wsio_close), the close shall be indicated up by calling the on_io_close_complete callback passed to wsio_close. ]*/
2366 /* Tests_SRS_WSIO_01_163: [ When on_io_close_complete is called, the context passed to wsio_close shall be passed as argument to on_io_close_complete. ]*/
2367 TEST_FUNCTION(on_underlying_ws_close_complete_while_closing_triggers_the_send_complete_callback)
2368 {
2369 // arrange
2370 CONCRETE_IO_HANDLE wsio;
2371
2372 wsio = wsio_get_interface_description()->concrete_io_create(&default_wsio_config);
2373 (void)wsio_get_interface_description()->concrete_io_open(wsio, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
2374 g_on_ws_open_complete(g_on_ws_open_complete_context, WS_OPEN_OK);
2375 (void)wsio_get_interface_description()->concrete_io_close(wsio, test_on_io_close_complete, (void*)0x4245);
2376 umock_c_reset_all_calls();
2377
2378 STRICT_EXPECTED_CALL(test_on_io_close_complete((void*)0x4245));
2379
2380 // act
2381 g_on_ws_close_complete(g_on_ws_close_complete_context);
2382
2383 // assert
2384 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2385
2386 // cleanup
2387 wsio_get_interface_description()->concrete_io_destroy(wsio);
2388 }
2389
2390 /* Tests_SRS_WSIO_01_160: [ If NULL was passed to wsio_close no callback shall be called. ]*/
2391 TEST_FUNCTION(when_on_close_complete_was_NULL_on_underlying_ws_close_complete_does_not_trigger_any_callback)
2392 {
2393 // arrange
2394 CONCRETE_IO_HANDLE wsio;
2395
2396 wsio = wsio_get_interface_description()->concrete_io_create(&default_wsio_config);
2397 (void)wsio_get_interface_description()->concrete_io_open(wsio, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
2398 g_on_ws_open_complete(g_on_ws_open_complete_context, WS_OPEN_OK);
2399 (void)wsio_get_interface_description()->concrete_io_close(wsio, NULL, (void*)0x4245);
2400 umock_c_reset_all_calls();
2401
2402 // act
2403 g_on_ws_close_complete(g_on_ws_close_complete_context);
2404
2405 // assert
2406 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2407
2408 // cleanup
2409 wsio_get_interface_description()->concrete_io_destroy(wsio);
2410 }
2411
2412 /* Tests_SRS_WSIO_01_161: [ If the context passed to on_underlying_ws_close_complete is NULL, on_underlying_ws_close_complete shall do nothing. ]*/
2413 TEST_FUNCTION(on_underlying_ws_close_complete_with_NULL_context_does_nothing)
2414 {
2415 // arrange
2416 CONCRETE_IO_HANDLE wsio;
2417
2418 wsio = wsio_get_interface_description()->concrete_io_create(&default_wsio_config);
2419 (void)wsio_get_interface_description()->concrete_io_open(wsio, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
2420 g_on_ws_open_complete(g_on_ws_open_complete_context, WS_OPEN_OK);
2421 (void)wsio_get_interface_description()->concrete_io_close(wsio, test_on_io_close_complete, (void*)0x4245);
2422 umock_c_reset_all_calls();
2423
2424 // act
2425 g_on_ws_close_complete(NULL);
2426
2427 // assert
2428 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2429
2430 // cleanup
2431 wsio_get_interface_description()->concrete_io_destroy(wsio);
2432 }
2433
2434 /* on_underlying_ws_peer_closed */
2435
2436 /* Tests_SRS_WSIO_01_170: [ When on_underlying_ws_peer_closed and the state of the IO is OPENING an error shall be indicated by calling the on_io_open_complete callback passed to wsio_open with the error code WS_OPEN_ERROR. ]*/
2437 /* Tests_SRS_WSIO_01_168: [ The close_code, extra_data and extra_data_length arguments shall be ignored. ]*/
2438 TEST_FUNCTION(on_underlying_ws_peer_closed_when_OPENING_indicates_an_error)
2439 {
2440 // arrange
2441 CONCRETE_IO_HANDLE wsio;
2442
2443 wsio = wsio_get_interface_description()->concrete_io_create(&default_wsio_config);
2444 (void)wsio_get_interface_description()->concrete_io_open(wsio, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
2445 umock_c_reset_all_calls();
2446
2447 STRICT_EXPECTED_CALL(test_on_io_open_complete((void*)0x4242, IO_OPEN_ERROR));
2448
2449 // act
2450 g_on_ws_peer_closed(g_on_ws_peer_closed_context, NULL, NULL, 0);
2451
2452 // assert
2453 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2454
2455 // cleanup
2456 wsio_get_interface_description()->concrete_io_destroy(wsio);
2457 }
2458
2459 /* Tests_SRS_WSIO_01_166: [ When on_underlying_ws_peer_closed and the state of the IO is OPEN an error shall be indicated by calling the on_io_error callback passed to wsio_open. ]*/
2460 /* Tests_SRS_WSIO_01_168: [ The close_code, extra_data and extra_data_length arguments shall be ignored. ]*/
2461 TEST_FUNCTION(on_underlying_ws_peer_closed_when_OPEN_indicates_an_error)
2462 {
2463 // arrange
2464 CONCRETE_IO_HANDLE wsio;
2465
2466 wsio = wsio_get_interface_description()->concrete_io_create(&default_wsio_config);
2467 (void)wsio_get_interface_description()->concrete_io_open(wsio, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
2468 g_on_ws_open_complete(g_on_ws_open_complete_context, WS_OPEN_OK);
2469 umock_c_reset_all_calls();
2470
2471 STRICT_EXPECTED_CALL(test_on_io_error((void*)0x4244));
2472
2473 // act
2474 g_on_ws_peer_closed(g_on_ws_peer_closed_context, NULL, NULL, 0);
2475
2476 // assert
2477 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2478
2479 // cleanup
2480 wsio_get_interface_description()->concrete_io_destroy(wsio);
2481 }
2482
2483 /* Tests_SRS_WSIO_01_166: [ When on_underlying_ws_peer_closed and the state of the IO is OPEN an error shall be indicated by calling the on_io_error callback passed to wsio_open. ]*/
2484 /* Tests_SRS_WSIO_01_168: [ The close_code, extra_data and extra_data_length arguments shall be ignored. ]*/
2485 TEST_FUNCTION(on_underlying_ws_peer_closed_when_CLOSING_indicates_an_error)
2486 {
2487 // arrange
2488 CONCRETE_IO_HANDLE wsio;
2489
2490 wsio = wsio_get_interface_description()->concrete_io_create(&default_wsio_config);
2491 (void)wsio_get_interface_description()->concrete_io_open(wsio, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
2492 g_on_ws_open_complete(g_on_ws_open_complete_context, WS_OPEN_OK);
2493 (void)wsio_get_interface_description()->concrete_io_close(wsio, test_on_io_close_complete, (void*)0x4245);
2494 umock_c_reset_all_calls();
2495
2496 STRICT_EXPECTED_CALL(test_on_io_error((void*)0x4244));
2497
2498 // act
2499 g_on_ws_peer_closed(g_on_ws_peer_closed_context, NULL, NULL, 0);
2500
2501 // assert
2502 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2503
2504 // cleanup
2505 wsio_get_interface_description()->concrete_io_destroy(wsio);
2506 }
2507
2508 // Tests_SRS_WSIO_07_001: [When on_underlying_ws_peer_closed and the state of the IO is NOT_OPEN an error will be raised and the io_state will remain as NOT_OPEN]
2509 TEST_FUNCTION(on_underlying_ws_peer_closed_when_NOT_OPEN_indicates_an_error)
2510 {
2511 // arrange
2512 CONCRETE_IO_HANDLE wsio;
2513
2514 wsio = wsio_get_interface_description()->concrete_io_create(&default_wsio_config);
2515 (void)wsio_get_interface_description()->concrete_io_open(wsio, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
2516 g_on_ws_open_complete(g_on_ws_open_complete_context, WS_OPEN_OK);
2517 (void)wsio_get_interface_description()->concrete_io_close(wsio, test_on_io_close_complete, (void*)0x4245);
2518 umock_c_reset_all_calls();
2519
2520 STRICT_EXPECTED_CALL(test_on_io_error((void*)0x4244));
2521
2522 // act
2523 g_on_ws_peer_closed(g_on_ws_peer_closed_context, NULL, NULL, 0);
2524
2525 // assert
2526 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2527
2528 // cleanup
2529 wsio_get_interface_description()->concrete_io_destroy(wsio);
2530 }
2531
2532 /* Tests_SRS_WSIO_01_167: [ If on_underlying_ws_peer_closed is called with a NULL context it shall do nothing. ]*/
2533 TEST_FUNCTION(on_underlying_ws_peer_closed_with_NULL_context_does_nothing)
2534 {
2535 // arrange
2536 CONCRETE_IO_HANDLE wsio;
2537
2538 wsio = wsio_get_interface_description()->concrete_io_create(&default_wsio_config);
2539 (void)wsio_get_interface_description()->concrete_io_open(wsio, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
2540 g_on_ws_open_complete(g_on_ws_open_complete_context, WS_OPEN_OK);
2541 (void)wsio_get_interface_description()->concrete_io_close(wsio, test_on_io_close_complete, (void*)0x4245);
2542 umock_c_reset_all_calls();
2543
2544 // act
2545 g_on_ws_peer_closed(NULL, NULL, NULL, 0);
2546
2547 // assert
2548 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2549
2550 // cleanup
2551 wsio_get_interface_description()->concrete_io_destroy(wsio);
2552 }
2553
2554 END_TEST_SUITE(wsio_ut)