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
|
// 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 <windows.h>
#include <fstream>
#include "base/base_paths.h"
#include "base/command_line.h"
#include "base/file_path.h"
#include "base/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/path_service.h"
#include "base/process_util.h"
#include "base/string_util.h"
#include "base/test/test_reg_util_win.h"
#include "base/utf_string_conversions.h"
#include "base/version.h"
#include "base/win/registry.h"
#include "base/win/scoped_handle.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/installer/test/alternate_version_generator.h"
#include "chrome/installer/util/fake_installation_state.h"
#include "chrome/installer/util/fake_product_state.h"
#include "chrome/installer/util/google_update_constants.h"
#include "chrome/installer/util/helper.h"
#include "chrome/installer/util/installation_state.h"
#include "chrome/installer/util/installer_state.h"
#include "chrome/installer/util/master_preferences.h"
#include "chrome/installer/util/product_unittest.h"
#include "chrome/installer/util/util_constants.h"
#include "chrome/installer/util/work_item.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "installer_util_strings.h" // NOLINT
using base::win::RegKey;
using installer::InstallationState;
using installer::InstallerState;
using installer::MasterPreferences;
using registry_util::RegistryOverrideManager;
class InstallerStateTest : public TestWithTempDirAndDeleteTempOverrideKeys {
protected:
};
// An installer state on which we can access otherwise protected members.
class MockInstallerState : public InstallerState {
public:
MockInstallerState() : InstallerState() { }
void set_target_path(const FilePath& target_path) {
target_path_ = target_path;
}
static bool IsFileInUse(const FilePath& file) {
return InstallerState::IsFileInUse(file);
}
const Version& critical_update_version() const {
return critical_update_version_;
}
void GetExistingExeVersions(std::set<std::string>* existing_version_strings) {
return InstallerState::GetExistingExeVersions(existing_version_strings);
}
};
// Simple function to dump some text into a new file.
void CreateTextFile(const std::wstring& filename,
const std::wstring& contents) {
std::ofstream file;
file.open(filename.c_str());
ASSERT_TRUE(file.is_open());
file << contents;
file.close();
}
void BuildSingleChromeState(const FilePath& target_dir,
MockInstallerState* installer_state) {
CommandLine cmd_line = CommandLine::FromString(L"setup.exe");
MasterPreferences prefs(cmd_line);
InstallationState machine_state;
machine_state.Initialize();
installer_state->Initialize(cmd_line, prefs, machine_state);
installer_state->set_target_path(target_dir);
EXPECT_TRUE(installer_state->FindProduct(BrowserDistribution::CHROME_BROWSER)
!= NULL);
EXPECT_TRUE(installer_state->FindProduct(BrowserDistribution::CHROME_FRAME)
== NULL);
}
wchar_t text_content_1[] = L"delete me";
wchar_t text_content_2[] = L"delete me as well";
// Delete version directories. Everything lower than the given version
// should be deleted.
TEST_F(InstallerStateTest, Delete) {
// TODO(grt): move common stuff into the test fixture.
// Create a Chrome dir
FilePath chrome_dir(test_dir_.path());
chrome_dir = chrome_dir.AppendASCII("chrome");
file_util::CreateDirectory(chrome_dir);
ASSERT_TRUE(file_util::PathExists(chrome_dir));
FilePath chrome_dir_1(chrome_dir);
chrome_dir_1 = chrome_dir_1.AppendASCII("1.0.1.0");
file_util::CreateDirectory(chrome_dir_1);
ASSERT_TRUE(file_util::PathExists(chrome_dir_1));
FilePath chrome_dir_2(chrome_dir);
chrome_dir_2 = chrome_dir_2.AppendASCII("1.0.2.0");
file_util::CreateDirectory(chrome_dir_2);
ASSERT_TRUE(file_util::PathExists(chrome_dir_2));
FilePath chrome_dir_3(chrome_dir);
chrome_dir_3 = chrome_dir_3.AppendASCII("1.0.3.0");
file_util::CreateDirectory(chrome_dir_3);
ASSERT_TRUE(file_util::PathExists(chrome_dir_3));
FilePath chrome_dir_4(chrome_dir);
chrome_dir_4 = chrome_dir_4.AppendASCII("1.0.4.0");
file_util::CreateDirectory(chrome_dir_4);
ASSERT_TRUE(file_util::PathExists(chrome_dir_4));
FilePath chrome_dll_1(chrome_dir_1);
chrome_dll_1 = chrome_dll_1.AppendASCII("chrome.dll");
CreateTextFile(chrome_dll_1.value(), text_content_1);
ASSERT_TRUE(file_util::PathExists(chrome_dll_1));
FilePath chrome_dll_2(chrome_dir_2);
chrome_dll_2 = chrome_dll_2.AppendASCII("chrome.dll");
CreateTextFile(chrome_dll_2.value(), text_content_1);
ASSERT_TRUE(file_util::PathExists(chrome_dll_2));
FilePath chrome_dll_3(chrome_dir_3);
chrome_dll_3 = chrome_dll_3.AppendASCII("chrome.dll");
CreateTextFile(chrome_dll_3.value(), text_content_1);
ASSERT_TRUE(file_util::PathExists(chrome_dll_3));
FilePath chrome_dll_4(chrome_dir_4);
chrome_dll_4 = chrome_dll_4.AppendASCII("chrome.dll");
CreateTextFile(chrome_dll_4.value(), text_content_1);
ASSERT_TRUE(file_util::PathExists(chrome_dll_4));
MockInstallerState installer_state;
BuildSingleChromeState(chrome_dir, &installer_state);
Version latest_version("1.0.4.0");
{
base::ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
installer_state.RemoveOldVersionDirectories(latest_version, NULL,
temp_dir.path());
}
// old versions should be gone
EXPECT_FALSE(file_util::PathExists(chrome_dir_1));
EXPECT_FALSE(file_util::PathExists(chrome_dir_2));
EXPECT_FALSE(file_util::PathExists(chrome_dir_3));
// the latest version should stay
EXPECT_TRUE(file_util::PathExists(chrome_dll_4));
}
// Delete older version directories, keeping the one in used intact.
TEST_F(InstallerStateTest, DeleteInUsed) {
// Create a Chrome dir
FilePath chrome_dir(test_dir_.path());
chrome_dir = chrome_dir.AppendASCII("chrome");
file_util::CreateDirectory(chrome_dir);
ASSERT_TRUE(file_util::PathExists(chrome_dir));
FilePath chrome_dir_1(chrome_dir);
chrome_dir_1 = chrome_dir_1.AppendASCII("1.0.1.0");
file_util::CreateDirectory(chrome_dir_1);
ASSERT_TRUE(file_util::PathExists(chrome_dir_1));
FilePath chrome_dir_2(chrome_dir);
chrome_dir_2 = chrome_dir_2.AppendASCII("1.0.2.0");
file_util::CreateDirectory(chrome_dir_2);
ASSERT_TRUE(file_util::PathExists(chrome_dir_2));
FilePath chrome_dir_3(chrome_dir);
chrome_dir_3 = chrome_dir_3.AppendASCII("1.0.3.0");
file_util::CreateDirectory(chrome_dir_3);
ASSERT_TRUE(file_util::PathExists(chrome_dir_3));
FilePath chrome_dir_4(chrome_dir);
chrome_dir_4 = chrome_dir_4.AppendASCII("1.0.4.0");
file_util::CreateDirectory(chrome_dir_4);
ASSERT_TRUE(file_util::PathExists(chrome_dir_4));
FilePath chrome_dll_1(chrome_dir_1);
chrome_dll_1 = chrome_dll_1.AppendASCII("chrome.dll");
CreateTextFile(chrome_dll_1.value(), text_content_1);
ASSERT_TRUE(file_util::PathExists(chrome_dll_1));
FilePath chrome_dll_2(chrome_dir_2);
chrome_dll_2 = chrome_dll_2.AppendASCII("chrome.dll");
CreateTextFile(chrome_dll_2.value(), text_content_1);
ASSERT_TRUE(file_util::PathExists(chrome_dll_2));
// Open the file to make it in use.
std::ofstream file;
file.open(chrome_dll_2.value().c_str());
FilePath chrome_othera_2(chrome_dir_2);
chrome_othera_2 = chrome_othera_2.AppendASCII("othera.dll");
CreateTextFile(chrome_othera_2.value(), text_content_2);
ASSERT_TRUE(file_util::PathExists(chrome_othera_2));
FilePath chrome_otherb_2(chrome_dir_2);
chrome_otherb_2 = chrome_otherb_2.AppendASCII("otherb.dll");
CreateTextFile(chrome_otherb_2.value(), text_content_2);
ASSERT_TRUE(file_util::PathExists(chrome_otherb_2));
FilePath chrome_dll_3(chrome_dir_3);
chrome_dll_3 = chrome_dll_3.AppendASCII("chrome.dll");
CreateTextFile(chrome_dll_3.value(), text_content_1);
ASSERT_TRUE(file_util::PathExists(chrome_dll_3));
FilePath chrome_dll_4(chrome_dir_4);
chrome_dll_4 = chrome_dll_4.AppendASCII("chrome.dll");
CreateTextFile(chrome_dll_4.value(), text_content_1);
ASSERT_TRUE(file_util::PathExists(chrome_dll_4));
MockInstallerState installer_state;
BuildSingleChromeState(chrome_dir, &installer_state);
Version latest_version("1.0.4.0");
Version existing_version("1.0.1.0");
{
base::ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
installer_state.RemoveOldVersionDirectories(latest_version,
&existing_version,
temp_dir.path());
}
// the version defined as the existing version should stay
EXPECT_TRUE(file_util::PathExists(chrome_dir_1));
// old versions not in used should be gone
EXPECT_FALSE(file_util::PathExists(chrome_dir_3));
// every thing under in used version should stay
EXPECT_TRUE(file_util::PathExists(chrome_dir_2));
EXPECT_TRUE(file_util::PathExists(chrome_dll_2));
EXPECT_TRUE(file_util::PathExists(chrome_othera_2));
EXPECT_TRUE(file_util::PathExists(chrome_otherb_2));
// the latest version should stay
EXPECT_TRUE(file_util::PathExists(chrome_dll_4));
}
// Tests a few basic things of the Package class. Makes sure that the path
// operations are correct
TEST_F(InstallerStateTest, Basic) {
const bool multi_install = false;
const bool system_level = true;
CommandLine cmd_line = CommandLine::FromString(
std::wstring(L"setup.exe") +
(multi_install ? L" --multi-install --chrome" : L"") +
(system_level ? L" --system-level" : L""));
MasterPreferences prefs(cmd_line);
InstallationState machine_state;
machine_state.Initialize();
MockInstallerState installer_state;
installer_state.Initialize(cmd_line, prefs, machine_state);
installer_state.set_target_path(test_dir_.path());
EXPECT_EQ(test_dir_.path().value(), installer_state.target_path().value());
EXPECT_EQ(1U, installer_state.products().size());
const char kOldVersion[] = "1.2.3.4";
const char kNewVersion[] = "2.3.4.5";
Version new_version(kNewVersion);
Version old_version(kOldVersion);
ASSERT_TRUE(new_version.IsValid());
ASSERT_TRUE(old_version.IsValid());
FilePath installer_dir(installer_state.GetInstallerDirectory(new_version));
EXPECT_FALSE(installer_dir.empty());
FilePath new_version_dir(installer_state.target_path().Append(
UTF8ToWide(new_version.GetString())));
FilePath old_version_dir(installer_state.target_path().Append(
UTF8ToWide(old_version.GetString())));
EXPECT_FALSE(file_util::PathExists(new_version_dir));
EXPECT_FALSE(file_util::PathExists(old_version_dir));
EXPECT_FALSE(file_util::PathExists(installer_dir));
file_util::CreateDirectory(installer_dir);
EXPECT_TRUE(file_util::PathExists(new_version_dir));
file_util::CreateDirectory(old_version_dir);
EXPECT_TRUE(file_util::PathExists(old_version_dir));
// Create a fake chrome.dll key file in the old version directory. This
// should prevent the old version directory from getting deleted.
FilePath old_chrome_dll(old_version_dir.Append(installer::kChromeDll));
EXPECT_FALSE(file_util::PathExists(old_chrome_dll));
// Hold on to the file exclusively to prevent the directory from
// being deleted.
base::win::ScopedHandle file(
::CreateFile(old_chrome_dll.value().c_str(), GENERIC_READ,
0, NULL, OPEN_ALWAYS, 0, NULL));
EXPECT_TRUE(file.IsValid());
EXPECT_TRUE(file_util::PathExists(old_chrome_dll));
base::ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
// Don't explicitly tell the directory cleanup logic not to delete the
// old version, rely on the key files to keep it around.
installer_state.RemoveOldVersionDirectories(new_version,
NULL,
temp_dir.path());
// The old directory should still exist.
EXPECT_TRUE(file_util::PathExists(old_version_dir));
EXPECT_TRUE(file_util::PathExists(new_version_dir));
// Now close the file handle to make it possible to delete our key file.
file.Close();
installer_state.RemoveOldVersionDirectories(new_version,
NULL,
temp_dir.path());
// The new directory should still exist.
EXPECT_TRUE(file_util::PathExists(new_version_dir));
// Now, the old directory and key file should be gone.
EXPECT_FALSE(file_util::PathExists(old_chrome_dll));
EXPECT_FALSE(file_util::PathExists(old_version_dir));
}
TEST_F(InstallerStateTest, WithProduct) {
const bool multi_install = false;
const bool system_level = true;
CommandLine cmd_line = CommandLine::FromString(
std::wstring(L"setup.exe") +
(multi_install ? L" --multi-install --chrome" : L"") +
(system_level ? L" --system-level" : L""));
MasterPreferences prefs(cmd_line);
InstallationState machine_state;
machine_state.Initialize();
MockInstallerState installer_state;
installer_state.Initialize(cmd_line, prefs, machine_state);
installer_state.set_target_path(test_dir_.path());
EXPECT_EQ(1U, installer_state.products().size());
EXPECT_EQ(system_level, installer_state.system_install());
const char kCurrentVersion[] = "1.2.3.4";
Version current_version(kCurrentVersion);
HKEY root = system_level ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER;
EXPECT_EQ(root, installer_state.root_key());
{
RegistryOverrideManager override_manager;
override_manager.OverrideRegistry(root, L"root_pit");
BrowserDistribution* dist = BrowserDistribution::GetSpecificDistribution(
BrowserDistribution::CHROME_BROWSER);
RegKey chrome_key(root, dist->GetVersionKey().c_str(), KEY_ALL_ACCESS);
EXPECT_TRUE(chrome_key.Valid());
if (chrome_key.Valid()) {
chrome_key.WriteValue(google_update::kRegVersionField,
UTF8ToWide(current_version.GetString()).c_str());
machine_state.Initialize();
// TODO(tommi): Also test for when there exists a new_chrome.exe.
Version found_version(*installer_state.GetCurrentVersion(machine_state));
EXPECT_TRUE(found_version.IsValid());
if (found_version.IsValid())
EXPECT_TRUE(current_version.Equals(found_version));
}
}
}
TEST_F(InstallerStateTest, InstallerResult) {
const bool system_level = true;
bool multi_install = false;
HKEY root = system_level ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER;
RegKey key;
std::wstring launch_cmd = L"hey diddle diddle";
std::wstring value;
DWORD dw_value;
// check results for a fresh install of single Chrome
{
RegistryOverrideManager override_manager;
override_manager.OverrideRegistry(root, L"root_inst_res");
CommandLine cmd_line = CommandLine::FromString(L"setup.exe --system-level");
const MasterPreferences prefs(cmd_line);
InstallationState machine_state;
machine_state.Initialize();
InstallerState state;
state.Initialize(cmd_line, prefs, machine_state);
state.WriteInstallerResult(installer::FIRST_INSTALL_SUCCESS,
IDS_INSTALL_OS_ERROR_BASE, &launch_cmd);
BrowserDistribution* distribution =
BrowserDistribution::GetSpecificDistribution(
BrowserDistribution::CHROME_BROWSER);
EXPECT_EQ(ERROR_SUCCESS,
key.Open(root, distribution->GetStateKey().c_str(), KEY_READ));
EXPECT_EQ(ERROR_SUCCESS,
key.ReadValueDW(installer::kInstallerResult, &dw_value));
EXPECT_EQ(static_cast<DWORD>(0), dw_value);
EXPECT_EQ(ERROR_SUCCESS,
key.ReadValueDW(installer::kInstallerError, &dw_value));
EXPECT_EQ(static_cast<DWORD>(installer::FIRST_INSTALL_SUCCESS), dw_value);
EXPECT_EQ(ERROR_SUCCESS,
key.ReadValue(installer::kInstallerResultUIString, &value));
EXPECT_FALSE(value.empty());
EXPECT_EQ(ERROR_SUCCESS,
key.ReadValue(installer::kInstallerSuccessLaunchCmdLine, &value));
EXPECT_EQ(launch_cmd, value);
}
// check results for a fresh install of multi Chrome
{
RegistryOverrideManager override_manager;
override_manager.OverrideRegistry(root, L"root_inst_res");
CommandLine cmd_line = CommandLine::FromString(
L"setup.exe --system-level --multi-install --chrome");
const MasterPreferences prefs(cmd_line);
InstallationState machine_state;
machine_state.Initialize();
InstallerState state;
state.Initialize(cmd_line, prefs, machine_state);
state.WriteInstallerResult(installer::FIRST_INSTALL_SUCCESS, 0,
&launch_cmd);
BrowserDistribution* distribution =
BrowserDistribution::GetSpecificDistribution(
BrowserDistribution::CHROME_BROWSER);
BrowserDistribution* binaries =
BrowserDistribution::GetSpecificDistribution(
BrowserDistribution::CHROME_BINARIES);
EXPECT_EQ(ERROR_SUCCESS,
key.Open(root, distribution->GetStateKey().c_str(), KEY_READ));
EXPECT_EQ(ERROR_SUCCESS,
key.ReadValue(installer::kInstallerSuccessLaunchCmdLine, &value));
EXPECT_EQ(launch_cmd, value);
EXPECT_EQ(ERROR_SUCCESS,
key.Open(root, binaries->GetStateKey().c_str(), KEY_READ));
EXPECT_EQ(ERROR_SUCCESS,
key.ReadValue(installer::kInstallerSuccessLaunchCmdLine, &value));
EXPECT_EQ(launch_cmd, value);
key.Close();
}
}
// Test GetCurrentVersion when migrating single Chrome to multi
TEST_F(InstallerStateTest, GetCurrentVersionMigrateChrome) {
using installer::FakeInstallationState;
const bool system_install = false;
FakeInstallationState machine_state;
// Pretend that this version of single-install Chrome is already installed.
machine_state.AddChrome(system_install, false,
new Version(chrome::kChromeVersion));
// Now we're invoked to install multi Chrome.
CommandLine cmd_line(
CommandLine::FromString(L"setup.exe --multi-install --chrome"));
MasterPreferences prefs(cmd_line);
InstallerState installer_state;
installer_state.Initialize(cmd_line, prefs, machine_state);
// Is the Chrome version picked up?
scoped_ptr<Version> version(installer_state.GetCurrentVersion(machine_state));
EXPECT_TRUE(version.get() != NULL);
}
TEST_F(InstallerStateTest, IsFileInUse) {
base::ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
FilePath temp_file;
ASSERT_TRUE(file_util::CreateTemporaryFileInDir(temp_dir.path(), &temp_file));
EXPECT_FALSE(MockInstallerState::IsFileInUse(temp_file));
{
// Open a handle to the file with the same access mode and sharing options
// as the loader.
base::win::ScopedHandle temp_handle(
CreateFile(temp_file.value().c_str(),
SYNCHRONIZE | FILE_EXECUTE,
FILE_SHARE_DELETE | FILE_SHARE_READ,
NULL, OPEN_EXISTING, 0, 0));
ASSERT_TRUE(temp_handle != NULL);
// The file should now be in use.
EXPECT_TRUE(MockInstallerState::IsFileInUse(temp_file));
}
// And once the handle is gone, it should no longer be in use.
EXPECT_FALSE(MockInstallerState::IsFileInUse(temp_file));
}
TEST_F(InstallerStateTest, RemoveOldVersionDirs) {
MockInstallerState installer_state;
installer_state.set_target_path(test_dir_.path());
EXPECT_EQ(test_dir_.path().value(), installer_state.target_path().value());
const char kOldVersion[] = "2.0.0.0";
const char kNewVersion[] = "3.0.0.0";
const char kOldChromeExeVersion[] = "2.1.0.0";
const char kChromeExeVersion[] = "2.1.1.1";
const char kNewChromeExeVersion[] = "3.0.0.0";
Version new_version(kNewVersion);
Version old_version(kOldVersion);
Version old_chrome_exe_version(kOldChromeExeVersion);
Version chrome_exe_version(kChromeExeVersion);
Version new_chrome_exe_version(kNewChromeExeVersion);
ASSERT_TRUE(new_version.IsValid());
ASSERT_TRUE(old_version.IsValid());
ASSERT_TRUE(old_chrome_exe_version.IsValid());
ASSERT_TRUE(chrome_exe_version.IsValid());
ASSERT_TRUE(new_chrome_exe_version.IsValid());
// Set up a bunch of version dir paths.
FilePath version_dirs[] = {
installer_state.target_path().Append(L"1.2.3.4"),
installer_state.target_path().Append(L"1.2.3.5"),
installer_state.target_path().Append(L"1.2.3.6"),
installer_state.target_path().Append(ASCIIToWide(kOldVersion)),
installer_state.target_path().Append(ASCIIToWide(kOldChromeExeVersion)),
installer_state.target_path().Append(L"2.1.1.0"),
installer_state.target_path().Append(ASCIIToWide(kChromeExeVersion)),
installer_state.target_path().Append(ASCIIToWide(kNewVersion)),
installer_state.target_path().Append(L"3.9.1.1"),
};
// Create the version directories.
for (int i = 0; i < arraysize(version_dirs); i++) {
file_util::CreateDirectory(version_dirs[i]);
EXPECT_TRUE(file_util::PathExists(version_dirs[i]));
}
// Create exes with the appropriate version resource.
// Use the current test exe as a baseline.
FilePath exe_path;
ASSERT_TRUE(PathService::Get(base::FILE_EXE, &exe_path));
struct target_info {
FilePath target_file;
const Version& target_version;
} targets[] = {
{ installer_state.target_path().Append(installer::kChromeOldExe),
old_chrome_exe_version },
{ installer_state.target_path().Append(installer::kChromeExe),
chrome_exe_version },
{ installer_state.target_path().Append(installer::kChromeNewExe),
new_chrome_exe_version },
};
for (int i = 0; i < arraysize(targets); ++i) {
ASSERT_TRUE(upgrade_test::GenerateSpecificPEFileVersion(
exe_path, targets[i].target_file, targets[i].target_version));
}
// Call GetExistingExeVersions, validate that picks up the
// exe resources.
std::set<std::string> expected_exe_versions;
expected_exe_versions.insert(kOldChromeExeVersion);
expected_exe_versions.insert(kChromeExeVersion);
expected_exe_versions.insert(kNewChromeExeVersion);
std::set<std::string> actual_exe_versions;
installer_state.GetExistingExeVersions(&actual_exe_versions);
EXPECT_EQ(expected_exe_versions, actual_exe_versions);
// Call RemoveOldVersionDirectories
installer_state.RemoveOldVersionDirectories(new_version,
&old_version,
installer_state.target_path());
// What we expect to have left.
std::set<std::string> expected_remaining_dirs;
expected_remaining_dirs.insert(kOldVersion);
expected_remaining_dirs.insert(kNewVersion);
expected_remaining_dirs.insert(kOldChromeExeVersion);
expected_remaining_dirs.insert(kChromeExeVersion);
expected_remaining_dirs.insert(kNewChromeExeVersion);
// Enumerate dirs in target_path(), ensure only desired remain.
file_util::FileEnumerator version_enum(installer_state.target_path(), false,
file_util::FileEnumerator::DIRECTORIES);
for (FilePath next_version = version_enum.Next(); !next_version.empty();
next_version = version_enum.Next()) {
FilePath dir_name(next_version.BaseName());
Version version(WideToASCII(dir_name.value()));
if (version.IsValid()) {
EXPECT_TRUE(expected_remaining_dirs.erase(version.GetString()))
<< "Unexpected version dir found: " << version.GetString();
}
}
std::set<std::string>::const_iterator iter(
expected_remaining_dirs.begin());
for (; iter != expected_remaining_dirs.end(); ++iter)
ADD_FAILURE() << "Expected to find version dir for " << *iter;
}
// A fixture for testing InstallerState::DetermineCriticalVersion. Individual
// tests must invoke Initialize() with a critical version.
class InstallerStateCriticalVersionTest : public ::testing::Test {
protected:
InstallerStateCriticalVersionTest() : cmd_line_(CommandLine::NO_PROGRAM) {}
// Creates a set of versions for use by all test runs.
static void SetUpTestCase() {
low_version_ = new Version("15.0.874.106");
opv_version_ = new Version("15.0.874.255");
middle_version_ = new Version("16.0.912.32");
pv_version_ = new Version("16.0.912.255");
high_version_ = new Version("17.0.932.0");
}
// Cleans up versions used by all test runs.
static void TearDownTestCase() {
delete low_version_;
delete opv_version_;
delete middle_version_;
delete pv_version_;
delete high_version_;
}
// Initializes the InstallerState to use for a test run. The returned
// instance's critical update version is set to |version|. |version| may be
// NULL, in which case the critical update version is unset.
MockInstallerState& Initialize(const Version* version) {
cmd_line_ = version == NULL ?
CommandLine::FromString(L"setup.exe") :
CommandLine::FromString(
L"setup.exe --critical-update-version=" +
ASCIIToWide(version->GetString()));
prefs_.reset(new MasterPreferences(cmd_line_));
machine_state_.Initialize();
installer_state_.Initialize(cmd_line_, *prefs_, machine_state_);
return installer_state_;
}
static Version* low_version_;
static Version* opv_version_;
static Version* middle_version_;
static Version* pv_version_;
static Version* high_version_;
CommandLine cmd_line_;
scoped_ptr<MasterPreferences> prefs_;
InstallationState machine_state_;
MockInstallerState installer_state_;
};
Version* InstallerStateCriticalVersionTest::low_version_ = NULL;
Version* InstallerStateCriticalVersionTest::opv_version_ = NULL;
Version* InstallerStateCriticalVersionTest::middle_version_ = NULL;
Version* InstallerStateCriticalVersionTest::pv_version_ = NULL;
Version* InstallerStateCriticalVersionTest::high_version_ = NULL;
// Test the case where the critical version is less than the currently-running
// Chrome. The critical version is ignored since it doesn't apply.
TEST_F(InstallerStateCriticalVersionTest, CriticalBeforeOpv) {
MockInstallerState& installer_state(Initialize(low_version_));
EXPECT_TRUE(installer_state.critical_update_version().Equals(*low_version_));
// Unable to determine the installed version, so assume critical update.
EXPECT_TRUE(
installer_state.DetermineCriticalVersion(NULL, *pv_version_).IsValid());
// Installed version is past the critical update.
EXPECT_FALSE(
installer_state.DetermineCriticalVersion(opv_version_, *pv_version_)
.IsValid());
// Installed version is past the critical update.
EXPECT_FALSE(
installer_state.DetermineCriticalVersion(pv_version_, *pv_version_)
.IsValid());
}
// Test the case where the critical version is equal to the currently-running
// Chrome. The critical version is ignored since it doesn't apply.
TEST_F(InstallerStateCriticalVersionTest, CriticalEqualsOpv) {
MockInstallerState& installer_state(Initialize(opv_version_));
EXPECT_TRUE(installer_state.critical_update_version().Equals(*opv_version_));
// Unable to determine the installed version, so assume critical update.
EXPECT_TRUE(
installer_state.DetermineCriticalVersion(NULL, *pv_version_).IsValid());
// Installed version equals the critical update.
EXPECT_FALSE(
installer_state.DetermineCriticalVersion(opv_version_, *pv_version_)
.IsValid());
// Installed version equals the critical update.
EXPECT_FALSE(
installer_state.DetermineCriticalVersion(pv_version_, *pv_version_)
.IsValid());
}
// Test the case where the critical version is between the currently-running
// Chrome and the to-be-installed Chrome.
TEST_F(InstallerStateCriticalVersionTest, CriticalBetweenOpvAndPv) {
MockInstallerState& installer_state(Initialize(middle_version_));
EXPECT_TRUE(installer_state.critical_update_version().Equals(
*middle_version_));
// Unable to determine the installed version, so assume critical update.
EXPECT_TRUE(
installer_state.DetermineCriticalVersion(NULL, *pv_version_).IsValid());
// Installed version before the critical update.
EXPECT_TRUE(
installer_state.DetermineCriticalVersion(opv_version_, *pv_version_)
.IsValid());
// Installed version is past the critical update.
EXPECT_FALSE(
installer_state.DetermineCriticalVersion(pv_version_, *pv_version_)
.IsValid());
}
// Test the case where the critical version is the same as the to-be-installed
// Chrome.
TEST_F(InstallerStateCriticalVersionTest, CriticalEqualsPv) {
MockInstallerState& installer_state(Initialize(pv_version_));
EXPECT_TRUE(installer_state.critical_update_version().Equals(
*pv_version_));
// Unable to determine the installed version, so assume critical update.
EXPECT_TRUE(
installer_state.DetermineCriticalVersion(NULL, *pv_version_).IsValid());
// Installed version before the critical update.
EXPECT_TRUE(
installer_state.DetermineCriticalVersion(opv_version_, *pv_version_)
.IsValid());
// Installed version equals the critical update.
EXPECT_FALSE(
installer_state.DetermineCriticalVersion(pv_version_, *pv_version_)
.IsValid());
}
// Test the case where the critical version is greater than the to-be-installed
// Chrome.
TEST_F(InstallerStateCriticalVersionTest, CriticalAfterPv) {
MockInstallerState& installer_state(Initialize(high_version_));
EXPECT_TRUE(installer_state.critical_update_version().Equals(
*high_version_));
// Critical update newer than the new version.
EXPECT_FALSE(
installer_state.DetermineCriticalVersion(NULL, *pv_version_).IsValid());
EXPECT_FALSE(
installer_state.DetermineCriticalVersion(opv_version_, *pv_version_)
.IsValid());
EXPECT_FALSE(
installer_state.DetermineCriticalVersion(pv_version_, *pv_version_)
.IsValid());
}
|