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

Side by Side Diff: webkit/fileapi/obfuscated_file_system_file_util_unittest.cc

Issue 7608011: Simplify directory path accounting. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rolled in CR feedback. Created 9 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
« no previous file with comments | « webkit/fileapi/obfuscated_file_system_file_util.cc ('k') | webkit/fileapi/quota_file_util.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 <algorithm> 5 #include <algorithm>
6 #include <set> 6 #include <set>
7 #include <string> 7 #include <string>
8 8
9 #include "base/file_path.h" 9 #include "base/file_path.h"
10 #include "base/file_util.h" 10 #include "base/file_util.h"
11 #include "base/memory/scoped_ptr.h" 11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/scoped_callback_factory.h"
13 #include "base/message_loop.h"
12 #include "base/platform_file.h" 14 #include "base/platform_file.h"
13 #include "base/scoped_temp_dir.h" 15 #include "base/scoped_temp_dir.h"
14 #include "base/sys_string_conversions.h" 16 #include "base/sys_string_conversions.h"
15 #include "testing/gtest/include/gtest/gtest.h" 17 #include "testing/gtest/include/gtest/gtest.h"
16 #include "webkit/fileapi/file_system_context.h" 18 #include "webkit/fileapi/file_system_context.h"
17 #include "webkit/fileapi/file_system_operation_context.h" 19 #include "webkit/fileapi/file_system_operation_context.h"
20 #include "webkit/fileapi/file_system_path_manager.h"
18 #include "webkit/fileapi/file_system_test_helper.h" 21 #include "webkit/fileapi/file_system_test_helper.h"
22 #include "webkit/fileapi/file_system_usage_cache.h"
19 #include "webkit/fileapi/obfuscated_file_system_file_util.h" 23 #include "webkit/fileapi/obfuscated_file_system_file_util.h"
24 #include "webkit/quota/mock_special_storage_policy.h"
25 #include "webkit/quota/quota_manager.h"
26 #include "webkit/quota/quota_types.h"
20 27
21 using namespace fileapi; 28 using namespace fileapi;
22 29
23 namespace { 30 namespace {
24 31
25 FilePath UTF8ToFilePath(const std::string& str) { 32 FilePath UTF8ToFilePath(const std::string& str) {
26 FilePath::StringType result; 33 FilePath::StringType result;
27 #if defined(OS_POSIX) 34 #if defined(OS_POSIX)
28 result = str; 35 result = str;
29 #elif defined(OS_WIN) 36 #elif defined(OS_WIN)
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
119 126
120 // TODO(ericu): The vast majority of this and the other FSFU subclass tests 127 // TODO(ericu): The vast majority of this and the other FSFU subclass tests
121 // could theoretically be shared. It would basically be a FSFU interface 128 // could theoretically be shared. It would basically be a FSFU interface
122 // compliance test, and only the subclass-specific bits that look into the 129 // compliance test, and only the subclass-specific bits that look into the
123 // implementation would need to be written per-subclass. 130 // implementation would need to be written per-subclass.
124 class ObfuscatedFileSystemFileUtilTest : public testing::Test { 131 class ObfuscatedFileSystemFileUtilTest : public testing::Test {
125 public: 132 public:
126 ObfuscatedFileSystemFileUtilTest() 133 ObfuscatedFileSystemFileUtilTest()
127 : origin_(GURL("http://www.example.com")), 134 : origin_(GURL("http://www.example.com")),
128 type_(kFileSystemTypeTemporary), 135 type_(kFileSystemTypeTemporary),
129 test_helper_(origin_, type_) { 136 callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
137 test_helper_(origin_, type_),
138 quota_status_(quota::kQuotaStatusUnknown),
139 usage_(-1) {
130 } 140 }
131 141
132 void SetUp() { 142 void SetUp() {
133 ASSERT_TRUE(data_dir_.CreateUniqueTempDir()); 143 ASSERT_TRUE(data_dir_.CreateUniqueTempDir());
134 144
145 quota_manager_ = new quota::QuotaManager(
146 false /* is_incognito */,
147 data_dir_.path(),
148 base::MessageLoopProxy::current(),
149 base::MessageLoopProxy::current(),
150 NULL /* special storage policy */);
151
152 // Every time we create a new helper, it creates another context, which
153 // creates another path manager, another sandbox_mount_point_provider, and
154 // another OFSFU. We need to pass in the context to skip all that.
155 file_system_context_ = new FileSystemContext(
156 base::MessageLoopProxy::current(),
157 base::MessageLoopProxy::current(),
158 new quota::MockSpecialStoragePolicy(),
159 quota_manager_->proxy(),
160 data_dir_.path(),
161 false /* incognito */,
162 true /* allow_file_access_from_files */,
163 false /* unlimited_quota */,
164 NULL /* path_manager */);
165
135 obfuscated_file_system_file_util_ = 166 obfuscated_file_system_file_util_ =
136 new ObfuscatedFileSystemFileUtil(data_dir_.path(), 167 static_cast<ObfuscatedFileSystemFileUtil*>(
137 new FileSystemFileUtil()); 168 file_system_context_->path_manager()->GetFileSystemFileUtil(type_));
138 test_helper_.SetUp(data_dir_.path(), 169
139 false, // incognito 170
140 false, // unlimited quota 171 test_helper_.SetUp(file_system_context_.get(),
141 NULL, // quota::QuotaManagerProxy
142 obfuscated_file_system_file_util_.get()); 172 obfuscated_file_system_file_util_.get());
143 } 173 }
144 174
145 FileSystemOperationContext* NewContext() { 175 FileSystemOperationContext* NewContext(FileSystemTestOriginHelper* helper) {
146 FileSystemOperationContext* context = test_helper_.NewOperationContext(); 176 FileSystemOperationContext* context;
177 if (helper)
178 context = helper->NewOperationContext();
179 else
180 context = test_helper_.NewOperationContext();
181 context->set_allowed_bytes_growth(1024 * 1024); // Big enough for all tests.
147 return context; 182 return context;
148 } 183 }
149 184
185 // This can only be used after SetUp has run and created file_system_context_
186 // and obfuscated_file_system_file_util_.
187 // Use this for tests which need to run in multiple origins; we need a test
188 // helper per origin.
189 FileSystemTestOriginHelper* NewHelper(
190 const GURL& origin, fileapi::FileSystemType type) {
191 FileSystemTestOriginHelper* helper =
192 new FileSystemTestOriginHelper(origin, type);
193
194 helper->SetUp(file_system_context_.get(),
195 obfuscated_file_system_file_util_.get());
196 return helper;
197 }
198
150 ObfuscatedFileSystemFileUtil* ofsfu() { 199 ObfuscatedFileSystemFileUtil* ofsfu() {
151 return obfuscated_file_system_file_util_.get(); 200 return obfuscated_file_system_file_util_.get();
152 } 201 }
153 202
154 const FilePath& test_directory() const { 203 const FilePath& test_directory() const {
155 return data_dir_.path(); 204 return data_dir_.path();
156 } 205 }
157 206
158 const GURL& origin_url() const { 207 const GURL& origin() const {
159 return origin_; 208 return origin_;
160 } 209 }
161 210
162 fileapi::FileSystemType type() const { 211 fileapi::FileSystemType type() const {
163 return type_; 212 return type_;
164 } 213 }
165 214
215 void GetUsageFromQuotaManager() {
216 quota_manager_->GetUsageAndQuota(
217 origin(), test_helper_.storage_type(),
218 callback_factory_.NewCallback(
219 &ObfuscatedFileSystemFileUtilTest::OnGetUsage));
220 MessageLoop::current()->RunAllPending();
221 EXPECT_EQ(quota::kQuotaStatusOk, quota_status_);
222 }
223
224 void RevokeUsageCache() {
225 quota_manager_->ResetUsageTracker(test_helper_.storage_type());
226 ASSERT_TRUE(test_helper_.RevokeUsageCache());
227 }
228
229 int64 SizeInUsageFile() {
230 return test_helper_.GetCachedOriginUsage();
231 }
232
233 int64 usage() const { return usage_; }
234
235 void OnGetUsage(quota::QuotaStatusCode status, int64 usage, int64 unused) {
236 EXPECT_EQ(quota::kQuotaStatusOk, status);
237 quota_status_ = status;
238 usage_ = usage;
239 }
240
166 void CheckFileAndCloseHandle( 241 void CheckFileAndCloseHandle(
167 const FilePath& virtual_path, PlatformFile file_handle) { 242 const FilePath& virtual_path, PlatformFile file_handle) {
168 scoped_ptr<FileSystemOperationContext> context(NewContext()); 243 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
169 FilePath local_path; 244 FilePath local_path;
170 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->GetLocalFilePath( 245 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->GetLocalFilePath(
171 context.get(), virtual_path, &local_path)); 246 context.get(), virtual_path, &local_path));
172 247
173 base::PlatformFileInfo file_info0; 248 base::PlatformFileInfo file_info0;
174 FilePath data_path; 249 FilePath data_path;
175 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->GetFileInfo( 250 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->GetFileInfo(
176 context.get(), virtual_path, &file_info0, &data_path)); 251 context.get(), virtual_path, &file_info0, &data_path));
177 EXPECT_EQ(data_path, local_path); 252 EXPECT_EQ(data_path, local_path);
178 EXPECT_TRUE(FileExists(data_path)); 253 EXPECT_TRUE(FileExists(data_path));
(...skipping 12 matching lines...) Expand all
191 &error); 266 &error);
192 ASSERT_NE(base::kInvalidPlatformFileValue, file_handle); 267 ASSERT_NE(base::kInvalidPlatformFileValue, file_handle);
193 ASSERT_EQ(base::PLATFORM_FILE_OK, error); 268 ASSERT_EQ(base::PLATFORM_FILE_OK, error);
194 EXPECT_FALSE(created); 269 EXPECT_FALSE(created);
195 } 270 }
196 ASSERT_EQ(length, base::WritePlatformFile(file_handle, 0, data, length)); 271 ASSERT_EQ(length, base::WritePlatformFile(file_handle, 0, data, length));
197 EXPECT_TRUE(base::ClosePlatformFile(file_handle)); 272 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
198 273
199 base::PlatformFileInfo file_info1; 274 base::PlatformFileInfo file_info1;
200 EXPECT_EQ(length, GetSize(data_path)); 275 EXPECT_EQ(length, GetSize(data_path));
201 context.reset(NewContext()); 276 context.reset(NewContext(NULL));
202 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->GetFileInfo( 277 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->GetFileInfo(
203 context.get(), virtual_path, &file_info1, &data_path)); 278 context.get(), virtual_path, &file_info1, &data_path));
204 EXPECT_EQ(data_path, local_path); 279 EXPECT_EQ(data_path, local_path);
205 280
206 EXPECT_FALSE(file_info0.is_directory); 281 EXPECT_FALSE(file_info0.is_directory);
207 EXPECT_FALSE(file_info1.is_directory); 282 EXPECT_FALSE(file_info1.is_directory);
208 EXPECT_FALSE(file_info0.is_symbolic_link); 283 EXPECT_FALSE(file_info0.is_symbolic_link);
209 EXPECT_FALSE(file_info1.is_symbolic_link); 284 EXPECT_FALSE(file_info1.is_symbolic_link);
210 EXPECT_EQ(0, file_info0.size); 285 EXPECT_EQ(0, file_info0.size);
211 EXPECT_EQ(length, file_info1.size); 286 EXPECT_EQ(length, file_info1.size);
212 EXPECT_LE(file_info0.last_modified, file_info1.last_modified); 287 EXPECT_LE(file_info0.last_modified, file_info1.last_modified);
213 288
214 context.reset(NewContext()); 289 context.reset(NewContext(NULL));
215 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->Truncate( 290 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->Truncate(
216 context.get(), virtual_path, length * 2)); 291 context.get(), virtual_path, length * 2));
217 EXPECT_EQ(length * 2, GetSize(data_path)); 292 EXPECT_EQ(length * 2, GetSize(data_path));
218 293
219 context.reset(NewContext()); 294 context.reset(NewContext(NULL));
220 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->Truncate( 295 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->Truncate(
221 context.get(), virtual_path, 1)); 296 context.get(), virtual_path, 0));
222 EXPECT_EQ(1, GetSize(data_path)); 297 EXPECT_EQ(0, GetSize(data_path));
223 } 298 }
224 299
225 void ValidateTestDirectory( 300 void ValidateTestDirectory(
226 const FilePath& root_path, 301 const FilePath& root_path,
227 const std::set<FilePath::StringType>& files, 302 const std::set<FilePath::StringType>& files,
228 const std::set<FilePath::StringType>& directories) { 303 const std::set<FilePath::StringType>& directories) {
229 scoped_ptr<FileSystemOperationContext> context; 304 scoped_ptr<FileSystemOperationContext> context;
230 std::set<FilePath::StringType>::const_iterator iter; 305 std::set<FilePath::StringType>::const_iterator iter;
231 for (iter = files.begin(); iter != files.end(); ++iter) { 306 for (iter = files.begin(); iter != files.end(); ++iter) {
232 bool created = true; 307 bool created = true;
233 context.reset(NewContext()); 308 context.reset(NewContext(NULL));
234 ASSERT_EQ(base::PLATFORM_FILE_OK, 309 ASSERT_EQ(base::PLATFORM_FILE_OK,
235 ofsfu()->EnsureFileExists( 310 ofsfu()->EnsureFileExists(
236 context.get(), root_path.Append(*iter), 311 context.get(), root_path.Append(*iter),
237 &created)); 312 &created));
238 ASSERT_FALSE(created); 313 ASSERT_FALSE(created);
239 } 314 }
240 for (iter = directories.begin(); iter != directories.end(); ++iter) { 315 for (iter = directories.begin(); iter != directories.end(); ++iter) {
241 context.reset(NewContext()); 316 context.reset(NewContext(NULL));
242 EXPECT_TRUE(ofsfu()->DirectoryExists(context.get(), 317 EXPECT_TRUE(ofsfu()->DirectoryExists(context.get(),
243 root_path.Append(*iter))); 318 root_path.Append(*iter)));
244 } 319 }
245 } 320 }
246 321
247 void FillTestDirectory( 322 void FillTestDirectory(
248 const FilePath& root_path, 323 const FilePath& root_path,
249 std::set<FilePath::StringType>* files, 324 std::set<FilePath::StringType>* files,
250 std::set<FilePath::StringType>* directories) { 325 std::set<FilePath::StringType>* directories) {
251 scoped_ptr<FileSystemOperationContext> context; 326 scoped_ptr<FileSystemOperationContext> context;
252 context.reset(NewContext()); 327 context.reset(NewContext(NULL));
253 std::vector<base::FileUtilProxy::Entry> entries; 328 std::vector<base::FileUtilProxy::Entry> entries;
254 EXPECT_EQ(base::PLATFORM_FILE_OK, 329 EXPECT_EQ(base::PLATFORM_FILE_OK,
255 ofsfu()->ReadDirectory(context.get(), root_path, &entries)); 330 ofsfu()->ReadDirectory(context.get(), root_path, &entries));
256 EXPECT_EQ(0UL, entries.size()); 331 EXPECT_EQ(0UL, entries.size());
257 332
258 files->clear(); 333 files->clear();
259 files->insert(FILE_PATH_LITERAL("first")); 334 files->insert(FILE_PATH_LITERAL("first"));
260 files->insert(FILE_PATH_LITERAL("second")); 335 files->insert(FILE_PATH_LITERAL("second"));
261 files->insert(FILE_PATH_LITERAL("third")); 336 files->insert(FILE_PATH_LITERAL("third"));
262 directories->clear(); 337 directories->clear();
263 directories->insert(FILE_PATH_LITERAL("fourth")); 338 directories->insert(FILE_PATH_LITERAL("fourth"));
264 directories->insert(FILE_PATH_LITERAL("fifth")); 339 directories->insert(FILE_PATH_LITERAL("fifth"));
265 directories->insert(FILE_PATH_LITERAL("sixth")); 340 directories->insert(FILE_PATH_LITERAL("sixth"));
266 std::set<FilePath::StringType>::iterator iter; 341 std::set<FilePath::StringType>::iterator iter;
267 for (iter = files->begin(); iter != files->end(); ++iter) { 342 for (iter = files->begin(); iter != files->end(); ++iter) {
268 bool created = false; 343 bool created = false;
269 context.reset(NewContext()); 344 context.reset(NewContext(NULL));
270 ASSERT_EQ(base::PLATFORM_FILE_OK, 345 ASSERT_EQ(base::PLATFORM_FILE_OK,
271 ofsfu()->EnsureFileExists( 346 ofsfu()->EnsureFileExists(
272 context.get(), root_path.Append(*iter), &created)); 347 context.get(), root_path.Append(*iter), &created));
273 ASSERT_TRUE(created); 348 ASSERT_TRUE(created);
274 } 349 }
275 for (iter = directories->begin(); iter != directories->end(); ++iter) { 350 for (iter = directories->begin(); iter != directories->end(); ++iter) {
276 bool exclusive = true; 351 bool exclusive = true;
277 bool recursive = false; 352 bool recursive = false;
278 context.reset(NewContext()); 353 context.reset(NewContext(NULL));
279 EXPECT_EQ(base::PLATFORM_FILE_OK, 354 EXPECT_EQ(base::PLATFORM_FILE_OK,
280 ofsfu()->CreateDirectory( 355 ofsfu()->CreateDirectory(
281 context.get(), root_path.Append(*iter), exclusive, recursive)); 356 context.get(), root_path.Append(*iter), exclusive, recursive));
282 } 357 }
283 ValidateTestDirectory(root_path, *files, *directories); 358 ValidateTestDirectory(root_path, *files, *directories);
284 } 359 }
285 360
286 void TestReadDirectoryHelper(const FilePath& root_path) { 361 void TestReadDirectoryHelper(const FilePath& root_path) {
287 std::set<FilePath::StringType> files; 362 std::set<FilePath::StringType> files;
288 std::set<FilePath::StringType> directories; 363 std::set<FilePath::StringType> directories;
289 FillTestDirectory(root_path, &files, &directories); 364 FillTestDirectory(root_path, &files, &directories);
290 365
291 scoped_ptr<FileSystemOperationContext> context; 366 scoped_ptr<FileSystemOperationContext> context;
292 std::vector<base::FileUtilProxy::Entry> entries; 367 std::vector<base::FileUtilProxy::Entry> entries;
293 context.reset(NewContext()); 368 context.reset(NewContext(NULL));
294 EXPECT_EQ(base::PLATFORM_FILE_OK, 369 EXPECT_EQ(base::PLATFORM_FILE_OK,
295 ofsfu()->ReadDirectory(context.get(), root_path, &entries)); 370 ofsfu()->ReadDirectory(context.get(), root_path, &entries));
296 std::vector<base::FileUtilProxy::Entry>::iterator entry_iter; 371 std::vector<base::FileUtilProxy::Entry>::iterator entry_iter;
297 EXPECT_EQ(files.size() + directories.size(), entries.size()); 372 EXPECT_EQ(files.size() + directories.size(), entries.size());
298 for (entry_iter = entries.begin(); entry_iter != entries.end(); 373 for (entry_iter = entries.begin(); entry_iter != entries.end();
299 ++entry_iter) { 374 ++entry_iter) {
300 const base::FileUtilProxy::Entry& entry = *entry_iter; 375 const base::FileUtilProxy::Entry& entry = *entry_iter;
301 std::set<FilePath::StringType>::iterator iter = files.find(entry.name); 376 std::set<FilePath::StringType>::iterator iter = files.find(entry.name);
302 if (iter != files.end()) { 377 if (iter != files.end()) {
303 EXPECT_FALSE(entry.is_directory); 378 EXPECT_FALSE(entry.is_directory);
304 files.erase(iter); 379 files.erase(iter);
305 continue; 380 continue;
306 } 381 }
307 iter = directories.find(entry.name); 382 iter = directories.find(entry.name);
308 EXPECT_FALSE(directories.end() == iter); 383 EXPECT_FALSE(directories.end() == iter);
309 EXPECT_TRUE(entry.is_directory); 384 EXPECT_TRUE(entry.is_directory);
310 directories.erase(iter); 385 directories.erase(iter);
311 } 386 }
312 } 387 }
313 388
314 void TestTouchHelper(const FilePath& path) { 389 void TestTouchHelper(const FilePath& path, bool new_file) {
315 base::Time last_access_time = base::Time::Now(); // Ignored, so not tested. 390 base::Time last_access_time = base::Time::Now(); // Ignored, so not tested.
316 base::Time last_modified_time = base::Time::Now(); 391 base::Time last_modified_time = base::Time::Now();
317 scoped_ptr<FileSystemOperationContext> context(NewContext()); 392 scoped_ptr<FileSystemOperationContext> context;
393
394 if (new_file) {
395 // Verify that file creation requires sufficient quota for the path.
396 context.reset(NewContext(NULL));
397 context->set_allowed_bytes_growth(
398 ObfuscatedFileSystemFileUtil::ComputeFilePathCost(path) - 1);
399 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
400 ofsfu()->Touch(
401 context.get(), path, last_access_time, last_modified_time));
402 }
403
404 context.reset(NewContext(NULL));
405 context->set_allowed_bytes_growth(
406 ObfuscatedFileSystemFileUtil::ComputeFilePathCost(path));
318 EXPECT_EQ(base::PLATFORM_FILE_OK, 407 EXPECT_EQ(base::PLATFORM_FILE_OK,
319 ofsfu()->Touch( 408 ofsfu()->Touch(
320 context.get(), path, last_access_time, last_modified_time)); 409 context.get(), path, last_access_time, last_modified_time));
321 FilePath local_path; 410 FilePath local_path;
322 base::PlatformFileInfo file_info; 411 base::PlatformFileInfo file_info;
323 context.reset(NewContext()); 412 context.reset(NewContext(NULL));
324 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->GetFileInfo( 413 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->GetFileInfo(
325 context.get(), path, &file_info, &local_path)); 414 context.get(), path, &file_info, &local_path));
326 // We compare as time_t here to lower our resolution, to avoid false 415 // We compare as time_t here to lower our resolution, to avoid false
327 // negatives caused by conversion to the local filesystem's native 416 // negatives caused by conversion to the local filesystem's native
328 // representation and back. 417 // representation and back.
329 EXPECT_EQ(file_info.last_modified.ToTimeT(), last_modified_time.ToTimeT()); 418 EXPECT_EQ(file_info.last_modified.ToTimeT(), last_modified_time.ToTimeT());
330 419
331 context.reset(NewContext()); 420 context.reset(NewContext(NULL));
332 last_modified_time += base::TimeDelta::FromHours(1); 421 last_modified_time += base::TimeDelta::FromHours(1);
333 EXPECT_EQ(base::PLATFORM_FILE_OK, 422 EXPECT_EQ(base::PLATFORM_FILE_OK,
334 ofsfu()->Touch( 423 ofsfu()->Touch(
335 context.get(), path, last_access_time, last_modified_time)); 424 context.get(), path, last_access_time, last_modified_time));
336 context.reset(NewContext()); 425 context.reset(NewContext(NULL));
337 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->GetFileInfo( 426 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->GetFileInfo(
338 context.get(), path, &file_info, &local_path)); 427 context.get(), path, &file_info, &local_path));
339 EXPECT_EQ(file_info.last_modified.ToTimeT(), last_modified_time.ToTimeT()); 428 EXPECT_EQ(file_info.last_modified.ToTimeT(), last_modified_time.ToTimeT());
340 } 429 }
341 430
342 void TestCopyInForeignFileHelper(bool overwrite) { 431 void TestCopyInForeignFileHelper(bool overwrite) {
343 ScopedTempDir source_dir; 432 ScopedTempDir source_dir;
344 ASSERT_TRUE(source_dir.CreateUniqueTempDir()); 433 ASSERT_TRUE(source_dir.CreateUniqueTempDir());
345 FilePath root_path = source_dir.path(); 434 FilePath root_path = source_dir.path();
346 FilePath src_path = root_path.AppendASCII("file_name"); 435 FilePath src_path = root_path.AppendASCII("file_name");
347 FilePath dest_path(FILE_PATH_LITERAL("new file")); 436 FilePath dest_path(FILE_PATH_LITERAL("new file"));
348 int64 src_file_length = 87; 437 int64 src_file_length = 87;
349 438
350 base::PlatformFileError error_code; 439 base::PlatformFileError error_code;
351 bool created = false; 440 bool created = false;
352 int file_flags = base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE; 441 int file_flags = base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE;
353 base::PlatformFile file_handle = 442 base::PlatformFile file_handle =
354 base::CreatePlatformFile( 443 base::CreatePlatformFile(
355 src_path, file_flags, &created, &error_code); 444 src_path, file_flags, &created, &error_code);
356 EXPECT_TRUE(created); 445 EXPECT_TRUE(created);
357 ASSERT_EQ(base::PLATFORM_FILE_OK, error_code); 446 ASSERT_EQ(base::PLATFORM_FILE_OK, error_code);
358 ASSERT_NE(base::kInvalidPlatformFileValue, file_handle); 447 ASSERT_NE(base::kInvalidPlatformFileValue, file_handle);
359 ASSERT_TRUE(base::TruncatePlatformFile(file_handle, src_file_length)); 448 ASSERT_TRUE(base::TruncatePlatformFile(file_handle, src_file_length));
360 EXPECT_TRUE(base::ClosePlatformFile(file_handle)); 449 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
361 450
362 scoped_ptr<FileSystemOperationContext> context; 451 scoped_ptr<FileSystemOperationContext> context;
363 452
364 if (overwrite) { 453 if (overwrite) {
365 context.reset(NewContext()); 454 context.reset(NewContext(NULL));
366 EXPECT_EQ(base::PLATFORM_FILE_OK, 455 EXPECT_EQ(base::PLATFORM_FILE_OK,
367 ofsfu()->EnsureFileExists(context.get(), dest_path, &created)); 456 ofsfu()->EnsureFileExists(context.get(), dest_path, &created));
368 EXPECT_TRUE(created); 457 EXPECT_TRUE(created);
369 } 458 }
370 459
371 context.reset(NewContext()); 460 const int64 path_cost =
461 ObfuscatedFileSystemFileUtil::ComputeFilePathCost(dest_path);
462 if (!overwrite) {
463 // Verify that file creation requires sufficient quota for the path.
464 context.reset(NewContext(NULL));
465 context->set_allowed_bytes_growth(path_cost + src_file_length - 1);
466 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
467 ofsfu()->CopyInForeignFile(context.get(), src_path, dest_path));
468 }
469
470 context.reset(NewContext(NULL));
471 context->set_allowed_bytes_growth(path_cost + src_file_length);
372 EXPECT_EQ(base::PLATFORM_FILE_OK, 472 EXPECT_EQ(base::PLATFORM_FILE_OK,
373 ofsfu()->CopyInForeignFile(context.get(), src_path, dest_path)); 473 ofsfu()->CopyInForeignFile(context.get(), src_path, dest_path));
374 context.reset(NewContext()); 474
475 context.reset(NewContext(NULL));
375 EXPECT_TRUE(ofsfu()->PathExists(context.get(), dest_path)); 476 EXPECT_TRUE(ofsfu()->PathExists(context.get(), dest_path));
376 context.reset(NewContext()); 477 context.reset(NewContext(NULL));
377 EXPECT_FALSE(ofsfu()->DirectoryExists(context.get(), dest_path)); 478 EXPECT_FALSE(ofsfu()->DirectoryExists(context.get(), dest_path));
378 context.reset(NewContext()); 479 context.reset(NewContext(NULL));
379 base::PlatformFileInfo file_info; 480 base::PlatformFileInfo file_info;
380 FilePath data_path; 481 FilePath data_path;
381 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->GetFileInfo( 482 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->GetFileInfo(
382 context.get(), dest_path, &file_info, &data_path)); 483 context.get(), dest_path, &file_info, &data_path));
383 EXPECT_NE(data_path, src_path); 484 EXPECT_NE(data_path, src_path);
384 EXPECT_TRUE(FileExists(data_path)); 485 EXPECT_TRUE(FileExists(data_path));
385 EXPECT_EQ(src_file_length, GetSize(data_path)); 486 EXPECT_EQ(src_file_length, GetSize(data_path));
386 487
387 EXPECT_EQ(base::PLATFORM_FILE_OK, 488 EXPECT_EQ(base::PLATFORM_FILE_OK,
388 ofsfu()->DeleteFile(context.get(), dest_path)); 489 ofsfu()->DeleteFile(context.get(), dest_path));
389 } 490 }
390 491
391 private: 492 private:
392 ScopedTempDir data_dir_; 493 ScopedTempDir data_dir_;
393 scoped_refptr<ObfuscatedFileSystemFileUtil> obfuscated_file_system_file_util_; 494 scoped_refptr<ObfuscatedFileSystemFileUtil> obfuscated_file_system_file_util_;
495 scoped_refptr<quota::QuotaManager> quota_manager_;
496 scoped_refptr<FileSystemContext> file_system_context_;
394 GURL origin_; 497 GURL origin_;
395 fileapi::FileSystemType type_; 498 fileapi::FileSystemType type_;
499 base::ScopedCallbackFactory<ObfuscatedFileSystemFileUtilTest>
500 callback_factory_;
396 FileSystemTestOriginHelper test_helper_; 501 FileSystemTestOriginHelper test_helper_;
502 quota::QuotaStatusCode quota_status_;
503 int64 usage_;
397 504
398 DISALLOW_COPY_AND_ASSIGN(ObfuscatedFileSystemFileUtilTest); 505 DISALLOW_COPY_AND_ASSIGN(ObfuscatedFileSystemFileUtilTest);
399 }; 506 };
400 507
401 TEST_F(ObfuscatedFileSystemFileUtilTest, TestCreateAndDeleteFile) { 508 TEST_F(ObfuscatedFileSystemFileUtilTest, TestCreateAndDeleteFile) {
402 base::PlatformFile file_handle = base::kInvalidPlatformFileValue; 509 base::PlatformFile file_handle = base::kInvalidPlatformFileValue;
403 bool created; 510 bool created;
404 FilePath path = UTF8ToFilePath("fake/file"); 511 FilePath path = UTF8ToFilePath("fake/file");
405 scoped_ptr<FileSystemOperationContext> context(NewContext()); 512 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
406 int file_flags = base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE; 513 int file_flags = base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE;
407 514
408 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, 515 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
409 ofsfu()->CreateOrOpen( 516 ofsfu()->CreateOrOpen(
410 context.get(), path, file_flags, &file_handle, 517 context.get(), path, file_flags, &file_handle,
411 &created)); 518 &created));
412 519
413 context.reset(NewContext()); 520 context.reset(NewContext(NULL));
414 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, 521 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
415 ofsfu()->DeleteFile(context.get(), path)); 522 ofsfu()->DeleteFile(context.get(), path));
416 523
417 path = UTF8ToFilePath("test file"); 524 path = UTF8ToFilePath("test file");
418 525
419 context.reset(NewContext()); 526 // Verify that file creation requires sufficient quota for the path.
527 context.reset(NewContext(NULL));
528 context->set_allowed_bytes_growth(
529 ObfuscatedFileSystemFileUtil::ComputeFilePathCost(path) - 1);
530 ASSERT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
531 ofsfu()->CreateOrOpen(
532 context.get(), path, file_flags, &file_handle, &created));
533
534 context.reset(NewContext(NULL));
535 context->set_allowed_bytes_growth(
536 ObfuscatedFileSystemFileUtil::ComputeFilePathCost(path));
420 ASSERT_EQ(base::PLATFORM_FILE_OK, 537 ASSERT_EQ(base::PLATFORM_FILE_OK,
421 ofsfu()->CreateOrOpen( 538 ofsfu()->CreateOrOpen(
422 context.get(), path, file_flags, &file_handle, &created)); 539 context.get(), path, file_flags, &file_handle, &created));
423 ASSERT_TRUE(created); 540 ASSERT_TRUE(created);
424 EXPECT_NE(base::kInvalidPlatformFileValue, file_handle); 541 EXPECT_NE(base::kInvalidPlatformFileValue, file_handle);
425 542
426 CheckFileAndCloseHandle(path, file_handle); 543 CheckFileAndCloseHandle(path, file_handle);
427 544
428 context.reset(NewContext()); 545 context.reset(NewContext(NULL));
429 FilePath local_path; 546 FilePath local_path;
430 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->GetLocalFilePath( 547 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->GetLocalFilePath(
431 context.get(), path, &local_path)); 548 context.get(), path, &local_path));
432 EXPECT_TRUE(file_util::PathExists(local_path)); 549 EXPECT_TRUE(file_util::PathExists(local_path));
433 550
434 context.reset(NewContext()); 551 // Verify that deleting a file isn't stopped by zero quota, and that it frees
552 // up quote from its path.
553 context.reset(NewContext(NULL));
554 context->set_allowed_bytes_growth(0);
435 EXPECT_EQ(base::PLATFORM_FILE_OK, 555 EXPECT_EQ(base::PLATFORM_FILE_OK,
436 ofsfu()->DeleteFile(context.get(), path)); 556 ofsfu()->DeleteFile(context.get(), path));
437 EXPECT_FALSE(file_util::PathExists(local_path)); 557 EXPECT_FALSE(file_util::PathExists(local_path));
558 EXPECT_EQ(ObfuscatedFileSystemFileUtil::ComputeFilePathCost(path),
559 context->allowed_bytes_growth());
438 560
439 context.reset(NewContext()); 561 context.reset(NewContext(NULL));
440 bool exclusive = true; 562 bool exclusive = true;
441 bool recursive = true; 563 bool recursive = true;
442 FilePath directory_path = UTF8ToFilePath("series/of/directories"); 564 FilePath directory_path = UTF8ToFilePath("series/of/directories");
443 path = directory_path.AppendASCII("file name"); 565 path = directory_path.AppendASCII("file name");
444 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->CreateDirectory( 566 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->CreateDirectory(
445 context.get(), directory_path, exclusive, recursive)); 567 context.get(), directory_path, exclusive, recursive));
446 568
447 context.reset(NewContext()); 569 context.reset(NewContext(NULL));
448 file_handle = base::kInvalidPlatformFileValue; 570 file_handle = base::kInvalidPlatformFileValue;
449 ASSERT_EQ(base::PLATFORM_FILE_OK, 571 ASSERT_EQ(base::PLATFORM_FILE_OK,
450 ofsfu()->CreateOrOpen( 572 ofsfu()->CreateOrOpen(
451 context.get(), path, file_flags, &file_handle, &created)); 573 context.get(), path, file_flags, &file_handle, &created));
452 ASSERT_TRUE(created); 574 ASSERT_TRUE(created);
453 EXPECT_NE(base::kInvalidPlatformFileValue, file_handle); 575 EXPECT_NE(base::kInvalidPlatformFileValue, file_handle);
454 576
455 CheckFileAndCloseHandle(path, file_handle); 577 CheckFileAndCloseHandle(path, file_handle);
456 578
457 context.reset(NewContext()); 579 context.reset(NewContext(NULL));
458 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->GetLocalFilePath( 580 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->GetLocalFilePath(
459 context.get(), path, &local_path)); 581 context.get(), path, &local_path));
460 EXPECT_TRUE(file_util::PathExists(local_path)); 582 EXPECT_TRUE(file_util::PathExists(local_path));
461 583
462 context.reset(NewContext()); 584 context.reset(NewContext(NULL));
463 EXPECT_EQ(base::PLATFORM_FILE_OK, 585 EXPECT_EQ(base::PLATFORM_FILE_OK,
464 ofsfu()->DeleteFile(context.get(), path)); 586 ofsfu()->DeleteFile(context.get(), path));
465 EXPECT_FALSE(file_util::PathExists(local_path)); 587 EXPECT_FALSE(file_util::PathExists(local_path));
466 } 588 }
467 589
468 TEST_F(ObfuscatedFileSystemFileUtilTest, TestTruncate) { 590 TEST_F(ObfuscatedFileSystemFileUtilTest, TestTruncate) {
469 bool created = false; 591 bool created = false;
470 FilePath path = UTF8ToFilePath("file"); 592 FilePath path = UTF8ToFilePath("file");
471 scoped_ptr<FileSystemOperationContext> context(NewContext()); 593 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
472 594
473 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, 595 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
474 ofsfu()->Truncate(context.get(), path, 4)); 596 ofsfu()->Truncate(context.get(), path, 4));
475 597
476 context.reset(NewContext()); 598 context.reset(NewContext(NULL));
477 ASSERT_EQ(base::PLATFORM_FILE_OK, 599 ASSERT_EQ(base::PLATFORM_FILE_OK,
478 ofsfu()->EnsureFileExists(context.get(), path, &created)); 600 ofsfu()->EnsureFileExists(context.get(), path, &created));
479 ASSERT_TRUE(created); 601 ASSERT_TRUE(created);
480 602
481 context.reset(NewContext()); 603 context.reset(NewContext(NULL));
482 FilePath local_path; 604 FilePath local_path;
483 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->GetLocalFilePath( 605 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->GetLocalFilePath(
484 context.get(), path, &local_path)); 606 context.get(), path, &local_path));
485 EXPECT_EQ(0, GetSize(local_path)); 607 EXPECT_EQ(0, GetSize(local_path));
486 608
487 context.reset(NewContext()); 609 context.reset(NewContext(NULL));
488 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->Truncate( 610 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->Truncate(
489 context.get(), path, 10)); 611 context.get(), path, 10));
490 EXPECT_EQ(10, GetSize(local_path)); 612 EXPECT_EQ(10, GetSize(local_path));
491 613
492 context.reset(NewContext()); 614 context.reset(NewContext(NULL));
493 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->Truncate( 615 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->Truncate(
494 context.get(), path, 1)); 616 context.get(), path, 1));
495 EXPECT_EQ(1, GetSize(local_path)); 617 EXPECT_EQ(1, GetSize(local_path));
496 618
497 context.reset(NewContext()); 619 context.reset(NewContext(NULL));
498 EXPECT_FALSE(ofsfu()->DirectoryExists(context.get(), path)); 620 EXPECT_FALSE(ofsfu()->DirectoryExists(context.get(), path));
499 context.reset(NewContext()); 621 context.reset(NewContext(NULL));
500 EXPECT_TRUE(ofsfu()->PathExists(context.get(), path)); 622 EXPECT_TRUE(ofsfu()->PathExists(context.get(), path));
501 } 623 }
502 624
503 TEST_F(ObfuscatedFileSystemFileUtilTest, TestEnsureFileExists) { 625 TEST_F(ObfuscatedFileSystemFileUtilTest, TestEnsureFileExists) {
504 FilePath path = UTF8ToFilePath("fake/file"); 626 FilePath path = UTF8ToFilePath("fake/file");
505 bool created = false; 627 bool created = false;
506 scoped_ptr<FileSystemOperationContext> context(NewContext()); 628 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
507 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, 629 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
508 ofsfu()->EnsureFileExists( 630 ofsfu()->EnsureFileExists(
509 context.get(), path, &created)); 631 context.get(), path, &created));
510 632
511 context.reset(NewContext()); 633 // Verify that file creation requires sufficient quota for the path.
634 context.reset(NewContext(NULL));
512 path = UTF8ToFilePath("test file"); 635 path = UTF8ToFilePath("test file");
513 created = false; 636 created = false;
637 context->set_allowed_bytes_growth(
638 ObfuscatedFileSystemFileUtil::ComputeFilePathCost(path) - 1);
639 ASSERT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
640 ofsfu()->EnsureFileExists(context.get(), path, &created));
641 ASSERT_FALSE(created);
642
643 context.reset(NewContext(NULL));
644 context->set_allowed_bytes_growth(
645 ObfuscatedFileSystemFileUtil::ComputeFilePathCost(path));
514 ASSERT_EQ(base::PLATFORM_FILE_OK, 646 ASSERT_EQ(base::PLATFORM_FILE_OK,
515 ofsfu()->EnsureFileExists(context.get(), path, &created)); 647 ofsfu()->EnsureFileExists(context.get(), path, &created));
516 ASSERT_TRUE(created); 648 ASSERT_TRUE(created);
517 649
518 CheckFileAndCloseHandle(path, base::kInvalidPlatformFileValue); 650 CheckFileAndCloseHandle(path, base::kInvalidPlatformFileValue);
519 651
520 context.reset(NewContext()); 652 context.reset(NewContext(NULL));
521 ASSERT_EQ(base::PLATFORM_FILE_OK, 653 ASSERT_EQ(base::PLATFORM_FILE_OK,
522 ofsfu()->EnsureFileExists(context.get(), path, &created)); 654 ofsfu()->EnsureFileExists(context.get(), path, &created));
523 ASSERT_FALSE(created); 655 ASSERT_FALSE(created);
524 656
525 // Also test in a subdirectory. 657 // Also test in a subdirectory.
526 path = UTF8ToFilePath("path/to/file.txt"); 658 path = UTF8ToFilePath("path/to/file.txt");
527 context.reset(NewContext()); 659 context.reset(NewContext(NULL));
528 bool exclusive = true; 660 bool exclusive = true;
529 bool recursive = true; 661 bool recursive = true;
530 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->CreateDirectory( 662 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->CreateDirectory(
531 context.get(), path.DirName(), exclusive, recursive)); 663 context.get(), path.DirName(), exclusive, recursive));
532 664
533 context.reset(NewContext()); 665 context.reset(NewContext(NULL));
534 ASSERT_EQ(base::PLATFORM_FILE_OK, 666 ASSERT_EQ(base::PLATFORM_FILE_OK,
535 ofsfu()->EnsureFileExists(context.get(), path, &created)); 667 ofsfu()->EnsureFileExists(context.get(), path, &created));
536 ASSERT_TRUE(created); 668 ASSERT_TRUE(created);
537 context.reset(NewContext()); 669 context.reset(NewContext(NULL));
538 EXPECT_FALSE(ofsfu()->DirectoryExists(context.get(), path)); 670 EXPECT_FALSE(ofsfu()->DirectoryExists(context.get(), path));
539 context.reset(NewContext()); 671 context.reset(NewContext(NULL));
540 EXPECT_TRUE(ofsfu()->PathExists(context.get(), path)); 672 EXPECT_TRUE(ofsfu()->PathExists(context.get(), path));
541 } 673 }
542 674
543 TEST_F(ObfuscatedFileSystemFileUtilTest, TestDirectoryOps) { 675 TEST_F(ObfuscatedFileSystemFileUtilTest, TestDirectoryOps) {
544 scoped_ptr<FileSystemOperationContext> context(NewContext()); 676 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
545 677
546 bool exclusive = false; 678 bool exclusive = false;
547 bool recursive = false; 679 bool recursive = false;
548 FilePath path = UTF8ToFilePath("foo/bar"); 680 FilePath path = UTF8ToFilePath("foo/bar");
549 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, ofsfu()->CreateDirectory( 681 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, ofsfu()->CreateDirectory(
550 context.get(), path, exclusive, recursive)); 682 context.get(), path, exclusive, recursive));
551 683
552 context.reset(NewContext()); 684 context.reset(NewContext(NULL));
553 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, 685 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
554 ofsfu()->DeleteSingleDirectory(context.get(), path)); 686 ofsfu()->DeleteSingleDirectory(context.get(), path));
555 687
556 FilePath root = UTF8ToFilePath(""); 688 FilePath root = UTF8ToFilePath("");
557 context.reset(NewContext()); 689 context.reset(NewContext(NULL));
558 EXPECT_FALSE(ofsfu()->DirectoryExists(context.get(), path)); 690 EXPECT_FALSE(ofsfu()->DirectoryExists(context.get(), path));
559 context.reset(NewContext()); 691 context.reset(NewContext(NULL));
560 EXPECT_FALSE(ofsfu()->PathExists(context.get(), path)); 692 EXPECT_FALSE(ofsfu()->PathExists(context.get(), path));
561 context.reset(NewContext()); 693 context.reset(NewContext(NULL));
562 EXPECT_TRUE(ofsfu()->IsDirectoryEmpty(context.get(), root)); 694 EXPECT_TRUE(ofsfu()->IsDirectoryEmpty(context.get(), root));
563 695
564 context.reset(NewContext()); 696 context.reset(NewContext(NULL));
565 exclusive = false; 697 exclusive = false;
566 recursive = true; 698 recursive = true;
567 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->CreateDirectory( 699 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->CreateDirectory(
568 context.get(), path, exclusive, recursive)); 700 context.get(), path, exclusive, recursive));
569 701
570 context.reset(NewContext()); 702 context.reset(NewContext(NULL));
571 EXPECT_TRUE(ofsfu()->DirectoryExists(context.get(), path)); 703 EXPECT_TRUE(ofsfu()->DirectoryExists(context.get(), path));
572 context.reset(NewContext()); 704 context.reset(NewContext(NULL));
573 EXPECT_TRUE(ofsfu()->PathExists(context.get(), path)); 705 EXPECT_TRUE(ofsfu()->PathExists(context.get(), path));
574 context.reset(NewContext()); 706 context.reset(NewContext(NULL));
575 EXPECT_FALSE(ofsfu()->IsDirectoryEmpty(context.get(), root)); 707 EXPECT_FALSE(ofsfu()->IsDirectoryEmpty(context.get(), root));
576 context.reset(NewContext()); 708 context.reset(NewContext(NULL));
577 EXPECT_TRUE(ofsfu()->DirectoryExists(context.get(), path.DirName())); 709 EXPECT_TRUE(ofsfu()->DirectoryExists(context.get(), path.DirName()));
578 context.reset(NewContext()); 710 context.reset(NewContext(NULL));
579 EXPECT_FALSE(ofsfu()->IsDirectoryEmpty(context.get(), path.DirName())); 711 EXPECT_FALSE(ofsfu()->IsDirectoryEmpty(context.get(), path.DirName()));
580 712
581 // Can't remove a non-empty directory. 713 // Can't remove a non-empty directory.
582 context.reset(NewContext()); 714 context.reset(NewContext(NULL));
583 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_EMPTY, 715 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_EMPTY,
584 ofsfu()->DeleteSingleDirectory(context.get(), path.DirName())); 716 ofsfu()->DeleteSingleDirectory(context.get(), path.DirName()));
585 717
586 base::PlatformFileInfo file_info; 718 base::PlatformFileInfo file_info;
587 FilePath local_path; 719 FilePath local_path;
588 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->GetFileInfo( 720 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->GetFileInfo(
589 context.get(), path, &file_info, &local_path)); 721 context.get(), path, &file_info, &local_path));
590 EXPECT_TRUE(local_path.empty()); 722 EXPECT_TRUE(local_path.empty());
591 EXPECT_TRUE(file_info.is_directory); 723 EXPECT_TRUE(file_info.is_directory);
592 EXPECT_FALSE(file_info.is_symbolic_link); 724 EXPECT_FALSE(file_info.is_symbolic_link);
593 725
594 // Same create again should succeed, since exclusive is false. 726 // Same create again should succeed, since exclusive is false.
595 context.reset(NewContext()); 727 context.reset(NewContext(NULL));
596 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->CreateDirectory( 728 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->CreateDirectory(
597 context.get(), path, exclusive, recursive)); 729 context.get(), path, exclusive, recursive));
598 730
599 exclusive = true; 731 exclusive = true;
600 recursive = true; 732 recursive = true;
601 context.reset(NewContext()); 733 context.reset(NewContext(NULL));
602 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofsfu()->CreateDirectory( 734 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofsfu()->CreateDirectory(
603 context.get(), path, exclusive, recursive)); 735 context.get(), path, exclusive, recursive));
604 736
605 context.reset(NewContext()); 737 // Verify that deleting a directory isn't stopped by zero quota, and that it
738 // frees up quota from its path.
739 context.reset(NewContext(NULL));
740 context->set_allowed_bytes_growth(0);
606 EXPECT_EQ(base::PLATFORM_FILE_OK, 741 EXPECT_EQ(base::PLATFORM_FILE_OK,
607 ofsfu()->DeleteSingleDirectory(context.get(), path)); 742 ofsfu()->DeleteSingleDirectory(context.get(), path));
743 EXPECT_EQ(ObfuscatedFileSystemFileUtil::ComputeFilePathCost(path),
744 context->allowed_bytes_growth());
608 745
609 path = UTF8ToFilePath("foo/bop"); 746 path = UTF8ToFilePath("foo/bop");
610 747
611 context.reset(NewContext()); 748 context.reset(NewContext(NULL));
612 EXPECT_FALSE(ofsfu()->DirectoryExists(context.get(), path)); 749 EXPECT_FALSE(ofsfu()->DirectoryExists(context.get(), path));
613 context.reset(NewContext()); 750 context.reset(NewContext(NULL));
614 EXPECT_FALSE(ofsfu()->PathExists(context.get(), path)); 751 EXPECT_FALSE(ofsfu()->PathExists(context.get(), path));
615 context.reset(NewContext()); 752 context.reset(NewContext(NULL));
616 EXPECT_TRUE(ofsfu()->IsDirectoryEmpty(context.get(), path)); 753 EXPECT_TRUE(ofsfu()->IsDirectoryEmpty(context.get(), path));
617 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, ofsfu()->GetFileInfo( 754 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, ofsfu()->GetFileInfo(
618 context.get(), path, &file_info, &local_path)); 755 context.get(), path, &file_info, &local_path));
619 756
757 // Verify that file creation requires sufficient quota for the path.
620 exclusive = true; 758 exclusive = true;
621 recursive = false; 759 recursive = false;
760 context.reset(NewContext(NULL));
761 context->set_allowed_bytes_growth(
762 ObfuscatedFileSystemFileUtil::ComputeFilePathCost(path) - 1);
763 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, ofsfu()->CreateDirectory(
764 context.get(), path, exclusive, recursive));
765
766 context.reset(NewContext(NULL));
767 context->set_allowed_bytes_growth(
768 ObfuscatedFileSystemFileUtil::ComputeFilePathCost(path));
622 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->CreateDirectory( 769 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->CreateDirectory(
623 context.get(), path, exclusive, recursive)); 770 context.get(), path, exclusive, recursive));
624 771
625 context.reset(NewContext()); 772 context.reset(NewContext(NULL));
626 EXPECT_TRUE(ofsfu()->DirectoryExists(context.get(), path)); 773 EXPECT_TRUE(ofsfu()->DirectoryExists(context.get(), path));
627 context.reset(NewContext()); 774 context.reset(NewContext(NULL));
628 EXPECT_TRUE(ofsfu()->PathExists(context.get(), path)); 775 EXPECT_TRUE(ofsfu()->PathExists(context.get(), path));
629 776
630 exclusive = true; 777 exclusive = true;
631 recursive = false; 778 recursive = false;
632 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofsfu()->CreateDirectory( 779 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofsfu()->CreateDirectory(
633 context.get(), path, exclusive, recursive)); 780 context.get(), path, exclusive, recursive));
634 781
635 exclusive = true; 782 exclusive = true;
636 recursive = false; 783 recursive = false;
637 path = UTF8ToFilePath("foo"); 784 path = UTF8ToFilePath("foo");
638 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofsfu()->CreateDirectory( 785 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofsfu()->CreateDirectory(
639 context.get(), path, exclusive, recursive)); 786 context.get(), path, exclusive, recursive));
640 787
641 path = UTF8ToFilePath("blah"); 788 path = UTF8ToFilePath("blah");
642 789
643 context.reset(NewContext()); 790 context.reset(NewContext(NULL));
644 EXPECT_FALSE(ofsfu()->DirectoryExists(context.get(), path)); 791 EXPECT_FALSE(ofsfu()->DirectoryExists(context.get(), path));
645 context.reset(NewContext()); 792 context.reset(NewContext(NULL));
646 EXPECT_FALSE(ofsfu()->PathExists(context.get(), path)); 793 EXPECT_FALSE(ofsfu()->PathExists(context.get(), path));
647 794
648 exclusive = true; 795 exclusive = true;
649 recursive = false; 796 recursive = false;
650 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->CreateDirectory( 797 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->CreateDirectory(
651 context.get(), path, exclusive, recursive)); 798 context.get(), path, exclusive, recursive));
652 799
653 context.reset(NewContext()); 800 context.reset(NewContext(NULL));
654 EXPECT_TRUE(ofsfu()->DirectoryExists(context.get(), path)); 801 EXPECT_TRUE(ofsfu()->DirectoryExists(context.get(), path));
655 context.reset(NewContext()); 802 context.reset(NewContext(NULL));
656 EXPECT_TRUE(ofsfu()->PathExists(context.get(), path)); 803 EXPECT_TRUE(ofsfu()->PathExists(context.get(), path));
657 804
658 exclusive = true; 805 exclusive = true;
659 recursive = false; 806 recursive = false;
660 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofsfu()->CreateDirectory( 807 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofsfu()->CreateDirectory(
661 context.get(), path, exclusive, recursive)); 808 context.get(), path, exclusive, recursive));
662 } 809 }
663 810
664 TEST_F(ObfuscatedFileSystemFileUtilTest, TestReadDirectory) { 811 TEST_F(ObfuscatedFileSystemFileUtilTest, TestReadDirectory) {
665 scoped_ptr<FileSystemOperationContext> context(NewContext()); 812 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
666 bool exclusive = true; 813 bool exclusive = true;
667 bool recursive = true; 814 bool recursive = true;
668 FilePath path = UTF8ToFilePath("directory/to/use"); 815 FilePath path = UTF8ToFilePath("directory/to/use");
669 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->CreateDirectory( 816 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->CreateDirectory(
670 context.get(), path, exclusive, recursive)); 817 context.get(), path, exclusive, recursive));
671 TestReadDirectoryHelper(path); 818 TestReadDirectoryHelper(path);
672 } 819 }
673 820
674 TEST_F(ObfuscatedFileSystemFileUtilTest, TestReadRootWithSlash) { 821 TEST_F(ObfuscatedFileSystemFileUtilTest, TestReadRootWithSlash) {
675 TestReadDirectoryHelper(UTF8ToFilePath("")); 822 TestReadDirectoryHelper(UTF8ToFilePath(""));
676 } 823 }
677 824
678 TEST_F(ObfuscatedFileSystemFileUtilTest, TestReadRootWithEmptyString) { 825 TEST_F(ObfuscatedFileSystemFileUtilTest, TestReadRootWithEmptyString) {
679 TestReadDirectoryHelper(UTF8ToFilePath("/")); 826 TestReadDirectoryHelper(UTF8ToFilePath("/"));
680 } 827 }
681 828
682 TEST_F(ObfuscatedFileSystemFileUtilTest, TestReadDirectoryOnFile) { 829 TEST_F(ObfuscatedFileSystemFileUtilTest, TestReadDirectoryOnFile) {
683 FilePath path = UTF8ToFilePath("file"); 830 FilePath path = UTF8ToFilePath("file");
684 scoped_ptr<FileSystemOperationContext> context(NewContext()); 831 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
685 832
686 bool created = false; 833 bool created = false;
687 ASSERT_EQ(base::PLATFORM_FILE_OK, 834 ASSERT_EQ(base::PLATFORM_FILE_OK,
688 ofsfu()->EnsureFileExists(context.get(), path, &created)); 835 ofsfu()->EnsureFileExists(context.get(), path, &created));
689 ASSERT_TRUE(created); 836 ASSERT_TRUE(created);
690 837
691 context.reset(NewContext()); 838 context.reset(NewContext(NULL));
692 std::vector<base::FileUtilProxy::Entry> entries; 839 std::vector<base::FileUtilProxy::Entry> entries;
693 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, 840 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
694 ofsfu()->ReadDirectory(context.get(), path, &entries)); 841 ofsfu()->ReadDirectory(context.get(), path, &entries));
695 842
696 EXPECT_TRUE(ofsfu()->IsDirectoryEmpty(context.get(), path)); 843 EXPECT_TRUE(ofsfu()->IsDirectoryEmpty(context.get(), path));
697 } 844 }
698 845
699 TEST_F(ObfuscatedFileSystemFileUtilTest, TestTouch) { 846 TEST_F(ObfuscatedFileSystemFileUtilTest, TestTouch) {
700 FilePath path = UTF8ToFilePath("fake/file"); 847 FilePath path = UTF8ToFilePath("fake/file");
701 base::Time last_access_time = base::Time::Now(); // Ignored, so not tested. 848 base::Time last_access_time = base::Time::Now(); // Ignored, so not tested.
702 base::Time last_modified_time = base::Time::Now(); 849 base::Time last_modified_time = base::Time::Now();
703 scoped_ptr<FileSystemOperationContext> context(NewContext()); 850 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
704 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, 851 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
705 ofsfu()->Touch( 852 ofsfu()->Touch(
706 context.get(), path, last_access_time, last_modified_time)); 853 context.get(), path, last_access_time, last_modified_time));
707 854
708 // Touch will create a file if it's not there but its parent is. 855 // Touch will create a file if it's not there but its parent is.
856 bool new_file = true;
709 path = UTF8ToFilePath("file name"); 857 path = UTF8ToFilePath("file name");
710 TestTouchHelper(path); 858 TestTouchHelper(path, new_file);
711 859
712 bool exclusive = true; 860 bool exclusive = true;
713 bool recursive = true; 861 bool recursive = true;
714 path = UTF8ToFilePath("directory/to/use"); 862 path = UTF8ToFilePath("directory/to/use");
715 context.reset(NewContext()); 863 context.reset(NewContext(NULL));
716 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->CreateDirectory( 864 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->CreateDirectory(
717 context.get(), path, exclusive, recursive)); 865 context.get(), path, exclusive, recursive));
718 TestTouchHelper(path); 866 new_file = false;
867 TestTouchHelper(path, new_file);
868 }
869
870 TEST_F(ObfuscatedFileSystemFileUtilTest, TestPathQuotas) {
871 FilePath path = UTF8ToFilePath("fake/file");
872 base::Time last_access_time = base::Time::Now();
873 base::Time last_modified_time = base::Time::Now();
874 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
875
876 // Touch will create a file if it's not there but its parent is.
877 path = UTF8ToFilePath("file name");
878 context->set_allowed_bytes_growth(5);
879 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
880 ofsfu()->Touch(
881 context.get(), path, last_access_time, last_modified_time));
882 context->set_allowed_bytes_growth(1024);
883 EXPECT_EQ(base::PLATFORM_FILE_OK,
884 ofsfu()->Touch(
885 context.get(), path, last_access_time, last_modified_time));
886 int64 path_cost = ObfuscatedFileSystemFileUtil::ComputeFilePathCost(path);
887 EXPECT_EQ(1024 - path_cost, context->allowed_bytes_growth());
888
889 context->set_allowed_bytes_growth(1024);
890 bool exclusive = true;
891 bool recursive = true;
892 path = UTF8ToFilePath("directory/to/use");
893 std::vector<FilePath::StringType> components;
894 path.GetComponents(&components);
895 path_cost = 0;
896 for (std::vector<FilePath::StringType>::iterator iter = components.begin();
897 iter != components.end(); ++iter) {
898 path_cost += ObfuscatedFileSystemFileUtil::ComputeFilePathCost(
899 FilePath(*iter));
900 }
901 context.reset(NewContext(NULL));
902 context->set_allowed_bytes_growth(1024);
903 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->CreateDirectory(
904 context.get(), path, exclusive, recursive));
905 EXPECT_EQ(1024 - path_cost, context->allowed_bytes_growth());
719 } 906 }
720 907
721 TEST_F(ObfuscatedFileSystemFileUtilTest, TestCopyOrMoveFileNotFound) { 908 TEST_F(ObfuscatedFileSystemFileUtilTest, TestCopyOrMoveFileNotFound) {
722 FilePath source_path = UTF8ToFilePath("path0.txt"); 909 FilePath source_path = UTF8ToFilePath("path0.txt");
723 FilePath dest_path = UTF8ToFilePath("path1.txt"); 910 FilePath dest_path = UTF8ToFilePath("path1.txt");
724 scoped_ptr<FileSystemOperationContext> context(NewContext()); 911 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
725 912
726 bool is_copy_not_move = false; 913 bool is_copy_not_move = false;
727 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, 914 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
728 ofsfu()->CopyOrMoveFile(context.get(), source_path, dest_path, 915 ofsfu()->CopyOrMoveFile(context.get(), source_path, dest_path,
729 is_copy_not_move)); 916 is_copy_not_move));
730 context.reset(NewContext()); 917 context.reset(NewContext(NULL));
731 is_copy_not_move = true; 918 is_copy_not_move = true;
732 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, 919 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
733 ofsfu()->CopyOrMoveFile(context.get(), source_path, dest_path, 920 ofsfu()->CopyOrMoveFile(context.get(), source_path, dest_path,
734 is_copy_not_move)); 921 is_copy_not_move));
735 source_path = UTF8ToFilePath("dir/dir/file"); 922 source_path = UTF8ToFilePath("dir/dir/file");
736 bool exclusive = true; 923 bool exclusive = true;
737 bool recursive = true; 924 bool recursive = true;
738 context.reset(NewContext()); 925 context.reset(NewContext(NULL));
739 ASSERT_EQ(base::PLATFORM_FILE_OK, ofsfu()->CreateDirectory( 926 ASSERT_EQ(base::PLATFORM_FILE_OK, ofsfu()->CreateDirectory(
740 context.get(), source_path.DirName(), exclusive, recursive)); 927 context.get(), source_path.DirName(), exclusive, recursive));
741 is_copy_not_move = false; 928 is_copy_not_move = false;
742 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, 929 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
743 ofsfu()->CopyOrMoveFile(context.get(), source_path, dest_path, 930 ofsfu()->CopyOrMoveFile(context.get(), source_path, dest_path,
744 is_copy_not_move)); 931 is_copy_not_move));
745 context.reset(NewContext()); 932 context.reset(NewContext(NULL));
746 is_copy_not_move = true; 933 is_copy_not_move = true;
747 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, 934 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND,
748 ofsfu()->CopyOrMoveFile(context.get(), source_path, dest_path, 935 ofsfu()->CopyOrMoveFile(context.get(), source_path, dest_path,
749 is_copy_not_move)); 936 is_copy_not_move));
750 } 937 }
751 938
752 TEST_F(ObfuscatedFileSystemFileUtilTest, TestCopyOrMoveFileSuccess) { 939 TEST_F(ObfuscatedFileSystemFileUtilTest, TestCopyOrMoveFileSuccess) {
753 const int64 kSourceLength = 5; 940 const int64 kSourceLength = 5;
754 const int64 kDestLength = 50; 941 const int64 kDestLength = 50;
755 942
756 for (size_t i = 0; i < arraysize(kCopyMoveTestCases); ++i) { 943 for (size_t i = 0; i < arraysize(kCopyMoveTestCases); ++i) {
757 SCOPED_TRACE(testing::Message() << "kCopyMoveTestCase " << i); 944 SCOPED_TRACE(testing::Message() << "kCopyMoveTestCase " << i);
758 const CopyMoveTestCaseRecord& test_case = kCopyMoveTestCases[i]; 945 const CopyMoveTestCaseRecord& test_case = kCopyMoveTestCases[i];
759 SCOPED_TRACE(testing::Message() << "\t is_copy_not_move " << 946 SCOPED_TRACE(testing::Message() << "\t is_copy_not_move " <<
760 test_case.is_copy_not_move); 947 test_case.is_copy_not_move);
761 SCOPED_TRACE(testing::Message() << "\t source_path " << 948 SCOPED_TRACE(testing::Message() << "\t source_path " <<
762 test_case.source_path); 949 test_case.source_path);
763 SCOPED_TRACE(testing::Message() << "\t dest_path " << 950 SCOPED_TRACE(testing::Message() << "\t dest_path " <<
764 test_case.dest_path); 951 test_case.dest_path);
765 SCOPED_TRACE(testing::Message() << "\t cause_overwrite " << 952 SCOPED_TRACE(testing::Message() << "\t cause_overwrite " <<
766 test_case.cause_overwrite); 953 test_case.cause_overwrite);
767 scoped_ptr<FileSystemOperationContext> context(NewContext()); 954 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
768 955
769 bool exclusive = false; 956 bool exclusive = false;
770 bool recursive = true; 957 bool recursive = true;
771 FilePath source_path = UTF8ToFilePath(test_case.source_path); 958 FilePath source_path = UTF8ToFilePath(test_case.source_path);
772 FilePath dest_path = UTF8ToFilePath(test_case.dest_path); 959 FilePath dest_path = UTF8ToFilePath(test_case.dest_path);
773 960
774 context.reset(NewContext()); 961 context.reset(NewContext(NULL));
775 ASSERT_EQ(base::PLATFORM_FILE_OK, ofsfu()->CreateDirectory( 962 ASSERT_EQ(base::PLATFORM_FILE_OK, ofsfu()->CreateDirectory(
776 context.get(), source_path.DirName(), exclusive, recursive)); 963 context.get(), source_path.DirName(), exclusive, recursive));
777 context.reset(NewContext()); 964 context.reset(NewContext(NULL));
778 ASSERT_EQ(base::PLATFORM_FILE_OK, ofsfu()->CreateDirectory( 965 ASSERT_EQ(base::PLATFORM_FILE_OK, ofsfu()->CreateDirectory(
779 context.get(), dest_path.DirName(), exclusive, recursive)); 966 context.get(), dest_path.DirName(), exclusive, recursive));
780 967
781 bool created = false; 968 bool created = false;
782 context.reset(NewContext()); 969 context.reset(NewContext(NULL));
783 ASSERT_EQ(base::PLATFORM_FILE_OK, 970 ASSERT_EQ(base::PLATFORM_FILE_OK,
784 ofsfu()->EnsureFileExists(context.get(), source_path, &created)); 971 ofsfu()->EnsureFileExists(context.get(), source_path, &created));
785 ASSERT_TRUE(created); 972 ASSERT_TRUE(created);
786 context.reset(NewContext()); 973 context.reset(NewContext(NULL));
787 ASSERT_EQ(base::PLATFORM_FILE_OK, 974 ASSERT_EQ(base::PLATFORM_FILE_OK,
788 ofsfu()->Truncate(context.get(), source_path, kSourceLength)); 975 ofsfu()->Truncate(context.get(), source_path, kSourceLength));
789 976
790 if (test_case.cause_overwrite) { 977 if (test_case.cause_overwrite) {
791 context.reset(NewContext()); 978 context.reset(NewContext(NULL));
792 created = false; 979 created = false;
793 ASSERT_EQ(base::PLATFORM_FILE_OK, 980 ASSERT_EQ(base::PLATFORM_FILE_OK,
794 ofsfu()->EnsureFileExists(context.get(), dest_path, &created)); 981 ofsfu()->EnsureFileExists(context.get(), dest_path, &created));
795 ASSERT_TRUE(created); 982 ASSERT_TRUE(created);
796 context.reset(NewContext()); 983 context.reset(NewContext(NULL));
797 ASSERT_EQ(base::PLATFORM_FILE_OK, 984 ASSERT_EQ(base::PLATFORM_FILE_OK,
798 ofsfu()->Truncate(context.get(), dest_path, kDestLength)); 985 ofsfu()->Truncate(context.get(), dest_path, kDestLength));
799 } 986 }
800 987
801 context.reset(NewContext()); 988 context.reset(NewContext(NULL));
802 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->CopyOrMoveFile(context.get(), 989 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->CopyOrMoveFile(context.get(),
803 source_path, dest_path, test_case.is_copy_not_move)); 990 source_path, dest_path, test_case.is_copy_not_move));
804 if (test_case.is_copy_not_move) { 991 if (test_case.is_copy_not_move) {
805 base::PlatformFileInfo file_info; 992 base::PlatformFileInfo file_info;
806 FilePath local_path; 993 FilePath local_path;
807 context.reset(NewContext()); 994 context.reset(NewContext(NULL));
808 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->GetFileInfo( 995 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->GetFileInfo(
809 context.get(), source_path, &file_info, &local_path)); 996 context.get(), source_path, &file_info, &local_path));
810 EXPECT_EQ(kSourceLength, file_info.size); 997 EXPECT_EQ(kSourceLength, file_info.size);
811 EXPECT_EQ(base::PLATFORM_FILE_OK, 998 EXPECT_EQ(base::PLATFORM_FILE_OK,
812 ofsfu()->DeleteFile(context.get(), source_path)); 999 ofsfu()->DeleteFile(context.get(), source_path));
813 } else { 1000 } else {
814 base::PlatformFileInfo file_info; 1001 base::PlatformFileInfo file_info;
815 FilePath local_path; 1002 FilePath local_path;
816 context.reset(NewContext()); 1003 context.reset(NewContext(NULL));
817 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, ofsfu()->GetFileInfo( 1004 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, ofsfu()->GetFileInfo(
818 context.get(), source_path, &file_info, &local_path)); 1005 context.get(), source_path, &file_info, &local_path));
819 } 1006 }
820 base::PlatformFileInfo file_info; 1007 base::PlatformFileInfo file_info;
821 FilePath local_path; 1008 FilePath local_path;
822 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->GetFileInfo( 1009 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->GetFileInfo(
823 context.get(), dest_path, &file_info, &local_path)); 1010 context.get(), dest_path, &file_info, &local_path));
824 EXPECT_EQ(kSourceLength, file_info.size); 1011 EXPECT_EQ(kSourceLength, file_info.size);
825 1012
826 EXPECT_EQ(base::PLATFORM_FILE_OK, 1013 EXPECT_EQ(base::PLATFORM_FILE_OK,
827 ofsfu()->DeleteFile(context.get(), dest_path)); 1014 ofsfu()->DeleteFile(context.get(), dest_path));
828 } 1015 }
829 } 1016 }
830 1017
1018 TEST_F(ObfuscatedFileSystemFileUtilTest, TestCopyPathQuotas) {
1019 FilePath src_path = UTF8ToFilePath("src path");
1020 FilePath dest_path = UTF8ToFilePath("destination path");
1021 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1022 bool created = false;
1023 ASSERT_EQ(base::PLATFORM_FILE_OK, ofsfu()->EnsureFileExists(
1024 context.get(), src_path, &created));
1025
1026 bool is_copy = true;
1027 // Copy, no overwrite.
1028 context->set_allowed_bytes_growth(
1029 ObfuscatedFileSystemFileUtil::ComputeFilePathCost(dest_path) - 1);
1030 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
1031 ofsfu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy));
1032 context.reset(NewContext(NULL));
1033 context->set_allowed_bytes_growth(
1034 ObfuscatedFileSystemFileUtil::ComputeFilePathCost(dest_path));
1035 EXPECT_EQ(base::PLATFORM_FILE_OK,
1036 ofsfu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy));
1037
1038 // Copy, with overwrite.
1039 context.reset(NewContext(NULL));
1040 context->set_allowed_bytes_growth(0);
1041 EXPECT_EQ(base::PLATFORM_FILE_OK,
1042 ofsfu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy));
1043 }
1044
1045 TEST_F(ObfuscatedFileSystemFileUtilTest, TestMovePathQuotasWithRename) {
1046 FilePath src_path = UTF8ToFilePath("src path");
1047 FilePath dest_path = UTF8ToFilePath("destination path");
1048 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1049 bool created = false;
1050 ASSERT_EQ(base::PLATFORM_FILE_OK, ofsfu()->EnsureFileExists(
1051 context.get(), src_path, &created));
1052
1053 bool is_copy = false;
1054 // Move, rename, no overwrite.
1055 context.reset(NewContext(NULL));
1056 context->set_allowed_bytes_growth(
1057 ObfuscatedFileSystemFileUtil::ComputeFilePathCost(dest_path) -
1058 ObfuscatedFileSystemFileUtil::ComputeFilePathCost(src_path) - 1);
1059 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
1060 ofsfu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy));
1061 context.reset(NewContext(NULL));
1062 context->set_allowed_bytes_growth(
1063 ObfuscatedFileSystemFileUtil::ComputeFilePathCost(dest_path) -
1064 ObfuscatedFileSystemFileUtil::ComputeFilePathCost(src_path));
1065 EXPECT_EQ(base::PLATFORM_FILE_OK,
1066 ofsfu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy));
1067
1068 context.reset(NewContext(NULL));
1069 ASSERT_EQ(base::PLATFORM_FILE_OK, ofsfu()->EnsureFileExists(
1070 context.get(), src_path, &created));
1071
1072 // Move, rename, with overwrite.
1073 context.reset(NewContext(NULL));
1074 context->set_allowed_bytes_growth(0);
1075 EXPECT_EQ(base::PLATFORM_FILE_OK,
1076 ofsfu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy));
1077 }
1078
1079 TEST_F(ObfuscatedFileSystemFileUtilTest, TestMovePathQuotasWithoutRename) {
1080 FilePath src_path = UTF8ToFilePath("src path");
1081 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1082 bool created = false;
1083 ASSERT_EQ(base::PLATFORM_FILE_OK, ofsfu()->EnsureFileExists(
1084 context.get(), src_path, &created));
1085
1086 bool exclusive = true;
1087 bool recursive = false;
1088 FilePath dir_path = UTF8ToFilePath("directory path");
1089 context.reset(NewContext(NULL));
1090 ASSERT_EQ(base::PLATFORM_FILE_OK, ofsfu()->CreateDirectory(
1091 context.get(), dir_path, exclusive, recursive));
1092
1093 FilePath dest_path = dir_path.Append(src_path);
1094
1095 bool is_copy = false;
1096 int64 allowed_bytes_growth = -1000; // Over quota, this should still work.
1097 // Move, no rename, no overwrite.
1098 context.reset(NewContext(NULL));
1099 context->set_allowed_bytes_growth(allowed_bytes_growth);
1100 EXPECT_EQ(base::PLATFORM_FILE_OK,
1101 ofsfu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy));
1102 EXPECT_EQ(allowed_bytes_growth, context->allowed_bytes_growth());
1103
1104 // Move, no rename, with overwrite.
1105 context.reset(NewContext(NULL));
1106 ASSERT_EQ(base::PLATFORM_FILE_OK, ofsfu()->EnsureFileExists(
1107 context.get(), src_path, &created));
1108 context.reset(NewContext(NULL));
1109 context->set_allowed_bytes_growth(allowed_bytes_growth);
1110 EXPECT_EQ(base::PLATFORM_FILE_OK,
1111 ofsfu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy));
1112 EXPECT_EQ(
1113 allowed_bytes_growth +
1114 ObfuscatedFileSystemFileUtil::ComputeFilePathCost(src_path),
1115 context->allowed_bytes_growth());
1116 }
1117
831 TEST_F(ObfuscatedFileSystemFileUtilTest, TestCopyInForeignFile) { 1118 TEST_F(ObfuscatedFileSystemFileUtilTest, TestCopyInForeignFile) {
832 TestCopyInForeignFileHelper(false /* overwrite */); 1119 TestCopyInForeignFileHelper(false /* overwrite */);
833 TestCopyInForeignFileHelper(true /* overwrite */); 1120 TestCopyInForeignFileHelper(true /* overwrite */);
834 } 1121 }
835 1122
836 TEST_F(ObfuscatedFileSystemFileUtilTest, TestEnumerator) { 1123 TEST_F(ObfuscatedFileSystemFileUtilTest, TestEnumerator) {
837 scoped_ptr<FileSystemOperationContext> context(NewContext()); 1124 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
838 FilePath src_path = UTF8ToFilePath("source dir"); 1125 FilePath src_path = UTF8ToFilePath("source dir");
839 bool exclusive = true; 1126 bool exclusive = true;
840 bool recursive = false; 1127 bool recursive = false;
841 ASSERT_EQ(base::PLATFORM_FILE_OK, ofsfu()->CreateDirectory( 1128 ASSERT_EQ(base::PLATFORM_FILE_OK, ofsfu()->CreateDirectory(
842 context.get(), src_path, exclusive, recursive)); 1129 context.get(), src_path, exclusive, recursive));
843 1130
844 std::set<FilePath::StringType> files; 1131 std::set<FilePath::StringType> files;
845 std::set<FilePath::StringType> directories; 1132 std::set<FilePath::StringType> directories;
846 FillTestDirectory(src_path, &files, &directories); 1133 FillTestDirectory(src_path, &files, &directories);
847 1134
848 FilePath dest_path = UTF8ToFilePath("destination dir"); 1135 FilePath dest_path = UTF8ToFilePath("destination dir");
849 1136
850 context.reset(NewContext()); 1137 context.reset(NewContext(NULL));
851 EXPECT_FALSE(ofsfu()->DirectoryExists(context.get(), dest_path)); 1138 EXPECT_FALSE(ofsfu()->DirectoryExists(context.get(), dest_path));
852 context.reset(NewContext()); 1139 context.reset(NewContext(NULL));
853 ASSERT_EQ(base::PLATFORM_FILE_OK, 1140 ASSERT_EQ(base::PLATFORM_FILE_OK,
854 ofsfu()->Copy(context.get(), src_path, dest_path)); 1141 ofsfu()->Copy(context.get(), src_path, dest_path));
855 1142
856 ValidateTestDirectory(dest_path, files, directories); 1143 ValidateTestDirectory(dest_path, files, directories);
857 context.reset(NewContext()); 1144 context.reset(NewContext(NULL));
858 EXPECT_TRUE(ofsfu()->DirectoryExists(context.get(), src_path)); 1145 EXPECT_TRUE(ofsfu()->DirectoryExists(context.get(), src_path));
859 context.reset(NewContext()); 1146 context.reset(NewContext(NULL));
860 EXPECT_TRUE(ofsfu()->DirectoryExists(context.get(), dest_path)); 1147 EXPECT_TRUE(ofsfu()->DirectoryExists(context.get(), dest_path));
861 context.reset(NewContext()); 1148 context.reset(NewContext(NULL));
862 recursive = true; 1149 recursive = true;
863 ASSERT_EQ(base::PLATFORM_FILE_OK, 1150 ASSERT_EQ(base::PLATFORM_FILE_OK,
864 ofsfu()->Delete(context.get(), dest_path, recursive)); 1151 ofsfu()->Delete(context.get(), dest_path, recursive));
865 context.reset(NewContext()); 1152 context.reset(NewContext(NULL));
866 EXPECT_FALSE(ofsfu()->DirectoryExists(context.get(), dest_path)); 1153 EXPECT_FALSE(ofsfu()->DirectoryExists(context.get(), dest_path));
867 } 1154 }
868 1155
869 TEST_F(ObfuscatedFileSystemFileUtilTest, TestMigration) { 1156 TEST_F(ObfuscatedFileSystemFileUtilTest, TestMigration) {
870 ScopedTempDir source_dir; 1157 ScopedTempDir source_dir;
871 ASSERT_TRUE(source_dir.CreateUniqueTempDir()); 1158 ASSERT_TRUE(source_dir.CreateUniqueTempDir());
872 FilePath root_path = source_dir.path().AppendASCII("chrome-pLmnMWXE7NzTFRsn"); 1159 FilePath root_path = source_dir.path().AppendASCII("chrome-pLmnMWXE7NzTFRsn");
873 ASSERT_TRUE(file_util::CreateDirectory(root_path)); 1160 ASSERT_TRUE(file_util::CreateDirectory(root_path));
874 1161
875 for (size_t i = 0; i < arraysize(kMigrationTestCases); ++i) { 1162 for (size_t i = 0; i < arraysize(kMigrationTestCases); ++i) {
(...skipping 12 matching lines...) Expand all
888 local_src_path, file_flags, &created, &error_code); 1175 local_src_path, file_flags, &created, &error_code);
889 EXPECT_TRUE(created); 1176 EXPECT_TRUE(created);
890 ASSERT_NE(base::kInvalidPlatformFileValue, file_handle); 1177 ASSERT_NE(base::kInvalidPlatformFileValue, file_handle);
891 ASSERT_EQ(base::PLATFORM_FILE_OK, error_code); 1178 ASSERT_EQ(base::PLATFORM_FILE_OK, error_code);
892 ASSERT_TRUE( 1179 ASSERT_TRUE(
893 base::TruncatePlatformFile(file_handle, test_case.data_file_size)); 1180 base::TruncatePlatformFile(file_handle, test_case.data_file_size));
894 EXPECT_TRUE(base::ClosePlatformFile(file_handle)); 1181 EXPECT_TRUE(base::ClosePlatformFile(file_handle));
895 } 1182 }
896 } 1183 }
897 1184
898 EXPECT_TRUE(ofsfu()->MigrateFromOldSandbox(origin_url(), type(), root_path)); 1185 EXPECT_TRUE(ofsfu()->MigrateFromOldSandbox(origin(), type(), root_path));
899 1186
900 FilePath new_root = 1187 FilePath new_root =
901 test_directory().AppendASCII("000").Append( 1188 test_directory().AppendASCII("File System").AppendASCII("000").Append(
902 ofsfu()->GetDirectoryNameForType(type())).AppendASCII("Legacy"); 1189 ofsfu()->GetDirectoryNameForType(type())).AppendASCII("Legacy");
903 for (size_t i = 0; i < arraysize(kMigrationTestCases); ++i) { 1190 for (size_t i = 0; i < arraysize(kMigrationTestCases); ++i) {
904 SCOPED_TRACE(testing::Message() << "Validating kMigrationTestPath " << i); 1191 SCOPED_TRACE(testing::Message() << "Validating kMigrationTestPath " << i);
905 const MigrationTestCaseRecord& test_case = kMigrationTestCases[i]; 1192 const MigrationTestCaseRecord& test_case = kMigrationTestCases[i];
906 FilePath local_data_path = new_root.Append(test_case.path); 1193 FilePath local_data_path = new_root.Append(test_case.path);
907 #if defined(OS_WIN) 1194 #if defined(OS_WIN)
908 local_data_path = local_data_path.NormalizeWindowsPathSeparators(); 1195 local_data_path = local_data_path.NormalizeWindowsPathSeparators();
909 #endif 1196 #endif
910 scoped_ptr<FileSystemOperationContext> context(NewContext()); 1197 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
911 base::PlatformFileInfo ofsfu_file_info; 1198 base::PlatformFileInfo ofsfu_file_info;
912 FilePath data_path; 1199 FilePath data_path;
913 SCOPED_TRACE(testing::Message() << "Path is " << test_case.path); 1200 SCOPED_TRACE(testing::Message() << "Path is " << test_case.path);
914 EXPECT_EQ(base::PLATFORM_FILE_OK, 1201 EXPECT_EQ(base::PLATFORM_FILE_OK,
915 ofsfu()->GetFileInfo(context.get(), FilePath(test_case.path), 1202 ofsfu()->GetFileInfo(context.get(), FilePath(test_case.path),
916 &ofsfu_file_info, &data_path)); 1203 &ofsfu_file_info, &data_path));
917 if (test_case.is_directory) { 1204 if (test_case.is_directory) {
918 EXPECT_TRUE(ofsfu_file_info.is_directory); 1205 EXPECT_TRUE(ofsfu_file_info.is_directory);
919 } else { 1206 } else {
920 base::PlatformFileInfo platform_file_info; 1207 base::PlatformFileInfo platform_file_info;
921 SCOPED_TRACE(testing::Message() << "local_data_path is " << 1208 SCOPED_TRACE(testing::Message() << "local_data_path is " <<
922 local_data_path.value()); 1209 local_data_path.value());
923 SCOPED_TRACE(testing::Message() << "data_path is " << data_path.value()); 1210 SCOPED_TRACE(testing::Message() << "data_path is " << data_path.value());
924 ASSERT_TRUE(file_util::GetFileInfo(local_data_path, &platform_file_info)); 1211 ASSERT_TRUE(file_util::GetFileInfo(local_data_path, &platform_file_info));
925 EXPECT_EQ(test_case.data_file_size, platform_file_info.size); 1212 EXPECT_EQ(test_case.data_file_size, platform_file_info.size);
926 EXPECT_FALSE(platform_file_info.is_directory); 1213 EXPECT_FALSE(platform_file_info.is_directory);
927 scoped_ptr<FileSystemOperationContext> context(NewContext()); 1214 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
928 EXPECT_EQ(local_data_path, data_path); 1215 EXPECT_EQ(local_data_path, data_path);
929 EXPECT_EQ(platform_file_info.size, ofsfu_file_info.size); 1216 EXPECT_EQ(platform_file_info.size, ofsfu_file_info.size);
930 EXPECT_FALSE(ofsfu_file_info.is_directory); 1217 EXPECT_FALSE(ofsfu_file_info.is_directory);
931 } 1218 }
932 } 1219 }
933 } 1220 }
934 1221
935 TEST_F(ObfuscatedFileSystemFileUtilTest, TestOriginEnumerator) { 1222 TEST_F(ObfuscatedFileSystemFileUtilTest, TestOriginEnumerator) {
936 scoped_ptr<ObfuscatedFileSystemFileUtil::AbstractOriginEnumerator> 1223 scoped_ptr<ObfuscatedFileSystemFileUtil::AbstractOriginEnumerator>
937 enumerator(ofsfu()->CreateOriginEnumerator()); 1224 enumerator(ofsfu()->CreateOriginEnumerator());
1225 // The test helper starts out with a single filesystem.
938 EXPECT_TRUE(enumerator.get()); 1226 EXPECT_TRUE(enumerator.get());
1227 EXPECT_EQ(origin(), enumerator->Next());
1228 ASSERT_TRUE(type() == kFileSystemTypeTemporary);
1229 EXPECT_TRUE(enumerator->HasFileSystemType(kFileSystemTypeTemporary));
1230 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypePersistent));
939 EXPECT_EQ(GURL(), enumerator->Next()); 1231 EXPECT_EQ(GURL(), enumerator->Next());
940 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypeTemporary)); 1232 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypeTemporary));
941 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypePersistent)); 1233 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypePersistent));
942 1234
943 std::set<GURL> origins_expected; 1235 std::set<GURL> origins_expected;
1236 origins_expected.insert(origin());
944 1237
945 for (size_t i = 0; i < arraysize(kOriginEnumerationTestRecords); ++i) { 1238 for (size_t i = 0; i < arraysize(kOriginEnumerationTestRecords); ++i) {
946 SCOPED_TRACE(testing::Message() << 1239 SCOPED_TRACE(testing::Message() <<
947 "Validating kOriginEnumerationTestRecords " << i); 1240 "Validating kOriginEnumerationTestRecords " << i);
948 const OriginEnumerationTestRecord& record = 1241 const OriginEnumerationTestRecord& record =
949 kOriginEnumerationTestRecords[i]; 1242 kOriginEnumerationTestRecords[i];
950 GURL origin_url(record.origin_url); 1243 GURL origin_url(record.origin_url);
951 origins_expected.insert(origin_url); 1244 origins_expected.insert(origin_url);
952 if (record.has_temporary) { 1245 if (record.has_temporary) {
953 scoped_ptr<FileSystemOperationContext> context(NewContext()); 1246 scoped_ptr<FileSystemTestOriginHelper> helper(
1247 NewHelper(origin_url, kFileSystemTypeTemporary));
1248 scoped_ptr<FileSystemOperationContext> context(NewContext(helper.get()));
954 context->set_src_origin_url(origin_url); 1249 context->set_src_origin_url(origin_url);
955 context->set_src_type(kFileSystemTypeTemporary); 1250 context->set_src_type(kFileSystemTypeTemporary);
956 bool created = false; 1251 bool created = false;
957 ASSERT_EQ(base::PLATFORM_FILE_OK, 1252 ASSERT_EQ(base::PLATFORM_FILE_OK,
958 ofsfu()->EnsureFileExists(context.get(), 1253 ofsfu()->EnsureFileExists(context.get(),
959 FilePath().AppendASCII("file"), &created)); 1254 FilePath().AppendASCII("file"), &created));
960 EXPECT_TRUE(created); 1255 EXPECT_TRUE(created);
961 } 1256 }
962 if (record.has_persistent) { 1257 if (record.has_persistent) {
963 scoped_ptr<FileSystemOperationContext> context(NewContext()); 1258 scoped_ptr<FileSystemTestOriginHelper> helper(
1259 NewHelper(origin_url, kFileSystemTypePersistent));
1260 scoped_ptr<FileSystemOperationContext> context(NewContext(helper.get()));
964 context->set_src_origin_url(origin_url); 1261 context->set_src_origin_url(origin_url);
965 context->set_src_type(kFileSystemTypePersistent); 1262 context->set_src_type(kFileSystemTypePersistent);
966 bool created = false; 1263 bool created = false;
967 ASSERT_EQ(base::PLATFORM_FILE_OK, 1264 ASSERT_EQ(base::PLATFORM_FILE_OK,
968 ofsfu()->EnsureFileExists(context.get(), 1265 ofsfu()->EnsureFileExists(context.get(),
969 FilePath().AppendASCII("file"), &created)); 1266 FilePath().AppendASCII("file"), &created));
970 EXPECT_TRUE(created); 1267 EXPECT_TRUE(created);
971 } 1268 }
972 } 1269 }
973 enumerator.reset(ofsfu()->CreateOriginEnumerator()); 1270 enumerator.reset(ofsfu()->CreateOriginEnumerator());
974 EXPECT_TRUE(enumerator.get()); 1271 EXPECT_TRUE(enumerator.get());
975 std::set<GURL> origins_found; 1272 std::set<GURL> origins_found;
976 GURL origin; 1273 GURL origin_url;
977 while (!(origin = enumerator->Next()).is_empty()) { 1274 while (!(origin_url = enumerator->Next()).is_empty()) {
978 origins_found.insert(origin); 1275 origins_found.insert(origin_url);
979 SCOPED_TRACE(testing::Message() << "Handling " << origin.spec()); 1276 SCOPED_TRACE(testing::Message() << "Handling " << origin_url.spec());
980 bool found = false; 1277 bool found = false;
981 for (size_t i = 0; !found && i < arraysize(kOriginEnumerationTestRecords); 1278 for (size_t i = 0; !found && i < arraysize(kOriginEnumerationTestRecords);
982 ++i) { 1279 ++i) {
983 const OriginEnumerationTestRecord& record = 1280 const OriginEnumerationTestRecord& record =
984 kOriginEnumerationTestRecords[i]; 1281 kOriginEnumerationTestRecords[i];
985 if (GURL(record.origin_url) != origin) 1282 if (GURL(record.origin_url) != origin_url)
986 continue; 1283 continue;
987 found = true; 1284 found = true;
988 EXPECT_EQ(record.has_temporary, 1285 EXPECT_EQ(record.has_temporary,
989 enumerator->HasFileSystemType(kFileSystemTypeTemporary)); 1286 enumerator->HasFileSystemType(kFileSystemTypeTemporary));
990 EXPECT_EQ(record.has_persistent, 1287 EXPECT_EQ(record.has_persistent,
991 enumerator->HasFileSystemType(kFileSystemTypePersistent)); 1288 enumerator->HasFileSystemType(kFileSystemTypePersistent));
992 } 1289 }
1290 // Deal with the default filesystem created by the test helper.
1291 if (!found && origin_url == origin()) {
1292 ASSERT_TRUE(type() == kFileSystemTypeTemporary);
1293 EXPECT_EQ(true,
1294 enumerator->HasFileSystemType(kFileSystemTypeTemporary));
1295 EXPECT_EQ(false,
1296 enumerator->HasFileSystemType(kFileSystemTypePersistent));
1297 found = true;
1298 }
993 EXPECT_TRUE(found); 1299 EXPECT_TRUE(found);
994 } 1300 }
995 1301
996 std::set<GURL> diff; 1302 std::set<GURL> diff;
997 std::set_symmetric_difference(origins_expected.begin(), 1303 std::set_symmetric_difference(origins_expected.begin(),
998 origins_expected.end(), origins_found.begin(), origins_found.end(), 1304 origins_expected.end(), origins_found.begin(), origins_found.end(),
999 inserter(diff, diff.begin())); 1305 inserter(diff, diff.begin()));
1000 EXPECT_TRUE(diff.empty()); 1306 EXPECT_TRUE(diff.empty());
1001 } 1307 }
1308
1309 TEST_F(ObfuscatedFileSystemFileUtilTest, TestRevokeUsageCache) {
1310 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL));
1311
1312 int64 expected_quota = 0;
1313
1314 for (size_t i = 0; i < arraysize(kMigrationTestCases); ++i) {
1315 SCOPED_TRACE(testing::Message() << "Creating kMigrationTestPath " << i);
1316 const MigrationTestCaseRecord& test_case = kMigrationTestCases[i];
1317 FilePath path(test_case.path);
1318 expected_quota += ObfuscatedFileSystemFileUtil::ComputeFilePathCost(path);
1319 if (test_case.is_directory) {
1320 bool exclusive = true;
1321 bool recursive = false;
1322 ASSERT_EQ(base::PLATFORM_FILE_OK,
1323 ofsfu()->CreateDirectory(context.get(), path, exclusive, recursive));
1324 } else {
1325 bool created = false;
1326 ASSERT_EQ(base::PLATFORM_FILE_OK,
1327 ofsfu()->EnsureFileExists(context.get(), path, &created));
1328 ASSERT_TRUE(created);
1329 ASSERT_EQ(base::PLATFORM_FILE_OK,
1330 ofsfu()->Truncate(context.get(), path,
1331 test_case.data_file_size));
1332 expected_quota += test_case.data_file_size;
1333 }
1334 }
1335 EXPECT_EQ(expected_quota, SizeInUsageFile());
1336 RevokeUsageCache();
1337 EXPECT_EQ(-1, SizeInUsageFile());
1338 GetUsageFromQuotaManager();
1339 EXPECT_EQ(expected_quota, SizeInUsageFile());
1340 EXPECT_EQ(expected_quota, usage());
1341 }
OLDNEW
« no previous file with comments | « webkit/fileapi/obfuscated_file_system_file_util.cc ('k') | webkit/fileapi/quota_file_util.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698