Debian Codegraph @

src/vendor/azure-uamqp-c/tests/saslclientio_ut/saslclientio_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 <cstdint>
7 #else
8 #include <stdlib.h>
9 #include <stdint.h>
10 #endif
11
12 #include "azure_macro_utils/macro_utils.h"
13 #include "testrunnerswitcher.h"
14 #include "umock_c/umock_c.h"
15 #include "umock_c/umocktypes_stdint.h"
16 #include "umock_c/umocktypes_charptr.h"
17 #include "umock_c/umocktypes_bool.h"
18
19 #if defined _MSC_VER
20 #pragma warning(disable: 4054) /* MSC incorrectly fires this */
21 #endif
22
23 static void* my_gballoc_malloc(size_t size)
24 {
25 return malloc(size);
26 }
27
28 static void* my_gballoc_calloc(size_t nmemb, size_t size)
29 {
30 return calloc(nmemb, size);
31 }
32
33 static void my_gballoc_free(void* ptr)
34 {
35 free(ptr);
36 }
37
38 static void* my_gballoc_realloc(void* ptr, size_t size)
39 {
40 return realloc(ptr, size);
41 }
42
43 #define ENABLE_MOCKS
44
45 #include "azure_c_shared_utility/gballoc.h"
46 #include "azure_c_shared_utility/xio.h"
47 #include "azure_uamqp_c/amqpvalue.h"
48 #include "azure_uamqp_c/amqpvalue_to_string.h"
49 #include "azure_uamqp_c/frame_codec.h"
50 #include "azure_uamqp_c/sasl_frame_codec.h"
51 #include "azure_uamqp_c/sasl_mechanism.h"
52 #include "azure_uamqp_c/amqp_definitions.h"
53
54 #undef ENABLE_MOCKS
55
56 #include "azure_uamqp_c/saslclientio.h"
57
58 static XIO_HANDLE test_underlying_io = (XIO_HANDLE)0x4242;
59 static SASL_MECHANISM_HANDLE test_sasl_mechanism = (SASL_MECHANISM_HANDLE)0x4243;
60 static FRAME_CODEC_HANDLE test_frame_codec = (FRAME_CODEC_HANDLE)0x4244;
61 static SASL_FRAME_CODEC_HANDLE test_sasl_frame_codec = (SASL_FRAME_CODEC_HANDLE)0x4245;
62 static AMQP_VALUE test_descriptor_value = (AMQP_VALUE)0x4246;
63 static AMQP_VALUE test_sasl_server_mechanism = (AMQP_VALUE)0x4247;
64 static const char* test_mechanism = "test_mechanism";
65 static SASL_OUTCOME_HANDLE test_sasl_outcome_handle = (SASL_OUTCOME_HANDLE)0x4243;
66 static SASL_INIT_HANDLE test_sasl_init = (SASL_INIT_HANDLE)0x4244;
67 static AMQP_VALUE test_sasl_init_value = (AMQP_VALUE)0x4245;
68 static OPTIONHANDLER_HANDLE test_optionhandler_handle = (OPTIONHANDLER_HANDLE)0x4246;
69 static SASL_MECHANISMS_HANDLE test_sasl_mechanisms_handle = (SASL_MECHANISMS_HANDLE)0x5001;
70 static AMQP_VALUE test_sasl_server_mechanisms_value = (AMQP_VALUE)0x5002;
71 static SASL_CHALLENGE_HANDLE test_sasl_challenge_handle = (SASL_CHALLENGE_HANDLE)0x5003;
72 static SASL_RESPONSE_HANDLE test_sasl_response_handle = (SASL_RESPONSE_HANDLE)0x5004;
73 static AMQP_VALUE test_sasl_response_amqp_value = (AMQP_VALUE)0x5005;
74
75 static ON_IO_OPEN_COMPLETE saved_on_io_open_complete;
76 static void* saved_on_io_open_complete_context;
77 static ON_BYTES_RECEIVED saved_on_bytes_received;
78 static void* saved_on_bytes_received_context;
79 static ON_IO_ERROR saved_on_io_error;
80 static void* saved_on_io_error_context;
81 static ON_IO_CLOSE_COMPLETE saved_on_io_close_complete;
82 static void* saved_on_io_close_complete_context;
83
84 static ON_SASL_FRAME_RECEIVED saved_on_sasl_frame_received;
85 static ON_SASL_FRAME_CODEC_ERROR saved_on_sasl_frame_codec_error;
86 static void* saved_sasl_frame_codec_callback_context;
87
88 static ON_FRAME_RECEIVED saved_frame_received_callback;
89 static void* saved_frame_received_callback_context;
90
91 static ON_BYTES_ENCODED saved_on_bytes_encoded;
92 static void* saved_on_bytes_encoded_callback_context;
93
94 static ON_FRAME_CODEC_ERROR saved_on_frame_codec_error;
95 static void* saved_on_frame_codec_error_callback_context;
96
97 /* Tests_SRS_SASLCLIENTIO_01_002: [The protocol header consists of the upper case ASCII letters "AMQP" followed by a protocol id of three, followed by three unsigned bytes representing the major, minor, and revision of the specification version (currently 1 (SASL-MAJOR), 0 (SASLMINOR), 0 (SASL-REVISION)).] */
98 /* Tests_SRS_SASLCLIENTIO_01_124: [SASL-MAJOR 1 major protocol version.] */
99 /* Tests_SRS_SASLCLIENTIO_01_125: [SASL-MINOR 0 minor protocol version.] */
100 /* Tests_SRS_SASLCLIENTIO_01_126: [SASL-REVISION 0 protocol revision.] */
101 static const unsigned char sasl_header[] = { 'A', 'M', 'Q', 'P', 3, 1, 0, 0 };
102 static const unsigned char test_sasl_mechanisms_frame[] = { 'x', '1' }; /* these are some dummy bytes */
103 static const unsigned char test_sasl_outcome[] = { 'x', '2' }; /* these are some dummy bytes */
104 static const unsigned char test_sasl_challenge[] = { 'x', '3' }; /* these are some dummy bytes */
105
106 static AMQP_VALUE test_sasl_value = (AMQP_VALUE)0x5242;
107
108 static char expected_stringified_io[8192];
109 static char actual_stringified_io[8192];
110 static unsigned char* frame_codec_received_bytes;
111 static size_t frame_codec_received_byte_count;
112 static unsigned char* io_send_bytes;
113 static size_t io_send_byte_count;
114
115 static int umocktypes_copy_SASL_MECHANISM_BYTES_ptr(SASL_MECHANISM_BYTES** destination, const SASL_MECHANISM_BYTES** source)
116 {
117 int result;
118
119 if (*source == NULL)
120 {
121 *destination = NULL;
122 result = 0;
123 }
124 else
125 {
126 *destination = (SASL_MECHANISM_BYTES*)my_gballoc_malloc(sizeof(SASL_MECHANISM_BYTES));
127 if (*destination == NULL)
128 {
129 result = __LINE__;
130 }
131 else
132 {
133 (*destination)->length = (*source)->length;
134 if ((*destination)->length == 0)
135 {
136 (*destination)->bytes = NULL;
137 result = 0;
138 }
139 else
140 {
141 (*destination)->bytes = (const unsigned char*)my_gballoc_malloc((*destination)->length);
142 if ((*destination)->bytes == NULL)
143 {
144 my_gballoc_free(*destination);
145 result = __LINE__;
146 }
147 else
148 {
149 (void)memcpy((void*)(*destination)->bytes, (*source)->bytes, (*destination)->length);
150 result = 0;
151 }
152 }
153 }
154 }
155
156 return result;
157 }
158
159 static void umocktypes_free_SASL_MECHANISM_BYTES_ptr(SASL_MECHANISM_BYTES** value)
160 {
161 if (*value != NULL)
162 {
163 my_gballoc_free((void*)(*value)->bytes);
164 my_gballoc_free(*value);
165 }
166 }
167
168 static char* umocktypes_stringify_SASL_MECHANISM_BYTES_ptr(const SASL_MECHANISM_BYTES** value)
169 {
170 char* result;
171 if (*value == NULL)
172 {
173 result = (char*)my_gballoc_malloc(5);
174 if (result != NULL)
175 {
176 (void)memcpy(result, "NULL", 5);
177 }
178 }
179 else
180 {
181 result = (char*)my_gballoc_malloc(3 + (5 * (*value)->length));
182 if (result != NULL)
183 {
184 size_t pos = 0;
185 size_t i;
186
187 result[pos++] = '[';
188 for (i = 0; i < (*value)->length; i++)
189 {
190 (void)sprintf(&result[pos], "0x%02X ", ((unsigned char*)(*value)->bytes)[i]);
191 pos += 5;
192 }
193 result[pos++] = ']';
194 result[pos++] = '\0';
195 }
196 }
197
198 return result;
199 }
200
201 static int umocktypes_are_equal_SASL_MECHANISM_BYTES_ptr(SASL_MECHANISM_BYTES** left, SASL_MECHANISM_BYTES** right)
202 {
203 int result;
204
205 if (*left == *right)
206 {
207 result = 1;
208 }
209 else
210 {
211 if ((*left == NULL) ||
212 (*right == NULL))
213 {
214 result = 0;
215 }
216 else
217 {
218 if ((*left)->length != (*right)->length)
219 {
220 result = 0;
221 }
222 else
223 {
224 if ((*left)->length == 0)
225 {
226 result = 1;
227 }
228 else
229 {
230 result = (memcmp((*left)->bytes, (*right)->bytes, (*left)->length) == 0) ? 1 : 0;
231 }
232 }
233 }
234 }
235
236 return result;
237 }
238
239 static void stringify_bytes(const unsigned char* bytes, size_t byte_count, char* output_string)
240 {
241 size_t i;
242 size_t pos = 0;
243
244 output_string[pos++] = '[';
245 for (i = 0; i < byte_count; i++)
246 {
247 (void)sprintf(&output_string[pos], "0x%02X", bytes[i]);
248 if (i < byte_count - 1)
249 {
250 strcat(output_string, ",");
251 }
252 pos = strlen(output_string);
253 }
254 output_string[pos++] = ']';
255 output_string[pos++] = '\0';
256 }
257
258 static const unsigned char test_challenge_bytes[] = { 0x42 };
259 static const unsigned char test_response_bytes[] = { 0x43, 0x44 };
260 static const amqp_binary some_challenge_bytes = { test_challenge_bytes, sizeof(test_challenge_bytes) };
261 static const SASL_MECHANISM_BYTES sasl_mechanism_challenge_bytes = { test_challenge_bytes, sizeof(test_challenge_bytes) };
262 static const SASL_MECHANISM_BYTES sasl_mechanism_response_bytes = { test_response_bytes, sizeof(test_response_bytes) };
263 static const amqp_binary response_binary_value = { test_response_bytes, sizeof(test_response_bytes) };
264
265 static FRAME_CODEC_HANDLE my_frame_codec_create(ON_FRAME_CODEC_ERROR on_frame_codec_error, void* callback_context)
266 {
267 saved_on_frame_codec_error = on_frame_codec_error;
268 saved_on_frame_codec_error_callback_context = callback_context;
269 return test_frame_codec;
270 }
271
272 static int my_frame_codec_receive_bytes(FRAME_CODEC_HANDLE frame_codec, const unsigned char* buffer, size_t size)
273 {
274 unsigned char* new_bytes = (unsigned char*)my_gballoc_realloc(frame_codec_received_bytes, frame_codec_received_byte_count + size);
275 (void)frame_codec;
276 if (new_bytes != NULL)
277 {
278 frame_codec_received_bytes = new_bytes;
279 (void)memcpy(frame_codec_received_bytes + frame_codec_received_byte_count, buffer, size);
280 frame_codec_received_byte_count += size;
281 }
282 return 0;
283 }
284
285 static int my_frame_codec_subscribe(FRAME_CODEC_HANDLE frame_codec, uint8_t type, ON_FRAME_RECEIVED frame_received_callback, void* callback_context)
286 {
287 (void)type;
288 (void)frame_codec;
289 saved_frame_received_callback = frame_received_callback;
290 saved_frame_received_callback_context = callback_context;
291 return 0;
292 }
293
294 static SASL_FRAME_CODEC_HANDLE my_sasl_frame_codec_create(FRAME_CODEC_HANDLE frame_codec, ON_SASL_FRAME_RECEIVED on_sasl_frame_received, ON_SASL_FRAME_CODEC_ERROR on_sasl_frame_codec_error, void* callback_context)
295 {
296 (void)frame_codec;
297 saved_on_sasl_frame_received = on_sasl_frame_received;
298 saved_sasl_frame_codec_callback_context = callback_context;
299 saved_on_sasl_frame_codec_error = on_sasl_frame_codec_error;
300 return test_sasl_frame_codec;
301 }
302
303 static int my_sasl_frame_codec_encode_frame(SASL_FRAME_CODEC_HANDLE sasl_frame_codec, AMQP_VALUE sasl_frame_value, ON_BYTES_ENCODED on_bytes_encoded, void* callback_context)
304 {
305 (void)sasl_frame_codec;
306 (void)sasl_frame_value;
307 saved_on_bytes_encoded = on_bytes_encoded;
308 saved_on_bytes_encoded_callback_context = callback_context;
309 return 0;
310 }
311
312 static int my_xio_open(XIO_HANDLE xio, ON_IO_OPEN_COMPLETE on_io_open_complete, void* on_io_open_complete_context, ON_BYTES_RECEIVED on_bytes_received, void* on_bytes_received_context, ON_IO_ERROR on_io_error, void* on_io_error_context)
313 {
314 (void)xio;
315 saved_on_io_open_complete = on_io_open_complete;
316 saved_on_io_open_complete_context = on_io_open_complete_context;
317 saved_on_bytes_received = on_bytes_received;
318 saved_on_bytes_received_context = on_bytes_received_context;
319 saved_on_io_error = on_io_error;
320 saved_on_io_error_context = on_io_error_context;
321 return 0;
322 }
323
324 static int my_xio_close(XIO_HANDLE xio, ON_IO_CLOSE_COMPLETE on_io_close_complete, void* callback_context)
325 {
326 (void)xio;
327 saved_on_io_close_complete = on_io_close_complete;
328 saved_on_io_close_complete_context = callback_context;
329 return 0;
330 }
331
332 static int my_xio_send(XIO_HANDLE xio, const void* buffer, size_t size, ON_SEND_COMPLETE on_send_complete, void* callback_context)
333 {
334 unsigned char* new_bytes = (unsigned char*)my_gballoc_realloc(io_send_bytes, io_send_byte_count + size);
335 (void)callback_context;
336 (void)on_send_complete;
337 (void)xio;
338 if (new_bytes != NULL)
339 {
340 io_send_bytes = new_bytes;
341 (void)memcpy(io_send_bytes + io_send_byte_count, buffer, size);
342 io_send_byte_count += size;
343 }
344 return 0;
345 }
346
347 MOCK_FUNCTION_WITH_CODE(, void, test_on_bytes_received, void*, context, const unsigned char*, buffer, size_t, size)
348 MOCK_FUNCTION_END()
349 MOCK_FUNCTION_WITH_CODE(, void, test_on_io_open_complete, void*, context, IO_OPEN_RESULT, io_open_result)
350 MOCK_FUNCTION_END()
351 MOCK_FUNCTION_WITH_CODE(, void, test_on_io_error, void*, context)
352 MOCK_FUNCTION_END()
353 MOCK_FUNCTION_WITH_CODE(, void, test_on_send_complete, void*, context, IO_SEND_RESULT, send_result)
354 MOCK_FUNCTION_END()
355 MOCK_FUNCTION_WITH_CODE(, void, test_on_io_close_complete, void*, context)
356 MOCK_FUNCTION_END()
357
358 static TEST_MUTEX_HANDLE g_testByTest;
359
360 MU_DEFINE_ENUM_STRINGS(UMOCK_C_ERROR_CODE, UMOCK_C_ERROR_CODE_VALUES)
361 TEST_DEFINE_ENUM_TYPE(IO_OPEN_RESULT, IO_OPEN_RESULT_VALUES);
362 IMPLEMENT_UMOCK_C_ENUM_TYPE(IO_OPEN_RESULT, IO_OPEN_RESULT_VALUES);
363 TEST_DEFINE_ENUM_TYPE(OPTIONHANDLER_RESULT, OPTIONHANDLER_RESULT_VALUES);
364 IMPLEMENT_UMOCK_C_ENUM_TYPE(OPTIONHANDLER_RESULT, OPTIONHANDLER_RESULT_VALUES);
365
366 static void on_umock_c_error(UMOCK_C_ERROR_CODE error_code)
367 {
368 ASSERT_FAIL("umock_c reported error :%" PRI_MU_ENUM "", MU_ENUM_VALUE(UMOCK_C_ERROR_CODE, error_code));
369 }
370
371 static int umocktypes_copy_amqp_binary(amqp_binary* destination, const amqp_binary* source)
372 {
373 int result;
374
375 if (source->length > 0)
376 {
377 destination->bytes = (unsigned char*)my_gballoc_malloc(source->length);
378 if (destination->bytes == NULL)
379 {
380 result = MU_FAILURE;
381 }
382 else
383 {
384 if (source->length > 0)
385 {
386 (void)memcpy((void*)destination->bytes, source->bytes, source->length);
387 }
388
389 result = 0;
390 }
391 }
392 else
393 {
394 result = 0;
395 }
396
397 if (result == 0)
398 {
399 destination->length = source->length;
400 }
401
402 return result;
403 }
404
405 static void umocktypes_free_amqp_binary(amqp_binary* value)
406 {
407 if (value->bytes != NULL)
408 {
409 my_gballoc_free((void*)value->bytes);
410 }
411 }
412
413 static char* umocktypes_stringify_amqp_binary(const amqp_binary* value)
414 {
415 char* result;
416
417 result = (char*)my_gballoc_malloc(3 + (5 * value->length));
418 if (result != NULL)
419 {
420 size_t pos = 0;
421 size_t i;
422
423 result[pos++] = '[';
424 for (i = 0; i < value->length; i++)
425 {
426 (void)sprintf(&result[pos], "0x%02X ", ((const unsigned char*)value->bytes)[i]);
427 pos += 5;
428 }
429 result[pos++] = ']';
430 result[pos++] = '\0';
431 }
432
433 return result;
434 }
435
436 static int umocktypes_are_equal_amqp_binary(amqp_binary* left, amqp_binary* right)
437 {
438 int result;
439
440 if (left->length != right->length)
441 {
442 result = 0;
443 }
444 else
445 {
446 if (left->length == 0)
447 {
448 result = 1;
449 }
450 else
451 {
452 result = (memcmp(left->bytes, right->bytes, left->length) == 0) ? 1 : 0;
453 }
454 }
455
456 return result;
457 }
458
459 static int umocktypes_copy_bool_ptr(bool** destination, const bool** source)
460 {
461 int result;
462
463 *destination = (bool*)my_gballoc_malloc(sizeof(bool));
464 if (*destination == NULL)
465 {
466 result = MU_FAILURE;
467 }
468 else
469 {
470 *(*destination) = *(*source);
471
472 result = 0;
473 }
474
475 return result;
476 }
477
478 static void umocktypes_free_bool_ptr(bool** value)
479 {
480 if (*value != NULL)
481 {
482 my_gballoc_free(*value);
483 }
484 }
485
486 static char* umocktypes_stringify_bool_ptr(const bool** value)
487 {
488 char* result;
489
490 result = (char*)my_gballoc_malloc(8);
491 if (result != NULL)
492 {
493 if (*value == NULL)
494 {
495 (void)strcpy(result, "{NULL}");
496 }
497 else if (*(*value) == true)
498 {
499 (void)strcpy(result, "{true}");
500 }
501 else
502 {
503 (void)strcpy(result, "{false}");
504 }
505 }
506
507 return result;
508 }
509
510 static int umocktypes_are_equal_bool_ptr(bool** left, bool** right)
511 {
512 int result;
513
514 if (*left == *right)
515 {
516 result = 1;
517 }
518 else
519 {
520 if (*(*left) == *(*right))
521 {
522 result = 1;
523 }
524 else
525 {
526 result = 0;
527 }
528 }
529
530 return result;
531 }
532
533 static void setup_successful_sasl_handshake(void)
534 {
535 sasl_code sasl_outcome_code = sasl_code_ok;
536 uint32_t mechanism_count = 1;
537 SASL_MECHANISM_BYTES init_bytes;
538
539 init_bytes.length = 0;
540 init_bytes.bytes = NULL;
541
542 saved_on_io_open_complete(saved_on_io_open_complete_context, IO_OPEN_OK);
543 saved_on_bytes_received(saved_on_bytes_received_context, sasl_header, sizeof(sasl_header));
544 saved_on_bytes_received(saved_on_bytes_received_context, test_sasl_mechanisms_frame, sizeof(test_sasl_mechanisms_frame));
545 umock_c_reset_all_calls();
546 STRICT_EXPECTED_CALL(amqpvalue_get_inplace_descriptor(test_sasl_value));
547 STRICT_EXPECTED_CALL(is_sasl_mechanisms_type_by_descriptor(test_descriptor_value))
548 .SetReturn(true);
549 STRICT_EXPECTED_CALL(amqpvalue_get_sasl_mechanisms(test_sasl_value, IGNORED_PTR_ARG));
550 STRICT_EXPECTED_CALL(sasl_mechanisms_get_sasl_server_mechanisms(IGNORED_PTR_ARG, IGNORED_PTR_ARG));
551 STRICT_EXPECTED_CALL(amqpvalue_get_array_item_count(IGNORED_PTR_ARG, IGNORED_PTR_ARG))
552 .CopyOutArgumentBuffer_count(&mechanism_count, sizeof(mechanism_count));
553 STRICT_EXPECTED_CALL(saslmechanism_get_mechanism_name(IGNORED_PTR_ARG));
554 STRICT_EXPECTED_CALL(amqpvalue_get_symbol(IGNORED_PTR_ARG, IGNORED_PTR_ARG))
555 .CopyOutArgumentBuffer(2, &test_mechanism, sizeof(test_mechanism));
556 STRICT_EXPECTED_CALL(amqpvalue_destroy(IGNORED_PTR_ARG));
557 STRICT_EXPECTED_CALL(sasl_init_create(IGNORED_PTR_ARG));
558 STRICT_EXPECTED_CALL(saslmechanism_get_init_bytes(IGNORED_PTR_ARG, IGNORED_PTR_ARG))
559 .CopyOutArgumentBuffer(2, &init_bytes, sizeof(init_bytes));
560 saved_on_sasl_frame_received(saved_sasl_frame_codec_callback_context, test_sasl_value);
561 saved_on_bytes_received(saved_on_bytes_received_context, test_sasl_outcome, sizeof(test_sasl_outcome));
562 umock_c_reset_all_calls();
563 STRICT_EXPECTED_CALL(amqpvalue_get_inplace_descriptor(test_sasl_value));
564 STRICT_EXPECTED_CALL(is_sasl_mechanisms_type_by_descriptor(test_descriptor_value))
565 .SetReturn(false);
566 STRICT_EXPECTED_CALL(is_sasl_challenge_type_by_descriptor(test_descriptor_value))
567 .SetReturn(false);
568 STRICT_EXPECTED_CALL(is_sasl_outcome_type_by_descriptor(test_descriptor_value))
569 .SetReturn(true);
570 STRICT_EXPECTED_CALL(amqpvalue_get_sasl_outcome(test_sasl_value, IGNORED_PTR_ARG))
571 .CopyOutArgumentBuffer(2, &test_sasl_outcome_handle, sizeof(test_sasl_outcome_handle));
572 STRICT_EXPECTED_CALL(sasl_outcome_get_code(test_sasl_outcome_handle, IGNORED_PTR_ARG))
573 .CopyOutArgumentBuffer(2, &sasl_outcome_code, sizeof(sasl_outcome_code));
574 saved_on_sasl_frame_received(saved_sasl_frame_codec_callback_context, test_sasl_value);
575 }
576
577 static void setup_send_init(void)
578 {
579 uint32_t mechanisms_count = 1;
580 SASL_MECHANISM_BYTES init_bytes;
581 unsigned char test_init_bytes[] = { 0x42 };
582
583 init_bytes.bytes = test_init_bytes;
584 init_bytes.length = sizeof(test_init_bytes);
585
586 STRICT_EXPECTED_CALL(amqpvalue_get_inplace_descriptor(test_sasl_value));
587 STRICT_EXPECTED_CALL(is_sasl_mechanisms_type_by_descriptor(test_descriptor_value))
588 .SetReturn(true);
589 STRICT_EXPECTED_CALL(amqpvalue_get_sasl_mechanisms(test_sasl_value, IGNORED_PTR_ARG));
590 STRICT_EXPECTED_CALL(sasl_mechanisms_get_sasl_server_mechanisms(IGNORED_PTR_ARG, IGNORED_PTR_ARG));
591 STRICT_EXPECTED_CALL(amqpvalue_get_array_item_count(IGNORED_PTR_ARG, IGNORED_PTR_ARG))
592 .CopyOutArgumentBuffer_count(&mechanisms_count, sizeof(mechanisms_count));
593 STRICT_EXPECTED_CALL(saslmechanism_get_mechanism_name(IGNORED_PTR_ARG));
594 STRICT_EXPECTED_CALL(amqpvalue_get_symbol(IGNORED_PTR_ARG, IGNORED_PTR_ARG))
595 .CopyOutArgumentBuffer(2, &test_mechanism, sizeof(test_mechanism));
596 STRICT_EXPECTED_CALL(amqpvalue_destroy(IGNORED_PTR_ARG));
597 STRICT_EXPECTED_CALL(sasl_init_create(IGNORED_PTR_ARG));
598 STRICT_EXPECTED_CALL(saslmechanism_get_init_bytes(IGNORED_PTR_ARG, IGNORED_PTR_ARG))
599 .CopyOutArgumentBuffer(2, &init_bytes, sizeof(init_bytes));
600 STRICT_EXPECTED_CALL(amqpvalue_create_sasl_init(test_sasl_init));
601 STRICT_EXPECTED_CALL(sasl_frame_codec_encode_frame(test_sasl_frame_codec, test_sasl_init_value, IGNORED_PTR_ARG, IGNORED_PTR_ARG));
602 STRICT_EXPECTED_CALL(sasl_init_destroy(test_sasl_init));
603 STRICT_EXPECTED_CALL(amqpvalue_destroy(test_sasl_init_value));
604 STRICT_EXPECTED_CALL(sasl_mechanisms_destroy(test_sasl_mechanisms_handle));
605 }
606
607 BEGIN_TEST_SUITE(saslclientio_ut)
608
609 TEST_SUITE_INITIALIZE(suite_init)
610 {
611 int result;
612
613 g_testByTest = TEST_MUTEX_CREATE();
614 ASSERT_IS_NOT_NULL(g_testByTest);
615
616 umock_c_init(on_umock_c_error);
617
618 result = umocktypes_stdint_register_types();
619 ASSERT_ARE_EQUAL(int, 0, result, "Failed registering stdint types");
620
621 result = umocktypes_charptr_register_types();
622 ASSERT_ARE_EQUAL(int, 0, result, "Failed registering charptr types");
623
624 result = umocktypes_bool_register_types();
625 ASSERT_ARE_EQUAL(int, 0, result, "Failed registering bool types");
626
627 REGISTER_GLOBAL_MOCK_HOOK(gballoc_malloc, my_gballoc_malloc);
628 REGISTER_GLOBAL_MOCK_HOOK(gballoc_calloc, my_gballoc_calloc);
629 REGISTER_GLOBAL_MOCK_HOOK(gballoc_free, my_gballoc_free);
630 REGISTER_GLOBAL_MOCK_HOOK(frame_codec_create, my_frame_codec_create);
631 REGISTER_GLOBAL_MOCK_HOOK(frame_codec_receive_bytes, my_frame_codec_receive_bytes);
632 REGISTER_GLOBAL_MOCK_HOOK(frame_codec_subscribe, my_frame_codec_subscribe);
633 REGISTER_GLOBAL_MOCK_RETURN(frame_codec_unsubscribe, 0);
634 REGISTER_GLOBAL_MOCK_RETURN(frame_codec_encode_frame, 0);
635 REGISTER_GLOBAL_MOCK_RETURN(amqpvalue_create_sasl_response, test_sasl_response_amqp_value);
636 REGISTER_GLOBAL_MOCK_HOOK(sasl_frame_codec_create, my_sasl_frame_codec_create);
637 REGISTER_GLOBAL_MOCK_HOOK(sasl_frame_codec_encode_frame, my_sasl_frame_codec_encode_frame);
638 REGISTER_GLOBAL_MOCK_HOOK(xio_open, my_xio_open);
639 REGISTER_GLOBAL_MOCK_HOOK(xio_send, my_xio_send);
640 REGISTER_GLOBAL_MOCK_HOOK(xio_close, my_xio_close);
641 REGISTER_GLOBAL_MOCK_RETURN(xio_setoption, 0);
642 REGISTER_GLOBAL_MOCK_RETURN(saslmechanism_get_init_bytes, 0);
643 REGISTER_GLOBAL_MOCK_RETURN(saslmechanism_get_mechanism_name, test_mechanism);
644 REGISTER_GLOBAL_MOCK_RETURN(saslmechanism_challenge, 0);
645 REGISTER_GLOBAL_MOCK_RETURN(amqpvalue_to_string, NULL);
646 REGISTER_GLOBAL_MOCK_RETURN(amqpvalue_get_inplace_descriptor, test_descriptor_value);
647 REGISTER_GLOBAL_MOCK_RETURN(amqpvalue_get_array_item_count, 0);
648 REGISTER_GLOBAL_MOCK_RETURN(amqpvalue_get_array_item, test_sasl_server_mechanism);
649 REGISTER_GLOBAL_MOCK_RETURN(amqpvalue_get_symbol, 0);
650 REGISTER_GLOBAL_MOCK_RETURN(sasl_init_create, test_sasl_init);
651 REGISTER_GLOBAL_MOCK_RETURN(amqpvalue_create_sasl_init, test_sasl_init_value);
652 REGISTER_GLOBAL_MOCK_RETURN(sasl_response_create, test_sasl_response_handle);
653
654 REGISTER_GLOBAL_MOCK_RETURN(OptionHandler_Create, test_optionhandler_handle);
655
656 REGISTER_TYPE(SASL_MECHANISM_BYTES*, SASL_MECHANISM_BYTES_ptr);
657
658 REGISTER_UMOCK_ALIAS_TYPE(const SASL_MECHANISM_BYTES*, SASL_MECHANISM_BYTES*);
659 REGISTER_UMOCK_ALIAS_TYPE(ON_FRAME_CODEC_ERROR, void*);
660 REGISTER_UMOCK_ALIAS_TYPE(FRAME_CODEC_HANDLE, void*);
661 REGISTER_UMOCK_ALIAS_TYPE(ON_SASL_FRAME_RECEIVED, void*);
662 REGISTER_UMOCK_ALIAS_TYPE(ON_SASL_FRAME_CODEC_ERROR, void*);
663 REGISTER_UMOCK_ALIAS_TYPE(ON_IO_OPEN_COMPLETE, void*);
664 REGISTER_UMOCK_ALIAS_TYPE(ON_BYTES_RECEIVED, void*);
665 REGISTER_UMOCK_ALIAS_TYPE(ON_IO_ERROR, void*);
666 REGISTER_UMOCK_ALIAS_TYPE(ON_IO_CLOSE_COMPLETE, void*);
667 REGISTER_UMOCK_ALIAS_TYPE(ON_SEND_COMPLETE, void*);
668 REGISTER_UMOCK_ALIAS_TYPE(SASL_FRAME_CODEC_HANDLE, void*);
669 REGISTER_UMOCK_ALIAS_TYPE(XIO_HANDLE, void*);
670 REGISTER_UMOCK_ALIAS_TYPE(AMQP_VALUE, void*);
671 REGISTER_UMOCK_ALIAS_TYPE(SASL_MECHANISMS_HANDLE, void*);
672 REGISTER_UMOCK_ALIAS_TYPE(SASL_MECHANISM_HANDLE, void*);
673 REGISTER_UMOCK_ALIAS_TYPE(SASL_OUTCOME_HANDLE, void*);
674 REGISTER_UMOCK_ALIAS_TYPE(SASL_INIT_HANDLE, void*);
675 REGISTER_UMOCK_ALIAS_TYPE(const AMQP_VALUE, void*);
676 REGISTER_UMOCK_ALIAS_TYPE(ON_BYTES_ENCODED, void*);
677 REGISTER_UMOCK_ALIAS_TYPE(pfCloneOption, void*);
678 REGISTER_UMOCK_ALIAS_TYPE(pfDestroyOption, void*);
679 REGISTER_UMOCK_ALIAS_TYPE(pfSetOption, void*);
680 REGISTER_UMOCK_ALIAS_TYPE(OPTIONHANDLER_HANDLE, void*);
681 REGISTER_UMOCK_ALIAS_TYPE(SASL_CHALLENGE_HANDLE, void*);
682 REGISTER_UMOCK_ALIAS_TYPE(SASL_RESPONSE_HANDLE, void*);
683
684 REGISTER_TYPE(IO_OPEN_RESULT, IO_OPEN_RESULT);
685 REGISTER_TYPE(OPTIONHANDLER_RESULT, OPTIONHANDLER_RESULT);
686 REGISTER_TYPE(amqp_binary, amqp_binary);
687 REGISTER_TYPE(bool*, bool_ptr);
688 }
689
690 TEST_SUITE_CLEANUP(suite_cleanup)
691 {
692 umock_c_deinit();
693
694 TEST_MUTEX_DESTROY(g_testByTest);
695 }
696
697 TEST_FUNCTION_INITIALIZE(method_init)
698 {
699 if (TEST_MUTEX_ACQUIRE(g_testByTest))
700 {
701 ASSERT_FAIL("our mutex is ABANDONED. Failure in test framework");
702 }
703
704 umock_c_reset_all_calls();
705 }
706
707 TEST_FUNCTION_CLEANUP(method_cleanup)
708 {
709 if (frame_codec_received_bytes != NULL)
710 {
711 free(frame_codec_received_bytes);
712 frame_codec_received_bytes = NULL;
713 }
714 frame_codec_received_byte_count = 0;
715
716 if (io_send_bytes != NULL)
717 {
718 free(io_send_bytes);
719 io_send_bytes = NULL;
720 }
721 io_send_byte_count = 0;
722
723 TEST_MUTEX_RELEASE(g_testByTest);
724 }
725
726 /* saslclientio_create */
727
728 /* Tests_SRS_SASLCLIENTIO_01_004: [`saslclientio_create` shall return on success a non-NULL handle to a new SASL client IO instance.] */
729 /* Tests_SRS_SASLCLIENTIO_01_089: [`saslclientio_create` shall create a frame codec to be used for encoding/decoding frames by calling `frame_codec_create` and passing `on_frame_codec_error` and a context as arguments.] */
730 /* Tests_SRS_SASLCLIENTIO_01_084: [`saslclientio_create` shall create a SASL frame codec to be used for SASL frame encoding/decoding by calling `sasl_frame_codec_create` and passing the just created frame codec as argument.] */
731 TEST_FUNCTION(saslclientio_create_with_valid_args_succeeds)
732 {
733 // arrange
734 CONCRETE_IO_HANDLE result;
735 SASLCLIENTIO_CONFIG sasl_client_io_config;
736 sasl_client_io_config.underlying_io = test_underlying_io;
737 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
738
739 STRICT_EXPECTED_CALL(gballoc_calloc(IGNORED_NUM_ARG, IGNORED_NUM_ARG));
740 STRICT_EXPECTED_CALL(frame_codec_create(IGNORED_PTR_ARG, IGNORED_PTR_ARG));
741 STRICT_EXPECTED_CALL(sasl_frame_codec_create(test_frame_codec, IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG));
742
743 // act
744 result = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
745
746 // assert
747 ASSERT_IS_NOT_NULL(result);
748 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
749
750 // cleanup
751 saslclientio_get_interface_description()->concrete_io_destroy(result);
752 }
753
754 /* Tests_SRS_SASLCLIENTIO_01_005: [If `io_create_parameters` is NULL, `saslclientio_create` shall fail and return NULL.] */
755 TEST_FUNCTION(saslclientio_create_with_NULL_config_fails)
756 {
757 // arrange
758
759 // act
760 CONCRETE_IO_HANDLE result = saslclientio_get_interface_description()->concrete_io_create(NULL);
761
762 // assert
763 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
764 ASSERT_IS_NULL(result);
765 }
766
767 /* Tests_SRS_SASLCLIENTIO_01_006: [If memory cannot be allocated for the new instance, `saslclientio_create` shall fail and return NULL.] */
768 TEST_FUNCTION(when_allocating_memory_for_the_new_instance_fails_then_saslclientio_create_fails)
769 {
770 // arrange
771 CONCRETE_IO_HANDLE result;
772 SASLCLIENTIO_CONFIG sasl_client_io_config;
773 sasl_client_io_config.underlying_io = test_underlying_io;
774 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
775
776 STRICT_EXPECTED_CALL(gballoc_calloc(IGNORED_NUM_ARG, IGNORED_NUM_ARG))
777 .SetReturn(NULL);
778
779 // act
780 result = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
781
782 // assert
783 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
784 ASSERT_IS_NULL(result);
785 }
786
787 /* Tests_SRS_SASLCLIENTIO_01_090: [If frame_codec_create fails, then saslclientio_create shall fail and return NULL.] */
788 TEST_FUNCTION(when_creating_the_frame_codec_fails_then_saslclientio_create_fails)
789 {
790 // arrange
791 CONCRETE_IO_HANDLE result;
792 SASLCLIENTIO_CONFIG sasl_client_io_config;
793 sasl_client_io_config.underlying_io = test_underlying_io;
794 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
795
796 STRICT_EXPECTED_CALL(gballoc_calloc(IGNORED_NUM_ARG, IGNORED_NUM_ARG));
797 STRICT_EXPECTED_CALL(frame_codec_create(IGNORED_PTR_ARG, IGNORED_PTR_ARG))
798 .SetReturn(NULL);
799 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG));
800
801 // act
802 result = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
803
804 // assert
805 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
806 ASSERT_IS_NULL(result);
807 }
808
809 /* Tests_SRS_SASLCLIENTIO_01_085: [If `sasl_frame_codec_create` fails, then `saslclientio_create` shall fail and return NULL.] */
810 TEST_FUNCTION(when_creating_the_sasl_frame_codec_fails_then_saslclientio_create_fails)
811 {
812 // arrange
813 CONCRETE_IO_HANDLE result;
814 SASLCLIENTIO_CONFIG sasl_client_io_config;
815 sasl_client_io_config.underlying_io = test_underlying_io;
816 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
817
818 STRICT_EXPECTED_CALL(gballoc_calloc(IGNORED_NUM_ARG, IGNORED_NUM_ARG));
819 STRICT_EXPECTED_CALL(frame_codec_create(IGNORED_PTR_ARG, IGNORED_PTR_ARG));
820 STRICT_EXPECTED_CALL(sasl_frame_codec_create(test_frame_codec, IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
821 .SetReturn(NULL);
822 STRICT_EXPECTED_CALL(frame_codec_destroy(test_frame_codec));
823 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG));
824
825 // act
826 result = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
827
828 // assert
829 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
830 ASSERT_IS_NULL(result);
831 }
832
833 /* Tests_SRS_SASLCLIENTIO_01_092: [If any of the `sasl_mechanism` or `underlying_io` members of the configuration structure are NULL, `saslclientio_create` shall fail and return NULL.] */
834 TEST_FUNCTION(saslclientio_create_with_a_NULL_underlying_io_fails)
835 {
836 // arrange
837 CONCRETE_IO_HANDLE result;
838 SASLCLIENTIO_CONFIG sasl_client_io_config;
839 sasl_client_io_config.underlying_io = NULL;
840 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
841
842 // act
843 result = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
844
845 // assert
846 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
847 ASSERT_IS_NULL(result);
848 }
849
850 /* Tests_SRS_SASLCLIENTIO_01_092: [If any of the `sasl_mechanism` or `underlying_io` members of the configuration structure are NULL, `saslclientio_create` shall fail and return NULL.] */
851 TEST_FUNCTION(saslclientio_create_with_a_NULL_sasl_mechanism_fails)
852 {
853 // arrange
854 CONCRETE_IO_HANDLE result;
855 SASLCLIENTIO_CONFIG sasl_client_io_config;
856 sasl_client_io_config.underlying_io = test_underlying_io;
857 sasl_client_io_config.sasl_mechanism = NULL;
858
859 // act
860 result = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
861
862 // assert
863 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
864 ASSERT_IS_NULL(result);
865 }
866
867 /* saslclientio_destroy */
868
869 /* Tests_SRS_SASLCLIENTIO_01_007: [`saslclientio_destroy` shall free all resources associated with the SASL client IO handle.] */
870 /* Tests_SRS_SASLCLIENTIO_01_086: [`saslclientio_destroy` shall destroy the SASL frame codec created in `saslclientio_create` by calling `sasl_frame_codec_destroy`.] */
871 /* Tests_SRS_SASLCLIENTIO_01_091: [`saslclientio_destroy` shall destroy the frame codec created in `saslclientio_create` by calling `frame_codec_destroy`.] */
872 TEST_FUNCTION(saslclientio_destroy_frees_the_resources_allocated_in_create)
873 {
874 // arrange
875 CONCRETE_IO_HANDLE sasl_client_io;
876 SASLCLIENTIO_CONFIG sasl_client_io_config;
877 sasl_client_io_config.underlying_io = test_underlying_io;
878 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
879 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
880 umock_c_reset_all_calls();
881
882 STRICT_EXPECTED_CALL(sasl_frame_codec_destroy(test_sasl_frame_codec));
883 STRICT_EXPECTED_CALL(frame_codec_destroy(test_frame_codec));
884 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG));
885
886 // act
887 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
888
889 // assert
890 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
891 }
892
893 /* Tests_SRS_SASLCLIENTIO_01_008: [If the argument `sasl_client_io` is NULL, `saslclientio_destroy` shall do nothing.] */
894 TEST_FUNCTION(saslclientio_destroy_with_NULL_argument_does_nothing)
895 {
896 // arrange
897
898 // act
899 saslclientio_get_interface_description()->concrete_io_destroy(NULL);
900
901 // assert
902 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
903 }
904
905 /* saslclientio_open_async */
906
907 /* Tests_SRS_SASLCLIENTIO_01_009: [`saslclientio_open_async` shall call `xio_open` on the `underlying_io` passed to `saslclientio_create`.] */
908 /* Tests_SRS_SASLCLIENTIO_01_010: [On success, `saslclientio_open_async` shall return 0.]*/
909 /* Tests_SRS_SASLCLIENTIO_01_013: [`saslclientio_open_async` shall pass to `xio_open` the `on_underlying_io_open_complete` as `on_io_open_complete` argument, `on_underlying_io_bytes_received` as `on_bytes_received` argument and `on_underlying_io_error` as `on_io_error` argument.] */
910 TEST_FUNCTION(saslclientio_open_async_with_valid_args_succeeds)
911 {
912 // arrange
913 SASLCLIENTIO_CONFIG sasl_client_io_config;
914 CONCRETE_IO_HANDLE sasl_client_io;
915 int result;
916 sasl_client_io_config.underlying_io = test_underlying_io;
917 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
918 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
919 umock_c_reset_all_calls();
920
921 STRICT_EXPECTED_CALL(xio_open(test_underlying_io, IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG));
922
923 // act
924 result = saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
925
926 // assert
927 ASSERT_ARE_EQUAL(int, 0, result);
928 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
929
930 // cleanup
931 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
932 }
933
934 /* Tests_SRS_SASLCLIENTIO_01_011: [If any of the `sasl_client_io`, `on_io_open_complete`, `on_bytes_received` or `on_io_error` arguments is NULL, `saslclientio_open_async` shall fail and return a non-zero value.] */
935 TEST_FUNCTION(saslclientio_open_async_with_NULL_sasl_io_handle_fails)
936 {
937 // arrange
938
939 // act
940 int result = saslclientio_get_interface_description()->concrete_io_open(NULL, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
941
942 // assert
943 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
944 ASSERT_ARE_NOT_EQUAL(int, 0, result);
945 }
946
947 /* Tests_SRS_SASLCLIENTIO_01_011: [If any of the `sasl_client_io`, `on_io_open_complete`, `on_bytes_received` or `on_io_error` arguments is NULL, `saslclientio_open_async` shall fail and return a non-zero value.] */
948 TEST_FUNCTION(saslclientio_open_async_with_NULL_on_io_open_complete_fails)
949 {
950 // arrange
951 SASLCLIENTIO_CONFIG sasl_client_io_config;
952 CONCRETE_IO_HANDLE sasl_client_io;
953 int result;
954 sasl_client_io_config.underlying_io = test_underlying_io;
955 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
956 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
957 umock_c_reset_all_calls();
958
959 // act
960 result = saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, NULL, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
961
962 // assert
963 ASSERT_ARE_NOT_EQUAL(int, 0, result);
964 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
965
966 // cleanup
967 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
968 }
969
970 /* Tests_SRS_SASLCLIENTIO_01_011: [If any of the `sasl_client_io`, `on_io_open_complete`, `on_bytes_received` or `on_io_error` arguments is NULL, `saslclientio_open_async` shall fail and return a non-zero value.] */
971 TEST_FUNCTION(saslclientio_open_async_with_NULL_on_bytes_received_fails)
972 {
973 // arrange
974 SASLCLIENTIO_CONFIG sasl_client_io_config;
975 CONCRETE_IO_HANDLE sasl_client_io;
976 int result;
977 sasl_client_io_config.underlying_io = test_underlying_io;
978 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
979 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
980 umock_c_reset_all_calls();
981
982 // act
983 result = saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, NULL, (void*)0x4243, test_on_io_error, (void*)0x4244);
984
985 // assert
986 ASSERT_ARE_NOT_EQUAL(int, 0, result);
987 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
988
989 // cleanup
990 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
991 }
992
993 /* Tests_SRS_SASLCLIENTIO_01_011: [If any of the `sasl_client_io`, `on_io_open_complete`, `on_bytes_received` or `on_io_error` arguments is NULL, `saslclientio_open_async` shall fail and return a non-zero value.] */
994 TEST_FUNCTION(saslclientio_open_async_with_NULL_on_io_error_fails)
995 {
996 // arrange
997 SASLCLIENTIO_CONFIG sasl_client_io_config;
998 CONCRETE_IO_HANDLE sasl_client_io;
999 int result;
1000 sasl_client_io_config.underlying_io = test_underlying_io;
1001 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
1002 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
1003 umock_c_reset_all_calls();
1004
1005 // act
1006 result = saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, NULL, (void*)0x4244);
1007
1008 // assert
1009 ASSERT_ARE_NOT_EQUAL(int, 0, result);
1010 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1011
1012 // cleanup
1013 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
1014 }
1015
1016 /* Tests_SRS_SASLCLIENTIO_01_012: [If the open of the `underlying_io` fails, `saslclientio_open_async` shall fail and return non-zero value.] */
1017 TEST_FUNCTION(when_opening_the_underlying_io_fails_saslclientio_open_async_fails)
1018 {
1019 // arrange
1020 SASLCLIENTIO_CONFIG sasl_client_io_config;
1021 CONCRETE_IO_HANDLE sasl_client_io;
1022 int result;
1023 sasl_client_io_config.underlying_io = test_underlying_io;
1024 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
1025 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
1026 umock_c_reset_all_calls();
1027
1028 STRICT_EXPECTED_CALL(xio_open(test_underlying_io, IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
1029 .SetReturn(1);
1030
1031 // act
1032 result = saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
1033
1034 // assert
1035 ASSERT_ARE_NOT_EQUAL(int, 0, result);
1036 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1037
1038 // cleanup
1039 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
1040 }
1041
1042 /* saslclientio_close_async */
1043
1044 /* Tests_SRS_SASLCLIENTIO_01_015: [`saslclientio_close_async` shall close the underlying io handle passed in `saslclientio_create` by calling `xio_close`.] */
1045 /* Tests_SRS_SASLCLIENTIO_01_098: [`saslclientio_close_async` shall only perform the close if the state is OPEN, OPENING or ERROR.] */
1046 /* Tests_SRS_SASLCLIENTIO_01_016: [On success, `saslclientio_close_async` shall return 0.] */
1047 TEST_FUNCTION(saslclientio_close_async_when_the_io_state_is_OPENING_closes_the_underlying_io)
1048 {
1049 // arrange
1050 SASLCLIENTIO_CONFIG sasl_client_io_config;
1051 CONCRETE_IO_HANDLE sasl_client_io;
1052 int result;
1053 sasl_client_io_config.underlying_io = test_underlying_io;
1054 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
1055 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
1056 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
1057 umock_c_reset_all_calls();
1058
1059 STRICT_EXPECTED_CALL(xio_close(test_underlying_io, IGNORED_PTR_ARG, IGNORED_PTR_ARG));
1060
1061 // act
1062 result = saslclientio_get_interface_description()->concrete_io_close(sasl_client_io, test_on_io_close_complete, (void*)0x4245);
1063
1064 // assert
1065 ASSERT_ARE_EQUAL(int, 0, result);
1066 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1067
1068 // cleanup
1069 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
1070 }
1071
1072 /* Tests_SRS_SASLCLIENTIO_01_015: [`saslclientio_close_async` shall close the underlying io handle passed in `saslclientio_create` by calling `xio_close`.] */
1073 /* Tests_SRS_SASLCLIENTIO_01_098: [`saslclientio_close_async` shall only perform the close if the state is OPEN, OPENING or ERROR.] */
1074 /* Tests_SRS_SASLCLIENTIO_01_016: [On success, `saslclientio_close_async` shall return 0.] */
1075 TEST_FUNCTION(saslclientio_close_async_when_the_io_state_is_OPEN_closes_the_underlying_io)
1076 {
1077 // arrange
1078 SASLCLIENTIO_CONFIG sasl_client_io_config;
1079 CONCRETE_IO_HANDLE sasl_client_io;
1080 int result;
1081 sasl_client_io_config.underlying_io = test_underlying_io;
1082 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
1083 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
1084 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
1085 setup_successful_sasl_handshake();
1086 umock_c_reset_all_calls();
1087
1088 STRICT_EXPECTED_CALL(xio_close(test_underlying_io, IGNORED_PTR_ARG, IGNORED_PTR_ARG));
1089
1090 // act
1091 result = saslclientio_get_interface_description()->concrete_io_close(sasl_client_io, test_on_io_close_complete, (void*)0x4245);
1092
1093 // assert
1094 ASSERT_ARE_EQUAL(int, 0, result);
1095 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1096
1097 // cleanup
1098 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
1099 }
1100
1101 /* Tests_SRS_SASLCLIENTIO_01_015: [`saslclientio_close_async` shall close the underlying io handle passed in `saslclientio_create` by calling `xio_close`.] */
1102 /* Tests_SRS_SASLCLIENTIO_01_098: [`saslclientio_close_async` shall only perform the close if the state is OPEN, OPENING or ERROR.] */
1103 /* Tests_SRS_SASLCLIENTIO_01_016: [On success, `saslclientio_close_async` shall return 0.] */
1104 TEST_FUNCTION(saslclientio_close_async_when_the_io_state_is_ERROR_closes_the_underlying_io)
1105 {
1106 // arrange
1107 SASLCLIENTIO_CONFIG sasl_client_io_config;
1108 CONCRETE_IO_HANDLE sasl_client_io;
1109 int result;
1110 sasl_client_io_config.underlying_io = test_underlying_io;
1111 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
1112 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
1113 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
1114 setup_successful_sasl_handshake();
1115 saved_on_io_error(saved_on_io_error_context);
1116 umock_c_reset_all_calls();
1117
1118 STRICT_EXPECTED_CALL(xio_close(test_underlying_io, IGNORED_PTR_ARG, IGNORED_PTR_ARG));
1119
1120 // act
1121 result = saslclientio_get_interface_description()->concrete_io_close(sasl_client_io, test_on_io_close_complete, (void*)0x4245);
1122
1123 // assert
1124 ASSERT_ARE_EQUAL(int, 0, result);
1125 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1126
1127 // cleanup
1128 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
1129 }
1130
1131 /* Tests_SRS_SASLCLIENTIO_01_097: [If `saslclientio_close_async` is called when the IO is in the `IO_STATE_NOT_OPEN` state, `saslclientio_close_async` shall fail and return a non zero value.] */
1132 TEST_FUNCTION(saslclientio_close_async_when_the_io_state_is_NOT_OPEN_fails)
1133 {
1134 // arrange
1135 SASLCLIENTIO_CONFIG sasl_client_io_config;
1136 CONCRETE_IO_HANDLE sasl_client_io;
1137 int result;
1138 sasl_client_io_config.underlying_io = test_underlying_io;
1139 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
1140 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
1141 umock_c_reset_all_calls();
1142
1143 // act
1144 result = saslclientio_get_interface_description()->concrete_io_close(sasl_client_io, test_on_io_close_complete, (void*)0x4245);
1145
1146 // assert
1147 ASSERT_ARE_NOT_EQUAL(int, 0, result);
1148 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1149
1150 // cleanup
1151 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
1152 }
1153
1154 /* Tests_SRS_SASLCLIENTIO_01_097: [If `saslclientio_close_async` is called when the IO is in the `IO_STATE_NOT_OPEN` state, `saslclientio_close_async` shall fail and return a non zero value.] */
1155 TEST_FUNCTION(saslclientio_close_async_when_the_io_state_is_NOT_OPEN_due_to_a_previous_close_succeeds_without_calling_the_underlying_io)
1156 {
1157 // arrange
1158 SASLCLIENTIO_CONFIG sasl_client_io_config;
1159 CONCRETE_IO_HANDLE sasl_client_io;
1160 int result;
1161 sasl_client_io_config.underlying_io = test_underlying_io;
1162 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
1163 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
1164 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
1165 saved_on_io_open_complete(saved_on_io_open_complete_context, IO_OPEN_OK);
1166 (void)saslclientio_get_interface_description()->concrete_io_close(sasl_client_io, test_on_io_close_complete, (void*)0x4245);
1167 saved_on_io_close_complete(saved_on_io_close_complete_context);
1168 umock_c_reset_all_calls();
1169
1170 // act
1171 result = saslclientio_get_interface_description()->concrete_io_close(sasl_client_io, test_on_io_close_complete, (void*)0x4245);
1172
1173 // assert
1174 ASSERT_ARE_NOT_EQUAL(int, 0, result);
1175 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1176
1177 // cleanup
1178 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
1179 }
1180
1181 /* Tests_SRS_SASLCLIENTIO_01_017: [If `sasl_client_io` is NULL, `saslclientio_close_async` shall fail and return a non-zero value.] */
1182 TEST_FUNCTION(saslclientio_close_async_with_NULL_sasl_io_fails)
1183 {
1184 // arrange
1185
1186 // act
1187 int result = saslclientio_get_interface_description()->concrete_io_close(NULL, test_on_io_close_complete, (void*)0x4245);
1188
1189 // assert
1190 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1191 ASSERT_ARE_NOT_EQUAL(int, 0, result);
1192 }
1193
1194 /* Tests_SRS_SASLCLIENTIO_01_018: [If `xio_close` fails, then `saslclientio_close_async` shall return a non-zero value.] */
1195 TEST_FUNCTION(when_xio_close_fails_saslclientio_close_async_fails)
1196 {
1197 // arrange
1198 SASLCLIENTIO_CONFIG sasl_client_io_config;
1199 CONCRETE_IO_HANDLE sasl_client_io;
1200 int result;
1201 sasl_client_io_config.underlying_io = test_underlying_io;
1202 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
1203 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
1204 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
1205 saved_on_io_open_complete(saved_on_io_open_complete_context, IO_OPEN_OK);
1206 umock_c_reset_all_calls();
1207
1208 STRICT_EXPECTED_CALL(xio_close(test_underlying_io, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
1209 .SetReturn(1);
1210
1211 // act
1212 result = saslclientio_get_interface_description()->concrete_io_close(sasl_client_io, test_on_io_close_complete, (void*)0x4245);
1213
1214 // assert
1215 ASSERT_ARE_NOT_EQUAL(int, 0, result);
1216 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1217
1218 // cleanup
1219 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
1220 }
1221
1222 /* saslclientio_send_async */
1223
1224 /* Tests_SRS_SASLCLIENTIO_01_019: [If `saslclientio_send_async` is called while the SASL client IO state is not `IO_STATE_OPEN`, `saslclientio_send_async` shall fail and return a non-zero value.]*/
1225 TEST_FUNCTION(saslclientio_send_async_when_io_state_is_NOT_OPEN_fails)
1226 {
1227 // arrange
1228 SASLCLIENTIO_CONFIG sasl_client_io_config;
1229 CONCRETE_IO_HANDLE sasl_client_io;
1230 int result;
1231 unsigned char test_buffer[] = { 0x42 };
1232 sasl_client_io_config.underlying_io = test_underlying_io;
1233 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
1234 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
1235 umock_c_reset_all_calls();
1236
1237 // act
1238 result = saslclientio_get_interface_description()->concrete_io_send(sasl_client_io, test_buffer, sizeof(test_buffer), test_on_send_complete, (void*)0x4245);
1239
1240 // assert
1241 ASSERT_ARE_NOT_EQUAL(int, 0, result);
1242 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1243
1244 // cleanup
1245 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
1246 }
1247
1248 /* Tests_SRS_SASLCLIENTIO_01_019: [If `saslclientio_send_async` is called while the SASL client IO state is not `IO_STATE_OPEN`, `saslclientio_send_async` shall fail and return a non-zero value.]*/
1249 TEST_FUNCTION(saslclientio_send_async_when_io_state_is_OPENING_fails)
1250 {
1251 // arrange
1252 SASLCLIENTIO_CONFIG sasl_client_io_config;
1253 CONCRETE_IO_HANDLE sasl_client_io;
1254 int result;
1255 unsigned char test_buffer[] = { 0x42 };
1256 sasl_client_io_config.underlying_io = test_underlying_io;
1257 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
1258 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
1259 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
1260 umock_c_reset_all_calls();
1261
1262 // act
1263 result = saslclientio_get_interface_description()->concrete_io_send(sasl_client_io, test_buffer, sizeof(test_buffer), test_on_send_complete, (void*)0x4245);
1264
1265 // assert
1266 ASSERT_ARE_NOT_EQUAL(int, 0, result);
1267 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1268
1269 // cleanup
1270 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
1271 }
1272
1273 /* Tests_SRS_SASLCLIENTIO_01_019: [If `saslclientio_send_async` is called while the SASL client IO state is not `IO_STATE_OPEN`, `saslclientio_send_async` shall fail and return a non-zero value.]*/
1274 TEST_FUNCTION(saslclientio_send_async_when_io_state_is_ERROR_fails)
1275 {
1276 // arrange
1277 SASLCLIENTIO_CONFIG sasl_client_io_config;
1278 CONCRETE_IO_HANDLE sasl_client_io;
1279 int result;
1280 unsigned char test_buffer[] = { 0x42 };
1281 sasl_client_io_config.underlying_io = test_underlying_io;
1282 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
1283 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
1284 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
1285 setup_successful_sasl_handshake();
1286 saved_on_io_error(saved_on_io_error_context);
1287 umock_c_reset_all_calls();
1288
1289 // act
1290 result = saslclientio_get_interface_description()->concrete_io_send(sasl_client_io, test_buffer, sizeof(test_buffer), test_on_send_complete, (void*)0x4245);
1291
1292 // assert
1293 ASSERT_ARE_NOT_EQUAL(int, 0, result);
1294 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1295
1296 // cleanup
1297 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
1298 }
1299
1300 /* Tests_SRS_SASLCLIENTIO_01_020: [If the SASL client IO state is `IO_STATE_OPEN`, `saslclientio_send_async` shall call `xio_send` on the `underlying_io` passed to `saslclientio_create`, while passing as arguments the `buffer`,`size`, `on_send_complete` and `callback_context`.]*/
1301 /* Tests_SRS_SASLCLIENTIO_01_021: [On success, `saslclientio_send_async` shall return 0.]*/
1302 TEST_FUNCTION(saslclientio_send_async_when_io_state_is_OPEN_calls_the_underlying_io_send)
1303 {
1304 // arrange
1305 SASLCLIENTIO_CONFIG sasl_client_io_config;
1306 CONCRETE_IO_HANDLE sasl_client_io;
1307 int result;
1308 unsigned char test_buffer[] = { 0x42 };
1309 sasl_client_io_config.underlying_io = test_underlying_io;
1310 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
1311 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
1312 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
1313 setup_successful_sasl_handshake();
1314 umock_c_reset_all_calls();
1315
1316 STRICT_EXPECTED_CALL(xio_send(test_underlying_io, test_buffer, sizeof(test_buffer), test_on_send_complete, (void*)0x4245))
1317 .ValidateArgumentBuffer(2, test_buffer, sizeof(test_buffer));
1318
1319 // act
1320 result = saslclientio_get_interface_description()->concrete_io_send(sasl_client_io, test_buffer, sizeof(test_buffer), test_on_send_complete, (void*)0x4245);
1321
1322 // assert
1323 ASSERT_ARE_EQUAL(int, 0, result);
1324 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1325
1326 // cleanup
1327 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
1328 }
1329
1330 /* Tests_SRS_SASLCLIENTIO_01_020: [If the SASL client IO state is `IO_STATE_OPEN`, `saslclientio_send_async` shall call `xio_send` on the `underlying_io` passed to `saslclientio_create`, while passing as arguments the `buffer`,`size`, `on_send_complete` and `callback_context`.]*/
1331 /* Tests_SRS_SASLCLIENTIO_01_021: [On success, `saslclientio_send_async` shall return 0.]*/
1332 /* Tests_SRS_SASLCLIENTIO_01_127: [ `on_send_complete` shall be allowed to be NULL. ]*/
1333 TEST_FUNCTION(saslclientio_send_async_with_NULL_on_send_complete_passes_NULL_to_the_underlying_io)
1334 {
1335 // arrange
1336 SASLCLIENTIO_CONFIG sasl_client_io_config;
1337 CONCRETE_IO_HANDLE sasl_client_io;
1338 int result;
1339 unsigned char test_buffer[] = { 0x42 };
1340 sasl_client_io_config.underlying_io = test_underlying_io;
1341 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
1342 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
1343 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
1344 setup_successful_sasl_handshake();
1345 umock_c_reset_all_calls();
1346
1347 STRICT_EXPECTED_CALL(xio_send(test_underlying_io, test_buffer, sizeof(test_buffer), NULL, (void*)0x4245))
1348 .ValidateArgumentBuffer(2, test_buffer, sizeof(test_buffer));
1349
1350 // act
1351 result = saslclientio_get_interface_description()->concrete_io_send(sasl_client_io, test_buffer, sizeof(test_buffer), NULL, (void*)0x4245);
1352
1353 // assert
1354 ASSERT_ARE_EQUAL(int, 0, result);
1355 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1356
1357 // cleanup
1358 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
1359 }
1360
1361 /* Tests_SRS_SASLCLIENTIO_01_020: [If the SASL client IO state is `IO_STATE_OPEN`, `saslclientio_send_async` shall call `xio_send` on the `underlying_io` passed to `saslclientio_create`, while passing as arguments the `buffer`,`size`, `on_send_complete` and `callback_context`.]*/
1362 /* Tests_SRS_SASLCLIENTIO_01_021: [On success, `saslclientio_send_async` shall return 0.]*/
1363 TEST_FUNCTION(saslclientio_send_async_with_NULL_on_send_complete_context_passes_NULL_to_the_underlying_io)
1364 {
1365 // arrange
1366 SASLCLIENTIO_CONFIG sasl_client_io_config;
1367 CONCRETE_IO_HANDLE sasl_client_io;
1368 int result;
1369 unsigned char test_buffer[] = { 0x42 };
1370 sasl_client_io_config.underlying_io = test_underlying_io;
1371 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
1372 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
1373 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
1374 setup_successful_sasl_handshake();
1375 umock_c_reset_all_calls();
1376
1377 STRICT_EXPECTED_CALL(xio_send(test_underlying_io, test_buffer, sizeof(test_buffer), test_on_send_complete, NULL))
1378 .ValidateArgumentBuffer(2, test_buffer, sizeof(test_buffer));
1379
1380 // act
1381 result = saslclientio_get_interface_description()->concrete_io_send(sasl_client_io, test_buffer, sizeof(test_buffer), test_on_send_complete, NULL);
1382
1383 // assert
1384 ASSERT_ARE_EQUAL(int, 0, result);
1385 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1386
1387 // cleanup
1388 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
1389 }
1390
1391 /* Tests_SRS_SASLCLIENTIO_01_022: [If the `sasl_client_io` or `buffer` argument is NULL, `saslclientio_send_async` shall fail and return a non-zero value.]*/
1392 TEST_FUNCTION(saslclientio_send_async_with_NULL_sasl_io_fails)
1393 {
1394 // arrange
1395 unsigned char test_buffer[] = { 0x42 };
1396
1397 // act
1398 int result = saslclientio_get_interface_description()->concrete_io_send(NULL, test_buffer, sizeof(test_buffer), test_on_send_complete, (void*)0x4245);
1399
1400 // assert
1401 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1402 ASSERT_ARE_NOT_EQUAL(int, 0, result);
1403 }
1404
1405 /* Tests_SRS_SASLCLIENTIO_01_022: [If the `sasl_client_io` or `buffer` argument is NULL, `saslclientio_send_async` shall fail and return a non-zero value.]*/
1406 TEST_FUNCTION(saslclientio_send_async_with_NULL_buffer_fails)
1407 {
1408 // arrange
1409 SASLCLIENTIO_CONFIG sasl_client_io_config;
1410 CONCRETE_IO_HANDLE sasl_client_io;
1411 int result;
1412 sasl_client_io_config.underlying_io = test_underlying_io;
1413 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
1414 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
1415 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
1416 setup_successful_sasl_handshake();
1417 umock_c_reset_all_calls();
1418
1419 // act
1420 result = saslclientio_get_interface_description()->concrete_io_send(sasl_client_io, NULL, 1, test_on_send_complete, (void*)0x4245);
1421
1422 // assert
1423 ASSERT_ARE_NOT_EQUAL(int, 0, result);
1424 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1425
1426 // cleanup
1427 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
1428 }
1429
1430 /* Tests_SRS_SASLCLIENTIO_01_023: [If `size` is 0, `saslclientio_send_async` shall fail and return a non-zero value.]*/
1431 TEST_FUNCTION(saslclientio_send_async_with_0_size_fails)
1432 {
1433 // arrange
1434 SASLCLIENTIO_CONFIG sasl_client_io_config;
1435 CONCRETE_IO_HANDLE sasl_client_io;
1436 int result;
1437 unsigned char test_buffer[] = { 0x42 };
1438 sasl_client_io_config.underlying_io = test_underlying_io;
1439 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
1440 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
1441 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
1442 setup_successful_sasl_handshake();
1443 umock_c_reset_all_calls();
1444
1445 // act
1446 result = saslclientio_get_interface_description()->concrete_io_send(sasl_client_io, test_buffer, 0, test_on_send_complete, (void*)0x4245);
1447
1448 // assert
1449 ASSERT_ARE_NOT_EQUAL(int, 0, result);
1450 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1451
1452 // cleanup
1453 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
1454 }
1455
1456 /* Tests_SRS_SASLCLIENTIO_01_024: [If the call to `xio_send` fails, then `saslclientio_send_async` shall fail and return a non-zero value.]*/
1457 TEST_FUNCTION(when_the_underlying_xio_send_fails_then_saslclientio_send_async_fails)
1458 {
1459 // arrange
1460 SASLCLIENTIO_CONFIG sasl_client_io_config;
1461 CONCRETE_IO_HANDLE sasl_client_io;
1462 int result;
1463 unsigned char test_buffer[] = { 0x42 };
1464 sasl_client_io_config.underlying_io = test_underlying_io;
1465 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
1466 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
1467 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
1468 setup_successful_sasl_handshake();
1469 umock_c_reset_all_calls();
1470
1471 STRICT_EXPECTED_CALL(xio_send(test_underlying_io, test_buffer, sizeof(test_buffer), test_on_send_complete, (void*)0x4245))
1472 .ValidateArgumentBuffer(2, test_buffer, sizeof(test_buffer))
1473 .SetReturn(1);
1474
1475 // act
1476 result = saslclientio_get_interface_description()->concrete_io_send(sasl_client_io, test_buffer, sizeof(test_buffer), test_on_send_complete, (void*)0x4245);
1477
1478 // assert
1479 ASSERT_ARE_NOT_EQUAL(int, 0, result);
1480 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1481
1482 // cleanup
1483 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
1484 }
1485
1486 /* saslclientio_dowork */
1487
1488 /* Tests_SRS_SASLCLIENTIO_01_025: [`saslclientio_dowork` shall call the `xio_dowork` on the `underlying_io` passed in `saslclientio_create`.]*/
1489 TEST_FUNCTION(when_the_io_state_is_OPEN_xio_dowork_calls_the_underlying_IO)
1490 {
1491 // arrange
1492 SASLCLIENTIO_CONFIG sasl_client_io_config;
1493 CONCRETE_IO_HANDLE sasl_client_io;
1494 sasl_client_io_config.underlying_io = test_underlying_io;
1495 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
1496 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
1497 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
1498 setup_successful_sasl_handshake();
1499 umock_c_reset_all_calls();
1500
1501 STRICT_EXPECTED_CALL(xio_dowork(test_underlying_io));
1502
1503 // act
1504 saslclientio_get_interface_description()->concrete_io_dowork(sasl_client_io);
1505
1506 // assert
1507 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1508
1509 // cleanup
1510 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
1511 }
1512
1513 /* Tests_SRS_SASLCLIENTIO_01_025: [`saslclientio_dowork` shall call the `xio_dowork` on the `underlying_io` passed in `saslclientio_create`.]*/
1514 TEST_FUNCTION(when_the_io_state_is_OPENING_xio_dowork_calls_the_underlying_IO)
1515 {
1516 // arrange
1517 SASLCLIENTIO_CONFIG sasl_client_io_config;
1518 CONCRETE_IO_HANDLE sasl_client_io;
1519 sasl_client_io_config.underlying_io = test_underlying_io;
1520 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
1521 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
1522 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
1523 umock_c_reset_all_calls();
1524
1525 STRICT_EXPECTED_CALL(xio_dowork(test_underlying_io));
1526
1527 // act
1528 saslclientio_get_interface_description()->concrete_io_dowork(sasl_client_io);
1529
1530 // assert
1531 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1532
1533 // cleanup
1534 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
1535 }
1536
1537 /* Tests_SRS_SASLCLIENTIO_01_025: [`saslclientio_dowork` shall call the `xio_dowork` on the `underlying_io` passed in `saslclientio_create`.]*/
1538 TEST_FUNCTION(when_the_io_state_is_NOT_OPEN_xio_dowork_does_nothing)
1539 {
1540 // arrange
1541 SASLCLIENTIO_CONFIG sasl_client_io_config;
1542 CONCRETE_IO_HANDLE sasl_client_io;
1543 sasl_client_io_config.underlying_io = test_underlying_io;
1544 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
1545 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
1546 umock_c_reset_all_calls();
1547
1548 // act
1549 saslclientio_get_interface_description()->concrete_io_dowork(sasl_client_io);
1550
1551 // assert
1552 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1553
1554 // cleanup
1555 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
1556 }
1557
1558 /* Tests_SRS_SASLCLIENTIO_01_099: [If the state of the IO is `IO_NOT_OPEN`, `saslclientio_dowork` shall make no calls to the underlying IO.]*/
1559 TEST_FUNCTION(when_the_io_state_is_ERROR_xio_dowork_calls_the_underlying_dowork)
1560 {
1561 // arrange
1562 SASLCLIENTIO_CONFIG sasl_client_io_config;
1563 CONCRETE_IO_HANDLE sasl_client_io;
1564 sasl_client_io_config.underlying_io = test_underlying_io;
1565 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
1566 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
1567 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
1568 setup_successful_sasl_handshake();
1569 saved_on_io_error(saved_on_io_error_context);
1570 umock_c_reset_all_calls();
1571
1572 STRICT_EXPECTED_CALL(xio_dowork(test_underlying_io));
1573
1574 // act
1575 saslclientio_get_interface_description()->concrete_io_dowork(sasl_client_io);
1576
1577 // assert
1578 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1579
1580 // cleanup
1581 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
1582 }
1583
1584 /* Tests_SRS_SASLCLIENTIO_01_026: [If the `sasl_client_io` argument is NULL, `saslclientio_dowork` shall do nothing.]*/
1585 TEST_FUNCTION(saslclientio_dowork_with_NULL_sasl_io_handle_does_nothing)
1586 {
1587 // arrange
1588
1589 // act
1590 saslclientio_get_interface_description()->concrete_io_dowork(NULL);
1591
1592 // assert
1593 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1594 }
1595
1596 /* saslclientio_get_interface_description */
1597
1598 /* Tests_SRS_SASLCLIENTIO_01_087: [`saslclientio_get_interface_description` shall return a pointer to an `IO_INTERFACE_DESCRIPTION` structure that contains pointers to the functions: `saslclientio_create`, `saslclientio_destroy`, `saslclientio_open_async`, `saslclientio_close_async`, `saslclientio_send_async`, `saslclientio_setoption`, `saslclientio_retrieveoptions` and `saslclientio_dowork`.]*/
1599 TEST_FUNCTION(saslclientio_get_interface_description_returns_the_saslclientio_interface_functions)
1600 {
1601 // arrange
1602
1603 // act
1604 const IO_INTERFACE_DESCRIPTION* result = saslclientio_get_interface_description();
1605
1606 // assert
1607 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1608 ASSERT_IS_NOT_NULL(result);
1609 ASSERT_IS_NOT_NULL(result->concrete_io_create);
1610 ASSERT_IS_NOT_NULL(result->concrete_io_destroy);
1611 ASSERT_IS_NOT_NULL(result->concrete_io_open);
1612 ASSERT_IS_NOT_NULL(result->concrete_io_close);
1613 ASSERT_IS_NOT_NULL(result->concrete_io_send);
1614 ASSERT_IS_NOT_NULL(result->concrete_io_dowork);
1615 ASSERT_IS_NOT_NULL(result->concrete_io_setoption);
1616 ASSERT_IS_NOT_NULL(result->concrete_io_retrieveoptions);
1617 }
1618
1619 /* saslclientio_setoption */
1620
1621 /* Tests_SRS_SASLCLIENTIO_03_001: [`saslclientio_setoption` shall forward all unhandled options to underlying io by calling `xio_setoption`.]*/
1622 /* Tests_SRS_SASLCLIENTIO_01_128: [ On success, `saslclientio_setoption` shall return 0. ]*/
1623 TEST_FUNCTION(saslclientio_setoption_calls_the_underlying_io)
1624 {
1625 // arrange
1626 SASLCLIENTIO_CONFIG sasl_client_io_config;
1627 CONCRETE_IO_HANDLE sasl_client_io;
1628 int result;
1629 sasl_client_io_config.underlying_io = test_underlying_io;
1630 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
1631 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
1632 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
1633 setup_successful_sasl_handshake();
1634 saved_on_io_error(saved_on_io_error_context);
1635 umock_c_reset_all_calls();
1636
1637 STRICT_EXPECTED_CALL(xio_setoption(test_underlying_io, "option1", (void*)0x4244));
1638
1639 // act
1640 result = saslclientio_get_interface_description()->concrete_io_setoption(sasl_client_io, "option1", (void*)0x4244);
1641
1642 // assert
1643 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1644 ASSERT_ARE_EQUAL(int, 0, result);
1645
1646 // cleanup
1647 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
1648 }
1649
1650 TEST_FUNCTION(when_xio_setopion_fails_saslclientio_setoption_also_fails)
1651 {
1652 // arrange
1653 SASLCLIENTIO_CONFIG sasl_client_io_config;
1654 CONCRETE_IO_HANDLE sasl_client_io;
1655 int result;
1656 sasl_client_io_config.underlying_io = test_underlying_io;
1657 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
1658 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
1659 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
1660 setup_successful_sasl_handshake();
1661 saved_on_io_error(saved_on_io_error_context);
1662 umock_c_reset_all_calls();
1663
1664 STRICT_EXPECTED_CALL(xio_setoption(test_underlying_io, "option1", (void*)0x4244))
1665 .SetReturn(1);
1666
1667 // act
1668 result = saslclientio_get_interface_description()->concrete_io_setoption(sasl_client_io, "option1", (void*)0x4244);
1669
1670 // assert
1671 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1672 ASSERT_ARE_NOT_EQUAL(int, 0, result);
1673
1674 // cleanup
1675 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
1676 }
1677
1678 /* Tests_SRS_SASLCLIENTIO_01_130: [ If `sasl_client_io` or `option_name` is NULL, `saslclientio_setoption` shall fail and return a non-zero value. ]*/
1679 TEST_FUNCTION(saslclientio_setoption_with_NULL_sasl_client_io_fails)
1680 {
1681 // arrange
1682 int result;
1683
1684 // act
1685 result = saslclientio_get_interface_description()->concrete_io_setoption(NULL, "option1", (void*)0x4244);
1686
1687 // assert
1688 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1689 ASSERT_ARE_NOT_EQUAL(int, 0, result);
1690 }
1691
1692 /* Tests_SRS_SASLCLIENTIO_01_130: [ If `sasl_client_io` or `option_name` is NULL, `saslclientio_setoption` shall fail and return a non-zero value. ]*/
1693 TEST_FUNCTION(saslclientio_setoption_with_NULL_option_name_fails)
1694 {
1695 // arrange
1696 SASLCLIENTIO_CONFIG sasl_client_io_config;
1697 CONCRETE_IO_HANDLE sasl_client_io;
1698 int result;
1699 sasl_client_io_config.underlying_io = test_underlying_io;
1700 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
1701 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
1702 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
1703 setup_successful_sasl_handshake();
1704 saved_on_io_error(saved_on_io_error_context);
1705 umock_c_reset_all_calls();
1706
1707 // act
1708 result = saslclientio_get_interface_description()->concrete_io_setoption(sasl_client_io, NULL, (void*)0x4244);
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 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
1716 }
1717
1718 /* Tests_SRS_SASLCLIENTIO_01_131: [ SASL client IO shall handle the following options: ]*/
1719 /* Tests_SRS_SASLCLIENTIO_01_132: [ - logtrace - bool. ]*/
1720 TEST_FUNCTION(saslclientio_setoption_with_logtrace_true_succeeds)
1721 {
1722 // arrange
1723 SASLCLIENTIO_CONFIG sasl_client_io_config;
1724 CONCRETE_IO_HANDLE sasl_client_io;
1725 int result;
1726 bool log_trace = true;
1727 sasl_client_io_config.underlying_io = test_underlying_io;
1728 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
1729 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
1730 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
1731 setup_successful_sasl_handshake();
1732 saved_on_io_error(saved_on_io_error_context);
1733 umock_c_reset_all_calls();
1734
1735 // act
1736 result = saslclientio_get_interface_description()->concrete_io_setoption(sasl_client_io, "logtrace", &log_trace);
1737
1738 // assert
1739 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1740 ASSERT_ARE_EQUAL(int, 0, result);
1741
1742 // cleanup
1743 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
1744 }
1745
1746 /* Tests_SRS_SASLCLIENTIO_01_131: [ SASL client IO shall handle the following options: ]*/
1747 /* Tests_SRS_SASLCLIENTIO_01_132: [ - logtrace - bool. ]*/
1748 TEST_FUNCTION(saslclientio_setoption_with_logtrace_false_succeeds)
1749 {
1750 // arrange
1751 SASLCLIENTIO_CONFIG sasl_client_io_config;
1752 CONCRETE_IO_HANDLE sasl_client_io;
1753 int result;
1754 bool log_trace = false;
1755 sasl_client_io_config.underlying_io = test_underlying_io;
1756 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
1757 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
1758 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
1759 setup_successful_sasl_handshake();
1760 saved_on_io_error(saved_on_io_error_context);
1761 umock_c_reset_all_calls();
1762
1763 // act
1764 result = saslclientio_get_interface_description()->concrete_io_setoption(sasl_client_io, "logtrace", &log_trace);
1765
1766 // assert
1767 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1768 ASSERT_ARE_EQUAL(int, 0, result);
1769
1770 // cleanup
1771 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
1772 }
1773
1774 /* saslclientio_retrieveoptions */
1775
1776 /* Tests_SRS_SASLCLIENTIO_01_133: [ `saslclientio_retrieveoptions` shall create an option handler by calling `OptionHandler_Create`. ]*/
1777 TEST_FUNCTION(saslclientio_retrieveoptions_creates_an_option_handler)
1778 {
1779 // arrange
1780 SASLCLIENTIO_CONFIG sasl_client_io_config;
1781 CONCRETE_IO_HANDLE sasl_client_io;
1782 OPTIONHANDLER_HANDLE result;
1783 sasl_client_io_config.underlying_io = test_underlying_io;
1784 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
1785 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
1786 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
1787 setup_successful_sasl_handshake();
1788 saved_on_io_error(saved_on_io_error_context);
1789 umock_c_reset_all_calls();
1790
1791 STRICT_EXPECTED_CALL(OptionHandler_Create(IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG));
1792
1793 // act
1794 result = saslclientio_get_interface_description()->concrete_io_retrieveoptions(sasl_client_io);
1795
1796 // assert
1797 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1798 ASSERT_IS_NOT_NULL(result);
1799
1800 // cleanup
1801 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
1802 }
1803
1804 /* Tests_SRS_SASLCLIENTIO_01_138: [ If `OptionHandler_AddOption` or `OptionHandler_Create` fails then `saslclientio_retrieveoptions` shall fail and return NULL. ]*/
1805 TEST_FUNCTION(when_OptionHandler_Create_fails_then_saslclientio_retrieveoptions_fails)
1806 {
1807 // arrange
1808 SASLCLIENTIO_CONFIG sasl_client_io_config;
1809 CONCRETE_IO_HANDLE sasl_client_io;
1810 OPTIONHANDLER_HANDLE result;
1811 sasl_client_io_config.underlying_io = test_underlying_io;
1812 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
1813 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
1814 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
1815 setup_successful_sasl_handshake();
1816 saved_on_io_error(saved_on_io_error_context);
1817 umock_c_reset_all_calls();
1818
1819 STRICT_EXPECTED_CALL(OptionHandler_Create(IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
1820 .SetReturn(NULL);
1821
1822 // act
1823 result = saslclientio_get_interface_description()->concrete_io_retrieveoptions(sasl_client_io);
1824
1825 // assert
1826 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1827 ASSERT_IS_NULL(result);
1828
1829 // cleanup
1830 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
1831 }
1832
1833 /* Tests_SRS_SASLCLIENTIO_01_139: [ When `saslclientio_retrieveoptions` is called with NULL `sasl_client_io` it shall fail and return NULL. ]*/
1834 TEST_FUNCTION(saslclientio_retrieveoptions_with_NULL_sasl_clientio_fails)
1835 {
1836 // arrange
1837 OPTIONHANDLER_HANDLE result;
1838
1839 // act
1840 result = saslclientio_get_interface_description()->concrete_io_retrieveoptions(NULL);
1841
1842 // assert
1843 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1844 ASSERT_IS_NULL(result);
1845 }
1846
1847 /* Tests_SRS_SASLCLIENTIO_01_134: [ `saslclientio_retrieveoptions` shall add the handled options to it: ]*/
1848 /* Tests_SRS_SASLCLIENTIO_01_135: [ - logtrace - bool. ]*/
1849 /* Tests_SRS_SASLCLIENTIO_01_137: [ The options shall be added by calling `OptionHandler_AddOption`. ]*/
1850 TEST_FUNCTION(when_logtrace_was_set_to_true_saslclientio_retrieveoptions_adds_it_to_the_OptionHandler)
1851 {
1852 // arrange
1853 SASLCLIENTIO_CONFIG sasl_client_io_config;
1854 CONCRETE_IO_HANDLE sasl_client_io;
1855 OPTIONHANDLER_HANDLE result;
1856 bool log_trace = true;
1857 sasl_client_io_config.underlying_io = test_underlying_io;
1858 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
1859 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
1860 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
1861 setup_successful_sasl_handshake();
1862 saved_on_io_error(saved_on_io_error_context);
1863 (void)saslclientio_get_interface_description()->concrete_io_setoption(sasl_client_io, "logtrace", &log_trace);
1864 umock_c_reset_all_calls();
1865
1866 STRICT_EXPECTED_CALL(OptionHandler_Create(IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG));
1867 STRICT_EXPECTED_CALL(OptionHandler_AddOption(test_optionhandler_handle, "logtrace", &log_trace))
1868 .ValidateArgumentValue_value_AsType(UMOCK_TYPE(bool*));
1869
1870 // act
1871 result = saslclientio_get_interface_description()->concrete_io_retrieveoptions(sasl_client_io);
1872
1873 // assert
1874 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1875 ASSERT_IS_NOT_NULL(result);
1876
1877 // cleanup
1878 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
1879 }
1880
1881 /* Tests_SRS_SASLCLIENTIO_01_134: [ `saslclientio_retrieveoptions` shall add the handled options to it: ]*/
1882 /* Tests_SRS_SASLCLIENTIO_01_135: [ - logtrace - bool. ]*/
1883 /* Tests_SRS_SASLCLIENTIO_01_137: [ The options shall be added by calling `OptionHandler_AddOption`. ]*/
1884 TEST_FUNCTION(when_logtrace_was_set_to_false_saslclientio_retrieveoptions_adds_it_to_the_OptionHandler)
1885 {
1886 // arrange
1887 SASLCLIENTIO_CONFIG sasl_client_io_config;
1888 CONCRETE_IO_HANDLE sasl_client_io;
1889 OPTIONHANDLER_HANDLE result;
1890 bool log_trace = false;
1891 sasl_client_io_config.underlying_io = test_underlying_io;
1892 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
1893 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
1894 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
1895 setup_successful_sasl_handshake();
1896 saved_on_io_error(saved_on_io_error_context);
1897 (void)saslclientio_get_interface_description()->concrete_io_setoption(sasl_client_io, "logtrace", &log_trace);
1898 umock_c_reset_all_calls();
1899
1900 STRICT_EXPECTED_CALL(OptionHandler_Create(IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG));
1901 STRICT_EXPECTED_CALL(OptionHandler_AddOption(test_optionhandler_handle, "logtrace", &log_trace))
1902 .ValidateArgumentValue_value_AsType(UMOCK_TYPE(bool*));
1903
1904 // act
1905 result = saslclientio_get_interface_description()->concrete_io_retrieveoptions(sasl_client_io);
1906
1907 // assert
1908 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1909 ASSERT_IS_NOT_NULL(result);
1910
1911 // cleanup
1912 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
1913 }
1914
1915 /* Tests_SRS_SASLCLIENTIO_01_138: [ If `OptionHandler_AddOption` or `OptionHandler_Create` fails then `saslclientio_retrieveoptions` shall fail and return NULL. ]*/
1916 TEST_FUNCTION(when_OptionHandler_AddOption_fails_saslclientio_retrieveoptions_also_fails)
1917 {
1918 // arrange
1919 SASLCLIENTIO_CONFIG sasl_client_io_config;
1920 CONCRETE_IO_HANDLE sasl_client_io;
1921 OPTIONHANDLER_HANDLE result;
1922 bool log_trace = false;
1923 sasl_client_io_config.underlying_io = test_underlying_io;
1924 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
1925 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
1926 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
1927 setup_successful_sasl_handshake();
1928 saved_on_io_error(saved_on_io_error_context);
1929 (void)saslclientio_get_interface_description()->concrete_io_setoption(sasl_client_io, "logtrace", &log_trace);
1930 umock_c_reset_all_calls();
1931
1932 STRICT_EXPECTED_CALL(OptionHandler_Create(IGNORED_PTR_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG));
1933 STRICT_EXPECTED_CALL(OptionHandler_AddOption(test_optionhandler_handle, "logtrace", &log_trace))
1934 .ValidateArgumentValue_value_AsType(UMOCK_TYPE(bool*))
1935 .SetReturn(OPTIONHANDLER_ERROR);
1936 STRICT_EXPECTED_CALL(OptionHandler_Destroy(test_optionhandler_handle));
1937
1938 // act
1939 result = saslclientio_get_interface_description()->concrete_io_retrieveoptions(sasl_client_io);
1940
1941 // assert
1942 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1943 ASSERT_IS_NULL(result);
1944
1945 // cleanup
1946 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
1947 }
1948
1949 /* on_underlying_io_bytes_received */
1950
1951 /* Tests_SRS_SASLCLIENTIO_01_027: [When the `on_underlying_io_bytes_received` callback passed to the underlying IO is called and the SASL client IO state is `IO_STATE_OPEN`, the bytes shall be indicated to the user of SASL client IO by calling the `on_bytes_received` callback that was passed in `saslclientio_open`.]*/
1952 TEST_FUNCTION(when_io_state_is_open_and_bytes_are_received_they_are_indicated_up)
1953 {
1954 // arrange
1955 SASLCLIENTIO_CONFIG sasl_client_io_config;
1956 CONCRETE_IO_HANDLE sasl_client_io;
1957 unsigned char test_bytes[] = { 0x42, 0x43 };
1958 sasl_client_io_config.underlying_io = test_underlying_io;
1959 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
1960 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
1961 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
1962 setup_successful_sasl_handshake();
1963 umock_c_reset_all_calls();
1964
1965 STRICT_EXPECTED_CALL(test_on_bytes_received((void*)0x4243, test_bytes, sizeof(test_bytes)))
1966 .ValidateArgumentBuffer(2, test_bytes, sizeof(test_bytes));
1967
1968 // act
1969 saved_on_bytes_received(saved_on_bytes_received_context, test_bytes, sizeof(test_bytes));
1970
1971 // assert
1972 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1973
1974 // cleanup
1975 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
1976 }
1977
1978 /* Tests_SRS_SASLCLIENTIO_01_027: [When the `on_underlying_io_bytes_received` callback passed to the underlying IO is called and the SASL client IO state is `IO_STATE_OPEN`, the bytes shall be indicated to the user of SASL client IO by calling the `on_bytes_received` callback that was passed in `saslclientio_open`.]*/
1979 /* Tests_SRS_SASLCLIENTIO_01_029: [The `context` argument for `on_io_error` shall be set to the `on_io_error_context` passed in `saslclientio_open`.]*/
1980 TEST_FUNCTION(when_io_state_is_open_and_bytes_are_received_and_context_passed_to_open_was_NULL_NULL_is_passed_as_context_to_the_on_bytes_received_call)
1981 {
1982 // arrange
1983 SASLCLIENTIO_CONFIG sasl_client_io_config;
1984 CONCRETE_IO_HANDLE sasl_client_io;
1985 unsigned char test_bytes[] = { 0x42, 0x43 };
1986 sasl_client_io_config.underlying_io = test_underlying_io;
1987 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
1988 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
1989 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, NULL, test_on_io_error, (void*)0x4244);
1990 setup_successful_sasl_handshake();
1991 umock_c_reset_all_calls();
1992
1993 STRICT_EXPECTED_CALL(test_on_bytes_received(NULL, test_bytes, sizeof(test_bytes)))
1994 .ValidateArgumentBuffer(2, test_bytes, sizeof(test_bytes));
1995
1996 // act
1997 saved_on_bytes_received(saved_on_bytes_received_context, test_bytes, sizeof(test_bytes));
1998
1999 // assert
2000 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2001
2002 // cleanup
2003 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
2004 }
2005
2006 /* Tests_SRS_SASLCLIENTIO_01_028: [If `buffer` is NULL or `size` is zero, nothing should be indicated as received, the state shall be switched to ERROR and the `on_io_error` callback shall be triggered.]*/
2007 TEST_FUNCTION(when_io_state_is_open_and_bytes_are_received_with_bytes_NULL_nothing_is_indicated_up)
2008 {
2009 // arrange
2010 SASLCLIENTIO_CONFIG sasl_client_io_config;
2011 CONCRETE_IO_HANDLE sasl_client_io;
2012 unsigned char test_bytes[] = { 0x42, 0x43 };
2013 sasl_client_io_config.underlying_io = test_underlying_io;
2014 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
2015 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
2016 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
2017 setup_successful_sasl_handshake();
2018 umock_c_reset_all_calls();
2019
2020 STRICT_EXPECTED_CALL(test_on_io_error((void*)0x4244));
2021
2022 // act
2023 saved_on_bytes_received(saved_on_bytes_received_context, NULL, sizeof(test_bytes));
2024
2025 // assert
2026 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2027
2028 // cleanup
2029 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
2030 }
2031
2032 /* Tests_SRS_SASLCLIENTIO_01_028: [If `buffer` is NULL or `size` is zero, nothing should be indicated as received, the state shall be switched to ERROR and the `on_io_error` callback shall be triggered.]*/
2033 TEST_FUNCTION(when_io_state_is_open_and_bytes_are_received_with_size_zero_nothing_is_indicated_up)
2034 {
2035 // arrange
2036 SASLCLIENTIO_CONFIG sasl_client_io_config;
2037 CONCRETE_IO_HANDLE sasl_client_io;
2038 unsigned char test_bytes[] = { 0x42, 0x43 };
2039 sasl_client_io_config.underlying_io = test_underlying_io;
2040 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
2041 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
2042 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
2043 setup_successful_sasl_handshake();
2044 umock_c_reset_all_calls();
2045
2046 STRICT_EXPECTED_CALL(test_on_io_error((void*)0x4244));
2047
2048 // act
2049 saved_on_bytes_received(saved_on_bytes_received_context, test_bytes, 0);
2050
2051 // assert
2052 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2053
2054 // cleanup
2055 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
2056 }
2057
2058 /* Tests_SRS_SASLCLIENTIO_01_031: [If bytes are received when the SASL client IO state is `IO_STATE_ERROR`, SASL client IO shall do nothing.]*/
2059 TEST_FUNCTION(when_io_state_is_ERROR_and_bytes_are_received_nothing_is_done)
2060 {
2061 // arrange
2062 SASLCLIENTIO_CONFIG sasl_client_io_config;
2063 CONCRETE_IO_HANDLE sasl_client_io;
2064 unsigned char test_bytes[] = { 0x42, 0x43 };
2065 sasl_client_io_config.underlying_io = test_underlying_io;
2066 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
2067 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
2068 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
2069 setup_successful_sasl_handshake();
2070 saved_on_io_error(saved_on_io_error_context);
2071 umock_c_reset_all_calls();
2072
2073 // act
2074 saved_on_bytes_received(saved_on_bytes_received_context, test_bytes, 1);
2075
2076 // assert
2077 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2078
2079 // cleanup
2080 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
2081 }
2082
2083 /* Tests_SRS_SASLCLIENTIO_01_030: [If bytes are received when the SASL client IO state is `IO_STATE_OPENING`, the bytes shall be consumed by the SASL client IO to satisfy the SASL handshake.]*/
2084 /* Tests_SRS_SASLCLIENTIO_01_003: [Other than using a protocol id of three, the exchange of SASL layer headers follows the same rules specified in the version negotiation section of the transport specification (See Part 2: section 2.2).] */
2085 TEST_FUNCTION(when_io_state_is_opening_and_1_byte_is_received_it_is_used_for_the_header)
2086 {
2087 // arrange
2088 SASLCLIENTIO_CONFIG sasl_client_io_config;
2089 CONCRETE_IO_HANDLE sasl_client_io;
2090 sasl_client_io_config.underlying_io = test_underlying_io;
2091 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
2092 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
2093 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
2094 saved_on_io_open_complete(saved_on_io_open_complete_context, IO_OPEN_OK);
2095 umock_c_reset_all_calls();
2096
2097 // act
2098 saved_on_bytes_received(saved_on_bytes_received_context, sasl_header, 1);
2099
2100 // assert
2101 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2102
2103 // cleanup
2104 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
2105 }
2106
2107 /* Tests_SRS_SASLCLIENTIO_01_030: [If bytes are received when the SASL client IO state is `IO_STATE_OPENING`, the bytes shall be consumed by the SASL client IO to satisfy the SASL handshake.]*/
2108 /* Tests_SRS_SASLCLIENTIO_01_003: [Other than using a protocol id of three, the exchange of SASL layer headers follows the same rules specified in the version negotiation section of the transport specification (See Part 2: section 2.2).] */
2109 /* Tests_SRS_SASLCLIENTIO_01_073: [If the handshake fails (i.e. the outcome is an error) the `on_io_open_complete` callback shall be triggered with `IO_OPEN_ERROR`.]*/
2110 TEST_FUNCTION(when_io_state_is_opening_and_1_bad_byte_is_received_state_is_set_to_ERROR)
2111 {
2112 // arrange
2113 SASLCLIENTIO_CONFIG sasl_client_io_config;
2114 CONCRETE_IO_HANDLE sasl_client_io;
2115 unsigned char test_bytes[] = { 0x42 };
2116 sasl_client_io_config.underlying_io = test_underlying_io;
2117 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
2118 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
2119 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
2120 saved_on_io_open_complete(saved_on_io_open_complete_context, IO_OPEN_OK);
2121 umock_c_reset_all_calls();
2122
2123 STRICT_EXPECTED_CALL(xio_close(test_underlying_io, IGNORED_PTR_ARG, IGNORED_PTR_ARG));
2124 STRICT_EXPECTED_CALL(test_on_io_open_complete((void*)0x4242, IO_OPEN_ERROR));
2125
2126 // act
2127 saved_on_bytes_received(saved_on_bytes_received_context, test_bytes, sizeof(test_bytes));
2128 saved_on_io_close_complete(saved_on_io_close_complete_context);
2129
2130 // assert
2131 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2132
2133 // cleanup
2134 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
2135 }
2136
2137 /* Tests_SRS_SASLCLIENTIO_01_030: [If bytes are received when the SASL client IO state is `IO_STATE_OPENING`, the bytes shall be consumed by the SASL client IO to satisfy the SASL handshake.]*/
2138 /* Tests_SRS_SASLCLIENTIO_01_003: [Other than using a protocol id of three, the exchange of SASL layer headers follows the same rules specified in the version negotiation section of the transport specification (See Part 2: section 2.2).] */
2139 /* Tests_SRS_SASLCLIENTIO_01_073: [If the handshake fails (i.e. the outcome is an error) the `on_io_open_complete` callback shall be triggered with `IO_OPEN_ERROR`.]*/
2140 TEST_FUNCTION(when_io_state_is_opening_and_the_last_header_byte_is_bad_state_is_set_to_ERROR)
2141 {
2142 // arrange
2143 SASLCLIENTIO_CONFIG sasl_client_io_config;
2144 CONCRETE_IO_HANDLE sasl_client_io;
2145 unsigned char test_bytes[] = { 'A', 'M', 'Q', 'P', 3, 1, 0, 'x' };
2146 sasl_client_io_config.underlying_io = test_underlying_io;
2147 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
2148 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
2149 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
2150 saved_on_io_open_complete(saved_on_io_open_complete_context, IO_OPEN_OK);
2151 umock_c_reset_all_calls();
2152
2153 STRICT_EXPECTED_CALL(xio_close(test_underlying_io, IGNORED_PTR_ARG, IGNORED_PTR_ARG));
2154 STRICT_EXPECTED_CALL(test_on_io_open_complete((void*)0x4242, IO_OPEN_ERROR));
2155
2156 // act
2157 saved_on_bytes_received(saved_on_bytes_received_context, test_bytes, sizeof(test_bytes));
2158 saved_on_io_close_complete(saved_on_io_close_complete_context);
2159
2160 // assert
2161 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2162
2163 // cleanup
2164 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
2165 }
2166
2167 /* Tests_SRS_SASLCLIENTIO_01_030: [If bytes are received when the SASL client IO state is `IO_STATE_OPENING`, the bytes shall be consumed by the SASL client IO to satisfy the SASL handshake.]*/
2168 /* Tests_SRS_SASLCLIENTIO_01_003: [Other than using a protocol id of three, the exchange of SASL layer headers follows the same rules specified in the version negotiation section of the transport specification (See Part 2: section 2.2).] */
2169 /* Tests_SRS_SASLCLIENTIO_01_001: [To establish a SASL layer, each peer MUST start by sending a protocol header.] */
2170 /* Tests_SRS_SASLCLIENTIO_01_105: [start header exchange] */
2171 /* Tests_SRS_SASLCLIENTIO_01_078: [SASL client IO shall start the header exchange by sending the SASL header.] */
2172 /* Tests_SRS_SASLCLIENTIO_01_095: [Sending the header shall be done by using `xio_send`.]*/
2173 TEST_FUNCTION(when_underlying_IO_switches_the_state_to_OPEN_the_SASL_header_is_sent)
2174 {
2175 // arrange
2176 SASLCLIENTIO_CONFIG sasl_client_io_config;
2177 CONCRETE_IO_HANDLE sasl_client_io;
2178 sasl_client_io_config.underlying_io = test_underlying_io;
2179 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
2180 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
2181 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
2182 umock_c_reset_all_calls();
2183
2184 STRICT_EXPECTED_CALL(xio_send(test_underlying_io, IGNORED_PTR_ARG, IGNORED_NUM_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG));
2185 STRICT_EXPECTED_CALL(xio_send(test_underlying_io, IGNORED_PTR_ARG, IGNORED_NUM_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
2186 .IgnoreAllCalls();
2187
2188 // act
2189 saved_on_io_open_complete(saved_on_io_open_complete_context, IO_OPEN_OK);
2190
2191 // assert
2192 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2193 stringify_bytes(io_send_bytes, io_send_byte_count, actual_stringified_io);
2194 stringify_bytes(sasl_header, sizeof(sasl_header), expected_stringified_io);
2195 ASSERT_ARE_EQUAL(char_ptr, expected_stringified_io, actual_stringified_io);
2196
2197 // cleanup
2198 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
2199 }
2200
2201 /* Tests_SRS_SASLCLIENTIO_01_073: [If the handshake fails (i.e. the outcome is an error) the `on_io_open_complete` callback shall be triggered with `IO_OPEN_ERROR`.]*/
2202 TEST_FUNCTION(when_sending_the_header_fails_state_is_set_to_ERROR)
2203 {
2204 // arrange
2205 SASLCLIENTIO_CONFIG sasl_client_io_config;
2206 CONCRETE_IO_HANDLE sasl_client_io;
2207 sasl_client_io_config.underlying_io = test_underlying_io;
2208 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
2209 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
2210 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
2211 umock_c_reset_all_calls();
2212
2213 STRICT_EXPECTED_CALL(xio_send(test_underlying_io, IGNORED_PTR_ARG, IGNORED_NUM_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
2214 .SetReturn(1);
2215 STRICT_EXPECTED_CALL(xio_close(test_underlying_io, IGNORED_PTR_ARG, IGNORED_PTR_ARG));
2216 STRICT_EXPECTED_CALL(test_on_io_open_complete((void*)0x4242, IO_OPEN_ERROR));
2217
2218 // act
2219 saved_on_io_open_complete(saved_on_io_open_complete_context, IO_OPEN_OK);
2220 saved_on_io_close_complete(saved_on_io_close_complete_context);
2221
2222 // assert
2223 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2224
2225 // cleanup
2226 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
2227 }
2228
2229 /* Tests_SRS_SASLCLIENTIO_01_030: [If bytes are received when the SASL client IO state is `IO_STATE_OPENING`, the bytes shall be consumed by the SASL client IO to satisfy the SASL handshake.]*/
2230 /* Tests_SRS_SASLCLIENTIO_01_003: [Other than using a protocol id of three, the exchange of SASL layer headers follows the same rules specified in the version negotiation section of the transport specification (See Part 2: section 2.2).] */
2231 /* Tests_SRS_SASLCLIENTIO_01_073: [If the handshake fails (i.e. the outcome is an error) the `on_io_open_complete` callback shall be triggered with `IO_OPEN_ERROR`.]*/
2232 TEST_FUNCTION(when_a_bad_header_is_received_after_a_good_one_has_been_sent_state_is_set_to_ERROR)
2233 {
2234 // arrange
2235 SASLCLIENTIO_CONFIG sasl_client_io_config;
2236 CONCRETE_IO_HANDLE sasl_client_io;
2237 unsigned char test_bytes[] = { 'A', 'M', 'Q', 'P', 3, 1, 0, 'x' };
2238 sasl_client_io_config.underlying_io = test_underlying_io;
2239 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
2240 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
2241 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
2242 saved_on_io_open_complete(saved_on_io_open_complete_context, IO_OPEN_OK);
2243 umock_c_reset_all_calls();
2244
2245 STRICT_EXPECTED_CALL(xio_close(test_underlying_io, IGNORED_PTR_ARG, IGNORED_PTR_ARG));
2246 STRICT_EXPECTED_CALL(test_on_io_open_complete((void*)0x4242, IO_OPEN_ERROR));
2247
2248 // act
2249 saved_on_bytes_received(saved_on_bytes_received_context, test_bytes, sizeof(test_bytes));
2250 saved_on_io_close_complete(saved_on_io_close_complete_context);
2251
2252 // assert
2253 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2254 stringify_bytes(io_send_bytes, io_send_byte_count, actual_stringified_io);
2255 stringify_bytes(sasl_header, sizeof(sasl_header), expected_stringified_io);
2256 ASSERT_ARE_EQUAL(char_ptr, expected_stringified_io, actual_stringified_io);
2257
2258 // cleanup
2259 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
2260 }
2261
2262 /* Tests_SRS_SASLCLIENTIO_01_030: [If bytes are received when the SASL client IO state is `IO_STATE_OPENING`, the bytes shall be consumed by the SASL client IO to satisfy the SASL handshake.]*/
2263 /* Tests_SRS_SASLCLIENTIO_01_003: [Other than using a protocol id of three, the exchange of SASL layer headers follows the same rules specified in the version negotiation section of the transport specification (See Part 2: section 2.2).] */
2264 TEST_FUNCTION(when_a_good_header_is_received_after_the_header_has_been_sent_yields_no_error)
2265 {
2266 // arrange
2267 SASLCLIENTIO_CONFIG sasl_client_io_config;
2268 CONCRETE_IO_HANDLE sasl_client_io;
2269 sasl_client_io_config.underlying_io = test_underlying_io;
2270 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
2271 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
2272 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
2273 saved_on_io_open_complete(saved_on_io_open_complete_context, IO_OPEN_OK);
2274 umock_c_reset_all_calls();
2275
2276 // act
2277 saved_on_bytes_received(saved_on_bytes_received_context, sasl_header, sizeof(sasl_header));
2278
2279 // assert
2280 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2281
2282 // cleanup
2283 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
2284 }
2285
2286 /* Tests_SRS_SASLCLIENTIO_01_030: [If bytes are received when the SASL client IO state is `IO_STATE_OPENING`, the bytes shall be consumed by the SASL client IO to satisfy the SASL handshake.]*/
2287 /* Tests_SRS_SASLCLIENTIO_01_067: [The SASL frame exchange shall be started as soon as the SASL header handshake is done.] */
2288 /* Tests_SRS_SASLCLIENTIO_01_068: [During the SASL frame exchange that constitutes the handshake the received bytes from the underlying IO shall be fed to the frame codec instance created in `saslclientio_create` by calling `frame_codec_receive_bytes`.]*/
2289 TEST_FUNCTION(when_one_byte_is_received_after_header_handshake_it_is_sent_to_the_frame_codec)
2290 {
2291 // arrange
2292 SASLCLIENTIO_CONFIG sasl_client_io_config;
2293 CONCRETE_IO_HANDLE sasl_client_io;
2294 unsigned char test_bytes[] = { 0x42 };
2295 sasl_client_io_config.underlying_io = test_underlying_io;
2296 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
2297 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
2298 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
2299 saved_on_io_open_complete(saved_on_io_open_complete_context, IO_OPEN_OK);
2300 saved_on_bytes_received(saved_on_bytes_received_context, sasl_header, sizeof(sasl_header));
2301 umock_c_reset_all_calls();
2302
2303 STRICT_EXPECTED_CALL(frame_codec_receive_bytes(test_frame_codec, IGNORED_PTR_ARG, IGNORED_NUM_ARG));
2304 STRICT_EXPECTED_CALL(frame_codec_receive_bytes(test_frame_codec, IGNORED_PTR_ARG, IGNORED_NUM_ARG))
2305 .IgnoreAllCalls();
2306
2307 // act
2308 saved_on_bytes_received(saved_on_bytes_received_context, test_bytes, sizeof(test_bytes));
2309
2310 // assert
2311 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2312 stringify_bytes(frame_codec_received_bytes, frame_codec_received_byte_count, actual_stringified_io);
2313 stringify_bytes(test_bytes, sizeof(test_bytes), expected_stringified_io);
2314 ASSERT_ARE_EQUAL(char_ptr, expected_stringified_io, actual_stringified_io);
2315
2316 // cleanup
2317 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
2318 }
2319
2320 /* Tests_SRS_SASLCLIENTIO_01_088: [If `frame_codec_receive_bytes` fails, the `on_io_error` callback shall be triggered.]*/
2321 TEST_FUNCTION(when_frame_codec_receive_bytes_fails_then_the_state_is_switched_to_ERROR)
2322 {
2323 // arrange
2324 SASLCLIENTIO_CONFIG sasl_client_io_config;
2325 CONCRETE_IO_HANDLE sasl_client_io;
2326 unsigned char test_bytes[] = { 0x42 };
2327 sasl_client_io_config.underlying_io = test_underlying_io;
2328 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
2329 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
2330 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
2331 saved_on_io_open_complete(saved_on_io_open_complete_context, IO_OPEN_OK);
2332 saved_on_bytes_received(saved_on_bytes_received_context, sasl_header, sizeof(sasl_header));
2333 umock_c_reset_all_calls();
2334
2335 STRICT_EXPECTED_CALL(frame_codec_receive_bytes(test_frame_codec, IGNORED_PTR_ARG, IGNORED_NUM_ARG))
2336 .SetReturn(1);
2337 STRICT_EXPECTED_CALL(xio_close(test_underlying_io, IGNORED_PTR_ARG, IGNORED_PTR_ARG));
2338 STRICT_EXPECTED_CALL(test_on_io_open_complete((void*)0x4242, IO_OPEN_ERROR));
2339
2340 // act
2341 saved_on_bytes_received(saved_on_bytes_received_context, test_bytes, sizeof(test_bytes));
2342 saved_on_io_close_complete(saved_on_io_close_complete_context);
2343
2344 // assert
2345 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2346
2347 // cleanup
2348 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
2349 }
2350
2351 /* Tests_SRS_SASLCLIENTIO_01_101: [`on_open_complete` with `IO_OPEN_ERROR`]*/
2352 TEST_FUNCTION(ERROR_received_in_the_state_OPENING_indicates_on_io_open_complete)
2353 {
2354 // arrange
2355 SASLCLIENTIO_CONFIG sasl_client_io_config;
2356 CONCRETE_IO_HANDLE sasl_client_io;
2357 sasl_client_io_config.underlying_io = test_underlying_io;
2358 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
2359 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
2360 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
2361 umock_c_reset_all_calls();
2362
2363 STRICT_EXPECTED_CALL(xio_close(test_underlying_io, IGNORED_PTR_ARG, IGNORED_PTR_ARG));
2364 STRICT_EXPECTED_CALL(test_on_io_open_complete((void*)0x4242, IO_OPEN_ERROR));
2365
2366 // act
2367 saved_on_io_error(saved_on_io_error_context);
2368 saved_on_io_close_complete(saved_on_io_close_complete_context);
2369
2370 // assert
2371 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2372
2373 // cleanup
2374 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
2375 }
2376
2377 /* Tests_SRS_SASLCLIENTIO_01_102: [raise ERROR] */
2378 TEST_FUNCTION(ERROR_received_in_the_state_OPEN_sets_the_state_to_ERROR_and_triggers_callback)
2379 {
2380 // arrange
2381 SASLCLIENTIO_CONFIG sasl_client_io_config;
2382 CONCRETE_IO_HANDLE sasl_client_io;
2383 sasl_client_io_config.underlying_io = test_underlying_io;
2384 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
2385 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
2386 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
2387 setup_successful_sasl_handshake();
2388 umock_c_reset_all_calls();
2389
2390 STRICT_EXPECTED_CALL(test_on_io_error((void*)0x4244));
2391
2392 // act
2393 saved_on_io_error(saved_on_io_error_context);
2394
2395 // assert
2396 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2397
2398 // cleanup
2399 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
2400 }
2401
2402 /* Tests_SRS_SASLCLIENTIO_01_103: [do nothing] */
2403 TEST_FUNCTION(ERROR_received_in_the_state_ERROR_does_nothing)
2404 {
2405 // arrange
2406 SASLCLIENTIO_CONFIG sasl_client_io_config;
2407 CONCRETE_IO_HANDLE sasl_client_io;
2408 sasl_client_io_config.underlying_io = test_underlying_io;
2409 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
2410 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
2411 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
2412 setup_successful_sasl_handshake();
2413 saved_on_io_error(saved_on_io_error_context);
2414 umock_c_reset_all_calls();
2415
2416 // act
2417 saved_on_io_error(saved_on_io_error_context);
2418
2419 // assert
2420 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2421
2422 // cleanup
2423 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
2424 }
2425
2426 /* Tests_SRS_SASLCLIENTIO_01_110: [raise ERROR] */
2427 /* Tests_SRS_SASLCLIENTIO_01_116: [If the underlying IO indicates another open while the after the header exchange has been started an error shall be indicated by calling `on_io_error`.]*/
2428 TEST_FUNCTION(underlying_io_open_complete_again_in_OPENING_raises_ERROR)
2429 {
2430 // arrange
2431 SASLCLIENTIO_CONFIG sasl_client_io_config;
2432 CONCRETE_IO_HANDLE sasl_client_io;
2433 sasl_client_io_config.underlying_io = test_underlying_io;
2434 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
2435 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
2436 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
2437 saved_on_io_open_complete(saved_on_io_open_complete_context, IO_OPEN_OK);
2438 umock_c_reset_all_calls();
2439
2440 STRICT_EXPECTED_CALL(xio_close(test_underlying_io, IGNORED_PTR_ARG, IGNORED_PTR_ARG));
2441 STRICT_EXPECTED_CALL(test_on_io_open_complete((void*)0x4242, IO_OPEN_ERROR));
2442
2443 // act
2444 saved_on_io_open_complete(saved_on_io_open_complete_context, IO_OPEN_OK);
2445 saved_on_io_close_complete(saved_on_io_close_complete_context);
2446
2447 // assert
2448 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2449
2450 // cleanup
2451 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
2452 }
2453
2454 /* Tests_SRS_SASLCLIENTIO_01_111: [do nothing] */
2455 TEST_FUNCTION(OPENING_received_in_the_state_ERROR_does_nothing)
2456 {
2457 // arrange
2458 SASLCLIENTIO_CONFIG sasl_client_io_config;
2459 CONCRETE_IO_HANDLE sasl_client_io;
2460 sasl_client_io_config.underlying_io = test_underlying_io;
2461 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
2462 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
2463 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
2464 setup_successful_sasl_handshake();
2465 saved_on_io_error(saved_on_io_error_context);
2466 umock_c_reset_all_calls();
2467
2468 // act
2469 saved_on_io_open_complete(saved_on_io_open_complete_context, IO_OPEN_OK);
2470
2471 // assert
2472 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2473
2474 // cleanup
2475 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
2476 }
2477
2478 /* Tests_SRS_SASLCLIENTIO_01_106: [raise error] */
2479 TEST_FUNCTION(underlying_io_open_complete_again_in_OPEN_raises_ERROR)
2480 {
2481 // arrange
2482 SASLCLIENTIO_CONFIG sasl_client_io_config;
2483 CONCRETE_IO_HANDLE sasl_client_io;
2484 sasl_client_io_config.underlying_io = test_underlying_io;
2485 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
2486 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
2487 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
2488 setup_successful_sasl_handshake();
2489 umock_c_reset_all_calls();
2490
2491 STRICT_EXPECTED_CALL(test_on_io_error((void*)0x4244));
2492
2493 // act
2494 saved_on_io_open_complete(saved_on_io_open_complete_context, IO_OPEN_OK);
2495
2496 // assert
2497 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2498
2499 // cleanup
2500 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
2501 }
2502
2503 /* Tests_SRS_SASLCLIENTIO_01_107: [do nothing] */
2504 TEST_FUNCTION(underlying_io_open_complete_in_the_state_ERROR_does_nothing)
2505 {
2506 // arrange
2507 SASLCLIENTIO_CONFIG sasl_client_io_config;
2508 CONCRETE_IO_HANDLE sasl_client_io;
2509 sasl_client_io_config.underlying_io = test_underlying_io;
2510 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
2511 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
2512 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
2513 setup_successful_sasl_handshake();
2514 saved_on_io_error(saved_on_io_error_context);
2515 umock_c_reset_all_calls();
2516
2517 // act
2518 saved_on_io_open_complete(saved_on_io_open_complete_context, IO_OPEN_OK);
2519
2520 // assert
2521 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2522
2523 // cleanup
2524 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
2525 }
2526
2527 /* Tests_SRS_SASLCLIENTIO_01_077: [If sending the SASL header fails, the `on_io_open_complete` callback shall be triggered with `IO_OPEN_ERROR`.]*/
2528 TEST_FUNCTION(when_sending_the_header_with_xio_send_fails_then_the_io_state_is_set_to_ERROR)
2529 {
2530 // arrange
2531 SASLCLIENTIO_CONFIG sasl_client_io_config;
2532 CONCRETE_IO_HANDLE sasl_client_io;
2533 sasl_client_io_config.underlying_io = test_underlying_io;
2534 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
2535 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
2536 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
2537 umock_c_reset_all_calls();
2538
2539 STRICT_EXPECTED_CALL(xio_send(test_underlying_io, IGNORED_PTR_ARG, IGNORED_NUM_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
2540 .SetReturn(1);
2541 STRICT_EXPECTED_CALL(xio_close(test_underlying_io, IGNORED_PTR_ARG, IGNORED_PTR_ARG));
2542 STRICT_EXPECTED_CALL(test_on_io_open_complete((void*)0x4242, IO_OPEN_ERROR));
2543
2544 // act
2545 saved_on_io_open_complete(saved_on_io_open_complete_context, IO_OPEN_OK);
2546 saved_on_io_close_complete(saved_on_io_close_complete_context);
2547
2548 // assert
2549 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2550
2551 // cleanup
2552 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
2553 }
2554
2555 /* Tests_SRS_SASLCLIENTIO_01_032: [The peer acting as the SASL server MUST announce supported authentication mechanisms using the sasl-mechanisms frame.] */
2556 /* Tests_SRS_SASLCLIENTIO_01_040: [The peer playing the role of the SASL client and the peer playing the role of the SASL server MUST correspond to the TCP client and server respectively.] */
2557 /* Tests_SRS_SASLCLIENTIO_01_070: [When a frame needs to be sent as part of the SASL handshake frame exchange, the send shall be done by calling `sasl_frame_codec_encode_frame`.]*/
2558 /* Tests_SRS_SASLCLIENTIO_01_034: [<-- SASL-MECHANISMS] */
2559 /* Tests_SRS_SASLCLIENTIO_01_035: [SASL-INIT -->] */
2560 /* Tests_SRS_SASLCLIENTIO_01_033: [The partner MUST then choose one of the supported mechanisms and initiate a sasl exchange.] */
2561 /* Tests_SRS_SASLCLIENTIO_01_054: [Selects the sasl mechanism and provides the initial response if needed.] */
2562 /* Tests_SRS_SASLCLIENTIO_01_045: [The name of the SASL mechanism used for the SASL exchange.] */
2563 TEST_FUNCTION(when_a_SASL_mechanism_is_received_after_the_header_exchange_a_sasl_init_frame_is_send_with_the_selected_mechanism)
2564 {
2565 // arrange
2566 SASLCLIENTIO_CONFIG sasl_client_io_config;
2567 CONCRETE_IO_HANDLE sasl_client_io;
2568 uint32_t mechanisms_count = 1;
2569 SASL_MECHANISM_BYTES init_bytes;
2570
2571 init_bytes.bytes = NULL;
2572 init_bytes.length = 0;
2573
2574 sasl_client_io_config.underlying_io = test_underlying_io;
2575 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
2576 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
2577 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
2578 saved_on_io_open_complete(saved_on_io_open_complete_context, IO_OPEN_OK);
2579 saved_on_bytes_received(saved_on_bytes_received_context, sasl_header, sizeof(sasl_header));
2580 saved_on_bytes_received(saved_on_bytes_received_context, test_sasl_mechanisms_frame, sizeof(test_sasl_mechanisms_frame));
2581 umock_c_reset_all_calls();
2582
2583 STRICT_EXPECTED_CALL(amqpvalue_get_inplace_descriptor(test_sasl_value));
2584 STRICT_EXPECTED_CALL(is_sasl_mechanisms_type_by_descriptor(test_descriptor_value))
2585 .SetReturn(true);
2586 STRICT_EXPECTED_CALL(amqpvalue_get_sasl_mechanisms(test_sasl_value, IGNORED_PTR_ARG))
2587 .CopyOutArgumentBuffer(2, &test_sasl_mechanisms_handle, sizeof(test_sasl_mechanisms_handle));
2588 STRICT_EXPECTED_CALL(sasl_mechanisms_get_sasl_server_mechanisms(test_sasl_mechanisms_handle, IGNORED_PTR_ARG))
2589 .CopyOutArgumentBuffer_sasl_server_mechanisms_value(&test_sasl_server_mechanisms_value, sizeof(test_sasl_server_mechanisms_value));
2590 STRICT_EXPECTED_CALL(amqpvalue_get_array_item_count(test_sasl_server_mechanisms_value, IGNORED_PTR_ARG))
2591 .CopyOutArgumentBuffer_count(&mechanisms_count, sizeof(mechanisms_count));
2592 STRICT_EXPECTED_CALL(saslmechanism_get_mechanism_name(IGNORED_PTR_ARG));
2593 STRICT_EXPECTED_CALL(amqpvalue_get_array_item(test_sasl_server_mechanisms_value, 0))
2594 .SetReturn(test_sasl_server_mechanism);
2595 STRICT_EXPECTED_CALL(amqpvalue_get_symbol(test_sasl_server_mechanism, IGNORED_PTR_ARG))
2596 .CopyOutArgumentBuffer(2, &test_mechanism, sizeof(test_mechanism));
2597 STRICT_EXPECTED_CALL(amqpvalue_destroy(IGNORED_PTR_ARG));
2598 STRICT_EXPECTED_CALL(sasl_init_create(IGNORED_PTR_ARG));
2599 STRICT_EXPECTED_CALL(saslmechanism_get_init_bytes(IGNORED_PTR_ARG, IGNORED_PTR_ARG))
2600 .CopyOutArgumentBuffer(2, &init_bytes, sizeof(init_bytes));
2601 STRICT_EXPECTED_CALL(amqpvalue_create_sasl_init(test_sasl_init));
2602 STRICT_EXPECTED_CALL(sasl_frame_codec_encode_frame(test_sasl_frame_codec, test_sasl_init_value, IGNORED_PTR_ARG, IGNORED_PTR_ARG));
2603 STRICT_EXPECTED_CALL(amqpvalue_destroy(test_sasl_init_value));
2604 STRICT_EXPECTED_CALL(sasl_init_destroy(test_sasl_init));
2605 STRICT_EXPECTED_CALL(sasl_mechanisms_destroy(test_sasl_mechanisms_handle));
2606
2607 // act
2608 saved_on_sasl_frame_received(saved_sasl_frame_codec_callback_context, test_sasl_value);
2609
2610 // assert
2611 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2612
2613 // cleanup
2614 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
2615 }
2616
2617 /* Tests_SRS_SASLCLIENTIO_01_047: [A block of opaque data passed to the security mechanism.] */
2618 /* Tests_SRS_SASLCLIENTIO_01_048: [The contents of this data are defined by the SASL security mechanism.] */
2619 TEST_FUNCTION(when_a_SASL_mechanism_is_received_a_sasl_init_frame_is_send_with_the_mechanism_name_and_the_init_bytes)
2620 {
2621 // arrange
2622 SASLCLIENTIO_CONFIG sasl_client_io_config;
2623 CONCRETE_IO_HANDLE sasl_client_io;
2624 unsigned char test_init_bytes[] = { 0x42, 0x43 };
2625 uint32_t mechanisms_count = 1;
2626 SASL_MECHANISM_BYTES init_bytes;
2627 amqp_binary expected_creds;
2628
2629 init_bytes.bytes = test_init_bytes;
2630 init_bytes.length = sizeof(test_init_bytes);
2631
2632 expected_creds.bytes = test_init_bytes;
2633 expected_creds.length = sizeof(test_init_bytes);
2634
2635 sasl_client_io_config.underlying_io = test_underlying_io;
2636 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
2637 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
2638 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
2639 saved_on_io_open_complete(saved_on_io_open_complete_context, IO_OPEN_OK);
2640 saved_on_bytes_received(saved_on_bytes_received_context, sasl_header, sizeof(sasl_header));
2641 saved_on_bytes_received(saved_on_bytes_received_context, test_sasl_mechanisms_frame, sizeof(test_sasl_mechanisms_frame));
2642 umock_c_reset_all_calls();
2643
2644 STRICT_EXPECTED_CALL(amqpvalue_get_inplace_descriptor(test_sasl_value));
2645 STRICT_EXPECTED_CALL(is_sasl_mechanisms_type_by_descriptor(test_descriptor_value))
2646 .SetReturn(true);
2647 STRICT_EXPECTED_CALL(amqpvalue_get_sasl_mechanisms(test_sasl_value, IGNORED_PTR_ARG))
2648 .CopyOutArgumentBuffer(2, &test_sasl_mechanisms_handle, sizeof(test_sasl_mechanisms_handle));
2649 STRICT_EXPECTED_CALL(sasl_mechanisms_get_sasl_server_mechanisms(test_sasl_mechanisms_handle, IGNORED_PTR_ARG))
2650 .CopyOutArgumentBuffer_sasl_server_mechanisms_value(&test_sasl_server_mechanisms_value, sizeof(test_sasl_server_mechanisms_value));
2651 STRICT_EXPECTED_CALL(amqpvalue_get_array_item_count(test_sasl_server_mechanisms_value, &mechanisms_count))
2652 .CopyOutArgumentBuffer(2, &mechanisms_count, sizeof(mechanisms_count));
2653 STRICT_EXPECTED_CALL(saslmechanism_get_mechanism_name(test_sasl_mechanism));
2654 STRICT_EXPECTED_CALL(amqpvalue_get_array_item(test_sasl_server_mechanisms_value, 0));
2655 STRICT_EXPECTED_CALL(amqpvalue_get_symbol(test_sasl_server_mechanism, IGNORED_PTR_ARG))
2656 .CopyOutArgumentBuffer(2, &test_mechanism, sizeof(test_mechanism));
2657 STRICT_EXPECTED_CALL(amqpvalue_destroy(test_sasl_server_mechanism));
2658 STRICT_EXPECTED_CALL(sasl_init_create(test_mechanism));
2659 STRICT_EXPECTED_CALL(saslmechanism_get_init_bytes(test_sasl_mechanism, IGNORED_PTR_ARG))
2660 .CopyOutArgumentBuffer(2, &init_bytes, sizeof(init_bytes));
2661 STRICT_EXPECTED_CALL(sasl_init_set_initial_response(test_sasl_init, expected_creds));
2662 STRICT_EXPECTED_CALL(amqpvalue_create_sasl_init(test_sasl_init));
2663 STRICT_EXPECTED_CALL(sasl_frame_codec_encode_frame(test_sasl_frame_codec, test_sasl_init_value, IGNORED_PTR_ARG, IGNORED_PTR_ARG));
2664 STRICT_EXPECTED_CALL(amqpvalue_destroy(test_sasl_init_value));
2665 STRICT_EXPECTED_CALL(sasl_init_destroy(test_sasl_init));
2666 STRICT_EXPECTED_CALL(sasl_mechanisms_destroy(test_sasl_mechanisms_handle));
2667
2668 // act
2669 saved_on_sasl_frame_received(saved_sasl_frame_codec_callback_context, test_sasl_value);
2670
2671 // assert
2672 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2673
2674 // cleanup
2675 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
2676 }
2677
2678 /* Tests_SRS_SASLCLIENTIO_01_118: [If `on_sasl_frame_received_callback` is called in the OPENING state but the header exchange has not yet been completed, then the `on_io_error` callback shall be triggered.]*/
2679 TEST_FUNCTION(when_a_SASL_mechanism_is_received_when_header_handshake_is_not_done_the_IO_is_closed_pending_open_complete_with_error)
2680 {
2681 // arrange
2682 SASLCLIENTIO_CONFIG sasl_client_io_config;
2683 CONCRETE_IO_HANDLE sasl_client_io;
2684 sasl_client_io_config.underlying_io = test_underlying_io;
2685 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
2686 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
2687 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
2688 saved_on_io_open_complete(saved_on_io_open_complete_context, IO_OPEN_OK);
2689 umock_c_reset_all_calls();
2690
2691 STRICT_EXPECTED_CALL(xio_close(test_underlying_io, IGNORED_PTR_ARG, IGNORED_PTR_ARG));
2692 STRICT_EXPECTED_CALL(test_on_io_open_complete((void*)0x4242, IO_OPEN_ERROR));
2693
2694 // act
2695 saved_on_sasl_frame_received(saved_sasl_frame_codec_callback_context, test_sasl_value);
2696 saved_on_io_close_complete(saved_on_io_close_complete_context);
2697
2698 // assert
2699 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2700
2701 // cleanup
2702 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
2703 }
2704
2705 /* Tests_SRS_SASLCLIENTIO_01_117: [If `on_sasl_frame_received_callback` is called when the state of the IO is OPEN then the `on_io_error` callback shall be triggered.]*/
2706 TEST_FUNCTION(when_a_SASL_mechanism_is_received_in_the_OPEN_state_the_IO_state_is_set_to_ERROR)
2707 {
2708 // arrange
2709 SASLCLIENTIO_CONFIG sasl_client_io_config;
2710 CONCRETE_IO_HANDLE sasl_client_io;
2711 sasl_client_io_config.underlying_io = test_underlying_io;
2712 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
2713 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
2714 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
2715 setup_successful_sasl_handshake();
2716 umock_c_reset_all_calls();
2717
2718 STRICT_EXPECTED_CALL(test_on_io_error((void*)0x4244));
2719
2720 // act
2721 saved_on_sasl_frame_received(saved_sasl_frame_codec_callback_context, test_sasl_value);
2722
2723 // assert
2724 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2725
2726 // cleanup
2727 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
2728 }
2729
2730 /* Tests_SRS_SASLCLIENTIO_01_119: [If any error is encountered when parsing the received frame, the `on_io_open_complete` callback shall be triggered with `IO_OPEN_ERROR`.]*/
2731 TEST_FUNCTION(when_a_SASL_mechanism_is_received_and_getting_the_descriptor_fails_the_IO_is_closed_pending_open_complete_with_error)
2732 {
2733 // arrange
2734 SASLCLIENTIO_CONFIG sasl_client_io_config;
2735 CONCRETE_IO_HANDLE sasl_client_io;
2736 sasl_client_io_config.underlying_io = test_underlying_io;
2737 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
2738 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
2739 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
2740 saved_on_io_open_complete(saved_on_io_open_complete_context, IO_OPEN_OK);
2741 saved_on_bytes_received(saved_on_bytes_received_context, sasl_header, sizeof(sasl_header));
2742 saved_on_bytes_received(saved_on_bytes_received_context, test_sasl_mechanisms_frame, sizeof(test_sasl_mechanisms_frame));
2743 umock_c_reset_all_calls();
2744
2745 STRICT_EXPECTED_CALL(amqpvalue_get_inplace_descriptor(test_sasl_value))
2746 .SetReturn(NULL);
2747 STRICT_EXPECTED_CALL(xio_close(test_underlying_io, IGNORED_PTR_ARG, IGNORED_PTR_ARG));
2748 STRICT_EXPECTED_CALL(test_on_io_open_complete((void*)0x4242, IO_OPEN_ERROR));
2749
2750 // act
2751 saved_on_sasl_frame_received(saved_sasl_frame_codec_callback_context, test_sasl_value);
2752 saved_on_io_close_complete(saved_on_io_close_complete_context);
2753
2754 // assert
2755 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2756
2757 // cleanup
2758 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
2759 }
2760
2761 /* Tests_SRS_SASLCLIENTIO_01_119: [If any error is encountered when parsing the received frame, the `on_io_open_complete` callback shall be triggered with `IO_OPEN_ERROR`.]*/
2762 TEST_FUNCTION(when_a_SASL_mechanism_is_received_and_getting_the_mechanism_name_fails_the_IO_is_closed_pending_open_complete_with_error)
2763 {
2764 // arrange
2765 SASLCLIENTIO_CONFIG sasl_client_io_config;
2766 CONCRETE_IO_HANDLE sasl_client_io;
2767 uint32_t mechanisms_count = 1;
2768 sasl_client_io_config.underlying_io = test_underlying_io;
2769 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
2770
2771 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
2772 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
2773 saved_on_io_open_complete(saved_on_io_open_complete_context, IO_OPEN_OK);
2774 saved_on_bytes_received(saved_on_bytes_received_context, sasl_header, sizeof(sasl_header));
2775 saved_on_bytes_received(saved_on_bytes_received_context, test_sasl_mechanisms_frame, sizeof(test_sasl_mechanisms_frame));
2776 umock_c_reset_all_calls();
2777
2778 STRICT_EXPECTED_CALL(amqpvalue_get_inplace_descriptor(test_sasl_value));
2779 STRICT_EXPECTED_CALL(is_sasl_mechanisms_type_by_descriptor(test_descriptor_value))
2780 .SetReturn(true);
2781 STRICT_EXPECTED_CALL(amqpvalue_get_sasl_mechanisms(test_sasl_value, IGNORED_PTR_ARG))
2782 .CopyOutArgumentBuffer(2, &test_sasl_mechanisms_handle, sizeof(test_sasl_mechanisms_handle));
2783 STRICT_EXPECTED_CALL(sasl_mechanisms_get_sasl_server_mechanisms(test_sasl_mechanisms_handle, IGNORED_PTR_ARG))
2784 .CopyOutArgumentBuffer_sasl_server_mechanisms_value(&test_sasl_server_mechanisms_value, sizeof(test_sasl_server_mechanisms_value));
2785 STRICT_EXPECTED_CALL(amqpvalue_get_array_item_count(test_sasl_server_mechanisms_value, &mechanisms_count))
2786 .CopyOutArgumentBuffer(2, &mechanisms_count, sizeof(mechanisms_count));
2787 STRICT_EXPECTED_CALL(saslmechanism_get_mechanism_name(test_sasl_mechanism))
2788 .SetReturn(NULL);
2789 STRICT_EXPECTED_CALL(xio_close(test_underlying_io, IGNORED_PTR_ARG, IGNORED_PTR_ARG));
2790 STRICT_EXPECTED_CALL(sasl_mechanisms_destroy(test_sasl_mechanisms_handle));
2791 STRICT_EXPECTED_CALL(test_on_io_open_complete((void*)0x4242, IO_OPEN_ERROR));
2792
2793 // act
2794 saved_on_sasl_frame_received(saved_sasl_frame_codec_callback_context, test_sasl_value);
2795 saved_on_io_close_complete(saved_on_io_close_complete_context);
2796
2797 // assert
2798 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2799
2800 // cleanup
2801 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
2802 }
2803
2804 /* Tests_SRS_SASLCLIENTIO_01_119: [If any error is encountered when parsing the received frame, the `on_io_open_complete` callback shall be triggered with `IO_OPEN_ERROR`.]*/
2805 TEST_FUNCTION(when_a_SASL_mechanism_is_received_and_creating_the_sasl_init_value_fails_the_IO_is_closed_pending_open_complete_with_error)
2806 {
2807 // arrange
2808 SASLCLIENTIO_CONFIG sasl_client_io_config;
2809 CONCRETE_IO_HANDLE sasl_client_io;
2810 uint32_t mechanisms_count = 1;
2811 SASL_MECHANISM_BYTES init_bytes;
2812 sasl_client_io_config.underlying_io = test_underlying_io;
2813 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
2814
2815 init_bytes.length = 0;
2816 init_bytes.bytes = NULL;
2817
2818 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
2819 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
2820 saved_on_io_open_complete(saved_on_io_open_complete_context, IO_OPEN_OK);
2821 saved_on_bytes_received(saved_on_bytes_received_context, sasl_header, sizeof(sasl_header));
2822 saved_on_bytes_received(saved_on_bytes_received_context, test_sasl_mechanisms_frame, sizeof(test_sasl_mechanisms_frame));
2823 umock_c_reset_all_calls();
2824
2825 STRICT_EXPECTED_CALL(amqpvalue_get_inplace_descriptor(test_sasl_value));
2826 STRICT_EXPECTED_CALL(is_sasl_mechanisms_type_by_descriptor(test_descriptor_value))
2827 .SetReturn(true);
2828 STRICT_EXPECTED_CALL(amqpvalue_get_sasl_mechanisms(test_sasl_value, IGNORED_PTR_ARG))
2829 .CopyOutArgumentBuffer(2, &test_sasl_mechanisms_handle, sizeof(test_sasl_mechanisms_handle));
2830 STRICT_EXPECTED_CALL(sasl_mechanisms_get_sasl_server_mechanisms(test_sasl_mechanisms_handle, IGNORED_PTR_ARG))
2831 .CopyOutArgumentBuffer_sasl_server_mechanisms_value(&test_sasl_server_mechanisms_value, sizeof(test_sasl_server_mechanisms_value));
2832 STRICT_EXPECTED_CALL(amqpvalue_get_array_item_count(test_sasl_server_mechanisms_value, &mechanisms_count))
2833 .CopyOutArgumentBuffer(2, &mechanisms_count, sizeof(mechanisms_count));
2834 STRICT_EXPECTED_CALL(saslmechanism_get_mechanism_name(test_sasl_mechanism));
2835 STRICT_EXPECTED_CALL(amqpvalue_get_array_item(test_sasl_server_mechanisms_value, 0));
2836 STRICT_EXPECTED_CALL(amqpvalue_get_symbol(test_sasl_server_mechanism, IGNORED_PTR_ARG))
2837 .CopyOutArgumentBuffer(2, &test_mechanism, sizeof(test_mechanism));
2838 STRICT_EXPECTED_CALL(amqpvalue_destroy(test_sasl_server_mechanism));
2839 STRICT_EXPECTED_CALL(sasl_init_create(test_mechanism))
2840 .SetReturn(NULL);
2841 STRICT_EXPECTED_CALL(xio_close(test_underlying_io, IGNORED_PTR_ARG, IGNORED_PTR_ARG));
2842 STRICT_EXPECTED_CALL(sasl_mechanisms_destroy(test_sasl_mechanisms_handle));
2843 STRICT_EXPECTED_CALL(test_on_io_open_complete((void*)0x4242, IO_OPEN_ERROR));
2844
2845 // act
2846 saved_on_sasl_frame_received(saved_sasl_frame_codec_callback_context, test_sasl_value);
2847 saved_on_io_close_complete(saved_on_io_close_complete_context);
2848
2849 // assert
2850 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2851
2852 // cleanup
2853 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
2854 }
2855
2856 /* Tests_SRS_SASLCLIENTIO_01_119: [If any error is encountered when parsing the received frame, the `on_io_open_complete` callback shall be triggered with `IO_OPEN_ERROR`.]*/
2857 TEST_FUNCTION(when_a_SASL_mechanism_is_received_and_getting_the_initial_bytes_fails_the_IO_is_closed_pending_open_complete_with_error)
2858 {
2859 // arrange
2860 SASLCLIENTIO_CONFIG sasl_client_io_config;
2861 CONCRETE_IO_HANDLE sasl_client_io;
2862 uint32_t mechanisms_count = 1;
2863 SASL_MECHANISM_BYTES init_bytes;
2864
2865 init_bytes.length = 0;
2866 init_bytes.bytes = NULL;
2867
2868 sasl_client_io_config.underlying_io = test_underlying_io;
2869 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
2870 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
2871 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
2872 saved_on_io_open_complete(saved_on_io_open_complete_context, IO_OPEN_OK);
2873 saved_on_bytes_received(saved_on_bytes_received_context, sasl_header, sizeof(sasl_header));
2874 saved_on_bytes_received(saved_on_bytes_received_context, test_sasl_mechanisms_frame, sizeof(test_sasl_mechanisms_frame));
2875 umock_c_reset_all_calls();
2876
2877 STRICT_EXPECTED_CALL(amqpvalue_get_inplace_descriptor(test_sasl_value));
2878 STRICT_EXPECTED_CALL(is_sasl_mechanisms_type_by_descriptor(test_descriptor_value)).SetReturn(true);
2879 STRICT_EXPECTED_CALL(amqpvalue_get_sasl_mechanisms(test_sasl_value, IGNORED_PTR_ARG))
2880 .CopyOutArgumentBuffer(2, &test_sasl_mechanisms_handle, sizeof(test_sasl_mechanisms_handle));
2881 STRICT_EXPECTED_CALL(sasl_mechanisms_get_sasl_server_mechanisms(test_sasl_mechanisms_handle, IGNORED_PTR_ARG))
2882 .CopyOutArgumentBuffer_sasl_server_mechanisms_value(&test_sasl_server_mechanisms_value, sizeof(test_sasl_server_mechanisms_value));
2883 STRICT_EXPECTED_CALL(amqpvalue_get_array_item_count(test_sasl_server_mechanisms_value, &mechanisms_count))
2884 .CopyOutArgumentBuffer(2, &mechanisms_count, sizeof(mechanisms_count));
2885 STRICT_EXPECTED_CALL(saslmechanism_get_mechanism_name(test_sasl_mechanism));
2886 STRICT_EXPECTED_CALL(amqpvalue_get_array_item(test_sasl_server_mechanisms_value, 0));
2887 STRICT_EXPECTED_CALL(amqpvalue_get_symbol(test_sasl_server_mechanism, IGNORED_PTR_ARG))
2888 .CopyOutArgumentBuffer(2, &test_mechanism, sizeof(test_mechanism));
2889 STRICT_EXPECTED_CALL(amqpvalue_destroy(test_sasl_server_mechanism));
2890 STRICT_EXPECTED_CALL(sasl_init_create(test_mechanism));
2891 STRICT_EXPECTED_CALL(saslmechanism_get_init_bytes(test_sasl_mechanism, IGNORED_PTR_ARG))
2892 .CopyOutArgumentBuffer(2, &init_bytes, sizeof(init_bytes))
2893 .SetReturn(1);
2894 STRICT_EXPECTED_CALL(sasl_init_destroy(test_sasl_init));
2895 STRICT_EXPECTED_CALL(xio_close(test_underlying_io, IGNORED_PTR_ARG, IGNORED_PTR_ARG));
2896 STRICT_EXPECTED_CALL(sasl_mechanisms_destroy(test_sasl_mechanisms_handle));
2897 STRICT_EXPECTED_CALL(test_on_io_open_complete((void*)0x4242, IO_OPEN_ERROR));
2898
2899 // act
2900 saved_on_sasl_frame_received(saved_sasl_frame_codec_callback_context, test_sasl_value);
2901 saved_on_io_close_complete(saved_on_io_close_complete_context);
2902
2903 // assert
2904 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2905
2906 // cleanup
2907 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
2908 }
2909
2910 /* Tests_SRS_SASLCLIENTIO_01_119: [If any error is encountered when parsing the received frame, the `on_io_open_complete` callback shall be triggered with `IO_OPEN_ERROR`.]*/
2911 TEST_FUNCTION(when_a_SASL_mechanism_is_received_and_getting_the_AMQP_VALUE_fails_the_IO_is_closed_pending_open_complete_with_error)
2912 {
2913 // arrange
2914 SASLCLIENTIO_CONFIG sasl_client_io_config;
2915 CONCRETE_IO_HANDLE sasl_client_io;
2916 uint32_t mechanisms_count = 1;
2917 SASL_MECHANISM_BYTES init_bytes;
2918
2919 init_bytes.length = 0;
2920 init_bytes.bytes = NULL;
2921
2922 sasl_client_io_config.underlying_io = test_underlying_io;
2923 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
2924 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
2925 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
2926 saved_on_io_open_complete(saved_on_io_open_complete_context, IO_OPEN_OK);
2927 saved_on_bytes_received(saved_on_bytes_received_context, sasl_header, sizeof(sasl_header));
2928 saved_on_bytes_received(saved_on_bytes_received_context, test_sasl_mechanisms_frame, sizeof(test_sasl_mechanisms_frame));
2929 umock_c_reset_all_calls();
2930
2931 STRICT_EXPECTED_CALL(amqpvalue_get_inplace_descriptor(test_sasl_value));
2932 STRICT_EXPECTED_CALL(is_sasl_mechanisms_type_by_descriptor(test_descriptor_value)).SetReturn(true);
2933 STRICT_EXPECTED_CALL(amqpvalue_get_sasl_mechanisms(test_sasl_value, IGNORED_PTR_ARG))
2934 .CopyOutArgumentBuffer(2, &test_sasl_mechanisms_handle, sizeof(test_sasl_mechanisms_handle));
2935 STRICT_EXPECTED_CALL(sasl_mechanisms_get_sasl_server_mechanisms(test_sasl_mechanisms_handle, IGNORED_PTR_ARG))
2936 .CopyOutArgumentBuffer_sasl_server_mechanisms_value(&test_sasl_server_mechanisms_value, sizeof(test_sasl_server_mechanisms_value));
2937 STRICT_EXPECTED_CALL(amqpvalue_get_array_item_count(test_sasl_server_mechanisms_value, &mechanisms_count))
2938 .CopyOutArgumentBuffer(2, &mechanisms_count, sizeof(mechanisms_count));
2939 STRICT_EXPECTED_CALL(saslmechanism_get_mechanism_name(test_sasl_mechanism));
2940 STRICT_EXPECTED_CALL(amqpvalue_get_array_item(test_sasl_server_mechanisms_value, 0));
2941 STRICT_EXPECTED_CALL(amqpvalue_get_symbol(test_sasl_server_mechanism, IGNORED_PTR_ARG))
2942 .CopyOutArgumentBuffer(2, &test_mechanism, sizeof(test_mechanism));
2943 STRICT_EXPECTED_CALL(amqpvalue_destroy(test_sasl_server_mechanism));
2944 STRICT_EXPECTED_CALL(sasl_init_create(test_mechanism));
2945 STRICT_EXPECTED_CALL(saslmechanism_get_init_bytes(test_sasl_mechanism, IGNORED_PTR_ARG))
2946 .CopyOutArgumentBuffer(2, &init_bytes, sizeof(init_bytes));
2947 STRICT_EXPECTED_CALL(amqpvalue_create_sasl_init(test_sasl_init))
2948 .SetReturn(NULL);
2949 STRICT_EXPECTED_CALL(sasl_init_destroy(test_sasl_init));
2950 STRICT_EXPECTED_CALL(xio_close(test_underlying_io, IGNORED_PTR_ARG, IGNORED_PTR_ARG));
2951 STRICT_EXPECTED_CALL(sasl_mechanisms_destroy(test_sasl_mechanisms_handle));
2952 STRICT_EXPECTED_CALL(test_on_io_open_complete((void*)0x4242, IO_OPEN_ERROR));
2953
2954 // act
2955 saved_on_sasl_frame_received(saved_sasl_frame_codec_callback_context, test_sasl_value);
2956 saved_on_io_close_complete(saved_on_io_close_complete_context);
2957
2958 // assert
2959 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2960
2961 // cleanup
2962 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
2963 }
2964
2965 /* Tests_SRS_SASLCLIENTIO_01_071: [If `sasl_frame_codec_encode_frame` fails, then the `on_io_error` callback shall be triggered.]*/
2966 TEST_FUNCTION(when_a_SASL_mechanism_is_received_and_encoding_the_sasl_frame_fails_the_IO_is_closed_pending_open_complete_with_error)
2967 {
2968 // arrange
2969 SASLCLIENTIO_CONFIG sasl_client_io_config;
2970 CONCRETE_IO_HANDLE sasl_client_io;
2971 uint32_t mechanisms_count = 1;
2972 SASL_MECHANISM_BYTES init_bytes;
2973
2974 init_bytes.length = 0;
2975 init_bytes.bytes = NULL;
2976
2977 sasl_client_io_config.underlying_io = test_underlying_io;
2978 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
2979 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
2980 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
2981 saved_on_io_open_complete(saved_on_io_open_complete_context, IO_OPEN_OK);
2982 saved_on_bytes_received(saved_on_bytes_received_context, sasl_header, sizeof(sasl_header));
2983 saved_on_bytes_received(saved_on_bytes_received_context, test_sasl_mechanisms_frame, sizeof(test_sasl_mechanisms_frame));
2984 umock_c_reset_all_calls();
2985
2986 STRICT_EXPECTED_CALL(amqpvalue_get_inplace_descriptor(test_sasl_value));
2987 STRICT_EXPECTED_CALL(is_sasl_mechanisms_type_by_descriptor(test_descriptor_value)).SetReturn(true);
2988 STRICT_EXPECTED_CALL(amqpvalue_get_sasl_mechanisms(test_sasl_value, IGNORED_PTR_ARG))
2989 .CopyOutArgumentBuffer(2, &test_sasl_mechanisms_handle, sizeof(test_sasl_mechanisms_handle));
2990 STRICT_EXPECTED_CALL(sasl_mechanisms_get_sasl_server_mechanisms(test_sasl_mechanisms_handle, IGNORED_PTR_ARG))
2991 .CopyOutArgumentBuffer_sasl_server_mechanisms_value(&test_sasl_server_mechanisms_value, sizeof(test_sasl_server_mechanisms_value));
2992 STRICT_EXPECTED_CALL(amqpvalue_get_array_item_count(test_sasl_server_mechanisms_value, &mechanisms_count))
2993 .CopyOutArgumentBuffer(2, &mechanisms_count, sizeof(mechanisms_count));
2994 STRICT_EXPECTED_CALL(saslmechanism_get_mechanism_name(test_sasl_mechanism));
2995 STRICT_EXPECTED_CALL(amqpvalue_get_array_item(test_sasl_server_mechanisms_value, 0));
2996 STRICT_EXPECTED_CALL(amqpvalue_get_symbol(test_sasl_server_mechanism, IGNORED_PTR_ARG))
2997 .CopyOutArgumentBuffer(2, &test_mechanism, sizeof(test_mechanism));
2998 STRICT_EXPECTED_CALL(amqpvalue_destroy(test_sasl_server_mechanism));
2999 STRICT_EXPECTED_CALL(sasl_init_create(test_mechanism));
3000 STRICT_EXPECTED_CALL(saslmechanism_get_init_bytes(test_sasl_mechanism, IGNORED_PTR_ARG))
3001 .CopyOutArgumentBuffer(2, &init_bytes, sizeof(init_bytes));
3002 STRICT_EXPECTED_CALL(amqpvalue_create_sasl_init(test_sasl_init));
3003 STRICT_EXPECTED_CALL(sasl_frame_codec_encode_frame(test_sasl_frame_codec, test_sasl_init_value, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
3004 .SetReturn(1);
3005 STRICT_EXPECTED_CALL(amqpvalue_destroy(test_sasl_init_value));
3006 STRICT_EXPECTED_CALL(sasl_init_destroy(test_sasl_init));
3007 STRICT_EXPECTED_CALL(xio_close(test_underlying_io, IGNORED_PTR_ARG, IGNORED_PTR_ARG));
3008 STRICT_EXPECTED_CALL(sasl_mechanisms_destroy(test_sasl_mechanisms_handle));
3009 STRICT_EXPECTED_CALL(test_on_io_open_complete((void*)0x4242, IO_OPEN_ERROR));
3010
3011 // act
3012 saved_on_sasl_frame_received(saved_sasl_frame_codec_callback_context, test_sasl_value);
3013 saved_on_io_close_complete(saved_on_io_close_complete_context);
3014
3015 // assert
3016 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
3017
3018 // cleanup
3019 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
3020 }
3021
3022 /* Tests_SRS_SASLCLIENTIO_01_071: [If `sasl_frame_codec_encode_frame` fails, then the `on_io_error` callback shall be triggered.]*/
3023 TEST_FUNCTION(when_a_SASL_mechanism_is_received_and_setting_the_init_bytes_fails_the_IO_is_closed_pending_open_complete_with_error)
3024 {
3025 // arrange
3026 SASLCLIENTIO_CONFIG sasl_client_io_config;
3027 CONCRETE_IO_HANDLE sasl_client_io;
3028 unsigned char test_init_bytes[] = { 0x42 };
3029 uint32_t mechanisms_count = 1;
3030 SASL_MECHANISM_BYTES init_bytes;
3031 amqp_binary expected_creds;
3032
3033 init_bytes.bytes = test_init_bytes;
3034 init_bytes.length = sizeof(test_init_bytes);
3035
3036 expected_creds.bytes = test_init_bytes;
3037 expected_creds.length = sizeof(test_init_bytes);
3038
3039 sasl_client_io_config.underlying_io = test_underlying_io;
3040 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
3041 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
3042 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
3043 saved_on_io_open_complete(saved_on_io_open_complete_context, IO_OPEN_OK);
3044 saved_on_bytes_received(saved_on_bytes_received_context, sasl_header, sizeof(sasl_header));
3045 saved_on_bytes_received(saved_on_bytes_received_context, test_sasl_mechanisms_frame, sizeof(test_sasl_mechanisms_frame));
3046 umock_c_reset_all_calls();
3047
3048 STRICT_EXPECTED_CALL(amqpvalue_get_inplace_descriptor(test_sasl_value));
3049 STRICT_EXPECTED_CALL(is_sasl_mechanisms_type_by_descriptor(test_descriptor_value)).SetReturn(true);
3050 STRICT_EXPECTED_CALL(amqpvalue_get_sasl_mechanisms(test_sasl_value, IGNORED_PTR_ARG))
3051 .CopyOutArgumentBuffer(2, &test_sasl_mechanisms_handle, sizeof(test_sasl_mechanisms_handle));
3052 STRICT_EXPECTED_CALL(sasl_mechanisms_get_sasl_server_mechanisms(test_sasl_mechanisms_handle, IGNORED_PTR_ARG))
3053 .CopyOutArgumentBuffer_sasl_server_mechanisms_value(&test_sasl_server_mechanisms_value, sizeof(test_sasl_server_mechanisms_value));
3054 STRICT_EXPECTED_CALL(amqpvalue_get_array_item_count(test_sasl_server_mechanisms_value, &mechanisms_count))
3055 .CopyOutArgumentBuffer(2, &mechanisms_count, sizeof(mechanisms_count));
3056 STRICT_EXPECTED_CALL(saslmechanism_get_mechanism_name(test_sasl_mechanism));
3057 STRICT_EXPECTED_CALL(amqpvalue_get_array_item(test_sasl_server_mechanisms_value, 0));
3058 STRICT_EXPECTED_CALL(amqpvalue_get_symbol(test_sasl_server_mechanism, IGNORED_PTR_ARG))
3059 .CopyOutArgumentBuffer(2, &test_mechanism, sizeof(test_mechanism));
3060 STRICT_EXPECTED_CALL(amqpvalue_destroy(test_sasl_server_mechanism));
3061 STRICT_EXPECTED_CALL(sasl_init_create(test_mechanism));
3062 STRICT_EXPECTED_CALL(saslmechanism_get_init_bytes(test_sasl_mechanism, IGNORED_PTR_ARG))
3063 .CopyOutArgumentBuffer(2, &init_bytes, sizeof(init_bytes));
3064 STRICT_EXPECTED_CALL(sasl_init_set_initial_response(test_sasl_init, expected_creds));
3065 STRICT_EXPECTED_CALL(amqpvalue_create_sasl_init(test_sasl_init));
3066 STRICT_EXPECTED_CALL(sasl_frame_codec_encode_frame(test_sasl_frame_codec, test_sasl_init_value, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
3067 .SetReturn(1);
3068 STRICT_EXPECTED_CALL(amqpvalue_destroy(test_sasl_init_value));
3069 STRICT_EXPECTED_CALL(sasl_init_destroy(test_sasl_init));
3070 STRICT_EXPECTED_CALL(xio_close(test_underlying_io, IGNORED_PTR_ARG, IGNORED_PTR_ARG));
3071 STRICT_EXPECTED_CALL(sasl_mechanisms_destroy(test_sasl_mechanisms_handle));
3072 STRICT_EXPECTED_CALL(test_on_io_open_complete((void*)0x4242, IO_OPEN_ERROR));
3073
3074 // act
3075 saved_on_sasl_frame_received(saved_sasl_frame_codec_callback_context, test_sasl_value);
3076 saved_on_io_close_complete(saved_on_io_close_complete_context);
3077
3078 // assert
3079 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
3080
3081 // cleanup
3082 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
3083 }
3084
3085 /* Tests_SRS_SASLCLIENTIO_01_058: [This frame indicates the outcome of the SASL dialog.] */
3086 /* Tests_SRS_SASLCLIENTIO_01_059: [Upon successful completion of the SASL dialog the security layer has been established] */
3087 /* Tests_SRS_SASLCLIENTIO_01_060: [A reply-code indicating the outcome of the SASL dialog.] */
3088 /* Tests_SRS_SASLCLIENTIO_01_062: [0 Connection authentication succeeded.] */
3089 /* Tests_SRS_SASLCLIENTIO_01_038: [<-- SASL-OUTCOME] */
3090 /* Tests_SRS_SASLCLIENTIO_01_072: [When the SASL handshake is complete, if the handshake is successful, the SASL client IO state shall be switched to `IO_STATE_OPEN` and the `on_io_open_complete` callback shall be called with `IO_OPEN_OK`.]*/
3091 TEST_FUNCTION(when_a_SASL_outcome_frame_is_received_with_ok_the_SASL_IO_state_is_switched_to_OPEN)
3092 {
3093 // arrange
3094 SASLCLIENTIO_CONFIG sasl_client_io_config;
3095 CONCRETE_IO_HANDLE sasl_client_io;
3096 sasl_code sasl_outcome_code = sasl_code_ok;
3097
3098 sasl_client_io_config.underlying_io = test_underlying_io;
3099 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
3100 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
3101 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
3102 saved_on_io_open_complete(saved_on_io_open_complete_context, IO_OPEN_OK);
3103 saved_on_bytes_received(saved_on_bytes_received_context, sasl_header, sizeof(sasl_header));
3104 saved_on_bytes_received(saved_on_bytes_received_context, test_sasl_mechanisms_frame, sizeof(test_sasl_mechanisms_frame));
3105 umock_c_reset_all_calls();
3106 setup_send_init();
3107 saved_on_sasl_frame_received(saved_sasl_frame_codec_callback_context, test_sasl_value);
3108 saved_on_bytes_received(saved_on_bytes_received_context, test_sasl_outcome, sizeof(test_sasl_outcome));
3109 umock_c_reset_all_calls();
3110
3111 STRICT_EXPECTED_CALL(amqpvalue_get_inplace_descriptor(test_sasl_value));
3112 STRICT_EXPECTED_CALL(is_sasl_mechanisms_type_by_descriptor(test_descriptor_value))
3113 .SetReturn(false);
3114 STRICT_EXPECTED_CALL(is_sasl_challenge_type_by_descriptor(test_descriptor_value))
3115 .SetReturn(false);
3116 STRICT_EXPECTED_CALL(is_sasl_outcome_type_by_descriptor(test_descriptor_value))
3117 .SetReturn(true);
3118 STRICT_EXPECTED_CALL(amqpvalue_get_sasl_outcome(test_sasl_value, IGNORED_PTR_ARG))
3119 .CopyOutArgumentBuffer(2, &test_sasl_outcome_handle, sizeof(test_sasl_outcome_handle));
3120 STRICT_EXPECTED_CALL(sasl_outcome_get_code(test_sasl_outcome_handle, IGNORED_PTR_ARG))
3121 .CopyOutArgumentBuffer(2, &sasl_outcome_code, sizeof(sasl_outcome_code));
3122 STRICT_EXPECTED_CALL(test_on_io_open_complete((void*)0x4242, IO_OPEN_OK));
3123 STRICT_EXPECTED_CALL(sasl_outcome_destroy(test_sasl_outcome_handle));
3124
3125 // act
3126 saved_on_sasl_frame_received(saved_sasl_frame_codec_callback_context, test_sasl_value);
3127
3128 // assert
3129 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
3130
3131 // cleanup
3132 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
3133 }
3134
3135 void when_an_outcome_with_error_code_is_received_the_IO_is_closed_pending_open_complete_with_error(sasl_code test_sasl_code)
3136 {
3137 // arrange
3138 SASLCLIENTIO_CONFIG sasl_client_io_config;
3139 CONCRETE_IO_HANDLE sasl_client_io;
3140 sasl_code sasl_outcome_code = test_sasl_code;
3141
3142 sasl_client_io_config.underlying_io = test_underlying_io;
3143 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
3144 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
3145 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
3146 saved_on_io_open_complete(saved_on_io_open_complete_context, IO_OPEN_OK);
3147 saved_on_bytes_received(saved_on_bytes_received_context, sasl_header, sizeof(sasl_header));
3148 saved_on_bytes_received(saved_on_bytes_received_context, test_sasl_mechanisms_frame, sizeof(test_sasl_mechanisms_frame));
3149 umock_c_reset_all_calls();
3150 setup_send_init();
3151 saved_on_sasl_frame_received(saved_sasl_frame_codec_callback_context, test_sasl_value);
3152 saved_on_bytes_received(saved_on_bytes_received_context, test_sasl_outcome, sizeof(test_sasl_outcome));
3153 umock_c_reset_all_calls();
3154
3155 STRICT_EXPECTED_CALL(amqpvalue_get_inplace_descriptor(test_sasl_value));
3156 STRICT_EXPECTED_CALL(is_sasl_mechanisms_type_by_descriptor(test_descriptor_value))
3157 .SetReturn(false);
3158 STRICT_EXPECTED_CALL(is_sasl_challenge_type_by_descriptor(test_descriptor_value))
3159 .SetReturn(false);
3160 STRICT_EXPECTED_CALL(is_sasl_outcome_type_by_descriptor(test_descriptor_value))
3161 .SetReturn(true);
3162 STRICT_EXPECTED_CALL(amqpvalue_get_sasl_outcome(test_sasl_value, IGNORED_PTR_ARG))
3163 .CopyOutArgumentBuffer(2, &test_sasl_outcome_handle, sizeof(test_sasl_outcome_handle));
3164 STRICT_EXPECTED_CALL(sasl_outcome_get_code(test_sasl_outcome_handle, IGNORED_PTR_ARG))
3165 .CopyOutArgumentBuffer(2, &sasl_outcome_code, sizeof(sasl_outcome_code));
3166 STRICT_EXPECTED_CALL(xio_close(test_underlying_io, IGNORED_PTR_ARG, IGNORED_PTR_ARG));
3167 STRICT_EXPECTED_CALL(sasl_outcome_destroy(test_sasl_outcome_handle));
3168 STRICT_EXPECTED_CALL(test_on_io_open_complete((void*)0x4242, IO_OPEN_ERROR));
3169
3170 // act
3171 saved_on_sasl_frame_received(saved_sasl_frame_codec_callback_context, test_sasl_value);
3172 saved_on_io_close_complete(saved_on_io_close_complete_context);
3173
3174 // assert
3175 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
3176
3177 // cleanup
3178 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
3179 }
3180
3181 /* Tests_SRS_SASLCLIENTIO_01_063: [1 Connection authentication failed due to an unspecified problem with the supplied credentials.] */
3182 TEST_FUNCTION(when_a_SASL_outcome_frame_is_received_with_auth_error_code_the_SASL_IO_state_is_switched_to_ERROR)
3183 {
3184 when_an_outcome_with_error_code_is_received_the_IO_is_closed_pending_open_complete_with_error(sasl_code_auth);
3185 }
3186
3187 /* Tests_SRS_SASLCLIENTIO_01_064: [2 Connection authentication failed due to a system error.] */
3188 TEST_FUNCTION(when_a_SASL_outcome_frame_is_received_with_sys_error_code_the_SASL_IO_state_is_switched_to_ERROR)
3189 {
3190 when_an_outcome_with_error_code_is_received_the_IO_is_closed_pending_open_complete_with_error(sasl_code_sys);
3191 }
3192
3193 /* Tests_SRS_SASLCLIENTIO_01_065: [3 Connection authentication failed due to a system error that is unlikely to be corrected without intervention.] */
3194 TEST_FUNCTION(when_a_SASL_outcome_frame_is_received_with_sys_perm_error_code_the_SASL_IO_state_is_switched_to_ERROR)
3195 {
3196 when_an_outcome_with_error_code_is_received_the_IO_is_closed_pending_open_complete_with_error(sasl_code_sys_perm);
3197 }
3198
3199 /* Tests_SRS_SASLCLIENTIO_01_066: [4 Connection authentication failed due to a transient system error.] */
3200 TEST_FUNCTION(when_a_SASL_outcome_frame_is_received_with_sys_temp_error_code_the_SASL_IO_state_is_switched_to_ERROR)
3201 {
3202 when_an_outcome_with_error_code_is_received_the_IO_is_closed_pending_open_complete_with_error(sasl_code_sys_temp);
3203 }
3204
3205 /* Tests_SRS_SASLCLIENTIO_01_032: [The peer acting as the SASL server MUST announce supported authentication mechanisms using the sasl-mechanisms frame.] */
3206 TEST_FUNCTION(when_a_SASL_outcome_frame_is_received_before_mechanisms_the_IO_is_closed_pending_open_complete_with_error)
3207 {
3208 // arrange
3209 SASLCLIENTIO_CONFIG sasl_client_io_config;
3210 CONCRETE_IO_HANDLE sasl_client_io;
3211 sasl_client_io_config.underlying_io = test_underlying_io;
3212 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
3213 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
3214 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
3215 saved_on_io_open_complete(saved_on_io_open_complete_context, IO_OPEN_OK);
3216 saved_on_bytes_received(saved_on_bytes_received_context, sasl_header, sizeof(sasl_header));
3217 saved_on_bytes_received(saved_on_bytes_received_context, test_sasl_outcome, sizeof(test_sasl_outcome));
3218 umock_c_reset_all_calls();
3219
3220 STRICT_EXPECTED_CALL(amqpvalue_get_inplace_descriptor(test_sasl_value));
3221 STRICT_EXPECTED_CALL(is_sasl_mechanisms_type_by_descriptor(test_descriptor_value))
3222 .SetReturn(false);
3223 STRICT_EXPECTED_CALL(is_sasl_challenge_type_by_descriptor(test_descriptor_value))
3224 .SetReturn(false);
3225 STRICT_EXPECTED_CALL(is_sasl_outcome_type_by_descriptor(test_descriptor_value))
3226 .SetReturn(true);
3227
3228 STRICT_EXPECTED_CALL(xio_close(test_underlying_io, IGNORED_PTR_ARG, IGNORED_PTR_ARG));
3229 STRICT_EXPECTED_CALL(test_on_io_open_complete((void*)0x4242, IO_OPEN_ERROR));
3230
3231 // act
3232 saved_on_sasl_frame_received(saved_sasl_frame_codec_callback_context, test_sasl_value);
3233 saved_on_io_close_complete(saved_on_io_close_complete_context);
3234
3235 // assert
3236 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
3237
3238 // cleanup
3239 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
3240 }
3241
3242 /* Tests_SRS_SASLCLIENTIO_01_032: [The peer acting as the SASL server MUST announce supported authentication mechanisms using the sasl-mechanisms frame.] */
3243 TEST_FUNCTION(when_a_SASL_challenge_is_received_before_mechanisms_the_IO_is_closed_pending_open_complete_with_error)
3244 {
3245 // arrange
3246 SASLCLIENTIO_CONFIG sasl_client_io_config;
3247 CONCRETE_IO_HANDLE sasl_client_io;
3248 sasl_client_io_config.underlying_io = test_underlying_io;
3249 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
3250 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
3251 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
3252 saved_on_io_open_complete(saved_on_io_open_complete_context, IO_OPEN_OK);
3253 saved_on_bytes_received(saved_on_bytes_received_context, sasl_header, sizeof(sasl_header));
3254 saved_on_bytes_received(saved_on_bytes_received_context, test_sasl_challenge, sizeof(test_sasl_challenge));
3255 umock_c_reset_all_calls();
3256
3257 STRICT_EXPECTED_CALL(amqpvalue_get_inplace_descriptor(test_sasl_value));
3258 STRICT_EXPECTED_CALL(is_sasl_mechanisms_type_by_descriptor(test_descriptor_value))
3259 .SetReturn(false);
3260 STRICT_EXPECTED_CALL(is_sasl_challenge_type_by_descriptor(test_descriptor_value))
3261 .SetReturn(true);
3262
3263 STRICT_EXPECTED_CALL(xio_close(test_underlying_io, IGNORED_PTR_ARG, IGNORED_PTR_ARG));
3264 STRICT_EXPECTED_CALL(test_on_io_open_complete((void*)0x4242, IO_OPEN_ERROR));
3265
3266 // act
3267 saved_on_sasl_frame_received(saved_sasl_frame_codec_callback_context, test_sasl_value);
3268 saved_on_io_close_complete(saved_on_io_close_complete_context);
3269
3270 // assert
3271 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
3272
3273 // cleanup
3274 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
3275 }
3276
3277 static void setup_succesfull_challenge_response(void)
3278 {
3279 STRICT_EXPECTED_CALL(amqpvalue_get_inplace_descriptor(test_sasl_value));
3280 STRICT_EXPECTED_CALL(is_sasl_mechanisms_type_by_descriptor(test_descriptor_value))
3281 .SetReturn(false);
3282 STRICT_EXPECTED_CALL(is_sasl_challenge_type_by_descriptor(test_descriptor_value))
3283 .SetReturn(true);
3284 STRICT_EXPECTED_CALL(amqpvalue_get_sasl_challenge(test_sasl_value, IGNORED_PTR_ARG))
3285 .CopyOutArgumentBuffer(2, &test_sasl_challenge_handle, sizeof(test_sasl_challenge_handle));
3286 STRICT_EXPECTED_CALL(sasl_challenge_get_challenge(test_sasl_challenge_handle, IGNORED_PTR_ARG))
3287 .CopyOutArgumentBuffer(2, &some_challenge_bytes, sizeof(some_challenge_bytes));
3288 STRICT_EXPECTED_CALL(saslmechanism_challenge(test_sasl_mechanism, &sasl_mechanism_challenge_bytes, IGNORED_PTR_ARG))
3289 .CopyOutArgumentBuffer(3, &sasl_mechanism_response_bytes, sizeof(sasl_mechanism_response_bytes));
3290 STRICT_EXPECTED_CALL(sasl_response_create(response_binary_value));
3291 STRICT_EXPECTED_CALL(amqpvalue_create_sasl_response(test_sasl_response_handle));
3292 STRICT_EXPECTED_CALL(sasl_frame_codec_encode_frame(test_sasl_frame_codec, test_sasl_response_amqp_value, IGNORED_PTR_ARG, IGNORED_PTR_ARG));
3293 STRICT_EXPECTED_CALL(amqpvalue_destroy(test_sasl_response_amqp_value));
3294 STRICT_EXPECTED_CALL(sasl_response_destroy(test_sasl_response_handle));
3295 STRICT_EXPECTED_CALL(sasl_challenge_destroy(test_sasl_challenge_handle));
3296 }
3297
3298 /* Tests_SRS_SASLCLIENTIO_01_052: [Send the SASL challenge data as defined by the SASL specification.] */
3299 /* Tests_SRS_SASLCLIENTIO_01_053: [Challenge information, a block of opaque binary data passed to the security mechanism.] */
3300 /* Tests_SRS_SASLCLIENTIO_01_055: [Send the SASL response data as defined by the SASL specification.] */
3301 /* Tests_SRS_SASLCLIENTIO_01_056: [A block of opaque data passed to the security mechanism.] */
3302 /* Tests_SRS_SASLCLIENTIO_01_057: [The contents of this data are defined by the SASL security mechanism.] */
3303 /* Tests_SRS_SASLCLIENTIO_01_036: [<-- SASL-CHALLENGE *] */
3304 /* Tests_SRS_SASLCLIENTIO_01_037: [SASL-RESPONSE -->] */
3305 /* Tests_SRS_SASLCLIENTIO_01_070: [When a frame needs to be sent as part of the SASL handshake frame exchange, the send shall be done by calling `sasl_frame_codec_encode_frame`.]*/
3306 TEST_FUNCTION(when_a_SASL_challenge_is_received_after_the_mechanisms_the_sasl_mechanism_challenge_processing_is_invoked)
3307 {
3308 // arrange
3309 SASLCLIENTIO_CONFIG sasl_client_io_config;
3310 CONCRETE_IO_HANDLE sasl_client_io;
3311
3312 sasl_client_io_config.underlying_io = test_underlying_io;
3313 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
3314 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
3315 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
3316 saved_on_io_open_complete(saved_on_io_open_complete_context, IO_OPEN_OK);
3317 saved_on_bytes_received(saved_on_bytes_received_context, sasl_header, sizeof(sasl_header));
3318 saved_on_bytes_received(saved_on_bytes_received_context, test_sasl_mechanisms_frame, sizeof(test_sasl_mechanisms_frame));
3319 umock_c_reset_all_calls();
3320 setup_send_init();
3321 saved_on_sasl_frame_received(saved_sasl_frame_codec_callback_context, test_sasl_value);
3322 saved_on_bytes_received(saved_on_bytes_received_context, test_sasl_outcome, sizeof(test_sasl_outcome));
3323 umock_c_reset_all_calls();
3324
3325 setup_succesfull_challenge_response();
3326
3327 // act
3328 saved_on_sasl_frame_received(saved_sasl_frame_codec_callback_context, test_sasl_value);
3329
3330 // assert
3331 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
3332
3333 // cleanup
3334 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
3335 }
3336
3337 /* Tests_SRS_SASLCLIENTIO_01_119: [If any error is encountered when parsing the received frame, the `on_io_open_complete` callback shall be triggered with `IO_OPEN_ERROR`.]*/
3338 TEST_FUNCTION(when_getting_the_sasl_challenge_fails_then_the_IO_is_closed_pending_open_complete_with_error)
3339 {
3340 // arrange
3341 SASLCLIENTIO_CONFIG sasl_client_io_config;
3342 CONCRETE_IO_HANDLE sasl_client_io;
3343
3344 sasl_client_io_config.underlying_io = test_underlying_io;
3345 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
3346 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
3347 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
3348 saved_on_io_open_complete(saved_on_io_open_complete_context, IO_OPEN_OK);
3349 saved_on_bytes_received(saved_on_bytes_received_context, sasl_header, sizeof(sasl_header));
3350 saved_on_bytes_received(saved_on_bytes_received_context, test_sasl_mechanisms_frame, sizeof(test_sasl_mechanisms_frame));
3351 umock_c_reset_all_calls();
3352 setup_send_init();
3353 saved_on_sasl_frame_received(saved_sasl_frame_codec_callback_context, test_sasl_value);
3354 saved_on_bytes_received(saved_on_bytes_received_context, test_sasl_outcome, sizeof(test_sasl_outcome));
3355 umock_c_reset_all_calls();
3356
3357 STRICT_EXPECTED_CALL(amqpvalue_get_inplace_descriptor(test_sasl_value));
3358 STRICT_EXPECTED_CALL(is_sasl_mechanisms_type_by_descriptor(test_descriptor_value))
3359 .SetReturn(false);
3360 STRICT_EXPECTED_CALL(is_sasl_challenge_type_by_descriptor(test_descriptor_value))
3361 .SetReturn(true);
3362 STRICT_EXPECTED_CALL(amqpvalue_get_sasl_challenge(test_sasl_value, IGNORED_PTR_ARG))
3363 .CopyOutArgumentBuffer(2, &test_sasl_challenge_handle, sizeof(test_sasl_challenge_handle))
3364 .SetReturn(1);
3365 STRICT_EXPECTED_CALL(xio_close(test_underlying_io, IGNORED_PTR_ARG, IGNORED_PTR_ARG));
3366 STRICT_EXPECTED_CALL(test_on_io_open_complete((void*)0x4242, IO_OPEN_ERROR));
3367
3368 // act
3369 saved_on_sasl_frame_received(saved_sasl_frame_codec_callback_context, test_sasl_value);
3370 saved_on_io_close_complete(saved_on_io_close_complete_context);
3371
3372 // assert
3373 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
3374
3375 // cleanup
3376 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
3377 }
3378
3379 /* Tests_SRS_SASLCLIENTIO_01_119: [If any error is encountered when parsing the received frame, the `on_io_open_complete` callback shall be triggered with `IO_OPEN_ERROR`.]*/
3380 TEST_FUNCTION(when_getting_the_challenge_bytes_fails_then_the_IO_is_closed_pending_open_complete_with_error)
3381 {
3382 // arrange
3383 SASLCLIENTIO_CONFIG sasl_client_io_config;
3384 CONCRETE_IO_HANDLE sasl_client_io;
3385
3386 sasl_client_io_config.underlying_io = test_underlying_io;
3387 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
3388 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
3389 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
3390 saved_on_io_open_complete(saved_on_io_open_complete_context, IO_OPEN_OK);
3391 saved_on_bytes_received(saved_on_bytes_received_context, sasl_header, sizeof(sasl_header));
3392 saved_on_bytes_received(saved_on_bytes_received_context, test_sasl_mechanisms_frame, sizeof(test_sasl_mechanisms_frame));
3393 umock_c_reset_all_calls();
3394 setup_send_init();
3395 saved_on_sasl_frame_received(saved_sasl_frame_codec_callback_context, test_sasl_value);
3396 saved_on_bytes_received(saved_on_bytes_received_context, test_sasl_outcome, sizeof(test_sasl_outcome));
3397 umock_c_reset_all_calls();
3398
3399 STRICT_EXPECTED_CALL(amqpvalue_get_inplace_descriptor(test_sasl_value));
3400 STRICT_EXPECTED_CALL(is_sasl_mechanisms_type_by_descriptor(test_descriptor_value))
3401 .SetReturn(false);
3402 STRICT_EXPECTED_CALL(is_sasl_challenge_type_by_descriptor(test_descriptor_value))
3403 .SetReturn(true);
3404 STRICT_EXPECTED_CALL(amqpvalue_get_sasl_challenge(test_sasl_value, IGNORED_PTR_ARG))
3405 .CopyOutArgumentBuffer(2, &test_sasl_challenge_handle, sizeof(test_sasl_challenge_handle));
3406 STRICT_EXPECTED_CALL(sasl_challenge_get_challenge(test_sasl_challenge_handle, IGNORED_PTR_ARG))
3407 .CopyOutArgumentBuffer(2, &some_challenge_bytes, sizeof(some_challenge_bytes))
3408 .SetReturn(1);
3409 STRICT_EXPECTED_CALL(xio_close(test_underlying_io, IGNORED_PTR_ARG, IGNORED_PTR_ARG));
3410 STRICT_EXPECTED_CALL(sasl_challenge_destroy(test_sasl_challenge_handle));
3411 STRICT_EXPECTED_CALL(test_on_io_open_complete((void*)0x4242, IO_OPEN_ERROR));
3412
3413 // act
3414 saved_on_sasl_frame_received(saved_sasl_frame_codec_callback_context, test_sasl_value);
3415 saved_on_io_close_complete(saved_on_io_close_complete_context);
3416
3417 // assert
3418 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
3419
3420 // cleanup
3421 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
3422 }
3423
3424 /* Tests_SRS_SASLCLIENTIO_01_119: [If any error is encountered when parsing the received frame, the `on_io_open_complete` callback shall be triggered with `IO_OPEN_ERROR`.]*/
3425 TEST_FUNCTION(when_the_sasl_mechanism_challenge_response_function_fails_then_the_IO_is_closed_pending_open_complete_with_error)
3426 {
3427 // arrange
3428 SASLCLIENTIO_CONFIG sasl_client_io_config;
3429 CONCRETE_IO_HANDLE sasl_client_io;
3430
3431 sasl_client_io_config.underlying_io = test_underlying_io;
3432 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
3433 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
3434 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
3435 saved_on_io_open_complete(saved_on_io_open_complete_context, IO_OPEN_OK);
3436 saved_on_bytes_received(saved_on_bytes_received_context, sasl_header, sizeof(sasl_header));
3437 saved_on_bytes_received(saved_on_bytes_received_context, test_sasl_mechanisms_frame, sizeof(test_sasl_mechanisms_frame));
3438 umock_c_reset_all_calls();
3439 setup_send_init();
3440 saved_on_sasl_frame_received(saved_sasl_frame_codec_callback_context, test_sasl_value);
3441 saved_on_bytes_received(saved_on_bytes_received_context, test_sasl_outcome, sizeof(test_sasl_outcome));
3442 umock_c_reset_all_calls();
3443
3444 STRICT_EXPECTED_CALL(amqpvalue_get_inplace_descriptor(test_sasl_value));
3445 STRICT_EXPECTED_CALL(is_sasl_mechanisms_type_by_descriptor(test_descriptor_value))
3446 .SetReturn(false);
3447 STRICT_EXPECTED_CALL(is_sasl_challenge_type_by_descriptor(test_descriptor_value))
3448 .SetReturn(true);
3449 STRICT_EXPECTED_CALL(amqpvalue_get_sasl_challenge(test_sasl_value, IGNORED_PTR_ARG))
3450 .CopyOutArgumentBuffer(2, &test_sasl_challenge_handle, sizeof(test_sasl_challenge_handle));
3451 STRICT_EXPECTED_CALL(sasl_challenge_get_challenge(test_sasl_challenge_handle, IGNORED_PTR_ARG))
3452 .CopyOutArgumentBuffer(2, &some_challenge_bytes, sizeof(some_challenge_bytes));
3453 STRICT_EXPECTED_CALL(saslmechanism_challenge(test_sasl_mechanism, &sasl_mechanism_challenge_bytes, IGNORED_PTR_ARG))
3454 .CopyOutArgumentBuffer(3, &sasl_mechanism_response_bytes, sizeof(sasl_mechanism_response_bytes))
3455 .SetReturn(1);
3456 STRICT_EXPECTED_CALL(xio_close(test_underlying_io, IGNORED_PTR_ARG, IGNORED_PTR_ARG));
3457 STRICT_EXPECTED_CALL(sasl_challenge_destroy(test_sasl_challenge_handle));
3458 STRICT_EXPECTED_CALL(test_on_io_open_complete((void*)0x4242, IO_OPEN_ERROR));
3459
3460 // act
3461 saved_on_sasl_frame_received(saved_sasl_frame_codec_callback_context, test_sasl_value);
3462 saved_on_io_close_complete(saved_on_io_close_complete_context);
3463
3464 // assert
3465 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
3466
3467 // cleanup
3468 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
3469 }
3470
3471 /* Tests_SRS_SASLCLIENTIO_01_119: [If any error is encountered when parsing the received frame, the `on_io_open_complete` callback shall be triggered with `IO_OPEN_ERROR`.]*/
3472 TEST_FUNCTION(when_creating_the_sasl_response_fails_the_IO_is_closed_pending_open_complete_with_error)
3473 {
3474 // arrange
3475 SASLCLIENTIO_CONFIG sasl_client_io_config;
3476 CONCRETE_IO_HANDLE sasl_client_io;
3477
3478 sasl_client_io_config.underlying_io = test_underlying_io;
3479 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
3480 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
3481 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
3482 saved_on_io_open_complete(saved_on_io_open_complete_context, IO_OPEN_OK);
3483 saved_on_bytes_received(saved_on_bytes_received_context, sasl_header, sizeof(sasl_header));
3484 saved_on_bytes_received(saved_on_bytes_received_context, test_sasl_mechanisms_frame, sizeof(test_sasl_mechanisms_frame));
3485 umock_c_reset_all_calls();
3486 setup_send_init();
3487 saved_on_sasl_frame_received(saved_sasl_frame_codec_callback_context, test_sasl_value);
3488 saved_on_bytes_received(saved_on_bytes_received_context, test_sasl_outcome, sizeof(test_sasl_outcome));
3489 umock_c_reset_all_calls();
3490
3491 STRICT_EXPECTED_CALL(amqpvalue_get_inplace_descriptor(test_sasl_value));
3492 STRICT_EXPECTED_CALL(is_sasl_mechanisms_type_by_descriptor(test_descriptor_value))
3493 .SetReturn(false);
3494 STRICT_EXPECTED_CALL(is_sasl_challenge_type_by_descriptor(test_descriptor_value))
3495 .SetReturn(true);
3496 STRICT_EXPECTED_CALL(amqpvalue_get_sasl_challenge(test_sasl_value, IGNORED_PTR_ARG))
3497 .CopyOutArgumentBuffer(2, &test_sasl_challenge_handle, sizeof(test_sasl_challenge_handle));
3498 STRICT_EXPECTED_CALL(sasl_challenge_get_challenge(test_sasl_challenge_handle, IGNORED_PTR_ARG))
3499 .CopyOutArgumentBuffer(2, &some_challenge_bytes, sizeof(some_challenge_bytes));
3500 STRICT_EXPECTED_CALL(saslmechanism_challenge(test_sasl_mechanism, &sasl_mechanism_challenge_bytes, IGNORED_PTR_ARG))
3501 .CopyOutArgumentBuffer(3, &sasl_mechanism_response_bytes, sizeof(sasl_mechanism_response_bytes));
3502 STRICT_EXPECTED_CALL(sasl_response_create(response_binary_value))
3503 .SetReturn(NULL);
3504 STRICT_EXPECTED_CALL(xio_close(test_underlying_io, IGNORED_PTR_ARG, IGNORED_PTR_ARG));
3505 STRICT_EXPECTED_CALL(sasl_challenge_destroy(test_sasl_challenge_handle));
3506 STRICT_EXPECTED_CALL(test_on_io_open_complete((void*)0x4242, IO_OPEN_ERROR));
3507
3508 // act
3509 saved_on_sasl_frame_received(saved_sasl_frame_codec_callback_context, test_sasl_value);
3510 saved_on_io_close_complete(saved_on_io_close_complete_context);
3511
3512 // assert
3513 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
3514
3515 // cleanup
3516 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
3517 }
3518
3519 /* Tests_SRS_SASLCLIENTIO_01_119: [If any error is encountered when parsing the received frame, the `on_io_open_complete` callback shall be triggered with `IO_OPEN_ERROR`.]*/
3520 TEST_FUNCTION(when_creating_the_AMQP_VALUE_for_sasl_response_fails_the_IO_is_closed_pending_open_complete_with_error)
3521 {
3522 // arrange
3523 SASLCLIENTIO_CONFIG sasl_client_io_config;
3524 CONCRETE_IO_HANDLE sasl_client_io;
3525
3526 sasl_client_io_config.underlying_io = test_underlying_io;
3527 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
3528 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
3529 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
3530 saved_on_io_open_complete(saved_on_io_open_complete_context, IO_OPEN_OK);
3531 saved_on_bytes_received(saved_on_bytes_received_context, sasl_header, sizeof(sasl_header));
3532 saved_on_bytes_received(saved_on_bytes_received_context, test_sasl_mechanisms_frame, sizeof(test_sasl_mechanisms_frame));
3533 umock_c_reset_all_calls();
3534 setup_send_init();
3535 saved_on_sasl_frame_received(saved_sasl_frame_codec_callback_context, test_sasl_value);
3536 saved_on_bytes_received(saved_on_bytes_received_context, test_sasl_outcome, sizeof(test_sasl_outcome));
3537 umock_c_reset_all_calls();
3538
3539 STRICT_EXPECTED_CALL(amqpvalue_get_inplace_descriptor(test_sasl_value));
3540 STRICT_EXPECTED_CALL(is_sasl_mechanisms_type_by_descriptor(test_descriptor_value))
3541 .SetReturn(false);
3542 STRICT_EXPECTED_CALL(is_sasl_challenge_type_by_descriptor(test_descriptor_value))
3543 .SetReturn(true);
3544 STRICT_EXPECTED_CALL(amqpvalue_get_sasl_challenge(test_sasl_value, IGNORED_PTR_ARG))
3545 .CopyOutArgumentBuffer(2, &test_sasl_challenge_handle, sizeof(test_sasl_challenge_handle));
3546 STRICT_EXPECTED_CALL(sasl_challenge_get_challenge(test_sasl_challenge_handle, IGNORED_PTR_ARG))
3547 .CopyOutArgumentBuffer(2, &some_challenge_bytes, sizeof(some_challenge_bytes));
3548 STRICT_EXPECTED_CALL(saslmechanism_challenge(test_sasl_mechanism, &sasl_mechanism_challenge_bytes, IGNORED_PTR_ARG))
3549 .CopyOutArgumentBuffer(3, &sasl_mechanism_response_bytes, sizeof(sasl_mechanism_response_bytes));
3550 STRICT_EXPECTED_CALL(sasl_response_create(response_binary_value));
3551 STRICT_EXPECTED_CALL(amqpvalue_create_sasl_response(test_sasl_response_handle))
3552 .SetReturn(NULL);
3553 STRICT_EXPECTED_CALL(sasl_response_destroy(test_sasl_response_handle));
3554 STRICT_EXPECTED_CALL(xio_close(test_underlying_io, IGNORED_PTR_ARG, IGNORED_PTR_ARG));
3555 STRICT_EXPECTED_CALL(sasl_challenge_destroy(test_sasl_challenge_handle));
3556 STRICT_EXPECTED_CALL(test_on_io_open_complete((void*)0x4242, IO_OPEN_ERROR));
3557
3558 // act
3559 saved_on_sasl_frame_received(saved_sasl_frame_codec_callback_context, test_sasl_value);
3560 saved_on_io_close_complete(saved_on_io_close_complete_context);
3561
3562 // assert
3563 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
3564
3565 // cleanup
3566 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
3567 }
3568
3569 /* Tests_SRS_SASLCLIENTIO_01_119: [If any error is encountered when parsing the received frame, the `on_io_open_complete` callback shall be triggered with `IO_OPEN_ERROR`.]*/
3570 TEST_FUNCTION(when_encoding_the_sasl_frame_for_sasl_response_fails_the_IO_is_closed_pending_open_complete_with_error)
3571 {
3572 // arrange
3573 SASLCLIENTIO_CONFIG sasl_client_io_config;
3574 CONCRETE_IO_HANDLE sasl_client_io;
3575
3576 sasl_client_io_config.underlying_io = test_underlying_io;
3577 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
3578 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
3579 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
3580 saved_on_io_open_complete(saved_on_io_open_complete_context, IO_OPEN_OK);
3581 saved_on_bytes_received(saved_on_bytes_received_context, sasl_header, sizeof(sasl_header));
3582 saved_on_bytes_received(saved_on_bytes_received_context, test_sasl_mechanisms_frame, sizeof(test_sasl_mechanisms_frame));
3583 umock_c_reset_all_calls();
3584 setup_send_init();
3585 saved_on_sasl_frame_received(saved_sasl_frame_codec_callback_context, test_sasl_value);
3586 saved_on_bytes_received(saved_on_bytes_received_context, test_sasl_outcome, sizeof(test_sasl_outcome));
3587 umock_c_reset_all_calls();
3588
3589 STRICT_EXPECTED_CALL(amqpvalue_get_inplace_descriptor(test_sasl_value));
3590 STRICT_EXPECTED_CALL(is_sasl_mechanisms_type_by_descriptor(test_descriptor_value))
3591 .SetReturn(false);
3592 STRICT_EXPECTED_CALL(is_sasl_challenge_type_by_descriptor(test_descriptor_value))
3593 .SetReturn(true);
3594 STRICT_EXPECTED_CALL(amqpvalue_get_sasl_challenge(test_sasl_value, IGNORED_PTR_ARG))
3595 .CopyOutArgumentBuffer(2, &test_sasl_challenge_handle, sizeof(test_sasl_challenge_handle));
3596 STRICT_EXPECTED_CALL(sasl_challenge_get_challenge(test_sasl_challenge_handle, IGNORED_PTR_ARG))
3597 .CopyOutArgumentBuffer(2, &some_challenge_bytes, sizeof(some_challenge_bytes));
3598 STRICT_EXPECTED_CALL(saslmechanism_challenge(test_sasl_mechanism, &sasl_mechanism_challenge_bytes, IGNORED_PTR_ARG))
3599 .CopyOutArgumentBuffer(3, &sasl_mechanism_response_bytes, sizeof(sasl_mechanism_response_bytes));
3600 STRICT_EXPECTED_CALL(sasl_response_create(response_binary_value));
3601 STRICT_EXPECTED_CALL(amqpvalue_create_sasl_response(test_sasl_response_handle));
3602 STRICT_EXPECTED_CALL(sasl_frame_codec_encode_frame(test_sasl_frame_codec, test_sasl_response_amqp_value, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
3603 .SetReturn(1);
3604 STRICT_EXPECTED_CALL(amqpvalue_destroy(test_sasl_response_amqp_value));
3605 STRICT_EXPECTED_CALL(sasl_response_destroy(test_sasl_response_handle));
3606 STRICT_EXPECTED_CALL(xio_close(test_underlying_io, IGNORED_PTR_ARG, IGNORED_PTR_ARG));
3607 STRICT_EXPECTED_CALL(sasl_challenge_destroy(test_sasl_challenge_handle));
3608 STRICT_EXPECTED_CALL(test_on_io_open_complete((void*)0x4242, IO_OPEN_ERROR));
3609
3610 // act
3611 saved_on_sasl_frame_received(saved_sasl_frame_codec_callback_context, test_sasl_value);
3612 saved_on_io_close_complete(saved_on_io_close_complete_context);
3613
3614 // assert
3615 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
3616
3617 // cleanup
3618 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
3619 }
3620
3621 /* Tests_SRS_SASLCLIENTIO_01_039: [the SASL challenge/response step can occur zero or more times depending on the details of the SASL mechanism chosen.] */
3622 TEST_FUNCTION(SASL_challenge_response_twice_succeed)
3623 {
3624 // arrange
3625 SASLCLIENTIO_CONFIG sasl_client_io_config;
3626 CONCRETE_IO_HANDLE sasl_client_io;
3627
3628 sasl_client_io_config.underlying_io = test_underlying_io;
3629 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
3630 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
3631 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
3632 saved_on_io_open_complete(saved_on_io_open_complete_context, IO_OPEN_OK);
3633 saved_on_bytes_received(saved_on_bytes_received_context, sasl_header, sizeof(sasl_header));
3634 saved_on_bytes_received(saved_on_bytes_received_context, test_sasl_mechanisms_frame, sizeof(test_sasl_mechanisms_frame));
3635 umock_c_reset_all_calls();
3636 setup_send_init();
3637 saved_on_sasl_frame_received(saved_sasl_frame_codec_callback_context, test_sasl_value);
3638 saved_on_bytes_received(saved_on_bytes_received_context, test_sasl_outcome, sizeof(test_sasl_outcome));
3639 umock_c_reset_all_calls();
3640
3641 setup_succesfull_challenge_response();
3642 saved_on_sasl_frame_received(saved_sasl_frame_codec_callback_context, test_sasl_value);
3643
3644 setup_succesfull_challenge_response();
3645
3646 // act
3647 saved_on_sasl_frame_received(saved_sasl_frame_codec_callback_context, test_sasl_value);
3648
3649 // assert
3650 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
3651
3652 // cleanup
3653 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
3654 }
3655
3656 /* Tests_SRS_SASLCLIENTIO_01_039: [the SASL challenge/response step can occur zero or more times depending on the details of the SASL mechanism chosen.] */
3657 TEST_FUNCTION(SASL_challenge_response_256_times_succeeds)
3658 {
3659 // arrange
3660 SASLCLIENTIO_CONFIG sasl_client_io_config;
3661 CONCRETE_IO_HANDLE sasl_client_io;
3662 size_t i;
3663
3664 sasl_client_io_config.underlying_io = test_underlying_io;
3665 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
3666 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
3667
3668 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
3669 saved_on_io_open_complete(saved_on_io_open_complete_context, IO_OPEN_OK);
3670 saved_on_bytes_received(saved_on_bytes_received_context, sasl_header, sizeof(sasl_header));
3671 saved_on_bytes_received(saved_on_bytes_received_context, test_sasl_mechanisms_frame, sizeof(test_sasl_mechanisms_frame));
3672 umock_c_reset_all_calls();
3673 setup_send_init();
3674 saved_on_sasl_frame_received(saved_sasl_frame_codec_callback_context, test_sasl_value);
3675 saved_on_bytes_received(saved_on_bytes_received_context, test_sasl_outcome, sizeof(test_sasl_outcome));
3676 umock_c_reset_all_calls();
3677
3678 // act
3679 for (i = 0; i < 256; i++)
3680 {
3681 setup_succesfull_challenge_response();
3682 saved_on_sasl_frame_received(saved_sasl_frame_codec_callback_context, test_sasl_value);
3683 }
3684
3685 // assert
3686 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
3687
3688 // cleanup
3689 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
3690 }
3691
3692 /* Tests_SRS_SASLCLIENTIO_01_039: [the SASL challenge/response step can occur zero or more times depending on the details of the SASL mechanism chosen.] */
3693 /* Tests_SRS_SASLCLIENTIO_01_072: [When the SASL handshake is complete, if the handshake is successful, the SASL client IO state shall be switched to `IO_STATE_OPEN` and the `on_io_open_complete` callback shall be called with `IO_OPEN_OK`.]*/
3694 TEST_FUNCTION(SASL_challenge_response_256_times_followed_by_outcome_succeeds)
3695 {
3696 // arrange
3697 SASLCLIENTIO_CONFIG sasl_client_io_config;
3698 CONCRETE_IO_HANDLE sasl_client_io;
3699 size_t i;
3700 sasl_code sasl_outcome_code = sasl_code_ok;
3701
3702 sasl_client_io_config.underlying_io = test_underlying_io;
3703 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
3704 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
3705
3706 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
3707 saved_on_io_open_complete(saved_on_io_open_complete_context, IO_OPEN_OK);
3708 saved_on_bytes_received(saved_on_bytes_received_context, sasl_header, sizeof(sasl_header));
3709 saved_on_bytes_received(saved_on_bytes_received_context, test_sasl_mechanisms_frame, sizeof(test_sasl_mechanisms_frame));
3710 umock_c_reset_all_calls();
3711 setup_send_init();
3712 saved_on_sasl_frame_received(saved_sasl_frame_codec_callback_context, test_sasl_value);
3713 saved_on_bytes_received(saved_on_bytes_received_context, test_sasl_outcome, sizeof(test_sasl_outcome));
3714 umock_c_reset_all_calls();
3715
3716 for (i = 0; i < 256; i++)
3717 {
3718 setup_succesfull_challenge_response();
3719 saved_on_sasl_frame_received(saved_sasl_frame_codec_callback_context, test_sasl_value);
3720 }
3721
3722 STRICT_EXPECTED_CALL(amqpvalue_get_inplace_descriptor(test_sasl_value));
3723 STRICT_EXPECTED_CALL(is_sasl_mechanisms_type_by_descriptor(test_descriptor_value))
3724 .SetReturn(false);
3725 STRICT_EXPECTED_CALL(is_sasl_challenge_type_by_descriptor(test_descriptor_value))
3726 .SetReturn(false);
3727 STRICT_EXPECTED_CALL(is_sasl_outcome_type_by_descriptor(test_descriptor_value))
3728 .SetReturn(true);
3729 STRICT_EXPECTED_CALL(amqpvalue_get_sasl_outcome(test_sasl_value, IGNORED_PTR_ARG))
3730 .CopyOutArgumentBuffer(2, &test_sasl_outcome_handle, sizeof(test_sasl_outcome_handle));
3731 STRICT_EXPECTED_CALL(sasl_outcome_get_code(test_sasl_outcome_handle, IGNORED_PTR_ARG))
3732 .CopyOutArgumentBuffer(2, &sasl_outcome_code, sizeof(sasl_outcome_code));
3733 STRICT_EXPECTED_CALL(test_on_io_open_complete((void*)0x4242, IO_OPEN_OK));
3734 STRICT_EXPECTED_CALL(sasl_outcome_destroy(test_sasl_outcome_handle));
3735
3736 // act
3737 saved_on_sasl_frame_received(saved_sasl_frame_codec_callback_context, test_sasl_value);
3738
3739 // assert
3740 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
3741
3742 // cleanup
3743 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
3744 }
3745
3746 /* Tests_SRS_SASLCLIENTIO_01_119: [If any error is encountered when parsing the received frame, the `on_io_open_complete` callback shall be triggered with `IO_OPEN_ERROR`.]*/
3747 TEST_FUNCTION(when_the_mechanisms_sasl_value_cannot_be_decoded_the_IO_is_closed_pending_open_complete_with_error)
3748 {
3749 // arrange
3750 SASLCLIENTIO_CONFIG sasl_client_io_config;
3751 CONCRETE_IO_HANDLE sasl_client_io;
3752 sasl_client_io_config.underlying_io = test_underlying_io;
3753 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
3754 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
3755
3756 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
3757 saved_on_io_open_complete(saved_on_io_open_complete_context, IO_OPEN_OK);
3758 saved_on_bytes_received(saved_on_bytes_received_context, sasl_header, sizeof(sasl_header));
3759 saved_on_bytes_received(saved_on_bytes_received_context, test_sasl_mechanisms_frame, sizeof(test_sasl_mechanisms_frame));
3760 umock_c_reset_all_calls();
3761
3762 STRICT_EXPECTED_CALL(amqpvalue_get_inplace_descriptor(test_sasl_value));
3763 STRICT_EXPECTED_CALL(is_sasl_mechanisms_type_by_descriptor(test_descriptor_value)).SetReturn(true);
3764 STRICT_EXPECTED_CALL(amqpvalue_get_sasl_mechanisms(test_sasl_value, IGNORED_PTR_ARG))
3765 .CopyOutArgumentBuffer(2, &test_sasl_mechanisms_handle, sizeof(test_sasl_mechanisms_handle));
3766 STRICT_EXPECTED_CALL(sasl_mechanisms_get_sasl_server_mechanisms(test_sasl_mechanisms_handle, IGNORED_PTR_ARG))
3767 .SetReturn(1);
3768 STRICT_EXPECTED_CALL(xio_close(test_underlying_io, IGNORED_PTR_ARG, IGNORED_PTR_ARG));
3769 STRICT_EXPECTED_CALL(sasl_mechanisms_destroy(test_sasl_mechanisms_handle));
3770 STRICT_EXPECTED_CALL(test_on_io_open_complete((void*)0x4242, IO_OPEN_ERROR));
3771
3772 // act
3773 saved_on_sasl_frame_received(saved_sasl_frame_codec_callback_context, test_sasl_value);
3774 saved_on_io_close_complete(saved_on_io_close_complete_context);
3775
3776 // assert
3777 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
3778
3779 // cleanup
3780 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
3781 }
3782
3783 /* Tests_SRS_SASLCLIENTIO_01_042: [It is invalid for this list to be null or empty.] */
3784 TEST_FUNCTION(when_a_NULL_list_is_received_in_the_SASL_mechanisms_then_the_IO_is_closed_pending_open_complete_with_error)
3785 {
3786 // arrange
3787 SASLCLIENTIO_CONFIG sasl_client_io_config;
3788 CONCRETE_IO_HANDLE sasl_client_io;
3789 sasl_client_io_config.underlying_io = test_underlying_io;
3790 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
3791 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
3792
3793 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
3794 saved_on_io_open_complete(saved_on_io_open_complete_context, IO_OPEN_OK);
3795 saved_on_bytes_received(saved_on_bytes_received_context, sasl_header, sizeof(sasl_header));
3796 saved_on_bytes_received(saved_on_bytes_received_context, test_sasl_mechanisms_frame, sizeof(test_sasl_mechanisms_frame));
3797 umock_c_reset_all_calls();
3798
3799 STRICT_EXPECTED_CALL(amqpvalue_get_inplace_descriptor(test_sasl_value));
3800 STRICT_EXPECTED_CALL(is_sasl_mechanisms_type_by_descriptor(test_descriptor_value)).SetReturn(true);
3801 STRICT_EXPECTED_CALL(amqpvalue_get_sasl_mechanisms(test_sasl_value, IGNORED_PTR_ARG))
3802 .CopyOutArgumentBuffer(2, &test_sasl_mechanisms_handle, sizeof(test_sasl_mechanisms_handle));
3803 STRICT_EXPECTED_CALL(sasl_mechanisms_get_sasl_server_mechanisms(test_sasl_mechanisms_handle, IGNORED_PTR_ARG))
3804 .SetReturn(1);
3805 STRICT_EXPECTED_CALL(xio_close(test_underlying_io, IGNORED_PTR_ARG, IGNORED_PTR_ARG));
3806 STRICT_EXPECTED_CALL(sasl_mechanisms_destroy(test_sasl_mechanisms_handle));
3807 STRICT_EXPECTED_CALL(test_on_io_open_complete((void*)0x4242, IO_OPEN_ERROR));
3808
3809 // act
3810 saved_on_sasl_frame_received(saved_sasl_frame_codec_callback_context, test_sasl_value);
3811 saved_on_io_close_complete(saved_on_io_close_complete_context);
3812
3813 // assert
3814 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
3815
3816 // cleanup
3817 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
3818 }
3819
3820 /* Tests_SRS_SASLCLIENTIO_01_042: [It is invalid for this list to be null or empty.] */
3821 TEST_FUNCTION(when_an_empty_array_is_received_in_the_SASL_mechanisms_then_the_IO_is_closed_pending_open_complete_with_error)
3822 {
3823 // arrange
3824 SASLCLIENTIO_CONFIG sasl_client_io_config;
3825 CONCRETE_IO_HANDLE sasl_client_io;
3826 uint32_t mechanisms_count = 0;
3827
3828 sasl_client_io_config.underlying_io = test_underlying_io;
3829 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
3830 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
3831
3832 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
3833 saved_on_io_open_complete(saved_on_io_open_complete_context, IO_OPEN_OK);
3834 saved_on_bytes_received(saved_on_bytes_received_context, sasl_header, sizeof(sasl_header));
3835 saved_on_bytes_received(saved_on_bytes_received_context, test_sasl_mechanisms_frame, sizeof(test_sasl_mechanisms_frame));
3836 umock_c_reset_all_calls();
3837
3838 STRICT_EXPECTED_CALL(amqpvalue_get_inplace_descriptor(test_sasl_value));
3839 STRICT_EXPECTED_CALL(is_sasl_mechanisms_type_by_descriptor(test_descriptor_value)).SetReturn(true);
3840 STRICT_EXPECTED_CALL(amqpvalue_get_sasl_mechanisms(test_sasl_value, IGNORED_PTR_ARG))
3841 .CopyOutArgumentBuffer(2, &test_sasl_mechanisms_handle, sizeof(test_sasl_mechanisms_handle));
3842 STRICT_EXPECTED_CALL(sasl_mechanisms_get_sasl_server_mechanisms(test_sasl_mechanisms_handle, IGNORED_PTR_ARG))
3843 .CopyOutArgumentBuffer_sasl_server_mechanisms_value(&test_sasl_server_mechanisms_value, sizeof(test_sasl_server_mechanisms_value));
3844 STRICT_EXPECTED_CALL(amqpvalue_get_array_item_count(test_sasl_server_mechanisms_value, &mechanisms_count))
3845 .CopyOutArgumentBuffer(2, &mechanisms_count, sizeof(mechanisms_count));
3846 STRICT_EXPECTED_CALL(xio_close(test_underlying_io, IGNORED_PTR_ARG, IGNORED_PTR_ARG));
3847 STRICT_EXPECTED_CALL(sasl_mechanisms_destroy(test_sasl_mechanisms_handle));
3848 STRICT_EXPECTED_CALL(test_on_io_open_complete((void*)0x4242, IO_OPEN_ERROR));
3849
3850 // act
3851 saved_on_sasl_frame_received(saved_sasl_frame_codec_callback_context, test_sasl_value);
3852 saved_on_io_close_complete(saved_on_io_close_complete_context);
3853
3854 // assert
3855 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
3856
3857 // cleanup
3858 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
3859 }
3860
3861 /* Tests_SRS_SASLCLIENTIO_01_119: [If any error is encountered when parsing the received frame, the `on_io_open_complete` callback shall be triggered with `IO_OPEN_ERROR`.]*/
3862 TEST_FUNCTION(when_getting_the_mechanisms_array_item_count_fails_then_the_IO_is_closed_pending_open_complete_with_error)
3863 {
3864 // arrange
3865 SASLCLIENTIO_CONFIG sasl_client_io_config;
3866 CONCRETE_IO_HANDLE sasl_client_io;
3867 uint32_t mechanisms_count = 1;
3868
3869 sasl_client_io_config.underlying_io = test_underlying_io;
3870 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
3871 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
3872
3873 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
3874 saved_on_io_open_complete(saved_on_io_open_complete_context, IO_OPEN_OK);
3875 saved_on_bytes_received(saved_on_bytes_received_context, sasl_header, sizeof(sasl_header));
3876 saved_on_bytes_received(saved_on_bytes_received_context, test_sasl_mechanisms_frame, sizeof(test_sasl_mechanisms_frame));
3877 umock_c_reset_all_calls();
3878
3879 STRICT_EXPECTED_CALL(amqpvalue_get_inplace_descriptor(test_sasl_value));
3880 STRICT_EXPECTED_CALL(is_sasl_mechanisms_type_by_descriptor(test_descriptor_value)).SetReturn(true);
3881 STRICT_EXPECTED_CALL(amqpvalue_get_sasl_mechanisms(test_sasl_value, IGNORED_PTR_ARG))
3882 .CopyOutArgumentBuffer(2, &test_sasl_mechanisms_handle, sizeof(test_sasl_mechanisms_handle));
3883 STRICT_EXPECTED_CALL(sasl_mechanisms_get_sasl_server_mechanisms(test_sasl_mechanisms_handle, IGNORED_PTR_ARG))
3884 .CopyOutArgumentBuffer_sasl_server_mechanisms_value(&test_sasl_server_mechanisms_value, sizeof(test_sasl_server_mechanisms_value));
3885 STRICT_EXPECTED_CALL(amqpvalue_get_array_item_count(test_sasl_server_mechanisms_value, &mechanisms_count))
3886 .CopyOutArgumentBuffer(2, &mechanisms_count, sizeof(mechanisms_count))
3887 .SetReturn(1);
3888 STRICT_EXPECTED_CALL(xio_close(test_underlying_io, IGNORED_PTR_ARG, IGNORED_PTR_ARG));
3889 STRICT_EXPECTED_CALL(sasl_mechanisms_destroy(test_sasl_mechanisms_handle));
3890 STRICT_EXPECTED_CALL(test_on_io_open_complete((void*)0x4242, IO_OPEN_ERROR));
3891
3892 // act
3893 saved_on_sasl_frame_received(saved_sasl_frame_codec_callback_context, test_sasl_value);
3894 saved_on_io_close_complete(saved_on_io_close_complete_context);
3895
3896 // assert
3897 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
3898
3899 // cleanup
3900 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
3901 }
3902
3903 /* Tests_SRS_SASLCLIENTIO_01_073: [If the handshake fails (i.e. the outcome is an error) the `on_io_open_complete` callback shall be triggered with `IO_OPEN_ERROR`.]*/
3904 TEST_FUNCTION(when_the_mechanisms_array_does_not_contain_a_usable_SASL_mechanism_then_the_IO_is_closed_pending_open_complete_with_error)
3905 {
3906 // arrange
3907 SASLCLIENTIO_CONFIG sasl_client_io_config;
3908 CONCRETE_IO_HANDLE sasl_client_io;
3909 uint32_t mechanisms_count = 1;
3910 const char* test_sasl_server_mechanism_name = "blahblah";
3911
3912 sasl_client_io_config.underlying_io = test_underlying_io;
3913 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
3914 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
3915
3916 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
3917 saved_on_io_open_complete(saved_on_io_open_complete_context, IO_OPEN_OK);
3918 saved_on_bytes_received(saved_on_bytes_received_context, sasl_header, sizeof(sasl_header));
3919 saved_on_bytes_received(saved_on_bytes_received_context, test_sasl_mechanisms_frame, sizeof(test_sasl_mechanisms_frame));
3920 umock_c_reset_all_calls();
3921
3922 STRICT_EXPECTED_CALL(amqpvalue_get_inplace_descriptor(test_sasl_value));
3923 STRICT_EXPECTED_CALL(is_sasl_mechanisms_type_by_descriptor(test_descriptor_value)).SetReturn(true);
3924 STRICT_EXPECTED_CALL(amqpvalue_get_sasl_mechanisms(test_sasl_value, IGNORED_PTR_ARG))
3925 .CopyOutArgumentBuffer(2, &test_sasl_mechanisms_handle, sizeof(test_sasl_mechanisms_handle));
3926 STRICT_EXPECTED_CALL(sasl_mechanisms_get_sasl_server_mechanisms(test_sasl_mechanisms_handle, IGNORED_PTR_ARG))
3927 .CopyOutArgumentBuffer_sasl_server_mechanisms_value(&test_sasl_server_mechanisms_value, sizeof(test_sasl_server_mechanisms_value));
3928 STRICT_EXPECTED_CALL(amqpvalue_get_array_item_count(test_sasl_server_mechanisms_value, &mechanisms_count))
3929 .CopyOutArgumentBuffer(2, &mechanisms_count, sizeof(mechanisms_count));
3930 STRICT_EXPECTED_CALL(saslmechanism_get_mechanism_name(test_sasl_mechanism));
3931 STRICT_EXPECTED_CALL(amqpvalue_get_array_item(test_sasl_server_mechanisms_value, 0));
3932 STRICT_EXPECTED_CALL(amqpvalue_get_symbol(test_sasl_server_mechanism, IGNORED_PTR_ARG))
3933 .CopyOutArgumentBuffer(2, &test_sasl_server_mechanism_name, sizeof(test_sasl_server_mechanism_name));
3934 STRICT_EXPECTED_CALL(amqpvalue_destroy(test_sasl_server_mechanism));
3935 STRICT_EXPECTED_CALL(xio_close(test_underlying_io, IGNORED_PTR_ARG, IGNORED_PTR_ARG));
3936 STRICT_EXPECTED_CALL(sasl_mechanisms_destroy(test_sasl_mechanisms_handle));
3937 STRICT_EXPECTED_CALL(test_on_io_open_complete((void*)0x4242, IO_OPEN_ERROR));
3938
3939 // act
3940 saved_on_sasl_frame_received(saved_sasl_frame_codec_callback_context, test_sasl_value);
3941 saved_on_io_close_complete(saved_on_io_close_complete_context);
3942
3943 // assert
3944 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
3945
3946 // cleanup
3947 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
3948 }
3949
3950 /* Tests_SRS_SASLCLIENTIO_01_073: [If the handshake fails (i.e. the outcome is an error) the `on_io_open_complete` callback shall be triggered with `IO_OPEN_ERROR`.]*/
3951 TEST_FUNCTION(when_the_mechanisms_array_has_2_mechanisms_and_none_matches_the_IO_is_closed_pending_open_complete_with_error)
3952 {
3953 // arrange
3954 SASLCLIENTIO_CONFIG sasl_client_io_config;
3955 CONCRETE_IO_HANDLE sasl_client_io;
3956 uint32_t mechanisms_count = 2;
3957 const char* test_sasl_server_mechanism_name_1 = "blahblah";
3958 const char* test_sasl_server_mechanism_name_2 = "another_blah";
3959
3960 sasl_client_io_config.underlying_io = test_underlying_io;
3961 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
3962 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
3963
3964 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
3965 saved_on_io_open_complete(saved_on_io_open_complete_context, IO_OPEN_OK);
3966 saved_on_bytes_received(saved_on_bytes_received_context, sasl_header, sizeof(sasl_header));
3967 saved_on_bytes_received(saved_on_bytes_received_context, test_sasl_mechanisms_frame, sizeof(test_sasl_mechanisms_frame));
3968 umock_c_reset_all_calls();
3969
3970 STRICT_EXPECTED_CALL(amqpvalue_get_inplace_descriptor(test_sasl_value));
3971 STRICT_EXPECTED_CALL(is_sasl_mechanisms_type_by_descriptor(test_descriptor_value)).SetReturn(true);
3972 STRICT_EXPECTED_CALL(amqpvalue_get_sasl_mechanisms(test_sasl_value, IGNORED_PTR_ARG))
3973 .CopyOutArgumentBuffer(2, &test_sasl_mechanisms_handle, sizeof(test_sasl_mechanisms_handle));
3974 STRICT_EXPECTED_CALL(sasl_mechanisms_get_sasl_server_mechanisms(test_sasl_mechanisms_handle, IGNORED_PTR_ARG))
3975 .CopyOutArgumentBuffer_sasl_server_mechanisms_value(&test_sasl_server_mechanisms_value, sizeof(test_sasl_server_mechanisms_value));
3976 STRICT_EXPECTED_CALL(amqpvalue_get_array_item_count(test_sasl_server_mechanisms_value, &mechanisms_count))
3977 .CopyOutArgumentBuffer(2, &mechanisms_count, sizeof(mechanisms_count));
3978 STRICT_EXPECTED_CALL(saslmechanism_get_mechanism_name(test_sasl_mechanism));
3979 STRICT_EXPECTED_CALL(amqpvalue_get_array_item(test_sasl_server_mechanisms_value, 0));
3980 STRICT_EXPECTED_CALL(amqpvalue_get_symbol(test_sasl_server_mechanism, IGNORED_PTR_ARG))
3981 .CopyOutArgumentBuffer(2, &test_sasl_server_mechanism_name_1, sizeof(test_sasl_server_mechanism_name_1));
3982 STRICT_EXPECTED_CALL(amqpvalue_destroy(test_sasl_server_mechanism));
3983 STRICT_EXPECTED_CALL(amqpvalue_get_array_item(test_sasl_server_mechanisms_value, 1));
3984 STRICT_EXPECTED_CALL(amqpvalue_get_symbol(test_sasl_server_mechanism, IGNORED_PTR_ARG))
3985 .CopyOutArgumentBuffer(2, &test_sasl_server_mechanism_name_2, sizeof(test_sasl_server_mechanism_name_2));
3986 STRICT_EXPECTED_CALL(amqpvalue_destroy(test_sasl_server_mechanism));
3987 STRICT_EXPECTED_CALL(xio_close(test_underlying_io, IGNORED_PTR_ARG, IGNORED_PTR_ARG));
3988 STRICT_EXPECTED_CALL(sasl_mechanisms_destroy(test_sasl_mechanisms_handle));
3989 STRICT_EXPECTED_CALL(test_on_io_open_complete((void*)0x4242, IO_OPEN_ERROR));
3990
3991 // act
3992 saved_on_sasl_frame_received(saved_sasl_frame_codec_callback_context, test_sasl_value);
3993 saved_on_io_close_complete(saved_on_io_close_complete_context);
3994
3995 // assert
3996 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
3997
3998 // cleanup
3999 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
4000 }
4001
4002 /* Tests_SRS_SASLCLIENTIO_01_120: [When SASL client IO is notified by `sasl_frame_codec` of bytes that have been encoded via the `on_bytes_encoded` callback and SASL client IO is in the state OPENING, SASL client IO shall send these bytes by using `xio_send`.]*/
4003 TEST_FUNCTION(when_encoded_bytes_are_received_they_are_given_to_xio_send)
4004 {
4005 // arrange
4006 SASLCLIENTIO_CONFIG sasl_client_io_config;
4007 CONCRETE_IO_HANDLE sasl_client_io;
4008 unsigned char encoded_bytes[] = { 0x42, 0x43 };
4009 sasl_client_io_config.underlying_io = test_underlying_io;
4010 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
4011 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
4012
4013 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
4014 saved_on_io_open_complete(saved_on_io_open_complete_context, IO_OPEN_OK);
4015 saved_on_bytes_received(saved_on_bytes_received_context, sasl_header, sizeof(sasl_header));
4016 saved_on_bytes_received(saved_on_bytes_received_context, test_sasl_mechanisms_frame, sizeof(test_sasl_mechanisms_frame));
4017 umock_c_reset_all_calls();
4018
4019 setup_send_init();
4020 saved_on_sasl_frame_received(saved_sasl_frame_codec_callback_context, test_sasl_value);
4021 umock_c_reset_all_calls();
4022
4023 STRICT_EXPECTED_CALL(xio_send(test_underlying_io, encoded_bytes, sizeof(encoded_bytes), IGNORED_PTR_ARG, NULL))
4024 .ValidateArgumentBuffer(2, &encoded_bytes, sizeof(encoded_bytes));
4025
4026 // act
4027 saved_on_bytes_encoded(saved_on_bytes_encoded_callback_context, encoded_bytes, sizeof(encoded_bytes), true);
4028
4029 // assert
4030 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
4031
4032 // cleanup
4033 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
4034 }
4035
4036 /* Tests_SRS_SASLCLIENTIO_01_120: [When SASL client IO is notified by `sasl_frame_codec` of bytes that have been encoded via the `on_bytes_encoded` callback and SASL client IO is in the state OPENING, SASL client IO shall send these bytes by using `xio_send`.]*/
4037 TEST_FUNCTION(when_encoded_bytes_are_received_with_encoded_complete_flag_set_to_false_they_are_given_to_xio_send)
4038 {
4039 // arrange
4040 SASLCLIENTIO_CONFIG sasl_client_io_config;
4041 CONCRETE_IO_HANDLE sasl_client_io;
4042 unsigned char encoded_bytes[] = { 0x42, 0x43 };
4043 sasl_client_io_config.underlying_io = test_underlying_io;
4044 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
4045 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
4046
4047 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
4048 saved_on_io_open_complete(saved_on_io_open_complete_context, IO_OPEN_OK);
4049 saved_on_bytes_received(saved_on_bytes_received_context, sasl_header, sizeof(sasl_header));
4050 saved_on_bytes_received(saved_on_bytes_received_context, test_sasl_mechanisms_frame, sizeof(test_sasl_mechanisms_frame));
4051 umock_c_reset_all_calls();
4052
4053 setup_send_init();
4054 saved_on_sasl_frame_received(saved_sasl_frame_codec_callback_context, test_sasl_value);
4055 umock_c_reset_all_calls();
4056
4057 STRICT_EXPECTED_CALL(xio_send(test_underlying_io, encoded_bytes, sizeof(encoded_bytes), IGNORED_PTR_ARG, NULL))
4058 .ValidateArgumentBuffer(2, &encoded_bytes, sizeof(encoded_bytes));
4059
4060 // act
4061 saved_on_bytes_encoded(saved_on_bytes_encoded_callback_context, encoded_bytes, sizeof(encoded_bytes), false);
4062
4063 // assert
4064 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
4065
4066 // cleanup
4067 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
4068 }
4069
4070 /* Tests_SRS_SASLCLIENTIO_01_121: [If `xio_send` fails, the `on_io_error` callback shall be triggered.]*/
4071 TEST_FUNCTION(when_xio_send_fails_when_sending_encoded_bytes_the_IO_is_closed_pending_open_complete_with_error)
4072 {
4073 // arrange
4074 SASLCLIENTIO_CONFIG sasl_client_io_config;
4075 CONCRETE_IO_HANDLE sasl_client_io;
4076 unsigned char encoded_bytes[] = { 0x42, 0x43 };
4077 sasl_client_io_config.underlying_io = test_underlying_io;
4078 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
4079 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
4080
4081 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
4082 saved_on_io_open_complete(saved_on_io_open_complete_context, IO_OPEN_OK);
4083 saved_on_bytes_received(saved_on_bytes_received_context, sasl_header, sizeof(sasl_header));
4084 saved_on_bytes_received(saved_on_bytes_received_context, test_sasl_mechanisms_frame, sizeof(test_sasl_mechanisms_frame));
4085 umock_c_reset_all_calls();
4086
4087 setup_send_init();
4088 saved_on_sasl_frame_received(saved_sasl_frame_codec_callback_context, test_sasl_value);
4089 umock_c_reset_all_calls();
4090
4091 STRICT_EXPECTED_CALL(xio_send(test_underlying_io, encoded_bytes, sizeof(encoded_bytes), IGNORED_PTR_ARG, NULL))
4092 .ValidateArgumentBuffer(2, &encoded_bytes, sizeof(encoded_bytes))
4093 .SetReturn(1);
4094 STRICT_EXPECTED_CALL(xio_close(test_underlying_io, IGNORED_PTR_ARG, IGNORED_PTR_ARG));
4095 STRICT_EXPECTED_CALL(test_on_io_open_complete((void*)0x4242, IO_OPEN_ERROR));
4096
4097 // act
4098 saved_on_bytes_encoded(saved_on_bytes_encoded_callback_context, encoded_bytes, sizeof(encoded_bytes), false);
4099 saved_on_io_close_complete(saved_on_io_close_complete_context);
4100
4101 // assert
4102 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
4103
4104 // cleanup
4105 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
4106 }
4107
4108 /* Tests_SRS_SASLCLIENTIO_01_122: [When `on_frame_codec_error` is called while in the OPENING state the `on_io_open_complete` callback shall be triggered with `IO_OPEN_ERROR`.]*/
4109 TEST_FUNCTION(when_the_frame_codec_triggers_an_error_in_the_OPENING_state_the_IO_is_closed_pending_open_complete_with_error)
4110 {
4111 // arrange
4112 SASLCLIENTIO_CONFIG sasl_client_io_config;
4113 CONCRETE_IO_HANDLE sasl_client_io;
4114 sasl_client_io_config.underlying_io = test_underlying_io;
4115 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
4116 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
4117
4118 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
4119 saved_on_io_open_complete(saved_on_io_open_complete_context, IO_OPEN_OK);
4120 saved_on_bytes_received(saved_on_bytes_received_context, sasl_header, sizeof(sasl_header));
4121 saved_on_bytes_received(saved_on_bytes_received_context, test_sasl_mechanisms_frame, sizeof(test_sasl_mechanisms_frame));
4122 umock_c_reset_all_calls();
4123
4124 STRICT_EXPECTED_CALL(xio_close(test_underlying_io, IGNORED_PTR_ARG, IGNORED_PTR_ARG));
4125 STRICT_EXPECTED_CALL(test_on_io_open_complete((void*)0x4242, IO_OPEN_ERROR));
4126
4127 // act
4128 saved_on_frame_codec_error(saved_on_frame_codec_error_callback_context);
4129 saved_on_io_close_complete(saved_on_io_close_complete_context);
4130
4131 // assert
4132 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
4133
4134 // cleanup
4135 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
4136 }
4137
4138 /* Tests_SRS_SASLCLIENTIO_01_143: [ When `on_frame_codec_error` is called while in the OPEN state the `on_io_error` callback shall be triggered. ]*/
4139 TEST_FUNCTION(when_the_frame_codec_triggers_an_error_in_the_OPEN_state_the_on_io_error_callback_is_triggered)
4140 {
4141 // arrange
4142 SASLCLIENTIO_CONFIG sasl_client_io_config;
4143 CONCRETE_IO_HANDLE sasl_client_io;
4144 sasl_client_io_config.underlying_io = test_underlying_io;
4145 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
4146 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
4147 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
4148 setup_successful_sasl_handshake();
4149 umock_c_reset_all_calls();
4150
4151 STRICT_EXPECTED_CALL(test_on_io_error((void*)0x4244));
4152
4153 // act
4154 saved_on_frame_codec_error(saved_on_frame_codec_error_callback_context);
4155
4156 // assert
4157 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
4158
4159 // cleanup
4160 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
4161 }
4162
4163 /* Tests_SRS_SASLCLIENTIO_01_123: [When `on_frame_codec_error` is called in the ERROR state nothing shall be done.]*/
4164 TEST_FUNCTION(when_the_frame_codec_triggers_an_error_in_the_ERROR_state_nothing_is_done)
4165 {
4166 // arrange
4167 SASLCLIENTIO_CONFIG sasl_client_io_config;
4168 CONCRETE_IO_HANDLE sasl_client_io;
4169 sasl_client_io_config.underlying_io = test_underlying_io;
4170 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
4171 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
4172 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
4173 setup_successful_sasl_handshake();
4174 saved_on_frame_codec_error(saved_sasl_frame_codec_callback_context);
4175 umock_c_reset_all_calls();
4176
4177 // act
4178 saved_on_frame_codec_error(saved_on_frame_codec_error_callback_context);
4179
4180 // assert
4181 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
4182
4183 // cleanup
4184 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
4185 }
4186
4187 /* Tests_SRS_SASLCLIENTIO_01_141: [ When `on_sasl_frame_codec_error` is called while in the OPENING state the `on_io_open_complete` callback shall be triggered with `IO_OPEN_ERROR`. ]*/
4188 TEST_FUNCTION(when_the_sasl_frame_codec_triggers_an_error_in_the_OPENING_state_the_IO_is_closed_pending_open_complete_with_error)
4189 {
4190 // arrange
4191 SASLCLIENTIO_CONFIG sasl_client_io_config;
4192 CONCRETE_IO_HANDLE sasl_client_io;
4193 sasl_client_io_config.underlying_io = test_underlying_io;
4194 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
4195 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
4196
4197 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
4198 saved_on_io_open_complete(saved_on_io_open_complete_context, IO_OPEN_OK);
4199 saved_on_bytes_received(saved_on_bytes_received_context, sasl_header, sizeof(sasl_header));
4200 saved_on_bytes_received(saved_on_bytes_received_context, test_sasl_mechanisms_frame, sizeof(test_sasl_mechanisms_frame));
4201 umock_c_reset_all_calls();
4202
4203 STRICT_EXPECTED_CALL(xio_close(test_underlying_io, IGNORED_PTR_ARG, IGNORED_PTR_ARG));
4204 STRICT_EXPECTED_CALL(test_on_io_open_complete((void*)0x4242, IO_OPEN_ERROR));
4205
4206 // act
4207 saved_on_sasl_frame_codec_error(saved_sasl_frame_codec_callback_context);
4208 saved_on_io_close_complete(saved_on_io_close_complete_context);
4209
4210 // assert
4211 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
4212
4213 // cleanup
4214 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
4215 }
4216
4217 /* Tests_SRS_SASLCLIENTIO_01_144: [ When `on_sasl_frame_codec_error` is called while OPEN state the `on_io_error` callback shall be triggered. ]*/
4218 TEST_FUNCTION(when_the_sasl_frame_codec_triggers_an_error_in_the_OPEN_state_the_saslclientio_state_is_set_to_ERROR)
4219 {
4220 // arrange
4221 SASLCLIENTIO_CONFIG sasl_client_io_config;
4222 CONCRETE_IO_HANDLE sasl_client_io;
4223 sasl_client_io_config.underlying_io = test_underlying_io;
4224 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
4225 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
4226 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
4227 setup_successful_sasl_handshake();
4228 umock_c_reset_all_calls();
4229
4230 STRICT_EXPECTED_CALL(test_on_io_error((void*)0x4244));
4231
4232 // act
4233 saved_on_sasl_frame_codec_error(saved_sasl_frame_codec_callback_context);
4234
4235 // assert
4236 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
4237
4238 // cleanup
4239 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
4240 }
4241
4242 /* Tests_SRS_SASLCLIENTIO_01_142: [ When `on_sasl_frame_codec_error` is called in the ERROR state nothing shall be done. ]*/
4243 TEST_FUNCTION(when_the_sasl_frame_codec_triggers_an_error_in_the_ERROR_state_nothing_is_done)
4244 {
4245 // arrange
4246 SASLCLIENTIO_CONFIG sasl_client_io_config;
4247 CONCRETE_IO_HANDLE sasl_client_io;
4248 sasl_client_io_config.underlying_io = test_underlying_io;
4249 sasl_client_io_config.sasl_mechanism = test_sasl_mechanism;
4250 sasl_client_io = saslclientio_get_interface_description()->concrete_io_create(&sasl_client_io_config);
4251 (void)saslclientio_get_interface_description()->concrete_io_open(sasl_client_io, test_on_io_open_complete, (void*)0x4242, test_on_bytes_received, (void*)0x4243, test_on_io_error, (void*)0x4244);
4252 setup_successful_sasl_handshake();
4253 saved_on_sasl_frame_codec_error(saved_sasl_frame_codec_callback_context);
4254 umock_c_reset_all_calls();
4255
4256 // act
4257 saved_on_sasl_frame_codec_error(saved_sasl_frame_codec_callback_context);
4258
4259 // assert
4260 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
4261
4262 // cleanup
4263 saslclientio_get_interface_description()->concrete_io_destroy(sasl_client_io);
4264 }
4265
4266 END_TEST_SUITE(saslclientio_ut)