OLD | NEW |
| (Empty) |
1 // Copyright 2007-2009 Google Inc. | |
2 // | |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
4 // you may not use this file except in compliance with the License. | |
5 // You may obtain a copy of the License at | |
6 // | |
7 // http://www.apache.org/licenses/LICENSE-2.0 | |
8 // | |
9 // Unless required by applicable law or agreed to in writing, software | |
10 // distributed under the License is distributed on an "AS IS" BASIS, | |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 // See the License for the specific language governing permissions and | |
13 // limitations under the License. | |
14 // ======================================================================== | |
15 | |
16 // | |
17 // Unit tests for the file execution module of the MSP custom action. | |
18 | |
19 #include "omaha/base/app_util.h" | |
20 #include "omaha/base/file.h" | |
21 #include "omaha/base/path.h" | |
22 #include "omaha/base/vistautil.h" | |
23 #include "omaha/recovery/repair_exe/custom_action/execute_repair_file.h" | |
24 #include "omaha/testing/unit_test.h" | |
25 | |
26 namespace omaha { | |
27 | |
28 namespace { | |
29 | |
30 // The valid repair file saves the arguments passed to it to a file. | |
31 void RunAndVerifySavedArgs(const CString& args) { | |
32 CString expected_copy_path = | |
33 _T("%PROGRAMFILES%\\Google\\Update\\SaveArguments.exe"); | |
34 EXPECT_SUCCEEDED(ExpandStringWithSpecialFolders(&expected_copy_path)); | |
35 CString saved_arguments_file_path = | |
36 _T("%PROGRAMFILES%\\Google\\Update\\saved_arguments.txt"); | |
37 EXPECT_SUCCEEDED(ExpandStringWithSpecialFolders(&saved_arguments_file_path)); | |
38 | |
39 CString repair_file(app_util::GetCurrentModuleDirectory()); | |
40 EXPECT_TRUE(::PathAppend(CStrBuf(repair_file, MAX_PATH), | |
41 _T("unittest_support\\SaveArguments.exe"))); | |
42 | |
43 ::DeleteFile(saved_arguments_file_path); | |
44 | |
45 if (vista_util::IsUserAdmin()) { | |
46 EXPECT_FALSE(File::Exists(saved_arguments_file_path)); | |
47 | |
48 EXPECT_SUCCEEDED(omaha::VerifyFileAndExecute(repair_file, args)); | |
49 | |
50 bool is_found = false; | |
51 for (int tries = 0; tries < 100 && !is_found; ++tries) { | |
52 ::Sleep(50); | |
53 is_found = File::Exists(saved_arguments_file_path); | |
54 } | |
55 ASSERT_TRUE(is_found); | |
56 | |
57 scoped_hfile file; | |
58 for (int tries = 0; tries < 100 && !valid(file); ++tries) { | |
59 ::Sleep(50); | |
60 reset(file, ::CreateFile(saved_arguments_file_path, | |
61 GENERIC_READ, | |
62 0, // do not share | |
63 NULL, // default security | |
64 OPEN_EXISTING, // existing file only | |
65 FILE_ATTRIBUTE_NORMAL, | |
66 NULL)); // no template | |
67 } | |
68 ASSERT_TRUE(valid(file)); | |
69 | |
70 const int kBufferLen = 50; | |
71 TCHAR buffer[kBufferLen + 1] = {0}; | |
72 DWORD bytes_read = 0; | |
73 | |
74 // Do not assume the buffer read by ReadFile remains zero-terminated. | |
75 EXPECT_TRUE(::ReadFile(get(file), | |
76 buffer, | |
77 kBufferLen * sizeof(TCHAR), | |
78 &bytes_read, | |
79 NULL)); | |
80 EXPECT_EQ(0, bytes_read % sizeof(TCHAR)); | |
81 buffer[bytes_read / sizeof(TCHAR)] = _T('\0'); | |
82 EXPECT_STREQ(args, buffer); | |
83 | |
84 reset(file); | |
85 | |
86 ::DeleteFile(expected_copy_path); | |
87 EXPECT_TRUE(::DeleteFile(saved_arguments_file_path)); | |
88 } else { | |
89 const bool expected_file_exists = File::Exists(saved_arguments_file_path); | |
90 EXPECT_EQ(E_ACCESSDENIED, omaha::VerifyFileAndExecute(repair_file, args)); | |
91 | |
92 // We can't force the file to be deleted, so make sure it wasn't created | |
93 // or deleted by the above method. | |
94 EXPECT_EQ(expected_file_exists, File::Exists(saved_arguments_file_path)); | |
95 } | |
96 } | |
97 | |
98 } // namespace | |
99 | |
100 TEST(ExecuteRepairFileTest, VerifyFileAndExecute_EmptyFilename) { | |
101 EXPECT_EQ(E_INVALIDARG, VerifyFileAndExecute(_T(""), _T(""))); | |
102 } | |
103 | |
104 TEST(ExecuteRepairFileTest, VerifyFileAndExecute_FileDoesNotExist) { | |
105 EXPECT_EQ(HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND), | |
106 VerifyFileAndExecute(_T("no_such_file.exe"), _T(""))); | |
107 } | |
108 | |
109 TEST(ExecuteRepairFileTest, VerifyFileAndExecute_FilenameIsDirectory) { | |
110 EXPECT_EQ(E_ACCESSDENIED, | |
111 VerifyFileAndExecute(_T("C:\\Windows"), _T(""))); | |
112 } | |
113 | |
114 TEST(ExecuteRepairFileTest, VerifyFileAndExecute_UnsignedFile) { | |
115 CString expected_copy_path = | |
116 _T("%PROGRAMFILES%\\Google\\Update\\GoogleUpdate_unsigned.exe"); | |
117 EXPECT_SUCCEEDED(ExpandStringWithSpecialFolders(&expected_copy_path)); | |
118 CString repair_file(app_util::GetCurrentModuleDirectory()); | |
119 EXPECT_TRUE(::PathAppend(CStrBuf(repair_file, MAX_PATH), | |
120 _T("GoogleUpdate_unsigned.exe"))); | |
121 | |
122 if (vista_util::IsUserAdmin()) { | |
123 EXPECT_EQ(TRUST_E_NOSIGNATURE, VerifyFileAndExecute(repair_file, _T(""))); | |
124 | |
125 EXPECT_TRUE(File::Exists(expected_copy_path)); | |
126 EXPECT_TRUE(::DeleteFile(expected_copy_path)); | |
127 } else { | |
128 const bool expected_file_exists = File::Exists(expected_copy_path); | |
129 EXPECT_EQ(E_ACCESSDENIED, VerifyFileAndExecute(repair_file, _T(""))); | |
130 | |
131 // We can't force the file to be deleted, so make sure it wasn't created | |
132 // or deleted by the above method. | |
133 EXPECT_EQ(expected_file_exists, File::Exists(expected_copy_path)); | |
134 } | |
135 } | |
136 | |
137 TEST(ExecuteRepairFileTest, VerifyFileAndExecute_ValidRepairFileWithArgs) { | |
138 RunAndVerifySavedArgs(_T("These /are the args.")); | |
139 } | |
140 | |
141 TEST(ExecuteRepairFileTest, VerifyFileAndExecute_ValidRepairFileWithoutArgs) { | |
142 RunAndVerifySavedArgs(_T("")); | |
143 } | |
144 | |
145 } // namespace omaha | |
OLD | NEW |