Use WeakNSProtocol/WeakNSObject for WebControllerObserverBridge ivars.
[chromium-blink-merge.git] / chrome / browser / component_updater / component_updater_service_unittest.cc
blob49d86bd8eca1f780cddf8f7106a69da594abd51e
1 // Copyright 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/component_updater/component_updater_service_unittest.h"
7 #include <vector>
9 #include "base/files/file_util.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/path_service.h"
12 #include "base/run_loop.h"
13 #include "base/strings/string_number_conversions.h"
14 #include "base/strings/string_util.h"
15 #include "base/strings/stringprintf.h"
16 #include "base/values.h"
17 #include "chrome/browser/component_updater/component_updater_resource_throttle.h"
18 #include "chrome/common/chrome_paths.h"
19 #include "components/update_client/test_configurator.h"
20 #include "components/update_client/test_installer.h"
21 #include "components/update_client/url_request_post_interceptor.h"
22 #include "components/update_client/utils.h"
23 #include "content/public/browser/browser_thread.h"
24 #include "content/public/browser/resource_controller.h"
25 #include "content/public/browser/resource_request_info.h"
26 #include "content/public/browser/resource_throttle.h"
27 #include "libxml/globals.h"
28 #include "net/base/upload_bytes_element_reader.h"
29 #include "net/url_request/test_url_request_interceptor.h"
30 #include "net/url_request/url_request.h"
31 #include "net/url_request/url_request_test_util.h"
32 #include "url/gurl.h"
34 using ::testing::_;
35 using ::testing::AnyNumber;
36 using ::testing::InSequence;
37 using ::testing::Mock;
39 using content::BrowserThread;
41 using std::string;
43 using update_client::CrxComponent;
44 using update_client::PartialMatch;
45 using update_client::InterceptorFactory;
46 using update_client::TestConfigurator;
47 using update_client::TestInstaller;
48 using update_client::URLRequestPostInterceptor;
49 using update_client::VersionedTestInstaller;
51 using update_client::abag_hash;
52 using update_client::ihfo_hash;
53 using update_client::jebg_hash;
55 using Events = component_updater::ServiceObserver::Events;
56 using Status = component_updater::ComponentUpdateService::Status;
58 namespace component_updater {
60 MockServiceObserver::MockServiceObserver() {
63 MockServiceObserver::~MockServiceObserver() {
66 ComponentUpdaterTest::ComponentUpdaterTest()
67 : post_interceptor_(NULL),
68 thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP),
69 test_config_(NULL) {
70 // The component updater instance under test.
71 test_config_ = new TestConfigurator(
72 BrowserThread::GetBlockingPool()
73 ->GetSequencedTaskRunnerWithShutdownBehavior(
74 BrowserThread::GetBlockingPool()->GetSequenceToken(),
75 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN),
76 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
77 component_updater_.reset(
78 ComponentUpdateServiceFactory(test_config_).release());
81 ComponentUpdaterTest::~ComponentUpdaterTest() {
84 void ComponentUpdaterTest::SetUp() {
85 get_interceptor_.reset(new GetInterceptor(
86 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO),
87 BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior(
88 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)));
89 interceptor_factory_.reset(new InterceptorFactory(
90 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO)));
91 post_interceptor_ = interceptor_factory_->CreateInterceptor();
92 EXPECT_TRUE(post_interceptor_);
95 void ComponentUpdaterTest::TearDown() {
96 interceptor_factory_.reset();
97 get_interceptor_.reset();
98 xmlCleanupGlobals();
101 ComponentUpdateService* ComponentUpdaterTest::component_updater() {
102 return component_updater_.get();
105 // Makes the full path to a component updater test file.
106 const base::FilePath ComponentUpdaterTest::test_file(const char* file) {
107 base::FilePath path;
108 PathService::Get(base::DIR_SOURCE_ROOT, &path);
109 return path.AppendASCII("components")
110 .AppendASCII("test")
111 .AppendASCII("data")
112 .AppendASCII("update_client")
113 .AppendASCII(file);
116 scoped_refptr<update_client::TestConfigurator>
117 ComponentUpdaterTest::test_configurator() {
118 return test_config_;
121 ComponentUpdateService::Status ComponentUpdaterTest::RegisterComponent(
122 CrxComponent* com,
123 TestComponents component,
124 const Version& version,
125 const scoped_refptr<TestInstaller>& installer) {
126 switch (component) {
127 case kTestComponent_abag: {
128 com->name = "test_abag";
129 com->pk_hash.assign(abag_hash, abag_hash + arraysize(abag_hash));
130 break;
132 case kTestComponent_jebg: {
133 com->name = "test_jebg";
134 com->pk_hash.assign(jebg_hash, jebg_hash + arraysize(jebg_hash));
135 break;
137 case kTestComponent_ihfo: {
138 com->name = "test_ihfo";
139 com->pk_hash.assign(ihfo_hash, ihfo_hash + arraysize(ihfo_hash));
140 break;
143 com->version = version;
144 com->installer = installer;
145 return component_updater_->RegisterComponent(*com);
148 void ComponentUpdaterTest::RunThreads() {
149 base::RunLoop runloop;
150 test_configurator()->SetQuitClosure(runloop.QuitClosure());
151 runloop.Run();
153 // Since some tests need to drain currently enqueued tasks such as network
154 // intercepts on the IO thread, run the threads until they are
155 // idle. The component updater service won't loop again until the loop count
156 // is set and the service is started.
157 RunThreadsUntilIdle();
160 void ComponentUpdaterTest::RunThreadsUntilIdle() {
161 base::RunLoop().RunUntilIdle();
164 ComponentUpdateService::Status OnDemandTester::OnDemand(
165 ComponentUpdateService* cus,
166 const std::string& component_id) {
167 return cus->GetOnDemandUpdater().OnDemandUpdate(component_id);
170 // Verify that our test fixture work and the component updater can
171 // be created and destroyed with no side effects.
172 TEST_F(ComponentUpdaterTest, VerifyFixture) {
173 EXPECT_TRUE(component_updater() != NULL);
176 // Verify that the component updater can be caught in a quick
177 // start-shutdown situation. Failure of this test will be a crash.
178 TEST_F(ComponentUpdaterTest, StartStop) {
179 component_updater()->Start();
180 RunThreadsUntilIdle();
181 component_updater()->Stop();
184 // Verify that when the server has no updates, we go back to sleep and
185 // the COMPONENT_UPDATER_STARTED and COMPONENT_UPDATER_SLEEPING notifications
186 // are generated. No pings are sent.
187 TEST_F(ComponentUpdaterTest, CheckCrxSleep) {
188 MockServiceObserver observer;
190 EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATER_STARTED, ""))
191 .Times(1);
192 EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATER_SLEEPING, ""))
193 .Times(2);
194 EXPECT_CALL(observer, OnEvent(Events::COMPONENT_NOT_UPDATED,
195 "abagagagagagagagagagagagagagagag")).Times(2);
197 EXPECT_TRUE(post_interceptor_->ExpectRequest(
198 new PartialMatch("updatecheck"), test_file("updatecheck_reply_1.xml")));
199 EXPECT_TRUE(post_interceptor_->ExpectRequest(
200 new PartialMatch("updatecheck"), test_file("updatecheck_reply_1.xml")));
202 scoped_refptr<TestInstaller> installer(new TestInstaller);
203 CrxComponent com;
204 component_updater()->AddObserver(&observer);
205 EXPECT_EQ(Status::kOk, RegisterComponent(&com, kTestComponent_abag,
206 Version("1.1"), installer));
208 // We loop twice, but there are no updates so we expect two sleep messages.
209 test_configurator()->SetLoopCount(2);
210 component_updater()->Start();
211 RunThreads();
213 EXPECT_EQ(0, installer->error());
214 EXPECT_EQ(0, installer->install_count());
216 // Expect to see the two update check requests and no other requests,
217 // including pings.
218 EXPECT_EQ(2, post_interceptor_->GetHitCount())
219 << post_interceptor_->GetRequestsAsString();
220 EXPECT_EQ(2, post_interceptor_->GetCount())
221 << post_interceptor_->GetRequestsAsString();
222 EXPECT_NE(
223 string::npos,
224 post_interceptor_->GetRequests()[0].find(
225 "<app appid=\"abagagagagagagagagagagagagagagag\" version=\"1.1\">"
226 "<updatecheck /></app>"))
227 << post_interceptor_->GetRequestsAsString();
228 EXPECT_NE(
229 string::npos,
230 post_interceptor_->GetRequests()[1].find(
231 "<app appid=\"abagagagagagagagagagagagagagagag\" version=\"1.1\">"
232 "<updatecheck /></app>"))
233 << post_interceptor_->GetRequestsAsString();
235 component_updater()->Stop();
237 // Loop twice again but this case we simulate a server error by returning
238 // an empty file. Expect the behavior of the service to be the same as before.
239 EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATER_STARTED, ""))
240 .Times(1);
241 EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATER_SLEEPING, ""))
242 .Times(2);
243 EXPECT_CALL(observer, OnEvent(Events::COMPONENT_NOT_UPDATED,
244 "abagagagagagagagagagagagagagagag")).Times(2);
246 post_interceptor_->Reset();
247 EXPECT_TRUE(post_interceptor_->ExpectRequest(
248 new PartialMatch("updatecheck"), test_file("updatecheck_reply_empty")));
249 EXPECT_TRUE(post_interceptor_->ExpectRequest(
250 new PartialMatch("updatecheck"), test_file("updatecheck_reply_empty")));
252 test_configurator()->SetLoopCount(2);
253 component_updater()->Start();
254 RunThreads();
256 EXPECT_EQ(0, installer->error());
257 EXPECT_EQ(0, installer->install_count());
259 EXPECT_EQ(2, post_interceptor_->GetHitCount())
260 << post_interceptor_->GetRequestsAsString();
261 EXPECT_EQ(2, post_interceptor_->GetCount())
262 << post_interceptor_->GetRequestsAsString();
263 EXPECT_NE(
264 string::npos,
265 post_interceptor_->GetRequests()[0].find(
266 "<app appid=\"abagagagagagagagagagagagagagagag\" version=\"1.1\">"
267 "<updatecheck /></app>"))
268 << post_interceptor_->GetRequestsAsString();
269 EXPECT_NE(
270 string::npos,
271 post_interceptor_->GetRequests()[1].find(
272 "<app appid=\"abagagagagagagagagagagagagagagag\" version=\"1.1\">"
273 "<updatecheck /></app>"))
274 << post_interceptor_->GetRequestsAsString();
276 component_updater()->Stop();
279 // Verify that we can check for updates and install one component. Besides
280 // the notifications above COMPONENT_UPDATE_FOUND and COMPONENT_UPDATE_READY
281 // should have been fired. We do two loops so the second time around there
282 // should be nothing left to do.
283 // We also check that the following network requests are issued:
284 // 1- update check
285 // 2- download crx
286 // 3- ping
287 // 4- second update check.
288 TEST_F(ComponentUpdaterTest, InstallCrx) {
289 MockServiceObserver observer;
291 InSequence seq;
292 EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATER_STARTED, ""))
293 .Times(1);
294 EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATE_FOUND,
295 "jebgalgnebhfojomionfpkfelancnnkf")).Times(1);
296 EXPECT_CALL(observer, OnEvent(Events::COMPONENT_NOT_UPDATED,
297 "abagagagagagagagagagagagagagagag")).Times(1);
298 EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATE_DOWNLOADING,
299 "jebgalgnebhfojomionfpkfelancnnkf"))
300 .Times(AnyNumber());
301 EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATE_READY,
302 "jebgalgnebhfojomionfpkfelancnnkf")).Times(1);
303 EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATED,
304 "jebgalgnebhfojomionfpkfelancnnkf")).Times(1);
305 EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATER_SLEEPING, ""))
306 .Times(1);
307 EXPECT_CALL(observer, OnEvent(Events::COMPONENT_NOT_UPDATED,
308 "jebgalgnebhfojomionfpkfelancnnkf")).Times(1);
309 EXPECT_CALL(observer, OnEvent(Events::COMPONENT_NOT_UPDATED,
310 "abagagagagagagagagagagagagagagag")).Times(1);
311 EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATER_SLEEPING, ""))
312 .Times(1);
315 EXPECT_TRUE(post_interceptor_->ExpectRequest(
316 new PartialMatch("updatecheck"), test_file("updatecheck_reply_1.xml")));
317 EXPECT_TRUE(post_interceptor_->ExpectRequest(new PartialMatch("event")));
318 EXPECT_TRUE(post_interceptor_->ExpectRequest(
319 new PartialMatch("updatecheck"), test_file("updatecheck_reply_1.xml")));
321 get_interceptor_->SetResponse(
322 GURL(expected_crx_url),
323 test_file("jebgalgnebhfojomionfpkfelancnnkf.crx"));
325 component_updater()->AddObserver(&observer);
327 scoped_refptr<TestInstaller> installer1(new TestInstaller);
328 CrxComponent com1;
329 RegisterComponent(&com1, kTestComponent_jebg, Version("0.9"), installer1);
330 scoped_refptr<TestInstaller> installer2(new TestInstaller);
331 CrxComponent com2;
332 RegisterComponent(&com2, kTestComponent_abag, Version("2.2"), installer2);
334 test_configurator()->SetLoopCount(2);
335 component_updater()->Start();
336 RunThreads();
338 EXPECT_EQ(0, installer1->error());
339 EXPECT_EQ(1, installer1->install_count());
340 EXPECT_EQ(0, installer2->error());
341 EXPECT_EQ(0, installer2->install_count());
343 // Expect three request in total: two update checks and one ping.
344 EXPECT_EQ(3, post_interceptor_->GetHitCount())
345 << post_interceptor_->GetRequestsAsString();
346 EXPECT_EQ(3, post_interceptor_->GetCount())
347 << post_interceptor_->GetRequestsAsString();
349 // Expect one component download.
350 EXPECT_EQ(1, get_interceptor_->GetHitCount());
352 EXPECT_NE(
353 string::npos,
354 post_interceptor_->GetRequests()[0].find(
355 "<app appid=\"jebgalgnebhfojomionfpkfelancnnkf\" version=\"0.9\">"
356 "<updatecheck /></app>"))
357 << post_interceptor_->GetRequestsAsString();
358 EXPECT_NE(
359 string::npos,
360 post_interceptor_->GetRequests()[0].find(
361 "<app appid=\"abagagagagagagagagagagagagagagag\" version=\"2.2\">"
362 "<updatecheck /></app>"))
363 << post_interceptor_->GetRequestsAsString();
365 EXPECT_NE(string::npos, post_interceptor_->GetRequests()[1].find(
366 "<app appid=\"jebgalgnebhfojomionfpkfelancnnkf\" "
367 "version=\"0.9\" nextversion=\"1.0\">"
368 "<event eventtype=\"3\" eventresult=\"1\"/>"))
369 << post_interceptor_->GetRequestsAsString();
371 EXPECT_NE(
372 string::npos,
373 post_interceptor_->GetRequests()[2].find(
374 "<app appid=\"jebgalgnebhfojomionfpkfelancnnkf\" version=\"1.0\">"
375 "<updatecheck /></app>"));
376 EXPECT_NE(
377 string::npos,
378 post_interceptor_->GetRequests()[2].find(
379 "<app appid=\"abagagagagagagagagagagagagagagag\" version=\"2.2\">"
380 "<updatecheck /></app>"))
381 << post_interceptor_->GetRequestsAsString();
383 // Test the protocol version is correct and the extra request attributes
384 // are included in the request.
385 EXPECT_NE(string::npos, post_interceptor_->GetRequests()[0].find(
386 "request protocol=\"3.0\" extra=\"foo\""))
387 << post_interceptor_->GetRequestsAsString();
389 // Tokenize the request string to look for specific attributes, which
390 // are important for backward compatibility with the version v2 of the update
391 // protocol. In this case, inspect the <request>, which is the first element
392 // after the xml declaration of the update request body.
393 // Expect to find the |os|, |arch|, |prodchannel|, and |prodversion|
394 // attributes:
395 // <?xml version="1.0" encoding="UTF-8"?>
396 // <request... os=... arch=... prodchannel=... prodversion=...>
397 // ...
398 // </request>
399 const std::string update_request(post_interceptor_->GetRequests()[0]);
400 std::vector<base::StringPiece> elements;
401 Tokenize(update_request, "<>", &elements);
402 EXPECT_NE(string::npos, elements[1].find(" os="));
403 EXPECT_NE(string::npos, elements[1].find(" arch="));
404 EXPECT_NE(string::npos, elements[1].find(" prodchannel="));
405 EXPECT_NE(string::npos, elements[1].find(" prodversion="));
407 // Look for additional attributes of the request, such as |version|,
408 // |requestid|, |lang|, and |nacl_arch|.
409 EXPECT_NE(string::npos, elements[1].find(" version="));
410 EXPECT_NE(string::npos, elements[1].find(" requestid="));
411 EXPECT_NE(string::npos, elements[1].find(" lang="));
412 EXPECT_NE(string::npos, elements[1].find(" nacl_arch="));
414 component_updater()->Stop();
417 // This test checks that the "prodversionmin" value is handled correctly. In
418 // particular there should not be an install because the minimum product
419 // version is much higher than of chrome.
420 TEST_F(ComponentUpdaterTest, ProdVersionCheck) {
421 EXPECT_TRUE(post_interceptor_->ExpectRequest(
422 new PartialMatch("updatecheck"), test_file("updatecheck_reply_2.xml")));
424 get_interceptor_->SetResponse(
425 GURL(expected_crx_url),
426 test_file("jebgalgnebhfojomionfpkfelancnnkf.crx"));
428 scoped_refptr<TestInstaller> installer(new TestInstaller);
429 CrxComponent com;
430 RegisterComponent(&com, kTestComponent_jebg, Version("0.9"), installer);
432 test_configurator()->SetLoopCount(1);
433 component_updater()->Start();
434 RunThreads();
436 // Expect one update check and no ping.
437 EXPECT_EQ(1, post_interceptor_->GetHitCount())
438 << post_interceptor_->GetRequestsAsString();
439 EXPECT_EQ(1, post_interceptor_->GetCount())
440 << post_interceptor_->GetRequestsAsString();
442 // Expect no download to occur.
443 EXPECT_EQ(0, get_interceptor_->GetHitCount());
445 EXPECT_EQ(0, installer->error());
446 EXPECT_EQ(0, installer->install_count());
448 component_updater()->Stop();
451 // Test that a update check due to an on demand call can cause installs.
452 // Here is the timeline:
453 // - First loop: we return a reply that indicates no update, so
454 // nothing happens.
455 // - We make an on demand call.
456 // - This triggers a second loop, which has a reply that triggers an install.
457 #if defined(OS_LINUX)
458 // http://crbug.com/396488
459 #define MAYBE_OnDemandUpdate DISABLED_OnDemandUpdate
460 #else
461 #define MAYBE_OnDemandUpdate OnDemandUpdate
462 #endif
463 TEST_F(ComponentUpdaterTest, MAYBE_OnDemandUpdate) {
464 MockServiceObserver observer;
466 InSequence seq;
467 EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATER_STARTED, ""))
468 .Times(1);
469 EXPECT_CALL(observer, OnEvent(Events::COMPONENT_NOT_UPDATED,
470 "abagagagagagagagagagagagagagagag")).Times(1);
471 EXPECT_CALL(observer, OnEvent(Events::COMPONENT_NOT_UPDATED,
472 "jebgalgnebhfojomionfpkfelancnnkf")).Times(1);
473 EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATER_SLEEPING, ""))
474 .Times(1);
475 EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATER_STARTED, ""))
476 .Times(1);
477 EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATE_FOUND,
478 "jebgalgnebhfojomionfpkfelancnnkf")).Times(1);
479 EXPECT_CALL(observer, OnEvent(Events::COMPONENT_NOT_UPDATED,
480 "abagagagagagagagagagagagagagagag")).Times(1);
481 EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATE_DOWNLOADING,
482 "jebgalgnebhfojomionfpkfelancnnkf"))
483 .Times(AnyNumber());
484 EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATE_READY,
485 "jebgalgnebhfojomionfpkfelancnnkf")).Times(1);
486 EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATED,
487 "jebgalgnebhfojomionfpkfelancnnkf")).Times(1);
488 EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATER_SLEEPING, ""))
489 .Times(1);
492 EXPECT_TRUE(post_interceptor_->ExpectRequest(
493 new PartialMatch("updatecheck"), test_file("updatecheck_reply_empty")));
495 get_interceptor_->SetResponse(
496 GURL(expected_crx_url),
497 test_file("jebgalgnebhfojomionfpkfelancnnkf.crx"));
499 component_updater()->AddObserver(&observer);
501 scoped_refptr<TestInstaller> installer1(new TestInstaller);
502 CrxComponent com1;
503 RegisterComponent(&com1, kTestComponent_abag, Version("2.2"), installer1);
504 scoped_refptr<TestInstaller> installer2(new TestInstaller);
505 CrxComponent com2;
506 RegisterComponent(&com2, kTestComponent_jebg, Version("0.9"), installer2);
508 // No update normally.
509 test_configurator()->SetLoopCount(1);
510 component_updater()->Start();
511 RunThreads();
512 component_updater()->Stop();
514 EXPECT_EQ(1, post_interceptor_->GetHitCount())
515 << post_interceptor_->GetRequestsAsString();
516 EXPECT_EQ(1, post_interceptor_->GetCount())
517 << post_interceptor_->GetRequestsAsString();
519 EXPECT_EQ(0, get_interceptor_->GetHitCount());
521 // Update after an on-demand check is issued.
522 post_interceptor_->Reset();
523 EXPECT_TRUE(post_interceptor_->ExpectRequest(
524 new PartialMatch("updatecheck"), test_file("updatecheck_reply_1.xml")));
525 EXPECT_TRUE(post_interceptor_->ExpectRequest(new PartialMatch("event")));
527 EXPECT_EQ(Status::kOk, OnDemandTester::OnDemand(component_updater(),
528 GetCrxComponentID(com2)));
529 test_configurator()->SetLoopCount(1);
530 component_updater()->Start();
531 RunThreads();
533 EXPECT_EQ(0, installer1->error());
534 EXPECT_EQ(0, installer1->install_count());
535 EXPECT_EQ(0, installer2->error());
536 EXPECT_EQ(1, installer2->install_count());
538 EXPECT_EQ(2, post_interceptor_->GetHitCount())
539 << post_interceptor_->GetRequestsAsString();
540 EXPECT_EQ(2, post_interceptor_->GetCount())
541 << post_interceptor_->GetRequestsAsString();
543 EXPECT_EQ(1, get_interceptor_->GetHitCount());
545 // Expect the update check to contain an "ondemand" request for the
546 // second component (com2) and a normal request for the other component.
547 EXPECT_NE(string::npos, post_interceptor_->GetRequests()[0].find(
548 "<app appid=\"abagagagagagagagagagagagagagagag\" "
549 "version=\"2.2\"><updatecheck /></app>"))
550 << post_interceptor_->GetRequestsAsString();
551 EXPECT_NE(
552 string::npos,
553 post_interceptor_->GetRequests()[0].find(
554 "<app appid=\"jebgalgnebhfojomionfpkfelancnnkf\" "
555 "version=\"0.9\" installsource=\"ondemand\"><updatecheck /></app>"))
556 << post_interceptor_->GetRequestsAsString();
557 EXPECT_NE(string::npos, post_interceptor_->GetRequests()[1].find(
558 "<app appid=\"jebgalgnebhfojomionfpkfelancnnkf\" "
559 "version=\"0.9\" nextversion=\"1.0\">"
560 "<event eventtype=\"3\" eventresult=\"1\"/>"))
561 << post_interceptor_->GetRequestsAsString();
563 // Also check what happens if previous check too soon. It works, since this
564 // direct OnDemand call does not implement a cooldown.
565 test_configurator()->SetOnDemandTime(60 * 60);
566 EXPECT_EQ(Status::kOk, OnDemandTester::OnDemand(component_updater(),
567 GetCrxComponentID(com2)));
568 // Okay, now reset to 0 for the other tests.
569 test_configurator()->SetOnDemandTime(0);
570 component_updater()->Stop();
572 // Test a few error cases. NOTE: We don't have callbacks for
573 // when the updates failed yet.
574 EXPECT_TRUE(Mock::VerifyAndClearExpectations(&observer));
576 InSequence seq;
577 EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATER_STARTED, ""))
578 .Times(1);
579 EXPECT_CALL(observer, OnEvent(Events::COMPONENT_NOT_UPDATED,
580 "abagagagagagagagagagagagagagagag")).Times(1);
581 EXPECT_CALL(observer, OnEvent(Events::COMPONENT_NOT_UPDATED,
582 "jebgalgnebhfojomionfpkfelancnnkf")).Times(1);
583 EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATER_SLEEPING, ""))
584 .Times(1);
587 // No update: error from no server response
588 post_interceptor_->Reset();
589 EXPECT_TRUE(post_interceptor_->ExpectRequest(
590 new PartialMatch("updatecheck"), test_file("updatecheck_reply_empty")));
592 test_configurator()->SetLoopCount(1);
593 component_updater()->Start();
594 EXPECT_EQ(Status::kOk, OnDemandTester::OnDemand(component_updater(),
595 GetCrxComponentID(com2)));
596 RunThreads();
597 component_updater()->Stop();
599 EXPECT_EQ(1, post_interceptor_->GetHitCount())
600 << post_interceptor_->GetRequestsAsString();
601 EXPECT_EQ(1, post_interceptor_->GetCount())
602 << post_interceptor_->GetRequestsAsString();
604 // No update: already updated to 1.0 so nothing new
605 EXPECT_TRUE(Mock::VerifyAndClearExpectations(&observer));
607 InSequence seq;
608 EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATER_STARTED, ""))
609 .Times(1);
610 EXPECT_CALL(observer, OnEvent(Events::COMPONENT_NOT_UPDATED,
611 "jebgalgnebhfojomionfpkfelancnnkf")).Times(1);
612 EXPECT_CALL(observer, OnEvent(Events::COMPONENT_NOT_UPDATED,
613 "abagagagagagagagagagagagagagagag")).Times(1);
614 EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATER_SLEEPING, ""))
615 .Times(1);
618 post_interceptor_->Reset();
619 EXPECT_TRUE(post_interceptor_->ExpectRequest(
620 new PartialMatch("updatecheck"), test_file("updatecheck_reply_1.xml")));
622 test_configurator()->SetLoopCount(1);
623 component_updater()->Start();
624 EXPECT_EQ(Status::kOk, OnDemandTester::OnDemand(component_updater(),
625 GetCrxComponentID(com2)));
626 RunThreads();
628 EXPECT_EQ(1, post_interceptor_->GetHitCount())
629 << post_interceptor_->GetRequestsAsString();
630 EXPECT_EQ(1, post_interceptor_->GetCount())
631 << post_interceptor_->GetRequestsAsString();
633 component_updater()->Stop();
636 // Verify that a previously registered component can get re-registered
637 // with a different version.
638 TEST_F(ComponentUpdaterTest, CheckReRegistration) {
639 MockServiceObserver observer;
641 InSequence seq;
642 EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATER_STARTED, ""))
643 .Times(1);
644 EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATE_FOUND,
645 "jebgalgnebhfojomionfpkfelancnnkf")).Times(1);
646 EXPECT_CALL(observer, OnEvent(Events::COMPONENT_NOT_UPDATED,
647 "abagagagagagagagagagagagagagagag")).Times(1);
648 EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATE_DOWNLOADING,
649 "jebgalgnebhfojomionfpkfelancnnkf"))
650 .Times(AnyNumber());
651 EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATE_READY,
652 "jebgalgnebhfojomionfpkfelancnnkf")).Times(1);
653 EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATED,
654 "jebgalgnebhfojomionfpkfelancnnkf")).Times(1);
655 EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATER_SLEEPING, ""))
656 .Times(1);
657 EXPECT_CALL(observer, OnEvent(Events::COMPONENT_NOT_UPDATED,
658 "jebgalgnebhfojomionfpkfelancnnkf")).Times(1);
659 EXPECT_CALL(observer, OnEvent(Events::COMPONENT_NOT_UPDATED,
660 "abagagagagagagagagagagagagagagag")).Times(1);
661 EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATER_SLEEPING, ""))
662 .Times(1);
665 EXPECT_TRUE(post_interceptor_->ExpectRequest(
666 new PartialMatch("updatecheck"), test_file("updatecheck_reply_1.xml")));
667 EXPECT_TRUE(post_interceptor_->ExpectRequest(new PartialMatch("event")));
668 EXPECT_TRUE(post_interceptor_->ExpectRequest(
669 new PartialMatch("updatecheck"), test_file("updatecheck_reply_1.xml")));
671 get_interceptor_->SetResponse(
672 GURL(expected_crx_url),
673 test_file("jebgalgnebhfojomionfpkfelancnnkf.crx"));
675 component_updater()->AddObserver(&observer);
677 scoped_refptr<TestInstaller> installer1(new TestInstaller);
678 CrxComponent com1;
679 RegisterComponent(&com1, kTestComponent_jebg, Version("0.9"), installer1);
680 scoped_refptr<TestInstaller> installer2(new TestInstaller);
681 CrxComponent com2;
682 RegisterComponent(&com2, kTestComponent_abag, Version("2.2"), installer2);
684 // Loop twice to issue two checks: (1) with original 0.9 version, update to
685 // 1.0, and do the second check (2) with the updated 1.0 version.
686 test_configurator()->SetLoopCount(2);
687 component_updater()->Start();
688 RunThreads();
690 EXPECT_EQ(0, installer1->error());
691 EXPECT_EQ(1, installer1->install_count());
692 EXPECT_EQ(0, installer2->error());
693 EXPECT_EQ(0, installer2->install_count());
695 EXPECT_EQ(3, post_interceptor_->GetHitCount())
696 << post_interceptor_->GetRequestsAsString();
697 EXPECT_EQ(1, get_interceptor_->GetHitCount());
699 EXPECT_NE(
700 string::npos,
701 post_interceptor_->GetRequests()[0].find(
702 "<app appid=\"jebgalgnebhfojomionfpkfelancnnkf\" version=\"0.9\">"
703 "<updatecheck /></app>"))
704 << post_interceptor_->GetRequestsAsString();
705 EXPECT_NE(string::npos, post_interceptor_->GetRequests()[1].find(
706 "<app appid=\"jebgalgnebhfojomionfpkfelancnnkf\" "
707 "version=\"0.9\" nextversion=\"1.0\">"
708 "<event eventtype=\"3\" eventresult=\"1\"/>"))
709 << post_interceptor_->GetRequestsAsString();
710 EXPECT_NE(
711 string::npos,
712 post_interceptor_->GetRequests()[2].find(
713 "<app appid=\"jebgalgnebhfojomionfpkfelancnnkf\" version=\"1.0\">"
714 "<updatecheck /></app>"))
715 << post_interceptor_->GetRequestsAsString();
717 component_updater()->Stop();
719 // Now re-register, pretending to be an even newer version (2.2)
720 EXPECT_TRUE(Mock::VerifyAndClearExpectations(&observer));
722 InSequence seq;
723 EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATER_STARTED, ""))
724 .Times(1);
725 EXPECT_CALL(observer, OnEvent(Events::COMPONENT_NOT_UPDATED,
726 "jebgalgnebhfojomionfpkfelancnnkf")).Times(1);
727 EXPECT_CALL(observer, OnEvent(Events::COMPONENT_NOT_UPDATED,
728 "abagagagagagagagagagagagagagagag")).Times(1);
729 EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATER_SLEEPING, ""))
730 .Times(1);
733 post_interceptor_->Reset();
734 EXPECT_TRUE(post_interceptor_->ExpectRequest(
735 new PartialMatch("updatecheck"), test_file("updatecheck_reply_1.xml")));
737 scoped_refptr<TestInstaller> installer3(new TestInstaller);
738 EXPECT_EQ(Status::kReplaced, RegisterComponent(&com1, kTestComponent_jebg,
739 Version("2.2"), installer3));
741 // Loop once just to notice the check happening with the re-register version.
742 test_configurator()->SetLoopCount(1);
743 component_updater()->Start();
744 RunThreads();
746 // We created a new installer, so the counts go back to 0.
747 EXPECT_EQ(0, installer3->error());
748 EXPECT_EQ(0, installer3->install_count());
749 EXPECT_EQ(0, installer2->error());
750 EXPECT_EQ(0, installer2->install_count());
752 // One update check and no additional pings are expected.
753 EXPECT_EQ(1, post_interceptor_->GetHitCount())
754 << post_interceptor_->GetRequestsAsString();
755 EXPECT_EQ(1, post_interceptor_->GetCount())
756 << post_interceptor_->GetRequestsAsString();
758 EXPECT_NE(
759 string::npos,
760 post_interceptor_->GetRequests()[0].find(
761 "<app appid=\"jebgalgnebhfojomionfpkfelancnnkf\" version=\"2.2\">"
762 "<updatecheck /></app>"));
764 component_updater()->Stop();
767 // Verify that we can download and install a component and a differential
768 // update to that component. We do three loops; the final loop should do
769 // nothing.
770 // We also check that exactly 5 non-ping network requests are issued:
771 // 1- update check (response: v1 available)
772 // 2- download crx (v1)
773 // 3- update check (response: v2 available)
774 // 4- download differential crx (v1 to v2)
775 // 5- update check (response: no further update available)
776 // There should be two pings, one for each update. The second will bear a
777 // diffresult=1, while the first will not.
778 TEST_F(ComponentUpdaterTest, DifferentialUpdate) {
779 EXPECT_TRUE(post_interceptor_->ExpectRequest(
780 new PartialMatch("updatecheck"),
781 test_file("updatecheck_diff_reply_1.xml")));
782 EXPECT_TRUE(post_interceptor_->ExpectRequest(new PartialMatch("event")));
783 EXPECT_TRUE(post_interceptor_->ExpectRequest(
784 new PartialMatch("updatecheck"),
785 test_file("updatecheck_diff_reply_2.xml")));
786 EXPECT_TRUE(post_interceptor_->ExpectRequest(new PartialMatch("event")));
787 EXPECT_TRUE(post_interceptor_->ExpectRequest(
788 new PartialMatch("updatecheck"),
789 test_file("updatecheck_diff_reply_3.xml")));
791 get_interceptor_->SetResponse(
792 GURL("http://localhost/download/ihfokbkgjpifnbbojhneepfflplebdkc_1.crx"),
793 test_file("ihfokbkgjpifnbbojhneepfflplebdkc_1.crx"));
794 get_interceptor_->SetResponse(
795 GURL(
796 "http://localhost/download/"
797 "ihfokbkgjpifnbbojhneepfflplebdkc_1to2.crx"),
798 test_file("ihfokbkgjpifnbbojhneepfflplebdkc_1to2.crx"));
800 scoped_refptr<TestInstaller> installer(new VersionedTestInstaller);
801 CrxComponent com;
802 RegisterComponent(&com, kTestComponent_ihfo, Version("0.0"), installer);
804 test_configurator()->SetLoopCount(3);
805 component_updater()->Start();
806 RunThreads();
808 EXPECT_EQ(0, installer->error());
809 EXPECT_EQ(2, installer->install_count());
811 EXPECT_EQ(5, post_interceptor_->GetHitCount())
812 << post_interceptor_->GetRequestsAsString();
813 EXPECT_EQ(5, post_interceptor_->GetCount())
814 << post_interceptor_->GetRequestsAsString();
815 EXPECT_EQ(2, get_interceptor_->GetHitCount());
817 EXPECT_NE(
818 string::npos,
819 post_interceptor_->GetRequests()[0].find(
820 "<app appid=\"ihfokbkgjpifnbbojhneepfflplebdkc\" version=\"0.0\">"
821 "<updatecheck /></app>"))
822 << post_interceptor_->GetRequestsAsString();
823 EXPECT_NE(string::npos,
824 post_interceptor_->GetRequests()[1].find(
825 "<app appid=\"ihfokbkgjpifnbbojhneepfflplebdkc\" "
826 "version=\"0.0\" nextversion=\"1.0\">"
827 "<event eventtype=\"3\" eventresult=\"1\" nextfp=\"1\"/>"))
828 << post_interceptor_->GetRequestsAsString();
829 EXPECT_NE(
830 string::npos,
831 post_interceptor_->GetRequests()[2].find(
832 "<app appid=\"ihfokbkgjpifnbbojhneepfflplebdkc\" version=\"1.0\">"
833 "<updatecheck /><packages><package fp=\"1\"/></packages></app>"))
834 << post_interceptor_->GetRequestsAsString();
835 EXPECT_NE(string::npos,
836 post_interceptor_->GetRequests()[3].find(
837 "<app appid=\"ihfokbkgjpifnbbojhneepfflplebdkc\" "
838 "version=\"1.0\" nextversion=\"2.0\">"
839 "<event eventtype=\"3\" eventresult=\"1\" diffresult=\"1\" "
840 "previousfp=\"1\" nextfp=\"22\"/>"))
841 << post_interceptor_->GetRequestsAsString();
842 EXPECT_NE(
843 string::npos,
844 post_interceptor_->GetRequests()[4].find(
845 "<app appid=\"ihfokbkgjpifnbbojhneepfflplebdkc\" version=\"2.0\">"
846 "<updatecheck /><packages><package fp=\"22\"/></packages></app>"))
847 << post_interceptor_->GetRequestsAsString();
848 component_updater()->Stop();
851 // Verify that component installation falls back to downloading and installing
852 // a full update if the differential update fails (in this case, because the
853 // installer does not know about the existing files). We do two loops; the final
854 // loop should do nothing.
855 // We also check that exactly 4 non-ping network requests are issued:
856 // 1- update check (loop 1)
857 // 2- download differential crx
858 // 3- download full crx
859 // 4- update check (loop 2 - no update available)
860 // There should be one ping for the first attempted update.
861 // This test is flaky on Android. crbug.com/329883
862 #if defined(OS_ANDROID)
863 #define MAYBE_DifferentialUpdateFails DISABLED_DifferentialUpdateFails
864 #else
865 #define MAYBE_DifferentialUpdateFails DifferentialUpdateFails
866 #endif
867 TEST_F(ComponentUpdaterTest, MAYBE_DifferentialUpdateFails) {
868 EXPECT_TRUE(post_interceptor_->ExpectRequest(
869 new PartialMatch("updatecheck"),
870 test_file("updatecheck_diff_reply_2.xml")));
871 EXPECT_TRUE(post_interceptor_->ExpectRequest(new PartialMatch("event")));
872 EXPECT_TRUE(post_interceptor_->ExpectRequest(
873 new PartialMatch("updatecheck"),
874 test_file("updatecheck_diff_reply_3.xml")));
876 get_interceptor_->SetResponse(
877 GURL("http://localhost/download/ihfokbkgjpifnbbojhneepfflplebdkc_1.crx"),
878 test_file("ihfokbkgjpifnbbojhneepfflplebdkc_1.crx"));
879 get_interceptor_->SetResponse(
880 GURL(
881 "http://localhost/download/"
882 "ihfokbkgjpifnbbojhneepfflplebdkc_1to2.crx"),
883 test_file("ihfokbkgjpifnbbojhneepfflplebdkc_1to2.crx"));
884 get_interceptor_->SetResponse(
885 GURL("http://localhost/download/ihfokbkgjpifnbbojhneepfflplebdkc_2.crx"),
886 test_file("ihfokbkgjpifnbbojhneepfflplebdkc_2.crx"));
888 scoped_refptr<TestInstaller> installer(new TestInstaller);
889 CrxComponent com;
890 RegisterComponent(&com, kTestComponent_ihfo, Version("1.0"), installer);
892 test_configurator()->SetLoopCount(2);
893 component_updater()->Start();
894 RunThreads();
896 // A failed differential update does not count as a failed install.
897 EXPECT_EQ(0, installer->error());
898 EXPECT_EQ(1, installer->install_count());
900 EXPECT_EQ(3, post_interceptor_->GetHitCount())
901 << post_interceptor_->GetRequestsAsString();
902 EXPECT_EQ(3, post_interceptor_->GetCount())
903 << post_interceptor_->GetRequestsAsString();
904 EXPECT_EQ(2, get_interceptor_->GetHitCount());
906 EXPECT_NE(
907 string::npos,
908 post_interceptor_->GetRequests()[0].find(
909 "<app appid=\"ihfokbkgjpifnbbojhneepfflplebdkc\" version=\"1.0\">"
910 "<updatecheck /></app>"))
911 << post_interceptor_->GetRequestsAsString();
912 EXPECT_NE(string::npos,
913 post_interceptor_->GetRequests()[1].find(
914 "<app appid=\"ihfokbkgjpifnbbojhneepfflplebdkc\" "
915 "version=\"1.0\" nextversion=\"2.0\">"
916 "<event eventtype=\"3\" eventresult=\"1\" diffresult=\"0\" "
917 "differrorcat=\"2\" differrorcode=\"16\" nextfp=\"22\"/>"))
918 << post_interceptor_->GetRequestsAsString();
919 EXPECT_NE(
920 string::npos,
921 post_interceptor_->GetRequests()[2].find(
922 "<app appid=\"ihfokbkgjpifnbbojhneepfflplebdkc\" version=\"2.0\">"
923 "<updatecheck /><packages><package fp=\"22\"/></packages></app>"))
924 << post_interceptor_->GetRequestsAsString();
926 component_updater()->Stop();
929 // Test is flakey on Android bots. See crbug.com/331420.
930 #if defined(OS_ANDROID)
931 #define MAYBE_CheckFailedInstallPing DISABLED_CheckFailedInstallPing
932 #else
933 #define MAYBE_CheckFailedInstallPing CheckFailedInstallPing
934 #endif
935 // Verify that a failed installation causes an install failure ping.
936 TEST_F(ComponentUpdaterTest, MAYBE_CheckFailedInstallPing) {
937 // This test installer reports installation failure.
938 class FailingTestInstaller : public TestInstaller {
939 bool Install(const base::DictionaryValue& manifest,
940 const base::FilePath& unpack_path) override {
941 ++install_count_;
942 base::DeleteFile(unpack_path, true);
943 return false;
946 private:
947 ~FailingTestInstaller() override {}
949 scoped_refptr<FailingTestInstaller> installer(new FailingTestInstaller);
951 EXPECT_TRUE(post_interceptor_->ExpectRequest(
952 new PartialMatch("updatecheck"), test_file("updatecheck_reply_1.xml")));
953 EXPECT_TRUE(post_interceptor_->ExpectRequest(new PartialMatch("event")));
954 EXPECT_TRUE(post_interceptor_->ExpectRequest(
955 new PartialMatch("updatecheck"), test_file("updatecheck_reply_1.xml")));
956 EXPECT_TRUE(post_interceptor_->ExpectRequest(new PartialMatch("event")));
957 get_interceptor_->SetResponse(
958 GURL(expected_crx_url),
959 test_file("jebgalgnebhfojomionfpkfelancnnkf.crx"));
961 // Start with 0.9, and attempt update to 1.0.
962 // Loop twice to issue two checks: (1) with original 0.9 version
963 // and (2), which should retry with 0.9.
964 CrxComponent com;
965 RegisterComponent(&com, kTestComponent_jebg, Version("0.9"), installer);
967 test_configurator()->SetLoopCount(2);
968 component_updater()->Start();
969 RunThreads();
971 EXPECT_EQ(4, post_interceptor_->GetHitCount())
972 << post_interceptor_->GetRequestsAsString();
973 EXPECT_EQ(2, get_interceptor_->GetHitCount());
975 EXPECT_NE(
976 string::npos,
977 post_interceptor_->GetRequests()[0].find(
978 "<app appid=\"jebgalgnebhfojomionfpkfelancnnkf\" version=\"0.9\">"
979 "<updatecheck /></app>"))
980 << post_interceptor_->GetRequestsAsString();
981 EXPECT_NE(string::npos, post_interceptor_->GetRequests()[1].find(
982 "<app appid=\"jebgalgnebhfojomionfpkfelancnnkf\" "
983 "version=\"0.9\" nextversion=\"1.0\">"
984 "<event eventtype=\"3\" eventresult=\"0\" "
985 "errorcat=\"3\" errorcode=\"9\"/>"))
986 << post_interceptor_->GetRequestsAsString();
987 EXPECT_NE(
988 string::npos,
989 post_interceptor_->GetRequests()[2].find(
990 "<app appid=\"jebgalgnebhfojomionfpkfelancnnkf\" version=\"0.9\">"
991 "<updatecheck /></app>"))
992 << post_interceptor_->GetRequestsAsString();
993 EXPECT_NE(string::npos, post_interceptor_->GetRequests()[3].find(
994 "<app appid=\"jebgalgnebhfojomionfpkfelancnnkf\" "
995 "version=\"0.9\" nextversion=\"1.0\">"
996 "<event eventtype=\"3\" eventresult=\"0\" "
997 "errorcat=\"3\" errorcode=\"9\"/>"))
998 << post_interceptor_->GetRequestsAsString();
1000 // Loop once more, but expect no ping because a noupdate response is issued.
1001 // This is necessary to clear out the fire-and-forget ping from the previous
1002 // iteration.
1003 post_interceptor_->Reset();
1004 EXPECT_TRUE(post_interceptor_->ExpectRequest(
1005 new PartialMatch("updatecheck"),
1006 test_file("updatecheck_reply_noupdate.xml")));
1008 test_configurator()->SetLoopCount(1);
1009 component_updater()->Start();
1010 RunThreads();
1012 EXPECT_EQ(0, installer->error());
1013 EXPECT_EQ(2, installer->install_count());
1015 EXPECT_EQ(1, post_interceptor_->GetHitCount())
1016 << post_interceptor_->GetRequestsAsString();
1017 EXPECT_EQ(1, post_interceptor_->GetCount())
1018 << post_interceptor_->GetRequestsAsString();
1020 EXPECT_NE(
1021 string::npos,
1022 post_interceptor_->GetRequests()[0].find(
1023 "<app appid=\"jebgalgnebhfojomionfpkfelancnnkf\" version=\"0.9\">"
1024 "<updatecheck /></app>"))
1025 << post_interceptor_->GetRequestsAsString();
1027 component_updater()->Stop();
1030 // Verify that we successfully propagate a patcher error.
1031 // ihfokbkgjpifnbbojhneepfflplebdkc_1to2_bad.crx contains an incorrect
1032 // patching instruction that should fail.
1033 TEST_F(ComponentUpdaterTest, DifferentialUpdateFailErrorcode) {
1034 EXPECT_TRUE(post_interceptor_->ExpectRequest(
1035 new PartialMatch("updatecheck"),
1036 test_file("updatecheck_diff_reply_1.xml")));
1037 EXPECT_TRUE(post_interceptor_->ExpectRequest(new PartialMatch("event")));
1038 EXPECT_TRUE(post_interceptor_->ExpectRequest(
1039 new PartialMatch("updatecheck"),
1040 test_file("updatecheck_diff_reply_2.xml")));
1041 EXPECT_TRUE(post_interceptor_->ExpectRequest(new PartialMatch("event")));
1042 EXPECT_TRUE(post_interceptor_->ExpectRequest(
1043 new PartialMatch("updatecheck"),
1044 test_file("updatecheck_diff_reply_3.xml")));
1046 get_interceptor_->SetResponse(
1047 GURL("http://localhost/download/ihfokbkgjpifnbbojhneepfflplebdkc_1.crx"),
1048 test_file("ihfokbkgjpifnbbojhneepfflplebdkc_1.crx"));
1049 // This intercept returns a different file than what is specified in the
1050 // update check response and requested in the download. The file that is
1051 // actually dowloaded contains a patching error, an therefore, an error
1052 // is injected at the time of patching.
1053 get_interceptor_->SetResponse(
1054 GURL(
1055 "http://localhost/download/"
1056 "ihfokbkgjpifnbbojhneepfflplebdkc_1to2.crx"),
1057 test_file("ihfokbkgjpifnbbojhneepfflplebdkc_1to2_bad.crx"));
1058 get_interceptor_->SetResponse(
1059 GURL("http://localhost/download/ihfokbkgjpifnbbojhneepfflplebdkc_2.crx"),
1060 test_file("ihfokbkgjpifnbbojhneepfflplebdkc_2.crx"));
1062 scoped_refptr<TestInstaller> installer(new VersionedTestInstaller);
1063 CrxComponent com;
1064 RegisterComponent(&com, kTestComponent_ihfo, Version("0.0"), installer);
1066 test_configurator()->SetLoopCount(3);
1067 component_updater()->Start();
1068 RunThreads();
1069 component_updater()->Stop();
1071 EXPECT_EQ(0, installer->error());
1072 EXPECT_EQ(2, installer->install_count());
1074 EXPECT_EQ(5, post_interceptor_->GetHitCount())
1075 << post_interceptor_->GetRequestsAsString();
1076 EXPECT_EQ(5, post_interceptor_->GetCount())
1077 << post_interceptor_->GetRequestsAsString();
1078 EXPECT_EQ(3, get_interceptor_->GetHitCount());
1080 EXPECT_NE(
1081 string::npos,
1082 post_interceptor_->GetRequests()[0].find(
1083 "<app appid=\"ihfokbkgjpifnbbojhneepfflplebdkc\" version=\"0.0\">"
1084 "<updatecheck /></app>"))
1085 << post_interceptor_->GetRequestsAsString();
1086 EXPECT_NE(string::npos,
1087 post_interceptor_->GetRequests()[1].find(
1088 "<app appid=\"ihfokbkgjpifnbbojhneepfflplebdkc\" "
1089 "version=\"0.0\" nextversion=\"1.0\">"
1090 "<event eventtype=\"3\" eventresult=\"1\" nextfp=\"1\"/>"))
1091 << post_interceptor_->GetRequestsAsString();
1092 EXPECT_NE(
1093 string::npos,
1094 post_interceptor_->GetRequests()[2].find(
1095 "<app appid=\"ihfokbkgjpifnbbojhneepfflplebdkc\" version=\"1.0\">"
1096 "<updatecheck /><packages><package fp=\"1\"/></packages></app>"))
1097 << post_interceptor_->GetRequestsAsString();
1098 EXPECT_NE(string::npos, post_interceptor_->GetRequests()[3].find(
1099 "<app appid=\"ihfokbkgjpifnbbojhneepfflplebdkc\" "
1100 "version=\"1.0\" nextversion=\"2.0\">"
1101 "<event eventtype=\"3\" eventresult=\"1\" "
1102 "diffresult=\"0\" differrorcat=\"2\" "
1103 "differrorcode=\"14\" diffextracode1=\"305\" "
1104 "previousfp=\"1\" nextfp=\"22\"/>"))
1105 << post_interceptor_->GetRequestsAsString();
1106 EXPECT_NE(
1107 string::npos,
1108 post_interceptor_->GetRequests()[4].find(
1109 "<app appid=\"ihfokbkgjpifnbbojhneepfflplebdkc\" version=\"2.0\">"
1110 "<updatecheck /><packages><package fp=\"22\"/></packages></app>"))
1111 << post_interceptor_->GetRequestsAsString();
1114 class TestResourceController : public content::ResourceController {
1115 public:
1116 virtual void SetThrottle(content::ResourceThrottle* throttle) {}
1119 content::ResourceThrottle* RequestTestResourceThrottle(
1120 ComponentUpdateService* cus,
1121 TestResourceController* controller,
1122 const char* crx_id) {
1123 net::TestURLRequestContext context;
1124 scoped_ptr<net::URLRequest> url_request(context.CreateRequest(
1125 GURL("http://foo.example.com/thing.bin"), net::DEFAULT_PRIORITY, NULL));
1127 content::ResourceThrottle* rt = GetOnDemandResourceThrottle(cus, crx_id);
1128 rt->set_controller_for_testing(controller);
1129 controller->SetThrottle(rt);
1130 return rt;
1133 void RequestAndDeleteResourceThrottle(ComponentUpdateService* cus,
1134 const char* crx_id) {
1135 // By requesting a throttle and deleting it immediately we ensure that we
1136 // hit the case where the component updater tries to use the weak
1137 // pointer to a dead Resource throttle.
1138 class NoCallResourceController : public TestResourceController {
1139 public:
1140 ~NoCallResourceController() override {}
1141 void Cancel() override { CHECK(false); }
1142 void CancelAndIgnore() override { CHECK(false); }
1143 void CancelWithError(int error_code) override { CHECK(false); }
1144 void Resume() override { CHECK(false); }
1145 } controller;
1147 delete RequestTestResourceThrottle(cus, &controller, crx_id);
1150 TEST_F(ComponentUpdaterTest, ResourceThrottleDeletedNoUpdate) {
1151 MockServiceObserver observer;
1153 InSequence seq;
1154 EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATER_STARTED, ""))
1155 .Times(1);
1156 EXPECT_CALL(observer, OnEvent(Events::COMPONENT_NOT_UPDATED,
1157 "abagagagagagagagagagagagagagagag")).Times(1);
1158 EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATER_SLEEPING, ""))
1159 .Times(1);
1162 EXPECT_TRUE(post_interceptor_->ExpectRequest(
1163 new PartialMatch("updatecheck"), test_file("updatecheck_reply_1.xml")));
1165 scoped_refptr<TestInstaller> installer(new TestInstaller);
1166 CrxComponent com;
1167 component_updater()->AddObserver(&observer);
1168 EXPECT_EQ(Status::kOk, RegisterComponent(&com, kTestComponent_abag,
1169 Version("1.1"), installer));
1170 // The following two calls ensure that we don't do an update check via the
1171 // timer, so the only update check should be the on-demand one.
1172 test_configurator()->SetInitialDelay(1000000);
1173 test_configurator()->SetRecheckTime(1000000);
1174 test_configurator()->SetLoopCount(1);
1175 component_updater()->Start();
1177 RunThreadsUntilIdle();
1179 EXPECT_EQ(0, post_interceptor_->GetHitCount());
1181 BrowserThread::PostTask(
1182 BrowserThread::IO, FROM_HERE,
1183 base::Bind(&RequestAndDeleteResourceThrottle, component_updater(),
1184 "abagagagagagagagagagagagagagagag"));
1186 RunThreads();
1188 EXPECT_EQ(1, post_interceptor_->GetHitCount());
1189 EXPECT_EQ(0, installer->error());
1190 EXPECT_EQ(0, installer->install_count());
1192 component_updater()->Stop();
1195 class CancelResourceController : public TestResourceController {
1196 public:
1197 CancelResourceController() : throttle_(NULL), resume_called_(0) {}
1198 ~CancelResourceController() override {
1199 // Check that the throttle has been resumed by the time we
1200 // exit the test.
1201 CHECK_EQ(1, resume_called_);
1202 delete throttle_;
1204 void Cancel() override { CHECK(false); }
1205 void CancelAndIgnore() override { CHECK(false); }
1206 void CancelWithError(int error_code) override { CHECK(false); }
1207 void Resume() override {
1208 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
1209 base::Bind(&CancelResourceController::ResumeCalled,
1210 base::Unretained(this)));
1212 void SetThrottle(content::ResourceThrottle* throttle) override {
1213 throttle_ = throttle;
1214 bool defer = false;
1215 // Initially the throttle is blocked. The CUS needs to run a
1216 // task on the UI thread to decide if it should unblock.
1217 throttle_->WillStartRequest(&defer);
1218 CHECK(defer);
1221 private:
1222 void ResumeCalled() { ++resume_called_; }
1224 content::ResourceThrottle* throttle_;
1225 int resume_called_;
1228 // Tests the on-demand update with resource throttle, including the
1229 // cooldown interval between calls.
1230 TEST_F(ComponentUpdaterTest, ResourceThrottleLiveNoUpdate) {
1231 MockServiceObserver observer;
1233 InSequence seq;
1234 EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATER_STARTED, ""))
1235 .Times(1);
1236 EXPECT_CALL(observer, OnEvent(Events::COMPONENT_NOT_UPDATED,
1237 "abagagagagagagagagagagagagagagag")).Times(1);
1238 EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATER_SLEEPING, ""))
1239 .Times(1);
1240 EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATER_STARTED, ""))
1241 .Times(1);
1242 EXPECT_CALL(observer, OnEvent(Events::COMPONENT_NOT_UPDATED,
1243 "abagagagagagagagagagagagagagagag")).Times(1);
1244 EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATER_SLEEPING, ""))
1245 .Times(1);
1246 EXPECT_CALL(observer, OnEvent(Events::COMPONENT_UPDATER_STARTED, ""))
1247 .Times(1);
1250 EXPECT_TRUE(post_interceptor_->ExpectRequest(
1251 new PartialMatch("updatecheck"), test_file("updatecheck_reply_1.xml")));
1253 scoped_refptr<TestInstaller> installer(new TestInstaller);
1254 CrxComponent com;
1255 component_updater()->AddObserver(&observer);
1256 EXPECT_EQ(Status::kOk, RegisterComponent(&com, kTestComponent_abag,
1257 Version("1.1"), installer));
1258 // The following two calls ensure that we don't do an update check via the
1259 // timer, so the only update check should be the on-demand one.
1260 test_configurator()->SetInitialDelay(1000000);
1261 test_configurator()->SetRecheckTime(1000000);
1262 test_configurator()->SetLoopCount(1);
1263 component_updater()->Start();
1265 RunThreadsUntilIdle();
1267 EXPECT_EQ(0, post_interceptor_->GetHitCount());
1270 // First on-demand update check is expected to succeeded.
1271 CancelResourceController controller;
1273 BrowserThread::PostTask(
1274 BrowserThread::IO, FROM_HERE,
1275 base::Bind(base::IgnoreResult(&RequestTestResourceThrottle),
1276 component_updater(), &controller,
1277 "abagagagagagagagagagagagagagagag"));
1279 RunThreads();
1281 EXPECT_EQ(1, post_interceptor_->GetHitCount());
1282 EXPECT_EQ(0, installer->error());
1283 EXPECT_EQ(0, installer->install_count());
1285 component_updater()->Stop();
1289 // Second on-demand update check is expected to succeed as well, since there
1290 // is no cooldown interval between calls, due to calling SetOnDemandTime.
1291 test_configurator()->SetOnDemandTime(0);
1292 test_configurator()->SetLoopCount(1);
1293 component_updater()->Start();
1295 CancelResourceController controller;
1297 BrowserThread::PostTask(
1298 BrowserThread::IO, FROM_HERE,
1299 base::Bind(base::IgnoreResult(&RequestTestResourceThrottle),
1300 component_updater(), &controller,
1301 "abagagagagagagagagagagagagagagag"));
1303 RunThreads();
1305 EXPECT_EQ(1, post_interceptor_->GetHitCount());
1306 EXPECT_EQ(0, installer->error());
1307 EXPECT_EQ(0, installer->install_count());
1309 component_updater()->Stop();
1313 // This on-demand call is expected not to trigger a component update check.
1314 test_configurator()->SetOnDemandTime(1000000);
1315 component_updater()->Start();
1317 CancelResourceController controller;
1319 BrowserThread::PostTask(
1320 BrowserThread::IO, FROM_HERE,
1321 base::Bind(base::IgnoreResult(&RequestTestResourceThrottle),
1322 component_updater(), &controller,
1323 "abagagagagagagagagagagagagagagag"));
1324 RunThreadsUntilIdle();
1328 // Tests adding and removing observers.
1329 TEST_F(ComponentUpdaterTest, Observer) {
1330 MockServiceObserver observer1, observer2;
1332 // Expect that two observers see the events.
1334 InSequence seq;
1335 EXPECT_CALL(observer1, OnEvent(Events::COMPONENT_UPDATER_STARTED, ""))
1336 .Times(1);
1337 EXPECT_CALL(observer2, OnEvent(Events::COMPONENT_UPDATER_STARTED, ""))
1338 .Times(1);
1339 EXPECT_CALL(observer1, OnEvent(Events::COMPONENT_NOT_UPDATED,
1340 "abagagagagagagagagagagagagagagag"))
1341 .Times(1);
1342 EXPECT_CALL(observer2, OnEvent(Events::COMPONENT_NOT_UPDATED,
1343 "abagagagagagagagagagagagagagagag"))
1344 .Times(1);
1345 EXPECT_CALL(observer1, OnEvent(Events::COMPONENT_UPDATER_SLEEPING, ""))
1346 .Times(1);
1347 EXPECT_CALL(observer2, OnEvent(Events::COMPONENT_UPDATER_SLEEPING, ""))
1348 .Times(1);
1351 EXPECT_TRUE(post_interceptor_->ExpectRequest(
1352 new PartialMatch("updatecheck"), test_file("updatecheck_reply_1.xml")));
1354 component_updater()->AddObserver(&observer1);
1355 component_updater()->AddObserver(&observer2);
1357 scoped_refptr<TestInstaller> installer(new TestInstaller);
1358 CrxComponent com;
1359 EXPECT_EQ(Status::kOk, RegisterComponent(&com, kTestComponent_abag,
1360 Version("1.1"), installer));
1361 test_configurator()->SetLoopCount(1);
1362 component_updater()->Start();
1363 RunThreads();
1365 // After removing the first observer, it's only the second observer that
1366 // gets the events.
1367 EXPECT_TRUE(Mock::VerifyAndClearExpectations(&observer1));
1368 EXPECT_TRUE(Mock::VerifyAndClearExpectations(&observer2));
1370 InSequence seq;
1371 EXPECT_CALL(observer2, OnEvent(Events::COMPONENT_UPDATER_STARTED, ""))
1372 .Times(1);
1373 EXPECT_CALL(observer2, OnEvent(Events::COMPONENT_NOT_UPDATED,
1374 "abagagagagagagagagagagagagagagag"))
1375 .Times(1);
1376 EXPECT_CALL(observer2, OnEvent(Events::COMPONENT_UPDATER_SLEEPING, ""))
1377 .Times(1);
1380 component_updater()->RemoveObserver(&observer1);
1382 test_configurator()->SetLoopCount(1);
1383 component_updater()->Start();
1384 RunThreads();
1386 // Both observers are removed and no one gets the events.
1387 EXPECT_TRUE(Mock::VerifyAndClearExpectations(&observer1));
1388 EXPECT_TRUE(Mock::VerifyAndClearExpectations(&observer2));
1389 component_updater()->RemoveObserver(&observer2);
1391 test_configurator()->SetLoopCount(1);
1392 component_updater()->Start();
1393 RunThreads();
1395 component_updater()->Stop();
1398 } // namespace component_updater