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

Side by Side Diff: sync/test/engine/mock_connection_manager.cc

Issue 9348036: Trim code from sync's ServerConnectionManager (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase after Fred's patch Created 8 years, 9 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 | Annotate | Revision Log
« no previous file with comments | « sync/test/engine/mock_connection_manager.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 // Mock ServerConnectionManager class for use in client regression tests. 5 // Mock ServerConnectionManager class for use in client regression tests.
6 6
7 #include "sync/test/engine/mock_connection_manager.h" 7 #include "sync/test/engine/mock_connection_manager.h"
8 8
9 #include <map> 9 #include <map>
10 10
(...skipping 18 matching lines...) Expand all
29 using sync_pb::CommitResponse; 29 using sync_pb::CommitResponse;
30 using sync_pb::CommitResponse_EntryResponse; 30 using sync_pb::CommitResponse_EntryResponse;
31 using sync_pb::GetUpdatesMessage; 31 using sync_pb::GetUpdatesMessage;
32 using sync_pb::SyncEnums; 32 using sync_pb::SyncEnums;
33 using sync_pb::SyncEntity; 33 using sync_pb::SyncEntity;
34 using syncable::FIRST_REAL_MODEL_TYPE; 34 using syncable::FIRST_REAL_MODEL_TYPE;
35 using syncable::MODEL_TYPE_COUNT; 35 using syncable::MODEL_TYPE_COUNT;
36 using syncable::ModelType; 36 using syncable::ModelType;
37 using syncable::WriteTransaction; 37 using syncable::WriteTransaction;
38 38
39 static char kValidAuthToken[] = "AuthToken";
40
39 MockConnectionManager::MockConnectionManager(syncable::Directory* directory) 41 MockConnectionManager::MockConnectionManager(syncable::Directory* directory)
40 : ServerConnectionManager("unused", 0, false, "version"), 42 : ServerConnectionManager("unused", 0, false, "version"),
43 server_reachable_(true),
41 conflict_all_commits_(false), 44 conflict_all_commits_(false),
42 conflict_n_commits_(0), 45 conflict_n_commits_(0),
43 next_new_id_(10000), 46 next_new_id_(10000),
44 store_birthday_("Store BDay!"), 47 store_birthday_("Store BDay!"),
45 store_birthday_sent_(false), 48 store_birthday_sent_(false),
46 client_stuck_(false), 49 client_stuck_(false),
47 commit_time_rename_prepended_string_(""), 50 commit_time_rename_prepended_string_(""),
48 fail_next_postbuffer_(false), 51 fail_next_postbuffer_(false),
49 directory_(directory), 52 directory_(directory),
50 mid_commit_observer_(NULL), 53 mid_commit_observer_(NULL),
51 throttling_(false), 54 throttling_(false),
52 fail_with_auth_invalid_(false), 55 fail_with_auth_invalid_(false),
53 fail_non_periodic_get_updates_(false), 56 fail_non_periodic_get_updates_(false),
54 client_command_(NULL), 57 client_command_(NULL),
55 next_position_in_parent_(2), 58 next_position_in_parent_(2),
56 use_legacy_bookmarks_protocol_(false), 59 use_legacy_bookmarks_protocol_(false),
57 num_get_updates_requests_(0) { 60 num_get_updates_requests_(0) {
58 server_reachable_ = true;
59 SetNewTimestamp(0); 61 SetNewTimestamp(0);
62 set_auth_token(kValidAuthToken);
60 } 63 }
61 64
62 MockConnectionManager::~MockConnectionManager() { 65 MockConnectionManager::~MockConnectionManager() {
63 EXPECT_TRUE(update_queue_.empty()) << "Unfetched updates."; 66 EXPECT_TRUE(update_queue_.empty()) << "Unfetched updates.";
64 } 67 }
65 68
66 void MockConnectionManager::SetCommitTimeRename(string prepend) { 69 void MockConnectionManager::SetCommitTimeRename(string prepend) {
67 commit_time_rename_prepended_string_ = prepend; 70 commit_time_rename_prepended_string_ = prepend;
68 } 71 }
69 72
(...skipping 20 matching lines...) Expand all
90 93
91 if (directory_) { 94 if (directory_) {
92 // If the Directory's locked when we do this, it's a problem as in normal 95 // If the Directory's locked when we do this, it's a problem as in normal
93 // use this function could take a while to return because it accesses the 96 // use this function could take a while to return because it accesses the
94 // network. As we can't test this we do the next best thing and hang here 97 // network. As we can't test this we do the next best thing and hang here
95 // when there's an issue. 98 // when there's an issue.
96 CHECK(directory_->good()); 99 CHECK(directory_->good());
97 WriteTransaction wt(FROM_HERE, syncable::UNITTEST, directory_); 100 WriteTransaction wt(FROM_HERE, syncable::UNITTEST, directory_);
98 } 101 }
99 102
103 if (auth_token.empty()) {
104 params->response.server_status = HttpResponse::SYNC_AUTH_ERROR;
105 return false;
106 }
107
108 if (auth_token != kValidAuthToken) {
109 // Simulate server-side auth failure.
110 params->response.server_status = HttpResponse::SYNC_AUTH_ERROR;
111 InvalidateAndClearAuthToken();
112 }
113
100 if (fail_next_postbuffer_) { 114 if (fail_next_postbuffer_) {
101 fail_next_postbuffer_ = false; 115 fail_next_postbuffer_ = false;
102 return false; 116 return false;
103 } 117 }
104 118
105 if (!server_reachable_) { 119 if (!server_reachable_) {
106 params->response.server_status = HttpResponse::CONNECTION_UNAVAILABLE; 120 params->response.server_status = HttpResponse::CONNECTION_UNAVAILABLE;
107 return false; 121 return false;
108 } 122 }
109 123
(...skipping 12 matching lines...) Expand all
122 } 136 }
123 bool result = true; 137 bool result = true;
124 EXPECT_TRUE(!store_birthday_sent_ || post.has_store_birthday() || 138 EXPECT_TRUE(!store_birthday_sent_ || post.has_store_birthday() ||
125 post.message_contents() == ClientToServerMessage::AUTHENTICATE); 139 post.message_contents() == ClientToServerMessage::AUTHENTICATE);
126 store_birthday_sent_ = true; 140 store_birthday_sent_ = true;
127 141
128 if (post.message_contents() == ClientToServerMessage::COMMIT) { 142 if (post.message_contents() == ClientToServerMessage::COMMIT) {
129 ProcessCommit(&post, &response); 143 ProcessCommit(&post, &response);
130 } else if (post.message_contents() == ClientToServerMessage::GET_UPDATES) { 144 } else if (post.message_contents() == ClientToServerMessage::GET_UPDATES) {
131 ProcessGetUpdates(&post, &response); 145 ProcessGetUpdates(&post, &response);
132 } else if (post.message_contents() == ClientToServerMessage::AUTHENTICATE) {
133 ProcessAuthenticate(&post, &response, auth_token);
134 } else if (post.message_contents() == ClientToServerMessage::CLEAR_DATA) { 146 } else if (post.message_contents() == ClientToServerMessage::CLEAR_DATA) {
135 ProcessClearData(&post, &response); 147 ProcessClearData(&post, &response);
136 } else { 148 } else {
137 EXPECT_TRUE(false) << "Unknown/unsupported ClientToServerMessage"; 149 EXPECT_TRUE(false) << "Unknown/unsupported ClientToServerMessage";
138 return false; 150 return false;
139 } 151 }
140 if (client_command_.get()) { 152 if (client_command_.get()) {
141 response.mutable_client_command()->CopyFrom(*client_command_.get()); 153 response.mutable_client_command()->CopyFrom(*client_command_.get());
142 } 154 }
143 155
(...skipping 13 matching lines...) Expand all
157 !mid_commit_callback_.is_null()) { 169 !mid_commit_callback_.is_null()) {
158 mid_commit_callback_.Run(); 170 mid_commit_callback_.Run();
159 } 171 }
160 if (mid_commit_observer_) { 172 if (mid_commit_observer_) {
161 mid_commit_observer_->Observe(); 173 mid_commit_observer_->Observe();
162 } 174 }
163 175
164 return result; 176 return result;
165 } 177 }
166 178
167 bool MockConnectionManager::IsServerReachable() {
168 return true;
169 }
170
171 bool MockConnectionManager::IsUserAuthenticated() {
172 return true;
173 }
174
175 sync_pb::GetUpdatesResponse* MockConnectionManager::GetUpdateResponse() { 179 sync_pb::GetUpdatesResponse* MockConnectionManager::GetUpdateResponse() {
176 if (update_queue_.empty()) { 180 if (update_queue_.empty()) {
177 NextUpdateBatch(); 181 NextUpdateBatch();
178 } 182 }
179 return &update_queue_.back(); 183 return &update_queue_.back();
180 } 184 }
181 185
182 void MockConnectionManager::AddDefaultBookmarkData(sync_pb::SyncEntity* entity, 186 void MockConnectionManager::AddDefaultBookmarkData(sync_pb::SyncEntity* entity,
183 bool is_folder) { 187 bool is_folder) {
184 if (use_legacy_bookmarks_protocol_) { 188 if (use_legacy_bookmarks_protocol_) {
(...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after
453 } 457 }
454 458
455 void MockConnectionManager::ProcessClearData(ClientToServerMessage* csm, 459 void MockConnectionManager::ProcessClearData(ClientToServerMessage* csm,
456 ClientToServerResponse* response) { 460 ClientToServerResponse* response) {
457 CHECK(csm->has_clear_user_data()); 461 CHECK(csm->has_clear_user_data());
458 ASSERT_EQ(csm->message_contents(), ClientToServerMessage::CLEAR_DATA); 462 ASSERT_EQ(csm->message_contents(), ClientToServerMessage::CLEAR_DATA);
459 response->clear_user_data(); 463 response->clear_user_data();
460 response->set_error_code(clear_user_data_response_errortype_); 464 response->set_error_code(clear_user_data_response_errortype_);
461 } 465 }
462 466
463 void MockConnectionManager::ProcessAuthenticate(
464 ClientToServerMessage* csm,
465 ClientToServerResponse* response,
466 const std::string& auth_token) {
467 ASSERT_EQ(csm->message_contents(), ClientToServerMessage::AUTHENTICATE);
468 EXPECT_FALSE(auth_token.empty());
469
470 if (auth_token != valid_auth_token_) {
471 response->set_error_code(SyncEnums::AUTH_INVALID);
472 return;
473 }
474
475 response->set_error_code(SyncEnums::SUCCESS);
476 response->mutable_authenticate()->CopyFrom(auth_response_);
477 auth_response_.Clear();
478 }
479
480 void MockConnectionManager::SetAuthenticationResponseInfo(
481 const std::string& valid_auth_token,
482 const std::string& user_display_name,
483 const std::string& user_display_email,
484 const std::string& user_obfuscated_id) {
485 valid_auth_token_ = valid_auth_token;
486 sync_pb::UserIdentification* user = auth_response_.mutable_user();
487 user->set_display_name(user_display_name);
488 user->set_email(user_display_email);
489 user->set_obfuscated_id(user_obfuscated_id);
490 }
491
492 bool MockConnectionManager::ShouldConflictThisCommit() { 467 bool MockConnectionManager::ShouldConflictThisCommit() {
493 bool conflict = false; 468 bool conflict = false;
494 if (conflict_all_commits_) { 469 if (conflict_all_commits_) {
495 conflict = true; 470 conflict = true;
496 } else if (conflict_n_commits_ > 0) { 471 } else if (conflict_n_commits_ > 0) {
497 conflict = true; 472 conflict = true;
498 --conflict_n_commits_; 473 --conflict_n_commits_;
499 } 474 }
500 return conflict; 475 return conflict;
501 } 476 }
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after
634 int data_type_id = syncable::GetSpecificsFieldNumberFromModelType(value); 609 int data_type_id = syncable::GetSpecificsFieldNumberFromModelType(value);
635 for (int i = 0; i < filter.size(); ++i) { 610 for (int i = 0; i < filter.size(); ++i) {
636 if (filter.Get(i).data_type_id() == data_type_id) { 611 if (filter.Get(i).data_type_id() == data_type_id) {
637 return &(filter.Get(i)); 612 return &(filter.Get(i));
638 } 613 }
639 } 614 }
640 return NULL; 615 return NULL;
641 } 616 }
642 617
643 void MockConnectionManager::SetServerReachable() { 618 void MockConnectionManager::SetServerReachable() {
644 server_status_ = HttpResponse::SERVER_CONNECTION_OK;
645 server_reachable_ = true; 619 server_reachable_ = true;
646
647 FOR_EACH_OBSERVER(ServerConnectionEventListener, listeners_,
648 OnServerConnectionEvent(
649 ServerConnectionEvent(server_status_, server_reachable_)));
650 } 620 }
651 621
652 void MockConnectionManager::SetServerNotReachable() { 622 void MockConnectionManager::SetServerNotReachable() {
653 server_status_ = HttpResponse::CONNECTION_UNAVAILABLE;
654 server_reachable_ = false; 623 server_reachable_ = false;
624 }
655 625
656 FOR_EACH_OBSERVER(ServerConnectionEventListener, listeners_, 626 void MockConnectionManager::UpdateConnectionStatus() {
657 OnServerConnectionEvent( 627 if (!server_reachable_) {
658 ServerConnectionEvent(server_status_, server_reachable_))); 628 server_status_ = HttpResponse::CONNECTION_UNAVAILABLE;
629 } else {
630 server_status_ = HttpResponse::SERVER_CONNECTION_OK;
631 }
659 } 632 }
OLDNEW
« no previous file with comments | « sync/test/engine/mock_connection_manager.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698