OLD | NEW |
1 // Copyright 2015 The Crashpad Authors. All rights reserved. | 1 // Copyright 2015 The Crashpad Authors. All rights reserved. |
2 // | 2 // |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
4 // you may not use this file except in compliance with 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 | 5 // You may obtain a copy of the License at |
6 // | 6 // |
7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
8 // | 8 // |
9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 // See the License for the specific language governing permissions and | 12 // See the License for the specific language governing permissions and |
13 // limitations under the License. | 13 // limitations under the License. |
14 | 14 |
15 #include "util/file/file_io.h" | 15 #include "util/file/file_io.h" |
16 | 16 |
17 #include "base/atomicops.h" | 17 #include "base/atomicops.h" |
18 #include "base/basictypes.h" | 18 #include "base/basictypes.h" |
19 #include "base/files/file_path.h" | 19 #include "base/files/file_path.h" |
20 #include "gtest/gtest.h" | 20 #include "gtest/gtest.h" |
21 #include "test/errors.h" | 21 #include "test/errors.h" |
| 22 #include "test/file.h" |
22 #include "test/scoped_temp_dir.h" | 23 #include "test/scoped_temp_dir.h" |
| 24 #include "util/misc/implicit_cast.h" |
23 #include "util/thread/thread.h" | 25 #include "util/thread/thread.h" |
24 | 26 |
25 namespace crashpad { | 27 namespace crashpad { |
26 namespace test { | 28 namespace test { |
27 namespace { | 29 namespace { |
28 | 30 |
| 31 TEST(FileIO, OpenFileForWrite) { |
| 32 ScopedTempDir temp_dir; |
| 33 base::FilePath file_path_1 = |
| 34 temp_dir.path().Append(FILE_PATH_LITERAL("file_1")); |
| 35 ASSERT_FALSE(FileExists(file_path_1)); |
| 36 |
| 37 ScopedFileHandle |
| 38 file_handle(LoggingOpenFileForWrite(file_path_1, |
| 39 FileWriteMode::kReuseOrFail, |
| 40 FilePermissions::kWorldReadable)); |
| 41 EXPECT_EQ(kInvalidFileHandle, file_handle); |
| 42 EXPECT_FALSE(FileExists(file_path_1)); |
| 43 |
| 44 file_handle.reset(LoggingOpenFileForWrite(file_path_1, |
| 45 FileWriteMode::kCreateOrFail, |
| 46 FilePermissions::kWorldReadable)); |
| 47 EXPECT_NE(kInvalidFileHandle, file_handle); |
| 48 EXPECT_TRUE(FileExists(file_path_1)); |
| 49 EXPECT_EQ(0, FileSize(file_path_1)); |
| 50 |
| 51 file_handle.reset(LoggingOpenFileForWrite(file_path_1, |
| 52 FileWriteMode::kReuseOrCreate, |
| 53 FilePermissions::kWorldReadable)); |
| 54 EXPECT_NE(kInvalidFileHandle, file_handle); |
| 55 EXPECT_TRUE(FileExists(file_path_1)); |
| 56 EXPECT_EQ(0, FileSize(file_path_1)); |
| 57 |
| 58 const char data = '%'; |
| 59 EXPECT_TRUE(LoggingWriteFile(file_handle.get(), &data, sizeof(data))); |
| 60 |
| 61 // Close file_handle to ensure that the write is flushed to disk. |
| 62 file_handle.reset(); |
| 63 EXPECT_EQ(implicit_cast<FileOffset>(sizeof(data)), FileSize(file_path_1)); |
| 64 |
| 65 file_handle.reset(LoggingOpenFileForWrite(file_path_1, |
| 66 FileWriteMode::kReuseOrCreate, |
| 67 FilePermissions::kWorldReadable)); |
| 68 EXPECT_NE(kInvalidFileHandle, file_handle); |
| 69 EXPECT_TRUE(FileExists(file_path_1)); |
| 70 EXPECT_EQ(implicit_cast<FileOffset>(sizeof(data)), FileSize(file_path_1)); |
| 71 |
| 72 file_handle.reset(LoggingOpenFileForWrite(file_path_1, |
| 73 FileWriteMode::kCreateOrFail, |
| 74 FilePermissions::kWorldReadable)); |
| 75 EXPECT_EQ(kInvalidFileHandle, file_handle); |
| 76 EXPECT_TRUE(FileExists(file_path_1)); |
| 77 EXPECT_EQ(implicit_cast<FileOffset>(sizeof(data)), FileSize(file_path_1)); |
| 78 |
| 79 file_handle.reset(LoggingOpenFileForWrite(file_path_1, |
| 80 FileWriteMode::kReuseOrFail, |
| 81 FilePermissions::kWorldReadable)); |
| 82 EXPECT_NE(kInvalidFileHandle, file_handle); |
| 83 EXPECT_TRUE(FileExists(file_path_1)); |
| 84 EXPECT_EQ(implicit_cast<FileOffset>(sizeof(data)), FileSize(file_path_1)); |
| 85 |
| 86 file_handle.reset(LoggingOpenFileForWrite(file_path_1, |
| 87 FileWriteMode::kTruncateOrCreate, |
| 88 FilePermissions::kWorldReadable)); |
| 89 EXPECT_NE(kInvalidFileHandle, file_handle); |
| 90 EXPECT_TRUE(FileExists(file_path_1)); |
| 91 EXPECT_EQ(0, FileSize(file_path_1)); |
| 92 |
| 93 base::FilePath file_path_2 = |
| 94 temp_dir.path().Append(FILE_PATH_LITERAL("file_2")); |
| 95 ASSERT_FALSE(FileExists(file_path_2)); |
| 96 |
| 97 file_handle.reset(LoggingOpenFileForWrite(file_path_2, |
| 98 FileWriteMode::kTruncateOrCreate, |
| 99 FilePermissions::kWorldReadable)); |
| 100 EXPECT_NE(kInvalidFileHandle, file_handle); |
| 101 EXPECT_TRUE(FileExists(file_path_2)); |
| 102 EXPECT_EQ(0, FileSize(file_path_2)); |
| 103 |
| 104 base::FilePath file_path_3 = |
| 105 temp_dir.path().Append(FILE_PATH_LITERAL("file_3")); |
| 106 ASSERT_FALSE(FileExists(file_path_3)); |
| 107 |
| 108 file_handle.reset(LoggingOpenFileForWrite(file_path_3, |
| 109 FileWriteMode::kReuseOrCreate, |
| 110 FilePermissions::kWorldReadable)); |
| 111 EXPECT_NE(kInvalidFileHandle, file_handle); |
| 112 EXPECT_TRUE(FileExists(file_path_3)); |
| 113 EXPECT_EQ(0, FileSize(file_path_3)); |
| 114 } |
| 115 |
29 enum class ReadOrWrite : bool { | 116 enum class ReadOrWrite : bool { |
30 kRead, | 117 kRead, |
31 kWrite, | 118 kWrite, |
32 }; | 119 }; |
33 | 120 |
34 void FileShareModeTest(ReadOrWrite first, ReadOrWrite second) { | 121 void FileShareModeTest(ReadOrWrite first, ReadOrWrite second) { |
35 ScopedTempDir temp_dir; | 122 ScopedTempDir temp_dir; |
36 base::FilePath shared_file = | 123 base::FilePath shared_file = |
37 temp_dir.path().Append(FILE_PATH_LITERAL("shared_file")); | 124 temp_dir.path().Append(FILE_PATH_LITERAL("shared_file")); |
38 { | 125 { |
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
198 LockingTest(FileLocking::kExclusive, FileLocking::kShared); | 285 LockingTest(FileLocking::kExclusive, FileLocking::kShared); |
199 } | 286 } |
200 | 287 |
201 TEST(FileIO, SharedVsExclusives) { | 288 TEST(FileIO, SharedVsExclusives) { |
202 LockingTest(FileLocking::kShared, FileLocking::kExclusive); | 289 LockingTest(FileLocking::kShared, FileLocking::kExclusive); |
203 } | 290 } |
204 | 291 |
205 } // namespace | 292 } // namespace |
206 } // namespace test | 293 } // namespace test |
207 } // namespace crashpad | 294 } // namespace crashpad |
OLD | NEW |