Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 The Crashpad Authors. All rights reserved. | |
| 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 #include "util/file/file_io.h" | |
| 16 | |
| 17 #include "base/atomicops.h" | |
| 18 #include "base/basictypes.h" | |
| 19 #include "base/files/file_path.h" | |
| 20 #include "gtest/gtest.h" | |
| 21 #include "util/test/errors.h" | |
| 22 #include "util/test/scoped_temp_dir.h" | |
| 23 #include "util/test/thread.h" | |
| 24 | |
| 25 namespace crashpad { | |
| 26 namespace test { | |
| 27 namespace { | |
| 28 | |
| 29 enum class ReadOrWrite : bool { | |
| 30 kRead, | |
| 31 kWrite, | |
| 32 }; | |
| 33 | |
| 34 void FileShareModeTest(ReadOrWrite first, ReadOrWrite second) { | |
| 35 ScopedTempDir temp_dir; | |
| 36 base::FilePath shared_file = | |
| 37 temp_dir.path().Append(FILE_PATH_LITERAL("shared_file")); | |
| 38 { | |
| 39 // Create an empty file to work on. | |
| 40 ScopedFileHandle create( | |
| 41 LoggingOpenFileForWrite(shared_file, | |
| 42 FileWriteMode::kCreateOrFail, | |
| 43 FilePermissions::kOwnerOnly)); | |
| 44 } | |
| 45 | |
| 46 auto handle1 = ScopedFileHandle( | |
| 47 (first == ReadOrWrite::kRead) | |
| 48 ? LoggingOpenFileForRead(shared_file) | |
| 49 : LoggingOpenFileForWrite(shared_file, | |
| 50 FileWriteMode::kReuseOrCreate, | |
| 51 FilePermissions::kOwnerOnly)); | |
| 52 ASSERT_NE(handle1, kInvalidFileHandle); | |
| 53 auto handle2 = ScopedFileHandle( | |
| 54 (second == ReadOrWrite::kRead) | |
| 55 ? LoggingOpenFileForRead(shared_file) | |
| 56 : LoggingOpenFileForWrite(shared_file, | |
| 57 FileWriteMode::kReuseOrCreate, | |
| 58 FilePermissions::kOwnerOnly)); | |
| 59 EXPECT_NE(handle2, kInvalidFileHandle); | |
| 60 | |
| 61 EXPECT_NE(handle1.get(), handle2.get()); | |
| 62 } | |
| 63 | |
| 64 TEST(FileIO, FileShareMode_Read_Read) { | |
| 65 FileShareModeTest(ReadOrWrite::kRead, ReadOrWrite::kRead); | |
| 66 } | |
| 67 | |
| 68 TEST(FileIO, FileShareMode_Read_Write) { | |
| 69 FileShareModeTest(ReadOrWrite::kRead, ReadOrWrite::kWrite); | |
| 70 } | |
| 71 | |
| 72 TEST(FileIO, FileShareMode_Write_Read) { | |
| 73 FileShareModeTest(ReadOrWrite::kWrite, ReadOrWrite::kRead); | |
| 74 } | |
| 75 | |
| 76 TEST(FileIO, FileShareMode_Write_Write) { | |
| 77 FileShareModeTest(ReadOrWrite::kWrite, ReadOrWrite::kWrite); | |
| 78 } | |
| 79 | |
| 80 TEST(FileIO, MultipleSharedLocks) { | |
| 81 ScopedTempDir temp_dir; | |
| 82 base::FilePath shared_file = | |
| 83 temp_dir.path().Append(FILE_PATH_LITERAL("file_to_lock")); | |
| 84 | |
| 85 { | |
| 86 // Create an empty file to lock. | |
| 87 ScopedFileHandle create( | |
| 88 LoggingOpenFileForWrite(shared_file, | |
| 89 FileWriteMode::kCreateOrFail, | |
| 90 FilePermissions::kOwnerOnly)); | |
| 91 } | |
| 92 | |
| 93 auto handle1 = ScopedFileHandle(LoggingOpenFileForRead(shared_file)); | |
| 94 ASSERT_NE(handle1, kInvalidFileHandle); | |
| 95 EXPECT_TRUE(LoggingLockFile(handle1.get(), FileLocking::kShared)); | |
| 96 | |
| 97 auto handle2 = ScopedFileHandle(LoggingOpenFileForRead(shared_file)); | |
| 98 ASSERT_NE(handle1, kInvalidFileHandle); | |
| 99 EXPECT_TRUE(LoggingLockFile(handle2.get(), FileLocking::kShared)); | |
| 100 | |
| 101 EXPECT_TRUE(LoggingUnlockFile(handle1.get())); | |
| 102 EXPECT_TRUE(LoggingUnlockFile(handle2.get())); | |
| 103 } | |
| 104 | |
| 105 class LockingTestThread : public Thread { | |
| 106 public: | |
| 107 LockingTestThread() | |
| 108 : file_(), lock_type_(), iterations_(), actual_iterations_() {} | |
| 109 | |
| 110 void Init(FileHandle file, | |
| 111 FileLocking lock_type, | |
| 112 int iterations, | |
| 113 base::subtle::Atomic32* actual_iterations) { | |
| 114 ASSERT_NE(file, kInvalidFileHandle); | |
| 115 file_ = ScopedFileHandle(file); | |
| 116 lock_type_ = lock_type; | |
| 117 iterations_ = iterations; | |
| 118 actual_iterations_ = actual_iterations; | |
| 119 } | |
| 120 | |
| 121 private: | |
| 122 void ThreadMain() override { | |
| 123 for (int i = 0; i < iterations_; ++i) { | |
| 124 EXPECT_TRUE(LoggingLockFile(file_.get(), lock_type_)); | |
| 125 base::subtle::NoBarrier_AtomicIncrement(actual_iterations_, 1); | |
| 126 EXPECT_TRUE(LoggingUnlockFile(file_.get())); | |
| 127 } | |
| 128 } | |
| 129 | |
| 130 ScopedFileHandle file_; | |
| 131 FileLocking lock_type_; | |
| 132 int iterations_; | |
| 133 base::subtle::Atomic32* actual_iterations_; | |
| 134 | |
| 135 DISALLOW_COPY_AND_ASSIGN(LockingTestThread); | |
| 136 }; | |
| 137 | |
| 138 void LockingTest(FileLocking main_lock, FileLocking other_locks) { | |
| 139 ScopedTempDir temp_dir; | |
| 140 base::FilePath shared_file = | |
| 141 temp_dir.path().Append(FILE_PATH_LITERAL("file_to_lock")); | |
| 142 | |
| 143 { | |
| 144 // Create an empty file to lock. | |
| 145 ScopedFileHandle create( | |
| 146 LoggingOpenFileForWrite(shared_file, | |
| 147 FileWriteMode::kCreateOrFail, | |
| 148 FilePermissions::kOwnerOnly)); | |
| 149 } | |
| 150 | |
| 151 auto initial = ScopedFileHandle( | |
| 152 (main_lock == FileLocking::kShared) | |
| 153 ? LoggingOpenFileForRead(shared_file) | |
| 154 : LoggingOpenFileForWrite(shared_file, | |
| 155 FileWriteMode::kReuseOrCreate, | |
| 156 FilePermissions::kOwnerOnly)); | |
| 157 ASSERT_NE(initial, kInvalidFileHandle); | |
| 158 ASSERT_TRUE(LoggingLockFile(initial.get(), main_lock)); | |
| 159 | |
| 160 base::subtle::Atomic32 actual_iterations = 0; | |
| 161 | |
| 162 LockingTestThread threads[20]; | |
| 163 int expected_iterations = 0; | |
| 164 for (size_t index = 0; index < arraysize(threads); ++index) { | |
| 165 int iterations_for_this_thread = static_cast<int>(index * 10); | |
| 166 threads[index].Init( | |
| 167 (other_locks == FileLocking::kShared) | |
| 168 ? LoggingOpenFileForRead(shared_file) | |
| 169 : LoggingOpenFileForWrite(shared_file, | |
| 170 FileWriteMode::kReuseOrCreate, | |
| 171 FilePermissions::kOwnerOnly), | |
| 172 other_locks, | |
| 173 iterations_for_this_thread, | |
| 174 &actual_iterations); | |
| 175 expected_iterations += iterations_for_this_thread; | |
| 176 | |
| 177 ASSERT_NO_FATAL_FAILURE(threads[index].Start()); | |
| 178 } | |
| 179 | |
| 180 base::subtle::Atomic32 result = | |
| 181 base::subtle::Release_Load(&actual_iterations); | |
| 182 EXPECT_EQ(0, result); | |
| 183 | |
| 184 ASSERT_TRUE(LoggingUnlockFile(initial.get())); | |
| 185 | |
| 186 for (auto& t : threads) | |
| 187 t.Join(); | |
| 188 | |
| 189 EXPECT_EQ(expected_iterations, actual_iterations); | |
|
Mark Mentovai
2015/03/20 22:41:46
Release_Load into result again and use that instea
scottmg
2015/03/20 22:45:34
Done.
| |
| 190 } | |
| 191 | |
| 192 TEST(FileIO, ExclusiveVsExclusives) { | |
| 193 LockingTest(FileLocking::kExclusive, FileLocking::kExclusive); | |
| 194 } | |
| 195 | |
| 196 TEST(FileIO, ExclusiveVsShareds) { | |
| 197 LockingTest(FileLocking::kExclusive, FileLocking::kShared); | |
| 198 } | |
| 199 | |
| 200 TEST(FileIO, SharedVsExclusives) { | |
| 201 LockingTest(FileLocking::kShared, FileLocking::kExclusive); | |
| 202 } | |
| 203 | |
| 204 } // namespace | |
| 205 } // namespace test | |
| 206 } // namespace crashpad | |
| OLD | NEW |