rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 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. |
| 4 | |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 5 | #include "base/files/file.h" |
brettw@chromium.org | 01f3da4 | 2014-08-14 05:22:14 +0900 | [diff] [blame] | 6 | #include "base/files/file_util.h" |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 7 | #include "base/files/scoped_temp_dir.h" |
gavinp | b746766 | 2014-11-08 02:46:12 +0900 | [diff] [blame] | 8 | #include "base/memory/scoped_ptr.h" |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 9 | #include "base/time/time.h" |
| 10 | #include "testing/gtest/include/gtest/gtest.h" |
| 11 | |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 12 | using base::File; |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 13 | using base::FilePath; |
| 14 | |
rvargas@chromium.org | 799ba6c | 2014-03-21 09:41:15 +0900 | [diff] [blame] | 15 | TEST(FileTest, Create) { |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 16 | base::ScopedTempDir temp_dir; |
| 17 | ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 18 | FilePath file_path = temp_dir.path().AppendASCII("create_file_1"); |
| 19 | |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 20 | { |
rvargas@chromium.org | ca704f4 | 2014-03-26 18:59:31 +0900 | [diff] [blame] | 21 | // Don't create a File at all. |
| 22 | File file; |
| 23 | EXPECT_FALSE(file.IsValid()); |
| 24 | EXPECT_EQ(base::File::FILE_ERROR_FAILED, file.error_details()); |
| 25 | |
| 26 | File file2(base::File::FILE_ERROR_TOO_MANY_OPENED); |
| 27 | EXPECT_FALSE(file2.IsValid()); |
| 28 | EXPECT_EQ(base::File::FILE_ERROR_TOO_MANY_OPENED, file2.error_details()); |
| 29 | } |
| 30 | |
| 31 | { |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 32 | // Open a file that doesn't exist. |
| 33 | File file(file_path, base::File::FLAG_OPEN | base::File::FLAG_READ); |
| 34 | EXPECT_FALSE(file.IsValid()); |
rvargas@chromium.org | 9cce032 | 2014-01-09 07:30:21 +0900 | [diff] [blame] | 35 | EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND, file.error_details()); |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 36 | } |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 37 | |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 38 | { |
| 39 | // Open or create a file. |
| 40 | File file(file_path, base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_READ); |
| 41 | EXPECT_TRUE(file.IsValid()); |
| 42 | EXPECT_TRUE(file.created()); |
rvargas@chromium.org | 9cce032 | 2014-01-09 07:30:21 +0900 | [diff] [blame] | 43 | EXPECT_EQ(base::File::FILE_OK, file.error_details()); |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 44 | } |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 45 | |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 46 | { |
| 47 | // Open an existing file. |
| 48 | File file(file_path, base::File::FLAG_OPEN | base::File::FLAG_READ); |
| 49 | EXPECT_TRUE(file.IsValid()); |
| 50 | EXPECT_FALSE(file.created()); |
rvargas@chromium.org | 9cce032 | 2014-01-09 07:30:21 +0900 | [diff] [blame] | 51 | EXPECT_EQ(base::File::FILE_OK, file.error_details()); |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 52 | |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 53 | // This time verify closing the file. |
| 54 | file.Close(); |
| 55 | EXPECT_FALSE(file.IsValid()); |
| 56 | } |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 57 | |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 58 | { |
rvargas@chromium.org | e207eae | 2014-01-04 07:14:15 +0900 | [diff] [blame] | 59 | // Open an existing file through Initialize |
| 60 | File file; |
| 61 | file.Initialize(file_path, base::File::FLAG_OPEN | base::File::FLAG_READ); |
| 62 | EXPECT_TRUE(file.IsValid()); |
| 63 | EXPECT_FALSE(file.created()); |
rvargas@chromium.org | 9cce032 | 2014-01-09 07:30:21 +0900 | [diff] [blame] | 64 | EXPECT_EQ(base::File::FILE_OK, file.error_details()); |
rvargas@chromium.org | e207eae | 2014-01-04 07:14:15 +0900 | [diff] [blame] | 65 | |
| 66 | // This time verify closing the file. |
| 67 | file.Close(); |
| 68 | EXPECT_FALSE(file.IsValid()); |
| 69 | } |
| 70 | |
| 71 | { |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 72 | // Create a file that exists. |
| 73 | File file(file_path, base::File::FLAG_CREATE | base::File::FLAG_READ); |
| 74 | EXPECT_FALSE(file.IsValid()); |
| 75 | EXPECT_FALSE(file.created()); |
rvargas@chromium.org | 9cce032 | 2014-01-09 07:30:21 +0900 | [diff] [blame] | 76 | EXPECT_EQ(base::File::FILE_ERROR_EXISTS, file.error_details()); |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 77 | } |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 78 | |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 79 | { |
| 80 | // Create or overwrite a file. |
| 81 | File file(file_path, |
mdempsky@chromium.org | 6acba39 | 2014-06-11 03:37:56 +0900 | [diff] [blame] | 82 | base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE); |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 83 | EXPECT_TRUE(file.IsValid()); |
| 84 | EXPECT_TRUE(file.created()); |
rvargas@chromium.org | 9cce032 | 2014-01-09 07:30:21 +0900 | [diff] [blame] | 85 | EXPECT_EQ(base::File::FILE_OK, file.error_details()); |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 86 | } |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 87 | |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 88 | { |
| 89 | // Create a delete-on-close file. |
| 90 | file_path = temp_dir.path().AppendASCII("create_file_2"); |
| 91 | File file(file_path, |
| 92 | base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_READ | |
| 93 | base::File::FLAG_DELETE_ON_CLOSE); |
| 94 | EXPECT_TRUE(file.IsValid()); |
| 95 | EXPECT_TRUE(file.created()); |
rvargas@chromium.org | 9cce032 | 2014-01-09 07:30:21 +0900 | [diff] [blame] | 96 | EXPECT_EQ(base::File::FILE_OK, file.error_details()); |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 97 | } |
| 98 | |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 99 | EXPECT_FALSE(base::PathExists(file_path)); |
| 100 | } |
| 101 | |
rvargas@chromium.org | 799ba6c | 2014-03-21 09:41:15 +0900 | [diff] [blame] | 102 | TEST(FileTest, Async) { |
| 103 | base::ScopedTempDir temp_dir; |
| 104 | ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 105 | FilePath file_path = temp_dir.path().AppendASCII("create_file"); |
| 106 | |
| 107 | { |
| 108 | File file(file_path, base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_ASYNC); |
| 109 | EXPECT_TRUE(file.IsValid()); |
| 110 | EXPECT_TRUE(file.async()); |
| 111 | } |
| 112 | |
| 113 | { |
| 114 | File file(file_path, base::File::FLAG_OPEN_ALWAYS); |
| 115 | EXPECT_TRUE(file.IsValid()); |
| 116 | EXPECT_FALSE(file.async()); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | TEST(FileTest, DeleteOpenFile) { |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 121 | base::ScopedTempDir temp_dir; |
| 122 | ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 123 | FilePath file_path = temp_dir.path().AppendASCII("create_file_1"); |
| 124 | |
| 125 | // Create a file. |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 126 | File file(file_path, |
| 127 | base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_READ | |
| 128 | base::File::FLAG_SHARE_DELETE); |
| 129 | EXPECT_TRUE(file.IsValid()); |
| 130 | EXPECT_TRUE(file.created()); |
rvargas@chromium.org | 9cce032 | 2014-01-09 07:30:21 +0900 | [diff] [blame] | 131 | EXPECT_EQ(base::File::FILE_OK, file.error_details()); |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 132 | |
| 133 | // Open an existing file and mark it as delete on close. |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 134 | File same_file(file_path, |
| 135 | base::File::FLAG_OPEN | base::File::FLAG_DELETE_ON_CLOSE | |
| 136 | base::File::FLAG_READ); |
| 137 | EXPECT_TRUE(file.IsValid()); |
| 138 | EXPECT_FALSE(same_file.created()); |
rvargas@chromium.org | 9cce032 | 2014-01-09 07:30:21 +0900 | [diff] [blame] | 139 | EXPECT_EQ(base::File::FILE_OK, same_file.error_details()); |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 140 | |
| 141 | // Close both handles and check that the file is gone. |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 142 | file.Close(); |
| 143 | same_file.Close(); |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 144 | EXPECT_FALSE(base::PathExists(file_path)); |
| 145 | } |
| 146 | |
rvargas@chromium.org | 799ba6c | 2014-03-21 09:41:15 +0900 | [diff] [blame] | 147 | TEST(FileTest, ReadWrite) { |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 148 | base::ScopedTempDir temp_dir; |
| 149 | ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 150 | FilePath file_path = temp_dir.path().AppendASCII("read_write_file"); |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 151 | File file(file_path, |
| 152 | base::File::FLAG_CREATE | base::File::FLAG_READ | |
| 153 | base::File::FLAG_WRITE); |
| 154 | ASSERT_TRUE(file.IsValid()); |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 155 | |
| 156 | char data_to_write[] = "test"; |
| 157 | const int kTestDataSize = 4; |
| 158 | |
| 159 | // Write 0 bytes to the file. |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 160 | int bytes_written = file.Write(0, data_to_write, 0); |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 161 | EXPECT_EQ(0, bytes_written); |
| 162 | |
| 163 | // Write "test" to the file. |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 164 | bytes_written = file.Write(0, data_to_write, kTestDataSize); |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 165 | EXPECT_EQ(kTestDataSize, bytes_written); |
| 166 | |
| 167 | // Read from EOF. |
| 168 | char data_read_1[32]; |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 169 | int bytes_read = file.Read(kTestDataSize, data_read_1, kTestDataSize); |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 170 | EXPECT_EQ(0, bytes_read); |
| 171 | |
| 172 | // Read from somewhere in the middle of the file. |
| 173 | const int kPartialReadOffset = 1; |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 174 | bytes_read = file.Read(kPartialReadOffset, data_read_1, kTestDataSize); |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 175 | EXPECT_EQ(kTestDataSize - kPartialReadOffset, bytes_read); |
| 176 | for (int i = 0; i < bytes_read; i++) |
| 177 | EXPECT_EQ(data_to_write[i + kPartialReadOffset], data_read_1[i]); |
| 178 | |
| 179 | // Read 0 bytes. |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 180 | bytes_read = file.Read(0, data_read_1, 0); |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 181 | EXPECT_EQ(0, bytes_read); |
| 182 | |
| 183 | // Read the entire file. |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 184 | bytes_read = file.Read(0, data_read_1, kTestDataSize); |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 185 | EXPECT_EQ(kTestDataSize, bytes_read); |
| 186 | for (int i = 0; i < bytes_read; i++) |
| 187 | EXPECT_EQ(data_to_write[i], data_read_1[i]); |
| 188 | |
| 189 | // Read again, but using the trivial native wrapper. |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 190 | bytes_read = file.ReadNoBestEffort(0, data_read_1, kTestDataSize); |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 191 | EXPECT_LE(bytes_read, kTestDataSize); |
| 192 | for (int i = 0; i < bytes_read; i++) |
| 193 | EXPECT_EQ(data_to_write[i], data_read_1[i]); |
| 194 | |
| 195 | // Write past the end of the file. |
| 196 | const int kOffsetBeyondEndOfFile = 10; |
| 197 | const int kPartialWriteLength = 2; |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 198 | bytes_written = file.Write(kOffsetBeyondEndOfFile, |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 199 | data_to_write, kPartialWriteLength); |
| 200 | EXPECT_EQ(kPartialWriteLength, bytes_written); |
| 201 | |
| 202 | // Make sure the file was extended. |
| 203 | int64 file_size = 0; |
brettw@chromium.org | 7068424 | 2013-12-05 03:22:49 +0900 | [diff] [blame] | 204 | EXPECT_TRUE(GetFileSize(file_path, &file_size)); |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 205 | EXPECT_EQ(kOffsetBeyondEndOfFile + kPartialWriteLength, file_size); |
| 206 | |
| 207 | // Make sure the file was zero-padded. |
| 208 | char data_read_2[32]; |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 209 | bytes_read = file.Read(0, data_read_2, static_cast<int>(file_size)); |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 210 | EXPECT_EQ(file_size, bytes_read); |
| 211 | for (int i = 0; i < kTestDataSize; i++) |
| 212 | EXPECT_EQ(data_to_write[i], data_read_2[i]); |
| 213 | for (int i = kTestDataSize; i < kOffsetBeyondEndOfFile; i++) |
| 214 | EXPECT_EQ(0, data_read_2[i]); |
| 215 | for (int i = kOffsetBeyondEndOfFile; i < file_size; i++) |
| 216 | EXPECT_EQ(data_to_write[i - kOffsetBeyondEndOfFile], data_read_2[i]); |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 217 | } |
| 218 | |
rvargas@chromium.org | 799ba6c | 2014-03-21 09:41:15 +0900 | [diff] [blame] | 219 | TEST(FileTest, Append) { |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 220 | base::ScopedTempDir temp_dir; |
| 221 | ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 222 | FilePath file_path = temp_dir.path().AppendASCII("append_file"); |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 223 | File file(file_path, base::File::FLAG_CREATE | base::File::FLAG_APPEND); |
| 224 | ASSERT_TRUE(file.IsValid()); |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 225 | |
| 226 | char data_to_write[] = "test"; |
| 227 | const int kTestDataSize = 4; |
| 228 | |
| 229 | // Write 0 bytes to the file. |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 230 | int bytes_written = file.Write(0, data_to_write, 0); |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 231 | EXPECT_EQ(0, bytes_written); |
| 232 | |
| 233 | // Write "test" to the file. |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 234 | bytes_written = file.Write(0, data_to_write, kTestDataSize); |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 235 | EXPECT_EQ(kTestDataSize, bytes_written); |
| 236 | |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 237 | file.Close(); |
| 238 | File file2(file_path, |
| 239 | base::File::FLAG_OPEN | base::File::FLAG_READ | |
| 240 | base::File::FLAG_APPEND); |
| 241 | ASSERT_TRUE(file2.IsValid()); |
| 242 | |
| 243 | // Test passing the file around. |
| 244 | file = file2.Pass(); |
| 245 | EXPECT_FALSE(file2.IsValid()); |
| 246 | ASSERT_TRUE(file.IsValid()); |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 247 | |
| 248 | char append_data_to_write[] = "78"; |
| 249 | const int kAppendDataSize = 2; |
| 250 | |
| 251 | // Append "78" to the file. |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 252 | bytes_written = file.Write(0, append_data_to_write, kAppendDataSize); |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 253 | EXPECT_EQ(kAppendDataSize, bytes_written); |
| 254 | |
| 255 | // Read the entire file. |
| 256 | char data_read_1[32]; |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 257 | int bytes_read = file.Read(0, data_read_1, |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 258 | kTestDataSize + kAppendDataSize); |
| 259 | EXPECT_EQ(kTestDataSize + kAppendDataSize, bytes_read); |
| 260 | for (int i = 0; i < kTestDataSize; i++) |
| 261 | EXPECT_EQ(data_to_write[i], data_read_1[i]); |
| 262 | for (int i = 0; i < kAppendDataSize; i++) |
| 263 | EXPECT_EQ(append_data_to_write[i], data_read_1[kTestDataSize + i]); |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | |
rvargas@chromium.org | 799ba6c | 2014-03-21 09:41:15 +0900 | [diff] [blame] | 267 | TEST(FileTest, Length) { |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 268 | base::ScopedTempDir temp_dir; |
| 269 | ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 270 | FilePath file_path = temp_dir.path().AppendASCII("truncate_file"); |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 271 | File file(file_path, |
| 272 | base::File::FLAG_CREATE | base::File::FLAG_READ | |
| 273 | base::File::FLAG_WRITE); |
| 274 | ASSERT_TRUE(file.IsValid()); |
rvargas@chromium.org | e207eae | 2014-01-04 07:14:15 +0900 | [diff] [blame] | 275 | EXPECT_EQ(0, file.GetLength()); |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 276 | |
| 277 | // Write "test" to the file. |
| 278 | char data_to_write[] = "test"; |
| 279 | int kTestDataSize = 4; |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 280 | int bytes_written = file.Write(0, data_to_write, kTestDataSize); |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 281 | EXPECT_EQ(kTestDataSize, bytes_written); |
| 282 | |
| 283 | // Extend the file. |
| 284 | const int kExtendedFileLength = 10; |
| 285 | int64 file_size = 0; |
rvargas@chromium.org | e207eae | 2014-01-04 07:14:15 +0900 | [diff] [blame] | 286 | EXPECT_TRUE(file.SetLength(kExtendedFileLength)); |
| 287 | EXPECT_EQ(kExtendedFileLength, file.GetLength()); |
brettw@chromium.org | 7068424 | 2013-12-05 03:22:49 +0900 | [diff] [blame] | 288 | EXPECT_TRUE(GetFileSize(file_path, &file_size)); |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 289 | EXPECT_EQ(kExtendedFileLength, file_size); |
| 290 | |
| 291 | // Make sure the file was zero-padded. |
| 292 | char data_read[32]; |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 293 | int bytes_read = file.Read(0, data_read, static_cast<int>(file_size)); |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 294 | EXPECT_EQ(file_size, bytes_read); |
| 295 | for (int i = 0; i < kTestDataSize; i++) |
| 296 | EXPECT_EQ(data_to_write[i], data_read[i]); |
| 297 | for (int i = kTestDataSize; i < file_size; i++) |
| 298 | EXPECT_EQ(0, data_read[i]); |
| 299 | |
| 300 | // Truncate the file. |
| 301 | const int kTruncatedFileLength = 2; |
rvargas@chromium.org | e207eae | 2014-01-04 07:14:15 +0900 | [diff] [blame] | 302 | EXPECT_TRUE(file.SetLength(kTruncatedFileLength)); |
| 303 | EXPECT_EQ(kTruncatedFileLength, file.GetLength()); |
brettw@chromium.org | 7068424 | 2013-12-05 03:22:49 +0900 | [diff] [blame] | 304 | EXPECT_TRUE(GetFileSize(file_path, &file_size)); |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 305 | EXPECT_EQ(kTruncatedFileLength, file_size); |
| 306 | |
| 307 | // Make sure the file was truncated. |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 308 | bytes_read = file.Read(0, data_read, kTestDataSize); |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 309 | EXPECT_EQ(file_size, bytes_read); |
| 310 | for (int i = 0; i < file_size; i++) |
| 311 | EXPECT_EQ(data_to_write[i], data_read[i]); |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | // Flakily fails: http://crbug.com/86494 |
| 315 | #if defined(OS_ANDROID) |
rvargas@chromium.org | 799ba6c | 2014-03-21 09:41:15 +0900 | [diff] [blame] | 316 | TEST(FileTest, TouchGetInfo) { |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 317 | #else |
rvargas@chromium.org | 799ba6c | 2014-03-21 09:41:15 +0900 | [diff] [blame] | 318 | TEST(FileTest, DISABLED_TouchGetInfo) { |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 319 | #endif |
| 320 | base::ScopedTempDir temp_dir; |
| 321 | ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 322 | File file(temp_dir.path().AppendASCII("touch_get_info_file"), |
| 323 | base::File::FLAG_CREATE | base::File::FLAG_WRITE | |
| 324 | base::File::FLAG_WRITE_ATTRIBUTES); |
| 325 | ASSERT_TRUE(file.IsValid()); |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 326 | |
| 327 | // Get info for a newly created file. |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 328 | base::File::Info info; |
| 329 | EXPECT_TRUE(file.GetInfo(&info)); |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 330 | |
| 331 | // Add 2 seconds to account for possible rounding errors on |
| 332 | // filesystems that use a 1s or 2s timestamp granularity. |
| 333 | base::Time now = base::Time::Now() + base::TimeDelta::FromSeconds(2); |
| 334 | EXPECT_EQ(0, info.size); |
| 335 | EXPECT_FALSE(info.is_directory); |
| 336 | EXPECT_FALSE(info.is_symbolic_link); |
| 337 | EXPECT_LE(info.last_accessed.ToInternalValue(), now.ToInternalValue()); |
| 338 | EXPECT_LE(info.last_modified.ToInternalValue(), now.ToInternalValue()); |
| 339 | EXPECT_LE(info.creation_time.ToInternalValue(), now.ToInternalValue()); |
| 340 | base::Time creation_time = info.creation_time; |
| 341 | |
| 342 | // Write "test" to the file. |
| 343 | char data[] = "test"; |
| 344 | const int kTestDataSize = 4; |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 345 | int bytes_written = file.Write(0, data, kTestDataSize); |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 346 | EXPECT_EQ(kTestDataSize, bytes_written); |
| 347 | |
| 348 | // Change the last_accessed and last_modified dates. |
| 349 | // It's best to add values that are multiples of 2 (in seconds) |
| 350 | // to the current last_accessed and last_modified times, because |
| 351 | // FATxx uses a 2s timestamp granularity. |
| 352 | base::Time new_last_accessed = |
| 353 | info.last_accessed + base::TimeDelta::FromSeconds(234); |
| 354 | base::Time new_last_modified = |
| 355 | info.last_modified + base::TimeDelta::FromMinutes(567); |
| 356 | |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 357 | EXPECT_TRUE(file.SetTimes(new_last_accessed, new_last_modified)); |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 358 | |
| 359 | // Make sure the file info was updated accordingly. |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 360 | EXPECT_TRUE(file.GetInfo(&info)); |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 361 | EXPECT_EQ(info.size, kTestDataSize); |
| 362 | EXPECT_FALSE(info.is_directory); |
| 363 | EXPECT_FALSE(info.is_symbolic_link); |
| 364 | |
| 365 | // ext2/ext3 and HPS/HPS+ seem to have a timestamp granularity of 1s. |
| 366 | #if defined(OS_POSIX) |
| 367 | EXPECT_EQ(info.last_accessed.ToTimeVal().tv_sec, |
| 368 | new_last_accessed.ToTimeVal().tv_sec); |
| 369 | EXPECT_EQ(info.last_modified.ToTimeVal().tv_sec, |
| 370 | new_last_modified.ToTimeVal().tv_sec); |
| 371 | #else |
| 372 | EXPECT_EQ(info.last_accessed.ToInternalValue(), |
| 373 | new_last_accessed.ToInternalValue()); |
| 374 | EXPECT_EQ(info.last_modified.ToInternalValue(), |
| 375 | new_last_modified.ToInternalValue()); |
| 376 | #endif |
| 377 | |
| 378 | EXPECT_EQ(info.creation_time.ToInternalValue(), |
| 379 | creation_time.ToInternalValue()); |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 380 | } |
| 381 | |
rvargas@chromium.org | 799ba6c | 2014-03-21 09:41:15 +0900 | [diff] [blame] | 382 | TEST(FileTest, ReadAtCurrentPosition) { |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 383 | base::ScopedTempDir temp_dir; |
| 384 | ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
rvargas@chromium.org | cb69d09 | 2014-03-08 00:35:26 +0900 | [diff] [blame] | 385 | FilePath file_path = temp_dir.path().AppendASCII("read_at_current_position"); |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 386 | File file(file_path, |
| 387 | base::File::FLAG_CREATE | base::File::FLAG_READ | |
| 388 | base::File::FLAG_WRITE); |
| 389 | EXPECT_TRUE(file.IsValid()); |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 390 | |
| 391 | const char kData[] = "test"; |
rvargas@chromium.org | cb69d09 | 2014-03-08 00:35:26 +0900 | [diff] [blame] | 392 | const int kDataSize = sizeof(kData) - 1; |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 393 | EXPECT_EQ(kDataSize, file.Write(0, kData, kDataSize)); |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 394 | |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 395 | EXPECT_EQ(0, file.Seek(base::File::FROM_BEGIN, 0)); |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 396 | |
| 397 | char buffer[kDataSize]; |
| 398 | int first_chunk_size = kDataSize / 2; |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 399 | EXPECT_EQ(first_chunk_size, file.ReadAtCurrentPos(buffer, first_chunk_size)); |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 400 | EXPECT_EQ(kDataSize - first_chunk_size, |
rvargas@chromium.org | 12938d7 | 2013-12-04 09:46:32 +0900 | [diff] [blame] | 401 | file.ReadAtCurrentPos(buffer + first_chunk_size, |
| 402 | kDataSize - first_chunk_size)); |
rvargas@chromium.org | cb69d09 | 2014-03-08 00:35:26 +0900 | [diff] [blame] | 403 | EXPECT_EQ(std::string(buffer, buffer + kDataSize), std::string(kData)); |
| 404 | } |
| 405 | |
rvargas@chromium.org | 799ba6c | 2014-03-21 09:41:15 +0900 | [diff] [blame] | 406 | TEST(FileTest, WriteAtCurrentPosition) { |
rvargas@chromium.org | cb69d09 | 2014-03-08 00:35:26 +0900 | [diff] [blame] | 407 | base::ScopedTempDir temp_dir; |
| 408 | ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 409 | FilePath file_path = temp_dir.path().AppendASCII("write_at_current_position"); |
| 410 | File file(file_path, |
| 411 | base::File::FLAG_CREATE | base::File::FLAG_READ | |
| 412 | base::File::FLAG_WRITE); |
| 413 | EXPECT_TRUE(file.IsValid()); |
| 414 | |
| 415 | const char kData[] = "test"; |
| 416 | const int kDataSize = sizeof(kData) - 1; |
| 417 | |
| 418 | int first_chunk_size = kDataSize / 2; |
| 419 | EXPECT_EQ(first_chunk_size, file.WriteAtCurrentPos(kData, first_chunk_size)); |
| 420 | EXPECT_EQ(kDataSize - first_chunk_size, |
| 421 | file.WriteAtCurrentPos(kData + first_chunk_size, |
| 422 | kDataSize - first_chunk_size)); |
| 423 | |
| 424 | char buffer[kDataSize]; |
| 425 | EXPECT_EQ(kDataSize, file.Read(0, buffer, kDataSize)); |
| 426 | EXPECT_EQ(std::string(buffer, buffer + kDataSize), std::string(kData)); |
rvargas@google.com | b1ae319 | 2013-11-28 10:31:31 +0900 | [diff] [blame] | 427 | } |
rvargas@chromium.org | b005b38 | 2014-01-07 19:06:58 +0900 | [diff] [blame] | 428 | |
rvargas@chromium.org | ba1a954 | 2014-06-21 08:30:18 +0900 | [diff] [blame] | 429 | TEST(FileTest, Seek) { |
| 430 | base::ScopedTempDir temp_dir; |
| 431 | ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 432 | FilePath file_path = temp_dir.path().AppendASCII("seek_file"); |
| 433 | File file(file_path, |
| 434 | base::File::FLAG_CREATE | base::File::FLAG_READ | |
| 435 | base::File::FLAG_WRITE); |
| 436 | ASSERT_TRUE(file.IsValid()); |
| 437 | |
| 438 | const int64 kOffset = 10; |
| 439 | EXPECT_EQ(kOffset, file.Seek(base::File::FROM_BEGIN, kOffset)); |
| 440 | EXPECT_EQ(2 * kOffset, file.Seek(base::File::FROM_CURRENT, kOffset)); |
| 441 | EXPECT_EQ(kOffset, file.Seek(base::File::FROM_CURRENT, -kOffset)); |
| 442 | EXPECT_TRUE(file.SetLength(kOffset * 2)); |
| 443 | EXPECT_EQ(kOffset, file.Seek(base::File::FROM_END, -kOffset)); |
| 444 | } |
| 445 | |
grt | 98ea39f | 2015-03-20 02:54:40 +0900 | [diff] [blame] | 446 | TEST(FileTest, Duplicate) { |
| 447 | base::ScopedTempDir temp_dir; |
| 448 | ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 449 | FilePath file_path = temp_dir.path().AppendASCII("file"); |
| 450 | File file(file_path,(base::File::FLAG_CREATE | |
| 451 | base::File::FLAG_READ | |
| 452 | base::File::FLAG_WRITE)); |
| 453 | ASSERT_TRUE(file.IsValid()); |
| 454 | |
| 455 | File file2(file.Duplicate()); |
| 456 | ASSERT_TRUE(file2.IsValid()); |
| 457 | |
| 458 | // Write through one handle, close it, read through the other. |
| 459 | static const char kData[] = "now is a good time."; |
| 460 | static const int kDataLen = sizeof(kData) - 1; |
| 461 | |
| 462 | ASSERT_EQ(0, file.Seek(base::File::FROM_CURRENT, 0)); |
| 463 | ASSERT_EQ(0, file2.Seek(base::File::FROM_CURRENT, 0)); |
| 464 | ASSERT_EQ(kDataLen, file.WriteAtCurrentPos(kData, kDataLen)); |
| 465 | ASSERT_EQ(kDataLen, file.Seek(base::File::FROM_CURRENT, 0)); |
| 466 | ASSERT_EQ(kDataLen, file2.Seek(base::File::FROM_CURRENT, 0)); |
| 467 | file.Close(); |
| 468 | char buf[kDataLen]; |
| 469 | ASSERT_EQ(kDataLen, file2.Read(0, &buf[0], kDataLen)); |
| 470 | ASSERT_EQ(std::string(kData, kDataLen), std::string(&buf[0], kDataLen)); |
| 471 | } |
| 472 | |
| 473 | TEST(FileTest, DuplicateDeleteOnClose) { |
| 474 | base::ScopedTempDir temp_dir; |
| 475 | ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 476 | FilePath file_path = temp_dir.path().AppendASCII("file"); |
| 477 | File file(file_path,(base::File::FLAG_CREATE | |
| 478 | base::File::FLAG_READ | |
| 479 | base::File::FLAG_WRITE | |
| 480 | base::File::FLAG_DELETE_ON_CLOSE)); |
| 481 | ASSERT_TRUE(file.IsValid()); |
| 482 | File file2(file.Duplicate()); |
| 483 | ASSERT_TRUE(file2.IsValid()); |
| 484 | file.Close(); |
| 485 | file2.Close(); |
| 486 | ASSERT_FALSE(base::PathExists(file_path)); |
| 487 | } |
| 488 | |
rvargas@chromium.org | b005b38 | 2014-01-07 19:06:58 +0900 | [diff] [blame] | 489 | #if defined(OS_WIN) |
rvargas@chromium.org | 799ba6c | 2014-03-21 09:41:15 +0900 | [diff] [blame] | 490 | TEST(FileTest, GetInfoForDirectory) { |
rvargas@chromium.org | b005b38 | 2014-01-07 19:06:58 +0900 | [diff] [blame] | 491 | base::ScopedTempDir temp_dir; |
| 492 | ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 493 | FilePath empty_dir = temp_dir.path().Append(FILE_PATH_LITERAL("gpfi_test")); |
| 494 | ASSERT_TRUE(CreateDirectory(empty_dir)); |
| 495 | |
| 496 | base::File dir( |
| 497 | ::CreateFile(empty_dir.value().c_str(), |
| 498 | FILE_ALL_ACCESS, |
| 499 | FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, |
| 500 | NULL, |
| 501 | OPEN_EXISTING, |
| 502 | FILE_FLAG_BACKUP_SEMANTICS, // Needed to open a directory. |
| 503 | NULL)); |
| 504 | ASSERT_TRUE(dir.IsValid()); |
| 505 | |
| 506 | base::File::Info info; |
| 507 | EXPECT_TRUE(dir.GetInfo(&info)); |
| 508 | EXPECT_TRUE(info.is_directory); |
| 509 | EXPECT_FALSE(info.is_symbolic_link); |
| 510 | EXPECT_EQ(0, info.size); |
| 511 | } |
| 512 | #endif // defined(OS_WIN) |
gavinp | b746766 | 2014-11-08 02:46:12 +0900 | [diff] [blame] | 513 | |
| 514 | #if defined(OS_POSIX) && defined(GTEST_HAS_DEATH_TEST) |
| 515 | TEST(FileTest, MemoryCorruption) { |
| 516 | { |
| 517 | // Test that changing the checksum value is detected. |
| 518 | base::File file; |
| 519 | EXPECT_NE(file.file_.file_memory_checksum_, |
danakj | 9110131 | 2015-09-16 05:27:24 +0900 | [diff] [blame^] | 520 | static_cast<unsigned int>(file.GetPlatformFile())); |
gavinp | b746766 | 2014-11-08 02:46:12 +0900 | [diff] [blame] | 521 | file.file_.file_memory_checksum_ = file.GetPlatformFile(); |
gavinp | 54aaf9f | 2014-11-10 06:43:18 +0900 | [diff] [blame] | 522 | EXPECT_DEATH(file.IsValid(), ""); |
gavinp | b746766 | 2014-11-08 02:46:12 +0900 | [diff] [blame] | 523 | |
| 524 | file.file_.UpdateChecksum(); // Do not crash on File::~File(). |
| 525 | } |
| 526 | |
| 527 | { |
| 528 | // Test that changing the file descriptor value is detected. |
| 529 | base::File file; |
| 530 | file.file_.file_.reset(17); |
gavinp | 54aaf9f | 2014-11-10 06:43:18 +0900 | [diff] [blame] | 531 | EXPECT_DEATH(file.IsValid(), ""); |
gavinp | b746766 | 2014-11-08 02:46:12 +0900 | [diff] [blame] | 532 | |
| 533 | // Do not crash on File::~File(). |
| 534 | ignore_result(file.file_.file_.release()); |
| 535 | file.file_.UpdateChecksum(); |
| 536 | } |
| 537 | |
| 538 | { |
| 539 | // Test that GetPlatformFile() checks for corruption. |
| 540 | base::File file; |
| 541 | file.file_.file_memory_checksum_ = file.GetPlatformFile(); |
gavinp | 54aaf9f | 2014-11-10 06:43:18 +0900 | [diff] [blame] | 542 | EXPECT_DEATH(file.GetPlatformFile(), ""); |
gavinp | b746766 | 2014-11-08 02:46:12 +0900 | [diff] [blame] | 543 | |
| 544 | file.file_.UpdateChecksum(); // Do not crash on File::~File(). |
| 545 | } |
| 546 | |
| 547 | { |
| 548 | // Test that the base::File destructor checks for corruption. |
| 549 | scoped_ptr<base::File> file(new File()); |
| 550 | file->file_.file_memory_checksum_ = file->GetPlatformFile(); |
gavinp | 54aaf9f | 2014-11-10 06:43:18 +0900 | [diff] [blame] | 551 | EXPECT_DEATH(file.reset(), ""); |
gavinp | b746766 | 2014-11-08 02:46:12 +0900 | [diff] [blame] | 552 | |
| 553 | // Do not crash on this thread's destructor call. |
| 554 | file->file_.UpdateChecksum(); |
| 555 | } |
| 556 | |
| 557 | { |
| 558 | // Test that the base::File constructor checks for corruption. |
| 559 | base::File file; |
| 560 | file.file_.file_memory_checksum_ = file.GetPlatformFile(); |
gavinp | 54aaf9f | 2014-11-10 06:43:18 +0900 | [diff] [blame] | 561 | EXPECT_DEATH(File f(file.Pass()), ""); |
gavinp | b746766 | 2014-11-08 02:46:12 +0900 | [diff] [blame] | 562 | |
| 563 | file.file_.UpdateChecksum(); // Do not crash on File::~File(). |
| 564 | } |
| 565 | |
| 566 | { |
| 567 | // Test that doing IO checks for corruption. |
| 568 | base::File file; |
| 569 | file.file_.file_.reset(17); // A fake open FD value. |
| 570 | |
gavinp | 54aaf9f | 2014-11-10 06:43:18 +0900 | [diff] [blame] | 571 | EXPECT_DEATH(file.Seek(File::FROM_BEGIN, 0), ""); |
| 572 | EXPECT_DEATH(file.Read(0, NULL, 0), ""); |
| 573 | EXPECT_DEATH(file.ReadAtCurrentPos(NULL, 0), ""); |
| 574 | EXPECT_DEATH(file.Write(0, NULL, 0), ""); |
gavinp | b746766 | 2014-11-08 02:46:12 +0900 | [diff] [blame] | 575 | |
| 576 | ignore_result(file.file_.file_.release()); |
| 577 | file.file_.UpdateChecksum(); |
| 578 | } |
| 579 | } |
| 580 | #endif // defined(OS_POSIX) |