| 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 28 matching lines...) Expand all Loading... |
| 39 } | 39 } |
| 40 // Make a directory. | 40 // Make a directory. |
| 41 error = mojom::FileError::FAILED; | 41 error = mojom::FileError::FAILED; |
| 42 bool handled = directory->OpenDirectory( | 42 bool handled = directory->OpenDirectory( |
| 43 "my_dir", nullptr, | 43 "my_dir", nullptr, |
| 44 mojom::kFlagRead | mojom::kFlagWrite | mojom::kFlagCreate, &error); | 44 mojom::kFlagRead | mojom::kFlagWrite | mojom::kFlagCreate, &error); |
| 45 ASSERT_TRUE(handled); | 45 ASSERT_TRUE(handled); |
| 46 EXPECT_EQ(mojom::FileError::OK, error); | 46 EXPECT_EQ(mojom::FileError::OK, error); |
| 47 | 47 |
| 48 error = mojom::FileError::FAILED; | 48 error = mojom::FileError::FAILED; |
| 49 mojo::Array<mojom::DirectoryEntryPtr> directory_contents; | 49 base::Optional<std::vector<mojom::DirectoryEntryPtr>> directory_contents; |
| 50 handled = directory->Read(&error, &directory_contents); | 50 handled = directory->Read(&error, &directory_contents); |
| 51 ASSERT_TRUE(handled); | 51 ASSERT_TRUE(handled); |
| 52 EXPECT_EQ(mojom::FileError::OK, error); | 52 EXPECT_EQ(mojom::FileError::OK, error); |
| 53 ASSERT_TRUE(directory_contents.has_value()); |
| 53 | 54 |
| 54 // Expected contents of the directory. | 55 // Expected contents of the directory. |
| 55 std::map<std::string, mojom::FsFileType> expected_contents; | 56 std::map<std::string, mojom::FsFileType> expected_contents; |
| 56 expected_contents["my_file1"] = mojom::FsFileType::REGULAR_FILE; | 57 expected_contents["my_file1"] = mojom::FsFileType::REGULAR_FILE; |
| 57 expected_contents["my_file2"] = mojom::FsFileType::REGULAR_FILE; | 58 expected_contents["my_file2"] = mojom::FsFileType::REGULAR_FILE; |
| 58 expected_contents["my_file3"] = mojom::FsFileType::REGULAR_FILE; | 59 expected_contents["my_file3"] = mojom::FsFileType::REGULAR_FILE; |
| 59 expected_contents["my_dir"] = mojom::FsFileType::DIRECTORY; | 60 expected_contents["my_dir"] = mojom::FsFileType::DIRECTORY; |
| 60 // Note: We don't expose ".." or ".". | 61 // Note: We don't expose ".." or ".". |
| 61 | 62 |
| 62 EXPECT_EQ(expected_contents.size(), directory_contents.size()); | 63 EXPECT_EQ(expected_contents.size(), directory_contents->size()); |
| 63 for (size_t i = 0; i < directory_contents.size(); i++) { | 64 for (size_t i = 0; i < directory_contents->size(); i++) { |
| 64 ASSERT_TRUE(directory_contents[i]); | 65 auto& item = directory_contents.value()[i]; |
| 65 ASSERT_TRUE(directory_contents[i]->name); | 66 ASSERT_TRUE(item); |
| 66 auto it = expected_contents.find(directory_contents[i]->name.get()); | 67 auto it = expected_contents.find(item->name); |
| 67 ASSERT_TRUE(it != expected_contents.end()); | 68 ASSERT_TRUE(it != expected_contents.end()); |
| 68 EXPECT_EQ(it->second, directory_contents[i]->type); | 69 EXPECT_EQ(it->second, item->type); |
| 69 expected_contents.erase(it); | 70 expected_contents.erase(it); |
| 70 } | 71 } |
| 71 } | 72 } |
| 72 | 73 |
| 73 // TODO(vtl): Properly test OpenFile() and OpenDirectory() (including flags). | 74 // TODO(vtl): Properly test OpenFile() and OpenDirectory() (including flags). |
| 74 | 75 |
| 75 TEST_F(DirectoryImplTest, BasicRenameDelete) { | 76 TEST_F(DirectoryImplTest, BasicRenameDelete) { |
| 76 mojom::DirectoryPtr directory; | 77 mojom::DirectoryPtr directory; |
| 77 GetTemporaryRoot(&directory); | 78 GetTemporaryRoot(&directory); |
| 78 mojom::FileError error; | 79 mojom::FileError error; |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 170 | 171 |
| 171 std::string data("one two three"); | 172 std::string data("one two three"); |
| 172 { | 173 { |
| 173 bool handled = | 174 bool handled = |
| 174 clone_one->WriteFile("data", mojo::Array<uint8_t>::From(data), &error); | 175 clone_one->WriteFile("data", mojo::Array<uint8_t>::From(data), &error); |
| 175 ASSERT_TRUE(handled); | 176 ASSERT_TRUE(handled); |
| 176 EXPECT_EQ(mojom::FileError::OK, error); | 177 EXPECT_EQ(mojom::FileError::OK, error); |
| 177 } | 178 } |
| 178 | 179 |
| 179 { | 180 { |
| 180 mojo::Array<uint8_t> file_contents; | 181 std::vector<uint8_t> file_contents; |
| 181 bool handled = clone_two->ReadEntireFile("data", &error, &file_contents); | 182 bool handled = clone_two->ReadEntireFile("data", &error, &file_contents); |
| 182 ASSERT_TRUE(handled); | 183 ASSERT_TRUE(handled); |
| 183 EXPECT_EQ(mojom::FileError::OK, error); | 184 EXPECT_EQ(mojom::FileError::OK, error); |
| 184 | 185 |
| 185 EXPECT_EQ(data, file_contents.To<std::string>()); | 186 EXPECT_EQ(data, |
| 187 mojo::Array<uint8_t>(std::move(file_contents)).To<std::string>()); |
| 186 } | 188 } |
| 187 } | 189 } |
| 188 | 190 |
| 189 TEST_F(DirectoryImplTest, WriteFileReadFile) { | 191 TEST_F(DirectoryImplTest, WriteFileReadFile) { |
| 190 mojom::DirectoryPtr directory; | 192 mojom::DirectoryPtr directory; |
| 191 GetTemporaryRoot(&directory); | 193 GetTemporaryRoot(&directory); |
| 192 mojom::FileError error; | 194 mojom::FileError error; |
| 193 | 195 |
| 194 std::string data("one two three"); | 196 std::string data("one two three"); |
| 195 { | 197 { |
| 196 bool handled = | 198 bool handled = |
| 197 directory->WriteFile("data", mojo::Array<uint8_t>::From(data), &error); | 199 directory->WriteFile("data", mojo::Array<uint8_t>::From(data), &error); |
| 198 ASSERT_TRUE(handled); | 200 ASSERT_TRUE(handled); |
| 199 EXPECT_EQ(mojom::FileError::OK, error); | 201 EXPECT_EQ(mojom::FileError::OK, error); |
| 200 } | 202 } |
| 201 | 203 |
| 202 { | 204 { |
| 203 mojo::Array<uint8_t> file_contents; | 205 std::vector<uint8_t> file_contents; |
| 204 bool handled = directory->ReadEntireFile("data", &error, &file_contents); | 206 bool handled = directory->ReadEntireFile("data", &error, &file_contents); |
| 205 ASSERT_TRUE(handled); | 207 ASSERT_TRUE(handled); |
| 206 EXPECT_EQ(mojom::FileError::OK, error); | 208 EXPECT_EQ(mojom::FileError::OK, error); |
| 207 | 209 |
| 208 EXPECT_EQ(data, file_contents.To<std::string>()); | 210 EXPECT_EQ(data, |
| 211 mojo::Array<uint8_t>(std::move(file_contents)).To<std::string>()); |
| 209 } | 212 } |
| 210 } | 213 } |
| 211 | 214 |
| 212 TEST_F(DirectoryImplTest, ReadEmptyFileIsNotFoundError) { | 215 TEST_F(DirectoryImplTest, ReadEmptyFileIsNotFoundError) { |
| 213 mojom::DirectoryPtr directory; | 216 mojom::DirectoryPtr directory; |
| 214 GetTemporaryRoot(&directory); | 217 GetTemporaryRoot(&directory); |
| 215 mojom::FileError error; | 218 mojom::FileError error; |
| 216 | 219 |
| 217 { | 220 { |
| 218 mojo::Array<uint8_t> file_contents; | 221 std::vector<uint8_t> file_contents; |
| 219 bool handled = | 222 bool handled = |
| 220 directory->ReadEntireFile("doesnt_exist", &error, &file_contents); | 223 directory->ReadEntireFile("doesnt_exist", &error, &file_contents); |
| 221 ASSERT_TRUE(handled); | 224 ASSERT_TRUE(handled); |
| 222 EXPECT_EQ(mojom::FileError::NOT_FOUND, error); | 225 EXPECT_EQ(mojom::FileError::NOT_FOUND, error); |
| 223 } | 226 } |
| 224 } | 227 } |
| 225 | 228 |
| 226 TEST_F(DirectoryImplTest, CantReadEntireFileOnADirectory) { | 229 TEST_F(DirectoryImplTest, CantReadEntireFileOnADirectory) { |
| 227 mojom::DirectoryPtr directory; | 230 mojom::DirectoryPtr directory; |
| 228 GetTemporaryRoot(&directory); | 231 GetTemporaryRoot(&directory); |
| 229 mojom::FileError error; | 232 mojom::FileError error; |
| 230 | 233 |
| 231 // Create a directory | 234 // Create a directory |
| 232 { | 235 { |
| 233 mojom::DirectoryPtr my_file_directory; | 236 mojom::DirectoryPtr my_file_directory; |
| 234 error = mojom::FileError::FAILED; | 237 error = mojom::FileError::FAILED; |
| 235 bool handled = directory->OpenDirectory( | 238 bool handled = directory->OpenDirectory( |
| 236 "my_dir", GetProxy(&my_file_directory), | 239 "my_dir", GetProxy(&my_file_directory), |
| 237 mojom::kFlagRead | mojom::kFlagWrite | mojom::kFlagCreate, &error); | 240 mojom::kFlagRead | mojom::kFlagWrite | mojom::kFlagCreate, &error); |
| 238 ASSERT_TRUE(handled); | 241 ASSERT_TRUE(handled); |
| 239 EXPECT_EQ(mojom::FileError::OK, error); | 242 EXPECT_EQ(mojom::FileError::OK, error); |
| 240 } | 243 } |
| 241 | 244 |
| 242 // Try to read it as a file | 245 // Try to read it as a file |
| 243 { | 246 { |
| 244 mojo::Array<uint8_t> file_contents; | 247 std::vector<uint8_t> file_contents; |
| 245 bool handled = directory->ReadEntireFile("my_dir", &error, &file_contents); | 248 bool handled = directory->ReadEntireFile("my_dir", &error, &file_contents); |
| 246 ASSERT_TRUE(handled); | 249 ASSERT_TRUE(handled); |
| 247 EXPECT_EQ(mojom::FileError::NOT_A_FILE, error); | 250 EXPECT_EQ(mojom::FileError::NOT_A_FILE, error); |
| 248 } | 251 } |
| 249 } | 252 } |
| 250 | 253 |
| 251 TEST_F(DirectoryImplTest, CantWriteFileOnADirectory) { | 254 TEST_F(DirectoryImplTest, CantWriteFileOnADirectory) { |
| 252 mojom::DirectoryPtr directory; | 255 mojom::DirectoryPtr directory; |
| 253 GetTemporaryRoot(&directory); | 256 GetTemporaryRoot(&directory); |
| 254 mojom::FileError error; | 257 mojom::FileError error; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 270 "my_dir", mojo::Array<uint8_t>::From(data), &error); | 273 "my_dir", mojo::Array<uint8_t>::From(data), &error); |
| 271 ASSERT_TRUE(handled); | 274 ASSERT_TRUE(handled); |
| 272 EXPECT_EQ(mojom::FileError::NOT_A_FILE, error); | 275 EXPECT_EQ(mojom::FileError::NOT_A_FILE, error); |
| 273 } | 276 } |
| 274 } | 277 } |
| 275 | 278 |
| 276 // TODO(vtl): Test delete flags. | 279 // TODO(vtl): Test delete flags. |
| 277 | 280 |
| 278 } // namespace | 281 } // namespace |
| 279 } // namespace filesystem | 282 } // namespace filesystem |
| OLD | NEW |