| 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 // Unit tests for msp_executable_elevator. | |
| 17 // | |
| 18 // Note for Windows Vista and later: These tests will fail because the Msi* | |
| 19 // methods return an error because they do not elevate due to the UI level NONE. | |
| 20 // Enabling the UI would cause UAC prompts and the creation of restore points. | |
| 21 // The workaround is to run from an administrator command prompt. | |
| 22 | |
| 23 #include <windows.h> | |
| 24 #include <msi.h> | |
| 25 #include "omaha/base/app_util.h" | |
| 26 #include "omaha/base/constants.h" | |
| 27 #include "omaha/base/file.h" | |
| 28 #include "omaha/base/path.h" | |
| 29 #include "omaha/base/reg_key.h" | |
| 30 #include "omaha/base/utils.h" | |
| 31 #include "omaha/base/vistautil.h" | |
| 32 #include "omaha/recovery/repair_exe/mspexecutableelevator.h" | |
| 33 #include "omaha/setup/msi_test_utils.h" | |
| 34 #include "omaha/testing/unit_test.h" | |
| 35 | |
| 36 namespace omaha { | |
| 37 | |
| 38 // Note: For some reason, the product ID GUIDs are swizzled in the registry. | |
| 39 extern const TCHAR kMsiProductPatchesKey[] = | |
| 40 _T("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\") | |
| 41 _T("Installer\\UserData\\S-1-5-18\\Products\\") | |
| 42 _T("93BAD29AC2E44034A96BCB446EB8552E\\Patches"); | |
| 43 | |
| 44 void InstallMsi() { | |
| 45 CString msi_path(app_util::GetCurrentModuleDirectory()); | |
| 46 EXPECT_TRUE(::PathAppend(CStrBuf(msi_path, MAX_PATH), | |
| 47 kHelperInstallerName)); | |
| 48 ::MsiSetInternalUI(INSTALLUILEVEL_NONE, NULL); | |
| 49 | |
| 50 UINT res = ::MsiInstallProduct(msi_path, _T("")); | |
| 51 | |
| 52 if (vista_util::IsUserAdmin()) { | |
| 53 if (ERROR_SUCCESS != res) { | |
| 54 EXPECT_EQ(ERROR_PRODUCT_VERSION, res); | |
| 55 // The product may already be installed. Force a reinstall of everything. | |
| 56 res = ::MsiInstallProduct(msi_path, | |
| 57 _T("REINSTALL=ALL REINSTALLMODE=vamus")); | |
| 58 } | |
| 59 EXPECT_EQ(ERROR_SUCCESS, res); | |
| 60 } else { | |
| 61 if (IsMsiHelperInstalled()) { | |
| 62 EXPECT_EQ(ERROR_INSTALL_PACKAGE_REJECTED, res); | |
| 63 } else { | |
| 64 EXPECT_EQ(ERROR_INSTALL_FAILURE, res); | |
| 65 } | |
| 66 } | |
| 67 } | |
| 68 | |
| 69 void RemoveMsi() { | |
| 70 ::MsiSetInternalUI(INSTALLUILEVEL_NONE, NULL); | |
| 71 UINT res = ::MsiConfigureProduct(kHelperInstallerProductGuid, | |
| 72 INSTALLLEVEL_DEFAULT, | |
| 73 INSTALLSTATE_ABSENT); | |
| 74 if (vista_util::IsUserAdmin()) { | |
| 75 EXPECT_TRUE((ERROR_SUCCESS == res) || | |
| 76 ((ERROR_UNKNOWN_PRODUCT == res) && !IsMsiHelperInstalled())); | |
| 77 } else { | |
| 78 if (IsMsiHelperInstalled()) { | |
| 79 EXPECT_EQ(ERROR_INSTALL_FAILURE, res); | |
| 80 } else { | |
| 81 EXPECT_EQ(ERROR_UNKNOWN_PRODUCT, res); | |
| 82 } | |
| 83 } | |
| 84 } | |
| 85 | |
| 86 HRESULT ExecuteGoogleSignedExeWithCorrectPatchInfo(const TCHAR* executable, | |
| 87 const TCHAR* arguments, | |
| 88 HANDLE* process) { | |
| 89 return msp_executable_elevator::ExecuteGoogleSignedExe( | |
| 90 executable, | |
| 91 arguments, | |
| 92 kHelperInstallerProductGuid, | |
| 93 kHelperPatchGuid, | |
| 94 kHelperPatchName, | |
| 95 process); | |
| 96 } | |
| 97 | |
| 98 // Base class for tests that expect the MSI to be installed. | |
| 99 // The elevation mechanism will not be installed when these test complete. | |
| 100 class RepairGoopdateWithMsiInstalledTest : public testing::Test { | |
| 101 protected: | |
| 102 static void SetUpTestCase() { | |
| 103 InstallMsi(); | |
| 104 } | |
| 105 | |
| 106 static void TearDownTestCase() { | |
| 107 RemoveMsi(); | |
| 108 } | |
| 109 }; | |
| 110 | |
| 111 // Base class for tests that expect the MSI not to be installed. | |
| 112 // The elevation mechanism will not be installed when these test complete. | |
| 113 class RepairGoopdateWithoutMsiInstalledTest : public testing::Test { | |
| 114 protected: | |
| 115 static void SetUpTestCase() { | |
| 116 RemoveMsi(); | |
| 117 } | |
| 118 }; | |
| 119 | |
| 120 TEST_F(RepairGoopdateWithMsiInstalledTest, | |
| 121 ExecuteGoogleSignedExe_RepairFileDoesNotExist) { | |
| 122 CString repair_file(_T("no_such_file.exe")); | |
| 123 HANDLE process = NULL; | |
| 124 HRESULT hr = ExecuteGoogleSignedExeWithCorrectPatchInfo(repair_file, | |
| 125 _T(""), | |
| 126 &process); | |
| 127 if (vista_util::IsUserAdmin() || IsMsiHelperInstalled()) { | |
| 128 EXPECT_EQ(HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND), hr); | |
| 129 } else { | |
| 130 EXPECT_EQ(HRESULT_FROM_WIN32(ERROR_PATCH_TARGET_NOT_FOUND), hr); | |
| 131 } | |
| 132 | |
| 133 EXPECT_TRUE(NULL == process); | |
| 134 } | |
| 135 | |
| 136 TEST_F(RepairGoopdateWithMsiInstalledTest, | |
| 137 ExecuteGoogleSignedExe_UnsignedFile) { | |
| 138 CString repair_file(app_util::GetCurrentModuleDirectory()); | |
| 139 EXPECT_TRUE(::PathAppend(CStrBuf(repair_file, MAX_PATH), | |
| 140 _T("GoogleUpdate_unsigned.exe"))); | |
| 141 HANDLE process = NULL; | |
| 142 HRESULT hr = ExecuteGoogleSignedExeWithCorrectPatchInfo(repair_file, | |
| 143 _T(""), | |
| 144 &process); | |
| 145 if (vista_util::IsUserAdmin() || IsMsiHelperInstalled()) { | |
| 146 EXPECT_EQ(TRUST_E_NOSIGNATURE, hr); | |
| 147 } else { | |
| 148 EXPECT_EQ(HRESULT_FROM_WIN32(ERROR_PATCH_TARGET_NOT_FOUND), hr); | |
| 149 } | |
| 150 EXPECT_TRUE(NULL == process); | |
| 151 } | |
| 152 | |
| 153 // This valid repair file saves the arguments passed to it to a file. | |
| 154 TEST_F(RepairGoopdateWithMsiInstalledTest, | |
| 155 ExecuteGoogleSignedExe_ValidRepairFile) { | |
| 156 const TCHAR kArgs[] = _T("These /are the args."); | |
| 157 CString repair_file(app_util::GetCurrentModuleDirectory()); | |
| 158 EXPECT_TRUE(::PathAppend(CStrBuf(repair_file, MAX_PATH), | |
| 159 _T("unittest_support\\SaveArguments.exe"))); | |
| 160 CString program_files_path; | |
| 161 EXPECT_SUCCEEDED(GetFolderPath(CSIDL_PROGRAM_FILES, &program_files_path)); | |
| 162 CString saved_arguments_file_path = | |
| 163 program_files_path + _T("\\Google\\Update\\saved_arguments.txt"); | |
| 164 | |
| 165 ::DeleteFile(saved_arguments_file_path); | |
| 166 | |
| 167 bool is_msi_installed = IsMsiHelperInstalled(); | |
| 168 | |
| 169 if (vista_util::IsUserAdmin() || is_msi_installed) { | |
| 170 if (vista_util::IsUserAdmin()) { | |
| 171 EXPECT_FALSE(File::Exists(saved_arguments_file_path)); | |
| 172 } | |
| 173 | |
| 174 // Verify that no patch is installed. | |
| 175 EXPECT_TRUE(RegKey::HasNativeKey(kMsiProductPatchesKey)); | |
| 176 RegKey product_patches_key; | |
| 177 EXPECT_SUCCEEDED(product_patches_key.Open(kMsiProductPatchesKey, | |
| 178 KEY_READ | KEY_WOW64_64KEY)); | |
| 179 EXPECT_EQ(0, product_patches_key.GetSubkeyCount()); | |
| 180 | |
| 181 HANDLE process = NULL; | |
| 182 EXPECT_SUCCEEDED(ExecuteGoogleSignedExeWithCorrectPatchInfo(repair_file, | |
| 183 kArgs, | |
| 184 &process)); | |
| 185 EXPECT_TRUE(NULL == process); | |
| 186 | |
| 187 // Verify that patch was uninstalled. | |
| 188 // GetSubkeyCount fails if we don't re-open the key. | |
| 189 EXPECT_SUCCEEDED(product_patches_key.Open(kMsiProductPatchesKey, | |
| 190 KEY_READ | KEY_WOW64_64KEY)); | |
| 191 EXPECT_EQ(0, product_patches_key.GetSubkeyCount()); | |
| 192 | |
| 193 bool is_found = false; | |
| 194 for (int tries = 0; tries < 100 && !is_found; ++tries) { | |
| 195 ::Sleep(50); | |
| 196 is_found = File::Exists(saved_arguments_file_path); | |
| 197 } | |
| 198 ASSERT_TRUE(is_found); | |
| 199 | |
| 200 scoped_hfile file; | |
| 201 for (int tries = 0; tries < 100 && !valid(file); ++tries) { | |
| 202 ::Sleep(50); | |
| 203 reset(file, ::CreateFile(saved_arguments_file_path, | |
| 204 GENERIC_READ, | |
| 205 0, // do not share | |
| 206 NULL, // default security | |
| 207 OPEN_EXISTING, // existing file only | |
| 208 FILE_ATTRIBUTE_NORMAL, | |
| 209 NULL)); // no template | |
| 210 } | |
| 211 ASSERT_TRUE(valid(file)); | |
| 212 | |
| 213 const int kBufferLen = 50; | |
| 214 TCHAR buffer[kBufferLen + 1] = {0}; | |
| 215 DWORD bytes_read = 0; | |
| 216 | |
| 217 EXPECT_TRUE(::ReadFile(get(file), | |
| 218 buffer, | |
| 219 kBufferLen * sizeof(TCHAR), | |
| 220 &bytes_read, | |
| 221 NULL)); | |
| 222 EXPECT_EQ(0, bytes_read % sizeof(TCHAR)); | |
| 223 buffer[bytes_read / sizeof(TCHAR)] = _T('\0'); | |
| 224 EXPECT_STREQ(kArgs, buffer); | |
| 225 | |
| 226 reset(file); | |
| 227 | |
| 228 bool succeeded = !!::DeleteFile(saved_arguments_file_path); | |
| 229 if (vista_util::IsUserAdmin()) { | |
| 230 EXPECT_TRUE(succeeded); | |
| 231 } | |
| 232 } else { | |
| 233 bool expected_file_exists = File::Exists(saved_arguments_file_path); | |
| 234 HANDLE process = NULL; | |
| 235 EXPECT_EQ(HRESULT_FROM_WIN32(ERROR_PATCH_TARGET_NOT_FOUND), | |
| 236 ExecuteGoogleSignedExeWithCorrectPatchInfo(repair_file, | |
| 237 kArgs, | |
| 238 &process)); | |
| 239 EXPECT_TRUE(NULL == process); | |
| 240 | |
| 241 // We can't force the file to be deleted, so make sure it wasn't created | |
| 242 // or deleted by the above method. | |
| 243 EXPECT_EQ(expected_file_exists, File::Exists(saved_arguments_file_path)); | |
| 244 } | |
| 245 } | |
| 246 | |
| 247 TEST_F(RepairGoopdateWithoutMsiInstalledTest, | |
| 248 ExecuteGoogleSignedExe_MsiNotInstalled) { | |
| 249 if (!vista_util::IsUserAdmin() && IsMsiHelperInstalled()) { | |
| 250 std::wcout << _T("\tThis test did not run because the user is not an ") | |
| 251 _T("admin and the MSI is already installed.") << std::endl; | |
| 252 return; | |
| 253 } | |
| 254 | |
| 255 CString repair_file(_T("notepad.exe")); | |
| 256 HANDLE process = NULL; | |
| 257 EXPECT_EQ(HRESULT_FROM_WIN32(ERROR_PATCH_TARGET_NOT_FOUND), | |
| 258 ExecuteGoogleSignedExeWithCorrectPatchInfo(repair_file, | |
| 259 _T(""), | |
| 260 &process)); | |
| 261 EXPECT_TRUE(NULL == process); | |
| 262 } | |
| 263 | |
| 264 } // namespace omaha | |
| OLD | NEW |