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"
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"
35 using ::testing::AnyNumber
;
36 using ::testing::InSequence
;
37 using ::testing::Mock
;
39 using content::BrowserThread
;
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
),
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();
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
) {
108 PathService::Get(base::DIR_SOURCE_ROOT
, &path
);
109 return path
.AppendASCII("components")
112 .AppendASCII("update_client")
116 scoped_refptr
<update_client::TestConfigurator
>
117 ComponentUpdaterTest::test_configurator() {
121 ComponentUpdateService::Status
ComponentUpdaterTest::RegisterComponent(
123 TestComponents component
,
124 const Version
& version
,
125 const scoped_refptr
<TestInstaller
>& installer
) {
127 case kTestComponent_abag
: {
128 com
->name
= "test_abag";
129 com
->pk_hash
.assign(abag_hash
, abag_hash
+ arraysize(abag_hash
));
132 case kTestComponent_jebg
: {
133 com
->name
= "test_jebg";
134 com
->pk_hash
.assign(jebg_hash
, jebg_hash
+ arraysize(jebg_hash
));
137 case kTestComponent_ihfo
: {
138 com
->name
= "test_ihfo";
139 com
->pk_hash
.assign(ihfo_hash
, ihfo_hash
+ arraysize(ihfo_hash
));
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());
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
, ""))
192 EXPECT_CALL(observer
, OnEvent(Events::COMPONENT_UPDATER_SLEEPING
, ""))
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
);
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();
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,
218 EXPECT_EQ(2, post_interceptor_
->GetHitCount())
219 << post_interceptor_
->GetRequestsAsString();
220 EXPECT_EQ(2, post_interceptor_
->GetCount())
221 << post_interceptor_
->GetRequestsAsString();
224 post_interceptor_
->GetRequests()[0].find(
225 "<app appid=\"abagagagagagagagagagagagagagagag\" version=\"1.1\">"
226 "<updatecheck /></app>"))
227 << post_interceptor_
->GetRequestsAsString();
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
, ""))
241 EXPECT_CALL(observer
, OnEvent(Events::COMPONENT_UPDATER_SLEEPING
, ""))
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();
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();
265 post_interceptor_
->GetRequests()[0].find(
266 "<app appid=\"abagagagagagagagagagagagagagagag\" version=\"1.1\">"
267 "<updatecheck /></app>"))
268 << post_interceptor_
->GetRequestsAsString();
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:
287 // 4- second update check.
288 TEST_F(ComponentUpdaterTest
, InstallCrx
) {
289 MockServiceObserver observer
;
292 EXPECT_CALL(observer
, OnEvent(Events::COMPONENT_UPDATER_STARTED
, ""))
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"))
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
, ""))
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
, ""))
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
);
329 RegisterComponent(&com1
, kTestComponent_jebg
, Version("0.9"), installer1
);
330 scoped_refptr
<TestInstaller
> installer2(new TestInstaller
);
332 RegisterComponent(&com2
, kTestComponent_abag
, Version("2.2"), installer2
);
334 test_configurator()->SetLoopCount(2);
335 component_updater()->Start();
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());
354 post_interceptor_
->GetRequests()[0].find(
355 "<app appid=\"jebgalgnebhfojomionfpkfelancnnkf\" version=\"0.9\">"
356 "<updatecheck /></app>"))
357 << post_interceptor_
->GetRequestsAsString();
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();
373 post_interceptor_
->GetRequests()[2].find(
374 "<app appid=\"jebgalgnebhfojomionfpkfelancnnkf\" version=\"1.0\">"
375 "<updatecheck /></app>"));
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|
395 // <?xml version="1.0" encoding="UTF-8"?>
396 // <request... os=... arch=... prodchannel=... prodversion=...>
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
);
430 RegisterComponent(&com
, kTestComponent_jebg
, Version("0.9"), installer
);
432 test_configurator()->SetLoopCount(1);
433 component_updater()->Start();
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
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
461 #define MAYBE_OnDemandUpdate OnDemandUpdate
463 TEST_F(ComponentUpdaterTest
, MAYBE_OnDemandUpdate
) {
464 MockServiceObserver observer
;
467 EXPECT_CALL(observer
, OnEvent(Events::COMPONENT_UPDATER_STARTED
, ""))
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
, ""))
475 EXPECT_CALL(observer
, OnEvent(Events::COMPONENT_UPDATER_STARTED
, ""))
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"))
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
, ""))
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
);
503 RegisterComponent(&com1
, kTestComponent_abag
, Version("2.2"), installer1
);
504 scoped_refptr
<TestInstaller
> installer2(new TestInstaller
);
506 RegisterComponent(&com2
, kTestComponent_jebg
, Version("0.9"), installer2
);
508 // No update normally.
509 test_configurator()->SetLoopCount(1);
510 component_updater()->Start();
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();
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();
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
));
577 EXPECT_CALL(observer
, OnEvent(Events::COMPONENT_UPDATER_STARTED
, ""))
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
, ""))
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
)));
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
));
608 EXPECT_CALL(observer
, OnEvent(Events::COMPONENT_UPDATER_STARTED
, ""))
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
, ""))
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
)));
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
;
642 EXPECT_CALL(observer
, OnEvent(Events::COMPONENT_UPDATER_STARTED
, ""))
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"))
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
, ""))
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
, ""))
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
);
679 RegisterComponent(&com1
, kTestComponent_jebg
, Version("0.9"), installer1
);
680 scoped_refptr
<TestInstaller
> installer2(new TestInstaller
);
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();
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());
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();
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
));
723 EXPECT_CALL(observer
, OnEvent(Events::COMPONENT_UPDATER_STARTED
, ""))
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
, ""))
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();
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();
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
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(
796 "http://localhost/download/"
797 "ihfokbkgjpifnbbojhneepfflplebdkc_1to2.crx"),
798 test_file("ihfokbkgjpifnbbojhneepfflplebdkc_1to2.crx"));
800 scoped_refptr
<TestInstaller
> installer(new VersionedTestInstaller
);
802 RegisterComponent(&com
, kTestComponent_ihfo
, Version("0.0"), installer
);
804 test_configurator()->SetLoopCount(3);
805 component_updater()->Start();
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());
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();
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();
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
865 #define MAYBE_DifferentialUpdateFails DifferentialUpdateFails
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(
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
);
890 RegisterComponent(&com
, kTestComponent_ihfo
, Version("1.0"), installer
);
892 test_configurator()->SetLoopCount(2);
893 component_updater()->Start();
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());
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();
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
933 #define MAYBE_CheckFailedInstallPing CheckFailedInstallPing
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
{
942 base::DeleteFile(unpack_path
, true);
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.
965 RegisterComponent(&com
, kTestComponent_jebg
, Version("0.9"), installer
);
967 test_configurator()->SetLoopCount(2);
968 component_updater()->Start();
971 EXPECT_EQ(4, post_interceptor_
->GetHitCount())
972 << post_interceptor_
->GetRequestsAsString();
973 EXPECT_EQ(2, get_interceptor_
->GetHitCount());
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();
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
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();
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();
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(
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
);
1064 RegisterComponent(&com
, kTestComponent_ihfo
, Version("0.0"), installer
);
1066 test_configurator()->SetLoopCount(3);
1067 component_updater()->Start();
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());
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();
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();
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
{
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
);
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
{
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); }
1147 delete RequestTestResourceThrottle(cus
, &controller
, crx_id
);
1150 TEST_F(ComponentUpdaterTest
, ResourceThrottleDeletedNoUpdate
) {
1151 MockServiceObserver observer
;
1154 EXPECT_CALL(observer
, OnEvent(Events::COMPONENT_UPDATER_STARTED
, ""))
1156 EXPECT_CALL(observer
, OnEvent(Events::COMPONENT_NOT_UPDATED
,
1157 "abagagagagagagagagagagagagagagag")).Times(1);
1158 EXPECT_CALL(observer
, OnEvent(Events::COMPONENT_UPDATER_SLEEPING
, ""))
1162 EXPECT_TRUE(post_interceptor_
->ExpectRequest(
1163 new PartialMatch("updatecheck"), test_file("updatecheck_reply_1.xml")));
1165 scoped_refptr
<TestInstaller
> installer(new TestInstaller
);
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"));
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
{
1197 CancelResourceController() : throttle_(NULL
), resume_called_(0) {}
1198 ~CancelResourceController() override
{
1199 // Check that the throttle has been resumed by the time we
1201 CHECK_EQ(1, resume_called_
);
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
;
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
);
1222 void ResumeCalled() { ++resume_called_
; }
1224 content::ResourceThrottle
* throttle_
;
1228 // Tests the on-demand update with resource throttle, including the
1229 // cooldown interval between calls.
1230 TEST_F(ComponentUpdaterTest
, ResourceThrottleLiveNoUpdate
) {
1231 MockServiceObserver observer
;
1234 EXPECT_CALL(observer
, OnEvent(Events::COMPONENT_UPDATER_STARTED
, ""))
1236 EXPECT_CALL(observer
, OnEvent(Events::COMPONENT_NOT_UPDATED
,
1237 "abagagagagagagagagagagagagagagag")).Times(1);
1238 EXPECT_CALL(observer
, OnEvent(Events::COMPONENT_UPDATER_SLEEPING
, ""))
1240 EXPECT_CALL(observer
, OnEvent(Events::COMPONENT_UPDATER_STARTED
, ""))
1242 EXPECT_CALL(observer
, OnEvent(Events::COMPONENT_NOT_UPDATED
,
1243 "abagagagagagagagagagagagagagagag")).Times(1);
1244 EXPECT_CALL(observer
, OnEvent(Events::COMPONENT_UPDATER_SLEEPING
, ""))
1246 EXPECT_CALL(observer
, OnEvent(Events::COMPONENT_UPDATER_STARTED
, ""))
1250 EXPECT_TRUE(post_interceptor_
->ExpectRequest(
1251 new PartialMatch("updatecheck"), test_file("updatecheck_reply_1.xml")));
1253 scoped_refptr
<TestInstaller
> installer(new TestInstaller
);
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"));
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"));
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.
1335 EXPECT_CALL(observer1
, OnEvent(Events::COMPONENT_UPDATER_STARTED
, ""))
1337 EXPECT_CALL(observer2
, OnEvent(Events::COMPONENT_UPDATER_STARTED
, ""))
1339 EXPECT_CALL(observer1
, OnEvent(Events::COMPONENT_NOT_UPDATED
,
1340 "abagagagagagagagagagagagagagagag"))
1342 EXPECT_CALL(observer2
, OnEvent(Events::COMPONENT_NOT_UPDATED
,
1343 "abagagagagagagagagagagagagagagag"))
1345 EXPECT_CALL(observer1
, OnEvent(Events::COMPONENT_UPDATER_SLEEPING
, ""))
1347 EXPECT_CALL(observer2
, OnEvent(Events::COMPONENT_UPDATER_SLEEPING
, ""))
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
);
1359 EXPECT_EQ(Status::kOk
, RegisterComponent(&com
, kTestComponent_abag
,
1360 Version("1.1"), installer
));
1361 test_configurator()->SetLoopCount(1);
1362 component_updater()->Start();
1365 // After removing the first observer, it's only the second observer that
1367 EXPECT_TRUE(Mock::VerifyAndClearExpectations(&observer1
));
1368 EXPECT_TRUE(Mock::VerifyAndClearExpectations(&observer2
));
1371 EXPECT_CALL(observer2
, OnEvent(Events::COMPONENT_UPDATER_STARTED
, ""))
1373 EXPECT_CALL(observer2
, OnEvent(Events::COMPONENT_NOT_UPDATED
,
1374 "abagagagagagagagagagagagagagagag"))
1376 EXPECT_CALL(observer2
, OnEvent(Events::COMPONENT_UPDATER_SLEEPING
, ""))
1380 component_updater()->RemoveObserver(&observer1
);
1382 test_configurator()->SetLoopCount(1);
1383 component_updater()->Start();
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();
1395 component_updater()->Stop();
1398 } // namespace component_updater