| 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 "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" |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 147 AttachmentStore::CreateOnDiskStore( | 147 AttachmentStore::CreateOnDiskStore( |
| 148 temp_dir_.path(), | 148 temp_dir_.path(), |
| 149 base::ThreadTaskRunnerHandle::Get(), | 149 base::ThreadTaskRunnerHandle::Get(), |
| 150 base::Bind(&AttachmentStoreCreated, &store_, &result)); | 150 base::Bind(&AttachmentStoreCreated, &store_, &result)); |
| 151 RunLoop(); | 151 RunLoop(); |
| 152 EXPECT_EQ(result, AttachmentStore::SUCCESS); | 152 EXPECT_EQ(result, AttachmentStore::SUCCESS); |
| 153 | 153 |
| 154 result = AttachmentStore::UNSPECIFIED_ERROR; | 154 result = AttachmentStore::UNSPECIFIED_ERROR; |
| 155 std::string some_data = "data"; | 155 std::string some_data = "data"; |
| 156 Attachment attachment = | 156 Attachment attachment = |
| 157 Attachment::Create(base::RefCountedString::TakeString(&some_data)); | 157 Attachment::CreateNew(base::RefCountedString::TakeString(&some_data)); |
| 158 AttachmentList attachments; | 158 AttachmentList attachments; |
| 159 attachments.push_back(attachment); | 159 attachments.push_back(attachment); |
| 160 store_->Write(attachments, | 160 store_->Write(attachments, |
| 161 base::Bind(&OnDiskAttachmentStoreSpecificTest::CopyResult, | 161 base::Bind(&OnDiskAttachmentStoreSpecificTest::CopyResult, |
| 162 base::Unretained(this), | 162 base::Unretained(this), |
| 163 &result)); | 163 &result)); |
| 164 RunLoop(); | 164 RunLoop(); |
| 165 EXPECT_EQ(result, AttachmentStore::SUCCESS); | 165 EXPECT_EQ(result, AttachmentStore::SUCCESS); |
| 166 | 166 |
| 167 // Close attachment store. | 167 // Close attachment store. |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 259 result = AttachmentStore::SUCCESS; | 259 result = AttachmentStore::SUCCESS; |
| 260 AttachmentStore::CreateOnDiskStore( | 260 AttachmentStore::CreateOnDiskStore( |
| 261 temp_dir_.path(), | 261 temp_dir_.path(), |
| 262 base::ThreadTaskRunnerHandle::Get(), | 262 base::ThreadTaskRunnerHandle::Get(), |
| 263 base::Bind(&AttachmentStoreCreated, &store_, &result)); | 263 base::Bind(&AttachmentStoreCreated, &store_, &result)); |
| 264 RunLoop(); | 264 RunLoop(); |
| 265 EXPECT_EQ(result, AttachmentStore::UNSPECIFIED_ERROR); | 265 EXPECT_EQ(result, AttachmentStore::UNSPECIFIED_ERROR); |
| 266 EXPECT_EQ(store_.get(), nullptr); | 266 EXPECT_EQ(store_.get(), nullptr); |
| 267 } | 267 } |
| 268 | 268 |
| 269 // Ensure that attachment store correctly maintains metadata records for |
| 270 // attachments. |
| 269 TEST_F(OnDiskAttachmentStoreSpecificTest, RecordMetadata) { | 271 TEST_F(OnDiskAttachmentStoreSpecificTest, RecordMetadata) { |
| 270 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 272 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 271 base::FilePath db_path = | 273 base::FilePath db_path = |
| 272 temp_dir_.path().Append(FILE_PATH_LITERAL("leveldb")); | 274 temp_dir_.path().Append(FILE_PATH_LITERAL("leveldb")); |
| 273 | 275 |
| 274 // Create attachment store. | 276 // Create attachment store. |
| 275 AttachmentStore::Result result = AttachmentStore::UNSPECIFIED_ERROR; | 277 AttachmentStore::Result result = AttachmentStore::UNSPECIFIED_ERROR; |
| 276 AttachmentStore::CreateOnDiskStore( | 278 AttachmentStore::CreateOnDiskStore( |
| 277 temp_dir_.path(), | 279 temp_dir_.path(), |
| 278 base::ThreadTaskRunnerHandle::Get(), | 280 base::ThreadTaskRunnerHandle::Get(), |
| 279 base::Bind(&AttachmentStoreCreated, &store_, &result)); | 281 base::Bind(&AttachmentStoreCreated, &store_, &result)); |
| 280 RunLoop(); | 282 RunLoop(); |
| 281 EXPECT_EQ(result, AttachmentStore::SUCCESS); | 283 EXPECT_EQ(result, AttachmentStore::SUCCESS); |
| 282 | 284 |
| 283 // Write two attachments. | 285 // Write two attachments. |
| 284 std::string some_data; | 286 std::string some_data; |
| 285 AttachmentList attachments; | 287 AttachmentList attachments; |
| 286 some_data = "data1"; | 288 some_data = "data1"; |
| 287 attachments.push_back( | 289 attachments.push_back( |
| 288 Attachment::Create(base::RefCountedString::TakeString(&some_data))); | 290 Attachment::CreateNew(base::RefCountedString::TakeString(&some_data))); |
| 289 some_data = "data2"; | 291 some_data = "data2"; |
| 290 attachments.push_back( | 292 attachments.push_back( |
| 291 Attachment::Create(base::RefCountedString::TakeString(&some_data))); | 293 Attachment::CreateNew(base::RefCountedString::TakeString(&some_data))); |
| 292 store_->Write(attachments, | 294 store_->Write(attachments, |
| 293 base::Bind(&OnDiskAttachmentStoreSpecificTest::CopyResult, | 295 base::Bind(&OnDiskAttachmentStoreSpecificTest::CopyResult, |
| 294 base::Unretained(this), | 296 base::Unretained(this), |
| 295 &result)); | 297 &result)); |
| 296 RunLoop(); | 298 RunLoop(); |
| 297 EXPECT_EQ(result, AttachmentStore::SUCCESS); | 299 EXPECT_EQ(result, AttachmentStore::SUCCESS); |
| 298 | 300 |
| 299 // Delete one of written attachments. | 301 // Delete one of written attachments. |
| 300 AttachmentIdList attachment_ids; | 302 AttachmentIdList attachment_ids; |
| 301 attachment_ids.push_back(attachments[0].GetId()); | 303 attachment_ids.push_back(attachments[0].GetId()); |
| 302 store_->Drop(attachment_ids, | 304 store_->Drop(attachment_ids, |
| 303 base::Bind(&OnDiskAttachmentStoreSpecificTest::CopyResult, | 305 base::Bind(&OnDiskAttachmentStoreSpecificTest::CopyResult, |
| 304 base::Unretained(this), | 306 base::Unretained(this), |
| 305 &result)); | 307 &result)); |
| 306 RunLoop(); | 308 RunLoop(); |
| 307 EXPECT_EQ(result, AttachmentStore::SUCCESS); | 309 EXPECT_EQ(result, AttachmentStore::SUCCESS); |
| 308 store_ = nullptr; | 310 store_ = nullptr; |
| 309 RunLoop(); | 311 RunLoop(); |
| 310 | 312 |
| 311 // Verify that attachment store contains only records for second attachment. | 313 // Verify that attachment store contains only records for second attachment. |
| 312 VerifyAttachmentRecordsPresent(db_path, attachments[0].GetId(), false); | 314 VerifyAttachmentRecordsPresent(db_path, attachments[0].GetId(), false); |
| 313 VerifyAttachmentRecordsPresent(db_path, attachments[1].GetId(), true); | 315 VerifyAttachmentRecordsPresent(db_path, attachments[1].GetId(), true); |
| 314 } | 316 } |
| 315 | 317 |
| 318 // Ensure that attachment store fails to load attachment with mismatched crc. |
| 319 TEST_F(OnDiskAttachmentStoreSpecificTest, MismatchedCrc) { |
| 320 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 321 // base::FilePath db_path = |
| 322 // temp_dir_.path().Append(FILE_PATH_LITERAL("leveldb")); |
| 323 |
| 324 // Create attachment store. |
| 325 AttachmentStore::Result result = AttachmentStore::UNSPECIFIED_ERROR; |
| 326 AttachmentStore::CreateOnDiskStore( |
| 327 temp_dir_.path(), base::ThreadTaskRunnerHandle::Get(), |
| 328 base::Bind(&AttachmentStoreCreated, &store_, &result)); |
| 329 RunLoop(); |
| 330 EXPECT_EQ(result, AttachmentStore::SUCCESS); |
| 331 |
| 332 // Write attachment. |
| 333 std::string some_data("data1"); |
| 334 Attachment attachment = Attachment::RestoreExisting( |
| 335 AttachmentId::Create(), base::RefCountedString::TakeString(&some_data), |
| 336 0); |
| 337 AttachmentList attachments; |
| 338 attachments.push_back(attachment); |
| 339 store_->Write(attachments, |
| 340 base::Bind(&OnDiskAttachmentStoreSpecificTest::CopyResult, |
| 341 base::Unretained(this), &result)); |
| 342 RunLoop(); |
| 343 EXPECT_EQ(result, AttachmentStore::SUCCESS); |
| 344 |
| 345 // Read attachment. |
| 346 AttachmentIdList attachment_ids; |
| 347 attachment_ids.push_back(attachment.GetId()); |
| 348 store_->Read( |
| 349 attachment_ids, |
| 350 base::Bind(&OnDiskAttachmentStoreSpecificTest::CopyResultAttachments, |
| 351 base::Unretained(this), &result)); |
| 352 RunLoop(); |
| 353 EXPECT_EQ(result, AttachmentStore::UNSPECIFIED_ERROR); |
| 354 } |
| 355 |
| 316 } // namespace syncer | 356 } // namespace syncer |
| OLD | NEW |