Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(15)

Side by Side Diff: util/file/file_io_test.cc

Issue 1395543002: Add non-logging OpenFileForWrite() and OpenFileForReadAndWrite() (Closed) Base URL: https://chromium.googlesource.com/crashpad/crashpad@master
Patch Set: Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « util/file/file_io_posix.cc ('k') | util/file/file_io_win.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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,
(...skipping 10 matching lines...) Expand all
21 #include "test/errors.h" 21 #include "test/errors.h"
22 #include "test/file.h" 22 #include "test/file.h"
23 #include "test/scoped_temp_dir.h" 23 #include "test/scoped_temp_dir.h"
24 #include "util/misc/implicit_cast.h" 24 #include "util/misc/implicit_cast.h"
25 #include "util/thread/thread.h" 25 #include "util/thread/thread.h"
26 26
27 namespace crashpad { 27 namespace crashpad {
28 namespace test { 28 namespace test {
29 namespace { 29 namespace {
30 30
31 TEST(FileIO, OpenFileForWrite) { 31 void TestOpenFileForWrite(FileHandle (*opener)(const base::FilePath&,
32 FileWriteMode,
33 FilePermissions)) {
32 ScopedTempDir temp_dir; 34 ScopedTempDir temp_dir;
33 base::FilePath file_path_1 = 35 base::FilePath file_path_1 =
34 temp_dir.path().Append(FILE_PATH_LITERAL("file_1")); 36 temp_dir.path().Append(FILE_PATH_LITERAL("file_1"));
35 ASSERT_FALSE(FileExists(file_path_1)); 37 ASSERT_FALSE(FileExists(file_path_1));
36 38
37 ScopedFileHandle 39 ScopedFileHandle file_handle(opener(file_path_1,
38 file_handle(LoggingOpenFileForWrite(file_path_1, 40 FileWriteMode::kReuseOrFail,
39 FileWriteMode::kReuseOrFail, 41 FilePermissions::kWorldReadable));
40 FilePermissions::kWorldReadable));
41 EXPECT_EQ(kInvalidFileHandle, file_handle); 42 EXPECT_EQ(kInvalidFileHandle, file_handle);
42 EXPECT_FALSE(FileExists(file_path_1)); 43 EXPECT_FALSE(FileExists(file_path_1));
43 44
44 file_handle.reset(LoggingOpenFileForWrite(file_path_1, 45 file_handle.reset(opener(file_path_1,
45 FileWriteMode::kCreateOrFail, 46 FileWriteMode::kCreateOrFail,
46 FilePermissions::kWorldReadable)); 47 FilePermissions::kWorldReadable));
47 EXPECT_NE(kInvalidFileHandle, file_handle); 48 EXPECT_NE(kInvalidFileHandle, file_handle);
48 EXPECT_TRUE(FileExists(file_path_1)); 49 EXPECT_TRUE(FileExists(file_path_1));
49 EXPECT_EQ(0, FileSize(file_path_1)); 50 EXPECT_EQ(0, FileSize(file_path_1));
50 51
51 file_handle.reset(LoggingOpenFileForWrite(file_path_1, 52 file_handle.reset(opener(file_path_1,
52 FileWriteMode::kReuseOrCreate, 53 FileWriteMode::kReuseOrCreate,
53 FilePermissions::kWorldReadable)); 54 FilePermissions::kWorldReadable));
54 EXPECT_NE(kInvalidFileHandle, file_handle); 55 EXPECT_NE(kInvalidFileHandle, file_handle);
55 EXPECT_TRUE(FileExists(file_path_1)); 56 EXPECT_TRUE(FileExists(file_path_1));
56 EXPECT_EQ(0, FileSize(file_path_1)); 57 EXPECT_EQ(0, FileSize(file_path_1));
57 58
58 const char data = '%'; 59 const char data = '%';
59 EXPECT_TRUE(LoggingWriteFile(file_handle.get(), &data, sizeof(data))); 60 EXPECT_TRUE(LoggingWriteFile(file_handle.get(), &data, sizeof(data)));
60 61
61 // Close file_handle to ensure that the write is flushed to disk. 62 // Close file_handle to ensure that the write is flushed to disk.
62 file_handle.reset(); 63 file_handle.reset();
63 EXPECT_EQ(implicit_cast<FileOffset>(sizeof(data)), FileSize(file_path_1)); 64 EXPECT_EQ(implicit_cast<FileOffset>(sizeof(data)), FileSize(file_path_1));
64 65
65 file_handle.reset(LoggingOpenFileForWrite(file_path_1, 66 file_handle.reset(opener(file_path_1,
66 FileWriteMode::kReuseOrCreate, 67 FileWriteMode::kReuseOrCreate,
67 FilePermissions::kWorldReadable)); 68 FilePermissions::kWorldReadable));
68 EXPECT_NE(kInvalidFileHandle, file_handle); 69 EXPECT_NE(kInvalidFileHandle, file_handle);
69 EXPECT_TRUE(FileExists(file_path_1)); 70 EXPECT_TRUE(FileExists(file_path_1));
70 EXPECT_EQ(implicit_cast<FileOffset>(sizeof(data)), FileSize(file_path_1)); 71 EXPECT_EQ(implicit_cast<FileOffset>(sizeof(data)), FileSize(file_path_1));
71 72
72 file_handle.reset(LoggingOpenFileForWrite(file_path_1, 73 file_handle.reset(opener(file_path_1,
73 FileWriteMode::kCreateOrFail, 74 FileWriteMode::kCreateOrFail,
74 FilePermissions::kWorldReadable)); 75 FilePermissions::kWorldReadable));
75 EXPECT_EQ(kInvalidFileHandle, file_handle); 76 EXPECT_EQ(kInvalidFileHandle, file_handle);
76 EXPECT_TRUE(FileExists(file_path_1)); 77 EXPECT_TRUE(FileExists(file_path_1));
77 EXPECT_EQ(implicit_cast<FileOffset>(sizeof(data)), FileSize(file_path_1)); 78 EXPECT_EQ(implicit_cast<FileOffset>(sizeof(data)), FileSize(file_path_1));
78 79
79 file_handle.reset(LoggingOpenFileForWrite(file_path_1, 80 file_handle.reset(opener(file_path_1,
80 FileWriteMode::kReuseOrFail, 81 FileWriteMode::kReuseOrFail,
81 FilePermissions::kWorldReadable)); 82 FilePermissions::kWorldReadable));
82 EXPECT_NE(kInvalidFileHandle, file_handle); 83 EXPECT_NE(kInvalidFileHandle, file_handle);
83 EXPECT_TRUE(FileExists(file_path_1)); 84 EXPECT_TRUE(FileExists(file_path_1));
84 EXPECT_EQ(implicit_cast<FileOffset>(sizeof(data)), FileSize(file_path_1)); 85 EXPECT_EQ(implicit_cast<FileOffset>(sizeof(data)), FileSize(file_path_1));
85 86
86 file_handle.reset(LoggingOpenFileForWrite(file_path_1, 87 file_handle.reset(opener(file_path_1,
87 FileWriteMode::kTruncateOrCreate, 88 FileWriteMode::kTruncateOrCreate,
88 FilePermissions::kWorldReadable)); 89 FilePermissions::kWorldReadable));
89 EXPECT_NE(kInvalidFileHandle, file_handle); 90 EXPECT_NE(kInvalidFileHandle, file_handle);
90 EXPECT_TRUE(FileExists(file_path_1)); 91 EXPECT_TRUE(FileExists(file_path_1));
91 EXPECT_EQ(0, FileSize(file_path_1)); 92 EXPECT_EQ(0, FileSize(file_path_1));
92 93
93 base::FilePath file_path_2 = 94 base::FilePath file_path_2 =
94 temp_dir.path().Append(FILE_PATH_LITERAL("file_2")); 95 temp_dir.path().Append(FILE_PATH_LITERAL("file_2"));
95 ASSERT_FALSE(FileExists(file_path_2)); 96 ASSERT_FALSE(FileExists(file_path_2));
96 97
97 file_handle.reset(LoggingOpenFileForWrite(file_path_2, 98 file_handle.reset(opener(file_path_2,
98 FileWriteMode::kTruncateOrCreate, 99 FileWriteMode::kTruncateOrCreate,
99 FilePermissions::kWorldReadable)); 100 FilePermissions::kWorldReadable));
100 EXPECT_NE(kInvalidFileHandle, file_handle); 101 EXPECT_NE(kInvalidFileHandle, file_handle);
101 EXPECT_TRUE(FileExists(file_path_2)); 102 EXPECT_TRUE(FileExists(file_path_2));
102 EXPECT_EQ(0, FileSize(file_path_2)); 103 EXPECT_EQ(0, FileSize(file_path_2));
103 104
104 base::FilePath file_path_3 = 105 base::FilePath file_path_3 =
105 temp_dir.path().Append(FILE_PATH_LITERAL("file_3")); 106 temp_dir.path().Append(FILE_PATH_LITERAL("file_3"));
106 ASSERT_FALSE(FileExists(file_path_3)); 107 ASSERT_FALSE(FileExists(file_path_3));
107 108
108 file_handle.reset(LoggingOpenFileForWrite(file_path_3, 109 file_handle.reset(opener(file_path_3,
109 FileWriteMode::kReuseOrCreate, 110 FileWriteMode::kReuseOrCreate,
110 FilePermissions::kWorldReadable)); 111 FilePermissions::kWorldReadable));
111 EXPECT_NE(kInvalidFileHandle, file_handle); 112 EXPECT_NE(kInvalidFileHandle, file_handle);
112 EXPECT_TRUE(FileExists(file_path_3)); 113 EXPECT_TRUE(FileExists(file_path_3));
113 EXPECT_EQ(0, FileSize(file_path_3)); 114 EXPECT_EQ(0, FileSize(file_path_3));
114 } 115 }
115 116
117 TEST(FileIO, OpenFileForWrite) {
118 TestOpenFileForWrite(OpenFileForWrite);
119 }
120
121 TEST(FileIO, OpenFileForReadAndWrite) {
122 TestOpenFileForWrite(OpenFileForReadAndWrite);
123 }
124
125 TEST(FileIO, LoggingOpenFileForWrite) {
126 TestOpenFileForWrite(LoggingOpenFileForWrite);
127 }
128
129 TEST(FileIO, LoggingOpenFileForReadAndWrite) {
130 TestOpenFileForWrite(LoggingOpenFileForReadAndWrite);
131 }
132
116 enum class ReadOrWrite : bool { 133 enum class ReadOrWrite : bool {
117 kRead, 134 kRead,
118 kWrite, 135 kWrite,
119 }; 136 };
120 137
121 void FileShareModeTest(ReadOrWrite first, ReadOrWrite second) { 138 void FileShareModeTest(ReadOrWrite first, ReadOrWrite second) {
122 ScopedTempDir temp_dir; 139 ScopedTempDir temp_dir;
123 base::FilePath shared_file = 140 base::FilePath shared_file =
124 temp_dir.path().Append(FILE_PATH_LITERAL("shared_file")); 141 temp_dir.path().Append(FILE_PATH_LITERAL("shared_file"));
125 { 142 {
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after
285 LockingTest(FileLocking::kExclusive, FileLocking::kShared); 302 LockingTest(FileLocking::kExclusive, FileLocking::kShared);
286 } 303 }
287 304
288 TEST(FileIO, SharedVsExclusives) { 305 TEST(FileIO, SharedVsExclusives) {
289 LockingTest(FileLocking::kShared, FileLocking::kExclusive); 306 LockingTest(FileLocking::kShared, FileLocking::kExclusive);
290 } 307 }
291 308
292 } // namespace 309 } // namespace
293 } // namespace test 310 } // namespace test
294 } // namespace crashpad 311 } // namespace crashpad
OLDNEW
« no previous file with comments | « util/file/file_io_posix.cc ('k') | util/file/file_io_win.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698