OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/file_util.h" | 5 #include "base/file_util.h" |
| 6 #include "base/files/file.h" |
6 #include "base/files/scoped_temp_dir.h" | 7 #include "base/files/scoped_temp_dir.h" |
7 #include "base/platform_file.h" | |
8 #include "base/time/time.h" | 8 #include "base/time/time.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
10 | 10 |
| 11 using base::File; |
11 using base::FilePath; | 12 using base::FilePath; |
12 | 13 |
13 namespace { | 14 TEST(File, Create) { |
14 | |
15 // Reads from a file the given number of bytes, or until EOF is reached. | |
16 // Returns the number of bytes read. | |
17 int ReadFully(base::PlatformFile file, int64 offset, char* data, int size) { | |
18 return base::ReadPlatformFile(file, offset, data, size); | |
19 } | |
20 | |
21 // Writes the given number of bytes to a file. | |
22 // Returns the number of bytes written. | |
23 int WriteFully(base::PlatformFile file, int64 offset, | |
24 const char* data, int size) { | |
25 return base::WritePlatformFile(file, offset, data, size); | |
26 } | |
27 | |
28 } // namespace | |
29 | |
30 TEST(PlatformFile, CreatePlatformFile) { | |
31 base::ScopedTempDir temp_dir; | 15 base::ScopedTempDir temp_dir; |
32 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 16 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
33 FilePath file_path = temp_dir.path().AppendASCII("create_file_1"); | 17 FilePath file_path = temp_dir.path().AppendASCII("create_file_1"); |
34 | 18 |
35 // Open a file that doesn't exist. | 19 { |
36 base::PlatformFileError error_code = base::PLATFORM_FILE_OK; | 20 // Open a file that doesn't exist. |
37 base::PlatformFile file = base::CreatePlatformFile( | 21 File file(file_path, base::File::FLAG_OPEN | base::File::FLAG_READ); |
38 file_path, | 22 EXPECT_FALSE(file.IsValid()); |
39 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ, | 23 EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND, file.error()); |
40 NULL, | 24 } |
41 &error_code); | |
42 EXPECT_EQ(base::kInvalidPlatformFileValue, file); | |
43 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, error_code); | |
44 | 25 |
45 // Open or create a file. | 26 { |
46 bool created = false; | 27 // Open or create a file. |
47 error_code = base::PLATFORM_FILE_OK; | 28 File file(file_path, base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_READ); |
48 file = base::CreatePlatformFile( | 29 EXPECT_TRUE(file.IsValid()); |
49 file_path, | 30 EXPECT_TRUE(file.created()); |
50 base::PLATFORM_FILE_OPEN_ALWAYS | base::PLATFORM_FILE_READ, | 31 EXPECT_EQ(base::File::FILE_OK, file.error()); |
51 &created, | 32 } |
52 &error_code); | |
53 EXPECT_NE(base::kInvalidPlatformFileValue, file); | |
54 EXPECT_TRUE(created); | |
55 EXPECT_EQ(base::PLATFORM_FILE_OK, error_code); | |
56 base::ClosePlatformFile(file); | |
57 | 33 |
58 // Open an existing file. | 34 { |
59 created = false; | 35 // Open an existing file. |
60 file = base::CreatePlatformFile( | 36 File file(file_path, base::File::FLAG_OPEN | base::File::FLAG_READ); |
61 file_path, | 37 EXPECT_TRUE(file.IsValid()); |
62 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ, | 38 EXPECT_FALSE(file.created()); |
63 &created, | 39 EXPECT_EQ(base::File::FILE_OK, file.error()); |
64 &error_code); | |
65 EXPECT_NE(base::kInvalidPlatformFileValue, file); | |
66 EXPECT_FALSE(created); | |
67 EXPECT_EQ(base::PLATFORM_FILE_OK, error_code); | |
68 base::ClosePlatformFile(file); | |
69 | 40 |
70 // Create a file that exists. | 41 // This time verify closing the file. |
71 file = base::CreatePlatformFile( | 42 file.Close(); |
72 file_path, | 43 EXPECT_FALSE(file.IsValid()); |
73 base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_READ, | 44 } |
74 &created, | |
75 &error_code); | |
76 EXPECT_EQ(base::kInvalidPlatformFileValue, file); | |
77 EXPECT_FALSE(created); | |
78 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, error_code); | |
79 | 45 |
80 // Create or overwrite a file. | 46 { |
81 error_code = base::PLATFORM_FILE_OK; | 47 // Create a file that exists. |
82 file = base::CreatePlatformFile( | 48 File file(file_path, base::File::FLAG_CREATE | base::File::FLAG_READ); |
83 file_path, | 49 EXPECT_FALSE(file.IsValid()); |
84 base::PLATFORM_FILE_CREATE_ALWAYS | base::PLATFORM_FILE_READ, | 50 EXPECT_FALSE(file.created()); |
85 &created, | 51 EXPECT_EQ(base::File::FILE_ERROR_EXISTS, file.error()); |
86 &error_code); | 52 } |
87 EXPECT_NE(base::kInvalidPlatformFileValue, file); | |
88 EXPECT_TRUE(created); | |
89 EXPECT_EQ(base::PLATFORM_FILE_OK, error_code); | |
90 base::ClosePlatformFile(file); | |
91 | 53 |
92 // Create a delete-on-close file. | 54 { |
93 created = false; | 55 // Create or overwrite a file. |
94 file_path = temp_dir.path().AppendASCII("create_file_2"); | 56 File file(file_path, |
95 file = base::CreatePlatformFile( | 57 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_READ); |
96 file_path, | 58 EXPECT_TRUE(file.IsValid()); |
97 base::PLATFORM_FILE_OPEN_ALWAYS | base::PLATFORM_FILE_DELETE_ON_CLOSE | | 59 EXPECT_TRUE(file.created()); |
98 base::PLATFORM_FILE_READ, | 60 EXPECT_EQ(base::File::FILE_OK, file.error()); |
99 &created, | 61 } |
100 &error_code); | |
101 EXPECT_NE(base::kInvalidPlatformFileValue, file); | |
102 EXPECT_TRUE(created); | |
103 EXPECT_EQ(base::PLATFORM_FILE_OK, error_code); | |
104 | 62 |
105 EXPECT_TRUE(base::ClosePlatformFile(file)); | 63 { |
| 64 // Create a delete-on-close file. |
| 65 file_path = temp_dir.path().AppendASCII("create_file_2"); |
| 66 File file(file_path, |
| 67 base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_READ | |
| 68 base::File::FLAG_DELETE_ON_CLOSE); |
| 69 EXPECT_TRUE(file.IsValid()); |
| 70 EXPECT_TRUE(file.created()); |
| 71 EXPECT_EQ(base::File::FILE_OK, file.error()); |
| 72 } |
| 73 |
106 EXPECT_FALSE(base::PathExists(file_path)); | 74 EXPECT_FALSE(base::PathExists(file_path)); |
107 } | 75 } |
108 | 76 |
109 TEST(PlatformFile, DeleteOpenFile) { | 77 TEST(File, DeleteOpenFile) { |
110 base::ScopedTempDir temp_dir; | 78 base::ScopedTempDir temp_dir; |
111 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 79 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
112 FilePath file_path = temp_dir.path().AppendASCII("create_file_1"); | 80 FilePath file_path = temp_dir.path().AppendASCII("create_file_1"); |
113 | 81 |
114 // Create a file. | 82 // Create a file. |
115 bool created = false; | 83 File file(file_path, |
116 base::PlatformFileError error_code = base::PLATFORM_FILE_OK; | 84 base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_READ | |
117 base::PlatformFile file = base::CreatePlatformFile( | 85 base::File::FLAG_SHARE_DELETE); |
118 file_path, | 86 EXPECT_TRUE(file.IsValid()); |
119 base::PLATFORM_FILE_OPEN_ALWAYS | base::PLATFORM_FILE_READ | | 87 EXPECT_TRUE(file.created()); |
120 base::PLATFORM_FILE_SHARE_DELETE, | 88 EXPECT_EQ(base::File::FILE_OK, file.error()); |
121 &created, | |
122 &error_code); | |
123 EXPECT_NE(base::kInvalidPlatformFileValue, file); | |
124 EXPECT_TRUE(created); | |
125 EXPECT_EQ(base::PLATFORM_FILE_OK, error_code); | |
126 | 89 |
127 // Open an existing file and mark it as delete on close. | 90 // Open an existing file and mark it as delete on close. |
128 created = false; | 91 File same_file(file_path, |
129 base::PlatformFile same_file = base::CreatePlatformFile( | 92 base::File::FLAG_OPEN | base::File::FLAG_DELETE_ON_CLOSE | |
130 file_path, | 93 base::File::FLAG_READ); |
131 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_DELETE_ON_CLOSE | | 94 EXPECT_TRUE(file.IsValid()); |
132 base::PLATFORM_FILE_READ, | 95 EXPECT_FALSE(same_file.created()); |
133 &created, | 96 EXPECT_EQ(base::File::FILE_OK, same_file.error()); |
134 &error_code); | |
135 EXPECT_NE(base::kInvalidPlatformFileValue, file); | |
136 EXPECT_FALSE(created); | |
137 EXPECT_EQ(base::PLATFORM_FILE_OK, error_code); | |
138 | 97 |
139 // Close both handles and check that the file is gone. | 98 // Close both handles and check that the file is gone. |
140 base::ClosePlatformFile(file); | 99 file.Close(); |
141 base::ClosePlatformFile(same_file); | 100 same_file.Close(); |
142 EXPECT_FALSE(base::PathExists(file_path)); | 101 EXPECT_FALSE(base::PathExists(file_path)); |
143 } | 102 } |
144 | 103 |
145 TEST(PlatformFile, ReadWritePlatformFile) { | 104 TEST(File, ReadWrite) { |
146 base::ScopedTempDir temp_dir; | 105 base::ScopedTempDir temp_dir; |
147 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 106 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
148 FilePath file_path = temp_dir.path().AppendASCII("read_write_file"); | 107 FilePath file_path = temp_dir.path().AppendASCII("read_write_file"); |
149 base::PlatformFile file = base::CreatePlatformFile( | 108 File file(file_path, |
150 file_path, | 109 base::File::FLAG_CREATE | base::File::FLAG_READ | |
151 base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_READ | | 110 base::File::FLAG_WRITE); |
152 base::PLATFORM_FILE_WRITE, | 111 ASSERT_TRUE(file.IsValid()); |
153 NULL, | |
154 NULL); | |
155 EXPECT_NE(base::kInvalidPlatformFileValue, file); | |
156 | 112 |
157 char data_to_write[] = "test"; | 113 char data_to_write[] = "test"; |
158 const int kTestDataSize = 4; | 114 const int kTestDataSize = 4; |
159 | 115 |
160 // Write 0 bytes to the file. | 116 // Write 0 bytes to the file. |
161 int bytes_written = WriteFully(file, 0, data_to_write, 0); | 117 int bytes_written = file.Write(0, data_to_write, 0); |
162 EXPECT_EQ(0, bytes_written); | 118 EXPECT_EQ(0, bytes_written); |
163 | 119 |
164 // Write "test" to the file. | 120 // Write "test" to the file. |
165 bytes_written = WriteFully(file, 0, data_to_write, kTestDataSize); | 121 bytes_written = file.Write(0, data_to_write, kTestDataSize); |
166 EXPECT_EQ(kTestDataSize, bytes_written); | 122 EXPECT_EQ(kTestDataSize, bytes_written); |
167 | 123 |
168 // Read from EOF. | 124 // Read from EOF. |
169 char data_read_1[32]; | 125 char data_read_1[32]; |
170 int bytes_read = ReadFully(file, kTestDataSize, data_read_1, kTestDataSize); | 126 int bytes_read = file.Read(kTestDataSize, data_read_1, kTestDataSize); |
171 EXPECT_EQ(0, bytes_read); | 127 EXPECT_EQ(0, bytes_read); |
172 | 128 |
173 // Read from somewhere in the middle of the file. | 129 // Read from somewhere in the middle of the file. |
174 const int kPartialReadOffset = 1; | 130 const int kPartialReadOffset = 1; |
175 bytes_read = ReadFully(file, kPartialReadOffset, data_read_1, kTestDataSize); | 131 bytes_read = file.Read(kPartialReadOffset, data_read_1, kTestDataSize); |
176 EXPECT_EQ(kTestDataSize - kPartialReadOffset, bytes_read); | 132 EXPECT_EQ(kTestDataSize - kPartialReadOffset, bytes_read); |
177 for (int i = 0; i < bytes_read; i++) | 133 for (int i = 0; i < bytes_read; i++) |
178 EXPECT_EQ(data_to_write[i + kPartialReadOffset], data_read_1[i]); | 134 EXPECT_EQ(data_to_write[i + kPartialReadOffset], data_read_1[i]); |
179 | 135 |
180 // Read 0 bytes. | 136 // Read 0 bytes. |
181 bytes_read = ReadFully(file, 0, data_read_1, 0); | 137 bytes_read = file.Read(0, data_read_1, 0); |
182 EXPECT_EQ(0, bytes_read); | 138 EXPECT_EQ(0, bytes_read); |
183 | 139 |
184 // Read the entire file. | 140 // Read the entire file. |
185 bytes_read = ReadFully(file, 0, data_read_1, kTestDataSize); | 141 bytes_read = file.Read(0, data_read_1, kTestDataSize); |
186 EXPECT_EQ(kTestDataSize, bytes_read); | 142 EXPECT_EQ(kTestDataSize, bytes_read); |
187 for (int i = 0; i < bytes_read; i++) | 143 for (int i = 0; i < bytes_read; i++) |
188 EXPECT_EQ(data_to_write[i], data_read_1[i]); | 144 EXPECT_EQ(data_to_write[i], data_read_1[i]); |
189 | 145 |
190 // Read again, but using the trivial native wrapper. | 146 // Read again, but using the trivial native wrapper. |
191 bytes_read = base::ReadPlatformFileNoBestEffort(file, 0, data_read_1, | 147 bytes_read = file.ReadNoBestEffort(0, data_read_1, kTestDataSize); |
192 kTestDataSize); | |
193 EXPECT_LE(bytes_read, kTestDataSize); | 148 EXPECT_LE(bytes_read, kTestDataSize); |
194 for (int i = 0; i < bytes_read; i++) | 149 for (int i = 0; i < bytes_read; i++) |
195 EXPECT_EQ(data_to_write[i], data_read_1[i]); | 150 EXPECT_EQ(data_to_write[i], data_read_1[i]); |
196 | 151 |
197 // Write past the end of the file. | 152 // Write past the end of the file. |
198 const int kOffsetBeyondEndOfFile = 10; | 153 const int kOffsetBeyondEndOfFile = 10; |
199 const int kPartialWriteLength = 2; | 154 const int kPartialWriteLength = 2; |
200 bytes_written = WriteFully(file, kOffsetBeyondEndOfFile, | 155 bytes_written = file.Write(kOffsetBeyondEndOfFile, |
201 data_to_write, kPartialWriteLength); | 156 data_to_write, kPartialWriteLength); |
202 EXPECT_EQ(kPartialWriteLength, bytes_written); | 157 EXPECT_EQ(kPartialWriteLength, bytes_written); |
203 | 158 |
204 // Make sure the file was extended. | 159 // Make sure the file was extended. |
205 int64 file_size = 0; | 160 int64 file_size = 0; |
206 EXPECT_TRUE(file_util::GetFileSize(file_path, &file_size)); | 161 EXPECT_TRUE(file_util::GetFileSize(file_path, &file_size)); |
207 EXPECT_EQ(kOffsetBeyondEndOfFile + kPartialWriteLength, file_size); | 162 EXPECT_EQ(kOffsetBeyondEndOfFile + kPartialWriteLength, file_size); |
208 | 163 |
209 // Make sure the file was zero-padded. | 164 // Make sure the file was zero-padded. |
210 char data_read_2[32]; | 165 char data_read_2[32]; |
211 bytes_read = ReadFully(file, 0, data_read_2, static_cast<int>(file_size)); | 166 bytes_read = file.Read(0, data_read_2, static_cast<int>(file_size)); |
212 EXPECT_EQ(file_size, bytes_read); | 167 EXPECT_EQ(file_size, bytes_read); |
213 for (int i = 0; i < kTestDataSize; i++) | 168 for (int i = 0; i < kTestDataSize; i++) |
214 EXPECT_EQ(data_to_write[i], data_read_2[i]); | 169 EXPECT_EQ(data_to_write[i], data_read_2[i]); |
215 for (int i = kTestDataSize; i < kOffsetBeyondEndOfFile; i++) | 170 for (int i = kTestDataSize; i < kOffsetBeyondEndOfFile; i++) |
216 EXPECT_EQ(0, data_read_2[i]); | 171 EXPECT_EQ(0, data_read_2[i]); |
217 for (int i = kOffsetBeyondEndOfFile; i < file_size; i++) | 172 for (int i = kOffsetBeyondEndOfFile; i < file_size; i++) |
218 EXPECT_EQ(data_to_write[i - kOffsetBeyondEndOfFile], data_read_2[i]); | 173 EXPECT_EQ(data_to_write[i - kOffsetBeyondEndOfFile], data_read_2[i]); |
219 | |
220 // Close the file handle to allow the temp directory to be deleted. | |
221 base::ClosePlatformFile(file); | |
222 } | 174 } |
223 | 175 |
224 TEST(PlatformFile, AppendPlatformFile) { | 176 TEST(File, Append) { |
225 base::ScopedTempDir temp_dir; | 177 base::ScopedTempDir temp_dir; |
226 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 178 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
227 FilePath file_path = temp_dir.path().AppendASCII("append_file"); | 179 FilePath file_path = temp_dir.path().AppendASCII("append_file"); |
228 base::PlatformFile file = base::CreatePlatformFile( | 180 File file(file_path, base::File::FLAG_CREATE | base::File::FLAG_APPEND); |
229 file_path, | 181 ASSERT_TRUE(file.IsValid()); |
230 base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_APPEND, | |
231 NULL, | |
232 NULL); | |
233 EXPECT_NE(base::kInvalidPlatformFileValue, file); | |
234 | 182 |
235 char data_to_write[] = "test"; | 183 char data_to_write[] = "test"; |
236 const int kTestDataSize = 4; | 184 const int kTestDataSize = 4; |
237 | 185 |
238 // Write 0 bytes to the file. | 186 // Write 0 bytes to the file. |
239 int bytes_written = WriteFully(file, 0, data_to_write, 0); | 187 int bytes_written = file.Write(0, data_to_write, 0); |
240 EXPECT_EQ(0, bytes_written); | 188 EXPECT_EQ(0, bytes_written); |
241 | 189 |
242 // Write "test" to the file. | 190 // Write "test" to the file. |
243 bytes_written = WriteFully(file, 0, data_to_write, kTestDataSize); | 191 bytes_written = file.Write(0, data_to_write, kTestDataSize); |
244 EXPECT_EQ(kTestDataSize, bytes_written); | 192 EXPECT_EQ(kTestDataSize, bytes_written); |
245 | 193 |
246 base::ClosePlatformFile(file); | 194 file.Close(); |
247 file = base::CreatePlatformFile( | 195 File file2(file_path, |
248 file_path, | 196 base::File::FLAG_OPEN | base::File::FLAG_READ | |
249 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ | | 197 base::File::FLAG_APPEND); |
250 base::PLATFORM_FILE_APPEND, | 198 ASSERT_TRUE(file2.IsValid()); |
251 NULL, | 199 |
252 NULL); | 200 // Test passing the file around. |
253 EXPECT_NE(base::kInvalidPlatformFileValue, file); | 201 file = file2.Pass(); |
| 202 EXPECT_FALSE(file2.IsValid()); |
| 203 ASSERT_TRUE(file.IsValid()); |
254 | 204 |
255 char append_data_to_write[] = "78"; | 205 char append_data_to_write[] = "78"; |
256 const int kAppendDataSize = 2; | 206 const int kAppendDataSize = 2; |
257 | 207 |
258 // Append "78" to the file. | 208 // Append "78" to the file. |
259 bytes_written = WriteFully(file, 0, append_data_to_write, kAppendDataSize); | 209 bytes_written = file.Write(0, append_data_to_write, kAppendDataSize); |
260 EXPECT_EQ(kAppendDataSize, bytes_written); | 210 EXPECT_EQ(kAppendDataSize, bytes_written); |
261 | 211 |
262 // Read the entire file. | 212 // Read the entire file. |
263 char data_read_1[32]; | 213 char data_read_1[32]; |
264 int bytes_read = ReadFully(file, 0, data_read_1, | 214 int bytes_read = file.Read(0, data_read_1, |
265 kTestDataSize + kAppendDataSize); | 215 kTestDataSize + kAppendDataSize); |
266 EXPECT_EQ(kTestDataSize + kAppendDataSize, bytes_read); | 216 EXPECT_EQ(kTestDataSize + kAppendDataSize, bytes_read); |
267 for (int i = 0; i < kTestDataSize; i++) | 217 for (int i = 0; i < kTestDataSize; i++) |
268 EXPECT_EQ(data_to_write[i], data_read_1[i]); | 218 EXPECT_EQ(data_to_write[i], data_read_1[i]); |
269 for (int i = 0; i < kAppendDataSize; i++) | 219 for (int i = 0; i < kAppendDataSize; i++) |
270 EXPECT_EQ(append_data_to_write[i], data_read_1[kTestDataSize + i]); | 220 EXPECT_EQ(append_data_to_write[i], data_read_1[kTestDataSize + i]); |
271 | |
272 // Close the file handle to allow the temp directory to be deleted. | |
273 base::ClosePlatformFile(file); | |
274 } | 221 } |
275 | 222 |
276 | 223 |
277 TEST(PlatformFile, TruncatePlatformFile) { | 224 TEST(File, Truncate) { |
278 base::ScopedTempDir temp_dir; | 225 base::ScopedTempDir temp_dir; |
279 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 226 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
280 FilePath file_path = temp_dir.path().AppendASCII("truncate_file"); | 227 FilePath file_path = temp_dir.path().AppendASCII("truncate_file"); |
281 base::PlatformFile file = base::CreatePlatformFile( | 228 File file(file_path, |
282 file_path, | 229 base::File::FLAG_CREATE | base::File::FLAG_READ | |
283 base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_READ | | 230 base::File::FLAG_WRITE); |
284 base::PLATFORM_FILE_WRITE, | 231 ASSERT_TRUE(file.IsValid()); |
285 NULL, | |
286 NULL); | |
287 EXPECT_NE(base::kInvalidPlatformFileValue, file); | |
288 | 232 |
289 // Write "test" to the file. | 233 // Write "test" to the file. |
290 char data_to_write[] = "test"; | 234 char data_to_write[] = "test"; |
291 int kTestDataSize = 4; | 235 int kTestDataSize = 4; |
292 int bytes_written = WriteFully(file, 0, data_to_write, kTestDataSize); | 236 int bytes_written = file.Write(0, data_to_write, kTestDataSize); |
293 EXPECT_EQ(kTestDataSize, bytes_written); | 237 EXPECT_EQ(kTestDataSize, bytes_written); |
294 | 238 |
295 // Extend the file. | 239 // Extend the file. |
296 const int kExtendedFileLength = 10; | 240 const int kExtendedFileLength = 10; |
297 int64 file_size = 0; | 241 int64 file_size = 0; |
298 EXPECT_TRUE(base::TruncatePlatformFile(file, kExtendedFileLength)); | 242 EXPECT_TRUE(file.Truncate(kExtendedFileLength)); |
299 EXPECT_TRUE(file_util::GetFileSize(file_path, &file_size)); | 243 EXPECT_TRUE(file_util::GetFileSize(file_path, &file_size)); |
300 EXPECT_EQ(kExtendedFileLength, file_size); | 244 EXPECT_EQ(kExtendedFileLength, file_size); |
301 | 245 |
302 // Make sure the file was zero-padded. | 246 // Make sure the file was zero-padded. |
303 char data_read[32]; | 247 char data_read[32]; |
304 int bytes_read = ReadFully(file, 0, data_read, static_cast<int>(file_size)); | 248 int bytes_read = file.Read(0, data_read, static_cast<int>(file_size)); |
305 EXPECT_EQ(file_size, bytes_read); | 249 EXPECT_EQ(file_size, bytes_read); |
306 for (int i = 0; i < kTestDataSize; i++) | 250 for (int i = 0; i < kTestDataSize; i++) |
307 EXPECT_EQ(data_to_write[i], data_read[i]); | 251 EXPECT_EQ(data_to_write[i], data_read[i]); |
308 for (int i = kTestDataSize; i < file_size; i++) | 252 for (int i = kTestDataSize; i < file_size; i++) |
309 EXPECT_EQ(0, data_read[i]); | 253 EXPECT_EQ(0, data_read[i]); |
310 | 254 |
311 // Truncate the file. | 255 // Truncate the file. |
312 const int kTruncatedFileLength = 2; | 256 const int kTruncatedFileLength = 2; |
313 EXPECT_TRUE(base::TruncatePlatformFile(file, kTruncatedFileLength)); | 257 EXPECT_TRUE(file.Truncate(kTruncatedFileLength)); |
314 EXPECT_TRUE(file_util::GetFileSize(file_path, &file_size)); | 258 EXPECT_TRUE(file_util::GetFileSize(file_path, &file_size)); |
315 EXPECT_EQ(kTruncatedFileLength, file_size); | 259 EXPECT_EQ(kTruncatedFileLength, file_size); |
316 | 260 |
317 // Make sure the file was truncated. | 261 // Make sure the file was truncated. |
318 bytes_read = ReadFully(file, 0, data_read, kTestDataSize); | 262 bytes_read = file.Read(0, data_read, kTestDataSize); |
319 EXPECT_EQ(file_size, bytes_read); | 263 EXPECT_EQ(file_size, bytes_read); |
320 for (int i = 0; i < file_size; i++) | 264 for (int i = 0; i < file_size; i++) |
321 EXPECT_EQ(data_to_write[i], data_read[i]); | 265 EXPECT_EQ(data_to_write[i], data_read[i]); |
322 | |
323 // Close the file handle to allow the temp directory to be deleted. | |
324 base::ClosePlatformFile(file); | |
325 } | 266 } |
326 | 267 |
327 // Flakily fails: http://crbug.com/86494 | 268 // Flakily fails: http://crbug.com/86494 |
328 #if defined(OS_ANDROID) | 269 #if defined(OS_ANDROID) |
329 TEST(PlatformFile, TouchGetInfoPlatformFile) { | 270 TEST(File, TouchGetInfo) { |
330 #else | 271 #else |
331 TEST(PlatformFile, DISABLED_TouchGetInfoPlatformFile) { | 272 TEST(File, DISABLED_TouchGetInfo) { |
332 #endif | 273 #endif |
333 base::ScopedTempDir temp_dir; | 274 base::ScopedTempDir temp_dir; |
334 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 275 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
335 base::PlatformFile file = base::CreatePlatformFile( | 276 File file(temp_dir.path().AppendASCII("touch_get_info_file"), |
336 temp_dir.path().AppendASCII("touch_get_info_file"), | 277 base::File::FLAG_CREATE | base::File::FLAG_WRITE | |
337 base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE | | 278 base::File::FLAG_WRITE_ATTRIBUTES); |
338 base::PLATFORM_FILE_WRITE_ATTRIBUTES, | 279 ASSERT_TRUE(file.IsValid()); |
339 NULL, | |
340 NULL); | |
341 EXPECT_NE(base::kInvalidPlatformFileValue, file); | |
342 | 280 |
343 // Get info for a newly created file. | 281 // Get info for a newly created file. |
344 base::PlatformFileInfo info; | 282 base::File::Info info; |
345 EXPECT_TRUE(base::GetPlatformFileInfo(file, &info)); | 283 EXPECT_TRUE(file.GetInfo(&info)); |
346 | 284 |
347 // Add 2 seconds to account for possible rounding errors on | 285 // Add 2 seconds to account for possible rounding errors on |
348 // filesystems that use a 1s or 2s timestamp granularity. | 286 // filesystems that use a 1s or 2s timestamp granularity. |
349 base::Time now = base::Time::Now() + base::TimeDelta::FromSeconds(2); | 287 base::Time now = base::Time::Now() + base::TimeDelta::FromSeconds(2); |
350 EXPECT_EQ(0, info.size); | 288 EXPECT_EQ(0, info.size); |
351 EXPECT_FALSE(info.is_directory); | 289 EXPECT_FALSE(info.is_directory); |
352 EXPECT_FALSE(info.is_symbolic_link); | 290 EXPECT_FALSE(info.is_symbolic_link); |
353 EXPECT_LE(info.last_accessed.ToInternalValue(), now.ToInternalValue()); | 291 EXPECT_LE(info.last_accessed.ToInternalValue(), now.ToInternalValue()); |
354 EXPECT_LE(info.last_modified.ToInternalValue(), now.ToInternalValue()); | 292 EXPECT_LE(info.last_modified.ToInternalValue(), now.ToInternalValue()); |
355 EXPECT_LE(info.creation_time.ToInternalValue(), now.ToInternalValue()); | 293 EXPECT_LE(info.creation_time.ToInternalValue(), now.ToInternalValue()); |
356 base::Time creation_time = info.creation_time; | 294 base::Time creation_time = info.creation_time; |
357 | 295 |
358 // Write "test" to the file. | 296 // Write "test" to the file. |
359 char data[] = "test"; | 297 char data[] = "test"; |
360 const int kTestDataSize = 4; | 298 const int kTestDataSize = 4; |
361 int bytes_written = WriteFully(file, 0, data, kTestDataSize); | 299 int bytes_written = file.Write(0, data, kTestDataSize); |
362 EXPECT_EQ(kTestDataSize, bytes_written); | 300 EXPECT_EQ(kTestDataSize, bytes_written); |
363 | 301 |
364 // Change the last_accessed and last_modified dates. | 302 // Change the last_accessed and last_modified dates. |
365 // It's best to add values that are multiples of 2 (in seconds) | 303 // It's best to add values that are multiples of 2 (in seconds) |
366 // to the current last_accessed and last_modified times, because | 304 // to the current last_accessed and last_modified times, because |
367 // FATxx uses a 2s timestamp granularity. | 305 // FATxx uses a 2s timestamp granularity. |
368 base::Time new_last_accessed = | 306 base::Time new_last_accessed = |
369 info.last_accessed + base::TimeDelta::FromSeconds(234); | 307 info.last_accessed + base::TimeDelta::FromSeconds(234); |
370 base::Time new_last_modified = | 308 base::Time new_last_modified = |
371 info.last_modified + base::TimeDelta::FromMinutes(567); | 309 info.last_modified + base::TimeDelta::FromMinutes(567); |
372 | 310 |
373 EXPECT_TRUE(base::TouchPlatformFile(file, new_last_accessed, | 311 EXPECT_TRUE(file.SetTimes(new_last_accessed, new_last_modified)); |
374 new_last_modified)); | |
375 | 312 |
376 // Make sure the file info was updated accordingly. | 313 // Make sure the file info was updated accordingly. |
377 EXPECT_TRUE(base::GetPlatformFileInfo(file, &info)); | 314 EXPECT_TRUE(file.GetInfo(&info)); |
378 EXPECT_EQ(info.size, kTestDataSize); | 315 EXPECT_EQ(info.size, kTestDataSize); |
379 EXPECT_FALSE(info.is_directory); | 316 EXPECT_FALSE(info.is_directory); |
380 EXPECT_FALSE(info.is_symbolic_link); | 317 EXPECT_FALSE(info.is_symbolic_link); |
381 | 318 |
382 // ext2/ext3 and HPS/HPS+ seem to have a timestamp granularity of 1s. | 319 // ext2/ext3 and HPS/HPS+ seem to have a timestamp granularity of 1s. |
383 #if defined(OS_POSIX) | 320 #if defined(OS_POSIX) |
384 EXPECT_EQ(info.last_accessed.ToTimeVal().tv_sec, | 321 EXPECT_EQ(info.last_accessed.ToTimeVal().tv_sec, |
385 new_last_accessed.ToTimeVal().tv_sec); | 322 new_last_accessed.ToTimeVal().tv_sec); |
386 EXPECT_EQ(info.last_modified.ToTimeVal().tv_sec, | 323 EXPECT_EQ(info.last_modified.ToTimeVal().tv_sec, |
387 new_last_modified.ToTimeVal().tv_sec); | 324 new_last_modified.ToTimeVal().tv_sec); |
388 #else | 325 #else |
389 EXPECT_EQ(info.last_accessed.ToInternalValue(), | 326 EXPECT_EQ(info.last_accessed.ToInternalValue(), |
390 new_last_accessed.ToInternalValue()); | 327 new_last_accessed.ToInternalValue()); |
391 EXPECT_EQ(info.last_modified.ToInternalValue(), | 328 EXPECT_EQ(info.last_modified.ToInternalValue(), |
392 new_last_modified.ToInternalValue()); | 329 new_last_modified.ToInternalValue()); |
393 #endif | 330 #endif |
394 | 331 |
395 EXPECT_EQ(info.creation_time.ToInternalValue(), | 332 EXPECT_EQ(info.creation_time.ToInternalValue(), |
396 creation_time.ToInternalValue()); | 333 creation_time.ToInternalValue()); |
397 | |
398 // Close the file handle to allow the temp directory to be deleted. | |
399 base::ClosePlatformFile(file); | |
400 } | 334 } |
401 | 335 |
402 TEST(PlatformFile, ReadFileAtCurrentPosition) { | 336 TEST(File, ReadFileAtCurrentPosition) { |
403 base::ScopedTempDir temp_dir; | 337 base::ScopedTempDir temp_dir; |
404 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 338 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
405 FilePath file_path = | 339 FilePath file_path = |
406 temp_dir.path().AppendASCII("read_file_at_current_position"); | 340 temp_dir.path().AppendASCII("read_file_at_current_position"); |
407 base::PlatformFile file = base::CreatePlatformFile( | 341 File file(file_path, |
408 file_path, | 342 base::File::FLAG_CREATE | base::File::FLAG_READ | |
409 base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_READ | | 343 base::File::FLAG_WRITE); |
410 base::PLATFORM_FILE_WRITE, | 344 EXPECT_TRUE(file.IsValid()); |
411 NULL, NULL); | |
412 EXPECT_NE(base::kInvalidPlatformFileValue, file); | |
413 | 345 |
414 const char kData[] = "test"; | 346 const char kData[] = "test"; |
415 const int kDataSize = arraysize(kData) - 1; | 347 const int kDataSize = arraysize(kData) - 1; |
416 EXPECT_EQ(kDataSize, WriteFully(file, 0, kData, kDataSize)); | 348 EXPECT_EQ(kDataSize, file.Write(0, kData, kDataSize)); |
417 | 349 |
418 EXPECT_EQ(0, SeekPlatformFile( | 350 EXPECT_EQ(0, file.Seek(base::File::FROM_BEGIN, 0)); |
419 file, base::PLATFORM_FILE_FROM_BEGIN, 0)); | |
420 | 351 |
421 char buffer[kDataSize]; | 352 char buffer[kDataSize]; |
422 int first_chunk_size = kDataSize / 2; | 353 int first_chunk_size = kDataSize / 2; |
423 EXPECT_EQ(first_chunk_size, | 354 EXPECT_EQ(first_chunk_size, file.ReadAtCurrentPos(buffer, first_chunk_size)); |
424 base::ReadPlatformFileAtCurrentPos( | |
425 file, buffer, first_chunk_size)); | |
426 EXPECT_EQ(kDataSize - first_chunk_size, | 355 EXPECT_EQ(kDataSize - first_chunk_size, |
427 base::ReadPlatformFileAtCurrentPos( | 356 file.ReadAtCurrentPos(buffer + first_chunk_size, |
428 file, buffer + first_chunk_size, | 357 kDataSize - first_chunk_size)); |
429 kDataSize - first_chunk_size)); | |
430 EXPECT_EQ(std::string(buffer, buffer + kDataSize), | 358 EXPECT_EQ(std::string(buffer, buffer + kDataSize), |
431 std::string(kData)); | 359 std::string(kData)); |
432 | |
433 base::ClosePlatformFile(file); | |
434 } | 360 } |
OLD | NEW |