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