Debian Codegraph @

src/vendor/azure-uamqp-c/tests/cbs_ut/cbs_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 #else
8 #include <stdlib.h>
9 #include <stddef.h>
10 #include <stdbool.h>
11 #endif
12
13 #include "azure_macro_utils/macro_utils.h"
14 #include "testrunnerswitcher.h"
15 #include "umock_c/umock_c.h"
16 #include "umock_c/umocktypes_charptr.h"
17 #include "umock_c/umocktypes_bool.h"
18 #include "umock_c/umock_c_negative_tests.h"
19
20 static void* my_gballoc_malloc(size_t size)
21 {
22 return malloc(size);
23 }
24
25 static void* my_gballoc_calloc(size_t nmemb, size_t size)
26 {
27 return calloc(nmemb, size);
28 }
29
30 static void* my_gballoc_realloc(void* ptr, size_t size)
31 {
32 return realloc(ptr, size);
33 }
34
35 static void my_gballoc_free(void* ptr)
36 {
37 free(ptr);
38 }
39
40 #define ENABLE_MOCKS
41
42 #include "azure_c_shared_utility/gballoc.h"
43 #include "azure_c_shared_utility/singlylinkedlist.h"
44 #include "azure_uamqp_c/message.h"
45 #include "azure_uamqp_c/session.h"
46 #include "azure_uamqp_c/amqp_management.h"
47
48 #undef ENABLE_MOCKS
49
50 #include "azure_uamqp_c/cbs.h"
51
52 static TEST_MUTEX_HANDLE g_testByTest;
53
54 static SESSION_HANDLE test_session_handle = (SESSION_HANDLE)0x4242;
55 static AMQP_MANAGEMENT_HANDLE test_amqp_management_handle = (AMQP_MANAGEMENT_HANDLE)0x4243;
56 static MESSAGE_HANDLE test_message = (MESSAGE_HANDLE)0x4300;
57 static AMQP_VALUE test_token_value = (AMQP_VALUE)0x4301;
58 static AMQP_VALUE test_map_value = (AMQP_VALUE)0x4302;
59 static AMQP_VALUE test_name_propery_key = (AMQP_VALUE)0x4303;
60 static AMQP_VALUE test_name_propery_value = (AMQP_VALUE)0x4304;
61 static SINGLYLINKEDLIST_HANDLE test_singlylinkedlist = (SINGLYLINKEDLIST_HANDLE)0x4305;
62 static AMQP_VALUE test_default_amqp_value = (AMQP_VALUE)0x4306;
63 static MESSAGE_HANDLE test_response_message = (MESSAGE_HANDLE)0x4307;
64 static ON_AMQP_MANAGEMENT_OPEN_COMPLETE saved_on_amqp_management_open_complete;
65 static void* saved_on_amqp_management_open_complete_context;
66 static ON_AMQP_MANAGEMENT_ERROR saved_on_amqp_management_error;
67 static void* saved_on_amqp_management_error_context;
68 static ON_AMQP_MANAGEMENT_EXECUTE_OPERATION_COMPLETE saved_on_execute_operation_complete;
69 static void* saved_on_execute_operation_complete_context;
70 static ASYNC_OPERATION_HANDLE test_my_amqp_management_execute_operation_async_result = (ASYNC_OPERATION_HANDLE)0x4308;
71
72 #define SIZE_OF_CBS_OPERATION_STRUCT 48
73
74 MOCK_FUNCTION_WITH_CODE(, void, test_on_cbs_open_complete, void*, context, CBS_OPEN_COMPLETE_RESULT, open_complete_result);
75 MOCK_FUNCTION_END();
76 MOCK_FUNCTION_WITH_CODE(, void, test_on_cbs_error, void*, context);
77 MOCK_FUNCTION_END();
78 MOCK_FUNCTION_WITH_CODE(, void, test_on_cbs_put_token_complete, void*, context, CBS_OPERATION_RESULT, put_token_complete_result, unsigned int, status_code, const char*, status_description);
79 MOCK_FUNCTION_END();
80 MOCK_FUNCTION_WITH_CODE(, void, test_on_cbs_delete_token_complete, void*, context, CBS_OPERATION_RESULT, delete_token_complete_result, unsigned int, status_code, const char*, status_description);
81 MOCK_FUNCTION_END();
82
83 AMQP_MANAGEMENT_HANDLE my_amqp_management_create(SESSION_HANDLE session, const char* management_node)
84 {
85 (void)session;
86 (void)management_node;
87 return test_amqp_management_handle;
88 }
89
90 int my_amqp_management_open_async(AMQP_MANAGEMENT_HANDLE amqp_management, ON_AMQP_MANAGEMENT_OPEN_COMPLETE on_amqp_management_open_complete, void* on_amqp_management_open_complete_context, ON_AMQP_MANAGEMENT_ERROR on_amqp_management_error, void* on_amqp_management_error_context)
91 {
92 (void)amqp_management;
93 saved_on_amqp_management_open_complete = on_amqp_management_open_complete;
94 saved_on_amqp_management_open_complete_context = on_amqp_management_open_complete_context;
95 saved_on_amqp_management_error = on_amqp_management_error;
96 saved_on_amqp_management_error_context = on_amqp_management_error_context;
97 return 0;
98 }
99
100
101 ASYNC_OPERATION_HANDLE my_amqp_management_execute_operation_async(AMQP_MANAGEMENT_HANDLE amqp_management, const char* operation, const char* type, const char* locales, MESSAGE_HANDLE message, ON_AMQP_MANAGEMENT_EXECUTE_OPERATION_COMPLETE on_execute_operation_complete, void* on_execute_operation_complete_context)
102 {
103 (void)message;
104 (void)locales;
105 (void)type;
106 (void)operation;
107 (void)amqp_management;
108 saved_on_execute_operation_complete = on_execute_operation_complete;
109 saved_on_execute_operation_complete_context = on_execute_operation_complete_context;
110 return test_my_amqp_management_execute_operation_async_result;
111 }
112
113 static const void** list_items = NULL;
114 static size_t list_item_count = 0;
115
116 static LIST_ITEM_HANDLE add_to_list(const void* item)
117 {
118 const void** items = (const void**)my_gballoc_realloc((void*)list_items, (list_item_count + 1) * sizeof(item));
119 if (items != NULL)
120 {
121 list_items = items;
122 list_items[list_item_count++] = item;
123 }
124 return (LIST_ITEM_HANDLE)list_item_count;
125 }
126
127 static int my_singlylinkedlist_remove(SINGLYLINKEDLIST_HANDLE list, LIST_ITEM_HANDLE item)
128 {
129 size_t index = (size_t)item - 1;
130 (void)list;
131 (void)memmove((void*)&list_items[index], &list_items[index + 1], sizeof(const void*) * (list_item_count - index - 1));
132 list_item_count--;
133 if (list_item_count == 0)
134 {
135 my_gballoc_free((void*)list_items);
136 list_items = NULL;
137 }
138 return 0;
139 }
140
141 static LIST_ITEM_HANDLE my_singlylinkedlist_get_head_item(SINGLYLINKEDLIST_HANDLE list)
142 {
143 LIST_ITEM_HANDLE list_item_handle = NULL;
144 (void)list;
145 if (list_item_count > 0)
146 {
147 list_item_handle = (LIST_ITEM_HANDLE)1;
148 }
149 else
150 {
151 list_item_handle = NULL;
152 }
153 return list_item_handle;
154 }
155
156 static LIST_ITEM_HANDLE my_singlylinkedlist_add(SINGLYLINKEDLIST_HANDLE list, const void* item)
157 {
158 (void)list;
159 return add_to_list(item);
160 }
161
162 static const void* my_singlylinkedlist_item_get_value(LIST_ITEM_HANDLE item_handle)
163 {
164 return (const void*)list_items[(size_t)item_handle - 1];
165 }
166
167 static LIST_ITEM_HANDLE my_singlylinkedlist_find(SINGLYLINKEDLIST_HANDLE handle, LIST_MATCH_FUNCTION match_function, const void* match_context)
168 {
169 size_t i;
170 const void* found_item = NULL;
171 (void)handle;
172 for (i = 0; i < list_item_count; i++)
173 {
174 if (match_function((LIST_ITEM_HANDLE)list_items[i], match_context))
175 {
176 found_item = list_items[i];
177 break;
178 }
179 }
180 return (LIST_ITEM_HANDLE)found_item;
181 }
182
183 static int my_singlylinkedlist_remove_if(SINGLYLINKEDLIST_HANDLE list, LIST_CONDITION_FUNCTION condition_function, const void* match_context)
184 {
185 bool continue_processing = true;
186
187 for (size_t index = 0; continue_processing && index < list_item_count; index++)
188 {
189 if (condition_function(list_items[index], match_context, &continue_processing))
190 {
191 (void)mock_hook_singlylinkedlist_remove(list, (LIST_ITEM_HANDLE)(index + 1)); // See my_singlylinkedlist_remove to see why.
192 }
193 }
194
195 return 0;
196 }
197
198 MU_DEFINE_ENUM_STRINGS(UMOCK_C_ERROR_CODE, UMOCK_C_ERROR_CODE_VALUES)
199 TEST_DEFINE_ENUM_TYPE(CBS_OPEN_COMPLETE_RESULT, CBS_OPEN_COMPLETE_RESULT_VALUES);
200 IMPLEMENT_UMOCK_C_ENUM_TYPE(CBS_OPEN_COMPLETE_RESULT, CBS_OPEN_COMPLETE_RESULT_VALUES);
201 TEST_DEFINE_ENUM_TYPE(CBS_OPERATION_RESULT, CBS_OPERATION_RESULT_VALUES);
202 IMPLEMENT_UMOCK_C_ENUM_TYPE(CBS_OPERATION_RESULT, CBS_OPERATION_RESULT_VALUES);
203
204 static void ASYNC_OPERATION_HANDLE_ToString(char* string, size_t bufferSize, ASYNC_OPERATION_HANDLE val)
205 {
206 (void)bufferSize;
207 (void)sprintf(string, "%p", val);
208 }
209
210 static int ASYNC_OPERATION_HANDLE_Compare(ASYNC_OPERATION_HANDLE left, ASYNC_OPERATION_HANDLE right)
211 {
212 return left != right;
213 }
214
215 typedef struct ASYNC_OPERATION_CONTEXT_STRUCT_TEST_TAG
216 {
217 ASYNC_OPERATION_CANCEL_HANDLER_FUNC async_operation_cancel_handler;
218 unsigned char context[SIZE_OF_CBS_OPERATION_STRUCT]; // This block of memory will be used in cbs.c for the CBS_OPERATION instance.
219 } ASYNC_OPERATION_CONTEXT_STRUCT_TEST;
220
221 static ASYNC_OPERATION_HANDLE my_async_operation_create(ASYNC_OPERATION_CANCEL_HANDLER_FUNC async_operation_cancel_handler, size_t context_size)
222 {
223 (void)context_size;
224 ASYNC_OPERATION_CONTEXT_STRUCT_TEST* result = my_gballoc_malloc(sizeof(ASYNC_OPERATION_CONTEXT_STRUCT_TEST));
225 memset(result, 0, sizeof(ASYNC_OPERATION_CONTEXT_STRUCT_TEST));
226 result->async_operation_cancel_handler = async_operation_cancel_handler;
227
228 return (ASYNC_OPERATION_HANDLE)result;
229 }
230
231 static void my_async_operation_destroy(ASYNC_OPERATION_HANDLE async_operation)
232 {
233 my_gballoc_free(async_operation);
234 }
235
236 static void on_umock_c_error(UMOCK_C_ERROR_CODE error_code)
237 {
238 ASSERT_FAIL("umock_c reported error :%" PRI_MU_ENUM "", MU_ENUM_VALUE(UMOCK_C_ERROR_CODE, error_code));
239 }
240
241 BEGIN_TEST_SUITE(cbs_ut)
242
243 TEST_SUITE_INITIALIZE(suite_init)
244 {
245 int result;
246
247 g_testByTest = TEST_MUTEX_CREATE();
248 ASSERT_IS_NOT_NULL(g_testByTest);
249
250 umock_c_init(on_umock_c_error);
251
252 result = umocktypes_charptr_register_types();
253 ASSERT_ARE_EQUAL(int, 0, result);
254
255 result = umocktypes_bool_register_types();
256 ASSERT_ARE_EQUAL(int, 0, result);
257
258 REGISTER_GLOBAL_MOCK_HOOK(gballoc_malloc, my_gballoc_malloc);
259 REGISTER_GLOBAL_MOCK_HOOK(gballoc_calloc, my_gballoc_calloc);
260 REGISTER_GLOBAL_MOCK_HOOK(gballoc_free, my_gballoc_free);
261 REGISTER_GLOBAL_MOCK_HOOK(amqp_management_create, my_amqp_management_create);
262 REGISTER_GLOBAL_MOCK_HOOK(amqp_management_open_async, my_amqp_management_open_async);
263 REGISTER_GLOBAL_MOCK_HOOK(amqp_management_execute_operation_async, my_amqp_management_execute_operation_async);
264 REGISTER_GLOBAL_MOCK_RETURN(amqpvalue_create_string, test_default_amqp_value);
265 REGISTER_GLOBAL_MOCK_RETURN(amqpvalue_create_map, test_default_amqp_value);
266 REGISTER_GLOBAL_MOCK_RETURN(amqpvalue_set_map_value, 0);
267 REGISTER_TYPE(CBS_OPEN_COMPLETE_RESULT, CBS_OPEN_COMPLETE_RESULT);
268 REGISTER_TYPE(CBS_OPERATION_RESULT, CBS_OPERATION_RESULT);
269 REGISTER_GLOBAL_MOCK_RETURN(message_create, test_message);
270 REGISTER_GLOBAL_MOCK_RETURN(singlylinkedlist_create, test_singlylinkedlist);
271 REGISTER_GLOBAL_MOCK_HOOK(singlylinkedlist_get_head_item, my_singlylinkedlist_get_head_item);
272 REGISTER_GLOBAL_MOCK_HOOK(singlylinkedlist_remove, my_singlylinkedlist_remove);
273 REGISTER_GLOBAL_MOCK_HOOK(singlylinkedlist_add, my_singlylinkedlist_add);
274 REGISTER_GLOBAL_MOCK_HOOK(singlylinkedlist_item_get_value, my_singlylinkedlist_item_get_value);
275 REGISTER_GLOBAL_MOCK_HOOK(singlylinkedlist_find, my_singlylinkedlist_find);
276 REGISTER_GLOBAL_MOCK_HOOK(singlylinkedlist_remove_if, my_singlylinkedlist_remove_if);
277 REGISTER_UMOCKC_PAIRED_CREATE_DESTROY_CALLS(amqp_management_create, amqp_management_destroy);
278 REGISTER_UMOCKC_PAIRED_CREATE_DESTROY_CALLS(message_create, message_destroy);
279 REGISTER_UMOCKC_PAIRED_CREATE_DESTROY_CALLS(properties_create, properties_destroy);
280 REGISTER_GLOBAL_MOCK_HOOK(async_operation_create, my_async_operation_create);
281 REGISTER_GLOBAL_MOCK_HOOK(async_operation_destroy, my_async_operation_destroy);
282
283 REGISTER_UMOCK_ALIAS_TYPE(CBS_HANDLE, void*);
284 REGISTER_UMOCK_ALIAS_TYPE(SESSION_HANDLE, void*);
285 REGISTER_UMOCK_ALIAS_TYPE(AMQP_MANAGEMENT_HANDLE, void*);
286 REGISTER_UMOCK_ALIAS_TYPE(ON_AMQP_MANAGEMENT_OPEN_COMPLETE, void*);
287 REGISTER_UMOCK_ALIAS_TYPE(ON_AMQP_MANAGEMENT_ERROR, void*);
288 REGISTER_UMOCK_ALIAS_TYPE(MESSAGE_HANDLE, void*);
289 REGISTER_UMOCK_ALIAS_TYPE(AMQP_VALUE, void*);
290 REGISTER_UMOCK_ALIAS_TYPE(ON_AMQP_MANAGEMENT_EXECUTE_OPERATION_COMPLETE, void*);
291 REGISTER_UMOCK_ALIAS_TYPE(SINGLYLINKEDLIST_HANDLE, void*);
292 REGISTER_UMOCK_ALIAS_TYPE(LIST_ITEM_HANDLE, void*);
293 REGISTER_UMOCK_ALIAS_TYPE(LIST_CONDITION_FUNCTION, void*);
294 REGISTER_UMOCK_ALIAS_TYPE(ASYNC_OPERATION_HANDLE, void*);
295 REGISTER_UMOCK_ALIAS_TYPE(ASYNC_OPERATION_CANCEL_HANDLER_FUNC, void*);
296 }
297
298 TEST_SUITE_CLEANUP(suite_cleanup)
299 {
300 umock_c_deinit();
301
302 TEST_MUTEX_DESTROY(g_testByTest);
303 }
304
305 TEST_FUNCTION_INITIALIZE(test_init)
306 {
307 if (TEST_MUTEX_ACQUIRE(g_testByTest))
308 {
309 ASSERT_FAIL("our mutex is ABANDONED. Failure in test framework");
310 }
311
312 umock_c_reset_all_calls();
313 }
314
315 TEST_FUNCTION_CLEANUP(test_cleanup)
316 {
317 TEST_MUTEX_RELEASE(g_testByTest);
318 }
319
320 /* cbs_create */
321
322 /* Tests_SRS_CBS_01_001: [ `cbs_create` shall create a new CBS instance and on success return a non-NULL handle to it. ]*/
323 /* Tests_SRS_CBS_01_034: [ `cbs_create` shall create an AMQP management handle by calling `amqp_management_create`. ]*/
324 /* Tests_SRS_CBS_01_097: [ `cbs_create` shall create a singly linked list for pending operations by calling `singlylinkedlist_create`. ]*/
325 /* Tests_SRS_CBS_01_002: [ Tokens are communicated between AMQP peers by sending specially-formatted AMQP messages to the Claims-based Security Node. ]*/
326 /* Tests_SRS_CBS_01_003: [ The mechanism follows the scheme defined in the AMQP Management specification [AMQPMAN]. ]*/
327 /* Tests_SRS_CBS_01_117: [ `cbs_create` shall set the override status code key name on the AMQP management handle to `status-code` by calling `amqp_management_set_override_status_code_key_name`. ]*/
328 /* Tests_SRS_CBS_01_118: [ `cbs_create` shall set the override status description key name on the AMQP management handle to `status-description` by calling `amqp_management_set_override_status_description_key_name`. ]*/
329 TEST_FUNCTION(cbs_create_returns_a_valid_handle)
330 {
331 // arrange
332 CBS_HANDLE cbs;
333 STRICT_EXPECTED_CALL(gballoc_calloc(IGNORED_NUM_ARG, IGNORED_NUM_ARG));
334 STRICT_EXPECTED_CALL(singlylinkedlist_create());
335 STRICT_EXPECTED_CALL(amqp_management_create(test_session_handle, "$cbs"));
336 STRICT_EXPECTED_CALL(amqp_management_set_override_status_code_key_name(test_amqp_management_handle, "status-code"));
337 STRICT_EXPECTED_CALL(amqp_management_set_override_status_description_key_name(test_amqp_management_handle, "status-description"));
338
339 // act
340 cbs = cbs_create(test_session_handle);
341
342 // assert
343 ASSERT_IS_NOT_NULL(cbs);
344 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
345
346 // cleanup
347 cbs_destroy(cbs);
348 }
349
350 /* Tests_SRS_CBS_01_033: [** If `session` is NULL then `cbs_create` shall fail and return NULL. ]*/
351 TEST_FUNCTION(cbs_create_with_NULL_session_handle_fails)
352 {
353 // arrange
354 CBS_HANDLE cbs;
355
356 // act
357 cbs = cbs_create(NULL);
358
359 // assert
360 ASSERT_IS_NULL(cbs);
361 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
362 }
363
364 /* Tests_SRS_CBS_01_035: [ If `amqp_management_create` fails then `cbs_create` shall fail and return NULL. ]*/
365 /* Tests_SRS_CBS_01_076: [ If allocating memory for the new handle fails, `cbs_create` shall fail and return NULL. ]*/
366 /* Tests_SRS_CBS_01_101: [ If `singlylinkedlist_create` fails, `cbs_create` shall fail and return NULL. ]*/
367 /* Tests_SRS_CBS_01_116: [ If setting the override key names fails, then `cbs_create` shall fail and return NULL. ]*/
368 TEST_FUNCTION(when_one_of_the_functions_called_by_cbs_create_fails_then_cbs_create_fails)
369 {
370 // arrange
371 int negativeTestsInitResult = umock_c_negative_tests_init();
372 size_t count;
373 size_t index;
374 ASSERT_ARE_EQUAL(int, 0, negativeTestsInitResult);
375
376 STRICT_EXPECTED_CALL(gballoc_calloc(IGNORED_NUM_ARG, IGNORED_NUM_ARG))
377 .SetFailReturn(NULL);
378 STRICT_EXPECTED_CALL(singlylinkedlist_create())
379 .SetReturn(NULL);
380 STRICT_EXPECTED_CALL(amqp_management_create(test_session_handle, "$cbs"))
381 .SetFailReturn(NULL);
382 STRICT_EXPECTED_CALL(amqp_management_set_override_status_code_key_name(test_amqp_management_handle, "status-code"))
383 .SetFailReturn(1);
384 STRICT_EXPECTED_CALL(amqp_management_set_override_status_description_key_name(test_amqp_management_handle, "status-description"))
385 .SetFailReturn(1);
386 umock_c_negative_tests_snapshot();
387
388 count = umock_c_negative_tests_call_count();
389 for (index = 0; index < count; index++)
390 {
391 char tmp_msg[128];
392 CBS_HANDLE cbs;
393 (void)sprintf(tmp_msg, "Failure in test %u/%u", (unsigned int)(index + 1), (unsigned int)count);
394
395 umock_c_negative_tests_reset();
396 umock_c_negative_tests_fail_call(index);
397
398 // act
399 cbs = cbs_create(test_session_handle);
400
401 // assert
402 ASSERT_IS_NULL(cbs, tmp_msg);
403 }
404
405 // cleanup
406 umock_c_negative_tests_deinit();
407 }
408
409 /* cbs_destroy */
410
411 /* Tests_SRS_CBS_01_036: [ `cbs_destroy` shall free all resources associated with the handle `cbs`. ]*/
412 /* Tests_SRS_CBS_01_038: [ `cbs_destroy` shall free the AMQP management handle created in `cbs_create` by calling `amqp_management_destroy`. ]*/
413 /* Tests_SRS_CBS_01_098: [ `cbs_destroy` shall free the pending operations list by calling `singlylinkedlist_destroy`. ]*/
414 /* Tests_SRS_CBS_01_099: [ All pending operations shall be freed. ]*/
415 TEST_FUNCTION(cbs_destroy_frees_all_resources)
416 {
417 // arrange
418 CBS_HANDLE cbs;
419 cbs = cbs_create(test_session_handle);
420 umock_c_reset_all_calls();
421
422 STRICT_EXPECTED_CALL(amqp_management_destroy(test_amqp_management_handle));
423 STRICT_EXPECTED_CALL(singlylinkedlist_get_head_item(test_singlylinkedlist));
424 STRICT_EXPECTED_CALL(singlylinkedlist_destroy(test_singlylinkedlist));
425 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG));
426
427 // act
428 cbs_destroy(cbs);
429
430 // assert
431 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
432 }
433
434 /* Tests_SRS_CBS_01_099: [ All pending operations shall be freed. ]*/
435 /* Tests_SRS_CBS_01_100: [ If the CBS instance is not closed, all actions performed by `cbs_close` shall be performed. ]*/
436 TEST_FUNCTION(cbs_destroy_frees_all_resources_including_the_pending_operations)
437 {
438 // arrange
439 CBS_HANDLE cbs;
440 cbs = cbs_create(test_session_handle);
441 (void)cbs_open_async(cbs, test_on_cbs_open_complete, (void*)0x4242, test_on_cbs_error, (void*)0x4243);
442 saved_on_amqp_management_open_complete(saved_on_amqp_management_open_complete_context, AMQP_MANAGEMENT_OPEN_OK);
443 (void)cbs_put_token_async(cbs, "some_type", "my_audience", "blah_token", test_on_cbs_put_token_complete, (void*)0x4244);
444 umock_c_reset_all_calls();
445
446 STRICT_EXPECTED_CALL(amqp_management_close(test_amqp_management_handle));
447 STRICT_EXPECTED_CALL(amqp_management_destroy(test_amqp_management_handle));
448 STRICT_EXPECTED_CALL(singlylinkedlist_get_head_item(test_singlylinkedlist));
449 STRICT_EXPECTED_CALL(singlylinkedlist_item_get_value(IGNORED_PTR_ARG));
450 STRICT_EXPECTED_CALL(test_on_cbs_put_token_complete((void*)0x4244, CBS_OPERATION_RESULT_INSTANCE_CLOSED, 0, NULL));
451 STRICT_EXPECTED_CALL(async_operation_destroy(IGNORED_PTR_ARG));
452 STRICT_EXPECTED_CALL(singlylinkedlist_remove(test_singlylinkedlist, IGNORED_PTR_ARG));
453 STRICT_EXPECTED_CALL(singlylinkedlist_get_head_item(test_singlylinkedlist));
454 STRICT_EXPECTED_CALL(singlylinkedlist_destroy(test_singlylinkedlist));
455 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG));
456
457 // act
458 cbs_destroy(cbs);
459
460 // assert
461 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
462 }
463
464 /* Tests_SRS_CBS_01_037: [ If `cbs` is NULL, `cbs_destroy` shall do nothing. ]*/
465 TEST_FUNCTION(cbs_destroy_with_NULL_handle_does_nothing)
466 {
467 // arrange
468
469 // act
470 cbs_destroy(NULL);
471
472 // assert
473 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
474 }
475
476 /* cbs_open_async */
477
478 /* Tests_SRS_CBS_01_039: [ `cbs_open_async` shall open the cbs communication by calling `amqp_management_open_async` on the AMQP management handle created in `cbs_create`. ]*/
479 /* Tests_SRS_CBS_01_077: [ On success, `cbs_open_async` shall return 0. ]*/
480 /* Tests_SRS_CBS_01_078: [ The cbs instance shall be considered OPENING until the `on_amqp_management_open_complete` callback is called by the AMQP management instance indicating that the open has completed (succesfull or not). ]*/
481 TEST_FUNCTION(cbs_open_async_opens_the_amqp_management_instance)
482 {
483 // arrange
484 CBS_HANDLE cbs;
485 int result;
486 cbs = cbs_create(test_session_handle);
487 umock_c_reset_all_calls();
488
489 STRICT_EXPECTED_CALL(amqp_management_open_async(test_amqp_management_handle, IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG));
490
491 // act
492 result = cbs_open_async(cbs, test_on_cbs_open_complete, (void*)0x4242, test_on_cbs_error, (void*)0x4243);
493
494 // assert
495 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
496 ASSERT_ARE_EQUAL(int, 0, result);
497
498 // cleanup
499 cbs_destroy(cbs);
500 }
501
502 /* Tests_SRS_CBS_01_040: [ If any of the arguments `cbs`, `on_cbs_open_complete` or `on_cbs_error` is NULL, then `cbs_open_async` shall fail and return a non-zero value. ]*/
503 TEST_FUNCTION(cbs_open_async_with_NULL_handle_fails)
504 {
505 // arrange
506 int result;
507 umock_c_reset_all_calls();
508
509 // act
510 result = cbs_open_async(NULL, test_on_cbs_open_complete, (void*)0x4242, test_on_cbs_error, (void*)0x4243);
511 ASSERT_ARE_NOT_EQUAL(int, 0, result);
512
513 // assert
514 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
515 }
516
517 /* Tests_SRS_CBS_01_040: [ If any of the arguments `cbs`, `on_cbs_open_complete` or `on_cbs_error` is NULL, then `cbs_open_async` shall fail and return a non-zero value. ]*/
518 TEST_FUNCTION(cbs_open_async_with_NULL_on_open_complete_callback_fails)
519 {
520 // arrange
521 CBS_HANDLE cbs;
522 int result;
523 cbs = cbs_create(test_session_handle);
524 umock_c_reset_all_calls();
525
526 // act
527 result = cbs_open_async(cbs, NULL, (void*)0x4242, test_on_cbs_error, (void*)0x4243);
528
529 // assert
530 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
531 ASSERT_ARE_NOT_EQUAL(int, 0, result);
532
533 // cleanup
534 cbs_destroy(cbs);
535 }
536
537 /* Tests_SRS_CBS_01_040: [ If any of the arguments `cbs`, `on_cbs_open_complete` or `on_cbs_error` is NULL, then `cbs_open_async` shall fail and return a non-zero value. ]*/
538 TEST_FUNCTION(cbs_open_async_with_NULL_on_error_callback_fails)
539 {
540 // arrange
541 CBS_HANDLE cbs;
542 int result;
543 cbs = cbs_create(test_session_handle);
544 umock_c_reset_all_calls();
545
546 // act
547 result = cbs_open_async(cbs, test_on_cbs_open_complete, (void*)0x4242, NULL, (void*)0x4243);
548
549 // assert
550 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
551 ASSERT_ARE_NOT_EQUAL(int, 0, result);
552
553 // cleanup
554 cbs_destroy(cbs);
555 }
556
557 /* Tests_SRS_CBS_01_041: [ If `amqp_management_open_async` fails, shall fail and return a non-zero value. ]*/
558 TEST_FUNCTION(when_amqpmanagement_open_fails_cbs_open_async_fails)
559 {
560 // arrange
561 CBS_HANDLE cbs;
562 int result;
563 cbs = cbs_create(test_session_handle);
564 umock_c_reset_all_calls();
565
566 STRICT_EXPECTED_CALL(amqp_management_open_async(test_amqp_management_handle, IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
567 .SetReturn(1);
568
569 // act
570 result = cbs_open_async(cbs, test_on_cbs_open_complete, (void*)0x4242, test_on_cbs_error, (void*)0x4243);
571
572 // assert
573 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
574 ASSERT_ARE_NOT_EQUAL(int, 0, result);
575
576 // cleanup
577 cbs_destroy(cbs);
578 }
579
580 /* Tests_SRS_CBS_01_042: [ `on_cbs_open_complete_context` and `on_cbs_error_context` shall be allowed to be NULL. ]*/
581 TEST_FUNCTION(cbs_open_async_with_NULL_on_open_complete_context_succeeds)
582 {
583 // arrange
584 CBS_HANDLE cbs;
585 int result;
586 cbs = cbs_create(test_session_handle);
587 umock_c_reset_all_calls();
588
589 STRICT_EXPECTED_CALL(amqp_management_open_async(test_amqp_management_handle, IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG));
590
591 // act
592 result = cbs_open_async(cbs, test_on_cbs_open_complete, NULL, test_on_cbs_error, (void*)0x4243);
593
594 // assert
595 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
596 ASSERT_ARE_EQUAL(int, 0, result);
597
598 // cleanup
599 cbs_destroy(cbs);
600 }
601
602 /* Tests_SRS_CBS_01_042: [ `on_cbs_open_complete_context` and `on_cbs_error_context` shall be allowed to be NULL. ]*/
603 TEST_FUNCTION(cbs_open_async_with_NULL_on_error_context_succeeds)
604 {
605 // arrange
606 CBS_HANDLE cbs;
607 int result;
608 cbs = cbs_create(test_session_handle);
609 umock_c_reset_all_calls();
610
611 STRICT_EXPECTED_CALL(amqp_management_open_async(test_amqp_management_handle, IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG));
612
613 // act
614 result = cbs_open_async(cbs, test_on_cbs_open_complete, (void*)0x4243, test_on_cbs_error, NULL);
615
616 // assert
617 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
618 ASSERT_ARE_EQUAL(int, 0, result);
619
620 // cleanup
621 cbs_destroy(cbs);
622 }
623
624 /* Tests_SRS_CBS_01_043: [ `cbs_open_async` while already open or opening shall fail and return a non-zero value. ]*/
625 TEST_FUNCTION(cbs_open_async_while_opening_fails)
626 {
627 // arrange
628 CBS_HANDLE cbs;
629 int result;
630 cbs = cbs_create(test_session_handle);
631 (void)cbs_open_async(cbs, test_on_cbs_open_complete, (void*)0x4243, test_on_cbs_error, NULL);
632 umock_c_reset_all_calls();
633
634 // act
635 result = cbs_open_async(cbs, test_on_cbs_open_complete, (void*)0x4243, test_on_cbs_error, NULL);
636
637 // assert
638 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
639 ASSERT_ARE_NOT_EQUAL(int, 0, result);
640
641 // cleanup
642 cbs_destroy(cbs);
643 }
644
645 /* Tests_SRS_CBS_01_043: [ `cbs_open_async` while already open or opening shall fail and return a non-zero value. ]*/
646 TEST_FUNCTION(cbs_open_async_while_already_open_fails)
647 {
648 // arrange
649 CBS_HANDLE cbs;
650 int result;
651 cbs = cbs_create(test_session_handle);
652 (void)cbs_open_async(cbs, test_on_cbs_open_complete, (void*)0x4242, test_on_cbs_error, (void*)0x4243);
653 saved_on_amqp_management_open_complete(saved_on_amqp_management_open_complete_context, AMQP_MANAGEMENT_OPEN_OK);
654 umock_c_reset_all_calls();
655
656 // act
657 result = cbs_open_async(cbs, test_on_cbs_open_complete, (void*)0x4243, test_on_cbs_error, NULL);
658
659 // assert
660 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
661 ASSERT_ARE_NOT_EQUAL(int, 0, result);
662
663 // cleanup
664 cbs_destroy(cbs);
665 }
666
667 /* Tests_SRS_CBS_01_039: [ `cbs_open_async` shall open the cbs communication by calling `amqp_management_open_async` on the AMQP management handle created in `cbs_create`. ]*/
668 /* Tests_SRS_CBS_01_077: [ On success, `cbs_open_async` shall return 0. ]*/
669 TEST_FUNCTION(after_an_open_cancelled_due_to_amqp_management_cbs_open_async_still_succeeds)
670 {
671 // arrange
672 CBS_HANDLE cbs;
673 int result;
674 cbs = cbs_create(test_session_handle);
675 (void)cbs_open_async(cbs, test_on_cbs_open_complete, (void*)0x4242, test_on_cbs_error, (void*)0x4243);
676 saved_on_amqp_management_open_complete(saved_on_amqp_management_open_complete_context, AMQP_MANAGEMENT_OPEN_CANCELLED);
677 umock_c_reset_all_calls();
678
679 STRICT_EXPECTED_CALL(amqp_management_open_async(test_amqp_management_handle, IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG));
680
681 // act
682 result = cbs_open_async(cbs, test_on_cbs_open_complete, (void*)0x4242, test_on_cbs_error, (void*)0x4243);
683
684 // assert
685 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
686 ASSERT_ARE_EQUAL(int, 0, result);
687
688 // cleanup
689 cbs_destroy(cbs);
690 }
691
692 /* Tests_SRS_CBS_01_039: [ `cbs_open_async` shall open the cbs communication by calling `amqp_management_open_async` on the AMQP management handle created in `cbs_create`. ]*/
693 /* Tests_SRS_CBS_01_077: [ On success, `cbs_open_async` shall return 0. ]*/
694 TEST_FUNCTION(after_an_open_error_due_to_amqp_management_cbs_open_async_still_succeeds)
695 {
696 // arrange
697 CBS_HANDLE cbs;
698 int result;
699 cbs = cbs_create(test_session_handle);
700 (void)cbs_open_async(cbs, test_on_cbs_open_complete, (void*)0x4242, test_on_cbs_error, (void*)0x4243);
701 saved_on_amqp_management_open_complete(saved_on_amqp_management_open_complete_context, AMQP_MANAGEMENT_OPEN_ERROR);
702 umock_c_reset_all_calls();
703
704 STRICT_EXPECTED_CALL(amqp_management_open_async(test_amqp_management_handle, IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG));
705
706 // act
707 result = cbs_open_async(cbs, test_on_cbs_open_complete, (void*)0x4242, test_on_cbs_error, (void*)0x4243);
708
709 // assert
710 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
711 ASSERT_ARE_EQUAL(int, 0, result);
712
713 // cleanup
714 cbs_destroy(cbs);
715 }
716
717 /* Tests_SRS_CBS_01_039: [ `cbs_open_async` shall open the cbs communication by calling `amqp_management_open_async` on the AMQP management handle created in `cbs_create`. ]*/
718 /* Tests_SRS_CBS_01_077: [ On success, `cbs_open_async` shall return 0. ]*/
719 TEST_FUNCTION(after_an_open_error_due_to_amqp_management_error_callback_cbs_open_async_still_succeeds)
720 {
721 // arrange
722 CBS_HANDLE cbs;
723 int result;
724 cbs = cbs_create(test_session_handle);
725 (void)cbs_open_async(cbs, test_on_cbs_open_complete, (void*)0x4242, test_on_cbs_error, (void*)0x4243);
726 saved_on_amqp_management_error(saved_on_amqp_management_open_complete_context);
727 umock_c_reset_all_calls();
728
729 STRICT_EXPECTED_CALL(amqp_management_open_async(test_amqp_management_handle, IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG));
730
731 // act
732 result = cbs_open_async(cbs, test_on_cbs_open_complete, (void*)0x4242, test_on_cbs_error, (void*)0x4243);
733
734 // assert
735 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
736 ASSERT_ARE_EQUAL(int, 0, result);
737
738 // cleanup
739 cbs_destroy(cbs);
740 }
741
742 /* cbs_close */
743
744 /* Tests_SRS_CBS_01_044: [ `cbs_close` shall close the CBS instance by calling `amqp_management_close` on the underlying AMQP management handle. ]*/
745 /* Tests_SRS_CBS_01_080: [ On success, `cbs_close` shall return 0. ]*/
746 TEST_FUNCTION(cbs_close_closes_the_underlying_amqp_management_instance)
747 {
748 // arrange
749 CBS_HANDLE cbs;
750 int result;
751 cbs = cbs_create(test_session_handle);
752 (void)cbs_open_async(cbs, test_on_cbs_open_complete, (void*)0x4242, test_on_cbs_error, (void*)0x4243);
753 saved_on_amqp_management_open_complete(saved_on_amqp_management_open_complete_context, AMQP_MANAGEMENT_OPEN_OK);
754 umock_c_reset_all_calls();
755
756 STRICT_EXPECTED_CALL(amqp_management_close(test_amqp_management_handle));
757
758 // act
759 result = cbs_close(cbs);
760
761 // assert
762 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
763 ASSERT_ARE_EQUAL(int, 0, result);
764
765 // cleanup
766 cbs_destroy(cbs);
767 }
768
769 /* Tests_SRS_CBS_01_079: [ If `cbs_close` is called while OPENING, the `on_cbs_open_complete` callback shall be called with `CBS_OPEN_CANCELLED`, while passing the `on_cbs_open_complete_context` as argument. ]*/
770 /* Tests_SRS_CBS_01_080: [ On success, `cbs_close` shall return 0. ]*/
771 TEST_FUNCTION(cbs_close_when_opening_cancels_the_open)
772 {
773 // arrange
774 CBS_HANDLE cbs;
775 int result;
776 cbs = cbs_create(test_session_handle);
777 (void)cbs_open_async(cbs, test_on_cbs_open_complete, (void*)0x4242, test_on_cbs_error, (void*)0x4243);
778 umock_c_reset_all_calls();
779
780 STRICT_EXPECTED_CALL(amqp_management_close(test_amqp_management_handle));
781 STRICT_EXPECTED_CALL(test_on_cbs_open_complete((void*)0x4242, CBS_OPEN_CANCELLED));
782
783 // act
784 result = cbs_close(cbs);
785
786 // assert
787 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
788 ASSERT_ARE_EQUAL(int, 0, result);
789
790 // cleanup
791 cbs_destroy(cbs);
792 }
793
794 /* Tests_SRS_CBS_01_045: [ If the argument `cbs` is NULL, `cbs_close` shall fail and return a non-zero value. ]*/
795 TEST_FUNCTION(cbs_close_with_NULL_handle_fails)
796 {
797 // arrange
798 int result;
799
800 // act
801 result = cbs_close(NULL);
802
803 // assert
804 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
805 ASSERT_ARE_NOT_EQUAL(int, 0, result);
806 }
807
808 /* Tests_SRS_CBS_01_046: [ If `amqp_management_close` fails, `cbs_close` shall fail and return a non-zero value. ]*/
809 TEST_FUNCTION(when_amqpmanagement_close_fails_then_cbs_close_fails)
810 {
811 // arrange
812 CBS_HANDLE cbs;
813 int result;
814 cbs = cbs_create(test_session_handle);
815 (void)cbs_open_async(cbs, test_on_cbs_open_complete, (void*)0x4242, test_on_cbs_error, (void*)0x4243);
816 saved_on_amqp_management_open_complete(saved_on_amqp_management_open_complete_context, AMQP_MANAGEMENT_OPEN_OK);
817 umock_c_reset_all_calls();
818
819 STRICT_EXPECTED_CALL(amqp_management_close(test_amqp_management_handle))
820 .SetReturn(1);
821
822 // act
823 result = cbs_close(cbs);
824
825 // assert
826 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
827 ASSERT_ARE_NOT_EQUAL(int, 0, result);
828
829 // cleanup
830 cbs_destroy(cbs);
831 }
832
833 /* Tests_SRS_CBS_01_047: [ `cbs_close` when closed shall fail and return a non-zero value. ]*/
834 TEST_FUNCTION(cbs_close_after_cbs_close_fails)
835 {
836 // arrange
837 CBS_HANDLE cbs;
838 int result;
839 cbs = cbs_create(test_session_handle);
840 (void)cbs_open_async(cbs, test_on_cbs_open_complete, (void*)0x4242, test_on_cbs_error, (void*)0x4243);
841 saved_on_amqp_management_open_complete(saved_on_amqp_management_open_complete_context, AMQP_MANAGEMENT_OPEN_OK);
842 (void)cbs_close(cbs);
843 umock_c_reset_all_calls();
844
845 // act
846 result = cbs_close(cbs);
847
848 // assert
849 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
850 ASSERT_ARE_NOT_EQUAL(int, 0, result);
851
852 // cleanup
853 cbs_destroy(cbs);
854 }
855
856 /* Tests_SRS_CBS_01_048: [ `cbs_close` when not opened shall fail and return a non-zero value. ]*/
857 TEST_FUNCTION(cbs_close_when_not_open_fails)
858 {
859 // arrange
860 CBS_HANDLE cbs;
861 int result;
862 cbs = cbs_create(test_session_handle);
863 umock_c_reset_all_calls();
864
865 // act
866 result = cbs_close(cbs);
867
868 // assert
869 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
870 ASSERT_ARE_NOT_EQUAL(int, 0, result);
871
872 // cleanup
873 cbs_destroy(cbs);
874 }
875
876 /* cbs_put_token_async */
877
878 /* Tests_SRS_CBS_01_049: [ `cbs_put_token_async` shall construct a request message for the `put-token` operation. ]*/
879 /* Tests_SRS_CBS_01_081: [ On success `cbs_put_token_async` shall return an ASYNC_OPERATION_HANDLE. ]*/
880 /* Tests_SRS_CBS_01_051: [ `cbs_put_token_async` shall start the AMQP management operation by calling `amqp_management_execute_operation_async`, while passing to it: ]*/
881 /* Tests_SRS_CBS_01_052: [ The `amqp_management` argument shall be the one for the AMQP management instance created in `cbs_create`. ]*/
882 /* Tests_SRS_CBS_01_053: [ The `operation` argument shall be `put-token`. ]*/
883 /* Tests_SRS_CBS_01_054: [ The `type` argument shall be set to the `type` argument. ]*/
884 /* Tests_SRS_CBS_01_055: [ The `locales` argument shall be set to NULL. ]*/
885 /* Tests_SRS_CBS_01_056: [ The `message` argument shall be the message constructed earlier according to the CBS spec. ]*/
886 /* Tests_SRS_CBS_01_057: [ The arguments `on_execute_operation_complete` and `context` shall be set to a callback that is to be called by the AMQP management module when the operation is complete. ]*/
887 /* Tests_SRS_CBS_01_005: [ operation No string "put-token" ]*/
888 /* Tests_SRS_CBS_01_006: [ Type No string The type of the token being put, e.g., "amqp:jwt". ]*/
889 /* Tests_SRS_CBS_01_007: [ name No string The "audience" to which the token applies. ]*/
890 /* Tests_SRS_CBS_01_009: [ The body of the message MUST contain the token. ]*/
891 TEST_FUNCTION(cbs_put_token_async_creates_the_message_and_starts_the_amqp_management_operation)
892 {
893 // arrange
894 CBS_HANDLE cbs;
895 ASYNC_OPERATION_HANDLE result;
896 cbs = cbs_create(test_session_handle);
897 (void)cbs_open_async(cbs, test_on_cbs_open_complete, (void*)0x4242, test_on_cbs_error, (void*)0x4243);
898 saved_on_amqp_management_open_complete(saved_on_amqp_management_open_complete_context, AMQP_MANAGEMENT_OPEN_OK);
899 umock_c_reset_all_calls();
900
901 STRICT_EXPECTED_CALL(message_create());
902 STRICT_EXPECTED_CALL(amqpvalue_create_string("blah_token"))
903 .SetReturn(test_token_value);
904 STRICT_EXPECTED_CALL(message_set_body_amqp_value(test_message, test_token_value));
905 STRICT_EXPECTED_CALL(amqpvalue_create_map())
906 .SetReturn(test_map_value);
907 STRICT_EXPECTED_CALL(amqpvalue_create_string("name"))
908 .SetReturn(test_name_propery_key);
909 STRICT_EXPECTED_CALL(amqpvalue_create_string("my_audience"))
910 .SetReturn(test_name_propery_value);
911 STRICT_EXPECTED_CALL(amqpvalue_set_map_value(test_map_value, test_name_propery_key, test_name_propery_value));
912 STRICT_EXPECTED_CALL(amqpvalue_destroy(test_name_propery_value));
913 STRICT_EXPECTED_CALL(amqpvalue_destroy(test_name_propery_key));
914 STRICT_EXPECTED_CALL(message_set_application_properties(test_message, test_map_value));
915 STRICT_EXPECTED_CALL(async_operation_create(IGNORED_PTR_ARG, IGNORED_NUM_ARG));
916 STRICT_EXPECTED_CALL(singlylinkedlist_add(test_singlylinkedlist, IGNORED_PTR_ARG));
917 STRICT_EXPECTED_CALL(amqp_management_execute_operation_async(test_amqp_management_handle, "put-token", "some_type", NULL, test_message, IGNORED_PTR_ARG, IGNORED_PTR_ARG));
918 STRICT_EXPECTED_CALL(amqpvalue_destroy(test_map_value));
919 STRICT_EXPECTED_CALL(amqpvalue_destroy(test_token_value));
920 STRICT_EXPECTED_CALL(message_destroy(test_message));
921
922 // act
923 result = cbs_put_token_async(cbs, "some_type", "my_audience", "blah_token", test_on_cbs_put_token_complete, (void*)0x4244);
924
925 // assert
926 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
927 ASSERT_ARE_NOT_EQUAL(ASYNC_OPERATION_HANDLE, NULL, result);
928
929 // cleanup
930 cbs_destroy(cbs);
931 }
932
933 /* Tests_SRS_CBS_09_001: [ The `ASYNC_OPERATION_HANDLE` cancel function shall cancel the underlying amqp management operation, remove this operation from the pending list, destroy this async operation. ] */
934 TEST_FUNCTION(when_cbs_put_token_async_is_cancelled_succeeds)
935 {
936 // arrange
937 CBS_HANDLE cbs;
938 ASYNC_OPERATION_HANDLE result;
939 cbs = cbs_create(test_session_handle);
940 (void)cbs_open_async(cbs, test_on_cbs_open_complete, (void*)0x4242, test_on_cbs_error, (void*)0x4243);
941 saved_on_amqp_management_open_complete(saved_on_amqp_management_open_complete_context, AMQP_MANAGEMENT_OPEN_OK);
942 umock_c_reset_all_calls();
943
944 STRICT_EXPECTED_CALL(message_create());
945 STRICT_EXPECTED_CALL(amqpvalue_create_string("blah_token"))
946 .SetReturn(test_token_value);
947 STRICT_EXPECTED_CALL(message_set_body_amqp_value(test_message, test_token_value));
948 STRICT_EXPECTED_CALL(amqpvalue_create_map())
949 .SetReturn(test_map_value);
950 STRICT_EXPECTED_CALL(amqpvalue_create_string("name"))
951 .SetReturn(test_name_propery_key);
952 STRICT_EXPECTED_CALL(amqpvalue_create_string("my_audience"))
953 .SetReturn(test_name_propery_value);
954 STRICT_EXPECTED_CALL(amqpvalue_set_map_value(test_map_value, test_name_propery_key, test_name_propery_value));
955 STRICT_EXPECTED_CALL(amqpvalue_destroy(test_name_propery_value));
956 STRICT_EXPECTED_CALL(amqpvalue_destroy(test_name_propery_key));
957 STRICT_EXPECTED_CALL(message_set_application_properties(test_message, test_map_value));
958 STRICT_EXPECTED_CALL(async_operation_create(IGNORED_PTR_ARG, IGNORED_NUM_ARG));
959 STRICT_EXPECTED_CALL(singlylinkedlist_add(test_singlylinkedlist, IGNORED_PTR_ARG));
960 STRICT_EXPECTED_CALL(amqp_management_execute_operation_async(test_amqp_management_handle, "put-token", "some_type", NULL, test_message, IGNORED_PTR_ARG, IGNORED_PTR_ARG));
961 STRICT_EXPECTED_CALL(amqpvalue_destroy(test_map_value));
962 STRICT_EXPECTED_CALL(amqpvalue_destroy(test_token_value));
963 STRICT_EXPECTED_CALL(message_destroy(test_message));
964 result = cbs_put_token_async(cbs, "some_type", "my_audience", "blah_token", test_on_cbs_put_token_complete, (void*)0x4244);
965
966 umock_c_reset_all_calls();
967 STRICT_EXPECTED_CALL(async_operation_cancel(IGNORED_PTR_ARG));
968 STRICT_EXPECTED_CALL(singlylinkedlist_remove_if(IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG));
969 STRICT_EXPECTED_CALL(async_operation_destroy(result));
970
971 // act
972 ((ASYNC_OPERATION_CONTEXT_STRUCT_TEST*)result)->async_operation_cancel_handler(result);
973
974 // assert
975 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
976
977 // cleanup
978 cbs_destroy(cbs);
979 }
980
981
982 /* Tests_SRS_CBS_01_050: [ If any of the arguments `cbs`, `type`, `audience`, `token` or `on_cbs_put_token_complete` is NULL `cbs_put_token_async` shall fail and return a non-zero value. ]*/
983 TEST_FUNCTION(cbs_put_token_async_with_NULL_cbs_handle_fails)
984 {
985 // arrange
986 ASYNC_OPERATION_HANDLE result;
987
988 // act
989 result = cbs_put_token_async(NULL, "some_type", "my_audience", "blah_token", test_on_cbs_put_token_complete, (void*)0x4244);
990
991 // assert
992 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
993 ASSERT_ARE_EQUAL(ASYNC_OPERATION_HANDLE, NULL, result);
994 }
995
996 /* Tests_SRS_CBS_01_050: [ If any of the arguments `cbs`, `type`, `audience`, `token` or `on_cbs_put_token_complete` is NULL `cbs_put_token_async` shall fail and return a non-zero value. ]*/
997 TEST_FUNCTION(cbs_put_token_async_with_NULL_type_fails)
998 {
999 // arrange
1000 CBS_HANDLE cbs;
1001 ASYNC_OPERATION_HANDLE result;
1002 cbs = cbs_create(test_session_handle);
1003 (void)cbs_open_async(cbs, test_on_cbs_open_complete, (void*)0x4242, test_on_cbs_error, (void*)0x4243);
1004 saved_on_amqp_management_open_complete(saved_on_amqp_management_open_complete_context, AMQP_MANAGEMENT_OPEN_OK);
1005 umock_c_reset_all_calls();
1006
1007 // act
1008 result = cbs_put_token_async(cbs, NULL, "my_audience", "blah_token", test_on_cbs_put_token_complete, (void*)0x4244);
1009
1010 // assert
1011 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1012 ASSERT_ARE_EQUAL(ASYNC_OPERATION_HANDLE, NULL, result);
1013
1014 // cleanup
1015 cbs_destroy(cbs);
1016 }
1017
1018 /* Tests_SRS_CBS_01_050: [ If any of the arguments `cbs`, `type`, `audience`, `token` or `on_cbs_put_token_complete` is NULL `cbs_put_token_async` shall fail and return a non-zero value. ]*/
1019 TEST_FUNCTION(cbs_put_token_async_with_NULL_audience_fails)
1020 {
1021 // arrange
1022 CBS_HANDLE cbs;
1023 ASYNC_OPERATION_HANDLE result;
1024 cbs = cbs_create(test_session_handle);
1025 (void)cbs_open_async(cbs, test_on_cbs_open_complete, (void*)0x4242, test_on_cbs_error, (void*)0x4243);
1026 saved_on_amqp_management_open_complete(saved_on_amqp_management_open_complete_context, AMQP_MANAGEMENT_OPEN_OK);
1027 umock_c_reset_all_calls();
1028
1029 // act
1030 result = cbs_put_token_async(cbs, "some_type", NULL, "blah_token", test_on_cbs_put_token_complete, (void*)0x4244);
1031
1032 // assert
1033 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1034 ASSERT_IS_NULL(result);
1035
1036 // cleanup
1037 cbs_destroy(cbs);
1038 }
1039
1040 /* Tests_SRS_CBS_01_050: [ If any of the arguments `cbs`, `type`, `audience`, `token` or `on_cbs_put_token_complete` is NULL `cbs_put_token_async` shall fail and return a non-zero value. ]*/
1041 TEST_FUNCTION(cbs_put_token_async_with_NULL_token_fails)
1042 {
1043 // arrange
1044 CBS_HANDLE cbs;
1045 ASYNC_OPERATION_HANDLE result;
1046 cbs = cbs_create(test_session_handle);
1047 (void)cbs_open_async(cbs, test_on_cbs_open_complete, (void*)0x4242, test_on_cbs_error, (void*)0x4243);
1048 saved_on_amqp_management_open_complete(saved_on_amqp_management_open_complete_context, AMQP_MANAGEMENT_OPEN_OK);
1049 umock_c_reset_all_calls();
1050
1051 // act
1052 result = cbs_put_token_async(cbs, "some_type", "my_audience", NULL, test_on_cbs_put_token_complete, (void*)0x4244);
1053
1054 // assert
1055 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1056 ASSERT_ARE_EQUAL(ASYNC_OPERATION_HANDLE, NULL, result);
1057
1058 // cleanup
1059 cbs_destroy(cbs);
1060 }
1061
1062 /* Tests_SRS_CBS_01_050: [ If any of the arguments `cbs`, `type`, `audience`, `token` or `on_cbs_put_token_complete` is NULL `cbs_put_token_async` shall fail and return a non-zero value. ]*/
1063 TEST_FUNCTION(cbs_put_token_async_with_NULL_complete_callback_fails)
1064 {
1065 // arrange
1066 CBS_HANDLE cbs;
1067 ASYNC_OPERATION_HANDLE result;
1068 cbs = cbs_create(test_session_handle);
1069 (void)cbs_open_async(cbs, test_on_cbs_open_complete, (void*)0x4242, test_on_cbs_error, (void*)0x4243);
1070 saved_on_amqp_management_open_complete(saved_on_amqp_management_open_complete_context, AMQP_MANAGEMENT_OPEN_OK);
1071 umock_c_reset_all_calls();
1072
1073 // act
1074 result = cbs_put_token_async(cbs, "some_type", "my_audience", "blah_token", NULL, (void*)0x4244);
1075
1076 // assert
1077 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1078 ASSERT_ARE_EQUAL(ASYNC_OPERATION_HANDLE, NULL, result);
1079
1080 // cleanup
1081 cbs_destroy(cbs);
1082 }
1083
1084 /* Tests_SRS_CBS_01_083: [ `on_cbs_put_token_complete_context` shall be allowed to be NULL. ]*/
1085 TEST_FUNCTION(cbs_put_token_async_with_NULL_complete_context_succeeds)
1086 {
1087 // arrange
1088 CBS_HANDLE cbs;
1089 ASYNC_OPERATION_HANDLE result;
1090 cbs = cbs_create(test_session_handle);
1091 (void)cbs_open_async(cbs, test_on_cbs_open_complete, (void*)0x4242, test_on_cbs_error, (void*)0x4243);
1092 saved_on_amqp_management_open_complete(saved_on_amqp_management_open_complete_context, AMQP_MANAGEMENT_OPEN_OK);
1093 umock_c_reset_all_calls();
1094
1095 STRICT_EXPECTED_CALL(message_create());
1096 STRICT_EXPECTED_CALL(amqpvalue_create_string("blah_token"))
1097 .SetReturn(test_token_value);
1098 STRICT_EXPECTED_CALL(message_set_body_amqp_value(test_message, test_token_value));
1099 STRICT_EXPECTED_CALL(amqpvalue_create_map())
1100 .SetReturn(test_map_value);
1101 STRICT_EXPECTED_CALL(amqpvalue_create_string("name"))
1102 .SetReturn(test_name_propery_key);
1103 STRICT_EXPECTED_CALL(amqpvalue_create_string("my_audience"))
1104 .SetReturn(test_name_propery_value);
1105 STRICT_EXPECTED_CALL(amqpvalue_set_map_value(test_map_value, test_name_propery_key, test_name_propery_value));
1106 STRICT_EXPECTED_CALL(amqpvalue_destroy(test_name_propery_value));
1107 STRICT_EXPECTED_CALL(amqpvalue_destroy(test_name_propery_key));
1108 STRICT_EXPECTED_CALL(message_set_application_properties(test_message, test_map_value));
1109 STRICT_EXPECTED_CALL(async_operation_create(IGNORED_PTR_ARG, IGNORED_NUM_ARG));
1110 STRICT_EXPECTED_CALL(singlylinkedlist_add(test_singlylinkedlist, IGNORED_PTR_ARG));
1111 STRICT_EXPECTED_CALL(amqp_management_execute_operation_async(test_amqp_management_handle, "put-token", "some_type", NULL, test_message, IGNORED_PTR_ARG, IGNORED_PTR_ARG));
1112 STRICT_EXPECTED_CALL(amqpvalue_destroy(test_map_value));
1113 STRICT_EXPECTED_CALL(amqpvalue_destroy(test_token_value));
1114 STRICT_EXPECTED_CALL(message_destroy(test_message));
1115
1116 // act
1117 result = cbs_put_token_async(cbs, "some_type", "my_audience", "blah_token", test_on_cbs_put_token_complete, NULL);
1118
1119 // assert
1120 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1121 ASSERT_ARE_NOT_EQUAL(ASYNC_OPERATION_HANDLE, NULL, result);
1122
1123 // cleanup
1124 cbs_destroy(cbs);
1125 }
1126
1127 /* Tests_SRS_CBS_01_072: [ If constructing the message fails, `cbs_put_token_async` shall fail and return a non-zero value. ]*/
1128 /* Tests_SRS_CBS_01_084: [ If `amqp_management_execute_operation_async` fails `cbs_put_token_async` shall fail and return a non-zero value. ]*/
1129 TEST_FUNCTION(when_any_underlying_call_fails_cbs_put_token_async_fails)
1130 {
1131 // arrange
1132 CBS_HANDLE cbs;
1133 ASYNC_OPERATION_HANDLE result;
1134 size_t count;
1135 size_t index;
1136 int negativeTestsInitResult = umock_c_negative_tests_init();
1137 ASSERT_ARE_EQUAL(int, 0, negativeTestsInitResult);
1138
1139 cbs = cbs_create(test_session_handle);
1140 (void)cbs_open_async(cbs, test_on_cbs_open_complete, (void*)0x4242, test_on_cbs_error, (void*)0x4243);
1141 saved_on_amqp_management_open_complete(saved_on_amqp_management_open_complete_context, AMQP_MANAGEMENT_OPEN_OK);
1142 umock_c_reset_all_calls();
1143
1144 STRICT_EXPECTED_CALL(message_create())
1145 .SetFailReturn(NULL);
1146 STRICT_EXPECTED_CALL(amqpvalue_create_string("blah_token"))
1147 .SetReturn(test_token_value).SetFailReturn(NULL);
1148 STRICT_EXPECTED_CALL(message_set_body_amqp_value(test_message, test_token_value))
1149 .SetFailReturn(42);
1150 STRICT_EXPECTED_CALL(amqpvalue_create_map())
1151 .SetReturn(test_map_value).SetFailReturn(NULL);
1152 STRICT_EXPECTED_CALL(amqpvalue_create_string("name"))
1153 .SetReturn(test_name_propery_key).SetFailReturn(NULL);
1154 STRICT_EXPECTED_CALL(amqpvalue_create_string("my_audience"))
1155 .SetReturn(test_name_propery_value).SetFailReturn(NULL);
1156 STRICT_EXPECTED_CALL(amqpvalue_set_map_value(test_map_value, test_name_propery_key, test_name_propery_value))
1157 .SetFailReturn(42);
1158 STRICT_EXPECTED_CALL(amqpvalue_destroy(test_name_propery_value));
1159 STRICT_EXPECTED_CALL(amqpvalue_destroy(test_name_propery_key));
1160 STRICT_EXPECTED_CALL(message_set_application_properties(test_message, test_map_value))
1161 .SetFailReturn(42);
1162 STRICT_EXPECTED_CALL(async_operation_create(IGNORED_PTR_ARG, IGNORED_NUM_ARG))
1163 .SetFailReturn(NULL);
1164 STRICT_EXPECTED_CALL(singlylinkedlist_add(test_singlylinkedlist, IGNORED_PTR_ARG))
1165 .SetFailReturn(NULL);
1166 STRICT_EXPECTED_CALL(amqp_management_execute_operation_async(test_amqp_management_handle, "put-token", "some_type", NULL, test_message, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
1167 .SetFailReturn(NULL);
1168
1169 umock_c_negative_tests_snapshot();
1170
1171 count = umock_c_negative_tests_call_count();
1172 for (index = 0; index < count; index++)
1173 {
1174 char tmp_msg[128];
1175
1176 if ((index == 7) ||
1177 (index == 8))
1178 {
1179 continue;
1180 }
1181
1182 (void)sprintf(tmp_msg, "Failure in test %u/%u", (unsigned int)(index + 1), (unsigned int)count);
1183
1184 umock_c_negative_tests_reset();
1185 umock_c_negative_tests_fail_call(index);
1186
1187 // act
1188 result = cbs_put_token_async(cbs, "some_type", "my_audience", "blah_token", test_on_cbs_put_token_complete, NULL);
1189
1190 // assert
1191 ASSERT_ARE_EQUAL(ASYNC_OPERATION_HANDLE, NULL, result, tmp_msg);
1192 }
1193
1194 // cleanup
1195 umock_c_negative_tests_deinit();
1196 cbs_destroy(cbs);
1197 }
1198
1199 /* Tests_SRS_CBS_01_058: [ If `cbs_put_token_async` is called when the CBS instance is not yet open or in error, it shall fail and return `NULL`. ]*/
1200 TEST_FUNCTION(cbs_put_token_async_when_not_open_fails)
1201 {
1202 // arrange
1203 CBS_HANDLE cbs;
1204 ASYNC_OPERATION_HANDLE result;
1205 cbs = cbs_create(test_session_handle);
1206 umock_c_reset_all_calls();
1207
1208 // act
1209 result = cbs_put_token_async(cbs, "some_type", "my_audience", "blah_token", test_on_cbs_put_token_complete, NULL);
1210
1211 // assert
1212 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1213 ASSERT_ARE_EQUAL(ASYNC_OPERATION_HANDLE, NULL, result);
1214
1215 // cleanup
1216 cbs_destroy(cbs);
1217 }
1218
1219 /* Tests_SRS_CBS_01_058: [ If `cbs_put_token_async` is called when the CBS instance is not yet open or in error, it shall fail and return `NULL`. ]*/
1220 TEST_FUNCTION(cbs_put_token_async_while_opening_succeeds)
1221 {
1222 // arrange
1223 CBS_HANDLE cbs;
1224 ASYNC_OPERATION_HANDLE result;
1225 cbs = cbs_create(test_session_handle);
1226 (void)cbs_open_async(cbs, test_on_cbs_open_complete, (void*)0x4242, test_on_cbs_error, (void*)0x4243);
1227 umock_c_reset_all_calls();
1228
1229 STRICT_EXPECTED_CALL(message_create());
1230 STRICT_EXPECTED_CALL(amqpvalue_create_string("blah_token"))
1231 .SetReturn(test_token_value);
1232 STRICT_EXPECTED_CALL(message_set_body_amqp_value(test_message, test_token_value));
1233 STRICT_EXPECTED_CALL(amqpvalue_create_map())
1234 .SetReturn(test_map_value);
1235 STRICT_EXPECTED_CALL(amqpvalue_create_string("name"))
1236 .SetReturn(test_name_propery_key);
1237 STRICT_EXPECTED_CALL(amqpvalue_create_string("my_audience"))
1238 .SetReturn(test_name_propery_value);
1239 STRICT_EXPECTED_CALL(amqpvalue_set_map_value(test_map_value, test_name_propery_key, test_name_propery_value));
1240 STRICT_EXPECTED_CALL(amqpvalue_destroy(test_name_propery_value));
1241 STRICT_EXPECTED_CALL(amqpvalue_destroy(test_name_propery_key));
1242 STRICT_EXPECTED_CALL(message_set_application_properties(test_message, test_map_value));
1243 STRICT_EXPECTED_CALL(async_operation_create(IGNORED_PTR_ARG, IGNORED_NUM_ARG));
1244 STRICT_EXPECTED_CALL(singlylinkedlist_add(test_singlylinkedlist, IGNORED_PTR_ARG));
1245 STRICT_EXPECTED_CALL(amqp_management_execute_operation_async(test_amqp_management_handle, "put-token", "some_type", NULL, test_message, IGNORED_PTR_ARG, IGNORED_PTR_ARG));
1246 STRICT_EXPECTED_CALL(amqpvalue_destroy(test_map_value));
1247 STRICT_EXPECTED_CALL(amqpvalue_destroy(test_token_value));
1248 STRICT_EXPECTED_CALL(message_destroy(test_message));
1249
1250 // act
1251 result = cbs_put_token_async(cbs, "some_type", "my_audience", "blah_token", test_on_cbs_put_token_complete, NULL);
1252
1253 // assert
1254 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1255 ASSERT_ARE_NOT_EQUAL(ASYNC_OPERATION_HANDLE, NULL, result);
1256
1257 // cleanup
1258 cbs_destroy(cbs);
1259 }
1260
1261 /* Tests_SRS_CBS_01_058: [ If `cbs_put_token_async` is called when the CBS instance is not yet open or in error, it shall fail and return `NULL`. ]*/
1262 TEST_FUNCTION(cbs_put_token_async_when_in_error_fails)
1263 {
1264 // arrange
1265 CBS_HANDLE cbs;
1266 ASYNC_OPERATION_HANDLE result;
1267 cbs = cbs_create(test_session_handle);
1268 (void)cbs_open_async(cbs, test_on_cbs_open_complete, (void*)0x4242, test_on_cbs_error, (void*)0x4243);
1269 saved_on_amqp_management_open_complete(saved_on_amqp_management_open_complete_context, AMQP_MANAGEMENT_OPEN_OK);
1270 saved_on_amqp_management_error(saved_on_amqp_management_error_context);
1271 umock_c_reset_all_calls();
1272
1273 // act
1274 result = cbs_put_token_async(cbs, "some_type", "my_audience", "blah_token", test_on_cbs_put_token_complete, NULL);
1275
1276 // assert
1277 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1278 ASSERT_ARE_EQUAL(ASYNC_OPERATION_HANDLE, NULL, result);
1279
1280 // cleanup
1281 cbs_destroy(cbs);
1282 }
1283
1284 /* cbs_delete_token_async */
1285
1286 /* Tests_SRS_CBS_01_059: [ `cbs_delete_token_async` shall construct a request message for the `delete-token` operation. ]*/
1287 /* Tests_SRS_CBS_01_082: [ On success `cbs_delete_token_async` shall return an ASYNC_OPERATION_HANDLE. ]*/
1288 /* Tests_SRS_CBS_01_061: [ `cbs_delete_token_async` shall start the AMQP management operation by calling `amqp_management_execute_operation_async`, while passing to it: ]*/
1289 /* Tests_SRS_CBS_01_085: [ The `amqp_management` argument shall be the one for the AMQP management instance created in `cbs_create`. ]*/
1290 /* Tests_SRS_CBS_01_062: [ The `operation` argument shall be `delete-token`. ]*/
1291 /* Tests_SRS_CBS_01_063: [ The `type` argument shall be set to the `type` argument. ]*/
1292 /* Tests_SRS_CBS_01_064: [ The `locales` argument shall be set to NULL. ]*/
1293 /* Tests_SRS_CBS_01_065: [ The `message` argument shall be the message constructed earlier according to the CBS spec. ]*/
1294 /* Tests_SRS_CBS_01_066: [ The arguments `on_operation_complete` and `context` shall be set to a callback that is to be called by the AMQP management module when the operation is complete. ]*/
1295 /* Tests_SRS_CBS_01_020: [ To instruct a peer to delete a token associated with a specific audience, a "delete-token" message can be sent to the CBS Node ]*/
1296 /* Tests_SRS_CBS_01_021: [ The request message has the following application-properties: ]*/
1297 /* Tests_SRS_CBS_01_022: [ operation Yes string "delete-token" ]*/
1298 /* Tests_SRS_CBS_01_023: [ Type Yes string The type of the token being deleted, e.g., "amqp:jwt". ]*/
1299 /* Tests_SRS_CBS_01_024: [ name Yes string The "audience" of the token being deleted. ]*/
1300 /* Tests_SRS_CBS_01_025: [ The body of the message MUST be empty. ]*/
1301 TEST_FUNCTION(cbs_delete_token_async_creates_the_message_and_starts_the_amqp_management_operation)
1302 {
1303 // arrange
1304 CBS_HANDLE cbs;
1305 ASYNC_OPERATION_HANDLE result;
1306 cbs = cbs_create(test_session_handle);
1307 (void)cbs_open_async(cbs, test_on_cbs_open_complete, (void*)0x4242, test_on_cbs_error, (void*)0x4243);
1308 saved_on_amqp_management_open_complete(saved_on_amqp_management_open_complete_context, AMQP_MANAGEMENT_OPEN_OK);
1309 umock_c_reset_all_calls();
1310
1311 STRICT_EXPECTED_CALL(message_create());
1312 STRICT_EXPECTED_CALL(amqpvalue_create_map())
1313 .SetReturn(test_map_value);
1314 STRICT_EXPECTED_CALL(amqpvalue_create_string("name"))
1315 .SetReturn(test_name_propery_key);
1316 STRICT_EXPECTED_CALL(amqpvalue_create_string("my_audience"))
1317 .SetReturn(test_name_propery_value);
1318 STRICT_EXPECTED_CALL(amqpvalue_set_map_value(test_map_value, test_name_propery_key, test_name_propery_value));
1319 STRICT_EXPECTED_CALL(amqpvalue_destroy(test_name_propery_value));
1320 STRICT_EXPECTED_CALL(amqpvalue_destroy(test_name_propery_key));
1321 STRICT_EXPECTED_CALL(message_set_application_properties(test_message, test_map_value));
1322 STRICT_EXPECTED_CALL(async_operation_create(IGNORED_PTR_ARG, IGNORED_NUM_ARG));
1323 STRICT_EXPECTED_CALL(singlylinkedlist_add(test_singlylinkedlist, IGNORED_PTR_ARG));
1324 STRICT_EXPECTED_CALL(amqp_management_execute_operation_async(test_amqp_management_handle, "delete-token", "some_type", NULL, test_message, IGNORED_PTR_ARG, IGNORED_PTR_ARG));
1325 STRICT_EXPECTED_CALL(amqpvalue_destroy(test_map_value));
1326 STRICT_EXPECTED_CALL(message_destroy(test_message));
1327
1328 // act
1329 result = cbs_delete_token_async(cbs, "some_type", "my_audience", test_on_cbs_delete_token_complete, (void*)0x4244);
1330
1331 // assert
1332 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1333 ASSERT_ARE_NOT_EQUAL(ASYNC_OPERATION_HANDLE, NULL, result);
1334
1335 // cleanup
1336 cbs_destroy(cbs);
1337 }
1338
1339 /* Tests_SRS_CBS_09_001: [ The `ASYNC_OPERATION_HANDLE` cancel function shall cancel the underlying amqp management operation, remove this operation from the pending list, destroy this async operation. ] */
1340 TEST_FUNCTION(when_cbs_delete_token_async_is_cancelled)
1341 {
1342 // arrange
1343 CBS_HANDLE cbs;
1344 ASYNC_OPERATION_HANDLE result;
1345 cbs = cbs_create(test_session_handle);
1346 (void)cbs_open_async(cbs, test_on_cbs_open_complete, (void*)0x4242, test_on_cbs_error, (void*)0x4243);
1347 saved_on_amqp_management_open_complete(saved_on_amqp_management_open_complete_context, AMQP_MANAGEMENT_OPEN_OK);
1348 umock_c_reset_all_calls();
1349
1350 STRICT_EXPECTED_CALL(message_create());
1351 STRICT_EXPECTED_CALL(amqpvalue_create_map())
1352 .SetReturn(test_map_value);
1353 STRICT_EXPECTED_CALL(amqpvalue_create_string("name"))
1354 .SetReturn(test_name_propery_key);
1355 STRICT_EXPECTED_CALL(amqpvalue_create_string("my_audience"))
1356 .SetReturn(test_name_propery_value);
1357 STRICT_EXPECTED_CALL(amqpvalue_set_map_value(test_map_value, test_name_propery_key, test_name_propery_value));
1358 STRICT_EXPECTED_CALL(amqpvalue_destroy(test_name_propery_value));
1359 STRICT_EXPECTED_CALL(amqpvalue_destroy(test_name_propery_key));
1360 STRICT_EXPECTED_CALL(message_set_application_properties(test_message, test_map_value));
1361 STRICT_EXPECTED_CALL(async_operation_create(IGNORED_PTR_ARG, IGNORED_NUM_ARG));
1362 STRICT_EXPECTED_CALL(singlylinkedlist_add(test_singlylinkedlist, IGNORED_PTR_ARG));
1363 STRICT_EXPECTED_CALL(amqp_management_execute_operation_async(test_amqp_management_handle, "delete-token", "some_type", NULL, test_message, IGNORED_PTR_ARG, IGNORED_PTR_ARG));
1364 STRICT_EXPECTED_CALL(amqpvalue_destroy(test_map_value));
1365 STRICT_EXPECTED_CALL(message_destroy(test_message));
1366 result = cbs_delete_token_async(cbs, "some_type", "my_audience", test_on_cbs_delete_token_complete, (void*)0x4244);
1367
1368 umock_c_reset_all_calls();
1369 STRICT_EXPECTED_CALL(async_operation_cancel(IGNORED_PTR_ARG));
1370 STRICT_EXPECTED_CALL(singlylinkedlist_remove_if(IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG));
1371 STRICT_EXPECTED_CALL(async_operation_destroy(result));
1372
1373 // act
1374 ((ASYNC_OPERATION_CONTEXT_STRUCT_TEST*)result)->async_operation_cancel_handler(result);
1375
1376 // assert
1377 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1378
1379 // cleanup
1380 cbs_destroy(cbs);
1381 }
1382
1383 /* Tests_SRS_CBS_01_060: [ If any of the arguments `cbs`, `type`, `audience` or `on_cbs_delete_token_complete` is NULL `cbs_put_token_async` shall fail and return a non-zero value. ]*/
1384 TEST_FUNCTION(cbs_delete_token_with_NULL_cbs_handle_fails)
1385 {
1386 // arrange
1387 ASYNC_OPERATION_HANDLE result;
1388
1389 // act
1390 result = cbs_delete_token_async(NULL, "test_type", "my_audience", test_on_cbs_delete_token_complete, (void*)0x4244);
1391
1392 // assert
1393 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1394 ASSERT_ARE_EQUAL(ASYNC_OPERATION_HANDLE, NULL, result);
1395 }
1396
1397 /* Tests_SRS_CBS_01_060: [ If any of the arguments `cbs`, `type`, `audience` or `on_cbs_delete_token_complete` is NULL `cbs_put_token_async` shall fail and return a non-zero value. ]*/
1398 TEST_FUNCTION(cbs_delete_token_with_NULL_type_fails)
1399 {
1400 // arrange
1401 CBS_HANDLE cbs;
1402 ASYNC_OPERATION_HANDLE result;
1403 cbs = cbs_create(test_session_handle);
1404 (void)cbs_open_async(cbs, test_on_cbs_open_complete, (void*)0x4242, test_on_cbs_error, (void*)0x4243);
1405 saved_on_amqp_management_open_complete(saved_on_amqp_management_open_complete_context, AMQP_MANAGEMENT_OPEN_OK);
1406 umock_c_reset_all_calls();
1407
1408 // act
1409 result = cbs_delete_token_async(cbs, NULL, "my_audience", test_on_cbs_delete_token_complete, (void*)0x4244);
1410
1411 // assert
1412 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1413 ASSERT_ARE_EQUAL(ASYNC_OPERATION_HANDLE, NULL, result);
1414
1415 // cleanup
1416 cbs_destroy(cbs);
1417 }
1418
1419 /* Tests_SRS_CBS_01_060: [ If any of the arguments `cbs`, `type`, `audience` or `on_cbs_delete_token_complete` is NULL `cbs_put_token_async` shall fail and return a non-zero value. ]*/
1420 TEST_FUNCTION(cbs_delete_token_with_NULL_audience_fails)
1421 {
1422 // arrange
1423 CBS_HANDLE cbs;
1424 ASYNC_OPERATION_HANDLE result;
1425 cbs = cbs_create(test_session_handle);
1426 (void)cbs_open_async(cbs, test_on_cbs_open_complete, (void*)0x4242, test_on_cbs_error, (void*)0x4243);
1427 saved_on_amqp_management_open_complete(saved_on_amqp_management_open_complete_context, AMQP_MANAGEMENT_OPEN_OK);
1428 umock_c_reset_all_calls();
1429
1430 // act
1431 result = cbs_delete_token_async(cbs, "some_type", NULL, test_on_cbs_delete_token_complete, (void*)0x4244);
1432
1433 // assert
1434 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1435 ASSERT_ARE_EQUAL(ASYNC_OPERATION_HANDLE, NULL, result);
1436
1437 // cleanup
1438 cbs_destroy(cbs);
1439 }
1440
1441 /* Tests_SRS_CBS_01_060: [ If any of the arguments `cbs`, `type`, `audience` or `on_cbs_delete_token_complete` is NULL `cbs_put_token_async` shall fail and return a non-zero value. ]*/
1442 TEST_FUNCTION(cbs_delete_token_with_NULL_complete_callback_fails)
1443 {
1444 // arrange
1445 CBS_HANDLE cbs;
1446 ASYNC_OPERATION_HANDLE result;
1447 cbs = cbs_create(test_session_handle);
1448 (void)cbs_open_async(cbs, test_on_cbs_open_complete, (void*)0x4242, test_on_cbs_error, (void*)0x4243);
1449 saved_on_amqp_management_open_complete(saved_on_amqp_management_open_complete_context, AMQP_MANAGEMENT_OPEN_OK);
1450 umock_c_reset_all_calls();
1451
1452 // act
1453 result = cbs_delete_token_async(cbs, "some_type", "my_audience", NULL, (void*)0x4244);
1454
1455 // assert
1456 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1457 ASSERT_ARE_EQUAL(ASYNC_OPERATION_HANDLE, NULL, result);
1458
1459 // cleanup
1460 cbs_destroy(cbs);
1461 }
1462
1463 /* Tests_SRS_CBS_01_086: [ `on_cbs_delete_token_complete_context` shall be allowed to be NULL. ]*/
1464 TEST_FUNCTION(cbs_delete_token_async_with_NULL_complete_context_succeeds)
1465 {
1466 // arrange
1467 CBS_HANDLE cbs;
1468 ASYNC_OPERATION_HANDLE result;
1469 cbs = cbs_create(test_session_handle);
1470 (void)cbs_open_async(cbs, test_on_cbs_open_complete, (void*)0x4242, test_on_cbs_error, (void*)0x4243);
1471 saved_on_amqp_management_open_complete(saved_on_amqp_management_open_complete_context, AMQP_MANAGEMENT_OPEN_OK);
1472 umock_c_reset_all_calls();
1473
1474 STRICT_EXPECTED_CALL(message_create());
1475 STRICT_EXPECTED_CALL(amqpvalue_create_map())
1476 .SetReturn(test_map_value);
1477 STRICT_EXPECTED_CALL(amqpvalue_create_string("name"))
1478 .SetReturn(test_name_propery_key);
1479 STRICT_EXPECTED_CALL(amqpvalue_create_string("my_audience"))
1480 .SetReturn(test_name_propery_value);
1481 STRICT_EXPECTED_CALL(amqpvalue_set_map_value(test_map_value, test_name_propery_key, test_name_propery_value));
1482 STRICT_EXPECTED_CALL(amqpvalue_destroy(test_name_propery_value));
1483 STRICT_EXPECTED_CALL(amqpvalue_destroy(test_name_propery_key));
1484 STRICT_EXPECTED_CALL(message_set_application_properties(test_message, test_map_value));
1485 STRICT_EXPECTED_CALL(async_operation_create(IGNORED_PTR_ARG, IGNORED_NUM_ARG));
1486 STRICT_EXPECTED_CALL(singlylinkedlist_add(test_singlylinkedlist, IGNORED_PTR_ARG));
1487 STRICT_EXPECTED_CALL(amqp_management_execute_operation_async(test_amqp_management_handle, "delete-token", "some_type", NULL, test_message, IGNORED_PTR_ARG, IGNORED_PTR_ARG));
1488 STRICT_EXPECTED_CALL(amqpvalue_destroy(test_map_value));
1489 STRICT_EXPECTED_CALL(message_destroy(test_message));
1490
1491 // act
1492 result = cbs_delete_token_async(cbs, "some_type", "my_audience", test_on_cbs_delete_token_complete, NULL);
1493
1494 // assert
1495 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1496 ASSERT_ARE_NOT_EQUAL(ASYNC_OPERATION_HANDLE, NULL, result);
1497
1498 // cleanup
1499 cbs_destroy(cbs);
1500 }
1501
1502 /* Tests_SRS_CBS_01_071: [ If constructing the message fails, `cbs_delete_token_async` shall fail and return a non-zero value. ]*/
1503 /* Tests_SRS_CBS_01_087: [ If `amqp_management_execute_operation_async` fails `cbs_put_token_async` shall fail and return a non-zero value. ]*/
1504 TEST_FUNCTION(when_any_underlying_call_fails_cbs_delete_token_async_fails)
1505 {
1506 // arrange
1507 CBS_HANDLE cbs;
1508 ASYNC_OPERATION_HANDLE result;
1509 size_t count;
1510 size_t index;
1511 int negativeTestsInitResult = umock_c_negative_tests_init();
1512 ASSERT_ARE_EQUAL(int, 0, negativeTestsInitResult);
1513
1514 cbs = cbs_create(test_session_handle);
1515 (void)cbs_open_async(cbs, test_on_cbs_open_complete, (void*)0x4242, test_on_cbs_error, (void*)0x4243);
1516 saved_on_amqp_management_open_complete(saved_on_amqp_management_open_complete_context, AMQP_MANAGEMENT_OPEN_OK);
1517 umock_c_reset_all_calls();
1518
1519 STRICT_EXPECTED_CALL(message_create())
1520 .SetFailReturn(NULL);
1521 STRICT_EXPECTED_CALL(amqpvalue_create_map())
1522 .SetReturn(test_map_value).SetFailReturn(NULL);
1523 STRICT_EXPECTED_CALL(amqpvalue_create_string("name"))
1524 .SetReturn(test_name_propery_key).SetFailReturn(NULL);
1525 STRICT_EXPECTED_CALL(amqpvalue_create_string("my_audience"))
1526 .SetReturn(test_name_propery_value).SetFailReturn(NULL);
1527 STRICT_EXPECTED_CALL(amqpvalue_set_map_value(test_map_value, test_name_propery_key, test_name_propery_value))
1528 .SetFailReturn(42);
1529 STRICT_EXPECTED_CALL(amqpvalue_destroy(test_name_propery_value));
1530 STRICT_EXPECTED_CALL(amqpvalue_destroy(test_name_propery_key));
1531 STRICT_EXPECTED_CALL(message_set_application_properties(test_message, test_map_value))
1532 .SetFailReturn(42);
1533 STRICT_EXPECTED_CALL(async_operation_create(IGNORED_PTR_ARG, IGNORED_NUM_ARG))
1534 .SetFailReturn(NULL);
1535 STRICT_EXPECTED_CALL(singlylinkedlist_add(test_singlylinkedlist, IGNORED_PTR_ARG))
1536 .SetFailReturn(NULL);
1537 STRICT_EXPECTED_CALL(amqp_management_execute_operation_async(test_amqp_management_handle, "delete-token", "some_type", NULL, test_message, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
1538 .SetFailReturn(NULL);
1539
1540 umock_c_negative_tests_snapshot();
1541
1542 count = umock_c_negative_tests_call_count();
1543 for (index = 0; index < count; index++)
1544 {
1545 char tmp_msg[128];
1546
1547 if ((index == 5) ||
1548 (index == 6))
1549 {
1550 continue;
1551 }
1552
1553 (void)sprintf(tmp_msg, "Failure in test %u/%u", (unsigned int)(index + 1), (unsigned int)count);
1554
1555 umock_c_negative_tests_reset();
1556 umock_c_negative_tests_fail_call(index);
1557
1558 // act
1559 result = cbs_delete_token_async(cbs, "some_type", "my_audience", test_on_cbs_put_token_complete, NULL);
1560
1561 // assert
1562 ASSERT_ARE_EQUAL(ASYNC_OPERATION_HANDLE, NULL, result, tmp_msg);
1563 }
1564
1565 // cleanup
1566 umock_c_negative_tests_deinit();
1567 cbs_destroy(cbs);
1568 }
1569
1570 /* Tests_SRS_CBS_01_067: [ If `cbs_delete_token_async` is called when the CBS instance is not yet open or in error, it shall fail and return `NULL`. ]*/
1571 TEST_FUNCTION(cbs_delete_token_async_when_not_open_fails)
1572 {
1573 // arrange
1574 CBS_HANDLE cbs;
1575 ASYNC_OPERATION_HANDLE result;
1576 cbs = cbs_create(test_session_handle);
1577 umock_c_reset_all_calls();
1578
1579 // act
1580 result = cbs_delete_token_async(cbs, "some_type", "my_audience", test_on_cbs_delete_token_complete, (void*)0x4244);
1581
1582 // assert
1583 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1584 ASSERT_ARE_EQUAL(ASYNC_OPERATION_HANDLE, NULL, result);
1585
1586 // cleanup
1587 cbs_destroy(cbs);
1588 }
1589
1590 /* Tests_SRS_CBS_01_067: [ If `cbs_delete_token_async` is called when the CBS instance is not yet open or in error, it shall fail and return `NULL`. ]*/
1591 TEST_FUNCTION(cbs_delete_token_async_while_opening_succeeds)
1592 {
1593 // arrange
1594 CBS_HANDLE cbs;
1595 ASYNC_OPERATION_HANDLE result;
1596 cbs = cbs_create(test_session_handle);
1597 (void)cbs_open_async(cbs, test_on_cbs_open_complete, (void*)0x4242, test_on_cbs_error, (void*)0x4243);
1598 umock_c_reset_all_calls();
1599
1600 STRICT_EXPECTED_CALL(message_create());
1601 STRICT_EXPECTED_CALL(amqpvalue_create_map())
1602 .SetReturn(test_map_value);
1603 STRICT_EXPECTED_CALL(amqpvalue_create_string("name"))
1604 .SetReturn(test_name_propery_key);
1605 STRICT_EXPECTED_CALL(amqpvalue_create_string("my_audience"))
1606 .SetReturn(test_name_propery_value);
1607 STRICT_EXPECTED_CALL(amqpvalue_set_map_value(test_map_value, test_name_propery_key, test_name_propery_value));
1608 STRICT_EXPECTED_CALL(amqpvalue_destroy(test_name_propery_value));
1609 STRICT_EXPECTED_CALL(amqpvalue_destroy(test_name_propery_key));
1610 STRICT_EXPECTED_CALL(message_set_application_properties(test_message, test_map_value));
1611 STRICT_EXPECTED_CALL(async_operation_create(IGNORED_PTR_ARG, IGNORED_NUM_ARG));
1612 STRICT_EXPECTED_CALL(singlylinkedlist_add(test_singlylinkedlist, IGNORED_PTR_ARG));
1613 STRICT_EXPECTED_CALL(amqp_management_execute_operation_async(test_amqp_management_handle, "delete-token", "some_type", NULL, test_message, IGNORED_PTR_ARG, IGNORED_PTR_ARG));
1614 STRICT_EXPECTED_CALL(amqpvalue_destroy(test_map_value));
1615 STRICT_EXPECTED_CALL(message_destroy(test_message));
1616
1617 // act
1618 result = cbs_delete_token_async(cbs, "some_type", "my_audience", test_on_cbs_delete_token_complete, (void*)0x4244);
1619
1620 // assert
1621 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1622 ASSERT_ARE_NOT_EQUAL(ASYNC_OPERATION_HANDLE, NULL, result);
1623
1624 // cleanup
1625 cbs_destroy(cbs);
1626 }
1627
1628 /* Tests_SRS_CBS_01_067: [ If `cbs_delete_token_async` is called when the CBS instance is not yet open or in error, it shall fail and return `NULL`. ]*/
1629 TEST_FUNCTION(cbs_delete_token_async_when_in_error_fails)
1630 {
1631 // arrange
1632 CBS_HANDLE cbs;
1633 ASYNC_OPERATION_HANDLE result;
1634 cbs = cbs_create(test_session_handle);
1635 (void)cbs_open_async(cbs, test_on_cbs_open_complete, (void*)0x4242, test_on_cbs_error, (void*)0x4243);
1636 saved_on_amqp_management_open_complete(saved_on_amqp_management_open_complete_context, AMQP_MANAGEMENT_OPEN_OK);
1637 saved_on_amqp_management_error(saved_on_amqp_management_error_context);
1638 umock_c_reset_all_calls();
1639
1640 // act
1641 result = cbs_delete_token_async(cbs, "some_type", "my_audience", test_on_cbs_delete_token_complete, (void*)0x4244);
1642
1643 // assert
1644 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1645 ASSERT_ARE_EQUAL(ASYNC_OPERATION_HANDLE, NULL, result);
1646
1647 // cleanup
1648 cbs_destroy(cbs);
1649 }
1650
1651 /* cbs_set_trace */
1652
1653 /* Tests_SRS_CBS_01_088: [ `cbs_set_trace` shall enable or disable tracing by calling `amqp_management_set_trace` to pass down the `trace_on` value. ]*/
1654 TEST_FUNCTION(cbs_set_trace_with_true_succeeds)
1655 {
1656 // arrange
1657 CBS_HANDLE cbs;
1658 int result;
1659 cbs = cbs_create(test_session_handle);
1660 umock_c_reset_all_calls();
1661
1662 STRICT_EXPECTED_CALL(amqp_management_set_trace(test_amqp_management_handle, true));
1663
1664 // act
1665 result = cbs_set_trace(cbs, true);
1666
1667 // assert
1668 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1669 ASSERT_ARE_EQUAL(int, 0, result);
1670
1671 // cleanup
1672 cbs_destroy(cbs);
1673 }
1674
1675 /* Tests_SRS_CBS_01_088: [ `cbs_set_trace` shall enable or disable tracing by calling `amqp_management_set_trace` to pass down the `trace_on` value. ]*/
1676 /* Tests_SRS_CBS_01_089: [ On success, `cbs_set_trace` shall return 0. ]*/
1677 TEST_FUNCTION(cbs_set_trace_with_false_succeeds)
1678 {
1679 // arrange
1680 CBS_HANDLE cbs;
1681 int result;
1682 cbs = cbs_create(test_session_handle);
1683 umock_c_reset_all_calls();
1684
1685 STRICT_EXPECTED_CALL(amqp_management_set_trace(test_amqp_management_handle, false));
1686
1687 // act
1688 result = cbs_set_trace(cbs, false);
1689
1690 // assert
1691 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1692 ASSERT_ARE_EQUAL(int, 0, result);
1693
1694 // cleanup
1695 cbs_destroy(cbs);
1696 }
1697
1698 /* Tests_SRS_CBS_01_090: [ If the argument `cbs` is NULL, `cbs_set_trace` shall fail and return a non-zero value. ]*/
1699 TEST_FUNCTION(cbs_set_trace_with_NULL_handle_fails)
1700 {
1701 // arrange
1702 CBS_HANDLE cbs;
1703 int result;
1704 cbs = cbs_create(test_session_handle);
1705 umock_c_reset_all_calls();
1706
1707 // act
1708 result = cbs_set_trace(NULL, false);
1709
1710 // assert
1711 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1712 ASSERT_ARE_NOT_EQUAL(int, 0, result);
1713
1714 // cleanup
1715 cbs_destroy(cbs);
1716 }
1717
1718 /* on_amqp_management_open_complete */
1719
1720 /* Tests_SRS_CBS_01_105: [ When `on_amqp_management_open_complete` is called with NULL `context`, it shall do nothing. ]*/
1721 TEST_FUNCTION(on_amqp_management_open_complete_with_NULL_context_does_nothing)
1722 {
1723 // arrange
1724 CBS_HANDLE cbs;
1725 cbs = cbs_create(test_session_handle);
1726 (void)cbs_open_async(cbs, test_on_cbs_open_complete, (void*)0x4242, test_on_cbs_error, (void*)0x4243);
1727 umock_c_reset_all_calls();
1728
1729 // act
1730 saved_on_amqp_management_open_complete(NULL, AMQP_MANAGEMENT_OPEN_OK);
1731
1732 // assert
1733 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1734
1735 // cleanup
1736 cbs_destroy(cbs);
1737 }
1738
1739 /* Tests_SRS_CBS_01_106: [ If CBS is OPENING and `open_result` is `AMQP_MANAGEMENT_OPEN_OK` the callback `on_cbs_open_complete` shall be called with `CBS_OPEN_OK` and the `on_cbs_open_complete_context` shall be passed as argument. ]*/
1740 TEST_FUNCTION(on_amqp_management_open_complete_with_OK_when_CBS_is_OPENING_indicates_the_open_complete)
1741 {
1742 // arrange
1743 CBS_HANDLE cbs;
1744 cbs = cbs_create(test_session_handle);
1745 (void)cbs_open_async(cbs, test_on_cbs_open_complete, (void*)0x4242, test_on_cbs_error, (void*)0x4243);
1746 umock_c_reset_all_calls();
1747
1748 STRICT_EXPECTED_CALL(test_on_cbs_open_complete((void*)0x4242, CBS_OPEN_OK));
1749
1750 // act
1751 saved_on_amqp_management_open_complete(saved_on_amqp_management_open_complete_context, AMQP_MANAGEMENT_OPEN_OK);
1752
1753 // assert
1754 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1755
1756 // cleanup
1757 cbs_destroy(cbs);
1758 }
1759
1760 /* Tests_SRS_CBS_01_107: [ If CBS is OPENING and `open_result` is `AMQP_MANAGEMENT_OPEN_ERROR` the callback `on_cbs_open_complete` shall be called with `CBS_OPEN_ERROR` and the `on_cbs_open_complete_context` shall be passed as argument. ]*/
1761 /* Tests_SRS_CBS_01_113: [ When `on_amqp_management_open_complete` reports a failure, the underlying AMQP management shall be closed by calling `amqp_management_close`. ]*/
1762 TEST_FUNCTION(on_amqp_management_open_complete_with_ERROR_when_CBS_is_OPENING_indicates_the_open_complete)
1763 {
1764 // arrange
1765 CBS_HANDLE cbs;
1766 cbs = cbs_create(test_session_handle);
1767 (void)cbs_open_async(cbs, test_on_cbs_open_complete, (void*)0x4242, test_on_cbs_error, (void*)0x4243);
1768 umock_c_reset_all_calls();
1769
1770 STRICT_EXPECTED_CALL(amqp_management_close(test_amqp_management_handle));
1771 STRICT_EXPECTED_CALL(test_on_cbs_open_complete((void*)0x4242, CBS_OPEN_ERROR));
1772
1773 // act
1774 saved_on_amqp_management_open_complete(saved_on_amqp_management_open_complete_context, AMQP_MANAGEMENT_OPEN_ERROR);
1775
1776 // assert
1777 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1778
1779 // cleanup
1780 cbs_destroy(cbs);
1781 }
1782
1783 /* Tests_SRS_CBS_01_108: [ If CBS is OPENING and `open_result` is `AMQP_MANAGEMENT_OPEN_CANCELLED` the callback `on_cbs_open_complete` shall be called with `CBS_OPEN_CANCELLED` and the `on_cbs_open_complete_context` shall be passed as argument. ]*/
1784 /* Tests_SRS_CBS_01_113: [ When `on_amqp_management_open_complete` reports a failure, the underlying AMQP management shall be closed by calling `amqp_management_close`. ]*/
1785 TEST_FUNCTION(on_amqp_management_open_complete_with_CANCELLED_when_CBS_is_OPENING_indicates_the_open_complete)
1786 {
1787 // arrange
1788 CBS_HANDLE cbs;
1789 cbs = cbs_create(test_session_handle);
1790 (void)cbs_open_async(cbs, test_on_cbs_open_complete, (void*)0x4242, test_on_cbs_error, (void*)0x4243);
1791 umock_c_reset_all_calls();
1792
1793 STRICT_EXPECTED_CALL(amqp_management_close(test_amqp_management_handle));
1794 STRICT_EXPECTED_CALL(test_on_cbs_open_complete((void*)0x4242, CBS_OPEN_CANCELLED));
1795
1796 // act
1797 saved_on_amqp_management_open_complete(saved_on_amqp_management_open_complete_context, AMQP_MANAGEMENT_OPEN_CANCELLED);
1798
1799 // assert
1800 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1801
1802 // cleanup
1803 cbs_destroy(cbs);
1804 }
1805
1806 /* Tests_SRS_CBS_01_109: [ When `on_amqp_management_open_complete` is called when the CBS is OPEN, the callback `on_cbs_error` shall be called and the `on_cbs_error_context` shall be passed as argument. ]*/
1807 TEST_FUNCTION(on_amqp_management_open_complete_with_OK_when_already_OPEN_triggers_an_error)
1808 {
1809 // arrange
1810 CBS_HANDLE cbs;
1811 cbs = cbs_create(test_session_handle);
1812 (void)cbs_open_async(cbs, test_on_cbs_open_complete, (void*)0x4242, test_on_cbs_error, (void*)0x4243);
1813 saved_on_amqp_management_open_complete(saved_on_amqp_management_open_complete_context, AMQP_MANAGEMENT_OPEN_OK);
1814 umock_c_reset_all_calls();
1815
1816 STRICT_EXPECTED_CALL(test_on_cbs_error((void*)0x4243));
1817
1818 // act
1819 saved_on_amqp_management_open_complete(saved_on_amqp_management_open_complete_context, AMQP_MANAGEMENT_OPEN_OK);
1820
1821 // assert
1822 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1823
1824 // cleanup
1825 cbs_destroy(cbs);
1826 }
1827
1828 /* Tests_SRS_CBS_01_109: [ When `on_amqp_management_open_complete` is called when the CBS is OPEN, the callback `on_cbs_error` shall be called and the `on_cbs_error_context` shall be passed as argument. ]*/
1829 TEST_FUNCTION(on_amqp_management_open_complete_with_ERROR_when_already_OPEN_triggers_an_error)
1830 {
1831 // arrange
1832 CBS_HANDLE cbs;
1833 cbs = cbs_create(test_session_handle);
1834 (void)cbs_open_async(cbs, test_on_cbs_open_complete, (void*)0x4242, test_on_cbs_error, (void*)0x4243);
1835 saved_on_amqp_management_open_complete(saved_on_amqp_management_open_complete_context, AMQP_MANAGEMENT_OPEN_OK);
1836 umock_c_reset_all_calls();
1837
1838 STRICT_EXPECTED_CALL(test_on_cbs_error((void*)0x4243));
1839
1840 // act
1841 saved_on_amqp_management_open_complete(saved_on_amqp_management_open_complete_context, AMQP_MANAGEMENT_OPEN_ERROR);
1842
1843 // assert
1844 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1845
1846 // cleanup
1847 cbs_destroy(cbs);
1848 }
1849
1850 /* on_amqp_management_error */
1851
1852 /* Tests_SRS_CBS_01_110: [ When `on_amqp_management_error` is called with NULL `context`, it shall do nothing. ]*/
1853 TEST_FUNCTION(on_amqp_management_error_with_NULL_context_does_nothing)
1854 {
1855 // arrange
1856 CBS_HANDLE cbs;
1857 cbs = cbs_create(test_session_handle);
1858 (void)cbs_open_async(cbs, test_on_cbs_open_complete, (void*)0x4242, test_on_cbs_error, (void*)0x4243);
1859 saved_on_amqp_management_open_complete(saved_on_amqp_management_open_complete_context, AMQP_MANAGEMENT_OPEN_OK);
1860 umock_c_reset_all_calls();
1861
1862 // act
1863 saved_on_amqp_management_error(NULL);
1864
1865 // assert
1866 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1867
1868 // cleanup
1869 cbs_destroy(cbs);
1870 }
1871
1872 /* Tests_SRS_CBS_01_111: [ If CBS is OPENING the callback `on_cbs_open_complete` shall be called with `CBS_OPEN_ERROR` and the `on_cbs_open_complete_context` shall be passed as argument. ]*/
1873 /* Tests_SRS_CBS_01_114: [ Additionally the underlying AMQP management shall be closed by calling `amqp_management_close`. ]*/
1874 TEST_FUNCTION(on_amqp_management_error_when_OPENING_indicates_open_complete_with_error)
1875 {
1876 // arrange
1877 CBS_HANDLE cbs;
1878 cbs = cbs_create(test_session_handle);
1879 (void)cbs_open_async(cbs, test_on_cbs_open_complete, (void*)0x4242, test_on_cbs_error, (void*)0x4243);
1880 umock_c_reset_all_calls();
1881
1882 STRICT_EXPECTED_CALL(amqp_management_close(test_amqp_management_handle));
1883 STRICT_EXPECTED_CALL(test_on_cbs_open_complete((void*)0x4242, CBS_OPEN_ERROR));
1884
1885 // act
1886 saved_on_amqp_management_error(saved_on_amqp_management_error_context);
1887
1888 // assert
1889 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1890
1891 // cleanup
1892 cbs_destroy(cbs);
1893 }
1894
1895 /* Tests_SRS_CBS_01_112: [ If CBS is OPEN the callback `on_cbs_error` shall be called and the `on_cbs_error_context` shall be passed as argument. ]*/
1896 TEST_FUNCTION(on_amqp_management_error_when_OPEN_indicates_the_error_up)
1897 {
1898 // arrange
1899 CBS_HANDLE cbs;
1900 cbs = cbs_create(test_session_handle);
1901 (void)cbs_open_async(cbs, test_on_cbs_open_complete, (void*)0x4242, test_on_cbs_error, (void*)0x4243);
1902 saved_on_amqp_management_open_complete(saved_on_amqp_management_open_complete_context, AMQP_MANAGEMENT_OPEN_OK);
1903 umock_c_reset_all_calls();
1904
1905 STRICT_EXPECTED_CALL(test_on_cbs_error((void*)0x4243));
1906
1907 // act
1908 saved_on_amqp_management_error(saved_on_amqp_management_error_context);
1909
1910 // assert
1911 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1912
1913 // cleanup
1914 cbs_destroy(cbs);
1915 }
1916
1917 /* on_amqp_management_operation_complete */
1918
1919 /* Tests_SRS_CBS_01_091: [ When `on_amqp_management_operation_complete` is called with a NULL context it shall do nothing. ]*/
1920 TEST_FUNCTION(on_amqp_management_operation_complete_with_NULL_context_does_nothing)
1921 {
1922 // arrange
1923 CBS_HANDLE cbs;
1924 cbs = cbs_create(test_session_handle);
1925 (void)cbs_open_async(cbs, test_on_cbs_open_complete, (void*)0x4242, test_on_cbs_error, (void*)0x4243);
1926 saved_on_amqp_management_open_complete(saved_on_amqp_management_open_complete_context, AMQP_MANAGEMENT_OPEN_OK);
1927 (void)cbs_put_token_async(cbs, "some_type", "my_audience", "my_token", test_on_cbs_put_token_complete, (void*)0x4244);
1928 umock_c_reset_all_calls();
1929
1930 // act
1931 saved_on_execute_operation_complete(NULL, AMQP_MANAGEMENT_EXECUTE_OPERATION_OK, 200, "blah", test_response_message);
1932
1933 // assert
1934 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1935
1936 // cleanup
1937 cbs_destroy(cbs);
1938 }
1939
1940 /* Tests_SRS_CBS_01_104: [ If `singlylinkedlist_item_get_value` returns NULL, `on_amqp_management_operation_complete` shall do nothing. ]*/
1941 TEST_FUNCTION(when_singlylinkedlist_item_get_value_fails_then_on_amqp_management_operation_complete_does_nothing_more)
1942 {
1943 // arrange
1944 CBS_HANDLE cbs;
1945 cbs = cbs_create(test_session_handle);
1946 (void)cbs_open_async(cbs, test_on_cbs_open_complete, (void*)0x4242, test_on_cbs_error, (void*)0x4243);
1947 saved_on_amqp_management_open_complete(saved_on_amqp_management_open_complete_context, AMQP_MANAGEMENT_OPEN_OK);
1948 (void)cbs_put_token_async(cbs, "some_type", "my_audience", "my_token", test_on_cbs_put_token_complete, (void*)0x4244);
1949 umock_c_reset_all_calls();
1950
1951 STRICT_EXPECTED_CALL(singlylinkedlist_item_get_value(IGNORED_PTR_ARG))
1952 .SetReturn(NULL);
1953
1954 // act
1955 saved_on_execute_operation_complete(saved_on_execute_operation_complete_context, AMQP_MANAGEMENT_EXECUTE_OPERATION_OK, 200, "blah", test_response_message);
1956
1957 // assert
1958 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1959
1960 // cleanup
1961 cbs_destroy(cbs);
1962 }
1963
1964 /* Tests_SRS_CBS_01_092: [ When `on_amqp_management_operation_complete` is called with `AMQP_MANAGEMENT_EXECUTE_OPERATION_OK`, the associated cbs operation complete callback shall be called with `CBS_OPERATION_RESULT_OK` and passing the `on_cbs_put_token_complete_context` as the context argument. ]*/
1965 /* Tests_SRS_CBS_01_095: [ `status_code` and `status_description` shall be passed as they are to the cbs operation complete callback. ]*/
1966 /* Tests_SRS_CBS_01_103: [ The `context` shall be used to obtain the pending operation information stored in the pending operations linked list by calling `singlylinkedlist_item_get_value`. ]*/
1967 /* Tests_SRS_CBS_01_102: [ The pending operation shall be removed from the pending operations list by calling `singlylinkedlist_remove`. ]*/
1968 /* Tests_SRS_CBS_01_096: [ The `context` for the operation shall also be freed. ]*/
1969 /* Tests_SRS_CBS_01_014: [ The response message has the following application-properties: ]*/
1970 /* Tests_SRS_CBS_01_013: [ status-code No int HTTP response code [RFC2616]. ]*/
1971 /* Tests_SRS_CBS_01_015: [ status-description Yes string Description of the status. ]*/
1972 /* Tests_SRS_CBS_01_016: [ The body of the message MUST be empty. ]*/
1973 TEST_FUNCTION(on_amqp_management_operation_complete_with_OK_triggers_the_cbs_operation_complete_with_OK)
1974 {
1975 // arrange
1976 CBS_HANDLE cbs;
1977 cbs = cbs_create(test_session_handle);
1978 (void)cbs_open_async(cbs, test_on_cbs_open_complete, (void*)0x4242, test_on_cbs_error, (void*)0x4243);
1979 saved_on_amqp_management_open_complete(saved_on_amqp_management_open_complete_context, AMQP_MANAGEMENT_OPEN_OK);
1980 (void)cbs_put_token_async(cbs, "some_type", "my_audience", "my_token", test_on_cbs_put_token_complete, (void*)0x4244);
1981 umock_c_reset_all_calls();
1982
1983 STRICT_EXPECTED_CALL(singlylinkedlist_item_get_value(IGNORED_PTR_ARG));
1984 STRICT_EXPECTED_CALL(test_on_cbs_put_token_complete((void*)0x4244, CBS_OPERATION_RESULT_OK, 200, "blah"));
1985 STRICT_EXPECTED_CALL(singlylinkedlist_remove(test_singlylinkedlist, IGNORED_PTR_ARG));
1986 STRICT_EXPECTED_CALL(async_operation_destroy(IGNORED_PTR_ARG));
1987
1988 // act
1989 saved_on_execute_operation_complete(saved_on_execute_operation_complete_context, AMQP_MANAGEMENT_EXECUTE_OPERATION_OK, 200, "blah", test_response_message);
1990
1991 // assert
1992 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1993
1994 // cleanup
1995 cbs_destroy(cbs);
1996 }
1997
1998 /* Tests_SRS_CBS_01_093: [ When `on_amqp_management_operation_complete` is called with `AMQP_MANAGEMENT_EXECUTE_OPERATION_ERROR`, the associated cbs operation complete callback shall be called with `CBS_OPERATION_RESULT_CBS_ERROR` and passing the `on_cbs_put_token_complete_context` as the context argument. ]*/
1999 /* Tests_SRS_CBS_01_095: [ `status_code` and `status_description` shall be passed as they are to the cbs operation complete callback. ]*/
2000 /* Tests_SRS_CBS_01_103: [ The `context` shall be used to obtain the pending operation information stored in the pending operations linked list by calling `singlylinkedlist_item_get_value`. ]*/
2001 /* Tests_SRS_CBS_01_102: [ The pending operation shall be removed from the pending operations list by calling `singlylinkedlist_remove`. ]*/
2002 /* Tests_SRS_CBS_01_096: [ The `context` for the operation shall also be freed. ]*/
2003 TEST_FUNCTION(on_amqp_management_operation_complete_with_ERROR_triggers_the_cbs_operation_complete_with_ERROR)
2004 {
2005 // arrange
2006 CBS_HANDLE cbs;
2007 cbs = cbs_create(test_session_handle);
2008 (void)cbs_open_async(cbs, test_on_cbs_open_complete, (void*)0x4242, test_on_cbs_error, (void*)0x4243);
2009 saved_on_amqp_management_open_complete(saved_on_amqp_management_open_complete_context, AMQP_MANAGEMENT_OPEN_OK);
2010 (void)cbs_put_token_async(cbs, "some_type", "my_audience", "my_token", test_on_cbs_put_token_complete, (void*)0x4244);
2011 umock_c_reset_all_calls();
2012
2013 STRICT_EXPECTED_CALL(singlylinkedlist_item_get_value(IGNORED_PTR_ARG));
2014 STRICT_EXPECTED_CALL(test_on_cbs_put_token_complete((void*)0x4244, CBS_OPERATION_RESULT_CBS_ERROR, 401, "blah"));
2015 STRICT_EXPECTED_CALL(singlylinkedlist_remove(test_singlylinkedlist, IGNORED_PTR_ARG));
2016 STRICT_EXPECTED_CALL(async_operation_destroy(IGNORED_PTR_ARG));
2017
2018 // act
2019 saved_on_execute_operation_complete(saved_on_execute_operation_complete_context, AMQP_MANAGEMENT_EXECUTE_OPERATION_ERROR, 401, "blah", test_response_message);
2020
2021 // assert
2022 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2023
2024 // cleanup
2025 cbs_destroy(cbs);
2026 }
2027
2028 /* Tests_SRS_CBS_01_094: [ When `on_amqp_management_operation_complete` is called with `AMQP_MANAGEMENT_EXECUTE_OPERATION_FAILED_BAD_STATUS`, the associated cbs operation complete callback shall be called with `CBS_OPERATION_RESULT_OPERATION_FAILED` and passing the `on_cbs_put_token_complete_context` as the context argument. ]*/
2029 /* Tests_SRS_CBS_01_095: [ `status_code` and `status_description` shall be passed as they are to the cbs operation complete callback. ]*/
2030 /* Tests_SRS_CBS_01_103: [ The `context` shall be used to obtain the pending operation information stored in the pending operations linked list by calling `singlylinkedlist_item_get_value`. ]*/
2031 /* Tests_SRS_CBS_01_102: [ The pending operation shall be removed from the pending operations list by calling `singlylinkedlist_remove`. ]*/
2032 /* Tests_SRS_CBS_01_096: [ The `context` for the operation shall also be freed. ]*/
2033 TEST_FUNCTION(on_amqp_management_operation_complete_with_OPERATION_FAILED_BAD_STATUS_triggers_the_cbs_operation_complete_with_OPERATION_FAILED)
2034 {
2035 // arrange
2036 CBS_HANDLE cbs;
2037 cbs = cbs_create(test_session_handle);
2038 (void)cbs_open_async(cbs, test_on_cbs_open_complete, (void*)0x4242, test_on_cbs_error, (void*)0x4243);
2039 saved_on_amqp_management_open_complete(saved_on_amqp_management_open_complete_context, AMQP_MANAGEMENT_OPEN_OK);
2040 (void)cbs_put_token_async(cbs, "some_type", "my_audience", "my_token", test_on_cbs_put_token_complete, (void*)0x4244);
2041 umock_c_reset_all_calls();
2042
2043 STRICT_EXPECTED_CALL(singlylinkedlist_item_get_value(IGNORED_PTR_ARG));
2044 STRICT_EXPECTED_CALL(test_on_cbs_put_token_complete((void*)0x4244, CBS_OPERATION_RESULT_OPERATION_FAILED, 0, "blah"));
2045 STRICT_EXPECTED_CALL(singlylinkedlist_remove(test_singlylinkedlist, IGNORED_PTR_ARG));
2046 STRICT_EXPECTED_CALL(async_operation_destroy(IGNORED_PTR_ARG));
2047
2048 // act
2049 saved_on_execute_operation_complete(saved_on_execute_operation_complete_context, AMQP_MANAGEMENT_EXECUTE_OPERATION_FAILED_BAD_STATUS, 0, "blah", test_response_message);
2050
2051 // assert
2052 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2053
2054 // cleanup
2055 cbs_destroy(cbs);
2056 }
2057
2058 /* Tests_SRS_CBS_01_115: [ When `on_amqp_management_execute_operation_complete` is called with `AMQP_MANAGEMENT_EXECUTE_OPERATION_INSTANCE_CLOSED`, the associated cbs operation complete callback shall be called with `CBS_OPERATION_RESULT_INSTANCE_CLOSED` and passing the `on_cbs_put_token_complete_context` as the context argument. ]*/
2059 /* Tests_SRS_CBS_01_095: [ `status_code` and `status_description` shall be passed as they are to the cbs operation complete callback. ]*/
2060 /* Tests_SRS_CBS_01_103: [ The `context` shall be used to obtain the pending operation information stored in the pending operations linked list by calling `singlylinkedlist_item_get_value`. ]*/
2061 /* Tests_SRS_CBS_01_102: [ The pending operation shall be removed from the pending operations list by calling `singlylinkedlist_remove`. ]*/
2062 /* Tests_SRS_CBS_01_096: [ The `context` for the operation shall also be freed. ]*/
2063 TEST_FUNCTION(on_amqp_management_operation_complete_with_INSTANCE_CLOSED_triggers_the_cbs_operation_complete_with_INSTANCE_CLOSED)
2064 {
2065 // arrange
2066 CBS_HANDLE cbs;
2067 cbs = cbs_create(test_session_handle);
2068 (void)cbs_open_async(cbs, test_on_cbs_open_complete, (void*)0x4242, test_on_cbs_error, (void*)0x4243);
2069 saved_on_amqp_management_open_complete(saved_on_amqp_management_open_complete_context, AMQP_MANAGEMENT_OPEN_OK);
2070 (void)cbs_put_token_async(cbs, "some_type", "my_audience", "my_token", test_on_cbs_put_token_complete, (void*)0x4244);
2071 umock_c_reset_all_calls();
2072
2073 STRICT_EXPECTED_CALL(singlylinkedlist_item_get_value(IGNORED_PTR_ARG));
2074 STRICT_EXPECTED_CALL(test_on_cbs_put_token_complete((void*)0x4244, CBS_OPERATION_RESULT_INSTANCE_CLOSED, 0, "blah"));
2075 STRICT_EXPECTED_CALL(singlylinkedlist_remove(test_singlylinkedlist, IGNORED_PTR_ARG));
2076 STRICT_EXPECTED_CALL(async_operation_destroy(IGNORED_PTR_ARG));
2077
2078 // act
2079 saved_on_execute_operation_complete(saved_on_execute_operation_complete_context, AMQP_MANAGEMENT_EXECUTE_OPERATION_INSTANCE_CLOSED, 0, "blah", test_response_message);
2080
2081 // assert
2082 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2083
2084 // cleanup
2085 cbs_destroy(cbs);
2086 }
2087
2088 /* Tests_SRS_CBS_01_092: [ When `on_amqp_management_operation_complete` is called with `AMQP_MANAGEMENT_EXECUTE_OPERATION_OK`, the associated cbs operation complete callback shall be called with `CBS_OPERATION_RESULT_OK` and passing the `on_cbs_put_token_complete_context` as the context argument. ]*/
2089 /* Tests_SRS_CBS_01_095: [ `status_code` and `status_description` shall be passed as they are to the cbs operation complete callback. ]*/
2090 /* Tests_SRS_CBS_01_103: [ The `context` shall be used to obtain the pending operation information stored in the pending operations linked list by calling `singlylinkedlist_item_get_value`. ]*/
2091 /* Tests_SRS_CBS_01_102: [ The pending operation shall be removed from the pending operations list by calling `singlylinkedlist_remove`. ]*/
2092 /* Tests_SRS_CBS_01_096: [ The `context` for the operation shall also be freed. ]*/
2093 /* Tests_SRS_CBS_01_026: [ The response message has the following application-properties: ]*/
2094 /* Tests_SRS_CBS_01_027: [ status-code Yes int HTTP response code [RFC2616]. ]*/
2095 /* Tests_SRS_CBS_01_028: [ status-description No string Description of the status. ]*/
2096 /* Tests_SRS_CBS_01_029: [ The body of the message MUST be empty. ]*/
2097 TEST_FUNCTION(on_amqp_management_operation_complete_with_OK_for_delete_token_triggers_the_cbs_operation_complete_with_OK)
2098 {
2099 // arrange
2100 CBS_HANDLE cbs;
2101 cbs = cbs_create(test_session_handle);
2102 (void)cbs_open_async(cbs, test_on_cbs_open_complete, (void*)0x4242, test_on_cbs_error, (void*)0x4243);
2103 saved_on_amqp_management_open_complete(saved_on_amqp_management_open_complete_context, AMQP_MANAGEMENT_OPEN_OK);
2104 (void)cbs_delete_token_async(cbs, "some_type", "my_audience", test_on_cbs_delete_token_complete, (void*)0x4244);
2105 umock_c_reset_all_calls();
2106
2107 STRICT_EXPECTED_CALL(singlylinkedlist_item_get_value(IGNORED_PTR_ARG));
2108 STRICT_EXPECTED_CALL(test_on_cbs_delete_token_complete((void*)0x4244, CBS_OPERATION_RESULT_OK, 200, "blah"));
2109 STRICT_EXPECTED_CALL(singlylinkedlist_remove(test_singlylinkedlist, IGNORED_PTR_ARG));
2110 STRICT_EXPECTED_CALL(async_operation_destroy(IGNORED_PTR_ARG));
2111
2112 // act
2113 saved_on_execute_operation_complete(saved_on_execute_operation_complete_context, AMQP_MANAGEMENT_EXECUTE_OPERATION_OK, 200, "blah", test_response_message);
2114
2115 // assert
2116 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2117
2118 // cleanup
2119 cbs_destroy(cbs);
2120 }
2121
2122 /* Tests_SRS_CBS_01_093: [ When `on_amqp_management_operation_complete` is called with `AMQP_MANAGEMENT_EXECUTE_OPERATION_ERROR`, the associated cbs operation complete callback shall be called with `CBS_OPERATION_RESULT_CBS_ERROR` and passing the `on_cbs_put_token_complete_context` as the context argument. ]*/
2123 /* Tests_SRS_CBS_01_095: [ `status_code` and `status_description` shall be passed as they are to the cbs operation complete callback. ]*/
2124 /* Tests_SRS_CBS_01_103: [ The `context` shall be used to obtain the pending operation information stored in the pending operations linked list by calling `singlylinkedlist_item_get_value`. ]*/
2125 /* Tests_SRS_CBS_01_102: [ The pending operation shall be removed from the pending operations list by calling `singlylinkedlist_remove`. ]*/
2126 /* Tests_SRS_CBS_01_096: [ The `context` for the operation shall also be freed. ]*/
2127 TEST_FUNCTION(on_amqp_management_operation_complete_with_ERROR_for_delete_token_triggers_the_cbs_operation_complete_with_ERROR)
2128 {
2129 // arrange
2130 CBS_HANDLE cbs;
2131 cbs = cbs_create(test_session_handle);
2132 (void)cbs_open_async(cbs, test_on_cbs_open_complete, (void*)0x4242, test_on_cbs_error, (void*)0x4243);
2133 saved_on_amqp_management_open_complete(saved_on_amqp_management_open_complete_context, AMQP_MANAGEMENT_OPEN_OK);
2134 (void)cbs_delete_token_async(cbs, "some_type", "my_audience", test_on_cbs_delete_token_complete, (void*)0x4244);
2135 umock_c_reset_all_calls();
2136
2137 STRICT_EXPECTED_CALL(singlylinkedlist_item_get_value(IGNORED_PTR_ARG));
2138 STRICT_EXPECTED_CALL(test_on_cbs_delete_token_complete((void*)0x4244, CBS_OPERATION_RESULT_CBS_ERROR, 401, "blah"));
2139 STRICT_EXPECTED_CALL(singlylinkedlist_remove(test_singlylinkedlist, IGNORED_PTR_ARG));
2140 STRICT_EXPECTED_CALL(async_operation_destroy(IGNORED_PTR_ARG));
2141
2142 // act
2143 saved_on_execute_operation_complete(saved_on_execute_operation_complete_context, AMQP_MANAGEMENT_EXECUTE_OPERATION_ERROR, 401, "blah", test_response_message);
2144
2145 // assert
2146 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2147
2148 // cleanup
2149 cbs_destroy(cbs);
2150 }
2151
2152 /* Tests_SRS_CBS_01_094: [ When `on_amqp_management_operation_complete` is called with `AMQP_MANAGEMENT_EXECUTE_OPERATION_FAILED_BAD_STATUS`, the associated cbs operation complete callback shall be called with `CBS_OPERATION_RESULT_OPERATION_FAILED` and passing the `on_cbs_put_token_complete_context` as the context argument. ]*/
2153 /* Tests_SRS_CBS_01_095: [ `status_code` and `status_description` shall be passed as they are to the cbs operation complete callback. ]*/
2154 /* Tests_SRS_CBS_01_103: [ The `context` shall be used to obtain the pending operation information stored in the pending operations linked list by calling `singlylinkedlist_item_get_value`. ]*/
2155 /* Tests_SRS_CBS_01_102: [ The pending operation shall be removed from the pending operations list by calling `singlylinkedlist_remove`. ]*/
2156 /* Tests_SRS_CBS_01_096: [ The `context` for the operation shall also be freed. ]*/
2157 TEST_FUNCTION(on_amqp_management_operation_complete_with_OPERATION_FAILED_BAD_STATUSfor_delete_token_triggers_the_cbs_operation_complete_with_OPERATION_FAILED)
2158 {
2159 // arrange
2160 CBS_HANDLE cbs;
2161 cbs = cbs_create(test_session_handle);
2162 (void)cbs_open_async(cbs, test_on_cbs_open_complete, (void*)0x4242, test_on_cbs_error, (void*)0x4243);
2163 saved_on_amqp_management_open_complete(saved_on_amqp_management_open_complete_context, AMQP_MANAGEMENT_OPEN_OK);
2164 (void)cbs_delete_token_async(cbs, "some_type", "my_audience", test_on_cbs_delete_token_complete, (void*)0x4244);
2165 umock_c_reset_all_calls();
2166
2167 STRICT_EXPECTED_CALL(singlylinkedlist_item_get_value(IGNORED_PTR_ARG));
2168 STRICT_EXPECTED_CALL(test_on_cbs_delete_token_complete((void*)0x4244, CBS_OPERATION_RESULT_OPERATION_FAILED, 0, "blah"));
2169 STRICT_EXPECTED_CALL(singlylinkedlist_remove(test_singlylinkedlist, IGNORED_PTR_ARG));
2170 STRICT_EXPECTED_CALL(async_operation_destroy(IGNORED_PTR_ARG));
2171
2172 // act
2173 saved_on_execute_operation_complete(saved_on_execute_operation_complete_context, AMQP_MANAGEMENT_EXECUTE_OPERATION_FAILED_BAD_STATUS, 0, "blah", test_response_message);
2174
2175 // assert
2176 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2177
2178 // cleanup
2179 cbs_destroy(cbs);
2180 }
2181
2182 /* Tests_SRS_CBS_01_115: [ When `on_amqp_management_execute_operation_complete` is called with `AMQP_MANAGEMENT_EXECUTE_OPERATION_INSTANCE_CLOSED`, the associated cbs operation complete callback shall be called with `CBS_OPERATION_RESULT_INSTANCE_CLOSED` and passing the `on_cbs_put_token_complete_context` as the context argument. ]*/
2183 /* Tests_SRS_CBS_01_095: [ `status_code` and `status_description` shall be passed as they are to the cbs operation complete callback. ]*/
2184 /* Tests_SRS_CBS_01_103: [ The `context` shall be used to obtain the pending operation information stored in the pending operations linked list by calling `singlylinkedlist_item_get_value`. ]*/
2185 /* Tests_SRS_CBS_01_102: [ The pending operation shall be removed from the pending operations list by calling `singlylinkedlist_remove`. ]*/
2186 /* Tests_SRS_CBS_01_096: [ The `context` for the operation shall also be freed. ]*/
2187 TEST_FUNCTION(on_amqp_management_operation_complete_with_INSTANCE_CLOSED_for_delete_token_triggers_the_cbs_operation_complete_with_INSTANCE_CLOSED)
2188 {
2189 // arrange
2190 CBS_HANDLE cbs;
2191 cbs = cbs_create(test_session_handle);
2192 (void)cbs_open_async(cbs, test_on_cbs_open_complete, (void*)0x4242, test_on_cbs_error, (void*)0x4243);
2193 saved_on_amqp_management_open_complete(saved_on_amqp_management_open_complete_context, AMQP_MANAGEMENT_OPEN_OK);
2194 (void)cbs_delete_token_async(cbs, "some_type", "my_audience", test_on_cbs_delete_token_complete, (void*)0x4244);
2195 umock_c_reset_all_calls();
2196
2197 STRICT_EXPECTED_CALL(singlylinkedlist_item_get_value(IGNORED_PTR_ARG));
2198 STRICT_EXPECTED_CALL(test_on_cbs_delete_token_complete((void*)0x4244, CBS_OPERATION_RESULT_INSTANCE_CLOSED, 0, "blah"));
2199 STRICT_EXPECTED_CALL(singlylinkedlist_remove(test_singlylinkedlist, IGNORED_PTR_ARG));
2200 STRICT_EXPECTED_CALL(async_operation_destroy(IGNORED_PTR_ARG));
2201
2202 // act
2203 saved_on_execute_operation_complete(saved_on_execute_operation_complete_context, AMQP_MANAGEMENT_EXECUTE_OPERATION_INSTANCE_CLOSED, 0, "blah", test_response_message);
2204
2205 // assert
2206 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2207
2208 // cleanup
2209 cbs_destroy(cbs);
2210 }
2211
2212 END_TEST_SUITE(cbs_ut)