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_mount_point_provider.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.h" | |
14 #include "base/message_loop/message_loop_proxy.h" | |
15 #include "testing/gtest/include/gtest/gtest.h" | |
16 #include "url/gurl.h" | |
17 #include "webkit/browser/fileapi/file_system_mount_point_provider.h" | |
18 #include "webkit/browser/fileapi/file_system_url.h" | |
19 #include "webkit/browser/fileapi/mock_file_system_options.h" | |
20 #include "webkit/common/fileapi/file_system_util.h" | |
21 | |
22 // PS stands for path separator. | |
23 #if defined(FILE_PATH_USES_WIN_SEPARATORS) | |
24 #define PS "\\" | |
25 #else | |
26 #define PS "/" | |
27 #endif | |
28 | |
29 namespace fileapi { | |
30 | |
31 namespace { | |
32 | |
33 const struct RootPathTest { | |
34 fileapi::FileSystemType type; | |
35 const char* origin_url; | |
36 const char* expected_path; | |
37 } kRootPathTestCases[] = { | |
38 { fileapi::kFileSystemTypeTemporary, "http://foo:1/", | |
39 "000" PS "t" }, | |
40 { fileapi::kFileSystemTypePersistent, "http://foo:1/", | |
41 "000" PS "p" }, | |
42 { fileapi::kFileSystemTypeTemporary, "http://bar.com/", | |
43 "001" PS "t" }, | |
44 { fileapi::kFileSystemTypePersistent, "http://bar.com/", | |
45 "001" PS "p" }, | |
46 { fileapi::kFileSystemTypeTemporary, "https://foo:2/", | |
47 "002" PS "t" }, | |
48 { fileapi::kFileSystemTypePersistent, "https://foo:2/", | |
49 "002" PS "p" }, | |
50 { fileapi::kFileSystemTypeTemporary, "https://bar.com/", | |
51 "003" PS "t" }, | |
52 { fileapi::kFileSystemTypePersistent, "https://bar.com/", | |
53 "003" PS "p" }, | |
54 }; | |
55 | |
56 const struct RootPathFileURITest { | |
57 fileapi::FileSystemType type; | |
58 const char* origin_url; | |
59 const char* expected_path; | |
60 const char* virtual_path; | |
61 } kRootPathFileURITestCases[] = { | |
62 { fileapi::kFileSystemTypeTemporary, "file:///", | |
63 "000" PS "t", NULL }, | |
64 { fileapi::kFileSystemTypePersistent, "file:///", | |
65 "000" PS "p", NULL }, | |
66 }; | |
67 | |
68 FileSystemURL CreateFileSystemURL(const char* path) { | |
69 const GURL kOrigin("http://foo/"); | |
70 return FileSystemURL::CreateForTest( | |
71 kOrigin, kFileSystemTypeTemporary, base::FilePath::FromUTF8Unsafe(path)); | |
72 } | |
73 | |
74 void DidOpenFileSystem(base::PlatformFileError* error_out, | |
75 base::PlatformFileError error) { | |
76 *error_out = error; | |
77 } | |
78 | |
79 } // namespace | |
80 | |
81 class SandboxMountPointProviderTest : public testing::Test { | |
82 protected: | |
83 virtual void SetUp() { | |
84 ASSERT_TRUE(data_dir_.CreateUniqueTempDir()); | |
85 } | |
86 | |
87 void SetUpNewProvider(const FileSystemOptions& options) { | |
88 provider_.reset( | |
89 new SandboxMountPointProvider(NULL, | |
90 base::MessageLoopProxy::current().get(), | |
91 data_dir_.path(), | |
92 options, | |
93 NULL)); | |
94 } | |
95 | |
96 SandboxMountPointProvider::OriginEnumerator* CreateOriginEnumerator() const { | |
97 return provider_->CreateOriginEnumerator(); | |
98 } | |
99 | |
100 void CreateOriginTypeDirectory(const GURL& origin, | |
101 fileapi::FileSystemType type) { | |
102 base::FilePath target = provider_-> | |
103 GetBaseDirectoryForOriginAndType(origin, type, true); | |
104 ASSERT_TRUE(!target.empty()); | |
105 ASSERT_TRUE(file_util::DirectoryExists(target)); | |
106 } | |
107 | |
108 bool GetRootPath(const GURL& origin_url, | |
109 fileapi::FileSystemType type, | |
110 OpenFileSystemMode mode, | |
111 base::FilePath* root_path) { | |
112 base::PlatformFileError error = base::PLATFORM_FILE_OK; | |
113 provider_->OpenFileSystem( | |
114 origin_url, type, mode, | |
115 base::Bind(&DidOpenFileSystem, &error)); | |
116 base::MessageLoop::current()->RunUntilIdle(); | |
117 if (error != base::PLATFORM_FILE_OK) | |
118 return false; | |
119 base::FilePath returned_root_path = | |
120 provider_->GetBaseDirectoryForOriginAndType( | |
121 origin_url, type, false /* create */); | |
122 if (root_path) | |
123 *root_path = returned_root_path; | |
124 return !returned_root_path.empty(); | |
125 } | |
126 | |
127 base::FilePath file_system_path() const { | |
128 return data_dir_.path().Append( | |
129 SandboxMountPointProvider::kFileSystemDirectory); | |
130 } | |
131 | |
132 base::ScopedTempDir data_dir_; | |
133 base::MessageLoop message_loop_; | |
134 scoped_ptr<SandboxMountPointProvider> provider_; | |
135 }; | |
136 | |
137 TEST_F(SandboxMountPointProviderTest, Empty) { | |
138 SetUpNewProvider(CreateAllowFileAccessOptions()); | |
139 scoped_ptr<SandboxMountPointProvider::OriginEnumerator> enumerator( | |
140 CreateOriginEnumerator()); | |
141 ASSERT_TRUE(enumerator->Next().is_empty()); | |
142 } | |
143 | |
144 TEST_F(SandboxMountPointProviderTest, EnumerateOrigins) { | |
145 SetUpNewProvider(CreateAllowFileAccessOptions()); | |
146 const char* temporary_origins[] = { | |
147 "http://www.bar.com/", | |
148 "http://www.foo.com/", | |
149 "http://www.foo.com:1/", | |
150 "http://www.example.com:8080/", | |
151 "http://www.google.com:80/", | |
152 }; | |
153 const char* persistent_origins[] = { | |
154 "http://www.bar.com/", | |
155 "http://www.foo.com:8080/", | |
156 "http://www.foo.com:80/", | |
157 }; | |
158 size_t temporary_size = ARRAYSIZE_UNSAFE(temporary_origins); | |
159 size_t persistent_size = ARRAYSIZE_UNSAFE(persistent_origins); | |
160 std::set<GURL> temporary_set, persistent_set; | |
161 for (size_t i = 0; i < temporary_size; ++i) { | |
162 CreateOriginTypeDirectory(GURL(temporary_origins[i]), | |
163 fileapi::kFileSystemTypeTemporary); | |
164 temporary_set.insert(GURL(temporary_origins[i])); | |
165 } | |
166 for (size_t i = 0; i < persistent_size; ++i) { | |
167 CreateOriginTypeDirectory(GURL(persistent_origins[i]), | |
168 kFileSystemTypePersistent); | |
169 persistent_set.insert(GURL(persistent_origins[i])); | |
170 } | |
171 | |
172 scoped_ptr<SandboxMountPointProvider::OriginEnumerator> enumerator( | |
173 CreateOriginEnumerator()); | |
174 size_t temporary_actual_size = 0; | |
175 size_t persistent_actual_size = 0; | |
176 GURL current; | |
177 while (!(current = enumerator->Next()).is_empty()) { | |
178 SCOPED_TRACE(testing::Message() << "EnumerateOrigin " << current.spec()); | |
179 if (enumerator->HasFileSystemType(kFileSystemTypeTemporary)) { | |
180 ASSERT_TRUE(temporary_set.find(current) != temporary_set.end()); | |
181 ++temporary_actual_size; | |
182 } | |
183 if (enumerator->HasFileSystemType(kFileSystemTypePersistent)) { | |
184 ASSERT_TRUE(persistent_set.find(current) != persistent_set.end()); | |
185 ++persistent_actual_size; | |
186 } | |
187 } | |
188 | |
189 EXPECT_EQ(temporary_size, temporary_actual_size); | |
190 EXPECT_EQ(persistent_size, persistent_actual_size); | |
191 } | |
192 | |
193 TEST_F(SandboxMountPointProviderTest, IsAccessValid) { | |
194 SetUpNewProvider(CreateAllowFileAccessOptions()); | |
195 | |
196 // Normal case. | |
197 EXPECT_TRUE(provider_->IsAccessValid(CreateFileSystemURL("a"))); | |
198 | |
199 // Access to a path with parent references ('..') should be disallowed. | |
200 EXPECT_FALSE(provider_->IsAccessValid(CreateFileSystemURL("a/../b"))); | |
201 | |
202 // Access from non-allowed scheme should be disallowed. | |
203 EXPECT_FALSE(provider_->IsAccessValid( | |
204 FileSystemURL::CreateForTest( | |
205 GURL("unknown://bar"), kFileSystemTypeTemporary, | |
206 base::FilePath::FromUTF8Unsafe("foo")))); | |
207 | |
208 // Access for non-sandbox type should be disallowed. | |
209 EXPECT_FALSE(provider_->IsAccessValid( | |
210 FileSystemURL::CreateForTest( | |
211 GURL("http://foo/"), kFileSystemTypeTest, | |
212 base::FilePath::FromUTF8Unsafe("foo")))); | |
213 | |
214 // Access with restricted name should be disallowed. | |
215 EXPECT_FALSE(provider_->IsAccessValid(CreateFileSystemURL("."))); | |
216 EXPECT_FALSE(provider_->IsAccessValid(CreateFileSystemURL(".."))); | |
217 | |
218 // This is also diallowed due to Windows XP parent path handling. | |
219 EXPECT_FALSE(provider_->IsAccessValid(CreateFileSystemURL("..."))); | |
220 | |
221 // These are identified as unsafe cases due to weird path handling | |
222 // on Windows. | |
223 EXPECT_FALSE(provider_->IsAccessValid(CreateFileSystemURL(" .."))); | |
224 EXPECT_FALSE(provider_->IsAccessValid(CreateFileSystemURL(".. "))); | |
225 | |
226 // Similar but safe cases. | |
227 EXPECT_TRUE(provider_->IsAccessValid(CreateFileSystemURL(" ."))); | |
228 EXPECT_TRUE(provider_->IsAccessValid(CreateFileSystemURL(". "))); | |
229 EXPECT_TRUE(provider_->IsAccessValid(CreateFileSystemURL("b."))); | |
230 EXPECT_TRUE(provider_->IsAccessValid(CreateFileSystemURL(".b"))); | |
231 | |
232 // A path that looks like a drive letter. | |
233 EXPECT_TRUE(provider_->IsAccessValid(CreateFileSystemURL("c:"))); | |
234 } | |
235 | |
236 TEST_F(SandboxMountPointProviderTest, GetRootPathCreateAndExamine) { | |
237 std::vector<base::FilePath> returned_root_path( | |
238 ARRAYSIZE_UNSAFE(kRootPathTestCases)); | |
239 SetUpNewProvider(CreateAllowFileAccessOptions()); | |
240 | |
241 // Create a new root directory. | |
242 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kRootPathTestCases); ++i) { | |
243 SCOPED_TRACE(testing::Message() << "RootPath (create) #" << i << " " | |
244 << kRootPathTestCases[i].expected_path); | |
245 | |
246 base::FilePath root_path; | |
247 EXPECT_TRUE(GetRootPath(GURL(kRootPathTestCases[i].origin_url), | |
248 kRootPathTestCases[i].type, | |
249 OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT, | |
250 &root_path)); | |
251 | |
252 base::FilePath expected = file_system_path().AppendASCII( | |
253 kRootPathTestCases[i].expected_path); | |
254 EXPECT_EQ(expected.value(), root_path.value()); | |
255 EXPECT_TRUE(file_util::DirectoryExists(root_path)); | |
256 ASSERT_TRUE(returned_root_path.size() > i); | |
257 returned_root_path[i] = root_path; | |
258 } | |
259 | |
260 // Get the root directory with create=false and see if we get the | |
261 // same directory. | |
262 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kRootPathTestCases); ++i) { | |
263 SCOPED_TRACE(testing::Message() << "RootPath (get) #" << i << " " | |
264 << kRootPathTestCases[i].expected_path); | |
265 | |
266 base::FilePath root_path; | |
267 EXPECT_TRUE(GetRootPath(GURL(kRootPathTestCases[i].origin_url), | |
268 kRootPathTestCases[i].type, | |
269 OPEN_FILE_SYSTEM_FAIL_IF_NONEXISTENT, | |
270 &root_path)); | |
271 ASSERT_TRUE(returned_root_path.size() > i); | |
272 EXPECT_EQ(returned_root_path[i].value(), root_path.value()); | |
273 } | |
274 } | |
275 | |
276 TEST_F(SandboxMountPointProviderTest, | |
277 GetRootPathCreateAndExamineWithNewProvider) { | |
278 std::vector<base::FilePath> returned_root_path( | |
279 ARRAYSIZE_UNSAFE(kRootPathTestCases)); | |
280 SetUpNewProvider(CreateAllowFileAccessOptions()); | |
281 | |
282 GURL origin_url("http://foo.com:1/"); | |
283 | |
284 base::FilePath root_path1; | |
285 EXPECT_TRUE(GetRootPath(origin_url, kFileSystemTypeTemporary, | |
286 OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT, | |
287 &root_path1)); | |
288 | |
289 SetUpNewProvider(CreateDisallowFileAccessOptions()); | |
290 base::FilePath root_path2; | |
291 EXPECT_TRUE(GetRootPath(origin_url, kFileSystemTypeTemporary, | |
292 OPEN_FILE_SYSTEM_FAIL_IF_NONEXISTENT, | |
293 &root_path2)); | |
294 | |
295 EXPECT_EQ(root_path1.value(), root_path2.value()); | |
296 } | |
297 | |
298 TEST_F(SandboxMountPointProviderTest, GetRootPathGetWithoutCreate) { | |
299 SetUpNewProvider(CreateDisallowFileAccessOptions()); | |
300 | |
301 // Try to get a root directory without creating. | |
302 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kRootPathTestCases); ++i) { | |
303 SCOPED_TRACE(testing::Message() << "RootPath (create=false) #" << i << " " | |
304 << kRootPathTestCases[i].expected_path); | |
305 EXPECT_FALSE(GetRootPath(GURL(kRootPathTestCases[i].origin_url), | |
306 kRootPathTestCases[i].type, | |
307 OPEN_FILE_SYSTEM_FAIL_IF_NONEXISTENT, | |
308 NULL)); | |
309 } | |
310 } | |
311 | |
312 TEST_F(SandboxMountPointProviderTest, GetRootPathInIncognito) { | |
313 SetUpNewProvider(CreateIncognitoFileSystemOptions()); | |
314 | |
315 // Try to get a root directory. | |
316 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kRootPathTestCases); ++i) { | |
317 SCOPED_TRACE(testing::Message() << "RootPath (incognito) #" << i << " " | |
318 << kRootPathTestCases[i].expected_path); | |
319 EXPECT_FALSE( | |
320 GetRootPath(GURL(kRootPathTestCases[i].origin_url), | |
321 kRootPathTestCases[i].type, | |
322 OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT, | |
323 NULL)); | |
324 } | |
325 } | |
326 | |
327 TEST_F(SandboxMountPointProviderTest, GetRootPathFileURI) { | |
328 SetUpNewProvider(CreateDisallowFileAccessOptions()); | |
329 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kRootPathFileURITestCases); ++i) { | |
330 SCOPED_TRACE(testing::Message() << "RootPathFileURI (disallow) #" | |
331 << i << " " << kRootPathFileURITestCases[i].expected_path); | |
332 EXPECT_FALSE( | |
333 GetRootPath(GURL(kRootPathFileURITestCases[i].origin_url), | |
334 kRootPathFileURITestCases[i].type, | |
335 OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT, | |
336 NULL)); | |
337 } | |
338 } | |
339 | |
340 TEST_F(SandboxMountPointProviderTest, GetRootPathFileURIWithAllowFlag) { | |
341 SetUpNewProvider(CreateAllowFileAccessOptions()); | |
342 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kRootPathFileURITestCases); ++i) { | |
343 SCOPED_TRACE(testing::Message() << "RootPathFileURI (allow) #" | |
344 << i << " " << kRootPathFileURITestCases[i].expected_path); | |
345 base::FilePath root_path; | |
346 EXPECT_TRUE(GetRootPath(GURL(kRootPathFileURITestCases[i].origin_url), | |
347 kRootPathFileURITestCases[i].type, | |
348 OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT, | |
349 &root_path)); | |
350 base::FilePath expected = file_system_path().AppendASCII( | |
351 kRootPathFileURITestCases[i].expected_path); | |
352 EXPECT_EQ(expected.value(), root_path.value()); | |
353 EXPECT_TRUE(file_util::DirectoryExists(root_path)); | |
354 } | |
355 } | |
356 | |
357 } // namespace fileapi | |
OLD | NEW |