| 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/file.h" | 6 #include "base/files/file.h" |
| 7 #include "base/files/scoped_temp_dir.h" | 7 #include "base/files/scoped_temp_dir.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 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 37 EXPECT_TRUE(file.IsValid()); | 37 EXPECT_TRUE(file.IsValid()); |
| 38 EXPECT_FALSE(file.created()); | 38 EXPECT_FALSE(file.created()); |
| 39 EXPECT_EQ(base::File::FILE_OK, file.error()); | 39 EXPECT_EQ(base::File::FILE_OK, file.error()); |
| 40 | 40 |
| 41 // This time verify closing the file. | 41 // This time verify closing the file. |
| 42 file.Close(); | 42 file.Close(); |
| 43 EXPECT_FALSE(file.IsValid()); | 43 EXPECT_FALSE(file.IsValid()); |
| 44 } | 44 } |
| 45 | 45 |
| 46 { | 46 { |
| 47 // Open an existing file through Initialize |
| 48 File file; |
| 49 file.Initialize(file_path, base::File::FLAG_OPEN | base::File::FLAG_READ); |
| 50 EXPECT_TRUE(file.IsValid()); |
| 51 EXPECT_FALSE(file.created()); |
| 52 EXPECT_EQ(base::File::FILE_OK, file.error()); |
| 53 |
| 54 // This time verify closing the file. |
| 55 file.Close(); |
| 56 EXPECT_FALSE(file.IsValid()); |
| 57 } |
| 58 |
| 59 { |
| 47 // Create a file that exists. | 60 // Create a file that exists. |
| 48 File file(file_path, base::File::FLAG_CREATE | base::File::FLAG_READ); | 61 File file(file_path, base::File::FLAG_CREATE | base::File::FLAG_READ); |
| 49 EXPECT_FALSE(file.IsValid()); | 62 EXPECT_FALSE(file.IsValid()); |
| 50 EXPECT_FALSE(file.created()); | 63 EXPECT_FALSE(file.created()); |
| 51 EXPECT_EQ(base::File::FILE_ERROR_EXISTS, file.error()); | 64 EXPECT_EQ(base::File::FILE_ERROR_EXISTS, file.error()); |
| 52 } | 65 } |
| 53 | 66 |
| 54 { | 67 { |
| 55 // Create or overwrite a file. | 68 // Create or overwrite a file. |
| 56 File file(file_path, | 69 File file(file_path, |
| (...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 214 int bytes_read = file.Read(0, data_read_1, | 227 int bytes_read = file.Read(0, data_read_1, |
| 215 kTestDataSize + kAppendDataSize); | 228 kTestDataSize + kAppendDataSize); |
| 216 EXPECT_EQ(kTestDataSize + kAppendDataSize, bytes_read); | 229 EXPECT_EQ(kTestDataSize + kAppendDataSize, bytes_read); |
| 217 for (int i = 0; i < kTestDataSize; i++) | 230 for (int i = 0; i < kTestDataSize; i++) |
| 218 EXPECT_EQ(data_to_write[i], data_read_1[i]); | 231 EXPECT_EQ(data_to_write[i], data_read_1[i]); |
| 219 for (int i = 0; i < kAppendDataSize; i++) | 232 for (int i = 0; i < kAppendDataSize; i++) |
| 220 EXPECT_EQ(append_data_to_write[i], data_read_1[kTestDataSize + i]); | 233 EXPECT_EQ(append_data_to_write[i], data_read_1[kTestDataSize + i]); |
| 221 } | 234 } |
| 222 | 235 |
| 223 | 236 |
| 224 TEST(File, Truncate) { | 237 TEST(File, Length) { |
| 225 base::ScopedTempDir temp_dir; | 238 base::ScopedTempDir temp_dir; |
| 226 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 239 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 227 FilePath file_path = temp_dir.path().AppendASCII("truncate_file"); | 240 FilePath file_path = temp_dir.path().AppendASCII("truncate_file"); |
| 228 File file(file_path, | 241 File file(file_path, |
| 229 base::File::FLAG_CREATE | base::File::FLAG_READ | | 242 base::File::FLAG_CREATE | base::File::FLAG_READ | |
| 230 base::File::FLAG_WRITE); | 243 base::File::FLAG_WRITE); |
| 231 ASSERT_TRUE(file.IsValid()); | 244 ASSERT_TRUE(file.IsValid()); |
| 245 EXPECT_EQ(0, file.GetLength()); |
| 232 | 246 |
| 233 // Write "test" to the file. | 247 // Write "test" to the file. |
| 234 char data_to_write[] = "test"; | 248 char data_to_write[] = "test"; |
| 235 int kTestDataSize = 4; | 249 int kTestDataSize = 4; |
| 236 int bytes_written = file.Write(0, data_to_write, kTestDataSize); | 250 int bytes_written = file.Write(0, data_to_write, kTestDataSize); |
| 237 EXPECT_EQ(kTestDataSize, bytes_written); | 251 EXPECT_EQ(kTestDataSize, bytes_written); |
| 238 | 252 |
| 239 // Extend the file. | 253 // Extend the file. |
| 240 const int kExtendedFileLength = 10; | 254 const int kExtendedFileLength = 10; |
| 241 int64 file_size = 0; | 255 int64 file_size = 0; |
| 242 EXPECT_TRUE(file.Truncate(kExtendedFileLength)); | 256 EXPECT_TRUE(file.SetLength(kExtendedFileLength)); |
| 257 EXPECT_EQ(kExtendedFileLength, file.GetLength()); |
| 243 EXPECT_TRUE(GetFileSize(file_path, &file_size)); | 258 EXPECT_TRUE(GetFileSize(file_path, &file_size)); |
| 244 EXPECT_EQ(kExtendedFileLength, file_size); | 259 EXPECT_EQ(kExtendedFileLength, file_size); |
| 245 | 260 |
| 246 // Make sure the file was zero-padded. | 261 // Make sure the file was zero-padded. |
| 247 char data_read[32]; | 262 char data_read[32]; |
| 248 int bytes_read = file.Read(0, data_read, static_cast<int>(file_size)); | 263 int bytes_read = file.Read(0, data_read, static_cast<int>(file_size)); |
| 249 EXPECT_EQ(file_size, bytes_read); | 264 EXPECT_EQ(file_size, bytes_read); |
| 250 for (int i = 0; i < kTestDataSize; i++) | 265 for (int i = 0; i < kTestDataSize; i++) |
| 251 EXPECT_EQ(data_to_write[i], data_read[i]); | 266 EXPECT_EQ(data_to_write[i], data_read[i]); |
| 252 for (int i = kTestDataSize; i < file_size; i++) | 267 for (int i = kTestDataSize; i < file_size; i++) |
| 253 EXPECT_EQ(0, data_read[i]); | 268 EXPECT_EQ(0, data_read[i]); |
| 254 | 269 |
| 255 // Truncate the file. | 270 // Truncate the file. |
| 256 const int kTruncatedFileLength = 2; | 271 const int kTruncatedFileLength = 2; |
| 257 EXPECT_TRUE(file.Truncate(kTruncatedFileLength)); | 272 EXPECT_TRUE(file.SetLength(kTruncatedFileLength)); |
| 273 EXPECT_EQ(kTruncatedFileLength, file.GetLength()); |
| 258 EXPECT_TRUE(GetFileSize(file_path, &file_size)); | 274 EXPECT_TRUE(GetFileSize(file_path, &file_size)); |
| 259 EXPECT_EQ(kTruncatedFileLength, file_size); | 275 EXPECT_EQ(kTruncatedFileLength, file_size); |
| 260 | 276 |
| 261 // Make sure the file was truncated. | 277 // Make sure the file was truncated. |
| 262 bytes_read = file.Read(0, data_read, kTestDataSize); | 278 bytes_read = file.Read(0, data_read, kTestDataSize); |
| 263 EXPECT_EQ(file_size, bytes_read); | 279 EXPECT_EQ(file_size, bytes_read); |
| 264 for (int i = 0; i < file_size; i++) | 280 for (int i = 0; i < file_size; i++) |
| 265 EXPECT_EQ(data_to_write[i], data_read[i]); | 281 EXPECT_EQ(data_to_write[i], data_read[i]); |
| 266 } | 282 } |
| 267 | 283 |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 351 | 367 |
| 352 char buffer[kDataSize]; | 368 char buffer[kDataSize]; |
| 353 int first_chunk_size = kDataSize / 2; | 369 int first_chunk_size = kDataSize / 2; |
| 354 EXPECT_EQ(first_chunk_size, file.ReadAtCurrentPos(buffer, first_chunk_size)); | 370 EXPECT_EQ(first_chunk_size, file.ReadAtCurrentPos(buffer, first_chunk_size)); |
| 355 EXPECT_EQ(kDataSize - first_chunk_size, | 371 EXPECT_EQ(kDataSize - first_chunk_size, |
| 356 file.ReadAtCurrentPos(buffer + first_chunk_size, | 372 file.ReadAtCurrentPos(buffer + first_chunk_size, |
| 357 kDataSize - first_chunk_size)); | 373 kDataSize - first_chunk_size)); |
| 358 EXPECT_EQ(std::string(buffer, buffer + kDataSize), | 374 EXPECT_EQ(std::string(buffer, buffer + kDataSize), |
| 359 std::string(kData)); | 375 std::string(kData)); |
| 360 } | 376 } |
| OLD | NEW |