OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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. | |
4 | |
5 #include "webkit/browser/fileapi/sandbox_file_system_backend.h" | |
6 | |
7 #include <set> | |
8 | |
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 "testing/gtest/include/gtest/gtest.h" | |
16 #include "url/gurl.h" | |
17 #include "webkit/browser/fileapi/file_system_backend.h" | |
18 #include "webkit/browser/fileapi/file_system_url.h" | |
19 #include "webkit/browser/fileapi/mock_file_system_options.h" | |
20 #include "webkit/browser/fileapi/sandbox_file_system_backend_delegate.h" | |
21 #include "webkit/common/fileapi/file_system_util.h" | |
22 | |
23 // PS stands for path separator. | |
24 #if defined(FILE_PATH_USES_WIN_SEPARATORS) | |
25 #define PS "\\" | |
26 #else | |
27 #define PS "/" | |
28 #endif | |
29 | |
30 namespace fileapi { | |
31 | |
32 namespace { | |
33 | |
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" }, | |
55 }; | |
56 | |
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 }, | |
67 }; | |
68 | |
69 FileSystemURL CreateFileSystemURL(const char* path) { | |
70 const GURL kOrigin("http://foo/"); | |
71 return FileSystemURL::CreateForTest( | |
72 kOrigin, kFileSystemTypeTemporary, base::FilePath::FromUTF8Unsafe(path)); | |
73 } | |
74 | |
75 void DidOpenFileSystem(base::PlatformFileError* error_out, | |
76 const GURL& origin_url, | |
77 const std::string& name, | |
78 base::PlatformFileError error) { | |
79 *error_out = error; | |
80 } | |
81 | |
82 } // namespace | |
83 | |
84 class SandboxFileSystemBackendTest : public testing::Test { | |
85 protected: | |
86 virtual void SetUp() { | |
87 ASSERT_TRUE(data_dir_.CreateUniqueTempDir()); | |
88 SetUpNewDelegate(CreateAllowFileAccessOptions()); | |
89 } | |
90 | |
91 void SetUpNewDelegate(const FileSystemOptions& options) { | |
92 delegate_.reset(new SandboxFileSystemBackendDelegate( | |
93 NULL /* quota_manager_proxy */, | |
94 base::MessageLoopProxy::current().get(), | |
95 data_dir_.path(), | |
96 NULL /* special_storage_policy */, | |
97 options)); | |
98 } | |
99 | |
100 void SetUpNewBackend(const FileSystemOptions& options) { | |
101 SetUpNewDelegate(options); | |
102 backend_.reset(new SandboxFileSystemBackend(delegate_.get())); | |
103 } | |
104 | |
105 SandboxFileSystemBackendDelegate::OriginEnumerator* | |
106 CreateOriginEnumerator() const { | |
107 return backend_->CreateOriginEnumerator(); | |
108 } | |
109 | |
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)); | |
116 } | |
117 | |
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) | |
128 return false; | |
129 base::FilePath returned_root_path = | |
130 delegate_->GetBaseDirectoryForOriginAndType( | |
131 origin_url, type, false /* create */); | |
132 if (root_path) | |
133 *root_path = returned_root_path; | |
134 return !returned_root_path.empty(); | |
135 } | |
136 | |
137 base::FilePath file_system_path() const { | |
138 return data_dir_.path().Append( | |
139 SandboxFileSystemBackendDelegate::kFileSystemDirectory); | |
140 } | |
141 | |
142 base::ScopedTempDir data_dir_; | |
143 base::MessageLoop message_loop_; | |
144 scoped_ptr<SandboxFileSystemBackendDelegate> delegate_; | |
145 scoped_ptr<SandboxFileSystemBackend> backend_; | |
146 }; | |
147 | |
148 TEST_F(SandboxFileSystemBackendTest, Empty) { | |
149 SetUpNewBackend(CreateAllowFileAccessOptions()); | |
150 scoped_ptr<SandboxFileSystemBackendDelegate::OriginEnumerator> enumerator( | |
151 CreateOriginEnumerator()); | |
152 ASSERT_TRUE(enumerator->Next().is_empty()); | |
153 } | |
154 | |
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/", | |
163 }; | |
164 const char* persistent_origins[] = { | |
165 "http://www.bar.com/", | |
166 "http://www.foo.com:8080/", | |
167 "http://www.foo.com:80/", | |
168 }; | |
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])); | |
176 } | |
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])); | |
181 } | |
182 | |
183 scoped_ptr<SandboxFileSystemBackendDelegate::OriginEnumerator> enumerator( | |
184 CreateOriginEnumerator()); | |
185 size_t temporary_actual_size = 0; | |
186 size_t persistent_actual_size = 0; | |
187 GURL current; | |
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; | |
193 } | |
194 if (enumerator->HasFileSystemType(kFileSystemTypePersistent)) { | |
195 ASSERT_TRUE(persistent_set.find(current) != persistent_set.end()); | |
196 ++persistent_actual_size; | |
197 } | |
198 } | |
199 | |
200 EXPECT_EQ(temporary_size, temporary_actual_size); | |
201 EXPECT_EQ(persistent_size, persistent_actual_size); | |
202 } | |
203 | |
204 TEST_F(SandboxFileSystemBackendTest, GetRootPathCreateAndExamine) { | |
205 std::vector<base::FilePath> returned_root_path( | |
206 ARRAYSIZE_UNSAFE(kRootPathTestCases)); | |
207 SetUpNewBackend(CreateAllowFileAccessOptions()); | |
208 | |
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); | |
213 | |
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, | |
218 &root_path)); | |
219 | |
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; | |
226 } | |
227 | |
228 // Get the root directory with create=false and see if we get the | |
229 // same directory. | |
230 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kRootPathTestCases); ++i) { | |
231 SCOPED_TRACE(testing::Message() << "RootPath (get) #" << i << " " | |
232 << kRootPathTestCases[i].expected_path); | |
233 | |
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, | |
238 &root_path)); | |
239 ASSERT_TRUE(returned_root_path.size() > i); | |
240 EXPECT_EQ(returned_root_path[i].value(), root_path.value()); | |
241 } | |
242 } | |
243 | |
244 TEST_F(SandboxFileSystemBackendTest, | |
245 GetRootPathCreateAndExamineWithNewBackend) { | |
246 std::vector<base::FilePath> returned_root_path( | |
247 ARRAYSIZE_UNSAFE(kRootPathTestCases)); | |
248 SetUpNewBackend(CreateAllowFileAccessOptions()); | |
249 | |
250 GURL origin_url("http://foo.com:1/"); | |
251 | |
252 base::FilePath root_path1; | |
253 EXPECT_TRUE(GetRootPath(origin_url, kFileSystemTypeTemporary, | |
254 OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT, | |
255 &root_path1)); | |
256 | |
257 SetUpNewBackend(CreateDisallowFileAccessOptions()); | |
258 base::FilePath root_path2; | |
259 EXPECT_TRUE(GetRootPath(origin_url, kFileSystemTypeTemporary, | |
260 OPEN_FILE_SYSTEM_FAIL_IF_NONEXISTENT, | |
261 &root_path2)); | |
262 | |
263 EXPECT_EQ(root_path1.value(), root_path2.value()); | |
264 } | |
265 | |
266 TEST_F(SandboxFileSystemBackendTest, GetRootPathGetWithoutCreate) { | |
267 SetUpNewBackend(CreateDisallowFileAccessOptions()); | |
268 | |
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, | |
276 NULL)); | |
277 } | |
278 } | |
279 | |
280 TEST_F(SandboxFileSystemBackendTest, GetRootPathInIncognito) { | |
281 SetUpNewBackend(CreateIncognitoFileSystemOptions()); | |
282 | |
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); | |
287 EXPECT_FALSE( | |
288 GetRootPath(GURL(kRootPathTestCases[i].origin_url), | |
289 kRootPathTestCases[i].type, | |
290 OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT, | |
291 NULL)); | |
292 } | |
293 } | |
294 | |
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); | |
300 EXPECT_FALSE( | |
301 GetRootPath(GURL(kRootPathFileURITestCases[i].origin_url), | |
302 kRootPathFileURITestCases[i].type, | |
303 OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT, | |
304 NULL)); | |
305 } | |
306 } | |
307 | |
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, | |
317 &root_path)); | |
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)); | |
322 } | |
323 } | |
324 | |
325 } // namespace fileapi | |
OLD | NEW |