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

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

Powered by Google App Engine
This is Rietveld 408576698