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

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

Issue 2130453004: [Sync] Move //sync to //components/sync. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase. Created 4 years, 4 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 "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>
11 #include <memory> 11 #include <memory>
12 #include <set> 12 #include <set>
13 #include <string> 13 #include <string>
14 #include <utility> 14 #include <utility>
15 #include <vector> 15 #include <vector>
16 16
17 #include "base/guid.h" 17 #include "base/guid.h"
18 #include "base/logging.h" 18 #include "base/logging.h"
19 #include "base/stl_util.h" 19 #include "base/stl_util.h"
20 #include "base/strings/string_number_conversions.h" 20 #include "base/strings/string_number_conversions.h"
21 #include "base/strings/string_util.h" 21 #include "base/strings/string_util.h"
22 #include "base/strings/stringprintf.h" 22 #include "base/strings/stringprintf.h"
23 #include "base/synchronization/lock.h" 23 #include "base/synchronization/lock.h"
24 #include "components/sync/base/model_type.h"
25 #include "components/sync/protocol/sync.pb.h"
26 #include "components/sync/test/fake_server/bookmark_entity.h"
27 #include "components/sync/test/fake_server/permanent_entity.h"
28 #include "components/sync/test/fake_server/tombstone_entity.h"
29 #include "components/sync/test/fake_server/unique_client_entity.h"
24 #include "net/base/net_errors.h" 30 #include "net/base/net_errors.h"
25 #include "net/http/http_status_code.h" 31 #include "net/http/http_status_code.h"
26 #include "sync/internal_api/public/base/model_type.h"
27 #include "sync/protocol/sync.pb.h"
28 #include "sync/test/fake_server/bookmark_entity.h"
29 #include "sync/test/fake_server/permanent_entity.h"
30 #include "sync/test/fake_server/tombstone_entity.h"
31 #include "sync/test/fake_server/unique_client_entity.h"
32 32
33 using std::string; 33 using std::string;
34 using std::vector; 34 using std::vector;
35 35
36 using syncer::GetModelType; 36 using syncer::GetModelType;
37 using syncer::GetModelTypeFromSpecifics; 37 using syncer::GetModelTypeFromSpecifics;
38 using syncer::ModelType; 38 using syncer::ModelType;
39 using syncer::ModelTypeSet; 39 using syncer::ModelTypeSet;
40 40
41 namespace fake_server { 41 namespace fake_server {
42 42
43 class FakeServerEntity; 43 class FakeServerEntity;
44 44
45 namespace { 45 namespace {
46 46
47 // The default keystore key. 47 // The default keystore key.
48 static const char kDefaultKeystoreKey[] = "1111111111111111"; 48 static const char kDefaultKeystoreKey[] = "1111111111111111";
49 49
50 // Properties of the bookmark bar permanent folder. 50 // Properties of the bookmark bar permanent folder.
51 static const char kBookmarkBarFolderServerTag[] = "bookmark_bar"; 51 static const char kBookmarkBarFolderServerTag[] = "bookmark_bar";
52 static const char kBookmarkBarFolderName[] = "Bookmark Bar"; 52 static const char kBookmarkBarFolderName[] = "Bookmark Bar";
53 53
54 // Properties of the other bookmarks permanent folder. 54 // Properties of the other bookmarks permanent folder.
55 static const char kOtherBookmarksFolderServerTag[] = "other_bookmarks"; 55 static const char kOtherBookmarksFolderServerTag[] = "other_bookmarks";
56 static const char kOtherBookmarksFolderName[] = "Other Bookmarks"; 56 static const char kOtherBookmarksFolderName[] = "Other Bookmarks";
57 57
58 // Properties of the synced bookmarks permanent folder. 58 // Properties of the synced bookmarks permanent folder.
59 static const char kSyncedBookmarksFolderServerTag[] = "synced_bookmarks"; 59 static const char kSyncedBookmarksFolderServerTag[] = "synced_bookmarks";
60 static const char kSyncedBookmarksFolderName[] = "Synced Bookmarks"; 60 static const char kSyncedBookmarksFolderName[] = "Synced Bookmarks";
61 61
62 // A filter used during GetUpdates calls to determine what information to 62 // A filter used during GetUpdates calls to determine what information to
63 // send back to the client. There is a 1:1 correspondence between any given 63 // send back to the client. There is a 1:1 correspondence between any given
64 // GetUpdates call and an UpdateSieve instance. 64 // GetUpdates call and an UpdateSieve instance.
65 class UpdateSieve { 65 class UpdateSieve {
66 public: 66 public:
67 ~UpdateSieve() { } 67 ~UpdateSieve() {}
68 68
69 // Factory method for creating an UpdateSieve. 69 // Factory method for creating an UpdateSieve.
70 static std::unique_ptr<UpdateSieve> Create( 70 static std::unique_ptr<UpdateSieve> Create(
71 const sync_pb::GetUpdatesMessage& get_updates_message); 71 const sync_pb::GetUpdatesMessage& get_updates_message);
72 72
73 // Sets the progress markers in |get_updates_response| given the progress 73 // Sets the progress markers in |get_updates_response| given the progress
74 // markers from the original GetUpdatesMessage and |new_version| (the latest 74 // markers from the original GetUpdatesMessage and |new_version| (the latest
75 // version in the entries sent back). 75 // version in the entries sent back).
76 void UpdateProgressMarkers( 76 void UpdateProgressMarkers(
77 int64_t new_version, 77 int64_t new_version,
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
136 get_updates_message.from_progress_marker(i); 136 get_updates_message.from_progress_marker(i);
137 137
138 int64_t version = 0; 138 int64_t version = 0;
139 // Let the version remain zero if there is no token or an empty token (the 139 // Let the version remain zero if there is no token or an empty token (the
140 // first request for this type). 140 // first request for this type).
141 if (marker.has_token() && !marker.token().empty()) { 141 if (marker.has_token() && !marker.token().empty()) {
142 bool parsed = base::StringToInt64(marker.token(), &version); 142 bool parsed = base::StringToInt64(marker.token(), &version);
143 CHECK(parsed) << "Unable to parse progress marker token."; 143 CHECK(parsed) << "Unable to parse progress marker token.";
144 } 144 }
145 145
146 ModelType model_type = syncer::GetModelTypeFromSpecificsFieldNumber( 146 ModelType model_type =
147 marker.data_type_id()); 147 syncer::GetModelTypeFromSpecificsFieldNumber(marker.data_type_id());
148 request_from_version[model_type] = version; 148 request_from_version[model_type] = version;
149 149
150 if (version < min_version) 150 if (version < min_version)
151 min_version = version; 151 min_version = version;
152 } 152 }
153 153
154 return std::unique_ptr<UpdateSieve>( 154 return std::unique_ptr<UpdateSieve>(
155 new UpdateSieve(request_from_version, min_version)); 155 new UpdateSieve(request_from_version, min_version));
156 } 156 }
157 157
158 // Returns whether |entity| is deleted or permanent. 158 // Returns whether |entity| is deleted or permanent.
159 bool IsDeletedOrPermanent(const FakeServerEntity& entity) { 159 bool IsDeletedOrPermanent(const FakeServerEntity& entity) {
160 return entity.IsDeleted() || entity.IsPermanent(); 160 return entity.IsDeleted() || entity.IsPermanent();
161 } 161 }
162 162
163 } // namespace 163 } // namespace
164 164
165 FakeServer::FakeServer() : version_(0), 165 FakeServer::FakeServer()
166 store_birthday_(0), 166 : version_(0),
167 authenticated_(true), 167 store_birthday_(0),
168 error_type_(sync_pb::SyncEnums::SUCCESS), 168 authenticated_(true),
169 alternate_triggered_errors_(false), 169 error_type_(sync_pb::SyncEnums::SUCCESS),
170 request_counter_(0), 170 alternate_triggered_errors_(false),
171 network_enabled_(true), 171 request_counter_(0),
172 weak_ptr_factory_(this) { 172 network_enabled_(true),
173 weak_ptr_factory_(this) {
173 Init(); 174 Init();
174 } 175 }
175 176
176 FakeServer::~FakeServer() {} 177 FakeServer::~FakeServer() {}
177 178
178 void FakeServer::Init() { 179 void FakeServer::Init() {
179 keystore_keys_.push_back(kDefaultKeystoreKey); 180 keystore_keys_.push_back(kDefaultKeystoreKey);
180 181
181 const bool create_result = CreateDefaultPermanentItems(); 182 const bool create_result = CreateDefaultPermanentItems();
182 DCHECK(create_result) << "Permanent items were not created successfully."; 183 DCHECK(create_result) << "Permanent items were not created successfully.";
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
314 } 315 }
315 316
316 response_proto.set_store_birthday(GetStoreBirthday()); 317 response_proto.set_store_birthday(GetStoreBirthday());
317 318
318 *error_code = 0; 319 *error_code = 0;
319 *response_code = net::HTTP_OK; 320 *response_code = net::HTTP_OK;
320 *response = response_proto.SerializeAsString(); 321 *response = response_proto.SerializeAsString();
321 completion_closure.Run(); 322 completion_closure.Run();
322 } 323 }
323 324
324 bool FakeServer::GetLastCommitMessage( 325 bool FakeServer::GetLastCommitMessage(sync_pb::ClientToServerMessage* message) {
325 sync_pb::ClientToServerMessage* message) {
326 if (!last_commit_message_.has_commit()) 326 if (!last_commit_message_.has_commit())
327 return false; 327 return false;
328 328
329 message->CopyFrom(last_commit_message_); 329 message->CopyFrom(last_commit_message_);
330 return true; 330 return true;
331 } 331 }
332 332
333 bool FakeServer::GetLastGetUpdatesMessage( 333 bool FakeServer::GetLastGetUpdatesMessage(
334 sync_pb::ClientToServerMessage* message) { 334 sync_pb::ClientToServerMessage* message) {
335 if (!last_getupdates_message_.has_get_updates()) 335 if (!last_getupdates_message_.has_get_updates())
(...skipping 22 matching lines...) Expand all
358 358
359 bool send_encryption_keys_based_on_nigori = false; 359 bool send_encryption_keys_based_on_nigori = false;
360 int64_t max_response_version = 0; 360 int64_t max_response_version = 0;
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 = std::max(max_response_version, 368 max_response_version =
369 response_entity->version()); 369 std::max(max_response_version, response_entity->version());
370 370
371 if (entity.GetModelType() == syncer::NIGORI) { 371 if (entity.GetModelType() == 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();
382 it != keystore_keys_.end(); ++it) { 382 it != keystore_keys_.end(); ++it) {
383 response->add_encryption_keys(*it); 383 response->add_encryption_keys(*it);
384 } 384 }
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
488 ModelTypeSet committed_model_types; 488 ModelTypeSet committed_model_types;
489 489
490 // TODO(pvalenzuela): Add validation of CommitMessage.entries. 490 // TODO(pvalenzuela): Add validation of CommitMessage.entries.
491 ::google::protobuf::RepeatedPtrField<sync_pb::SyncEntity>::const_iterator it; 491 ::google::protobuf::RepeatedPtrField<sync_pb::SyncEntity>::const_iterator it;
492 for (it = commit.entries().begin(); it != commit.entries().end(); ++it) { 492 for (it = commit.entries().begin(); it != commit.entries().end(); ++it) {
493 sync_pb::CommitResponse_EntryResponse* entry_response = 493 sync_pb::CommitResponse_EntryResponse* entry_response =
494 response->add_entryresponse(); 494 response->add_entryresponse();
495 495
496 sync_pb::SyncEntity client_entity = *it; 496 sync_pb::SyncEntity client_entity = *it;
497 string parent_id = client_entity.parent_id_string(); 497 string parent_id = client_entity.parent_id_string();
498 if (client_to_server_ids.find(parent_id) != 498 if (client_to_server_ids.find(parent_id) != client_to_server_ids.end()) {
499 client_to_server_ids.end()) {
500 parent_id = client_to_server_ids[parent_id]; 499 parent_id = client_to_server_ids[parent_id];
501 } 500 }
502 501
503 const string entity_id = 502 const string entity_id =
504 CommitEntity(client_entity, entry_response, guid, parent_id); 503 CommitEntity(client_entity, entry_response, guid, parent_id);
505 if (entity_id.empty()) { 504 if (entity_id.empty()) {
506 return false; 505 return false;
507 } 506 }
508 507
509 // Record the ID if it was renamed. 508 // Record the ID if it was renamed.
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after
706 void FakeServer::DisableNetwork() { 705 void FakeServer::DisableNetwork() {
707 DCHECK(thread_checker_.CalledOnValidThread()); 706 DCHECK(thread_checker_.CalledOnValidThread());
708 network_enabled_ = false; 707 network_enabled_ = false;
709 } 708 }
710 709
711 std::string FakeServer::GetBookmarkBarFolderId() const { 710 std::string FakeServer::GetBookmarkBarFolderId() const {
712 DCHECK(thread_checker_.CalledOnValidThread()); 711 DCHECK(thread_checker_.CalledOnValidThread());
713 for (EntityMap::const_iterator it = entities_.begin(); it != entities_.end(); 712 for (EntityMap::const_iterator it = entities_.begin(); it != entities_.end();
714 ++it) { 713 ++it) {
715 FakeServerEntity* entity = it->second.get(); 714 FakeServerEntity* entity = it->second.get();
716 if (entity->GetName() == kBookmarkBarFolderName && 715 if (entity->GetName() == kBookmarkBarFolderName && entity->IsFolder() &&
717 entity->IsFolder() &&
718 entity->GetModelType() == syncer::BOOKMARKS) { 716 entity->GetModelType() == syncer::BOOKMARKS) {
719 return entity->GetId(); 717 return entity->GetId();
720 } 718 }
721 } 719 }
722 NOTREACHED() << "Bookmark Bar entity not found."; 720 NOTREACHED() << "Bookmark Bar entity not found.";
723 return ""; 721 return "";
724 } 722 }
725 723
726 base::WeakPtr<FakeServer> FakeServer::AsWeakPtr() { 724 base::WeakPtr<FakeServer> FakeServer::AsWeakPtr() {
727 DCHECK(thread_checker_.CalledOnValidThread()); 725 DCHECK(thread_checker_.CalledOnValidThread());
728 return weak_ptr_factory_.GetWeakPtr(); 726 return weak_ptr_factory_.GetWeakPtr();
729 } 727 }
730 728
731 std::string FakeServer::GetStoreBirthday() const { 729 std::string FakeServer::GetStoreBirthday() const {
732 DCHECK(thread_checker_.CalledOnValidThread()); 730 DCHECK(thread_checker_.CalledOnValidThread());
733 return base::Int64ToString(store_birthday_); 731 return base::Int64ToString(store_birthday_);
734 } 732 }
735 733
736 } // namespace fake_server 734 } // namespace fake_server
OLDNEW
« no previous file with comments | « components/sync/test/fake_server/fake_server.h ('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