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
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <map>
#include <set>
#include <string>
#include "base/bind.h"
#include "base/callback.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop/message_loop.h"
#include "base/path_service.h"
#include "base/prefs/scoped_user_pref_update.h"
#include "base/run_loop.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/component_updater/supervised_user_whitelist_installer.h"
#include "chrome/browser/supervised_user/supervised_user_site_list.h"
#include "chrome/browser/supervised_user/supervised_user_whitelist_service.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/testing_profile.h"
#include "sync/api/sync_change.h"
#include "sync/api/sync_error_factory.h"
#include "sync/protocol/sync.pb.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
class MockSupervisedUserWhitelistInstaller
: public component_updater::SupervisedUserWhitelistInstaller {
public:
MockSupervisedUserWhitelistInstaller() {}
~MockSupervisedUserWhitelistInstaller() override {}
const std::set<std::string>& registered_whitelists() {
return registered_whitelists_;
}
void NotifyWhitelistReady(const std::string& crx_id,
const base::FilePath& path) {
// We purposely don't check whether the whitelist is registered, to allow
// testing the case where a whitelist will become ready after it has been
// unregistered.
ASSERT_TRUE(ready_callbacks_.count(crx_id) > 0) << crx_id;
ready_callbacks_[crx_id].Run(path);
}
// SupervisedUserWhitelistInstaller implementation:
void RegisterWhitelist(const std::string& crx_id,
const std::string& name,
bool new_installation,
const WhitelistReadyCallback& callback) override {
ASSERT_FALSE(WhitelistIsRegistered(crx_id)) << crx_id;
registered_whitelists_.insert(crx_id);
ready_callbacks_[crx_id] = callback;
}
void UnregisterWhitelist(const std::string& crx_id) override {
EXPECT_TRUE(WhitelistIsRegistered(crx_id)) << crx_id;
registered_whitelists_.erase(crx_id);
// Don't remove the callback (see above).
}
private:
bool WhitelistIsRegistered(const std::string& crx_id) {
return registered_whitelists_.count(crx_id) > 0;
}
std::set<std::string> registered_whitelists_;
std::map<std::string, WhitelistReadyCallback> ready_callbacks_;
};
} // namespace
class SupervisedUserWhitelistServiceTest : public testing::Test {
public:
SupervisedUserWhitelistServiceTest() {
installer_ = new MockSupervisedUserWhitelistInstaller;
service_.reset(new SupervisedUserWhitelistService(
profile_.GetPrefs(), make_scoped_ptr(installer_)));
service_->AddSiteListsChangedCallback(
base::Bind(&SupervisedUserWhitelistServiceTest::OnSiteListsChanged,
base::Unretained(this)));
SupervisedUserSiteList::SetLoadInProcessForTesting(true);
}
~SupervisedUserWhitelistServiceTest() override {
SupervisedUserSiteList::SetLoadInProcessForTesting(false);
}
protected:
void PrepareInitialPreferences() {
// Create two whitelists.
DictionaryPrefUpdate update(profile_.GetPrefs(),
prefs::kSupervisedUserWhitelists);
base::DictionaryValue* dict = update.Get();
scoped_ptr<base::DictionaryValue> whitelist_dict(new base::DictionaryValue);
whitelist_dict->SetString("name", "Whitelist A");
dict->Set("aaaa", whitelist_dict.release());
whitelist_dict.reset(new base::DictionaryValue);
whitelist_dict->SetString("name", "Whitelist B");
dict->Set("bbbb", whitelist_dict.release());
}
void CheckFinalStateAndPreferences() {
EXPECT_EQ(2u, installer_->registered_whitelists().size());
EXPECT_EQ(1u, installer_->registered_whitelists().count("bbbb"));
EXPECT_EQ(1u, installer_->registered_whitelists().count("cccc"));
const base::DictionaryValue* dict =
profile_.GetPrefs()->GetDictionary(prefs::kSupervisedUserWhitelists);
EXPECT_EQ(2u, dict->size());
const base::DictionaryValue* whitelist_dict = nullptr;
ASSERT_TRUE(dict->GetDictionary("bbbb", &whitelist_dict));
std::string name;
ASSERT_TRUE(whitelist_dict->GetString("name", &name));
EXPECT_EQ("Whitelist B New", name);
ASSERT_TRUE(dict->GetDictionary("cccc", &whitelist_dict));
ASSERT_TRUE(whitelist_dict->GetString("name", &name));
EXPECT_EQ("Whitelist C", name);
}
const sync_pb::ManagedUserWhitelistSpecifics* FindWhitelist(
const syncer::SyncDataList& data_list,
const std::string& id) {
for (const syncer::SyncData& data : data_list) {
const sync_pb::ManagedUserWhitelistSpecifics& whitelist =
data.GetSpecifics().managed_user_whitelist();
if (whitelist.id() == id)
return &whitelist;
}
return nullptr;
}
void OnSiteListsChanged(
const std::vector<scoped_refptr<SupervisedUserSiteList>>& site_lists) {
site_lists_ = site_lists;
if (!site_lists_changed_callback_.is_null())
site_lists_changed_callback_.Run();
}
base::MessageLoop message_loop_;
TestingProfile profile_;
// Owned by the SupervisedUserWhitelistService.
MockSupervisedUserWhitelistInstaller* installer_;
scoped_ptr<SupervisedUserWhitelistService> service_;
std::vector<scoped_refptr<SupervisedUserSiteList>> site_lists_;
base::Closure site_lists_changed_callback_;
};
TEST_F(SupervisedUserWhitelistServiceTest, MergeEmpty) {
service_->Init();
EXPECT_EQ(0u, installer_->registered_whitelists().size());
syncer::SyncMergeResult result = service_->MergeDataAndStartSyncing(
syncer::SUPERVISED_USER_WHITELISTS, syncer::SyncDataList(),
scoped_ptr<syncer::SyncChangeProcessor>(),
scoped_ptr<syncer::SyncErrorFactory>());
EXPECT_FALSE(result.error().IsSet());
EXPECT_EQ(0, result.num_items_added());
EXPECT_EQ(0, result.num_items_modified());
EXPECT_EQ(0, result.num_items_deleted());
EXPECT_EQ(0, result.num_items_before_association());
EXPECT_EQ(0, result.num_items_after_association());
EXPECT_EQ(0u, installer_->registered_whitelists().size());
}
TEST_F(SupervisedUserWhitelistServiceTest, MergeExisting) {
PrepareInitialPreferences();
// Initialize the service. Both whitelists should be registered, but not ready
// yet.
service_->Init();
EXPECT_EQ(2u, installer_->registered_whitelists().size());
EXPECT_EQ(1u, installer_->registered_whitelists().count("aaaa"));
EXPECT_EQ(1u, installer_->registered_whitelists().count("bbbb"));
EXPECT_EQ(0u, site_lists_.size());
// Notify that whitelist A is ready.
base::RunLoop run_loop;
site_lists_changed_callback_ = run_loop.QuitClosure();
base::FilePath test_data_dir;
ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir));
base::FilePath whitelist_path =
test_data_dir.AppendASCII("whitelists/content_pack/site_list.json");
installer_->NotifyWhitelistReady("aaaa", whitelist_path);
run_loop.Run();
ASSERT_EQ(1u, site_lists_.size());
const std::vector<SupervisedUserSiteList::Site>& sites =
site_lists_[0]->sites();
EXPECT_EQ(3u, sites.size());
EXPECT_EQ("YouTube", base::UTF16ToUTF8(sites[0].name));
// Do the initial merge. One item should be added (whitelist C), one should be
// modified (whitelist B), and one item should be removed (whitelist A).
syncer::SyncDataList initial_data;
initial_data.push_back(
SupervisedUserWhitelistService::CreateWhitelistSyncData(
"bbbb", "Whitelist B New"));
initial_data.push_back(
SupervisedUserWhitelistService::CreateWhitelistSyncData(
"cccc", "Whitelist C"));
syncer::SyncMergeResult result = service_->MergeDataAndStartSyncing(
syncer::SUPERVISED_USER_WHITELISTS, initial_data,
scoped_ptr<syncer::SyncChangeProcessor>(),
scoped_ptr<syncer::SyncErrorFactory>());
EXPECT_FALSE(result.error().IsSet());
EXPECT_EQ(1, result.num_items_added());
EXPECT_EQ(1, result.num_items_modified());
EXPECT_EQ(1, result.num_items_deleted());
EXPECT_EQ(2, result.num_items_before_association());
EXPECT_EQ(2, result.num_items_after_association());
// Whitelist A (which was previously ready) should be removed now, and
// whitelist B was never ready.
EXPECT_EQ(0u, site_lists_.size());
CheckFinalStateAndPreferences();
}
TEST_F(SupervisedUserWhitelistServiceTest, ApplyChanges) {
PrepareInitialPreferences();
service_->Init();
// Process some changes.
syncer::SyncChangeList changes;
changes.push_back(syncer::SyncChange(
FROM_HERE, syncer::SyncChange::ACTION_ADD,
SupervisedUserWhitelistService::CreateWhitelistSyncData(
"cccc", "Whitelist C")));
changes.push_back(syncer::SyncChange(
FROM_HERE, syncer::SyncChange::ACTION_UPDATE,
SupervisedUserWhitelistService::CreateWhitelistSyncData(
"bbbb", "Whitelist B New")));
changes.push_back(syncer::SyncChange(
FROM_HERE, syncer::SyncChange::ACTION_DELETE,
SupervisedUserWhitelistService::CreateWhitelistSyncData(
"aaaa", "Ignored")));
syncer::SyncError error = service_->ProcessSyncChanges(FROM_HERE, changes);
EXPECT_FALSE(error.IsSet());
EXPECT_EQ(0u, site_lists_.size());
// If whitelist A now becomes ready, it should be ignored.
installer_->NotifyWhitelistReady(
"aaaa", base::FilePath(FILE_PATH_LITERAL("/path/to/aaaa")));
EXPECT_EQ(0u, site_lists_.size());
CheckFinalStateAndPreferences();
}
TEST_F(SupervisedUserWhitelistServiceTest, GetAllSyncData) {
PrepareInitialPreferences();
syncer::SyncDataList sync_data =
service_->GetAllSyncData(syncer::SUPERVISED_USER_WHITELISTS);
ASSERT_EQ(2u, sync_data.size());
const sync_pb::ManagedUserWhitelistSpecifics* whitelist =
FindWhitelist(sync_data, "aaaa");
ASSERT_TRUE(whitelist);
EXPECT_EQ("Whitelist A", whitelist->name());
whitelist = FindWhitelist(sync_data, "bbbb");
ASSERT_TRUE(whitelist);
EXPECT_EQ("Whitelist B", whitelist->name());
}
|