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/test/component_updater_service_unittest.h"
6 #include "base/file_util.h"
7 #include "base/path_service.h"
8 #include "base/run_loop.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "base/strings/string_util.h"
11 #include "base/strings/stringprintf.h"
12 #include "base/values.h"
13 #include "chrome/browser/component_updater/component_updater_utils.h"
14 #include "chrome/browser/component_updater/test/test_installer.h"
15 #include "chrome/common/chrome_paths.h"
16 #include "content/public/browser/browser_thread.h"
17 #include "content/public/browser/resource_controller.h"
18 #include "content/public/browser/resource_request_info.h"
19 #include "content/public/browser/resource_throttle.h"
20 #include "libxml/globals.h"
21 #include "net/base/upload_bytes_element_reader.h"
22 #include "net/url_request/url_fetcher.h"
23 #include "net/url_request/url_request_test_util.h"
26 using content::BrowserThread
;
29 using ::testing::AnyNumber
;
30 using ::testing::InSequence
;
31 using ::testing::Mock
;
33 namespace component_updater
{
35 #define POST_INTERCEPT_SCHEME "https"
36 #define POST_INTERCEPT_HOSTNAME "localhost2"
37 #define POST_INTERCEPT_PATH "/update2"
39 MockServiceObserver::MockServiceObserver() {
42 MockServiceObserver::~MockServiceObserver() {
45 bool PartialMatch::Match(const std::string
& actual
) const {
46 return actual
.find(expected_
) != std::string::npos
;
49 TestConfigurator::TestConfigurator()
55 context_(new net::TestURLRequestContextGetter(
56 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO
))) {
59 TestConfigurator::~TestConfigurator() {
62 int TestConfigurator::InitialDelay() {
66 int TestConfigurator::NextCheckDelay() {
67 // This is called when a new full cycle of checking for updates is going
68 // to happen. In test we normally only test one cycle so it is a good
69 // time to break from the test messageloop Run() method so the test can
78 int TestConfigurator::StepDelay() {
82 int TestConfigurator::StepDelayMedium() {
83 return NextCheckDelay();
86 int TestConfigurator::MinimumReCheckWait() {
90 int TestConfigurator::OnDemandDelay() {
91 return ondemand_time_
;
94 GURL
TestConfigurator::UpdateUrl() {
95 return GURL(POST_INTERCEPT_SCHEME
96 "://" POST_INTERCEPT_HOSTNAME POST_INTERCEPT_PATH
);
99 GURL
TestConfigurator::PingUrl() {
103 std::string
TestConfigurator::ExtraRequestParams() {
104 return "extra=\"foo\"";
107 size_t TestConfigurator::UrlSizeLimit() {
111 net::URLRequestContextGetter
* TestConfigurator::RequestContext() {
112 return context_
.get();
115 // Don't use the utility process to run code out-of-process.
116 bool TestConfigurator::InProcess() {
120 bool TestConfigurator::DeltasEnabled() const {
124 bool TestConfigurator::UseBackgroundDownloader() const {
128 // Set how many update checks are called, the default value is just once.
129 void TestConfigurator::SetLoopCount(int times
) {
133 void TestConfigurator::SetRecheckTime(int seconds
) {
134 recheck_time_
= seconds
;
137 void TestConfigurator::SetOnDemandTime(int seconds
) {
138 ondemand_time_
= seconds
;
141 void TestConfigurator::SetComponentUpdateService(ComponentUpdateService
* cus
) {
145 void TestConfigurator::SetQuitClosure(const base::Closure
& quit_closure
) {
146 quit_closure_
= quit_closure
;
149 void TestConfigurator::SetInitialDelay(int seconds
) {
150 initial_time_
= seconds
;
153 InterceptorFactory::InterceptorFactory()
154 : URLRequestPostInterceptorFactory(POST_INTERCEPT_SCHEME
,
155 POST_INTERCEPT_HOSTNAME
) {
158 InterceptorFactory::~InterceptorFactory() {
161 URLRequestPostInterceptor
* InterceptorFactory::CreateInterceptor() {
162 return URLRequestPostInterceptorFactory::CreateInterceptor(
163 base::FilePath::FromUTF8Unsafe(POST_INTERCEPT_PATH
));
166 ComponentUpdaterTest::ComponentUpdaterTest()
167 : test_config_(NULL
),
168 thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP
) {
169 // The component updater instance under test.
170 test_config_
= new TestConfigurator
;
171 component_updater_
.reset(ComponentUpdateServiceFactory(test_config_
));
172 test_config_
->SetComponentUpdateService(component_updater_
.get());
174 // The test directory is chrome/test/data/components.
175 PathService::Get(chrome::DIR_TEST_DATA
, &test_data_dir_
);
176 test_data_dir_
= test_data_dir_
.AppendASCII("components");
178 net::URLFetcher::SetEnableInterceptionForTests(true);
181 ComponentUpdaterTest::~ComponentUpdaterTest() {
182 net::URLFetcher::SetEnableInterceptionForTests(false);
185 void ComponentUpdaterTest::SetUp() {
186 get_interceptor_
.reset(new GetInterceptor
);
187 interceptor_factory_
.reset(new InterceptorFactory
);
188 post_interceptor_
= interceptor_factory_
->CreateInterceptor();
189 EXPECT_TRUE(post_interceptor_
);
192 void ComponentUpdaterTest::TearDown() {
193 interceptor_factory_
.reset();
194 get_interceptor_
.reset();
198 ComponentUpdateService
* ComponentUpdaterTest::component_updater() {
199 return component_updater_
.get();
202 // Makes the full path to a component updater test file.
203 const base::FilePath
ComponentUpdaterTest::test_file(const char* file
) {
204 return test_data_dir_
.AppendASCII(file
);
207 TestConfigurator
* ComponentUpdaterTest::test_configurator() {
211 ComponentUpdateService::Status
ComponentUpdaterTest::RegisterComponent(
213 TestComponents component
,
214 const Version
& version
,
215 TestInstaller
* installer
) {
216 if (component
== kTestComponent_abag
) {
217 com
->name
= "test_abag";
218 com
->pk_hash
.assign(abag_hash
, abag_hash
+ arraysize(abag_hash
));
219 } else if (component
== kTestComponent_jebg
) {
220 com
->name
= "test_jebg";
221 com
->pk_hash
.assign(jebg_hash
, jebg_hash
+ arraysize(jebg_hash
));
223 com
->name
= "test_ihfo";
224 com
->pk_hash
.assign(ihfo_hash
, ihfo_hash
+ arraysize(ihfo_hash
));
226 com
->version
= version
;
227 com
->installer
= installer
;
228 return component_updater_
->RegisterComponent(*com
);
231 void ComponentUpdaterTest::RunThreads() {
232 base::RunLoop runloop
;
233 test_configurator()->SetQuitClosure(runloop
.QuitClosure());
236 // Since some tests need to drain currently enqueued tasks such as network
237 // intercepts on the IO thread, run the threads until they are
238 // idle. The component updater service won't loop again until the loop count
239 // is set and the service is started.
240 RunThreadsUntilIdle();
243 void ComponentUpdaterTest::RunThreadsUntilIdle() {
244 base::RunLoop().RunUntilIdle();
247 ComponentUpdateService::Status
OnDemandTester::OnDemand(
248 ComponentUpdateService
* cus
,
249 const std::string
& component_id
) {
250 return cus
->OnDemandUpdate(component_id
);
253 // Verify that our test fixture work and the component updater can
254 // be created and destroyed with no side effects.
255 TEST_F(ComponentUpdaterTest
, VerifyFixture
) {
256 EXPECT_TRUE(component_updater() != NULL
);
259 // Verify that the component updater can be caught in a quick
260 // start-shutdown situation. Failure of this test will be a crash.
261 TEST_F(ComponentUpdaterTest
, StartStop
) {
262 component_updater()->Start();
263 RunThreadsUntilIdle();
264 component_updater()->Stop();
267 // Verify that when the server has no updates, we go back to sleep and
268 // the COMPONENT_UPDATER_STARTED and COMPONENT_UPDATER_SLEEPING notifications
269 // are generated. No pings are sent.
270 TEST_F(ComponentUpdaterTest
, CheckCrxSleep
) {
271 MockServiceObserver observer
;
273 EXPECT_CALL(observer
,
274 OnEvent(ServiceObserver::COMPONENT_UPDATER_STARTED
, ""))
276 EXPECT_CALL(observer
,
277 OnEvent(ServiceObserver::COMPONENT_UPDATER_SLEEPING
, ""))
279 EXPECT_CALL(observer
,
280 OnEvent(ServiceObserver::COMPONENT_NOT_UPDATED
,
281 "abagagagagagagagagagagagagagagag"))
284 EXPECT_TRUE(post_interceptor_
->ExpectRequest(
285 new PartialMatch("updatecheck"), test_file("updatecheck_reply_1.xml")));
286 EXPECT_TRUE(post_interceptor_
->ExpectRequest(
287 new PartialMatch("updatecheck"), test_file("updatecheck_reply_1.xml")));
289 TestInstaller installer
;
291 component_updater()->AddObserver(&observer
);
293 ComponentUpdateService::kOk
,
294 RegisterComponent(&com
, kTestComponent_abag
, Version("1.1"), &installer
));
296 // We loop twice, but there are no updates so we expect two sleep messages.
297 test_configurator()->SetLoopCount(2);
298 component_updater()->Start();
301 EXPECT_EQ(0, static_cast<TestInstaller
*>(com
.installer
)->error());
302 EXPECT_EQ(0, static_cast<TestInstaller
*>(com
.installer
)->install_count());
304 // Expect to see the two update check requests and no other requests,
306 EXPECT_EQ(2, post_interceptor_
->GetHitCount())
307 << post_interceptor_
->GetRequestsAsString();
308 EXPECT_EQ(2, post_interceptor_
->GetCount())
309 << post_interceptor_
->GetRequestsAsString();
312 post_interceptor_
->GetRequests()[0].find(
313 "<app appid=\"abagagagagagagagagagagagagagagag\" version=\"1.1\">"
314 "<updatecheck /></app>"))
315 << post_interceptor_
->GetRequestsAsString();
318 post_interceptor_
->GetRequests()[1].find(
319 "<app appid=\"abagagagagagagagagagagagagagagag\" version=\"1.1\">"
320 "<updatecheck /></app>"))
321 << post_interceptor_
->GetRequestsAsString();
323 component_updater()->Stop();
325 // Loop twice again but this case we simulate a server error by returning
326 // an empty file. Expect the behavior of the service to be the same as before.
327 EXPECT_CALL(observer
, OnEvent(ServiceObserver::COMPONENT_UPDATER_STARTED
, ""))
329 EXPECT_CALL(observer
,
330 OnEvent(ServiceObserver::COMPONENT_UPDATER_SLEEPING
, ""))
332 EXPECT_CALL(observer
,
333 OnEvent(ServiceObserver::COMPONENT_NOT_UPDATED
,
334 "abagagagagagagagagagagagagagagag"))
337 post_interceptor_
->Reset();
338 EXPECT_TRUE(post_interceptor_
->ExpectRequest(
339 new PartialMatch("updatecheck"), test_file("updatecheck_reply_empty")));
340 EXPECT_TRUE(post_interceptor_
->ExpectRequest(
341 new PartialMatch("updatecheck"), test_file("updatecheck_reply_empty")));
343 test_configurator()->SetLoopCount(2);
344 component_updater()->Start();
347 EXPECT_EQ(0, static_cast<TestInstaller
*>(com
.installer
)->error());
348 EXPECT_EQ(0, static_cast<TestInstaller
*>(com
.installer
)->install_count());
350 EXPECT_EQ(2, post_interceptor_
->GetHitCount())
351 << post_interceptor_
->GetRequestsAsString();
352 EXPECT_EQ(2, post_interceptor_
->GetCount())
353 << post_interceptor_
->GetRequestsAsString();
356 post_interceptor_
->GetRequests()[0].find(
357 "<app appid=\"abagagagagagagagagagagagagagagag\" version=\"1.1\">"
358 "<updatecheck /></app>"))
359 << post_interceptor_
->GetRequestsAsString();
362 post_interceptor_
->GetRequests()[1].find(
363 "<app appid=\"abagagagagagagagagagagagagagagag\" version=\"1.1\">"
364 "<updatecheck /></app>"))
365 << post_interceptor_
->GetRequestsAsString();
367 component_updater()->Stop();
370 // Verify that we can check for updates and install one component. Besides
371 // the notifications above COMPONENT_UPDATE_FOUND and COMPONENT_UPDATE_READY
372 // should have been fired. We do two loops so the second time around there
373 // should be nothing left to do.
374 // We also check that the following network requests are issued:
378 // 4- second update check.
379 TEST_F(ComponentUpdaterTest
, InstallCrx
) {
380 MockServiceObserver observer
;
383 EXPECT_CALL(observer
,
384 OnEvent(ServiceObserver::COMPONENT_UPDATER_STARTED
, ""))
386 EXPECT_CALL(observer
,
387 OnEvent(ServiceObserver::COMPONENT_UPDATE_FOUND
,
388 "jebgalgnebhfojomionfpkfelancnnkf"))
390 EXPECT_CALL(observer
,
391 OnEvent(ServiceObserver::COMPONENT_NOT_UPDATED
,
392 "abagagagagagagagagagagagagagagag"))
394 EXPECT_CALL(observer
,
395 OnEvent(ServiceObserver::COMPONENT_UPDATE_DOWNLOADING
,
396 "jebgalgnebhfojomionfpkfelancnnkf"))
398 EXPECT_CALL(observer
,
399 OnEvent(ServiceObserver::COMPONENT_UPDATE_READY
,
400 "jebgalgnebhfojomionfpkfelancnnkf"))
402 EXPECT_CALL(observer
,
403 OnEvent(ServiceObserver::COMPONENT_UPDATED
,
404 "jebgalgnebhfojomionfpkfelancnnkf"))
406 EXPECT_CALL(observer
,
407 OnEvent(ServiceObserver::COMPONENT_UPDATER_SLEEPING
, ""))
409 EXPECT_CALL(observer
,
410 OnEvent(ServiceObserver::COMPONENT_NOT_UPDATED
,
411 "jebgalgnebhfojomionfpkfelancnnkf"))
413 EXPECT_CALL(observer
,
414 OnEvent(ServiceObserver::COMPONENT_NOT_UPDATED
,
415 "abagagagagagagagagagagagagagagag"))
417 EXPECT_CALL(observer
,
418 OnEvent(ServiceObserver::COMPONENT_UPDATER_SLEEPING
, ""))
422 EXPECT_TRUE(post_interceptor_
->ExpectRequest(
423 new PartialMatch("updatecheck"), test_file("updatecheck_reply_1.xml")));
424 EXPECT_TRUE(post_interceptor_
->ExpectRequest(new PartialMatch("event")));
425 EXPECT_TRUE(post_interceptor_
->ExpectRequest(
426 new PartialMatch("updatecheck"), test_file("updatecheck_reply_1.xml")));
428 get_interceptor_
->SetResponse(
429 GURL(expected_crx_url
),
430 test_file("jebgalgnebhfojomionfpkfelancnnkf.crx"));
432 component_updater()->AddObserver(&observer
);
434 TestInstaller installer1
;
436 RegisterComponent(&com1
, kTestComponent_jebg
, Version("0.9"), &installer1
);
437 TestInstaller installer2
;
439 RegisterComponent(&com2
, kTestComponent_abag
, Version("2.2"), &installer2
);
441 test_configurator()->SetLoopCount(2);
442 component_updater()->Start();
445 EXPECT_EQ(0, static_cast<TestInstaller
*>(com1
.installer
)->error());
446 EXPECT_EQ(1, static_cast<TestInstaller
*>(com1
.installer
)->install_count());
447 EXPECT_EQ(0, static_cast<TestInstaller
*>(com2
.installer
)->error());
448 EXPECT_EQ(0, static_cast<TestInstaller
*>(com2
.installer
)->install_count());
450 // Expect three request in total: two update checks and one ping.
451 EXPECT_EQ(3, post_interceptor_
->GetHitCount())
452 << post_interceptor_
->GetRequestsAsString();
453 EXPECT_EQ(3, post_interceptor_
->GetCount())
454 << post_interceptor_
->GetRequestsAsString();
456 // Expect one component download.
457 EXPECT_EQ(1, get_interceptor_
->GetHitCount());
461 post_interceptor_
->GetRequests()[0].find(
462 "<app appid=\"jebgalgnebhfojomionfpkfelancnnkf\" version=\"0.9\">"
463 "<updatecheck /></app>"))
464 << post_interceptor_
->GetRequestsAsString();
467 post_interceptor_
->GetRequests()[0].find(
468 "<app appid=\"abagagagagagagagagagagagagagagag\" version=\"2.2\">"
469 "<updatecheck /></app>"))
470 << post_interceptor_
->GetRequestsAsString();
474 post_interceptor_
->GetRequests()[1].find(
475 "<app appid=\"jebgalgnebhfojomionfpkfelancnnkf\" "
476 "version=\"0.9\" nextversion=\"1.0\">"
477 "<event eventtype=\"3\" eventresult=\"1\"/>"))
478 << post_interceptor_
->GetRequestsAsString();
482 post_interceptor_
->GetRequests()[2].find(
483 "<app appid=\"jebgalgnebhfojomionfpkfelancnnkf\" version=\"1.0\">"
484 "<updatecheck /></app>"));
487 post_interceptor_
->GetRequests()[2].find(
488 "<app appid=\"abagagagagagagagagagagagagagagag\" version=\"2.2\">"
489 "<updatecheck /></app>"))
490 << post_interceptor_
->GetRequestsAsString();
492 // Test the protocol version is correct and the extra request attributes
493 // are included in the request.
496 post_interceptor_
->GetRequests()[0].find(
497 "request protocol=\"3.0\" extra=\"foo\""))
498 << post_interceptor_
->GetRequestsAsString();
500 // Tokenize the request string to look for specific attributes, which
501 // are important for backward compatibility with the version v2 of the update
502 // protocol. In this case, inspect the <request>, which is the first element
503 // after the xml declaration of the update request body.
504 // Expect to find the |os|, |arch|, |prodchannel|, and |prodversion|
506 // <?xml version="1.0" encoding="UTF-8"?>
507 // <request... os=... arch=... prodchannel=... prodversion=...>
510 const std::string
update_request(post_interceptor_
->GetRequests()[0]);
511 std::vector
<base::StringPiece
> elements
;
512 Tokenize(update_request
, "<>", &elements
);
513 EXPECT_NE(string::npos
, elements
[1].find(" os="));
514 EXPECT_NE(string::npos
, elements
[1].find(" arch="));
515 EXPECT_NE(string::npos
, elements
[1].find(" prodchannel="));
516 EXPECT_NE(string::npos
, elements
[1].find(" prodversion="));
518 // Look for additional attributes of the request, such as |version|,
519 // |requestid|, |lang|, and |nacl_arch|.
520 EXPECT_NE(string::npos
, elements
[1].find(" version="));
521 EXPECT_NE(string::npos
, elements
[1].find(" requestid="));
522 EXPECT_NE(string::npos
, elements
[1].find(" lang="));
523 EXPECT_NE(string::npos
, elements
[1].find(" nacl_arch="));
525 component_updater()->Stop();
528 // This test checks that the "prodversionmin" value is handled correctly. In
529 // particular there should not be an install because the minimum product
530 // version is much higher than of chrome.
531 TEST_F(ComponentUpdaterTest
, ProdVersionCheck
) {
532 EXPECT_TRUE(post_interceptor_
->ExpectRequest(
533 new PartialMatch("updatecheck"), test_file("updatecheck_reply_2.xml")));
535 get_interceptor_
->SetResponse(
536 GURL(expected_crx_url
),
537 test_file("jebgalgnebhfojomionfpkfelancnnkf.crx"));
539 TestInstaller installer
;
541 RegisterComponent(&com
, kTestComponent_jebg
, Version("0.9"), &installer
);
543 test_configurator()->SetLoopCount(1);
544 component_updater()->Start();
547 // Expect one update check and no ping.
548 EXPECT_EQ(1, post_interceptor_
->GetHitCount())
549 << post_interceptor_
->GetRequestsAsString();
550 EXPECT_EQ(1, post_interceptor_
->GetCount())
551 << post_interceptor_
->GetRequestsAsString();
553 // Expect no download to occur.
554 EXPECT_EQ(0, get_interceptor_
->GetHitCount());
556 EXPECT_EQ(0, static_cast<TestInstaller
*>(com
.installer
)->error());
557 EXPECT_EQ(0, static_cast<TestInstaller
*>(com
.installer
)->install_count());
559 component_updater()->Stop();
562 // Test that a update check due to an on demand call can cause installs.
563 // Here is the timeline:
564 // - First loop: we return a reply that indicates no update, so
566 // - We make an on demand call.
567 // - This triggers a second loop, which has a reply that triggers an install.
568 TEST_F(ComponentUpdaterTest
, OnDemandUpdate
) {
569 MockServiceObserver observer
;
572 EXPECT_CALL(observer
,
573 OnEvent(ServiceObserver::COMPONENT_UPDATER_STARTED
, ""))
575 EXPECT_CALL(observer
,
576 OnEvent(ServiceObserver::COMPONENT_NOT_UPDATED
,
577 "abagagagagagagagagagagagagagagag"))
579 EXPECT_CALL(observer
,
580 OnEvent(ServiceObserver::COMPONENT_NOT_UPDATED
,
581 "jebgalgnebhfojomionfpkfelancnnkf"))
583 EXPECT_CALL(observer
,
584 OnEvent(ServiceObserver::COMPONENT_UPDATER_SLEEPING
, ""))
586 EXPECT_CALL(observer
,
587 OnEvent(ServiceObserver::COMPONENT_UPDATER_STARTED
, ""))
589 EXPECT_CALL(observer
,
590 OnEvent(ServiceObserver::COMPONENT_UPDATE_FOUND
,
591 "jebgalgnebhfojomionfpkfelancnnkf"))
593 EXPECT_CALL(observer
,
594 OnEvent(ServiceObserver::COMPONENT_NOT_UPDATED
,
595 "abagagagagagagagagagagagagagagag"))
597 EXPECT_CALL(observer
,
598 OnEvent(ServiceObserver::COMPONENT_UPDATE_DOWNLOADING
,
599 "jebgalgnebhfojomionfpkfelancnnkf"))
601 EXPECT_CALL(observer
,
602 OnEvent(ServiceObserver::COMPONENT_UPDATE_READY
,
603 "jebgalgnebhfojomionfpkfelancnnkf"))
605 EXPECT_CALL(observer
,
606 OnEvent(ServiceObserver::COMPONENT_UPDATED
,
607 "jebgalgnebhfojomionfpkfelancnnkf"))
609 EXPECT_CALL(observer
,
610 OnEvent(ServiceObserver::COMPONENT_UPDATER_SLEEPING
, ""))
614 EXPECT_TRUE(post_interceptor_
->ExpectRequest(
615 new PartialMatch("updatecheck"), test_file("updatecheck_reply_empty")));
617 get_interceptor_
->SetResponse(
618 GURL(expected_crx_url
),
619 test_file("jebgalgnebhfojomionfpkfelancnnkf.crx"));
621 component_updater()->AddObserver(&observer
);
623 TestInstaller installer1
;
625 RegisterComponent(&com1
, kTestComponent_abag
, Version("2.2"), &installer1
);
626 TestInstaller installer2
;
628 RegisterComponent(&com2
, kTestComponent_jebg
, Version("0.9"), &installer2
);
630 // No update normally.
631 test_configurator()->SetLoopCount(1);
632 component_updater()->Start();
634 component_updater()->Stop();
636 EXPECT_EQ(1, post_interceptor_
->GetHitCount())
637 << post_interceptor_
->GetRequestsAsString();
638 EXPECT_EQ(1, post_interceptor_
->GetCount())
639 << post_interceptor_
->GetRequestsAsString();
641 EXPECT_EQ(0, get_interceptor_
->GetHitCount());
643 // Update after an on-demand check is issued.
644 post_interceptor_
->Reset();
645 EXPECT_TRUE(post_interceptor_
->ExpectRequest(
646 new PartialMatch("updatecheck"), test_file("updatecheck_reply_1.xml")));
647 EXPECT_TRUE(post_interceptor_
->ExpectRequest(new PartialMatch("event")));
650 ComponentUpdateService::kOk
,
651 OnDemandTester::OnDemand(component_updater(), GetCrxComponentID(com2
)));
652 test_configurator()->SetLoopCount(1);
653 component_updater()->Start();
656 EXPECT_EQ(0, static_cast<TestInstaller
*>(com1
.installer
)->error());
657 EXPECT_EQ(0, static_cast<TestInstaller
*>(com1
.installer
)->install_count());
658 EXPECT_EQ(0, static_cast<TestInstaller
*>(com2
.installer
)->error());
659 EXPECT_EQ(1, static_cast<TestInstaller
*>(com2
.installer
)->install_count());
661 EXPECT_EQ(2, post_interceptor_
->GetHitCount())
662 << post_interceptor_
->GetRequestsAsString();
663 EXPECT_EQ(2, post_interceptor_
->GetCount())
664 << post_interceptor_
->GetRequestsAsString();
666 EXPECT_EQ(1, get_interceptor_
->GetHitCount());
668 // Expect the update check to contain an "ondemand" request for the
669 // second component (com2) and a normal request for the other component.
672 post_interceptor_
->GetRequests()[0].find(
673 "<app appid=\"abagagagagagagagagagagagagagagag\" "
674 "version=\"2.2\"><updatecheck /></app>"))
675 << post_interceptor_
->GetRequestsAsString();
678 post_interceptor_
->GetRequests()[0].find(
679 "<app appid=\"jebgalgnebhfojomionfpkfelancnnkf\" "
680 "version=\"0.9\" installsource=\"ondemand\"><updatecheck /></app>"))
681 << post_interceptor_
->GetRequestsAsString();
684 post_interceptor_
->GetRequests()[1].find(
685 "<app appid=\"jebgalgnebhfojomionfpkfelancnnkf\" "
686 "version=\"0.9\" nextversion=\"1.0\">"
687 "<event eventtype=\"3\" eventresult=\"1\"/>"))
688 << post_interceptor_
->GetRequestsAsString();
690 // Also check what happens if previous check too soon.
691 test_configurator()->SetOnDemandTime(60 * 60);
693 ComponentUpdateService::kError
,
694 OnDemandTester::OnDemand(component_updater(), GetCrxComponentID(com2
)));
695 // Okay, now reset to 0 for the other tests.
696 test_configurator()->SetOnDemandTime(0);
697 component_updater()->Stop();
699 // Test a few error cases. NOTE: We don't have callbacks for
700 // when the updates failed yet.
701 EXPECT_TRUE(Mock::VerifyAndClearExpectations(&observer
));
704 EXPECT_CALL(observer
,
705 OnEvent(ServiceObserver::COMPONENT_UPDATER_STARTED
, ""))
707 EXPECT_CALL(observer
,
708 OnEvent(ServiceObserver::COMPONENT_NOT_UPDATED
,
709 "abagagagagagagagagagagagagagagag"))
711 EXPECT_CALL(observer
,
712 OnEvent(ServiceObserver::COMPONENT_NOT_UPDATED
,
713 "jebgalgnebhfojomionfpkfelancnnkf"))
715 EXPECT_CALL(observer
,
716 OnEvent(ServiceObserver::COMPONENT_UPDATER_SLEEPING
, ""))
720 // No update: error from no server response
721 post_interceptor_
->Reset();
722 EXPECT_TRUE(post_interceptor_
->ExpectRequest(
723 new PartialMatch("updatecheck"), test_file("updatecheck_reply_empty")));
725 test_configurator()->SetLoopCount(1);
726 component_updater()->Start();
728 ComponentUpdateService::kOk
,
729 OnDemandTester::OnDemand(component_updater(), GetCrxComponentID(com2
)));
731 component_updater()->Stop();
733 EXPECT_EQ(1, post_interceptor_
->GetHitCount())
734 << post_interceptor_
->GetRequestsAsString();
735 EXPECT_EQ(1, post_interceptor_
->GetCount())
736 << post_interceptor_
->GetRequestsAsString();
738 // No update: already updated to 1.0 so nothing new
739 EXPECT_TRUE(Mock::VerifyAndClearExpectations(&observer
));
742 EXPECT_CALL(observer
,
743 OnEvent(ServiceObserver::COMPONENT_UPDATER_STARTED
, ""))
745 EXPECT_CALL(observer
,
746 OnEvent(ServiceObserver::COMPONENT_NOT_UPDATED
,
747 "jebgalgnebhfojomionfpkfelancnnkf"))
749 EXPECT_CALL(observer
,
750 OnEvent(ServiceObserver::COMPONENT_NOT_UPDATED
,
751 "abagagagagagagagagagagagagagagag"))
753 EXPECT_CALL(observer
,
754 OnEvent(ServiceObserver::COMPONENT_UPDATER_SLEEPING
, ""))
758 post_interceptor_
->Reset();
759 EXPECT_TRUE(post_interceptor_
->ExpectRequest(
760 new PartialMatch("updatecheck"), test_file("updatecheck_reply_1.xml")));
762 test_configurator()->SetLoopCount(1);
763 component_updater()->Start();
765 ComponentUpdateService::kOk
,
766 OnDemandTester::OnDemand(component_updater(), GetCrxComponentID(com2
)));
769 EXPECT_EQ(1, post_interceptor_
->GetHitCount())
770 << post_interceptor_
->GetRequestsAsString();
771 EXPECT_EQ(1, post_interceptor_
->GetCount())
772 << post_interceptor_
->GetRequestsAsString();
774 component_updater()->Stop();
777 // Verify that a previously registered component can get re-registered
778 // with a different version.
779 TEST_F(ComponentUpdaterTest
, CheckReRegistration
) {
780 MockServiceObserver observer
;
783 EXPECT_CALL(observer
,
784 OnEvent(ServiceObserver::COMPONENT_UPDATER_STARTED
, ""))
786 EXPECT_CALL(observer
,
787 OnEvent(ServiceObserver::COMPONENT_UPDATE_FOUND
,
788 "jebgalgnebhfojomionfpkfelancnnkf"))
790 EXPECT_CALL(observer
,
791 OnEvent(ServiceObserver::COMPONENT_NOT_UPDATED
,
792 "abagagagagagagagagagagagagagagag"))
794 EXPECT_CALL(observer
,
795 OnEvent(ServiceObserver::COMPONENT_UPDATE_DOWNLOADING
,
796 "jebgalgnebhfojomionfpkfelancnnkf"))
798 EXPECT_CALL(observer
,
799 OnEvent(ServiceObserver::COMPONENT_UPDATE_READY
,
800 "jebgalgnebhfojomionfpkfelancnnkf"))
802 EXPECT_CALL(observer
,
803 OnEvent(ServiceObserver::COMPONENT_UPDATED
,
804 "jebgalgnebhfojomionfpkfelancnnkf"))
806 EXPECT_CALL(observer
,
807 OnEvent(ServiceObserver::COMPONENT_UPDATER_SLEEPING
, ""))
809 EXPECT_CALL(observer
,
810 OnEvent(ServiceObserver::COMPONENT_NOT_UPDATED
,
811 "jebgalgnebhfojomionfpkfelancnnkf"))
813 EXPECT_CALL(observer
,
814 OnEvent(ServiceObserver::COMPONENT_NOT_UPDATED
,
815 "abagagagagagagagagagagagagagagag"))
817 EXPECT_CALL(observer
,
818 OnEvent(ServiceObserver::COMPONENT_UPDATER_SLEEPING
, ""))
822 EXPECT_TRUE(post_interceptor_
->ExpectRequest(
823 new PartialMatch("updatecheck"), test_file("updatecheck_reply_1.xml")));
824 EXPECT_TRUE(post_interceptor_
->ExpectRequest(new PartialMatch("event")));
825 EXPECT_TRUE(post_interceptor_
->ExpectRequest(
826 new PartialMatch("updatecheck"), test_file("updatecheck_reply_1.xml")));
828 get_interceptor_
->SetResponse(
829 GURL(expected_crx_url
),
830 test_file("jebgalgnebhfojomionfpkfelancnnkf.crx"));
832 component_updater()->AddObserver(&observer
);
834 TestInstaller installer1
;
836 RegisterComponent(&com1
, kTestComponent_jebg
, Version("0.9"), &installer1
);
837 TestInstaller installer2
;
839 RegisterComponent(&com2
, kTestComponent_abag
, Version("2.2"), &installer2
);
841 // Loop twice to issue two checks: (1) with original 0.9 version, update to
842 // 1.0, and do the second check (2) with the updated 1.0 version.
843 test_configurator()->SetLoopCount(2);
844 component_updater()->Start();
847 EXPECT_EQ(0, static_cast<TestInstaller
*>(com1
.installer
)->error());
848 EXPECT_EQ(1, static_cast<TestInstaller
*>(com1
.installer
)->install_count());
849 EXPECT_EQ(0, static_cast<TestInstaller
*>(com2
.installer
)->error());
850 EXPECT_EQ(0, static_cast<TestInstaller
*>(com2
.installer
)->install_count());
852 EXPECT_EQ(3, post_interceptor_
->GetHitCount())
853 << post_interceptor_
->GetRequestsAsString();
854 EXPECT_EQ(1, get_interceptor_
->GetHitCount());
858 post_interceptor_
->GetRequests()[0].find(
859 "<app appid=\"jebgalgnebhfojomionfpkfelancnnkf\" version=\"0.9\">"
860 "<updatecheck /></app>"))
861 << post_interceptor_
->GetRequestsAsString();
864 post_interceptor_
->GetRequests()[1].find(
865 "<app appid=\"jebgalgnebhfojomionfpkfelancnnkf\" "
866 "version=\"0.9\" nextversion=\"1.0\">"
867 "<event eventtype=\"3\" eventresult=\"1\"/>"))
868 << post_interceptor_
->GetRequestsAsString();
871 post_interceptor_
->GetRequests()[2].find(
872 "<app appid=\"jebgalgnebhfojomionfpkfelancnnkf\" version=\"1.0\">"
873 "<updatecheck /></app>"))
874 << post_interceptor_
->GetRequestsAsString();
876 component_updater()->Stop();
878 // Now re-register, pretending to be an even newer version (2.2)
879 EXPECT_TRUE(Mock::VerifyAndClearExpectations(&observer
));
882 EXPECT_CALL(observer
,
883 OnEvent(ServiceObserver::COMPONENT_UPDATER_STARTED
, ""))
885 EXPECT_CALL(observer
,
886 OnEvent(ServiceObserver::COMPONENT_NOT_UPDATED
,
887 "jebgalgnebhfojomionfpkfelancnnkf"))
889 EXPECT_CALL(observer
,
890 OnEvent(ServiceObserver::COMPONENT_NOT_UPDATED
,
891 "abagagagagagagagagagagagagagagag"))
893 EXPECT_CALL(observer
,
894 OnEvent(ServiceObserver::COMPONENT_UPDATER_SLEEPING
, ""))
898 post_interceptor_
->Reset();
899 EXPECT_TRUE(post_interceptor_
->ExpectRequest(
900 new PartialMatch("updatecheck"), test_file("updatecheck_reply_1.xml")));
902 TestInstaller installer3
;
903 EXPECT_EQ(ComponentUpdateService::kReplaced
,
905 &com1
, kTestComponent_jebg
, Version("2.2"), &installer3
));
907 // Loop once just to notice the check happening with the re-register version.
908 test_configurator()->SetLoopCount(1);
909 component_updater()->Start();
912 // We created a new installer, so the counts go back to 0.
913 EXPECT_EQ(0, static_cast<TestInstaller
*>(com1
.installer
)->error());
914 EXPECT_EQ(0, static_cast<TestInstaller
*>(com1
.installer
)->install_count());
915 EXPECT_EQ(0, static_cast<TestInstaller
*>(com2
.installer
)->error());
916 EXPECT_EQ(0, static_cast<TestInstaller
*>(com2
.installer
)->install_count());
918 // One update check and no additional pings are expected.
919 EXPECT_EQ(1, post_interceptor_
->GetHitCount())
920 << post_interceptor_
->GetRequestsAsString();
921 EXPECT_EQ(1, post_interceptor_
->GetCount())
922 << post_interceptor_
->GetRequestsAsString();
926 post_interceptor_
->GetRequests()[0].find(
927 "<app appid=\"jebgalgnebhfojomionfpkfelancnnkf\" version=\"2.2\">"
928 "<updatecheck /></app>"));
930 component_updater()->Stop();
933 // Verify that we can download and install a component and a differential
934 // update to that component. We do three loops; the final loop should do
936 // We also check that exactly 5 non-ping network requests are issued:
937 // 1- update check (response: v1 available)
938 // 2- download crx (v1)
939 // 3- update check (response: v2 available)
940 // 4- download differential crx (v1 to v2)
941 // 5- update check (response: no further update available)
942 // There should be two pings, one for each update. The second will bear a
943 // diffresult=1, while the first will not.
944 TEST_F(ComponentUpdaterTest
, DifferentialUpdate
) {
945 EXPECT_TRUE(post_interceptor_
->ExpectRequest(
946 new PartialMatch("updatecheck"),
947 test_file("updatecheck_diff_reply_1.xml")));
948 EXPECT_TRUE(post_interceptor_
->ExpectRequest(new PartialMatch("event")));
949 EXPECT_TRUE(post_interceptor_
->ExpectRequest(
950 new PartialMatch("updatecheck"),
951 test_file("updatecheck_diff_reply_2.xml")));
952 EXPECT_TRUE(post_interceptor_
->ExpectRequest(new PartialMatch("event")));
953 EXPECT_TRUE(post_interceptor_
->ExpectRequest(
954 new PartialMatch("updatecheck"),
955 test_file("updatecheck_diff_reply_3.xml")));
957 get_interceptor_
->SetResponse(
958 GURL("http://localhost/download/ihfokbkgjpifnbbojhneepfflplebdkc_1.crx"),
959 test_file("ihfokbkgjpifnbbojhneepfflplebdkc_1.crx"));
960 get_interceptor_
->SetResponse(
961 GURL("http://localhost/download/"
962 "ihfokbkgjpifnbbojhneepfflplebdkc_1to2.crx"),
963 test_file("ihfokbkgjpifnbbojhneepfflplebdkc_1to2.crx"));
965 VersionedTestInstaller installer
;
967 RegisterComponent(&com
, kTestComponent_ihfo
, Version("0.0"), &installer
);
969 test_configurator()->SetLoopCount(3);
970 component_updater()->Start();
973 EXPECT_EQ(0, static_cast<TestInstaller
*>(com
.installer
)->error());
974 EXPECT_EQ(2, static_cast<TestInstaller
*>(com
.installer
)->install_count());
976 EXPECT_EQ(5, post_interceptor_
->GetHitCount())
977 << post_interceptor_
->GetRequestsAsString();
978 EXPECT_EQ(5, post_interceptor_
->GetCount())
979 << post_interceptor_
->GetRequestsAsString();
980 EXPECT_EQ(2, get_interceptor_
->GetHitCount());
984 post_interceptor_
->GetRequests()[0].find(
985 "<app appid=\"ihfokbkgjpifnbbojhneepfflplebdkc\" version=\"0.0\">"
986 "<updatecheck /></app>"))
987 << post_interceptor_
->GetRequestsAsString();
990 post_interceptor_
->GetRequests()[1].find(
991 "<app appid=\"ihfokbkgjpifnbbojhneepfflplebdkc\" "
992 "version=\"0.0\" nextversion=\"1.0\">"
993 "<event eventtype=\"3\" eventresult=\"1\" nextfp=\"1\"/>"))
994 << post_interceptor_
->GetRequestsAsString();
997 post_interceptor_
->GetRequests()[2].find(
998 "<app appid=\"ihfokbkgjpifnbbojhneepfflplebdkc\" version=\"1.0\">"
999 "<updatecheck /><packages><package fp=\"1\"/></packages></app>"))
1000 << post_interceptor_
->GetRequestsAsString();
1003 post_interceptor_
->GetRequests()[3].find(
1004 "<app appid=\"ihfokbkgjpifnbbojhneepfflplebdkc\" "
1005 "version=\"1.0\" nextversion=\"2.0\">"
1006 "<event eventtype=\"3\" eventresult=\"1\" diffresult=\"1\" "
1007 "previousfp=\"1\" nextfp=\"22\"/>"))
1008 << post_interceptor_
->GetRequestsAsString();
1011 post_interceptor_
->GetRequests()[4].find(
1012 "<app appid=\"ihfokbkgjpifnbbojhneepfflplebdkc\" version=\"2.0\">"
1013 "<updatecheck /><packages><package fp=\"22\"/></packages></app>"))
1014 << post_interceptor_
->GetRequestsAsString();
1015 component_updater()->Stop();
1018 // Verify that component installation falls back to downloading and installing
1019 // a full update if the differential update fails (in this case, because the
1020 // installer does not know about the existing files). We do two loops; the final
1021 // loop should do nothing.
1022 // We also check that exactly 4 non-ping network requests are issued:
1023 // 1- update check (loop 1)
1024 // 2- download differential crx
1025 // 3- download full crx
1026 // 4- update check (loop 2 - no update available)
1027 // There should be one ping for the first attempted update.
1028 // This test is flaky on Android. crbug.com/329883
1029 #if defined(OS_ANDROID)
1030 #define MAYBE_DifferentialUpdateFails DISABLED_DifferentialUpdateFails
1032 #define MAYBE_DifferentialUpdateFails DifferentialUpdateFails
1034 TEST_F(ComponentUpdaterTest
, MAYBE_DifferentialUpdateFails
) {
1035 EXPECT_TRUE(post_interceptor_
->ExpectRequest(
1036 new PartialMatch("updatecheck"),
1037 test_file("updatecheck_diff_reply_2.xml")));
1038 EXPECT_TRUE(post_interceptor_
->ExpectRequest(new PartialMatch("event")));
1039 EXPECT_TRUE(post_interceptor_
->ExpectRequest(
1040 new PartialMatch("updatecheck"),
1041 test_file("updatecheck_diff_reply_3.xml")));
1043 get_interceptor_
->SetResponse(
1044 GURL("http://localhost/download/ihfokbkgjpifnbbojhneepfflplebdkc_1.crx"),
1045 test_file("ihfokbkgjpifnbbojhneepfflplebdkc_1.crx"));
1046 get_interceptor_
->SetResponse(
1047 GURL("http://localhost/download/"
1048 "ihfokbkgjpifnbbojhneepfflplebdkc_1to2.crx"),
1049 test_file("ihfokbkgjpifnbbojhneepfflplebdkc_1to2.crx"));
1050 get_interceptor_
->SetResponse(
1051 GURL("http://localhost/download/ihfokbkgjpifnbbojhneepfflplebdkc_2.crx"),
1052 test_file("ihfokbkgjpifnbbojhneepfflplebdkc_2.crx"));
1054 TestInstaller installer
;
1056 RegisterComponent(&com
, kTestComponent_ihfo
, Version("1.0"), &installer
);
1058 test_configurator()->SetLoopCount(2);
1059 component_updater()->Start();
1062 // A failed differential update does not count as a failed install.
1063 EXPECT_EQ(0, static_cast<TestInstaller
*>(com
.installer
)->error());
1064 EXPECT_EQ(1, static_cast<TestInstaller
*>(com
.installer
)->install_count());
1066 EXPECT_EQ(3, post_interceptor_
->GetHitCount())
1067 << post_interceptor_
->GetRequestsAsString();
1068 EXPECT_EQ(3, post_interceptor_
->GetCount())
1069 << post_interceptor_
->GetRequestsAsString();
1070 EXPECT_EQ(2, get_interceptor_
->GetHitCount());
1074 post_interceptor_
->GetRequests()[0].find(
1075 "<app appid=\"ihfokbkgjpifnbbojhneepfflplebdkc\" version=\"1.0\">"
1076 "<updatecheck /></app>"))
1077 << post_interceptor_
->GetRequestsAsString();
1080 post_interceptor_
->GetRequests()[1].find(
1081 "<app appid=\"ihfokbkgjpifnbbojhneepfflplebdkc\" "
1082 "version=\"1.0\" nextversion=\"2.0\">"
1083 "<event eventtype=\"3\" eventresult=\"1\" diffresult=\"0\" "
1084 "differrorcat=\"2\" differrorcode=\"16\" nextfp=\"22\"/>"))
1085 << post_interceptor_
->GetRequestsAsString();
1088 post_interceptor_
->GetRequests()[2].find(
1089 "<app appid=\"ihfokbkgjpifnbbojhneepfflplebdkc\" version=\"2.0\">"
1090 "<updatecheck /><packages><package fp=\"22\"/></packages></app>"))
1091 << post_interceptor_
->GetRequestsAsString();
1093 component_updater()->Stop();
1096 // Test is flakey on Android bots. See crbug.com/331420.
1097 #if defined(OS_ANDROID)
1098 #define MAYBE_CheckFailedInstallPing DISABLED_CheckFailedInstallPing
1100 #define MAYBE_CheckFailedInstallPing CheckFailedInstallPing
1102 // Verify that a failed installation causes an install failure ping.
1103 TEST_F(ComponentUpdaterTest
, MAYBE_CheckFailedInstallPing
) {
1104 // This test installer reports installation failure.
1105 class : public TestInstaller
{
1106 virtual bool Install(const base::DictionaryValue
& manifest
,
1107 const base::FilePath
& unpack_path
) OVERRIDE
{
1109 base::DeleteFile(unpack_path
, true);
1114 EXPECT_TRUE(post_interceptor_
->ExpectRequest(
1115 new PartialMatch("updatecheck"), test_file("updatecheck_reply_1.xml")));
1116 EXPECT_TRUE(post_interceptor_
->ExpectRequest(new PartialMatch("event")));
1117 EXPECT_TRUE(post_interceptor_
->ExpectRequest(
1118 new PartialMatch("updatecheck"), test_file("updatecheck_reply_1.xml")));
1119 EXPECT_TRUE(post_interceptor_
->ExpectRequest(new PartialMatch("event")));
1120 get_interceptor_
->SetResponse(
1121 GURL(expected_crx_url
),
1122 test_file("jebgalgnebhfojomionfpkfelancnnkf.crx"));
1124 // Start with 0.9, and attempt update to 1.0.
1125 // Loop twice to issue two checks: (1) with original 0.9 version
1126 // and (2), which should retry with 0.9.
1128 RegisterComponent(&com
, kTestComponent_jebg
, Version("0.9"), &installer
);
1130 test_configurator()->SetLoopCount(2);
1131 component_updater()->Start();
1134 EXPECT_EQ(4, post_interceptor_
->GetHitCount())
1135 << post_interceptor_
->GetRequestsAsString();
1136 EXPECT_EQ(2, get_interceptor_
->GetHitCount());
1140 post_interceptor_
->GetRequests()[0].find(
1141 "<app appid=\"jebgalgnebhfojomionfpkfelancnnkf\" version=\"0.9\">"
1142 "<updatecheck /></app>"))
1143 << post_interceptor_
->GetRequestsAsString();
1146 post_interceptor_
->GetRequests()[1].find(
1147 "<app appid=\"jebgalgnebhfojomionfpkfelancnnkf\" "
1148 "version=\"0.9\" nextversion=\"1.0\">"
1149 "<event eventtype=\"3\" eventresult=\"0\" "
1150 "errorcat=\"3\" errorcode=\"9\"/>"))
1151 << post_interceptor_
->GetRequestsAsString();
1154 post_interceptor_
->GetRequests()[2].find(
1155 "<app appid=\"jebgalgnebhfojomionfpkfelancnnkf\" version=\"0.9\">"
1156 "<updatecheck /></app>"))
1157 << post_interceptor_
->GetRequestsAsString();
1160 post_interceptor_
->GetRequests()[3].find(
1161 "<app appid=\"jebgalgnebhfojomionfpkfelancnnkf\" "
1162 "version=\"0.9\" nextversion=\"1.0\">"
1163 "<event eventtype=\"3\" eventresult=\"0\" "
1164 "errorcat=\"3\" errorcode=\"9\"/>"))
1165 << post_interceptor_
->GetRequestsAsString();
1167 // Loop once more, but expect no ping because a noupdate response is issued.
1168 // This is necessary to clear out the fire-and-forget ping from the previous
1170 post_interceptor_
->Reset();
1171 EXPECT_TRUE(post_interceptor_
->ExpectRequest(
1172 new PartialMatch("updatecheck"),
1173 test_file("updatecheck_reply_noupdate.xml")));
1175 test_configurator()->SetLoopCount(1);
1176 component_updater()->Start();
1179 EXPECT_EQ(0, static_cast<TestInstaller
*>(com
.installer
)->error());
1180 EXPECT_EQ(2, static_cast<TestInstaller
*>(com
.installer
)->install_count());
1182 EXPECT_EQ(1, post_interceptor_
->GetHitCount())
1183 << post_interceptor_
->GetRequestsAsString();
1184 EXPECT_EQ(1, post_interceptor_
->GetCount())
1185 << post_interceptor_
->GetRequestsAsString();
1189 post_interceptor_
->GetRequests()[0].find(
1190 "<app appid=\"jebgalgnebhfojomionfpkfelancnnkf\" version=\"0.9\">"
1191 "<updatecheck /></app>"))
1192 << post_interceptor_
->GetRequestsAsString();
1194 component_updater()->Stop();
1197 // Verify that we successfully propagate a patcher error.
1198 // ihfokbkgjpifnbbojhneepfflplebdkc_1to2_bad.crx contains an incorrect
1199 // patching instruction that should fail.
1200 TEST_F(ComponentUpdaterTest
, DifferentialUpdateFailErrorcode
) {
1201 EXPECT_TRUE(post_interceptor_
->ExpectRequest(
1202 new PartialMatch("updatecheck"),
1203 test_file("updatecheck_diff_reply_1.xml")));
1204 EXPECT_TRUE(post_interceptor_
->ExpectRequest(new PartialMatch("event")));
1205 EXPECT_TRUE(post_interceptor_
->ExpectRequest(
1206 new PartialMatch("updatecheck"),
1207 test_file("updatecheck_diff_reply_2.xml")));
1208 EXPECT_TRUE(post_interceptor_
->ExpectRequest(new PartialMatch("event")));
1209 EXPECT_TRUE(post_interceptor_
->ExpectRequest(
1210 new PartialMatch("updatecheck"),
1211 test_file("updatecheck_diff_reply_3.xml")));
1213 get_interceptor_
->SetResponse(
1214 GURL("http://localhost/download/ihfokbkgjpifnbbojhneepfflplebdkc_1.crx"),
1215 test_file("ihfokbkgjpifnbbojhneepfflplebdkc_1.crx"));
1216 // This intercept returns a different file than what is specified in the
1217 // update check response and requested in the download. The file that is
1218 // actually dowloaded contains a patching error, an therefore, an error
1219 // is injected at the time of patching.
1220 get_interceptor_
->SetResponse(
1221 GURL("http://localhost/download/"
1222 "ihfokbkgjpifnbbojhneepfflplebdkc_1to2.crx"),
1223 test_file("ihfokbkgjpifnbbojhneepfflplebdkc_1to2_bad.crx"));
1224 get_interceptor_
->SetResponse(
1225 GURL("http://localhost/download/ihfokbkgjpifnbbojhneepfflplebdkc_2.crx"),
1226 test_file("ihfokbkgjpifnbbojhneepfflplebdkc_2.crx"));
1228 VersionedTestInstaller installer
;
1230 RegisterComponent(&com
, kTestComponent_ihfo
, Version("0.0"), &installer
);
1232 test_configurator()->SetLoopCount(3);
1233 component_updater()->Start();
1235 component_updater()->Stop();
1237 EXPECT_EQ(0, static_cast<TestInstaller
*>(com
.installer
)->error());
1238 EXPECT_EQ(2, static_cast<TestInstaller
*>(com
.installer
)->install_count());
1240 EXPECT_EQ(5, post_interceptor_
->GetHitCount())
1241 << post_interceptor_
->GetRequestsAsString();
1242 EXPECT_EQ(5, post_interceptor_
->GetCount())
1243 << post_interceptor_
->GetRequestsAsString();
1244 EXPECT_EQ(3, get_interceptor_
->GetHitCount());
1248 post_interceptor_
->GetRequests()[0].find(
1249 "<app appid=\"ihfokbkgjpifnbbojhneepfflplebdkc\" version=\"0.0\">"
1250 "<updatecheck /></app>"))
1251 << post_interceptor_
->GetRequestsAsString();
1254 post_interceptor_
->GetRequests()[1].find(
1255 "<app appid=\"ihfokbkgjpifnbbojhneepfflplebdkc\" "
1256 "version=\"0.0\" nextversion=\"1.0\">"
1257 "<event eventtype=\"3\" eventresult=\"1\" nextfp=\"1\"/>"))
1258 << post_interceptor_
->GetRequestsAsString();
1261 post_interceptor_
->GetRequests()[2].find(
1262 "<app appid=\"ihfokbkgjpifnbbojhneepfflplebdkc\" version=\"1.0\">"
1263 "<updatecheck /><packages><package fp=\"1\"/></packages></app>"))
1264 << post_interceptor_
->GetRequestsAsString();
1267 post_interceptor_
->GetRequests()[3].find(
1268 "<app appid=\"ihfokbkgjpifnbbojhneepfflplebdkc\" "
1269 "version=\"1.0\" nextversion=\"2.0\">"
1270 "<event eventtype=\"3\" eventresult=\"1\" "
1271 "diffresult=\"0\" differrorcat=\"2\" "
1272 "differrorcode=\"14\" diffextracode1=\"305\" "
1273 "previousfp=\"1\" nextfp=\"22\"/>"))
1274 << post_interceptor_
->GetRequestsAsString();
1277 post_interceptor_
->GetRequests()[4].find(
1278 "<app appid=\"ihfokbkgjpifnbbojhneepfflplebdkc\" version=\"2.0\">"
1279 "<updatecheck /><packages><package fp=\"22\"/></packages></app>"))
1280 << post_interceptor_
->GetRequestsAsString();
1283 class TestResourceController
: public content::ResourceController
{
1285 virtual void SetThrottle(content::ResourceThrottle
* throttle
) {}
1288 content::ResourceThrottle
* RequestTestResourceThrottle(
1289 ComponentUpdateService
* cus
,
1290 TestResourceController
* controller
,
1291 const char* crx_id
) {
1292 net::TestURLRequestContext context
;
1293 net::TestURLRequest
url_request(GURL("http://foo.example.com/thing.bin"),
1294 net::DEFAULT_PRIORITY
,
1298 content::ResourceThrottle
* rt
=
1299 cus
->GetOnDemandResourceThrottle(&url_request
, crx_id
);
1300 rt
->set_controller_for_testing(controller
);
1301 controller
->SetThrottle(rt
);
1305 void RequestAndDeleteResourceThrottle(ComponentUpdateService
* cus
,
1306 const char* crx_id
) {
1307 // By requesting a throttle and deleting it immediately we ensure that we
1308 // hit the case where the component updater tries to use the weak
1309 // pointer to a dead Resource throttle.
1310 class NoCallResourceController
: public TestResourceController
{
1312 virtual ~NoCallResourceController() {}
1313 virtual void Cancel() OVERRIDE
{ CHECK(false); }
1314 virtual void CancelAndIgnore() OVERRIDE
{ CHECK(false); }
1315 virtual void CancelWithError(int error_code
) OVERRIDE
{ CHECK(false); }
1316 virtual void Resume() OVERRIDE
{ CHECK(false); }
1319 delete RequestTestResourceThrottle(cus
, &controller
, crx_id
);
1322 TEST_F(ComponentUpdaterTest
, ResourceThrottleDeletedNoUpdate
) {
1323 MockServiceObserver observer
;
1326 EXPECT_CALL(observer
,
1327 OnEvent(ServiceObserver::COMPONENT_UPDATER_STARTED
, ""))
1329 EXPECT_CALL(observer
,
1330 OnEvent(ServiceObserver::COMPONENT_NOT_UPDATED
,
1331 "abagagagagagagagagagagagagagagag"))
1333 EXPECT_CALL(observer
,
1334 OnEvent(ServiceObserver::COMPONENT_UPDATER_SLEEPING
, ""))
1338 EXPECT_TRUE(post_interceptor_
->ExpectRequest(
1339 new PartialMatch("updatecheck"), test_file("updatecheck_reply_1.xml")));
1341 TestInstaller installer
;
1343 component_updater()->AddObserver(&observer
);
1345 ComponentUpdateService::kOk
,
1346 RegisterComponent(&com
, kTestComponent_abag
, Version("1.1"), &installer
));
1347 // The following two calls ensure that we don't do an update check via the
1348 // timer, so the only update check should be the on-demand one.
1349 test_configurator()->SetInitialDelay(1000000);
1350 test_configurator()->SetRecheckTime(1000000);
1351 test_configurator()->SetLoopCount(1);
1352 component_updater()->Start();
1354 RunThreadsUntilIdle();
1356 EXPECT_EQ(0, post_interceptor_
->GetHitCount());
1358 BrowserThread::PostTask(BrowserThread::IO
,
1360 base::Bind(&RequestAndDeleteResourceThrottle
,
1361 component_updater(),
1362 "abagagagagagagagagagagagagagagag"));
1366 EXPECT_EQ(1, post_interceptor_
->GetHitCount());
1367 EXPECT_EQ(0, static_cast<TestInstaller
*>(com
.installer
)->error());
1368 EXPECT_EQ(0, static_cast<TestInstaller
*>(com
.installer
)->install_count());
1370 component_updater()->Stop();
1373 class CancelResourceController
: public TestResourceController
{
1375 CancelResourceController() : throttle_(NULL
), resume_called_(0) {}
1376 virtual ~CancelResourceController() {
1377 // Check that the throttle has been resumed by the time we
1379 CHECK_EQ(1, resume_called_
);
1382 virtual void Cancel() OVERRIDE
{ CHECK(false); }
1383 virtual void CancelAndIgnore() OVERRIDE
{ CHECK(false); }
1384 virtual void CancelWithError(int error_code
) OVERRIDE
{ CHECK(false); }
1385 virtual void Resume() OVERRIDE
{
1386 BrowserThread::PostTask(BrowserThread::IO
,
1388 base::Bind(&CancelResourceController::ResumeCalled
,
1389 base::Unretained(this)));
1391 virtual void SetThrottle(content::ResourceThrottle
* throttle
) OVERRIDE
{
1392 throttle_
= throttle
;
1394 // Initially the throttle is blocked. The CUS needs to run a
1395 // task on the UI thread to decide if it should unblock.
1396 throttle_
->WillStartRequest(&defer
);
1401 void ResumeCalled() { ++resume_called_
; }
1403 content::ResourceThrottle
* throttle_
;
1407 TEST_F(ComponentUpdaterTest
, ResourceThrottleLiveNoUpdate
) {
1408 MockServiceObserver observer
;
1411 EXPECT_CALL(observer
,
1412 OnEvent(ServiceObserver::COMPONENT_UPDATER_STARTED
, ""))
1414 EXPECT_CALL(observer
,
1415 OnEvent(ServiceObserver::COMPONENT_NOT_UPDATED
,
1416 "abagagagagagagagagagagagagagagag"))
1418 EXPECT_CALL(observer
,
1419 OnEvent(ServiceObserver::COMPONENT_UPDATER_SLEEPING
, ""))
1423 EXPECT_TRUE(post_interceptor_
->ExpectRequest(
1424 new PartialMatch("updatecheck"), test_file("updatecheck_reply_1.xml")));
1426 TestInstaller installer
;
1428 component_updater()->AddObserver(&observer
);
1430 ComponentUpdateService::kOk
,
1431 RegisterComponent(&com
, kTestComponent_abag
, Version("1.1"), &installer
));
1432 // The following two calls ensure that we don't do an update check via the
1433 // timer, so the only update check should be the on-demand one.
1434 test_configurator()->SetInitialDelay(1000000);
1435 test_configurator()->SetRecheckTime(1000000);
1436 test_configurator()->SetLoopCount(1);
1437 component_updater()->Start();
1439 RunThreadsUntilIdle();
1441 EXPECT_EQ(0, post_interceptor_
->GetHitCount());
1443 CancelResourceController controller
;
1445 BrowserThread::PostTask(
1448 base::Bind(base::IgnoreResult(&RequestTestResourceThrottle
),
1449 component_updater(),
1451 "abagagagagagagagagagagagagagagag"));
1455 EXPECT_EQ(1, post_interceptor_
->GetHitCount());
1456 EXPECT_EQ(0, static_cast<TestInstaller
*>(com
.installer
)->error());
1457 EXPECT_EQ(0, static_cast<TestInstaller
*>(com
.installer
)->install_count());
1459 component_updater()->Stop();
1462 // Tests adding and removing observers.
1463 TEST_F(ComponentUpdaterTest
, Observer
) {
1464 MockServiceObserver observer1
, observer2
;
1466 // Expect that two observers see the events.
1469 EXPECT_CALL(observer1
,
1470 OnEvent(ServiceObserver::COMPONENT_UPDATER_STARTED
, ""))
1472 EXPECT_CALL(observer2
,
1473 OnEvent(ServiceObserver::COMPONENT_UPDATER_STARTED
, ""))
1475 EXPECT_CALL(observer1
,
1476 OnEvent(ServiceObserver::COMPONENT_NOT_UPDATED
,
1477 "abagagagagagagagagagagagagagagag"))
1479 EXPECT_CALL(observer2
,
1480 OnEvent(ServiceObserver::COMPONENT_NOT_UPDATED
,
1481 "abagagagagagagagagagagagagagagag"))
1483 EXPECT_CALL(observer1
,
1484 OnEvent(ServiceObserver::COMPONENT_UPDATER_SLEEPING
, ""))
1486 EXPECT_CALL(observer2
,
1487 OnEvent(ServiceObserver::COMPONENT_UPDATER_SLEEPING
, ""))
1491 EXPECT_TRUE(post_interceptor_
->ExpectRequest(
1492 new PartialMatch("updatecheck"), test_file("updatecheck_reply_1.xml")));
1494 component_updater()->AddObserver(&observer1
);
1495 component_updater()->AddObserver(&observer2
);
1497 TestInstaller installer
;
1500 ComponentUpdateService::kOk
,
1501 RegisterComponent(&com
, kTestComponent_abag
, Version("1.1"), &installer
));
1502 test_configurator()->SetLoopCount(1);
1503 component_updater()->Start();
1506 // After removing the first observer, it's only the second observer that
1508 EXPECT_TRUE(Mock::VerifyAndClearExpectations(&observer1
));
1509 EXPECT_TRUE(Mock::VerifyAndClearExpectations(&observer2
));
1512 EXPECT_CALL(observer2
,
1513 OnEvent(ServiceObserver::COMPONENT_UPDATER_STARTED
, ""))
1515 EXPECT_CALL(observer2
,
1516 OnEvent(ServiceObserver::COMPONENT_NOT_UPDATED
,
1517 "abagagagagagagagagagagagagagagag"))
1519 EXPECT_CALL(observer2
,
1520 OnEvent(ServiceObserver::COMPONENT_UPDATER_SLEEPING
, ""))
1524 component_updater()->RemoveObserver(&observer1
);
1526 test_configurator()->SetLoopCount(1);
1527 component_updater()->Start();
1530 // Both observers are removed and no one gets the events.
1531 EXPECT_TRUE(Mock::VerifyAndClearExpectations(&observer1
));
1532 EXPECT_TRUE(Mock::VerifyAndClearExpectations(&observer2
));
1533 component_updater()->RemoveObserver(&observer2
);
1535 test_configurator()->SetLoopCount(1);
1536 component_updater()->Start();
1539 component_updater()->Stop();
1542 } // namespace component_updater