File: policy_value_store_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (280 lines) | stat: -rw-r--r-- 9,901 bytes parent folder | download | duplicates (3)
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/extensions/api/storage/policy_value_store.h"

#include <memory>

#include "base/files/file_path.h"
#include "base/files/scoped_temp_dir.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/json/json_writer.h"
#include "base/memory/ref_counted.h"
#include "components/policy/core/common/external_data_fetcher.h"
#include "components/policy/core/common/policy_map.h"
#include "components/policy/core/common/policy_types.h"
#include "components/value_store/leveldb_value_store.h"
#include "components/value_store/value_store_test_suite.h"
#include "content/public/test/browser_task_environment.h"
#include "content/public/test/test_utils.h"
#include "extensions/browser/api/storage/backend_task_runner.h"
#include "extensions/browser/api/storage/settings_observer.h"
#include "extensions/browser/api/storage/storage_area_namespace.h"
#include "extensions/buildflags/buildflags.h"
#include "extensions/common/extension_id.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

static_assert(BUILDFLAG(ENABLE_EXTENSIONS_CORE));

using testing::_;
using testing::Mock;
using testing::NiceMock;
using value_store::ValueStore;
using value_store::ValueStoreTestSuite;

namespace extensions {

namespace {

const char kTestExtensionId[] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
const char kDatabaseUMAClientName[] = "Test";

std::string ValueToJson(const base::Value& changes) {
  std::string json;
  bool success = base::JSONWriter::Write(changes, &json);
  DCHECK(success);
  return json;
}

std::string ValueStoreChangeToJson(value_store::ValueStoreChangeList changes) {
  return ValueToJson(
      value_store::ValueStoreChange::ToValue(std::move(changes)));
}

class MockSettingsObserver {
 public:
  MOCK_METHOD4(
      OnSettingsChangedJSON,
      void(const ExtensionId& extension_id,
           StorageAreaNamespace storage_area,
           std::optional<api::storage::AccessLevel> session_access_level,
           const std::string& changes_json));

  void OnSettingsChanged(
      const ExtensionId& extension_id,
      StorageAreaNamespace storage_area,
      std::optional<api::storage::AccessLevel> session_access_level,
      base::Value changes) {
    OnSettingsChangedJSON(extension_id, storage_area, session_access_level,
                          ValueToJson(std::move(changes)));
  }
};

// Extends PolicyValueStore by overriding the mutating methods, so that the
// Get() base implementation can be tested with the ValueStoreTestSuite
// parameterized tests.
class MutablePolicyValueStore : public PolicyValueStore {
 public:
  explicit MutablePolicyValueStore(const base::FilePath& path)
      : PolicyValueStore(
            kTestExtensionId,
            SequenceBoundSettingsChangedCallback(base::DoNothing()),
            std::make_unique<value_store::LeveldbValueStore>(
                kDatabaseUMAClientName,
                path)) {}

  MutablePolicyValueStore(const MutablePolicyValueStore&) = delete;
  MutablePolicyValueStore& operator=(const MutablePolicyValueStore&) = delete;

  ~MutablePolicyValueStore() override = default;

  WriteResult Set(WriteOptions options,
                  const std::string& key,
                  const base::Value& value) override {
    return delegate()->Set(options, key, value);
  }

  WriteResult Set(WriteOptions options,
                  const base::Value::Dict& values) override {
    return delegate()->Set(options, values);
  }

  WriteResult Remove(const std::string& key) override {
    return delegate()->Remove(key);
  }

  WriteResult Remove(const std::vector<std::string>& keys) override {
    return delegate()->Remove(keys);
  }

  WriteResult Clear() override { return delegate()->Clear(); }
};

ValueStore* Param(const base::FilePath& file_path) {
  return new MutablePolicyValueStore(file_path);
}

}  // namespace

INSTANTIATE_TEST_SUITE_P(PolicyValueStoreTest,
                         ValueStoreTestSuite,
                         testing::Values(&Param));

class PolicyValueStoreTest : public testing::Test {
 public:
  PolicyValueStoreTest() = default;
  ~PolicyValueStoreTest() override = default;

  void SetUp() override {
    ASSERT_TRUE(scoped_temp_dir_.CreateUniqueTempDir());
    store_ = std::make_unique<PolicyValueStore>(
        kTestExtensionId,
        SequenceBoundSettingsChangedCallback(
            base::BindRepeating(&MockSettingsObserver::OnSettingsChanged,
                                base::Unretained(&observer_))),
        std::make_unique<value_store::LeveldbValueStore>(
            kDatabaseUMAClientName, scoped_temp_dir_.GetPath()));
  }

  void TearDown() override { store_.reset(); }

 protected:
  void SetCurrentPolicy(const policy::PolicyMap& policies) {
    GetBackendTaskRunner()->PostTask(
        FROM_HERE,
        base::BindOnce(&PolicyValueStoreTest::SetCurrentPolicyOnBackendSequence,
                       base::Unretained(this), policies.Clone()));
    content::RunAllTasksUntilIdle();
  }

  void SetCurrentPolicyOnBackendSequence(const policy::PolicyMap& policies) {
    DCHECK(IsOnBackendSequence());
    store_->SetCurrentPolicy(policies);
  }

  base::ScopedTempDir scoped_temp_dir_;
  content::BrowserTaskEnvironment task_environment_;
  std::unique_ptr<PolicyValueStore> store_;
  NiceMock<MockSettingsObserver> observer_;
};

TEST_F(PolicyValueStoreTest, DontProvideRecommendedPolicies) {
  policy::PolicyMap policies;
  base::Value expected(123);
  policies.Set("must", policy::POLICY_LEVEL_MANDATORY,
               policy::POLICY_SCOPE_USER, policy::POLICY_SOURCE_CLOUD,
               expected.Clone(), nullptr);
  policies.Set("may", policy::POLICY_LEVEL_RECOMMENDED,
               policy::POLICY_SCOPE_USER, policy::POLICY_SOURCE_CLOUD,
               base::Value(456), nullptr);
  SetCurrentPolicy(policies);

  ValueStore::ReadResult result = store_->Get();
  ASSERT_TRUE(result.status().ok());
  EXPECT_EQ(1u, result.settings().size());
  base::Value* value = result.settings().Find("may");
  EXPECT_FALSE(value);
  value = result.settings().Find("must");
  ASSERT_TRUE(value);
  EXPECT_EQ(expected, *value);
}

TEST_F(PolicyValueStoreTest, ReadOnly) {
  ValueStore::WriteOptions options = ValueStore::DEFAULTS;

  base::Value string_value("value");
  EXPECT_FALSE(store_->Set(options, "key", string_value).status().ok());

  base::Value::Dict dict;
  dict.Set("key", "value");
  EXPECT_FALSE(store_->Set(options, dict).status().ok());

  EXPECT_FALSE(store_->Remove("key").status().ok());
  std::vector<std::string> keys;
  keys.push_back("key");
  EXPECT_FALSE(store_->Remove(keys).status().ok());
  EXPECT_FALSE(store_->Clear().status().ok());
}

TEST_F(PolicyValueStoreTest, NotifyOnChanges) {
  // Notify when setting the initial policy.
  const base::Value value("111");
  {
    value_store::ValueStoreChangeList changes;
    changes.push_back(
        value_store::ValueStoreChange("aaa", std::nullopt, value.Clone()));
    EXPECT_CALL(observer_,
                OnSettingsChangedJSON(
                    kTestExtensionId, StorageAreaNamespace::kManaged,
                    /*session_access_level=*/testing::Eq(std::nullopt),
                    ValueStoreChangeToJson(std::move(changes))));
  }

  policy::PolicyMap policies;
  policies.Set("aaa", policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_USER,
               policy::POLICY_SOURCE_CLOUD, value.Clone(), nullptr);
  SetCurrentPolicy(policies);
  Mock::VerifyAndClearExpectations(&observer_);

  // Notify when new policies are added.
  {
    value_store::ValueStoreChangeList changes;
    changes.push_back(
        value_store::ValueStoreChange("bbb", std::nullopt, value.Clone()));
    EXPECT_CALL(observer_,
                OnSettingsChangedJSON(
                    kTestExtensionId, StorageAreaNamespace::kManaged,
                    /*session_access_level=*/testing::Eq(std::nullopt),
                    ValueStoreChangeToJson(std::move(changes))));
  }

  policies.Set("bbb", policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_USER,
               policy::POLICY_SOURCE_CLOUD, value.Clone(), nullptr);
  SetCurrentPolicy(policies);
  Mock::VerifyAndClearExpectations(&observer_);

  // Notify when policies change.
  const base::Value new_value("222");
  {
    value_store::ValueStoreChangeList changes;
    changes.push_back(
        value_store::ValueStoreChange("bbb", value.Clone(), new_value.Clone()));
    EXPECT_CALL(observer_,
                OnSettingsChangedJSON(
                    kTestExtensionId, StorageAreaNamespace::kManaged,
                    /*session_access_level=*/testing::Eq(std::nullopt),
                    ValueStoreChangeToJson(std::move(changes))));
  }

  policies.Set("bbb", policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_USER,
               policy::POLICY_SOURCE_CLOUD, new_value.Clone(), nullptr);
  SetCurrentPolicy(policies);
  Mock::VerifyAndClearExpectations(&observer_);

  // Notify when policies are removed.
  {
    value_store::ValueStoreChangeList changes;
    changes.push_back(
        value_store::ValueStoreChange("bbb", new_value.Clone(), std::nullopt));
    EXPECT_CALL(observer_,
                OnSettingsChangedJSON(
                    kTestExtensionId, StorageAreaNamespace::kManaged,

                    /*session_access_level=*/testing::Eq(std::nullopt),
                    ValueStoreChangeToJson(std::move(changes))));
  }

  policies.Erase("bbb");
  SetCurrentPolicy(policies);
  Mock::VerifyAndClearExpectations(&observer_);

  // Don't notify when there aren't any changes.
  EXPECT_CALL(observer_, OnSettingsChangedJSON(_, _, _, _)).Times(0);
  SetCurrentPolicy(policies);
  Mock::VerifyAndClearExpectations(&observer_);
}

}  // namespace extensions