Debian Codegraph @

src/vendor/azure-uamqp-c/deps/azure-c-shared-utility/tests/condition_ut/condition_ut.c

raw
1 // Copyright (c) Microsoft. All rights reserved.
2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
4 #ifdef __cplusplus
5 #include <cstdlib>
6 #include <cstddef>
7 #else
8 #include <stdlib.h>
9 #include <stddef.h>
10 #endif
11
12 #include "azure_macro_utils/macro_utils.h"
13 #include "testrunnerswitcher.h"
14 #include "azure_c_shared_utility/crt_abstractions.h"
15 #include "azure_c_shared_utility/condition.h"
16 #include "azure_c_shared_utility/lock.h"
17 #include "azure_c_shared_utility/threadapi.h"
18
19 #define ENABLE_MOCKS
20
21 #include "umock_c/umock_c_prod.h"
22
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 MOCKABLE_FUNCTION(, void*, gballoc_malloc, size_t, size);
27 MOCKABLE_FUNCTION(, void*, gballoc_calloc, size_t, nmemb, size_t, size);
28 MOCKABLE_FUNCTION(, void, gballoc_free, void*, ptr);
29 #ifdef __cplusplus
30 }
31 #endif
32
33 #include "umock_c/umock_c.h"
34
35 #define GBALLOC_H
36
37 void* real_gballoc_malloc(size_t size);
38 void* real_gballoc_calloc(size_t nmemb, size_t size);
39 void* real_gballoc_realloc(void* ptr, size_t size);
40 void real_gballoc_free(void* ptr);
41
42 #define TEST_LOCK_HANDLE (LOCK_HANDLE)0x4443
43 #define TEST_TIME ((double)3600)
44 #define TEST_TIME_T ((time_t)TEST_TIME)
45 #define CONDITION_WAIT_MS 2000
46
47 static TEST_MUTEX_HANDLE g_testByTest;
48
49 static bool malloc_will_fail = false;
50 static bool calloc_will_fail = false;
51
52 TEST_DEFINE_ENUM_TYPE(COND_RESULT, COND_RESULT_VALUES)
53
54 #ifdef __cplusplus
55 extern "C" {
56 #endif
57
58 void* my_gballoc_malloc(size_t size)
59 {
60 void* result = NULL;
61 if (malloc_will_fail == false)
62 {
63 result = real_gballoc_malloc(size);
64 }
65
66 return result;
67 }
68
69 void* my_gballoc_calloc(size_t nmemb, size_t size)
70 {
71 void* result = NULL;
72 if (calloc_will_fail == false)
73 {
74 result = real_gballoc_calloc(nmemb, size);
75 }
76
77 return result;
78 }
79
80 void my_gballoc_free(void* ptr)
81 {
82 real_gballoc_free(ptr);
83 }
84
85 #ifdef __cplusplus
86 }
87 #endif
88
89 MU_DEFINE_ENUM_STRINGS(UMOCK_C_ERROR_CODE, UMOCK_C_ERROR_CODE_VALUES)
90
91 static void on_umock_c_error(UMOCK_C_ERROR_CODE error_code)
92 {
93 ASSERT_FAIL("umock_c reported error :%" PRI_MU_ENUM "", MU_ENUM_VALUE(UMOCK_C_ERROR_CODE, error_code));
94 }
95
96 COND_RESULT Condition_Handle_ToString(COND_HANDLE handle)
97 {
98 COND_RESULT result = COND_OK;
99 if (handle == NULL)
100 {
101 result = COND_ERROR;
102 }
103 return result;
104 }
105
106 BEGIN_TEST_SUITE(Condition_UnitTests)
107
108 TEST_SUITE_INITIALIZE(a)
109 {
110 g_testByTest = TEST_MUTEX_CREATE();
111 ASSERT_IS_NOT_NULL(g_testByTest);
112
113 umock_c_init(on_umock_c_error);
114
115 REGISTER_GLOBAL_MOCK_HOOK(gballoc_malloc, my_gballoc_malloc);
116 REGISTER_GLOBAL_MOCK_HOOK(gballoc_calloc, my_gballoc_calloc);
117 REGISTER_GLOBAL_MOCK_HOOK(gballoc_free, my_gballoc_free);
118 }
119
120 TEST_SUITE_CLEANUP(b)
121 {
122 umock_c_deinit();
123
124 TEST_MUTEX_DESTROY(g_testByTest);
125 }
126
127 TEST_FUNCTION_INITIALIZE(f)
128 {
129 if (TEST_MUTEX_ACQUIRE(g_testByTest))
130 {
131 ASSERT_FAIL("our mutex is ABANDONED. Failure in test framework");
132 }
133
134 umock_c_reset_all_calls();
135 malloc_will_fail = false;
136 calloc_will_fail = false;
137 }
138
139 TEST_FUNCTION_CLEANUP(cleans)
140 {
141 TEST_MUTEX_RELEASE(g_testByTest);
142 }
143
144 // Tests_SRS_CONDITION_18_002: [ Condition_Init shall create and return a CONDITION_HANDLE ]
145 TEST_FUNCTION(Condition_Init_Success)
146 {
147 //arrange
148 COND_HANDLE handle = NULL;
149 EXPECTED_CALL(gballoc_malloc(4));
150 EXPECTED_CALL(gballoc_free(0));
151
152 //act
153 handle = Condition_Init();
154
155 //assert
156 ASSERT_IS_NOT_NULL(handle);
157
158 //free
159 Condition_Deinit(handle);
160 }
161
162 // Tests_SRS_CONDITION_18_001: [ Condition_Post shall return COND_INVALID_ARG if handle is NULL ]
163 TEST_FUNCTION(Condition_Post_Handle_NULL_Failure)
164 {
165 //arrange
166 COND_RESULT result;
167
168 //act
169 result = Condition_Post(NULL);
170
171 //assert
172 ASSERT_ARE_EQUAL(COND_RESULT, COND_INVALID_ARG, result);
173
174 //free
175 }
176
177 // Tests_SRS_CONDITION_18_003: [ Condition_Post shall return COND_OK if it succcessfully posts the condition ]
178 TEST_FUNCTION(Condition_Post_Handle_Succeed)
179 {
180 //arrange
181 COND_HANDLE handle = NULL;
182 COND_RESULT result;
183
184 EXPECTED_CALL(gballoc_malloc(4));
185 EXPECTED_CALL(gballoc_free(0));
186
187 handle = Condition_Init();
188
189 //act
190 result = Condition_Post(handle);
191
192 //assert
193 ASSERT_ARE_EQUAL(COND_RESULT, COND_OK, result);
194
195 //free
196 Condition_Deinit(handle);
197 }
198
199 // Tests_SRS_CONDITION_18_004: [ Condition_Wait shall return COND_INVALID_ARG if handle is NULL ]
200 TEST_FUNCTION(Condition_Wait_Handle_NULL_Fail)
201 {
202 //arrange
203 COND_RESULT result;
204 LOCK_HANDLE lock;
205
206 lock = Lock_Init();
207
208 //act
209 result = Condition_Wait(NULL, lock, 0);
210
211 //assert
212 ASSERT_ARE_EQUAL(COND_RESULT, COND_INVALID_ARG, result);
213
214 //free
215 Lock_Deinit(lock);
216 }
217
218 // Tests_SRS_CONDITION_18_005: [ Condition_Wait shall return COND_INVALID_ARG if lock is NULL and timeout_milliseconds is 0 ]
219 TEST_FUNCTION(Condition_Wait_LOCK_NULL_Fail)
220 {
221 //arrange
222 COND_HANDLE handle = NULL;
223 COND_RESULT result;
224 EXPECTED_CALL(gballoc_malloc(4));
225 EXPECTED_CALL(gballoc_free(0));
226
227 handle = Condition_Init();
228
229 //act
230 result = Condition_Wait(handle, NULL, 0);
231
232 //assert
233 ASSERT_ARE_EQUAL(COND_RESULT, COND_INVALID_ARG, result);
234
235 //free
236 Condition_Deinit(handle);
237 }
238
239 // Tests_SRS_CONDITION_18_006: [ Condition_Wait shall return COND_INVALID_ARG if lock is NULL and timeout_milliseconds is not 0. ]
240 TEST_FUNCTION(Condition_Wait_LOCK_NULL_Ms_Fail)
241 {
242 //arrange
243 COND_HANDLE handle = NULL;
244 COND_RESULT result;
245 EXPECTED_CALL(gballoc_malloc(4));
246 EXPECTED_CALL(gballoc_free(0));
247
248 handle = Condition_Init();
249
250 //act
251 result = Condition_Wait(handle, NULL, CONDITION_WAIT_MS);
252
253 //assert
254 ASSERT_ARE_EQUAL(COND_RESULT, COND_INVALID_ARG, result);
255
256 //free
257 Condition_Deinit(handle);
258 }
259
260 // Tests_SRS_CONDITION_18_007: [ Condition_Deinit will not fail if handle is NULL ]
261 TEST_FUNCTION(Condition_Deinit_Fail)
262 {
263 //arrange
264 COND_HANDLE handle = NULL;
265
266 //act
267
268 //assert
269
270 //free
271 Condition_Deinit(handle);
272 }
273
274 // Tests_SRS_CONDITION_18_008: [ Condition_Init shall return NULL if it fails to allocate the CONDITION_HANDLE ]
275 TEST_FUNCTION(Condition_Init_allocation_fail)
276 {
277 // arrange
278 COND_HANDLE handle = NULL;
279 malloc_will_fail = true;
280
281 EXPECTED_CALL(gballoc_malloc(4));
282
283 // act
284 handle = Condition_Init();
285
286 //assert
287 ASSERT_IS_NULL(handle);
288
289 //free
290 }
291
292 // Tests_SRS_CONDITION_18_009: [ Condition_Deinit will deallocate handle if it is not NULL ]
293 TEST_FUNCTION(Condition_Deinit_deallocates_handle)
294 {
295 // arrange
296 COND_HANDLE handle = NULL;
297 handle = Condition_Init();
298 umock_c_reset_all_calls();
299 EXPECTED_CALL(gballoc_free(NULL));
300
301 // act
302 Condition_Deinit(handle);
303
304 // assert
305
306 }
307
308 typedef struct _tagLockAndConfition {
309 LOCK_HANDLE lock;
310 COND_HANDLE condition;
311 } LockAndCondition;
312
313 static int trigger_thread_proc(void *h)
314 {
315 LockAndCondition *m = (LockAndCondition*)h;
316
317 ThreadAPI_Sleep(50);
318 Lock(m->lock);
319 Condition_Post(m->condition);
320 Unlock(m->lock);
321 return 0;
322 }
323
324 THREAD_HANDLE trigger_after_50_ms(COND_HANDLE handle)
325 {
326 THREAD_HANDLE th = NULL;
327
328 ThreadAPI_Create(&th, trigger_thread_proc, handle);
329
330 return th;
331 }
332
333 // Tests_SRS_CONDITION_18_010: [ Condition_Wait shall return COND_OK if the condition is triggered and timeout_milliseconds is 0 ]
334 TEST_FUNCTION(Condition_Wait_ok_on_trigger_and_zero_timeout)
335 {
336 // arrange
337 LockAndCondition m;
338 THREAD_HANDLE th;
339 COND_RESULT result;
340 m.condition = Condition_Init();
341 m.lock = Lock_Init();
342
343 // act
344 th = trigger_after_50_ms(&m);
345 Lock(m.lock);
346 result = Condition_Wait(m.condition, m.lock, 0);
347 ThreadAPI_Join(th, NULL);
348 Unlock(m.lock);
349
350 // assert
351 ASSERT_ARE_EQUAL(COND_RESULT, COND_OK, result);
352 Lock_Deinit(m.lock);
353 Condition_Deinit(m.condition);
354 umock_c_reset_all_calls();
355 }
356
357 // Tests_SRS_CONDITION_18_011: [Condition_wait shall return COND_TIMEOUT if the condition is NOT triggered and timeout_milliseconds is not 0]
358 TEST_FUNCTION(Condition_Wait_timeout_when_not_triggered)
359 {
360 // arrange
361 LockAndCondition m;
362 COND_RESULT result;
363 m.condition = Condition_Init();
364 m.lock = Lock_Init();
365
366 // act
367 Lock(m.lock);
368 result = Condition_Wait(m.condition, m.lock, 150);
369 Unlock(m.lock);
370
371 // assert
372 ASSERT_ARE_EQUAL(COND_RESULT, COND_TIMEOUT, result);
373 Lock_Deinit(m.lock);
374 Condition_Deinit(m.condition);
375 }
376
377 // Tests_SRS_CONDITION_18_012: [ Condition_Wait shall return COND_OK if the condition is triggered and timeout_milliseconds is not 0 ]
378 // Tests_SRS_CONDITION_18_013: [ Condition_Wait shall accept relative timeouts ]
379 TEST_FUNCTION(Condition_Wait_ok_on_trigger_with_timeout)
380 {
381 // arrange
382 LockAndCondition m;
383 COND_RESULT result;
384 THREAD_HANDLE th;
385 m.condition = Condition_Init();
386 m.lock = Lock_Init();
387
388 // act
389 th = trigger_after_50_ms(&m);
390 Lock(m.lock);
391 result = Condition_Wait(m.condition, m.lock, 1000);
392 Unlock(m.lock);
393 ThreadAPI_Join(th, NULL);
394
395 // assert
396 ASSERT_ARE_EQUAL(COND_RESULT, COND_OK, result);
397 Lock_Deinit(m.lock);
398 Condition_Deinit(m.condition);
399 umock_c_reset_all_calls();
400 }
401
402 END_TEST_SUITE(Condition_UnitTests);
403
404 /*if malloc is defined as gballoc_malloc at this moment, there'd be serious trouble*/
405 #define Lock(x) (LOCK_OK + gballocState - gballocState) /*compiler warning about constant in if condition*/
406 #define Unlock(x) (LOCK_OK + gballocState - gballocState)
407 #define Lock_Init() (LOCK_HANDLE)0x42
408 #define Lock_Deinit(x) (LOCK_OK + gballocState - gballocState)
409 #define gballoc_malloc real_gballoc_malloc
410 #define gballoc_realloc real_gballoc_realloc
411 #define gballoc_calloc real_gballoc_calloc
412 #define gballoc_free real_gballoc_free
413 #include "gballoc.c"
414 #undef Lock
415 #undef Unlock
416 #undef Lock_Init
417 #undef Lock_Deinit