| 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 |
| 11 using base::File; | 11 using base::File; |
| 12 using base::FilePath; | 12 using base::FilePath; |
| 13 | 13 |
| 14 TEST(File, Create) { | 14 TEST(File, Create) { |
| 15 base::ScopedTempDir temp_dir; | 15 base::ScopedTempDir temp_dir; |
| 16 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 16 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 17 FilePath file_path = temp_dir.path().AppendASCII("create_file_1"); | 17 FilePath file_path = temp_dir.path().AppendASCII("create_file_1"); |
| 18 | 18 |
| 19 { | 19 { |
| 20 // Open a file that doesn't exist. | 20 // Open a file that doesn't exist. |
| 21 File file(file_path, base::File::FLAG_OPEN | base::File::FLAG_READ); | 21 File file(file_path, base::File::FLAG_OPEN | base::File::FLAG_READ); |
| 22 EXPECT_FALSE(file.IsValid()); | 22 EXPECT_FALSE(file.IsValid()); |
| 23 EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND, file.error()); | 23 EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND, file.error_details()); |
| 24 } | 24 } |
| 25 | 25 |
| 26 { | 26 { |
| 27 // Open or create a file. | 27 // Open or create a file. |
| 28 File file(file_path, base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_READ); | 28 File file(file_path, base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_READ); |
| 29 EXPECT_TRUE(file.IsValid()); | 29 EXPECT_TRUE(file.IsValid()); |
| 30 EXPECT_TRUE(file.created()); | 30 EXPECT_TRUE(file.created()); |
| 31 EXPECT_EQ(base::File::FILE_OK, file.error()); | 31 EXPECT_EQ(base::File::FILE_OK, file.error_details()); |
| 32 } | 32 } |
| 33 | 33 |
| 34 { | 34 { |
| 35 // Open an existing file. | 35 // Open an existing file. |
| 36 File file(file_path, base::File::FLAG_OPEN | base::File::FLAG_READ); | 36 File file(file_path, base::File::FLAG_OPEN | base::File::FLAG_READ); |
| 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_details()); |
| 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 | 47 // Open an existing file through Initialize |
| 48 File file; | 48 File file; |
| 49 file.Initialize(file_path, base::File::FLAG_OPEN | base::File::FLAG_READ); | 49 file.Initialize(file_path, base::File::FLAG_OPEN | base::File::FLAG_READ); |
| 50 EXPECT_TRUE(file.IsValid()); | 50 EXPECT_TRUE(file.IsValid()); |
| 51 EXPECT_FALSE(file.created()); | 51 EXPECT_FALSE(file.created()); |
| 52 EXPECT_EQ(base::File::FILE_OK, file.error()); | 52 EXPECT_EQ(base::File::FILE_OK, file.error_details()); |
| 53 | 53 |
| 54 // This time verify closing the file. | 54 // This time verify closing the file. |
| 55 file.Close(); | 55 file.Close(); |
| 56 EXPECT_FALSE(file.IsValid()); | 56 EXPECT_FALSE(file.IsValid()); |
| 57 } | 57 } |
| 58 | 58 |
| 59 { | 59 { |
| 60 // Create a file that exists. | 60 // Create a file that exists. |
| 61 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); |
| 62 EXPECT_FALSE(file.IsValid()); | 62 EXPECT_FALSE(file.IsValid()); |
| 63 EXPECT_FALSE(file.created()); | 63 EXPECT_FALSE(file.created()); |
| 64 EXPECT_EQ(base::File::FILE_ERROR_EXISTS, file.error()); | 64 EXPECT_EQ(base::File::FILE_ERROR_EXISTS, file.error_details()); |
| 65 } | 65 } |
| 66 | 66 |
| 67 { | 67 { |
| 68 // Create or overwrite a file. | 68 // Create or overwrite a file. |
| 69 File file(file_path, | 69 File file(file_path, |
| 70 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_READ); | 70 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_READ); |
| 71 EXPECT_TRUE(file.IsValid()); | 71 EXPECT_TRUE(file.IsValid()); |
| 72 EXPECT_TRUE(file.created()); | 72 EXPECT_TRUE(file.created()); |
| 73 EXPECT_EQ(base::File::FILE_OK, file.error()); | 73 EXPECT_EQ(base::File::FILE_OK, file.error_details()); |
| 74 } | 74 } |
| 75 | 75 |
| 76 { | 76 { |
| 77 // Create a delete-on-close file. | 77 // Create a delete-on-close file. |
| 78 file_path = temp_dir.path().AppendASCII("create_file_2"); | 78 file_path = temp_dir.path().AppendASCII("create_file_2"); |
| 79 File file(file_path, | 79 File file(file_path, |
| 80 base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_READ | | 80 base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_READ | |
| 81 base::File::FLAG_DELETE_ON_CLOSE); | 81 base::File::FLAG_DELETE_ON_CLOSE); |
| 82 EXPECT_TRUE(file.IsValid()); | 82 EXPECT_TRUE(file.IsValid()); |
| 83 EXPECT_TRUE(file.created()); | 83 EXPECT_TRUE(file.created()); |
| 84 EXPECT_EQ(base::File::FILE_OK, file.error()); | 84 EXPECT_EQ(base::File::FILE_OK, file.error_details()); |
| 85 } | 85 } |
| 86 | 86 |
| 87 EXPECT_FALSE(base::PathExists(file_path)); | 87 EXPECT_FALSE(base::PathExists(file_path)); |
| 88 } | 88 } |
| 89 | 89 |
| 90 TEST(File, DeleteOpenFile) { | 90 TEST(File, DeleteOpenFile) { |
| 91 base::ScopedTempDir temp_dir; | 91 base::ScopedTempDir temp_dir; |
| 92 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 92 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 93 FilePath file_path = temp_dir.path().AppendASCII("create_file_1"); | 93 FilePath file_path = temp_dir.path().AppendASCII("create_file_1"); |
| 94 | 94 |
| 95 // Create a file. | 95 // Create a file. |
| 96 File file(file_path, | 96 File file(file_path, |
| 97 base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_READ | | 97 base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_READ | |
| 98 base::File::FLAG_SHARE_DELETE); | 98 base::File::FLAG_SHARE_DELETE); |
| 99 EXPECT_TRUE(file.IsValid()); | 99 EXPECT_TRUE(file.IsValid()); |
| 100 EXPECT_TRUE(file.created()); | 100 EXPECT_TRUE(file.created()); |
| 101 EXPECT_EQ(base::File::FILE_OK, file.error()); | 101 EXPECT_EQ(base::File::FILE_OK, file.error_details()); |
| 102 | 102 |
| 103 // Open an existing file and mark it as delete on close. | 103 // Open an existing file and mark it as delete on close. |
| 104 File same_file(file_path, | 104 File same_file(file_path, |
| 105 base::File::FLAG_OPEN | base::File::FLAG_DELETE_ON_CLOSE | | 105 base::File::FLAG_OPEN | base::File::FLAG_DELETE_ON_CLOSE | |
| 106 base::File::FLAG_READ); | 106 base::File::FLAG_READ); |
| 107 EXPECT_TRUE(file.IsValid()); | 107 EXPECT_TRUE(file.IsValid()); |
| 108 EXPECT_FALSE(same_file.created()); | 108 EXPECT_FALSE(same_file.created()); |
| 109 EXPECT_EQ(base::File::FILE_OK, same_file.error()); | 109 EXPECT_EQ(base::File::FILE_OK, same_file.error_details()); |
| 110 | 110 |
| 111 // Close both handles and check that the file is gone. | 111 // Close both handles and check that the file is gone. |
| 112 file.Close(); | 112 file.Close(); |
| 113 same_file.Close(); | 113 same_file.Close(); |
| 114 EXPECT_FALSE(base::PathExists(file_path)); | 114 EXPECT_FALSE(base::PathExists(file_path)); |
| 115 } | 115 } |
| 116 | 116 |
| 117 TEST(File, ReadWrite) { | 117 TEST(File, ReadWrite) { |
| 118 base::ScopedTempDir temp_dir; | 118 base::ScopedTempDir temp_dir; |
| 119 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 119 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| (...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 392 NULL)); | 392 NULL)); |
| 393 ASSERT_TRUE(dir.IsValid()); | 393 ASSERT_TRUE(dir.IsValid()); |
| 394 | 394 |
| 395 base::File::Info info; | 395 base::File::Info info; |
| 396 EXPECT_TRUE(dir.GetInfo(&info)); | 396 EXPECT_TRUE(dir.GetInfo(&info)); |
| 397 EXPECT_TRUE(info.is_directory); | 397 EXPECT_TRUE(info.is_directory); |
| 398 EXPECT_FALSE(info.is_symbolic_link); | 398 EXPECT_FALSE(info.is_symbolic_link); |
| 399 EXPECT_EQ(0, info.size); | 399 EXPECT_EQ(0, info.size); |
| 400 } | 400 } |
| 401 #endif // defined(OS_WIN) | 401 #endif // defined(OS_WIN) |
| OLD | NEW |