| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "chrome/browser/chromeos/drive/resource_metadata_storage.h" | 5 #include "chrome/browser/chromeos/drive/resource_metadata_storage.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/files/scoped_temp_dir.h" | 10 #include "base/files/scoped_temp_dir.h" |
| (...skipping 422 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 433 // Create a file. | 433 // Create a file. |
| 434 base::FilePath path; | 434 base::FilePath path; |
| 435 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(temp_dir_.path(), &path)); | 435 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(temp_dir_.path(), &path)); |
| 436 | 436 |
| 437 storage_.reset(new ResourceMetadataStorage( | 437 storage_.reset(new ResourceMetadataStorage( |
| 438 path, base::MessageLoopProxy::current().get())); | 438 path, base::MessageLoopProxy::current().get())); |
| 439 // Cannot initialize DB beacause the path does not point a directory. | 439 // Cannot initialize DB beacause the path does not point a directory. |
| 440 ASSERT_FALSE(storage_->Initialize()); | 440 ASSERT_FALSE(storage_->Initialize()); |
| 441 } | 441 } |
| 442 | 442 |
| 443 TEST_F(ResourceMetadataStorageTest, RecoverCacheEntriesFromTrashedResourceMap) { |
| 444 // Put some cache entries. |
| 445 FileCacheEntry cache_entry; |
| 446 cache_entry.set_md5("md5_foo"); |
| 447 EXPECT_TRUE(storage_->PutCacheEntry("id_foo", cache_entry)); |
| 448 cache_entry.set_md5("md5_bar"); |
| 449 EXPECT_TRUE(storage_->PutCacheEntry("id_bar", cache_entry)); |
| 450 |
| 451 // Put entry with id_foo. |
| 452 ResourceEntry entry; |
| 453 entry.set_local_id("id_foo"); |
| 454 entry.set_base_name("foo"); |
| 455 EXPECT_TRUE(storage_->PutEntry(entry)); |
| 456 |
| 457 // Put entry with id_bar as a id_foo's child. |
| 458 entry.set_local_id("id_bar"); |
| 459 entry.set_parent_local_id("id_foo"); |
| 460 entry.set_base_name("bar"); |
| 461 EXPECT_TRUE(storage_->PutEntry(entry)); |
| 462 |
| 463 // Remove parent-child relationship to make the DB invalid. |
| 464 RemoveChild("id_foo", "bar"); |
| 465 EXPECT_FALSE(CheckValidity()); |
| 466 |
| 467 // Reopen. This should result in trashing the DB. |
| 468 storage_.reset(new ResourceMetadataStorage( |
| 469 temp_dir_.path(), base::MessageLoopProxy::current().get())); |
| 470 ASSERT_TRUE(storage_->Initialize()); |
| 471 |
| 472 // Recover cache entries from the trashed DB. |
| 473 std::map<std::string, FileCacheEntry> recovered_cache_entries; |
| 474 storage_->RecoverCacheEntriesFromTrashedResourceMap(&recovered_cache_entries); |
| 475 EXPECT_EQ(2U, recovered_cache_entries.size()); |
| 476 EXPECT_EQ("md5_foo", recovered_cache_entries["id_foo"].md5()); |
| 477 EXPECT_EQ("md5_bar", recovered_cache_entries["id_bar"].md5()); |
| 478 } |
| 479 |
| 443 TEST_F(ResourceMetadataStorageTest, CheckValidity) { | 480 TEST_F(ResourceMetadataStorageTest, CheckValidity) { |
| 444 const std::string key1 = "foo"; | 481 const std::string key1 = "foo"; |
| 445 const std::string name1 = "hoge"; | 482 const std::string name1 = "hoge"; |
| 446 const std::string key2 = "bar"; | 483 const std::string key2 = "bar"; |
| 447 const std::string name2 = "fuga"; | 484 const std::string name2 = "fuga"; |
| 448 const std::string key3 = "boo"; | 485 const std::string key3 = "boo"; |
| 449 const std::string name3 = "piyo"; | 486 const std::string name3 = "piyo"; |
| 450 | 487 |
| 451 // Empty storage is valid. | 488 // Empty storage is valid. |
| 452 EXPECT_TRUE(CheckValidity()); | 489 EXPECT_TRUE(CheckValidity()); |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 512 EXPECT_TRUE(storage_->RemoveEntry(key3)); | 549 EXPECT_TRUE(storage_->RemoveEntry(key3)); |
| 513 EXPECT_TRUE(CheckValidity()); | 550 EXPECT_TRUE(CheckValidity()); |
| 514 | 551 |
| 515 // Remove key1. | 552 // Remove key1. |
| 516 EXPECT_TRUE(storage_->RemoveEntry(key1)); | 553 EXPECT_TRUE(storage_->RemoveEntry(key1)); |
| 517 EXPECT_TRUE(CheckValidity()); | 554 EXPECT_TRUE(CheckValidity()); |
| 518 } | 555 } |
| 519 | 556 |
| 520 } // namespace internal | 557 } // namespace internal |
| 521 } // namespace drive | 558 } // namespace drive |
| OLD | NEW |