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 384 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
395 ASSERT_TRUE(storage_->Initialize()); | 395 ASSERT_TRUE(storage_->Initialize()); |
396 | 396 |
397 // Data is erased because of the incompatible version. | 397 // Data is erased because of the incompatible version. |
398 int64 largest_changestamp = 0; | 398 int64 largest_changestamp = 0; |
399 EXPECT_EQ(FILE_ERROR_OK, | 399 EXPECT_EQ(FILE_ERROR_OK, |
400 storage_->GetLargestChangestamp(&largest_changestamp)); | 400 storage_->GetLargestChangestamp(&largest_changestamp)); |
401 EXPECT_EQ(0, largest_changestamp); | 401 EXPECT_EQ(0, largest_changestamp); |
402 EXPECT_EQ(FILE_ERROR_NOT_FOUND, storage_->GetEntry(key1, &entry)); | 402 EXPECT_EQ(FILE_ERROR_NOT_FOUND, storage_->GetEntry(key1, &entry)); |
403 } | 403 } |
404 | 404 |
| 405 TEST_F(ResourceMetadataStorageTest, DeleteUnusedIDEntries) { |
| 406 leveldb::WriteBatch batch; |
| 407 |
| 408 // Put an ID entry with a corresponding ResourceEntry. |
| 409 ResourceEntry entry; |
| 410 entry.set_local_id("id1"); |
| 411 entry.set_resource_id("resource_id1"); |
| 412 |
| 413 std::string serialized_entry; |
| 414 EXPECT_TRUE(entry.SerializeToString(&serialized_entry)); |
| 415 batch.Put("id1", serialized_entry); |
| 416 batch.Put('\0' + std::string("ID") + '\0' + "resource_id1", "id1"); |
| 417 |
| 418 // Put an ID entry with a corresponding FileCacheEntry. |
| 419 FileCacheEntry cache_entry; |
| 420 EXPECT_TRUE(cache_entry.SerializeToString(&serialized_entry)); |
| 421 batch.Put(std::string("id2") + '\0' + "CACHE", serialized_entry); |
| 422 batch.Put('\0' + std::string("ID") + '\0' + "resource_id2", "id2"); |
| 423 |
| 424 // Put an ID entry without any corresponding entries. |
| 425 batch.Put('\0' + std::string("ID") + '\0' + "resource_id3", "id3"); |
| 426 |
| 427 EXPECT_TRUE(resource_map()->Write(leveldb::WriteOptions(), &batch).ok()); |
| 428 |
| 429 // Upgrade and reopen. |
| 430 storage_.reset(); |
| 431 EXPECT_TRUE(ResourceMetadataStorage::UpgradeOldDB( |
| 432 temp_dir_.path(), base::Bind(&util::CanonicalizeResourceId))); |
| 433 storage_.reset(new ResourceMetadataStorage( |
| 434 temp_dir_.path(), base::MessageLoopProxy::current().get())); |
| 435 ASSERT_TRUE(storage_->Initialize()); |
| 436 |
| 437 // Only the unused entry is deleted. |
| 438 std::string id; |
| 439 EXPECT_EQ(FILE_ERROR_OK, storage_->GetIdByResourceId("resource_id1", &id)); |
| 440 EXPECT_EQ("id1", id); |
| 441 EXPECT_EQ(FILE_ERROR_OK, storage_->GetIdByResourceId("resource_id2", &id)); |
| 442 EXPECT_EQ("id2", id); |
| 443 EXPECT_EQ(FILE_ERROR_NOT_FOUND, |
| 444 storage_->GetIdByResourceId("resource_id3", &id)); |
| 445 } |
| 446 |
405 TEST_F(ResourceMetadataStorageTest, WrongPath) { | 447 TEST_F(ResourceMetadataStorageTest, WrongPath) { |
406 // Create a file. | 448 // Create a file. |
407 base::FilePath path; | 449 base::FilePath path; |
408 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), &path)); | 450 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), &path)); |
409 | 451 |
410 storage_.reset(new ResourceMetadataStorage( | 452 storage_.reset(new ResourceMetadataStorage( |
411 path, base::MessageLoopProxy::current().get())); | 453 path, base::MessageLoopProxy::current().get())); |
412 // Cannot initialize DB beacause the path does not point a directory. | 454 // Cannot initialize DB beacause the path does not point a directory. |
413 ASSERT_FALSE(storage_->Initialize()); | 455 ASSERT_FALSE(storage_->Initialize()); |
414 } | 456 } |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
519 EXPECT_EQ(FILE_ERROR_OK, storage_->RemoveEntry(key3)); | 561 EXPECT_EQ(FILE_ERROR_OK, storage_->RemoveEntry(key3)); |
520 EXPECT_TRUE(CheckValidity()); | 562 EXPECT_TRUE(CheckValidity()); |
521 | 563 |
522 // Remove key1. | 564 // Remove key1. |
523 EXPECT_EQ(FILE_ERROR_OK, storage_->RemoveEntry(key1)); | 565 EXPECT_EQ(FILE_ERROR_OK, storage_->RemoveEntry(key1)); |
524 EXPECT_TRUE(CheckValidity()); | 566 EXPECT_TRUE(CheckValidity()); |
525 } | 567 } |
526 | 568 |
527 } // namespace internal | 569 } // namespace internal |
528 } // namespace drive | 570 } // namespace drive |
OLD | NEW |