blob: 671deb03ce6569b486a7074c81752af5dc263357 [file] [log] [blame]
evan@chromium.org3c0bc352012-02-14 09:29:14 +09001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
dumi@chromium.org23915982010-09-10 12:01:14 +09002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "base/file_util.h"
6#include "base/platform_file.h"
thakis@chromium.org927d7282011-05-20 08:34:17 +09007#include "base/scoped_temp_dir.h"
dumi@chromium.org23915982010-09-10 12:01:14 +09008#include "base/time.h"
9#include "testing/gtest/include/gtest/gtest.h"
10
11namespace {
12
13// Reads from a file the given number of bytes, or until EOF is reached.
14// Returns the number of bytes read.
15int ReadFully(base::PlatformFile file, int64 offset, char* data, int size) {
rvargas@google.com1fe806d2011-09-02 08:04:07 +090016 return base::ReadPlatformFile(file, offset, data, size);
dumi@chromium.org23915982010-09-10 12:01:14 +090017}
18
19// Writes the given number of bytes to a file.
20// Returns the number of bytes written.
21int WriteFully(base::PlatformFile file, int64 offset,
22 const char* data, int size) {
rvargas@google.com8f08f2b2011-08-27 07:17:19 +090023 return base::WritePlatformFile(file, offset, data, size);
dumi@chromium.org23915982010-09-10 12:01:14 +090024}
25
26} // namespace
27
28TEST(PlatformFile, CreatePlatformFile) {
29 ScopedTempDir temp_dir;
30 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
31 FilePath file_path = temp_dir.path().AppendASCII("create_file_1");
32
33 // Open a file that doesn't exist.
34 base::PlatformFileError error_code = base::PLATFORM_FILE_OK;
35 base::PlatformFile file = base::CreatePlatformFile(
36 file_path, base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ,
37 NULL, &error_code);
38 EXPECT_EQ(base::kInvalidPlatformFileValue, file);
39 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, error_code);
40
41 // Open or create a file.
42 bool created = false;
43 error_code = base::PLATFORM_FILE_OK;
44 file = base::CreatePlatformFile(
45 file_path, base::PLATFORM_FILE_OPEN_ALWAYS | base::PLATFORM_FILE_READ,
46 &created, &error_code);
47 EXPECT_NE(base::kInvalidPlatformFileValue, file);
48 EXPECT_TRUE(created);
49 EXPECT_EQ(base::PLATFORM_FILE_OK, error_code);
50 base::ClosePlatformFile(file);
51
52 // Open an existing file.
53 created = false;
54 file = base::CreatePlatformFile(
55 file_path, base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ,
56 &created, &error_code);
57 EXPECT_NE(base::kInvalidPlatformFileValue, file);
58 EXPECT_FALSE(created);
59 EXPECT_EQ(base::PLATFORM_FILE_OK, error_code);
60 base::ClosePlatformFile(file);
61
62 // Create a file that exists.
63 file = base::CreatePlatformFile(
64 file_path, base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_READ,
65 &created, &error_code);
66 EXPECT_EQ(base::kInvalidPlatformFileValue, file);
67 EXPECT_FALSE(created);
68 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, error_code);
69
70 // Create or overwrite a file.
71 error_code = base::PLATFORM_FILE_OK;
72 file = base::CreatePlatformFile(
73 file_path, base::PLATFORM_FILE_CREATE_ALWAYS | base::PLATFORM_FILE_READ,
74 &created, &error_code);
75 EXPECT_NE(base::kInvalidPlatformFileValue, file);
76 EXPECT_TRUE(created);
77 EXPECT_EQ(base::PLATFORM_FILE_OK, error_code);
78 base::ClosePlatformFile(file);
79
80 // Create a delete-on-close file.
81 created = false;
82 file_path = temp_dir.path().AppendASCII("create_file_2");
83 file = base::CreatePlatformFile(
84 file_path,
85 base::PLATFORM_FILE_OPEN_ALWAYS |
86 base::PLATFORM_FILE_DELETE_ON_CLOSE |
87 base::PLATFORM_FILE_READ,
88 &created, &error_code);
89 EXPECT_NE(base::kInvalidPlatformFileValue, file);
90 EXPECT_TRUE(created);
91 EXPECT_EQ(base::PLATFORM_FILE_OK, error_code);
92
93 EXPECT_TRUE(base::ClosePlatformFile(file));
94 EXPECT_FALSE(file_util::PathExists(file_path));
95}
96
jochen@chromium.org35480452011-06-23 04:29:23 +090097TEST(PlatformFile, DeleteOpenFile) {
98 ScopedTempDir temp_dir;
99 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
100 FilePath file_path = temp_dir.path().AppendASCII("create_file_1");
101
102 // Create a file.
103 bool created = false;
104 base::PlatformFileError error_code = base::PLATFORM_FILE_OK;
105 base::PlatformFile file = base::CreatePlatformFile(
106 file_path,
107 base::PLATFORM_FILE_OPEN_ALWAYS |
108 base::PLATFORM_FILE_READ |
109 base::PLATFORM_FILE_SHARE_DELETE,
110 &created, &error_code);
111 EXPECT_NE(base::kInvalidPlatformFileValue, file);
112 EXPECT_TRUE(created);
113 EXPECT_EQ(base::PLATFORM_FILE_OK, error_code);
114
115 // Open an existing file and mark it as delete on close.
116 created = false;
117 base::PlatformFile same_file = base::CreatePlatformFile(
118 file_path,
119 base::PLATFORM_FILE_OPEN |
120 base::PLATFORM_FILE_DELETE_ON_CLOSE |
121 base::PLATFORM_FILE_READ,
122 &created, &error_code);
123 EXPECT_NE(base::kInvalidPlatformFileValue, file);
124 EXPECT_FALSE(created);
125 EXPECT_EQ(base::PLATFORM_FILE_OK, error_code);
126
127 // Close both handles and check that the file is gone.
128 base::ClosePlatformFile(file);
129 base::ClosePlatformFile(same_file);
130 EXPECT_FALSE(file_util::PathExists(file_path));
131}
132
dumi@chromium.org23915982010-09-10 12:01:14 +0900133TEST(PlatformFile, ReadWritePlatformFile) {
134 ScopedTempDir temp_dir;
135 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
136 FilePath file_path = temp_dir.path().AppendASCII("read_write_file");
137 base::PlatformFile file = base::CreatePlatformFile(
138 file_path,
139 base::PLATFORM_FILE_CREATE |
140 base::PLATFORM_FILE_READ |
141 base::PLATFORM_FILE_WRITE,
142 NULL, NULL);
143 EXPECT_NE(base::kInvalidPlatformFileValue, file);
144
145 char data_to_write[] = "test";
146 const int kTestDataSize = 4;
147
148 // Write 0 bytes to the file.
149 int bytes_written = WriteFully(file, 0, data_to_write, 0);
150 EXPECT_EQ(0, bytes_written);
151
152 // Write "test" to the file.
153 bytes_written = WriteFully(file, 0, data_to_write, kTestDataSize);
154 EXPECT_EQ(kTestDataSize, bytes_written);
155
156 // Read from EOF.
157 char data_read_1[32];
158 int bytes_read = ReadFully(file, kTestDataSize, data_read_1, kTestDataSize);
159 EXPECT_EQ(0, bytes_read);
160
161 // Read from somewhere in the middle of the file.
162 const int kPartialReadOffset = 1;
163 bytes_read = ReadFully(file, kPartialReadOffset, data_read_1, kTestDataSize);
164 EXPECT_EQ(kTestDataSize - kPartialReadOffset, bytes_read);
165 for (int i = 0; i < bytes_read; i++)
166 EXPECT_EQ(data_to_write[i + kPartialReadOffset], data_read_1[i]);
167
168 // Read 0 bytes.
169 bytes_read = ReadFully(file, 0, data_read_1, 0);
170 EXPECT_EQ(0, bytes_read);
171
172 // Read the entire file.
173 bytes_read = ReadFully(file, 0, data_read_1, kTestDataSize);
174 EXPECT_EQ(kTestDataSize, bytes_read);
175 for (int i = 0; i < bytes_read; i++)
176 EXPECT_EQ(data_to_write[i], data_read_1[i]);
177
rvargas@google.com1fe806d2011-09-02 08:04:07 +0900178 // Read again, but using the trivial native wrapper.
179 bytes_read = base::ReadPlatformFileNoBestEffort(file, 0, data_read_1,
180 kTestDataSize);
181 EXPECT_LE(bytes_read, kTestDataSize);
182 for (int i = 0; i < bytes_read; i++)
183 EXPECT_EQ(data_to_write[i], data_read_1[i]);
184
dumi@chromium.org23915982010-09-10 12:01:14 +0900185 // Write past the end of the file.
186 const int kOffsetBeyondEndOfFile = 10;
187 const int kPartialWriteLength = 2;
188 bytes_written = WriteFully(file, kOffsetBeyondEndOfFile,
189 data_to_write, kPartialWriteLength);
190 EXPECT_EQ(kPartialWriteLength, bytes_written);
191
192 // Make sure the file was extended.
193 int64 file_size = 0;
194 EXPECT_TRUE(file_util::GetFileSize(file_path, &file_size));
195 EXPECT_EQ(kOffsetBeyondEndOfFile + kPartialWriteLength, file_size);
196
197 // Make sure the file was zero-padded.
198 char data_read_2[32];
199 bytes_read = ReadFully(file, 0, data_read_2, static_cast<int>(file_size));
200 EXPECT_EQ(file_size, bytes_read);
201 for (int i = 0; i < kTestDataSize; i++)
202 EXPECT_EQ(data_to_write[i], data_read_2[i]);
203 for (int i = kTestDataSize; i < kOffsetBeyondEndOfFile; i++)
204 EXPECT_EQ(0, data_read_2[i]);
205 for (int i = kOffsetBeyondEndOfFile; i < file_size; i++)
206 EXPECT_EQ(data_to_write[i - kOffsetBeyondEndOfFile], data_read_2[i]);
207
208 // Close the file handle to allow the temp directory to be deleted.
209 base::ClosePlatformFile(file);
210}
211
212TEST(PlatformFile, TruncatePlatformFile) {
213 ScopedTempDir temp_dir;
214 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
215 FilePath file_path = temp_dir.path().AppendASCII("truncate_file");
216 base::PlatformFile file = base::CreatePlatformFile(
217 file_path,
218 base::PLATFORM_FILE_CREATE |
219 base::PLATFORM_FILE_READ |
220 base::PLATFORM_FILE_WRITE,
221 NULL, NULL);
222 EXPECT_NE(base::kInvalidPlatformFileValue, file);
223
224 // Write "test" to the file.
225 char data_to_write[] = "test";
226 int kTestDataSize = 4;
227 int bytes_written = WriteFully(file, 0, data_to_write, kTestDataSize);
228 EXPECT_EQ(kTestDataSize, bytes_written);
229
230 // Extend the file.
231 const int kExtendedFileLength = 10;
232 int64 file_size = 0;
233 EXPECT_TRUE(base::TruncatePlatformFile(file, kExtendedFileLength));
234 EXPECT_TRUE(file_util::GetFileSize(file_path, &file_size));
235 EXPECT_EQ(kExtendedFileLength, file_size);
236
237 // Make sure the file was zero-padded.
238 char data_read[32];
239 int bytes_read = ReadFully(file, 0, data_read, static_cast<int>(file_size));
240 EXPECT_EQ(file_size, bytes_read);
241 for (int i = 0; i < kTestDataSize; i++)
242 EXPECT_EQ(data_to_write[i], data_read[i]);
243 for (int i = kTestDataSize; i < file_size; i++)
244 EXPECT_EQ(0, data_read[i]);
245
246 // Truncate the file.
247 const int kTruncatedFileLength = 2;
248 EXPECT_TRUE(base::TruncatePlatformFile(file, kTruncatedFileLength));
249 EXPECT_TRUE(file_util::GetFileSize(file_path, &file_size));
250 EXPECT_EQ(kTruncatedFileLength, file_size);
251
252 // Make sure the file was truncated.
253 bytes_read = ReadFully(file, 0, data_read, kTestDataSize);
254 EXPECT_EQ(file_size, bytes_read);
255 for (int i = 0; i < file_size; i++)
256 EXPECT_EQ(data_to_write[i], data_read[i]);
257
258 // Close the file handle to allow the temp directory to be deleted.
259 base::ClosePlatformFile(file);
260}
261
leandrogracia@chromium.orga273ff12011-06-17 23:16:47 +0900262// Flakily fails: http://crbug.com/86494
evan@chromium.org3c0bc352012-02-14 09:29:14 +0900263TEST(PlatformFile, DISABLED_TouchGetInfoPlatformFile) {
dumi@chromium.org23915982010-09-10 12:01:14 +0900264 ScopedTempDir temp_dir;
265 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
266 base::PlatformFile file = base::CreatePlatformFile(
267 temp_dir.path().AppendASCII("touch_get_info_file"),
268 base::PLATFORM_FILE_CREATE |
269 base::PLATFORM_FILE_WRITE |
270 base::PLATFORM_FILE_WRITE_ATTRIBUTES,
271 NULL, NULL);
272 EXPECT_NE(base::kInvalidPlatformFileValue, file);
273
274 // Get info for a newly created file.
275 base::PlatformFileInfo info;
276 EXPECT_TRUE(base::GetPlatformFileInfo(file, &info));
277
278 // Add 2 seconds to account for possible rounding errors on
279 // filesystems that use a 1s or 2s timestamp granularity.
280 base::Time now = base::Time::Now() + base::TimeDelta::FromSeconds(2);
281 EXPECT_EQ(0, info.size);
282 EXPECT_FALSE(info.is_directory);
gspencer@chromium.org4dcc02c2010-11-30 09:43:37 +0900283 EXPECT_FALSE(info.is_symbolic_link);
dumi@chromium.org5b433622010-09-16 04:27:13 +0900284 EXPECT_LE(info.last_accessed.ToInternalValue(), now.ToInternalValue());
285 EXPECT_LE(info.last_modified.ToInternalValue(), now.ToInternalValue());
286 EXPECT_LE(info.creation_time.ToInternalValue(), now.ToInternalValue());
dumi@chromium.org23915982010-09-10 12:01:14 +0900287 base::Time creation_time = info.creation_time;
288
289 // Write "test" to the file.
290 char data[] = "test";
291 const int kTestDataSize = 4;
292 int bytes_written = WriteFully(file, 0, data, kTestDataSize);
293 EXPECT_EQ(kTestDataSize, bytes_written);
294
295 // Change the last_accessed and last_modified dates.
296 // It's best to add values that are multiples of 2 (in seconds)
297 // to the current last_accessed and last_modified times, because
298 // FATxx uses a 2s timestamp granularity.
299 base::Time new_last_accessed =
300 info.last_accessed + base::TimeDelta::FromSeconds(234);
301 base::Time new_last_modified =
302 info.last_modified + base::TimeDelta::FromMinutes(567);
303
304 EXPECT_TRUE(base::TouchPlatformFile(file, new_last_accessed,
305 new_last_modified));
306
307 // Make sure the file info was updated accordingly.
308 EXPECT_TRUE(base::GetPlatformFileInfo(file, &info));
309 EXPECT_EQ(info.size, kTestDataSize);
310 EXPECT_FALSE(info.is_directory);
gspencer@chromium.org4dcc02c2010-11-30 09:43:37 +0900311 EXPECT_FALSE(info.is_symbolic_link);
dumi@chromium.org23915982010-09-10 12:01:14 +0900312
313 // ext2/ext3 and HPS/HPS+ seem to have a timestamp granularity of 1s.
314#if defined(OS_POSIX)
315 EXPECT_EQ(info.last_accessed.ToTimeVal().tv_sec,
316 new_last_accessed.ToTimeVal().tv_sec);
317 EXPECT_EQ(info.last_modified.ToTimeVal().tv_sec,
318 new_last_modified.ToTimeVal().tv_sec);
319#else
dumi@chromium.org5b433622010-09-16 04:27:13 +0900320 EXPECT_EQ(info.last_accessed.ToInternalValue(),
321 new_last_accessed.ToInternalValue());
322 EXPECT_EQ(info.last_modified.ToInternalValue(),
323 new_last_modified.ToInternalValue());
dumi@chromium.org23915982010-09-10 12:01:14 +0900324#endif
325
dumi@chromium.org5b433622010-09-16 04:27:13 +0900326 EXPECT_EQ(info.creation_time.ToInternalValue(),
327 creation_time.ToInternalValue());
dumi@chromium.org23915982010-09-10 12:01:14 +0900328
329 // Close the file handle to allow the temp directory to be deleted.
330 base::ClosePlatformFile(file);
331}