Make USB permissions work in the new permission message system
[chromium-blink-merge.git] / content / browser / fileapi / sandbox_file_system_backend_unittest.cc
blob38630b877908617ec6dde7d196d9402c5f4387b8
1 // Copyright 2013 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.
5 #include "storage/browser/fileapi/sandbox_file_system_backend.h"
7 #include <set>
9 #include "base/basictypes.h"
10 #include "base/files/file_util.h"
11 #include "base/files/scoped_temp_dir.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/run_loop.h"
14 #include "base/thread_task_runner_handle.h"
15 #include "content/public/test/test_file_system_options.h"
16 #include "storage/browser/fileapi/file_system_backend.h"
17 #include "storage/browser/fileapi/file_system_url.h"
18 #include "storage/browser/fileapi/sandbox_file_system_backend_delegate.h"
19 #include "storage/common/fileapi/file_system_util.h"
20 #include "testing/gtest/include/gtest/gtest.h"
21 #include "url/gurl.h"
23 using storage::FileSystemURL;
24 using storage::SandboxFileSystemBackend;
25 using storage::SandboxFileSystemBackendDelegate;
27 // PS stands for path separator.
28 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
29 #define PS "\\"
30 #else
31 #define PS "/"
32 #endif
34 namespace content {
36 namespace {
38 const struct RootPathTest {
39 storage::FileSystemType type;
40 const char* origin_url;
41 const char* expected_path;
42 } kRootPathTestCases[] = {
43 {storage::kFileSystemTypeTemporary, "http://foo:1/", "000" PS "t"},
44 {storage::kFileSystemTypePersistent, "http://foo:1/", "000" PS "p"},
45 {storage::kFileSystemTypeTemporary, "http://bar.com/", "001" PS "t"},
46 {storage::kFileSystemTypePersistent, "http://bar.com/", "001" PS "p"},
47 {storage::kFileSystemTypeTemporary, "https://foo:2/", "002" PS "t"},
48 {storage::kFileSystemTypePersistent, "https://foo:2/", "002" PS "p"},
49 {storage::kFileSystemTypeTemporary, "https://bar.com/", "003" PS "t"},
50 {storage::kFileSystemTypePersistent, "https://bar.com/", "003" PS "p"},
53 const struct RootPathFileURITest {
54 storage::FileSystemType type;
55 const char* origin_url;
56 const char* expected_path;
57 const char* virtual_path;
58 } kRootPathFileURITestCases[] = {
59 {storage::kFileSystemTypeTemporary, "file:///", "000" PS "t", NULL},
60 {storage::kFileSystemTypePersistent, "file:///", "000" PS "p", NULL},
63 void DidOpenFileSystem(base::File::Error* error_out,
64 const GURL& origin_url,
65 const std::string& name,
66 base::File::Error error) {
67 *error_out = error;
70 } // namespace
72 class SandboxFileSystemBackendTest : public testing::Test {
73 protected:
74 void SetUp() override {
75 ASSERT_TRUE(data_dir_.CreateUniqueTempDir());
76 SetUpNewDelegate(CreateAllowFileAccessOptions());
79 void SetUpNewDelegate(const storage::FileSystemOptions& options) {
80 delegate_.reset(new SandboxFileSystemBackendDelegate(
81 NULL /* quota_manager_proxy */,
82 base::ThreadTaskRunnerHandle::Get().get(), data_dir_.path(),
83 NULL /* special_storage_policy */, options));
86 void SetUpNewBackend(const storage::FileSystemOptions& options) {
87 SetUpNewDelegate(options);
88 backend_.reset(new SandboxFileSystemBackend(delegate_.get()));
91 storage::SandboxFileSystemBackendDelegate::OriginEnumerator*
92 CreateOriginEnumerator() const {
93 return backend_->CreateOriginEnumerator();
96 void CreateOriginTypeDirectory(const GURL& origin,
97 storage::FileSystemType type) {
98 base::FilePath target = delegate_->
99 GetBaseDirectoryForOriginAndType(origin, type, true);
100 ASSERT_TRUE(!target.empty());
101 ASSERT_TRUE(base::DirectoryExists(target));
104 bool GetRootPath(const GURL& origin_url,
105 storage::FileSystemType type,
106 storage::OpenFileSystemMode mode,
107 base::FilePath* root_path) {
108 base::File::Error error = base::File::FILE_OK;
109 backend_->ResolveURL(
110 FileSystemURL::CreateForTest(origin_url, type, base::FilePath()),
111 mode,
112 base::Bind(&DidOpenFileSystem, &error));
113 base::RunLoop().RunUntilIdle();
114 if (error != base::File::FILE_OK)
115 return false;
116 base::FilePath returned_root_path =
117 delegate_->GetBaseDirectoryForOriginAndType(
118 origin_url, type, false /* create */);
119 if (root_path)
120 *root_path = returned_root_path;
121 return !returned_root_path.empty();
124 base::FilePath file_system_path() const {
125 return data_dir_.path().Append(
126 SandboxFileSystemBackendDelegate::kFileSystemDirectory);
129 base::ScopedTempDir data_dir_;
130 base::MessageLoop message_loop_;
131 scoped_ptr<storage::SandboxFileSystemBackendDelegate> delegate_;
132 scoped_ptr<storage::SandboxFileSystemBackend> backend_;
135 TEST_F(SandboxFileSystemBackendTest, Empty) {
136 SetUpNewBackend(CreateAllowFileAccessOptions());
137 scoped_ptr<SandboxFileSystemBackendDelegate::OriginEnumerator> enumerator(
138 CreateOriginEnumerator());
139 ASSERT_TRUE(enumerator->Next().is_empty());
142 TEST_F(SandboxFileSystemBackendTest, EnumerateOrigins) {
143 SetUpNewBackend(CreateAllowFileAccessOptions());
144 const char* temporary_origins[] = {
145 "http://www.bar.com/",
146 "http://www.foo.com/",
147 "http://www.foo.com:1/",
148 "http://www.example.com:8080/",
149 "http://www.google.com:80/",
151 const char* persistent_origins[] = {
152 "http://www.bar.com/",
153 "http://www.foo.com:8080/",
154 "http://www.foo.com:80/",
156 size_t temporary_size = arraysize(temporary_origins);
157 size_t persistent_size = arraysize(persistent_origins);
158 std::set<GURL> temporary_set, persistent_set;
159 for (size_t i = 0; i < temporary_size; ++i) {
160 CreateOriginTypeDirectory(GURL(temporary_origins[i]),
161 storage::kFileSystemTypeTemporary);
162 temporary_set.insert(GURL(temporary_origins[i]));
164 for (size_t i = 0; i < persistent_size; ++i) {
165 CreateOriginTypeDirectory(GURL(persistent_origins[i]),
166 storage::kFileSystemTypePersistent);
167 persistent_set.insert(GURL(persistent_origins[i]));
170 scoped_ptr<SandboxFileSystemBackendDelegate::OriginEnumerator> enumerator(
171 CreateOriginEnumerator());
172 size_t temporary_actual_size = 0;
173 size_t persistent_actual_size = 0;
174 GURL current;
175 while (!(current = enumerator->Next()).is_empty()) {
176 SCOPED_TRACE(testing::Message() << "EnumerateOrigin " << current.spec());
177 if (enumerator->HasFileSystemType(storage::kFileSystemTypeTemporary)) {
178 ASSERT_TRUE(temporary_set.find(current) != temporary_set.end());
179 ++temporary_actual_size;
181 if (enumerator->HasFileSystemType(storage::kFileSystemTypePersistent)) {
182 ASSERT_TRUE(persistent_set.find(current) != persistent_set.end());
183 ++persistent_actual_size;
187 EXPECT_EQ(temporary_size, temporary_actual_size);
188 EXPECT_EQ(persistent_size, persistent_actual_size);
191 TEST_F(SandboxFileSystemBackendTest, GetRootPathCreateAndExamine) {
192 std::vector<base::FilePath> returned_root_path(arraysize(kRootPathTestCases));
193 SetUpNewBackend(CreateAllowFileAccessOptions());
195 // Create a new root directory.
196 for (size_t i = 0; i < arraysize(kRootPathTestCases); ++i) {
197 SCOPED_TRACE(testing::Message() << "RootPath (create) #" << i << " "
198 << kRootPathTestCases[i].expected_path);
200 base::FilePath root_path;
201 EXPECT_TRUE(GetRootPath(GURL(kRootPathTestCases[i].origin_url),
202 kRootPathTestCases[i].type,
203 storage::OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT,
204 &root_path));
206 base::FilePath expected = file_system_path().AppendASCII(
207 kRootPathTestCases[i].expected_path);
208 EXPECT_EQ(expected.value(), root_path.value());
209 EXPECT_TRUE(base::DirectoryExists(root_path));
210 ASSERT_TRUE(returned_root_path.size() > i);
211 returned_root_path[i] = root_path;
214 // Get the root directory with create=false and see if we get the
215 // same directory.
216 for (size_t i = 0; i < arraysize(kRootPathTestCases); ++i) {
217 SCOPED_TRACE(testing::Message() << "RootPath (get) #" << i << " "
218 << kRootPathTestCases[i].expected_path);
220 base::FilePath root_path;
221 EXPECT_TRUE(GetRootPath(GURL(kRootPathTestCases[i].origin_url),
222 kRootPathTestCases[i].type,
223 storage::OPEN_FILE_SYSTEM_FAIL_IF_NONEXISTENT,
224 &root_path));
225 ASSERT_TRUE(returned_root_path.size() > i);
226 EXPECT_EQ(returned_root_path[i].value(), root_path.value());
230 TEST_F(SandboxFileSystemBackendTest,
231 GetRootPathCreateAndExamineWithNewBackend) {
232 std::vector<base::FilePath> returned_root_path(arraysize(kRootPathTestCases));
233 SetUpNewBackend(CreateAllowFileAccessOptions());
235 GURL origin_url("http://foo.com:1/");
237 base::FilePath root_path1;
238 EXPECT_TRUE(GetRootPath(origin_url,
239 storage::kFileSystemTypeTemporary,
240 storage::OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT,
241 &root_path1));
243 SetUpNewBackend(CreateDisallowFileAccessOptions());
244 base::FilePath root_path2;
245 EXPECT_TRUE(GetRootPath(origin_url,
246 storage::kFileSystemTypeTemporary,
247 storage::OPEN_FILE_SYSTEM_FAIL_IF_NONEXISTENT,
248 &root_path2));
250 EXPECT_EQ(root_path1.value(), root_path2.value());
253 TEST_F(SandboxFileSystemBackendTest, GetRootPathGetWithoutCreate) {
254 SetUpNewBackend(CreateDisallowFileAccessOptions());
256 // Try to get a root directory without creating.
257 for (size_t i = 0; i < arraysize(kRootPathTestCases); ++i) {
258 SCOPED_TRACE(testing::Message() << "RootPath (create=false) #" << i << " "
259 << kRootPathTestCases[i].expected_path);
260 EXPECT_FALSE(GetRootPath(GURL(kRootPathTestCases[i].origin_url),
261 kRootPathTestCases[i].type,
262 storage::OPEN_FILE_SYSTEM_FAIL_IF_NONEXISTENT,
263 NULL));
267 TEST_F(SandboxFileSystemBackendTest, GetRootPathInIncognito) {
268 SetUpNewBackend(CreateIncognitoFileSystemOptions());
270 // Try to get a root directory.
271 for (size_t i = 0; i < arraysize(kRootPathTestCases); ++i) {
272 SCOPED_TRACE(testing::Message() << "RootPath (incognito) #" << i << " "
273 << kRootPathTestCases[i].expected_path);
274 EXPECT_FALSE(GetRootPath(GURL(kRootPathTestCases[i].origin_url),
275 kRootPathTestCases[i].type,
276 storage::OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT,
277 NULL));
281 TEST_F(SandboxFileSystemBackendTest, GetRootPathFileURI) {
282 SetUpNewBackend(CreateDisallowFileAccessOptions());
283 for (size_t i = 0; i < arraysize(kRootPathFileURITestCases); ++i) {
284 SCOPED_TRACE(testing::Message() << "RootPathFileURI (disallow) #"
285 << i << " " << kRootPathFileURITestCases[i].expected_path);
286 EXPECT_FALSE(GetRootPath(GURL(kRootPathFileURITestCases[i].origin_url),
287 kRootPathFileURITestCases[i].type,
288 storage::OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT,
289 NULL));
293 TEST_F(SandboxFileSystemBackendTest, GetRootPathFileURIWithAllowFlag) {
294 SetUpNewBackend(CreateAllowFileAccessOptions());
295 for (size_t i = 0; i < arraysize(kRootPathFileURITestCases); ++i) {
296 SCOPED_TRACE(testing::Message() << "RootPathFileURI (allow) #"
297 << i << " " << kRootPathFileURITestCases[i].expected_path);
298 base::FilePath root_path;
299 EXPECT_TRUE(GetRootPath(GURL(kRootPathFileURITestCases[i].origin_url),
300 kRootPathFileURITestCases[i].type,
301 storage::OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT,
302 &root_path));
303 base::FilePath expected = file_system_path().AppendASCII(
304 kRootPathFileURITestCases[i].expected_path);
305 EXPECT_EQ(expected.value(), root_path.value());
306 EXPECT_TRUE(base::DirectoryExists(root_path));
310 } // namespace content