Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(10)

Side by Side Diff: webkit/browser/fileapi/sandbox_file_system_backend_unittest.cc

Issue 21116008: FileAPI: Move FileSystemQuotaUtil related functions into SandboxContext (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "webkit/browser/fileapi/sandbox_file_system_backend.h" 5 #include "webkit/browser/fileapi/sandbox_file_system_backend.h"
6 6
7 #include <set> 7 #include <set>
8 8
9 #include "base/basictypes.h" 9 #include "base/basictypes.h"
10 #include "base/file_util.h" 10 #include "base/file_util.h"
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
82 } // namespace 82 } // namespace
83 83
84 class SandboxFileSystemBackendTest : public testing::Test { 84 class SandboxFileSystemBackendTest : public testing::Test {
85 protected: 85 protected:
86 virtual void SetUp() { 86 virtual void SetUp() {
87 ASSERT_TRUE(data_dir_.CreateUniqueTempDir()); 87 ASSERT_TRUE(data_dir_.CreateUniqueTempDir());
88 context_.reset(new SandboxContext( 88 context_.reset(new SandboxContext(
89 NULL /* quota_manager_proxy */, 89 NULL /* quota_manager_proxy */,
90 base::MessageLoopProxy::current().get(), 90 base::MessageLoopProxy::current().get(),
91 data_dir_.path(), 91 data_dir_.path(),
92 NULL /* special_storage_policy */)); 92 NULL /* special_storage_policy */,
93 CreateAllowFileAccessOptions()));
93 } 94 }
94 95
95 void SetUpNewBackend(const FileSystemOptions& options) { 96 void SetUpNewBackend(const FileSystemOptions& options) {
96 backend_.reset(new SandboxFileSystemBackend(context_.get(), options)); 97 context_->set_file_system_options(options);
kinuko 2013/07/31 06:12:34 Is set_file_system_option added only for this? Lo
nhiroki 2013/07/31 07:23:19 Right.
98 backend_.reset(new SandboxFileSystemBackend(context_.get()));
97 } 99 }
98 100
99 SandboxFileSystemBackend::OriginEnumerator* CreateOriginEnumerator() const { 101 SandboxContext::OriginEnumerator* CreateOriginEnumerator() const {
100 return backend_->CreateOriginEnumerator(); 102 return backend_->CreateOriginEnumerator();
101 } 103 }
102 104
103 void CreateOriginTypeDirectory(const GURL& origin, 105 void CreateOriginTypeDirectory(const GURL& origin,
104 fileapi::FileSystemType type) { 106 fileapi::FileSystemType type) {
105 base::FilePath target = backend_-> 107 base::FilePath target = context_->
106 GetBaseDirectoryForOriginAndType(origin, type, true); 108 GetBaseDirectoryForOriginAndType(origin, type, true);
107 ASSERT_TRUE(!target.empty()); 109 ASSERT_TRUE(!target.empty());
108 ASSERT_TRUE(base::DirectoryExists(target)); 110 ASSERT_TRUE(base::DirectoryExists(target));
109 } 111 }
110 112
111 bool GetRootPath(const GURL& origin_url, 113 bool GetRootPath(const GURL& origin_url,
112 fileapi::FileSystemType type, 114 fileapi::FileSystemType type,
113 OpenFileSystemMode mode, 115 OpenFileSystemMode mode,
114 base::FilePath* root_path) { 116 base::FilePath* root_path) {
115 base::PlatformFileError error = base::PLATFORM_FILE_OK; 117 base::PlatformFileError error = base::PLATFORM_FILE_OK;
116 backend_->OpenFileSystem( 118 backend_->OpenFileSystem(
117 origin_url, type, mode, 119 origin_url, type, mode,
118 base::Bind(&DidOpenFileSystem, &error)); 120 base::Bind(&DidOpenFileSystem, &error));
119 base::MessageLoop::current()->RunUntilIdle(); 121 base::MessageLoop::current()->RunUntilIdle();
120 if (error != base::PLATFORM_FILE_OK) 122 if (error != base::PLATFORM_FILE_OK)
121 return false; 123 return false;
122 base::FilePath returned_root_path = 124 base::FilePath returned_root_path =
123 backend_->GetBaseDirectoryForOriginAndType( 125 context_->GetBaseDirectoryForOriginAndType(
124 origin_url, type, false /* create */); 126 origin_url, type, false /* create */);
125 if (root_path) 127 if (root_path)
126 *root_path = returned_root_path; 128 *root_path = returned_root_path;
127 return !returned_root_path.empty(); 129 return !returned_root_path.empty();
128 } 130 }
129 131
130 base::FilePath file_system_path() const { 132 base::FilePath file_system_path() const {
131 return data_dir_.path().Append(SandboxContext::kFileSystemDirectory); 133 return data_dir_.path().Append(SandboxContext::kFileSystemDirectory);
132 } 134 }
133 135
134 base::ScopedTempDir data_dir_; 136 base::ScopedTempDir data_dir_;
135 base::MessageLoop message_loop_; 137 base::MessageLoop message_loop_;
136 scoped_ptr<SandboxContext> context_; 138 scoped_ptr<SandboxContext> context_;
137 scoped_ptr<SandboxFileSystemBackend> backend_; 139 scoped_ptr<SandboxFileSystemBackend> backend_;
138 }; 140 };
139 141
140 TEST_F(SandboxFileSystemBackendTest, Empty) { 142 TEST_F(SandboxFileSystemBackendTest, Empty) {
141 SetUpNewBackend(CreateAllowFileAccessOptions()); 143 SetUpNewBackend(CreateAllowFileAccessOptions());
142 scoped_ptr<SandboxFileSystemBackend::OriginEnumerator> enumerator( 144 scoped_ptr<SandboxContext::OriginEnumerator> enumerator(
143 CreateOriginEnumerator()); 145 CreateOriginEnumerator());
144 ASSERT_TRUE(enumerator->Next().is_empty()); 146 ASSERT_TRUE(enumerator->Next().is_empty());
145 } 147 }
146 148
147 TEST_F(SandboxFileSystemBackendTest, EnumerateOrigins) { 149 TEST_F(SandboxFileSystemBackendTest, EnumerateOrigins) {
148 SetUpNewBackend(CreateAllowFileAccessOptions()); 150 SetUpNewBackend(CreateAllowFileAccessOptions());
149 const char* temporary_origins[] = { 151 const char* temporary_origins[] = {
150 "http://www.bar.com/", 152 "http://www.bar.com/",
151 "http://www.foo.com/", 153 "http://www.foo.com/",
152 "http://www.foo.com:1/", 154 "http://www.foo.com:1/",
(...skipping 12 matching lines...) Expand all
165 CreateOriginTypeDirectory(GURL(temporary_origins[i]), 167 CreateOriginTypeDirectory(GURL(temporary_origins[i]),
166 fileapi::kFileSystemTypeTemporary); 168 fileapi::kFileSystemTypeTemporary);
167 temporary_set.insert(GURL(temporary_origins[i])); 169 temporary_set.insert(GURL(temporary_origins[i]));
168 } 170 }
169 for (size_t i = 0; i < persistent_size; ++i) { 171 for (size_t i = 0; i < persistent_size; ++i) {
170 CreateOriginTypeDirectory(GURL(persistent_origins[i]), 172 CreateOriginTypeDirectory(GURL(persistent_origins[i]),
171 kFileSystemTypePersistent); 173 kFileSystemTypePersistent);
172 persistent_set.insert(GURL(persistent_origins[i])); 174 persistent_set.insert(GURL(persistent_origins[i]));
173 } 175 }
174 176
175 scoped_ptr<SandboxFileSystemBackend::OriginEnumerator> enumerator( 177 scoped_ptr<SandboxContext::OriginEnumerator> enumerator(
176 CreateOriginEnumerator()); 178 CreateOriginEnumerator());
177 size_t temporary_actual_size = 0; 179 size_t temporary_actual_size = 0;
178 size_t persistent_actual_size = 0; 180 size_t persistent_actual_size = 0;
179 GURL current; 181 GURL current;
180 while (!(current = enumerator->Next()).is_empty()) { 182 while (!(current = enumerator->Next()).is_empty()) {
181 SCOPED_TRACE(testing::Message() << "EnumerateOrigin " << current.spec()); 183 SCOPED_TRACE(testing::Message() << "EnumerateOrigin " << current.spec());
182 if (enumerator->HasFileSystemType(kFileSystemTypeTemporary)) { 184 if (enumerator->HasFileSystemType(kFileSystemTypeTemporary)) {
183 ASSERT_TRUE(temporary_set.find(current) != temporary_set.end()); 185 ASSERT_TRUE(temporary_set.find(current) != temporary_set.end());
184 ++temporary_actual_size; 186 ++temporary_actual_size;
185 } 187 }
186 if (enumerator->HasFileSystemType(kFileSystemTypePersistent)) { 188 if (enumerator->HasFileSystemType(kFileSystemTypePersistent)) {
187 ASSERT_TRUE(persistent_set.find(current) != persistent_set.end()); 189 ASSERT_TRUE(persistent_set.find(current) != persistent_set.end());
188 ++persistent_actual_size; 190 ++persistent_actual_size;
189 } 191 }
190 } 192 }
191 193
192 EXPECT_EQ(temporary_size, temporary_actual_size); 194 EXPECT_EQ(temporary_size, temporary_actual_size);
193 EXPECT_EQ(persistent_size, persistent_actual_size); 195 EXPECT_EQ(persistent_size, persistent_actual_size);
194 } 196 }
195 197
196 TEST_F(SandboxFileSystemBackendTest, IsAccessValid) {
197 SetUpNewBackend(CreateAllowFileAccessOptions());
198
199 // Normal case.
200 EXPECT_TRUE(backend_->IsAccessValid(CreateFileSystemURL("a")));
201
202 // Access to a path with parent references ('..') should be disallowed.
203 EXPECT_FALSE(backend_->IsAccessValid(CreateFileSystemURL("a/../b")));
204
205 // Access from non-allowed scheme should be disallowed.
206 EXPECT_FALSE(backend_->IsAccessValid(
207 FileSystemURL::CreateForTest(
208 GURL("unknown://bar"), kFileSystemTypeTemporary,
209 base::FilePath::FromUTF8Unsafe("foo"))));
210
211 // Access for non-sandbox type should be disallowed.
212 EXPECT_FALSE(backend_->IsAccessValid(
213 FileSystemURL::CreateForTest(
214 GURL("http://foo/"), kFileSystemTypeTest,
215 base::FilePath::FromUTF8Unsafe("foo"))));
216
217 // Access with restricted name should be disallowed.
218 EXPECT_FALSE(backend_->IsAccessValid(CreateFileSystemURL(".")));
219 EXPECT_FALSE(backend_->IsAccessValid(CreateFileSystemURL("..")));
220
221 // This is also diallowed due to Windows XP parent path handling.
222 EXPECT_FALSE(backend_->IsAccessValid(CreateFileSystemURL("...")));
223
224 // These are identified as unsafe cases due to weird path handling
225 // on Windows.
226 EXPECT_FALSE(backend_->IsAccessValid(CreateFileSystemURL(" ..")));
227 EXPECT_FALSE(backend_->IsAccessValid(CreateFileSystemURL(".. ")));
228
229 // Similar but safe cases.
230 EXPECT_TRUE(backend_->IsAccessValid(CreateFileSystemURL(" .")));
231 EXPECT_TRUE(backend_->IsAccessValid(CreateFileSystemURL(". ")));
232 EXPECT_TRUE(backend_->IsAccessValid(CreateFileSystemURL("b.")));
233 EXPECT_TRUE(backend_->IsAccessValid(CreateFileSystemURL(".b")));
234
235 // A path that looks like a drive letter.
236 EXPECT_TRUE(backend_->IsAccessValid(CreateFileSystemURL("c:")));
237 }
238
239 TEST_F(SandboxFileSystemBackendTest, GetRootPathCreateAndExamine) { 198 TEST_F(SandboxFileSystemBackendTest, GetRootPathCreateAndExamine) {
240 std::vector<base::FilePath> returned_root_path( 199 std::vector<base::FilePath> returned_root_path(
241 ARRAYSIZE_UNSAFE(kRootPathTestCases)); 200 ARRAYSIZE_UNSAFE(kRootPathTestCases));
242 SetUpNewBackend(CreateAllowFileAccessOptions()); 201 SetUpNewBackend(CreateAllowFileAccessOptions());
243 202
244 // Create a new root directory. 203 // Create a new root directory.
245 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kRootPathTestCases); ++i) { 204 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kRootPathTestCases); ++i) {
246 SCOPED_TRACE(testing::Message() << "RootPath (create) #" << i << " " 205 SCOPED_TRACE(testing::Message() << "RootPath (create) #" << i << " "
247 << kRootPathTestCases[i].expected_path); 206 << kRootPathTestCases[i].expected_path);
248 207
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
351 OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT, 310 OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT,
352 &root_path)); 311 &root_path));
353 base::FilePath expected = file_system_path().AppendASCII( 312 base::FilePath expected = file_system_path().AppendASCII(
354 kRootPathFileURITestCases[i].expected_path); 313 kRootPathFileURITestCases[i].expected_path);
355 EXPECT_EQ(expected.value(), root_path.value()); 314 EXPECT_EQ(expected.value(), root_path.value());
356 EXPECT_TRUE(base::DirectoryExists(root_path)); 315 EXPECT_TRUE(base::DirectoryExists(root_path));
357 } 316 }
358 } 317 }
359 318
360 } // namespace fileapi 319 } // namespace fileapi
OLDNEW
« no previous file with comments | « webkit/browser/fileapi/sandbox_file_system_backend.cc ('k') | webkit/browser/fileapi/sandbox_file_system_test_helper.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698