Commit efa29982 authored by Pavol Marko's avatar Pavol Marko Committed by Commit Bot

Split PlatformKeysServiceBrowserTest to have one test per token

Previously some tests were looping over available tokens and performing
tests per token in that way, which is bad for error messages.

Bug: 1045895
Change-Id: I45e542d06d44ce08cc40031296c69bc7625c78bd
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2159468
Commit-Queue: Pavol Marko <pmarko@chromium.org>
Reviewed-by: default avatarMaksim Ivanov <emaxx@chromium.org>
Reviewed-by: default avatarOmar Morsi <omorsi@google.com>
Cr-Commit-Position: refs/heads/master@{#789492}
parent 679eb796
...@@ -73,17 +73,26 @@ enum class ProfileToUse { ...@@ -73,17 +73,26 @@ enum class ProfileToUse {
kSigninProfile kSigninProfile
}; };
// Describes a test configuration for the test suite. // Describes a test configuration for the test suite that runs one test per
struct TestConfig { // profile.
struct TestConfigPerProfile {
// The profile for which PlatformKeysService should be tested. // The profile for which PlatformKeysService should be tested.
ProfileToUse profile_to_use; ProfileToUse profile_to_use;
// The token IDs that are expected to be available. This will be checked by // The token IDs that are expected to be available for |profile_to_use|.
// the GetTokens test, and operation for these tokens will be performed by the
// other tests.
std::vector<TokenId> token_ids; std::vector<TokenId> token_ids;
}; };
// Describes a test configuration for the test suite that runs one test per
// (profile, token) combination.
struct TestConfigPerToken {
// The profile for which PlatformKeysService should be tested.
ProfileToUse profile_to_use;
// The token ID to perform the tests on.
TokenId token_id;
};
// Softoken NSS PKCS11 module (used for testing) allows only predefined key // Softoken NSS PKCS11 module (used for testing) allows only predefined key
// attributes to be set and retrieved. Chaps supports setting and retrieving // attributes to be set and retrieved. Chaps supports setting and retrieving
// custom attributes. // custom attributes.
...@@ -264,16 +273,15 @@ class GetAllKeysExecutionWaiter ...@@ -264,16 +273,15 @@ class GetAllKeysExecutionWaiter
}; };
} // namespace } // namespace
class PlatformKeysServiceBrowserTest class PlatformKeysServiceBrowserTestBase
: public MixinBasedInProcessBrowserTest, : public MixinBasedInProcessBrowserTest {
public ::testing::WithParamInterface<TestConfig> {
public: public:
PlatformKeysServiceBrowserTest() = default; PlatformKeysServiceBrowserTestBase() = default;
~PlatformKeysServiceBrowserTest() override = default; PlatformKeysServiceBrowserTestBase(
PlatformKeysServiceBrowserTest(const PlatformKeysServiceBrowserTest& other) = const PlatformKeysServiceBrowserTestBase& other) = delete;
delete; PlatformKeysServiceBrowserTestBase& operator=(
PlatformKeysServiceBrowserTest& operator=( const PlatformKeysServiceBrowserTestBase& other) = delete;
const PlatformKeysServiceBrowserTest& other) = delete; ~PlatformKeysServiceBrowserTestBase() override = default;
void SetUpInProcessBrowserTestFixture() override { void SetUpInProcessBrowserTestFixture() override {
MixinBasedInProcessBrowserTest::SetUpInProcessBrowserTestFixture(); MixinBasedInProcessBrowserTest::SetUpInProcessBrowserTestFixture();
...@@ -282,7 +290,7 @@ class PlatformKeysServiceBrowserTest ...@@ -282,7 +290,7 @@ class PlatformKeysServiceBrowserTest
// (empty) policy blobs are prepared in FakeSessionManagerClient. // (empty) policy blobs are prepared in FakeSessionManagerClient.
auto user_policy_update = user_policy_mixin_.RequestPolicyUpdate(); auto user_policy_update = user_policy_mixin_.RequestPolicyUpdate();
auto device_policy_update = device_state_mixin_.RequestDevicePolicyUpdate(); auto device_policy_update = device_state_mixin_.RequestDevicePolicyUpdate();
if (GetParam().profile_to_use == ProfileToUse::kAffiliatedUserProfile) { if (GetProfileToUse() == ProfileToUse::kAffiliatedUserProfile) {
device_policy_update->policy_data()->add_device_affiliation_ids( device_policy_update->policy_data()->add_device_affiliation_ids(
kTestAffiliationId); kTestAffiliationId);
user_policy_update->policy_data()->add_user_affiliation_ids( user_policy_update->policy_data()->add_user_affiliation_ids(
...@@ -293,7 +301,7 @@ class PlatformKeysServiceBrowserTest ...@@ -293,7 +301,7 @@ class PlatformKeysServiceBrowserTest
void SetUpOnMainThread() override { void SetUpOnMainThread() override {
MixinBasedInProcessBrowserTest::SetUpOnMainThread(); MixinBasedInProcessBrowserTest::SetUpOnMainThread();
if (GetParam().profile_to_use == ProfileToUse::kSigninProfile) { if (GetProfileToUse() == ProfileToUse::kSigninProfile) {
profile_ = ProfileHelper::GetSigninProfile(); profile_ = ProfileHelper::GetSigninProfile();
} else { } else {
ASSERT_TRUE(login_manager_mixin_.LoginAndWaitForActiveSession( ASSERT_TRUE(login_manager_mixin_.LoginAndWaitForActiveSession(
...@@ -303,7 +311,7 @@ class PlatformKeysServiceBrowserTest ...@@ -303,7 +311,7 @@ class PlatformKeysServiceBrowserTest
base::RunLoop loop; base::RunLoop loop;
GetNSSCertDatabaseForProfile( GetNSSCertDatabaseForProfile(
profile_, profile_,
base::BindRepeating(&PlatformKeysServiceBrowserTest::SetUserSlot, base::BindRepeating(&PlatformKeysServiceBrowserTestBase::SetUserSlot,
base::Unretained(this), loop.QuitClosure())); base::Unretained(this), loop.QuitClosure()));
loop.Run(); loop.Run();
} }
...@@ -326,6 +334,8 @@ class PlatformKeysServiceBrowserTest ...@@ -326,6 +334,8 @@ class PlatformKeysServiceBrowserTest
} }
protected: protected:
virtual ProfileToUse GetProfileToUse() = 0;
PlatformKeysService* platform_keys_service() { PlatformKeysService* platform_keys_service() {
return platform_keys_service_; return platform_keys_service_;
} }
...@@ -353,6 +363,30 @@ class PlatformKeysServiceBrowserTest ...@@ -353,6 +363,30 @@ class PlatformKeysServiceBrowserTest
return generate_key_waiter.public_key_spki_der(); return generate_key_waiter.public_key_spki_der();
} }
// Imports the certificate and key described by the |cert_filename| and
// |key_filename| files in |source_dir| into the Token |token_id|, then stores
// the resulting certificate in *|out_cert| and the SPKI of the public key in
// *|out_spki_der|. Should be wrapped in ASSERT_NO_FATAL_FAILURE.
void ImportCertAndKey(TokenId token_id,
const base::FilePath& source_dir,
const std::string& cert_filename,
const std::string& key_filename,
net::ScopedCERTCertificate* out_cert,
std::string* out_spki_der) {
// Import testing key pair and certificate.
{
base::ScopedAllowBlockingForTesting allow_io;
net::ImportClientCertAndKeyFromFile(
source_dir, cert_filename, key_filename, GetSlot(token_id), out_cert);
}
CERTCertificate* cert = out_cert->get();
ASSERT_TRUE(cert);
ASSERT_GT(cert->derPublicKey.len, 0U);
*out_spki_der =
std::string(reinterpret_cast<const char*>(cert->derPublicKey.data),
cert->derPublicKey.len);
}
private: private:
void SetUserSlot(const base::Closure& done_callback, void SetUserSlot(const base::Closure& done_callback,
net::NSSCertDatabase* db) { net::NSSCertDatabase* db) {
...@@ -386,8 +420,23 @@ class PlatformKeysServiceBrowserTest ...@@ -386,8 +420,23 @@ class PlatformKeysServiceBrowserTest
std::unique_ptr<ScopedSoftokenAttrsMapping> scoped_softoken_attrs_mapping_; std::unique_ptr<ScopedSoftokenAttrsMapping> scoped_softoken_attrs_mapping_;
}; };
class PlatformKeysServicePerProfileBrowserTest
: public PlatformKeysServiceBrowserTestBase,
public ::testing::WithParamInterface<TestConfigPerProfile> {
public:
PlatformKeysServicePerProfileBrowserTest() = default;
PlatformKeysServicePerProfileBrowserTest(
const PlatformKeysServicePerProfileBrowserTest& other) = delete;
PlatformKeysServicePerProfileBrowserTest& operator=(
const PlatformKeysServicePerProfileBrowserTest& other) = delete;
~PlatformKeysServicePerProfileBrowserTest() override = default;
protected:
ProfileToUse GetProfileToUse() override { return GetParam().profile_to_use; }
};
// Tests that GetTokens() is callable and returns the expected tokens. // Tests that GetTokens() is callable and returns the expected tokens.
IN_PROC_BROWSER_TEST_P(PlatformKeysServiceBrowserTest, GetTokens) { IN_PROC_BROWSER_TEST_P(PlatformKeysServicePerProfileBrowserTest, GetTokens) {
GetTokensExecutionWaiter get_tokens_waiter; GetTokensExecutionWaiter get_tokens_waiter;
platform_keys_service()->GetTokens(get_tokens_waiter.GetCallback()); platform_keys_service()->GetTokens(get_tokens_waiter.GetCallback());
get_tokens_waiter.Wait(); get_tokens_waiter.Wait();
...@@ -398,15 +447,119 @@ IN_PROC_BROWSER_TEST_P(PlatformKeysServiceBrowserTest, GetTokens) { ...@@ -398,15 +447,119 @@ IN_PROC_BROWSER_TEST_P(PlatformKeysServiceBrowserTest, GetTokens) {
::testing::UnorderedElementsAreArray(GetParam().token_ids)); ::testing::UnorderedElementsAreArray(GetParam().token_ids));
} }
// Generates a key pair in tokens accessible from the profile under test and
// retrieves them.
IN_PROC_BROWSER_TEST_P(PlatformKeysServicePerProfileBrowserTest, GetAllKeys) {
// Generate key pair in every token.
std::map<TokenId, std::string> token_key_map;
for (TokenId token_id : GetParam().token_ids) {
const std::string public_key_spki_der = GenerateKeyPair(token_id);
ASSERT_FALSE(public_key_spki_der.empty());
token_key_map[token_id] = public_key_spki_der;
}
// Only keys in the requested token should be retrieved.
for (TokenId token_id : GetParam().token_ids) {
GetAllKeysExecutionWaiter get_all_keys_waiter;
platform_keys_service()->GetAllKeys(token_id,
get_all_keys_waiter.GetCallback());
get_all_keys_waiter.Wait();
EXPECT_TRUE(get_all_keys_waiter.error_message().empty());
std::vector<std::string> public_keys = get_all_keys_waiter.public_keys();
ASSERT_EQ(public_keys.size(), 1U);
EXPECT_EQ(public_keys[0], token_key_map[token_id]);
}
}
// Imports the same key into all tokens. Verifies that key attributes are stored
// per-token and don't leak between tokens.
IN_PROC_BROWSER_TEST_P(PlatformKeysServicePerProfileBrowserTest,
KeyAttributesPerToken) {
// Import the same key pair + cert in every token, remember its SPKI.
std::string spki_der;
for (TokenId token_id : GetParam().token_ids) {
net::ScopedCERTCertificate cert;
std::string current_spki_der;
ASSERT_NO_FATAL_FAILURE(
ImportCertAndKey(token_id, net::GetTestCertsDirectory(), "client_1.pem",
"client_1.pk8", &cert, &current_spki_der));
// The SPKI must be the same on every slot, because the same key was
// imported.
if (!spki_der.empty()) {
EXPECT_EQ(current_spki_der, spki_der);
continue;
}
spki_der = current_spki_der;
}
// Set an attribute for the key on each token.
const KeyAttributeType kAttributeType =
KeyAttributeType::CertificateProvisioningId;
std::map<TokenId, std::string> token_to_value;
for (TokenId token_id : GetParam().token_ids) {
token_to_value[token_id] =
base::StringPrintf("test_value_%d", static_cast<int>(token_id));
// Set key attribute.
SetAttributeForKeyExecutionWaiter set_attr_waiter;
platform_keys_service()->SetAttributeForKey(
token_id, spki_der, kAttributeType, token_to_value[token_id],
set_attr_waiter.GetCallback());
set_attr_waiter.Wait();
EXPECT_TRUE(set_attr_waiter.error_message().empty());
}
// Verify the token-specific attribute value for the key on each token.
for (TokenId token_id : GetParam().token_ids) {
// Get key attribute.
GetAttributeForKeyExecutionWaiter get_attr_waiter;
platform_keys_service()->GetAttributeForKey(
token_id, spki_der, kAttributeType, get_attr_waiter.GetCallback());
get_attr_waiter.Wait();
EXPECT_TRUE(get_attr_waiter.error_message().empty());
ASSERT_TRUE(get_attr_waiter.attribute_value());
EXPECT_EQ(get_attr_waiter.attribute_value().value(),
token_to_value[token_id]);
}
}
INSTANTIATE_TEST_SUITE_P(
AllSupportedProfileTypes,
PlatformKeysServicePerProfileBrowserTest,
::testing::Values(
TestConfigPerProfile{ProfileToUse::kSigninProfile, {TokenId::kSystem}},
TestConfigPerProfile{ProfileToUse::kUnaffiliatedUserProfile,
{TokenId::kUser}},
TestConfigPerProfile{ProfileToUse::kAffiliatedUserProfile,
{TokenId::kUser, TokenId::kSystem}}));
class PlatformKeysServicePerTokenBrowserTest
: public PlatformKeysServiceBrowserTestBase,
public ::testing::WithParamInterface<TestConfigPerToken> {
public:
PlatformKeysServicePerTokenBrowserTest() = default;
PlatformKeysServicePerTokenBrowserTest(
const PlatformKeysServicePerTokenBrowserTest& other) = delete;
PlatformKeysServicePerTokenBrowserTest& operator=(
const PlatformKeysServicePerTokenBrowserTest& other) = delete;
~PlatformKeysServicePerTokenBrowserTest() override = default;
protected:
ProfileToUse GetProfileToUse() override { return GetParam().profile_to_use; }
};
// Generates a Rsa key pair and tests signing using that key pair. // Generates a Rsa key pair and tests signing using that key pair.
IN_PROC_BROWSER_TEST_P(PlatformKeysServiceBrowserTest, GenerateRsaAndSign) { IN_PROC_BROWSER_TEST_P(PlatformKeysServicePerTokenBrowserTest,
const std::string data_to_sign = "test"; GenerateRsaAndSign) {
const std::string kDataToSign = "test";
const unsigned int kKeySize = 2048; const unsigned int kKeySize = 2048;
const HashAlgorithm hash_algorithm = HASH_ALGORITHM_SHA256; const HashAlgorithm kHashAlgorithm = HASH_ALGORITHM_SHA256;
const crypto::SignatureVerifier::SignatureAlgorithm signature_algorithm = const crypto::SignatureVerifier::SignatureAlgorithm kSignatureAlgorithm =
crypto::SignatureVerifier::RSA_PKCS1_SHA256; crypto::SignatureVerifier::RSA_PKCS1_SHA256;
for (TokenId token_id : GetParam().token_ids) { const TokenId token_id = GetParam().token_id;
GenerateKeyExecutionWaiter generate_key_waiter; GenerateKeyExecutionWaiter generate_key_waiter;
platform_keys_service()->GenerateRSAKey(token_id, kKeySize, platform_keys_service()->GenerateRSAKey(token_id, kKeySize,
generate_key_waiter.GetCallback()); generate_key_waiter.GetCallback());
...@@ -419,31 +572,27 @@ IN_PROC_BROWSER_TEST_P(PlatformKeysServiceBrowserTest, GenerateRsaAndSign) { ...@@ -419,31 +572,27 @@ IN_PROC_BROWSER_TEST_P(PlatformKeysServiceBrowserTest, GenerateRsaAndSign) {
SignExecutionWaiter sign_waiter; SignExecutionWaiter sign_waiter;
platform_keys_service()->SignRSAPKCS1Digest( platform_keys_service()->SignRSAPKCS1Digest(
token_id, data_to_sign, public_key_spki_der, hash_algorithm, token_id, kDataToSign, public_key_spki_der, kHashAlgorithm,
sign_waiter.GetCallback()); sign_waiter.GetCallback());
sign_waiter.Wait(); sign_waiter.Wait();
EXPECT_TRUE(sign_waiter.error_message().empty()); EXPECT_TRUE(sign_waiter.error_message().empty());
crypto::SignatureVerifier signature_verifier; crypto::SignatureVerifier signature_verifier;
ASSERT_TRUE(signature_verifier.VerifyInit( ASSERT_TRUE(signature_verifier.VerifyInit(
signature_algorithm, kSignatureAlgorithm,
base::as_bytes(base::make_span(sign_waiter.signature())), base::as_bytes(base::make_span(sign_waiter.signature())),
base::as_bytes(base::make_span(public_key_spki_der)))); base::as_bytes(base::make_span(public_key_spki_der))));
signature_verifier.VerifyUpdate( signature_verifier.VerifyUpdate(base::as_bytes(base::make_span(kDataToSign)));
base::as_bytes(base::make_span(data_to_sign)));
EXPECT_TRUE(signature_verifier.VerifyFinal()); EXPECT_TRUE(signature_verifier.VerifyFinal());
}
} }
IN_PROC_BROWSER_TEST_P(PlatformKeysServiceBrowserTest, SetAndGetKeyAttribute) { IN_PROC_BROWSER_TEST_P(PlatformKeysServicePerTokenBrowserTest,
SetAndGetKeyAttribute) {
// The attribute type to be set and retrieved using platform keys service. // The attribute type to be set and retrieved using platform keys service.
const KeyAttributeType kAttributeType = const KeyAttributeType kAttributeType =
KeyAttributeType::CertificateProvisioningId; KeyAttributeType::CertificateProvisioningId;
const TokenId token_id = GetParam().token_id;
for (TokenId token_id : GetParam().token_ids) { const std::string kAttributeValue = "test_attr_value";
const int token_id_as_int = static_cast<int>(token_id);
const std::string attribute_value =
base::StringPrintf("test%d", token_id_as_int);
// Generate key pair. // Generate key pair.
const std::string public_key_spki_der = GenerateKeyPair(token_id); const std::string public_key_spki_der = GenerateKeyPair(token_id);
...@@ -452,7 +601,7 @@ IN_PROC_BROWSER_TEST_P(PlatformKeysServiceBrowserTest, SetAndGetKeyAttribute) { ...@@ -452,7 +601,7 @@ IN_PROC_BROWSER_TEST_P(PlatformKeysServiceBrowserTest, SetAndGetKeyAttribute) {
// Set key attribute. // Set key attribute.
SetAttributeForKeyExecutionWaiter set_attribute_for_key_execution_waiter; SetAttributeForKeyExecutionWaiter set_attribute_for_key_execution_waiter;
platform_keys_service()->SetAttributeForKey( platform_keys_service()->SetAttributeForKey(
token_id, public_key_spki_der, kAttributeType, attribute_value, token_id, public_key_spki_der, kAttributeType, kAttributeValue,
set_attribute_for_key_execution_waiter.GetCallback()); set_attribute_for_key_execution_waiter.GetCallback());
set_attribute_for_key_execution_waiter.Wait(); set_attribute_for_key_execution_waiter.Wait();
...@@ -466,20 +615,19 @@ IN_PROC_BROWSER_TEST_P(PlatformKeysServiceBrowserTest, SetAndGetKeyAttribute) { ...@@ -466,20 +615,19 @@ IN_PROC_BROWSER_TEST_P(PlatformKeysServiceBrowserTest, SetAndGetKeyAttribute) {
EXPECT_TRUE(get_attribute_for_key_execution_waiter.error_message().empty()); EXPECT_TRUE(get_attribute_for_key_execution_waiter.error_message().empty());
ASSERT_TRUE(get_attribute_for_key_execution_waiter.attribute_value()); ASSERT_TRUE(get_attribute_for_key_execution_waiter.attribute_value());
EXPECT_EQ(get_attribute_for_key_execution_waiter.attribute_value().value(), EXPECT_EQ(get_attribute_for_key_execution_waiter.attribute_value().value(),
attribute_value); kAttributeValue);
}
} }
// TODO(https://crbug.com/1073515): Add a test for an unset key attribute when // TODO(https://crbug.com/1073515): Add a test for an unset key attribute when
// simulating chaps behavior is possible. // simulating chaps behavior is possible.
IN_PROC_BROWSER_TEST_P(PlatformKeysServiceBrowserTest, IN_PROC_BROWSER_TEST_P(PlatformKeysServicePerTokenBrowserTest,
GetKeyAttributeForNonExistingKey) { GetKeyAttributeForNonExistingKey) {
const KeyAttributeType kAttributeType = const KeyAttributeType kAttributeType =
KeyAttributeType::CertificateProvisioningId; KeyAttributeType::CertificateProvisioningId;
const TokenId token_id = GetParam().token_id;
const std::string kPublicKey = "Non Existing public key"; const std::string kPublicKey = "Non Existing public key";
for (TokenId token_id : GetParam().token_ids) {
// Get key attribute. // Get key attribute.
GetAttributeForKeyExecutionWaiter get_attribute_for_key_execution_waiter; GetAttributeForKeyExecutionWaiter get_attribute_for_key_execution_waiter;
platform_keys_service()->GetAttributeForKey( platform_keys_service()->GetAttributeForKey(
...@@ -487,20 +635,18 @@ IN_PROC_BROWSER_TEST_P(PlatformKeysServiceBrowserTest, ...@@ -487,20 +635,18 @@ IN_PROC_BROWSER_TEST_P(PlatformKeysServiceBrowserTest,
get_attribute_for_key_execution_waiter.GetCallback()); get_attribute_for_key_execution_waiter.GetCallback());
get_attribute_for_key_execution_waiter.Wait(); get_attribute_for_key_execution_waiter.Wait();
EXPECT_FALSE( EXPECT_FALSE(get_attribute_for_key_execution_waiter.error_message().empty());
get_attribute_for_key_execution_waiter.error_message().empty()); EXPECT_FALSE(get_attribute_for_key_execution_waiter.attribute_value());
ASSERT_FALSE(get_attribute_for_key_execution_waiter.attribute_value());
}
} }
IN_PROC_BROWSER_TEST_P(PlatformKeysServiceBrowserTest, IN_PROC_BROWSER_TEST_P(PlatformKeysServicePerTokenBrowserTest,
SetKeyAttributeForNonExistingKey) { SetKeyAttributeForNonExistingKey) {
const KeyAttributeType kAttributeType = const KeyAttributeType kAttributeType =
KeyAttributeType::CertificateProvisioningId; KeyAttributeType::CertificateProvisioningId;
const TokenId token_id = GetParam().token_id;
const std::string kAttributeValue = "test"; const std::string kAttributeValue = "test";
const std::string kPublicKey = "Non Existing public key"; const std::string kPublicKey = "Non Existing public key";
for (TokenId token_id : GetParam().token_ids) {
// Set key attribute. // Set key attribute.
SetAttributeForKeyExecutionWaiter set_attribute_for_key_execution_waiter; SetAttributeForKeyExecutionWaiter set_attribute_for_key_execution_waiter;
platform_keys_service()->SetAttributeForKey( platform_keys_service()->SetAttributeForKey(
...@@ -508,14 +654,13 @@ IN_PROC_BROWSER_TEST_P(PlatformKeysServiceBrowserTest, ...@@ -508,14 +654,13 @@ IN_PROC_BROWSER_TEST_P(PlatformKeysServiceBrowserTest,
set_attribute_for_key_execution_waiter.GetCallback()); set_attribute_for_key_execution_waiter.GetCallback());
set_attribute_for_key_execution_waiter.Wait(); set_attribute_for_key_execution_waiter.Wait();
EXPECT_FALSE( EXPECT_FALSE(set_attribute_for_key_execution_waiter.error_message().empty());
set_attribute_for_key_execution_waiter.error_message().empty());
}
} }
IN_PROC_BROWSER_TEST_P(PlatformKeysServiceBrowserTest, IN_PROC_BROWSER_TEST_P(PlatformKeysServicePerTokenBrowserTest,
RemoveKeyWithNoMatchingCertificates) { RemoveKeyWithNoMatchingCertificates) {
for (TokenId token_id : GetParam().token_ids) { const TokenId token_id = GetParam().token_id;
// Generate first key pair. // Generate first key pair.
const std::string public_key_1 = GenerateKeyPair(token_id); const std::string public_key_1 = GenerateKeyPair(token_id);
ASSERT_FALSE(public_key_1.empty()); ASSERT_FALSE(public_key_1.empty());
...@@ -537,13 +682,11 @@ IN_PROC_BROWSER_TEST_P(PlatformKeysServiceBrowserTest, ...@@ -537,13 +682,11 @@ IN_PROC_BROWSER_TEST_P(PlatformKeysServiceBrowserTest,
EXPECT_TRUE(remove_key_waiter.error_message().empty()); EXPECT_TRUE(remove_key_waiter.error_message().empty());
EXPECT_FALSE(crypto::FindNSSKeyFromPublicKeyInfo(public_key_bytes_1)); EXPECT_FALSE(crypto::FindNSSKeyFromPublicKeyInfo(public_key_bytes_1));
EXPECT_TRUE(crypto::FindNSSKeyFromPublicKeyInfo(public_key_bytes_2)); EXPECT_TRUE(crypto::FindNSSKeyFromPublicKeyInfo(public_key_bytes_2));
}
} }
IN_PROC_BROWSER_TEST_P(PlatformKeysServiceBrowserTest, IN_PROC_BROWSER_TEST_P(PlatformKeysServicePerTokenBrowserTest,
RemoveKeyWithMatchingCertificate) { RemoveKeyWithMatchingCertificate) {
for (TokenId token_id : GetParam().token_ids) { const TokenId token_id = GetParam().token_id;
PK11SlotInfo* const slot = GetSlot(token_id);
// Assert that there are no certificates before importing. // Assert that there are no certificates before importing.
GetCertificatesExecutionWaiter get_certificates_waiter; GetCertificatesExecutionWaiter get_certificates_waiter;
...@@ -552,14 +695,11 @@ IN_PROC_BROWSER_TEST_P(PlatformKeysServiceBrowserTest, ...@@ -552,14 +695,11 @@ IN_PROC_BROWSER_TEST_P(PlatformKeysServiceBrowserTest,
get_certificates_waiter.Wait(); get_certificates_waiter.Wait();
ASSERT_EQ(get_certificates_waiter.matches().size(), 0U); ASSERT_EQ(get_certificates_waiter.matches().size(), 0U);
// Import testing key pair and certificate.
net::ScopedCERTCertificate cert; net::ScopedCERTCertificate cert;
{ std::string public_key;
base::ScopedAllowBlockingForTesting allow_io; ASSERT_NO_FATAL_FAILURE(
net::ImportClientCertAndKeyFromFile(net::GetTestCertsDirectory(), ImportCertAndKey(token_id, net::GetTestCertsDirectory(), "client_1.pem",
"client_1.pem", "client_1.pk8", slot, "client_1.pk8", &cert, &public_key));
&cert);
}
// Assert that the certificate is imported correctly. // Assert that the certificate is imported correctly.
ASSERT_TRUE(cert.get()); ASSERT_TRUE(cert.get());
...@@ -569,10 +709,6 @@ IN_PROC_BROWSER_TEST_P(PlatformKeysServiceBrowserTest, ...@@ -569,10 +709,6 @@ IN_PROC_BROWSER_TEST_P(PlatformKeysServiceBrowserTest,
get_certificates_waiter_2.Wait(); get_certificates_waiter_2.Wait();
ASSERT_EQ(get_certificates_waiter_2.matches().size(), 1U); ASSERT_EQ(get_certificates_waiter_2.matches().size(), 1U);
ASSERT_GT(cert->derPublicKey.len, 0U);
std::string public_key(
reinterpret_cast<const char*>(cert->derPublicKey.data),
cert->derPublicKey.len);
auto public_key_bytes = base::as_bytes(base::make_span(public_key)); auto public_key_bytes = base::as_bytes(base::make_span(public_key));
EXPECT_TRUE(crypto::FindNSSKeyFromPublicKeyInfo(public_key_bytes)); EXPECT_TRUE(crypto::FindNSSKeyFromPublicKeyInfo(public_key_bytes));
...@@ -596,37 +732,11 @@ IN_PROC_BROWSER_TEST_P(PlatformKeysServiceBrowserTest, ...@@ -596,37 +732,11 @@ IN_PROC_BROWSER_TEST_P(PlatformKeysServiceBrowserTest,
// Assert that the key pair is not deleted. // Assert that the key pair is not deleted.
EXPECT_TRUE(crypto::FindNSSKeyFromPublicKeyInfo(public_key_bytes)); EXPECT_TRUE(crypto::FindNSSKeyFromPublicKeyInfo(public_key_bytes));
}
} }
// Generates a key pair in tokens accessible from the profile under test and IN_PROC_BROWSER_TEST_P(PlatformKeysServicePerTokenBrowserTest,
// retrieves them.
IN_PROC_BROWSER_TEST_P(PlatformKeysServiceBrowserTest, GetAllKeys) {
// Generate key pair in every token.
std::map<TokenId, std::string> token_key_map;
for (TokenId token_id : GetParam().token_ids) {
const std::string public_key_spki_der = GenerateKeyPair(token_id);
ASSERT_FALSE(public_key_spki_der.empty());
token_key_map[token_id] = public_key_spki_der;
}
// Only keys in the requested token should be retrieved.
for (TokenId token_id : GetParam().token_ids) {
GetAllKeysExecutionWaiter get_all_keys_waiter;
platform_keys_service()->GetAllKeys(token_id,
get_all_keys_waiter.GetCallback());
get_all_keys_waiter.Wait();
EXPECT_TRUE(get_all_keys_waiter.error_message().empty());
std::vector<std::string> public_keys = get_all_keys_waiter.public_keys();
ASSERT_EQ(public_keys.size(), 1U);
EXPECT_EQ(public_keys[0], token_key_map[token_id]);
}
}
IN_PROC_BROWSER_TEST_P(PlatformKeysServiceBrowserTest,
GetAllKeysWhenNoKeysGenerated) { GetAllKeysWhenNoKeysGenerated) {
for (TokenId token_id : GetParam().token_ids) { const TokenId token_id = GetParam().token_id;
GetAllKeysExecutionWaiter get_all_keys_waiter; GetAllKeysExecutionWaiter get_all_keys_waiter;
platform_keys_service()->GetAllKeys(token_id, platform_keys_service()->GetAllKeys(token_id,
get_all_keys_waiter.GetCallback()); get_all_keys_waiter.GetCallback());
...@@ -635,17 +745,47 @@ IN_PROC_BROWSER_TEST_P(PlatformKeysServiceBrowserTest, ...@@ -635,17 +745,47 @@ IN_PROC_BROWSER_TEST_P(PlatformKeysServiceBrowserTest,
EXPECT_TRUE(get_all_keys_waiter.error_message().empty()); EXPECT_TRUE(get_all_keys_waiter.error_message().empty());
std::vector<std::string> public_keys = get_all_keys_waiter.public_keys(); std::vector<std::string> public_keys = get_all_keys_waiter.public_keys();
EXPECT_TRUE(public_keys.empty()); EXPECT_TRUE(public_keys.empty());
}
} }
INSTANTIATE_TEST_SUITE_P( INSTANTIATE_TEST_SUITE_P(
AllSupportedProfileTypes, AllSupportedProfilesAndTokensTypes,
PlatformKeysServiceBrowserTest, PlatformKeysServicePerTokenBrowserTest,
::testing::Values( ::testing::Values(TestConfigPerToken{ProfileToUse::kSigninProfile,
TestConfig{ProfileToUse::kSigninProfile, {TokenId::kSystem}}, TokenId::kSystem},
TestConfig{ProfileToUse::kUnaffiliatedUserProfile, {TokenId::kUser}}, TestConfigPerToken{ProfileToUse::kUnaffiliatedUserProfile,
TestConfig{ProfileToUse::kAffiliatedUserProfile, TokenId::kUser},
{TokenId::kSystem, TokenId::kUser}})); TestConfigPerToken{ProfileToUse::kAffiliatedUserProfile,
TokenId::kSystem},
TestConfigPerToken{ProfileToUse::kAffiliatedUserProfile,
TokenId::kUser}));
// PlatformKeysServicePerUnavailableTokenBrowserTest is essentially the same as
// PlatformKeysServicePerTokenBrowserTest but contains different test cases
// (testing that the token is not available) and runs on a different set of
// (profile, token) pairs accordingly.
using PlatformKeysServicePerUnavailableTokenBrowserTest =
PlatformKeysServicePerTokenBrowserTest;
// Uses GenerateRSAKey as an example operation that should fail because the
// token is not available.
IN_PROC_BROWSER_TEST_P(PlatformKeysServicePerUnavailableTokenBrowserTest,
GenerateRsa) {
const unsigned int kKeySize = 2048;
const TokenId token_id = GetParam().token_id;
GenerateKeyExecutionWaiter generate_key_waiter;
platform_keys_service()->GenerateRSAKey(token_id, kKeySize,
generate_key_waiter.GetCallback());
generate_key_waiter.Wait();
EXPECT_FALSE(generate_key_waiter.error_message().empty());
}
INSTANTIATE_TEST_SUITE_P(
AllSupportedProfilesAndTokensTypes,
PlatformKeysServicePerUnavailableTokenBrowserTest,
::testing::Values(TestConfigPerToken{ProfileToUse::kSigninProfile,
TokenId::kUser},
TestConfigPerToken{ProfileToUse::kUnaffiliatedUserProfile,
TokenId::kSystem}));
} // namespace platform_keys } // namespace platform_keys
} // namespace chromeos } // namespace chromeos
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment