IndexedDBFactory now ForceCloses databases.
[chromium-blink-merge.git] / content / browser / fileapi / sandbox_file_system_backend_unittest.cc
blob4bb12261a978637ce74e2b0c98f9efae5e9bfb70
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"
7 #include <set>
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"
17 #include "url/gurl.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)
25 #define PS "\\"
26 #else
27 #define PS "/"
28 #endif
30 namespace fileapi {
32 namespace {
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/",
40 "000" PS "t" },
41 { fileapi::kFileSystemTypePersistent, "http://foo:1/",
42 "000" PS "p" },
43 { fileapi::kFileSystemTypeTemporary, "http://bar.com/",
44 "001" PS "t" },
45 { fileapi::kFileSystemTypePersistent, "http://bar.com/",
46 "001" PS "p" },
47 { fileapi::kFileSystemTypeTemporary, "https://foo:2/",
48 "002" PS "t" },
49 { fileapi::kFileSystemTypePersistent, "https://foo:2/",
50 "002" PS "p" },
51 { fileapi::kFileSystemTypeTemporary, "https://bar.com/",
52 "003" PS "t" },
53 { fileapi::kFileSystemTypePersistent, "https://bar.com/",
54 "003" PS "p" },
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:///",
64 "000" PS "t", NULL },
65 { fileapi::kFileSystemTypePersistent, "file:///",
66 "000" PS "p", NULL },
69 void DidOpenFileSystem(base::PlatformFileError* error_out,
70 const GURL& origin_url,
71 const std::string& name,
72 base::PlatformFileError error) {
73 *error_out = error;
76 } // namespace
78 class SandboxFileSystemBackendTest : public testing::Test {
79 protected:
80 virtual void SetUp() {
81 ASSERT_TRUE(data_dir_.CreateUniqueTempDir());
82 SetUpNewDelegate(CreateAllowFileAccessOptions());
85 void SetUpNewDelegate(const FileSystemOptions& options) {
86 delegate_.reset(new SandboxFileSystemBackendDelegate(
87 NULL /* quota_manager_proxy */,
88 base::MessageLoopProxy::current().get(),
89 data_dir_.path(),
90 NULL /* special_storage_policy */,
91 options));
94 void SetUpNewBackend(const FileSystemOptions& options) {
95 SetUpNewDelegate(options);
96 backend_.reset(new SandboxFileSystemBackend(delegate_.get()));
99 SandboxFileSystemBackendDelegate::OriginEnumerator*
100 CreateOriginEnumerator() const {
101 return backend_->CreateOriginEnumerator();
104 void CreateOriginTypeDirectory(const GURL& origin,
105 fileapi::FileSystemType type) {
106 base::FilePath target = delegate_->
107 GetBaseDirectoryForOriginAndType(origin, type, true);
108 ASSERT_TRUE(!target.empty());
109 ASSERT_TRUE(base::DirectoryExists(target));
112 bool GetRootPath(const GURL& origin_url,
113 fileapi::FileSystemType type,
114 OpenFileSystemMode mode,
115 base::FilePath* root_path) {
116 base::PlatformFileError error = base::PLATFORM_FILE_OK;
117 backend_->OpenFileSystem(
118 origin_url, type, mode,
119 base::Bind(&DidOpenFileSystem, &error));
120 base::RunLoop().RunUntilIdle();
121 if (error != base::PLATFORM_FILE_OK)
122 return false;
123 base::FilePath returned_root_path =
124 delegate_->GetBaseDirectoryForOriginAndType(
125 origin_url, type, false /* create */);
126 if (root_path)
127 *root_path = returned_root_path;
128 return !returned_root_path.empty();
131 base::FilePath file_system_path() const {
132 return data_dir_.path().Append(
133 SandboxFileSystemBackendDelegate::kFileSystemDirectory);
136 base::ScopedTempDir data_dir_;
137 base::MessageLoop message_loop_;
138 scoped_ptr<SandboxFileSystemBackendDelegate> delegate_;
139 scoped_ptr<SandboxFileSystemBackend> backend_;
142 TEST_F(SandboxFileSystemBackendTest, Empty) {
143 SetUpNewBackend(CreateAllowFileAccessOptions());
144 scoped_ptr<SandboxFileSystemBackendDelegate::OriginEnumerator> enumerator(
145 CreateOriginEnumerator());
146 ASSERT_TRUE(enumerator->Next().is_empty());
149 TEST_F(SandboxFileSystemBackendTest, EnumerateOrigins) {
150 SetUpNewBackend(CreateAllowFileAccessOptions());
151 const char* temporary_origins[] = {
152 "http://www.bar.com/",
153 "http://www.foo.com/",
154 "http://www.foo.com:1/",
155 "http://www.example.com:8080/",
156 "http://www.google.com:80/",
158 const char* persistent_origins[] = {
159 "http://www.bar.com/",
160 "http://www.foo.com:8080/",
161 "http://www.foo.com:80/",
163 size_t temporary_size = ARRAYSIZE_UNSAFE(temporary_origins);
164 size_t persistent_size = ARRAYSIZE_UNSAFE(persistent_origins);
165 std::set<GURL> temporary_set, persistent_set;
166 for (size_t i = 0; i < temporary_size; ++i) {
167 CreateOriginTypeDirectory(GURL(temporary_origins[i]),
168 fileapi::kFileSystemTypeTemporary);
169 temporary_set.insert(GURL(temporary_origins[i]));
171 for (size_t i = 0; i < persistent_size; ++i) {
172 CreateOriginTypeDirectory(GURL(persistent_origins[i]),
173 kFileSystemTypePersistent);
174 persistent_set.insert(GURL(persistent_origins[i]));
177 scoped_ptr<SandboxFileSystemBackendDelegate::OriginEnumerator> enumerator(
178 CreateOriginEnumerator());
179 size_t temporary_actual_size = 0;
180 size_t persistent_actual_size = 0;
181 GURL current;
182 while (!(current = enumerator->Next()).is_empty()) {
183 SCOPED_TRACE(testing::Message() << "EnumerateOrigin " << current.spec());
184 if (enumerator->HasFileSystemType(kFileSystemTypeTemporary)) {
185 ASSERT_TRUE(temporary_set.find(current) != temporary_set.end());
186 ++temporary_actual_size;
188 if (enumerator->HasFileSystemType(kFileSystemTypePersistent)) {
189 ASSERT_TRUE(persistent_set.find(current) != persistent_set.end());
190 ++persistent_actual_size;
194 EXPECT_EQ(temporary_size, temporary_actual_size);
195 EXPECT_EQ(persistent_size, persistent_actual_size);
198 TEST_F(SandboxFileSystemBackendTest, GetRootPathCreateAndExamine) {
199 std::vector<base::FilePath> returned_root_path(
200 ARRAYSIZE_UNSAFE(kRootPathTestCases));
201 SetUpNewBackend(CreateAllowFileAccessOptions());
203 // Create a new root directory.
204 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kRootPathTestCases); ++i) {
205 SCOPED_TRACE(testing::Message() << "RootPath (create) #" << i << " "
206 << kRootPathTestCases[i].expected_path);
208 base::FilePath root_path;
209 EXPECT_TRUE(GetRootPath(GURL(kRootPathTestCases[i].origin_url),
210 kRootPathTestCases[i].type,
211 OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT,
212 &root_path));
214 base::FilePath expected = file_system_path().AppendASCII(
215 kRootPathTestCases[i].expected_path);
216 EXPECT_EQ(expected.value(), root_path.value());
217 EXPECT_TRUE(base::DirectoryExists(root_path));
218 ASSERT_TRUE(returned_root_path.size() > i);
219 returned_root_path[i] = root_path;
222 // Get the root directory with create=false and see if we get the
223 // same directory.
224 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kRootPathTestCases); ++i) {
225 SCOPED_TRACE(testing::Message() << "RootPath (get) #" << i << " "
226 << kRootPathTestCases[i].expected_path);
228 base::FilePath root_path;
229 EXPECT_TRUE(GetRootPath(GURL(kRootPathTestCases[i].origin_url),
230 kRootPathTestCases[i].type,
231 OPEN_FILE_SYSTEM_FAIL_IF_NONEXISTENT,
232 &root_path));
233 ASSERT_TRUE(returned_root_path.size() > i);
234 EXPECT_EQ(returned_root_path[i].value(), root_path.value());
238 TEST_F(SandboxFileSystemBackendTest,
239 GetRootPathCreateAndExamineWithNewBackend) {
240 std::vector<base::FilePath> returned_root_path(
241 ARRAYSIZE_UNSAFE(kRootPathTestCases));
242 SetUpNewBackend(CreateAllowFileAccessOptions());
244 GURL origin_url("http://foo.com:1/");
246 base::FilePath root_path1;
247 EXPECT_TRUE(GetRootPath(origin_url, kFileSystemTypeTemporary,
248 OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT,
249 &root_path1));
251 SetUpNewBackend(CreateDisallowFileAccessOptions());
252 base::FilePath root_path2;
253 EXPECT_TRUE(GetRootPath(origin_url, kFileSystemTypeTemporary,
254 OPEN_FILE_SYSTEM_FAIL_IF_NONEXISTENT,
255 &root_path2));
257 EXPECT_EQ(root_path1.value(), root_path2.value());
260 TEST_F(SandboxFileSystemBackendTest, GetRootPathGetWithoutCreate) {
261 SetUpNewBackend(CreateDisallowFileAccessOptions());
263 // Try to get a root directory without creating.
264 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kRootPathTestCases); ++i) {
265 SCOPED_TRACE(testing::Message() << "RootPath (create=false) #" << i << " "
266 << kRootPathTestCases[i].expected_path);
267 EXPECT_FALSE(GetRootPath(GURL(kRootPathTestCases[i].origin_url),
268 kRootPathTestCases[i].type,
269 OPEN_FILE_SYSTEM_FAIL_IF_NONEXISTENT,
270 NULL));
274 TEST_F(SandboxFileSystemBackendTest, GetRootPathInIncognito) {
275 SetUpNewBackend(CreateIncognitoFileSystemOptions());
277 // Try to get a root directory.
278 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kRootPathTestCases); ++i) {
279 SCOPED_TRACE(testing::Message() << "RootPath (incognito) #" << i << " "
280 << kRootPathTestCases[i].expected_path);
281 EXPECT_FALSE(
282 GetRootPath(GURL(kRootPathTestCases[i].origin_url),
283 kRootPathTestCases[i].type,
284 OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT,
285 NULL));
289 TEST_F(SandboxFileSystemBackendTest, GetRootPathFileURI) {
290 SetUpNewBackend(CreateDisallowFileAccessOptions());
291 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kRootPathFileURITestCases); ++i) {
292 SCOPED_TRACE(testing::Message() << "RootPathFileURI (disallow) #"
293 << i << " " << kRootPathFileURITestCases[i].expected_path);
294 EXPECT_FALSE(
295 GetRootPath(GURL(kRootPathFileURITestCases[i].origin_url),
296 kRootPathFileURITestCases[i].type,
297 OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT,
298 NULL));
302 TEST_F(SandboxFileSystemBackendTest, GetRootPathFileURIWithAllowFlag) {
303 SetUpNewBackend(CreateAllowFileAccessOptions());
304 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kRootPathFileURITestCases); ++i) {
305 SCOPED_TRACE(testing::Message() << "RootPathFileURI (allow) #"
306 << i << " " << kRootPathFileURITestCases[i].expected_path);
307 base::FilePath root_path;
308 EXPECT_TRUE(GetRootPath(GURL(kRootPathFileURITestCases[i].origin_url),
309 kRootPathFileURITestCases[i].type,
310 OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT,
311 &root_path));
312 base::FilePath expected = file_system_path().AppendASCII(
313 kRootPathFileURITestCases[i].expected_path);
314 EXPECT_EQ(expected.value(), root_path.value());
315 EXPECT_TRUE(base::DirectoryExists(root_path));
319 } // namespace fileapi