1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <stddef.h>
#include <stdint.h>
#include <map>
#include <string>
#include "base/macros.h"
#include "components/filesystem/files_test_base.h"
#include "mojo/util/capture_util.h"
using mojo::Capture;
namespace filesystem {
namespace {
using DirectoryImplTest = FilesTestBase;
TEST_F(DirectoryImplTest, Read) {
DirectoryPtr directory;
GetTemporaryRoot(&directory);
FileError error;
// Make some files.
const struct {
const char* name;
uint32_t open_flags;
} files_to_create[] = {
{"my_file1", kFlagRead | kFlagWrite | kFlagCreate},
{"my_file2", kFlagWrite | kFlagCreate},
{"my_file3", kFlagAppend | kFlagCreate }};
for (size_t i = 0; i < arraysize(files_to_create); i++) {
error = FILE_ERROR_FAILED;
directory->OpenFile(files_to_create[i].name, nullptr,
files_to_create[i].open_flags, Capture(&error));
ASSERT_TRUE(directory.WaitForIncomingResponse());
EXPECT_EQ(FILE_ERROR_OK, error);
}
// Make a directory.
error = FILE_ERROR_FAILED;
directory->OpenDirectory(
"my_dir", nullptr, kFlagRead | kFlagWrite | kFlagCreate, Capture(&error));
ASSERT_TRUE(directory.WaitForIncomingResponse());
EXPECT_EQ(FILE_ERROR_OK, error);
error = FILE_ERROR_FAILED;
mojo::Array<DirectoryEntryPtr> directory_contents;
directory->Read(Capture(&error, &directory_contents));
ASSERT_TRUE(directory.WaitForIncomingResponse());
EXPECT_EQ(FILE_ERROR_OK, error);
// Expected contents of the directory.
std::map<std::string, FsFileType> expected_contents;
expected_contents["my_file1"] = FS_FILE_TYPE_REGULAR_FILE;
expected_contents["my_file2"] = FS_FILE_TYPE_REGULAR_FILE;
expected_contents["my_file3"] = FS_FILE_TYPE_REGULAR_FILE;
expected_contents["my_dir"] = FS_FILE_TYPE_DIRECTORY;
// Note: We don't expose ".." or ".".
EXPECT_EQ(expected_contents.size(), directory_contents.size());
for (size_t i = 0; i < directory_contents.size(); i++) {
ASSERT_TRUE(directory_contents[i]);
ASSERT_TRUE(directory_contents[i]->name);
auto it = expected_contents.find(directory_contents[i]->name.get());
ASSERT_TRUE(it != expected_contents.end());
EXPECT_EQ(it->second, directory_contents[i]->type);
expected_contents.erase(it);
}
}
// TODO(vtl): Properly test OpenFile() and OpenDirectory() (including flags).
TEST_F(DirectoryImplTest, BasicRenameDelete) {
DirectoryPtr directory;
GetTemporaryRoot(&directory);
FileError error;
// Create my_file.
error = FILE_ERROR_FAILED;
directory->OpenFile("my_file", nullptr, kFlagWrite | kFlagCreate,
Capture(&error));
ASSERT_TRUE(directory.WaitForIncomingResponse());
EXPECT_EQ(FILE_ERROR_OK, error);
// Opening my_file should succeed.
error = FILE_ERROR_FAILED;
directory->OpenFile("my_file", nullptr, kFlagRead | kFlagOpen,
Capture(&error));
ASSERT_TRUE(directory.WaitForIncomingResponse());
EXPECT_EQ(FILE_ERROR_OK, error);
// Rename my_file to my_new_file.
directory->Rename("my_file", "my_new_file", Capture(&error));
ASSERT_TRUE(directory.WaitForIncomingResponse());
EXPECT_EQ(FILE_ERROR_OK, error);
// Opening my_file should fail.
error = FILE_ERROR_FAILED;
directory->OpenFile("my_file", nullptr, kFlagRead | kFlagOpen,
Capture(&error));
ASSERT_TRUE(directory.WaitForIncomingResponse());
EXPECT_EQ(FILE_ERROR_FAILED, error);
// Opening my_new_file should succeed.
error = FILE_ERROR_FAILED;
directory->OpenFile("my_new_file", nullptr, kFlagRead | kFlagOpen,
Capture(&error));
ASSERT_TRUE(directory.WaitForIncomingResponse());
EXPECT_EQ(FILE_ERROR_OK, error);
// Delete my_new_file (no flags).
directory->Delete("my_new_file", 0, Capture(&error));
ASSERT_TRUE(directory.WaitForIncomingResponse());
EXPECT_EQ(FILE_ERROR_OK, error);
// Opening my_new_file should fail.
error = FILE_ERROR_FAILED;
directory->OpenFile("my_new_file", nullptr, kFlagRead | kFlagOpen,
Capture(&error));
ASSERT_TRUE(directory.WaitForIncomingResponse());
EXPECT_EQ(FILE_ERROR_FAILED, error);
}
TEST_F(DirectoryImplTest, CantOpenDirectoriesAsFiles) {
DirectoryPtr directory;
GetTemporaryRoot(&directory);
FileError error;
{
// Create a directory called 'my_file'
DirectoryPtr my_file_directory;
error = FILE_ERROR_FAILED;
directory->OpenDirectory(
"my_file", GetProxy(&my_file_directory),
kFlagRead | kFlagWrite | kFlagCreate,
Capture(&error));
ASSERT_TRUE(directory.WaitForIncomingResponse());
EXPECT_EQ(FILE_ERROR_OK, error);
}
{
// Attempt to open that directory as a file. This must fail!
FilePtr file;
error = FILE_ERROR_FAILED;
directory->OpenFile("my_file", GetProxy(&file), kFlagRead | kFlagOpen,
Capture(&error));
ASSERT_TRUE(directory.WaitForIncomingResponse());
EXPECT_EQ(FILE_ERROR_NOT_A_FILE, error);
}
}
// TODO(vtl): Test delete flags.
} // namespace
} // namespace filesystem
|