Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(13)

Side by Side Diff: components/sync/test/fake_server/fake_server.cc

Issue 2328393002: [Sync] Add a sanity integration test for USS. (Closed)
Patch Set: Rebase. Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "components/sync/test/fake_server/fake_server.h" 5 #include "components/sync/test/fake_server/fake_server.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 8
9 #include <algorithm> 9 #include <algorithm>
10 #include <limits> 10 #include <limits>
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
93 // part of a GetUpdatesResponse. 93 // part of a GetUpdatesResponse.
94 bool ClientWantsItem(const FakeServerEntity& entity) const { 94 bool ClientWantsItem(const FakeServerEntity& entity) const {
95 int64_t version = entity.GetVersion(); 95 int64_t version = entity.GetVersion();
96 if (version <= min_version_) { 96 if (version <= min_version_) {
97 return false; 97 return false;
98 } else if (entity.IsDeleted()) { 98 } else if (entity.IsDeleted()) {
99 return true; 99 return true;
100 } 100 }
101 101
102 ModelTypeToVersionMap::const_iterator it = 102 ModelTypeToVersionMap::const_iterator it =
103 request_from_version_.find(entity.GetModelType()); 103 request_from_version_.find(entity.model_type());
104 104
105 return it == request_from_version_.end() ? false : it->second < version; 105 return it == request_from_version_.end() ? false : it->second < version;
106 } 106 }
107 107
108 // Returns the minimum version seen across all types. 108 // Returns the minimum version seen across all types.
109 int64_t GetMinVersion() const { return min_version_; } 109 int64_t GetMinVersion() const { return min_version_; }
110 110
111 private: 111 private:
112 typedef std::map<ModelType, int64_t> ModelTypeToVersionMap; 112 typedef std::map<ModelType, int64_t> ModelTypeToVersionMap;
113 113
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
225 225
226 return true; 226 return true;
227 } 227 }
228 228
229 void FakeServer::UpdateEntityVersion(FakeServerEntity* entity) { 229 void FakeServer::UpdateEntityVersion(FakeServerEntity* entity) {
230 entity->SetVersion(++version_); 230 entity->SetVersion(++version_);
231 } 231 }
232 232
233 void FakeServer::SaveEntity(std::unique_ptr<FakeServerEntity> entity) { 233 void FakeServer::SaveEntity(std::unique_ptr<FakeServerEntity> entity) {
234 UpdateEntityVersion(entity.get()); 234 UpdateEntityVersion(entity.get());
235 entities_[entity->GetId()] = std::move(entity); 235 entities_[entity->id()] = std::move(entity);
236 } 236 }
237 237
238 void FakeServer::HandleCommand(const string& request, 238 void FakeServer::HandleCommand(const string& request,
239 const base::Closure& completion_closure, 239 const base::Closure& completion_closure,
240 int* error_code, 240 int* error_code,
241 int* response_code, 241 int* response_code,
242 std::string* response) { 242 std::string* response) {
243 DCHECK(thread_checker_.CalledOnValidThread()); 243 DCHECK(thread_checker_.CalledOnValidThread());
244 if (!network_enabled_) { 244 if (!network_enabled_) {
245 *error_code = net::ERR_FAILED; 245 *error_code = net::ERR_FAILED;
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
361 for (EntityMap::const_iterator it = entities_.begin(); it != entities_.end(); 361 for (EntityMap::const_iterator it = entities_.begin(); it != entities_.end();
362 ++it) { 362 ++it) {
363 const FakeServerEntity& entity = *it->second; 363 const FakeServerEntity& entity = *it->second;
364 if (sieve->ClientWantsItem(entity)) { 364 if (sieve->ClientWantsItem(entity)) {
365 sync_pb::SyncEntity* response_entity = response->add_entries(); 365 sync_pb::SyncEntity* response_entity = response->add_entries();
366 entity.SerializeAsProto(response_entity); 366 entity.SerializeAsProto(response_entity);
367 367
368 max_response_version = 368 max_response_version =
369 std::max(max_response_version, response_entity->version()); 369 std::max(max_response_version, response_entity->version());
370 370
371 if (entity.GetModelType() == syncer::NIGORI) { 371 if (entity.model_type() == syncer::NIGORI) {
372 send_encryption_keys_based_on_nigori = 372 send_encryption_keys_based_on_nigori =
373 response_entity->specifics().nigori().passphrase_type() == 373 response_entity->specifics().nigori().passphrase_type() ==
374 sync_pb::NigoriSpecifics::KEYSTORE_PASSPHRASE; 374 sync_pb::NigoriSpecifics::KEYSTORE_PASSPHRASE;
375 } 375 }
376 } 376 }
377 } 377 }
378 378
379 if (send_encryption_keys_based_on_nigori || 379 if (send_encryption_keys_based_on_nigori ||
380 get_updates.need_encryption_key()) { 380 get_updates.need_encryption_key()) {
381 for (vector<string>::iterator it = keystore_keys_.begin(); 381 for (vector<string>::iterator it = keystore_keys_.begin();
(...skipping 10 matching lines...) Expand all
392 const sync_pb::SyncEntity& client_entity, 392 const sync_pb::SyncEntity& client_entity,
393 sync_pb::CommitResponse_EntryResponse* entry_response, 393 sync_pb::CommitResponse_EntryResponse* entry_response,
394 const string& client_guid, 394 const string& client_guid,
395 const string& parent_id) { 395 const string& parent_id) {
396 if (client_entity.version() == 0 && client_entity.deleted()) { 396 if (client_entity.version() == 0 && client_entity.deleted()) {
397 return string(); 397 return string();
398 } 398 }
399 399
400 std::unique_ptr<FakeServerEntity> entity; 400 std::unique_ptr<FakeServerEntity> entity;
401 if (client_entity.deleted()) { 401 if (client_entity.deleted()) {
402 entity = TombstoneEntity::Create(client_entity.id_string()); 402 entity = TombstoneEntity::Create(client_entity.id_string(),
403 client_entity.client_defined_unique_tag());
403 DeleteChildren(client_entity.id_string()); 404 DeleteChildren(client_entity.id_string());
404 } else if (GetModelType(client_entity) == syncer::NIGORI) { 405 } else if (GetModelType(client_entity) == syncer::NIGORI) {
405 // NIGORI is the only permanent item type that should be updated by the 406 // NIGORI is the only permanent item type that should be updated by the
406 // client. 407 // client.
407 EntityMap::const_iterator iter = entities_.find(client_entity.id_string()); 408 EntityMap::const_iterator iter = entities_.find(client_entity.id_string());
408 CHECK(iter != entities_.end()); 409 CHECK(iter != entities_.end());
409 entity = PermanentEntity::CreateUpdatedNigoriEntity(client_entity, 410 entity = PermanentEntity::CreateUpdatedNigoriEntity(client_entity,
410 *iter->second); 411 *iter->second);
411 } else if (client_entity.has_client_defined_unique_tag()) { 412 } else if (client_entity.has_client_defined_unique_tag()) {
412 entity = UniqueClientEntity::Create(client_entity); 413 entity = UniqueClientEntity::Create(client_entity);
413 } else { 414 } else {
414 // TODO(pvalenzuela): Validate entity's parent ID. 415 // TODO(pvalenzuela): Validate entity's parent ID.
415 EntityMap::const_iterator iter = entities_.find(client_entity.id_string()); 416 EntityMap::const_iterator iter = entities_.find(client_entity.id_string());
416 if (iter != entities_.end()) { 417 if (iter != entities_.end()) {
417 entity = BookmarkEntity::CreateUpdatedVersion(client_entity, 418 entity = BookmarkEntity::CreateUpdatedVersion(client_entity,
418 *iter->second, parent_id); 419 *iter->second, parent_id);
419 } else { 420 } else {
420 entity = BookmarkEntity::CreateNew(client_entity, parent_id, client_guid); 421 entity = BookmarkEntity::CreateNew(client_entity, parent_id, client_guid);
421 } 422 }
422 } 423 }
423 424
424 if (!entity) { 425 if (!entity) {
425 // TODO(pvalenzuela): Add logging so that it is easier to determine why 426 // TODO(pvalenzuela): Add logging so that it is easier to determine why
426 // creation failed. 427 // creation failed.
427 return string(); 428 return string();
428 } 429 }
429 430
430 const std::string id = entity->GetId(); 431 const std::string id = entity->id();
431 SaveEntity(std::move(entity)); 432 SaveEntity(std::move(entity));
432 BuildEntryResponseForSuccessfulCommit(id, entry_response); 433 BuildEntryResponseForSuccessfulCommit(id, entry_response);
433 return id; 434 return id;
434 } 435 }
435 436
436 void FakeServer::BuildEntryResponseForSuccessfulCommit( 437 void FakeServer::BuildEntryResponseForSuccessfulCommit(
437 const std::string& entity_id, 438 const std::string& entity_id,
438 sync_pb::CommitResponse_EntryResponse* entry_response) { 439 sync_pb::CommitResponse_EntryResponse* entry_response) {
439 EntityMap::const_iterator iter = entities_.find(entity_id); 440 EntityMap::const_iterator iter = entities_.find(entity_id);
440 CHECK(iter != entities_.end()); 441 CHECK(iter != entities_.end());
441 const FakeServerEntity& entity = *iter->second; 442 const FakeServerEntity& entity = *iter->second;
442 entry_response->set_response_type(sync_pb::CommitResponse::SUCCESS); 443 entry_response->set_response_type(sync_pb::CommitResponse::SUCCESS);
443 entry_response->set_id_string(entity.GetId()); 444 entry_response->set_id_string(entity.id());
444 445
445 if (entity.IsDeleted()) { 446 if (entity.IsDeleted()) {
446 entry_response->set_version(entity.GetVersion() + 1); 447 entry_response->set_version(entity.GetVersion() + 1);
447 } else { 448 } else {
448 entry_response->set_version(entity.GetVersion()); 449 entry_response->set_version(entity.GetVersion());
449 entry_response->set_name(entity.GetName()); 450 entry_response->set_name(entity.GetName());
450 } 451 }
451 } 452 }
452 453
453 bool FakeServer::IsChild(const string& id, const string& potential_parent_id) { 454 bool FakeServer::IsChild(const string& id, const string& potential_parent_id) {
454 EntityMap::const_iterator iter = entities_.find(id); 455 EntityMap::const_iterator iter = entities_.find(id);
455 if (iter == entities_.end()) { 456 if (iter == entities_.end()) {
456 // We've hit an ID (probably the imaginary root entity) that isn't stored 457 // We've hit an ID (probably the imaginary root entity) that isn't stored
457 // by the server, so it can't be a child. 458 // by the server, so it can't be a child.
458 return false; 459 return false;
459 } 460 }
460 461
461 const FakeServerEntity& entity = *iter->second; 462 const FakeServerEntity& entity = *iter->second;
462 if (entity.GetParentId() == potential_parent_id) 463 if (entity.GetParentId() == potential_parent_id)
463 return true; 464 return true;
464 465
465 // Recursively look up the tree. 466 // Recursively look up the tree.
466 return IsChild(entity.GetParentId(), potential_parent_id); 467 return IsChild(entity.GetParentId(), potential_parent_id);
467 } 468 }
468 469
469 void FakeServer::DeleteChildren(const string& id) { 470 void FakeServer::DeleteChildren(const string& id) {
470 std::set<string> tombstones_ids; 471 std::vector<std::unique_ptr<FakeServerEntity>> tombstones;
471 // Find all the children of id. 472 // Find all the children of id.
472 for (auto& entity : entities_) { 473 for (const auto& entity : entities_) {
473 if (IsChild(entity.first, id)) { 474 if (IsChild(entity.first, id)) {
474 tombstones_ids.insert(entity.first); 475 tombstones.push_back(TombstoneEntity::Create(
476 entity.first, entity.second->client_defined_unique_tag()));
475 } 477 }
476 } 478 }
477 479
478 for (auto& tombstone_id : tombstones_ids) { 480 for (auto& tombstone : tombstones) {
479 SaveEntity(TombstoneEntity::Create(tombstone_id)); 481 SaveEntity(std::move(tombstone));
480 } 482 }
481 } 483 }
482 484
483 bool FakeServer::HandleCommitRequest(const sync_pb::CommitMessage& commit, 485 bool FakeServer::HandleCommitRequest(const sync_pb::CommitMessage& commit,
484 const std::string& invalidator_client_id, 486 const std::string& invalidator_client_id,
485 sync_pb::CommitResponse* response) { 487 sync_pb::CommitResponse* response) {
486 std::map<string, string> client_to_server_ids; 488 std::map<string, string> client_to_server_ids;
487 string guid = commit.cache_guid(); 489 string guid = commit.cache_guid();
488 ModelTypeSet committed_model_types; 490 ModelTypeSet committed_model_types;
489 491
(...skipping 15 matching lines...) Expand all
505 return false; 507 return false;
506 } 508 }
507 509
508 // Record the ID if it was renamed. 510 // Record the ID if it was renamed.
509 if (entity_id != client_entity.id_string()) { 511 if (entity_id != client_entity.id_string()) {
510 client_to_server_ids[client_entity.id_string()] = entity_id; 512 client_to_server_ids[client_entity.id_string()] = entity_id;
511 } 513 }
512 514
513 EntityMap::const_iterator iter = entities_.find(entity_id); 515 EntityMap::const_iterator iter = entities_.find(entity_id);
514 CHECK(iter != entities_.end()); 516 CHECK(iter != entities_.end());
515 committed_model_types.Put(iter->second->GetModelType()); 517 committed_model_types.Put(iter->second->model_type());
516 } 518 }
517 519
518 FOR_EACH_OBSERVER(Observer, observers_, 520 FOR_EACH_OBSERVER(Observer, observers_,
519 OnCommit(invalidator_client_id, committed_model_types)); 521 OnCommit(invalidator_client_id, committed_model_types));
520 return true; 522 return true;
521 } 523 }
522 524
523 std::unique_ptr<base::DictionaryValue> 525 std::unique_ptr<base::DictionaryValue>
524 FakeServer::GetEntitiesAsDictionaryValue() { 526 FakeServer::GetEntitiesAsDictionaryValue() {
525 DCHECK(thread_checker_.CalledOnValidThread()); 527 DCHECK(thread_checker_.CalledOnValidThread());
526 std::unique_ptr<base::DictionaryValue> dictionary( 528 std::unique_ptr<base::DictionaryValue> dictionary(
527 new base::DictionaryValue()); 529 new base::DictionaryValue());
528 530
529 // Initialize an empty ListValue for all ModelTypes. 531 // Initialize an empty ListValue for all ModelTypes.
530 ModelTypeSet all_types = ModelTypeSet::All(); 532 ModelTypeSet all_types = ModelTypeSet::All();
531 for (ModelTypeSet::Iterator it = all_types.First(); it.Good(); it.Inc()) { 533 for (ModelTypeSet::Iterator it = all_types.First(); it.Good(); it.Inc()) {
532 dictionary->Set(ModelTypeToString(it.Get()), new base::ListValue()); 534 dictionary->Set(ModelTypeToString(it.Get()), new base::ListValue());
533 } 535 }
534 536
535 for (EntityMap::const_iterator it = entities_.begin(); it != entities_.end(); 537 for (EntityMap::const_iterator it = entities_.begin(); it != entities_.end();
536 ++it) { 538 ++it) {
537 const FakeServerEntity& entity = *it->second; 539 const FakeServerEntity& entity = *it->second;
538 if (IsDeletedOrPermanent(entity)) { 540 if (IsDeletedOrPermanent(entity)) {
539 // Tombstones are ignored as they don't represent current data. Folders 541 // Tombstones are ignored as they don't represent current data. Folders
540 // are also ignored as current verification infrastructure does not 542 // are also ignored as current verification infrastructure does not
541 // consider them. 543 // consider them.
542 continue; 544 continue;
543 } 545 }
544 base::ListValue* list_value; 546 base::ListValue* list_value;
545 if (!dictionary->GetList(ModelTypeToString(entity.GetModelType()), 547 if (!dictionary->GetList(ModelTypeToString(entity.model_type()),
546 &list_value)) { 548 &list_value)) {
547 return std::unique_ptr<base::DictionaryValue>(); 549 return std::unique_ptr<base::DictionaryValue>();
548 } 550 }
549 // TODO(pvalenzuela): Store more data for each entity so additional 551 // TODO(pvalenzuela): Store more data for each entity so additional
550 // verification can be performed. One example of additional verification 552 // verification can be performed. One example of additional verification
551 // is checking the correctness of the bookmark hierarchy. 553 // is checking the correctness of the bookmark hierarchy.
552 list_value->AppendString(entity.GetName()); 554 list_value->AppendString(entity.GetName());
553 } 555 }
554 556
555 return dictionary; 557 return dictionary;
556 } 558 }
557 559
558 std::vector<sync_pb::SyncEntity> FakeServer::GetSyncEntitiesByModelType( 560 std::vector<sync_pb::SyncEntity> FakeServer::GetSyncEntitiesByModelType(
559 ModelType model_type) { 561 ModelType model_type) {
560 std::vector<sync_pb::SyncEntity> sync_entities; 562 std::vector<sync_pb::SyncEntity> sync_entities;
561 DCHECK(thread_checker_.CalledOnValidThread()); 563 DCHECK(thread_checker_.CalledOnValidThread());
562 for (EntityMap::const_iterator it = entities_.begin(); it != entities_.end(); 564 for (EntityMap::const_iterator it = entities_.begin(); it != entities_.end();
563 ++it) { 565 ++it) {
564 const FakeServerEntity& entity = *it->second; 566 const FakeServerEntity& entity = *it->second;
565 if (!IsDeletedOrPermanent(entity) && entity.GetModelType() == model_type) { 567 if (!IsDeletedOrPermanent(entity) && entity.model_type() == model_type) {
566 sync_pb::SyncEntity sync_entity; 568 sync_pb::SyncEntity sync_entity;
567 entity.SerializeAsProto(&sync_entity); 569 entity.SerializeAsProto(&sync_entity);
568 sync_entities.push_back(sync_entity); 570 sync_entities.push_back(sync_entity);
569 } 571 }
570 } 572 }
571 return sync_entities; 573 return sync_entities;
572 } 574 }
573 575
574 void FakeServer::InjectEntity(std::unique_ptr<FakeServerEntity> entity) { 576 void FakeServer::InjectEntity(std::unique_ptr<FakeServerEntity> entity) {
575 DCHECK(thread_checker_.CalledOnValidThread()); 577 DCHECK(thread_checker_.CalledOnValidThread());
576 SaveEntity(std::move(entity)); 578 SaveEntity(std::move(entity));
577 } 579 }
578 580
579 bool FakeServer::ModifyEntitySpecifics( 581 bool FakeServer::ModifyEntitySpecifics(
580 const std::string& id, 582 const std::string& id,
581 const sync_pb::EntitySpecifics& updated_specifics) { 583 const sync_pb::EntitySpecifics& updated_specifics) {
582 EntityMap::const_iterator iter = entities_.find(id); 584 EntityMap::const_iterator iter = entities_.find(id);
583 if (iter == entities_.end() || 585 if (iter == entities_.end() ||
584 iter->second->GetModelType() != 586 iter->second->model_type() !=
585 GetModelTypeFromSpecifics(updated_specifics)) { 587 GetModelTypeFromSpecifics(updated_specifics)) {
586 return false; 588 return false;
587 } 589 }
588 590
589 FakeServerEntity* entity = iter->second.get(); 591 FakeServerEntity* entity = iter->second.get();
590 entity->SetSpecifics(updated_specifics); 592 entity->SetSpecifics(updated_specifics);
591 UpdateEntityVersion(entity); 593 UpdateEntityVersion(entity);
592 return true; 594 return true;
593 } 595 }
594 596
595 bool FakeServer::ModifyBookmarkEntity( 597 bool FakeServer::ModifyBookmarkEntity(
596 const std::string& id, 598 const std::string& id,
597 const std::string& parent_id, 599 const std::string& parent_id,
598 const sync_pb::EntitySpecifics& updated_specifics) { 600 const sync_pb::EntitySpecifics& updated_specifics) {
599 EntityMap::const_iterator iter = entities_.find(id); 601 EntityMap::const_iterator iter = entities_.find(id);
600 if (iter == entities_.end() || 602 if (iter == entities_.end() ||
601 iter->second->GetModelType() != syncer::BOOKMARKS || 603 iter->second->model_type() != syncer::BOOKMARKS ||
602 GetModelTypeFromSpecifics(updated_specifics) != syncer::BOOKMARKS) { 604 GetModelTypeFromSpecifics(updated_specifics) != syncer::BOOKMARKS) {
603 return false; 605 return false;
604 } 606 }
605 607
606 BookmarkEntity* entity = static_cast<BookmarkEntity*>(iter->second.get()); 608 BookmarkEntity* entity = static_cast<BookmarkEntity*>(iter->second.get());
607 609
608 entity->SetParentId(parent_id); 610 entity->SetParentId(parent_id);
609 entity->SetSpecifics(updated_specifics); 611 entity->SetSpecifics(updated_specifics);
610 if (updated_specifics.has_bookmark()) { 612 if (updated_specifics.has_bookmark()) {
611 entity->SetName(updated_specifics.bookmark().title()); 613 entity->SetName(updated_specifics.bookmark().title());
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
706 DCHECK(thread_checker_.CalledOnValidThread()); 708 DCHECK(thread_checker_.CalledOnValidThread());
707 network_enabled_ = false; 709 network_enabled_ = false;
708 } 710 }
709 711
710 std::string FakeServer::GetBookmarkBarFolderId() const { 712 std::string FakeServer::GetBookmarkBarFolderId() const {
711 DCHECK(thread_checker_.CalledOnValidThread()); 713 DCHECK(thread_checker_.CalledOnValidThread());
712 for (EntityMap::const_iterator it = entities_.begin(); it != entities_.end(); 714 for (EntityMap::const_iterator it = entities_.begin(); it != entities_.end();
713 ++it) { 715 ++it) {
714 FakeServerEntity* entity = it->second.get(); 716 FakeServerEntity* entity = it->second.get();
715 if (entity->GetName() == kBookmarkBarFolderName && entity->IsFolder() && 717 if (entity->GetName() == kBookmarkBarFolderName && entity->IsFolder() &&
716 entity->GetModelType() == syncer::BOOKMARKS) { 718 entity->model_type() == syncer::BOOKMARKS) {
717 return entity->GetId(); 719 return entity->id();
718 } 720 }
719 } 721 }
720 NOTREACHED() << "Bookmark Bar entity not found."; 722 NOTREACHED() << "Bookmark Bar entity not found.";
721 return ""; 723 return "";
722 } 724 }
723 725
724 base::WeakPtr<FakeServer> FakeServer::AsWeakPtr() { 726 base::WeakPtr<FakeServer> FakeServer::AsWeakPtr() {
725 DCHECK(thread_checker_.CalledOnValidThread()); 727 DCHECK(thread_checker_.CalledOnValidThread());
726 return weak_ptr_factory_.GetWeakPtr(); 728 return weak_ptr_factory_.GetWeakPtr();
727 } 729 }
728 730
729 std::string FakeServer::GetStoreBirthday() const { 731 std::string FakeServer::GetStoreBirthday() const {
730 DCHECK(thread_checker_.CalledOnValidThread()); 732 DCHECK(thread_checker_.CalledOnValidThread());
731 return base::Int64ToString(store_birthday_); 733 return base::Int64ToString(store_birthday_);
732 } 734 }
733 735
734 } // namespace fake_server 736 } // namespace fake_server
OLDNEW
« no previous file with comments | « components/sync/test/fake_server/bookmark_entity.cc ('k') | components/sync/test/fake_server/fake_server_entity.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698