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" | |
14 | 13 |
15 namespace filesystem { | 14 namespace filesystem { |
16 namespace { | 15 namespace { |
17 | 16 |
18 using DirectoryImplTest = FilesTestBase; | 17 using DirectoryImplTest = FilesTestBase; |
19 | 18 |
| 19 constexpr char kData[] = "one two three"; |
| 20 |
20 TEST_F(DirectoryImplTest, Read) { | 21 TEST_F(DirectoryImplTest, Read) { |
21 mojom::DirectoryPtr directory; | 22 mojom::DirectoryPtr directory; |
22 GetTemporaryRoot(&directory); | 23 GetTemporaryRoot(&directory); |
23 mojom::FileError error; | 24 mojom::FileError error; |
24 | 25 |
25 // Make some files. | 26 // Make some files. |
26 const struct { | 27 const struct { |
27 const char* name; | 28 const char* name; |
28 uint32_t open_flags; | 29 uint32_t open_flags; |
29 } files_to_create[] = { | 30 } files_to_create[] = { |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
162 mojom::DirectoryPtr directory; | 163 mojom::DirectoryPtr directory; |
163 GetTemporaryRoot(&directory); | 164 GetTemporaryRoot(&directory); |
164 | 165 |
165 directory->Clone(MakeRequest(&clone_one)); | 166 directory->Clone(MakeRequest(&clone_one)); |
166 directory->Clone(MakeRequest(&clone_two)); | 167 directory->Clone(MakeRequest(&clone_two)); |
167 | 168 |
168 // Original temporary directory goes out of scope here; shouldn't be | 169 // Original temporary directory goes out of scope here; shouldn't be |
169 // deleted since it has clones. | 170 // deleted since it has clones. |
170 } | 171 } |
171 | 172 |
172 std::string data("one two three"); | 173 std::vector<uint8_t> data(kData, kData + strlen(kData)); |
173 { | 174 { |
174 bool handled = | 175 bool handled = clone_one->WriteFile("data", data, &error); |
175 clone_one->WriteFile("data", mojo::Array<uint8_t>::From(data), &error); | |
176 ASSERT_TRUE(handled); | 176 ASSERT_TRUE(handled); |
177 EXPECT_EQ(mojom::FileError::OK, error); | 177 EXPECT_EQ(mojom::FileError::OK, error); |
178 } | 178 } |
179 | 179 |
180 { | 180 { |
181 std::vector<uint8_t> file_contents; | 181 std::vector<uint8_t> file_contents; |
182 bool handled = clone_two->ReadEntireFile("data", &error, &file_contents); | 182 bool handled = clone_two->ReadEntireFile("data", &error, &file_contents); |
183 ASSERT_TRUE(handled); | 183 ASSERT_TRUE(handled); |
184 EXPECT_EQ(mojom::FileError::OK, error); | 184 EXPECT_EQ(mojom::FileError::OK, error); |
185 | 185 |
186 EXPECT_EQ(data, | 186 EXPECT_EQ(data, file_contents); |
187 mojo::Array<uint8_t>(std::move(file_contents)).To<std::string>()); | |
188 } | 187 } |
189 } | 188 } |
190 | 189 |
191 TEST_F(DirectoryImplTest, WriteFileReadFile) { | 190 TEST_F(DirectoryImplTest, WriteFileReadFile) { |
192 mojom::DirectoryPtr directory; | 191 mojom::DirectoryPtr directory; |
193 GetTemporaryRoot(&directory); | 192 GetTemporaryRoot(&directory); |
194 mojom::FileError error; | 193 mojom::FileError error; |
195 | 194 |
196 std::string data("one two three"); | 195 std::vector<uint8_t> data(kData, kData + strlen(kData)); |
197 { | 196 { |
198 bool handled = | 197 bool handled = directory->WriteFile("data", data, &error); |
199 directory->WriteFile("data", mojo::Array<uint8_t>::From(data), &error); | |
200 ASSERT_TRUE(handled); | 198 ASSERT_TRUE(handled); |
201 EXPECT_EQ(mojom::FileError::OK, error); | 199 EXPECT_EQ(mojom::FileError::OK, error); |
202 } | 200 } |
203 | 201 |
204 { | 202 { |
205 std::vector<uint8_t> file_contents; | 203 std::vector<uint8_t> file_contents; |
206 bool handled = directory->ReadEntireFile("data", &error, &file_contents); | 204 bool handled = directory->ReadEntireFile("data", &error, &file_contents); |
207 ASSERT_TRUE(handled); | 205 ASSERT_TRUE(handled); |
208 EXPECT_EQ(mojom::FileError::OK, error); | 206 EXPECT_EQ(mojom::FileError::OK, error); |
209 | 207 |
210 EXPECT_EQ(data, | 208 EXPECT_EQ(data, file_contents); |
211 mojo::Array<uint8_t>(std::move(file_contents)).To<std::string>()); | |
212 } | 209 } |
213 } | 210 } |
214 | 211 |
215 TEST_F(DirectoryImplTest, ReadEmptyFileIsNotFoundError) { | 212 TEST_F(DirectoryImplTest, ReadEmptyFileIsNotFoundError) { |
216 mojom::DirectoryPtr directory; | 213 mojom::DirectoryPtr directory; |
217 GetTemporaryRoot(&directory); | 214 GetTemporaryRoot(&directory); |
218 mojom::FileError error; | 215 mojom::FileError error; |
219 | 216 |
220 { | 217 { |
221 std::vector<uint8_t> file_contents; | 218 std::vector<uint8_t> file_contents; |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
261 mojom::DirectoryPtr my_file_directory; | 258 mojom::DirectoryPtr my_file_directory; |
262 error = mojom::FileError::FAILED; | 259 error = mojom::FileError::FAILED; |
263 bool handled = directory->OpenDirectory( | 260 bool handled = directory->OpenDirectory( |
264 "my_dir", MakeRequest(&my_file_directory), | 261 "my_dir", MakeRequest(&my_file_directory), |
265 mojom::kFlagRead | mojom::kFlagWrite | mojom::kFlagCreate, &error); | 262 mojom::kFlagRead | mojom::kFlagWrite | mojom::kFlagCreate, &error); |
266 ASSERT_TRUE(handled); | 263 ASSERT_TRUE(handled); |
267 EXPECT_EQ(mojom::FileError::OK, error); | 264 EXPECT_EQ(mojom::FileError::OK, error); |
268 } | 265 } |
269 | 266 |
270 { | 267 { |
271 std::string data("one two three"); | 268 std::vector<uint8_t> data(kData, kData + strlen(kData)); |
272 bool handled = directory->WriteFile( | 269 bool handled = directory->WriteFile("my_dir", data, &error); |
273 "my_dir", mojo::Array<uint8_t>::From(data), &error); | |
274 ASSERT_TRUE(handled); | 270 ASSERT_TRUE(handled); |
275 EXPECT_EQ(mojom::FileError::NOT_A_FILE, error); | 271 EXPECT_EQ(mojom::FileError::NOT_A_FILE, error); |
276 } | 272 } |
277 } | 273 } |
278 | 274 |
279 TEST_F(DirectoryImplTest, Flush) { | 275 TEST_F(DirectoryImplTest, Flush) { |
280 mojom::DirectoryPtr directory; | 276 mojom::DirectoryPtr directory; |
281 GetTemporaryRoot(&directory); | 277 GetTemporaryRoot(&directory); |
282 mojom::FileError error; | 278 mojom::FileError error; |
283 | 279 |
284 { | 280 { |
285 bool handled = directory->Flush(&error); | 281 bool handled = directory->Flush(&error); |
286 ASSERT_TRUE(handled); | 282 ASSERT_TRUE(handled); |
287 EXPECT_EQ(mojom::FileError::OK, error); | 283 EXPECT_EQ(mojom::FileError::OK, error); |
288 } | 284 } |
289 } | 285 } |
290 | 286 |
291 // TODO(vtl): Test delete flags. | 287 // TODO(vtl): Test delete flags. |
292 | 288 |
293 } // namespace | 289 } // namespace |
294 } // namespace filesystem | 290 } // namespace filesystem |
OLD | NEW |