1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <list>
#include <utility>
#include "base/compiler_specific.h"
#include "base/file_util.h"
#include "base/files/file_path.h"
#include "base/memory/scoped_vector.h"
#include "base/message_loop.h"
#include "base/path_service.h"
#include "base/strings/stringprintf.h"
#include "base/strings/string_number_conversions.h"
#include "base/values.h"
#include "chrome/browser/component_updater/component_updater_service.h"
#include "chrome/browser/component_updater/test/component_patcher_mock.h"
#include "chrome/browser/component_updater/test/component_updater_service_unittest.h"
#include "chrome/browser/component_updater/test/test_installer.h"
#include "chrome/common/chrome_notification_types.h"
#include "chrome/common/chrome_paths.h"
#include "content/public/browser/notification_observer.h"
#include "content/public/browser/notification_service.h"
#include "content/public/test/test_browser_thread.h"
#include "content/public/test/test_notification_tracker.h"
#include "content/test/net/url_request_prepackaged_interceptor.h"
#include "googleurl/src/gurl.h"
#include "libxml/globals.h"
#include "net/base/upload_bytes_element_reader.h"
#include "net/url_request/url_fetcher.h"
#include "net/url_request/url_request.h"
#include "net/url_request/url_request_filter.h"
#include "net/url_request/url_request_simple_job.h"
#include "net/url_request/url_request_test_util.h"
#include "testing/gtest/include/gtest/gtest.h"
using content::BrowserThread;
using content::TestNotificationTracker;
TestConfigurator::TestConfigurator()
: times_(1), recheck_time_(0), ondemand_time_(0), cus_(NULL) {
}
TestConfigurator::~TestConfigurator() {
}
int TestConfigurator::InitialDelay() { return 0; }
int TestConfigurator::NextCheckDelay() {
// This is called when a new full cycle of checking for updates is going
// to happen. In test we normally only test one cycle so it is a good
// time to break from the test messageloop Run() method so the test can
// finish.
if (--times_ <= 0) {
base::MessageLoop::current()->Quit();
return 0;
}
// Look for checks to issue in the middle of the loop.
for (std::list<CheckAtLoopCount>::iterator
i = components_to_check_.begin();
i != components_to_check_.end(); ) {
if (i->second == times_) {
cus_->CheckForUpdateSoon(*i->first);
i = components_to_check_.erase(i);
} else {
++i;
}
}
return 1;
}
int TestConfigurator::StepDelay() {
return 0;
}
int TestConfigurator::MinimumReCheckWait() {
return recheck_time_;
}
int TestConfigurator::OnDemandDelay() {
return ondemand_time_;
}
GURL TestConfigurator::UpdateUrl() {
return GURL("http://localhost/upd");
}
const char* TestConfigurator::ExtraRequestParams() { return "extra=foo"; }
size_t TestConfigurator::UrlSizeLimit() { return 256; }
net::URLRequestContextGetter* TestConfigurator::RequestContext() {
return new net::TestURLRequestContextGetter(
BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO));
}
// Don't use the utility process to decode files.
bool TestConfigurator::InProcess() { return true; }
void TestConfigurator::OnEvent(Events event, int extra) { }
ComponentPatcher* TestConfigurator::CreateComponentPatcher() {
return new MockComponentPatcher();
}
bool TestConfigurator::DeltasEnabled() const {
return true;
}
// Set how many update checks are called, the default value is just once.
void TestConfigurator::SetLoopCount(int times) { times_ = times; }
void TestConfigurator::SetRecheckTime(int seconds) {
recheck_time_ = seconds;
}
void TestConfigurator::SetOnDemandTime(int seconds) {
ondemand_time_ = seconds;
}
void TestConfigurator::AddComponentToCheck(CrxComponent* com,
int at_loop_iter) {
components_to_check_.push_back(std::make_pair(com, at_loop_iter));
}
void TestConfigurator::SetComponentUpdateService(ComponentUpdateService* cus) {
cus_ = cus;
}
ComponentUpdaterTest::ComponentUpdaterTest() : test_config_(NULL) {
// The component updater instance under test.
test_config_ = new TestConfigurator;
component_updater_.reset(ComponentUpdateServiceFactory(test_config_));
test_config_->SetComponentUpdateService(component_updater_.get());
// The test directory is chrome/test/data/components.
PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir_);
test_data_dir_ = test_data_dir_.AppendASCII("components");
// Subscribe to all component updater notifications.
const int notifications[] = {
chrome::NOTIFICATION_COMPONENT_UPDATER_STARTED,
chrome::NOTIFICATION_COMPONENT_UPDATER_SLEEPING,
chrome::NOTIFICATION_COMPONENT_UPDATE_FOUND,
chrome::NOTIFICATION_COMPONENT_UPDATE_READY
};
for (int ix = 0; ix != arraysize(notifications); ++ix) {
notification_tracker_.ListenFor(
notifications[ix], content::NotificationService::AllSources());
}
net::URLFetcher::SetEnableInterceptionForTests(true);
}
ComponentUpdaterTest::~ComponentUpdaterTest() {
net::URLFetcher::SetEnableInterceptionForTests(false);
}
void ComponentUpdaterTest::TearDown() {
xmlCleanupGlobals();
}
ComponentUpdateService* ComponentUpdaterTest::component_updater() {
return component_updater_.get();
}
// Makes the full path to a component updater test file.
const base::FilePath ComponentUpdaterTest::test_file(const char* file) {
return test_data_dir_.AppendASCII(file);
}
TestNotificationTracker& ComponentUpdaterTest::notification_tracker() {
return notification_tracker_;
}
TestConfigurator* ComponentUpdaterTest::test_configurator() {
return test_config_;
}
ComponentUpdateService::Status ComponentUpdaterTest::RegisterComponent(
CrxComponent* com,
TestComponents component,
const Version& version,
TestInstaller* installer) {
if (component == kTestComponent_abag) {
com->name = "test_abag";
com->pk_hash.assign(abag_hash, abag_hash + arraysize(abag_hash));
} else if (component == kTestComponent_jebg) {
com->name = "test_jebg";
com->pk_hash.assign(jebg_hash, jebg_hash + arraysize(jebg_hash));
} else {
com->name = "test_ihfo";
com->pk_hash.assign(ihfo_hash, ihfo_hash + arraysize(ihfo_hash));
}
com->version = version;
com->installer = installer;
return component_updater_->RegisterComponent(*com);
}
// Verify that our test fixture work and the component updater can
// be created and destroyed with no side effects.
TEST_F(ComponentUpdaterTest, VerifyFixture) {
EXPECT_TRUE(component_updater() != NULL);
EXPECT_EQ(0ul, notification_tracker().size());
}
// Verify that the component updater can be caught in a quick
// start-shutdown situation. Failure of this test will be a crash. Also
// if there is no work to do, there are no notifications generated.
TEST_F(ComponentUpdaterTest, StartStop) {
base::MessageLoop message_loop;
content::TestBrowserThread ui_thread(BrowserThread::UI, &message_loop);
component_updater()->Start();
message_loop.RunUntilIdle();
component_updater()->Stop();
EXPECT_EQ(0ul, notification_tracker().size());
}
// Verify that when the server has no updates, we go back to sleep and
// the COMPONENT_UPDATER_STARTED and COMPONENT_UPDATER_SLEEPING notifications
// are generated.
TEST_F(ComponentUpdaterTest, CheckCrxSleep) {
base::MessageLoop message_loop;
content::TestBrowserThread ui_thread(BrowserThread::UI, &message_loop);
content::TestBrowserThread file_thread(BrowserThread::FILE);
content::TestBrowserThread io_thread(BrowserThread::IO);
io_thread.StartIOThread();
file_thread.Start();
content::URLLocalHostRequestPrepackagedInterceptor interceptor;
TestInstaller installer;
CrxComponent com;
EXPECT_EQ(ComponentUpdateService::kOk,
RegisterComponent(&com,
kTestComponent_abag,
Version("1.1"),
&installer));
const GURL expected_update_url(
"http://localhost/upd?extra=foo"
"&x=id%3Dabagagagagagagagagagagagagagagag%26v%3D1.1%26fp%3D%26uc");
interceptor.SetResponse(expected_update_url,
test_file("updatecheck_reply_1.xml"));
// We loop twice, but there are no updates so we expect two sleep messages.
test_configurator()->SetLoopCount(2);
component_updater()->Start();
ASSERT_EQ(1ul, notification_tracker().size());
TestNotificationTracker::Event ev1 = notification_tracker().at(0);
EXPECT_EQ(chrome::NOTIFICATION_COMPONENT_UPDATER_STARTED, ev1.type);
message_loop.Run();
ASSERT_EQ(3ul, notification_tracker().size());
TestNotificationTracker::Event ev2 = notification_tracker().at(1);
EXPECT_EQ(chrome::NOTIFICATION_COMPONENT_UPDATER_SLEEPING, ev2.type);
TestNotificationTracker::Event ev3 = notification_tracker().at(2);
EXPECT_EQ(chrome::NOTIFICATION_COMPONENT_UPDATER_SLEEPING, ev2.type);
EXPECT_EQ(2, interceptor.GetHitCount());
EXPECT_EQ(0, static_cast<TestInstaller*>(com.installer)->error());
EXPECT_EQ(0, static_cast<TestInstaller*>(com.installer)->install_count());
component_updater()->Stop();
// Loop twice again but this case we simulate a server error by returning
// an empty file.
interceptor.SetResponse(expected_update_url,
test_file("updatecheck_reply_empty"));
notification_tracker().Reset();
test_configurator()->SetLoopCount(2);
component_updater()->Start();
message_loop.Run();
ASSERT_EQ(3ul, notification_tracker().size());
ev1 = notification_tracker().at(0);
EXPECT_EQ(chrome::NOTIFICATION_COMPONENT_UPDATER_STARTED, ev1.type);
ev2 = notification_tracker().at(1);
EXPECT_EQ(chrome::NOTIFICATION_COMPONENT_UPDATER_SLEEPING, ev2.type);
ev3 = notification_tracker().at(2);
EXPECT_EQ(chrome::NOTIFICATION_COMPONENT_UPDATER_SLEEPING, ev2.type);
EXPECT_EQ(4, interceptor.GetHitCount());
EXPECT_EQ(0, static_cast<TestInstaller*>(com.installer)->error());
EXPECT_EQ(0, static_cast<TestInstaller*>(com.installer)->install_count());
component_updater()->Stop();
}
// Verify that we can check for updates and install one component. Besides
// the notifications above NOTIFICATION_COMPONENT_UPDATE_FOUND and
// NOTIFICATION_COMPONENT_UPDATE_READY should have been fired. We do two loops
// so the second time around there should be nothing left to do.
// We also check that only 3 network requests are issued:
// 1- manifest check
// 2- download crx
// 3- second manifest check.
TEST_F(ComponentUpdaterTest, InstallCrx) {
base::MessageLoop message_loop;
content::TestBrowserThread ui_thread(BrowserThread::UI, &message_loop);
content::TestBrowserThread file_thread(BrowserThread::FILE);
content::TestBrowserThread io_thread(BrowserThread::IO);
io_thread.StartIOThread();
file_thread.Start();
content::URLLocalHostRequestPrepackagedInterceptor interceptor;
TestInstaller installer1;
CrxComponent com1;
RegisterComponent(&com1, kTestComponent_jebg, Version("0.9"), &installer1);
TestInstaller installer2;
CrxComponent com2;
RegisterComponent(&com2, kTestComponent_abag, Version("2.2"), &installer2);
const GURL expected_update_url_1(
"http://localhost/upd?extra=foo"
"&x=id%3Djebgalgnebhfojomionfpkfelancnnkf%26v%3D0.9%26fp%3D%26uc"
"&x=id%3Dabagagagagagagagagagagagagagagag%26v%3D2.2%26fp%3D%26uc");
const GURL expected_update_url_2(
"http://localhost/upd?extra=foo"
"&x=id%3Dabagagagagagagagagagagagagagagag%26v%3D2.2%26fp%3D%26uc"
"&x=id%3Djebgalgnebhfojomionfpkfelancnnkf%26v%3D1.0%26fp%3D%26uc");
interceptor.SetResponse(expected_update_url_1,
test_file("updatecheck_reply_1.xml"));
interceptor.SetResponse(expected_update_url_2,
test_file("updatecheck_reply_1.xml"));
interceptor.SetResponse(GURL(expected_crx_url),
test_file("jebgalgnebhfojomionfpkfelancnnkf.crx"));
test_configurator()->SetLoopCount(2);
component_updater()->Start();
message_loop.Run();
EXPECT_EQ(0, static_cast<TestInstaller*>(com1.installer)->error());
EXPECT_EQ(1, static_cast<TestInstaller*>(com1.installer)->install_count());
EXPECT_EQ(0, static_cast<TestInstaller*>(com2.installer)->error());
EXPECT_EQ(0, static_cast<TestInstaller*>(com2.installer)->install_count());
EXPECT_EQ(3, interceptor.GetHitCount());
ASSERT_EQ(5ul, notification_tracker().size());
TestNotificationTracker::Event ev1 = notification_tracker().at(1);
EXPECT_EQ(chrome::NOTIFICATION_COMPONENT_UPDATE_FOUND, ev1.type);
TestNotificationTracker::Event ev2 = notification_tracker().at(2);
EXPECT_EQ(chrome::NOTIFICATION_COMPONENT_UPDATE_READY, ev2.type);
TestNotificationTracker::Event ev3 = notification_tracker().at(3);
EXPECT_EQ(chrome::NOTIFICATION_COMPONENT_UPDATER_SLEEPING, ev3.type);
TestNotificationTracker::Event ev4 = notification_tracker().at(4);
EXPECT_EQ(chrome::NOTIFICATION_COMPONENT_UPDATER_SLEEPING, ev4.type);
component_updater()->Stop();
}
// This test checks that the "prodversionmin" value is handled correctly. In
// particular there should not be an install because the minimum product
// version is much higher than of chrome.
TEST_F(ComponentUpdaterTest, ProdVersionCheck) {
base::MessageLoop message_loop;
content::TestBrowserThread ui_thread(BrowserThread::UI, &message_loop);
content::TestBrowserThread file_thread(BrowserThread::FILE);
content::TestBrowserThread io_thread(BrowserThread::IO);
io_thread.StartIOThread();
file_thread.Start();
content::URLLocalHostRequestPrepackagedInterceptor interceptor;
TestInstaller installer;
CrxComponent com;
RegisterComponent(&com, kTestComponent_jebg, Version("0.9"), &installer);
const GURL expected_update_url(
"http://localhost/upd?extra=foo&x=id%3D"
"jebgalgnebhfojomionfpkfelancnnkf%26v%3D0.9%26fp%3D%26uc");
interceptor.SetResponse(expected_update_url,
test_file("updatecheck_reply_2.xml"));
interceptor.SetResponse(GURL(expected_crx_url),
test_file("jebgalgnebhfojomionfpkfelancnnkf.crx"));
test_configurator()->SetLoopCount(1);
component_updater()->Start();
message_loop.Run();
EXPECT_EQ(1, interceptor.GetHitCount());
EXPECT_EQ(0, static_cast<TestInstaller*>(com.installer)->error());
EXPECT_EQ(0, static_cast<TestInstaller*>(com.installer)->install_count());
component_updater()->Stop();
}
// Test that a ping for an update check can cause installs.
// Here is the timeline:
// - First loop: we return a reply that indicates no update, so
// nothing happens.
// - We ping.
// - This triggers a second loop, which has a reply that triggers an install.
TEST_F(ComponentUpdaterTest, CheckForUpdateSoon) {
base::MessageLoop message_loop;
content::TestBrowserThread ui_thread(BrowserThread::UI, &message_loop);
content::TestBrowserThread file_thread(BrowserThread::FILE);
content::TestBrowserThread io_thread(BrowserThread::IO);
io_thread.StartIOThread();
file_thread.Start();
content::URLLocalHostRequestPrepackagedInterceptor interceptor;
TestInstaller installer1;
CrxComponent com1;
RegisterComponent(&com1, kTestComponent_abag, Version("2.2"), &installer1);
TestInstaller installer2;
CrxComponent com2;
RegisterComponent(&com2, kTestComponent_jebg, Version("0.9"), &installer2);
const GURL expected_update_url_1(
"http://localhost/upd?extra=foo"
"&x=id%3Dabagagagagagagagagagagagagagagag%26v%3D2.2%26fp%3D%26uc"
"&x=id%3Djebgalgnebhfojomionfpkfelancnnkf%26v%3D0.9%26fp%3D%26uc");
const GURL expected_update_url_2(
"http://localhost/upd?extra=foo"
"&x=id%3Djebgalgnebhfojomionfpkfelancnnkf%26v%3D0.9%26fp%3D%26uc"
"%26installsource%3Dondemand"
"&x=id%3Dabagagagagagagagagagagagagagagag%26v%3D2.2%26fp%3D%26uc");
interceptor.SetResponse(expected_update_url_1,
test_file("updatecheck_reply_empty"));
interceptor.SetResponse(expected_update_url_2,
test_file("updatecheck_reply_1.xml"));
interceptor.SetResponse(GURL(expected_crx_url),
test_file("jebgalgnebhfojomionfpkfelancnnkf.crx"));
// Test success.
test_configurator()->SetLoopCount(2);
test_configurator()->AddComponentToCheck(&com2, 1);
component_updater()->Start();
message_loop.Run();
EXPECT_EQ(0, static_cast<TestInstaller*>(com1.installer)->error());
EXPECT_EQ(0, static_cast<TestInstaller*>(com1.installer)->install_count());
EXPECT_EQ(0, static_cast<TestInstaller*>(com2.installer)->error());
EXPECT_EQ(1, static_cast<TestInstaller*>(com2.installer)->install_count());
EXPECT_EQ(3, interceptor.GetHitCount());
ASSERT_EQ(5ul, notification_tracker().size());
TestNotificationTracker::Event ev0= notification_tracker().at(0);
EXPECT_EQ(chrome::NOTIFICATION_COMPONENT_UPDATER_STARTED, ev0.type);
TestNotificationTracker::Event ev1 = notification_tracker().at(1);
EXPECT_EQ(chrome::NOTIFICATION_COMPONENT_UPDATER_SLEEPING, ev1.type);
TestNotificationTracker::Event ev2 = notification_tracker().at(2);
EXPECT_EQ(chrome::NOTIFICATION_COMPONENT_UPDATE_FOUND, ev2.type);
TestNotificationTracker::Event ev3 = notification_tracker().at(3);
EXPECT_EQ(chrome::NOTIFICATION_COMPONENT_UPDATE_READY, ev3.type);
TestNotificationTracker::Event ev4 = notification_tracker().at(4);
EXPECT_EQ(chrome::NOTIFICATION_COMPONENT_UPDATER_SLEEPING, ev4.type);
// Also check what happens if previous check too soon.
test_configurator()->SetOnDemandTime(60 * 60);
EXPECT_EQ(ComponentUpdateService::kError,
component_updater()->CheckForUpdateSoon(com2));
// Okay, now reset to 0 for the other tests.
test_configurator()->SetOnDemandTime(0);
component_updater()->Stop();
// Test a few error cases. NOTE: We don't have callbacks for
// when the updates failed yet.
const GURL expected_update_url_3(
"http://localhost/upd?extra=foo"
"&x=id%3Djebgalgnebhfojomionfpkfelancnnkf%26v%3D1.0%26fp%3D%26uc"
"&x=id%3Dabagagagagagagagagagagagagagagag%26v%3D2.2%26fp%3D%26uc");
// No update: error from no server response
interceptor.SetResponse(expected_update_url_3,
test_file("updatecheck_reply_empty"));
notification_tracker().Reset();
test_configurator()->SetLoopCount(1);
component_updater()->Start();
EXPECT_EQ(ComponentUpdateService::kOk,
component_updater()->CheckForUpdateSoon(com2));
message_loop.Run();
ASSERT_EQ(2ul, notification_tracker().size());
ev0 = notification_tracker().at(0);
EXPECT_EQ(chrome::NOTIFICATION_COMPONENT_UPDATER_STARTED, ev0.type);
ev1 = notification_tracker().at(1);
EXPECT_EQ(chrome::NOTIFICATION_COMPONENT_UPDATER_SLEEPING, ev1.type);
component_updater()->Stop();
// No update: already updated to 1.0 so nothing new
interceptor.SetResponse(expected_update_url_3,
test_file("updatecheck_reply_1.xml"));
notification_tracker().Reset();
test_configurator()->SetLoopCount(1);
component_updater()->Start();
EXPECT_EQ(ComponentUpdateService::kOk,
component_updater()->CheckForUpdateSoon(com2));
message_loop.Run();
ASSERT_EQ(2ul, notification_tracker().size());
ev0 = notification_tracker().at(0);
EXPECT_EQ(chrome::NOTIFICATION_COMPONENT_UPDATER_STARTED, ev0.type);
ev1 = notification_tracker().at(1);
EXPECT_EQ(chrome::NOTIFICATION_COMPONENT_UPDATER_SLEEPING, ev1.type);
component_updater()->Stop();
}
// Verify that a previously registered component can get re-registered
// with a different version.
TEST_F(ComponentUpdaterTest, CheckReRegistration) {
base::MessageLoop message_loop;
content::TestBrowserThread ui_thread(BrowserThread::UI, &message_loop);
content::TestBrowserThread file_thread(BrowserThread::FILE);
content::TestBrowserThread io_thread(BrowserThread::IO);
io_thread.StartIOThread();
file_thread.Start();
content::URLLocalHostRequestPrepackagedInterceptor interceptor;
TestInstaller installer1;
CrxComponent com1;
RegisterComponent(&com1, kTestComponent_jebg, Version("0.9"), &installer1);
TestInstaller installer2;
CrxComponent com2;
RegisterComponent(&com2, kTestComponent_abag, Version("2.2"), &installer2);
// Start with 0.9, and update to 1.0
const GURL expected_update_url_1(
"http://localhost/upd?extra=foo"
"&x=id%3Djebgalgnebhfojomionfpkfelancnnkf%26v%3D0.9%26fp%3D%26uc"
"&x=id%3Dabagagagagagagagagagagagagagagag%26v%3D2.2%26fp%3D%26uc");
const GURL expected_update_url_2(
"http://localhost/upd?extra=foo"
"&x=id%3Dabagagagagagagagagagagagagagagag%26v%3D2.2%26fp%3D%26uc"
"&x=id%3Djebgalgnebhfojomionfpkfelancnnkf%26v%3D1.0%26fp%3D%26uc");
interceptor.SetResponse(expected_update_url_1,
test_file("updatecheck_reply_1.xml"));
interceptor.SetResponse(expected_update_url_2,
test_file("updatecheck_reply_1.xml"));
interceptor.SetResponse(GURL(expected_crx_url),
test_file("jebgalgnebhfojomionfpkfelancnnkf.crx"));
// Loop twice to issue two checks: (1) with original 0.9 version
// and (2) with the updated 1.0 version.
test_configurator()->SetLoopCount(2);
component_updater()->Start();
message_loop.Run();
EXPECT_EQ(0, static_cast<TestInstaller*>(com1.installer)->error());
EXPECT_EQ(1, static_cast<TestInstaller*>(com1.installer)->install_count());
EXPECT_EQ(0, static_cast<TestInstaller*>(com2.installer)->error());
EXPECT_EQ(0, static_cast<TestInstaller*>(com2.installer)->install_count());
EXPECT_EQ(3, interceptor.GetHitCount());
ASSERT_EQ(5ul, notification_tracker().size());
TestNotificationTracker::Event ev0 = notification_tracker().at(0);
EXPECT_EQ(chrome::NOTIFICATION_COMPONENT_UPDATER_STARTED, ev0.type);
TestNotificationTracker::Event ev1 = notification_tracker().at(1);
EXPECT_EQ(chrome::NOTIFICATION_COMPONENT_UPDATE_FOUND, ev1.type);
TestNotificationTracker::Event ev2 = notification_tracker().at(2);
EXPECT_EQ(chrome::NOTIFICATION_COMPONENT_UPDATE_READY, ev2.type);
TestNotificationTracker::Event ev3 = notification_tracker().at(3);
EXPECT_EQ(chrome::NOTIFICATION_COMPONENT_UPDATER_SLEEPING, ev3.type);
TestNotificationTracker::Event ev4 = notification_tracker().at(4);
EXPECT_EQ(chrome::NOTIFICATION_COMPONENT_UPDATER_SLEEPING, ev4.type);
// Now re-register, pretending to be an even newer version (2.2)
TestInstaller installer3;
component_updater()->Stop();
EXPECT_EQ(ComponentUpdateService::kReplaced,
RegisterComponent(&com1,
kTestComponent_jebg,
Version("2.2"),
&installer3));
// Check that we send out 2.2 as our version.
// Interceptor's hit count should go up by 1.
const GURL expected_update_url_3(
"http://localhost/upd?extra=foo"
"&x=id%3Djebgalgnebhfojomionfpkfelancnnkf%26v%3D2.2%26fp%3D%26uc"
"&x=id%3Dabagagagagagagagagagagagagagagag%26v%3D2.2%26fp%3D%26uc");
interceptor.SetResponse(expected_update_url_3,
test_file("updatecheck_reply_1.xml"));
notification_tracker().Reset();
// Loop once just to notice the check happening with the re-register version.
test_configurator()->SetLoopCount(1);
component_updater()->Start();
message_loop.Run();
ASSERT_EQ(2ul, notification_tracker().size());
ev0 = notification_tracker().at(0);
EXPECT_EQ(chrome::NOTIFICATION_COMPONENT_UPDATER_STARTED, ev0.type);
ev1 = notification_tracker().at(1);
EXPECT_EQ(chrome::NOTIFICATION_COMPONENT_UPDATER_SLEEPING, ev1.type);
EXPECT_EQ(4, interceptor.GetHitCount());
// We created a new installer, so the counts go back to 0.
EXPECT_EQ(0, static_cast<TestInstaller*>(com1.installer)->error());
EXPECT_EQ(0, static_cast<TestInstaller*>(com1.installer)->install_count());
EXPECT_EQ(0, static_cast<TestInstaller*>(com2.installer)->error());
EXPECT_EQ(0, static_cast<TestInstaller*>(com2.installer)->install_count());
component_updater()->Stop();
}
// Verify that component installation falls back to downloading and installing
// a full update if the differential update fails (in this case, because the
// installer does not know about the existing files). We do two loops; the final
// loop should do nothing.
// We also check that exactly 4 network requests are issued:
// 1- update check (loop 1)
// 2- download differential crx
// 3- download full crx
// 4- update check (loop 2 - no update available)
TEST_F(ComponentUpdaterTest, DifferentialUpdateFails) {
base::MessageLoop message_loop;
content::TestBrowserThread ui_thread(BrowserThread::UI, &message_loop);
content::TestBrowserThread file_thread(BrowserThread::FILE);
content::TestBrowserThread io_thread(BrowserThread::IO);
io_thread.StartIOThread();
file_thread.Start();
content::URLLocalHostRequestPrepackagedInterceptor interceptor;
TestInstaller installer;
CrxComponent com;
RegisterComponent(&com, kTestComponent_ihfo, Version("1.0"), &installer);
const GURL expected_update_url_1(
"http://localhost/upd?extra=foo"
"&x=id%3Dihfokbkgjpifnbbojhneepfflplebdkc%26v%3D1.0%26fp%3D%26uc");
const GURL expected_update_url_2(
"http://localhost/upd?extra=foo"
"&x=id%3Dihfokbkgjpifnbbojhneepfflplebdkc%26v%3D2.0%26fp%3Df22%26uc");
const GURL expected_crx_url_1(
"http://localhost/download/ihfokbkgjpifnbbojhneepfflplebdkc_1.crx");
const GURL expected_crx_url_1_diff_2(
"http://localhost/download/ihfokbkgjpifnbbojhneepfflplebdkc_1to2.crx");
const GURL expected_crx_url_2(
"http://localhost/download/ihfokbkgjpifnbbojhneepfflplebdkc_2.crx");
interceptor.SetResponse(expected_update_url_1,
test_file("updatecheck_diff_reply_2.xml"));
interceptor.SetResponse(expected_update_url_2,
test_file("updatecheck_diff_reply_3.xml"));
interceptor.SetResponse(expected_crx_url_1,
test_file("ihfokbkgjpifnbbojhneepfflplebdkc_1.crx"));
interceptor.SetResponse(
expected_crx_url_1_diff_2,
test_file("ihfokbkgjpifnbbojhneepfflplebdkc_1to2.crx"));
interceptor.SetResponse(expected_crx_url_2,
test_file("ihfokbkgjpifnbbojhneepfflplebdkc_2.crx"));
test_configurator()->SetLoopCount(2);
component_updater()->Start();
message_loop.Run();
// A failed differential update does not count as a failed install.
EXPECT_EQ(0, static_cast<TestInstaller*>(com.installer)->error());
EXPECT_EQ(1, static_cast<TestInstaller*>(com.installer)->install_count());
EXPECT_EQ(4, interceptor.GetHitCount());
component_updater()->Stop();
}
// Verify that we successfully propagate a patcher error.
// ihfokbkgjpifnbbojhneepfflplebdkc_1to2_bad.crx contains an incorrect
// patching instruction that should fail.
TEST_F(ComponentUpdaterTest, DifferentialUpdateFailErrorcode) {
base::MessageLoop message_loop;
content::TestBrowserThread ui_thread(BrowserThread::UI, &message_loop);
content::TestBrowserThread file_thread(BrowserThread::FILE);
content::TestBrowserThread io_thread(BrowserThread::IO);
io_thread.StartIOThread();
file_thread.Start();
content::URLLocalHostRequestPrepackagedInterceptor interceptor;
VersionedTestInstaller installer;
CrxComponent com;
RegisterComponent(&com, kTestComponent_ihfo, Version("0.0"), &installer);
const GURL expected_update_url_0(
"http://localhost/upd?extra=foo"
"&x=id%3Dihfokbkgjpifnbbojhneepfflplebdkc%26v%3D0.0%26fp%3D%26uc");
const GURL expected_update_url_1(
"http://localhost/upd?extra=foo"
"&x=id%3Dihfokbkgjpifnbbojhneepfflplebdkc%26v%3D1.0%26fp%3D1%26uc");
const GURL expected_update_url_2(
"http://localhost/upd?extra=foo"
"&x=id%3Dihfokbkgjpifnbbojhneepfflplebdkc%26v%3D2.0%26fp%3Df22%26uc");
const GURL expected_crx_url_1(
"http://localhost/download/ihfokbkgjpifnbbojhneepfflplebdkc_1.crx");
const GURL expected_crx_url_1_diff_2(
"http://localhost/download/ihfokbkgjpifnbbojhneepfflplebdkc_1to2.crx");
const GURL expected_crx_url_2(
"http://localhost/download/ihfokbkgjpifnbbojhneepfflplebdkc_2.crx");
interceptor.SetResponse(expected_update_url_0,
test_file("updatecheck_diff_reply_1.xml"));
interceptor.SetResponse(expected_update_url_1,
test_file("updatecheck_diff_reply_2.xml"));
interceptor.SetResponse(expected_update_url_2,
test_file("updatecheck_diff_reply_3.xml"));
interceptor.SetResponse(expected_crx_url_1,
test_file("ihfokbkgjpifnbbojhneepfflplebdkc_1.crx"));
interceptor.SetResponse(
expected_crx_url_1_diff_2,
test_file("ihfokbkgjpifnbbojhneepfflplebdkc_1to2_bad.crx"));
interceptor.SetResponse(expected_crx_url_2,
test_file("ihfokbkgjpifnbbojhneepfflplebdkc_2.crx"));
test_configurator()->SetLoopCount(3);
component_updater()->Start();
message_loop.Run();
EXPECT_EQ(0, static_cast<TestInstaller*>(com.installer)->error());
EXPECT_EQ(2, static_cast<TestInstaller*>(com.installer)->install_count());
EXPECT_EQ(6, interceptor.GetHitCount());
component_updater()->Stop();
}
|