Debian Codegraph @

src/vendor/azure-uamqp-c/deps/azure-c-shared-utility/tests/uuid_ut/uuid_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 #include "azure_macro_utils/macro_utils.h"
11 #include "testrunnerswitcher.h"
12 #include "umock_c/umock_c.h"
13 #include "umock_c/umocktypes_charptr.h"
14 #include "umock_c/umocktypes_bool.h"
15 #include "umock_c/umock_c_negative_tests.h"
16
17 #define ENABLE_MOCKS
18 #include "azure_c_shared_utility/gballoc.h"
19 #include "azure_c_shared_utility/uniqueid.h"
20 #undef ENABLE_MOCKS
21
22 #include "azure_c_shared_utility/uuid.h"
23
24 static TEST_MUTEX_HANDLE g_testByTest;
25
26 MU_DEFINE_ENUM_STRINGS(UMOCK_C_ERROR_CODE, UMOCK_C_ERROR_CODE_VALUES)
27
28 static void on_umock_c_error(UMOCK_C_ERROR_CODE error_code)
29 {
30 ASSERT_FAIL("umock_c reported error :%" PRI_MU_ENUM "", MU_ENUM_VALUE(UMOCK_C_ERROR_CODE, error_code));
31 }
32
33
34 #define UUID_OCTET_COUNT 16
35 #define UUID_STRING_LENGTH 36
36 #define UUID_STRING_SIZE (UUID_STRING_LENGTH + 1)
37
38 static const UUID_T TEST_UUID = { 222, 193, 74, 152, 197, 252, 67, 14, 180, 227, 51, 193, 196, 52, 220, 175 };
39 static char* TEST_UUID_STRING = "dec14a98-c5fc-430e-b4e3-33c1c434dcaf";
40 static const char* INVALID_UUIDS[] =
41 {
42 "d",
43 "-",
44 "dec14a98",
45 "dec14a98-c5fc",
46 "dec14a98-c5fc-430e-b4e3-",
47 "dec14a98-c5fc-430e-b4e3-33c1c434dca",
48 "dec14a98c5fc-430e-b4e3-33c1c434dcaf",
49 "dec14a98c5fc-430e-b4e3-33c1c434dcaf1",
50 "dec14a98-c5fc430e-b4e3-33c1c434dcaf",
51 "dec14a98-c5fc-430eb4e3-33c1c434dcaf",
52 "dec14a98-c5fc-430e-b4e333c1c434dcaf",
53 "dec14a98c5fc430eb4e333c1c434dcaf",
54 "dec14a98-c5fc-430e-b4e3-33c1c434dca-",
55 "dec14a98-c5fc-430e-b-e3-33c1c434dcaf",
56 "dec14a98-c5fc-43-e-b4e3-33c1c434dcaf",
57 "dec14a98-c-fc-430e-b4e3-33c1c434dcaf",
58 "dec14-98-c5fc-430e-b4e3-33c1c434dcaf",
59 "-ec14a98-c5fc-430e-b4e3-33c1c434dcaf",
60 };
61
62 static UNIQUEID_RESULT mock_UniqueId_Generate_result;
63 static UNIQUEID_RESULT mock_UniqueId_Generate(char* uid, size_t bufferSize)
64 {
65 (void)memcpy(uid, TEST_UUID_STRING, bufferSize);
66 return mock_UniqueId_Generate_result;
67 }
68
69 static void initialize_variables()
70 {
71 mock_UniqueId_Generate_result = UNIQUEID_OK;
72 }
73
74 static void register_global_mock_returns()
75 {
76 REGISTER_GLOBAL_MOCK_FAIL_RETURN(malloc, NULL);
77 REGISTER_GLOBAL_MOCK_FAIL_RETURN(UniqueId_Generate, UNIQUEID_ERROR);
78 }
79
80 static void register_global_function_hooks()
81 {
82 REGISTER_GLOBAL_MOCK_HOOK(UniqueId_Generate, mock_UniqueId_Generate);
83 }
84
85 static void register_mock_aliases()
86 {
87 REGISTER_UMOCK_ALIAS_TYPE(UNIQUEID_RESULT, int);
88 }
89
90
91 BEGIN_TEST_SUITE(uuid_unittests)
92
93 TEST_SUITE_INITIALIZE(suite_init)
94 {
95 int result;
96
97 result = umock_c_init(on_umock_c_error);
98 ASSERT_ARE_EQUAL(int, 0, result);
99
100 g_testByTest = TEST_MUTEX_CREATE();
101 ASSERT_IS_NOT_NULL(g_testByTest);
102
103 register_mock_aliases();
104 register_global_mock_returns();
105 register_global_function_hooks();
106 initialize_variables();
107 }
108
109 TEST_SUITE_CLEANUP(suite_cleanup)
110 {
111 TEST_MUTEX_DESTROY(g_testByTest);
112 }
113
114 TEST_FUNCTION_INITIALIZE(TestMethodInitialize)
115 {
116 if (TEST_MUTEX_ACQUIRE(g_testByTest) != 0)
117 {
118 ASSERT_FAIL("our mutex is ABANDONED. Failure in test framework");
119 }
120 }
121
122 TEST_FUNCTION_CLEANUP(TestMethodCleanup)
123 {
124 TEST_MUTEX_RELEASE(g_testByTest);
125 }
126
127 // Tests_SRS_UUID_09_001: [ If uuid is NULL, UUID_generate shall return a non-zero value ]
128 TEST_FUNCTION(UUID_generate_NULL_uuid)
129 {
130 //Arrange
131 int result;
132
133 umock_c_reset_all_calls();
134
135 //Act
136 result = UUID_generate(NULL);
137
138 //Assert
139 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
140 ASSERT_ARE_NOT_EQUAL(int, 0, result);
141 }
142
143 // Tests_SRS_UUID_09_002: [ UUID_generate shall obtain an UUID string from UniqueId_Generate ]
144 // Tests_SRS_UUID_09_004: [ The UUID string shall be parsed into an UUID_T type (16 unsigned char array) and filled in uuid ]
145 // Tests_SRS_UUID_09_006: [ If no failures occur, UUID_generate shall return zero ]
146 TEST_FUNCTION(UUID_generate_succeed)
147 {
148 //Arrange
149 UUID_T uuid;
150 int result;
151 char uuid_string[UUID_STRING_SIZE];
152
153 umock_c_reset_all_calls();
154 STRICT_EXPECTED_CALL(malloc(UUID_STRING_SIZE))
155 .SetReturn(uuid_string);
156 STRICT_EXPECTED_CALL(UniqueId_Generate(uuid_string, UUID_STRING_SIZE));
157 STRICT_EXPECTED_CALL(free(uuid_string));
158
159 //Act
160 result = UUID_generate(&uuid);
161
162 //Assert
163 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
164 ASSERT_ARE_EQUAL(int, 0, result);
165
166 {
167 int i;
168 for (i = 0; i < UUID_OCTET_COUNT; i++)
169 {
170 ASSERT_ARE_EQUAL(int, TEST_UUID[i], uuid[i]);
171 }
172 }
173 }
174
175 // Tests_SRS_UUID_09_003: [ If the UUID string fails to be obtained, UUID_generate shall fail and return a non-zero value ]
176 // Tests_SRS_UUID_09_005: [ If uuid fails to be set, UUID_generate shall fail and return a non-zero value ]
177 TEST_FUNCTION(UUID_generate_failure_checks)
178 {
179 //Arrange
180 UUID_T uuid;
181 int result;
182 size_t i;
183 char uuid_string[UUID_STRING_SIZE];
184
185 ASSERT_ARE_EQUAL(int, 0, umock_c_negative_tests_init());
186
187 umock_c_reset_all_calls();
188 STRICT_EXPECTED_CALL(malloc(UUID_STRING_SIZE))
189 .SetReturn(uuid_string);
190 STRICT_EXPECTED_CALL(UniqueId_Generate(uuid_string, UUID_STRING_SIZE));
191 STRICT_EXPECTED_CALL(free(uuid_string));
192 umock_c_negative_tests_snapshot();
193
194 for (i = 0; i < umock_c_negative_tests_call_count(); i++)
195 {
196 char temp_str[64];
197
198 if (i == 2) continue;
199
200 umock_c_negative_tests_reset();
201 umock_c_negative_tests_fail_call(i);
202
203 (void)sprintf(temp_str, "On failed call %lu", (unsigned long)i);
204
205 // act
206 result = UUID_generate(&uuid);
207
208 // assert
209 ASSERT_ARE_NOT_EQUAL(int, 0, result, temp_str);
210 }
211
212 umock_c_negative_tests_reset();
213 umock_c_negative_tests_deinit();
214 }
215
216 // Tests_SRS_UUID_09_011: [ If uuid is NULL, UUID_to_string shall return a non-zero value ]
217 TEST_FUNCTION(UUID_to_string_NULL_uuid)
218 {
219 //Arrange
220
221 //Act
222 char* result = UUID_to_string(NULL);
223
224 //Assert
225 ASSERT_IS_NULL(result);
226 }
227
228 // Tests_SRS_UUID_09_012: [ UUID_to_string shall allocate a valid UUID string (uuid_string) as per RFC 4122 ]
229 // Tests_SRS_UUID_09_014: [ Each character in uuid shall be written in the respective positions of uuid_string as a 2-digit HEX value ]
230 // Tests_SRS_UUID_09_016: [ If no failures occur, UUID_to_string shall return uuid_string ]
231 TEST_FUNCTION(UUID_to_string_succeed)
232 {
233 //Arrange
234 char* result;
235 char buffer[UUID_STRING_SIZE];
236
237 umock_c_reset_all_calls();
238 STRICT_EXPECTED_CALL(malloc(UUID_STRING_SIZE * sizeof(char)))
239 .SetReturn(buffer);
240
241 //Act
242 result = UUID_to_string(&TEST_UUID);
243
244 //Assert
245 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
246 ASSERT_ARE_EQUAL(char_ptr, TEST_UUID_STRING, result);
247 }
248
249 // Tests_SRS_UUID_09_013: [ If uuid_string fails to be allocated, UUID_to_string shall return NULL ]
250 // Tests_SRS_UUID_09_015: [ If uuid_string fails to be set, UUID_to_string shall return NULL ]
251 TEST_FUNCTION(UUID_to_string_failure_checks)
252 {
253 //Arrange
254 char* result;
255 char buffer[UUID_STRING_SIZE];
256 size_t i;
257
258 ASSERT_ARE_EQUAL(int, 0, umock_c_negative_tests_init());
259
260 umock_c_reset_all_calls();
261 STRICT_EXPECTED_CALL(malloc(UUID_STRING_SIZE * sizeof(char)))
262 .SetReturn(buffer);
263
264 umock_c_negative_tests_snapshot();
265
266 for (i = 0; i < umock_c_negative_tests_call_count(); i++)
267 {
268 char temp_str[64];
269
270 umock_c_negative_tests_reset();
271 umock_c_negative_tests_fail_call(i);
272
273 (void)sprintf(temp_str, "On failed call %lu", (unsigned long)i);
274
275 // act
276 result = UUID_to_string(&TEST_UUID);
277
278 // assert
279 ASSERT_IS_NULL(result, temp_str);
280 }
281
282 umock_c_negative_tests_reset();
283 umock_c_negative_tests_deinit();
284 }
285
286 // Tests_SRS_UUID_09_007: [ If uuid_string or uuid are NULL, UUID_from_string shall return a non-zero value ]
287 TEST_FUNCTION(UUID_from_string_NULL_uuid_string)
288 {
289 //Arrange
290 int result;
291 UUID_T uuid;
292
293 umock_c_reset_all_calls();
294
295 //Act
296 result = UUID_from_string(NULL, &uuid);
297
298 //Assert
299 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
300 ASSERT_ARE_NOT_EQUAL(int, 0, result);
301 }
302
303 // Tests_SRS_UUID_09_007: [ If uuid_string or uuid are NULL, UUID_from_string shall return a non-zero value ]
304 TEST_FUNCTION(UUID_from_string_NULL_uuid)
305 {
306 //Arrange
307 int result;
308
309 umock_c_reset_all_calls();
310
311 //Act
312 result = UUID_from_string(TEST_UUID_STRING, NULL);
313
314 //Assert
315 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
316 ASSERT_ARE_NOT_EQUAL(int, 0, result);
317 }
318
319 // Tests_SRS_UUID_09_008: [ Each pair of digits in uuid_string, excluding dashes, shall be read as a single HEX value and saved on the respective position in uuid ]
320 // Tests_SRS_UUID_09_010: [ If no failures occur, UUID_from_string shall return zero ]
321 TEST_FUNCTION(UUID_from_string_succeed)
322 {
323 //Arrange
324 int result;
325 UUID_T uuid;
326
327 umock_c_reset_all_calls();
328
329 //Act
330 result = UUID_from_string(TEST_UUID_STRING, &uuid);
331
332 //Assert
333 ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls());
334 ASSERT_ARE_EQUAL(int, 0, result);
335
336 {
337 int i;
338 for (i = 0; i < UUID_OCTET_COUNT; i++)
339 {
340 ASSERT_ARE_EQUAL(int, TEST_UUID[i], uuid[i]);
341 }
342 }
343 }
344
345 TEST_FUNCTION(UUID_from_string_invalid_fails)
346 {
347 //Arrange
348 int result;
349 UUID_T uuid;
350
351 umock_c_reset_all_calls();
352
353 for (size_t i = 0; i < sizeof(INVALID_UUIDS)/sizeof(INVALID_UUIDS[0]); i++)
354 {
355 //Act
356 result = UUID_from_string(INVALID_UUIDS[i], &uuid);
357
358 //Assert
359 ASSERT_ARE_NOT_EQUAL(int, 0, result);
360 }
361 }
362
363 // Tests_SRS_UUID_09_009: [ If uuid fails to be generated, UUID_from_string shall return a non-zero value ]
364 // To be implemented once sscanf mock is implemented.
365
366 END_TEST_SUITE(uuid_unittests)