Encode method and error message inside LevelDB::Status message
[chromium-blink-merge.git] / base / platform_file_unittest.cc
blob748387610f0436ca3ee90147e81dd3cc2482e114
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.h"
9 #include "testing/gtest/include/gtest/gtest.h"
11 using base::FilePath;
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(base::PlatformFile file, int64 offset, char* data, int size) {
18 return base::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(base::PlatformFile file, int64 offset,
24 const char* data, int size) {
25 return base::WritePlatformFile(file, offset, data, size);
28 } // namespace
30 TEST(PlatformFile, CreatePlatformFile) {
31 base::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 base::PlatformFileError error_code = base::PLATFORM_FILE_OK;
37 base::PlatformFile file = base::CreatePlatformFile(
38 file_path, base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ,
39 NULL, &error_code);
40 EXPECT_EQ(base::kInvalidPlatformFileValue, file);
41 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, error_code);
43 // Open or create a file.
44 bool created = false;
45 error_code = base::PLATFORM_FILE_OK;
46 file = base::CreatePlatformFile(
47 file_path, base::PLATFORM_FILE_OPEN_ALWAYS | base::PLATFORM_FILE_READ,
48 &created, &error_code);
49 EXPECT_NE(base::kInvalidPlatformFileValue, file);
50 EXPECT_TRUE(created);
51 EXPECT_EQ(base::PLATFORM_FILE_OK, error_code);
52 base::ClosePlatformFile(file);
54 // Open an existing file.
55 created = false;
56 file = base::CreatePlatformFile(
57 file_path, base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ,
58 &created, &error_code);
59 EXPECT_NE(base::kInvalidPlatformFileValue, file);
60 EXPECT_FALSE(created);
61 EXPECT_EQ(base::PLATFORM_FILE_OK, error_code);
62 base::ClosePlatformFile(file);
64 // Create a file that exists.
65 file = base::CreatePlatformFile(
66 file_path, base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_READ,
67 &created, &error_code);
68 EXPECT_EQ(base::kInvalidPlatformFileValue, file);
69 EXPECT_FALSE(created);
70 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, error_code);
72 // Create or overwrite a file.
73 error_code = base::PLATFORM_FILE_OK;
74 file = base::CreatePlatformFile(
75 file_path, base::PLATFORM_FILE_CREATE_ALWAYS | base::PLATFORM_FILE_READ,
76 &created, &error_code);
77 EXPECT_NE(base::kInvalidPlatformFileValue, file);
78 EXPECT_TRUE(created);
79 EXPECT_EQ(base::PLATFORM_FILE_OK, error_code);
80 base::ClosePlatformFile(file);
82 // Create a delete-on-close file.
83 created = false;
84 file_path = temp_dir.path().AppendASCII("create_file_2");
85 file = base::CreatePlatformFile(
86 file_path,
87 base::PLATFORM_FILE_OPEN_ALWAYS |
88 base::PLATFORM_FILE_DELETE_ON_CLOSE |
89 base::PLATFORM_FILE_READ,
90 &created, &error_code);
91 EXPECT_NE(base::kInvalidPlatformFileValue, file);
92 EXPECT_TRUE(created);
93 EXPECT_EQ(base::PLATFORM_FILE_OK, error_code);
95 EXPECT_TRUE(base::ClosePlatformFile(file));
96 EXPECT_FALSE(file_util::PathExists(file_path));
99 TEST(PlatformFile, DeleteOpenFile) {
100 base::ScopedTempDir temp_dir;
101 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
102 FilePath file_path = temp_dir.path().AppendASCII("create_file_1");
104 // Create a file.
105 bool created = false;
106 base::PlatformFileError error_code = base::PLATFORM_FILE_OK;
107 base::PlatformFile file = base::CreatePlatformFile(
108 file_path,
109 base::PLATFORM_FILE_OPEN_ALWAYS |
110 base::PLATFORM_FILE_READ |
111 base::PLATFORM_FILE_SHARE_DELETE,
112 &created, &error_code);
113 EXPECT_NE(base::kInvalidPlatformFileValue, file);
114 EXPECT_TRUE(created);
115 EXPECT_EQ(base::PLATFORM_FILE_OK, error_code);
117 // Open an existing file and mark it as delete on close.
118 created = false;
119 base::PlatformFile same_file = base::CreatePlatformFile(
120 file_path,
121 base::PLATFORM_FILE_OPEN |
122 base::PLATFORM_FILE_DELETE_ON_CLOSE |
123 base::PLATFORM_FILE_READ,
124 &created, &error_code);
125 EXPECT_NE(base::kInvalidPlatformFileValue, file);
126 EXPECT_FALSE(created);
127 EXPECT_EQ(base::PLATFORM_FILE_OK, error_code);
129 // Close both handles and check that the file is gone.
130 base::ClosePlatformFile(file);
131 base::ClosePlatformFile(same_file);
132 EXPECT_FALSE(file_util::PathExists(file_path));
135 TEST(PlatformFile, ReadWritePlatformFile) {
136 base::ScopedTempDir temp_dir;
137 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
138 FilePath file_path = temp_dir.path().AppendASCII("read_write_file");
139 base::PlatformFile file = base::CreatePlatformFile(
140 file_path,
141 base::PLATFORM_FILE_CREATE |
142 base::PLATFORM_FILE_READ |
143 base::PLATFORM_FILE_WRITE,
144 NULL, NULL);
145 EXPECT_NE(base::kInvalidPlatformFileValue, file);
147 char data_to_write[] = "test";
148 const int kTestDataSize = 4;
150 // Write 0 bytes to the file.
151 int bytes_written = WriteFully(file, 0, data_to_write, 0);
152 EXPECT_EQ(0, bytes_written);
154 // Write "test" to the file.
155 bytes_written = WriteFully(file, 0, data_to_write, kTestDataSize);
156 EXPECT_EQ(kTestDataSize, bytes_written);
158 // Read from EOF.
159 char data_read_1[32];
160 int bytes_read = ReadFully(file, kTestDataSize, data_read_1, kTestDataSize);
161 EXPECT_EQ(0, bytes_read);
163 // Read from somewhere in the middle of the file.
164 const int kPartialReadOffset = 1;
165 bytes_read = ReadFully(file, kPartialReadOffset, data_read_1, kTestDataSize);
166 EXPECT_EQ(kTestDataSize - kPartialReadOffset, bytes_read);
167 for (int i = 0; i < bytes_read; i++)
168 EXPECT_EQ(data_to_write[i + kPartialReadOffset], data_read_1[i]);
170 // Read 0 bytes.
171 bytes_read = ReadFully(file, 0, data_read_1, 0);
172 EXPECT_EQ(0, bytes_read);
174 // Read the entire file.
175 bytes_read = ReadFully(file, 0, data_read_1, kTestDataSize);
176 EXPECT_EQ(kTestDataSize, bytes_read);
177 for (int i = 0; i < bytes_read; i++)
178 EXPECT_EQ(data_to_write[i], data_read_1[i]);
180 // Read again, but using the trivial native wrapper.
181 bytes_read = base::ReadPlatformFileNoBestEffort(file, 0, data_read_1,
182 kTestDataSize);
183 EXPECT_LE(bytes_read, kTestDataSize);
184 for (int i = 0; i < bytes_read; i++)
185 EXPECT_EQ(data_to_write[i], data_read_1[i]);
187 // Write past the end of the file.
188 const int kOffsetBeyondEndOfFile = 10;
189 const int kPartialWriteLength = 2;
190 bytes_written = WriteFully(file, kOffsetBeyondEndOfFile,
191 data_to_write, kPartialWriteLength);
192 EXPECT_EQ(kPartialWriteLength, bytes_written);
194 // Make sure the file was extended.
195 int64 file_size = 0;
196 EXPECT_TRUE(file_util::GetFileSize(file_path, &file_size));
197 EXPECT_EQ(kOffsetBeyondEndOfFile + kPartialWriteLength, file_size);
199 // Make sure the file was zero-padded.
200 char data_read_2[32];
201 bytes_read = ReadFully(file, 0, data_read_2, static_cast<int>(file_size));
202 EXPECT_EQ(file_size, bytes_read);
203 for (int i = 0; i < kTestDataSize; i++)
204 EXPECT_EQ(data_to_write[i], data_read_2[i]);
205 for (int i = kTestDataSize; i < kOffsetBeyondEndOfFile; i++)
206 EXPECT_EQ(0, data_read_2[i]);
207 for (int i = kOffsetBeyondEndOfFile; i < file_size; i++)
208 EXPECT_EQ(data_to_write[i - kOffsetBeyondEndOfFile], data_read_2[i]);
210 // Close the file handle to allow the temp directory to be deleted.
211 base::ClosePlatformFile(file);
214 TEST(PlatformFile, TruncatePlatformFile) {
215 base::ScopedTempDir temp_dir;
216 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
217 FilePath file_path = temp_dir.path().AppendASCII("truncate_file");
218 base::PlatformFile file = base::CreatePlatformFile(
219 file_path,
220 base::PLATFORM_FILE_CREATE |
221 base::PLATFORM_FILE_READ |
222 base::PLATFORM_FILE_WRITE,
223 NULL, NULL);
224 EXPECT_NE(base::kInvalidPlatformFileValue, file);
226 // Write "test" to the file.
227 char data_to_write[] = "test";
228 int kTestDataSize = 4;
229 int bytes_written = WriteFully(file, 0, data_to_write, kTestDataSize);
230 EXPECT_EQ(kTestDataSize, bytes_written);
232 // Extend the file.
233 const int kExtendedFileLength = 10;
234 int64 file_size = 0;
235 EXPECT_TRUE(base::TruncatePlatformFile(file, kExtendedFileLength));
236 EXPECT_TRUE(file_util::GetFileSize(file_path, &file_size));
237 EXPECT_EQ(kExtendedFileLength, file_size);
239 // Make sure the file was zero-padded.
240 char data_read[32];
241 int bytes_read = ReadFully(file, 0, data_read, static_cast<int>(file_size));
242 EXPECT_EQ(file_size, bytes_read);
243 for (int i = 0; i < kTestDataSize; i++)
244 EXPECT_EQ(data_to_write[i], data_read[i]);
245 for (int i = kTestDataSize; i < file_size; i++)
246 EXPECT_EQ(0, data_read[i]);
248 // Truncate the file.
249 const int kTruncatedFileLength = 2;
250 EXPECT_TRUE(base::TruncatePlatformFile(file, kTruncatedFileLength));
251 EXPECT_TRUE(file_util::GetFileSize(file_path, &file_size));
252 EXPECT_EQ(kTruncatedFileLength, file_size);
254 // Make sure the file was truncated.
255 bytes_read = ReadFully(file, 0, data_read, kTestDataSize);
256 EXPECT_EQ(file_size, bytes_read);
257 for (int i = 0; i < file_size; i++)
258 EXPECT_EQ(data_to_write[i], data_read[i]);
260 // Close the file handle to allow the temp directory to be deleted.
261 base::ClosePlatformFile(file);
264 // Flakily fails: http://crbug.com/86494
265 #if defined(OS_ANDROID)
266 TEST(PlatformFile, TouchGetInfoPlatformFile) {
267 #else
268 TEST(PlatformFile, DISABLED_TouchGetInfoPlatformFile) {
269 #endif
270 base::ScopedTempDir temp_dir;
271 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
272 base::PlatformFile file = base::CreatePlatformFile(
273 temp_dir.path().AppendASCII("touch_get_info_file"),
274 base::PLATFORM_FILE_CREATE |
275 base::PLATFORM_FILE_WRITE |
276 base::PLATFORM_FILE_WRITE_ATTRIBUTES,
277 NULL, NULL);
278 EXPECT_NE(base::kInvalidPlatformFileValue, file);
280 // Get info for a newly created file.
281 base::PlatformFileInfo info;
282 EXPECT_TRUE(base::GetPlatformFileInfo(file, &info));
284 // Add 2 seconds to account for possible rounding errors on
285 // filesystems that use a 1s or 2s timestamp granularity.
286 base::Time now = base::Time::Now() + base::TimeDelta::FromSeconds(2);
287 EXPECT_EQ(0, info.size);
288 EXPECT_FALSE(info.is_directory);
289 EXPECT_FALSE(info.is_symbolic_link);
290 EXPECT_LE(info.last_accessed.ToInternalValue(), now.ToInternalValue());
291 EXPECT_LE(info.last_modified.ToInternalValue(), now.ToInternalValue());
292 EXPECT_LE(info.creation_time.ToInternalValue(), now.ToInternalValue());
293 base::Time creation_time = info.creation_time;
295 // Write "test" to the file.
296 char data[] = "test";
297 const int kTestDataSize = 4;
298 int bytes_written = WriteFully(file, 0, data, kTestDataSize);
299 EXPECT_EQ(kTestDataSize, bytes_written);
301 // Change the last_accessed and last_modified dates.
302 // It's best to add values that are multiples of 2 (in seconds)
303 // to the current last_accessed and last_modified times, because
304 // FATxx uses a 2s timestamp granularity.
305 base::Time new_last_accessed =
306 info.last_accessed + base::TimeDelta::FromSeconds(234);
307 base::Time new_last_modified =
308 info.last_modified + base::TimeDelta::FromMinutes(567);
310 EXPECT_TRUE(base::TouchPlatformFile(file, new_last_accessed,
311 new_last_modified));
313 // Make sure the file info was updated accordingly.
314 EXPECT_TRUE(base::GetPlatformFileInfo(file, &info));
315 EXPECT_EQ(info.size, kTestDataSize);
316 EXPECT_FALSE(info.is_directory);
317 EXPECT_FALSE(info.is_symbolic_link);
319 // ext2/ext3 and HPS/HPS+ seem to have a timestamp granularity of 1s.
320 #if defined(OS_POSIX)
321 EXPECT_EQ(info.last_accessed.ToTimeVal().tv_sec,
322 new_last_accessed.ToTimeVal().tv_sec);
323 EXPECT_EQ(info.last_modified.ToTimeVal().tv_sec,
324 new_last_modified.ToTimeVal().tv_sec);
325 #else
326 EXPECT_EQ(info.last_accessed.ToInternalValue(),
327 new_last_accessed.ToInternalValue());
328 EXPECT_EQ(info.last_modified.ToInternalValue(),
329 new_last_modified.ToInternalValue());
330 #endif
332 EXPECT_EQ(info.creation_time.ToInternalValue(),
333 creation_time.ToInternalValue());
335 // Close the file handle to allow the temp directory to be deleted.
336 base::ClosePlatformFile(file);