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

Side by Side Diff: components/update_client/update_checker_unittest.cc

Issue 1861383004: Add module for counting date-last-roll-call and persisting those counts (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: ASAN Created 4 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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 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 "base/bind.h" 5 #include "base/bind.h"
6 #include "base/bind_helpers.h" 6 #include "base/bind_helpers.h"
7 #include "base/files/file_util.h" 7 #include "base/files/file_util.h"
8 #include "base/macros.h" 8 #include "base/macros.h"
9 #include "base/memory/ref_counted.h" 9 #include "base/memory/ref_counted.h"
10 #include "base/memory/scoped_ptr.h" 10 #include "base/memory/scoped_ptr.h"
11 #include "base/path_service.h" 11 #include "base/path_service.h"
12 #include "base/run_loop.h" 12 #include "base/run_loop.h"
13 #include "base/thread_task_runner_handle.h" 13 #include "base/thread_task_runner_handle.h"
14 #include "base/version.h" 14 #include "base/version.h"
15 #include "components/prefs/testing_pref_service.h"
15 #include "components/update_client/crx_update_item.h" 16 #include "components/update_client/crx_update_item.h"
17 #include "components/update_client/persisted_data.h"
16 #include "components/update_client/test_configurator.h" 18 #include "components/update_client/test_configurator.h"
17 #include "components/update_client/update_checker.h" 19 #include "components/update_client/update_checker.h"
18 #include "components/update_client/url_request_post_interceptor.h" 20 #include "components/update_client/url_request_post_interceptor.h"
19 #include "net/url_request/url_request_test_util.h" 21 #include "net/url_request/url_request_test_util.h"
20 #include "testing/gtest/include/gtest/gtest.h" 22 #include "testing/gtest/include/gtest/gtest.h"
21 #include "url/gurl.h" 23 #include "url/gurl.h"
22 24
23 using std::string; 25 using std::string;
24 26
25 namespace update_client { 27 namespace update_client {
(...skipping 26 matching lines...) Expand all
52 int retry_after_sec); 54 int retry_after_sec);
53 55
54 protected: 56 protected:
55 void Quit(); 57 void Quit();
56 void RunThreads(); 58 void RunThreads();
57 void RunThreadsUntilIdle(); 59 void RunThreadsUntilIdle();
58 60
59 CrxUpdateItem BuildCrxUpdateItem(); 61 CrxUpdateItem BuildCrxUpdateItem();
60 62
61 scoped_refptr<TestConfigurator> config_; 63 scoped_refptr<TestConfigurator> config_;
64 scoped_ptr<TestingPrefServiceSimple> pref_;
65 scoped_refptr<PersistedData> metadata_;
62 66
63 scoped_ptr<UpdateChecker> update_checker_; 67 scoped_ptr<UpdateChecker> update_checker_;
64 68
65 scoped_ptr<InterceptorFactory> interceptor_factory_; 69 scoped_ptr<InterceptorFactory> interceptor_factory_;
66 URLRequestPostInterceptor* post_interceptor_; // Owned by the factory. 70 URLRequestPostInterceptor* post_interceptor_; // Owned by the factory.
67 71
68 int error_; 72 int error_;
69 UpdateResponse::Results results_; 73 UpdateResponse::Results results_;
70 74
71 private: 75 private:
72 base::MessageLoopForIO loop_; 76 base::MessageLoopForIO loop_;
73 base::Closure quit_closure_; 77 base::Closure quit_closure_;
74 78
75 DISALLOW_COPY_AND_ASSIGN(UpdateCheckerTest); 79 DISALLOW_COPY_AND_ASSIGN(UpdateCheckerTest);
76 }; 80 };
77 81
78 UpdateCheckerTest::UpdateCheckerTest() : post_interceptor_(NULL), error_(0) { 82 UpdateCheckerTest::UpdateCheckerTest() : post_interceptor_(NULL), error_(0) {
79 } 83 }
80 84
81 UpdateCheckerTest::~UpdateCheckerTest() { 85 UpdateCheckerTest::~UpdateCheckerTest() {
82 } 86 }
83 87
84 void UpdateCheckerTest::SetUp() { 88 void UpdateCheckerTest::SetUp() {
85 config_ = new TestConfigurator(base::ThreadTaskRunnerHandle::Get(), 89 config_ = new TestConfigurator(base::ThreadTaskRunnerHandle::Get(),
86 base::ThreadTaskRunnerHandle::Get()); 90 base::ThreadTaskRunnerHandle::Get());
91 pref_.reset(new TestingPrefServiceSimple());
92 RegisterPersistedDataPreferences(pref_->registry());
93 metadata_ = new PersistedData(pref_.get());
87 interceptor_factory_.reset( 94 interceptor_factory_.reset(
88 new InterceptorFactory(base::ThreadTaskRunnerHandle::Get())); 95 new InterceptorFactory(base::ThreadTaskRunnerHandle::Get()));
89 post_interceptor_ = interceptor_factory_->CreateInterceptor(); 96 post_interceptor_ = interceptor_factory_->CreateInterceptor();
90 EXPECT_TRUE(post_interceptor_); 97 EXPECT_TRUE(post_interceptor_);
91 98
92 update_checker_.reset(); 99 update_checker_.reset();
93 100
94 error_ = 0; 101 error_ = 0;
95 results_ = UpdateResponse::Results(); 102 results_ = UpdateResponse::Results();
96 } 103 }
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
151 crx_update_item.id = "jebgalgnebhfojomionfpkfelancnnkf"; 158 crx_update_item.id = "jebgalgnebhfojomionfpkfelancnnkf";
152 crx_update_item.component = crx_component; 159 crx_update_item.component = crx_component;
153 160
154 return crx_update_item; 161 return crx_update_item;
155 } 162 }
156 163
157 TEST_F(UpdateCheckerTest, UpdateCheckSuccess) { 164 TEST_F(UpdateCheckerTest, UpdateCheckSuccess) {
158 EXPECT_TRUE(post_interceptor_->ExpectRequest( 165 EXPECT_TRUE(post_interceptor_->ExpectRequest(
159 new PartialMatch("updatecheck"), test_file("updatecheck_reply_1.xml"))); 166 new PartialMatch("updatecheck"), test_file("updatecheck_reply_1.xml")));
160 167
161 update_checker_ = UpdateChecker::Create(config_); 168 update_checker_ = UpdateChecker::Create(config_, metadata_);
162 169
163 CrxUpdateItem item(BuildCrxUpdateItem()); 170 CrxUpdateItem item(BuildCrxUpdateItem());
164 std::vector<CrxUpdateItem*> items_to_check; 171 std::vector<CrxUpdateItem*> items_to_check;
165 items_to_check.push_back(&item); 172 items_to_check.push_back(&item);
166 173
167 update_checker_->CheckForUpdates( 174 update_checker_->CheckForUpdates(
168 items_to_check, "extra=\"params\"", 175 items_to_check, "extra=\"params\"",
169 base::Bind(&UpdateCheckerTest::UpdateCheckComplete, 176 base::Bind(&UpdateCheckerTest::UpdateCheckComplete,
170 base::Unretained(this))); 177 base::Unretained(this)));
171 178
172 RunThreads(); 179 RunThreads();
173 180
174 EXPECT_EQ(1, post_interceptor_->GetHitCount()) 181 EXPECT_EQ(1, post_interceptor_->GetHitCount())
175 << post_interceptor_->GetRequestsAsString(); 182 << post_interceptor_->GetRequestsAsString();
176 ASSERT_EQ(1, post_interceptor_->GetCount()) 183 ASSERT_EQ(1, post_interceptor_->GetCount())
177 << post_interceptor_->GetRequestsAsString(); 184 << post_interceptor_->GetRequestsAsString();
178 185
179 // Sanity check the request. 186 // Sanity check the request.
180 EXPECT_NE(string::npos, post_interceptor_->GetRequests()[0].find( 187 EXPECT_NE(string::npos, post_interceptor_->GetRequests()[0].find(
181 "request protocol=\"3.0\" extra=\"params\"")); 188 "request protocol=\"3.0\" extra=\"params\""));
182 // The request must not contain any "dlpref" in the default case. 189 // The request must not contain any "dlpref" in the default case.
183 EXPECT_EQ(string::npos, 190 EXPECT_EQ(string::npos,
184 post_interceptor_->GetRequests()[0].find(" dlpref=\"")); 191 post_interceptor_->GetRequests()[0].find(" dlpref=\""));
185 EXPECT_NE( 192 EXPECT_NE(
186 string::npos, 193 string::npos,
187 post_interceptor_->GetRequests()[0].find( 194 post_interceptor_->GetRequests()[0].find(
188 "app appid=\"jebgalgnebhfojomionfpkfelancnnkf\" version=\"0.9\" " 195 "app appid=\"jebgalgnebhfojomionfpkfelancnnkf\" version=\"0.9\" "
189 "brand=\"TEST\"><updatecheck />" 196 "brand=\"TEST\"><updatecheck /><ping rd=\"-2\" />"
190 "<packages><package fp=\"fp1\"/></packages></app>")); 197 "<packages><package fp=\"fp1\"/></packages></app>"));
191 198
192 EXPECT_NE(string::npos, 199 EXPECT_NE(string::npos,
193 post_interceptor_->GetRequests()[0].find("<hw physmemory=")); 200 post_interceptor_->GetRequests()[0].find("<hw physmemory="));
194 201
195 // Sanity check the arguments of the callback after parsing. 202 // Sanity check the arguments of the callback after parsing.
196 EXPECT_EQ(0, error_); 203 EXPECT_EQ(0, error_);
197 EXPECT_EQ(1ul, results_.list.size()); 204 EXPECT_EQ(1ul, results_.list.size());
198 EXPECT_STREQ("jebgalgnebhfojomionfpkfelancnnkf", 205 EXPECT_STREQ("jebgalgnebhfojomionfpkfelancnnkf",
199 results_.list[0].extension_id.c_str()); 206 results_.list[0].extension_id.c_str());
200 EXPECT_STREQ("1.0", results_.list[0].manifest.version.c_str()); 207 EXPECT_STREQ("1.0", results_.list[0].manifest.version.c_str());
201 } 208 }
202 209
203 TEST_F(UpdateCheckerTest, UpdateCheckSuccessNoBrand) { 210 TEST_F(UpdateCheckerTest, UpdateCheckSuccessNoBrand) {
204 EXPECT_TRUE(post_interceptor_->ExpectRequest( 211 EXPECT_TRUE(post_interceptor_->ExpectRequest(
205 new PartialMatch("updatecheck"), test_file("updatecheck_reply_1.xml"))); 212 new PartialMatch("updatecheck"), test_file("updatecheck_reply_1.xml")));
206 213
207 config_->SetBrand("TOOLONG"); // Sets an invalid brand code. 214 config_->SetBrand("TOOLONG"); // Sets an invalid brand code.
208 update_checker_ = UpdateChecker::Create(config_); 215 update_checker_ = UpdateChecker::Create(config_, metadata_);
209 216
210 CrxUpdateItem item(BuildCrxUpdateItem()); 217 CrxUpdateItem item(BuildCrxUpdateItem());
211 std::vector<CrxUpdateItem*> items_to_check; 218 std::vector<CrxUpdateItem*> items_to_check;
212 items_to_check.push_back(&item); 219 items_to_check.push_back(&item);
213 220
214 update_checker_->CheckForUpdates( 221 update_checker_->CheckForUpdates(
215 items_to_check, "", base::Bind(&UpdateCheckerTest::UpdateCheckComplete, 222 items_to_check, "", base::Bind(&UpdateCheckerTest::UpdateCheckComplete,
216 base::Unretained(this))); 223 base::Unretained(this)));
217 224
218 RunThreads(); 225 RunThreads();
219 226
220 EXPECT_NE( 227 EXPECT_NE(
221 string::npos, 228 string::npos,
222 post_interceptor_->GetRequests()[0].find( 229 post_interceptor_->GetRequests()[0].find(
223 "app appid=\"jebgalgnebhfojomionfpkfelancnnkf\" version=\"0.9\">" 230 "app appid=\"jebgalgnebhfojomionfpkfelancnnkf\" version=\"0.9\">"
224 "<updatecheck /><packages><package fp=\"fp1\"/></packages></app>")); 231 "<updatecheck /><ping rd=\"-2\" /><packages><package fp=\"fp1\"/>"
232 "</packages></app>"));
225 } 233 }
226 234
227 // Simulates a 403 server response error. 235 // Simulates a 403 server response error.
228 TEST_F(UpdateCheckerTest, UpdateCheckError) { 236 TEST_F(UpdateCheckerTest, UpdateCheckError) {
229 EXPECT_TRUE( 237 EXPECT_TRUE(
230 post_interceptor_->ExpectRequest(new PartialMatch("updatecheck"), 403)); 238 post_interceptor_->ExpectRequest(new PartialMatch("updatecheck"), 403));
231 239
232 update_checker_ = UpdateChecker::Create(config_); 240 update_checker_ = UpdateChecker::Create(config_, metadata_);
233 241
234 CrxUpdateItem item(BuildCrxUpdateItem()); 242 CrxUpdateItem item(BuildCrxUpdateItem());
235 std::vector<CrxUpdateItem*> items_to_check; 243 std::vector<CrxUpdateItem*> items_to_check;
236 items_to_check.push_back(&item); 244 items_to_check.push_back(&item);
237 245
238 update_checker_->CheckForUpdates( 246 update_checker_->CheckForUpdates(
239 items_to_check, "", base::Bind(&UpdateCheckerTest::UpdateCheckComplete, 247 items_to_check, "", base::Bind(&UpdateCheckerTest::UpdateCheckComplete,
240 base::Unretained(this))); 248 base::Unretained(this)));
241 RunThreads(); 249 RunThreads();
242 250
243 EXPECT_EQ(1, post_interceptor_->GetHitCount()) 251 EXPECT_EQ(1, post_interceptor_->GetHitCount())
244 << post_interceptor_->GetRequestsAsString(); 252 << post_interceptor_->GetRequestsAsString();
245 EXPECT_EQ(1, post_interceptor_->GetCount()) 253 EXPECT_EQ(1, post_interceptor_->GetCount())
246 << post_interceptor_->GetRequestsAsString(); 254 << post_interceptor_->GetRequestsAsString();
247 255
248 EXPECT_EQ(403, error_); 256 EXPECT_EQ(403, error_);
249 EXPECT_EQ(0ul, results_.list.size()); 257 EXPECT_EQ(0ul, results_.list.size());
250 } 258 }
251 259
252 TEST_F(UpdateCheckerTest, UpdateCheckDownloadPreference) { 260 TEST_F(UpdateCheckerTest, UpdateCheckDownloadPreference) {
253 EXPECT_TRUE(post_interceptor_->ExpectRequest( 261 EXPECT_TRUE(post_interceptor_->ExpectRequest(
254 new PartialMatch("updatecheck"), test_file("updatecheck_reply_1.xml"))); 262 new PartialMatch("updatecheck"), test_file("updatecheck_reply_1.xml")));
255 263
256 config_->SetDownloadPreference(string("cacheable")); 264 config_->SetDownloadPreference(string("cacheable"));
257 265
258 update_checker_ = UpdateChecker::Create(config_); 266 update_checker_ = UpdateChecker::Create(config_, metadata_);
259 267
260 CrxUpdateItem item(BuildCrxUpdateItem()); 268 CrxUpdateItem item(BuildCrxUpdateItem());
261 std::vector<CrxUpdateItem*> items_to_check; 269 std::vector<CrxUpdateItem*> items_to_check;
262 items_to_check.push_back(&item); 270 items_to_check.push_back(&item);
263 271
264 update_checker_->CheckForUpdates( 272 update_checker_->CheckForUpdates(
265 items_to_check, "extra=\"params\"", 273 items_to_check, "extra=\"params\"",
266 base::Bind(&UpdateCheckerTest::UpdateCheckComplete, 274 base::Bind(&UpdateCheckerTest::UpdateCheckComplete,
267 base::Unretained(this))); 275 base::Unretained(this)));
268 276
269 RunThreads(); 277 RunThreads();
270 278
271 // The request must contain dlpref="cacheable". 279 // The request must contain dlpref="cacheable".
272 EXPECT_NE(string::npos, 280 EXPECT_NE(string::npos,
273 post_interceptor_->GetRequests()[0].find(" dlpref=\"cacheable\"")); 281 post_interceptor_->GetRequests()[0].find(" dlpref=\"cacheable\""));
274 } 282 }
275 283
276 // This test is checking that an update check signed with CUP fails, since there 284 // This test is checking that an update check signed with CUP fails, since there
277 // is currently no entity that can respond with a valid signed response. 285 // is currently no entity that can respond with a valid signed response.
278 // A proper CUP test requires network mocks, which are not available now. 286 // A proper CUP test requires network mocks, which are not available now.
279 TEST_F(UpdateCheckerTest, UpdateCheckCupError) { 287 TEST_F(UpdateCheckerTest, UpdateCheckCupError) {
280 EXPECT_TRUE(post_interceptor_->ExpectRequest( 288 EXPECT_TRUE(post_interceptor_->ExpectRequest(
281 new PartialMatch("updatecheck"), test_file("updatecheck_reply_1.xml"))); 289 new PartialMatch("updatecheck"), test_file("updatecheck_reply_1.xml")));
282 290
283 config_->SetUseCupSigning(true); 291 config_->SetUseCupSigning(true);
284 update_checker_ = UpdateChecker::Create(config_); 292 update_checker_ = UpdateChecker::Create(config_, metadata_);
285 293
286 CrxUpdateItem item(BuildCrxUpdateItem()); 294 CrxUpdateItem item(BuildCrxUpdateItem());
287 std::vector<CrxUpdateItem*> items_to_check; 295 std::vector<CrxUpdateItem*> items_to_check;
288 items_to_check.push_back(&item); 296 items_to_check.push_back(&item);
289 297
290 update_checker_->CheckForUpdates( 298 update_checker_->CheckForUpdates(
291 items_to_check, "", base::Bind(&UpdateCheckerTest::UpdateCheckComplete, 299 items_to_check, "", base::Bind(&UpdateCheckerTest::UpdateCheckComplete,
292 base::Unretained(this))); 300 base::Unretained(this)));
293 301
294 RunThreads(); 302 RunThreads();
295 303
296 EXPECT_EQ(1, post_interceptor_->GetHitCount()) 304 EXPECT_EQ(1, post_interceptor_->GetHitCount())
297 << post_interceptor_->GetRequestsAsString(); 305 << post_interceptor_->GetRequestsAsString();
298 ASSERT_EQ(1, post_interceptor_->GetCount()) 306 ASSERT_EQ(1, post_interceptor_->GetCount())
299 << post_interceptor_->GetRequestsAsString(); 307 << post_interceptor_->GetRequestsAsString();
300 308
301 // Sanity check the request. 309 // Sanity check the request.
302 EXPECT_NE( 310 EXPECT_NE(
303 string::npos, 311 string::npos,
304 post_interceptor_->GetRequests()[0].find( 312 post_interceptor_->GetRequests()[0].find(
305 "app appid=\"jebgalgnebhfojomionfpkfelancnnkf\" version=\"0.9\" " 313 "app appid=\"jebgalgnebhfojomionfpkfelancnnkf\" version=\"0.9\" "
306 "brand=\"TEST\"><updatecheck />" 314 "brand=\"TEST\"><updatecheck /><ping rd=\"-2\" />"
307 "<packages><package fp=\"fp1\"/></packages></app>")); 315 "<packages><package fp=\"fp1\"/></packages></app>"));
308 316
309 // Expect an error since the response is not trusted. 317 // Expect an error since the response is not trusted.
310 EXPECT_EQ(-10000, error_); 318 EXPECT_EQ(-10000, error_);
311 EXPECT_EQ(0ul, results_.list.size()); 319 EXPECT_EQ(0ul, results_.list.size());
312 } 320 }
313 321
314 // Tests that the UpdateCheckers will not make an update check for a 322 // Tests that the UpdateCheckers will not make an update check for a
315 // component that requires encryption when the update check URL is unsecure. 323 // component that requires encryption when the update check URL is unsecure.
316 TEST_F(UpdateCheckerTest, UpdateCheckRequiresEncryptionError) { 324 TEST_F(UpdateCheckerTest, UpdateCheckRequiresEncryptionError) {
317 config_->SetUpdateCheckUrl(GURL("http:\\foo\bar")); 325 config_->SetUpdateCheckUrl(GURL("http:\\foo\bar"));
318 326
319 update_checker_ = UpdateChecker::Create(config_); 327 update_checker_ = UpdateChecker::Create(config_, metadata_);
320 328
321 CrxUpdateItem item(BuildCrxUpdateItem()); 329 CrxUpdateItem item(BuildCrxUpdateItem());
322 item.component.requires_network_encryption = true; 330 item.component.requires_network_encryption = true;
323 std::vector<CrxUpdateItem*> items_to_check; 331 std::vector<CrxUpdateItem*> items_to_check;
324 items_to_check.push_back(&item); 332 items_to_check.push_back(&item);
325 333
326 update_checker_->CheckForUpdates( 334 update_checker_->CheckForUpdates(
327 items_to_check, "", base::Bind(&UpdateCheckerTest::UpdateCheckComplete, 335 items_to_check, "", base::Bind(&UpdateCheckerTest::UpdateCheckComplete,
328 base::Unretained(this))); 336 base::Unretained(this)));
329 RunThreads(); 337 RunThreads();
330 338
331 EXPECT_EQ(-1, error_); 339 EXPECT_EQ(-1, error_);
332 EXPECT_EQ(0u, results_.list.size()); 340 EXPECT_EQ(0u, results_.list.size());
333 } 341 }
334 342
343 // Tests that the PersistedData will get correctly update and reserialize
344 // the elapsed_days value.
345 TEST_F(UpdateCheckerTest, UpdateCheckDateLastRollCall) {
346 EXPECT_TRUE(post_interceptor_->ExpectRequest(
347 new PartialMatch("updatecheck"), test_file("updatecheck_reply_4.xml")));
348 EXPECT_TRUE(post_interceptor_->ExpectRequest(
349 new PartialMatch("updatecheck"), test_file("updatecheck_reply_4.xml")));
350
351 update_checker_ = UpdateChecker::Create(config_, metadata_);
352
353 CrxUpdateItem item(BuildCrxUpdateItem());
354 std::vector<CrxUpdateItem*> items_to_check;
355 items_to_check.push_back(&item);
356
357 // Do two update-checks.
358 update_checker_->CheckForUpdates(
359 items_to_check, "extra=\"params\"",
360 base::Bind(&UpdateCheckerTest::UpdateCheckComplete,
361 base::Unretained(this)));
362 RunThreads();
363 update_checker_ = UpdateChecker::Create(config_, metadata_);
364 update_checker_->CheckForUpdates(
365 items_to_check, "extra=\"params\"",
366 base::Bind(&UpdateCheckerTest::UpdateCheckComplete,
367 base::Unretained(this)));
368 RunThreads();
369
370 EXPECT_EQ(2, post_interceptor_->GetHitCount())
371 << post_interceptor_->GetRequestsAsString();
372 ASSERT_EQ(2, post_interceptor_->GetCount())
373 << post_interceptor_->GetRequestsAsString();
374 EXPECT_NE(
375 string::npos,
376 post_interceptor_->GetRequests()[0].find("<ping rd=\"-2\" />"));
377 EXPECT_NE(
378 string::npos,
379 post_interceptor_->GetRequests()[1].find("<ping rd=\"3383\" />"));
380 }
381
335 } // namespace update_client 382 } // namespace update_client
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698