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

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: Fixed copy&paste mistake that tripped up clang. 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
« no previous file with comments | « chrome/browser/policy/cloud/component_cloud_policy_updater.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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";
41 const char kTestExtension3[] = "cccccccccccccccccccccccccccccccc";
40 const char kTestDownload[] = "http://example.com/getpolicy?id=123"; 42 const char kTestDownload[] = "http://example.com/getpolicy?id=123";
41 const char kTestDownload2[] = "http://example.com/getpolicy?id=456"; 43 const char kTestDownload2[] = "http://example.com/getpolicy?id=456";
42 const char kTestDownload3[] = "http://example.com/getpolicy?id=789"; 44 const char kTestDownload3[] = "http://example.com/getpolicy?id=789";
43 const char kTestPolicy[] = 45 const char kTestPolicy[] =
44 "{" 46 "{"
45 " \"Name\": {" 47 " \"Name\": {"
46 " \"Value\": \"disabled\"" 48 " \"Value\": \"disabled\""
47 " }," 49 " },"
48 " \"Second\": {" 50 " \"Second\": {"
49 " \"Value\": \"maybe\"," 51 " \"Value\": \"maybe\","
50 " \"Level\": \"Recommended\"" 52 " \"Level\": \"Recommended\""
51 " }" 53 " }"
52 "}"; 54 "}";
53 55
54 class MockComponentCloudPolicyStoreDelegate 56 class MockComponentCloudPolicyStoreDelegate
55 : public ComponentCloudPolicyStore::Delegate { 57 : public ComponentCloudPolicyStore::Delegate {
56 public: 58 public:
57 virtual ~MockComponentCloudPolicyStoreDelegate() {} 59 virtual ~MockComponentCloudPolicyStoreDelegate() {}
58 60
59 MOCK_METHOD0(OnComponentCloudPolicyStoreUpdated, void()); 61 MOCK_METHOD0(OnComponentCloudPolicyStoreUpdated, void());
60 }; 62 };
61 63
62 } // namespace 64 } // namespace
63 65
64 class ComponentCloudPolicyUpdaterTest : public testing::Test { 66 class ComponentCloudPolicyUpdaterTest : public testing::Test {
65 protected: 67 protected:
66 virtual void SetUp() OVERRIDE { 68 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 69
80 builder_.policy_data().set_policy_type( 70 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 71
119 base::ScopedTempDir temp_dir_; 72 base::ScopedTempDir temp_dir_;
120 scoped_ptr<ResourceCache> cache_; 73 scoped_ptr<ResourceCache> cache_;
121 scoped_ptr<ComponentCloudPolicyStore> store_; 74 scoped_ptr<ComponentCloudPolicyStore> store_;
122 MockComponentCloudPolicyStoreDelegate store_delegate_; 75 MockComponentCloudPolicyStoreDelegate store_delegate_;
123 net::TestURLFetcherFactory fetcher_factory_; 76 net::TestURLFetcherFactory fetcher_factory_;
124 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; 77 scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
125 scoped_ptr<ComponentCloudPolicyUpdater> updater_; 78 scoped_ptr<ComponentCloudPolicyUpdater> updater_;
126 ComponentPolicyBuilder builder_; 79 ComponentPolicyBuilder builder_;
127 PolicyBundle expected_bundle_; 80 PolicyBundle expected_bundle_;
128 }; 81 };
129 82
83 void ComponentCloudPolicyUpdaterTest::SetUp() {
84 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
85 cache_.reset(new ResourceCache(temp_dir_.path()));
86 store_.reset(new ComponentCloudPolicyStore(&store_delegate_, cache_.get()));
87 store_->SetCredentials(ComponentPolicyBuilder::kFakeUsername,
88 ComponentPolicyBuilder::kFakeToken);
89 fetcher_factory_.set_remove_fetcher_on_delete(true);
90 task_runner_ = new base::TestSimpleTaskRunner();
91 updater_.reset(new ComponentCloudPolicyUpdater(
92 task_runner_,
93 scoped_refptr<net::URLRequestContextGetter>(),
94 store_.get()));
95 ASSERT_EQ(store_->policy().end(), store_->policy().begin());
96
97 builder_.policy_data().set_policy_type(
98 dm_protocol::kChromeExtensionPolicyType);
99 builder_.policy_data().set_settings_entity_id(kTestExtension);
100 builder_.payload().set_download_url(kTestDownload);
101 builder_.payload().set_secure_hash(base::SHA1HashString(kTestPolicy));
102
103 PolicyNamespace ns(POLICY_DOMAIN_EXTENSIONS, kTestExtension);
104 PolicyMap& policy = expected_bundle_.Get(ns);
105 policy.Set("Name", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
106 base::Value::CreateStringValue("disabled"));
107 policy.Set("Second", POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_USER,
108 base::Value::CreateStringValue("maybe"));
109 }
110
111 scoped_ptr<em::PolicyFetchResponse>
112 ComponentCloudPolicyUpdaterTest::CreateResponse() {
113 builder_.Build();
114 return make_scoped_ptr(new em::PolicyFetchResponse(builder_.policy()));
115 }
116
130 TEST_F(ComponentCloudPolicyUpdaterTest, FetchAndCache) { 117 TEST_F(ComponentCloudPolicyUpdaterTest, FetchAndCache) {
131 StartTwoFetches(); 118 // Submit a policy fetch response.
132 119 updater_->UpdateExternalPolicy(CreateResponse());
133 // The first fetch was scheduled; complete it. 120
134 net::TestURLFetcher* fetcher = GetCurrentFetcher(); 121 // Verify that a download has been started.
135 ASSERT_TRUE(fetcher); 122 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
136 EXPECT_EQ(GURL(kTestDownload), fetcher->GetOriginalURL()); 123 ASSERT_TRUE(fetcher);
124 EXPECT_EQ(GURL(kTestDownload), fetcher->GetOriginalURL());
125
126 // Complete the download.
137 fetcher->set_response_code(200); 127 fetcher->set_response_code(200);
138 fetcher->SetResponseString(kTestPolicy); 128 fetcher->SetResponseString(kTestPolicy);
139 EXPECT_CALL(store_delegate_, OnComponentCloudPolicyStoreUpdated()); 129 EXPECT_CALL(store_delegate_, OnComponentCloudPolicyStoreUpdated());
140 fetcher->delegate()->OnURLFetchComplete(fetcher); 130 fetcher->delegate()->OnURLFetchComplete(fetcher);
141 Mock::VerifyAndClearExpectations(&store_delegate_); 131 Mock::VerifyAndClearExpectations(&store_delegate_);
142 132
143 // The second fetch was scheduled. 133 // Verify that the download is no longer running.
144 fetcher = GetCurrentFetcher(); 134 ASSERT_FALSE(fetcher_factory_.GetFetcherByID(0));
145 ASSERT_TRUE(fetcher); 135
146 EXPECT_EQ(GURL(kTestDownload2), fetcher->GetOriginalURL()); 136 // Verify that the downloaded policy is being served.
147
148 // The policy is being served.
149 EXPECT_TRUE(store_->policy().Equals(expected_bundle_)); 137 EXPECT_TRUE(store_->policy().Equals(expected_bundle_));
150 138 }
151 // No retries have been scheduled. 139
152 EXPECT_TRUE(task_runner_->GetPendingTasks().empty()); 140 TEST_F(ComponentCloudPolicyUpdaterTest, PolicyFetchResponseTooLarge) {
153 } 141 // 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="); 142 std::string long_download("http://example.com/get?id=");
157 long_download.append(20 * 1024, '1'); 143 long_download.append(20 * 1024, '1');
158 builder_.payload().set_download_url(long_download); 144 builder_.payload().set_download_url(long_download);
159 updater_->UpdateExternalPolicy(CreateResponse()); 145 updater_->UpdateExternalPolicy(CreateResponse());
160 146
147 // Submit two valid policy fetch responses.
161 builder_.policy_data().set_settings_entity_id(kTestExtension2); 148 builder_.policy_data().set_settings_entity_id(kTestExtension2);
162 builder_.payload().set_download_url(kTestDownload2); 149 builder_.payload().set_download_url(kTestDownload2);
163 updater_->UpdateExternalPolicy(CreateResponse()); 150 updater_->UpdateExternalPolicy(CreateResponse());
164 151 builder_.policy_data().set_settings_entity_id(kTestExtension3);
165 // The first request was dropped, and the second was started. 152 builder_.payload().set_download_url(kTestDownload3);
166 net::TestURLFetcher* fetcher = GetCurrentFetcher(); 153 updater_->UpdateExternalPolicy(CreateResponse());
167 ASSERT_TRUE(fetcher); 154
168 EXPECT_EQ(GURL(kTestDownload2), fetcher->GetOriginalURL()); 155 // Verify that the first policy fetch response has been ignored and downloads
169 156 // have been started for the next two fetch responses instead.
170 // No retry is scheduled for the first request, because the policy was 157 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
171 // rejected (too large). 158 ASSERT_TRUE(fetcher);
172 EXPECT_TRUE(task_runner_->GetPendingTasks().empty()); 159 EXPECT_EQ(GURL(kTestDownload2), fetcher->GetOriginalURL());
173 } 160 fetcher = fetcher_factory_.GetFetcherByID(1);
174 161 ASSERT_TRUE(fetcher);
175 TEST_F(ComponentCloudPolicyUpdaterTest, InvalidPolicy) { 162 EXPECT_EQ(GURL(kTestDownload3), fetcher->GetOriginalURL());
163 }
164
165 TEST_F(ComponentCloudPolicyUpdaterTest, PolicyFetchResponseInvalid) {
166 // Submit an invalid policy fetch response.
176 builder_.policy_data().set_username("wronguser@example.com"); 167 builder_.policy_data().set_username("wronguser@example.com");
177 updater_->UpdateExternalPolicy(CreateResponse()); 168 updater_->UpdateExternalPolicy(CreateResponse());
178 169
170 // Submit two valid policy fetch responses.
179 builder_.policy_data().set_username(ComponentPolicyBuilder::kFakeUsername); 171 builder_.policy_data().set_username(ComponentPolicyBuilder::kFakeUsername);
180 builder_.policy_data().set_settings_entity_id(kTestExtension2); 172 builder_.policy_data().set_settings_entity_id(kTestExtension2);
181 builder_.payload().set_download_url(kTestDownload2); 173 builder_.payload().set_download_url(kTestDownload2);
182 updater_->UpdateExternalPolicy(CreateResponse()); 174 updater_->UpdateExternalPolicy(CreateResponse());
183 175 builder_.policy_data().set_settings_entity_id(kTestExtension3);
184 // The first request was dropped, and the second was started. 176 builder_.payload().set_download_url(kTestDownload3);
185 net::TestURLFetcher* fetcher = GetCurrentFetcher(); 177 updater_->UpdateExternalPolicy(CreateResponse());
186 ASSERT_TRUE(fetcher); 178
187 EXPECT_EQ(GURL(kTestDownload2), fetcher->GetOriginalURL()); 179 // Verify that the first policy fetch response has been ignored and downloads
188 180 // have been started for the next two fetch responses instead.
189 // No retry is scheduled for the first request, because the policy was 181 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
190 // rejected (invalid). 182 ASSERT_TRUE(fetcher);
191 EXPECT_TRUE(task_runner_->GetPendingTasks().empty()); 183 EXPECT_EQ(GURL(kTestDownload2), fetcher->GetOriginalURL());
184 fetcher = fetcher_factory_.GetFetcherByID(1);
185 ASSERT_TRUE(fetcher);
186 EXPECT_EQ(GURL(kTestDownload3), fetcher->GetOriginalURL());
192 } 187 }
193 188
194 TEST_F(ComponentCloudPolicyUpdaterTest, AlreadyCached) { 189 TEST_F(ComponentCloudPolicyUpdaterTest, AlreadyCached) {
190 // Cache policy for an extension.
191 builder_.Build();
195 PolicyNamespace ns(POLICY_DOMAIN_EXTENSIONS, kTestExtension); 192 PolicyNamespace ns(POLICY_DOMAIN_EXTENSIONS, kTestExtension);
196 EXPECT_CALL(store_delegate_, OnComponentCloudPolicyStoreUpdated()); 193 EXPECT_CALL(store_delegate_, OnComponentCloudPolicyStoreUpdated());
197 EXPECT_TRUE(store_->Store(ns, 194 EXPECT_TRUE(store_->Store(ns,
198 CreateSerializedResponse(), 195 builder_.GetBlob(),
199 base::SHA1HashString(kTestPolicy), 196 base::SHA1HashString(kTestPolicy),
200 kTestPolicy)); 197 kTestPolicy));
201 Mock::VerifyAndClearExpectations(&store_delegate_); 198 Mock::VerifyAndClearExpectations(&store_delegate_);
202 199
203 // Seeing the same policy data again won't trigger a new fetch. 200 // Submit a policy fetch response whose extension ID and hash match the
204 updater_->UpdateExternalPolicy(CreateResponse()); 201 // already cached policy.
205 EXPECT_FALSE(GetCurrentFetcher()); 202 updater_->UpdateExternalPolicy(CreateResponse());
206 203
207 // And no retry is scheduled either. 204 // Verify that no download has been started.
208 EXPECT_TRUE(task_runner_->GetPendingTasks().empty()); 205 EXPECT_FALSE(fetcher_factory_.GetFetcherByID(0));
209 } 206 }
210 207
211 TEST_F(ComponentCloudPolicyUpdaterTest, LargeFetch) { 208 TEST_F(ComponentCloudPolicyUpdaterTest, PolicyDataInvalid) {
212 StartTwoFetches(); 209 // Submit two policy fetch responses.
213 210 updater_->UpdateExternalPolicy(CreateResponse());
214 net::TestURLFetcher* fetcher = GetCurrentFetcher(); 211 builder_.payload().set_download_url(kTestDownload2);
215 ASSERT_TRUE(fetcher); 212 builder_.policy_data().set_settings_entity_id(kTestExtension2);
216 EXPECT_EQ(GURL(kTestDownload), fetcher->GetOriginalURL()); 213 updater_->UpdateExternalPolicy(CreateResponse());
214
215 // Verify that the first download has been started.
216 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
217 ASSERT_TRUE(fetcher);
218 EXPECT_EQ(GURL(kTestDownload), fetcher->GetOriginalURL());
219
220 // Indicate that the policy data size will exceed allowed maximum.
217 fetcher->delegate()->OnURLFetchDownloadProgress(fetcher, 6 * 1024 * 1024, -1); 221 fetcher->delegate()->OnURLFetchDownloadProgress(fetcher, 6 * 1024 * 1024, -1);
218 222
219 // The second fetch was scheduled next. 223 // Verify that the first download is no longer running.
220 fetcher = GetCurrentFetcher(); 224 ASSERT_FALSE(fetcher_factory_.GetFetcherByID(0));
221 ASSERT_TRUE(fetcher); 225
222 EXPECT_EQ(GURL(kTestDownload2), fetcher->GetOriginalURL()); 226 // Verify that the second download has been started.
223 227 fetcher = fetcher_factory_.GetFetcherByID(1);
224 // A retry is scheduled for the first fetcher. 228 ASSERT_TRUE(fetcher);
225 EXPECT_EQ(1u, task_runner_->GetPendingTasks().size()); 229 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 } 230 }
357 231
358 TEST_F(ComponentCloudPolicyUpdaterTest, FetchUpdatedData) { 232 TEST_F(ComponentCloudPolicyUpdaterTest, FetchUpdatedData) {
359 updater_->UpdateExternalPolicy(CreateResponse()); 233 // Submit a policy fetch response.
360 234 updater_->UpdateExternalPolicy(CreateResponse());
361 // Same extension, but the download location has changed. 235
362 builder_.payload().set_download_url(kTestDownload2); 236 // Verify that the first download has been started.
363 updater_->UpdateExternalPolicy(CreateResponse()); 237 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
364 238 ASSERT_TRUE(fetcher);
365 // The first was cancelled and overridden by the second. 239 EXPECT_EQ(GURL(kTestDownload), fetcher->GetOriginalURL());
366 net::TestURLFetcher* fetcher = GetCurrentFetcher(); 240
367 ASSERT_TRUE(fetcher); 241 // Submit a second policy fetch response for the same extension with an
368 EXPECT_EQ(GURL(kTestDownload2), fetcher->GetOriginalURL()); 242 // updated download URL.
369 243 builder_.payload().set_download_url(kTestDownload2);
370 // No retries are scheduled. 244 updater_->UpdateExternalPolicy(CreateResponse());
371 EXPECT_TRUE(task_runner_->GetPendingTasks().empty()); 245
246 // Verify that the first download is no longer running.
247 ASSERT_FALSE(fetcher_factory_.GetFetcherByID(0));
248
249 // Verify that the second download has been started.
250 fetcher = fetcher_factory_.GetFetcherByID(1);
251 ASSERT_TRUE(fetcher);
252 EXPECT_EQ(GURL(kTestDownload2), fetcher->GetOriginalURL());
372 } 253 }
373 254
374 TEST_F(ComponentCloudPolicyUpdaterTest, FetchUpdatedDataWithoutPolicy) { 255 TEST_F(ComponentCloudPolicyUpdaterTest, FetchUpdatedDataWithoutPolicy) {
375 // Fetch the initial policy data. 256 // Submit a policy fetch response.
376 updater_->UpdateExternalPolicy(CreateResponse()); 257 updater_->UpdateExternalPolicy(CreateResponse());
377 258
378 net::TestURLFetcher* fetcher = GetCurrentFetcher(); 259 // Verify that the download has been started.
379 ASSERT_TRUE(fetcher); 260 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0);
380 EXPECT_EQ(GURL(kTestDownload), fetcher->GetOriginalURL()); 261 ASSERT_TRUE(fetcher);
262 EXPECT_EQ(GURL(kTestDownload), fetcher->GetOriginalURL());
263
264 // Complete the download.
381 fetcher->set_response_code(200); 265 fetcher->set_response_code(200);
382 fetcher->SetResponseString(kTestPolicy); 266 fetcher->SetResponseString(kTestPolicy);
383 EXPECT_CALL(store_delegate_, OnComponentCloudPolicyStoreUpdated()); 267 EXPECT_CALL(store_delegate_, OnComponentCloudPolicyStoreUpdated());
384 fetcher->delegate()->OnURLFetchComplete(fetcher); 268 fetcher->delegate()->OnURLFetchComplete(fetcher);
385 Mock::VerifyAndClearExpectations(&store_delegate_); 269 Mock::VerifyAndClearExpectations(&store_delegate_);
386 EXPECT_FALSE(GetCurrentFetcher()); 270
387 271 // Verify that the download is no longer running.
388 // The policy is being served. 272 EXPECT_FALSE(fetcher_factory_.GetFetcherByID(0));
273
274 // Verify that the downloaded policy is being served.
389 EXPECT_TRUE(store_->policy().Equals(expected_bundle_)); 275 EXPECT_TRUE(store_->policy().Equals(expected_bundle_));
390 276
391 // Same extension, but the download location has changed and is empty, meaning 277 // Submit a second policy fetch response for the same extension with no
392 // that no policy should be served anymore. 278 // download URL, meaning that no policy should be provided for this extension.
393 EXPECT_CALL(store_delegate_, OnComponentCloudPolicyStoreUpdated());
394 builder_.payload().clear_download_url(); 279 builder_.payload().clear_download_url();
395 builder_.payload().clear_secure_hash(); 280 builder_.payload().clear_secure_hash();
396 updater_->UpdateExternalPolicy(CreateResponse()); 281 EXPECT_CALL(store_delegate_, OnComponentCloudPolicyStoreUpdated());
397 Mock::VerifyAndClearExpectations(&store_delegate_); 282 updater_->UpdateExternalPolicy(CreateResponse());
398 283 Mock::VerifyAndClearExpectations(&store_delegate_);
399 // No fetcher was started for that. 284
400 EXPECT_FALSE(GetCurrentFetcher()); 285 // Verify that no download has been started.
401 286 EXPECT_FALSE(fetcher_factory_.GetFetcherByID(1));
402 // And the policy has been removed. 287
288 // Verify that the policy is no longer being served.
403 const PolicyBundle empty_bundle; 289 const PolicyBundle empty_bundle;
404 EXPECT_TRUE(store_->policy().Equals(empty_bundle)); 290 EXPECT_TRUE(store_->policy().Equals(empty_bundle));
405 } 291 }
406 292
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) { 293 TEST_F(ComponentCloudPolicyUpdaterTest, NoPolicy) {
432 // Start a fetch with a valid download url. 294 // Submit a policy fetch response with a valid download URL.
433 updater_->UpdateExternalPolicy(CreateResponse()); 295 updater_->UpdateExternalPolicy(CreateResponse());
434 net::TestURLFetcher* fetcher = GetCurrentFetcher(); 296
435 ASSERT_TRUE(fetcher); 297 // Verify that the download has been started.
436 298 ASSERT_TRUE(fetcher_factory_.GetFetcherByID(0));
437 // Now update the policy fetch response before the fetch completes. The new 299
438 // data does not have a download url. 300 // Update the policy fetch response before the download has finished. The new
301 // policy fetch response has no download URL.
439 builder_.payload().Clear(); 302 builder_.payload().Clear();
440 updater_->UpdateExternalPolicy(CreateResponse()); 303 updater_->UpdateExternalPolicy(CreateResponse());
441 304
442 // The download has been cancelled. 305 // Verify that the download is no longer running.
443 fetcher = GetCurrentFetcher(); 306 ASSERT_FALSE(fetcher_factory_.GetFetcherByID(0));
444 ASSERT_FALSE(fetcher);
445 } 307 }
446 308
447 } // namespace policy 309 } // namespace policy
OLDNEW
« no previous file with comments | « chrome/browser/policy/cloud/component_cloud_policy_updater.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698