Commit 179a6c96 authored by Hidehiko Abe's avatar Hidehiko Abe Committed by Commit Bot

Replace MockCryptohomeClient by fake in UserCloudPolicyStoreChromeOSTest.

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

BUG=739622
TEST=Ran trybot.

Change-Id: I51397108a365d8b79b8de8a48263d7b62ef0182b
Reviewed-on: https://chromium-review.googlesource.com/563153Reviewed-by: default avatarRyo Hashimoto <hashimoto@chromium.org>
Reviewed-by: default avatarBartosz Fabianowski <bartfab@chromium.org>
Commit-Queue: Hidehiko Abe <hidehiko@chromium.org>
Cr-Commit-Position: refs/heads/master@{#485545}
parent 45f7994f
...@@ -12,7 +12,6 @@ ...@@ -12,7 +12,6 @@
#include "base/bind.h" #include "base/bind.h"
#include "base/files/file_util.h" #include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h" #include "base/files/scoped_temp_dir.h"
#include "base/location.h"
#include "base/macros.h" #include "base/macros.h"
#include "base/memory/ptr_util.h" #include "base/memory/ptr_util.h"
#include "base/run_loop.h" #include "base/run_loop.h"
...@@ -20,7 +19,7 @@ ...@@ -20,7 +19,7 @@
#include "base/test/scoped_task_environment.h" #include "base/test/scoped_task_environment.h"
#include "base/threading/sequenced_worker_pool.h" #include "base/threading/sequenced_worker_pool.h"
#include "base/threading/thread_task_runner_handle.h" #include "base/threading/thread_task_runner_handle.h"
#include "chromeos/dbus/mock_cryptohome_client.h" #include "chromeos/dbus/fake_cryptohome_client.h"
#include "chromeos/dbus/mock_session_manager_client.h" #include "chromeos/dbus/mock_session_manager_client.h"
#include "components/policy/core/common/cloud/cloud_policy_constants.h" #include "components/policy/core/common/cloud/cloud_policy_constants.h"
#include "components/policy/core/common/cloud/mock_cloud_policy_store.h" #include "components/policy/core/common/cloud/mock_cloud_policy_store.h"
...@@ -38,7 +37,6 @@ using RetrievePolicyResponseType = ...@@ -38,7 +37,6 @@ using RetrievePolicyResponseType =
chromeos::SessionManagerClient::RetrievePolicyResponseType; chromeos::SessionManagerClient::RetrievePolicyResponseType;
using testing::AllOf; using testing::AllOf;
using testing::AnyNumber;
using testing::Eq; using testing::Eq;
using testing::Mock; using testing::Mock;
using testing::Property; using testing::Property;
...@@ -51,15 +49,8 @@ namespace policy { ...@@ -51,15 +49,8 @@ namespace policy {
namespace { namespace {
const char kSanitizedUsername[] =
"0123456789ABCDEF0123456789ABCDEF012345678@example.com";
const char kDefaultHomepage[] = "http://chromium.org"; const char kDefaultHomepage[] = "http://chromium.org";
ACTION_P2(SendSanitizedUsername, call_status, sanitized_username) {
base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE, base::BindOnce(arg1, call_status, sanitized_username));
}
class UserCloudPolicyStoreChromeOSTest : public testing::Test { class UserCloudPolicyStoreChromeOSTest : public testing::Test {
protected: protected:
UserCloudPolicyStoreChromeOSTest() UserCloudPolicyStoreChromeOSTest()
...@@ -67,11 +58,6 @@ class UserCloudPolicyStoreChromeOSTest : public testing::Test { ...@@ -67,11 +58,6 @@ class UserCloudPolicyStoreChromeOSTest : public testing::Test {
base::test::ScopedTaskEnvironment::MainThreadType::UI) {} base::test::ScopedTaskEnvironment::MainThreadType::UI) {}
void SetUp() override { void SetUp() override {
EXPECT_CALL(cryptohome_client_, GetSanitizedUsername(cryptohome_id_, _))
.Times(AnyNumber())
.WillRepeatedly(SendSanitizedUsername(
chromeos::DBUS_METHOD_CALL_SUCCESS, kSanitizedUsername));
ASSERT_TRUE(tmp_dir_.CreateUniqueTempDir()); ASSERT_TRUE(tmp_dir_.CreateUniqueTempDir());
store_.reset(new UserCloudPolicyStoreChromeOS( store_.reset(new UserCloudPolicyStoreChromeOS(
&cryptohome_client_, &session_manager_client_, &cryptohome_client_, &session_manager_client_,
...@@ -83,7 +69,7 @@ class UserCloudPolicyStoreChromeOSTest : public testing::Test { ...@@ -83,7 +69,7 @@ class UserCloudPolicyStoreChromeOSTest : public testing::Test {
// the stored/loaded policy blob succeeds. // the stored/loaded policy blob succeeds.
std::string public_key = policy_.GetPublicSigningKeyAsString(); std::string public_key = policy_.GetPublicSigningKeyAsString();
ASSERT_FALSE(public_key.empty()); ASSERT_FALSE(public_key.empty());
StoreUserPolicyKey(public_key); ASSERT_NO_FATAL_FAILURE(StoreUserPolicyKey(public_key));
policy_.payload().mutable_homepagelocation()->set_value(kDefaultHomepage); policy_.payload().mutable_homepagelocation()->set_value(kDefaultHomepage);
policy_.Build(); policy_.Build();
...@@ -211,12 +197,15 @@ class UserCloudPolicyStoreChromeOSTest : public testing::Test { ...@@ -211,12 +197,15 @@ class UserCloudPolicyStoreChromeOSTest : public testing::Test {
} }
base::FilePath user_policy_key_file() { base::FilePath user_policy_key_file() {
return user_policy_dir().AppendASCII(kSanitizedUsername) const std::string sanitized_username =
.AppendASCII("policy.pub"); chromeos::CryptohomeClient::GetStubSanitizedUsername(cryptohome_id_);
return user_policy_dir()
.AppendASCII(sanitized_username)
.AppendASCII("policy.pub");
} }
base::test::ScopedTaskEnvironment scoped_task_environment_; base::test::ScopedTaskEnvironment scoped_task_environment_;
chromeos::MockCryptohomeClient cryptohome_client_; chromeos::FakeCryptohomeClient cryptohome_client_;
chromeos::MockSessionManagerClient session_manager_client_; chromeos::MockSessionManagerClient session_manager_client_;
UserPolicyBuilder policy_; UserPolicyBuilder policy_;
MockCloudPolicyStoreObserver observer_; MockCloudPolicyStoreObserver observer_;
...@@ -369,11 +358,7 @@ TEST_F(UserCloudPolicyStoreChromeOSTest, StoreValidationError) { ...@@ -369,11 +358,7 @@ TEST_F(UserCloudPolicyStoreChromeOSTest, StoreValidationError) {
TEST_F(UserCloudPolicyStoreChromeOSTest, StoreWithoutPolicyKey) { TEST_F(UserCloudPolicyStoreChromeOSTest, StoreWithoutPolicyKey) {
// Make the dbus call to cryptohome fail. // Make the dbus call to cryptohome fail.
Mock::VerifyAndClearExpectations(&cryptohome_client_); cryptohome_client_.SetServiceIsAvailable(false);
EXPECT_CALL(cryptohome_client_, GetSanitizedUsername(cryptohome_id_, _))
.Times(AnyNumber())
.WillRepeatedly(SendSanitizedUsername(chromeos::DBUS_METHOD_CALL_FAILURE,
std::string()));
// Store policy. // Store policy.
chromeos::SessionManagerClient::StorePolicyCallback store_callback; chromeos::SessionManagerClient::StorePolicyCallback store_callback;
...@@ -503,8 +488,6 @@ TEST_F(UserCloudPolicyStoreChromeOSTest, LoadImmediately) { ...@@ -503,8 +488,6 @@ TEST_F(UserCloudPolicyStoreChromeOSTest, LoadImmediately) {
BlockingRetrievePolicyForUser(cryptohome_id_, _)) BlockingRetrievePolicyForUser(cryptohome_id_, _))
.WillOnce(DoAll(SetArgPointee<1>(policy_.GetBlob()), .WillOnce(DoAll(SetArgPointee<1>(policy_.GetBlob()),
Return(RetrievePolicyResponseType::SUCCESS))); Return(RetrievePolicyResponseType::SUCCESS)));
EXPECT_CALL(cryptohome_client_, BlockingGetSanitizedUsername(cryptohome_id_))
.WillOnce(Return(kSanitizedUsername));
EXPECT_FALSE(store_->policy()); EXPECT_FALSE(store_->policy());
store_->LoadImmediately(); store_->LoadImmediately();
...@@ -513,7 +496,6 @@ TEST_F(UserCloudPolicyStoreChromeOSTest, LoadImmediately) { ...@@ -513,7 +496,6 @@ TEST_F(UserCloudPolicyStoreChromeOSTest, LoadImmediately) {
// must be done within the test. // must be done within the test.
Mock::VerifyAndClearExpectations(&observer_); Mock::VerifyAndClearExpectations(&observer_);
Mock::VerifyAndClearExpectations(&session_manager_client_); Mock::VerifyAndClearExpectations(&session_manager_client_);
Mock::VerifyAndClearExpectations(&cryptohome_client_);
// The policy should become available without having to spin any loops. // The policy should become available without having to spin any loops.
ASSERT_TRUE(store_->policy()); ASSERT_TRUE(store_->policy());
...@@ -567,14 +549,14 @@ TEST_F(UserCloudPolicyStoreChromeOSTest, LoadImmediatelyDBusFailure) { ...@@ -567,14 +549,14 @@ TEST_F(UserCloudPolicyStoreChromeOSTest, LoadImmediatelyDBusFailure) {
BlockingRetrievePolicyForUser(cryptohome_id_, _)) BlockingRetrievePolicyForUser(cryptohome_id_, _))
.WillOnce(DoAll(SetArgPointee<1>(policy_.GetBlob()), .WillOnce(DoAll(SetArgPointee<1>(policy_.GetBlob()),
Return(RetrievePolicyResponseType::SUCCESS))); Return(RetrievePolicyResponseType::SUCCESS)));
EXPECT_CALL(cryptohome_client_, BlockingGetSanitizedUsername(cryptohome_id_))
.WillOnce(Return("")); // Make the dbus call to cryptohome fail.
cryptohome_client_.SetServiceIsAvailable(false);
EXPECT_FALSE(store_->policy()); EXPECT_FALSE(store_->policy());
store_->LoadImmediately(); store_->LoadImmediately();
Mock::VerifyAndClearExpectations(&observer_); Mock::VerifyAndClearExpectations(&observer_);
Mock::VerifyAndClearExpectations(&session_manager_client_); Mock::VerifyAndClearExpectations(&session_manager_client_);
Mock::VerifyAndClearExpectations(&cryptohome_client_);
EXPECT_FALSE(store_->policy()); EXPECT_FALSE(store_->policy());
EXPECT_TRUE(store_->policy_map().empty()); EXPECT_TRUE(store_->policy_map().empty());
...@@ -588,14 +570,13 @@ TEST_F(UserCloudPolicyStoreChromeOSTest, LoadImmediatelyNoUserPolicyKey) { ...@@ -588,14 +570,13 @@ TEST_F(UserCloudPolicyStoreChromeOSTest, LoadImmediatelyNoUserPolicyKey) {
BlockingRetrievePolicyForUser(cryptohome_id_, _)) BlockingRetrievePolicyForUser(cryptohome_id_, _))
.WillOnce(DoAll(SetArgPointee<1>(policy_.GetBlob()), .WillOnce(DoAll(SetArgPointee<1>(policy_.GetBlob()),
Return(RetrievePolicyResponseType::SUCCESS))); Return(RetrievePolicyResponseType::SUCCESS)));
EXPECT_CALL(cryptohome_client_, BlockingGetSanitizedUsername(cryptohome_id_)) // Ensure no policy data.
.WillOnce(Return("wrong@example.com")); ASSERT_TRUE(base::DeleteFile(user_policy_key_file(), false));
EXPECT_FALSE(store_->policy()); EXPECT_FALSE(store_->policy());
store_->LoadImmediately(); store_->LoadImmediately();
Mock::VerifyAndClearExpectations(&observer_); Mock::VerifyAndClearExpectations(&observer_);
Mock::VerifyAndClearExpectations(&session_manager_client_); Mock::VerifyAndClearExpectations(&session_manager_client_);
Mock::VerifyAndClearExpectations(&cryptohome_client_);
EXPECT_FALSE(store_->policy()); EXPECT_FALSE(store_->policy());
EXPECT_TRUE(store_->policy_map().empty()); EXPECT_TRUE(store_->policy_map().empty());
......
...@@ -149,15 +149,18 @@ void FakeCryptohomeClient::GetSanitizedUsername( ...@@ -149,15 +149,18 @@ void FakeCryptohomeClient::GetSanitizedUsername(
const StringDBusMethodCallback& callback) { const StringDBusMethodCallback& callback) {
// Even for stub implementation we have to return different values so that // Even for stub implementation we have to return different values so that
// multi-profiles would work. // multi-profiles would work.
std::string sanitized_username = GetStubSanitizedUsername(cryptohome_id); auto task =
base::ThreadTaskRunnerHandle::Get()->PostTask( service_is_available_
FROM_HERE, ? base::BindOnce(callback, DBUS_METHOD_CALL_SUCCESS,
base::Bind(callback, DBUS_METHOD_CALL_SUCCESS, sanitized_username)); GetStubSanitizedUsername(cryptohome_id))
: base::BindOnce(callback, DBUS_METHOD_CALL_FAILURE, std::string());
base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, std::move(task));
} }
std::string FakeCryptohomeClient::BlockingGetSanitizedUsername( std::string FakeCryptohomeClient::BlockingGetSanitizedUsername(
const cryptohome::Identification& cryptohome_id) { const cryptohome::Identification& cryptohome_id) {
return GetStubSanitizedUsername(cryptohome_id); return service_is_available_ ? GetStubSanitizedUsername(cryptohome_id)
: std::string();
} }
void FakeCryptohomeClient::AsyncMount( void FakeCryptohomeClient::AsyncMount(
......
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