| 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/scoped_temp_dir.h" |
| 6 #include "base/platform_file.h" | 7 #include "base/platform_file.h" |
| 7 #include "base/scoped_temp_dir.h" | |
| 8 #include "base/time.h" | 8 #include "base/time.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 | 10 |
| 11 namespace { | 11 namespace { |
| 12 | 12 |
| 13 // Reads from a file the given number of bytes, or until EOF is reached. | 13 // Reads from a file the given number of bytes, or until EOF is reached. |
| 14 // Returns the number of bytes read. | 14 // Returns the number of bytes read. |
| 15 int ReadFully(base::PlatformFile file, int64 offset, char* data, int size) { | 15 int ReadFully(base::PlatformFile file, int64 offset, char* data, int size) { |
| 16 return base::ReadPlatformFile(file, offset, data, size); | 16 return base::ReadPlatformFile(file, offset, data, size); |
| 17 } | 17 } |
| 18 | 18 |
| 19 // Writes the given number of bytes to a file. | 19 // Writes the given number of bytes to a file. |
| 20 // Returns the number of bytes written. | 20 // Returns the number of bytes written. |
| 21 int WriteFully(base::PlatformFile file, int64 offset, | 21 int WriteFully(base::PlatformFile file, int64 offset, |
| 22 const char* data, int size) { | 22 const char* data, int size) { |
| 23 return base::WritePlatformFile(file, offset, data, size); | 23 return base::WritePlatformFile(file, offset, data, size); |
| 24 } | 24 } |
| 25 | 25 |
| 26 } // namespace | 26 } // namespace |
| 27 | 27 |
| 28 TEST(PlatformFile, CreatePlatformFile) { | 28 TEST(PlatformFile, CreatePlatformFile) { |
| 29 ScopedTempDir temp_dir; | 29 base::ScopedTempDir temp_dir; |
| 30 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 30 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 31 FilePath file_path = temp_dir.path().AppendASCII("create_file_1"); | 31 FilePath file_path = temp_dir.path().AppendASCII("create_file_1"); |
| 32 | 32 |
| 33 // Open a file that doesn't exist. | 33 // Open a file that doesn't exist. |
| 34 base::PlatformFileError error_code = base::PLATFORM_FILE_OK; | 34 base::PlatformFileError error_code = base::PLATFORM_FILE_OK; |
| 35 base::PlatformFile file = base::CreatePlatformFile( | 35 base::PlatformFile file = base::CreatePlatformFile( |
| 36 file_path, base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ, | 36 file_path, base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ, |
| 37 NULL, &error_code); | 37 NULL, &error_code); |
| 38 EXPECT_EQ(base::kInvalidPlatformFileValue, file); | 38 EXPECT_EQ(base::kInvalidPlatformFileValue, file); |
| 39 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, error_code); | 39 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, error_code); |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 88 &created, &error_code); | 88 &created, &error_code); |
| 89 EXPECT_NE(base::kInvalidPlatformFileValue, file); | 89 EXPECT_NE(base::kInvalidPlatformFileValue, file); |
| 90 EXPECT_TRUE(created); | 90 EXPECT_TRUE(created); |
| 91 EXPECT_EQ(base::PLATFORM_FILE_OK, error_code); | 91 EXPECT_EQ(base::PLATFORM_FILE_OK, error_code); |
| 92 | 92 |
| 93 EXPECT_TRUE(base::ClosePlatformFile(file)); | 93 EXPECT_TRUE(base::ClosePlatformFile(file)); |
| 94 EXPECT_FALSE(file_util::PathExists(file_path)); | 94 EXPECT_FALSE(file_util::PathExists(file_path)); |
| 95 } | 95 } |
| 96 | 96 |
| 97 TEST(PlatformFile, DeleteOpenFile) { | 97 TEST(PlatformFile, DeleteOpenFile) { |
| 98 ScopedTempDir temp_dir; | 98 base::ScopedTempDir temp_dir; |
| 99 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 99 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 100 FilePath file_path = temp_dir.path().AppendASCII("create_file_1"); | 100 FilePath file_path = temp_dir.path().AppendASCII("create_file_1"); |
| 101 | 101 |
| 102 // Create a file. | 102 // Create a file. |
| 103 bool created = false; | 103 bool created = false; |
| 104 base::PlatformFileError error_code = base::PLATFORM_FILE_OK; | 104 base::PlatformFileError error_code = base::PLATFORM_FILE_OK; |
| 105 base::PlatformFile file = base::CreatePlatformFile( | 105 base::PlatformFile file = base::CreatePlatformFile( |
| 106 file_path, | 106 file_path, |
| 107 base::PLATFORM_FILE_OPEN_ALWAYS | | 107 base::PLATFORM_FILE_OPEN_ALWAYS | |
| 108 base::PLATFORM_FILE_READ | | 108 base::PLATFORM_FILE_READ | |
| (...skipping 15 matching lines...) Expand all Loading... |
| 124 EXPECT_FALSE(created); | 124 EXPECT_FALSE(created); |
| 125 EXPECT_EQ(base::PLATFORM_FILE_OK, error_code); | 125 EXPECT_EQ(base::PLATFORM_FILE_OK, error_code); |
| 126 | 126 |
| 127 // Close both handles and check that the file is gone. | 127 // Close both handles and check that the file is gone. |
| 128 base::ClosePlatformFile(file); | 128 base::ClosePlatformFile(file); |
| 129 base::ClosePlatformFile(same_file); | 129 base::ClosePlatformFile(same_file); |
| 130 EXPECT_FALSE(file_util::PathExists(file_path)); | 130 EXPECT_FALSE(file_util::PathExists(file_path)); |
| 131 } | 131 } |
| 132 | 132 |
| 133 TEST(PlatformFile, ReadWritePlatformFile) { | 133 TEST(PlatformFile, ReadWritePlatformFile) { |
| 134 ScopedTempDir temp_dir; | 134 base::ScopedTempDir temp_dir; |
| 135 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 135 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 136 FilePath file_path = temp_dir.path().AppendASCII("read_write_file"); | 136 FilePath file_path = temp_dir.path().AppendASCII("read_write_file"); |
| 137 base::PlatformFile file = base::CreatePlatformFile( | 137 base::PlatformFile file = base::CreatePlatformFile( |
| 138 file_path, | 138 file_path, |
| 139 base::PLATFORM_FILE_CREATE | | 139 base::PLATFORM_FILE_CREATE | |
| 140 base::PLATFORM_FILE_READ | | 140 base::PLATFORM_FILE_READ | |
| 141 base::PLATFORM_FILE_WRITE, | 141 base::PLATFORM_FILE_WRITE, |
| 142 NULL, NULL); | 142 NULL, NULL); |
| 143 EXPECT_NE(base::kInvalidPlatformFileValue, file); | 143 EXPECT_NE(base::kInvalidPlatformFileValue, file); |
| 144 | 144 |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 203 for (int i = kTestDataSize; i < kOffsetBeyondEndOfFile; i++) | 203 for (int i = kTestDataSize; i < kOffsetBeyondEndOfFile; i++) |
| 204 EXPECT_EQ(0, data_read_2[i]); | 204 EXPECT_EQ(0, data_read_2[i]); |
| 205 for (int i = kOffsetBeyondEndOfFile; i < file_size; i++) | 205 for (int i = kOffsetBeyondEndOfFile; i < file_size; i++) |
| 206 EXPECT_EQ(data_to_write[i - kOffsetBeyondEndOfFile], data_read_2[i]); | 206 EXPECT_EQ(data_to_write[i - kOffsetBeyondEndOfFile], data_read_2[i]); |
| 207 | 207 |
| 208 // Close the file handle to allow the temp directory to be deleted. | 208 // Close the file handle to allow the temp directory to be deleted. |
| 209 base::ClosePlatformFile(file); | 209 base::ClosePlatformFile(file); |
| 210 } | 210 } |
| 211 | 211 |
| 212 TEST(PlatformFile, TruncatePlatformFile) { | 212 TEST(PlatformFile, TruncatePlatformFile) { |
| 213 ScopedTempDir temp_dir; | 213 base::ScopedTempDir temp_dir; |
| 214 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 214 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 215 FilePath file_path = temp_dir.path().AppendASCII("truncate_file"); | 215 FilePath file_path = temp_dir.path().AppendASCII("truncate_file"); |
| 216 base::PlatformFile file = base::CreatePlatformFile( | 216 base::PlatformFile file = base::CreatePlatformFile( |
| 217 file_path, | 217 file_path, |
| 218 base::PLATFORM_FILE_CREATE | | 218 base::PLATFORM_FILE_CREATE | |
| 219 base::PLATFORM_FILE_READ | | 219 base::PLATFORM_FILE_READ | |
| 220 base::PLATFORM_FILE_WRITE, | 220 base::PLATFORM_FILE_WRITE, |
| 221 NULL, NULL); | 221 NULL, NULL); |
| 222 EXPECT_NE(base::kInvalidPlatformFileValue, file); | 222 EXPECT_NE(base::kInvalidPlatformFileValue, file); |
| 223 | 223 |
| (...skipping 30 matching lines...) Expand all Loading... |
| 254 EXPECT_EQ(file_size, bytes_read); | 254 EXPECT_EQ(file_size, bytes_read); |
| 255 for (int i = 0; i < file_size; i++) | 255 for (int i = 0; i < file_size; i++) |
| 256 EXPECT_EQ(data_to_write[i], data_read[i]); | 256 EXPECT_EQ(data_to_write[i], data_read[i]); |
| 257 | 257 |
| 258 // Close the file handle to allow the temp directory to be deleted. | 258 // Close the file handle to allow the temp directory to be deleted. |
| 259 base::ClosePlatformFile(file); | 259 base::ClosePlatformFile(file); |
| 260 } | 260 } |
| 261 | 261 |
| 262 // Flakily fails: http://crbug.com/86494 | 262 // Flakily fails: http://crbug.com/86494 |
| 263 TEST(PlatformFile, DISABLED_TouchGetInfoPlatformFile) { | 263 TEST(PlatformFile, DISABLED_TouchGetInfoPlatformFile) { |
| 264 ScopedTempDir temp_dir; | 264 base::ScopedTempDir temp_dir; |
| 265 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 265 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 266 base::PlatformFile file = base::CreatePlatformFile( | 266 base::PlatformFile file = base::CreatePlatformFile( |
| 267 temp_dir.path().AppendASCII("touch_get_info_file"), | 267 temp_dir.path().AppendASCII("touch_get_info_file"), |
| 268 base::PLATFORM_FILE_CREATE | | 268 base::PLATFORM_FILE_CREATE | |
| 269 base::PLATFORM_FILE_WRITE | | 269 base::PLATFORM_FILE_WRITE | |
| 270 base::PLATFORM_FILE_WRITE_ATTRIBUTES, | 270 base::PLATFORM_FILE_WRITE_ATTRIBUTES, |
| 271 NULL, NULL); | 271 NULL, NULL); |
| 272 EXPECT_NE(base::kInvalidPlatformFileValue, file); | 272 EXPECT_NE(base::kInvalidPlatformFileValue, file); |
| 273 | 273 |
| 274 // Get info for a newly created file. | 274 // Get info for a newly created file. |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 322 EXPECT_EQ(info.last_modified.ToInternalValue(), | 322 EXPECT_EQ(info.last_modified.ToInternalValue(), |
| 323 new_last_modified.ToInternalValue()); | 323 new_last_modified.ToInternalValue()); |
| 324 #endif | 324 #endif |
| 325 | 325 |
| 326 EXPECT_EQ(info.creation_time.ToInternalValue(), | 326 EXPECT_EQ(info.creation_time.ToInternalValue(), |
| 327 creation_time.ToInternalValue()); | 327 creation_time.ToInternalValue()); |
| 328 | 328 |
| 329 // Close the file handle to allow the temp directory to be deleted. | 329 // Close the file handle to allow the temp directory to be deleted. |
| 330 base::ClosePlatformFile(file); | 330 base::ClosePlatformFile(file); |
| 331 } | 331 } |
| OLD | NEW |