diff --git a/sdk/keyvault/azure-security-keyvault-keys/test/ut/CMakeLists.txt b/sdk/keyvault/azure-security-keyvault-keys/test/ut/CMakeLists.txt index 59d443e18..2f825c2f9 100644 --- a/sdk/keyvault/azure-security-keyvault-keys/test/ut/CMakeLists.txt +++ b/sdk/keyvault/azure-security-keyvault-keys/test/ut/CMakeLists.txt @@ -17,7 +17,7 @@ add_executable ( key_client_test.cpp macro_guard.cpp mocked_transport_adapter_test.hpp - telemetry_header_test.cpp + mocked_client_test.cpp ) if (MSVC) diff --git a/sdk/keyvault/azure-security-keyvault-keys/test/ut/mocked_client_test.cpp b/sdk/keyvault/azure-security-keyvault-keys/test/ut/mocked_client_test.cpp new file mode 100644 index 000000000..a91b77a94 --- /dev/null +++ b/sdk/keyvault/azure-security-keyvault-keys/test/ut/mocked_client_test.cpp @@ -0,0 +1,168 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// SPDX-License-Identifier: MIT + +#if defined(_MSC_VER) +#define _CRT_SECURE_NO_WARNINGS +#endif + +#include "gtest/gtest.h" + +#include "mocked_transport_adapter_test.hpp" + +#include +#include + +#include +using namespace Azure::Security::KeyVault::Keys; +using namespace Azure::Security::KeyVault::Keys::Test; + +TEST_F(MockedTransportAdapterTest, keyvaultTelemetryId) +{ + std::string applicationId("ourApplicationId"); + m_clientOptions.Telemetry.ApplicationId = applicationId; + m_client = std::make_unique< + Azure::Security::KeyVault::Keys::Test::KeyClientWithNoAuthenticationPolicy>( + "url", m_clientOptions); + + // The fake response from the mocked transport adapter is good for parsing a Key back + auto response = m_client->GetKey("name"); + + // The response is an echo of the sent headers. Let's find the telemetry ID + auto foundHeader = false; + for (auto& header : response.RawResponse->GetHeaders()) + { + if (Azure::Core::_internal::StringExtensions::LocaleInvariantCaseInsensitiveEqual( + header.first, "User-Agent")) + { + foundHeader = true; + EXPECT_PRED2( + [](std::string const& received, std::string const& sent) { + auto telemetryInfoWithNoOSAndDate = received.substr(0, sent.size()); + return Azure::Core::_internal::StringExtensions::LocaleInvariantCaseInsensitiveEqual( + telemetryInfoWithNoOSAndDate, sent); + }, + header.second, + applicationId); + break; + } + } + EXPECT_TRUE(foundHeader); +} + +TEST_F(MockedTransportAdapterTest, CreateKeyRSA) +{ + std::string applicationId("CreateKeyRSA"); + m_clientOptions.Telemetry.ApplicationId = applicationId; + m_client = std::make_unique< + Azure::Security::KeyVault::Keys::Test::KeyClientWithNoAuthenticationPolicy>( + "url", m_clientOptions); + + // The fake response from the mocked transport adapter is good for parsing a Key back + auto response = m_client->CreateKey("name", KeyVaultKeyType::Rsa); + + EXPECT_EQ(response.Value.GetKeyType(), KeyVaultKeyType::Rsa); +} + +TEST_F(MockedTransportAdapterTest, CreateKeyRSA2) +{ + std::string applicationId("CreateKeyRSA"); + m_clientOptions.Telemetry.ApplicationId = applicationId; + m_client = std::make_unique< + Azure::Security::KeyVault::Keys::Test::KeyClientWithNoAuthenticationPolicy>( + "url", m_clientOptions); + + auto options = CreateRsaKeyOptions("name"); + // The fake response from the mocked transport adapter is good for parsing a Key back + auto response = m_client->CreateRsaKey(options); + + EXPECT_EQ(response.Value.GetKeyType(), KeyVaultKeyType::Rsa); +} + +TEST_F(MockedTransportAdapterTest, CreateKeyRSAHSM) +{ + std::string applicationId("CreateKeyRSAHSM"); + m_clientOptions.Telemetry.ApplicationId = applicationId; + m_client = std::make_unique< + Azure::Security::KeyVault::Keys::Test::KeyClientWithNoAuthenticationPolicy>( + "url", m_clientOptions); + + auto options = CreateRsaKeyOptions("name", true); + // The fake response from the mocked transport adapter is good for parsing a Key back + auto response = m_client->CreateRsaKey(options); + + EXPECT_EQ(response.Value.GetKeyType(), KeyVaultKeyType::RsaHsm); +} + +TEST_F(MockedTransportAdapterTest, CreateKeyEC) +{ + std::string applicationId("CreateKeyEC"); + m_clientOptions.Telemetry.ApplicationId = applicationId; + m_client = std::make_unique< + Azure::Security::KeyVault::Keys::Test::KeyClientWithNoAuthenticationPolicy>( + "url", m_clientOptions); + + auto options = CreateEcKeyOptions("name"); + // The fake response from the mocked transport adapter is good for parsing a Key back + auto response = m_client->CreateEcKey(options); + + EXPECT_EQ(response.Value.GetKeyType(), KeyVaultKeyType::Ec); +} + +TEST_F(MockedTransportAdapterTest, CreateKeyECHSM) +{ + std::string applicationId("CreateKeyECHSM"); + m_clientOptions.Telemetry.ApplicationId = applicationId; + m_client = std::make_unique< + Azure::Security::KeyVault::Keys::Test::KeyClientWithNoAuthenticationPolicy>( + "url", m_clientOptions); + + auto options = CreateEcKeyOptions("name", true); + // The fake response from the mocked transport adapter is good for parsing a Key back + auto response = m_client->CreateEcKey(options); + + EXPECT_EQ(response.Value.GetKeyType(), KeyVaultKeyType::EcHsm); +} + +TEST_F(MockedTransportAdapterTest, CreateKeyOCT) +{ + std::string applicationId("CreateKeyOCT"); + m_clientOptions.Telemetry.ApplicationId = applicationId; + m_client = std::make_unique< + Azure::Security::KeyVault::Keys::Test::KeyClientWithNoAuthenticationPolicy>( + "url", m_clientOptions); + + auto options = CreateOctKeyOptions("name"); + // The fake response from the mocked transport adapter is good for parsing a Key back + auto response = m_client->CreateOctKey(options); + + EXPECT_EQ(response.Value.GetKeyType(), KeyVaultKeyType::Oct); +} + +TEST_F(MockedTransportAdapterTest, CreateKeyOCTHSM) +{ + std::string applicationId("CreateKeyOCTHSM"); + m_clientOptions.Telemetry.ApplicationId = applicationId; + m_client = std::make_unique< + Azure::Security::KeyVault::Keys::Test::KeyClientWithNoAuthenticationPolicy>( + "url", m_clientOptions); + + auto options = CreateOctKeyOptions("name", true); + // The fake response from the mocked transport adapter is good for parsing a Key back + auto response = m_client->CreateOctKey(options); + + EXPECT_EQ(response.Value.GetKeyType(), KeyVaultKeyType::OctHsm); +} + +TEST_F(MockedTransportAdapterTest, GetPropertiesOfKeys) +{ + std::string applicationId("CreateKey"); + m_clientOptions.Telemetry.ApplicationId = applicationId; + m_client = std::make_unique< + Azure::Security::KeyVault::Keys::Test::KeyClientWithNoAuthenticationPolicy>( + "url", m_clientOptions); + + auto options = GetPropertiesOfKeysOptions(); + // The fake response from the mocked transport adapter is good for parsing a Key back + auto response = m_client->GetPropertiesOfKeys(); + EXPECT_NE(response.RawResponse, nullptr); +} diff --git a/sdk/keyvault/azure-security-keyvault-keys/test/ut/mocked_transport_adapter_test.hpp b/sdk/keyvault/azure-security-keyvault-keys/test/ut/mocked_transport_adapter_test.hpp index d89df29e4..7c8165a0f 100644 --- a/sdk/keyvault/azure-security-keyvault-keys/test/ut/mocked_transport_adapter_test.hpp +++ b/sdk/keyvault/azure-security-keyvault-keys/test/ut/mocked_transport_adapter_test.hpp @@ -9,20 +9,21 @@ #include +#include "./../../src/private/key_serializers.hpp" #include #include - #include +#include namespace Azure { namespace Security { namespace KeyVault { namespace Keys { namespace Test { namespace _detail { // Return a simple key as response so keyvault can parse it to create the T response // Fake key from https://docs.microsoft.com/en-us/rest/api/keyvault/GetKey/GetKey#examples - constexpr static const char FakeKey[] + static const char FakeKey[] = "{ \"key\": { \"kid\": " "\"https://myvault.vault.azure.net/keys/CreateSoftKeyTest/" - "78deebed173b48e48f55abf87ed4cf71\", \"kty\": \"RSA\", \"key_ops\": [ " + "78deebed173b48e48f55abf87ed4cf71\", \"kty\": \"%s\", \"key_ops\": [ " "\"encrypt\", \"decrypt\", \"sign\", \"verify\", \"wrapKey\", " "\"unwrapKey\" ]}, \"attributes\": { \"enabled\": true, " "\"created\": 1493942451, \"updated\": 1493942451, \"recoveryLevel\": " @@ -46,11 +47,47 @@ namespace Azure { namespace Security { namespace KeyVault { namespace Keys { nam { response->SetHeader(header.first, header.second); } - std::string bodyCount(_detail::FakeKey); + auto updatedFakeKey = UpdateFakeKey(_detail::FakeKey, request.GetHeaders()["user-agent"]); + std::string bodyCount(updatedFakeKey); response->SetBodyStream(std::make_unique( - reinterpret_cast(_detail::FakeKey), bodyCount.size())); + reinterpret_cast(updatedFakeKey), bodyCount.size())); return response; } // namespace Azure + + const char* UpdateFakeKey(const char fakeKey[], std::string header) + { + char* result; + std::string keyType = "RSA"; + + if (header.find("CreateKeyRSAHSM") != std::string::npos) + { + keyType = "RSA-HSM"; + } + else if (header.find("CreateKeyECHSM") != std::string::npos) + { + keyType = "EC-HSM"; + } + else if (header.find("CreateKeyOCTHSM") != std::string::npos) + { + keyType = "oct-HSM"; + } + else if (header.find("CreateKeyRSA") != std::string::npos) + { + keyType = "RSA"; + } + else if (header.find("CreateKeyEC") != std::string::npos) + { + keyType = "EC"; + } + else if (header.find("CreateKeyOCT") != std::string::npos) + { + keyType = "oct"; + } + + result = new char[std::string(fakeKey).size() + keyType.size()]; + std::sprintf(result, fakeKey, keyType.c_str()); + return result; + } }; // namespace Test // A derived class with no credential and authentication diff --git a/sdk/keyvault/azure-security-keyvault-keys/test/ut/telemetry_header_test.cpp b/sdk/keyvault/azure-security-keyvault-keys/test/ut/telemetry_header_test.cpp deleted file mode 100644 index a7fd9e9c9..000000000 --- a/sdk/keyvault/azure-security-keyvault-keys/test/ut/telemetry_header_test.cpp +++ /dev/null @@ -1,50 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// SPDX-License-Identifier: MIT - -#if defined(_MSC_VER) -#define _CRT_SECURE_NO_WARNINGS -#endif - -#include "gtest/gtest.h" - -#include "mocked_transport_adapter_test.hpp" - -#include -#include - -#include - -using namespace Azure::Security::KeyVault::Keys::Test; - -TEST_F(MockedTransportAdapterTest, keyvaultTelemetryId) -{ - std::string applicationId("ourApplicationId"); - m_clientOptions.Telemetry.ApplicationId = applicationId; - m_client = std::make_unique< - Azure::Security::KeyVault::Keys::Test::KeyClientWithNoAuthenticationPolicy>( - "url", m_clientOptions); - - // The fake response from the mocked transport adapter is good for parsing a Key back - auto response = m_client->GetKey("name"); - - // The response is an echo of the sent headers. Let's find the telemetry ID - auto foundHeader = false; - for (auto& header : response.RawResponse->GetHeaders()) - { - if (Azure::Core::_internal::StringExtensions::LocaleInvariantCaseInsensitiveEqual( - header.first, "User-Agent")) - { - foundHeader = true; - EXPECT_PRED2( - [](std::string const& received, std::string const& sent) { - auto telemetryInfoWithNoOSAndDate = received.substr(0, sent.size()); - return Azure::Core::_internal::StringExtensions::LocaleInvariantCaseInsensitiveEqual( - telemetryInfoWithNoOSAndDate, sent); - }, - header.second, - applicationId); - break; - } - } - EXPECT_TRUE(foundHeader); -}