add some more uts for create (#2510)

* add some more uts for create

* format

* remove unused include

* Update sdk/keyvault/azure-security-keyvault-keys/test/ut/mocked_transport_adapter_test.hpp

Co-authored-by: Anton Kolesnyk <41349689+antkmsft@users.noreply.github.com>

* put back include, it doesn't work otherwise

* format

* Revert "format"

This reverts commit afefd264868ededefe8ad715dd1674a333fd1176.

* Revert "put back include, it doesn't work otherwise"

This reverts commit 54f05fe8d2b63f089e76931c5592b64e80f98b5e.

* Revert "Update sdk/keyvault/azure-security-keyvault-keys/test/ut/mocked_transport_adapter_test.hpp"

This reverts commit 8ce4f0b3a2e893be69afdfc710c060fe76c5493a.

Co-authored-by: Anton Kolesnyk <41349689+antkmsft@users.noreply.github.com>
This commit is contained in:
George Arama 2021-07-07 21:00:11 -07:00 committed by GitHub
parent e8efcd21fd
commit ecbf8d0784
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 211 additions and 56 deletions

View File

@ -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)

View File

@ -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 <azure/core/internal/strings.hpp>
#include <azure/keyvault/key_vault_keys.hpp>
#include <string>
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);
}

View File

@ -9,20 +9,21 @@
#include <gtest/gtest.h>
#include "./../../src/private/key_serializers.hpp"
#include <azure/core.hpp>
#include <azure/keyvault/key_vault_keys.hpp>
#include <cstdio>
#include <string>
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<Azure::Core::IO::MemoryBodyStream>(
reinterpret_cast<const uint8_t*>(_detail::FakeKey), bodyCount.size()));
reinterpret_cast<const uint8_t*>(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

View File

@ -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 <azure/core/internal/strings.hpp>
#include <azure/keyvault/key_vault_keys.hpp>
#include <string>
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);
}