| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "base/command_line.h" | 5 #include "base/command_line.h" |
| 6 #include "base/files/file.h" | 6 #include "base/files/file.h" |
| 7 #include "base/files/file_util.h" | 7 #include "base/files/file_util.h" |
| 8 #include "base/files/scoped_temp_dir.h" | 8 #include "base/files/scoped_temp_dir.h" |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/test/multiprocess_test.h" | 10 #include "base/test/multiprocess_test.h" |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 public: | 122 public: |
| 123 FileLockingTest() {} | 123 FileLockingTest() {} |
| 124 | 124 |
| 125 protected: | 125 protected: |
| 126 void SetUp() override { | 126 void SetUp() override { |
| 127 testing::Test::SetUp(); | 127 testing::Test::SetUp(); |
| 128 | 128 |
| 129 // Setup the temp dir and the lock file. | 129 // Setup the temp dir and the lock file. |
| 130 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 130 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 131 lock_file_.Initialize( | 131 lock_file_.Initialize( |
| 132 temp_dir_.path().AppendASCII(kLockFile), | 132 temp_dir_.GetPath().AppendASCII(kLockFile), |
| 133 File::FLAG_CREATE | File::FLAG_READ | File::FLAG_WRITE); | 133 File::FLAG_CREATE | File::FLAG_READ | File::FLAG_WRITE); |
| 134 ASSERT_TRUE(lock_file_.IsValid()); | 134 ASSERT_TRUE(lock_file_.IsValid()); |
| 135 } | 135 } |
| 136 | 136 |
| 137 bool SignalEvent(const char* signal_file) { | 137 bool SignalEvent(const char* signal_file) { |
| 138 return ::SignalEvent(temp_dir_.path(), signal_file); | 138 return ::SignalEvent(temp_dir_.GetPath(), signal_file); |
| 139 } | 139 } |
| 140 | 140 |
| 141 bool WaitForEventOrTimeout(const char* signal_file) { | 141 bool WaitForEventOrTimeout(const char* signal_file) { |
| 142 return ::WaitForEventWithTimeout(temp_dir_.path(), signal_file, | 142 return ::WaitForEventWithTimeout(temp_dir_.GetPath(), signal_file, |
| 143 TestTimeouts::action_timeout()); | 143 TestTimeouts::action_timeout()); |
| 144 } | 144 } |
| 145 | 145 |
| 146 // Start a child process set to use the specified unlock action, and wait for | 146 // Start a child process set to use the specified unlock action, and wait for |
| 147 // it to lock the file. | 147 // it to lock the file. |
| 148 void StartChildAndSignalLock(const char* unlock_action) { | 148 void StartChildAndSignalLock(const char* unlock_action) { |
| 149 // Create a temporary dir and spin up a ChildLockExit subprocess against it. | 149 // Create a temporary dir and spin up a ChildLockExit subprocess against it. |
| 150 const FilePath temp_path = temp_dir_.path(); | 150 const FilePath temp_path = temp_dir_.GetPath(); |
| 151 base::CommandLine child_command_line( | 151 base::CommandLine child_command_line( |
| 152 base::GetMultiProcessTestChildBaseCommandLine()); | 152 base::GetMultiProcessTestChildBaseCommandLine()); |
| 153 child_command_line.AppendSwitchPath(kTempDirFlag, temp_path); | 153 child_command_line.AppendSwitchPath(kTempDirFlag, temp_path); |
| 154 child_command_line.AppendSwitch(unlock_action); | 154 child_command_line.AppendSwitch(unlock_action); |
| 155 lock_child_ = | 155 lock_child_ = |
| 156 base::SpawnMultiProcessTestChild(ChildMainString, child_command_line, | 156 base::SpawnMultiProcessTestChild(ChildMainString, child_command_line, |
| 157 base::LaunchOptions()); | 157 base::LaunchOptions()); |
| 158 ASSERT_TRUE(lock_child_.IsValid()); | 158 ASSERT_TRUE(lock_child_.IsValid()); |
| 159 | 159 |
| 160 // Wait for the child to lock the file. | 160 // Wait for the child to lock the file. |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 217 // Test that killing the process releases the lock. This should cover crashing. | 217 // Test that killing the process releases the lock. This should cover crashing. |
| 218 TEST_F(FileLockingTest, UnlockOnTerminate) { | 218 TEST_F(FileLockingTest, UnlockOnTerminate) { |
| 219 // The child will wait for an exit which never arrives. | 219 // The child will wait for an exit which never arrives. |
| 220 StartChildAndSignalLock(kExitUnlock); | 220 StartChildAndSignalLock(kExitUnlock); |
| 221 | 221 |
| 222 ASSERT_NE(File::FILE_OK, lock_file_.Lock()); | 222 ASSERT_NE(File::FILE_OK, lock_file_.Lock()); |
| 223 ASSERT_TRUE(lock_child_.Terminate(0, true)); | 223 ASSERT_TRUE(lock_child_.Terminate(0, true)); |
| 224 ASSERT_EQ(File::FILE_OK, lock_file_.Lock()); | 224 ASSERT_EQ(File::FILE_OK, lock_file_.Lock()); |
| 225 ASSERT_EQ(File::FILE_OK, lock_file_.Unlock()); | 225 ASSERT_EQ(File::FILE_OK, lock_file_.Unlock()); |
| 226 } | 226 } |
| OLD | NEW |