Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
|
viettrungluu
2013/12/03 21:20:19
Ditto for this file.
| |
| 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/scoped_temp_dir.h" | 6 #include "base/files/scoped_temp_dir.h" |
| 7 #include "base/platform_file.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::FilePath; | 11 using base::FilePath; |
| (...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 196 | 196 |
| 197 // Write past the end of the file. | 197 // Write past the end of the file. |
| 198 const int kOffsetBeyondEndOfFile = 10; | 198 const int kOffsetBeyondEndOfFile = 10; |
| 199 const int kPartialWriteLength = 2; | 199 const int kPartialWriteLength = 2; |
| 200 bytes_written = WriteFully(file, kOffsetBeyondEndOfFile, | 200 bytes_written = WriteFully(file, kOffsetBeyondEndOfFile, |
| 201 data_to_write, kPartialWriteLength); | 201 data_to_write, kPartialWriteLength); |
| 202 EXPECT_EQ(kPartialWriteLength, bytes_written); | 202 EXPECT_EQ(kPartialWriteLength, bytes_written); |
| 203 | 203 |
| 204 // Make sure the file was extended. | 204 // Make sure the file was extended. |
| 205 int64 file_size = 0; | 205 int64 file_size = 0; |
| 206 EXPECT_TRUE(file_util::GetFileSize(file_path, &file_size)); | 206 EXPECT_TRUE(base::GetFileSize(file_path, &file_size)); |
| 207 EXPECT_EQ(kOffsetBeyondEndOfFile + kPartialWriteLength, file_size); | 207 EXPECT_EQ(kOffsetBeyondEndOfFile + kPartialWriteLength, file_size); |
| 208 | 208 |
| 209 // Make sure the file was zero-padded. | 209 // Make sure the file was zero-padded. |
| 210 char data_read_2[32]; | 210 char data_read_2[32]; |
| 211 bytes_read = ReadFully(file, 0, data_read_2, static_cast<int>(file_size)); | 211 bytes_read = ReadFully(file, 0, data_read_2, static_cast<int>(file_size)); |
| 212 EXPECT_EQ(file_size, bytes_read); | 212 EXPECT_EQ(file_size, bytes_read); |
| 213 for (int i = 0; i < kTestDataSize; i++) | 213 for (int i = 0; i < kTestDataSize; i++) |
| 214 EXPECT_EQ(data_to_write[i], data_read_2[i]); | 214 EXPECT_EQ(data_to_write[i], data_read_2[i]); |
| 215 for (int i = kTestDataSize; i < kOffsetBeyondEndOfFile; i++) | 215 for (int i = kTestDataSize; i < kOffsetBeyondEndOfFile; i++) |
| 216 EXPECT_EQ(0, data_read_2[i]); | 216 EXPECT_EQ(0, data_read_2[i]); |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 289 // Write "test" to the file. | 289 // Write "test" to the file. |
| 290 char data_to_write[] = "test"; | 290 char data_to_write[] = "test"; |
| 291 int kTestDataSize = 4; | 291 int kTestDataSize = 4; |
| 292 int bytes_written = WriteFully(file, 0, data_to_write, kTestDataSize); | 292 int bytes_written = WriteFully(file, 0, data_to_write, kTestDataSize); |
| 293 EXPECT_EQ(kTestDataSize, bytes_written); | 293 EXPECT_EQ(kTestDataSize, bytes_written); |
| 294 | 294 |
| 295 // Extend the file. | 295 // Extend the file. |
| 296 const int kExtendedFileLength = 10; | 296 const int kExtendedFileLength = 10; |
| 297 int64 file_size = 0; | 297 int64 file_size = 0; |
| 298 EXPECT_TRUE(base::TruncatePlatformFile(file, kExtendedFileLength)); | 298 EXPECT_TRUE(base::TruncatePlatformFile(file, kExtendedFileLength)); |
| 299 EXPECT_TRUE(file_util::GetFileSize(file_path, &file_size)); | 299 EXPECT_TRUE(base::GetFileSize(file_path, &file_size)); |
| 300 EXPECT_EQ(kExtendedFileLength, file_size); | 300 EXPECT_EQ(kExtendedFileLength, file_size); |
| 301 | 301 |
| 302 // Make sure the file was zero-padded. | 302 // Make sure the file was zero-padded. |
| 303 char data_read[32]; | 303 char data_read[32]; |
| 304 int bytes_read = ReadFully(file, 0, data_read, static_cast<int>(file_size)); | 304 int bytes_read = ReadFully(file, 0, data_read, static_cast<int>(file_size)); |
| 305 EXPECT_EQ(file_size, bytes_read); | 305 EXPECT_EQ(file_size, bytes_read); |
| 306 for (int i = 0; i < kTestDataSize; i++) | 306 for (int i = 0; i < kTestDataSize; i++) |
| 307 EXPECT_EQ(data_to_write[i], data_read[i]); | 307 EXPECT_EQ(data_to_write[i], data_read[i]); |
| 308 for (int i = kTestDataSize; i < file_size; i++) | 308 for (int i = kTestDataSize; i < file_size; i++) |
| 309 EXPECT_EQ(0, data_read[i]); | 309 EXPECT_EQ(0, data_read[i]); |
| 310 | 310 |
| 311 // Truncate the file. | 311 // Truncate the file. |
| 312 const int kTruncatedFileLength = 2; | 312 const int kTruncatedFileLength = 2; |
| 313 EXPECT_TRUE(base::TruncatePlatformFile(file, kTruncatedFileLength)); | 313 EXPECT_TRUE(base::TruncatePlatformFile(file, kTruncatedFileLength)); |
| 314 EXPECT_TRUE(file_util::GetFileSize(file_path, &file_size)); | 314 EXPECT_TRUE(base::GetFileSize(file_path, &file_size)); |
| 315 EXPECT_EQ(kTruncatedFileLength, file_size); | 315 EXPECT_EQ(kTruncatedFileLength, file_size); |
| 316 | 316 |
| 317 // Make sure the file was truncated. | 317 // Make sure the file was truncated. |
| 318 bytes_read = ReadFully(file, 0, data_read, kTestDataSize); | 318 bytes_read = ReadFully(file, 0, data_read, kTestDataSize); |
| 319 EXPECT_EQ(file_size, bytes_read); | 319 EXPECT_EQ(file_size, bytes_read); |
| 320 for (int i = 0; i < file_size; i++) | 320 for (int i = 0; i < file_size; i++) |
| 321 EXPECT_EQ(data_to_write[i], data_read[i]); | 321 EXPECT_EQ(data_to_write[i], data_read[i]); |
| 322 | 322 |
| 323 // Close the file handle to allow the temp directory to be deleted. | 323 // Close the file handle to allow the temp directory to be deleted. |
| 324 base::ClosePlatformFile(file); | 324 base::ClosePlatformFile(file); |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 425 file, buffer, first_chunk_size)); | 425 file, buffer, first_chunk_size)); |
| 426 EXPECT_EQ(kDataSize - first_chunk_size, | 426 EXPECT_EQ(kDataSize - first_chunk_size, |
| 427 base::ReadPlatformFileAtCurrentPos( | 427 base::ReadPlatformFileAtCurrentPos( |
| 428 file, buffer + first_chunk_size, | 428 file, buffer + first_chunk_size, |
| 429 kDataSize - first_chunk_size)); | 429 kDataSize - first_chunk_size)); |
| 430 EXPECT_EQ(std::string(buffer, buffer + kDataSize), | 430 EXPECT_EQ(std::string(buffer, buffer + kDataSize), |
| 431 std::string(kData)); | 431 std::string(kData)); |
| 432 | 432 |
| 433 base::ClosePlatformFile(file); | 433 base::ClosePlatformFile(file); |
| 434 } | 434 } |
| OLD | NEW |