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"
9 #include "mojo/util/capture_util.h"
13 namespace filesystem
{
16 using DirectoryImplTest
= FilesTestBase
;
18 TEST_F(DirectoryImplTest
, Read
) {
19 DirectoryPtr directory
;
20 GetTemporaryRoot(&directory
);
27 } files_to_create
[] = {
28 {"my_file1", kFlagRead
| kFlagWrite
| kFlagCreate
},
29 {"my_file2", kFlagWrite
| kFlagCreate
},
30 {"my_file3", kFlagAppend
| kFlagCreate
}};
31 for (size_t i
= 0; i
< arraysize(files_to_create
); i
++) {
32 error
= FILE_ERROR_FAILED
;
33 directory
->OpenFile(files_to_create
[i
].name
, nullptr,
34 files_to_create
[i
].open_flags
, Capture(&error
));
35 ASSERT_TRUE(directory
.WaitForIncomingResponse());
36 EXPECT_EQ(FILE_ERROR_OK
, error
);
39 error
= FILE_ERROR_FAILED
;
40 directory
->OpenDirectory(
41 "my_dir", nullptr, kFlagRead
| kFlagWrite
| kFlagCreate
, Capture(&error
));
42 ASSERT_TRUE(directory
.WaitForIncomingResponse());
43 EXPECT_EQ(FILE_ERROR_OK
, error
);
45 error
= FILE_ERROR_FAILED
;
46 mojo::Array
<DirectoryEntryPtr
> directory_contents
;
47 directory
->Read(Capture(&error
, &directory_contents
));
48 ASSERT_TRUE(directory
.WaitForIncomingResponse());
49 EXPECT_EQ(FILE_ERROR_OK
, error
);
51 // Expected contents of the directory.
52 std::map
<std::string
, FsFileType
> expected_contents
;
53 expected_contents
["my_file1"] = FS_FILE_TYPE_REGULAR_FILE
;
54 expected_contents
["my_file2"] = FS_FILE_TYPE_REGULAR_FILE
;
55 expected_contents
["my_file3"] = FS_FILE_TYPE_REGULAR_FILE
;
56 expected_contents
["my_dir"] = FS_FILE_TYPE_DIRECTORY
;
57 // Note: We don't expose ".." or ".".
59 EXPECT_EQ(expected_contents
.size(), directory_contents
.size());
60 for (size_t i
= 0; i
< directory_contents
.size(); i
++) {
61 ASSERT_TRUE(directory_contents
[i
]);
62 ASSERT_TRUE(directory_contents
[i
]->name
);
63 auto it
= expected_contents
.find(directory_contents
[i
]->name
.get());
64 ASSERT_TRUE(it
!= expected_contents
.end());
65 EXPECT_EQ(it
->second
, directory_contents
[i
]->type
);
66 expected_contents
.erase(it
);
70 // TODO(vtl): Properly test OpenFile() and OpenDirectory() (including flags).
72 TEST_F(DirectoryImplTest
, BasicRenameDelete
) {
73 DirectoryPtr directory
;
74 GetTemporaryRoot(&directory
);
78 error
= FILE_ERROR_FAILED
;
79 directory
->OpenFile("my_file", nullptr, kFlagWrite
| kFlagCreate
,
81 ASSERT_TRUE(directory
.WaitForIncomingResponse());
82 EXPECT_EQ(FILE_ERROR_OK
, error
);
84 // Opening my_file should succeed.
85 error
= FILE_ERROR_FAILED
;
86 directory
->OpenFile("my_file", nullptr, kFlagRead
| kFlagOpen
,
88 ASSERT_TRUE(directory
.WaitForIncomingResponse());
89 EXPECT_EQ(FILE_ERROR_OK
, error
);
91 // Rename my_file to my_new_file.
92 directory
->Rename("my_file", "my_new_file", Capture(&error
));
93 ASSERT_TRUE(directory
.WaitForIncomingResponse());
94 EXPECT_EQ(FILE_ERROR_OK
, error
);
96 // Opening my_file should fail.
98 error
= FILE_ERROR_FAILED
;
99 directory
->OpenFile("my_file", nullptr, kFlagRead
| kFlagOpen
,
101 ASSERT_TRUE(directory
.WaitForIncomingResponse());
102 EXPECT_EQ(FILE_ERROR_FAILED
, error
);
104 // Opening my_new_file should succeed.
105 error
= FILE_ERROR_FAILED
;
106 directory
->OpenFile("my_new_file", nullptr, kFlagRead
| kFlagOpen
,
108 ASSERT_TRUE(directory
.WaitForIncomingResponse());
109 EXPECT_EQ(FILE_ERROR_OK
, error
);
111 // Delete my_new_file (no flags).
112 directory
->Delete("my_new_file", 0, Capture(&error
));
113 ASSERT_TRUE(directory
.WaitForIncomingResponse());
114 EXPECT_EQ(FILE_ERROR_OK
, error
);
116 // Opening my_new_file should fail.
117 error
= FILE_ERROR_FAILED
;
118 directory
->OpenFile("my_new_file", nullptr, kFlagRead
| kFlagOpen
,
120 ASSERT_TRUE(directory
.WaitForIncomingResponse());
121 EXPECT_EQ(FILE_ERROR_FAILED
, error
);
124 TEST_F(DirectoryImplTest
, CantOpenDirectoriesAsFiles
) {
125 DirectoryPtr directory
;
126 GetTemporaryRoot(&directory
);
130 // Create a directory called 'my_file'
131 DirectoryPtr my_file_directory
;
132 error
= FILE_ERROR_FAILED
;
133 directory
->OpenDirectory(
134 "my_file", GetProxy(&my_file_directory
),
135 kFlagRead
| kFlagWrite
| kFlagCreate
,
137 ASSERT_TRUE(directory
.WaitForIncomingResponse());
138 EXPECT_EQ(FILE_ERROR_OK
, error
);
142 // Attempt to open that directory as a file. This must fail!
144 error
= FILE_ERROR_FAILED
;
145 directory
->OpenFile("my_file", GetProxy(&file
), kFlagRead
| kFlagOpen
,
147 ASSERT_TRUE(directory
.WaitForIncomingResponse());
148 EXPECT_EQ(FILE_ERROR_NOT_A_FILE
, error
);
153 // TODO(vtl): Test delete flags.
156 } // namespace filesystem