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 |
11 #include "base/macros.h" | 11 #include "base/macros.h" |
12 #include "components/filesystem/files_test_base.h" | 12 #include "components/filesystem/files_test_base.h" |
13 #include "mojo/common/common_type_converters.h" | 13 #include "mojo/common/common_type_converters.h" |
14 #include "mojo/util/capture_util.h" | 14 #include "mojo/util/capture_util.h" |
15 | 15 |
16 using mojo::Capture; | 16 using mojo::Capture; |
17 | 17 |
18 namespace filesystem { | 18 namespace filesystem { |
19 namespace { | 19 namespace { |
20 | 20 |
21 using DirectoryImplTest = FilesTestBase; | 21 using DirectoryImplTest = FilesTestBase; |
22 | 22 |
23 TEST_F(DirectoryImplTest, Read) { | 23 TEST_F(DirectoryImplTest, Read) { |
24 DirectoryPtr directory; | 24 mojom::DirectoryPtr directory; |
25 GetTemporaryRoot(&directory); | 25 GetTemporaryRoot(&directory); |
26 FileError error; | 26 mojom::FileError error; |
27 | 27 |
28 // Make some files. | 28 // Make some files. |
29 const struct { | 29 const struct { |
30 const char* name; | 30 const char* name; |
31 uint32_t open_flags; | 31 uint32_t open_flags; |
32 } files_to_create[] = { | 32 } files_to_create[] = { |
33 {"my_file1", kFlagRead | kFlagWrite | kFlagCreate}, | 33 {"my_file1", mojom::kFlagRead | mojom::kFlagWrite | mojom::kFlagCreate}, |
34 {"my_file2", kFlagWrite | kFlagCreate}, | 34 {"my_file2", mojom::kFlagWrite | mojom::kFlagCreate}, |
35 {"my_file3", kFlagAppend | kFlagCreate }}; | 35 {"my_file3", mojom::kFlagAppend | mojom::kFlagCreate}}; |
36 for (size_t i = 0; i < arraysize(files_to_create); i++) { | 36 for (size_t i = 0; i < arraysize(files_to_create); i++) { |
37 error = FileError::FAILED; | 37 error = mojom::FileError::FAILED; |
38 directory->OpenFile(files_to_create[i].name, nullptr, | 38 directory->OpenFile(files_to_create[i].name, nullptr, |
39 files_to_create[i].open_flags, Capture(&error)); | 39 files_to_create[i].open_flags, Capture(&error)); |
40 ASSERT_TRUE(directory.WaitForIncomingResponse()); | 40 ASSERT_TRUE(directory.WaitForIncomingResponse()); |
41 EXPECT_EQ(FileError::OK, error); | 41 EXPECT_EQ(mojom::FileError::OK, error); |
42 } | 42 } |
43 // Make a directory. | 43 // Make a directory. |
44 error = FileError::FAILED; | 44 error = mojom::FileError::FAILED; |
45 directory->OpenDirectory( | 45 directory->OpenDirectory( |
46 "my_dir", nullptr, kFlagRead | kFlagWrite | kFlagCreate, Capture(&error)); | 46 "my_dir", nullptr, |
| 47 mojom::kFlagRead | mojom::kFlagWrite | mojom::kFlagCreate, |
| 48 Capture(&error)); |
47 ASSERT_TRUE(directory.WaitForIncomingResponse()); | 49 ASSERT_TRUE(directory.WaitForIncomingResponse()); |
48 EXPECT_EQ(FileError::OK, error); | 50 EXPECT_EQ(mojom::FileError::OK, error); |
49 | 51 |
50 error = FileError::FAILED; | 52 error = mojom::FileError::FAILED; |
51 mojo::Array<DirectoryEntryPtr> directory_contents; | 53 mojo::Array<mojom::DirectoryEntryPtr> directory_contents; |
52 directory->Read(Capture(&error, &directory_contents)); | 54 directory->Read(Capture(&error, &directory_contents)); |
53 ASSERT_TRUE(directory.WaitForIncomingResponse()); | 55 ASSERT_TRUE(directory.WaitForIncomingResponse()); |
54 EXPECT_EQ(FileError::OK, error); | 56 EXPECT_EQ(mojom::FileError::OK, error); |
55 | 57 |
56 // Expected contents of the directory. | 58 // Expected contents of the directory. |
57 std::map<std::string, FsFileType> expected_contents; | 59 std::map<std::string, mojom::FsFileType> expected_contents; |
58 expected_contents["my_file1"] = FsFileType::REGULAR_FILE; | 60 expected_contents["my_file1"] = mojom::FsFileType::REGULAR_FILE; |
59 expected_contents["my_file2"] = FsFileType::REGULAR_FILE; | 61 expected_contents["my_file2"] = mojom::FsFileType::REGULAR_FILE; |
60 expected_contents["my_file3"] = FsFileType::REGULAR_FILE; | 62 expected_contents["my_file3"] = mojom::FsFileType::REGULAR_FILE; |
61 expected_contents["my_dir"] = FsFileType::DIRECTORY; | 63 expected_contents["my_dir"] = mojom::FsFileType::DIRECTORY; |
62 // Note: We don't expose ".." or ".". | 64 // Note: We don't expose ".." or ".". |
63 | 65 |
64 EXPECT_EQ(expected_contents.size(), directory_contents.size()); | 66 EXPECT_EQ(expected_contents.size(), directory_contents.size()); |
65 for (size_t i = 0; i < directory_contents.size(); i++) { | 67 for (size_t i = 0; i < directory_contents.size(); i++) { |
66 ASSERT_TRUE(directory_contents[i]); | 68 ASSERT_TRUE(directory_contents[i]); |
67 ASSERT_TRUE(directory_contents[i]->name); | 69 ASSERT_TRUE(directory_contents[i]->name); |
68 auto it = expected_contents.find(directory_contents[i]->name.get()); | 70 auto it = expected_contents.find(directory_contents[i]->name.get()); |
69 ASSERT_TRUE(it != expected_contents.end()); | 71 ASSERT_TRUE(it != expected_contents.end()); |
70 EXPECT_EQ(it->second, directory_contents[i]->type); | 72 EXPECT_EQ(it->second, directory_contents[i]->type); |
71 expected_contents.erase(it); | 73 expected_contents.erase(it); |
72 } | 74 } |
73 } | 75 } |
74 | 76 |
75 // TODO(vtl): Properly test OpenFile() and OpenDirectory() (including flags). | 77 // TODO(vtl): Properly test OpenFile() and OpenDirectory() (including flags). |
76 | 78 |
77 TEST_F(DirectoryImplTest, BasicRenameDelete) { | 79 TEST_F(DirectoryImplTest, BasicRenameDelete) { |
78 DirectoryPtr directory; | 80 mojom::DirectoryPtr directory; |
79 GetTemporaryRoot(&directory); | 81 GetTemporaryRoot(&directory); |
80 FileError error; | 82 mojom::FileError error; |
81 | 83 |
82 // Create my_file. | 84 // Create my_file. |
83 error = FileError::FAILED; | 85 error = mojom::FileError::FAILED; |
84 directory->OpenFile("my_file", nullptr, kFlagWrite | kFlagCreate, | 86 directory->OpenFile("my_file", nullptr, |
| 87 mojom::kFlagWrite | mojom::kFlagCreate, Capture(&error)); |
| 88 ASSERT_TRUE(directory.WaitForIncomingResponse()); |
| 89 EXPECT_EQ(mojom::FileError::OK, error); |
| 90 |
| 91 // Opening my_file should succeed. |
| 92 error = mojom::FileError::FAILED; |
| 93 directory->OpenFile("my_file", nullptr, mojom::kFlagRead | mojom::kFlagOpen, |
85 Capture(&error)); | 94 Capture(&error)); |
86 ASSERT_TRUE(directory.WaitForIncomingResponse()); | 95 ASSERT_TRUE(directory.WaitForIncomingResponse()); |
87 EXPECT_EQ(FileError::OK, error); | 96 EXPECT_EQ(mojom::FileError::OK, error); |
88 | |
89 // Opening my_file should succeed. | |
90 error = FileError::FAILED; | |
91 directory->OpenFile("my_file", nullptr, kFlagRead | kFlagOpen, | |
92 Capture(&error)); | |
93 ASSERT_TRUE(directory.WaitForIncomingResponse()); | |
94 EXPECT_EQ(FileError::OK, error); | |
95 | 97 |
96 // Rename my_file to my_new_file. | 98 // Rename my_file to my_new_file. |
97 directory->Rename("my_file", "my_new_file", Capture(&error)); | 99 directory->Rename("my_file", "my_new_file", Capture(&error)); |
98 ASSERT_TRUE(directory.WaitForIncomingResponse()); | 100 ASSERT_TRUE(directory.WaitForIncomingResponse()); |
99 EXPECT_EQ(FileError::OK, error); | 101 EXPECT_EQ(mojom::FileError::OK, error); |
100 | 102 |
101 // Opening my_file should fail. | 103 // Opening my_file should fail. |
102 | 104 |
103 error = FileError::FAILED; | 105 error = mojom::FileError::FAILED; |
104 directory->OpenFile("my_file", nullptr, kFlagRead | kFlagOpen, | 106 directory->OpenFile("my_file", nullptr, mojom::kFlagRead | mojom::kFlagOpen, |
105 Capture(&error)); | 107 Capture(&error)); |
106 ASSERT_TRUE(directory.WaitForIncomingResponse()); | 108 ASSERT_TRUE(directory.WaitForIncomingResponse()); |
107 EXPECT_EQ(FileError::NOT_FOUND, error); | 109 EXPECT_EQ(mojom::FileError::NOT_FOUND, error); |
108 | 110 |
109 // Opening my_new_file should succeed. | 111 // Opening my_new_file should succeed. |
110 error = FileError::FAILED; | 112 error = mojom::FileError::FAILED; |
111 directory->OpenFile("my_new_file", nullptr, kFlagRead | kFlagOpen, | 113 directory->OpenFile("my_new_file", nullptr, |
112 Capture(&error)); | 114 mojom::kFlagRead | mojom::kFlagOpen, Capture(&error)); |
113 ASSERT_TRUE(directory.WaitForIncomingResponse()); | 115 ASSERT_TRUE(directory.WaitForIncomingResponse()); |
114 EXPECT_EQ(FileError::OK, error); | 116 EXPECT_EQ(mojom::FileError::OK, error); |
115 | 117 |
116 // Delete my_new_file (no flags). | 118 // Delete my_new_file (no flags). |
117 directory->Delete("my_new_file", 0, Capture(&error)); | 119 directory->Delete("my_new_file", 0, Capture(&error)); |
118 ASSERT_TRUE(directory.WaitForIncomingResponse()); | 120 ASSERT_TRUE(directory.WaitForIncomingResponse()); |
119 EXPECT_EQ(FileError::OK, error); | 121 EXPECT_EQ(mojom::FileError::OK, error); |
120 | 122 |
121 // Opening my_new_file should fail. | 123 // Opening my_new_file should fail. |
122 error = FileError::FAILED; | 124 error = mojom::FileError::FAILED; |
123 directory->OpenFile("my_new_file", nullptr, kFlagRead | kFlagOpen, | 125 directory->OpenFile("my_new_file", nullptr, |
124 Capture(&error)); | 126 mojom::kFlagRead | mojom::kFlagOpen, Capture(&error)); |
125 ASSERT_TRUE(directory.WaitForIncomingResponse()); | 127 ASSERT_TRUE(directory.WaitForIncomingResponse()); |
126 EXPECT_EQ(FileError::NOT_FOUND, error); | 128 EXPECT_EQ(mojom::FileError::NOT_FOUND, error); |
127 } | 129 } |
128 | 130 |
129 TEST_F(DirectoryImplTest, CantOpenDirectoriesAsFiles) { | 131 TEST_F(DirectoryImplTest, CantOpenDirectoriesAsFiles) { |
130 DirectoryPtr directory; | 132 mojom::DirectoryPtr directory; |
131 GetTemporaryRoot(&directory); | 133 GetTemporaryRoot(&directory); |
132 FileError error; | 134 mojom::FileError error; |
133 | 135 |
134 { | 136 { |
135 // Create a directory called 'my_file' | 137 // Create a directory called 'my_file' |
136 DirectoryPtr my_file_directory; | 138 mojom::DirectoryPtr my_file_directory; |
137 error = FileError::FAILED; | 139 error = mojom::FileError::FAILED; |
138 directory->OpenDirectory( | 140 directory->OpenDirectory( |
139 "my_file", GetProxy(&my_file_directory), | 141 "my_file", GetProxy(&my_file_directory), |
140 kFlagRead | kFlagWrite | kFlagCreate, | 142 mojom::kFlagRead | mojom::kFlagWrite | mojom::kFlagCreate, |
141 Capture(&error)); | 143 Capture(&error)); |
142 ASSERT_TRUE(directory.WaitForIncomingResponse()); | 144 ASSERT_TRUE(directory.WaitForIncomingResponse()); |
143 EXPECT_EQ(FileError::OK, error); | 145 EXPECT_EQ(mojom::FileError::OK, error); |
144 } | 146 } |
145 | 147 |
146 { | 148 { |
147 // Attempt to open that directory as a file. This must fail! | 149 // Attempt to open that directory as a file. This must fail! |
148 FilePtr file; | 150 mojom::FilePtr file; |
149 error = FileError::FAILED; | 151 error = mojom::FileError::FAILED; |
150 directory->OpenFile("my_file", GetProxy(&file), kFlagRead | kFlagOpen, | 152 directory->OpenFile("my_file", GetProxy(&file), |
151 Capture(&error)); | 153 mojom::kFlagRead | mojom::kFlagOpen, Capture(&error)); |
152 ASSERT_TRUE(directory.WaitForIncomingResponse()); | 154 ASSERT_TRUE(directory.WaitForIncomingResponse()); |
153 EXPECT_EQ(FileError::NOT_A_FILE, error); | 155 EXPECT_EQ(mojom::FileError::NOT_A_FILE, error); |
154 } | 156 } |
155 } | 157 } |
156 | 158 |
157 TEST_F(DirectoryImplTest, Clone) { | 159 TEST_F(DirectoryImplTest, Clone) { |
158 DirectoryPtr clone_one; | 160 mojom::DirectoryPtr clone_one; |
159 DirectoryPtr clone_two; | 161 mojom::DirectoryPtr clone_two; |
160 FileError error; | 162 mojom::FileError error; |
161 | 163 |
162 { | 164 { |
163 DirectoryPtr directory; | 165 mojom::DirectoryPtr directory; |
164 GetTemporaryRoot(&directory); | 166 GetTemporaryRoot(&directory); |
165 | 167 |
166 directory->Clone(GetProxy(&clone_one)); | 168 directory->Clone(GetProxy(&clone_one)); |
167 directory->Clone(GetProxy(&clone_two)); | 169 directory->Clone(GetProxy(&clone_two)); |
168 | 170 |
169 // Original temporary directory goes out of scope here; shouldn't be | 171 // Original temporary directory goes out of scope here; shouldn't be |
170 // deleted since it has clones. | 172 // deleted since it has clones. |
171 } | 173 } |
172 | 174 |
173 std::string data("one two three"); | 175 std::string data("one two three"); |
174 { | 176 { |
175 clone_one->WriteFile("data", mojo::Array<uint8_t>::From(data), | 177 clone_one->WriteFile("data", mojo::Array<uint8_t>::From(data), |
176 Capture(&error)); | 178 Capture(&error)); |
177 ASSERT_TRUE(clone_one.WaitForIncomingResponse()); | 179 ASSERT_TRUE(clone_one.WaitForIncomingResponse()); |
178 EXPECT_EQ(FileError::OK, error); | 180 EXPECT_EQ(mojom::FileError::OK, error); |
179 } | 181 } |
180 | 182 |
181 { | 183 { |
182 mojo::Array<uint8_t> file_contents; | 184 mojo::Array<uint8_t> file_contents; |
183 clone_two->ReadEntireFile("data", Capture(&error, &file_contents)); | 185 clone_two->ReadEntireFile("data", Capture(&error, &file_contents)); |
184 ASSERT_TRUE(clone_two.WaitForIncomingResponse()); | 186 ASSERT_TRUE(clone_two.WaitForIncomingResponse()); |
185 EXPECT_EQ(FileError::OK, error); | 187 EXPECT_EQ(mojom::FileError::OK, error); |
186 | 188 |
187 EXPECT_EQ(data, file_contents.To<std::string>()); | 189 EXPECT_EQ(data, file_contents.To<std::string>()); |
188 } | 190 } |
189 } | 191 } |
190 | 192 |
191 TEST_F(DirectoryImplTest, WriteFileReadFile) { | 193 TEST_F(DirectoryImplTest, WriteFileReadFile) { |
192 DirectoryPtr directory; | 194 mojom::DirectoryPtr directory; |
193 GetTemporaryRoot(&directory); | 195 GetTemporaryRoot(&directory); |
194 FileError error; | 196 mojom::FileError error; |
195 | 197 |
196 std::string data("one two three"); | 198 std::string data("one two three"); |
197 { | 199 { |
198 directory->WriteFile("data", mojo::Array<uint8_t>::From(data), | 200 directory->WriteFile("data", mojo::Array<uint8_t>::From(data), |
199 Capture(&error)); | 201 Capture(&error)); |
200 ASSERT_TRUE(directory.WaitForIncomingResponse()); | 202 ASSERT_TRUE(directory.WaitForIncomingResponse()); |
201 EXPECT_EQ(FileError::OK, error); | 203 EXPECT_EQ(mojom::FileError::OK, error); |
202 } | 204 } |
203 | 205 |
204 { | 206 { |
205 mojo::Array<uint8_t> file_contents; | 207 mojo::Array<uint8_t> file_contents; |
206 directory->ReadEntireFile("data", Capture(&error, &file_contents)); | 208 directory->ReadEntireFile("data", Capture(&error, &file_contents)); |
207 ASSERT_TRUE(directory.WaitForIncomingResponse()); | 209 ASSERT_TRUE(directory.WaitForIncomingResponse()); |
208 EXPECT_EQ(FileError::OK, error); | 210 EXPECT_EQ(mojom::FileError::OK, error); |
209 | 211 |
210 EXPECT_EQ(data, file_contents.To<std::string>()); | 212 EXPECT_EQ(data, file_contents.To<std::string>()); |
211 } | 213 } |
212 } | 214 } |
213 | 215 |
214 TEST_F(DirectoryImplTest, ReadEmptyFileIsNotFoundError) { | 216 TEST_F(DirectoryImplTest, ReadEmptyFileIsNotFoundError) { |
215 DirectoryPtr directory; | 217 mojom::DirectoryPtr directory; |
216 GetTemporaryRoot(&directory); | 218 GetTemporaryRoot(&directory); |
217 FileError error; | 219 mojom::FileError error; |
218 | 220 |
219 { | 221 { |
220 mojo::Array<uint8_t> file_contents; | 222 mojo::Array<uint8_t> file_contents; |
221 directory->ReadEntireFile("doesnt_exist", Capture(&error, &file_contents)); | 223 directory->ReadEntireFile("doesnt_exist", Capture(&error, &file_contents)); |
222 ASSERT_TRUE(directory.WaitForIncomingResponse()); | 224 ASSERT_TRUE(directory.WaitForIncomingResponse()); |
223 EXPECT_EQ(FileError::NOT_FOUND, error); | 225 EXPECT_EQ(mojom::FileError::NOT_FOUND, error); |
224 } | 226 } |
225 } | 227 } |
226 | 228 |
227 TEST_F(DirectoryImplTest, CantReadEntireFileOnADirectory) { | 229 TEST_F(DirectoryImplTest, CantReadEntireFileOnADirectory) { |
228 DirectoryPtr directory; | 230 mojom::DirectoryPtr directory; |
229 GetTemporaryRoot(&directory); | 231 GetTemporaryRoot(&directory); |
230 FileError error; | 232 mojom::FileError error; |
231 | 233 |
232 // Create a directory | 234 // Create a directory |
233 { | 235 { |
234 DirectoryPtr my_file_directory; | 236 mojom::DirectoryPtr my_file_directory; |
235 error = FileError::FAILED; | 237 error = mojom::FileError::FAILED; |
236 directory->OpenDirectory( | 238 directory->OpenDirectory( |
237 "my_dir", GetProxy(&my_file_directory), | 239 "my_dir", GetProxy(&my_file_directory), |
238 kFlagRead | kFlagWrite | kFlagCreate, | 240 mojom::kFlagRead | mojom::kFlagWrite | mojom::kFlagCreate, |
239 Capture(&error)); | 241 Capture(&error)); |
240 ASSERT_TRUE(directory.WaitForIncomingResponse()); | 242 ASSERT_TRUE(directory.WaitForIncomingResponse()); |
241 EXPECT_EQ(FileError::OK, error); | 243 EXPECT_EQ(mojom::FileError::OK, error); |
242 } | 244 } |
243 | 245 |
244 // Try to read it as a file | 246 // Try to read it as a file |
245 { | 247 { |
246 mojo::Array<uint8_t> file_contents; | 248 mojo::Array<uint8_t> file_contents; |
247 directory->ReadEntireFile("my_dir", Capture(&error, &file_contents)); | 249 directory->ReadEntireFile("my_dir", Capture(&error, &file_contents)); |
248 ASSERT_TRUE(directory.WaitForIncomingResponse()); | 250 ASSERT_TRUE(directory.WaitForIncomingResponse()); |
249 EXPECT_EQ(FileError::NOT_A_FILE, error); | 251 EXPECT_EQ(mojom::FileError::NOT_A_FILE, error); |
250 } | 252 } |
251 } | 253 } |
252 | 254 |
253 TEST_F(DirectoryImplTest, CantWriteFileOnADirectory) { | 255 TEST_F(DirectoryImplTest, CantWriteFileOnADirectory) { |
254 DirectoryPtr directory; | 256 mojom::DirectoryPtr directory; |
255 GetTemporaryRoot(&directory); | 257 GetTemporaryRoot(&directory); |
256 FileError error; | 258 mojom::FileError error; |
257 | 259 |
258 // Create a directory | 260 // Create a directory |
259 { | 261 { |
260 DirectoryPtr my_file_directory; | 262 mojom::DirectoryPtr my_file_directory; |
261 error = FileError::FAILED; | 263 error = mojom::FileError::FAILED; |
262 directory->OpenDirectory( | 264 directory->OpenDirectory( |
263 "my_dir", GetProxy(&my_file_directory), | 265 "my_dir", GetProxy(&my_file_directory), |
264 kFlagRead | kFlagWrite | kFlagCreate, | 266 mojom::kFlagRead | mojom::kFlagWrite | mojom::kFlagCreate, |
265 Capture(&error)); | 267 Capture(&error)); |
266 ASSERT_TRUE(directory.WaitForIncomingResponse()); | 268 ASSERT_TRUE(directory.WaitForIncomingResponse()); |
267 EXPECT_EQ(FileError::OK, error); | 269 EXPECT_EQ(mojom::FileError::OK, error); |
268 } | 270 } |
269 | 271 |
270 { | 272 { |
271 std::string data("one two three"); | 273 std::string data("one two three"); |
272 directory->WriteFile("my_dir", mojo::Array<uint8_t>::From(data), | 274 directory->WriteFile("my_dir", mojo::Array<uint8_t>::From(data), |
273 Capture(&error)); | 275 Capture(&error)); |
274 ASSERT_TRUE(directory.WaitForIncomingResponse()); | 276 ASSERT_TRUE(directory.WaitForIncomingResponse()); |
275 EXPECT_EQ(FileError::NOT_A_FILE, error); | 277 EXPECT_EQ(mojom::FileError::NOT_A_FILE, error); |
276 } | 278 } |
277 } | 279 } |
278 | 280 |
279 // TODO(vtl): Test delete flags. | 281 // TODO(vtl): Test delete flags. |
280 | 282 |
281 } // namespace | 283 } // namespace |
282 } // namespace filesystem | 284 } // namespace filesystem |
OLD | NEW |