Debian Codegraph @

src/vendor/azure-uamqp-c/tests/async_operation_ut/async_operation_ut.c

raw
1 // Copyright (c) Microsoft. All rights reserved.
2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
4 #ifdef __cplusplus
5 #include <cstdlib>
6 #include <cstddef>
7 #include <cstdio>
8 #include <cstdint>
9 #else
10 #include <stdlib.h>
11 #include <stddef.h>
12 #include <stdio.h>
13 #include <stdint.h>
14 #endif
15
16 #include "azure_macro_utils/macro_utils.h"
17 #include "testrunnerswitcher.h"
18 #include "umock_c/umock_c.h"
19 #include "umock_c/umocktypes_charptr.h"
20 #include "umock_c/umocktypes_bool.h"
21
22 static void* my_gballoc_malloc(size_t size)
23 {
24 return malloc(size);
25 }
26
27 static void* my_gballoc_realloc(void* ptr, size_t size)
28 {
29 return realloc(ptr, size);
30 }
31
32 static void my_gballoc_free(void* ptr)
33 {
34 free(ptr);
35 }
36
37 #define ENABLE_MOCKS
38
39 #include "azure_c_shared_utility/gballoc.h"
40
41 #undef ENABLE_MOCKS
42
43 #include "azure_uamqp_c/async_operation.h"
44
45 MOCK_FUNCTION_WITH_CODE(, void, test_cancel_handler, ASYNC_OPERATION_HANDLE, async_operation)
46 MOCK_FUNCTION_END()
47
48 static TEST_MUTEX_HANDLE g_testByTest;
49
50 MU_DEFINE_ENUM_STRINGS(UMOCK_C_ERROR_CODE, UMOCK_C_ERROR_CODE_VALUES)
51
52 static void on_umock_c_error(UMOCK_C_ERROR_CODE error_code)
53 {
54 ASSERT_FAIL("umock_c reported error :%" PRI_MU_ENUM "", MU_ENUM_VALUE(UMOCK_C_ERROR_CODE, error_code));
55 }
56
57 BEGIN_TEST_SUITE(async_operation_ut)
58
59 TEST_SUITE_INITIALIZE(suite_init)
60 {
61 int result;
62
63 g_testByTest = TEST_MUTEX_CREATE();
64 ASSERT_IS_NOT_NULL(g_testByTest);
65
66 umock_c_init(on_umock_c_error);
67
68 result = umocktypes_charptr_register_types();
69 ASSERT_ARE_EQUAL(int, 0, result);
70
71 REGISTER_GLOBAL_MOCK_HOOK(gballoc_malloc, my_gballoc_malloc);
72 REGISTER_GLOBAL_MOCK_HOOK(gballoc_realloc, my_gballoc_realloc);
73 REGISTER_GLOBAL_MOCK_HOOK(gballoc_free, my_gballoc_free);
74
75 REGISTER_UMOCK_ALIAS_TYPE(ASYNC_OPERATION_HANDLE, void*);
76 }
77
78 TEST_SUITE_CLEANUP(suite_cleanup)
79 {
80 umock_c_deinit();
81
82 TEST_MUTEX_DESTROY(g_testByTest);
83 }
84
85 TEST_FUNCTION_INITIALIZE(test_function_init)
86 {
87 if (TEST_MUTEX_ACQUIRE(g_testByTest))
88 {
89 ASSERT_FAIL("our mutex is ABANDONED. Failure in test framework");
90 }
91
92 umock_c_reset_all_calls();
93 }
94
95 TEST_FUNCTION_CLEANUP(test_function_cleanup)
96 {
97 TEST_MUTEX_RELEASE(g_testByTest);
98 }
99
100 /* async_operation_create */
101
102 /* Tests_SRS_ASYNC_OPERATION_01_001: [ `async_operation_create` shall return a non-NULL handle to a newly created asynchronous operation instance.]*/
103 TEST_FUNCTION(async_operation_create_succeeds)
104 {
105 // arrange
106 ASYNC_OPERATION_HANDLE result;
107 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG));
108
109 // act
110 result = async_operation_create(test_cancel_handler, sizeof(uintptr_t));
111
112 // assert
113 ASSERT_IS_NOT_NULL(result);
114 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
115
116 // cleanup
117 async_operation_destroy(result);
118 }
119
120 /* Tests_SRS_ASYNC_OPERATION_01_002: [ If `async_operation_cancel_handler` is NULL, `async_operation_create` shall fail and return NULL.]*/
121 TEST_FUNCTION(async_operation_create_with_NULL_cancel_handler_fails)
122 {
123 // arrange
124 ASYNC_OPERATION_HANDLE result;
125
126 // act
127 result = async_operation_create(NULL, 64);
128
129 // assert
130 ASSERT_IS_NULL(result);
131 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
132 }
133
134 /* Tests_SRS_ASYNC_OPERATION_01_003: [ If `context_size` is less than the size of the `async_operation_cancel_handler` argument, `async_operation_create` shall fail and return NULL.]*/
135 TEST_FUNCTION(async_operation_create_with_not_enough_context_size_fails)
136 {
137 // arrange
138 ASYNC_OPERATION_HANDLE result;
139
140 // act
141 result = async_operation_create(test_cancel_handler, sizeof(uintptr_t) - 1);
142
143 // assert
144 ASSERT_IS_NULL(result);
145 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
146 }
147
148 /* Tests_SRS_ASYNC_OPERATION_01_004: [ If allocating memory for the new asynchronous operation instance fails, `async_operation_create` shall fail and return NULL.]*/
149 TEST_FUNCTION(when_allocating_memory_fails_async_operation_create_fails)
150 {
151 // arrange
152 ASYNC_OPERATION_HANDLE result;
153 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG))
154 .SetReturn(NULL);
155
156 // act
157 result = async_operation_create(test_cancel_handler, sizeof(uintptr_t));
158
159 // assert
160 ASSERT_IS_NULL(result);
161 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
162 }
163
164 /* async_operation_destroy */
165
166 /* Tests_SRS_ASYNC_OPERATION_01_005: [ `async_operation_destroy` shall free all recources associated with the asyncronous operation instance.]*/
167 TEST_FUNCTION(async_operation_destroy_frees_allocated_memory)
168 {
169 // arrange
170 ASYNC_OPERATION_HANDLE async_operation;
171 async_operation = async_operation_create(test_cancel_handler, sizeof(uintptr_t));
172 umock_c_reset_all_calls();
173
174 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG));
175
176 // act
177 async_operation_destroy(async_operation);
178
179 // assert
180 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
181 }
182
183 /* Tests_SRS_ASYNC_OPERATION_01_006: [ If `async_operation` is NULL, `async_operation_destroy` shall do nothing.]*/
184 TEST_FUNCTION(async_operation_destroy_with_NULL_async_operation_does_not_free_anything)
185 {
186 // arrange
187
188 // act
189 async_operation_destroy(NULL);
190
191 // assert
192 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
193 }
194
195 /* async_operation_cancel */
196
197 /* Tests_SRS_ASYNC_OPERATION_01_007: [ `async_operation_cancel` shall cancel the operation by calling the cancel handler function passed to `async_operation_create`.]*/
198 /* Tests_SRS_ASYNC_OPERATION_01_008: [ On success `async_operation_cancel` shall return 0.]*/
199 TEST_FUNCTION(async_operation_cancel_calls_the_cancel_handles)
200 {
201 // arrange
202 ASYNC_OPERATION_HANDLE async_operation;
203 int result;
204 async_operation = async_operation_create(test_cancel_handler, sizeof(uintptr_t));
205 umock_c_reset_all_calls();
206
207 STRICT_EXPECTED_CALL(test_cancel_handler(async_operation));
208
209 // act
210 result = async_operation_cancel(async_operation);
211
212 // assert
213 ASSERT_ARE_EQUAL(int, 0, result);
214 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
215
216 // cleanup
217 async_operation_destroy(async_operation);
218 }
219
220 /* Tests_SRS_ASYNC_OPERATION_01_009: [ If `async_operation` is NULL, `async_operation_cancel` shall fail and return a non-zero value.]*/
221 TEST_FUNCTION(async_operation_cancel_with_NULL_async_operation_fails)
222 {
223 // arrange
224 int result;
225
226 // act
227 result = async_operation_cancel(NULL);
228
229 // assert
230 ASSERT_ARE_NOT_EQUAL(int, 0, result);
231 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
232 }
233
234 END_TEST_SUITE(async_operation_ut)