| 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 <cstddef> |
| 6 |
#else |
| 7 |
#include <stddef.h> |
| 8 |
#endif |
| 9 |
|
| 10 |
#include "azure_macro_utils/macro_utils.h" |
| 11 |
#include "testrunnerswitcher.h" |
| 12 |
#include "umock_c/umock_c.h" |
| 13 |
|
| 14 |
void* real_malloc(size_t size) |
| 15 |
{ |
| 16 |
return malloc(size); |
| 17 |
} |
| 18 |
|
| 19 |
void* real_calloc(size_t nmemb, size_t size) |
| 20 |
{ |
| 21 |
return calloc(nmemb, size); |
| 22 |
} |
| 23 |
|
| 24 |
void* real_realloc(void* ptr, size_t size) |
| 25 |
{ |
| 26 |
return realloc(ptr, size); |
| 27 |
} |
| 28 |
|
| 29 |
void real_free(void* ptr) |
| 30 |
{ |
| 31 |
free(ptr); |
| 32 |
} |
| 33 |
|
| 34 |
#define ENABLE_MOCKS |
| 35 |
#include "azure_c_shared_utility/gballoc.h" |
| 36 |
#include "azure_c_shared_utility/strings.h" |
| 37 |
#undef ENABLE_MOCKS |
| 38 |
#include "azure_c_shared_utility/hmacsha256.h" |
| 39 |
|
| 40 |
static TEST_MUTEX_HANDLE g_testByTest; |
| 41 |
|
| 42 |
TEST_DEFINE_ENUM_TYPE(HMACSHA256_RESULT, HMACSHA256_RESULT_VALUES); |
| 43 |
|
| 44 |
static BUFFER_HANDLE hash; |
| 45 |
|
| 46 |
MU_DEFINE_ENUM_STRINGS(UMOCK_C_ERROR_CODE, UMOCK_C_ERROR_CODE_VALUES) |
| 47 |
|
| 48 |
static void on_umock_c_error(UMOCK_C_ERROR_CODE error_code) |
| 49 |
{ |
| 50 |
ASSERT_FAIL("umock_c reported error :%" PRI_MU_ENUM "", MU_ENUM_VALUE(UMOCK_C_ERROR_CODE, error_code)); |
| 51 |
} |
| 52 |
|
| 53 |
BEGIN_TEST_SUITE(HMACSHA256_UnitTests) |
| 54 |
|
| 55 |
TEST_SUITE_INITIALIZE(TestClassInitialize) |
| 56 |
{ |
| 57 |
g_testByTest = TEST_MUTEX_CREATE(); |
| 58 |
ASSERT_IS_NOT_NULL(g_testByTest); |
| 59 |
|
| 60 |
umock_c_init(on_umock_c_error); |
| 61 |
|
| 62 |
REGISTER_GLOBAL_MOCK_HOOK(gballoc_malloc, real_malloc); |
| 63 |
REGISTER_GLOBAL_MOCK_HOOK(gballoc_calloc, real_calloc); |
| 64 |
REGISTER_GLOBAL_MOCK_HOOK(gballoc_free, real_free); |
| 65 |
REGISTER_GLOBAL_MOCK_HOOK(gballoc_realloc, real_realloc); |
| 66 |
} |
| 67 |
|
| 68 |
TEST_SUITE_CLEANUP(TestClassCleanup) |
| 69 |
{ |
| 70 |
umock_c_deinit(); |
| 71 |
|
| 72 |
TEST_MUTEX_DESTROY(g_testByTest); |
| 73 |
} |
| 74 |
|
| 75 |
TEST_FUNCTION_INITIALIZE(TestMethodInitialize) |
| 76 |
{ |
| 77 |
if (TEST_MUTEX_ACQUIRE(g_testByTest)) |
| 78 |
{ |
| 79 |
ASSERT_FAIL("our mutex is ABANDONED. Failure in test framework"); |
| 80 |
} |
| 81 |
umock_c_reset_all_calls(); |
| 82 |
hash = BUFFER_new(); |
| 83 |
} |
| 84 |
|
| 85 |
TEST_FUNCTION_CLEANUP(TestMethodCleanup) |
| 86 |
{ |
| 87 |
BUFFER_delete(hash); |
| 88 |
TEST_MUTEX_RELEASE(g_testByTest); |
| 89 |
} |
| 90 |
|
| 91 |
/* HMACSHA256_ComputeHash */ |
| 92 |
|
| 93 |
TEST_FUNCTION(HMACSHA256_ComputeHash_With_NULL_Key_Fails) |
| 94 |
{ |
| 95 |
// arrange |
| 96 |
static const unsigned char key[] = "key"; |
| 97 |
static const unsigned char buffer[] = "testPayload"; |
| 98 |
|
| 99 |
// act |
| 100 |
HMACSHA256_RESULT result = HMACSHA256_ComputeHash(NULL, sizeof(key) - 1, buffer, sizeof(buffer) - 1, hash); |
| 101 |
|
| 102 |
// assert |
| 103 |
ASSERT_ARE_EQUAL(HMACSHA256_RESULT, HMACSHA256_INVALID_ARG, result); |
| 104 |
} |
| 105 |
|
| 106 |
TEST_FUNCTION(HMACSHA256_ComputeHash_With_Zero_Key_Buffer_Size_Fails) |
| 107 |
{ |
| 108 |
// arrange |
| 109 |
static const unsigned char key[] = "key"; |
| 110 |
static const unsigned char buffer[] = "testPayload"; |
| 111 |
|
| 112 |
// act |
| 113 |
HMACSHA256_RESULT result = HMACSHA256_ComputeHash(key, 0, buffer, sizeof(buffer) - 1, hash); |
| 114 |
|
| 115 |
// assert |
| 116 |
ASSERT_ARE_EQUAL(HMACSHA256_RESULT, HMACSHA256_INVALID_ARG, result); |
| 117 |
} |
| 118 |
|
| 119 |
TEST_FUNCTION(HMACSHA256_ComputeHash_With_NULL_Payload_Fails) |
| 120 |
{ |
| 121 |
// arrange |
| 122 |
static const unsigned char key[] = "key"; |
| 123 |
static const unsigned char buffer[] = "testPayload"; |
| 124 |
|
| 125 |
// act |
| 126 |
HMACSHA256_RESULT result = HMACSHA256_ComputeHash(key, sizeof(key) - 1, NULL, sizeof(buffer) - 1, hash); |
| 127 |
|
| 128 |
// assert |
| 129 |
ASSERT_ARE_EQUAL(HMACSHA256_RESULT, HMACSHA256_INVALID_ARG, result); |
| 130 |
} |
| 131 |
|
| 132 |
TEST_FUNCTION(HMACSHA256_ComputeHash_With_Zero_Payload_Buffer_Size_Fails) |
| 133 |
{ |
| 134 |
// arrange |
| 135 |
static const unsigned char key[] = "key"; |
| 136 |
static const unsigned char buffer[] = "testPayload"; |
| 137 |
|
| 138 |
// act |
| 139 |
HMACSHA256_RESULT result = HMACSHA256_ComputeHash(key, sizeof(key) - 1, buffer, 0, hash); |
| 140 |
|
| 141 |
// assert |
| 142 |
ASSERT_ARE_EQUAL(HMACSHA256_RESULT, HMACSHA256_INVALID_ARG, result); |
| 143 |
} |
| 144 |
|
| 145 |
TEST_FUNCTION(HMACSHA256_ComputeHash_With_NULL_Hash_Fails) |
| 146 |
{ |
| 147 |
// arrange |
| 148 |
static const unsigned char key[] = "key"; |
| 149 |
static const unsigned char buffer[] = "testPayload"; |
| 150 |
|
| 151 |
// act |
| 152 |
HMACSHA256_RESULT result = HMACSHA256_ComputeHash(key, sizeof(key) - 1, buffer, sizeof(buffer) - 1, NULL); |
| 153 |
|
| 154 |
// assert |
| 155 |
ASSERT_ARE_EQUAL(HMACSHA256_RESULT, HMACSHA256_INVALID_ARG, result); |
| 156 |
} |
| 157 |
|
| 158 |
TEST_FUNCTION(HMACSHA256_ComputeHash_Succeeds) |
| 159 |
{ |
| 160 |
// arrange |
| 161 |
static const unsigned char key[] = "key"; |
| 162 |
static const unsigned char buffer[] = "testPayload"; |
| 163 |
unsigned char expectedHash[32] = { 108, 7, 130, 47, 104, 233, 39, 188, 126, 122, 134, 187, 63, 19, 52, 120, 172, 7, 43, 25, 133, 60, 92, 217, 59, 59, 69, 116, 85, 104, 55, 224 }; |
| 164 |
|
| 165 |
// act |
| 166 |
HMACSHA256_RESULT result = HMACSHA256_ComputeHash(key, sizeof(key) - 1, buffer, sizeof(buffer) - 1, hash); |
| 167 |
|
| 168 |
// assert |
| 169 |
ASSERT_ARE_EQUAL(HMACSHA256_RESULT, HMACSHA256_OK, result); |
| 170 |
ASSERT_ARE_EQUAL(int, 0, memcmp(BUFFER_u_char(hash), expectedHash, 8)); |
| 171 |
} |
| 172 |
|
| 173 |
END_TEST_SUITE(HMACSHA256_UnitTests) |