1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
|
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/policy/core/common/async_policy_provider.h"
#include <memory>
#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/ref_counted.h"
#include "base/run_loop.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/single_thread_task_runner.h"
#include "base/test/task_environment.h"
#include "base/values.h"
#include "components/policy/core/common/async_policy_loader.h"
#include "components/policy/core/common/external_data_fetcher.h"
#include "components/policy/core/common/mock_configuration_policy_provider.h"
#include "components/policy/core/common/policy_types.h"
#include "components/policy/core/common/schema_registry.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using testing::Mock;
using testing::Return;
using testing::Sequence;
namespace policy {
namespace {
// Helper to write a policy in |bundle| with less code.
void SetPolicy(PolicyBundle* bundle,
const std::string& name,
const std::string& value) {
bundle->Get(PolicyNamespace(POLICY_DOMAIN_CHROME, std::string()))
.Set(name, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
POLICY_SOURCE_PLATFORM, base::Value(value), nullptr);
}
class MockPolicyLoader : public AsyncPolicyLoader {
public:
explicit MockPolicyLoader(
scoped_refptr<base::SequencedTaskRunner> task_runner);
MockPolicyLoader(const MockPolicyLoader&) = delete;
MockPolicyLoader& operator=(const MockPolicyLoader&) = delete;
~MockPolicyLoader() override;
// Load() returns a PolicyBundle but it can't be mocked
// because PolicyBundle is moveable but not copyable. This override
// forwards the call to MockLoad() which returns a PolicyBundle*, and returns
// a copy wrapped.
PolicyBundle Load() override;
MOCK_METHOD0(MockLoad, const PolicyBundle*());
MOCK_METHOD0(InitOnBackgroundThread, void());
MOCK_METHOD0(LastModificationTime, base::Time());
};
MockPolicyLoader::MockPolicyLoader(
scoped_refptr<base::SequencedTaskRunner> task_runner)
: AsyncPolicyLoader(task_runner, /*periodic_updates=*/true) {}
MockPolicyLoader::~MockPolicyLoader() = default;
PolicyBundle MockPolicyLoader::Load() {
return MockLoad()->Clone();
}
} // namespace
class AsyncPolicyProviderTest : public testing::Test {
public:
AsyncPolicyProviderTest(const AsyncPolicyProviderTest&) = delete;
AsyncPolicyProviderTest& operator=(const AsyncPolicyProviderTest&) = delete;
protected:
AsyncPolicyProviderTest();
~AsyncPolicyProviderTest() override;
void SetUp() override;
void TearDown() override;
base::test::SingleThreadTaskEnvironment task_environment_;
SchemaRegistry schema_registry_;
PolicyBundle initial_bundle_;
raw_ptr<MockPolicyLoader, AcrossTasksDanglingUntriaged> loader_;
std::unique_ptr<AsyncPolicyProvider> provider_;
};
AsyncPolicyProviderTest::AsyncPolicyProviderTest() = default;
AsyncPolicyProviderTest::~AsyncPolicyProviderTest() = default;
void AsyncPolicyProviderTest::SetUp() {
SetPolicy(&initial_bundle_, "policy", "initial");
loader_ =
new MockPolicyLoader(base::SingleThreadTaskRunner::GetCurrentDefault());
EXPECT_CALL(*loader_, LastModificationTime())
.WillRepeatedly(Return(base::Time()));
EXPECT_CALL(*loader_, InitOnBackgroundThread()).Times(1);
EXPECT_CALL(*loader_, MockLoad()).WillOnce(Return(&initial_bundle_));
provider_ = std::make_unique<AsyncPolicyProvider>(
&schema_registry_, std::unique_ptr<AsyncPolicyLoader>(loader_));
provider_->Init(&schema_registry_);
// Verify that the initial load is done synchronously:
EXPECT_TRUE(provider_->policies().Equals(initial_bundle_));
base::RunLoop().RunUntilIdle();
Mock::VerifyAndClearExpectations(loader_);
EXPECT_CALL(*loader_, LastModificationTime())
.WillRepeatedly(Return(base::Time()));
}
void AsyncPolicyProviderTest::TearDown() {
if (provider_) {
provider_->Shutdown();
provider_.reset();
}
base::RunLoop().RunUntilIdle();
}
TEST_F(AsyncPolicyProviderTest, RefreshPolicies) {
PolicyBundle refreshed_bundle;
SetPolicy(&refreshed_bundle, "policy", "refreshed");
EXPECT_CALL(*loader_, MockLoad()).WillOnce(Return(&refreshed_bundle));
MockConfigurationPolicyObserver observer;
provider_->AddObserver(&observer);
EXPECT_CALL(observer, OnUpdatePolicy(provider_.get())).Times(1);
provider_->RefreshPolicies(PolicyFetchReason::kTest);
base::RunLoop().RunUntilIdle();
// The refreshed policies are now provided.
EXPECT_TRUE(provider_->policies().Equals(refreshed_bundle));
provider_->RemoveObserver(&observer);
}
TEST_F(AsyncPolicyProviderTest, RefreshPoliciesTwice) {
PolicyBundle refreshed_bundle;
SetPolicy(&refreshed_bundle, "policy", "refreshed");
EXPECT_CALL(*loader_, MockLoad()).WillRepeatedly(Return(&refreshed_bundle));
MockConfigurationPolicyObserver observer;
provider_->AddObserver(&observer);
EXPECT_CALL(observer, OnUpdatePolicy(provider_.get())).Times(0);
provider_->RefreshPolicies(PolicyFetchReason::kTest);
// Doesn't refresh before going through the background thread.
Mock::VerifyAndClearExpectations(&observer);
// Doesn't refresh if another RefreshPolicies request is made.
EXPECT_CALL(observer, OnUpdatePolicy(provider_.get())).Times(0);
provider_->RefreshPolicies(PolicyFetchReason::kTest);
Mock::VerifyAndClearExpectations(&observer);
EXPECT_CALL(observer, OnUpdatePolicy(provider_.get())).Times(1);
base::RunLoop().RunUntilIdle();
// The refreshed policies are now provided.
EXPECT_TRUE(provider_->policies().Equals(refreshed_bundle));
Mock::VerifyAndClearExpectations(&observer);
provider_->RemoveObserver(&observer);
}
TEST_F(AsyncPolicyProviderTest, RefreshPoliciesDuringReload) {
PolicyBundle reloaded_bundle;
SetPolicy(&reloaded_bundle, "policy", "reloaded");
PolicyBundle refreshed_bundle;
SetPolicy(&refreshed_bundle, "policy", "refreshed");
Sequence load_sequence;
// Reload.
EXPECT_CALL(*loader_, MockLoad()).InSequence(load_sequence)
.WillOnce(Return(&reloaded_bundle));
// RefreshPolicies.
EXPECT_CALL(*loader_, MockLoad()).InSequence(load_sequence)
.WillOnce(Return(&refreshed_bundle));
MockConfigurationPolicyObserver observer;
provider_->AddObserver(&observer);
EXPECT_CALL(observer, OnUpdatePolicy(provider_.get())).Times(0);
// A Reload is triggered before RefreshPolicies, and it shouldn't trigger
// notifications.
loader_->Reload(true);
Mock::VerifyAndClearExpectations(&observer);
// Doesn't refresh before going through the background thread.
EXPECT_CALL(observer, OnUpdatePolicy(provider_.get())).Times(0);
provider_->RefreshPolicies(PolicyFetchReason::kTest);
Mock::VerifyAndClearExpectations(&observer);
EXPECT_CALL(observer, OnUpdatePolicy(provider_.get())).Times(1);
base::RunLoop().RunUntilIdle();
// The refreshed policies are now provided, and the |reloaded_bundle| was
// dropped.
EXPECT_TRUE(provider_->policies().Equals(refreshed_bundle));
Mock::VerifyAndClearExpectations(&observer);
provider_->RemoveObserver(&observer);
}
TEST_F(AsyncPolicyProviderTest, Shutdown) {
EXPECT_CALL(*loader_, MockLoad()).WillRepeatedly(Return(&initial_bundle_));
MockConfigurationPolicyObserver observer;
provider_->AddObserver(&observer);
// Though there is a pending Reload, the provider and the loader can be
// deleted at any time.
EXPECT_CALL(observer, OnUpdatePolicy(provider_.get())).Times(0);
loader_->Reload(true);
Mock::VerifyAndClearExpectations(&observer);
EXPECT_CALL(observer, OnUpdatePolicy(provider_.get())).Times(0);
provider_->Shutdown();
base::RunLoop().RunUntilIdle();
Mock::VerifyAndClearExpectations(&observer);
provider_->RemoveObserver(&observer);
provider_.reset();
}
} // namespace policy
|