Debian Codegraph @

src/vendor/azure-uamqp-c/deps/azure-c-shared-utility/tests/template_ut/template_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 #else
7 #include <stdlib.h>
8 #endif
9
10 /**
11 * The gballoc.h will replace the malloc, free, and realloc by the my_gballoc functions, in this case,
12 * if you define these mock functions after include the gballoc.h, you will create an infinity recursion,
13 * so, places the my_gballoc functions before the #include "azure_c_shared_utility/gballoc.h"
14 */
15 void* my_gballoc_malloc(size_t size)
16 {
17 return malloc(size);
18 }
19
20 void* my_gballoc_realloc(void* ptr, size_t size)
21 {
22 return realloc(ptr, size);
23 }
24
25 void my_gballoc_free(void* ptr)
26 {
27 free(ptr);
28 }
29
30 /**
31 * Include the C standards here.
32 */
33 #ifdef __cplusplus
34 #include <cstddef>
35 #include <ctime>
36 #else
37 #include <stddef.h>
38 #include <time.h>
39 #endif
40
41 /**
42 * Include the test tools.
43 */
44 #include "azure_macro_utils/macro_utils.h"
45 #include "testrunnerswitcher.h"
46 #include "umock_c/umock_c.h"
47 #include "umock_c/umocktypes_charptr.h"
48 #include "umock_c/umock_c_negative_tests.h"
49
50 /**
51 * Include the mockable headers here.
52 * These are the headers that contains the functions that you will replace to execute the test.
53 *
54 * For instance, if you will test a target_create() function in the target.c that calls a callee_open() function
55 * in the callee.c, you must define callee_open() as a mockable function in the callee.h.
56 *
57 * Observe that we will replace the functions in callee.h here, so we don't care about its real implementation,
58 * in fact, on this example, we even have the callee.c.
59 *
60 * Include all header files that you will replace the mockable functions in the ENABLE_MOCKS session below.
61 *
62 */
63 #define ENABLE_MOCKS
64 #include "azure_c_shared_utility/gballoc.h"
65 #include "target/callee.h"
66 #undef ENABLE_MOCKS
67
68 /**
69 * Include the target header after the ENABLE_MOCKS session.
70 */
71 #include "target/target.h"
72
73
74 /**
75 * If your test need constants, this is a good place to define it. For examples:
76 *
77 * #define TEST_CREATE_CONNECTION_HOST_NAME (const char*)"https://test.azure-devices.net"
78 *
79 * static const char* SendBuffer = "Message to send";
80 *
81 */
82 #define SIZEOF_FOO_MEMORY 10
83
84 /**
85 * You can create some global variables that your test will need in some way.
86 */
87 static void* g_GenericPointer;
88
89 /**
90 * Umock error will helps you to identify errors in the test suite or in the way that you are
91 * using it, just keep it as is.
92 */
93 MU_DEFINE_ENUM_STRINGS(UMOCK_C_ERROR_CODE, UMOCK_C_ERROR_CODE_VALUES)
94
95 static void on_umock_c_error(UMOCK_C_ERROR_CODE error_code)
96 {
97 ASSERT_FAIL("umock_c reported error :%" PRI_MU_ENUM "", MU_ENUM_VALUE(UMOCK_C_ERROR_CODE, error_code));
98 }
99
100
101 /**
102 * Create the mock function that will replace your callee functions.
103 * For this example, we will replace the functions open and close of the callee. So we
104 * need to create the mock functions my_callee_open(), and my_callee_close().
105 */
106 bool my_callee_open_must_succeed; //This bool will manually determine the happy and unhappy paths.
107 CALLEE_HANDLE my_callee_open(size_t a)
108 {
109 void* result;
110
111 if(my_callee_open_must_succeed)
112 {
113 // Do something like when callee_open succeed...
114 result = malloc(a);
115 }
116 else
117 {
118 // Do something like when callee_open failed...
119 result = NULL;
120 }
121
122 return result;
123 }
124
125 void my_callee_close(CALLEE_HANDLE handle)
126 {
127 free(handle);
128 }
129
130 /**
131 * This is necessary for the test suite, just keep as is.
132 */
133 static TEST_MUTEX_HANDLE g_testByTest;
134
135 /**
136 * Tests begin here. Give a name for your test, for instance template_ut, use the same
137 * name to close the test suite on END_TEST_SUITE(template_ut), and to identify the
138 * test suit in the main() function
139 *
140 * RUN_TEST_SUITE(template_ut, failedTestCount);
141 *
142 */
143 BEGIN_TEST_SUITE(template_ut)
144
145 /**
146 * This is the place where we initialize the test system. Replace the test name to associate the test
147 * suite with your test cases.
148 * It is called once, before start the tests.
149 */
150 TEST_SUITE_INITIALIZE(a)
151 {
152 int result;
153 g_testByTest = TEST_MUTEX_CREATE();
154 ASSERT_IS_NOT_NULL(g_testByTest);
155
156 (void)umock_c_init(on_umock_c_error);
157
158 result = umocktypes_charptr_register_types();
159 ASSERT_ARE_EQUAL(int, 0, result);
160
161 /**
162 * It is necessary to identify the types defined on your target. With it, the test system will
163 * know how to use it.
164 *
165 * On the target.h example, there is the type TARGET_HANDLE that is a void*
166 */
167 REGISTER_UMOCK_ALIAS_TYPE(CALLEE_HANDLE, void*);
168
169 /**
170 * It is necessary to replace all mockable functions by the mock functions that you created here.
171 * It will tell the test suite to call my_callee_open besides to call the real callee_open.
172 */
173 REGISTER_GLOBAL_MOCK_HOOK(callee_open, my_callee_open);
174 REGISTER_GLOBAL_MOCK_HOOK(callee_close, my_callee_close);
175
176 /**
177 * If you don't care about what there is inside of the function in anyway, and you just need
178 * to control the function return you can use the REGISTER_GLOBAL_MOCK_RETURN and
179 * REGISTER_GLOBAL_MOCK_FAIL_RETURN.
180 *
181 * In the follow example, callee_bar_1 will always return CALLEE_RESULT_OK, so, we don't need to
182 * create the unhappy return; and callee_bar_2 can return CALLEE_RESULT_OK or CALLEE_RESULT_FAIL.
183 */
184 REGISTER_GLOBAL_MOCK_RETURN(callee_bar_1, CALLEE_RESULT_OK);
185 REGISTER_GLOBAL_MOCK_RETURN(callee_bar_2, CALLEE_RESULT_OK);
186 REGISTER_GLOBAL_MOCK_FAIL_RETURN(callee_bar_2, CALLEE_RESULT_FAIL);
187
188 /**
189 * Or you can combine, for example, in the success case malloc will call my_gballoc_malloc, and for
190 * the failed cases, it will return NULL.
191 */
192 REGISTER_GLOBAL_MOCK_FAIL_RETURN(callee_open, NULL); // Fail return for the callee_open.
193 REGISTER_GLOBAL_MOCK_HOOK(gballoc_malloc, my_gballoc_malloc);
194 REGISTER_GLOBAL_MOCK_FAIL_RETURN(gballoc_malloc, NULL);
195 REGISTER_GLOBAL_MOCK_HOOK(gballoc_realloc, my_gballoc_realloc);
196 REGISTER_GLOBAL_MOCK_FAIL_RETURN(gballoc_realloc, NULL);
197 REGISTER_GLOBAL_MOCK_HOOK(gballoc_free, my_gballoc_free);
198
199 /**
200 * You can initialize other global variables here, for instance image that you have a standard void* that will be converted
201 * any pointer that your test needs.
202 */
203 g_GenericPointer = malloc(1);
204 ASSERT_IS_NOT_NULL(g_GenericPointer);
205 }
206
207 /**
208 * The test suite will call this function to cleanup your machine.
209 * It is called only once, after all tests is done.
210 */
211 TEST_SUITE_CLEANUP(TestClassCleanup)
212 {
213 free(g_GenericPointer);
214
215 umock_c_deinit();
216
217 TEST_MUTEX_DESTROY(g_testByTest);
218 }
219
220 /**
221 * The test suite will call this function to prepare the machine for the new test.
222 * It is called before execute each test.
223 */
224 TEST_FUNCTION_INITIALIZE(initialize)
225 {
226 if (TEST_MUTEX_ACQUIRE(g_testByTest))
227 {
228 ASSERT_FAIL("Could not acquire test serialization mutex.");
229 }
230
231 umock_c_reset_all_calls();
232
233 my_callee_open_must_succeed = true; //As default, callee_open will return a valid pointer.
234 }
235
236 /**
237 * The test suite will call this function to cleanup your machine for the next test.
238 * It is called after execute each test.
239 */
240 TEST_FUNCTION_CLEANUP(cleans)
241 {
242 TEST_MUTEX_RELEASE(g_testByTest);
243 }
244
245
246 /* Tests_SRS_TEMPLATE_21_001: [ The target_create shall call callee_open to do stuff and allocate the memory. ]*/
247 TEST_FUNCTION(target_create_call_callee_open__succeed)
248 {
249 ///arrange
250 TARGET_RESULT result;
251
252 /**
253 * The STRICT_EXPECTED_CALL creates a list of functions that we expect that the target calls.
254 * The function umock_c_get_expected_calls() returns this list as a serialized string.
255 * You can determine all parameters, with the expected value, or define that the argument must
256 * be ignored by the test suite.
257 * During the execution, the suit will collect the same information, creating a second list of
258 * called functions.
259 * The function umock_c_get_actual_calls() return this list as a serialized string.
260 */
261 STRICT_EXPECTED_CALL(callee_open(SIZEOF_FOO_MEMORY));
262 STRICT_EXPECTED_CALL(gballoc_malloc(SIZEOF_FOO_MEMORY)); //This is the malloc in the mock my_callee_open().
263 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG)); //This is the malloc in the target_create().
264 STRICT_EXPECTED_CALL(gballoc_free(IGNORED_PTR_ARG)); //This is the free in the target_create().
265
266 ///act
267 result = target_create(SIZEOF_FOO_MEMORY);
268
269 ///assert
270 ASSERT_ARE_EQUAL(int, TARGET_RESULT_OK, result);
271 /**
272 * The follow assert will compare the expected calls with the actual calls. If it is different,
273 * it will show the serialized strings with the differences in the log.
274 */
275 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
276
277 ///cleanup
278 target_destroy();
279 }
280
281 /* Tests_SRS_TEMPLATE_21_001: [ The target_create shall call callee_open to do stuffs and allocate the memory. ]*/
282 /* Tests_SRS_TEMPLATE_21_002: [ If callee_open return error, the target_create shall return TARGET_RESULT_FAIL. ]*/
283 TEST_FUNCTION(target_create_call_callee_open_got_error_manual__failed)
284 {
285 ///arrange
286 TARGET_RESULT result;
287
288 /**
289 * Here we are demonstrating how to manually interfere in the mock function execution.
290 * The target_create() function will call the mock callee_open that will fail because
291 * we set my_callee_open_must_succeed to false.
292 * For this manual unhappy test, you DON'T need to specify the fail return in
293 * REGISTER_GLOBAL_MOCK_FAIL_RETURN(callee_open, NULL)
294 */
295 my_callee_open_must_succeed = false;
296
297 /**
298 * In the path where callee_open failed, no memory will be allocated or free. So, the list
299 * of expected calls only have one function, the callee_open().
300 */
301 STRICT_EXPECTED_CALL(callee_open(SIZEOF_FOO_MEMORY));
302
303 ///act
304 result = target_create(SIZEOF_FOO_MEMORY);
305
306 ///assert
307 ASSERT_ARE_EQUAL(int, TARGET_RESULT_FAIL, result);
308 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
309
310 ///cleanup
311 }
312
313 /* Tests_SRS_TEMPLATE_21_001: [ The target_create shall call callee_open to do stuffs and allocate the memory. ]*/
314 /* Tests_SRS_TEMPLATE_21_002: [ If callee_open return error, the target_create shall return TARGET_RESULT_FAIL. ]*/
315 /* Tests_SRS_TEMPLATE_21_003: [ If there is no memory to control the target_create information, it shall return TARGET_RESULT_OUT_OF_MEMORY. ]*/
316 TEST_FUNCTION(target_create_call_callee_open_automatic_unhappy_paths)
317 {
318 ///arrange
319 TARGET_RESULT result;
320 size_t i;
321 int negativeTestsInitResult = umock_c_negative_tests_init();
322 ASSERT_ARE_EQUAL(int, 0, negativeTestsInitResult);
323
324 /**
325 * Here we are demonstrating how to automatically test the multiples unhappy paths.
326 * The umock_c_negative_tests_ functions will return all possible unhappy cases,
327 * this list is created based on the STRICT_EXPECTED_CALL below.
328 *
329 * For each follow STRICT_EXPECTED_CALL, the code inside of the for loop will call the
330 * target function, and the function in the list will return the values specified
331 * on REGISTER_GLOBAL_MOCK_FAIL_RETURN.
332 *
333 * In this example, there are tree STRICT_EXPECTED_CALL (unhappy path). So,
334 * umock_c_negative_tests_call_count() will returns 3.
335 * In the first interaction of the loop, callee_open() will return NULL.
336 * In the second, the malloc in the mock my_callee_open() will return NULL.
337 * In the third, the malloc in the target_create() will return NULL.
338 *
339 * Observe that the second test do not make sense, because it is testing the mock
340 * function, not the target. In the next example we will reexecute this test
341 * avoiding the second test.
342 */
343 STRICT_EXPECTED_CALL(callee_open(SIZEOF_FOO_MEMORY));
344 STRICT_EXPECTED_CALL(gballoc_malloc(SIZEOF_FOO_MEMORY));
345 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG));
346
347 umock_c_negative_tests_snapshot();
348
349 for (i = 0; i < umock_c_negative_tests_call_count(); i++)
350 {
351 char temp_str[128];
352
353 umock_c_negative_tests_reset();
354 umock_c_negative_tests_fail_call(i);
355
356 (void)sprintf(temp_str, "On failed call %lu", (unsigned long)i);
357
358 ///act
359 result = target_create(SIZEOF_FOO_MEMORY);
360
361 ///assert
362 ASSERT_ARE_NOT_EQUAL(int, TARGET_RESULT_OK, result, temp_str);
363 }
364
365 ///cleanup
366 umock_c_negative_tests_deinit();
367 }
368
369 /* Tests_SRS_TEMPLATE_21_001: [ The target_create shall call callee_open to do stuff and allocate the memory. ]*/
370 /* Tests_SRS_TEMPLATE_21_002: [ If callee_open return error, the target_create shall return TARGET_RESULT_FAIL. ]*/
371 /* Tests_SRS_TEMPLATE_21_003: [ If there is no memory to control the target_create information, it shall return TARGET_RESULT_OUT_OF_MEMORY. ]*/
372 TEST_FUNCTION(target_create_call_callee_open_automatic_unhappy_paths_avoid_nonsence_test)
373 {
374 ///arrange
375 TARGET_RESULT result;
376 int negativeTestsInitResult = umock_c_negative_tests_init();
377 bool runTest[] = { true, false, true };
378 size_t i;
379
380 ASSERT_ARE_EQUAL(int, 0, negativeTestsInitResult);
381
382 /**
383 * As in the previus example, umock_c_negative_tests_call_count() will returns 3.
384 * 1. callee_open() will return NULL.
385 * 2. the malloc in the mock my_callee_open() will return NULL.
386 * 3. the malloc in the target_create() will return NULL.
387 *
388 * But here we will create a simple logit that will avoid the second test, you can
389 * create a better logic to do the same.
390 */
391 STRICT_EXPECTED_CALL(callee_open(SIZEOF_FOO_MEMORY));
392 STRICT_EXPECTED_CALL(gballoc_malloc(SIZEOF_FOO_MEMORY));
393 STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG));
394
395 umock_c_negative_tests_snapshot();
396
397 for (i = 0; i < umock_c_negative_tests_call_count(); i++)
398 {
399 if (runTest[i])
400 {
401 char temp_str[128];
402
403 umock_c_negative_tests_reset();
404 umock_c_negative_tests_fail_call(i);
405
406 (void)sprintf(temp_str, "On failed call %lu", (unsigned long)i);
407
408 ///act
409 result = target_create(SIZEOF_FOO_MEMORY);
410
411 ///assert
412 ASSERT_ARE_NOT_EQUAL(int, TARGET_RESULT_OK, result, temp_str);
413 }
414 }
415
416 ///cleanup
417 umock_c_negative_tests_deinit();
418 }
419
420 /* Tests_SRS_TEMPLATE_21_004: [ The target_foo shall do stuff calling callee_bar_1 and callee_bar_2. ]*/
421 /* Tests_SRS_TEMPLATE_21_005: [ If target_foo is called but the connection is not created, the target_foo shall return TARGET_RESULT_FAIL. ]*/
422 /* Tests_SRS_TEMPLATE_21_010: [ If target_foo cannot execute foo, the target_foo shall return TARGET_RESULT_FAIL. ]*/
423 TEST_FUNCTION(target_foo_unhappy_paths)
424 {
425 ///arrange
426 TARGET_RESULT result;
427 size_t i;
428 int negativeTestsInitResult = umock_c_negative_tests_init();
429 ASSERT_ARE_EQUAL(int, 0, negativeTestsInitResult);
430
431 result = target_create(SIZEOF_FOO_MEMORY);
432 ASSERT_ARE_EQUAL(int, TARGET_RESULT_OK, result);
433
434 /**
435 * Clear the function call list, to remove the ones created by the target_create(), so,
436 * now we can concentrate on the functions in target_foo().
437 */
438 umock_c_reset_all_calls();
439
440 STRICT_EXPECTED_CALL(callee_bar_2('a'));
441
442 umock_c_negative_tests_snapshot();
443
444 /**
445 * Here we are demonstrating how to automatically test an unhappy path from a function that
446 * we do not implement any mock to replace it.
447 */
448 for (i = 0; i < umock_c_negative_tests_call_count(); i++)
449 {
450 char temp_str[128];
451
452 umock_c_negative_tests_reset();
453 umock_c_negative_tests_fail_call(i);
454
455 (void)sprintf(temp_str, "On failed call %lu", (unsigned long)i);
456
457 ///act
458 result = target_foo();
459
460 ///assert
461 ASSERT_ARE_EQUAL(int, TARGET_RESULT_FAIL, result, temp_str);
462 }
463
464 ///cleanup
465 target_destroy();
466 umock_c_negative_tests_deinit();
467 }
468
469 END_TEST_SUITE(template_ut)