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 "webkit/browser/fileapi/sandbox_file_system_backend.h"
9 #include "base/basictypes.h"
10 #include "base/file_util.h"
11 #include "base/files/scoped_temp_dir.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/message_loop/message_loop_proxy.h"
14 #include "base/run_loop.h"
15 #include "content/public/test/test_file_system_options.h"
16 #include "testing/gtest/include/gtest/gtest.h"
18 #include "webkit/browser/fileapi/file_system_backend.h"
19 #include "webkit/browser/fileapi/file_system_url.h"
20 #include "webkit/browser/fileapi/sandbox_file_system_backend_delegate.h"
21 #include "webkit/common/fileapi/file_system_util.h"
23 // PS stands for path separator.
24 #if defined(FILE_PATH_USES_WIN_SEPARATORS)
34 const struct RootPathTest {
35 fileapi::FileSystemType type;
36 const char* origin_url;
37 const char* expected_path;
38 } kRootPathTestCases[] = {
39 { fileapi::kFileSystemTypeTemporary, "http://foo:1/",
41 { fileapi::kFileSystemTypePersistent, "http://foo:1/",
43 { fileapi::kFileSystemTypeTemporary, "http://bar.com/",
45 { fileapi::kFileSystemTypePersistent, "http://bar.com/",
47 { fileapi::kFileSystemTypeTemporary, "https://foo:2/",
49 { fileapi::kFileSystemTypePersistent, "https://foo:2/",
51 { fileapi::kFileSystemTypeTemporary, "https://bar.com/",
53 { fileapi::kFileSystemTypePersistent, "https://bar.com/",
57 const struct RootPathFileURITest {
58 fileapi::FileSystemType type;
59 const char* origin_url;
60 const char* expected_path;
61 const char* virtual_path;
62 } kRootPathFileURITestCases[] = {
63 { fileapi::kFileSystemTypeTemporary, "file:///",
65 { fileapi::kFileSystemTypePersistent, "file:///",
69 FileSystemURL CreateFileSystemURL(const char* path) {
70 const GURL kOrigin("http://foo/");
71 return FileSystemURL::CreateForTest(
72 kOrigin, kFileSystemTypeTemporary, base::FilePath::FromUTF8Unsafe(path));
75 void DidOpenFileSystem(base::PlatformFileError* error_out,
76 const GURL& origin_url,
77 const std::string& name,
78 base::PlatformFileError error) {
84 class SandboxFileSystemBackendTest : public testing::Test {
86 virtual void SetUp() {
87 ASSERT_TRUE(data_dir_.CreateUniqueTempDir());
88 SetUpNewDelegate(CreateAllowFileAccessOptions());
91 void SetUpNewDelegate(const FileSystemOptions& options) {
92 delegate_.reset(new SandboxFileSystemBackendDelegate(
93 NULL /* quota_manager_proxy */,
94 base::MessageLoopProxy::current().get(),
96 NULL /* special_storage_policy */,
100 void SetUpNewBackend(const FileSystemOptions& options) {
101 SetUpNewDelegate(options);
102 backend_.reset(new SandboxFileSystemBackend(delegate_.get()));
105 SandboxFileSystemBackendDelegate::OriginEnumerator*
106 CreateOriginEnumerator() const {
107 return backend_->CreateOriginEnumerator();
110 void CreateOriginTypeDirectory(const GURL& origin,
111 fileapi::FileSystemType type) {
112 base::FilePath target = delegate_->
113 GetBaseDirectoryForOriginAndType(origin, type, true);
114 ASSERT_TRUE(!target.empty());
115 ASSERT_TRUE(base::DirectoryExists(target));
118 bool GetRootPath(const GURL& origin_url,
119 fileapi::FileSystemType type,
120 OpenFileSystemMode mode,
121 base::FilePath* root_path) {
122 base::PlatformFileError error = base::PLATFORM_FILE_OK;
123 backend_->OpenFileSystem(
124 origin_url, type, mode,
125 base::Bind(&DidOpenFileSystem, &error));
126 base::RunLoop().RunUntilIdle();
127 if (error != base::PLATFORM_FILE_OK)
129 base::FilePath returned_root_path =
130 delegate_->GetBaseDirectoryForOriginAndType(
131 origin_url, type, false /* create */);
133 *root_path = returned_root_path;
134 return !returned_root_path.empty();
137 base::FilePath file_system_path() const {
138 return data_dir_.path().Append(
139 SandboxFileSystemBackendDelegate::kFileSystemDirectory);
142 base::ScopedTempDir data_dir_;
143 base::MessageLoop message_loop_;
144 scoped_ptr<SandboxFileSystemBackendDelegate> delegate_;
145 scoped_ptr<SandboxFileSystemBackend> backend_;
148 TEST_F(SandboxFileSystemBackendTest, Empty) {
149 SetUpNewBackend(CreateAllowFileAccessOptions());
150 scoped_ptr<SandboxFileSystemBackendDelegate::OriginEnumerator> enumerator(
151 CreateOriginEnumerator());
152 ASSERT_TRUE(enumerator->Next().is_empty());
155 TEST_F(SandboxFileSystemBackendTest, EnumerateOrigins) {
156 SetUpNewBackend(CreateAllowFileAccessOptions());
157 const char* temporary_origins[] = {
158 "http://www.bar.com/",
159 "http://www.foo.com/",
160 "http://www.foo.com:1/",
161 "http://www.example.com:8080/",
162 "http://www.google.com:80/",
164 const char* persistent_origins[] = {
165 "http://www.bar.com/",
166 "http://www.foo.com:8080/",
167 "http://www.foo.com:80/",
169 size_t temporary_size = ARRAYSIZE_UNSAFE(temporary_origins);
170 size_t persistent_size = ARRAYSIZE_UNSAFE(persistent_origins);
171 std::set<GURL> temporary_set, persistent_set;
172 for (size_t i = 0; i < temporary_size; ++i) {
173 CreateOriginTypeDirectory(GURL(temporary_origins[i]),
174 fileapi::kFileSystemTypeTemporary);
175 temporary_set.insert(GURL(temporary_origins[i]));
177 for (size_t i = 0; i < persistent_size; ++i) {
178 CreateOriginTypeDirectory(GURL(persistent_origins[i]),
179 kFileSystemTypePersistent);
180 persistent_set.insert(GURL(persistent_origins[i]));
183 scoped_ptr<SandboxFileSystemBackendDelegate::OriginEnumerator> enumerator(
184 CreateOriginEnumerator());
185 size_t temporary_actual_size = 0;
186 size_t persistent_actual_size = 0;
188 while (!(current = enumerator->Next()).is_empty()) {
189 SCOPED_TRACE(testing::Message() << "EnumerateOrigin " << current.spec());
190 if (enumerator->HasFileSystemType(kFileSystemTypeTemporary)) {
191 ASSERT_TRUE(temporary_set.find(current) != temporary_set.end());
192 ++temporary_actual_size;
194 if (enumerator->HasFileSystemType(kFileSystemTypePersistent)) {
195 ASSERT_TRUE(persistent_set.find(current) != persistent_set.end());
196 ++persistent_actual_size;
200 EXPECT_EQ(temporary_size, temporary_actual_size);
201 EXPECT_EQ(persistent_size, persistent_actual_size);
204 TEST_F(SandboxFileSystemBackendTest, GetRootPathCreateAndExamine) {
205 std::vector<base::FilePath> returned_root_path(
206 ARRAYSIZE_UNSAFE(kRootPathTestCases));
207 SetUpNewBackend(CreateAllowFileAccessOptions());
209 // Create a new root directory.
210 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kRootPathTestCases); ++i) {
211 SCOPED_TRACE(testing::Message() << "RootPath (create) #" << i << " "
212 << kRootPathTestCases[i].expected_path);
214 base::FilePath root_path;
215 EXPECT_TRUE(GetRootPath(GURL(kRootPathTestCases[i].origin_url),
216 kRootPathTestCases[i].type,
217 OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT,
220 base::FilePath expected = file_system_path().AppendASCII(
221 kRootPathTestCases[i].expected_path);
222 EXPECT_EQ(expected.value(), root_path.value());
223 EXPECT_TRUE(base::DirectoryExists(root_path));
224 ASSERT_TRUE(returned_root_path.size() > i);
225 returned_root_path[i] = root_path;
228 // Get the root directory with create=false and see if we get the
230 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kRootPathTestCases); ++i) {
231 SCOPED_TRACE(testing::Message() << "RootPath (get) #" << i << " "
232 << kRootPathTestCases[i].expected_path);
234 base::FilePath root_path;
235 EXPECT_TRUE(GetRootPath(GURL(kRootPathTestCases[i].origin_url),
236 kRootPathTestCases[i].type,
237 OPEN_FILE_SYSTEM_FAIL_IF_NONEXISTENT,
239 ASSERT_TRUE(returned_root_path.size() > i);
240 EXPECT_EQ(returned_root_path[i].value(), root_path.value());
244 TEST_F(SandboxFileSystemBackendTest,
245 GetRootPathCreateAndExamineWithNewBackend) {
246 std::vector<base::FilePath> returned_root_path(
247 ARRAYSIZE_UNSAFE(kRootPathTestCases));
248 SetUpNewBackend(CreateAllowFileAccessOptions());
250 GURL origin_url("http://foo.com:1/");
252 base::FilePath root_path1;
253 EXPECT_TRUE(GetRootPath(origin_url, kFileSystemTypeTemporary,
254 OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT,
257 SetUpNewBackend(CreateDisallowFileAccessOptions());
258 base::FilePath root_path2;
259 EXPECT_TRUE(GetRootPath(origin_url, kFileSystemTypeTemporary,
260 OPEN_FILE_SYSTEM_FAIL_IF_NONEXISTENT,
263 EXPECT_EQ(root_path1.value(), root_path2.value());
266 TEST_F(SandboxFileSystemBackendTest, GetRootPathGetWithoutCreate) {
267 SetUpNewBackend(CreateDisallowFileAccessOptions());
269 // Try to get a root directory without creating.
270 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kRootPathTestCases); ++i) {
271 SCOPED_TRACE(testing::Message() << "RootPath (create=false) #" << i << " "
272 << kRootPathTestCases[i].expected_path);
273 EXPECT_FALSE(GetRootPath(GURL(kRootPathTestCases[i].origin_url),
274 kRootPathTestCases[i].type,
275 OPEN_FILE_SYSTEM_FAIL_IF_NONEXISTENT,
280 TEST_F(SandboxFileSystemBackendTest, GetRootPathInIncognito) {
281 SetUpNewBackend(CreateIncognitoFileSystemOptions());
283 // Try to get a root directory.
284 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kRootPathTestCases); ++i) {
285 SCOPED_TRACE(testing::Message() << "RootPath (incognito) #" << i << " "
286 << kRootPathTestCases[i].expected_path);
288 GetRootPath(GURL(kRootPathTestCases[i].origin_url),
289 kRootPathTestCases[i].type,
290 OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT,
295 TEST_F(SandboxFileSystemBackendTest, GetRootPathFileURI) {
296 SetUpNewBackend(CreateDisallowFileAccessOptions());
297 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kRootPathFileURITestCases); ++i) {
298 SCOPED_TRACE(testing::Message() << "RootPathFileURI (disallow) #"
299 << i << " " << kRootPathFileURITestCases[i].expected_path);
301 GetRootPath(GURL(kRootPathFileURITestCases[i].origin_url),
302 kRootPathFileURITestCases[i].type,
303 OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT,
308 TEST_F(SandboxFileSystemBackendTest, GetRootPathFileURIWithAllowFlag) {
309 SetUpNewBackend(CreateAllowFileAccessOptions());
310 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kRootPathFileURITestCases); ++i) {
311 SCOPED_TRACE(testing::Message() << "RootPathFileURI (allow) #"
312 << i << " " << kRootPathFileURITestCases[i].expected_path);
313 base::FilePath root_path;
314 EXPECT_TRUE(GetRootPath(GURL(kRootPathFileURITestCases[i].origin_url),
315 kRootPathFileURITestCases[i].type,
316 OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT,
318 base::FilePath expected = file_system_path().AppendASCII(
319 kRootPathFileURITestCases[i].expected_path);
320 EXPECT_EQ(expected.value(), root_path.value());
321 EXPECT_TRUE(base::DirectoryExists(root_path));
325 } // namespace fileapi