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

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

Issue 13792003: Switch ComponentCloudPolicyUpdater to ExternalPolicyDataUpdater backend (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 8 months 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_updater.h" 5 #include "chrome/browser/policy/cloud/component_cloud_policy_updater.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/basictypes.h" 9 #include "base/compiler_specific.h"
10 #include "base/files/scoped_temp_dir.h" 10 #include "base/files/scoped_temp_dir.h"
11 #include "base/memory/ref_counted.h" 11 #include "base/sequenced_task_runner.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/sha1.h" 12 #include "base/sha1.h"
14 #include "base/test/test_pending_task.h"
15 #include "base/test/test_simple_task_runner.h" 13 #include "base/test/test_simple_task_runner.h"
16 #include "base/time.h" 14 #include "base/values.h"
17 #include "chrome/browser/policy/cloud/cloud_policy_constants.h" 15 #include "chrome/browser/policy/cloud/cloud_policy_constants.h"
18 #include "chrome/browser/policy/cloud/component_cloud_policy_store.h" 16 #include "chrome/browser/policy/cloud/component_cloud_policy_store.h"
19 #include "chrome/browser/policy/cloud/policy_builder.h" 17 #include "chrome/browser/policy/cloud/policy_builder.h"
20 #include "chrome/browser/policy/cloud/proto/chrome_extension_policy.pb.h" 18 #include "chrome/browser/policy/cloud/proto/chrome_extension_policy.pb.h"
21 #include "chrome/browser/policy/cloud/proto/device_management_backend.pb.h" 19 #include "chrome/browser/policy/cloud/proto/device_management_backend.pb.h"
22 #include "chrome/browser/policy/cloud/resource_cache.h" 20 #include "chrome/browser/policy/cloud/resource_cache.h"
23 #include "net/base/net_errors.h" 21 #include "chrome/browser/policy/policy_bundle.h"
22 #include "chrome/browser/policy/policy_map.h"
23 #include "chrome/browser/policy/policy_types.h"
24 #include "googleurl/src/gurl.h"
24 #include "net/url_request/test_url_fetcher_factory.h" 25 #include "net/url_request/test_url_fetcher_factory.h"
25 #include "net/url_request/url_fetcher_delegate.h" 26 #include "net/url_request/url_fetcher_delegate.h"
26 #include "net/url_request/url_request_context_getter.h" 27 #include "net/url_request/url_request_context_getter.h"
27 #include "testing/gmock/include/gmock/gmock.h" 28 #include "testing/gmock/include/gmock/gmock.h"
28 #include "testing/gtest/include/gtest/gtest.h" 29 #include "testing/gtest/include/gtest/gtest.h"
29 30
30 namespace em = enterprise_management; 31 namespace em = enterprise_management;
31 32
32 using testing::Mock; 33 using testing::Mock;
33 34
34 namespace policy { 35 namespace policy {
35 36
36 namespace { 37 namespace {
37 38
38 const char kTestExtension[] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; 39 const char kTestExtension[] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
39 const char kTestExtension2[] = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"; 40 const char kTestExtension2[] = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb";
40 const char kTestDownload[] = "http://example.com/getpolicy?id=123"; 41 const char kTestDownload[] = "http://example.com/getpolicy?id=123";
41 const char kTestDownload2[] = "http://example.com/getpolicy?id=456"; 42 const char kTestDownload2[] = "http://example.com/getpolicy?id=456";
42 const char kTestDownload3[] = "http://example.com/getpolicy?id=789";
43 const char kTestPolicy[] = 43 const char kTestPolicy[] =
44 "{" 44 "{"
45 " \"Name\": {" 45 " \"Name\": {"
46 " \"Value\": \"disabled\"" 46 " \"Value\": \"disabled\""
47 " }," 47 " },"
48 " \"Second\": {" 48 " \"Second\": {"
49 " \"Value\": \"maybe\"," 49 " \"Value\": \"maybe\","
50 " \"Level\": \"Recommended\"" 50 " \"Level\": \"Recommended\""
51 " }" 51 " }"
52 "}"; 52 "}";
53 53
54 class MockComponentCloudPolicyStoreDelegate 54 class MockComponentCloudPolicyStoreDelegate
55 : public ComponentCloudPolicyStore::Delegate { 55 : public ComponentCloudPolicyStore::Delegate {
56 public: 56 public:
57 virtual ~MockComponentCloudPolicyStoreDelegate() {} 57 virtual ~MockComponentCloudPolicyStoreDelegate() {}
58 58
59 MOCK_METHOD0(OnComponentCloudPolicyStoreUpdated, void()); 59 MOCK_METHOD0(OnComponentCloudPolicyStoreUpdated, void());
60 }; 60 };
61 61
62 } // namespace 62 } // namespace
63 63
64 class ComponentCloudPolicyUpdaterTest : public testing::Test { 64 class ComponentCloudPolicyUpdaterTest : public testing::Test {
65 protected: 65 protected:
66 virtual void SetUp() OVERRIDE { 66 virtual void SetUp() OVERRIDE;
67 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
68 cache_.reset(new ResourceCache(temp_dir_.path()));
69 store_.reset(new ComponentCloudPolicyStore(&store_delegate_, cache_.get()));
70 store_->SetCredentials(ComponentPolicyBuilder::kFakeUsername,
71 ComponentPolicyBuilder::kFakeToken);
72 fetcher_factory_.set_remove_fetcher_on_delete(true);
73 scoped_refptr<net::URLRequestContextGetter> request_context;
74 task_runner_ = new base::TestSimpleTaskRunner();
75 updater_.reset(new ComponentCloudPolicyUpdater(
76 task_runner_, request_context, store_.get()));
77 ASSERT_FALSE(GetCurrentFetcher());
78 ASSERT_TRUE(store_->policy().begin() == store_->policy().end());
79 67
80 builder_.policy_data().set_policy_type( 68 scoped_ptr<em::PolicyFetchResponse> CreateResponse();
81 dm_protocol::kChromeExtensionPolicyType);
82 builder_.policy_data().set_settings_entity_id(kTestExtension);
83 builder_.payload().set_download_url(kTestDownload);
84 builder_.payload().set_secure_hash(base::SHA1HashString(kTestPolicy));
85
86 PolicyNamespace ns(POLICY_DOMAIN_EXTENSIONS, kTestExtension);
87 PolicyMap& policy = expected_bundle_.Get(ns);
88 policy.Set("Name", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
89 base::Value::CreateStringValue("disabled"));
90 policy.Set("Second", POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_USER,
91 base::Value::CreateStringValue("maybe"));
92 }
93
94 net::TestURLFetcher* GetCurrentFetcher() {
95 return fetcher_factory_.GetFetcherByID(0);
96 }
97
98 // Many tests also verify that a second fetcher is scheduled once the first
99 // completes, either successfully or with a failure. This helper starts two
100 // fetches immediately, so that the second is queued.
101 void StartTwoFetches() {
102 updater_->UpdateExternalPolicy(CreateResponse());
103
104 builder_.payload().set_download_url(kTestDownload2);
105 builder_.policy_data().set_settings_entity_id(kTestExtension2);
106 updater_->UpdateExternalPolicy(CreateResponse());
107 }
108
109 scoped_ptr<em::PolicyFetchResponse> CreateResponse() {
110 builder_.Build();
111 return make_scoped_ptr(new em::PolicyFetchResponse(builder_.policy()));
112 }
113
114 std::string CreateSerializedResponse() {
115 builder_.Build();
116 return builder_.GetBlob();
117 }
118 69
119 base::ScopedTempDir temp_dir_; 70 base::ScopedTempDir temp_dir_;
120 scoped_ptr<ResourceCache> cache_; 71 scoped_ptr<ResourceCache> cache_;
121 scoped_ptr<ComponentCloudPolicyStore> store_; 72 scoped_ptr<ComponentCloudPolicyStore> store_;
122 MockComponentCloudPolicyStoreDelegate store_delegate_; 73 MockComponentCloudPolicyStoreDelegate store_delegate_;
123 net::TestURLFetcherFactory fetcher_factory_; 74 net::TestURLFetcherFactory fetcher_factory_;
124 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; 75 scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
125 scoped_ptr<ComponentCloudPolicyUpdater> updater_; 76 scoped_ptr<ComponentCloudPolicyUpdater> updater_;
126 ComponentPolicyBuilder builder_; 77 ComponentPolicyBuilder builder_;
127 PolicyBundle expected_bundle_; 78 PolicyBundle expected_bundle_;
128 }; 79 };
129 80
81 void ComponentCloudPolicyUpdaterTest::SetUp() OVERRIDE {
82 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
83 cache_.reset(new ResourceCache(temp_dir_.path()));
84 store_.reset(new ComponentCloudPolicyStore(&store_delegate_, cache_.get()));
85 store_->SetCredentials(ComponentPolicyBuilder::kFakeUsername,
86 ComponentPolicyBuilder::kFakeToken);
87 fetcher_factory_.set_remove_fetcher_on_delete(true);
88 task_runner_ = new base::TestSimpleTaskRunner();
89 updater_.reset(new ComponentCloudPolicyUpdater(
90 task_runner_,
91 scoped_refptr<net::URLRequestContextGetter>(),
92 store_.get()));
93 ASSERT_EQ(store_->policy().end(), store_->policy().begin());
94
95 builder_.policy_data().set_policy_type(
96 dm_protocol::kChromeExtensionPolicyType);
97 builder_.policy_data().set_settings_entity_id(kTestExtension);
98 builder_.payload().set_download_url(kTestDownload);
99 builder_.payload().set_secure_hash(base::SHA1HashString(kTestPolicy));
100
101 PolicyNamespace ns(POLICY_DOMAIN_EXTENSIONS, kTestExtension);
102 PolicyMap& policy = expected_bundle_.Get(ns);
103 policy.Set("Name", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
104 base::Value::CreateStringValue("disabled"));
105 policy.Set("Second", POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_USER,
106 base::Value::CreateStringValue("maybe"));
107 }
108
109 scoped_ptr<em::PolicyFetchResponse>
110 ComponentCloudPolicyUpdaterTest::CreateResponse() {
111 builder_.Build();
112 return make_scoped_ptr(new em::PolicyFetchResponse(builder_.policy()));
113 }
114
130 TEST_F(ComponentCloudPolicyUpdaterTest, FetchAndCache) { 115 TEST_F(ComponentCloudPolicyUpdaterTest, FetchAndCache) {
131 StartTwoFetches(); 116 // Submit a policy fetch response.
132 117 updater_->UpdateExternalPolicy(CreateResponse());
133 // The first fetch was scheduled; complete it. 118
134 net::TestURLFetcher* fetcher = GetCurrentFetcher(); 119 // Verify that a download has been started.
135 ASSERT_TRUE(fetcher); 120 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
136 EXPECT_EQ(GURL(kTestDownload), fetcher->GetOriginalURL()); 121 ASSERT_TRUE(fetcher);
122 EXPECT_EQ(GURL(kTestDownload), fetcher->GetOriginalURL());
123
124 // Complete the download.
137 fetcher->set_response_code(200); 125 fetcher->set_response_code(200);
138 fetcher->SetResponseString(kTestPolicy); 126 fetcher->SetResponseString(kTestPolicy);
139 EXPECT_CALL(store_delegate_, OnComponentCloudPolicyStoreUpdated()); 127 EXPECT_CALL(store_delegate_, OnComponentCloudPolicyStoreUpdated());
140 fetcher->delegate()->OnURLFetchComplete(fetcher); 128 fetcher->delegate()->OnURLFetchComplete(fetcher);
141 Mock::VerifyAndClearExpectations(&store_delegate_); 129 Mock::VerifyAndClearExpectations(&store_delegate_);
142 130
143 // The second fetch was scheduled. 131 // Verify that the download is no longer running.
144 fetcher = GetCurrentFetcher(); 132 ASSERT_FALSE(fetcher_factory_.GetFetcherByID(0));
145 ASSERT_TRUE(fetcher); 133
146 EXPECT_EQ(GURL(kTestDownload2), fetcher->GetOriginalURL()); 134 // Verify that the downloaded policy is being served.
147
148 // The policy is being served.
149 EXPECT_TRUE(store_->policy().Equals(expected_bundle_)); 135 EXPECT_TRUE(store_->policy().Equals(expected_bundle_));
150 136 }
151 // No retries have been scheduled. 137
152 EXPECT_TRUE(task_runner_->GetPendingTasks().empty()); 138 TEST_F(ComponentCloudPolicyUpdaterTest, PolicyFetchResponseTooLarge) {
153 } 139 // Submit a policy fetch response that exceeds the allowed maximum size.
154
155 TEST_F(ComponentCloudPolicyUpdaterTest, LargeResponse) {
156 std::string long_download("http://example.com/get?id="); 140 std::string long_download("http://example.com/get?id=");
157 long_download.append(20 * 1024, '1'); 141 long_download.append(20 * 1024, '1');
158 builder_.payload().set_download_url(long_download); 142 builder_.payload().set_download_url(long_download);
159 updater_->UpdateExternalPolicy(CreateResponse()); 143 updater_->UpdateExternalPolicy(CreateResponse());
160 144
145 // Submit a valid policy fetch response.
161 builder_.policy_data().set_settings_entity_id(kTestExtension2); 146 builder_.policy_data().set_settings_entity_id(kTestExtension2);
162 builder_.payload().set_download_url(kTestDownload2); 147 builder_.payload().set_download_url(kTestDownload2);
163 updater_->UpdateExternalPolicy(CreateResponse()); 148 updater_->UpdateExternalPolicy(CreateResponse());
164 149
165 // The first request was dropped, and the second was started. 150 // Verify that the first policy fetch response has been ignored and a download
166 net::TestURLFetcher* fetcher = GetCurrentFetcher(); 151 // has been started for the second instead.
167 ASSERT_TRUE(fetcher); 152 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
168 EXPECT_EQ(GURL(kTestDownload2), fetcher->GetOriginalURL()); 153 ASSERT_TRUE(fetcher);
169 154 EXPECT_EQ(GURL(kTestDownload2), fetcher->GetOriginalURL());
170 // No retry is scheduled for the first request, because the policy was 155 }
171 // rejected (too large). 156
172 EXPECT_TRUE(task_runner_->GetPendingTasks().empty()); 157 TEST_F(ComponentCloudPolicyUpdaterTest, PolicyFetchResponseInvalid) {
173 } 158 // Submit an invalid policy fetch response.
174
175 TEST_F(ComponentCloudPolicyUpdaterTest, InvalidPolicy) {
176 builder_.policy_data().set_username("wronguser@example.com"); 159 builder_.policy_data().set_username("wronguser@example.com");
177 updater_->UpdateExternalPolicy(CreateResponse()); 160 updater_->UpdateExternalPolicy(CreateResponse());
178 161
162 // Submit a valid policy fetch response.
179 builder_.policy_data().set_username(ComponentPolicyBuilder::kFakeUsername); 163 builder_.policy_data().set_username(ComponentPolicyBuilder::kFakeUsername);
180 builder_.policy_data().set_settings_entity_id(kTestExtension2); 164 builder_.policy_data().set_settings_entity_id(kTestExtension2);
181 builder_.payload().set_download_url(kTestDownload2); 165 builder_.payload().set_download_url(kTestDownload2);
182 updater_->UpdateExternalPolicy(CreateResponse()); 166 updater_->UpdateExternalPolicy(CreateResponse());
183 167
184 // The first request was dropped, and the second was started. 168 // Verify that the first policy fetch response has been ignored and a download
185 net::TestURLFetcher* fetcher = GetCurrentFetcher(); 169 // has been started for the second instead.
186 ASSERT_TRUE(fetcher); 170 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
187 EXPECT_EQ(GURL(kTestDownload2), fetcher->GetOriginalURL()); 171 ASSERT_TRUE(fetcher);
188 172 EXPECT_EQ(GURL(kTestDownload2), fetcher->GetOriginalURL());
189 // No retry is scheduled for the first request, because the policy was
190 // rejected (invalid).
191 EXPECT_TRUE(task_runner_->GetPendingTasks().empty());
192 } 173 }
193 174
194 TEST_F(ComponentCloudPolicyUpdaterTest, AlreadyCached) { 175 TEST_F(ComponentCloudPolicyUpdaterTest, AlreadyCached) {
176 // Cache policy for an extension.
177 builder_.Build();
195 PolicyNamespace ns(POLICY_DOMAIN_EXTENSIONS, kTestExtension); 178 PolicyNamespace ns(POLICY_DOMAIN_EXTENSIONS, kTestExtension);
196 EXPECT_CALL(store_delegate_, OnComponentCloudPolicyStoreUpdated()); 179 EXPECT_CALL(store_delegate_, OnComponentCloudPolicyStoreUpdated());
197 EXPECT_TRUE(store_->Store(ns, 180 EXPECT_TRUE(store_->Store(ns,
198 CreateSerializedResponse(), 181 builder_.GetBlob(),
199 base::SHA1HashString(kTestPolicy), 182 base::SHA1HashString(kTestPolicy),
200 kTestPolicy)); 183 kTestPolicy));
201 Mock::VerifyAndClearExpectations(&store_delegate_); 184 Mock::VerifyAndClearExpectations(&store_delegate_);
202 185
203 // Seeing the same policy data again won't trigger a new fetch. 186 // Submit a policy fetch response whose extension ID and hash match the
204 updater_->UpdateExternalPolicy(CreateResponse()); 187 // already cached policy.
205 EXPECT_FALSE(GetCurrentFetcher()); 188 updater_->UpdateExternalPolicy(CreateResponse());
206 189
207 // And no retry is scheduled either. 190 // Verify that no download has been started.
208 EXPECT_TRUE(task_runner_->GetPendingTasks().empty()); 191 EXPECT_FALSE(fetcher_factory_.GetFetcherByID(0));
209 } 192 }
210 193
211 TEST_F(ComponentCloudPolicyUpdaterTest, LargeFetch) { 194 TEST_F(ComponentCloudPolicyUpdaterTest, PolicyDataInvalid) {
212 StartTwoFetches(); 195 // Submit two policy fetch responses.
213 196 updater_->UpdateExternalPolicy(CreateResponse());
214 net::TestURLFetcher* fetcher = GetCurrentFetcher(); 197 builder_.payload().set_download_url(kTestDownload2);
215 ASSERT_TRUE(fetcher); 198 builder_.policy_data().set_settings_entity_id(kTestExtension2);
216 EXPECT_EQ(GURL(kTestDownload), fetcher->GetOriginalURL()); 199 updater_->UpdateExternalPolicy(CreateResponse());
200
201 // Verify that the first download has been started.
202 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
203 ASSERT_TRUE(fetcher);
204 EXPECT_EQ(GURL(kTestDownload), fetcher->GetOriginalURL());
205
206 // Indicate that the policy data size will exceed allowed maximum.
217 fetcher->delegate()->OnURLFetchDownloadProgress(fetcher, 6 * 1024 * 1024, -1); 207 fetcher->delegate()->OnURLFetchDownloadProgress(fetcher, 6 * 1024 * 1024, -1);
218 208
219 // The second fetch was scheduled next. 209 // Verify that the first download is no longer running.
220 fetcher = GetCurrentFetcher(); 210 ASSERT_FALSE(fetcher_factory_.GetFetcherByID(0));
221 ASSERT_TRUE(fetcher); 211
222 EXPECT_EQ(GURL(kTestDownload2), fetcher->GetOriginalURL()); 212 // Verify that the second download has been started.
223 213 fetcher = fetcher_factory_.GetFetcherByID(1);
224 // A retry is scheduled for the first fetcher. 214 ASSERT_TRUE(fetcher);
225 EXPECT_EQ(1u, task_runner_->GetPendingTasks().size()); 215 EXPECT_EQ(GURL(kTestDownload2), fetcher->GetOriginalURL());
226 }
227
228 TEST_F(ComponentCloudPolicyUpdaterTest, FetchFailed) {
229 StartTwoFetches();
230
231 net::TestURLFetcher* fetcher = GetCurrentFetcher();
232 ASSERT_TRUE(fetcher);
233 EXPECT_EQ(GURL(kTestDownload), fetcher->GetOriginalURL());
234 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::FAILED,
235 net::ERR_NETWORK_CHANGED));
236 fetcher->delegate()->OnURLFetchComplete(fetcher);
237
238 // The second fetch was scheduled next.
239 fetcher = GetCurrentFetcher();
240 ASSERT_TRUE(fetcher);
241 EXPECT_EQ(GURL(kTestDownload2), fetcher->GetOriginalURL());
242
243 // A retry is scheduled for the first fetcher.
244 EXPECT_EQ(1u, task_runner_->GetPendingTasks().size());
245 }
246
247 TEST_F(ComponentCloudPolicyUpdaterTest, ServerFailed) {
248 StartTwoFetches();
249
250 net::TestURLFetcher* fetcher = GetCurrentFetcher();
251 ASSERT_TRUE(fetcher);
252 EXPECT_EQ(GURL(kTestDownload), fetcher->GetOriginalURL());
253 fetcher->set_response_code(500);
254 fetcher->delegate()->OnURLFetchComplete(fetcher);
255
256 // The second fetch was scheduled next.
257 fetcher = GetCurrentFetcher();
258 ASSERT_TRUE(fetcher);
259 EXPECT_EQ(GURL(kTestDownload2), fetcher->GetOriginalURL());
260
261 // A retry is scheduled for the first fetcher.
262 EXPECT_EQ(1u, task_runner_->GetPendingTasks().size());
263 }
264
265 TEST_F(ComponentCloudPolicyUpdaterTest, RetryLimit) {
266 updater_->UpdateExternalPolicy(CreateResponse());
267
268 // Failing due to client errors retries up to 3 times.
269 for (int i = 0; i < 3; ++i) {
270 net::TestURLFetcher* fetcher = GetCurrentFetcher();
271 ASSERT_TRUE(fetcher);
272 EXPECT_EQ(GURL(kTestDownload), fetcher->GetOriginalURL());
273
274 // Make it fail with a 400 (bad request) client error.
275 fetcher->set_response_code(400);
276 fetcher->delegate()->OnURLFetchComplete(fetcher);
277
278 // A retry is scheduled.
279 EXPECT_EQ(1u, task_runner_->GetPendingTasks().size());
280 // Make it retry immediately.
281 task_runner_->RunPendingTasks();
282 EXPECT_TRUE(task_runner_->GetPendingTasks().empty());
283 }
284
285 net::TestURLFetcher* fetcher = GetCurrentFetcher();
286 ASSERT_TRUE(fetcher);
287 EXPECT_EQ(GURL(kTestDownload), fetcher->GetOriginalURL());
288
289 // Make the last retry fail too; it won't retry anymore.
290 fetcher->set_response_code(400);
291 fetcher->delegate()->OnURLFetchComplete(fetcher);
292 EXPECT_TRUE(task_runner_->GetPendingTasks().empty());
293 }
294
295 TEST_F(ComponentCloudPolicyUpdaterTest, RetryWithBackoff) {
296 updater_->UpdateExternalPolicy(CreateResponse());
297
298 base::TimeDelta expected_delay = base::TimeDelta::FromSeconds(60);
299 const base::TimeDelta delay_cap = base::TimeDelta::FromHours(12);
300
301 // The backoff delay is capped at 12 hours, which is reached after 10 retries:
302 // 60 * 2^10 == 61440 > 43200 == 12 * 60 * 60
303 for (int i = 0; i < 20; ++i) {
304 net::TestURLFetcher* fetcher = GetCurrentFetcher();
305 ASSERT_TRUE(fetcher);
306 EXPECT_EQ(GURL(kTestDownload), fetcher->GetOriginalURL());
307
308 // Make it fail with a 500 server error.
309 fetcher->set_response_code(500);
310 fetcher->delegate()->OnURLFetchComplete(fetcher);
311
312 // A retry is scheduled. The delay is twice the last delay, with random
313 // jitter from 80% to 100%.
314 ASSERT_EQ(1u, task_runner_->GetPendingTasks().size());
315 const base::TestPendingTask& task = task_runner_->GetPendingTasks().front();
316 EXPECT_GT(task.delay,
317 base::TimeDelta::FromMilliseconds(
318 0.799 * expected_delay.InMilliseconds()));
319 EXPECT_LE(task.delay, expected_delay);
320
321 if (i < 10) {
322 // The delay cap hasn't been reached yet.
323 EXPECT_LT(expected_delay, delay_cap);
324 expected_delay *= 2;
325
326 if (i == 9) {
327 // The last doubling reached the cap.
328 EXPECT_GT(expected_delay, delay_cap);
329 expected_delay = delay_cap;
330 }
331 }
332
333 // Make it retry immediately.
334 task_runner_->RunPendingTasks();
335 EXPECT_TRUE(task_runner_->GetPendingTasks().empty());
336 }
337 }
338
339 TEST_F(ComponentCloudPolicyUpdaterTest, DataValidationFails) {
340 StartTwoFetches();
341
342 net::TestURLFetcher* fetcher = GetCurrentFetcher();
343 ASSERT_TRUE(fetcher);
344 EXPECT_EQ(GURL(kTestDownload), fetcher->GetOriginalURL());
345 fetcher->set_response_code(200);
346 fetcher->SetResponseString("{ won't hash }");
347 fetcher->delegate()->OnURLFetchComplete(fetcher);
348
349 // The second fetch was scheduled next.
350 fetcher = GetCurrentFetcher();
351 ASSERT_TRUE(fetcher);
352 EXPECT_EQ(GURL(kTestDownload2), fetcher->GetOriginalURL());
353
354 // A retry is scheduled for the first fetcher.
355 EXPECT_EQ(1u, task_runner_->GetPendingTasks().size());
356 } 216 }
357 217
358 TEST_F(ComponentCloudPolicyUpdaterTest, FetchUpdatedData) { 218 TEST_F(ComponentCloudPolicyUpdaterTest, FetchUpdatedData) {
359 updater_->UpdateExternalPolicy(CreateResponse()); 219 // Submit a policy fetch response.
360 220 updater_->UpdateExternalPolicy(CreateResponse());
361 // Same extension, but the download location has changed. 221
362 builder_.payload().set_download_url(kTestDownload2); 222 // Verify that the first download has been started.
363 updater_->UpdateExternalPolicy(CreateResponse()); 223 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
364 224 ASSERT_TRUE(fetcher);
365 // The first was cancelled and overridden by the second. 225 EXPECT_EQ(GURL(kTestDownload), fetcher->GetOriginalURL());
366 net::TestURLFetcher* fetcher = GetCurrentFetcher(); 226
367 ASSERT_TRUE(fetcher); 227 // Submit a second policy fetch response for the same extension with an
368 EXPECT_EQ(GURL(kTestDownload2), fetcher->GetOriginalURL()); 228 // updated download URL.
369 229 builder_.payload().set_download_url(kTestDownload2);
370 // No retries are scheduled. 230 updater_->UpdateExternalPolicy(CreateResponse());
371 EXPECT_TRUE(task_runner_->GetPendingTasks().empty()); 231
232 // Verify that the first download is no longer running.
233 ASSERT_FALSE(fetcher_factory_.GetFetcherByID(0));
234
235 // Verify that the second download has been started.
236 fetcher = fetcher_factory_.GetFetcherByID(1);
237 ASSERT_TRUE(fetcher);
238 EXPECT_EQ(GURL(kTestDownload2), fetcher->GetOriginalURL());
372 } 239 }
373 240
374 TEST_F(ComponentCloudPolicyUpdaterTest, FetchUpdatedDataWithoutPolicy) { 241 TEST_F(ComponentCloudPolicyUpdaterTest, FetchUpdatedDataWithoutPolicy) {
375 // Fetch the initial policy data. 242 // Submit a policy fetch response.
376 updater_->UpdateExternalPolicy(CreateResponse()); 243 updater_->UpdateExternalPolicy(CreateResponse());
377 244
378 net::TestURLFetcher* fetcher = GetCurrentFetcher(); 245 // Verify that the download has been started.
379 ASSERT_TRUE(fetcher); 246 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
380 EXPECT_EQ(GURL(kTestDownload), fetcher->GetOriginalURL()); 247 ASSERT_TRUE(fetcher);
248 EXPECT_EQ(GURL(kTestDownload), fetcher->GetOriginalURL());
249
250 // Complete the download.
381 fetcher->set_response_code(200); 251 fetcher->set_response_code(200);
382 fetcher->SetResponseString(kTestPolicy); 252 fetcher->SetResponseString(kTestPolicy);
383 EXPECT_CALL(store_delegate_, OnComponentCloudPolicyStoreUpdated()); 253 EXPECT_CALL(store_delegate_, OnComponentCloudPolicyStoreUpdated());
384 fetcher->delegate()->OnURLFetchComplete(fetcher); 254 fetcher->delegate()->OnURLFetchComplete(fetcher);
385 Mock::VerifyAndClearExpectations(&store_delegate_); 255 Mock::VerifyAndClearExpectations(&store_delegate_);
386 EXPECT_FALSE(GetCurrentFetcher()); 256
387 257 // Verify that the download is no longer running.
388 // The policy is being served. 258 EXPECT_FALSE(fetcher_factory_.GetFetcherByID(0));
259
260 // Verify that the downloaded policy is being served.
389 EXPECT_TRUE(store_->policy().Equals(expected_bundle_)); 261 EXPECT_TRUE(store_->policy().Equals(expected_bundle_));
390 262
391 // Same extension, but the download location has changed and is empty, meaning 263 // Submit a second policy fetch response for the same extension with no
392 // that no policy should be served anymore. 264 // download URL, meaning that no policy should be provided for this extension.
393 EXPECT_CALL(store_delegate_, OnComponentCloudPolicyStoreUpdated());
394 builder_.payload().clear_download_url(); 265 builder_.payload().clear_download_url();
395 builder_.payload().clear_secure_hash(); 266 builder_.payload().clear_secure_hash();
396 updater_->UpdateExternalPolicy(CreateResponse()); 267 EXPECT_CALL(store_delegate_, OnComponentCloudPolicyStoreUpdated());
397 Mock::VerifyAndClearExpectations(&store_delegate_); 268 updater_->UpdateExternalPolicy(CreateResponse());
398 269 Mock::VerifyAndClearExpectations(&store_delegate_);
399 // No fetcher was started for that. 270
400 EXPECT_FALSE(GetCurrentFetcher()); 271 // Verify that no download has been started.
401 272 EXPECT_FALSE(fetcher_factory_.GetFetcherByID(1));
402 // And the policy has been removed. 273
274 // Verify that the policy is no longer being served.
403 const PolicyBundle empty_bundle; 275 const PolicyBundle empty_bundle;
404 EXPECT_TRUE(store_->policy().Equals(empty_bundle)); 276 EXPECT_TRUE(store_->policy().Equals(empty_bundle));
405 } 277 }
406 278
407 TEST_F(ComponentCloudPolicyUpdaterTest, InvalidatedJob) {
408 StartTwoFetches();
409
410 // Start a new fetch for the second extension with a new download URL; the
411 // queued job is invalidated.
412 builder_.payload().set_download_url(kTestDownload3);
413 updater_->UpdateExternalPolicy(CreateResponse());
414
415 // The first request is still pending.
416 net::TestURLFetcher* fetcher = GetCurrentFetcher();
417 ASSERT_TRUE(fetcher);
418 EXPECT_EQ(GURL(kTestDownload), fetcher->GetOriginalURL());
419
420 // Make it fail.
421 fetcher->set_response_code(500);
422 fetcher->delegate()->OnURLFetchComplete(fetcher);
423
424 // Now the second job was invalidated, and the third job (for the same
425 // extension) is the next one.
426 fetcher = GetCurrentFetcher();
427 ASSERT_TRUE(fetcher);
428 EXPECT_EQ(GURL(kTestDownload3), fetcher->GetOriginalURL());
429 }
430
431 TEST_F(ComponentCloudPolicyUpdaterTest, NoPolicy) { 279 TEST_F(ComponentCloudPolicyUpdaterTest, NoPolicy) {
432 // Start a fetch with a valid download url. 280 // Submit a policy fetch response with a valid download URL.
433 updater_->UpdateExternalPolicy(CreateResponse()); 281 updater_->UpdateExternalPolicy(CreateResponse());
434 net::TestURLFetcher* fetcher = GetCurrentFetcher(); 282
435 ASSERT_TRUE(fetcher); 283 // Verify that the download has been started.
436 284 ASSERT_TRUE(fetcher_factory_.GetFetcherByID(0));
437 // Now update the policy fetch response before the fetch completes. The new 285
438 // data does not have a download url. 286 // Update the policy fetch response before the download has finished. The new
287 // policy fetch response has no download URL.
439 builder_.payload().Clear(); 288 builder_.payload().Clear();
440 updater_->UpdateExternalPolicy(CreateResponse()); 289 updater_->UpdateExternalPolicy(CreateResponse());
441 290
442 // The download has been cancelled. 291 // Verify that the download is no longer running.
443 fetcher = GetCurrentFetcher(); 292 ASSERT_FALSE(fetcher_factory_.GetFetcherByID(0));
444 ASSERT_FALSE(fetcher);
445 } 293 }
446 294
447 } // namespace policy 295 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698