Add ICU message format support
[chromium-blink-merge.git] / chrome / browser / component_updater / supervised_user_whitelist_installer_unittest.cc
blobdad559fa366c10c3480435064447ca173081135a
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/bind.h"
6 #include "base/callback.h"
7 #include "base/files/file_path.h"
8 #include "base/files/file_util.h"
9 #include "base/files/scoped_temp_dir.h"
10 #include "base/json/json_file_value_serializer.h"
11 #include "base/json/json_writer.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/path_service.h"
14 #include "base/prefs/testing_pref_service.h"
15 #include "base/run_loop.h"
16 #include "base/sequenced_task_runner.h"
17 #include "base/strings/string_util.h"
18 #include "base/test/scoped_path_override.h"
19 #include "base/values.h"
20 #include "chrome/browser/component_updater/supervised_user_whitelist_installer.h"
21 #include "chrome/common/pref_names.h"
22 #include "components/component_updater/component_updater_paths.h"
23 #include "components/component_updater/component_updater_service.h"
24 #include "components/crx_file/id_util.h"
25 #include "components/update_client/crx_update_item.h"
26 #include "components/update_client/update_client.h"
27 #include "components/update_client/utils.h"
28 #include "testing/gmock/include/gmock/gmock.h"
29 #include "testing/gtest/include/gtest/gtest.h"
31 using update_client::CrxComponent;
32 using update_client::CrxUpdateItem;
34 namespace component_updater {
36 namespace {
38 const char kClientId[] = "client-id";
39 const char kCrxId[] = "abcdefghijklmnopponmlkjihgfedcba";
40 const char kName[] = "Some Whitelist";
41 const char kOtherClientId[] = "other-client-id";
42 const char kVersion[] = "1.2.3.4";
43 const char kWhitelistContents[] = "{\"foo\": \"bar\"}";
44 const char kWhitelistFile[] = "whitelist.json";
46 std::string CrxIdToHashToCrxId(const std::string& kCrxId) {
47 CrxComponent component;
48 component.pk_hash =
49 SupervisedUserWhitelistInstaller::GetHashFromCrxId(kCrxId);
50 EXPECT_EQ(16u, component.pk_hash.size());
51 return GetCrxComponentID(component);
54 std::string JsonToString(const base::DictionaryValue& dict) {
55 std::string json;
56 base::JSONWriter::Write(dict, &json);
57 return json;
60 class MockComponentUpdateService : public ComponentUpdateService,
61 public OnDemandUpdater {
62 public:
63 MockComponentUpdateService(
64 const scoped_refptr<base::SequencedTaskRunner>& task_runner)
65 : task_runner_(task_runner), on_demand_update_called_(false) {}
67 ~MockComponentUpdateService() override {}
69 bool on_demand_update_called() const { return on_demand_update_called_; }
71 const CrxComponent* registered_component() { return component_.get(); }
73 void set_registration_callback(const base::Closure& registration_callback) {
74 registration_callback_ = registration_callback;
77 // ComponentUpdateService implementation:
78 void AddObserver(Observer* observer) override { ADD_FAILURE(); }
79 void RemoveObserver(Observer* observer) override { ADD_FAILURE(); }
81 std::vector<std::string> GetComponentIDs() const override {
82 ADD_FAILURE();
83 return std::vector<std::string>();
86 bool RegisterComponent(const CrxComponent& component) override {
87 EXPECT_EQ(nullptr, component_.get());
88 component_.reset(new CrxComponent(component));
89 if (!registration_callback_.is_null())
90 registration_callback_.Run();
92 return true;
95 bool UnregisterComponent(const std::string& crx_id) override {
96 if (!component_) {
97 ADD_FAILURE();
98 return false;
101 EXPECT_EQ(GetCrxComponentID(*component_), crx_id);
102 if (!component_->installer->Uninstall()) {
103 ADD_FAILURE();
104 return false;
107 component_.reset();
108 return true;
111 OnDemandUpdater& GetOnDemandUpdater() override { return *this; }
113 void MaybeThrottle(const std::string& kCrxId,
114 const base::Closure& callback) override {
115 ADD_FAILURE();
118 scoped_refptr<base::SequencedTaskRunner> GetSequencedTaskRunner() override {
119 return task_runner_;
122 bool GetComponentDetails(const std::string& component_id,
123 CrxUpdateItem* item) const override {
124 ADD_FAILURE();
125 return false;
128 // OnDemandUpdater implementation:
129 bool OnDemandUpdate(const std::string& crx_id) override {
130 on_demand_update_called_ = true;
132 if (!component_) {
133 ADD_FAILURE() << "Trying to update unregistered component " << crx_id;
134 return false;
137 EXPECT_EQ(GetCrxComponentID(*component_), crx_id);
138 return true;
141 private:
142 scoped_refptr<base::SequencedTaskRunner> task_runner_;
143 scoped_ptr<CrxComponent> component_;
144 base::Closure registration_callback_;
145 bool on_demand_update_called_;
148 class WhitelistLoadObserver {
149 public:
150 explicit WhitelistLoadObserver(SupervisedUserWhitelistInstaller* installer)
151 : weak_ptr_factory_(this) {
152 installer->Subscribe(base::Bind(&WhitelistLoadObserver::OnWhitelistReady,
153 weak_ptr_factory_.GetWeakPtr()));
156 void Wait() { run_loop_.Run(); }
158 const base::FilePath& whitelist_path() { return whitelist_path_; }
160 private:
161 void OnWhitelistReady(const std::string& crx_id,
162 const base::FilePath& whitelist_path) {
163 EXPECT_EQ(base::FilePath::StringType(), whitelist_path_.value());
164 whitelist_path_ = whitelist_path;
165 run_loop_.Quit();
168 base::FilePath whitelist_path_;
170 base::RunLoop run_loop_;
171 base::WeakPtrFactory<WhitelistLoadObserver> weak_ptr_factory_;
174 } // namespace
176 class SupervisedUserWhitelistInstallerTest : public testing::Test {
177 public:
178 SupervisedUserWhitelistInstallerTest()
179 : path_override_(DIR_SUPERVISED_USER_WHITELISTS),
180 component_update_service_(message_loop_.task_runner()),
181 installer_(
182 SupervisedUserWhitelistInstaller::Create(&component_update_service_,
183 nullptr,
184 &local_state_)) {}
186 ~SupervisedUserWhitelistInstallerTest() override {}
188 void SetUp() override {
189 SupervisedUserWhitelistInstaller::RegisterPrefs(local_state_.registry());
191 ASSERT_TRUE(PathService::Get(DIR_SUPERVISED_USER_WHITELISTS,
192 &whitelist_base_directory_));
193 whitelist_directory_ = whitelist_base_directory_.AppendASCII(kCrxId);
194 whitelist_version_directory_ = whitelist_directory_.AppendASCII(kVersion);
195 whitelist_path_ = whitelist_version_directory_.AppendASCII(kWhitelistFile);
197 scoped_ptr<base::DictionaryValue> whitelist_dict(
198 new base::DictionaryValue);
199 whitelist_dict->SetString("file", kWhitelistFile);
200 manifest_.Set("whitelist", whitelist_dict.release());
201 manifest_.SetString("version", kVersion);
203 scoped_ptr<base::DictionaryValue> crx_dict(new base::DictionaryValue);
204 crx_dict->SetString("name", kName);
205 scoped_ptr<base::ListValue> clients(new base::ListValue);
206 clients->AppendString(kClientId);
207 clients->AppendString(kOtherClientId);
208 crx_dict->Set("clients", clients.release());
209 pref_.Set(kCrxId, crx_dict.release());
212 protected:
213 void PrepareWhitelistDirectory(const base::FilePath& whitelist_directory) {
214 base::FilePath whitelist_path =
215 whitelist_directory.AppendASCII(kWhitelistFile);
216 size_t whitelist_contents_length = sizeof(kWhitelistContents) - 1;
217 ASSERT_EQ(static_cast<int>(whitelist_contents_length),
218 base::WriteFile(whitelist_path, kWhitelistContents,
219 whitelist_contents_length));
220 base::FilePath manifest_file =
221 whitelist_directory.AppendASCII("manifest.json");
222 ASSERT_TRUE(JSONFileValueSerializer(manifest_file).Serialize(manifest_));
225 void RegisterExistingComponents() {
226 local_state_.Set(prefs::kRegisteredSupervisedUserWhitelists, pref_);
227 base::RunLoop run_loop;
228 installer_->RegisterComponents();
229 run_loop.RunUntilIdle();
232 void CheckRegisteredComponent(const char* version) {
233 const CrxComponent* component =
234 component_update_service_.registered_component();
235 ASSERT_TRUE(component);
236 EXPECT_EQ(kName, component->name);
237 EXPECT_EQ(kCrxId, GetCrxComponentID(*component));
238 EXPECT_EQ(version, component->version.GetString());
241 base::MessageLoop message_loop_;
242 base::ScopedPathOverride path_override_;
243 MockComponentUpdateService component_update_service_;
244 TestingPrefServiceSimple local_state_;
245 scoped_ptr<SupervisedUserWhitelistInstaller> installer_;
246 base::FilePath whitelist_base_directory_;
247 base::FilePath whitelist_directory_;
248 base::FilePath whitelist_version_directory_;
249 base::FilePath whitelist_path_;
250 base::DictionaryValue manifest_;
251 base::DictionaryValue pref_;
254 TEST_F(SupervisedUserWhitelistInstallerTest, GetHashFromCrxId) {
256 std::string extension_id = "abcdefghijklmnopponmlkjihgfedcba";
257 ASSERT_EQ(extension_id, CrxIdToHashToCrxId(extension_id));
261 std::string extension_id = "aBcDeFgHiJkLmNoPpOnMlKjIhGfEdCbA";
262 ASSERT_EQ(base::StringToLowerASCII(extension_id),
263 CrxIdToHashToCrxId(extension_id));
267 std::string extension_id = crx_file::id_util::GenerateId("Moose");
268 ASSERT_EQ(extension_id, CrxIdToHashToCrxId(extension_id));
272 TEST_F(SupervisedUserWhitelistInstallerTest, InstallNewWhitelist) {
273 base::RunLoop registration_run_loop;
274 component_update_service_.set_registration_callback(
275 registration_run_loop.QuitClosure());
277 WhitelistLoadObserver observer(installer_.get());
278 installer_->RegisterWhitelist(kClientId, kCrxId, kName);
279 registration_run_loop.Run();
281 ASSERT_NO_FATAL_FAILURE(CheckRegisteredComponent("0.0.0.0"));
282 EXPECT_TRUE(component_update_service_.on_demand_update_called());
284 // Registering the same whitelist for another client should not do anything.
285 installer_->RegisterWhitelist(kOtherClientId, kCrxId, kName);
287 base::ScopedTempDir temp_dir;
288 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
289 base::FilePath unpacked_path = temp_dir.path();
290 ASSERT_NO_FATAL_FAILURE(PrepareWhitelistDirectory(unpacked_path));
292 const CrxComponent* component =
293 component_update_service_.registered_component();
294 ASSERT_TRUE(component);
295 ASSERT_TRUE(component->installer->Install(manifest_, unpacked_path));
296 observer.Wait();
297 EXPECT_EQ(whitelist_path_.value(), observer.whitelist_path().value());
299 std::string whitelist_contents;
300 ASSERT_TRUE(base::ReadFileToString(whitelist_path_, &whitelist_contents));
301 EXPECT_EQ(kWhitelistContents, whitelist_contents);
303 EXPECT_EQ(JsonToString(pref_),
304 JsonToString(*local_state_.GetDictionary(
305 prefs::kRegisteredSupervisedUserWhitelists)));
308 TEST_F(SupervisedUserWhitelistInstallerTest,
309 RegisterAndUninstallExistingWhitelist) {
310 ASSERT_TRUE(base::CreateDirectory(whitelist_version_directory_));
311 ASSERT_NO_FATAL_FAILURE(
312 PrepareWhitelistDirectory(whitelist_version_directory_));
314 // Create another whitelist directory, with an ID that is not registered.
315 base::FilePath other_directory =
316 whitelist_base_directory_.AppendASCII("paobncmdlekfjgihhigjfkeldmcnboap");
317 ASSERT_TRUE(base::CreateDirectory(other_directory));
318 ASSERT_NO_FATAL_FAILURE(PrepareWhitelistDirectory(other_directory));
320 // Create a directory that is not a valid whitelist directory.
321 base::FilePath non_whitelist_directory =
322 whitelist_base_directory_.AppendASCII("Not a whitelist");
323 ASSERT_TRUE(base::CreateDirectory(non_whitelist_directory));
325 RegisterExistingComponents();
327 ASSERT_NO_FATAL_FAILURE(CheckRegisteredComponent(kVersion));
328 EXPECT_FALSE(component_update_service_.on_demand_update_called());
330 // Check that unregistered whitelists have been removed:
331 // The registered whitelist directory should still exist.
332 EXPECT_TRUE(base::DirectoryExists(whitelist_directory_));
334 // The other directory should be gone.
335 EXPECT_FALSE(base::DirectoryExists(other_directory));
337 // The non-whitelist directory should still exist as well.
338 EXPECT_TRUE(base::DirectoryExists(non_whitelist_directory));
340 // Unregistering for the first client should do nothing.
342 base::RunLoop run_loop;
343 installer_->UnregisterWhitelist(kClientId, kCrxId);
344 run_loop.RunUntilIdle();
346 EXPECT_TRUE(component_update_service_.registered_component());
347 EXPECT_TRUE(base::DirectoryExists(whitelist_version_directory_));
349 // Unregistering for the second client should uninstall the whitelist.
351 base::RunLoop run_loop;
352 installer_->UnregisterWhitelist(kOtherClientId, kCrxId);
353 run_loop.RunUntilIdle();
355 EXPECT_FALSE(component_update_service_.registered_component());
356 EXPECT_FALSE(base::DirectoryExists(whitelist_directory_));
359 } // namespace component_updater