| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "sync/internal_api/public/attachments/on_disk_attachment_store.h" | 5 #include "sync/internal_api/public/attachments/on_disk_attachment_store.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 |
| 8 #include <string> | 9 #include <string> |
| 9 #include <utility> | 10 #include <utility> |
| 10 | 11 |
| 11 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
| 12 #include "base/files/file_util.h" | 13 #include "base/files/file_util.h" |
| 13 #include "base/files/scoped_temp_dir.h" | 14 #include "base/files/scoped_temp_dir.h" |
| 15 #include "base/memory/ptr_util.h" |
| 14 #include "base/message_loop/message_loop.h" | 16 #include "base/message_loop/message_loop.h" |
| 15 #include "base/run_loop.h" | 17 #include "base/run_loop.h" |
| 16 #include "base/thread_task_runner_handle.h" | 18 #include "base/thread_task_runner_handle.h" |
| 17 #include "base/time/time.h" | 19 #include "base/time/time.h" |
| 18 #include "sync/internal_api/attachments/attachment_store_test_template.h" | 20 #include "sync/internal_api/attachments/attachment_store_test_template.h" |
| 19 #include "sync/internal_api/attachments/proto/attachment_store.pb.h" | 21 #include "sync/internal_api/attachments/proto/attachment_store.pb.h" |
| 20 #include "testing/gmock/include/gmock/gmock.h" | 22 #include "testing/gmock/include/gmock/gmock.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
| 22 #include "third_party/leveldatabase/src/include/leveldb/db.h" | 24 #include "third_party/leveldatabase/src/include/leveldb/db.h" |
| 23 #include "third_party/leveldatabase/src/include/leveldb/options.h" | 25 #include "third_party/leveldatabase/src/include/leveldb/options.h" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 34 } | 36 } |
| 35 | 37 |
| 36 } // namespace | 38 } // namespace |
| 37 | 39 |
| 38 // Instantiation of common attachment store tests. | 40 // Instantiation of common attachment store tests. |
| 39 class OnDiskAttachmentStoreFactory { | 41 class OnDiskAttachmentStoreFactory { |
| 40 public: | 42 public: |
| 41 OnDiskAttachmentStoreFactory() {} | 43 OnDiskAttachmentStoreFactory() {} |
| 42 ~OnDiskAttachmentStoreFactory() {} | 44 ~OnDiskAttachmentStoreFactory() {} |
| 43 | 45 |
| 44 scoped_ptr<AttachmentStore> CreateAttachmentStore() { | 46 std::unique_ptr<AttachmentStore> CreateAttachmentStore() { |
| 45 EXPECT_TRUE(temp_dir_.CreateUniqueTempDir()); | 47 EXPECT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 46 scoped_ptr<AttachmentStore> store; | 48 std::unique_ptr<AttachmentStore> store; |
| 47 AttachmentStore::Result result = AttachmentStore::UNSPECIFIED_ERROR; | 49 AttachmentStore::Result result = AttachmentStore::UNSPECIFIED_ERROR; |
| 48 store = AttachmentStore::CreateOnDiskStore( | 50 store = AttachmentStore::CreateOnDiskStore( |
| 49 temp_dir_.path(), base::ThreadTaskRunnerHandle::Get(), | 51 temp_dir_.path(), base::ThreadTaskRunnerHandle::Get(), |
| 50 base::Bind(&AttachmentStoreCreated, &result)); | 52 base::Bind(&AttachmentStoreCreated, &result)); |
| 51 base::RunLoop run_loop; | 53 base::RunLoop run_loop; |
| 52 run_loop.RunUntilIdle(); | 54 run_loop.RunUntilIdle(); |
| 53 EXPECT_EQ(AttachmentStore::SUCCESS, result); | 55 EXPECT_EQ(AttachmentStore::SUCCESS, result); |
| 54 return store; | 56 return store; |
| 55 } | 57 } |
| 56 | 58 |
| 57 private: | 59 private: |
| 58 base::ScopedTempDir temp_dir_; | 60 base::ScopedTempDir temp_dir_; |
| 59 }; | 61 }; |
| 60 | 62 |
| 61 INSTANTIATE_TYPED_TEST_CASE_P(OnDisk, | 63 INSTANTIATE_TYPED_TEST_CASE_P(OnDisk, |
| 62 AttachmentStoreTest, | 64 AttachmentStoreTest, |
| 63 OnDiskAttachmentStoreFactory); | 65 OnDiskAttachmentStoreFactory); |
| 64 | 66 |
| 65 // Tests specific to OnDiskAttachmentStore. | 67 // Tests specific to OnDiskAttachmentStore. |
| 66 class OnDiskAttachmentStoreSpecificTest : public testing::Test { | 68 class OnDiskAttachmentStoreSpecificTest : public testing::Test { |
| 67 public: | 69 public: |
| 68 base::ScopedTempDir temp_dir_; | 70 base::ScopedTempDir temp_dir_; |
| 69 base::FilePath db_path_; | 71 base::FilePath db_path_; |
| 70 base::MessageLoop message_loop_; | 72 base::MessageLoop message_loop_; |
| 71 scoped_ptr<AttachmentStore> store_; | 73 std::unique_ptr<AttachmentStore> store_; |
| 72 | 74 |
| 73 OnDiskAttachmentStoreSpecificTest() {} | 75 OnDiskAttachmentStoreSpecificTest() {} |
| 74 | 76 |
| 75 void SetUp() override { | 77 void SetUp() override { |
| 76 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 78 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 77 db_path_ = temp_dir_.path().Append(FILE_PATH_LITERAL("leveldb")); | 79 db_path_ = temp_dir_.path().Append(FILE_PATH_LITERAL("leveldb")); |
| 78 base::CreateDirectory(db_path_); | 80 base::CreateDirectory(db_path_); |
| 79 } | 81 } |
| 80 | 82 |
| 81 void CopyResult(AttachmentStore::Result* destination_result, | 83 void CopyResult(AttachmentStore::Result* destination_result, |
| 82 const AttachmentStore::Result& source_result) { | 84 const AttachmentStore::Result& source_result) { |
| 83 *destination_result = source_result; | 85 *destination_result = source_result; |
| 84 } | 86 } |
| 85 | 87 |
| 86 void CopyResultAttachments( | 88 void CopyResultAttachments( |
| 87 AttachmentStore::Result* destination_result, | 89 AttachmentStore::Result* destination_result, |
| 88 AttachmentIdList* destination_failed_attachment_ids, | 90 AttachmentIdList* destination_failed_attachment_ids, |
| 89 const AttachmentStore::Result& source_result, | 91 const AttachmentStore::Result& source_result, |
| 90 scoped_ptr<AttachmentMap> source_attachments, | 92 std::unique_ptr<AttachmentMap> source_attachments, |
| 91 scoped_ptr<AttachmentIdList> source_failed_attachment_ids) { | 93 std::unique_ptr<AttachmentIdList> source_failed_attachment_ids) { |
| 92 CopyResult(destination_result, source_result); | 94 CopyResult(destination_result, source_result); |
| 93 *destination_failed_attachment_ids = *source_failed_attachment_ids; | 95 *destination_failed_attachment_ids = *source_failed_attachment_ids; |
| 94 } | 96 } |
| 95 | 97 |
| 96 void CopyResultMetadata( | 98 void CopyResultMetadata( |
| 97 AttachmentStore::Result* destination_result, | 99 AttachmentStore::Result* destination_result, |
| 98 scoped_ptr<AttachmentMetadataList>* destination_metadata, | 100 std::unique_ptr<AttachmentMetadataList>* destination_metadata, |
| 99 const AttachmentStore::Result& source_result, | 101 const AttachmentStore::Result& source_result, |
| 100 scoped_ptr<AttachmentMetadataList> source_metadata) { | 102 std::unique_ptr<AttachmentMetadataList> source_metadata) { |
| 101 CopyResult(destination_result, source_result); | 103 CopyResult(destination_result, source_result); |
| 102 *destination_metadata = std::move(source_metadata); | 104 *destination_metadata = std::move(source_metadata); |
| 103 } | 105 } |
| 104 | 106 |
| 105 scoped_ptr<leveldb::DB> OpenLevelDB() { | 107 std::unique_ptr<leveldb::DB> OpenLevelDB() { |
| 106 leveldb::DB* db; | 108 leveldb::DB* db; |
| 107 leveldb::Options options; | 109 leveldb::Options options; |
| 108 options.create_if_missing = true; | 110 options.create_if_missing = true; |
| 109 leveldb::Status s = | 111 leveldb::Status s = |
| 110 leveldb::DB::Open(options, db_path_.AsUTF8Unsafe(), &db); | 112 leveldb::DB::Open(options, db_path_.AsUTF8Unsafe(), &db); |
| 111 EXPECT_TRUE(s.ok()); | 113 EXPECT_TRUE(s.ok()); |
| 112 return make_scoped_ptr(db); | 114 return base::WrapUnique(db); |
| 113 } | 115 } |
| 114 | 116 |
| 115 void UpdateRecord(const std::string& key, const std::string& content) { | 117 void UpdateRecord(const std::string& key, const std::string& content) { |
| 116 scoped_ptr<leveldb::DB> db = OpenLevelDB(); | 118 std::unique_ptr<leveldb::DB> db = OpenLevelDB(); |
| 117 leveldb::Status s = db->Put(leveldb::WriteOptions(), key, content); | 119 leveldb::Status s = db->Put(leveldb::WriteOptions(), key, content); |
| 118 EXPECT_TRUE(s.ok()); | 120 EXPECT_TRUE(s.ok()); |
| 119 } | 121 } |
| 120 | 122 |
| 121 void UpdateStoreMetadataRecord(const std::string& content) { | 123 void UpdateStoreMetadataRecord(const std::string& content) { |
| 122 UpdateRecord("database-metadata", content); | 124 UpdateRecord("database-metadata", content); |
| 123 } | 125 } |
| 124 | 126 |
| 125 void UpdateAttachmentMetadataRecord(const AttachmentId& attachment_id, | 127 void UpdateAttachmentMetadataRecord(const AttachmentId& attachment_id, |
| 126 const std::string& content) { | 128 const std::string& content) { |
| 127 std::string metadata_key = | 129 std::string metadata_key = |
| 128 OnDiskAttachmentStore::MakeMetadataKeyFromAttachmentId(attachment_id); | 130 OnDiskAttachmentStore::MakeMetadataKeyFromAttachmentId(attachment_id); |
| 129 UpdateRecord(metadata_key, content); | 131 UpdateRecord(metadata_key, content); |
| 130 } | 132 } |
| 131 | 133 |
| 132 std::string ReadStoreMetadataRecord() { | 134 std::string ReadStoreMetadataRecord() { |
| 133 scoped_ptr<leveldb::DB> db = OpenLevelDB(); | 135 std::unique_ptr<leveldb::DB> db = OpenLevelDB(); |
| 134 std::string content; | 136 std::string content; |
| 135 leveldb::Status s = | 137 leveldb::Status s = |
| 136 db->Get(leveldb::ReadOptions(), "database-metadata", &content); | 138 db->Get(leveldb::ReadOptions(), "database-metadata", &content); |
| 137 EXPECT_TRUE(s.ok()); | 139 EXPECT_TRUE(s.ok()); |
| 138 return content; | 140 return content; |
| 139 } | 141 } |
| 140 | 142 |
| 141 void VerifyAttachmentRecordsPresent(const AttachmentId& attachment_id, | 143 void VerifyAttachmentRecordsPresent(const AttachmentId& attachment_id, |
| 142 bool expect_records_present) { | 144 bool expect_records_present) { |
| 143 scoped_ptr<leveldb::DB> db = OpenLevelDB(); | 145 std::unique_ptr<leveldb::DB> db = OpenLevelDB(); |
| 144 | 146 |
| 145 std::string metadata_key = | 147 std::string metadata_key = |
| 146 OnDiskAttachmentStore::MakeMetadataKeyFromAttachmentId(attachment_id); | 148 OnDiskAttachmentStore::MakeMetadataKeyFromAttachmentId(attachment_id); |
| 147 std::string data_key = | 149 std::string data_key = |
| 148 OnDiskAttachmentStore::MakeDataKeyFromAttachmentId(attachment_id); | 150 OnDiskAttachmentStore::MakeDataKeyFromAttachmentId(attachment_id); |
| 149 std::string data; | 151 std::string data; |
| 150 leveldb::Status s = db->Get(leveldb::ReadOptions(), data_key, &data); | 152 leveldb::Status s = db->Get(leveldb::ReadOptions(), data_key, &data); |
| 151 if (expect_records_present) | 153 if (expect_records_present) |
| 152 EXPECT_TRUE(s.ok()); | 154 EXPECT_TRUE(s.ok()); |
| 153 else | 155 else |
| (...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 451 UpdateRecord("zzz", "foobar"); | 453 UpdateRecord("zzz", "foobar"); |
| 452 | 454 |
| 453 // Create attachment store. | 455 // Create attachment store. |
| 454 AttachmentStore::Result create_result = AttachmentStore::UNSPECIFIED_ERROR; | 456 AttachmentStore::Result create_result = AttachmentStore::UNSPECIFIED_ERROR; |
| 455 store_ = AttachmentStore::CreateOnDiskStore( | 457 store_ = AttachmentStore::CreateOnDiskStore( |
| 456 temp_dir_.path(), base::ThreadTaskRunnerHandle::Get(), | 458 temp_dir_.path(), base::ThreadTaskRunnerHandle::Get(), |
| 457 base::Bind(&AttachmentStoreCreated, &create_result)); | 459 base::Bind(&AttachmentStoreCreated, &create_result)); |
| 458 | 460 |
| 459 // Read all metadata. Should be getting no error and zero entries. | 461 // Read all metadata. Should be getting no error and zero entries. |
| 460 AttachmentStore::Result metadata_result = AttachmentStore::UNSPECIFIED_ERROR; | 462 AttachmentStore::Result metadata_result = AttachmentStore::UNSPECIFIED_ERROR; |
| 461 scoped_ptr<AttachmentMetadataList> metadata_list; | 463 std::unique_ptr<AttachmentMetadataList> metadata_list; |
| 462 store_->ReadMetadata( | 464 store_->ReadMetadata( |
| 463 base::Bind(&OnDiskAttachmentStoreSpecificTest::CopyResultMetadata, | 465 base::Bind(&OnDiskAttachmentStoreSpecificTest::CopyResultMetadata, |
| 464 base::Unretained(this), &metadata_result, &metadata_list)); | 466 base::Unretained(this), &metadata_result, &metadata_list)); |
| 465 RunLoop(); | 467 RunLoop(); |
| 466 EXPECT_EQ(AttachmentStore::SUCCESS, create_result); | 468 EXPECT_EQ(AttachmentStore::SUCCESS, create_result); |
| 467 EXPECT_EQ(AttachmentStore::SUCCESS, metadata_result); | 469 EXPECT_EQ(AttachmentStore::SUCCESS, metadata_result); |
| 468 EXPECT_EQ(0U, metadata_list->size()); | 470 EXPECT_EQ(0U, metadata_list->size()); |
| 469 metadata_list.reset(); | 471 metadata_list.reset(); |
| 470 | 472 |
| 471 // Write 3 attachments to the store | 473 // Write 3 attachments to the store |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 513 store_->ReadMetadata( | 515 store_->ReadMetadata( |
| 514 base::Bind(&OnDiskAttachmentStoreSpecificTest::CopyResultMetadata, | 516 base::Bind(&OnDiskAttachmentStoreSpecificTest::CopyResultMetadata, |
| 515 base::Unretained(this), &metadata_result, &metadata_list)); | 517 base::Unretained(this), &metadata_result, &metadata_list)); |
| 516 RunLoop(); | 518 RunLoop(); |
| 517 EXPECT_EQ(AttachmentStore::SUCCESS, create_result); | 519 EXPECT_EQ(AttachmentStore::SUCCESS, create_result); |
| 518 EXPECT_EQ(AttachmentStore::UNSPECIFIED_ERROR, metadata_result); | 520 EXPECT_EQ(AttachmentStore::UNSPECIFIED_ERROR, metadata_result); |
| 519 EXPECT_EQ(2U, metadata_list->size()); | 521 EXPECT_EQ(2U, metadata_list->size()); |
| 520 } | 522 } |
| 521 | 523 |
| 522 } // namespace syncer | 524 } // namespace syncer |
| OLD | NEW |