Commit 10ee02c0 authored by Leo Lai's avatar Leo Lai Committed by Chromium LUCI CQ

SystemTokenCertDBInitializer test cases of disabled TPM.

With the configuration of software fallback, it is getting harder to
implement unit test to test the case of TPM being disabled. Thus, this
CL convert the software fallback to be inject-able during unittest.

As for naming, since "enabled" doesn't explicitly express when we use
it, so the flag is changed to "allowed" so it is more clear about its
intention.

BUG=b:172748724
TEST=unit_tests.

Change-Id: I1aff3a36d1bc4524438a275b92d09ec859e86ba8
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2581706
Commit-Queue: Leo Lai <cylai@google.com>
Reviewed-by: default avatarAchuith Bhandarkar <achuith@chromium.org>
Reviewed-by: default avatarMaksim Ivanov <emaxx@chromium.org>
Cr-Commit-Position: refs/heads/master@{#836487}
parent 67ff150e
......@@ -38,6 +38,12 @@ constexpr base::TimeDelta kInitialRequestDelay =
base::TimeDelta::FromMilliseconds(100);
constexpr base::TimeDelta kMaxRequestDelay = base::TimeDelta::FromMinutes(5);
#if BUILDFLAG(SYSTEM_SLOT_SOFTWARE_FALLBACK)
constexpr bool kIsSystemSlotSoftwareFallbackAllowed = true;
#else
constexpr bool kIsSystemSlotSoftwareFallbackAllowed = false;
#endif
// Called on UI Thread when the system slot has been retrieved.
void GotSystemSlotOnUIThread(
base::OnceCallback<void(crypto::ScopedPK11Slot)> callback_ui_thread,
......@@ -83,15 +89,6 @@ bool ShallAttemptTpmOwnership() {
#endif
}
// Checks if the build flag system_slot_software_fallback is enabled.
bool IsSystemSlotSoftwareFallbackEnabled() {
#if BUILDFLAG(SYSTEM_SLOT_SOFTWARE_FALLBACK)
return true;
#else
return false;
#endif
}
// Calculates the delay before running next attempt to get the TPM state
// (enabled/disabled), if |last_delay| was the last or initial delay.
base::TimeDelta GetNextRequestDelay(base::TimeDelta last_delay) {
......@@ -110,7 +107,9 @@ constexpr base::TimeDelta
SystemTokenCertDBInitializer::kMaxCertDbRetrievalDelay;
SystemTokenCertDBInitializer::SystemTokenCertDBInitializer()
: tpm_request_delay_(kInitialRequestDelay) {
: tpm_request_delay_(kInitialRequestDelay),
is_system_slot_software_fallback_allowed_(
kIsSystemSlotSoftwareFallbackAllowed) {
// Only start loading the system token once cryptohome is available and only
// if the TPM is ready (available && owned && not being owned).
CryptohomeClient::Get()->WaitForServiceToBeAvailable(
......@@ -237,7 +236,7 @@ void SystemTokenCertDBInitializer::OnGetTpmNonsensitiveStatus(
// allowed. Note that we don't fall back to software solution as long as TPM
// is enabled.
if (reply.is_owned() ||
(!reply.is_enabled() && IsSystemSlotSoftwareFallbackEnabled())) {
(!reply.is_enabled() && is_system_slot_software_fallback_allowed_)) {
VLOG_IF(1, !reply.is_owned())
<< "Initializing database when TPM is not owned.";
MaybeStartInitializingDatabase();
......
......@@ -83,6 +83,12 @@ class SystemTokenCertDBInitializer : public TpmManagerClient::Observer {
// Removes |observer| as SystemTokenCertDBObserver.
void RemoveObserver(SystemTokenCertDBObserver* observer);
// Sets if the software fallback for system slot is allowed; useful for
// testing.
void set_is_system_slot_software_fallback_allowed(bool is_allowed) {
is_system_slot_software_fallback_allowed_ = is_allowed;
}
private:
// Called once the cryptohome service is available.
void OnCryptohomeAvailable(bool available);
......@@ -140,6 +146,9 @@ class SystemTokenCertDBInitializer : public TpmManagerClient::Observer {
// state. Will be adapted after each attempt.
base::TimeDelta tpm_request_delay_;
// The flag that determines if the system slot can use software fallback.
bool is_system_slot_software_fallback_allowed_;
SEQUENCE_CHECKER(sequence_checker_);
base::WeakPtrFactory<SystemTokenCertDBInitializer> weak_ptr_factory_{this};
......
......@@ -137,6 +137,85 @@ TEST_F(SystemTokenCertDbInitializerTest, GetSystemTokenCertDbSuccess) {
get_system_token_cert_db_callback_wrapper.IsDbRetrievalSucceeded());
}
// Tests that the system token certificate database will be returned
// successfully by SystemTokenCertDbInitializer if it was available in less than
// 5 minutes after being requested, and the system slot uses software fallback
// when it's allowed and TPM is disabled.
TEST_F(SystemTokenCertDbInitializerTest,
GetSystemTokenCertDbSuccessSoftwareFallback) {
TpmManagerClient::Get()
->GetTestInterface()
->mutable_nonsensitive_status_reply()
->set_is_enabled(false);
TpmManagerClient::Get()
->GetTestInterface()
->mutable_nonsensitive_status_reply()
->set_is_owned(false);
system_token_cert_db_initializer()
->set_is_system_slot_software_fallback_allowed(true);
GetSystemTokenCertDbCallbackWrapper get_system_token_cert_db_callback_wrapper;
system_token_cert_db_initializer()->GetSystemTokenCertDb(
get_system_token_cert_db_callback_wrapper.GetCallback());
EXPECT_FALSE(get_system_token_cert_db_callback_wrapper.IsCallbackCalled());
// Check that after 1 minute, SystemTokenCertDBInitializer is still waiting
// for the system token slot to be initialized and the DB retrieval hasn't
// timed out yet.
const auto kOneMinuteDelay = base::TimeDelta::FromMinutes(1);
EXPECT_LT(kOneMinuteDelay,
SystemTokenCertDBInitializer::kMaxCertDbRetrievalDelay);
task_environment()->FastForwardBy(kOneMinuteDelay);
EXPECT_FALSE(get_system_token_cert_db_callback_wrapper.IsCallbackCalled());
EXPECT_TRUE(InitializeTestSystemSlot());
get_system_token_cert_db_callback_wrapper.Wait();
EXPECT_TRUE(get_system_token_cert_db_callback_wrapper.IsCallbackCalled());
EXPECT_TRUE(
get_system_token_cert_db_callback_wrapper.IsDbRetrievalSucceeded());
}
// Tests that the system token certificate database will be not returned
// successfully by SystemTokenCertDbInitializer if TPM is disabled and system
// slot software fallback is not allowed.
TEST_F(SystemTokenCertDbInitializerTest,
GetSystemTokenCertDbFailureDisabledTPM) {
TpmManagerClient::Get()
->GetTestInterface()
->mutable_nonsensitive_status_reply()
->set_is_enabled(false);
TpmManagerClient::Get()
->GetTestInterface()
->mutable_nonsensitive_status_reply()
->set_is_owned(false);
system_token_cert_db_initializer()
->set_is_system_slot_software_fallback_allowed(false);
GetSystemTokenCertDbCallbackWrapper get_system_token_cert_db_callback_wrapper;
system_token_cert_db_initializer()->GetSystemTokenCertDb(
get_system_token_cert_db_callback_wrapper.GetCallback());
EXPECT_FALSE(get_system_token_cert_db_callback_wrapper.IsCallbackCalled());
// Check that after 1 minute, SystemTokenCertDBInitializer is still waiting
// for the system token slot to be initialized and the DB retrieval hasn't
// timed out yet.
const auto kOneMinuteDelay = base::TimeDelta::FromMinutes(1);
EXPECT_LT(kOneMinuteDelay,
SystemTokenCertDBInitializer::kMaxCertDbRetrievalDelay);
task_environment()->FastForwardBy(kOneMinuteDelay);
EXPECT_FALSE(get_system_token_cert_db_callback_wrapper.IsCallbackCalled());
EXPECT_TRUE(InitializeTestSystemSlot());
get_system_token_cert_db_callback_wrapper.Wait();
EXPECT_TRUE(get_system_token_cert_db_callback_wrapper.IsCallbackCalled());
EXPECT_FALSE(
get_system_token_cert_db_callback_wrapper.IsDbRetrievalSucceeded());
}
// Tests that the system token certificate database will be returned
// successfully by SystemTokenCertDbInitializer if it was available in less than
// 5 minutes after being requested even if the slot was available after more
......
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