OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/installer/setup/setup_singleton.h" |
| 6 |
| 7 #include <windows.h> |
| 8 |
| 9 #include <functional> |
| 10 #include <string> |
| 11 #include <vector> |
| 12 |
| 13 #include "base/command_line.h" |
| 14 #include "base/files/file.h" |
| 15 #include "base/files/file_path.h" |
| 16 #include "base/files/scoped_temp_dir.h" |
| 17 #include "base/strings/string16.h" |
| 18 #include "base/strings/string_number_conversions.h" |
| 19 #include "base/test/multiprocess_test.h" |
| 20 #include "base/test/test_timeouts.h" |
| 21 #include "base/threading/platform_thread.h" |
| 22 #include "base/time/time.h" |
| 23 #include "chrome/installer/util/installation_state.h" |
| 24 #include "chrome/installer/util/installer_state.h" |
| 25 #include "chrome/installer/util/master_preferences.h" |
| 26 #include "testing/gtest/include/gtest/gtest.h" |
| 27 #include "testing/multiprocess_func_list.h" |
| 28 |
| 29 namespace installer { |
| 30 |
| 31 namespace { |
| 32 |
| 33 constexpr char kInstallDirSwitch[] = "install-dir"; |
| 34 constexpr base::FilePath::CharType kSentinelFileName[] = |
| 35 FILE_PATH_LITERAL("sentinel.txt"); |
| 36 constexpr base::char16 kTestProcessReadyEventName[] = |
| 37 L"Local\\ChromeSetupSingletonTestProcessReady"; |
| 38 |
| 39 enum ErrorCode { |
| 40 SUCCESS, |
| 41 SETUP_SINGLETON_ACQUISITION_FAILED, |
| 42 SENTINEL_FILE_CREATE_ERROR, |
| 43 WAIT_RETURNED_FALSE, |
| 44 }; |
| 45 |
| 46 base::CommandLine GetDummyCommandLine() { |
| 47 return base::CommandLine(base::FilePath(FILE_PATH_LITERAL("dummy.exe"))); |
| 48 } |
| 49 |
| 50 base::string16 HashFilePath(const base::FilePath& path) { |
| 51 return base::SizeTToString16( |
| 52 std::hash<base::FilePath::StringType>()(path.value())); |
| 53 } |
| 54 |
| 55 ErrorCode CreateAndDeleteSentinelFile(const base::FilePath& install_dir) { |
| 56 const base::FilePath sentinel_file_path = |
| 57 install_dir.Append(kSentinelFileName); |
| 58 |
| 59 base::File file(sentinel_file_path, base::File::FLAG_CREATE | |
| 60 base::File::FLAG_WRITE | |
| 61 base::File::FLAG_DELETE_ON_CLOSE); |
| 62 if (!file.IsValid()) |
| 63 return SENTINEL_FILE_CREATE_ERROR; |
| 64 |
| 65 base::PlatformThread::Sleep(TestTimeouts::tiny_timeout()); |
| 66 return SUCCESS; |
| 67 } |
| 68 |
| 69 MULTIPROCESS_TEST_MAIN(SetupSingletonTestExclusiveAccessProcessMain) { |
| 70 base::CommandLine* const command_line = |
| 71 base::CommandLine::ForCurrentProcess(); |
| 72 const base::FilePath install_dir = |
| 73 command_line->GetSwitchValuePath(kInstallDirSwitch); |
| 74 |
| 75 InstallationState original_state; |
| 76 InstallerState installer_state; |
| 77 installer_state.set_target_path_for_testing(install_dir); |
| 78 |
| 79 // Acquire the exclusive right to modify the Chrome installation. |
| 80 std::unique_ptr<SetupSingleton> setup_singleton(SetupSingleton::Acquire( |
| 81 GetDummyCommandLine(), MasterPreferences::ForCurrentProcess(), |
| 82 &original_state, &installer_state)); |
| 83 if (!setup_singleton) |
| 84 return SETUP_SINGLETON_ACQUISITION_FAILED; |
| 85 |
| 86 // Create a sentinel file and delete it after a few milliseconds. This will |
| 87 // fail if the sentinel file already exists (which shouldn't be the case since |
| 88 // we are in the scope of a SetupSingleton). |
| 89 return CreateAndDeleteSentinelFile(install_dir); |
| 90 } |
| 91 |
| 92 MULTIPROCESS_TEST_MAIN(SetupSingletonTestWaitForInterruptProcessMain) { |
| 93 base::CommandLine* const command_line = |
| 94 base::CommandLine::ForCurrentProcess(); |
| 95 const base::FilePath install_dir = |
| 96 command_line->GetSwitchValuePath(kInstallDirSwitch); |
| 97 |
| 98 InstallationState original_state; |
| 99 InstallerState installer_state; |
| 100 installer_state.set_target_path_for_testing(install_dir); |
| 101 |
| 102 // Acquire the exclusive right to modify the Chrome installation. |
| 103 std::unique_ptr<SetupSingleton> setup_singleton(SetupSingleton::Acquire( |
| 104 GetDummyCommandLine(), MasterPreferences::ForCurrentProcess(), |
| 105 &original_state, &installer_state)); |
| 106 if (!setup_singleton) |
| 107 return SETUP_SINGLETON_ACQUISITION_FAILED; |
| 108 |
| 109 // Signal an event to indicate that this process has acquired the |
| 110 // SetupSingleton. |
| 111 base::WaitableEvent ready_event(base::win::ScopedHandle(::CreateEvent( |
| 112 nullptr, FALSE, FALSE, |
| 113 (kTestProcessReadyEventName + HashFilePath(install_dir)).c_str()))); |
| 114 ready_event.Signal(); |
| 115 |
| 116 // Wait indefinitely. This should only return when another SetupSingleton is |
| 117 // instantiated for |install_dir|. |
| 118 if (!setup_singleton->WaitForInterrupt(base::TimeDelta::Max())) |
| 119 return WAIT_RETURNED_FALSE; |
| 120 |
| 121 // Create a sentinel file and delete it after a few milliseconds. This will |
| 122 // fail if the sentinel file already exists (which shouldn't be the case since |
| 123 // we are in the scope of a SetupSingleton). |
| 124 return CreateAndDeleteSentinelFile(install_dir); |
| 125 } |
| 126 |
| 127 class SetupSingletonTest : public base::MultiProcessTest { |
| 128 public: |
| 129 SetupSingletonTest() = default; |
| 130 |
| 131 void SetUp() override { ASSERT_TRUE(install_dir_.CreateUniqueTempDir()); } |
| 132 |
| 133 base::CommandLine MakeCmdLine(const std::string& procname) override { |
| 134 base::CommandLine command_line = |
| 135 base::MultiProcessTest::MakeCmdLine(procname); |
| 136 command_line.AppendSwitchPath(kInstallDirSwitch, install_dir_path()); |
| 137 return command_line; |
| 138 } |
| 139 |
| 140 base::Process SpawnChildProcess(const std::string& process_name) { |
| 141 base::LaunchOptions options; |
| 142 options.start_hidden = true; |
| 143 return SpawnChildWithOptions(process_name, options); |
| 144 } |
| 145 |
| 146 const base::FilePath& install_dir_path() const { return install_dir_.path(); } |
| 147 |
| 148 private: |
| 149 base::ScopedTempDir install_dir_; |
| 150 |
| 151 DISALLOW_COPY_AND_ASSIGN(SetupSingletonTest); |
| 152 }; |
| 153 |
| 154 } // namespace |
| 155 |
| 156 // Verify that a single SetupSingleton can be active at a time for a given |
| 157 // Chrome installation. |
| 158 TEST_F(SetupSingletonTest, ExclusiveAccess) { |
| 159 constexpr int kNumProcesses = 10; |
| 160 |
| 161 std::vector<base::Process> processes; |
| 162 for (int i = 0; i < kNumProcesses; ++i) { |
| 163 processes.push_back( |
| 164 SpawnChildProcess("SetupSingletonTestExclusiveAccessProcessMain")); |
| 165 } |
| 166 |
| 167 for (base::Process& process : processes) { |
| 168 int exit_code = 0; |
| 169 EXPECT_TRUE(process.WaitForExit(&exit_code)); |
| 170 EXPECT_EQ(SUCCESS, exit_code); |
| 171 } |
| 172 } |
| 173 |
| 174 // Verify that WaitForInterrupt() returns false when its delay expires before |
| 175 TEST_F(SetupSingletonTest, WaitForInterruptNoInterrupt) { |
| 176 InstallationState original_state; |
| 177 InstallerState installer_state; |
| 178 installer_state.set_target_path_for_testing(install_dir_path()); |
| 179 std::unique_ptr<SetupSingleton> setup_singleton(SetupSingleton::Acquire( |
| 180 GetDummyCommandLine(), MasterPreferences::ForCurrentProcess(), |
| 181 &original_state, &installer_state)); |
| 182 ASSERT_TRUE(setup_singleton); |
| 183 |
| 184 EXPECT_FALSE(setup_singleton->WaitForInterrupt(TestTimeouts::tiny_timeout())); |
| 185 } |
| 186 |
| 187 // Verify that WaitForInterrupt() returns true immediately when another process |
| 188 // tries to acquire a SetupSingleton. |
| 189 TEST_F(SetupSingletonTest, WaitForInterruptWithInterrupt) { |
| 190 base::Process wait_process = |
| 191 SpawnChildProcess("SetupSingletonTestWaitForInterruptProcessMain"); |
| 192 |
| 193 // Wait until the other process acquires the SetupSingleton. |
| 194 base::WaitableEvent ready_event(base::win::ScopedHandle(::CreateEvent( |
| 195 nullptr, FALSE, FALSE, |
| 196 (kTestProcessReadyEventName + HashFilePath(install_dir_path())) |
| 197 .c_str()))); |
| 198 ready_event.Wait(); |
| 199 |
| 200 // Acquire the SetupSingleton. |
| 201 InstallationState original_state; |
| 202 InstallerState installer_state; |
| 203 installer_state.set_target_path_for_testing(install_dir_path()); |
| 204 std::unique_ptr<SetupSingleton> setup_singleton(SetupSingleton::Acquire( |
| 205 GetDummyCommandLine(), MasterPreferences::ForCurrentProcess(), |
| 206 &original_state, &installer_state)); |
| 207 ASSERT_TRUE(setup_singleton); |
| 208 |
| 209 // Create a sentinel file and delete it after a few milliseconds. This will |
| 210 // fail if the sentinel file already exists (which shouldn't be the case since |
| 211 // we are in the scope of a SetupSingleton). |
| 212 EXPECT_EQ(SUCCESS, CreateAndDeleteSentinelFile(install_dir_path())); |
| 213 |
| 214 // Join |wait_process|. |
| 215 int exit_code = 0; |
| 216 EXPECT_TRUE(wait_process.WaitForExit(&exit_code)); |
| 217 EXPECT_EQ(SUCCESS, exit_code); |
| 218 } |
| 219 |
| 220 } // namespace installer |
OLD | NEW |