forked from Azure/iotedge
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for hsm_tpm_select (Azure#31)
* Added selection tests plus a few more. * Remove hsm_set_env from utils. * removed unused vars. * We're not getting openssl 1.0, we're getting 1.1 * Better way to get libssl1.0.0
- Loading branch information
Showing
6 changed files
with
238 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
edgelet/hsm-sys/azure-iot-hsm-c/tests/hsm_tpm_select_ut/CMakeLists.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#Copyright (c) Microsoft. All rights reserved. | ||
#Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
#this is CMakeLists.txt for edge_hsm_tpm_ut | ||
cmake_minimum_required(VERSION 2.8.11) | ||
|
||
compileAsC11() | ||
|
||
set(theseTestsName hspm_tpm_select_ut) | ||
|
||
include_directories(../../src) | ||
|
||
add_definitions(-DGB_DEBUG_ALLOC) | ||
|
||
set(${theseTestsName}_test_files | ||
../../src/hsm_log.c | ||
../../src/hsm_utils.c | ||
../../src/constants.c | ||
${theseTestsName}.c | ||
) | ||
|
||
set(${theseTestsName}_h_files | ||
) | ||
|
||
build_c_test_artifacts(${theseTestsName} ON "tests/azure_c_shared_utility_tests") | ||
|
||
if(WIN32) | ||
target_link_libraries(${theseTestsName}_exe iothsm aziotsharedutil $ENV{OPENSSL_ROOT_DIR}/lib/ssleay32.lib $ENV{OPENSSL_ROOT_DIR}/lib/libeay32.lib) | ||
else() | ||
target_link_libraries(${theseTestsName}_exe iothsm aziotsharedutil ${OPENSSL_LIBRARIES}) | ||
endif(WIN32) | ||
|
||
copy_iothsm_dll(${theseTestsName}_exe ${CMAKE_CURRENT_BINARY_DIR}/$(Configuration)) |
174 changes: 174 additions & 0 deletions
174
edgelet/hsm-sys/azure-iot-hsm-c/tests/hsm_tpm_select_ut/hspm_client_tpm_ut.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <stddef.h> | ||
|
||
#include "testrunnerswitcher.h" | ||
#include "azure_c_shared_utility/gballoc.h" | ||
#include "azure_c_shared_utility/sastoken.h" | ||
#include "azure_c_shared_utility/urlencode.h" | ||
#include "azure_c_shared_utility/hmacsha256.h" | ||
#include "azure_c_shared_utility/base64.h" | ||
#include "azure_c_shared_utility/agenttime.h" | ||
#include "azure_c_shared_utility/strings.h" | ||
#include "azure_c_shared_utility/buffer_.h" | ||
#include "azure_c_shared_utility/xlogging.h" | ||
#include "azure_c_shared_utility/crt_abstractions.h" | ||
|
||
#include "hsm_client_data.h" | ||
|
||
//############################################################################# | ||
// Test defines and data | ||
//############################################################################# | ||
#define TEST_DATA_TO_BE_SIGNED "The quick brown fox jumped over the lazy dog" | ||
#define TEST_KEY_BASE64 "D7PuplFy7vIr0349blOugqCxyfMscyVZDoV9Ii0EFnA=" | ||
#define TEST_HOSTNAME "somehost.azure-devices.net" | ||
#define TEST_DEVICE_ID "some-device-id" | ||
#define TEST_MODULE_ID "some-module-id" | ||
#define TEST_GEN_ID "1" | ||
#define PRIMARY_URI "primary" | ||
#define SECONDARY_URI "secondary" | ||
|
||
static TEST_MUTEX_HANDLE g_testByTest; | ||
static TEST_MUTEX_HANDLE g_dllByDll; | ||
|
||
extern const char* const ENV_TPM_SELECT; | ||
|
||
//############################################################################# | ||
// Test helpers | ||
//############################################################################# | ||
|
||
static void test_helper_setup_env(const char *key, const char *val) | ||
{ | ||
#if defined __WINDOWS__ || defined _WIN32 || defined _WIN64 || defined _Windows | ||
errno_t status = _putenv_s(key, val); | ||
#else | ||
int status = setenv(key, val, 1); | ||
#endif | ||
printf("Env variable %s set to %s\n", key, val); | ||
ASSERT_ARE_EQUAL_WITH_MSG(int, 0, status, "Line:" TOSTRING(__LINE__)); | ||
} | ||
|
||
static void test_helper_unset_env(const char *key) | ||
{ | ||
#if defined __WINDOWS__ || defined _WIN32 || defined _WIN64 || defined _Windows | ||
errno_t status = _putenv_s(key, ""); | ||
#else | ||
int status = unsetenv(key); | ||
#endif | ||
ASSERT_ARE_EQUAL_WITH_MSG(int, 0, status, "Line:" TOSTRING(__LINE__)); | ||
} | ||
|
||
static void test_helper_setup_homedir(void) | ||
{ | ||
#if defined(TESTONLY_IOTEDGE_HOMEDIR) | ||
#if defined __WINDOWS__ || defined _WIN32 || defined _WIN64 || defined _Windows | ||
errno_t status = _putenv_s("IOTEDGE_HOMEDIR", TESTONLY_IOTEDGE_HOMEDIR); | ||
#else | ||
int status = setenv("IOTEDGE_HOMEDIR", TESTONLY_IOTEDGE_HOMEDIR, 1); | ||
#endif | ||
printf("IoT Edge home dir set to %s\n", TESTONLY_IOTEDGE_HOMEDIR); | ||
ASSERT_ARE_EQUAL_WITH_MSG(int, 0, status, "Line:" TOSTRING(__LINE__)); | ||
#else | ||
#error "Could not find symbol TESTONLY_IOTEDGE_HOMEDIR" | ||
#endif | ||
} | ||
|
||
const HSM_CLIENT_TPM_INTERFACE * init_get_if_deinit(void) | ||
{ | ||
int status; | ||
ASSERT_ARE_EQUAL_WITH_MSG(int, 0, status, "Line:" TOSTRING(__LINE__)); | ||
const HSM_CLIENT_TPM_INTERFACE* interface = hsm_client_tpm_interface(); | ||
hsm_client_tpm_deinit(); | ||
return interface; | ||
} | ||
|
||
|
||
//############################################################################# | ||
// Test functions | ||
//############################################################################# | ||
|
||
BEGIN_TEST_SUITE(edge_hsm_sas_auth_int_tests) | ||
TEST_SUITE_INITIALIZE(TestClassInitialize) | ||
{ | ||
TEST_INITIALIZE_MEMORY_DEBUG(g_dllByDll); | ||
g_testByTest = TEST_MUTEX_CREATE(); | ||
ASSERT_IS_NOT_NULL(g_testByTest); | ||
test_helper_setup_homedir(); | ||
|
||
REGISTER_UMOCK_ALIAS_TYPE(HSM_CLIENT_STORE_INTERFACE, void*); | ||
|
||
} | ||
|
||
TEST_SUITE_CLEANUP(TestClassCleanup) | ||
{ | ||
TEST_MUTEX_DESTROY(g_testByTest); | ||
TEST_DEINITIALIZE_MEMORY_DEBUG(g_dllByDll); | ||
} | ||
|
||
TEST_FUNCTION_INITIALIZE(TestMethodInitialize) | ||
{ | ||
if (TEST_MUTEX_ACQUIRE(g_testByTest)) | ||
{ | ||
ASSERT_FAIL("Mutex is ABANDONED. Failure in test framework."); | ||
} | ||
} | ||
|
||
TEST_FUNCTION_CLEANUP(TestMethodCleanup) | ||
{ | ||
TEST_MUTEX_RELEASE(g_testByTest); | ||
} | ||
|
||
TEST_FUNCTION(hsm_tpm_select_no_tpm_false) | ||
{ | ||
// arrange | ||
static const char * user_says_no[] = { "", | ||
"off", "OFF", "Off", | ||
"no", "NO", "No", | ||
"false", "FALSE", "False" }; | ||
int array_size = sizeof(user_says_no)/sizeof(user_says_no[0]); | ||
int status = test_helper_unset_env(ENV_TPM_SELECT); | ||
ASSERT_ARE_EQUAL_WITH_MSG(int, 0, status, "Line:" TOSTRING(__LINE__)); | ||
const HSM_CLIENT_TPM_INTERFACE * no_tpm = init_get_if_deinit(); | ||
// act | ||
// assert | ||
for(int no = 0; no < array_size; no++) | ||
{ | ||
int status = test_helper_setup_env(ENV_TPM_SELECT, user_says_no[no]); | ||
ASSERT_ARE_EQUAL_WITH_MSG(int, 0, status, "Line:" TOSTRING(__LINE__)); | ||
ASSERT_ARE_EQUAL_WITH_MSG(const HSM_CLIENT_TPM_INTERFACE *, | ||
no_tpm, init_get_if_deinit(), | ||
"Line:" TOSTRING(__LINE__)); | ||
} | ||
// cleanup | ||
} | ||
|
||
TEST_FUNCTION(hsm_tpm_select_tpm_true) | ||
{ | ||
// arrange | ||
static const char * user_says_yes[] = { "yes", "YES", "Yes", | ||
"on", "ON", "On", | ||
"true", "TRUE", "True", | ||
"Like CMAKE, it's anything that's not assocated with false", | ||
"plugh" }; | ||
int array_size = sizeof(user_says_yes)/sizeof(user_says_yes[0]); | ||
int status = test_helper_unset_env(ENV_TPM_SELECT); | ||
ASSERT_ARE_EQUAL_WITH_MSG(int, 0, status, "Line:" TOSTRING(__LINE__)); | ||
const HSM_CLIENT_TPM_INTERFACE * no_tpm = init_get_if_deinit(); | ||
// act | ||
// assert | ||
for(int yes = 0; yes < array_size; yes++) | ||
{ | ||
int status = test_helper_setup_env(ENV_TPM_SELECT, user_says_yes[yes]); | ||
ASSERT_ARE_EQUAL_WITH_MSG(int, 0, status, "Line:" TOSTRING(__LINE__)); | ||
ASSERT_ARE_NOT_EQUAL_WITH_MSG(const HSM_CLIENT_TPM_INTERFACE *, | ||
no_tpm, init_get_if_deinit(), | ||
"Line:" TOSTRING(__LINE__)); | ||
} | ||
// cleanup | ||
} | ||
|
||
|
||
END_TEST_SUITE(edge_hsm_sas_auth_int_tests) |
11 changes: 11 additions & 0 deletions
11
edgelet/hsm-sys/azure-iot-hsm-c/tests/hsm_tpm_select_ut/main.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
#include "testrunnerswitcher.h" | ||
|
||
int main(void) | ||
{ | ||
size_t failedTestCount = 0; | ||
RUN_TEST_SUITE(hspm_tpm_select_ut, failedTestCount); | ||
return failedTestCount; | ||
} |