| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 <map> | 5 #include <map> |
| 6 #include <string> | 6 #include <string> |
| 7 | 7 |
| 8 #include "components/filesystem/files_test_base.h" | 8 #include "components/filesystem/files_test_base.h" |
| 9 | 9 |
| 10 namespace filesystem { | 10 namespace filesystem { |
| 11 namespace { | 11 namespace { |
| 12 | 12 |
| 13 using DirectoryImplTest = FilesTestBase; | 13 using DirectoryImplTest = FilesTestBase; |
| 14 | 14 |
| 15 TEST_F(DirectoryImplTest, Read) { | 15 TEST_F(DirectoryImplTest, Read) { |
| 16 DirectoryPtr directory; | 16 DirectoryPtr directory; |
| 17 GetTemporaryRoot(&directory); | 17 GetTemporaryRoot(&directory); |
| 18 FileError error; | 18 Error error; |
| 19 | 19 |
| 20 // Make some files. | 20 // Make some files. |
| 21 const struct { | 21 const struct { |
| 22 const char* name; | 22 const char* name; |
| 23 uint32_t open_flags; | 23 uint32_t open_flags; |
| 24 } files_to_create[] = { | 24 } files_to_create[] = { |
| 25 {"my_file1", kFlagRead | kFlagWrite | kFlagCreate}, | 25 {"my_file1", kFlagRead | kFlagWrite | kFlagCreate}, |
| 26 {"my_file2", kFlagWrite | kFlagCreate | kFlagOpenAlways}, | 26 {"my_file2", kFlagWrite | kFlagCreate | kFlagOpenAlways}, |
| 27 {"my_file3", kFlagWrite | kFlagCreate | kFlagAppend}, | 27 {"my_file3", kFlagWrite | kFlagCreate | kFlagAppend}, |
| 28 {"my_file4", kFlagWrite | kFlagCreate}}; | 28 {"my_file4", kFlagWrite | kFlagCreate}}; |
| 29 for (size_t i = 0; i < arraysize(files_to_create); i++) { | 29 for (size_t i = 0; i < arraysize(files_to_create); i++) { |
| 30 error = FILE_ERROR_FAILED; | 30 error = ERROR_FAILED; |
| 31 directory->OpenFile(files_to_create[i].name, nullptr, | 31 directory->OpenFile(files_to_create[i].name, nullptr, |
| 32 files_to_create[i].open_flags, Capture(&error)); | 32 files_to_create[i].open_flags, Capture(&error)); |
| 33 ASSERT_TRUE(directory.WaitForIncomingResponse()); | 33 ASSERT_TRUE(directory.WaitForIncomingResponse()); |
| 34 EXPECT_EQ(FILE_ERROR_OK, error); | 34 EXPECT_EQ(ERROR_OK, error); |
| 35 } | 35 } |
| 36 // Make a directory. | 36 // Make a directory. |
| 37 error = FILE_ERROR_FAILED; | 37 error = ERROR_FAILED; |
| 38 directory->OpenDirectory( | 38 directory->OpenDirectory( |
| 39 "my_dir", nullptr, kFlagRead | kFlagWrite | kFlagCreate, Capture(&error)); | 39 "my_dir", nullptr, kFlagRead | kFlagWrite | kFlagCreate, Capture(&error)); |
| 40 ASSERT_TRUE(directory.WaitForIncomingResponse()); | 40 ASSERT_TRUE(directory.WaitForIncomingResponse()); |
| 41 EXPECT_EQ(FILE_ERROR_OK, error); | 41 EXPECT_EQ(ERROR_OK, error); |
| 42 | 42 |
| 43 error = FILE_ERROR_FAILED; | 43 error = ERROR_FAILED; |
| 44 mojo::Array<DirectoryEntryPtr> directory_contents; | 44 mojo::Array<DirectoryEntryPtr> directory_contents; |
| 45 directory->Read(Capture(&error, &directory_contents)); | 45 directory->Read(Capture(&error, &directory_contents)); |
| 46 ASSERT_TRUE(directory.WaitForIncomingResponse()); | 46 ASSERT_TRUE(directory.WaitForIncomingResponse()); |
| 47 EXPECT_EQ(FILE_ERROR_OK, error); | 47 EXPECT_EQ(ERROR_OK, error); |
| 48 | 48 |
| 49 // Expected contents of the directory. | 49 // Expected contents of the directory. |
| 50 std::map<std::string, FsFileType> expected_contents; | 50 std::map<std::string, FileType> expected_contents; |
| 51 expected_contents["my_file1"] = FS_FILE_TYPE_REGULAR_FILE; | 51 expected_contents["my_file1"] = FILE_TYPE_REGULAR_FILE; |
| 52 expected_contents["my_file2"] = FS_FILE_TYPE_REGULAR_FILE; | 52 expected_contents["my_file2"] = FILE_TYPE_REGULAR_FILE; |
| 53 expected_contents["my_file3"] = FS_FILE_TYPE_REGULAR_FILE; | 53 expected_contents["my_file3"] = FILE_TYPE_REGULAR_FILE; |
| 54 expected_contents["my_file4"] = FS_FILE_TYPE_REGULAR_FILE; | 54 expected_contents["my_file4"] = FILE_TYPE_REGULAR_FILE; |
| 55 expected_contents["my_dir"] = FS_FILE_TYPE_DIRECTORY; | 55 expected_contents["my_dir"] = FILE_TYPE_DIRECTORY; |
| 56 // Note: We don't expose ".." or ".". | 56 // Note: We don't expose ".." or ".". |
| 57 | 57 |
| 58 EXPECT_EQ(expected_contents.size(), directory_contents.size()); | 58 EXPECT_EQ(expected_contents.size(), directory_contents.size()); |
| 59 for (size_t i = 0; i < directory_contents.size(); i++) { | 59 for (size_t i = 0; i < directory_contents.size(); i++) { |
| 60 ASSERT_TRUE(directory_contents[i]); | 60 ASSERT_TRUE(directory_contents[i]); |
| 61 ASSERT_TRUE(directory_contents[i]->name); | 61 ASSERT_TRUE(directory_contents[i]->name); |
| 62 auto it = expected_contents.find(directory_contents[i]->name.get()); | 62 auto it = expected_contents.find(directory_contents[i]->name.get()); |
| 63 ASSERT_TRUE(it != expected_contents.end()); | 63 ASSERT_TRUE(it != expected_contents.end()); |
| 64 EXPECT_EQ(it->second, directory_contents[i]->type); | 64 EXPECT_EQ(it->second, directory_contents[i]->type); |
| 65 expected_contents.erase(it); | 65 expected_contents.erase(it); |
| 66 } | 66 } |
| 67 } | 67 } |
| 68 | 68 |
| 69 // TODO(vtl): Properly test OpenFile() and OpenDirectory() (including flags). | 69 // TODO(vtl): Properly test OpenFile() and OpenDirectory() (including flags). |
| 70 | 70 |
| 71 TEST_F(DirectoryImplTest, BasicRenameDelete) { | 71 TEST_F(DirectoryImplTest, BasicRenameDelete) { |
| 72 DirectoryPtr directory; | 72 DirectoryPtr directory; |
| 73 GetTemporaryRoot(&directory); | 73 GetTemporaryRoot(&directory); |
| 74 FileError error; | 74 Error error; |
| 75 | 75 |
| 76 // Create my_file. | 76 // Create my_file. |
| 77 error = FILE_ERROR_FAILED; | 77 error = ERROR_FAILED; |
| 78 directory->OpenFile("my_file", nullptr, kFlagWrite | kFlagCreate, | 78 directory->OpenFile("my_file", nullptr, kFlagWrite | kFlagCreate, |
| 79 Capture(&error)); | 79 Capture(&error)); |
| 80 ASSERT_TRUE(directory.WaitForIncomingResponse()); | 80 ASSERT_TRUE(directory.WaitForIncomingResponse()); |
| 81 EXPECT_EQ(FILE_ERROR_OK, error); | 81 EXPECT_EQ(ERROR_OK, error); |
| 82 | 82 |
| 83 // Opening my_file should succeed. | 83 // Opening my_file should succeed. |
| 84 error = FILE_ERROR_FAILED; | 84 error = ERROR_FAILED; |
| 85 directory->OpenFile("my_file", nullptr, kFlagRead | kFlagOpen, | 85 directory->OpenFile("my_file", nullptr, kFlagRead | kFlagOpen, |
| 86 Capture(&error)); | 86 Capture(&error)); |
| 87 ASSERT_TRUE(directory.WaitForIncomingResponse()); | 87 ASSERT_TRUE(directory.WaitForIncomingResponse()); |
| 88 EXPECT_EQ(FILE_ERROR_OK, error); | 88 EXPECT_EQ(ERROR_OK, error); |
| 89 | 89 |
| 90 // Rename my_file to my_new_file. | 90 // Rename my_file to my_new_file. |
| 91 directory->Rename("my_file", "my_new_file", Capture(&error)); | 91 directory->Rename("my_file", "my_new_file", Capture(&error)); |
| 92 ASSERT_TRUE(directory.WaitForIncomingResponse()); | 92 ASSERT_TRUE(directory.WaitForIncomingResponse()); |
| 93 EXPECT_EQ(FILE_ERROR_OK, error); | 93 EXPECT_EQ(ERROR_OK, error); |
| 94 | 94 |
| 95 // Opening my_file should fail. | 95 // Opening my_file should fail. |
| 96 | 96 |
| 97 error = FILE_ERROR_FAILED; | 97 error = ERROR_FAILED; |
| 98 directory->OpenFile("my_file", nullptr, kFlagRead | kFlagOpen, | 98 directory->OpenFile("my_file", nullptr, kFlagRead | kFlagOpen, |
| 99 Capture(&error)); | 99 Capture(&error)); |
| 100 ASSERT_TRUE(directory.WaitForIncomingResponse()); | 100 ASSERT_TRUE(directory.WaitForIncomingResponse()); |
| 101 EXPECT_EQ(FILE_ERROR_FAILED, error); | 101 EXPECT_EQ(ERROR_FAILED, error); |
| 102 | 102 |
| 103 // Opening my_new_file should succeed. | 103 // Opening my_new_file should succeed. |
| 104 error = FILE_ERROR_FAILED; | 104 error = ERROR_FAILED; |
| 105 directory->OpenFile("my_new_file", nullptr, kFlagRead | kFlagOpen, | 105 directory->OpenFile("my_new_file", nullptr, kFlagRead | kFlagOpen, |
| 106 Capture(&error)); | 106 Capture(&error)); |
| 107 ASSERT_TRUE(directory.WaitForIncomingResponse()); | 107 ASSERT_TRUE(directory.WaitForIncomingResponse()); |
| 108 EXPECT_EQ(FILE_ERROR_OK, error); | 108 EXPECT_EQ(ERROR_OK, error); |
| 109 | 109 |
| 110 // Delete my_new_file (no flags). | 110 // Delete my_new_file (no flags). |
| 111 directory->Delete("my_new_file", 0, Capture(&error)); | 111 directory->Delete("my_new_file", 0, Capture(&error)); |
| 112 ASSERT_TRUE(directory.WaitForIncomingResponse()); | 112 ASSERT_TRUE(directory.WaitForIncomingResponse()); |
| 113 EXPECT_EQ(FILE_ERROR_OK, error); | 113 EXPECT_EQ(ERROR_OK, error); |
| 114 | 114 |
| 115 // Opening my_new_file should fail. | 115 // Opening my_new_file should fail. |
| 116 error = FILE_ERROR_FAILED; | 116 error = ERROR_FAILED; |
| 117 directory->OpenFile("my_new_file", nullptr, kFlagRead | kFlagOpen, | 117 directory->OpenFile("my_new_file", nullptr, kFlagRead | kFlagOpen, |
| 118 Capture(&error)); | 118 Capture(&error)); |
| 119 ASSERT_TRUE(directory.WaitForIncomingResponse()); | 119 ASSERT_TRUE(directory.WaitForIncomingResponse()); |
| 120 EXPECT_EQ(FILE_ERROR_FAILED, error); | 120 EXPECT_EQ(ERROR_FAILED, error); |
| 121 } | 121 } |
| 122 | 122 |
| 123 TEST_F(DirectoryImplTest, CantOpenDirectoriesAsFiles) { | 123 TEST_F(DirectoryImplTest, CantOpenDirectoriesAsFiles) { |
| 124 DirectoryPtr directory; | 124 DirectoryPtr directory; |
| 125 GetTemporaryRoot(&directory); | 125 GetTemporaryRoot(&directory); |
| 126 FileError error; | 126 Error error; |
| 127 | 127 |
| 128 { | 128 { |
| 129 // Create a directory called 'my_file' | 129 // Create a directory called 'my_file' |
| 130 DirectoryPtr my_file_directory; | 130 DirectoryPtr my_file_directory; |
| 131 error = FILE_ERROR_FAILED; | 131 error = ERROR_FAILED; |
| 132 directory->OpenDirectory( | 132 directory->OpenDirectory( |
| 133 "my_file", GetProxy(&my_file_directory), | 133 "my_file", GetProxy(&my_file_directory), |
| 134 kFlagRead | kFlagWrite | kFlagCreate, | 134 kFlagRead | kFlagWrite | kFlagCreate, |
| 135 Capture(&error)); | 135 Capture(&error)); |
| 136 ASSERT_TRUE(directory.WaitForIncomingResponse()); | 136 ASSERT_TRUE(directory.WaitForIncomingResponse()); |
| 137 EXPECT_EQ(FILE_ERROR_OK, error); | 137 EXPECT_EQ(ERROR_OK, error); |
| 138 } | 138 } |
| 139 | 139 |
| 140 { | 140 { |
| 141 // Attempt to open that directory as a file. This must fail! | 141 // Attempt to open that directory as a file. This must fail! |
| 142 FilePtr file; | 142 FilePtr file; |
| 143 error = FILE_ERROR_FAILED; | 143 error = ERROR_FAILED; |
| 144 directory->OpenFile("my_file", GetProxy(&file), kFlagRead | kFlagOpen, | 144 directory->OpenFile("my_file", GetProxy(&file), kFlagRead | kFlagOpen, |
| 145 Capture(&error)); | 145 Capture(&error)); |
| 146 ASSERT_TRUE(directory.WaitForIncomingResponse()); | 146 ASSERT_TRUE(directory.WaitForIncomingResponse()); |
| 147 EXPECT_EQ(FILE_ERROR_NOT_A_FILE, error); | 147 EXPECT_EQ(ERROR_NOT_A_FILE, error); |
| 148 } | 148 } |
| 149 } | 149 } |
| 150 | 150 |
| 151 | 151 |
| 152 // TODO(vtl): Test delete flags. | 152 // TODO(vtl): Test delete flags. |
| 153 | 153 |
| 154 } // namespace | 154 } // namespace |
| 155 } // namespace filesystem | 155 } // namespace filesystem |
| OLD | NEW |