| 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 "azure_c_shared_utility/uniqueid.h" |
| 13 |
|
| 14 |
static TEST_MUTEX_HANDLE g_testByTest; |
| 15 |
static TEST_MUTEX_HANDLE g_dllByDll; |
| 16 |
|
| 17 |
TEST_DEFINE_ENUM_TYPE(UNIQUEID_RESULT, UNIQUEID_RESULT_VALUES); |
| 18 |
|
| 19 |
#define BUFFER_SIZE 37 |
| 20 |
|
| 21 |
static char* uidBuffer = NULL; |
| 22 |
static char* uidBuffer2 = NULL; |
| 23 |
|
| 24 |
BEGIN_TEST_SUITE(uniqueid_unittests) |
| 25 |
|
| 26 |
TEST_SUITE_INITIALIZE(suite_init) |
| 27 |
{ |
| 28 |
g_testByTest = TEST_MUTEX_CREATE(); |
| 29 |
ASSERT_IS_NOT_NULL(g_testByTest); |
| 30 |
} |
| 31 |
|
| 32 |
TEST_SUITE_CLEANUP(suite_cleanup) |
| 33 |
{ |
| 34 |
TEST_MUTEX_DESTROY(g_testByTest); |
| 35 |
} |
| 36 |
|
| 37 |
TEST_FUNCTION_INITIALIZE(TestMethodInitialize) |
| 38 |
{ |
| 39 |
if (TEST_MUTEX_ACQUIRE(g_testByTest) != 0) |
| 40 |
{ |
| 41 |
ASSERT_FAIL("our mutex is ABANDONED. Failure in test framework"); |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
TEST_FUNCTION_CLEANUP(TestMethodCleanup) |
| 46 |
{ |
| 47 |
if (uidBuffer != NULL) |
| 48 |
{ |
| 49 |
free(uidBuffer); |
| 50 |
uidBuffer = NULL; |
| 51 |
} |
| 52 |
|
| 53 |
if (uidBuffer2 != NULL) |
| 54 |
{ |
| 55 |
free(uidBuffer2); |
| 56 |
uidBuffer2 = NULL; |
| 57 |
} |
| 58 |
|
| 59 |
TEST_MUTEX_RELEASE(g_testByTest); |
| 60 |
} |
| 61 |
|
| 62 |
/* UniqueId_Generate */ |
| 63 |
TEST_FUNCTION(UniqueId_Generate_UID_NULL_Fail) |
| 64 |
{ |
| 65 |
//Arrange |
| 66 |
|
| 67 |
//Act |
| 68 |
UNIQUEID_RESULT result = UniqueId_Generate(NULL, BUFFER_SIZE); |
| 69 |
|
| 70 |
//Assert |
| 71 |
ASSERT_ARE_EQUAL(UNIQUEID_RESULT, UNIQUEID_INVALID_ARG, result); |
| 72 |
} |
| 73 |
|
| 74 |
TEST_FUNCTION(UniqueId_Generate_Len_too_small_Fail) |
| 75 |
{ |
| 76 |
//Arrange |
| 77 |
char uid[BUFFER_SIZE]; |
| 78 |
|
| 79 |
//Act |
| 80 |
UNIQUEID_RESULT result = UniqueId_Generate(uid, BUFFER_SIZE/2); |
| 81 |
|
| 82 |
//Assert |
| 83 |
ASSERT_ARE_EQUAL(UNIQUEID_RESULT, UNIQUEID_INVALID_ARG, result); |
| 84 |
} |
| 85 |
|
| 86 |
TEST_FUNCTION(UniqueId_Generate_Succeed) |
| 87 |
{ |
| 88 |
//Arrange |
| 89 |
char uid[BUFFER_SIZE]; |
| 90 |
|
| 91 |
//Act |
| 92 |
UNIQUEID_RESULT result = UniqueId_Generate(uid, BUFFER_SIZE); |
| 93 |
|
| 94 |
//Assert |
| 95 |
ASSERT_ARE_EQUAL(UNIQUEID_RESULT, UNIQUEID_OK, result); |
| 96 |
ASSERT_ARE_EQUAL(size_t, 36, strlen(uid) ); |
| 97 |
} |
| 98 |
|
| 99 |
END_TEST_SUITE(uniqueid_unittests) |