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
3 // found in the LICENSE file.
8 #include "components/filesystem/files_test_base.h"
10 namespace filesystem
{
13 using DirectoryImplTest
= FilesTestBase
;
15 TEST_F(DirectoryImplTest
, Read
) {
16 DirectoryPtr directory
;
17 GetTemporaryRoot(&directory
);
24 } files_to_create
[] = {
25 {"my_file1", kFlagRead
| kFlagWrite
| kFlagCreate
},
26 {"my_file2", kFlagWrite
| kFlagCreate
| kFlagOpenAlways
},
27 {"my_file3", kFlagWrite
| kFlagCreate
| kFlagAppend
},
28 {"my_file4", kFlagWrite
| kFlagCreate
}};
29 for (size_t i
= 0; i
< arraysize(files_to_create
); i
++) {
30 error
= FILE_ERROR_FAILED
;
31 directory
->OpenFile(files_to_create
[i
].name
, nullptr,
32 files_to_create
[i
].open_flags
, Capture(&error
));
33 ASSERT_TRUE(directory
.WaitForIncomingResponse());
34 EXPECT_EQ(FILE_ERROR_OK
, error
);
37 error
= FILE_ERROR_FAILED
;
38 directory
->OpenDirectory(
39 "my_dir", nullptr, kFlagRead
| kFlagWrite
| kFlagCreate
, Capture(&error
));
40 ASSERT_TRUE(directory
.WaitForIncomingResponse());
41 EXPECT_EQ(FILE_ERROR_OK
, error
);
43 error
= FILE_ERROR_FAILED
;
44 mojo::Array
<DirectoryEntryPtr
> directory_contents
;
45 directory
->Read(Capture(&error
, &directory_contents
));
46 ASSERT_TRUE(directory
.WaitForIncomingResponse());
47 EXPECT_EQ(FILE_ERROR_OK
, error
);
49 // Expected contents of the directory.
50 std::map
<std::string
, FsFileType
> expected_contents
;
51 expected_contents
["my_file1"] = FS_FILE_TYPE_REGULAR_FILE
;
52 expected_contents
["my_file2"] = FS_FILE_TYPE_REGULAR_FILE
;
53 expected_contents
["my_file3"] = FS_FILE_TYPE_REGULAR_FILE
;
54 expected_contents
["my_file4"] = FS_FILE_TYPE_REGULAR_FILE
;
55 expected_contents
["my_dir"] = FS_FILE_TYPE_DIRECTORY
;
56 // Note: We don't expose ".." or ".".
58 EXPECT_EQ(expected_contents
.size(), directory_contents
.size());
59 for (size_t i
= 0; i
< directory_contents
.size(); i
++) {
60 ASSERT_TRUE(directory_contents
[i
]);
61 ASSERT_TRUE(directory_contents
[i
]->name
);
62 auto it
= expected_contents
.find(directory_contents
[i
]->name
.get());
63 ASSERT_TRUE(it
!= expected_contents
.end());
64 EXPECT_EQ(it
->second
, directory_contents
[i
]->type
);
65 expected_contents
.erase(it
);
69 // TODO(vtl): Properly test OpenFile() and OpenDirectory() (including flags).
71 TEST_F(DirectoryImplTest
, BasicRenameDelete
) {
72 DirectoryPtr directory
;
73 GetTemporaryRoot(&directory
);
77 error
= FILE_ERROR_FAILED
;
78 directory
->OpenFile("my_file", nullptr, kFlagWrite
| kFlagCreate
,
80 ASSERT_TRUE(directory
.WaitForIncomingResponse());
81 EXPECT_EQ(FILE_ERROR_OK
, error
);
83 // Opening my_file should succeed.
84 error
= FILE_ERROR_FAILED
;
85 directory
->OpenFile("my_file", nullptr, kFlagRead
| kFlagOpen
,
87 ASSERT_TRUE(directory
.WaitForIncomingResponse());
88 EXPECT_EQ(FILE_ERROR_OK
, error
);
90 // Rename my_file to my_new_file.
91 directory
->Rename("my_file", "my_new_file", Capture(&error
));
92 ASSERT_TRUE(directory
.WaitForIncomingResponse());
93 EXPECT_EQ(FILE_ERROR_OK
, error
);
95 // Opening my_file should fail.
97 error
= FILE_ERROR_FAILED
;
98 directory
->OpenFile("my_file", nullptr, kFlagRead
| kFlagOpen
,
100 ASSERT_TRUE(directory
.WaitForIncomingResponse());
101 EXPECT_EQ(FILE_ERROR_FAILED
, error
);
103 // Opening my_new_file should succeed.
104 error
= FILE_ERROR_FAILED
;
105 directory
->OpenFile("my_new_file", nullptr, kFlagRead
| kFlagOpen
,
107 ASSERT_TRUE(directory
.WaitForIncomingResponse());
108 EXPECT_EQ(FILE_ERROR_OK
, error
);
110 // Delete my_new_file (no flags).
111 directory
->Delete("my_new_file", 0, Capture(&error
));
112 ASSERT_TRUE(directory
.WaitForIncomingResponse());
113 EXPECT_EQ(FILE_ERROR_OK
, error
);
115 // Opening my_new_file should fail.
116 error
= FILE_ERROR_FAILED
;
117 directory
->OpenFile("my_new_file", nullptr, kFlagRead
| kFlagOpen
,
119 ASSERT_TRUE(directory
.WaitForIncomingResponse());
120 EXPECT_EQ(FILE_ERROR_FAILED
, error
);
123 TEST_F(DirectoryImplTest
, CantOpenDirectoriesAsFiles
) {
124 DirectoryPtr directory
;
125 GetTemporaryRoot(&directory
);
129 // Create a directory called 'my_file'
130 DirectoryPtr my_file_directory
;
131 error
= FILE_ERROR_FAILED
;
132 directory
->OpenDirectory(
133 "my_file", GetProxy(&my_file_directory
),
134 kFlagRead
| kFlagWrite
| kFlagCreate
,
136 ASSERT_TRUE(directory
.WaitForIncomingResponse());
137 EXPECT_EQ(FILE_ERROR_OK
, error
);
141 // Attempt to open that directory as a file. This must fail!
143 error
= FILE_ERROR_FAILED
;
144 directory
->OpenFile("my_file", GetProxy(&file
), kFlagRead
| kFlagOpen
,
146 ASSERT_TRUE(directory
.WaitForIncomingResponse());
147 EXPECT_EQ(FILE_ERROR_NOT_A_FILE
, error
);
152 // TODO(vtl): Test delete flags.
155 } // namespace filesystem