Debian Codegraph @

src/vendor/azure-uamqp-c/tests/frame_codec_ut/frame_codec_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 <cstdint>
6 #include <cstdlib>
7 #else
8 #include <stdlib.h>
9 #include <stdint.h>
10 #include <stdbool.h>
11 #endif
12
13 #include "azure_macro_utils/macro_utils.h"
14 #include "testrunnerswitcher.h"
15 #include "umock_c/umock_c.h"
16 #include "umock_c/umocktypes_stdint.h"
17 #include "umock_c/umocktypes_bool.h"
18
19 static void* my_gballoc_malloc(size_t size)
20 {
21 return malloc(size);
22 }
23
24 static void* my_gballoc_calloc(size_t nmemb, size_t size)
25 {
26 return calloc(nmemb, size);
27 }
28
29 static void* my_gballoc_realloc(void* ptr, size_t size)
30 {
31 return realloc(ptr, size);
32 }
33
34 static void my_gballoc_free(void* ptr)
35 {
36 free(ptr);
37 }
38
39 #define ENABLE_MOCKS
40
41 #include "azure_c_shared_utility/gballoc.h"
42 #include "azure_c_shared_utility/singlylinkedlist.h"
43 #include "azure_uamqp_c/amqpvalue.h"
44
45 #undef ENABLE_MOCKS
46
47 #include "azure_uamqp_c/frame_codec.h"
48
49 #define TEST_DESCRIPTION_AMQP_VALUE (AMQP_VALUE)0x4243
50 #define TEST_LIST_HANDLE (SINGLYLINKEDLIST_HANDLE)0x4246
51 #define TEST_SUBSCRIPTION_ITEM (void*)0x4247
52 #define TEST_ERROR_CONTEXT (void*)0x4248
53 #define TEST_LIST_ITEM_HANDLE (LIST_ITEM_HANDLE)0x4249
54
55 typedef struct TEST_LIST_ITEM_TAG
56 {
57 const void* item_value;
58 } TEST_LIST_ITEM;
59
60 static TEST_LIST_ITEM** list_items = NULL;
61 static size_t list_item_count = 0;
62 static unsigned char* sent_io_bytes;
63 static size_t sent_io_byte_count;
64 static char expected_stringified_io[8192];
65 static char actual_stringified_io[8192];
66
67 void stringify_bytes(const unsigned char* bytes, size_t byte_count, char* output_string)
68 {
69 size_t i;
70 size_t pos = 0;
71
72 output_string[pos++] = '[';
73 for (i = 0; i < byte_count; i++)
74 {
75 (void)sprintf(&output_string[pos], "0x%02X", bytes[i]);
76 if (i < byte_count - 1)
77 {
78 strcat(output_string, ",");
79 }
80 pos = strlen(output_string);
81 }
82 output_string[pos++] = ']';
83 output_string[pos++] = '\0';
84 }
85
86 /* frame received callback */
87 MOCK_FUNCTION_WITH_CODE(, void, on_frame_received_1, void*, context, const unsigned char*, type_specific, uint32_t, type_specific_size, const unsigned char*, frame_body, uint32_t, frame_body_size)
88 MOCK_FUNCTION_END();
89 MOCK_FUNCTION_WITH_CODE(, void, on_frame_received_2, void*, context, const unsigned char*, type_specific, uint32_t, type_specific_size, const unsigned char*, frame_body, uint32_t, frame_body_size)
90 MOCK_FUNCTION_END();
91 MOCK_FUNCTION_WITH_CODE(, void, test_frame_codec_decode_error, void*, context)
92 MOCK_FUNCTION_END();
93
94 MOCK_FUNCTION_WITH_CODE(, void, test_on_bytes_encoded, void*, context, const unsigned char*, bytes, size_t, length, bool, encode_complete)
95 unsigned char* new_bytes = (unsigned char*)my_gballoc_realloc(sent_io_bytes, sent_io_byte_count + length);
96 if (new_bytes != NULL)
97 {
98 sent_io_bytes = new_bytes;
99 (void)memcpy(sent_io_bytes + sent_io_byte_count, bytes, length);
100 sent_io_byte_count += length;
101 }
102 MOCK_FUNCTION_END();
103
104 static LIST_ITEM_HANDLE my_singlylinkedlist_add(SINGLYLINKEDLIST_HANDLE list, const void* item)
105 {
106 TEST_LIST_ITEM** items = (TEST_LIST_ITEM**)my_gballoc_realloc(list_items, (list_item_count + 1) * sizeof(TEST_LIST_ITEM*));
107 LIST_ITEM_HANDLE to_return = NULL;
108 (void)list;
109 if (items != NULL)
110 {
111 list_items = items;
112 list_items[list_item_count] = (TEST_LIST_ITEM*)my_gballoc_malloc(sizeof(TEST_LIST_ITEM));
113 if (list_items[list_item_count] != NULL)
114 {
115 list_items[list_item_count]->item_value = item;
116 to_return = (LIST_ITEM_HANDLE)list_items[list_item_count];
117 list_item_count++;
118 }
119 }
120 return to_return;
121 }
122
123 static const void* my_singlylinkedlist_item_get_value(LIST_ITEM_HANDLE item_handle)
124 {
125 return ((TEST_LIST_ITEM*)item_handle)->item_value;
126 }
127
128 static LIST_ITEM_HANDLE my_singlylinkedlist_find(SINGLYLINKEDLIST_HANDLE handle, LIST_MATCH_FUNCTION match_function, const void* match_context)
129 {
130 size_t i;
131 LIST_ITEM_HANDLE found_item = NULL;
132 (void)handle;
133
134 for (i = 0; i < list_item_count; i++)
135 {
136 if (match_function((LIST_ITEM_HANDLE)list_items[i], match_context))
137 {
138 found_item = (LIST_ITEM_HANDLE)list_items[i];
139 break;
140 }
141 }
142 return found_item;
143 }
144
145 static int my_singlylinkedlist_remove(SINGLYLINKEDLIST_HANDLE list, LIST_ITEM_HANDLE list_item)
146 {
147 size_t i;
148 (void)list;
149
150 for (i = 0; i < list_item_count; i++)
151 {
152 if (((LIST_ITEM_HANDLE)list_items[i]) == list_item)
153 {
154 break;
155 }
156 }
157 if (i < list_item_count)
158 {
159 my_gballoc_free(list_items[i]);
160 memmove(&list_items[i], &list_items[i + 1], (list_item_count - i - 1) * sizeof(TEST_LIST_ITEM*));
161 list_item_count--;
162 if (list_item_count == 0)
163 {
164 my_gballoc_free(list_items);
165 list_items = NULL;
166 }
167 }
168 return 0;
169 }
170
171 static TEST_MUTEX_HANDLE g_testByTest;
172
173 MU_DEFINE_ENUM_STRINGS(UMOCK_C_ERROR_CODE, UMOCK_C_ERROR_CODE_VALUES)
174
175 static void on_umock_c_error(UMOCK_C_ERROR_CODE error_code)
176 {
177 ASSERT_FAIL("umock_c reported error :%" PRI_MU_ENUM "", MU_ENUM_VALUE(UMOCK_C_ERROR_CODE, error_code));
178 }
179
180 BEGIN_TEST_SUITE(frame_codec_ut)
181
182 TEST_SUITE_INITIALIZE(suite_init)
183 {
184 int result;
185
186 g_testByTest = TEST_MUTEX_CREATE();
187 ASSERT_IS_NOT_NULL(g_testByTest);
188
189 umock_c_init(on_umock_c_error);
190
191 result = umocktypes_stdint_register_types();
192 ASSERT_ARE_EQUAL(int, 0, result, "Failed registering stdint types");
193 result = umocktypes_bool_register_types();
194 ASSERT_ARE_EQUAL(int, 0, result, "Failed registering bool types");
195
196 REGISTER_GLOBAL_MOCK_HOOK(gballoc_malloc, my_gballoc_malloc);
197 REGISTER_GLOBAL_MOCK_HOOK(gballoc_calloc, my_gballoc_calloc);
198 REGISTER_GLOBAL_MOCK_HOOK(gballoc_free, my_gballoc_free);
199 REGISTER_GLOBAL_MOCK_HOOK(gballoc_realloc, my_gballoc_realloc);
200 REGISTER_GLOBAL_MOCK_RETURN(singlylinkedlist_create, TEST_LIST_HANDLE);
201 REGISTER_GLOBAL_MOCK_HOOK(singlylinkedlist_add, my_singlylinkedlist_add);
202 REGISTER_GLOBAL_MOCK_HOOK(singlylinkedlist_item_get_value, my_singlylinkedlist_item_get_value);
203 REGISTER_GLOBAL_MOCK_HOOK(singlylinkedlist_find, my_singlylinkedlist_find);
204 REGISTER_GLOBAL_MOCK_HOOK(singlylinkedlist_remove, my_singlylinkedlist_remove);
205
206 REGISTER_UMOCK_ALIAS_TYPE(SINGLYLINKEDLIST_HANDLE, void*);
207 REGISTER_UMOCK_ALIAS_TYPE(LIST_MATCH_FUNCTION, void*);
208 REGISTER_UMOCK_ALIAS_TYPE(LIST_ITEM_HANDLE, void*);
209 }
210
211 TEST_SUITE_CLEANUP(suite_cleanup)
212 {
213 umock_c_deinit();
214
215 TEST_MUTEX_DESTROY(g_testByTest);
216 }
217
218 TEST_FUNCTION_INITIALIZE(method_init)
219 {
220 if (TEST_MUTEX_ACQUIRE(g_testByTest))
221 {
222 ASSERT_FAIL("our mutex is ABANDONED. Failure in test framework");
223 }
224
225 umock_c_reset_all_calls();
226 }
227
228 TEST_FUNCTION_CLEANUP(method_cleanup)
229 {
230 size_t i;
231 if (sent_io_bytes != NULL)
232 {
233 free(sent_io_bytes);
234 sent_io_bytes = NULL;
235 }
236 for (i = 0; i < list_item_count; i++)
237 {
238 free(list_items[i]);
239 }
240 free(list_items);
241 list_items = NULL;
242 list_item_count = 0;
243
244 sent_io_byte_count = 0;
245
246 TEST_MUTEX_RELEASE(g_testByTest);
247 }
248
249 /* frame_codec_create */
250
251 /* Tests_SRS_FRAME_CODEC_01_021: [frame_codec_create shall create a new instance of frame_codec and return a non-NULL handle to it on success.] */
252 TEST_FUNCTION(frame_codec_create_with_valid_args_succeeds)
253 {
254 // arrange
255 FRAME_CODEC_HANDLE frame_codec;
256 STRICT_EXPECTED_CALL(gballoc_calloc(IGNORED_NUM_ARG, IGNORED_NUM_ARG));
257 STRICT_EXPECTED_CALL(singlylinkedlist_create());
258
259 // act
260 frame_codec = frame_codec_create(test_frame_codec_decode_error, TEST_ERROR_CONTEXT);
261
262 // assert
263 ASSERT_IS_NOT_NULL(frame_codec);
264 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
265
266 // cleanup
267 frame_codec_destroy(frame_codec);
268 }
269
270 /* Tests_SRS_FRAME_CODEC_01_020: [If the on_frame_codec_error argument is NULL, frame_codec_create shall return NULL.] */
271 TEST_FUNCTION(frame_codec_create_with_NULL_on_error_decode_fails)
272 {
273 // arrange
274
275 // act
276 FRAME_CODEC_HANDLE frame_codec = frame_codec_create(NULL, TEST_ERROR_CONTEXT);
277
278 // assert
279 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
280 ASSERT_IS_NULL(frame_codec);
281 }
282
283 /* Tests_SRS_FRAME_CODEC_01_104: [The callback_context shall be allowed to be NULL.] */
284 TEST_FUNCTION(frame_codec_create_with_NULL_frame_codec_decode_error_calback_context_succeeds)
285 {
286 // arrange
287 FRAME_CODEC_HANDLE frame_codec;
288 STRICT_EXPECTED_CALL(gballoc_calloc(IGNORED_NUM_ARG, IGNORED_NUM_ARG));
289 STRICT_EXPECTED_CALL(singlylinkedlist_create());
290
291 // act
292 frame_codec = frame_codec_create(test_frame_codec_decode_error, NULL);
293
294 // assert
295 ASSERT_IS_NOT_NULL(frame_codec);
296 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
297
298 // cleanup
299 frame_codec_destroy(frame_codec);
300 }
301
302 /* Tests_SRS_FRAME_CODEC_01_022: [If allocating memory for the frame_codec instance fails, frame_codec_create shall return NULL.] */
303 TEST_FUNCTION(when_allocating_memory_for_the_frame_codec_fails_frame_code_create_fails)
304 {
305 // arrange
306 FRAME_CODEC_HANDLE frame_codec;
307 STRICT_EXPECTED_CALL(gballoc_calloc(IGNORED_NUM_ARG, IGNORED_NUM_ARG))
308 .SetReturn(NULL);
309
310 // act
311 frame_codec = frame_codec_create(test_frame_codec_decode_error, TEST_ERROR_CONTEXT);
312
313 // assert
314 ASSERT_IS_NULL(frame_codec);
315 }
316
317 /* Tests_SRS_FRAME_CODEC_01_082: [The initial max_frame_size_shall be 512.] */
318 /* Tests_SRS_FRAME_CODEC_01_095: [If the frame_size needed for the frame is bigger than the maximum frame size, frame_codec_encode_frame shall fail and return a non-zero value.] */
319 TEST_FUNCTION(sending_a_frame_with_more_than_512_bytes_of_total_frame_size_fails_immediately_after_create)
320 {
321 // arrange
322 int result;
323 FRAME_CODEC_HANDLE frame_codec = frame_codec_create(test_frame_codec_decode_error, TEST_ERROR_CONTEXT);
324 unsigned char bytes[505] = { 0 };
325 PAYLOAD payload;
326 payload.bytes = bytes;
327 payload.length = sizeof(bytes);
328 umock_c_reset_all_calls();
329
330 // act
331 result = frame_codec_encode_frame(frame_codec, 0, &payload, 1, NULL, 0, NULL, NULL);
332
333 // assert
334 ASSERT_ARE_NOT_EQUAL(int, 0, result);
335 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
336
337 // cleanup
338 frame_codec_destroy(frame_codec);
339 }
340
341 /* Tests_SRS_FRAME_CODEC_01_082: [The initial max_frame_size_shall be 512.] */
342 /* Tests_SRS_FRAME_CODEC_01_075: [frame_codec_set_max_frame_size shall set the maximum frame size for a frame_codec.] */
343 /* Tests_SRS_FRAME_CODEC_01_088: [Encoded bytes shall be passed to the `on_bytes_encoded` callback in a single call, while setting the `encode complete` argument to true.] */
344 /* Tests_SRS_FRAME_CODEC_01_108: [ Memory shall be allocated to hold the entire frame. ]*/
345 TEST_FUNCTION(a_frame_of_exactly_max_frame_size_immediately_after_create_can_be_sent)
346 {
347 // arrange
348 int result;
349 FRAME_CODEC_HANDLE frame_codec = frame_codec_create(test_frame_codec_decode_error, TEST_ERROR_CONTEXT);
350 unsigned char bytes[504] = { 0 };
351 unsigned char expected_bytes[512] = { 0x00, 0x00, 0x02, 0x00, 0x02, 0x00, 0x00, 0x00 };
352 PAYLOAD payload;
353 payload.bytes = bytes;
354 payload.length = sizeof(bytes);
355 umock_c_reset_all_calls();
356
357 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG));
358 STRICT_EXPECTED_CALL(test_on_bytes_encoded((void*)0x4242, IGNORED_PTR_ARG, IGNORED_NUM_ARG, true));
359 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG));
360
361 // act
362 result = frame_codec_encode_frame(frame_codec, 0, &payload, 1, NULL, 0, test_on_bytes_encoded, (void*)0x4242);
363
364 // assert
365 stringify_bytes(sent_io_bytes, sent_io_byte_count, actual_stringified_io);
366 stringify_bytes(expected_bytes, sizeof(expected_bytes), expected_stringified_io);
367 ASSERT_ARE_EQUAL(char_ptr, expected_stringified_io, actual_stringified_io);
368 ASSERT_ARE_EQUAL(int, 0, result);
369 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
370
371 // cleanup
372 frame_codec_destroy(frame_codec);
373 }
374
375 /* Tests_SRS_FRAME_CODEC_01_082: [The initial max_frame_size_shall be 512.] */
376 /* Tests_SRS_FRAME_CODEC_01_096: [If a frame bigger than the current max frame size is received, frame_codec_receive_bytes shall fail and return a non-zero value.] */
377 /* Tests_SRS_FRAME_CODEC_01_103: [Upon any decode error, if an error callback has been passed to frame_codec_create, then the error callback shall be called with the context argument being the frame_codec_error_callback_context argument passed to frame_codec_create.] */
378 TEST_FUNCTION(receiving_a_frame_with_more_than_512_bytes_of_total_frame_size_immediately_after_create_fails)
379 {
380 // arrange
381 int result;
382 FRAME_CODEC_HANDLE frame_codec = frame_codec_create(test_frame_codec_decode_error, TEST_ERROR_CONTEXT);
383 unsigned char frame[] = { 0x00, 0x00, 0x02, 0x01 };
384 (void)frame_codec_subscribe(frame_codec, 0, on_frame_received_1, frame_codec);
385 umock_c_reset_all_calls();
386
387 STRICT_EXPECTED_CALL(test_frame_codec_decode_error(TEST_ERROR_CONTEXT));
388
389 // act
390 result = frame_codec_receive_bytes(frame_codec, frame, sizeof(frame));
391
392 // assert
393 ASSERT_ARE_NOT_EQUAL(int, 0, result);
394 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
395
396 // cleanup
397 frame_codec_unsubscribe(frame_codec, 0);
398 frame_codec_destroy(frame_codec);
399 }
400
401 /* Tests_SRS_FRAME_CODEC_01_082: [The initial max_frame_size_shall be 512.] */
402 /* Tests_SRS_FRAME_CODEC_01_096: [If a frame bigger than the current max frame size is received, frame_codec_receive_bytes shall fail and return a non-zero value.] */
403 TEST_FUNCTION(receiving_a_frame_with_exactly_512_bytes_of_total_frame_size_immediately_after_create_succeeds)
404 {
405 // arrange
406 int result;
407 FRAME_CODEC_HANDLE frame_codec = frame_codec_create(test_frame_codec_decode_error, TEST_ERROR_CONTEXT);
408 unsigned char frame[512] = { 0x00, 0x00, 0x02, 0x00, 0x02, 0x00 };
409 (void)frame_codec_subscribe(frame_codec, 0, on_frame_received_1, frame_codec);
410 umock_c_reset_all_calls();
411 (void)memset(frame + 6, 0, 506);
412
413 STRICT_EXPECTED_CALL(singlylinkedlist_find(TEST_LIST_HANDLE, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
414 .ValidateArgumentBuffer(3, &frame[5], 1);
415 STRICT_EXPECTED_CALL(singlylinkedlist_item_get_value(IGNORED_PTR_ARG));
416 STRICT_EXPECTED_CALL(singlylinkedlist_item_get_value(IGNORED_PTR_ARG));
417 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG));
418 STRICT_EXPECTED_CALL(on_frame_received_1(frame_codec, IGNORED_PTR_ARG, 2, IGNORED_PTR_ARG, 504))
419 .ValidateArgumentBuffer(2, &frame[6], 2)
420 .ValidateArgumentBuffer(4, &frame[8], 504);
421 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG));
422
423 // act
424 result = frame_codec_receive_bytes(frame_codec, frame, sizeof(frame));
425
426 // assert
427 ASSERT_ARE_EQUAL(int, 0, result);
428 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
429
430 // cleanup
431 (void)frame_codec_unsubscribe(frame_codec, 0);
432 frame_codec_destroy(frame_codec);
433 }
434
435 /* frame_codec_destroy */
436
437 /* Tests_SRS_FRAME_CODEC_01_023: [frame_codec_destroy shall free all resources associated with a frame_codec instance.] */
438 TEST_FUNCTION(frame_codec_destroy_frees_the_memory_for_frame_codec)
439 {
440 // arrange
441 FRAME_CODEC_HANDLE frame_codec = frame_codec_create(test_frame_codec_decode_error, TEST_ERROR_CONTEXT);
442 umock_c_reset_all_calls();
443
444 STRICT_EXPECTED_CALL(singlylinkedlist_destroy(TEST_LIST_HANDLE));
445 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG));
446
447 // act
448 frame_codec_destroy(frame_codec);
449
450 // assert
451 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
452 }
453
454 /* Tests_SRS_FRAME_CODEC_01_024: [If frame_codec is NULL, frame_codec_destroy shall do nothing.] */
455 TEST_FUNCTION(when_frame_codec_is_NULL_frame_codec_destroy_does_nothing)
456 {
457 // arrange
458
459 // act
460 frame_codec_destroy(NULL);
461
462 // assert
463 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
464 }
465
466 /* Tests_SRS_FRAME_CODEC_01_023: [frame_codec_destroy shall free all resources associated with a frame_codec instance.] */
467 TEST_FUNCTION(frame_codec_destroy_while_receiving_type_specific_data_frees_the_type_specific_buffer)
468 {
469 // arrange
470 unsigned char frame[] = { 0x00, 0x00, 0x00, 0x08, 0x02, 0x00, 0x00 };
471 FRAME_CODEC_HANDLE frame_codec = frame_codec_create(test_frame_codec_decode_error, TEST_ERROR_CONTEXT);
472 (void)frame_codec_subscribe(frame_codec, 0, on_frame_received_1, frame_codec);
473 frame_codec_receive_bytes(frame_codec, frame, sizeof(frame));
474 (void)frame_codec_unsubscribe(frame_codec, 0);
475 umock_c_reset_all_calls();
476
477 STRICT_EXPECTED_CALL(singlylinkedlist_destroy(TEST_LIST_HANDLE));
478 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG));
479 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG));
480
481 // act
482 frame_codec_destroy(frame_codec);
483
484 // assert
485 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
486 }
487
488 /* frame_codec_set_max_frame_size */
489
490 /* Tests_SRS_FRAME_CODEC_01_075: [frame_codec_set_max_frame_size shall set the maximum frame size for a frame_codec.] */
491 /* Tests_SRS_FRAME_CODEC_01_076: [On success, frame_codec_set_max_frame_size shall return 0.] */
492 TEST_FUNCTION(frame_codec_set_max_frame_size_with_8_succeeds)
493 {
494 // arrange
495 int result;
496 FRAME_CODEC_HANDLE frame_codec = frame_codec_create(test_frame_codec_decode_error, TEST_ERROR_CONTEXT);
497 umock_c_reset_all_calls();
498
499 // act
500 result = frame_codec_set_max_frame_size(frame_codec, 8);
501
502 // assert
503 ASSERT_ARE_EQUAL(int, 0, result);
504 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
505
506 // cleanup
507 frame_codec_destroy(frame_codec);
508 }
509
510 #if 0
511 /* Tests_SRS_FRAME_CODEC_01_075: [frame_codec_set_max_frame_size shall set the maximum frame size for a frame_codec.] */
512 TEST_FUNCTION(when_a_frame_bigger_than_max_frame_size_is_sent_frame_codec_encode_frame_fails)
513 {
514 // arrange
515 int result;
516 FRAME_CODEC_HANDLE frame_codec = frame_codec_create(test_frame_codec_decode_error, TEST_ERROR_CONTEXT);
517 (void)frame_codec_set_max_frame_size(frame_codec, 1024);
518 umock_c_reset_all_calls();
519
520 // act
521 result = frame_codec_encode_frame(frame_codec, 0, 1017, NULL, 0, NULL, NULL);
522
523 // assert
524 ASSERT_ARE_NOT_EQUAL(int, 0, result);
525 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
526
527 // cleanup
528 frame_codec_destroy(frame_codec);
529 }
530
531 /* Tests_SRS_FRAME_CODEC_01_075: [frame_codec_set_max_frame_size shall set the maximum frame size for a frame_codec.] */
532 TEST_FUNCTION(a_frame_of_exactly_max_frame_size_can_be_sent)
533 {
534 // arrange
535 int result;
536 FRAME_CODEC_HANDLE frame_codec = frame_codec_create(test_frame_codec_decode_error, TEST_ERROR_CONTEXT);
537 (void)frame_codec_set_max_frame_size(frame_codec, 1024);
538 umock_c_reset_all_calls();
539
540 STRICT_EXPECTED_CALL(xio_send(TEST_IO_HANDLE, IGNORED_PTR_ARG, IGNORED_NUM_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
541 .ExpectedAtLeastTimes(1);
542 STRICT_EXPECTED_CALL(xio_send(TEST_IO_HANDLE, IGNORED_PTR_ARG, IGNORED_NUM_ARG, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
543 .IgnoreAllCalls();
544
545 // act
546 result = frame_codec_encode_frame(frame_codec, 0, 1016, NULL, 0, NULL, NULL);
547
548 // assert
549 stringify_bytes(sent_io_bytes, sent_io_byte_count, actual_stringified_io);
550 ASSERT_ARE_EQUAL(char_ptr, "[0x00,0x00,0x04,0x00,0x02,0x00,0x00,0x00]", actual_stringified_io);
551 ASSERT_ARE_EQUAL(int, 0, result);
552 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
553
554 // cleanup
555 frame_codec_destroy(frame_codec);
556 }
557 #endif
558
559 /* Tests_SRS_FRAME_CODEC_01_096: [If a frame bigger than the current max frame size is received, frame_codec_receive_bytes shall fail and return a non-zero value.] */
560 /* Tests_SRS_FRAME_CODEC_01_103: [Upon any decode error, if an error callback has been passed to frame_codec_create, then the error callback shall be called with the context argument being the frame_codec_error_callback_context argument passed to frame_codec_create.] */
561 TEST_FUNCTION(receiving_a_frame_with_more_than_max_frame_size_bytes_of_total_frame_size_fails)
562 {
563 // arrange
564 int result;
565 FRAME_CODEC_HANDLE frame_codec = frame_codec_create(test_frame_codec_decode_error, TEST_ERROR_CONTEXT);
566 unsigned char frame[] = { 0x00, 0x00, 0x04, 0x01 };
567 (void)frame_codec_set_max_frame_size(frame_codec, 1024);
568 (void)frame_codec_subscribe(frame_codec, 0, on_frame_received_1, frame_codec);
569 umock_c_reset_all_calls();
570
571 STRICT_EXPECTED_CALL(test_frame_codec_decode_error(TEST_ERROR_CONTEXT));
572
573 // act
574 result = frame_codec_receive_bytes(frame_codec, frame, sizeof(frame));
575
576 // assert
577 ASSERT_ARE_NOT_EQUAL(int, 0, result);
578 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
579
580 // cleanup
581 (void)frame_codec_unsubscribe(frame_codec, 0);
582 frame_codec_destroy(frame_codec);
583 }
584
585 /* Tests_SRS_FRAME_CODEC_01_096: [If a frame bigger than the current max frame size is received, frame_codec_receive_bytes shall fail and return a non-zero value.] */
586 TEST_FUNCTION(receiving_a_frame_with_exactly_max_frame_size_bytes_of_total_frame_size_fails_succeeds)
587 {
588 // arrange
589 int result;
590 FRAME_CODEC_HANDLE frame_codec = frame_codec_create(test_frame_codec_decode_error, TEST_ERROR_CONTEXT);
591 unsigned char frame[1024] = { 0x00, 0x00, 0x04, 0x00, 0x02, 0x00 };
592 (void)frame_codec_subscribe(frame_codec, 0, on_frame_received_1, frame_codec);
593 (void)frame_codec_set_max_frame_size(frame_codec, 1024);
594 umock_c_reset_all_calls();
595 (void)memset(frame + 6, 0, 1016);
596
597 STRICT_EXPECTED_CALL(singlylinkedlist_find(TEST_LIST_HANDLE, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
598 .ValidateArgumentBuffer(3, &frame[5], 1);
599 STRICT_EXPECTED_CALL(singlylinkedlist_item_get_value(IGNORED_PTR_ARG));
600 STRICT_EXPECTED_CALL(singlylinkedlist_item_get_value(IGNORED_PTR_ARG));
601 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG));
602 STRICT_EXPECTED_CALL(on_frame_received_1(frame_codec, IGNORED_PTR_ARG, 2, IGNORED_PTR_ARG, 1016))
603 .ValidateArgumentBuffer(2, &frame[6], 2)
604 .ValidateArgumentBuffer(4, &frame[8], 1016);
605 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG));
606
607 // act
608 result = frame_codec_receive_bytes(frame_codec, frame, sizeof(frame));
609
610 // assert
611 ASSERT_ARE_EQUAL(int, 0, result);
612 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
613
614 // cleanup
615 (void)frame_codec_unsubscribe(frame_codec, 0);
616 frame_codec_destroy(frame_codec);
617 }
618
619 /* Tests_SRS_FRAME_CODEC_01_077: [If frame_codec is NULL, frame_codec_set_max_frame_size shall return a non-zero value.] */
620 TEST_FUNCTION(when_frame_codec_is_NULL_frame_codec_set_max_frame_size_fails)
621 {
622 // arrange
623
624 // act
625 int result = frame_codec_set_max_frame_size(NULL, 1024);
626
627 // assert
628 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
629 ASSERT_ARE_NOT_EQUAL(int, 0, result);
630 }
631
632 /* Tests_SRS_FRAME_CODEC_01_078: [If max_frame_size is invalid according to the AMQP standard, frame_codec_set_max_frame_size shall return a non-zero value.] */
633 /* Tests_SRS_FRAME_CODEC_01_010: [The frame is malformed if the size is less than the size of the frame header (8 bytes).] */
634 TEST_FUNCTION(when_frame_codec_is_too_small_then_frame_codec_set_max_frame_size_fails)
635 {
636 // arrange
637 int result;
638 FRAME_CODEC_HANDLE frame_codec = frame_codec_create(test_frame_codec_decode_error, TEST_ERROR_CONTEXT);
639 umock_c_reset_all_calls();
640
641 // act
642 result = frame_codec_set_max_frame_size(frame_codec, 7);
643
644 // assert
645 ASSERT_ARE_NOT_EQUAL(int, 0, result);
646 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
647
648 // cleanup
649 frame_codec_destroy(frame_codec);
650 }
651
652 /* Tests_SRS_FRAME_CODEC_01_081: [If a frame being decoded already has a size bigger than the max_frame_size argument then frame_codec_set_max_frame_size shall return a non-zero value and the previous frame size shall be kept.] */
653 TEST_FUNCTION(attempting_to_set_a_max_frame_size_lower_than_the_size_of_the_currently_being_received_frame_fails)
654 {
655 // arrange
656 int result;
657 FRAME_CODEC_HANDLE frame_codec = frame_codec_create(test_frame_codec_decode_error, TEST_ERROR_CONTEXT);
658 unsigned char frame[1024] = { 0x00, 0x00, 0x04, 0x00, 0x02, 0x00 };
659 (void)frame_codec_set_max_frame_size(frame_codec, 1024);
660 (void)memset(frame + 6, 0, 1016);
661
662 (void)frame_codec_receive_bytes(frame_codec, frame, 4);
663 umock_c_reset_all_calls();
664
665 // act
666 result = frame_codec_set_max_frame_size(frame_codec, 8);
667
668 // assert
669 ASSERT_ARE_NOT_EQUAL(int, 0, result);
670 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
671
672 // cleanup
673 frame_codec_destroy(frame_codec);
674 }
675
676 /* Tests_SRS_FRAME_CODEC_01_097: [Setting a frame size on a frame_codec that had a decode error shall fail.] */
677 TEST_FUNCTION(setting_the_max_frame_size_on_a_codec_with_a_decode_error_fails)
678 {
679 // arrange
680 int result;
681 FRAME_CODEC_HANDLE frame_codec = frame_codec_create(test_frame_codec_decode_error, TEST_ERROR_CONTEXT);
682 unsigned char frame[] = { 0x00, 0x00, 0x00, 0x07 };
683
684 (void)frame_codec_receive_bytes(frame_codec, frame, sizeof(frame));
685 umock_c_reset_all_calls();
686
687 // act
688 result = frame_codec_set_max_frame_size(frame_codec, 1024);
689
690 // assert
691 ASSERT_ARE_NOT_EQUAL(int, 0, result);
692 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
693
694 // cleanup
695 frame_codec_destroy(frame_codec);
696 }
697
698 #if 0
699 /* Tests_SRS_FRAME_CODEC_01_097: [Setting a frame size on a frame_codec that had a decode error shall fail.] */
700 TEST_FUNCTION(setting_the_max_frame_size_on_a_codec_with_an_encode_error_fails)
701 {
702 // arrange
703 int result;
704 FRAME_CODEC_HANDLE frame_codec = frame_codec_create(test_frame_codec_decode_error, TEST_ERROR_CONTEXT);
705 unsigned char bytes[] = { 0x42, 0x43 };
706 (void)frame_codec_encode_frame(frame_codec, 0x42, sizeof(bytes), NULL, 0, NULL, NULL);
707 umock_c_reset_all_calls();
708
709 STRICT_EXPECTED_CALL(xio_send(TEST_IO_HANDLE, IGNORED_PTR_ARG, sizeof(bytes), IGNORED_PTR_ARG, IGNORED_PTR_ARG))
710 .ValidateArgumentBuffer(2, &bytes, sizeof(bytes))
711 .SetReturn(1);
712
713 (void)frame_codec_encode_frame_bytes(frame_codec, bytes, sizeof(bytes));
714
715 // act
716 result = frame_codec_set_max_frame_size(frame_codec, 1024);
717
718 // assert
719 ASSERT_ARE_NOT_EQUAL(int, 0, result);
720 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
721
722 // cleanup
723 frame_codec_destroy(frame_codec);
724 }
725 #endif
726
727 /* Tests_SRS_FRAME_CODEC_01_079: [The new frame size shall take effect immediately, even for a frame that is being decoded at the time of the call.] */
728 /* Tests_SRS_FRAME_CODEC_01_103: [Upon any decode error, if an error callback has been passed to frame_codec_create, then the error callback shall be called with the context argument being the frame_codec_error_callback_context argument passed to frame_codec_create.] */
729 TEST_FUNCTION(setting_a_new_max_frame_while_the_frame_size_is_being_received_makes_the_new_frame_size_be_in_effect)
730 {
731 // arrange
732 int result;
733 FRAME_CODEC_HANDLE frame_codec = frame_codec_create(test_frame_codec_decode_error, TEST_ERROR_CONTEXT);
734 unsigned char frame[] = { 0x00, 0x00, 0x00, 0x09, 0x02, 0x00, 0x00, 0x00, 0x00 };
735
736 (void)frame_codec_receive_bytes(frame_codec, frame, 3);
737 umock_c_reset_all_calls();
738
739 (void)frame_codec_set_max_frame_size(frame_codec, 8);
740
741 STRICT_EXPECTED_CALL(test_frame_codec_decode_error(TEST_ERROR_CONTEXT));
742
743 // act
744 result = frame_codec_receive_bytes(frame_codec, frame + 3, sizeof(frame) - 3);
745
746 // assert
747 ASSERT_ARE_NOT_EQUAL(int, 0, result);
748 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
749
750 // cleanup
751 frame_codec_destroy(frame_codec);
752 }
753
754 /* frame_codec_receive_bytes */
755
756 /* Tests_SRS_FRAME_CODEC_01_025: [frame_codec_receive_bytes decodes a sequence of bytes into frames and on success it shall return zero.] */
757 /* Tests_SRS_FRAME_CODEC_01_028: [The sequence of bytes shall be decoded according to the AMQP ISO.] */
758 /* Tests_SRS_FRAME_CODEC_01_031: [When a complete frame is successfully decoded it shall be indicated to the upper layer by invoking the on_frame_received passed to frame_codec_subscribe.] */
759 /* Tests_SRS_FRAME_CODEC_01_032: [Besides passing the frame information, the callback_context value passed to frame_codec_subscribe shall be passed to the on_frame_received_1 function.] */
760 /* Tests_SRS_FRAME_CODEC_01_001: [Frames are divided into three distinct areas: a fixed width frame header, a variable width extended header, and a variable width frame body.] */
761 /* Tests_SRS_FRAME_CODEC_01_002: [frame header The frame header is a fixed size (8 byte) structure that precedes each frame.] */
762 /* Tests_SRS_FRAME_CODEC_01_003: [The frame header includes mandatory information necessary to parse the rest of the frame including size and type information.] */
763 /* Tests_SRS_FRAME_CODEC_01_004: [extended header The extended header is a variable width area preceding the frame body.] */
764 /* Tests_SRS_FRAME_CODEC_01_007: [frame body The frame body is a variable width sequence of bytes the format of which depends on the frame type.] */
765 /* Tests_SRS_FRAME_CODEC_01_008: [SIZE Bytes 0-3 of the frame header contain the frame size.] */
766 /* Tests_SRS_FRAME_CODEC_01_009: [This is an unsigned 32-bit integer that MUST contain the total frame size of the frame header, extended header, and frame body.] */
767 /* Tests_SRS_FRAME_CODEC_01_011: [DOFF Byte 4 of the frame header is the data offset.] */
768 /* Tests_SRS_FRAME_CODEC_01_012: [This gives the position of the body within the frame.] */
769 /* Tests_SRS_FRAME_CODEC_01_013: [The value of the data offset is an unsigned, 8-bit integer specifying a count of 4-byte words.] */
770 /* Tests_SRS_FRAME_CODEC_01_015: [TYPE Byte 5 of the frame header is a type code.] */
771 /* Tests_SRS_FRAME_CODEC_01_035: [After successfully registering a callback for a certain frame type, when subsequently that frame type is received the callbacks shall be invoked, passing to it the received frame and the callback_context value. */
772 /* Tests_SRS_FRAME_CODEC_01_100: [If the frame body size is 0, the frame_body pointer passed to on_frame_received shall be NULL.] */
773 TEST_FUNCTION(frame_codec_receive_bytes_decodes_one_empty_frame)
774 {
775 // arrange
776 int result;
777 FRAME_CODEC_HANDLE frame_codec = frame_codec_create(test_frame_codec_decode_error, TEST_ERROR_CONTEXT);
778 unsigned char frame[] = { 0x00, 0x00, 0x00, 0x08, 0x02, 0x00, 0x00, 0x00 };
779 (void)frame_codec_subscribe(frame_codec, 0, on_frame_received_1, frame_codec);
780 umock_c_reset_all_calls();
781
782 STRICT_EXPECTED_CALL(singlylinkedlist_find(TEST_LIST_HANDLE, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
783 .ValidateArgumentBuffer(3, &frame[5], 1);
784 STRICT_EXPECTED_CALL(singlylinkedlist_item_get_value(IGNORED_PTR_ARG));
785 STRICT_EXPECTED_CALL(singlylinkedlist_item_get_value(IGNORED_PTR_ARG));
786 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG));
787 STRICT_EXPECTED_CALL(on_frame_received_1(frame_codec, IGNORED_PTR_ARG, 2, IGNORED_PTR_ARG, 0))
788 .ValidateArgumentBuffer(2, &frame[6], 2);
789 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG));
790
791 // act
792 result = frame_codec_receive_bytes(frame_codec, frame, sizeof(frame));
793
794 // assert
795 ASSERT_ARE_EQUAL(int, 0, result);
796 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
797
798 // cleanup
799 (void)frame_codec_unsubscribe(frame_codec, 0);
800 frame_codec_destroy(frame_codec);
801 }
802
803 /* Tests_SRS_FRAME_CODEC_01_025: [frame_codec_receive_bytes decodes a sequence of bytes into frames and on success it shall return zero.] */
804 /* Tests_SRS_FRAME_CODEC_01_029: [The sequence of bytes does not have to be a complete frame, frame_codec shall be responsible for maintaining decoding state between frame_codec_receive_bytes calls.] */
805 TEST_FUNCTION(frame_codec_receive_bytes_with_not_enough_bytes_for_a_frame_does_not_trigger_callback)
806 {
807 // arrange
808 int result;
809 FRAME_CODEC_HANDLE frame_codec = frame_codec_create(test_frame_codec_decode_error, TEST_ERROR_CONTEXT);
810 unsigned char frame[] = { 0x00, 0x00, 0x00, 0x08, 0x02, 0x00, 0x00 };
811 (void)frame_codec_subscribe(frame_codec, 0, on_frame_received_1, frame_codec);
812 umock_c_reset_all_calls();
813
814 STRICT_EXPECTED_CALL(singlylinkedlist_find(TEST_LIST_HANDLE, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
815 .ValidateArgumentBuffer(3, &frame[5], 1);
816 STRICT_EXPECTED_CALL(singlylinkedlist_item_get_value(IGNORED_PTR_ARG));
817 STRICT_EXPECTED_CALL(singlylinkedlist_item_get_value(IGNORED_PTR_ARG));
818 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG));
819
820 // act
821 result = frame_codec_receive_bytes(frame_codec, frame, sizeof(frame));
822
823 // assert
824 ASSERT_ARE_EQUAL(int, 0, result);
825 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
826
827 // cleanup
828 (void)frame_codec_unsubscribe(frame_codec, 0);
829 frame_codec_destroy(frame_codec);
830 }
831
832 /* Tests_SRS_FRAME_CODEC_01_026: [If frame_codec or buffer are NULL, frame_codec_receive_bytes shall return a non-zero value.] */
833 TEST_FUNCTION(frame_codec_receive_bytes_with_NULL_frame_codec_handle_fails)
834 {
835 // arrange
836 unsigned char frame[] = { 0x00, 0x00, 0x00, 0x08, 0x02, 0x00, 0x00, 0x00 };
837
838 // act
839 int result = frame_codec_receive_bytes(NULL, frame, sizeof(frame));
840
841 // assert
842 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
843 ASSERT_ARE_NOT_EQUAL(int, 0, result);
844 }
845
846 /* Tests_SRS_FRAME_CODEC_01_026: [If frame_codec or buffer are NULL, frame_codec_receive_bytes shall return a non-zero value.] */
847 TEST_FUNCTION(frame_codec_receive_bytes_with_NULL_buffer_fails)
848 {
849 // arrange
850 int result;
851 FRAME_CODEC_HANDLE frame_codec = frame_codec_create(test_frame_codec_decode_error, TEST_ERROR_CONTEXT);
852 (void)frame_codec_subscribe(frame_codec, 0, on_frame_received_1, frame_codec);
853 umock_c_reset_all_calls();
854
855 // act
856 result = frame_codec_receive_bytes(frame_codec, NULL, 1);
857
858 // assert
859 ASSERT_ARE_NOT_EQUAL(int, 0, result);
860 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
861
862 // cleanup
863 (void)frame_codec_unsubscribe(frame_codec, 0);
864 frame_codec_destroy(frame_codec);
865 }
866
867 /* Tests_SRS_FRAME_CODEC_01_027: [If size is zero, frame_codec_receive_bytes shall return a non-zero value.] */
868 TEST_FUNCTION(frame_codec_receive_bytes_with_zero_size_fails)
869 {
870 // arrange
871 int result;
872 FRAME_CODEC_HANDLE frame_codec = frame_codec_create(test_frame_codec_decode_error, TEST_ERROR_CONTEXT);
873 unsigned char frame[] = { 0x00, 0x00, 0x00, 0x08, 0x02, 0x00, 0x00, 0x00 };
874 (void)frame_codec_subscribe(frame_codec, 0, on_frame_received_1, frame_codec);
875 umock_c_reset_all_calls();
876
877 // act
878 result = frame_codec_receive_bytes(frame_codec, frame, 0);
879
880 // assert
881 ASSERT_ARE_NOT_EQUAL(int, 0, result);
882 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
883
884 // cleanup
885 (void)frame_codec_unsubscribe(frame_codec, 0);
886 frame_codec_destroy(frame_codec);
887 }
888
889 /* Tests_SRS_FRAME_CODEC_01_029: [The sequence of bytes does not have to be a complete frame, frame_codec shall be responsible for maintaining decoding state between frame_codec_receive_bytes calls.] */
890 /* Codes_SRS_FRAME_CODEC_01_005: [This is an extension point defined for future expansion.] */
891 /* Codes_SRS_FRAME_CODEC_01_006: [The treatment of this area depends on the frame type.] */
892 TEST_FUNCTION(when_frame_codec_receive_1_byte_in_one_call_and_the_rest_of_the_frame_in_another_call_yields_succesfull_decode)
893 {
894 // arrange
895 int result;
896 FRAME_CODEC_HANDLE frame_codec = frame_codec_create(test_frame_codec_decode_error, TEST_ERROR_CONTEXT);
897 unsigned char frame[] = { 0x00, 0x00, 0x00, 0x08, 0x02, 0x00, 0x00, 0x00 };
898 (void)frame_codec_subscribe(frame_codec, 0, on_frame_received_1, frame_codec);
899 umock_c_reset_all_calls();
900
901 STRICT_EXPECTED_CALL(singlylinkedlist_find(TEST_LIST_HANDLE, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
902 .ValidateArgumentBuffer(3, &frame[5], 1);
903 STRICT_EXPECTED_CALL(singlylinkedlist_item_get_value(IGNORED_PTR_ARG));
904 STRICT_EXPECTED_CALL(singlylinkedlist_item_get_value(IGNORED_PTR_ARG));
905 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG));
906 STRICT_EXPECTED_CALL(on_frame_received_1(frame_codec, IGNORED_PTR_ARG, 2, IGNORED_PTR_ARG, 0))
907 .ValidateArgumentBuffer(2, &frame[6], 2);
908 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG));
909
910 (void)frame_codec_receive_bytes(frame_codec, frame, 1);
911
912 // act
913 result = frame_codec_receive_bytes(frame_codec, frame + 1, sizeof(frame) - 1);
914
915 // assert
916 ASSERT_ARE_EQUAL(int, 0, result);
917 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
918
919 // cleanup
920 (void)frame_codec_unsubscribe(frame_codec, 0);
921 frame_codec_destroy(frame_codec);
922 }
923
924 /* Tests_SRS_FRAME_CODEC_01_029: [The sequence of bytes does not have to be a complete frame, frame_codec shall be responsible for maintaining decoding state between frame_codec_receive_bytes calls.] */
925 TEST_FUNCTION(when_frame_codec_receive_the_frame_bytes_in_1_byte_per_call_a_succesfull_decode_happens)
926 {
927 // arrange
928 int result;
929 FRAME_CODEC_HANDLE frame_codec = frame_codec_create(test_frame_codec_decode_error, TEST_ERROR_CONTEXT);
930 unsigned char frame[] = { 0x00, 0x00, 0x00, 0x08, 0x02, 0x00, 0x00, 0x00 };
931 size_t i;
932 (void)frame_codec_subscribe(frame_codec, 0, on_frame_received_1, frame_codec);
933 umock_c_reset_all_calls();
934
935 STRICT_EXPECTED_CALL(singlylinkedlist_find(TEST_LIST_HANDLE, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
936 .ValidateArgumentBuffer(3, &frame[5], 1);
937 STRICT_EXPECTED_CALL(singlylinkedlist_item_get_value(IGNORED_PTR_ARG));
938 STRICT_EXPECTED_CALL(singlylinkedlist_item_get_value(IGNORED_PTR_ARG));
939 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG));
940 STRICT_EXPECTED_CALL(on_frame_received_1(frame_codec, IGNORED_PTR_ARG, 2, IGNORED_PTR_ARG, 0))
941 .ValidateArgumentBuffer(2, &frame[6], 2);
942 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG));
943
944 for (i = 0; i < sizeof(frame) - 1; i++)
945 {
946 (void)frame_codec_receive_bytes(frame_codec, &frame[i], 1);
947 }
948
949 // act
950 result = frame_codec_receive_bytes(frame_codec, &frame[sizeof(frame) - 1], 1);
951
952 // assert
953 ASSERT_ARE_EQUAL(int, 0, result);
954 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
955
956 // cleanup
957 (void)frame_codec_unsubscribe(frame_codec, 0);
958 frame_codec_destroy(frame_codec);
959 }
960
961 /* Tests_SRS_FRAME_CODEC_01_029: [The sequence of bytes does not have to be a complete frame, frame_codec shall be responsible for maintaining decoding state between frame_codec_receive_bytes calls.] */
962 TEST_FUNCTION(a_frame_codec_receive_bytes_call_with_bad_args_before_any_real_frame_bytes_does_not_affect_decoding)
963 {
964 // arrange
965 int result;
966 FRAME_CODEC_HANDLE frame_codec = frame_codec_create(test_frame_codec_decode_error, TEST_ERROR_CONTEXT);
967 unsigned char frame[] = { 0x00, 0x00, 0x00, 0x08, 0x02, 0x00, 0x00, 0x00 };
968 (void)frame_codec_subscribe(frame_codec, 0, on_frame_received_1, frame_codec);
969 umock_c_reset_all_calls();
970
971 STRICT_EXPECTED_CALL(singlylinkedlist_find(TEST_LIST_HANDLE, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
972 .ValidateArgumentBuffer(3, &frame[5], 1);
973 STRICT_EXPECTED_CALL(singlylinkedlist_item_get_value(IGNORED_PTR_ARG));
974 STRICT_EXPECTED_CALL(singlylinkedlist_item_get_value(IGNORED_PTR_ARG));
975 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG));
976 STRICT_EXPECTED_CALL(on_frame_received_1(frame_codec, IGNORED_PTR_ARG, 2, IGNORED_PTR_ARG, 0))
977 .ValidateArgumentBuffer(2, &frame[6], 2);
978 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG));
979
980 (void)frame_codec_receive_bytes(frame_codec, NULL, 1);
981
982 // act
983 result = frame_codec_receive_bytes(frame_codec, frame, sizeof(frame));
984
985 // assert
986 ASSERT_ARE_EQUAL(int, 0, result);
987 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
988
989 // cleanup
990 (void)frame_codec_unsubscribe(frame_codec, 0);
991 frame_codec_destroy(frame_codec);
992 }
993
994 /* Tests_SRS_FRAME_CODEC_01_029: [The sequence of bytes does not have to be a complete frame, frame_codec shall be responsible for maintaining decoding state between frame_codec_receive_bytes calls.] */
995 TEST_FUNCTION(a_frame_codec_receive_bytes_call_with_bad_args_in_the_middle_of_the_frame_does_not_affect_decoding)
996 {
997 // arrange
998 int result;
999 FRAME_CODEC_HANDLE frame_codec = frame_codec_create(test_frame_codec_decode_error, TEST_ERROR_CONTEXT);
1000 unsigned char frame[] = { 0x00, 0x00, 0x00, 0x08, 0x02, 0x00, 0x00, 0x00 };
1001 (void)frame_codec_subscribe(frame_codec, 0, on_frame_received_1, frame_codec);
1002 umock_c_reset_all_calls();
1003
1004 STRICT_EXPECTED_CALL(singlylinkedlist_find(TEST_LIST_HANDLE, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
1005 .ValidateArgumentBuffer(3, &frame[5], 1);
1006 STRICT_EXPECTED_CALL(singlylinkedlist_item_get_value(IGNORED_PTR_ARG));
1007 STRICT_EXPECTED_CALL(singlylinkedlist_item_get_value(IGNORED_PTR_ARG));
1008 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG));
1009 STRICT_EXPECTED_CALL(on_frame_received_1(frame_codec, IGNORED_PTR_ARG, 2, IGNORED_PTR_ARG, 0))
1010 .ValidateArgumentBuffer(2, &frame[6], 2);
1011 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG));
1012
1013 (void)frame_codec_receive_bytes(frame_codec, frame, 1);
1014 (void)frame_codec_receive_bytes(frame_codec, NULL, 1);
1015
1016 // act
1017 result = frame_codec_receive_bytes(frame_codec, frame + 1, sizeof(frame) - 1);
1018
1019 // assert
1020 ASSERT_ARE_EQUAL(int, 0, result);
1021 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1022
1023 // cleanup
1024 (void)frame_codec_unsubscribe(frame_codec, 0);
1025 frame_codec_destroy(frame_codec);
1026 }
1027
1028 /* Tests_SRS_FRAME_CODEC_01_025: [frame_codec_receive_bytes decodes a sequence of bytes into frames and on success it shall return zero.] */
1029 TEST_FUNCTION(frame_codec_receive_bytes_decodes_2_empty_frames)
1030 {
1031 // arrange
1032 int result;
1033 FRAME_CODEC_HANDLE frame_codec = frame_codec_create(test_frame_codec_decode_error, TEST_ERROR_CONTEXT);
1034 unsigned char frame1[] = { 0x00, 0x00, 0x00, 0x08, 0x02, 0x00, 0x01, 0x02 };
1035 unsigned char frame2[] = { 0x00, 0x00, 0x00, 0x08, 0x02, 0x00, 0x03, 0x04 };
1036 (void)frame_codec_subscribe(frame_codec, 0, on_frame_received_1, frame_codec);
1037 umock_c_reset_all_calls();
1038
1039 STRICT_EXPECTED_CALL(singlylinkedlist_find(TEST_LIST_HANDLE, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
1040 .ValidateArgumentBuffer(3, &frame1[5], 1);
1041 STRICT_EXPECTED_CALL(singlylinkedlist_item_get_value(IGNORED_PTR_ARG));
1042 STRICT_EXPECTED_CALL(singlylinkedlist_item_get_value(IGNORED_PTR_ARG));
1043 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG));
1044 STRICT_EXPECTED_CALL(on_frame_received_1(frame_codec, IGNORED_PTR_ARG, 2, IGNORED_PTR_ARG, 0))
1045 .ValidateArgumentBuffer(2, &frame1[6], 2);
1046 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG));
1047 STRICT_EXPECTED_CALL(singlylinkedlist_find(TEST_LIST_HANDLE, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
1048 .ValidateArgumentBuffer(3, &frame2[5], 1);
1049 STRICT_EXPECTED_CALL(singlylinkedlist_item_get_value(IGNORED_PTR_ARG));
1050 STRICT_EXPECTED_CALL(singlylinkedlist_item_get_value(IGNORED_PTR_ARG));
1051 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG));
1052 STRICT_EXPECTED_CALL(on_frame_received_1(frame_codec, IGNORED_PTR_ARG, 2, IGNORED_PTR_ARG, 0))
1053 .ValidateArgumentBuffer(2, &frame2[6], 2);
1054 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG));
1055
1056 (void)frame_codec_receive_bytes(frame_codec, frame1, sizeof(frame1));
1057
1058 // act
1059 result = frame_codec_receive_bytes(frame_codec, frame2, sizeof(frame2));
1060
1061 // assert
1062 ASSERT_ARE_EQUAL(int, 0, result);
1063 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1064
1065 // cleanup
1066 (void)frame_codec_unsubscribe(frame_codec, 0);
1067 frame_codec_destroy(frame_codec);
1068 }
1069
1070 /* Tests_SRS_FRAME_CODEC_01_025: [frame_codec_receive_bytes decodes a sequence of bytes into frames and on success it shall return zero.] */
1071 TEST_FUNCTION(a_call_to_frame_codec_receive_bytes_with_bad_args_between_2_frames_does_not_affect_decoding)
1072 {
1073 // arrange
1074 int result;
1075 FRAME_CODEC_HANDLE frame_codec = frame_codec_create(test_frame_codec_decode_error, TEST_ERROR_CONTEXT);
1076 unsigned char frame1[] = { 0x00, 0x00, 0x00, 0x08, 0x02, 0x00, 0x01, 0x02 };
1077 unsigned char frame2[] = { 0x00, 0x00, 0x00, 0x08, 0x02, 0x00, 0x03, 0x04 };
1078 (void)frame_codec_subscribe(frame_codec, 0, on_frame_received_1, frame_codec);
1079 umock_c_reset_all_calls();
1080
1081 STRICT_EXPECTED_CALL(singlylinkedlist_find(TEST_LIST_HANDLE, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
1082 .ValidateArgumentBuffer(3, &frame1[5], 1);
1083 STRICT_EXPECTED_CALL(singlylinkedlist_item_get_value(IGNORED_PTR_ARG));
1084 STRICT_EXPECTED_CALL(singlylinkedlist_item_get_value(IGNORED_PTR_ARG));
1085 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG));
1086 STRICT_EXPECTED_CALL(on_frame_received_1(frame_codec, IGNORED_PTR_ARG, 2, IGNORED_PTR_ARG, 0))
1087 .ValidateArgumentBuffer(2, &frame1[6], 2);
1088 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG));
1089 STRICT_EXPECTED_CALL(singlylinkedlist_find(TEST_LIST_HANDLE, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
1090 .ValidateArgumentBuffer(3, &frame2[5], 1);
1091 STRICT_EXPECTED_CALL(singlylinkedlist_item_get_value(IGNORED_PTR_ARG));
1092 STRICT_EXPECTED_CALL(singlylinkedlist_item_get_value(IGNORED_PTR_ARG));
1093 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG));
1094 STRICT_EXPECTED_CALL(on_frame_received_1(frame_codec, IGNORED_PTR_ARG, 2, IGNORED_PTR_ARG, 0))
1095 .ValidateArgumentBuffer(2, &frame2[6], 2);
1096 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG));
1097
1098 (void)frame_codec_receive_bytes(frame_codec, frame1, sizeof(frame1));
1099 (void)frame_codec_receive_bytes(frame_codec, NULL, 1);
1100
1101 // act
1102 result = frame_codec_receive_bytes(frame_codec, frame2, sizeof(frame2));
1103
1104 // assert
1105 ASSERT_ARE_EQUAL(int, 0, result);
1106 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1107
1108 // cleanup
1109 (void)frame_codec_unsubscribe(frame_codec, 0);
1110 frame_codec_destroy(frame_codec);
1111 }
1112
1113 /* Tests_SRS_FRAME_CODEC_01_025: [frame_codec_receive_bytes decodes a sequence of bytes into frames and on success it shall return zero.] */
1114 TEST_FUNCTION(when_getting_the_list_item_value_fails_no_callback_is_invoked)
1115 {
1116 // arrange
1117 int result;
1118 FRAME_CODEC_HANDLE frame_codec = frame_codec_create(test_frame_codec_decode_error, TEST_ERROR_CONTEXT);
1119 unsigned char frame[] = { 0x00, 0x00, 0x00, 0x08, 0x02, 0x00, 0x01, 0x02 };
1120 (void)frame_codec_subscribe(frame_codec, 0, on_frame_received_1, frame_codec);
1121 umock_c_reset_all_calls();
1122
1123 STRICT_EXPECTED_CALL(singlylinkedlist_find(TEST_LIST_HANDLE, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
1124 .ValidateArgumentBuffer(3, &frame[5], 1);
1125 STRICT_EXPECTED_CALL(singlylinkedlist_item_get_value(IGNORED_PTR_ARG))
1126 .SetReturn(NULL);
1127
1128 // act
1129 result = frame_codec_receive_bytes(frame_codec, frame, sizeof(frame));
1130
1131 // assert
1132 ASSERT_ARE_EQUAL(int, 0, result);
1133 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1134
1135 // cleanup
1136 (void)frame_codec_unsubscribe(frame_codec, 0);
1137 frame_codec_destroy(frame_codec);
1138 }
1139
1140 /* Tests_SRS_FRAME_CODEC_01_010: [The frame is malformed if the size is less than the size of the frame header (8 bytes).] */
1141 /* Tests_SRS_FRAME_CODEC_01_103: [Upon any decode error, if an error callback has been passed to frame_codec_create, then the error callback shall be called with the context argument being the frame_codec_error_callback_context argument passed to frame_codec_create.] */
1142 TEST_FUNCTION(when_frame_size_is_bad_frame_codec_receive_bytes_fails)
1143 {
1144 // arrange
1145 int result;
1146 FRAME_CODEC_HANDLE frame_codec = frame_codec_create(test_frame_codec_decode_error, TEST_ERROR_CONTEXT);
1147 unsigned char frame[] = { 0x00, 0x00, 0x00, 0x07, 0x02, 0x00, 0x01, 0x02 };
1148 (void)frame_codec_subscribe(frame_codec, 0, on_frame_received_1, frame_codec);
1149 umock_c_reset_all_calls();
1150
1151 STRICT_EXPECTED_CALL(test_frame_codec_decode_error(TEST_ERROR_CONTEXT));
1152
1153 // act
1154 result = frame_codec_receive_bytes(frame_codec, frame, sizeof(frame));
1155
1156 // assert
1157 ASSERT_ARE_NOT_EQUAL(int, 0, result);
1158 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1159
1160 // cleanup
1161 (void)frame_codec_unsubscribe(frame_codec, 0);
1162 frame_codec_destroy(frame_codec);
1163 }
1164
1165 /* Tests_SRS_FRAME_CODEC_01_014: [Due to the mandatory 8-byte frame header, the frame is malformed if the value is less than 2.] */
1166 /* Tests_SRS_FRAME_CODEC_01_103: [Upon any decode error, if an error callback has been passed to frame_codec_create, then the error callback shall be called with the context argument being the frame_codec_error_callback_context argument passed to frame_codec_create.] */
1167 TEST_FUNCTION(when_frame_size_has_a_bad_doff_frame_codec_receive_bytes_fails)
1168 {
1169 // arrange
1170 int result;
1171 FRAME_CODEC_HANDLE frame_codec = frame_codec_create(test_frame_codec_decode_error, TEST_ERROR_CONTEXT);
1172 unsigned char frame[] = { 0x00, 0x00, 0x00, 0x08, 0x01, 0x00, 0x01, 0x02 };
1173 (void)frame_codec_subscribe(frame_codec, 0, on_frame_received_1, frame_codec);
1174 umock_c_reset_all_calls();
1175
1176 STRICT_EXPECTED_CALL(test_frame_codec_decode_error(TEST_ERROR_CONTEXT));
1177
1178 // act
1179 result = frame_codec_receive_bytes(frame_codec, frame, sizeof(frame));
1180
1181 // assert
1182 ASSERT_ARE_NOT_EQUAL(int, 0, result);
1183 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1184
1185 // cleanup
1186 (void)frame_codec_unsubscribe(frame_codec, 0);
1187 frame_codec_destroy(frame_codec);
1188 }
1189
1190 /* Tests_SRS_FRAME_CODEC_01_074: [If a decoding error is detected, any subsequent calls on frame_codec_receive_bytes shall fail.] */
1191 TEST_FUNCTION(after_a_frame_decode_error_occurs_due_to_frame_size_a_subsequent_decode_fails)
1192 {
1193 // arrange
1194 int result;
1195 FRAME_CODEC_HANDLE frame_codec = frame_codec_create(test_frame_codec_decode_error, TEST_ERROR_CONTEXT);
1196 unsigned char bad_frame[] = { 0x00, 0x00, 0x00, 0x07, 0x02, 0x00, 0x01, 0x02 };
1197 unsigned char good_frame[] = { 0x00, 0x00, 0x00, 0x08, 0x02, 0x00, 0x01, 0x02 };
1198 (void)frame_codec_subscribe(frame_codec, 0, on_frame_received_1, frame_codec);
1199
1200 (void)frame_codec_receive_bytes(frame_codec, bad_frame, sizeof(bad_frame));
1201 umock_c_reset_all_calls();
1202
1203 // act
1204 result = frame_codec_receive_bytes(frame_codec, good_frame, sizeof(good_frame));
1205
1206 // assert
1207 ASSERT_ARE_NOT_EQUAL(int, 0, result);
1208 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1209
1210 // cleanup
1211 (void)frame_codec_unsubscribe(frame_codec, 0);
1212 frame_codec_destroy(frame_codec);
1213 }
1214
1215 /* Tests_SRS_FRAME_CODEC_01_074: [If a decoding error is detected, any subsequent calls on frame_codec_receive_bytes shall fail.] */
1216 TEST_FUNCTION(after_a_frame_decode_error_occurs_due_to_bad_doff_size_a_subsequent_decode_fails)
1217 {
1218 // arrange
1219 int result;
1220 FRAME_CODEC_HANDLE frame_codec = frame_codec_create(test_frame_codec_decode_error, TEST_ERROR_CONTEXT);
1221 unsigned char bad_frame[] = { 0x00, 0x00, 0x00, 0x08, 0x01, 0x00, 0x01, 0x02 };
1222 unsigned char good_frame[] = { 0x00, 0x00, 0x00, 0x08, 0x02, 0x00, 0x01, 0x02 };
1223 (void)frame_codec_subscribe(frame_codec, 0, on_frame_received_1, frame_codec);
1224
1225 (void)frame_codec_receive_bytes(frame_codec, bad_frame, sizeof(bad_frame));
1226 umock_c_reset_all_calls();
1227
1228 // act
1229 result = frame_codec_receive_bytes(frame_codec, good_frame, sizeof(good_frame));
1230
1231 // assert
1232 ASSERT_ARE_NOT_EQUAL(int, 0, result);
1233 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1234
1235 // cleanup
1236 (void)frame_codec_unsubscribe(frame_codec, 0);
1237 frame_codec_destroy(frame_codec);
1238 }
1239
1240 /* Tests_SRS_FRAME_CODEC_01_025: [frame_codec_receive_bytes decodes a sequence of bytes into frames and on success it shall return zero.] */
1241 /* Tests_SRS_FRAME_CODEC_01_031: [When a complete frame is successfully decoded it shall be indicated to the upper layer by invoking the on_frame_received passed to frame_codec_subscribe.] */
1242 /* Tests_SRS_FRAME_CODEC_01_099: [A pointer to the frame_body bytes shall also be passed to the on_frame_received.] */
1243 /* Tests_SRS_FRAME_CODEC_01_102: [frame_codec_receive_bytes shall allocate memory to hold the frame_body bytes.] */
1244 TEST_FUNCTION(receiving_a_frame_with_1_byte_frame_body_succeeds)
1245 {
1246 // arrange
1247 int result;
1248 FRAME_CODEC_HANDLE frame_codec = frame_codec_create(test_frame_codec_decode_error, TEST_ERROR_CONTEXT);
1249 unsigned char frame[] = { 0x00, 0x00, 0x00, 0x09, 0x02, 0x00, 0x01, 0x02, 0x42 };
1250 (void)frame_codec_subscribe(frame_codec, 0, on_frame_received_1, frame_codec);
1251 umock_c_reset_all_calls();
1252
1253 STRICT_EXPECTED_CALL(singlylinkedlist_find(TEST_LIST_HANDLE, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
1254 .ValidateArgumentBuffer(3, &frame[5], 1);
1255 STRICT_EXPECTED_CALL(singlylinkedlist_item_get_value(IGNORED_PTR_ARG));
1256 STRICT_EXPECTED_CALL(singlylinkedlist_item_get_value(IGNORED_PTR_ARG));
1257 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG));
1258 STRICT_EXPECTED_CALL(on_frame_received_1(frame_codec, IGNORED_PTR_ARG, 2, IGNORED_PTR_ARG, 1))
1259 .ValidateArgumentBuffer(2, &frame[6], 2)
1260 .ValidateArgumentBuffer(4, &frame[8], 1);
1261 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG));
1262
1263 // act
1264 result = frame_codec_receive_bytes(frame_codec, frame, sizeof(frame));
1265
1266 // assert
1267 ASSERT_ARE_EQUAL(int, 0, result);
1268 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1269
1270 // cleanup
1271 (void)frame_codec_unsubscribe(frame_codec, 0);
1272 frame_codec_destroy(frame_codec);
1273 }
1274
1275 /* Tests_SRS_FRAME_CODEC_01_101: [If the memory for the frame_body bytes cannot be allocated, frame_codec_receive_bytes shall fail and return a non-zero value.] */
1276 /* Tests_SRS_FRAME_CODEC_01_103: [Upon any decode error, if an error callback has been passed to frame_codec_create, then the error callback shall be called with the context argument being the frame_codec_error_callback_context argument passed to frame_codec_create.] */
1277 TEST_FUNCTION(when_allocating_type_specific_data_fails_frame_codec_receive_bytes_fails)
1278 {
1279 // arrange
1280 int result;
1281 FRAME_CODEC_HANDLE frame_codec = frame_codec_create(test_frame_codec_decode_error, TEST_ERROR_CONTEXT);
1282 unsigned char frame[] = { 0x00, 0x00, 0x00, 0x09, 0x02, 0x00, 0x01, 0x02, 0x42 };
1283 (void)frame_codec_subscribe(frame_codec, 0, on_frame_received_1, frame_codec);
1284 umock_c_reset_all_calls();
1285
1286 STRICT_EXPECTED_CALL(singlylinkedlist_find(TEST_LIST_HANDLE, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
1287 .ValidateArgumentBuffer(3, &frame[5], 1);
1288 STRICT_EXPECTED_CALL(singlylinkedlist_item_get_value(IGNORED_PTR_ARG));
1289 STRICT_EXPECTED_CALL(singlylinkedlist_item_get_value(IGNORED_PTR_ARG));
1290 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG))
1291 .SetReturn(NULL);
1292
1293 STRICT_EXPECTED_CALL(test_frame_codec_decode_error(TEST_ERROR_CONTEXT));
1294
1295 // act
1296 result = frame_codec_receive_bytes(frame_codec, frame, sizeof(frame));
1297
1298 // assert
1299 ASSERT_ARE_NOT_EQUAL(int, 0, result);
1300 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1301
1302 // cleanup
1303 (void)frame_codec_unsubscribe(frame_codec, 0);
1304 frame_codec_destroy(frame_codec);
1305 }
1306
1307 /* Tests_SRS_FRAME_CODEC_01_030: [If a decoding error occurs, frame_codec_receive_bytes shall return a non-zero value.] */
1308 /* Tests_SRS_FRAME_CODEC_01_074: [If a decoding error is detected, any subsequent calls on frame_codec_data_receive_bytes shall fail.] */
1309 TEST_FUNCTION(when_allocating_type_specific_data_fails_a_subsequent_decode_Call_fails)
1310 {
1311 // arrange
1312 int result;
1313 FRAME_CODEC_HANDLE frame_codec = frame_codec_create(test_frame_codec_decode_error, TEST_ERROR_CONTEXT);
1314 unsigned char frame[] = { 0x00, 0x00, 0x00, 0x09, 0x02, 0x00, 0x01, 0x02, 0x42 };
1315 frame_codec_subscribe(frame_codec, 0, on_frame_received_1, frame_codec);
1316 umock_c_reset_all_calls();
1317
1318 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG))
1319 .SetReturn(NULL);
1320
1321 (void)frame_codec_receive_bytes(frame_codec, frame, sizeof(frame));
1322 umock_c_reset_all_calls();
1323
1324 // act
1325 result = frame_codec_receive_bytes(frame_codec, frame, sizeof(frame));
1326
1327 // assert
1328 ASSERT_ARE_NOT_EQUAL(int, 0, result);
1329 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1330
1331 // cleanup
1332 (void)frame_codec_unsubscribe(frame_codec, 0);
1333 frame_codec_destroy(frame_codec);
1334 }
1335
1336 /* Tests_SRS_FRAME_CODEC_01_031: [When a complete frame is successfully decoded it shall be indicated to the upper layer by invoking the on_frame_received passed to frame_codec_subscribe.] */
1337 TEST_FUNCTION(a_frame_with_2_bytes_received_together_with_the_header_passes_the_bytes_in_one_call)
1338 {
1339 // arrange
1340 int result;
1341 FRAME_CODEC_HANDLE frame_codec = frame_codec_create(test_frame_codec_decode_error, TEST_ERROR_CONTEXT);
1342 unsigned char frame[] = { 0x00, 0x00, 0x00, 0x0A, 0x02, 0x00, 0x01, 0x02, 0x42, 0x43 };
1343 (void)frame_codec_subscribe(frame_codec, 0, on_frame_received_1, frame_codec);
1344 umock_c_reset_all_calls();
1345
1346 STRICT_EXPECTED_CALL(singlylinkedlist_find(TEST_LIST_HANDLE, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
1347 .ValidateArgumentBuffer(3, &frame[5], 1);
1348 STRICT_EXPECTED_CALL(singlylinkedlist_item_get_value(IGNORED_PTR_ARG));
1349 STRICT_EXPECTED_CALL(singlylinkedlist_item_get_value(IGNORED_PTR_ARG));
1350 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG));
1351 STRICT_EXPECTED_CALL(on_frame_received_1(frame_codec, IGNORED_PTR_ARG, 2, IGNORED_PTR_ARG, 2))
1352 .ValidateArgumentBuffer(2, &frame[6], 2)
1353 .ValidateArgumentBuffer(4, &frame[sizeof(frame) - 2], 2);
1354 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG));
1355
1356 // act
1357 result = frame_codec_receive_bytes(frame_codec, frame, sizeof(frame));
1358
1359 // assert
1360 ASSERT_ARE_EQUAL(int, 0, result);
1361 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1362
1363 // cleanup
1364 (void)frame_codec_unsubscribe(frame_codec, 0);
1365 frame_codec_destroy(frame_codec);
1366 }
1367
1368 /* Tests_SRS_FRAME_CODEC_01_025: [frame_codec_receive_bytes decodes a sequence of bytes into frames and on success it shall return zero.] */
1369 TEST_FUNCTION(two_empty_frames_received_in_the_same_call_yields_2_callbacks)
1370 {
1371 // arrange
1372 int result;
1373 FRAME_CODEC_HANDLE frame_codec = frame_codec_create(test_frame_codec_decode_error, TEST_ERROR_CONTEXT);
1374 unsigned char frame[] = { 0x00, 0x00, 0x00, 0x08, 0x02, 0x00, 0x01, 0x02,
1375 0x00, 0x00, 0x00, 0x08, 0x02, 0x00, 0x03, 0x04 };
1376 (void)frame_codec_subscribe(frame_codec, 0, on_frame_received_1, frame_codec);
1377 umock_c_reset_all_calls();
1378
1379 STRICT_EXPECTED_CALL(singlylinkedlist_find(TEST_LIST_HANDLE, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
1380 .ValidateArgumentBuffer(3, &frame[5], 1);
1381 STRICT_EXPECTED_CALL(singlylinkedlist_item_get_value(IGNORED_PTR_ARG));
1382 STRICT_EXPECTED_CALL(singlylinkedlist_item_get_value(IGNORED_PTR_ARG));
1383 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG));
1384 STRICT_EXPECTED_CALL(on_frame_received_1(frame_codec, IGNORED_PTR_ARG, 2, IGNORED_PTR_ARG, 0))
1385 .ValidateArgumentBuffer(2, &frame[6], 2);
1386 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG));
1387 STRICT_EXPECTED_CALL(singlylinkedlist_find(TEST_LIST_HANDLE, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
1388 .ValidateArgumentBuffer(3, &frame[5], 1);
1389 STRICT_EXPECTED_CALL(singlylinkedlist_item_get_value(IGNORED_PTR_ARG));
1390 STRICT_EXPECTED_CALL(singlylinkedlist_item_get_value(IGNORED_PTR_ARG));
1391 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG));
1392 STRICT_EXPECTED_CALL(on_frame_received_1(frame_codec, IGNORED_PTR_ARG, 2, IGNORED_PTR_ARG, 0))
1393 .ValidateArgumentBuffer(2, &frame[14], 2);
1394 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG));
1395
1396 // act
1397 result = frame_codec_receive_bytes(frame_codec, frame, sizeof(frame));
1398
1399 // assert
1400 ASSERT_ARE_EQUAL(int, 0, result);
1401 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1402
1403 // cleanup
1404 (void)frame_codec_unsubscribe(frame_codec, 0);
1405 frame_codec_destroy(frame_codec);
1406 }
1407
1408 /* Tests_SRS_FRAME_CODEC_01_025: [frame_codec_receive_bytes decodes a sequence of bytes into frames and on success it shall return zero.] */
1409 TEST_FUNCTION(two_frames_with_1_byte_each_received_in_the_same_call_yields_2_callbacks)
1410 {
1411 // arrange
1412 int result;
1413 FRAME_CODEC_HANDLE frame_codec = frame_codec_create(test_frame_codec_decode_error, TEST_ERROR_CONTEXT);
1414 unsigned char frame[] = { 0x00, 0x00, 0x00, 0x09, 0x02, 0x00, 0x01, 0x02, 0x42,
1415 0x00, 0x00, 0x00, 0x09, 0x02, 0x00, 0x03, 0x04, 0x43 };
1416 (void)frame_codec_subscribe(frame_codec, 0, on_frame_received_1, frame_codec);
1417 umock_c_reset_all_calls();
1418
1419 STRICT_EXPECTED_CALL(singlylinkedlist_find(TEST_LIST_HANDLE, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
1420 .ValidateArgumentBuffer(3, &frame[5], 1);
1421 STRICT_EXPECTED_CALL(singlylinkedlist_item_get_value(IGNORED_PTR_ARG));
1422 STRICT_EXPECTED_CALL(singlylinkedlist_item_get_value(IGNORED_PTR_ARG));
1423 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG));
1424 STRICT_EXPECTED_CALL(on_frame_received_1(frame_codec, IGNORED_PTR_ARG, 2, IGNORED_PTR_ARG, 1))
1425 .ValidateArgumentBuffer(2, &frame[6], 2)
1426 .ValidateArgumentBuffer(4, &frame[8], 1);
1427 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG));
1428 STRICT_EXPECTED_CALL(singlylinkedlist_find(TEST_LIST_HANDLE, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
1429 .ValidateArgumentBuffer(3, &frame[5], 1);
1430 STRICT_EXPECTED_CALL(singlylinkedlist_item_get_value(IGNORED_PTR_ARG));
1431 STRICT_EXPECTED_CALL(singlylinkedlist_item_get_value(IGNORED_PTR_ARG));
1432 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG));
1433 STRICT_EXPECTED_CALL(on_frame_received_1(frame_codec, IGNORED_PTR_ARG, 2, IGNORED_PTR_ARG, 1))
1434 .ValidateArgumentBuffer(2, &frame[15], 2)
1435 .ValidateArgumentBuffer(4, &frame[17], 1);
1436 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG));
1437
1438 // act
1439 result = frame_codec_receive_bytes(frame_codec, frame, sizeof(frame));
1440
1441 // assert
1442 ASSERT_ARE_EQUAL(int, 0, result);
1443 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1444
1445 // cleanup
1446 (void)frame_codec_unsubscribe(frame_codec, 0);
1447 frame_codec_destroy(frame_codec);
1448 }
1449
1450 /* frame_codec_subscribe */
1451
1452 /* Tests_SRS_FRAME_CODEC_01_033: [frame_codec_subscribe subscribes for a certain type of frame received by the frame_codec instance identified by frame_codec.] */
1453 /* Tests_SRS_FRAME_CODEC_01_087: [On success, frame_codec_subscribe shall return zero.] */
1454 TEST_FUNCTION(frame_codec_subscribe_with_valid_args_succeeds)
1455 {
1456 // arrange
1457 int result;
1458 FRAME_CODEC_HANDLE frame_codec = frame_codec_create(test_frame_codec_decode_error, TEST_ERROR_CONTEXT);
1459 uint8_t frame_type = 0;
1460 umock_c_reset_all_calls();
1461
1462 STRICT_EXPECTED_CALL(singlylinkedlist_find(TEST_LIST_HANDLE, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
1463 .ValidateArgumentBuffer(3, &frame_type, 1);
1464 STRICT_EXPECTED_CALL(gballoc_calloc(IGNORED_NUM_ARG, IGNORED_NUM_ARG));
1465 STRICT_EXPECTED_CALL(singlylinkedlist_add(TEST_LIST_HANDLE, IGNORED_PTR_ARG));
1466
1467 // act
1468 result = frame_codec_subscribe(frame_codec, 0, on_frame_received_1, frame_codec);
1469
1470 // assert
1471 ASSERT_ARE_EQUAL(int, 0, result);
1472 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1473
1474 // cleanup
1475 (void)frame_codec_unsubscribe(frame_codec, 0);
1476 frame_codec_destroy(frame_codec);
1477 }
1478
1479 /* Tests_SRS_FRAME_CODEC_01_033: [frame_codec_subscribe subscribes for a certain type of frame received by the frame_codec instance identified by frame_codec.] */
1480 /* Tests_SRS_FRAME_CODEC_01_087: [On success, frame_codec_subscribe shall return zero.] */
1481 TEST_FUNCTION(when_list_find_returns_NULL_a_new_subscription_is_created)
1482 {
1483 // arrange
1484 int result;
1485 FRAME_CODEC_HANDLE frame_codec = frame_codec_create(test_frame_codec_decode_error, TEST_ERROR_CONTEXT);
1486 uint8_t frame_type = 0;
1487 umock_c_reset_all_calls();
1488
1489 STRICT_EXPECTED_CALL(singlylinkedlist_find(TEST_LIST_HANDLE, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
1490 .ValidateArgumentBuffer(3, &frame_type, 1)
1491 .SetReturn(NULL);
1492 STRICT_EXPECTED_CALL(gballoc_calloc(IGNORED_NUM_ARG, IGNORED_NUM_ARG));
1493 STRICT_EXPECTED_CALL(singlylinkedlist_add(TEST_LIST_HANDLE, IGNORED_PTR_ARG));
1494
1495 // act
1496 result = frame_codec_subscribe(frame_codec, 0, on_frame_received_1, frame_codec);
1497
1498 // assert
1499 ASSERT_ARE_EQUAL(int, 0, result);
1500 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1501
1502 // cleanup
1503 (void)frame_codec_unsubscribe(frame_codec, 0);
1504 frame_codec_destroy(frame_codec);
1505 }
1506
1507 /* Tests_SRS_FRAME_CODEC_01_037: [If any failure occurs while performing the subscribe operation, frame_codec_subscribe shall return a non-zero value.] */
1508 TEST_FUNCTION(when_list_item_get_value_returns_NULL_subscribe_fails)
1509 {
1510 // arrange
1511 int result;
1512 FRAME_CODEC_HANDLE frame_codec = frame_codec_create(test_frame_codec_decode_error, TEST_ERROR_CONTEXT);
1513 uint8_t frame_type = 0;
1514 (void)frame_codec_subscribe(frame_codec, 0, on_frame_received_1, frame_codec);
1515 umock_c_reset_all_calls();
1516
1517 STRICT_EXPECTED_CALL(singlylinkedlist_find(TEST_LIST_HANDLE, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
1518 .ValidateArgumentBuffer(3, &frame_type, 1);
1519 STRICT_EXPECTED_CALL(singlylinkedlist_item_get_value(IGNORED_PTR_ARG));
1520 STRICT_EXPECTED_CALL(singlylinkedlist_item_get_value(IGNORED_PTR_ARG))
1521 .SetReturn(NULL);
1522
1523 // act
1524 result = frame_codec_subscribe(frame_codec, 0, on_frame_received_1, frame_codec);
1525
1526 // assert
1527 ASSERT_ARE_NOT_EQUAL(int, 0, result);
1528 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1529
1530 // cleanup
1531 (void)frame_codec_unsubscribe(frame_codec, 0);
1532 frame_codec_destroy(frame_codec);
1533 }
1534
1535 /* Tests_SRS_FRAME_CODEC_01_034: [If any of the frame_codec or on_frame_received arguments is NULL, frame_codec_subscribe shall return a non-zero value.] */
1536 TEST_FUNCTION(when_frame_codec_is_NULL_frame_codec_subscribe_fails)
1537 {
1538 // arrange
1539
1540 // act
1541 int result = frame_codec_subscribe(NULL, 0, on_frame_received_1, (void*)0x01);
1542
1543 // assert
1544 ASSERT_ARE_NOT_EQUAL(int, 0, result);
1545 }
1546
1547 /* Tests_SRS_FRAME_CODEC_01_034: [If any of the frame_codec or on_frame_received arguments is NULL, frame_codec_subscribe shall return a non-zero value.] */
1548 TEST_FUNCTION(when_on_frame_received_is_NULL_frame_codec_subscribe_fails)
1549 {
1550 // arrange
1551 int result;
1552 FRAME_CODEC_HANDLE frame_codec = frame_codec_create(test_frame_codec_decode_error, TEST_ERROR_CONTEXT);
1553 umock_c_reset_all_calls();
1554
1555 // act
1556 result = frame_codec_subscribe(frame_codec, 0, NULL, frame_codec);
1557
1558 // assert
1559 ASSERT_ARE_NOT_EQUAL(int, 0, result);
1560 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1561
1562 // cleanup
1563 frame_codec_destroy(frame_codec);
1564 }
1565
1566 /* Tests_SRS_FRAME_CODEC_01_035: [After successfully registering a callback for a certain frame type, when subsequently that frame type is received the callbacks shall be invoked, passing to it the received frame and the callback_context value. */
1567 TEST_FUNCTION(when_a_frame_type_that_has_no_subscribers_is_received_no_callback_is_called)
1568 {
1569 // arrange
1570 int result;
1571 FRAME_CODEC_HANDLE frame_codec = frame_codec_create(test_frame_codec_decode_error, TEST_ERROR_CONTEXT);
1572 unsigned char frame[] = { 0x00, 0x00, 0x00, 0x08, 0x02, 0x01, 0x00, 0x00 };
1573 (void)frame_codec_subscribe(frame_codec, 0, on_frame_received_1, frame_codec);
1574 umock_c_reset_all_calls();
1575
1576 STRICT_EXPECTED_CALL(singlylinkedlist_find(TEST_LIST_HANDLE, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
1577 .ValidateArgumentBuffer(3, &frame[5], 1);
1578 STRICT_EXPECTED_CALL(singlylinkedlist_item_get_value(IGNORED_PTR_ARG));
1579
1580 // act
1581 result = frame_codec_receive_bytes(frame_codec, frame, sizeof(frame));
1582
1583 // assert
1584 ASSERT_ARE_EQUAL(int, 0, result);
1585 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1586
1587 // cleanup
1588 (void)frame_codec_unsubscribe(frame_codec, 0);
1589 frame_codec_destroy(frame_codec);
1590 }
1591
1592 /* Tests_SRS_FRAME_CODEC_01_035: [After successfully registering a callback for a certain frame type, when subsequently that frame type is received the callbacks shall be invoked, passing to it the received frame and the callback_context value.] */
1593 TEST_FUNCTION(when_no_subscribe_is_done_no_callback_is_called)
1594 {
1595 // arrange
1596 int result;
1597 FRAME_CODEC_HANDLE frame_codec = frame_codec_create(test_frame_codec_decode_error, TEST_ERROR_CONTEXT);
1598 unsigned char frame[] = { 0x00, 0x00, 0x00, 0x08, 0x02, 0x01, 0x00, 0x00 };
1599 umock_c_reset_all_calls();
1600
1601 STRICT_EXPECTED_CALL(singlylinkedlist_find(TEST_LIST_HANDLE, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
1602 .ValidateArgumentBuffer(3, &frame[5], 1);
1603
1604 // act
1605 result = frame_codec_receive_bytes(frame_codec, frame, sizeof(frame));
1606
1607 // assert
1608 ASSERT_ARE_EQUAL(int, 0, result);
1609 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1610
1611 // cleanup
1612 frame_codec_destroy(frame_codec);
1613 }
1614
1615 /* Tests_SRS_FRAME_CODEC_01_035: [After successfully registering a callback for a certain frame type, when subsequently that frame type is received the callbacks shall be invoked, passing to it the received frame and the callback_context value.] */
1616 TEST_FUNCTION(when_2_subscriptions_exist_and_first_one_matches_the_callback_is_invoked)
1617 {
1618 // arrange
1619 int result;
1620 FRAME_CODEC_HANDLE frame_codec = frame_codec_create(test_frame_codec_decode_error, TEST_ERROR_CONTEXT);
1621 unsigned char frame[] = { 0x00, 0x00, 0x00, 0x0A, 0x02, 0x00, 0x01, 0x02, 0x42, 0x43 };
1622 (void)frame_codec_subscribe(frame_codec, 0, on_frame_received_1, frame_codec);
1623 (void)frame_codec_subscribe(frame_codec, 1, on_frame_received_2, frame_codec);
1624 umock_c_reset_all_calls();
1625
1626 STRICT_EXPECTED_CALL(singlylinkedlist_find(TEST_LIST_HANDLE, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
1627 .ValidateArgumentBuffer(3, &frame[5], 1);
1628 STRICT_EXPECTED_CALL(singlylinkedlist_item_get_value(IGNORED_PTR_ARG));
1629 STRICT_EXPECTED_CALL(singlylinkedlist_item_get_value(IGNORED_PTR_ARG));
1630 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG));
1631 STRICT_EXPECTED_CALL(on_frame_received_1(frame_codec, IGNORED_PTR_ARG, 2, IGNORED_PTR_ARG, 2))
1632 .ValidateArgumentBuffer(2, &frame[6], 2)
1633 .ValidateArgumentBuffer(4, &frame[sizeof(frame) - 2], 2);
1634 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG));
1635
1636 // act
1637 result = frame_codec_receive_bytes(frame_codec, frame, sizeof(frame));
1638
1639 // assert
1640 ASSERT_ARE_EQUAL(int, 0, result);
1641 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1642
1643 // cleanup
1644 (void)frame_codec_unsubscribe(frame_codec, 0);
1645 (void)frame_codec_unsubscribe(frame_codec, 1);
1646 frame_codec_destroy(frame_codec);
1647 }
1648
1649 /* Tests_SRS_FRAME_CODEC_01_035: [After successfully registering a callback for a certain frame type, when subsequently that frame type is received the callbacks shall be invoked, passing to it the received frame and the callback_context value.] */
1650 TEST_FUNCTION(when_2_subscriptions_exist_and_second_one_matches_the_callback_is_invoked)
1651 {
1652 // arrange
1653 int result;
1654 FRAME_CODEC_HANDLE frame_codec = frame_codec_create(test_frame_codec_decode_error, TEST_ERROR_CONTEXT);
1655 unsigned char frame[] = { 0x00, 0x00, 0x00, 0x0A, 0x02, 0x01, 0x01, 0x02, 0x42, 0x43 };
1656 (void)frame_codec_subscribe(frame_codec, 0, on_frame_received_1, frame_codec);
1657 (void)frame_codec_subscribe(frame_codec, 1, on_frame_received_2, frame_codec);
1658 umock_c_reset_all_calls();
1659
1660 STRICT_EXPECTED_CALL(singlylinkedlist_find(TEST_LIST_HANDLE, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
1661 .ValidateArgumentBuffer(3, &frame[5], 1);
1662 STRICT_EXPECTED_CALL(singlylinkedlist_item_get_value(IGNORED_PTR_ARG));
1663 STRICT_EXPECTED_CALL(singlylinkedlist_item_get_value(IGNORED_PTR_ARG));
1664 STRICT_EXPECTED_CALL(singlylinkedlist_item_get_value(IGNORED_PTR_ARG));
1665 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG));
1666 STRICT_EXPECTED_CALL(on_frame_received_2(frame_codec, IGNORED_PTR_ARG, 2, IGNORED_PTR_ARG, 2))
1667 .ValidateArgumentBuffer(2, &frame[6], 2)
1668 .ValidateArgumentBuffer(4, &frame[sizeof(frame) - 2], 2);
1669 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG));
1670
1671 // act
1672 result = frame_codec_receive_bytes(frame_codec, frame, sizeof(frame));
1673
1674 // assert
1675 ASSERT_ARE_EQUAL(int, 0, result);
1676 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1677
1678 // cleanup
1679 (void)frame_codec_unsubscribe(frame_codec, 0);
1680 (void)frame_codec_unsubscribe(frame_codec, 1);
1681 frame_codec_destroy(frame_codec);
1682 }
1683
1684 /* Tests_SRS_FRAME_CODEC_01_036: [Only one callback pair shall be allowed to be registered for a given frame type.] */
1685 TEST_FUNCTION(when_frame_codec_subscribe_is_called_twice_for_the_same_frame_type_it_succeeds)
1686 {
1687 // arrange
1688 int result;
1689 FRAME_CODEC_HANDLE frame_codec = frame_codec_create(test_frame_codec_decode_error, TEST_ERROR_CONTEXT);
1690 uint8_t frame_type = 0;
1691 (void)frame_codec_subscribe(frame_codec, 0, on_frame_received_1, frame_codec);
1692 umock_c_reset_all_calls();
1693
1694 STRICT_EXPECTED_CALL(singlylinkedlist_find(TEST_LIST_HANDLE, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
1695 .ValidateArgumentBuffer(3, &frame_type, 1);
1696 STRICT_EXPECTED_CALL(singlylinkedlist_item_get_value(IGNORED_PTR_ARG));
1697 STRICT_EXPECTED_CALL(singlylinkedlist_item_get_value(IGNORED_PTR_ARG));
1698
1699 // act
1700 result = frame_codec_subscribe(frame_codec, 0, on_frame_received_2, frame_codec);
1701
1702 // assert
1703 ASSERT_ARE_EQUAL(int, 0, result);
1704 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1705
1706 // cleanup
1707 (void)frame_codec_unsubscribe(frame_codec, 0);
1708 frame_codec_destroy(frame_codec);
1709 }
1710
1711 /* Tests_SRS_FRAME_CODEC_01_036: [Only one callback pair shall be allowed to be registered for a given frame type.] */
1712 TEST_FUNCTION(the_callbacks_for_the_2nd_frame_codec_subscribe_for_the_same_frame_type_remain_in_effect)
1713 {
1714 // arrange
1715 int result;
1716 FRAME_CODEC_HANDLE frame_codec = frame_codec_create(test_frame_codec_decode_error, TEST_ERROR_CONTEXT);
1717 unsigned char frame[] = { 0x00, 0x00, 0x00, 0x0A, 0x02, 0x00, 0x01, 0x02, 0x42, 0x43 };
1718 (void)frame_codec_subscribe(frame_codec, 0, on_frame_received_1, frame_codec);
1719 (void)frame_codec_subscribe(frame_codec, 0, on_frame_received_2, frame_codec);
1720 umock_c_reset_all_calls();
1721
1722 STRICT_EXPECTED_CALL(singlylinkedlist_find(TEST_LIST_HANDLE, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
1723 .ValidateArgumentBuffer(3, &frame[5], 1);
1724 STRICT_EXPECTED_CALL(singlylinkedlist_item_get_value(IGNORED_PTR_ARG));
1725 STRICT_EXPECTED_CALL(singlylinkedlist_item_get_value(IGNORED_PTR_ARG));
1726 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG));
1727 STRICT_EXPECTED_CALL(on_frame_received_2(frame_codec, IGNORED_PTR_ARG, 2, IGNORED_PTR_ARG, 2))
1728 .ValidateArgumentBuffer(2, &frame[6], 2)
1729 .ValidateArgumentBuffer(4, &frame[sizeof(frame) - 2], 2);
1730 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG));
1731
1732 // act
1733 result = frame_codec_receive_bytes(frame_codec, frame, sizeof(frame));
1734
1735 // assert
1736 ASSERT_ARE_EQUAL(int, 0, result);
1737 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1738
1739 // cleanup
1740 (void)frame_codec_unsubscribe(frame_codec, 0);
1741 frame_codec_destroy(frame_codec);
1742 }
1743
1744 /* Tests_SRS_FRAME_CODEC_01_037: [If any failure occurs while performing the subscribe operation, frame_codec_subscribe shall return a non-zero value.] */
1745 TEST_FUNCTION(when_allocating_memory_for_the_subscription_fails_frame_codec_subscribe_fails)
1746 {
1747 // arrange
1748 int result;
1749 FRAME_CODEC_HANDLE frame_codec = frame_codec_create(test_frame_codec_decode_error, TEST_ERROR_CONTEXT);
1750 uint8_t frame_type = 0;
1751 umock_c_reset_all_calls();
1752
1753 STRICT_EXPECTED_CALL(singlylinkedlist_find(TEST_LIST_HANDLE, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
1754 .ValidateArgumentBuffer(3, &frame_type, 1);
1755 STRICT_EXPECTED_CALL(gballoc_calloc(IGNORED_NUM_ARG, IGNORED_NUM_ARG))
1756 .SetReturn(NULL);
1757
1758 // act
1759 result = frame_codec_subscribe(frame_codec, 0, on_frame_received_1, frame_codec);
1760
1761 // assert
1762 ASSERT_ARE_NOT_EQUAL(int, 0, result);
1763 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1764
1765 // cleanup
1766 frame_codec_destroy(frame_codec);
1767 }
1768
1769 /* Tests_SRS_FRAME_CODEC_01_037: [If any failure occurs while performing the subscribe operation, frame_codec_subscribe shall return a non-zero value.] */
1770 TEST_FUNCTION(when_adding_the_subscription_fails_then_frame_codec_subscribe_fails)
1771 {
1772 // arrange
1773 int result;
1774 FRAME_CODEC_HANDLE frame_codec = frame_codec_create(test_frame_codec_decode_error, TEST_ERROR_CONTEXT);
1775 uint8_t frame_type = 0;
1776 umock_c_reset_all_calls();
1777
1778 STRICT_EXPECTED_CALL(singlylinkedlist_find(TEST_LIST_HANDLE, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
1779 .ValidateArgumentBuffer(3, &frame_type, 1);
1780 STRICT_EXPECTED_CALL(gballoc_calloc(IGNORED_NUM_ARG, IGNORED_NUM_ARG));
1781 STRICT_EXPECTED_CALL(singlylinkedlist_add(TEST_LIST_HANDLE, IGNORED_PTR_ARG))
1782 .SetReturn(NULL);
1783 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG));
1784
1785 // act
1786 result = frame_codec_subscribe(frame_codec, 0, on_frame_received_1, frame_codec);
1787
1788 // assert
1789 ASSERT_ARE_NOT_EQUAL(int, 0, result);
1790 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1791
1792 // cleanup
1793 frame_codec_destroy(frame_codec);
1794 }
1795
1796 /* frame_codec_unsubscribe */
1797
1798 /* Tests_SRS_FRAME_CODEC_01_038: [frame_codec_unsubscribe removes a previous subscription for frames of type type and on success it shall return 0.] */
1799 TEST_FUNCTION(removing_an_existing_subscription_succeeds)
1800 {
1801 // arrange
1802 int result;
1803 FRAME_CODEC_HANDLE frame_codec = frame_codec_create(test_frame_codec_decode_error, TEST_ERROR_CONTEXT);
1804 uint8_t frame_type = 0;
1805 (void)frame_codec_subscribe(frame_codec, 0, on_frame_received_1, frame_codec);
1806 umock_c_reset_all_calls();
1807
1808 STRICT_EXPECTED_CALL(singlylinkedlist_find(TEST_LIST_HANDLE, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
1809 .ValidateArgumentBuffer(3, &frame_type, 1);
1810 STRICT_EXPECTED_CALL(singlylinkedlist_item_get_value(IGNORED_PTR_ARG));
1811 STRICT_EXPECTED_CALL(singlylinkedlist_item_get_value(IGNORED_PTR_ARG));
1812 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG));
1813 STRICT_EXPECTED_CALL(singlylinkedlist_remove(TEST_LIST_HANDLE, IGNORED_PTR_ARG));
1814
1815 // act
1816 result = frame_codec_unsubscribe(frame_codec, 0);
1817
1818 // assert
1819 ASSERT_ARE_EQUAL(int, 0, result);
1820 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1821
1822 // cleanup
1823 frame_codec_destroy(frame_codec);
1824 }
1825
1826 /* Tests_SRS_FRAME_CODEC_01_038: [frame_codec_unsubscribe removes a previous subscription for frames of type type and on success it shall return 0.] */
1827 TEST_FUNCTION(removing_an_existing_subscription_does_not_trigger_callback_when_a_frame_of_that_type_is_received)
1828 {
1829 // arrange
1830 int result;
1831 FRAME_CODEC_HANDLE frame_codec = frame_codec_create(test_frame_codec_decode_error, TEST_ERROR_CONTEXT);
1832 unsigned char frame[] = { 0x00, 0x00, 0x00, 0x0A, 0x02, 0x00, 0x01, 0x02, 0x42, 0x43 };
1833 (void)frame_codec_subscribe(frame_codec, 0, on_frame_received_1, frame_codec);
1834 (void)frame_codec_unsubscribe(frame_codec, 0);
1835 umock_c_reset_all_calls();
1836
1837 STRICT_EXPECTED_CALL(singlylinkedlist_find(TEST_LIST_HANDLE, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
1838 .ValidateArgumentBuffer(3, &frame[5], 1);
1839
1840 // act
1841 result = frame_codec_receive_bytes(frame_codec, frame, sizeof(frame));
1842
1843 // assert
1844 ASSERT_ARE_EQUAL(int, 0, result);
1845 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1846
1847 // cleanup
1848 frame_codec_destroy(frame_codec);
1849 }
1850
1851 /* Tests_SRS_FRAME_CODEC_01_039: [If frame_codec is NULL, frame_codec_unsubscribe shall return a non-zero value.] */
1852 TEST_FUNCTION(frame_codec_unsubscribe_with_NULL_frame_codec_handle_fails)
1853 {
1854 // arrange
1855
1856 // act
1857 int result = frame_codec_unsubscribe(NULL, 0);
1858
1859 // assert
1860 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1861 ASSERT_ARE_NOT_EQUAL(int, 0, result);
1862 }
1863
1864 /* Tests_SRS_FRAME_CODEC_01_040: [If no subscription for the type frame type exists, frame_codec_unsubscribe shall return a non-zero value.] */
1865 TEST_FUNCTION(frame_codec_unsubscribe_with_no_subscribe_call_has_been_made_fails)
1866 {
1867 // arrange
1868 int result;
1869 FRAME_CODEC_HANDLE frame_codec = frame_codec_create(test_frame_codec_decode_error, TEST_ERROR_CONTEXT);
1870 uint8_t frame_type = 0;
1871 umock_c_reset_all_calls();
1872
1873 STRICT_EXPECTED_CALL(singlylinkedlist_find(TEST_LIST_HANDLE, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
1874 .ValidateArgumentBuffer(3, &frame_type, 1);
1875
1876 // act
1877 result = frame_codec_unsubscribe(frame_codec, 0);
1878
1879 // assert
1880 ASSERT_ARE_NOT_EQUAL(int, 0, result);
1881 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1882
1883 // cleanup
1884 frame_codec_destroy(frame_codec);
1885 }
1886
1887 /* Tests_SRS_FRAME_CODEC_01_041: [If any failure occurs while performing the unsubscribe operation, frame_codec_unsubscribe shall return a non-zero value.] */
1888 TEST_FUNCTION(when_list_remove_matching_item_fails_then_frame_codec_unsubscribe_fails)
1889 {
1890 // arrange
1891 int result;
1892 FRAME_CODEC_HANDLE frame_codec = frame_codec_create(test_frame_codec_decode_error, TEST_ERROR_CONTEXT);
1893 uint8_t frame_type = 0;
1894 umock_c_reset_all_calls();
1895
1896 STRICT_EXPECTED_CALL(singlylinkedlist_find(TEST_LIST_HANDLE, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
1897 .ValidateArgumentBuffer(3, &frame_type, 1)
1898 .SetReturn(NULL);
1899
1900 // act
1901 result = frame_codec_unsubscribe(frame_codec, 0);
1902
1903 // assert
1904 ASSERT_ARE_NOT_EQUAL(int, 0, result);
1905 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1906
1907 // cleanup
1908 frame_codec_destroy(frame_codec);
1909 }
1910
1911 /* Tests_SRS_FRAME_CODEC_01_038: [frame_codec_unsubscribe removes a previous subscription for frames of type type and on success it shall return 0.] */
1912 TEST_FUNCTION(unsubscribe_one_of_2_subscriptions_succeeds)
1913 {
1914 // arrange
1915 int result;
1916 FRAME_CODEC_HANDLE frame_codec = frame_codec_create(test_frame_codec_decode_error, TEST_ERROR_CONTEXT);
1917 uint8_t frame_type = 0;
1918 (void)frame_codec_subscribe(frame_codec, 0, on_frame_received_1, frame_codec);
1919 (void)frame_codec_subscribe(frame_codec, 1, on_frame_received_2, frame_codec);
1920 umock_c_reset_all_calls();
1921
1922 STRICT_EXPECTED_CALL(singlylinkedlist_find(TEST_LIST_HANDLE, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
1923 .ValidateArgumentBuffer(3, &frame_type, 1);
1924 STRICT_EXPECTED_CALL(singlylinkedlist_item_get_value(IGNORED_PTR_ARG));
1925 STRICT_EXPECTED_CALL(singlylinkedlist_item_get_value(IGNORED_PTR_ARG));
1926 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG));
1927 STRICT_EXPECTED_CALL(singlylinkedlist_remove(TEST_LIST_HANDLE, IGNORED_PTR_ARG));
1928
1929 // act
1930 result = frame_codec_unsubscribe(frame_codec, 0);
1931
1932 // assert
1933 ASSERT_ARE_EQUAL(int, 0, result);
1934 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1935
1936 // cleanup
1937 (void)frame_codec_unsubscribe(frame_codec, 1);
1938 frame_codec_destroy(frame_codec);
1939 }
1940
1941 /* Tests_SRS_FRAME_CODEC_01_038: [frame_codec_unsubscribe removes a previous subscription for frames of type type and on success it shall return 0.] */
1942 TEST_FUNCTION(unsubscribe_2nd_out_of_2_subscriptions_succeeds)
1943 {
1944 // arrange
1945 int result;
1946 FRAME_CODEC_HANDLE frame_codec = frame_codec_create(test_frame_codec_decode_error, TEST_ERROR_CONTEXT);
1947 uint8_t frame_type = 1;
1948 (void)frame_codec_subscribe(frame_codec, 0, on_frame_received_1, frame_codec);
1949 (void)frame_codec_subscribe(frame_codec, 1, on_frame_received_2, frame_codec);
1950 umock_c_reset_all_calls();
1951
1952 STRICT_EXPECTED_CALL(singlylinkedlist_find(TEST_LIST_HANDLE, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
1953 .ValidateArgumentBuffer(3, &frame_type, 1);
1954 STRICT_EXPECTED_CALL(singlylinkedlist_item_get_value(IGNORED_PTR_ARG));
1955 STRICT_EXPECTED_CALL(singlylinkedlist_item_get_value(IGNORED_PTR_ARG));
1956 STRICT_EXPECTED_CALL(singlylinkedlist_item_get_value(IGNORED_PTR_ARG));
1957 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG));
1958 STRICT_EXPECTED_CALL(singlylinkedlist_remove(TEST_LIST_HANDLE, IGNORED_PTR_ARG));
1959
1960 // act
1961 result = frame_codec_unsubscribe(frame_codec, 1);
1962
1963 // assert
1964 ASSERT_ARE_EQUAL(int, 0, result);
1965 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1966
1967 // cleanup
1968 (void)frame_codec_unsubscribe(frame_codec, 0);
1969 frame_codec_destroy(frame_codec);
1970 }
1971
1972 /* Tests_SRS_FRAME_CODEC_01_038: [frame_codec_unsubscribe removes a previous subscription for frames of type type and on success it shall return 0.] */
1973 TEST_FUNCTION(subscribe_unsubscribe_subscribe_succeeds)
1974 {
1975 // arrange
1976 int result;
1977 FRAME_CODEC_HANDLE frame_codec = frame_codec_create(test_frame_codec_decode_error, TEST_ERROR_CONTEXT);
1978 uint8_t frame_type = 0;
1979 (void)frame_codec_subscribe(frame_codec, 0, on_frame_received_1, frame_codec);
1980 (void)frame_codec_unsubscribe(frame_codec, 0);
1981 umock_c_reset_all_calls();
1982
1983 STRICT_EXPECTED_CALL(singlylinkedlist_find(TEST_LIST_HANDLE, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
1984 .ValidateArgumentBuffer(3, &frame_type, 1);
1985 STRICT_EXPECTED_CALL(gballoc_calloc(IGNORED_NUM_ARG, IGNORED_NUM_ARG));
1986 STRICT_EXPECTED_CALL(singlylinkedlist_add(TEST_LIST_HANDLE, IGNORED_PTR_ARG));
1987
1988 // act
1989 result = frame_codec_subscribe(frame_codec, 0, on_frame_received_1, frame_codec);
1990
1991 // assert
1992 ASSERT_ARE_EQUAL(int, 0, result);
1993 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
1994
1995 // cleanup
1996 (void)frame_codec_unsubscribe(frame_codec, 0);
1997 frame_codec_destroy(frame_codec);
1998 }
1999
2000 /* Tests_SRS_FRAME_CODEC_01_038: [frame_codec_unsubscribe removes a previous subscription for frames of type type and on success it shall return 0.] */
2001 TEST_FUNCTION(subscribe_unsubscribe_unsubscribe_fails)
2002 {
2003 // arrange
2004 int result;
2005 FRAME_CODEC_HANDLE frame_codec = frame_codec_create(test_frame_codec_decode_error, TEST_ERROR_CONTEXT);
2006 uint8_t frame_type = 0;
2007 (void)frame_codec_subscribe(frame_codec, 0, on_frame_received_1, frame_codec);
2008 (void)frame_codec_unsubscribe(frame_codec, 0);
2009 umock_c_reset_all_calls();
2010
2011 STRICT_EXPECTED_CALL(singlylinkedlist_find(TEST_LIST_HANDLE, IGNORED_PTR_ARG, IGNORED_PTR_ARG))
2012 .ValidateArgumentBuffer(3, &frame_type, 1);
2013
2014 // act
2015 result = frame_codec_unsubscribe(frame_codec, 0);
2016
2017 // assert
2018 ASSERT_ARE_NOT_EQUAL(int, 0, result);
2019 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2020
2021 // cleanup
2022 frame_codec_destroy(frame_codec);
2023 }
2024
2025 /* Tests_SRS_FRAME_CODEC_01_016: [The type code indicates the format and purpose of the frame.] */
2026 /* Tests_SRS_FRAME_CODEC_01_017: [The subsequent bytes in the frame header MAY be interpreted differently depending on the type of the frame.] */
2027 /* Tests_SRS_FRAME_CODEC_01_018: [A type code of 0x00 indicates that the frame is an AMQP frame.] */
2028 /* Tests_SRS_FRAME_CODEC_01_070: [The type code indicates the format and purpose of the frame.] */
2029 /* Tests_SRS_FRAME_CODEC_01_071: [The subsequent bytes in the frame header MAY be interpreted differently depending on the type of the frame.] */
2030 /* Tests_SRS_FRAME_CODEC_01_072: [A type code of 0x00 indicates that the frame is an AMQP frame.] */
2031 TEST_FUNCTION(frame_type_amqp_is_zero)
2032 {
2033 // arrange
2034
2035 // act
2036
2037 // assert
2038 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2039 ASSERT_ARE_EQUAL(uint8_t, 0, FRAME_TYPE_AMQP);
2040 }
2041
2042 /* Tests_SRS_FRAME_CODEC_01_016: [The type code indicates the format and purpose of the frame.] */
2043 /* Tests_SRS_FRAME_CODEC_01_017: [The subsequent bytes in the frame header MAY be interpreted differently depending on the type of the frame.] */
2044 /* Tests_SRS_FRAME_CODEC_01_019: [A type code of 0x01 indicates that the frame is a SASL frame] */
2045 /* Tests_SRS_FRAME_CODEC_01_070: [The type code indicates the format and purpose of the frame.] */
2046 /* Tests_SRS_FRAME_CODEC_01_071: [The subsequent bytes in the frame header MAY be interpreted differently depending on the type of the frame.] */
2047 /* Tests_SRS_FRAME_CODEC_01_073: [A type code of 0x01 indicates that the frame is a SASL frame] */
2048 TEST_FUNCTION(frame_type_sasl_is_one)
2049 {
2050 // arrange
2051
2052 // act
2053
2054 // assert
2055 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2056 ASSERT_ARE_EQUAL(uint8_t, 1, FRAME_TYPE_SASL);
2057 }
2058
2059 /* frame_codec_encode_frame */
2060
2061 /* Tests_SRS_FRAME_CODEC_01_042: [frame_codec_encode_frame encodes the header, type specific bytes and frame payload of a frame that has frame_payload_size bytes.]*/
2062 /* Tests_SRS_FRAME_CODEC_01_043: [On success it shall return 0.] */
2063 /* Tests_SRS_FRAME_CODEC_01_088: [Encoded bytes shall be passed to the `on_bytes_encoded` callback in a single call, while setting the `encode complete` argument to true.] */
2064 /* Tests_SRS_FRAME_CODEC_01_055: [Frames are divided into three distinct areas: a fixed width frame header, a variable width extended header, and a variable width frame body.] */
2065 /* Tests_SRS_FRAME_CODEC_01_056: [frame header The frame header is a fixed size (8 byte) structure that precedes each frame.] */
2066 /* Tests_SRS_FRAME_CODEC_01_057: [The frame header includes mandatory information necessary to parse the rest of the frame including size and type information.] */
2067 /* Tests_SRS_FRAME_CODEC_01_058: [extended header The extended header is a variable width area preceding the frame body.] */
2068 /* Tests_SRS_FRAME_CODEC_01_059: [This is an extension point defined for future expansion.] */
2069 /* Tests_SRS_FRAME_CODEC_01_060: [The treatment of this area depends on the frame type.] */
2070 /* Tests_SRS_FRAME_CODEC_01_062: [SIZE Bytes 0-3 of the frame header contain the frame size.] */
2071 /* Tests_SRS_FRAME_CODEC_01_063: [This is an unsigned 32-bit integer that MUST contain the total frame size of the frame header, extended header, and frame body.] */
2072 /* Tests_SRS_FRAME_CODEC_01_064: [The frame is malformed if the size is less than the size of the frame header (8 bytes).] */
2073 TEST_FUNCTION(frame_codec_encode_frame_with_a_zero_frame_body_length_succeeds)
2074 {
2075 // arrange
2076 int result;
2077 FRAME_CODEC_HANDLE frame_codec = frame_codec_create(test_frame_codec_decode_error, TEST_ERROR_CONTEXT);
2078 umock_c_reset_all_calls();
2079
2080 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG));
2081 STRICT_EXPECTED_CALL(test_on_bytes_encoded((void*)0x4242, IGNORED_PTR_ARG, IGNORED_NUM_ARG, true));
2082 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG));
2083
2084 // act
2085 result = frame_codec_encode_frame(frame_codec, 0, NULL, 0, NULL, 0, test_on_bytes_encoded, (void*)0x4242);
2086
2087 // assert
2088 stringify_bytes(sent_io_bytes, sent_io_byte_count, actual_stringified_io);
2089 ASSERT_ARE_EQUAL(char_ptr, "[0x00,0x00,0x00,0x08,0x02,0x00,0x00,0x00]", actual_stringified_io);
2090 ASSERT_ARE_EQUAL(int, 0, result);
2091 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2092
2093 // cleanup
2094 frame_codec_destroy(frame_codec);
2095 }
2096
2097 /* Tests_SRS_FRAME_CODEC_01_044: [If any of arguments `frame_codec` or `on_bytes_encoded` is NULL, `frame_codec_encode_frame` shall return a non-zero value.] */
2098 TEST_FUNCTION(when_frame_codec_is_NULL_frame_codec_encode_frame_fails)
2099 {
2100 // arrange
2101
2102 // act
2103 int result = frame_codec_encode_frame(NULL, 0, NULL, 0, NULL, 0, test_on_bytes_encoded, (void*)0x4242);
2104
2105 // assert
2106 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2107 ASSERT_ARE_NOT_EQUAL(int, 0, result);
2108 }
2109
2110 /* Tests_SRS_FRAME_CODEC_01_044: [If any of arguments `frame_codec` or `on_bytes_encoded` is NULL, `frame_codec_encode_frame` shall return a non-zero value.] */
2111 TEST_FUNCTION(when_on_bytes_encoded_is_NULL_frame_codec_encode_frame_fails)
2112 {
2113 // arrange
2114 int result;
2115 FRAME_CODEC_HANDLE frame_codec = frame_codec_create(test_frame_codec_decode_error, TEST_ERROR_CONTEXT);
2116 umock_c_reset_all_calls();
2117
2118 // act
2119 result = frame_codec_encode_frame(frame_codec, 0, NULL, 0, NULL, 0, NULL, (void*)0x4242);
2120
2121 // assert
2122 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2123 ASSERT_ARE_NOT_EQUAL(int, 0, result);
2124
2125 // cleanup
2126 frame_codec_destroy(frame_codec);
2127 }
2128
2129 /* Tests_SRS_FRAME_CODEC_01_091: [If the argument type_specific_size is greater than 0 and type_specific_bytes is NULL, frame_codec_encode_frame shall return a non-zero value.] */
2130 TEST_FUNCTION(when_type_specific_size_is_positive_and_type_specific_bytes_is_NULL_frame_codec_encode_frame_fails)
2131 {
2132 // arrange
2133 int result;
2134 FRAME_CODEC_HANDLE frame_codec = frame_codec_create(test_frame_codec_decode_error, TEST_ERROR_CONTEXT);
2135 umock_c_reset_all_calls();
2136
2137 // act
2138 result = frame_codec_encode_frame(frame_codec, 0, NULL, 0, NULL, 1, test_on_bytes_encoded, (void*)0x4242);
2139
2140 // assert
2141 ASSERT_ARE_NOT_EQUAL(int, 0, result);
2142 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2143
2144 // cleanup
2145 frame_codec_destroy(frame_codec);
2146 }
2147
2148 /* Tests_SRS_FRAME_CODEC_01_092: [If type_specific_size is too big to allow encoding the frame according to the AMQP ISO then frame_codec_encode_frame shall return a non-zero value.] */
2149 /* Tests_SRS_FRAME_CODEC_01_065: [DOFF Byte 4 of the frame header is the data offset.] */
2150 /* Tests_SRS_FRAME_CODEC_01_066: [This gives the position of the body within the frame.] */
2151 /* Tests_SRS_FRAME_CODEC_01_058: [extended header The extended header is a variable width area preceding the frame body.] */
2152 TEST_FUNCTION(when_type_specific_size_is_too_big_then_frame_codec_encode_frame_fails)
2153 {
2154 // arrange
2155 int result;
2156 unsigned char expected_frame[1020] = { 0x00, 0x00, 0x00, 0x0A, 0xFF, 0x00, 0x00, 0x00 };
2157 FRAME_CODEC_HANDLE frame_codec = frame_codec_create(test_frame_codec_decode_error, TEST_ERROR_CONTEXT);
2158 (void)frame_codec_set_max_frame_size(frame_codec, 4096);
2159 umock_c_reset_all_calls();
2160
2161 // act
2162 result = frame_codec_encode_frame(frame_codec, 0, NULL, 0, &expected_frame[6], 1015, test_on_bytes_encoded, (void*)0x4242);
2163
2164 // assert
2165 ASSERT_ARE_NOT_EQUAL(int, 0, result);
2166 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2167
2168 // cleanup
2169 frame_codec_destroy(frame_codec);
2170 }
2171
2172 /* Tests_SRS_FRAME_CODEC_01_092: [If type_specific_size is too big to allow encoding the frame according to the AMQP ISO then frame_codec_encode_frame shall return a non-zero value.] */
2173 /* Tests_SRS_FRAME_CODEC_01_065: [DOFF Byte 4 of the frame header is the data offset.] */
2174 /* Tests_SRS_FRAME_CODEC_01_066: [This gives the position of the body within the frame.] */
2175 /* Tests_SRS_FRAME_CODEC_01_058: [extended header The extended header is a variable width area preceding the frame body.] */
2176 /* Tests_SRS_FRAME_CODEC_01_065: [DOFF Byte 4 of the frame header is the data offset.] */
2177 /* Tests_SRS_FRAME_CODEC_01_066: [This gives the position of the body within the frame.] */
2178 /* Tests_SRS_FRAME_CODEC_01_067: [The value of the data offset is an unsigned, 8-bit integer specifying a count of 4-byte words.] */
2179 /* Tests_SRS_FRAME_CODEC_01_063: [This is an unsigned 32-bit integer that MUST contain the total frame size of the frame header, extended header, and frame body.] */
2180 TEST_FUNCTION(when_type_specific_size_is_max_allowed_then_frame_codec_encode_frame_succeeds)
2181 {
2182 // arrange
2183 int result;
2184 unsigned char expected_frame[1020] = { 0x00, 0x00, 0x03, 0xFC, 0xFF, 0x00, 0x00, 0x00 };
2185 FRAME_CODEC_HANDLE frame_codec = frame_codec_create(test_frame_codec_decode_error, TEST_ERROR_CONTEXT);
2186 (void)frame_codec_set_max_frame_size(frame_codec, 4096);
2187 umock_c_reset_all_calls();
2188
2189 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG));
2190 STRICT_EXPECTED_CALL(test_on_bytes_encoded((void*)0x4242, IGNORED_PTR_ARG, IGNORED_NUM_ARG, true));
2191 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG));
2192
2193 // act
2194 result = frame_codec_encode_frame(frame_codec, 0, NULL, 0, &expected_frame[6], 1014, test_on_bytes_encoded, (void*)0x4242);
2195
2196 // assert
2197 memset(expected_frame + 6, 0, 1020 - 6);
2198 stringify_bytes(expected_frame, sizeof(expected_frame), expected_stringified_io);
2199 stringify_bytes(sent_io_bytes, sent_io_byte_count, actual_stringified_io);
2200 ASSERT_ARE_EQUAL(char_ptr, expected_stringified_io, actual_stringified_io);
2201 ASSERT_ARE_EQUAL(int, 0, result);
2202 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2203
2204 // cleanup
2205 frame_codec_destroy(frame_codec);
2206 }
2207
2208 /* Tests_SRS_FRAME_CODEC_01_090: [If the type_specific_size - 2 does not divide by 4, frame_codec_encode_frame shall pad the type_specific bytes with zeroes so that type specific data is according to the AMQP ISO.] */
2209 /* Tests_SRS_FRAME_CODEC_01_065: [DOFF Byte 4 of the frame header is the data offset.] */
2210 /* Tests_SRS_FRAME_CODEC_01_066: [This gives the position of the body within the frame.] */
2211 /* Tests_SRS_FRAME_CODEC_01_067: [The value of the data offset is an unsigned, 8-bit integer specifying a count of 4-byte words.] */
2212 /* Tests_SRS_FRAME_CODEC_01_068: [Due to the mandatory 8-byte frame header, the frame is malformed if the value is less than 2.] */
2213 TEST_FUNCTION(one_byte_of_padding_is_added_to_type_specific_data_to_make_the_frame_header)
2214 {
2215 // arrange
2216 int result;
2217 unsigned char expected_frame[] = { 0x00, 0x00, 0x00, 0x8, 0x02, 0x00, 0x42, 0x00 };
2218 FRAME_CODEC_HANDLE frame_codec = frame_codec_create(test_frame_codec_decode_error, TEST_ERROR_CONTEXT);
2219 umock_c_reset_all_calls();
2220
2221 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG));
2222 STRICT_EXPECTED_CALL(test_on_bytes_encoded((void*)0x4242, IGNORED_PTR_ARG, IGNORED_NUM_ARG, true));
2223 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG));
2224
2225 // act
2226 result = frame_codec_encode_frame(frame_codec, 0, NULL, 0, &expected_frame[6], 1, test_on_bytes_encoded, (void*)0x4242);
2227
2228 // assert
2229 stringify_bytes(expected_frame, sizeof(expected_frame), expected_stringified_io);
2230 stringify_bytes(sent_io_bytes, sent_io_byte_count, actual_stringified_io);
2231 ASSERT_ARE_EQUAL(char_ptr, expected_stringified_io, actual_stringified_io);
2232 ASSERT_ARE_EQUAL(int, 0, result);
2233 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2234
2235 // cleanup
2236 frame_codec_destroy(frame_codec);
2237 }
2238
2239 /* Tests_SRS_FRAME_CODEC_01_090: [If the type_specific_size - 2 does not divide by 4, frame_codec_encode_frame shall pad the type_specific bytes with zeroes so that type specific data is according to the AMQP ISO.] */
2240 /* Tests_SRS_FRAME_CODEC_01_065: [DOFF Byte 4 of the frame header is the data offset.] */
2241 /* Tests_SRS_FRAME_CODEC_01_066: [This gives the position of the body within the frame.] */
2242 /* Tests_SRS_FRAME_CODEC_01_067: [The value of the data offset is an unsigned, 8-bit integer specifying a count of 4-byte words.] */
2243 /* Tests_SRS_FRAME_CODEC_01_068: [Due to the mandatory 8-byte frame header, the frame is malformed if the value is less than 2.] */
2244 TEST_FUNCTION(no_bytes_of_padding_are_added_to_type_specific_data_when_enough_bytes_are_there)
2245 {
2246 // arrange
2247 int result;
2248 unsigned char expected_frame[] = { 0x00, 0x00, 0x00, 0x8, 0x02, 0x00, 0x42, 0x00 };
2249 FRAME_CODEC_HANDLE frame_codec = frame_codec_create(test_frame_codec_decode_error, TEST_ERROR_CONTEXT);
2250 umock_c_reset_all_calls();
2251
2252 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG));
2253 STRICT_EXPECTED_CALL(test_on_bytes_encoded((void*)0x4242, IGNORED_PTR_ARG, IGNORED_NUM_ARG, true));
2254 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG));
2255
2256 // act
2257 result = frame_codec_encode_frame(frame_codec, 0, NULL, 0, &expected_frame[6], 2, test_on_bytes_encoded, (void*)0x4242);
2258
2259 // assert
2260 stringify_bytes(expected_frame, sizeof(expected_frame), expected_stringified_io);
2261 stringify_bytes(sent_io_bytes, sent_io_byte_count, actual_stringified_io);
2262 ASSERT_ARE_EQUAL(char_ptr, expected_stringified_io, actual_stringified_io);
2263 ASSERT_ARE_EQUAL(int, 0, result);
2264 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2265
2266 // cleanup
2267 frame_codec_destroy(frame_codec);
2268 }
2269
2270 /* Tests_SRS_FRAME_CODEC_01_069: [TYPE Byte 5 of the frame header is a type code.] */
2271 TEST_FUNCTION(the_type_is_placed_in_the_underlying_frame)
2272 {
2273 // arrange
2274 int result;
2275 FRAME_CODEC_HANDLE frame_codec = frame_codec_create(test_frame_codec_decode_error, TEST_ERROR_CONTEXT);
2276 umock_c_reset_all_calls();
2277
2278 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG));
2279 STRICT_EXPECTED_CALL(test_on_bytes_encoded((void*)0x4242, IGNORED_PTR_ARG, IGNORED_NUM_ARG, true));
2280 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG));
2281
2282 // act
2283 result = frame_codec_encode_frame(frame_codec, 0x42, NULL, 0, NULL, 0, test_on_bytes_encoded, (void*)0x4242);
2284
2285 // assert
2286 stringify_bytes(sent_io_bytes, sent_io_byte_count, actual_stringified_io);
2287 ASSERT_ARE_EQUAL(char_ptr, "[0x00,0x00,0x00,0x08,0x02,0x42,0x00,0x00]", actual_stringified_io);
2288 ASSERT_ARE_EQUAL(int, 0, result);
2289 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2290
2291 // cleanup
2292 frame_codec_destroy(frame_codec);
2293 }
2294
2295 /* Tests_SRS_FRAME_CODEC_01_105: [The frame_payload_size shall be computed by summing up the lengths of the payload segments identified by the payloads argument.]*/
2296 /* ----------- Tests_SRS_FRAME_CODEC_01_048: [If all bytes are successfully encoded, frame_codec_encode_frame_bytes shall return 0.] */
2297 /* Tests_SRS_FRAME_CODEC_01_061: [frame body The frame body is a variable width sequence of bytes the format of which depends on the frame type.] */
2298 TEST_FUNCTION(frame_codec_encode_frame_bytes_with_1_encoded_byte_succeeds)
2299 {
2300 // arrange
2301 int result;
2302 FRAME_CODEC_HANDLE frame_codec = frame_codec_create(test_frame_codec_decode_error, TEST_ERROR_CONTEXT);
2303 PAYLOAD payloads[1];
2304 uint8_t byte = 0x42;
2305 umock_c_reset_all_calls();
2306 payloads[0].bytes = &byte;
2307 payloads[0].length = 1;
2308
2309 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG));
2310 STRICT_EXPECTED_CALL(test_on_bytes_encoded((void*)0x4242, IGNORED_PTR_ARG, IGNORED_NUM_ARG, true));
2311 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG));
2312
2313 // act
2314 result = frame_codec_encode_frame(frame_codec, 0x42, payloads, 1, NULL, 0, test_on_bytes_encoded, (void*)0x4242);
2315
2316 // assert
2317 ASSERT_ARE_EQUAL(int, 0, result);
2318 stringify_bytes(sent_io_bytes, sent_io_byte_count, actual_stringified_io);
2319 ASSERT_ARE_EQUAL(char_ptr, "[0x00,0x00,0x00,0x09,0x02,0x42,0x00,0x00,0x42]", actual_stringified_io);
2320 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2321
2322 // cleanup
2323 frame_codec_destroy(frame_codec);
2324 }
2325
2326 /* ----------- Tests_SRS_FRAME_CODEC_01_047: [frame_codec_encode_frame_bytes encodes the frame bytes for a frame encoding started with a frame_codec_start_encode_frame call.] */
2327 /* ----------- Tests_SRS_FRAME_CODEC_01_048: [If all bytes are successfully encoded, frame_codec_encode_frame_bytes shall return 0.] */
2328 TEST_FUNCTION(frame_codec_encode_frame_bytes_with_2_bytes_succeeds)
2329 {
2330 // arrange
2331 int result;
2332 unsigned char bytes[] = { 0x42, 0x43 };
2333 PAYLOAD payloads[1];
2334 FRAME_CODEC_HANDLE frame_codec = frame_codec_create(test_frame_codec_decode_error, TEST_ERROR_CONTEXT);
2335 payloads[0].bytes = bytes;
2336 payloads[0].length = sizeof(bytes);
2337 umock_c_reset_all_calls();
2338
2339 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG));
2340 STRICT_EXPECTED_CALL(test_on_bytes_encoded((void*)0x4242, IGNORED_PTR_ARG, IGNORED_NUM_ARG, true));
2341 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG));
2342
2343 // act
2344 result = frame_codec_encode_frame(frame_codec, 0x42, payloads, 1, NULL, 0, test_on_bytes_encoded, (void*)0x4242);
2345
2346 // assert
2347 ASSERT_ARE_EQUAL(int, 0, result);
2348 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2349
2350 // cleanup
2351 frame_codec_destroy(frame_codec);
2352 }
2353
2354 /* Tests_SRS_FRAME_CODEC_01_110: [ If the `bytes` member of a payload entry is NULL, `frame_codec_encode_frame` shall fail and return a non-zero value. ] */
2355 TEST_FUNCTION(frame_codec_encode_frame_bytes_with_NULL_bytes_fails)
2356 {
2357 // arrange
2358 int result;
2359 FRAME_CODEC_HANDLE frame_codec = frame_codec_create(test_frame_codec_decode_error, TEST_ERROR_CONTEXT);
2360 PAYLOAD payloads[1];
2361 payloads[0].bytes = NULL;
2362 payloads[0].length = 1;
2363 umock_c_reset_all_calls();
2364
2365 // act
2366 result = frame_codec_encode_frame(frame_codec, 0x42, payloads, 1, NULL, 0, test_on_bytes_encoded, (void*)0x4242);
2367
2368 // assert
2369 ASSERT_ARE_NOT_EQUAL(int, 0, result);
2370 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2371
2372 // cleanup
2373 frame_codec_destroy(frame_codec);
2374 }
2375
2376 /* Tests_SRS_FRAME_CODEC_01_111: [ If the `length` member of a payload entry is 0, `frame_codec_encode_frame` shall fail and return a non-zero value. ] */
2377 TEST_FUNCTION(frame_codec_encode_frame_bytes_with_zero_length_fails)
2378 {
2379 // arrange
2380 int result;
2381 unsigned char bytes[] = { 0x42, 0x43 };
2382 PAYLOAD payloads[1];
2383 FRAME_CODEC_HANDLE frame_codec = frame_codec_create(test_frame_codec_decode_error, TEST_ERROR_CONTEXT);
2384 payloads[0].bytes = bytes;
2385 payloads[0].length = 0;
2386 umock_c_reset_all_calls();
2387
2388 // act
2389 result = frame_codec_encode_frame(frame_codec, 0x42, payloads, 1, NULL, 0, test_on_bytes_encoded, (void*)0x4242);
2390
2391 // assert
2392 ASSERT_ARE_NOT_EQUAL(int, 0, result);
2393 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2394
2395 // cleanup
2396 frame_codec_destroy(frame_codec);
2397 }
2398
2399 /* Tests_SRS_FRAME_CODEC_01_105: [The frame_payload_size shall be computed by summing up the lengths of the payload segments identified by the payloads argument.] */
2400 /* Tests_SRS_FRAME_CODEC_01_106: [All payloads shall be encoded in order as part of the frame.] */
2401 TEST_FUNCTION(sending_only_1_byte_out_of_2_frame_body_bytes_succeeds)
2402 {
2403 // arrange
2404 int result;
2405 unsigned char bytes[] = { 0x42, 0x43 };
2406 PAYLOAD payloads[2];
2407 FRAME_CODEC_HANDLE frame_codec = frame_codec_create(test_frame_codec_decode_error, TEST_ERROR_CONTEXT);
2408 payloads[0].bytes = bytes;
2409 payloads[0].length = 1;
2410 payloads[1].bytes = bytes + 1;
2411 payloads[1].length = 1;
2412 umock_c_reset_all_calls();
2413
2414 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG));
2415 STRICT_EXPECTED_CALL(test_on_bytes_encoded((void*)0x4242, IGNORED_PTR_ARG, IGNORED_NUM_ARG, true));
2416 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG));
2417
2418 // act
2419 result = frame_codec_encode_frame(frame_codec, 0x42, payloads, 2, NULL, 0, test_on_bytes_encoded, (void*)0x4242);
2420
2421 // assert
2422 ASSERT_ARE_EQUAL(int, 0, result);
2423 stringify_bytes(sent_io_bytes, sent_io_byte_count, actual_stringified_io);
2424 ASSERT_ARE_EQUAL(char_ptr, "[0x00,0x00,0x00,0x0A,0x02,0x42,0x00,0x00,0x42,0x43]", actual_stringified_io);
2425 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2426
2427 // cleanup
2428 frame_codec_destroy(frame_codec);
2429 }
2430
2431 /* Tests_SRS_FRAME_CODEC_01_105: [The frame_payload_size shall be computed by summing up the lengths of the payload segments identified by the payloads argument.] */
2432 /* Tests_SRS_FRAME_CODEC_01_106: [All payloads shall be encoded in order as part of the frame.] */
2433 TEST_FUNCTION(a_send_after_send_succeeds)
2434 {
2435 // arrange
2436 int result;
2437 unsigned char bytes[] = { 0x42, 0x43 };
2438 PAYLOAD payloads[1];
2439 FRAME_CODEC_HANDLE frame_codec = frame_codec_create(test_frame_codec_decode_error, TEST_ERROR_CONTEXT);
2440 payloads[0].bytes = bytes;
2441 payloads[0].length = 2;
2442 (void)frame_codec_encode_frame(frame_codec, 0x42, payloads, 1, NULL, 0, test_on_bytes_encoded, (void*)0x4242);
2443 umock_c_reset_all_calls();
2444
2445 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG));
2446 STRICT_EXPECTED_CALL(test_on_bytes_encoded((void*)0x4242, IGNORED_PTR_ARG, IGNORED_NUM_ARG, true));
2447 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG));
2448
2449 // act
2450 result = frame_codec_encode_frame(frame_codec, 0x42, payloads, 1, NULL, 0, test_on_bytes_encoded, (void*)0x4242);
2451
2452 // assert
2453 stringify_bytes(sent_io_bytes, sent_io_byte_count, actual_stringified_io);
2454 ASSERT_ARE_EQUAL(char_ptr, "[0x00,0x00,0x00,0x0A,0x02,0x42,0x00,0x00,0x42,0x43,0x00,0x00,0x00,0x0A,0x02,0x42,0x00,0x00,0x42,0x43]", actual_stringified_io);
2455 ASSERT_ARE_EQUAL(int, 0, result);
2456 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2457
2458 // cleanup
2459 frame_codec_destroy(frame_codec);
2460 }
2461
2462 /* Tests_SRS_FRAME_CODEC_01_109: [ If allocating memory fails, `frame_codec_encode_frame` shall fail and return a non-zero value. ]*/
2463 TEST_FUNCTION(when_allocating_memory_for_the_encoded_frame_fails_frame_codec_encode_frame_fails)
2464 {
2465 // arrange
2466 int result;
2467 FRAME_CODEC_HANDLE frame_codec = frame_codec_create(test_frame_codec_decode_error, TEST_ERROR_CONTEXT);
2468 umock_c_reset_all_calls();
2469
2470 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG))
2471 .SetReturn(NULL);
2472
2473 // act
2474 result = frame_codec_encode_frame(frame_codec, 0, NULL, 0, NULL, 0, test_on_bytes_encoded, NULL);
2475
2476 // assert
2477 ASSERT_ARE_NOT_EQUAL(int, 0, result);
2478 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2479
2480 // cleanup
2481 frame_codec_destroy(frame_codec);
2482 }
2483
2484 /* Tests_SRS_FRAME_CODEC_01_107: [If the argument `payloads` is NULL and `payload_count` is non-zero, `frame_codec_encode_frame` shall return a non-zero value.]*/
2485 TEST_FUNCTION(frame_codec_encode_frame_with_NULL_payloads_and_non_zero_payload_count_fails)
2486 {
2487 // arrange
2488 int result;
2489 FRAME_CODEC_HANDLE frame_codec = frame_codec_create(test_frame_codec_decode_error, TEST_ERROR_CONTEXT);
2490 umock_c_reset_all_calls();
2491
2492 // act
2493 result = frame_codec_encode_frame(frame_codec, 0, NULL, 1, NULL, 0, test_on_bytes_encoded, NULL);
2494
2495 // assert
2496 ASSERT_ARE_NOT_EQUAL(int, 0, result);
2497 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
2498
2499 // cleanup
2500 frame_codec_destroy(frame_codec);
2501 }
2502
2503 END_TEST_SUITE(frame_codec_ut)