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.
10 #include "base/callback.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/path_service.h"
14 #include "base/prefs/scoped_user_pref_update.h"
15 #include "base/run_loop.h"
16 #include "base/strings/string_util.h"
17 #include "base/strings/utf_string_conversions.h"
18 #include "chrome/browser/component_updater/supervised_user_whitelist_installer.h"
19 #include "chrome/browser/supervised_user/supervised_user_site_list.h"
20 #include "chrome/browser/supervised_user/supervised_user_whitelist_service.h"
21 #include "chrome/common/chrome_paths.h"
22 #include "chrome/common/pref_names.h"
23 #include "chrome/test/base/testing_profile.h"
24 #include "sync/api/sync_change.h"
25 #include "sync/api/sync_error_factory.h"
26 #include "sync/protocol/sync.pb.h"
27 #include "testing/gtest/include/gtest/gtest.h"
31 const char kClientId
[] = "client-id";
33 class MockSupervisedUserWhitelistInstaller
34 : public component_updater::SupervisedUserWhitelistInstaller
{
36 MockSupervisedUserWhitelistInstaller() {}
37 ~MockSupervisedUserWhitelistInstaller() override
{}
39 const std::set
<std::string
>& registered_whitelists() {
40 return registered_whitelists_
;
43 void NotifyWhitelistReady(const std::string
& crx_id
,
44 const base::FilePath
& path
) {
45 for (const auto& callback
: ready_callbacks_
)
46 callback
.Run(crx_id
, path
);
49 // SupervisedUserWhitelistInstaller implementation:
50 void RegisterComponents() override
{}
52 void Subscribe(const WhitelistReadyCallback
& callback
) override
{
53 ready_callbacks_
.push_back(callback
);
56 void RegisterWhitelist(const std::string
& client_id
,
57 const std::string
& crx_id
,
58 const std::string
& name
) override
{
59 EXPECT_EQ(kClientId
, client_id
);
60 EXPECT_FALSE(WhitelistIsRegistered(crx_id
)) << crx_id
;
61 registered_whitelists_
.insert(crx_id
);
64 void UnregisterWhitelist(const std::string
& client_id
,
65 const std::string
& crx_id
) override
{
66 EXPECT_EQ(kClientId
, client_id
);
67 EXPECT_TRUE(WhitelistIsRegistered(crx_id
)) << crx_id
;
68 registered_whitelists_
.erase(crx_id
);
72 bool WhitelistIsRegistered(const std::string
& crx_id
) {
73 return registered_whitelists_
.count(crx_id
) > 0;
76 std::set
<std::string
> registered_whitelists_
;
77 std::vector
<WhitelistReadyCallback
> ready_callbacks_
;
82 class SupervisedUserWhitelistServiceTest
: public testing::Test
{
84 SupervisedUserWhitelistServiceTest()
85 : installer_(new MockSupervisedUserWhitelistInstaller
),
86 service_(new SupervisedUserWhitelistService(profile_
.GetPrefs(),
89 service_
->AddSiteListsChangedCallback(
90 base::Bind(&SupervisedUserWhitelistServiceTest::OnSiteListsChanged
,
91 base::Unretained(this)));
92 SupervisedUserSiteList::SetLoadInProcessForTesting(true);
95 ~SupervisedUserWhitelistServiceTest() override
{
96 SupervisedUserSiteList::SetLoadInProcessForTesting(false);
100 void PrepareInitialStateAndPreferences() {
101 // Create two whitelists.
102 DictionaryPrefUpdate
update(profile_
.GetPrefs(),
103 prefs::kSupervisedUserWhitelists
);
104 base::DictionaryValue
* dict
= update
.Get();
106 scoped_ptr
<base::DictionaryValue
> whitelist_dict(new base::DictionaryValue
);
107 whitelist_dict
->SetString("name", "Whitelist A");
108 dict
->Set("aaaa", whitelist_dict
.release());
110 whitelist_dict
.reset(new base::DictionaryValue
);
111 whitelist_dict
->SetString("name", "Whitelist B");
112 dict
->Set("bbbb", whitelist_dict
.release());
114 installer_
->RegisterWhitelist(kClientId
, "aaaa", "Whitelist A");
115 installer_
->RegisterWhitelist(kClientId
, "bbbb", "Whitelist B");
118 void CheckFinalStateAndPreferences() {
119 EXPECT_EQ(2u, installer_
->registered_whitelists().size());
120 EXPECT_EQ(1u, installer_
->registered_whitelists().count("bbbb"));
121 EXPECT_EQ(1u, installer_
->registered_whitelists().count("cccc"));
123 const base::DictionaryValue
* dict
=
124 profile_
.GetPrefs()->GetDictionary(prefs::kSupervisedUserWhitelists
);
125 EXPECT_EQ(2u, dict
->size());
126 const base::DictionaryValue
* whitelist_dict
= nullptr;
127 ASSERT_TRUE(dict
->GetDictionary("bbbb", &whitelist_dict
));
129 ASSERT_TRUE(whitelist_dict
->GetString("name", &name
));
130 EXPECT_EQ("Whitelist B New", name
);
131 ASSERT_TRUE(dict
->GetDictionary("cccc", &whitelist_dict
));
132 ASSERT_TRUE(whitelist_dict
->GetString("name", &name
));
133 EXPECT_EQ("Whitelist C", name
);
136 const sync_pb::ManagedUserWhitelistSpecifics
* FindWhitelist(
137 const syncer::SyncDataList
& data_list
,
138 const std::string
& id
) {
139 for (const syncer::SyncData
& data
: data_list
) {
140 const sync_pb::ManagedUserWhitelistSpecifics
& whitelist
=
141 data
.GetSpecifics().managed_user_whitelist();
142 if (whitelist
.id() == id
)
148 void OnSiteListsChanged(
149 const std::vector
<scoped_refptr
<SupervisedUserSiteList
>>& site_lists
) {
150 site_lists_
= site_lists
;
151 if (!site_lists_changed_callback_
.is_null())
152 site_lists_changed_callback_
.Run();
155 base::MessageLoop message_loop_
;
156 TestingProfile profile_
;
158 scoped_ptr
<MockSupervisedUserWhitelistInstaller
> installer_
;
159 scoped_ptr
<SupervisedUserWhitelistService
> service_
;
161 std::vector
<scoped_refptr
<SupervisedUserSiteList
>> site_lists_
;
162 base::Closure site_lists_changed_callback_
;
165 TEST_F(SupervisedUserWhitelistServiceTest
, MergeEmpty
) {
168 syncer::SyncMergeResult result
= service_
->MergeDataAndStartSyncing(
169 syncer::SUPERVISED_USER_WHITELISTS
, syncer::SyncDataList(),
170 scoped_ptr
<syncer::SyncChangeProcessor
>(),
171 scoped_ptr
<syncer::SyncErrorFactory
>());
172 EXPECT_FALSE(result
.error().IsSet());
173 EXPECT_EQ(0, result
.num_items_added());
174 EXPECT_EQ(0, result
.num_items_modified());
175 EXPECT_EQ(0, result
.num_items_deleted());
176 EXPECT_EQ(0, result
.num_items_before_association());
177 EXPECT_EQ(0, result
.num_items_after_association());
179 EXPECT_EQ(0u, installer_
->registered_whitelists().size());
182 TEST_F(SupervisedUserWhitelistServiceTest
, MergeExisting
) {
183 PrepareInitialStateAndPreferences();
185 // Initialize the service. The whitelists should not be ready yet.
187 EXPECT_EQ(0u, site_lists_
.size());
189 // Notify that whitelist A is ready.
190 base::RunLoop run_loop
;
191 site_lists_changed_callback_
= run_loop
.QuitClosure();
192 base::FilePath test_data_dir
;
193 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA
, &test_data_dir
));
194 base::FilePath whitelist_path
=
195 test_data_dir
.AppendASCII("whitelists/content_pack/site_list.json");
196 installer_
->NotifyWhitelistReady("aaaa", whitelist_path
);
199 ASSERT_EQ(1u, site_lists_
.size());
200 const std::vector
<SupervisedUserSiteList::Site
>& sites
=
201 site_lists_
[0]->sites();
202 EXPECT_EQ(3u, sites
.size());
203 EXPECT_EQ("YouTube", base::UTF16ToUTF8(sites
[0].name
));
205 // Do the initial merge. One item should be added (whitelist C), one should be
206 // modified (whitelist B), and one item should be removed (whitelist A).
207 syncer::SyncDataList initial_data
;
208 initial_data
.push_back(
209 SupervisedUserWhitelistService::CreateWhitelistSyncData(
210 "bbbb", "Whitelist B New"));
211 initial_data
.push_back(
212 SupervisedUserWhitelistService::CreateWhitelistSyncData(
213 "cccc", "Whitelist C"));
214 syncer::SyncMergeResult result
= service_
->MergeDataAndStartSyncing(
215 syncer::SUPERVISED_USER_WHITELISTS
, initial_data
,
216 scoped_ptr
<syncer::SyncChangeProcessor
>(),
217 scoped_ptr
<syncer::SyncErrorFactory
>());
218 EXPECT_FALSE(result
.error().IsSet());
219 EXPECT_EQ(1, result
.num_items_added());
220 EXPECT_EQ(1, result
.num_items_modified());
221 EXPECT_EQ(1, result
.num_items_deleted());
222 EXPECT_EQ(2, result
.num_items_before_association());
223 EXPECT_EQ(2, result
.num_items_after_association());
225 // Whitelist A (which was previously ready) should be removed now, and
226 // whitelist B was never ready.
227 EXPECT_EQ(0u, site_lists_
.size());
229 CheckFinalStateAndPreferences();
232 TEST_F(SupervisedUserWhitelistServiceTest
, ApplyChanges
) {
233 PrepareInitialStateAndPreferences();
237 // Process some changes.
238 syncer::SyncChangeList changes
;
239 changes
.push_back(syncer::SyncChange(
240 FROM_HERE
, syncer::SyncChange::ACTION_ADD
,
241 SupervisedUserWhitelistService::CreateWhitelistSyncData(
242 "cccc", "Whitelist C")));
243 changes
.push_back(syncer::SyncChange(
244 FROM_HERE
, syncer::SyncChange::ACTION_UPDATE
,
245 SupervisedUserWhitelistService::CreateWhitelistSyncData(
246 "bbbb", "Whitelist B New")));
247 changes
.push_back(syncer::SyncChange(
248 FROM_HERE
, syncer::SyncChange::ACTION_DELETE
,
249 SupervisedUserWhitelistService::CreateWhitelistSyncData(
250 "aaaa", "Ignored")));
251 syncer::SyncError error
= service_
->ProcessSyncChanges(FROM_HERE
, changes
);
252 EXPECT_FALSE(error
.IsSet());
254 EXPECT_EQ(0u, site_lists_
.size());
256 // If whitelist A now becomes ready, it should be ignored.
257 installer_
->NotifyWhitelistReady(
258 "aaaa", base::FilePath(FILE_PATH_LITERAL("/path/to/aaaa")));
259 EXPECT_EQ(0u, site_lists_
.size());
261 CheckFinalStateAndPreferences();
264 TEST_F(SupervisedUserWhitelistServiceTest
, GetAllSyncData
) {
265 PrepareInitialStateAndPreferences();
267 syncer::SyncDataList sync_data
=
268 service_
->GetAllSyncData(syncer::SUPERVISED_USER_WHITELISTS
);
269 ASSERT_EQ(2u, sync_data
.size());
270 const sync_pb::ManagedUserWhitelistSpecifics
* whitelist
=
271 FindWhitelist(sync_data
, "aaaa");
272 ASSERT_TRUE(whitelist
);
273 EXPECT_EQ("Whitelist A", whitelist
->name());
274 whitelist
= FindWhitelist(sync_data
, "bbbb");
275 ASSERT_TRUE(whitelist
);
276 EXPECT_EQ("Whitelist B", whitelist
->name());