Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(101)

Side by Side Diff: chrome/browser/policy/cloud/component_cloud_policy_service_unittest.cc

Issue 70863002: Refactored the ComponentCloudPolicyService. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/policy/cloud/component_cloud_policy_service.h" 5 #include "chrome/browser/policy/cloud/component_cloud_policy_service.h"
6 6
7 #include "base/callback.h" 7 #include "base/callback.h"
8 #include "base/files/scoped_temp_dir.h" 8 #include "base/files/scoped_temp_dir.h"
9 #include "base/message_loop/message_loop.h" 9 #include "base/message_loop/message_loop.h"
10 #include "base/pickle.h"
11 #include "base/run_loop.h" 10 #include "base/run_loop.h"
12 #include "base/sha1.h" 11 #include "base/sha1.h"
13 #include "base/single_thread_task_runner.h" 12 #include "base/single_thread_task_runner.h"
14 #include "base/stl_util.h" 13 #include "base/stl_util.h"
15 #include "base/values.h" 14 #include "base/values.h"
16 #include "chrome/browser/policy/cloud/cloud_policy_constants.h" 15 #include "chrome/browser/policy/cloud/cloud_policy_constants.h"
17 #include "chrome/browser/policy/cloud/mock_cloud_policy_client.h" 16 #include "chrome/browser/policy/cloud/mock_cloud_policy_client.h"
18 #include "chrome/browser/policy/cloud/mock_cloud_policy_store.h" 17 #include "chrome/browser/policy/cloud/mock_cloud_policy_store.h"
19 #include "chrome/browser/policy/cloud/policy_builder.h" 18 #include "chrome/browser/policy/cloud/policy_builder.h"
20 #include "chrome/browser/policy/cloud/resource_cache.h" 19 #include "chrome/browser/policy/cloud/resource_cache.h"
21 #include "chrome/browser/policy/external_data_fetcher.h" 20 #include "chrome/browser/policy/external_data_fetcher.h"
22 #include "chrome/browser/policy/policy_map.h" 21 #include "chrome/browser/policy/policy_map.h"
23 #include "chrome/browser/policy/policy_types.h" 22 #include "chrome/browser/policy/policy_types.h"
24 #include "chrome/browser/policy/proto/cloud/chrome_extension_policy.pb.h" 23 #include "chrome/browser/policy/proto/cloud/chrome_extension_policy.pb.h"
25 #include "chrome/browser/policy/proto/cloud/device_management_backend.pb.h" 24 #include "chrome/browser/policy/proto/cloud/device_management_backend.pb.h"
25 #include "chrome/browser/policy/schema_map.h"
26 #include "chrome/browser/policy/schema_registry.h" 26 #include "chrome/browser/policy/schema_registry.h"
bartfab (slow) 2013/11/14 14:13:12 Nit: Already included by header file now.
Joao da Silva 2013/11/14 14:56:41 Done.
27 #include "components/policy/core/common/schema.h" 27 #include "components/policy/core/common/schema.h"
28 #include "net/url_request/test_url_fetcher_factory.h" 28 #include "net/url_request/test_url_fetcher_factory.h"
29 #include "net/url_request/url_fetcher_delegate.h" 29 #include "net/url_request/url_fetcher_delegate.h"
30 #include "net/url_request/url_request_context.h" 30 #include "net/url_request/url_request_context.h"
31 #include "net/url_request/url_request_context_getter.h" 31 #include "net/url_request/url_request_context_getter.h"
32 #include "testing/gmock/include/gmock/gmock.h" 32 #include "testing/gmock/include/gmock/gmock.h"
33 #include "testing/gtest/include/gtest/gtest.h" 33 #include "testing/gtest/include/gtest/gtest.h"
34 34
35 namespace em = enterprise_management; 35 namespace em = enterprise_management;
36 36
37 using testing::Mock; 37 using testing::Mock;
38 38
39 namespace policy { 39 namespace policy {
40 40
41 namespace { 41 namespace {
42 42
43 const char kTestExtension[] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; 43 const char kTestExtension[] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
44 const char kTestExtension2[] = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"; 44 const char kTestExtension2[] = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb";
45 const char kTestDownload[] = "http://example.com/getpolicy?id=123"; 45 const char kTestDownload[] = "http://example.com/getpolicy?id=123";
46 const char kTestDownload2[] = "http://example.com/getpolicy?id=456";
47 46
48 const char kTestPolicy[] = 47 const char kTestPolicy[] =
49 "{" 48 "{"
50 " \"Name\": {" 49 " \"Name\": {"
51 " \"Value\": \"disabled\"" 50 " \"Value\": \"disabled\""
52 " }," 51 " },"
53 " \"Second\": {" 52 " \"Second\": {"
54 " \"Value\": \"maybe\"," 53 " \"Value\": \"maybe\","
55 " \"Level\": \"Recommended\"" 54 " \"Level\": \"Recommended\""
56 " }" 55 " }"
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
94 }; 93 };
95 94
96 } // namespace 95 } // namespace
97 96
98 class ComponentCloudPolicyServiceTest : public testing::Test { 97 class ComponentCloudPolicyServiceTest : public testing::Test {
99 protected: 98 protected:
100 ComponentCloudPolicyServiceTest() {} 99 ComponentCloudPolicyServiceTest() {}
101 100
102 virtual void SetUp() OVERRIDE { 101 virtual void SetUp() OVERRIDE {
103 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 102 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
103
104 cache_ = new ResourceCache(temp_dir_.path(), loop_.message_loop_proxy()); 104 cache_ = new ResourceCache(temp_dir_.path(), loop_.message_loop_proxy());
105 request_context_ =
106 new TestURLRequestContextGetter(loop_.message_loop_proxy());
105 service_.reset(new ComponentCloudPolicyService( 107 service_.reset(new ComponentCloudPolicyService(
106 &delegate_, 108 &delegate_,
109 &registry_,
107 &store_, 110 &store_,
108 make_scoped_ptr(cache_), 111 make_scoped_ptr(cache_),
112 &client_,
113 request_context_,
109 loop_.message_loop_proxy(), 114 loop_.message_loop_proxy(),
110 loop_.message_loop_proxy())); 115 loop_.message_loop_proxy()));
111 116
112 builder_.policy_data().set_policy_type( 117 builder_.policy_data().set_policy_type(
113 dm_protocol::kChromeExtensionPolicyType); 118 dm_protocol::kChromeExtensionPolicyType);
114 builder_.policy_data().set_settings_entity_id(kTestExtension); 119 builder_.policy_data().set_settings_entity_id(kTestExtension);
115 builder_.payload().set_download_url(kTestDownload); 120 builder_.payload().set_download_url(kTestDownload);
116 builder_.payload().set_secure_hash(base::SHA1HashString(kTestPolicy)); 121 builder_.payload().set_secure_hash(base::SHA1HashString(kTestPolicy));
117 122
118 expected_policy_.Set("Name", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 123 expected_policy_.Set("Name", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
119 base::Value::CreateStringValue("disabled"), NULL); 124 base::Value::CreateStringValue("disabled"), NULL);
120 expected_policy_.Set("Second", POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_USER, 125 expected_policy_.Set("Second", POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_USER,
121 base::Value::CreateStringValue("maybe"), NULL); 126 base::Value::CreateStringValue("maybe"), NULL);
122 127
123 // A NULL |request_context_| is enough to construct the updater, but 128 EXPECT_EQ(0u, client_.namespaces_to_fetch_.size());
124 // ComponentCloudPolicyService::Backend::LoadStore() tests the pointer when
125 // Connect() is called before the store was loaded.
126 request_context_ =
127 new TestURLRequestContextGetter(loop_.message_loop_proxy());
128 } 129 }
129 130
130 virtual void TearDown() OVERRIDE { 131 virtual void TearDown() OVERRIDE {
131 // The service cleans up its backend on the FILE thread. 132 // The service cleans up its backend on the background thread.
132 service_.reset(); 133 service_.reset();
133 RunUntilIdle(); 134 RunUntilIdle();
134 } 135 }
135 136
136 void RunUntilIdle() { 137 void RunUntilIdle() {
137 base::RunLoop().RunUntilIdle(); 138 base::RunLoop().RunUntilIdle();
138 } 139 }
139 140
140 void LoadStore() { 141 void LoadStore() {
141 EXPECT_FALSE(store_.is_initialized()); 142 EXPECT_FALSE(store_.is_initialized());
142 EXPECT_FALSE(service_->is_initialized());
143 143
144 em::PolicyData* data = new em::PolicyData(); 144 em::PolicyData* data = new em::PolicyData();
145 data->set_username(ComponentPolicyBuilder::kFakeUsername); 145 data->set_username(ComponentPolicyBuilder::kFakeUsername);
146 data->set_request_token(ComponentPolicyBuilder::kFakeToken); 146 data->set_request_token(ComponentPolicyBuilder::kFakeToken);
147 store_.policy_.reset(data); 147 store_.policy_.reset(data);
148 148
149 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated());
150 store_.NotifyStoreLoaded(); 149 store_.NotifyStoreLoaded();
151 RunUntilIdle(); 150 RunUntilIdle();
152 Mock::VerifyAndClearExpectations(&delegate_); 151 EXPECT_TRUE(store_.is_initialized());
153 EXPECT_TRUE(service_->is_initialized()); 152 }
153
154 void InitializeRegistry() {
155 registry_.RegisterComponent(
156 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, kTestExtension),
157 CreateTestSchema());
158 registry_.SetReady(POLICY_DOMAIN_CHROME);
159 registry_.SetReady(POLICY_DOMAIN_EXTENSIONS);
154 } 160 }
155 161
156 void PopulateCache() { 162 void PopulateCache() {
157 Pickle pickle;
158 pickle.WriteString(kTestExtension);
159 pickle.WriteString(kTestExtension2);
160 std::string data(reinterpret_cast<const char*>(pickle.data()),
161 pickle.size());
162 EXPECT_TRUE(
163 cache_->Store(ComponentCloudPolicyService::kComponentNamespaceCache,
164 dm_protocol::kChromeExtensionPolicyType,
165 data));
166
167 EXPECT_TRUE(cache_->Store( 163 EXPECT_TRUE(cache_->Store(
168 "extension-policy", kTestExtension, CreateSerializedResponse())); 164 "extension-policy", kTestExtension, CreateSerializedResponse()));
169 EXPECT_TRUE( 165 EXPECT_TRUE(
170 cache_->Store("extension-policy-data", kTestExtension, kTestPolicy)); 166 cache_->Store("extension-policy-data", kTestExtension, kTestPolicy));
171 167
172 builder_.policy_data().set_settings_entity_id(kTestExtension2); 168 builder_.policy_data().set_settings_entity_id(kTestExtension2);
173 EXPECT_TRUE(cache_->Store( 169 EXPECT_TRUE(cache_->Store(
174 "extension-policy", kTestExtension2, CreateSerializedResponse())); 170 "extension-policy", kTestExtension2, CreateSerializedResponse()));
175 EXPECT_TRUE( 171 EXPECT_TRUE(
176 cache_->Store("extension-policy-data", kTestExtension2, kTestPolicy)); 172 cache_->Store("extension-policy-data", kTestExtension2, kTestPolicy));
177
178 EXPECT_TRUE(cache_->Store("unrelated", "stuff", "here"));
179 } 173 }
180 174
181 scoped_ptr<em::PolicyFetchResponse> CreateResponse() { 175 scoped_ptr<em::PolicyFetchResponse> CreateResponse() {
182 builder_.Build(); 176 builder_.Build();
183 return make_scoped_ptr(new em::PolicyFetchResponse(builder_.policy())); 177 return make_scoped_ptr(new em::PolicyFetchResponse(builder_.policy()));
184 } 178 }
185 179
186 std::string CreateSerializedResponse() { 180 std::string CreateSerializedResponse() {
187 builder_.Build(); 181 builder_.Build();
188 return builder_.GetBlob(); 182 return builder_.GetBlob();
(...skipping 15 matching lines...) Expand all
204 // is destroyed. 198 // is destroyed.
205 ResourceCache* cache_; 199 ResourceCache* cache_;
206 MockCloudPolicyClient client_; 200 MockCloudPolicyClient client_;
207 MockCloudPolicyStore store_; 201 MockCloudPolicyStore store_;
208 SchemaRegistry registry_; 202 SchemaRegistry registry_;
209 scoped_ptr<ComponentCloudPolicyService> service_; 203 scoped_ptr<ComponentCloudPolicyService> service_;
210 ComponentPolicyBuilder builder_; 204 ComponentPolicyBuilder builder_;
211 PolicyMap expected_policy_; 205 PolicyMap expected_policy_;
212 }; 206 };
213 207
214 TEST_F(ComponentCloudPolicyServiceTest, InitializeWithoutCredentials) { 208 TEST_F(ComponentCloudPolicyServiceTest, InitializedAtConstructionTime) {
215 EXPECT_FALSE(service_->is_initialized()); 209 service_.reset();
216 // Run the background task to initialize the backend. 210 LoadStore();
217 RunUntilIdle(); 211 InitializeRegistry();
218 // Still waiting for the |store_|. 212
213 cache_ = new ResourceCache(temp_dir_.path(), loop_.message_loop_proxy());
214 service_.reset(new ComponentCloudPolicyService(&delegate_,
215 &registry_,
216 &store_,
217 make_scoped_ptr(cache_),
218 &client_,
219 request_context_,
220 loop_.message_loop_proxy(),
221 loop_.message_loop_proxy()));
219 EXPECT_FALSE(service_->is_initialized()); 222 EXPECT_FALSE(service_->is_initialized());
220 223
221 // Initialize with a store without credentials. 224 EXPECT_CALL(delegate_, OnComponentCloudPolicyRefreshNeeded());
222 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated()); 225 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated());
223 store_.NotifyStoreLoaded();
224 RunUntilIdle(); 226 RunUntilIdle();
227 Mock::VerifyAndClearExpectations(&delegate_);
228
225 EXPECT_TRUE(service_->is_initialized()); 229 EXPECT_TRUE(service_->is_initialized());
226 Mock::VerifyAndClearExpectations(&delegate_); 230 EXPECT_EQ(1u, client_.namespaces_to_fetch_.size());
227 const PolicyBundle empty_bundle; 231 const PolicyBundle empty_bundle;
228 EXPECT_TRUE(service_->policy().Equals(empty_bundle)); 232 EXPECT_TRUE(service_->policy().Equals(empty_bundle));
229 } 233 }
230 234
231 TEST_F(ComponentCloudPolicyServiceTest, InitializationWithCachedComponents) { 235 TEST_F(ComponentCloudPolicyServiceTest, InitializeStoreThenRegistry) {
232 // Store a previous list of components. 236 EXPECT_CALL(delegate_, OnComponentCloudPolicyRefreshNeeded()).Times(0);
233 Pickle pickle; 237 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated()).Times(0);
234 pickle.WriteString("aa"); 238 LoadStore();
235 pickle.WriteString("bb"); 239 Mock::VerifyAndClearExpectations(&delegate_);
236 pickle.WriteString("cc"); 240 EXPECT_FALSE(service_->is_initialized());
237 std::string data(reinterpret_cast<const char*>(pickle.data()), pickle.size());
238 EXPECT_TRUE(
239 cache_->Store(ComponentCloudPolicyService::kComponentNamespaceCache,
240 dm_protocol::kChromeExtensionPolicyType,
241 data));
242 // Store some garbage in another domain; it won't be fetched.
243 EXPECT_TRUE(cache_->Store(
244 ComponentCloudPolicyService::kComponentNamespaceCache, "garbage", data));
245 241
246 // Connect a client before initialization is complete. 242 EXPECT_CALL(delegate_, OnComponentCloudPolicyRefreshNeeded());
247 service_->Connect(&client_, request_context_); 243 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated());
248 EXPECT_TRUE(client_.namespaces_to_fetch_.empty()); 244 InitializeRegistry();
249 245 RunUntilIdle();
250 // Now load the backend. 246 Mock::VerifyAndClearExpectations(&delegate_);
251 LoadStore(); 247 EXPECT_TRUE(service_->is_initialized());
252 248 EXPECT_EQ(1u, client_.namespaces_to_fetch_.size());
253 // The cached namespaces were added to the client. 249 const PolicyBundle empty_bundle;
254 std::set<PolicyNamespaceKey> set; 250 EXPECT_TRUE(service_->policy().Equals(empty_bundle));
255 set.insert(PolicyNamespaceKey(dm_protocol::kChromeExtensionPolicyType, "aa"));
256 set.insert(PolicyNamespaceKey(dm_protocol::kChromeExtensionPolicyType, "bb"));
257 set.insert(PolicyNamespaceKey(dm_protocol::kChromeExtensionPolicyType, "cc"));
258 EXPECT_EQ(set, client_.namespaces_to_fetch_);
259
260 // And the bad components were wiped from the cache.
261 std::map<std::string, std::string> contents;
262 cache_->LoadAllSubkeys(ComponentCloudPolicyService::kComponentNamespaceCache,
263 &contents);
264 ASSERT_EQ(1u, contents.size());
265 EXPECT_EQ(std::string(dm_protocol::kChromeExtensionPolicyType),
266 contents.begin()->first);
267 } 251 }
268 252
269 TEST_F(ComponentCloudPolicyServiceTest, ConnectAfterRegister) { 253 TEST_F(ComponentCloudPolicyServiceTest, InitializeRegistryThenStore) {
270 // Add some components. 254 EXPECT_CALL(delegate_, OnComponentCloudPolicyRefreshNeeded()).Times(0);
255 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated()).Times(0);
256 InitializeRegistry();
257 RunUntilIdle();
258 Mock::VerifyAndClearExpectations(&delegate_);
259 EXPECT_FALSE(service_->is_initialized());
260
261 EXPECT_CALL(delegate_, OnComponentCloudPolicyRefreshNeeded());
262 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated());
263 LoadStore();
264 Mock::VerifyAndClearExpectations(&delegate_);
265 EXPECT_TRUE(service_->is_initialized());
266 EXPECT_EQ(1u, client_.namespaces_to_fetch_.size());
267 const PolicyBundle empty_bundle;
268 EXPECT_TRUE(service_->policy().Equals(empty_bundle));
269 }
270
271 TEST_F(ComponentCloudPolicyServiceTest, InitializeWithCachedPolicy) {
272 PopulateCache();
273
274 EXPECT_CALL(delegate_, OnComponentCloudPolicyRefreshNeeded());
275 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated());
276 InitializeRegistry();
277 LoadStore();
278 Mock::VerifyAndClearExpectations(&delegate_);
279
280 EXPECT_TRUE(service_->is_initialized());
281 EXPECT_EQ(1u, client_.namespaces_to_fetch_.size());
282
283 // kTestExtension2 is not in the registry so it was dropped.
284 std::map<std::string, std::string> contents;
285 cache_->LoadAllSubkeys("extension-policy", &contents);
286 ASSERT_EQ(1u, contents.size());
287 EXPECT_EQ(kTestExtension, contents.begin()->first);
288
289 PolicyBundle expected_bundle;
290 const PolicyNamespace ns(POLICY_DOMAIN_EXTENSIONS, kTestExtension);
291 expected_bundle.Get(ns).CopyFrom(expected_policy_);
292 EXPECT_TRUE(service_->policy().Equals(expected_bundle));
293 }
294
295 TEST_F(ComponentCloudPolicyServiceTest, FetchPolicy) {
296 // Initialize the store and create the backend.
297 // A refresh is not needed, because no components are registered yet.
298 EXPECT_CALL(delegate_, OnComponentCloudPolicyRefreshNeeded()).Times(0);
299 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated());
300 registry_.SetReady(POLICY_DOMAIN_CHROME);
301 registry_.SetReady(POLICY_DOMAIN_EXTENSIONS);
302 LoadStore();
303 Mock::VerifyAndClearExpectations(&delegate_);
304 EXPECT_TRUE(service_->is_initialized());
305
306 // Register the components to fetch.
307 EXPECT_CALL(delegate_, OnComponentCloudPolicyRefreshNeeded());
271 registry_.RegisterComponent( 308 registry_.RegisterComponent(
272 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, kTestExtension), 309 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, kTestExtension),
273 CreateTestSchema()); 310 CreateTestSchema());
274 registry_.RegisterComponent( 311 RunUntilIdle();
275 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, kTestExtension2),
276 CreateTestSchema());
277 service_->OnSchemasUpdated(registry_.schema_map());
278
279 // Now connect the client.
280 EXPECT_TRUE(client_.namespaces_to_fetch_.empty());
281 service_->Connect(&client_, request_context_);
282 EXPECT_TRUE(client_.namespaces_to_fetch_.empty());
283
284 // It receives the namespaces once the backend is initialized.
285 LoadStore();
286 std::set<PolicyNamespaceKey> set;
287 set.insert(PolicyNamespaceKey(dm_protocol::kChromeExtensionPolicyType,
288 kTestExtension));
289 set.insert(PolicyNamespaceKey(dm_protocol::kChromeExtensionPolicyType,
290 kTestExtension2));
291 EXPECT_EQ(set, client_.namespaces_to_fetch_);
292
293 // The current components were also persisted to the cache.
294 std::map<std::string, std::string> contents;
295 cache_->LoadAllSubkeys(ComponentCloudPolicyService::kComponentNamespaceCache,
296 &contents);
297 ASSERT_EQ(1u, contents.size());
298 EXPECT_EQ(std::string(dm_protocol::kChromeExtensionPolicyType),
299 contents.begin()->first);
300 const std::string data(contents.begin()->second);
301 Pickle pickle(data.data(), data.size());
302 PickleIterator pickit(pickle);
303 std::string value0;
304 std::string value1;
305 std::string tmp;
306 ASSERT_TRUE(pickit.ReadString(&value0));
307 ASSERT_TRUE(pickit.ReadString(&value1));
308 EXPECT_FALSE(pickit.ReadString(&tmp));
309 std::set<std::string> unpickled;
310 unpickled.insert(value0);
311 unpickled.insert(value1);
312
313 std::set<std::string> expected_components;
314 expected_components.insert(kTestExtension);
315 expected_components.insert(kTestExtension2);
316
317 EXPECT_EQ(expected_components, unpickled);
318 }
319
320 TEST_F(ComponentCloudPolicyServiceTest, StoreReadyAfterConnectAndRegister) {
321 // Add some previous data to the cache.
322 PopulateCache();
323
324 // Add some components.
325 registry_.RegisterComponent(
326 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, kTestExtension),
327 CreateTestSchema());
328 service_->OnSchemasUpdated(registry_.schema_map());
329
330 // And connect the client. Make the client have some policies, with a new
331 // download_url.
332 builder_.payload().set_download_url(kTestDownload2);
333 client_.SetPolicy(PolicyNamespaceKey(dm_protocol::kChromeExtensionPolicyType,
334 kTestExtension),
335 *CreateResponse());
336 service_->Connect(&client_, request_context_);
337
338 // Now make the store ready.
339 LoadStore();
340
341 // The components that were registed before have their caches purged.
342 std::map<std::string, std::string> contents;
343 cache_->LoadAllSubkeys("extension-policy", &contents);
344 EXPECT_EQ(1u, contents.size()); // kTestExtension2 was purged.
345 EXPECT_TRUE(ContainsKey(contents, kTestExtension));
346 cache_->LoadAllSubkeys("unrelated", &contents);
347 EXPECT_EQ(1u, contents.size()); // unrelated keys are not purged.
348 EXPECT_TRUE(ContainsKey(contents, "stuff"));
349
350 // The policy responses that the client already had start updating.
351 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
352 ASSERT_TRUE(fetcher);
353 // Expect the URL of the client's PolicyFetchResponse, not the cached URL.
354 EXPECT_EQ(GURL(kTestDownload2), fetcher->GetOriginalURL());
355 }
356
357 TEST_F(ComponentCloudPolicyServiceTest, ConnectThenRegisterThenStoreReady) {
358 // Connect right after creating the service.
359 service_->Connect(&client_, request_context_);
360
361 // Now register the current components, before the backend has been
362 // initialized.
363 EXPECT_TRUE(client_.namespaces_to_fetch_.empty());
364 registry_.RegisterComponent(
365 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, kTestExtension),
366 CreateTestSchema());
367 service_->OnSchemasUpdated(registry_.schema_map());
368 EXPECT_TRUE(client_.namespaces_to_fetch_.empty());
369
370 // Now load the store. The client gets the namespaces.
371 LoadStore();
372 std::set<PolicyNamespaceKey> set;
373 set.insert(PolicyNamespaceKey(dm_protocol::kChromeExtensionPolicyType,
374 kTestExtension));
375 EXPECT_EQ(set, client_.namespaces_to_fetch_);
376 }
377
378 TEST_F(ComponentCloudPolicyServiceTest, FetchPolicy) {
379 // Initialize the store and create the backend, and connect the client.
380 LoadStore();
381 // A refresh is not needed, because no components were found.
382 EXPECT_CALL(delegate_, OnComponentCloudPolicyRefreshNeeded()).Times(0);
383 service_->Connect(&client_, request_context_);
384 Mock::VerifyAndClearExpectations(&delegate_);
385
386 // Register the components to fetch.
387 registry_.RegisterComponent(
388 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, kTestExtension),
389 CreateTestSchema());
390 EXPECT_CALL(delegate_, OnComponentCloudPolicyRefreshNeeded());
391 service_->OnSchemasUpdated(registry_.schema_map());
392 Mock::VerifyAndClearExpectations(&delegate_); 312 Mock::VerifyAndClearExpectations(&delegate_);
393 313
394 // Send back a fake policy fetch response. 314 // Send back a fake policy fetch response.
395 client_.SetPolicy(PolicyNamespaceKey(dm_protocol::kChromeExtensionPolicyType, 315 client_.SetPolicy(PolicyNamespaceKey(dm_protocol::kChromeExtensionPolicyType,
396 kTestExtension), 316 kTestExtension),
397 *CreateResponse()); 317 *CreateResponse());
398 service_->OnPolicyFetched(&client_); 318 service_->OnPolicyFetched(&client_);
399 RunUntilIdle(); 319 RunUntilIdle();
400 320
401 // That should have triggered the download fetch. 321 // That should have triggered the download fetch.
402 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0); 322 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
403 ASSERT_TRUE(fetcher); 323 ASSERT_TRUE(fetcher);
404 EXPECT_EQ(GURL(kTestDownload), fetcher->GetOriginalURL()); 324 EXPECT_EQ(GURL(kTestDownload), fetcher->GetOriginalURL());
405 fetcher->set_response_code(200); 325 fetcher->set_response_code(200);
406 fetcher->SetResponseString(kTestPolicy); 326 fetcher->SetResponseString(kTestPolicy);
407 fetcher->delegate()->OnURLFetchComplete(fetcher); 327 fetcher->delegate()->OnURLFetchComplete(fetcher);
408 328
409 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated()); 329 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated());
410 RunUntilIdle(); 330 RunUntilIdle();
411 Mock::VerifyAndClearExpectations(&delegate_); 331 Mock::VerifyAndClearExpectations(&delegate_);
412 332
413 // The policy is now being served. 333 // The policy is now being served.
414 PolicyNamespace ns(POLICY_DOMAIN_EXTENSIONS, kTestExtension); 334 const PolicyNamespace ns(POLICY_DOMAIN_EXTENSIONS, kTestExtension);
415 PolicyBundle expected_bundle; 335 PolicyBundle expected_bundle;
416 expected_bundle.Get(ns).CopyFrom(expected_policy_); 336 expected_bundle.Get(ns).CopyFrom(expected_policy_);
417 EXPECT_TRUE(service_->policy().Equals(expected_bundle)); 337 EXPECT_TRUE(service_->policy().Equals(expected_bundle));
418 } 338 }
419 339
420 TEST_F(ComponentCloudPolicyServiceTest, LoadAndPurgeCache) { 340 TEST_F(ComponentCloudPolicyServiceTest, LoadAndPurgeCache) {
421 // Insert data in the cache. 341 // Insert data in the cache.
422 PopulateCache(); 342 PopulateCache();
343 registry_.RegisterComponent(
344 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, kTestExtension2),
345 CreateTestSchema());
346 InitializeRegistry();
423 347
424 // Load the initial cache. 348 // Load the initial cache.
349 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated());
350 EXPECT_CALL(delegate_, OnComponentCloudPolicyRefreshNeeded());
425 LoadStore(); 351 LoadStore();
426
427 EXPECT_CALL(delegate_, OnComponentCloudPolicyRefreshNeeded());
428 service_->Connect(&client_, request_context_);
429 Mock::VerifyAndClearExpectations(&delegate_); 352 Mock::VerifyAndClearExpectations(&delegate_);
430 353
431 PolicyBundle expected_bundle; 354 PolicyBundle expected_bundle;
432 PolicyNamespace ns(POLICY_DOMAIN_EXTENSIONS, kTestExtension); 355 PolicyNamespace ns(POLICY_DOMAIN_EXTENSIONS, kTestExtension);
433 expected_bundle.Get(ns).CopyFrom(expected_policy_); 356 expected_bundle.Get(ns).CopyFrom(expected_policy_);
434 ns.component_id = kTestExtension2; 357 ns.component_id = kTestExtension2;
435 expected_bundle.Get(ns).CopyFrom(expected_policy_); 358 expected_bundle.Get(ns).CopyFrom(expected_policy_);
436 EXPECT_TRUE(service_->policy().Equals(expected_bundle)); 359 EXPECT_TRUE(service_->policy().Equals(expected_bundle));
437 360
438 // Now purge one of the extensions. 361 // Now purge one of the extensions.
439 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated()); 362 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated());
440 // The service will start updating the components that are registered, which 363 registry_.UnregisterComponent(
441 // starts by fetching policy for them. 364 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, kTestExtension));
442 registry_.RegisterComponent(
443 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, kTestExtension2),
444 CreateTestSchema());
445 service_->OnSchemasUpdated(registry_.schema_map());
446 RunUntilIdle(); 365 RunUntilIdle();
447 Mock::VerifyAndClearExpectations(&delegate_); 366 Mock::VerifyAndClearExpectations(&delegate_);
448 367
449 ns.component_id = kTestExtension; 368 ns.component_id = kTestExtension;
450 expected_bundle.Get(ns).Clear(); 369 expected_bundle.Get(ns).Clear();
451 EXPECT_TRUE(service_->policy().Equals(expected_bundle)); 370 EXPECT_TRUE(service_->policy().Equals(expected_bundle));
452 371
453 std::map<std::string, std::string> contents; 372 std::map<std::string, std::string> contents;
454 cache_->LoadAllSubkeys("extension-policy", &contents); 373 cache_->LoadAllSubkeys("extension-policy", &contents);
455 EXPECT_EQ(1u, contents.size()); 374 EXPECT_EQ(1u, contents.size());
456 EXPECT_TRUE(ContainsKey(contents, kTestExtension2)); 375 EXPECT_TRUE(ContainsKey(contents, kTestExtension2));
457 cache_->LoadAllSubkeys("unrelated", &contents);
458 EXPECT_EQ(1u, contents.size());
459 EXPECT_TRUE(ContainsKey(contents, "stuff"));
460 } 376 }
461 377
462 TEST_F(ComponentCloudPolicyServiceTest, UpdateCredentials) { 378 TEST_F(ComponentCloudPolicyServiceTest, SignInAfterStartup) {
379 registry_.SetReady(POLICY_DOMAIN_CHROME);
380 registry_.SetReady(POLICY_DOMAIN_EXTENSIONS);
381
463 // Do the same as LoadStore() but without the initial credentials. 382 // Do the same as LoadStore() but without the initial credentials.
464 EXPECT_FALSE(store_.is_initialized()); 383 EXPECT_FALSE(store_.is_initialized());
465 EXPECT_FALSE(service_->is_initialized()); 384 EXPECT_FALSE(service_->is_initialized());
466 store_.policy_.reset(new em::PolicyData()); // No credentials. 385 store_.policy_.reset(new em::PolicyData()); // No credentials.
467 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated()); 386 EXPECT_CALL(delegate_, OnComponentCloudPolicyUpdated());
468 store_.NotifyStoreLoaded(); 387 store_.NotifyStoreLoaded();
469 RunUntilIdle(); 388 RunUntilIdle();
470 Mock::VerifyAndClearExpectations(&delegate_); 389 Mock::VerifyAndClearExpectations(&delegate_);
471 EXPECT_TRUE(service_->is_initialized()); 390 EXPECT_TRUE(service_->is_initialized());
472 391
473 // Connect the client and register an extension. 392 // Register an extension.
474 service_->Connect(&client_, request_context_);
475 EXPECT_CALL(delegate_, OnComponentCloudPolicyRefreshNeeded()); 393 EXPECT_CALL(delegate_, OnComponentCloudPolicyRefreshNeeded());
476 registry_.RegisterComponent( 394 registry_.RegisterComponent(
477 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, kTestExtension), 395 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, kTestExtension),
478 CreateTestSchema()); 396 CreateTestSchema());
479 service_->OnSchemasUpdated(registry_.schema_map()); 397 RunUntilIdle();
480 Mock::VerifyAndClearExpectations(&delegate_); 398 Mock::VerifyAndClearExpectations(&delegate_);
481 399
482 // Send the response to the service. The response data will be rejected, 400 // Send the response to the service. The response data will be ignored,
483 // because the store doesn't have the updated credentials yet. 401 // because the store doesn't have the updated credentials yet.
484 client_.SetPolicy(PolicyNamespaceKey(dm_protocol::kChromeExtensionPolicyType, 402 client_.SetPolicy(PolicyNamespaceKey(dm_protocol::kChromeExtensionPolicyType,
485 kTestExtension), 403 kTestExtension),
486 *CreateResponse()); 404 *CreateResponse());
487 service_->OnPolicyFetched(&client_); 405 service_->OnPolicyFetched(&client_);
488 RunUntilIdle(); 406 RunUntilIdle();
489 407
490 // The policy was not verified, and no download is started. 408 // The policy was ignored, and no download is started.
491 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0); 409 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
492 EXPECT_FALSE(fetcher); 410 EXPECT_FALSE(fetcher);
493 411
494 // Now update the |store_| with the updated policy, which includes 412 // Now update the |store_| with the updated policy, which includes
495 // credentials. The responses in the |client_| will be reloaded. 413 // credentials. The responses in the |client_| will be reloaded.
496 em::PolicyData* data = new em::PolicyData(); 414 em::PolicyData* data = new em::PolicyData();
497 data->set_username(ComponentPolicyBuilder::kFakeUsername); 415 data->set_username(ComponentPolicyBuilder::kFakeUsername);
498 data->set_request_token(ComponentPolicyBuilder::kFakeToken); 416 data->set_request_token(ComponentPolicyBuilder::kFakeToken);
499 store_.policy_.reset(data); 417 store_.policy_.reset(data);
500 store_.NotifyStoreLoaded(); 418 store_.NotifyStoreLoaded();
(...skipping 12 matching lines...) Expand all
513 Mock::VerifyAndClearExpectations(&delegate_); 431 Mock::VerifyAndClearExpectations(&delegate_);
514 432
515 // The policy is now being served. 433 // The policy is now being served.
516 PolicyNamespace ns(POLICY_DOMAIN_EXTENSIONS, kTestExtension); 434 PolicyNamespace ns(POLICY_DOMAIN_EXTENSIONS, kTestExtension);
517 PolicyBundle expected_bundle; 435 PolicyBundle expected_bundle;
518 expected_bundle.Get(ns).CopyFrom(expected_policy_); 436 expected_bundle.Get(ns).CopyFrom(expected_policy_);
519 EXPECT_TRUE(service_->policy().Equals(expected_bundle)); 437 EXPECT_TRUE(service_->policy().Equals(expected_bundle));
520 } 438 }
521 439
522 } // namespace policy 440 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698