diff --git a/provisioning_client/src/prov_auth_client.c b/provisioning_client/src/prov_auth_client.c index 5b8644a6e0..a91ec29393 100644 --- a/provisioning_client/src/prov_auth_client.c +++ b/provisioning_client/src/prov_auth_client.c @@ -224,6 +224,7 @@ PROV_AUTH_HANDLE prov_auth_create() { memset(result, 0, sizeof(PROV_AUTH_INFO) ); SECURE_DEVICE_TYPE sec_type = prov_dev_security_get_type(); +#if defined(HSM_TYPE_SAS_TOKEN) || defined(HSM_AUTH_TYPE_CUSTOM) if (sec_type == SECURE_DEVICE_TYPE_TPM) { /* Codes_SRS_PROV_AUTH_CLIENT_07_003: [ prov_auth_create shall validate the specified secure enclave interface to ensure. ] */ @@ -244,7 +245,9 @@ PROV_AUTH_HANDLE prov_auth_create() result = NULL; } } - else if (sec_type == SECURE_DEVICE_TYPE_X509) +#endif +#if defined(HSM_TYPE_X509) || defined(HSM_AUTH_TYPE_CUSTOM) + if (sec_type == SECURE_DEVICE_TYPE_X509) { /* Codes_SRS_PROV_AUTH_CLIENT_07_003: [ prov_auth_create shall validate the specified secure enclave interface to ensure. ] */ result->sec_type = PROV_AUTH_TYPE_X509; @@ -262,9 +265,10 @@ PROV_AUTH_HANDLE prov_auth_create() result = NULL; } } - else - { +#endif #if defined(HSM_TYPE_SYMM_KEY) || defined(HSM_AUTH_TYPE_CUSTOM) + if (sec_type == SECURE_DEVICE_TYPE_SYMMETRIC_KEY) + { result->sec_type = PROV_AUTH_TYPE_KEY; const HSM_CLIENT_KEY_INTERFACE* key_interface = hsm_client_key_interface(); if ((key_interface == NULL) || @@ -279,13 +283,20 @@ PROV_AUTH_HANDLE prov_auth_create() free(result); result = NULL; } -#else - LogError("Invalid secure device type was specified"); - result = NULL; -#endif } +#endif - if (result != NULL) + if (result == NULL) + { + LogError("Error allocating result or else unsupported security type %d", sec_type); + } + else if (result->hsm_client_create == NULL) + { + LogError("hsm_client_create is not a valid address"); + free(result); + result = NULL; + } + else { /* Codes_SRS_PROV_AUTH_CLIENT_07_004: [ prov_auth_create shall call hsm_client_create on the secure enclave interface. ] */ if ((result->hsm_client_handle = result->hsm_client_create() ) == NULL) diff --git a/provisioning_client/tests/prov_auth_client_ut/prov_auth_client_ut.c b/provisioning_client/tests/prov_auth_client_ut/prov_auth_client_ut.c index ba8a3c0d81..1dab6fc288 100644 --- a/provisioning_client/tests/prov_auth_client_ut/prov_auth_client_ut.c +++ b/provisioning_client/tests/prov_auth_client_ut/prov_auth_client_ut.c @@ -114,6 +114,8 @@ static const char* TEST_TOKEN_SCOPE_VALUE = "token_scope"; static const char* TEST_KEY_NAME_VALUE = "key_value"; static const char* TEST_BASE32_VALUE = "aebagbaf"; +static const char* TEST_REGISTRATION_ID = "Registration Id"; + TEST_DEFINE_ENUM_TYPE(PROV_AUTH_RESULT, PROV_AUTH_RESULT_VALUES); IMPLEMENT_UMOCK_C_ENUM_TYPE(PROV_AUTH_RESULT, PROV_AUTH_RESULT_VALUES); @@ -1291,6 +1293,78 @@ BEGIN_TEST_SUITE(prov_auth_client_ut) prov_auth_destroy(sec_handle); } + TEST_FUNCTION(prov_auth_set_registration_id_handle_NULL_fail) + { + //arrange + + //act + int result = prov_auth_set_registration_id(NULL, TEST_REGISTRATION_ID); + + //assert + ASSERT_ARE_NOT_EQUAL(int, 0, result); + ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); + + //cleanup + } + + TEST_FUNCTION(prov_auth_set_registration_id_registration_id_NULL_fail) + { + PROV_AUTH_HANDLE sec_handle = prov_auth_create(); + umock_c_reset_all_calls(); + + //arrange + + //act + int result = prov_auth_set_registration_id(sec_handle, NULL); + + //assert + ASSERT_ARE_NOT_EQUAL(int, 0, result); + ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); + + //cleanup + prov_auth_destroy(sec_handle); + } + + TEST_FUNCTION(prov_auth_set_registration_id_success) + { + PROV_AUTH_HANDLE sec_handle = prov_auth_create(); + umock_c_reset_all_calls(); + + //arrange + STRICT_EXPECTED_CALL(mallocAndStrcpy_s(IGNORED_PTR_ARG, IGNORED_PTR_ARG)); + + //act + int result = prov_auth_set_registration_id(sec_handle, TEST_REGISTRATION_ID); + + //assert + ASSERT_ARE_EQUAL(int, 0, result); + ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); + + //cleanup + prov_auth_destroy(sec_handle); + } + + TEST_FUNCTION(prov_auth_set_registration_id_2_calls_success) + { + PROV_AUTH_HANDLE sec_handle = prov_auth_create(); + umock_c_reset_all_calls(); + + //arrange + STRICT_EXPECTED_CALL(mallocAndStrcpy_s(IGNORED_PTR_ARG, IGNORED_PTR_ARG)); + + //act + int result = prov_auth_set_registration_id(sec_handle, TEST_REGISTRATION_ID); + ASSERT_ARE_EQUAL(int, 0, result); + result = prov_auth_set_registration_id(sec_handle, TEST_REGISTRATION_ID); + + //assert + ASSERT_ARE_NOT_EQUAL(int, 0, result); + ASSERT_ARE_EQUAL(char_ptr, umock_c_get_expected_calls(), umock_c_get_actual_calls()); + + //cleanup + prov_auth_destroy(sec_handle); + } + TEST_FUNCTION(prov_auth_get_certificate_succeed) { STRICT_EXPECTED_CALL(gballoc_malloc(IGNORED_NUM_ARG));