Commit b6985578 authored by Hidehiko Abe's avatar Hidehiko Abe Committed by Commit Bot

Replace MockCryptohomeClient by fake in Enterprise API tests.

This is preparation to remove MockCryptohomeClinet,
as preparation to replace Callback by OnceCallback in chromeos/dbus.

BUG=739622
TEST=Ran trybot.

Change-Id: I2c326050e48dae5671a796f152c7e723ccba601d
Reviewed-on: https://chromium-review.googlesource.com/570119
Commit-Queue: Hidehiko Abe <hidehiko@chromium.org>
Reviewed-by: default avatarMaksim Ivanov <emaxx@chromium.org>
Reviewed-by: default avatarRyo Hashimoto <hashimoto@chromium.org>
Cr-Commit-Position: refs/heads/master@{#487956}
parent 562f080b
......@@ -27,7 +27,7 @@
#include "chromeos/cryptohome/async_method_caller.h"
#include "chromeos/cryptohome/mock_async_method_caller.h"
#include "chromeos/dbus/dbus_method_call_status.h"
#include "chromeos/dbus/mock_cryptohome_client.h"
#include "chromeos/dbus/fake_cryptohome_client.h"
#include "components/policy/core/common/cloud/cloud_policy_constants.h"
#include "components/prefs/pref_service.h"
#include "components/signin/core/account_id/account_id.h"
......@@ -56,22 +56,6 @@ const int kResetRequired = 4;
const char kUserEmail[] = "test@google.com";
// A simple functor to invoke a callback with predefined arguments.
class FakeBoolDBusMethod {
public:
FakeBoolDBusMethod(chromeos::DBusMethodCallStatus status, bool value)
: status_(status), value_(value) {}
void operator()(const chromeos::BoolDBusMethodCallback& callback) {
base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE, base::BindOnce(callback, status_, value_));
}
private:
chromeos::DBusMethodCallStatus status_;
bool value_;
};
void RegisterKeyCallbackTrue(
chromeos::attestation::AttestationKeyType key_type,
const cryptohome::Identification& user_id,
......@@ -147,12 +131,6 @@ class EPKChallengeKeyTestBase : public BrowserWithTestWindowTest {
fake_user_manager_(new chromeos::FakeChromeUserManager),
user_manager_enabler_(fake_user_manager_) {
// Set up the default behavior of mocks.
ON_CALL(mock_cryptohome_client_, TpmAttestationDoesKeyExist(_, _, _, _))
.WillByDefault(WithArgs<3>(Invoke(
FakeBoolDBusMethod(chromeos::DBUS_METHOD_CALL_SUCCESS, false))));
ON_CALL(mock_cryptohome_client_, TpmAttestationIsPrepared(_))
.WillByDefault(Invoke(
FakeBoolDBusMethod(chromeos::DBUS_METHOD_CALL_SUCCESS, true)));
ON_CALL(mock_async_method_caller_, TpmAttestationRegisterKey(_, _, _, _))
.WillByDefault(Invoke(RegisterKeyCallbackTrue));
ON_CALL(mock_async_method_caller_,
......@@ -231,7 +209,7 @@ class EPKChallengeKeyTestBase : public BrowserWithTestWindowTest {
return NULL;
}
NiceMock<chromeos::MockCryptohomeClient> mock_cryptohome_client_;
chromeos::FakeCryptohomeClient cryptohome_client_;
NiceMock<cryptohome::MockAsyncMethodCaller> mock_async_method_caller_;
NiceMock<chromeos::attestation::MockAttestationFlow> mock_attestation_flow_;
chromeos::ScopedCrosSettingsTestHelper settings_helper_;
......@@ -247,7 +225,7 @@ class EPKChallengeKeyTestBase : public BrowserWithTestWindowTest {
class EPKChallengeMachineKeyTest : public EPKChallengeKeyTestBase {
protected:
EPKChallengeMachineKeyTest()
: impl_(&mock_cryptohome_client_,
: impl_(&cryptohome_client_,
&mock_async_method_caller_,
&mock_attestation_flow_,
&stub_install_attributes_),
......@@ -311,9 +289,7 @@ TEST_F(EPKChallengeMachineKeyTest, DevicePolicyDisabled) {
}
TEST_F(EPKChallengeMachineKeyTest, DoesKeyExistDbusFailed) {
EXPECT_CALL(mock_cryptohome_client_, TpmAttestationDoesKeyExist(_, _, _, _))
.WillRepeatedly(WithArgs<3>(Invoke(
FakeBoolDBusMethod(chromeos::DBUS_METHOD_CALL_FAILURE, false))));
cryptohome_client_.set_tpm_attestation_does_key_exist_should_succeed(false);
EXPECT_EQ(GetCertificateError(kDBusError),
RunFunctionAndReturnError(func_.get(), CreateArgs(), browser()));
......@@ -346,9 +322,9 @@ TEST_F(EPKChallengeMachineKeyTest, KeyRegistrationFailed) {
}
TEST_F(EPKChallengeMachineKeyTest, KeyExists) {
EXPECT_CALL(mock_cryptohome_client_, TpmAttestationDoesKeyExist(_, _, _, _))
.WillRepeatedly(WithArgs<3>(Invoke(
FakeBoolDBusMethod(chromeos::DBUS_METHOD_CALL_SUCCESS, true))));
cryptohome_client_.SetTpmAttestationDeviceCertificate("attest-ent-machine",
std::string());
// GetCertificate must not be called if the key exists.
EXPECT_CALL(mock_attestation_flow_, GetCertificate(_, _, _, _, _)).Times(0);
......@@ -425,18 +401,14 @@ TEST_F(EPKChallengeMachineKeyTest, KeyRegisteredSuccess) {
}
TEST_F(EPKChallengeMachineKeyTest, AttestationNotPrepared) {
EXPECT_CALL(mock_cryptohome_client_, TpmAttestationIsPrepared(_))
.WillRepeatedly(Invoke(
FakeBoolDBusMethod(chromeos::DBUS_METHOD_CALL_SUCCESS, false)));
cryptohome_client_.set_tpm_attestation_is_prepared(false);
EXPECT_EQ(GetCertificateError(kResetRequired),
RunFunctionAndReturnError(func_.get(), CreateArgs(), browser()));
}
TEST_F(EPKChallengeMachineKeyTest, AttestationPreparedDbusFailed) {
EXPECT_CALL(mock_cryptohome_client_, TpmAttestationIsPrepared(_))
.WillRepeatedly(
Invoke(FakeBoolDBusMethod(chromeos::DBUS_METHOD_CALL_FAILURE, true)));
cryptohome_client_.SetServiceIsAvailable(false);
EXPECT_EQ(GetCertificateError(kDBusError),
RunFunctionAndReturnError(func_.get(), CreateArgs(), browser()));
......@@ -445,7 +417,7 @@ TEST_F(EPKChallengeMachineKeyTest, AttestationPreparedDbusFailed) {
class EPKChallengeUserKeyTest : public EPKChallengeKeyTestBase {
protected:
EPKChallengeUserKeyTest()
: impl_(&mock_cryptohome_client_,
: impl_(&cryptohome_client_,
&mock_async_method_caller_,
&mock_attestation_flow_,
&stub_install_attributes_),
......@@ -508,9 +480,7 @@ TEST_F(EPKChallengeUserKeyTest, DevicePolicyDisabled) {
}
TEST_F(EPKChallengeUserKeyTest, DoesKeyExistDbusFailed) {
EXPECT_CALL(mock_cryptohome_client_, TpmAttestationDoesKeyExist(_, _, _, _))
.WillRepeatedly(WithArgs<3>(Invoke(
FakeBoolDBusMethod(chromeos::DBUS_METHOD_CALL_FAILURE, false))));
cryptohome_client_.set_tpm_attestation_does_key_exist_should_succeed(false);
EXPECT_EQ(GetCertificateError(kDBusError),
RunFunctionAndReturnError(func_.get(), CreateArgs(), browser()));
......@@ -542,9 +512,9 @@ TEST_F(EPKChallengeUserKeyTest, KeyRegistrationFailed) {
}
TEST_F(EPKChallengeUserKeyTest, KeyExists) {
EXPECT_CALL(mock_cryptohome_client_, TpmAttestationDoesKeyExist(_, _, _, _))
.WillRepeatedly(WithArgs<3>(Invoke(
FakeBoolDBusMethod(chromeos::DBUS_METHOD_CALL_SUCCESS, true))));
cryptohome_client_.SetTpmAttestationUserCertificate(
cryptohome::Identification(AccountId::FromUserEmail(kUserEmail)),
"attest-ent-user", std::string());
// GetCertificate must not be called if the key exists.
EXPECT_CALL(mock_attestation_flow_, GetCertificate(_, _, _, _, _)).Times(0);
......@@ -599,18 +569,14 @@ TEST_F(EPKChallengeUserKeyTest, Success) {
}
TEST_F(EPKChallengeUserKeyTest, AttestationNotPrepared) {
EXPECT_CALL(mock_cryptohome_client_, TpmAttestationIsPrepared(_))
.WillRepeatedly(Invoke(
FakeBoolDBusMethod(chromeos::DBUS_METHOD_CALL_SUCCESS, false)));
cryptohome_client_.set_tpm_attestation_is_prepared(false);
EXPECT_EQ(GetCertificateError(kResetRequired),
RunFunctionAndReturnError(func_.get(), CreateArgs(), browser()));
}
TEST_F(EPKChallengeUserKeyTest, AttestationPreparedDbusFailed) {
EXPECT_CALL(mock_cryptohome_client_, TpmAttestationIsPrepared(_))
.WillRepeatedly(
Invoke(FakeBoolDBusMethod(chromeos::DBUS_METHOD_CALL_FAILURE, true)));
cryptohome_client_.SetServiceIsAvailable(false);
EXPECT_EQ(GetCertificateError(kDBusError),
RunFunctionAndReturnError(func_.get(), CreateArgs(), browser()));
......
......@@ -427,7 +427,8 @@ void FakeCryptohomeClient::TpmAttestationDoesKeyExist(
const cryptohome::Identification& cryptohome_id,
const std::string& key_name,
const BoolDBusMethodCallback& callback) {
if (!service_is_available_) {
if (!service_is_available_ ||
!tpm_attestation_does_key_exist_should_succeed_) {
base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE, base::BindOnce(callback, DBUS_METHOD_CALL_FAILURE, false));
return;
......
......@@ -246,6 +246,10 @@ class CHROMEOS_EXPORT FakeCryptohomeClient : public CryptohomeClient {
tpm_attestation_is_prepared_ = prepared;
}
void set_tpm_attestation_does_key_exist_should_succeed(bool should_succeed) {
tpm_attestation_does_key_exist_should_succeed_ = should_succeed;
}
void SetTpmAttestationUserCertificate(
const cryptohome::Identification& cryptohome_id,
const std::string& key_name,
......@@ -317,6 +321,7 @@ class CHROMEOS_EXPORT FakeCryptohomeClient : public CryptohomeClient {
bool needs_dircrypto_migration_ = false;
bool tpm_attestation_is_enrolled_ = true;
bool tpm_attestation_is_prepared_ = true;
bool tpm_attestation_does_key_exist_should_succeed_ = true;
base::WeakPtrFactory<FakeCryptohomeClient> weak_ptr_factory_;
......
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