Skip to content

Commit

Permalink
Add tests for hsm_tpm_select (Azure#31)
Browse files Browse the repository at this point in the history
* 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
darobs authored and myagley committed Jul 17, 2018
1 parent 46bc6e8 commit 4ee11c1
Show file tree
Hide file tree
Showing 6 changed files with 238 additions and 129 deletions.
5 changes: 3 additions & 2 deletions edgelet/build/linux/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,13 @@ add-apt-repository "deb http://archive.ubuntu.com/ubuntu/ trusty main universe"
apt-get update && \
apt-get install -y \
pkg-config \
libssl-dev \
uuid-dev curl \
libcurl4-openssl-dev \
debhelper \
dh-systemd \
valgrind
valgrind && \
apt-get remove --yes libssl-dev && \
apt-get install --yes --target-release xenial-updates libssl-dev

if [[ -n "$ARM_PACKAGE" ]]; then
# armhf cross tools for packaging
Expand Down
127 changes: 0 additions & 127 deletions edgelet/hsm-sys/azure-iot-hsm-c/src/hsm_client_tpm_device.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,133 +119,6 @@ static TPM2B_PUBLIC* GetSrkTemplate()
curr_pos += arrSize; \
}

/* static int dps_umarshal_array */
/* ( */
/* unsigned char* dst_ptr, */
/* uint32_t dest_size, */
/* unsigned char* act_buff, */
/* uint32_t act_size */
/* ) */
/* { */
/* int result; */
/* uint8_t* curr_pos = act_buff; */
/* */
/* DPS_UNMARSHAL(UINT32, &(dest_size)); */
/* if (act_size < dest_size) */
/* { */
/* LOG_ERROR("Unmarshaling failed: Need %d bytes, while only %d left", */
/* dest_size, act_size); */
/* result = __FAILURE__; */
/* } */
/* else */
/* { */
/* dst_ptr = act_buff - sizeof(UINT16); */
/* *(UINT16*)dst_ptr = (UINT16)dest_size; */
/* act_buff += dest_size; */
/* result = 0; */
/* } */
/* return result; */
/* } */
/* */
/* static int unmarshal_array */
/* ( */
/* uint8_t* dstptr, */
/* uint32_t size, */
/* uint8_t** curr_pos, */
/* uint32_t* curr_size */
/* ) */
/* { */
/* int result; */
/* */
/* TPM_RC tpm_res = UINT32_Unmarshal((uint32_t*)dstptr, curr_pos, (int32_t*)curr_size); */
/* if (tpm_res != TPM_RC_SUCCESS) */
/* { */
/* LOG_ERROR("Failure: unmarshalling array."); */
/* result = __FAILURE__; */
/* } */
/* else if (*curr_size < size) */
/* { */
/* LOG_ERROR("Failure: unmarshalling array need %d bytes, while only %d left.", */
/* size, *curr_size); */
/* result = __FAILURE__; */
/* } */
/* else */
/* { */
/* dstptr = *curr_pos - sizeof(UINT16); */
/* *(UINT16*)dstptr = (UINT16)size; */
/* curr_pos += size; */
/* result = 0; */
/* } */
/* return result; */
/* } */
/* */
/* static int marshal_array_values */
/* ( */
/* const unsigned char* key, */
/* size_t key_len, */
/* uint8_t** decrypt_blob, */
/* uint8_t** decrypt_secret, */
/* uint8_t** decrypt_wrap_key, */
/* TPM2B_PRIVATE* enc_key_blob */
/* ) */
/* { */
/* int result = 0; */
/* uint8_t* curr_pos = (uint8_t*)key; */
/* uint32_t act_size = (int32_t)key_len; */
/* uint32_t decrypt_size = 0; */
/* uint32_t decrypt_secret_size = 0; */
/* uint32_t decrypt_key_size = 0; */
/* TPM2B_PUBLIC id_key_Public = { TPM_ALG_NULL }; */
/* UINT16 gratuitousSizeField; // WORKAROUND for the current protocol */
/* */
/* DPS_UNMARSHAL_ARRAY(*decrypt_blob, decrypt_size); */
/* if (result != 0) */
/* { */
/* LOG_ERROR("Failure: decrypting blob"); */
/* } */
/* else */
/* { */
/* DPS_UNMARSHAL_ARRAY(*decrypt_secret, decrypt_secret_size); */
/* if (result != 0) */
/* { */
/* LOG_ERROR("Failure: decrypting secret"); */
/* } */
/* else */
/* { */
/* DPS_UNMARSHAL_ARRAY(*decrypt_wrap_key, decrypt_key_size); */
/* if (result != 0) */
/* { */
/* LOG_ERROR("Failure: decrypting wrap secret"); */
/* } */
/* else */
/* { */
/* DPS_UNMARSHAL_FLAGGED(TPM2B_PUBLIC, &id_key_Public); */
/* if (result != 0) */
/* { */
/* LOG_ERROR("Failure: id key public"); */
/* } */
/* else */
/* { */
/* DPS_UNMARSHAL(UINT16, &gratuitousSizeField); */
/* if (result != 0) */
/* { */
/* LOG_ERROR("Failure: gratuitousSizeField"); */
/* } */
/* else */
/* { */
/* DPS_UNMARSHAL(TPM2B_PRIVATE, enc_key_blob); */
/* if (result != 0) */
/* { */
/* LOG_ERROR("Failure: enc key blob"); */
/* } */
/* } */
/* } */
/* } */
/* } */
/* } */
/* return result; */
/* } */

static int create_tpm_session
(
HSM_CLIENT_INFO* sec_info,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,23 @@ BEGIN_TEST_SUITE(edge_hsm_util_int_tests)
// cleanup
}

TEST_FUNCTION(test_hsm_env_input)
{
// arrange
int status;
char *output = NULL;

// act
status = hsm_get_env(NULL,&output);
// assert
ASSERT_ARE_NOT_EQUAL_WITH_MSG(int, 0, status, "Line:" TOSTRING(__LINE__));
// act
status = hsm_get_env("TEST_ENV_1",NULL);
// assert
ASSERT_ARE_NOT_EQUAL_WITH_MSG(int, 0, status, "Line:" TOSTRING(__LINE__));
// cleanup
}

TEST_FUNCTION(test_hsm_env_get_smoke)
{
// arrange
Expand Down
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))
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 edgelet/hsm-sys/azure-iot-hsm-c/tests/hsm_tpm_select_ut/main.c
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;
}

0 comments on commit 4ee11c1

Please sign in to comment.