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