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

Side by Side Diff: components/drive/chromeos/file_cache_unittest.cc

Issue 2317993003: //chrome/browser and //components A-E: Change ScopedTempDir::path() to GetPath() (Closed)
Patch Set: Just rebased Created 4 years, 3 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
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/drive/chromeos/file_cache.h" 5 #include "components/drive/chromeos/file_cache.h"
6 6
7 #include <linux/fs.h> 7 #include <linux/fs.h>
8 #include <stddef.h> 8 #include <stddef.h>
9 #include <stdint.h> 9 #include <stdint.h>
10 #include <sys/ioctl.h> 10 #include <sys/ioctl.h>
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
63 return (GetFileAttributes(file_path) & FS_NODUMP_FL) == FS_NODUMP_FL; 63 return (GetFileAttributes(file_path) & FS_NODUMP_FL) == FS_NODUMP_FL;
64 } 64 }
65 65
66 } // namespace 66 } // namespace
67 67
68 // Tests FileCache methods working with the blocking task runner. 68 // Tests FileCache methods working with the blocking task runner.
69 class FileCacheTest : public testing::Test { 69 class FileCacheTest : public testing::Test {
70 protected: 70 protected:
71 void SetUp() override { 71 void SetUp() override {
72 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 72 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
73 const base::FilePath metadata_dir = temp_dir_.path().AppendASCII("meta"); 73 const base::FilePath metadata_dir = temp_dir_.GetPath().AppendASCII("meta");
74 cache_files_dir_ = temp_dir_.path().Append(kCacheFileDirectory); 74 cache_files_dir_ = temp_dir_.GetPath().Append(kCacheFileDirectory);
75 75
76 ASSERT_TRUE(base::CreateDirectory(metadata_dir)); 76 ASSERT_TRUE(base::CreateDirectory(metadata_dir));
77 ASSERT_TRUE(base::CreateDirectory(cache_files_dir_)); 77 ASSERT_TRUE(base::CreateDirectory(cache_files_dir_));
78 78
79 fake_free_disk_space_getter_.reset(new FakeFreeDiskSpaceGetter); 79 fake_free_disk_space_getter_.reset(new FakeFreeDiskSpaceGetter);
80 80
81 metadata_storage_.reset(new ResourceMetadataStorage( 81 metadata_storage_.reset(new ResourceMetadataStorage(
82 metadata_dir, 82 metadata_dir,
83 base::ThreadTaskRunnerHandle::Get().get())); 83 base::ThreadTaskRunnerHandle::Get().get()));
84 ASSERT_TRUE(metadata_storage_->Initialize()); 84 ASSERT_TRUE(metadata_storage_->Initialize());
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
136 136
137 // Store files. This file should not be moved. 137 // Store files. This file should not be moved.
138 ResourceEntry entry; 138 ResourceEntry entry;
139 entry.set_local_id("id_foo"); 139 entry.set_local_id("id_foo");
140 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry)); 140 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry));
141 EXPECT_EQ(FILE_ERROR_OK, cache_->Store("id_foo", "md5", src_path, 141 EXPECT_EQ(FILE_ERROR_OK, cache_->Store("id_foo", "md5", src_path,
142 FileCache::FILE_OPERATION_COPY)); 142 FileCache::FILE_OPERATION_COPY));
143 143
144 // Set up files in the cache directory. These files should be moved. 144 // Set up files in the cache directory. These files should be moved.
145 const base::FilePath file_directory = 145 const base::FilePath file_directory =
146 temp_dir_.path().Append(kCacheFileDirectory); 146 temp_dir_.GetPath().Append(kCacheFileDirectory);
147 ASSERT_TRUE(base::CopyFile(src_path, file_directory.AppendASCII("id_bar"))); 147 ASSERT_TRUE(base::CopyFile(src_path, file_directory.AppendASCII("id_bar")));
148 ASSERT_TRUE(base::CopyFile(src_path, file_directory.AppendASCII("id_baz"))); 148 ASSERT_TRUE(base::CopyFile(src_path, file_directory.AppendASCII("id_baz")));
149 149
150 // Insert a dirty entry with "id_baz" to |recovered_cache_info|. 150 // Insert a dirty entry with "id_baz" to |recovered_cache_info|.
151 // This should not prevent the file from being recovered. 151 // This should not prevent the file from being recovered.
152 ResourceMetadataStorage::RecoveredCacheInfoMap recovered_cache_info; 152 ResourceMetadataStorage::RecoveredCacheInfoMap recovered_cache_info;
153 recovered_cache_info["id_baz"].is_dirty = true; 153 recovered_cache_info["id_baz"].is_dirty = true;
154 recovered_cache_info["id_baz"].title = "baz.png"; 154 recovered_cache_info["id_baz"].title = "baz.png";
155 155
156 // Recover files. 156 // Recover files.
157 const base::FilePath dest_directory = temp_dir_.path().AppendASCII("dest"); 157 const base::FilePath dest_directory = temp_dir_.GetPath().AppendASCII("dest");
158 EXPECT_TRUE(cache_->RecoverFilesFromCacheDirectory(dest_directory, 158 EXPECT_TRUE(cache_->RecoverFilesFromCacheDirectory(dest_directory,
159 recovered_cache_info)); 159 recovered_cache_info));
160 160
161 // Only two files should be recovered. 161 // Only two files should be recovered.
162 EXPECT_TRUE(base::PathExists(dest_directory)); 162 EXPECT_TRUE(base::PathExists(dest_directory));
163 // base::FileEnumerator does not guarantee the order. 163 // base::FileEnumerator does not guarantee the order.
164 if (base::PathExists(dest_directory.AppendASCII("baz00000001.png"))) { 164 if (base::PathExists(dest_directory.AppendASCII("baz00000001.png"))) {
165 EXPECT_TRUE(base::ContentsEqual( 165 EXPECT_TRUE(base::ContentsEqual(
166 src_path, 166 src_path,
167 dest_directory.AppendASCII("baz00000001.png"))); 167 dest_directory.AppendASCII("baz00000001.png")));
168 EXPECT_TRUE(base::ContentsEqual( 168 EXPECT_TRUE(base::ContentsEqual(
169 src_path, 169 src_path,
170 dest_directory.AppendASCII("image00000002.png"))); 170 dest_directory.AppendASCII("image00000002.png")));
171 } else { 171 } else {
172 EXPECT_TRUE(base::ContentsEqual( 172 EXPECT_TRUE(base::ContentsEqual(
173 src_path, 173 src_path,
174 dest_directory.AppendASCII("image00000001.png"))); 174 dest_directory.AppendASCII("image00000001.png")));
175 EXPECT_TRUE(base::ContentsEqual( 175 EXPECT_TRUE(base::ContentsEqual(
176 src_path, 176 src_path,
177 dest_directory.AppendASCII("baz00000002.png"))); 177 dest_directory.AppendASCII("baz00000002.png")));
178 } 178 }
179 EXPECT_FALSE(base::PathExists( 179 EXPECT_FALSE(base::PathExists(
180 dest_directory.AppendASCII("image00000003.png"))); 180 dest_directory.AppendASCII("image00000003.png")));
181 } 181 }
182 182
183 TEST_F(FileCacheTest, FreeDiskSpaceIfNeededFor) { 183 TEST_F(FileCacheTest, FreeDiskSpaceIfNeededFor) {
184 base::FilePath src_file; 184 base::FilePath src_file;
185 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), &src_file)); 185 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.GetPath(), &src_file));
186 186
187 // Store a file as a 'temporary' file and remember the path. 187 // Store a file as a 'temporary' file and remember the path.
188 const std::string id_tmp = "id_tmp", md5_tmp = "md5_tmp"; 188 const std::string id_tmp = "id_tmp", md5_tmp = "md5_tmp";
189 const time_t last_accessed_tmp = 1; 189 const time_t last_accessed_tmp = 1;
190 const base::FilePath& tmp_path = 190 const base::FilePath& tmp_path =
191 AddTestEntry(id_tmp, md5_tmp, last_accessed_tmp, src_file); 191 AddTestEntry(id_tmp, md5_tmp, last_accessed_tmp, src_file);
192 192
193 // Store a file as a pinned file and remember the path. 193 // Store a file as a pinned file and remember the path.
194 const std::string id_pinned = "id_pinned", md5_pinned = "md5_pinned"; 194 const std::string id_pinned = "id_pinned", md5_pinned = "md5_pinned";
195 const time_t last_accessed_pinned = 1; 195 const time_t last_accessed_pinned = 1;
(...skipping 19 matching lines...) Expand all
215 EXPECT_TRUE(base::PathExists(pinned_path)); 215 EXPECT_TRUE(base::PathExists(pinned_path));
216 216
217 // Returns false when disk space cannot be freed. 217 // Returns false when disk space cannot be freed.
218 fake_free_disk_space_getter_->set_default_value(0); 218 fake_free_disk_space_getter_->set_default_value(0);
219 EXPECT_FALSE(cache_->FreeDiskSpaceIfNeededFor(kNeededBytes)); 219 EXPECT_FALSE(cache_->FreeDiskSpaceIfNeededFor(kNeededBytes));
220 } 220 }
221 221
222 TEST_F(FileCacheTest, EvictDriveCacheInLRU) { 222 TEST_F(FileCacheTest, EvictDriveCacheInLRU) {
223 // Create temporary file. 223 // Create temporary file.
224 base::FilePath src_file; 224 base::FilePath src_file;
225 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), &src_file)); 225 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.GetPath(), &src_file));
226 ASSERT_EQ(kTemporaryFileSizeInBytes, 226 ASSERT_EQ(kTemporaryFileSizeInBytes,
227 base::WriteFile(src_file, "abcdefghij", kTemporaryFileSizeInBytes)); 227 base::WriteFile(src_file, "abcdefghij", kTemporaryFileSizeInBytes));
228 228
229 // Add entries. 229 // Add entries.
230 const std::string id_a = "id_a", md5_a = "md5_a"; 230 const std::string id_a = "id_a", md5_a = "md5_a";
231 const time_t last_accessed_a = 1; 231 const time_t last_accessed_a = 1;
232 const base::FilePath& a_path = 232 const base::FilePath& a_path =
233 AddTestEntry(id_a, md5_a, last_accessed_a, src_file); 233 AddTestEntry(id_a, md5_a, last_accessed_a, src_file);
234 234
235 const std::string id_pinned = "id_pinned", md5_pinned = "md5_pinned"; 235 const std::string id_pinned = "id_pinned", md5_pinned = "md5_pinned";
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
274 // Entry C should not be evicted. 274 // Entry C should not be evicted.
275 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id_c, &entry)); 275 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id_c, &entry));
276 EXPECT_TRUE(entry.file_specific_info().cache_state().is_present()); 276 EXPECT_TRUE(entry.file_specific_info().cache_state().is_present());
277 EXPECT_TRUE(base::PathExists(c_path)); 277 EXPECT_TRUE(base::PathExists(c_path));
278 } 278 }
279 279
280 // Test case for deleting invalid cache files which don't have corresponding 280 // Test case for deleting invalid cache files which don't have corresponding
281 // metadata. 281 // metadata.
282 TEST_F(FileCacheTest, EvictInvalidCacheFile) { 282 TEST_F(FileCacheTest, EvictInvalidCacheFile) {
283 base::FilePath src_file; 283 base::FilePath src_file;
284 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), &src_file)); 284 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.GetPath(), &src_file));
285 285
286 // Add entries. 286 // Add entries.
287 const std::string id_a = "id_a", md5_a = "md5_a"; 287 const std::string id_a = "id_a", md5_a = "md5_a";
288 const time_t last_accessed_a = 1; 288 const time_t last_accessed_a = 1;
289 const base::FilePath& a_path = 289 const base::FilePath& a_path =
290 AddTestEntry(id_a, md5_a, last_accessed_a, src_file); 290 AddTestEntry(id_a, md5_a, last_accessed_a, src_file);
291 291
292 const std::string id_b = "id_b", md5_b = "md5_b"; 292 const std::string id_b = "id_b", md5_b = "md5_b";
293 const time_t last_accessed_b = 2; 293 const time_t last_accessed_b = 2;
294 const base::FilePath& b_path = 294 const base::FilePath& b_path =
(...skipping 21 matching lines...) Expand all
316 EXPECT_EQ(FILE_ERROR_NOT_FOUND, metadata_storage_->GetEntry(id_b, &entry)); 316 EXPECT_EQ(FILE_ERROR_NOT_FOUND, metadata_storage_->GetEntry(id_b, &entry));
317 EXPECT_FALSE(base::PathExists(b_path)); 317 EXPECT_FALSE(base::PathExists(b_path));
318 } 318 }
319 319
320 TEST_F(FileCacheTest, TooManyCacheFiles) { 320 TEST_F(FileCacheTest, TooManyCacheFiles) {
321 const size_t kMaxNumOfEvictedCacheFiles = 50; 321 const size_t kMaxNumOfEvictedCacheFiles = 50;
322 cache_->SetMaxNumOfEvictedCacheFilesForTest(kMaxNumOfEvictedCacheFiles); 322 cache_->SetMaxNumOfEvictedCacheFilesForTest(kMaxNumOfEvictedCacheFiles);
323 323
324 // Create temporary file. 324 // Create temporary file.
325 base::FilePath src_file; 325 base::FilePath src_file;
326 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), &src_file)); 326 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.GetPath(), &src_file));
327 ASSERT_EQ(kTemporaryFileSizeInBytes, 327 ASSERT_EQ(kTemporaryFileSizeInBytes,
328 base::WriteFile(src_file, "abcdefghij", kTemporaryFileSizeInBytes)); 328 base::WriteFile(src_file, "abcdefghij", kTemporaryFileSizeInBytes));
329 329
330 // Add kNumOfTestFiles=kMaxNumOfEvictedCacheFiles*2 entries. 330 // Add kNumOfTestFiles=kMaxNumOfEvictedCacheFiles*2 entries.
331 std::vector<base::FilePath> paths; 331 std::vector<base::FilePath> paths;
332 const int32_t kNumOfTestFiles = kMaxNumOfEvictedCacheFiles * 2; 332 const int32_t kNumOfTestFiles = kMaxNumOfEvictedCacheFiles * 2;
333 for (int i = 0; i < kNumOfTestFiles; ++i) { 333 for (int i = 0; i < kNumOfTestFiles; ++i) {
334 // Set last accessed in reverse order to the file name. i.e. If you sort 334 // Set last accessed in reverse order to the file name. i.e. If you sort
335 // files in name-asc order, they will be last access desc order. 335 // files in name-asc order, they will be last access desc order.
336 paths.push_back(AddTestEntry( 336 paths.push_back(AddTestEntry(
(...skipping 17 matching lines...) Expand all
354 (kMaxNumOfEvictedCacheFiles * 3 / 2) * kTemporaryFileSizeInBytes; 354 (kMaxNumOfEvictedCacheFiles * 3 / 2) * kTemporaryFileSizeInBytes;
355 EXPECT_FALSE(cache_->FreeDiskSpaceIfNeededFor(kNeededBytes)); 355 EXPECT_FALSE(cache_->FreeDiskSpaceIfNeededFor(kNeededBytes));
356 356
357 for (uint32_t i = 0; i < kNumOfTestFiles; ++i) { 357 for (uint32_t i = 0; i < kNumOfTestFiles; ++i) {
358 // Assert that only first kMaxNumOfEvictedCacheFiles exist. 358 // Assert that only first kMaxNumOfEvictedCacheFiles exist.
359 ASSERT_EQ(i < kMaxNumOfEvictedCacheFiles, base::PathExists(paths[i])); 359 ASSERT_EQ(i < kMaxNumOfEvictedCacheFiles, base::PathExists(paths[i]));
360 } 360 }
361 } 361 }
362 362
363 TEST_F(FileCacheTest, GetFile) { 363 TEST_F(FileCacheTest, GetFile) {
364 const base::FilePath src_file_path = temp_dir_.path().Append("test.dat"); 364 const base::FilePath src_file_path = temp_dir_.GetPath().Append("test.dat");
365 const std::string src_contents = "test"; 365 const std::string src_contents = "test";
366 EXPECT_TRUE(google_apis::test_util::WriteStringToFile(src_file_path, 366 EXPECT_TRUE(google_apis::test_util::WriteStringToFile(src_file_path,
367 src_contents)); 367 src_contents));
368 std::string id("id1"); 368 std::string id("id1");
369 std::string md5(base::MD5String(src_contents)); 369 std::string md5(base::MD5String(src_contents));
370 370
371 const base::FilePath cache_file_directory = 371 const base::FilePath cache_file_directory =
372 temp_dir_.path().Append(kCacheFileDirectory); 372 temp_dir_.GetPath().Append(kCacheFileDirectory);
373 373
374 // Try to get an existing file from cache. 374 // Try to get an existing file from cache.
375 ResourceEntry entry; 375 ResourceEntry entry;
376 entry.set_local_id(id); 376 entry.set_local_id(id);
377 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry)); 377 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry));
378 EXPECT_EQ(FILE_ERROR_OK, cache_->Store(id, md5, src_file_path, 378 EXPECT_EQ(FILE_ERROR_OK, cache_->Store(id, md5, src_file_path,
379 FileCache::FILE_OPERATION_COPY)); 379 FileCache::FILE_OPERATION_COPY));
380 base::FilePath cache_file_path; 380 base::FilePath cache_file_path;
381 EXPECT_EQ(FILE_ERROR_OK, cache_->GetFile(id, &cache_file_path)); 381 EXPECT_EQ(FILE_ERROR_OK, cache_->GetFile(id, &cache_file_path));
382 EXPECT_EQ( 382 EXPECT_EQ(
(...skipping 25 matching lines...) Expand all
408 EXPECT_EQ( 408 EXPECT_EQ(
409 cache_file_directory.AppendASCII(util::EscapeCacheFileName(id)).value(), 409 cache_file_directory.AppendASCII(util::EscapeCacheFileName(id)).value(),
410 cache_file_path.value()); 410 cache_file_path.value());
411 411
412 contents.clear(); 412 contents.clear();
413 EXPECT_TRUE(base::ReadFileToString(cache_file_path, &contents)); 413 EXPECT_TRUE(base::ReadFileToString(cache_file_path, &contents));
414 EXPECT_EQ(src_contents, contents); 414 EXPECT_EQ(src_contents, contents);
415 } 415 }
416 416
417 TEST_F(FileCacheTest, Store) { 417 TEST_F(FileCacheTest, Store) {
418 const base::FilePath src_file_path = temp_dir_.path().Append("test.dat"); 418 const base::FilePath src_file_path = temp_dir_.GetPath().Append("test.dat");
419 const std::string src_contents = "test"; 419 const std::string src_contents = "test";
420 EXPECT_TRUE(google_apis::test_util::WriteStringToFile(src_file_path, 420 EXPECT_TRUE(google_apis::test_util::WriteStringToFile(src_file_path,
421 src_contents)); 421 src_contents));
422 std::string id("id"); 422 std::string id("id");
423 std::string md5(base::MD5String(src_contents)); 423 std::string md5(base::MD5String(src_contents));
424 424
425 // Store a file. 425 // Store a file.
426 ResourceEntry entry; 426 ResourceEntry entry;
427 entry.set_local_id(id); 427 entry.set_local_id(id);
428 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry)); 428 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry));
(...skipping 27 matching lines...) Expand all
456 EXPECT_FALSE(HasRemovableFlag((dest_file_path))); 456 EXPECT_FALSE(HasRemovableFlag((dest_file_path)));
457 457
458 // No free space available. 458 // No free space available.
459 fake_free_disk_space_getter_->set_default_value(0); 459 fake_free_disk_space_getter_->set_default_value(0);
460 460
461 EXPECT_EQ(FILE_ERROR_NO_LOCAL_SPACE, cache_->Store( 461 EXPECT_EQ(FILE_ERROR_NO_LOCAL_SPACE, cache_->Store(
462 id, md5, src_file_path, FileCache::FILE_OPERATION_COPY)); 462 id, md5, src_file_path, FileCache::FILE_OPERATION_COPY));
463 } 463 }
464 464
465 TEST_F(FileCacheTest, PinAndUnpin) { 465 TEST_F(FileCacheTest, PinAndUnpin) {
466 const base::FilePath src_file_path = temp_dir_.path().Append("test.dat"); 466 const base::FilePath src_file_path = temp_dir_.GetPath().Append("test.dat");
467 const std::string src_contents = "test"; 467 const std::string src_contents = "test";
468 EXPECT_TRUE(google_apis::test_util::WriteStringToFile(src_file_path, 468 EXPECT_TRUE(google_apis::test_util::WriteStringToFile(src_file_path,
469 src_contents)); 469 src_contents));
470 std::string id("id_present"); 470 std::string id("id_present");
471 std::string md5(base::MD5String(src_contents)); 471 std::string md5(base::MD5String(src_contents));
472 472
473 // Store a file. 473 // Store a file.
474 ResourceEntry entry; 474 ResourceEntry entry;
475 entry.set_local_id(id); 475 entry.set_local_id(id);
476 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry)); 476 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry));
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
510 EXPECT_EQ(FILE_ERROR_OK, cache_->Unpin(id_non_present)); 510 EXPECT_EQ(FILE_ERROR_OK, cache_->Unpin(id_non_present));
511 511
512 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id_non_present, &entry)); 512 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id_non_present, &entry));
513 EXPECT_FALSE(entry.file_specific_info().has_cache_state()); 513 EXPECT_FALSE(entry.file_specific_info().has_cache_state());
514 514
515 // Unpin a file that doesn't exist in cache and is not pinned. 515 // Unpin a file that doesn't exist in cache and is not pinned.
516 EXPECT_EQ(FILE_ERROR_NOT_FOUND, cache_->Unpin("id_non_existent")); 516 EXPECT_EQ(FILE_ERROR_NOT_FOUND, cache_->Unpin("id_non_existent"));
517 } 517 }
518 518
519 TEST_F(FileCacheTest, MountUnmount) { 519 TEST_F(FileCacheTest, MountUnmount) {
520 const base::FilePath src_file_path = temp_dir_.path().Append("test.dat"); 520 const base::FilePath src_file_path = temp_dir_.GetPath().Append("test.dat");
521 const std::string src_contents = "test"; 521 const std::string src_contents = "test";
522 EXPECT_TRUE(google_apis::test_util::WriteStringToFile(src_file_path, 522 EXPECT_TRUE(google_apis::test_util::WriteStringToFile(src_file_path,
523 src_contents)); 523 src_contents));
524 std::string id("id_present"); 524 std::string id("id_present");
525 std::string md5(base::MD5String(src_contents)); 525 std::string md5(base::MD5String(src_contents));
526 526
527 // Store a file. 527 // Store a file.
528 ResourceEntry entry; 528 ResourceEntry entry;
529 entry.set_local_id(id); 529 entry.set_local_id(id);
530 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry)); 530 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry));
(...skipping 10 matching lines...) Expand all
541 // Clear mounted state of the file. 541 // Clear mounted state of the file.
542 EXPECT_EQ(FILE_ERROR_OK, cache_->MarkAsUnmounted(cache_file_path)); 542 EXPECT_EQ(FILE_ERROR_OK, cache_->MarkAsUnmounted(cache_file_path));
543 543
544 // Try to remove again. 544 // Try to remove again.
545 EXPECT_EQ(FILE_ERROR_OK, cache_->Remove(id)); 545 EXPECT_EQ(FILE_ERROR_OK, cache_->Remove(id));
546 } 546 }
547 547
548 TEST_F(FileCacheTest, OpenForWrite) { 548 TEST_F(FileCacheTest, OpenForWrite) {
549 // Prepare a file. 549 // Prepare a file.
550 base::FilePath src_file; 550 base::FilePath src_file;
551 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), &src_file)); 551 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.GetPath(), &src_file));
552 552
553 const std::string id = "id"; 553 const std::string id = "id";
554 ResourceEntry entry; 554 ResourceEntry entry;
555 entry.set_local_id(id); 555 entry.set_local_id(id);
556 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry)); 556 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry));
557 ASSERT_EQ(FILE_ERROR_OK, cache_->Store(id, "md5", src_file, 557 ASSERT_EQ(FILE_ERROR_OK, cache_->Store(id, "md5", src_file,
558 FileCache::FILE_OPERATION_COPY)); 558 FileCache::FILE_OPERATION_COPY));
559 EXPECT_EQ(0, entry.file_info().last_modified()); 559 EXPECT_EQ(0, entry.file_info().last_modified());
560 560
561 // Entry is not dirty nor opened. 561 // Entry is not dirty nor opened.
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
595 base::RunLoop().RunUntilIdle(); 595 base::RunLoop().RunUntilIdle();
596 EXPECT_FALSE(cache_->IsOpenedForWrite(id)); 596 EXPECT_FALSE(cache_->IsOpenedForWrite(id));
597 597
598 // Try to open non-existent file. 598 // Try to open non-existent file.
599 EXPECT_EQ(FILE_ERROR_NOT_FOUND, 599 EXPECT_EQ(FILE_ERROR_NOT_FOUND,
600 cache_->OpenForWrite("nonexistent_id", &file_closer1)); 600 cache_->OpenForWrite("nonexistent_id", &file_closer1));
601 } 601 }
602 602
603 TEST_F(FileCacheTest, UpdateMd5) { 603 TEST_F(FileCacheTest, UpdateMd5) {
604 // Store test data. 604 // Store test data.
605 const base::FilePath src_file_path = temp_dir_.path().Append("test.dat"); 605 const base::FilePath src_file_path = temp_dir_.GetPath().Append("test.dat");
606 const std::string contents_before = "before"; 606 const std::string contents_before = "before";
607 EXPECT_TRUE(google_apis::test_util::WriteStringToFile(src_file_path, 607 EXPECT_TRUE(google_apis::test_util::WriteStringToFile(src_file_path,
608 contents_before)); 608 contents_before));
609 std::string id("id1"); 609 std::string id("id1");
610 ResourceEntry entry; 610 ResourceEntry entry;
611 entry.set_local_id(id); 611 entry.set_local_id(id);
612 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry)); 612 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry));
613 EXPECT_EQ(FILE_ERROR_OK, cache_->Store(id, base::MD5String(contents_before), 613 EXPECT_EQ(FILE_ERROR_OK, cache_->Store(id, base::MD5String(contents_before),
614 src_file_path, 614 src_file_path,
615 FileCache::FILE_OPERATION_COPY)); 615 FileCache::FILE_OPERATION_COPY));
(...skipping 21 matching lines...) Expand all
637 // Update MD5. 637 // Update MD5.
638 EXPECT_EQ(FILE_ERROR_OK, cache_->UpdateMd5(id)); 638 EXPECT_EQ(FILE_ERROR_OK, cache_->UpdateMd5(id));
639 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id, &entry)); 639 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id, &entry));
640 EXPECT_EQ(base::MD5String(contents_after), 640 EXPECT_EQ(base::MD5String(contents_after),
641 entry.file_specific_info().cache_state().md5()); 641 entry.file_specific_info().cache_state().md5());
642 } 642 }
643 643
644 TEST_F(FileCacheTest, ClearDirty) { 644 TEST_F(FileCacheTest, ClearDirty) {
645 // Prepare a file. 645 // Prepare a file.
646 base::FilePath src_file; 646 base::FilePath src_file;
647 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), &src_file)); 647 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.GetPath(), &src_file));
648 648
649 const std::string id = "id"; 649 const std::string id = "id";
650 ResourceEntry entry; 650 ResourceEntry entry;
651 entry.set_local_id(id); 651 entry.set_local_id(id);
652 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry)); 652 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry));
653 ASSERT_EQ(FILE_ERROR_OK, cache_->Store(id, "md5", src_file, 653 ASSERT_EQ(FILE_ERROR_OK, cache_->Store(id, "md5", src_file,
654 FileCache::FILE_OPERATION_COPY)); 654 FileCache::FILE_OPERATION_COPY));
655 655
656 const base::FilePath dest_file = GetCacheFilePath(id); 656 const base::FilePath dest_file = GetCacheFilePath(id);
657 EXPECT_TRUE(HasRemovableFlag((dest_file))); 657 EXPECT_TRUE(HasRemovableFlag((dest_file)));
(...skipping 16 matching lines...) Expand all
674 base::RunLoop().RunUntilIdle(); 674 base::RunLoop().RunUntilIdle();
675 EXPECT_EQ(FILE_ERROR_OK, cache_->ClearDirty(id)); 675 EXPECT_EQ(FILE_ERROR_OK, cache_->ClearDirty(id));
676 676
677 // Entry is not dirty. 677 // Entry is not dirty.
678 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id, &entry)); 678 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id, &entry));
679 EXPECT_FALSE(entry.file_specific_info().cache_state().is_dirty()); 679 EXPECT_FALSE(entry.file_specific_info().cache_state().is_dirty());
680 EXPECT_TRUE(HasRemovableFlag((dest_file))); 680 EXPECT_TRUE(HasRemovableFlag((dest_file)));
681 } 681 }
682 682
683 TEST_F(FileCacheTest, Remove) { 683 TEST_F(FileCacheTest, Remove) {
684 const base::FilePath src_file_path = temp_dir_.path().Append("test.dat"); 684 const base::FilePath src_file_path = temp_dir_.GetPath().Append("test.dat");
685 const std::string src_contents = "test"; 685 const std::string src_contents = "test";
686 EXPECT_TRUE(google_apis::test_util::WriteStringToFile(src_file_path, 686 EXPECT_TRUE(google_apis::test_util::WriteStringToFile(src_file_path,
687 src_contents)); 687 src_contents));
688 std::string id("id"); 688 std::string id("id");
689 std::string md5(base::MD5String(src_contents)); 689 std::string md5(base::MD5String(src_contents));
690 690
691 // First store a file to cache. 691 // First store a file to cache.
692 ResourceEntry entry; 692 ResourceEntry entry;
693 entry.set_local_id(id); 693 entry.set_local_id(id);
694 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry)); 694 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry));
695 base::FilePath src_file; 695 base::FilePath src_file;
696 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), &src_file)); 696 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.GetPath(), &src_file));
697 EXPECT_EQ(FILE_ERROR_OK, cache_->Store( 697 EXPECT_EQ(FILE_ERROR_OK, cache_->Store(
698 id, md5, src_file_path, FileCache::FILE_OPERATION_COPY)); 698 id, md5, src_file_path, FileCache::FILE_OPERATION_COPY));
699 699
700 base::FilePath cache_file_path; 700 base::FilePath cache_file_path;
701 EXPECT_EQ(FILE_ERROR_OK, cache_->GetFile(id, &cache_file_path)); 701 EXPECT_EQ(FILE_ERROR_OK, cache_->GetFile(id, &cache_file_path));
702 702
703 // Then try to remove existing file from cache. 703 // Then try to remove existing file from cache.
704 EXPECT_EQ(FILE_ERROR_OK, cache_->Remove(id)); 704 EXPECT_EQ(FILE_ERROR_OK, cache_->Remove(id));
705 EXPECT_FALSE(base::PathExists(cache_file_path)); 705 EXPECT_FALSE(base::PathExists(cache_file_path));
706 } 706 }
707 707
708 TEST_F(FileCacheTest, RenameCacheFilesToNewFormat) { 708 TEST_F(FileCacheTest, RenameCacheFilesToNewFormat) {
709 const base::FilePath file_directory = 709 const base::FilePath file_directory =
710 temp_dir_.path().Append(kCacheFileDirectory); 710 temp_dir_.GetPath().Append(kCacheFileDirectory);
711 711
712 // File with an old style "<prefix>:<ID>.<MD5>" name. 712 // File with an old style "<prefix>:<ID>.<MD5>" name.
713 ASSERT_TRUE(google_apis::test_util::WriteStringToFile( 713 ASSERT_TRUE(google_apis::test_util::WriteStringToFile(
714 file_directory.AppendASCII("file:id_koo.md5"), "koo")); 714 file_directory.AppendASCII("file:id_koo.md5"), "koo"));
715 715
716 // File with multiple extensions should be removed. 716 // File with multiple extensions should be removed.
717 ASSERT_TRUE(google_apis::test_util::WriteStringToFile( 717 ASSERT_TRUE(google_apis::test_util::WriteStringToFile(
718 file_directory.AppendASCII("id_kyu.md5.mounted"), "kyu (mounted)")); 718 file_directory.AppendASCII("id_kyu.md5.mounted"), "kyu (mounted)"));
719 ASSERT_TRUE(google_apis::test_util::WriteStringToFile( 719 ASSERT_TRUE(google_apis::test_util::WriteStringToFile(
720 file_directory.AppendASCII("id_kyu.md5"), "kyu")); 720 file_directory.AppendASCII("id_kyu.md5"), "kyu"));
(...skipping 19 matching lines...) Expand all
740 EXPECT_EQ("koo", contents); 740 EXPECT_EQ("koo", contents);
741 contents.clear(); 741 contents.clear();
742 EXPECT_TRUE(base::ReadFileToString(file_directory.AppendASCII("id_kyu"), 742 EXPECT_TRUE(base::ReadFileToString(file_directory.AppendASCII("id_kyu"),
743 &contents)); 743 &contents));
744 EXPECT_EQ("kyu", contents); 744 EXPECT_EQ("kyu", contents);
745 } 745 }
746 746
747 TEST_F(FileCacheTest, FixMetadataAndFileAttributes) { 747 TEST_F(FileCacheTest, FixMetadataAndFileAttributes) {
748 // Create test files and metadata. 748 // Create test files and metadata.
749 base::FilePath temp_file; 749 base::FilePath temp_file;
750 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), &temp_file)); 750 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.GetPath(), &temp_file));
751 751
752 // Entry A: pinned cache file. 752 // Entry A: pinned cache file.
753 const std::string id_a = "id_a"; 753 const std::string id_a = "id_a";
754 ResourceEntry entry_a; 754 ResourceEntry entry_a;
755 entry_a.set_local_id(id_a); 755 entry_a.set_local_id(id_a);
756 FileCacheEntry* file_cache_entry_a = 756 FileCacheEntry* file_cache_entry_a =
757 entry_a.mutable_file_specific_info()->mutable_cache_state(); 757 entry_a.mutable_file_specific_info()->mutable_cache_state();
758 file_cache_entry_a->set_is_present(true); 758 file_cache_entry_a->set_is_present(true);
759 file_cache_entry_a->set_is_pinned(true); 759 file_cache_entry_a->set_is_pinned(true);
760 file_cache_entry_a->set_is_dirty(false); 760 file_cache_entry_a->set_is_dirty(false);
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
862 862
863 TEST_F(FileCacheTest, ClearAll) { 863 TEST_F(FileCacheTest, ClearAll) {
864 const std::string id("1a2b"); 864 const std::string id("1a2b");
865 const std::string md5("abcdef0123456789"); 865 const std::string md5("abcdef0123456789");
866 866
867 // Store an existing file. 867 // Store an existing file.
868 ResourceEntry entry; 868 ResourceEntry entry;
869 entry.set_local_id(id); 869 entry.set_local_id(id);
870 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry)); 870 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry));
871 base::FilePath src_file; 871 base::FilePath src_file;
872 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), &src_file)); 872 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.GetPath(), &src_file));
873 ASSERT_EQ(FILE_ERROR_OK, 873 ASSERT_EQ(FILE_ERROR_OK,
874 cache_->Store(id, md5, src_file, FileCache::FILE_OPERATION_COPY)); 874 cache_->Store(id, md5, src_file, FileCache::FILE_OPERATION_COPY));
875 875
876 // Clear cache. 876 // Clear cache.
877 EXPECT_TRUE(cache_->ClearAll()); 877 EXPECT_TRUE(cache_->ClearAll());
878 878
879 // Verify that the cache is removed. 879 // Verify that the cache is removed.
880 EXPECT_TRUE(base::IsDirectoryEmpty(cache_files_dir_)); 880 EXPECT_TRUE(base::IsDirectoryEmpty(cache_files_dir_));
881 } 881 }
882 882
883 } // namespace internal 883 } // namespace internal
884 } // namespace drive 884 } // namespace drive
OLDNEW
« no previous file with comments | « components/drive/chromeos/fake_file_system.cc ('k') | components/drive/directory_loader_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698