Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(274)

Side by Side Diff: chrome/installer/setup/setup_util_unittest.cc

Issue 10810021: Register Chrome in Active Setup to be called by Windows at user login. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <windows.h> 5 #include <windows.h>
6 6
7 #include <string>
8
7 #include "base/file_util.h" 9 #include "base/file_util.h"
8 #include "base/path_service.h" 10 #include "base/path_service.h"
9 #include "base/scoped_temp_dir.h" 11 #include "base/scoped_temp_dir.h"
10 #include "base/time.h" 12 #include "base/time.h"
13 #include "base/memory/scoped_ptr.h"
11 #include "base/threading/platform_thread.h" 14 #include "base/threading/platform_thread.h"
15 #include "base/win/scoped_handle.h"
12 #include "chrome/common/chrome_paths.h" 16 #include "chrome/common/chrome_paths.h"
13 #include "chrome/installer/setup/setup_util.h" 17 #include "chrome/installer/setup/setup_util.h"
14 #include "testing/gtest/include/gtest/gtest.h" 18 #include "testing/gtest/include/gtest/gtest.h"
15 19
16 namespace { 20 namespace {
17 class SetupUtilTest : public testing::Test { 21
22 class SetupUtilTestWithDir : public testing::Test {
18 protected: 23 protected:
19 virtual void SetUp() { 24 virtual void SetUp() {
20 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &data_dir_)); 25 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &data_dir_));
21 data_dir_ = data_dir_.AppendASCII("installer"); 26 data_dir_ = data_dir_.AppendASCII("installer");
22 ASSERT_TRUE(file_util::PathExists(data_dir_)); 27 ASSERT_TRUE(file_util::PathExists(data_dir_));
23 28
24 // Create a temp directory for testing. 29 // Create a temp directory for testing.
25 ASSERT_TRUE(test_dir_.CreateUniqueTempDir()); 30 ASSERT_TRUE(test_dir_.CreateUniqueTempDir());
26 } 31 }
27 32
28 virtual void TearDown() { 33 virtual void TearDown() {
29 // Clean up test directory manually so we can fail if it leaks. 34 // Clean up test directory manually so we can fail if it leaks.
30 ASSERT_TRUE(test_dir_.Delete()); 35 ASSERT_TRUE(test_dir_.Delete());
31 } 36 }
32 37
33 // The temporary directory used to contain the test operations. 38 // The temporary directory used to contain the test operations.
34 ScopedTempDir test_dir_; 39 ScopedTempDir test_dir_;
35 40
36 // The path to input data used in tests. 41 // The path to input data used in tests.
37 FilePath data_dir_; 42 FilePath data_dir_;
38 }; 43 };
44
45 // The privilege tested in ScopeTokenPrivilege tests below.
46 // Use SE_RESTORE_NAME as it is one of the many privileges that is available,
47 // but not enabled by default on processes running at high integrity.
48 static const wchar_t* kTestedPrivilege = SE_RESTORE_NAME;
tommi (sloooow) - chröme 2012/07/20 09:59:25 static const wchar_t kTestedPrivilege[] = SE_RESTO
gab 2012/07/20 14:45:47 Done.
49
50 // Returns true if the current process' token has privilege |privilege_name|
51 // enabled.
52 bool CurrentProcessHasPrivilege(const wchar_t* privilege_name) {
53 base::win::ScopedHandle token;
54 if (!::OpenProcessToken(::GetCurrentProcess(), TOKEN_QUERY,
55 token.Receive())) {
56 ADD_FAILURE();
57 return false;
58 }
59
60 // First get the size of the buffer needed for |privileges| below.
61 DWORD size;
62 EXPECT_FALSE(::GetTokenInformation(token, TokenPrivileges, NULL, 0, &size));
63
64 scoped_array<BYTE> privileges_bytes(new BYTE[size]);
65 TOKEN_PRIVILEGES* privileges =
66 reinterpret_cast<TOKEN_PRIVILEGES*>(privileges_bytes.get());
67
68 if (!::GetTokenInformation(token, TokenPrivileges, privileges, size, &size)) {
69 ADD_FAILURE();
70 return false;
71 }
72
73 // There is no point getting a buffer to store more than |privilege_name|\0 as
74 // anything longer will obviously not be equal to |privilege_name|.
75 const DWORD desired_size = wcslen(privilege_name);
76 const DWORD buffer_size = desired_size + 1;
77 scoped_array<wchar_t> name_buffer(new wchar_t[buffer_size]);
78 for (int i = privileges->PrivilegeCount - 1; i >= 0 ; --i) {
79 LUID_AND_ATTRIBUTES& luid_and_att = privileges->Privileges[i];
80 DWORD size = buffer_size;
81 ::LookupPrivilegeName(NULL, &luid_and_att.Luid,
82 name_buffer.get(), &size);
83 if (size == desired_size &&
84 wcscmp(name_buffer.get(), privilege_name) == 0) {
85 if (luid_and_att.Attributes == SE_PRIVILEGE_ENABLED)
tommi (sloooow) - chröme 2012/07/20 09:59:25 return luid_and_att.Attributes == SE_PRIVILEGE_ENA
gab 2012/07/20 14:45:47 Indeed :)!
86 return true;
87 else
88 return false;
89 }
90 }
91 return false;
39 } 92 }
40 93
94 } // namespace
95
41 // Test that we are parsing Chrome version correctly. 96 // Test that we are parsing Chrome version correctly.
42 TEST_F(SetupUtilTest, ApplyDiffPatchTest) { 97 TEST_F(SetupUtilTestWithDir, ApplyDiffPatchTest) {
43 FilePath work_dir(test_dir_.path()); 98 FilePath work_dir(test_dir_.path());
44 work_dir = work_dir.AppendASCII("ApplyDiffPatchTest"); 99 work_dir = work_dir.AppendASCII("ApplyDiffPatchTest");
45 ASSERT_FALSE(file_util::PathExists(work_dir)); 100 ASSERT_FALSE(file_util::PathExists(work_dir));
46 EXPECT_TRUE(file_util::CreateDirectory(work_dir)); 101 EXPECT_TRUE(file_util::CreateDirectory(work_dir));
47 ASSERT_TRUE(file_util::PathExists(work_dir)); 102 ASSERT_TRUE(file_util::PathExists(work_dir));
48 103
49 FilePath src = data_dir_.AppendASCII("archive1.7z"); 104 FilePath src = data_dir_.AppendASCII("archive1.7z");
50 FilePath patch = data_dir_.AppendASCII("archive.diff"); 105 FilePath patch = data_dir_.AppendASCII("archive.diff");
51 FilePath dest = work_dir.AppendASCII("archive2.7z"); 106 FilePath dest = work_dir.AppendASCII("archive2.7z");
52 EXPECT_EQ(installer::ApplyDiffPatch(src, patch, dest, NULL), 0); 107 EXPECT_EQ(installer::ApplyDiffPatch(src, patch, dest, NULL), 0);
53 FilePath base = data_dir_.AppendASCII("archive2.7z"); 108 FilePath base = data_dir_.AppendASCII("archive2.7z");
54 EXPECT_TRUE(file_util::ContentsEqual(dest, base)); 109 EXPECT_TRUE(file_util::ContentsEqual(dest, base));
55 110
56 EXPECT_EQ(installer::ApplyDiffPatch(FilePath(), FilePath(), FilePath(), NULL), 111 EXPECT_EQ(installer::ApplyDiffPatch(FilePath(), FilePath(), FilePath(), NULL),
57 6); 112 6);
58 } 113 }
59 114
60 // Test that we are parsing Chrome version correctly. 115 // Test that we are parsing Chrome version correctly.
61 TEST_F(SetupUtilTest, GetMaxVersionFromArchiveDirTest) { 116 TEST_F(SetupUtilTestWithDir, GetMaxVersionFromArchiveDirTest) {
62 // Create a version dir 117 // Create a version dir
63 FilePath chrome_dir = test_dir_.path().AppendASCII("1.0.0.0"); 118 FilePath chrome_dir = test_dir_.path().AppendASCII("1.0.0.0");
64 file_util::CreateDirectory(chrome_dir); 119 file_util::CreateDirectory(chrome_dir);
65 ASSERT_TRUE(file_util::PathExists(chrome_dir)); 120 ASSERT_TRUE(file_util::PathExists(chrome_dir));
66 scoped_ptr<Version> version( 121 scoped_ptr<Version> version(
67 installer::GetMaxVersionFromArchiveDir(test_dir_.path())); 122 installer::GetMaxVersionFromArchiveDir(test_dir_.path()));
68 ASSERT_EQ(version->GetString(), "1.0.0.0"); 123 ASSERT_EQ(version->GetString(), "1.0.0.0");
69 124
70 file_util::Delete(chrome_dir, true); 125 file_util::Delete(chrome_dir, true);
71 ASSERT_FALSE(file_util::PathExists(chrome_dir)); 126 ASSERT_FALSE(file_util::PathExists(chrome_dir));
(...skipping 15 matching lines...) Expand all
87 file_util::CreateDirectory(chrome_dir); 142 file_util::CreateDirectory(chrome_dir);
88 ASSERT_TRUE(file_util::PathExists(chrome_dir)); 143 ASSERT_TRUE(file_util::PathExists(chrome_dir));
89 chrome_dir = test_dir_.path().AppendASCII("1.1.1.1"); 144 chrome_dir = test_dir_.path().AppendASCII("1.1.1.1");
90 file_util::CreateDirectory(chrome_dir); 145 file_util::CreateDirectory(chrome_dir);
91 ASSERT_TRUE(file_util::PathExists(chrome_dir)); 146 ASSERT_TRUE(file_util::PathExists(chrome_dir));
92 147
93 version.reset(installer::GetMaxVersionFromArchiveDir(test_dir_.path())); 148 version.reset(installer::GetMaxVersionFromArchiveDir(test_dir_.path()));
94 ASSERT_EQ(version->GetString(), "9.9.9.9"); 149 ASSERT_EQ(version->GetString(), "9.9.9.9");
95 } 150 }
96 151
97 TEST_F(SetupUtilTest, DeleteFileFromTempProcess) { 152 TEST_F(SetupUtilTestWithDir, DeleteFileFromTempProcess) {
98 FilePath test_file; 153 FilePath test_file;
99 file_util::CreateTemporaryFileInDir(test_dir_.path(), &test_file); 154 file_util::CreateTemporaryFileInDir(test_dir_.path(), &test_file);
100 ASSERT_TRUE(file_util::PathExists(test_file)); 155 ASSERT_TRUE(file_util::PathExists(test_file));
101 file_util::WriteFile(test_file, "foo", 3); 156 file_util::WriteFile(test_file, "foo", 3);
102 EXPECT_TRUE(installer::DeleteFileFromTempProcess(test_file, 0)); 157 EXPECT_TRUE(installer::DeleteFileFromTempProcess(test_file, 0));
103 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(200)); 158 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(200));
104 EXPECT_FALSE(file_util::PathExists(test_file)); 159 EXPECT_FALSE(file_util::PathExists(test_file));
105 } 160 }
161
162 // Note: This test is only valid when ran at high integrity (i.e. it will fail
tommi (sloooow) - chröme 2012/07/20 09:59:25 s/when ran/when run
gab 2012/07/20 14:45:47 Done.
163 // at medium integrity).
164 TEST(SetupUtilTest, ScopedTokenPrivilegeBasic) {
165 ASSERT_FALSE(CurrentProcessHasPrivilege(kTestedPrivilege));
166
167 {
168 installer::ScopedTokenPrivilege test_scoped_privilege(kTestedPrivilege);
169 ASSERT_TRUE(test_scoped_privilege.is_enabled());
170 ASSERT_TRUE(CurrentProcessHasPrivilege(kTestedPrivilege));
171 }
172
173 ASSERT_FALSE(CurrentProcessHasPrivilege(kTestedPrivilege));
174 }
175
176 // Note: This test is only valid when ran at high integrity (i.e. it will fail
177 // at medium integrity).
178 TEST(SetupUtilTest, ScopedTokenPrivilegeAlreadyEnabled) {
179 ASSERT_FALSE(CurrentProcessHasPrivilege(kTestedPrivilege));
180
181 {
182 installer::ScopedTokenPrivilege test_scoped_privilege(kTestedPrivilege);
183 ASSERT_TRUE(test_scoped_privilege.is_enabled());
184 ASSERT_TRUE(CurrentProcessHasPrivilege(kTestedPrivilege));
185 {
186 installer::ScopedTokenPrivilege dup_scoped_privilege(kTestedPrivilege);
187 ASSERT_TRUE(dup_scoped_privilege.is_enabled());
188 ASSERT_TRUE(CurrentProcessHasPrivilege(kTestedPrivilege));
189 }
190 ASSERT_TRUE(CurrentProcessHasPrivilege(kTestedPrivilege));
191 }
192
193 ASSERT_FALSE(CurrentProcessHasPrivilege(kTestedPrivilege));
194 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698