| // Copyright 2020 The ChromiumOS Authors |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| #include "cryptohome/credential_verifier.h" |
| |
| #include <memory> |
| #include <variant> |
| |
| #include <gtest/gtest.h> |
| |
| #include "cryptohome/auth_factor/metadata.h" |
| #include "cryptohome/auth_factor/type.h" |
| #include "cryptohome/key_objects.h" |
| |
| namespace cryptohome { |
| namespace { |
| |
| using ::cryptohome::error::CryptohomeError; |
| using ::hwsec_foundation::status::MakeStatus; |
| using ::hwsec_foundation::status::OkStatus; |
| |
| // Minimal concrete implementation of CredentialVerifier, so that we can test |
| // the abstract base class functions. |
| class TestVerifier : public SyncCredentialVerifier { |
| public: |
| TestVerifier(AuthFactorType auth_factor_type, |
| std::string auth_factor_label, |
| AuthFactorMetadata auth_factor_metadata) |
| : SyncCredentialVerifier(auth_factor_type, |
| std::move(auth_factor_label), |
| std::move(auth_factor_metadata)) {} |
| |
| // Just work. Doesn't matter because we don't use this in the test. |
| CryptohomeStatus VerifySync(const AuthInput&) const override { |
| return OkStatus<CryptohomeError>(); |
| } |
| }; |
| |
| class CredentialVerifierTest : public ::testing::Test { |
| public: |
| CredentialVerifierTest() |
| : pw_verifier_(AuthFactorType::kPassword, |
| "password", |
| {.metadata = PasswordMetadata()}), |
| pin_verifier_( |
| AuthFactorType::kPin, "pin", {.metadata = PinMetadata()}) {} |
| |
| protected: |
| // A couple of verifiers that we can test with. |
| TestVerifier pw_verifier_; |
| TestVerifier pin_verifier_; |
| }; |
| |
| TEST_F(CredentialVerifierTest, AuthFactorType) { |
| EXPECT_EQ(pw_verifier_.auth_factor_type(), AuthFactorType::kPassword); |
| EXPECT_EQ(pin_verifier_.auth_factor_type(), AuthFactorType::kPin); |
| } |
| |
| TEST_F(CredentialVerifierTest, AuthFactorLabel) { |
| EXPECT_EQ(pw_verifier_.auth_factor_label(), "password"); |
| EXPECT_EQ(pin_verifier_.auth_factor_label(), "pin"); |
| } |
| |
| TEST_F(CredentialVerifierTest, AuthFactorMetadata) { |
| EXPECT_TRUE(std::holds_alternative<PasswordMetadata>( |
| pw_verifier_.auth_factor_metadata().metadata)); |
| EXPECT_TRUE(std::holds_alternative<PinMetadata>( |
| pin_verifier_.auth_factor_metadata().metadata)); |
| } |
| |
| TEST_F(CredentialVerifierTest, AuthFactorChangeLabel) { |
| EXPECT_EQ(pw_verifier_.auth_factor_label(), "password"); |
| pw_verifier_.ChangeLabel("pass"); |
| EXPECT_EQ(pw_verifier_.auth_factor_label(), "pass"); |
| } |
| |
| } // namespace |
| } // namespace cryptohome |