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/api/attachments/attachment_store.h" | 5 #include "sync/internal_api/public/attachments/on_disk_attachment_store.h" |
6 | 6 |
7 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
9 #include "base/files/scoped_temp_dir.h" | 9 #include "base/files/scoped_temp_dir.h" |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
11 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
12 #include "base/thread_task_runner_handle.h" | 12 #include "base/thread_task_runner_handle.h" |
13 #include "base/time/time.h" | 13 #include "base/time/time.h" |
14 #include "sync/internal_api/attachments/attachment_store_test_template.h" | 14 #include "sync/internal_api/attachments/attachment_store_test_template.h" |
15 #include "sync/internal_api/attachments/proto/attachment_store.pb.h" | 15 #include "sync/internal_api/attachments/proto/attachment_store.pb.h" |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
102 | 102 |
103 std::string ReadStoreMetadataRecord(const base::FilePath& path) { | 103 std::string ReadStoreMetadataRecord(const base::FilePath& path) { |
104 scoped_ptr<leveldb::DB> db = OpenLevelDB(path); | 104 scoped_ptr<leveldb::DB> db = OpenLevelDB(path); |
105 std::string content; | 105 std::string content; |
106 leveldb::Status s = | 106 leveldb::Status s = |
107 db->Get(leveldb::ReadOptions(), "database-metadata", &content); | 107 db->Get(leveldb::ReadOptions(), "database-metadata", &content); |
108 EXPECT_TRUE(s.ok()); | 108 EXPECT_TRUE(s.ok()); |
109 return content; | 109 return content; |
110 } | 110 } |
111 | 111 |
| 112 void VerifyAttachmentRecordsPresent(const base::FilePath& path, |
| 113 const AttachmentId& attachment_id, |
| 114 bool expect_records_present) { |
| 115 scoped_ptr<leveldb::DB> db = OpenLevelDB(path); |
| 116 |
| 117 std::string metadata_key = |
| 118 OnDiskAttachmentStore::MakeMetadataKeyFromAttachmentId(attachment_id); |
| 119 std::string data_key = |
| 120 OnDiskAttachmentStore::MakeDataKeyFromAttachmentId(attachment_id); |
| 121 std::string data; |
| 122 leveldb::Status s = db->Get(leveldb::ReadOptions(), data_key, &data); |
| 123 if (expect_records_present) |
| 124 EXPECT_TRUE(s.ok()); |
| 125 else |
| 126 EXPECT_TRUE(s.IsNotFound()); |
| 127 s = db->Get(leveldb::ReadOptions(), metadata_key, &data); |
| 128 if (expect_records_present) |
| 129 EXPECT_TRUE(s.ok()); |
| 130 else |
| 131 EXPECT_TRUE(s.IsNotFound()); |
| 132 } |
| 133 |
112 void RunLoop() { | 134 void RunLoop() { |
113 base::RunLoop run_loop; | 135 base::RunLoop run_loop; |
114 run_loop.RunUntilIdle(); | 136 run_loop.RunUntilIdle(); |
115 } | 137 } |
116 }; | 138 }; |
117 | 139 |
118 // Ensure that store can be closed and reopen while retaining stored | 140 // Ensure that store can be closed and reopen while retaining stored |
119 // attachments. | 141 // attachments. |
120 TEST_F(OnDiskAttachmentStoreSpecificTest, CloseAndReopen) { | 142 TEST_F(OnDiskAttachmentStoreSpecificTest, CloseAndReopen) { |
121 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 143 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
204 base::ThreadTaskRunnerHandle::Get(), | 226 base::ThreadTaskRunnerHandle::Get(), |
205 base::Bind(&AttachmentStoreCreated, &store_, &result)); | 227 base::Bind(&AttachmentStoreCreated, &store_, &result)); |
206 RunLoop(); | 228 RunLoop(); |
207 EXPECT_EQ(result, AttachmentStore::SUCCESS); | 229 EXPECT_EQ(result, AttachmentStore::SUCCESS); |
208 // Close AttachmentStore so that test can check content. | 230 // Close AttachmentStore so that test can check content. |
209 store_ = nullptr; | 231 store_ = nullptr; |
210 RunLoop(); | 232 RunLoop(); |
211 | 233 |
212 // AttachmentStore should create metadata record. | 234 // AttachmentStore should create metadata record. |
213 std::string data = ReadStoreMetadataRecord(db_path); | 235 std::string data = ReadStoreMetadataRecord(db_path); |
214 attachment_store_pb::AttachmentStoreMetadata metadata; | 236 attachment_store_pb::StoreMetadata metadata; |
215 EXPECT_TRUE(metadata.ParseFromString(data)); | 237 EXPECT_TRUE(metadata.ParseFromString(data)); |
216 EXPECT_EQ(metadata.schema_version(), 1); | 238 EXPECT_EQ(metadata.schema_version(), 1); |
217 | 239 |
218 // Set unknown future schema version. | 240 // Set unknown future schema version. |
219 metadata.set_schema_version(2); | 241 metadata.set_schema_version(2); |
220 data = metadata.SerializeAsString(); | 242 data = metadata.SerializeAsString(); |
221 UpdateStoreMetadataRecord(db_path, data); | 243 UpdateStoreMetadataRecord(db_path, data); |
222 | 244 |
223 // AttachmentStore should fail to load. | 245 // AttachmentStore should fail to load. |
224 result = AttachmentStore::SUCCESS; | 246 result = AttachmentStore::SUCCESS; |
(...skipping 12 matching lines...) Expand all Loading... |
237 result = AttachmentStore::SUCCESS; | 259 result = AttachmentStore::SUCCESS; |
238 AttachmentStore::CreateOnDiskStore( | 260 AttachmentStore::CreateOnDiskStore( |
239 temp_dir_.path(), | 261 temp_dir_.path(), |
240 base::ThreadTaskRunnerHandle::Get(), | 262 base::ThreadTaskRunnerHandle::Get(), |
241 base::Bind(&AttachmentStoreCreated, &store_, &result)); | 263 base::Bind(&AttachmentStoreCreated, &store_, &result)); |
242 RunLoop(); | 264 RunLoop(); |
243 EXPECT_EQ(result, AttachmentStore::UNSPECIFIED_ERROR); | 265 EXPECT_EQ(result, AttachmentStore::UNSPECIFIED_ERROR); |
244 EXPECT_EQ(store_.get(), nullptr); | 266 EXPECT_EQ(store_.get(), nullptr); |
245 } | 267 } |
246 | 268 |
| 269 TEST_F(OnDiskAttachmentStoreSpecificTest, RecordMetadata) { |
| 270 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 271 base::FilePath db_path = |
| 272 temp_dir_.path().Append(FILE_PATH_LITERAL("leveldb")); |
| 273 |
| 274 // Create attachment store. |
| 275 AttachmentStore::Result result = AttachmentStore::UNSPECIFIED_ERROR; |
| 276 AttachmentStore::CreateOnDiskStore( |
| 277 temp_dir_.path(), |
| 278 base::ThreadTaskRunnerHandle::Get(), |
| 279 base::Bind(&AttachmentStoreCreated, &store_, &result)); |
| 280 RunLoop(); |
| 281 EXPECT_EQ(result, AttachmentStore::SUCCESS); |
| 282 |
| 283 // Write two attachments. |
| 284 std::string some_data; |
| 285 AttachmentList attachments; |
| 286 some_data = "data1"; |
| 287 attachments.push_back( |
| 288 Attachment::Create(base::RefCountedString::TakeString(&some_data))); |
| 289 some_data = "data2"; |
| 290 attachments.push_back( |
| 291 Attachment::Create(base::RefCountedString::TakeString(&some_data))); |
| 292 store_->Write(attachments, |
| 293 base::Bind(&OnDiskAttachmentStoreSpecificTest::CopyResult, |
| 294 base::Unretained(this), |
| 295 &result)); |
| 296 RunLoop(); |
| 297 EXPECT_EQ(result, AttachmentStore::SUCCESS); |
| 298 |
| 299 // Delete one of written attachments. |
| 300 AttachmentIdList attachment_ids; |
| 301 attachment_ids.push_back(attachments[0].GetId()); |
| 302 store_->Drop(attachment_ids, |
| 303 base::Bind(&OnDiskAttachmentStoreSpecificTest::CopyResult, |
| 304 base::Unretained(this), |
| 305 &result)); |
| 306 RunLoop(); |
| 307 EXPECT_EQ(result, AttachmentStore::SUCCESS); |
| 308 store_ = nullptr; |
| 309 RunLoop(); |
| 310 |
| 311 // Verify that attachment store contains only records for second attachment. |
| 312 VerifyAttachmentRecordsPresent(db_path, attachments[0].GetId(), false); |
| 313 VerifyAttachmentRecordsPresent(db_path, attachments[1].GetId(), true); |
| 314 } |
| 315 |
247 } // namespace syncer | 316 } // namespace syncer |
OLD | NEW |