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