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

Side by Side Diff: chrome/browser/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: small updates Created 8 years, 10 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
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 "chrome/browser/sync/test/engine/mock_connection_manager.h" 7 #include "chrome/browser/sync/test/engine/mock_connection_manager.h"
8 8
9 #include <map> 9 #include <map>
10 10
(...skipping 20 matching lines...) Expand all
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::DirectoryManager; 34 using syncable::DirectoryManager;
35 using syncable::FIRST_REAL_MODEL_TYPE; 35 using syncable::FIRST_REAL_MODEL_TYPE;
36 using syncable::MODEL_TYPE_COUNT; 36 using syncable::MODEL_TYPE_COUNT;
37 using syncable::ModelType; 37 using syncable::ModelType;
38 using syncable::ScopedDirLookup; 38 using syncable::ScopedDirLookup;
39 using syncable::WriteTransaction; 39 using syncable::WriteTransaction;
40 40
41 static char kValidAuthToken[] = "AuthToken";
42
41 MockConnectionManager::MockConnectionManager(DirectoryManager* dirmgr, 43 MockConnectionManager::MockConnectionManager(DirectoryManager* dirmgr,
42 const string& name) 44 const string& name)
43 : ServerConnectionManager("unused", 0, false, "version"), 45 : ServerConnectionManager("unused", 0, false, "version"),
46 server_reachable_(true),
44 conflict_all_commits_(false), 47 conflict_all_commits_(false),
45 conflict_n_commits_(0), 48 conflict_n_commits_(0),
46 next_new_id_(10000), 49 next_new_id_(10000),
47 store_birthday_("Store BDay!"), 50 store_birthday_("Store BDay!"),
48 store_birthday_sent_(false), 51 store_birthday_sent_(false),
49 client_stuck_(false), 52 client_stuck_(false),
50 commit_time_rename_prepended_string_(""), 53 commit_time_rename_prepended_string_(""),
51 fail_next_postbuffer_(false), 54 fail_next_postbuffer_(false),
52 directory_manager_(dirmgr), 55 directory_manager_(dirmgr),
53 directory_name_(name), 56 directory_name_(name),
54 mid_commit_observer_(NULL), 57 mid_commit_observer_(NULL),
55 throttling_(false), 58 throttling_(false),
56 fail_with_auth_invalid_(false), 59 fail_with_auth_invalid_(false),
57 fail_non_periodic_get_updates_(false), 60 fail_non_periodic_get_updates_(false),
58 client_command_(NULL), 61 client_command_(NULL),
59 next_position_in_parent_(2), 62 next_position_in_parent_(2),
60 use_legacy_bookmarks_protocol_(false), 63 use_legacy_bookmarks_protocol_(false),
61 num_get_updates_requests_(0) { 64 num_get_updates_requests_(0) {
62 server_reachable_ = true;
63 SetNewTimestamp(0); 65 SetNewTimestamp(0);
66 set_auth_token(kValidAuthToken);
64 }; 67 };
65 68
66 MockConnectionManager::~MockConnectionManager() { 69 MockConnectionManager::~MockConnectionManager() {
67 EXPECT_TRUE(update_queue_.empty()) << "Unfetched updates."; 70 EXPECT_TRUE(update_queue_.empty()) << "Unfetched updates.";
68 } 71 }
69 72
70 void MockConnectionManager::SetCommitTimeRename(string prepend) { 73 void MockConnectionManager::SetCommitTimeRename(string prepend) {
71 commit_time_rename_prepended_string_ = prepend; 74 commit_time_rename_prepended_string_ = prepend;
72 } 75 }
73 76
(...skipping 12 matching lines...) Expand all
86 const string& auth_token, 89 const string& auth_token,
87 browser_sync::ScopedServerStatusWatcher* watcher) { 90 browser_sync::ScopedServerStatusWatcher* watcher) {
88 ClientToServerMessage post; 91 ClientToServerMessage post;
89 CHECK(post.ParseFromString(params->buffer_in)); 92 CHECK(post.ParseFromString(params->buffer_in));
90 last_request_.CopyFrom(post); 93 last_request_.CopyFrom(post);
91 client_stuck_ = post.sync_problem_detected(); 94 client_stuck_ = post.sync_problem_detected();
92 ClientToServerResponse response; 95 ClientToServerResponse response;
93 response.Clear(); 96 response.Clear();
94 97
95 ScopedDirLookup directory(directory_manager_, directory_name_); 98 ScopedDirLookup directory(directory_manager_, directory_name_);
96 // For any non-AUTHENTICATE requests, a valid directory should be set up. 99
97 // If the Directory's locked when we do this, it's a problem as in normal 100 // A valid directory should be set up. If the Directory's locked when we do
98 // use this function could take a while to return because it accesses the 101 // this, it's a problem as in normal use this function could take a while to
99 // network. As we can't test this we do the next best thing and hang here 102 // return because it accesses the network. As we can't test this we do the
100 // when there's an issue. 103 // next best thing and hang here when there's an issue.
101 if (post.message_contents() != ClientToServerMessage::AUTHENTICATE) { 104 if (post.message_contents() != ClientToServerMessage::AUTHENTICATE) {
102 CHECK(directory.good()); 105 CHECK(directory.good());
103 WriteTransaction wt(FROM_HERE, syncable::UNITTEST, directory); 106 WriteTransaction wt(FROM_HERE, syncable::UNITTEST, directory);
104 } 107 }
105 108
109 if (auth_token.empty()) {
110 params->response.server_status = HttpResponse::SYNC_AUTH_ERROR;
111 return false;
112 }
113
114 if (auth_token != kValidAuthToken) {
115 // Simulate server-side auth failure.
116 params->response.server_status = HttpResponse::SYNC_AUTH_ERROR;
117 InvalidateAndClearAuthToken();
118 }
119
106 if (fail_next_postbuffer_) { 120 if (fail_next_postbuffer_) {
107 fail_next_postbuffer_ = false; 121 fail_next_postbuffer_ = false;
108 return false; 122 return false;
109 } 123 }
110 124
111 if (!server_reachable_) { 125 if (!server_reachable_) {
112 params->response.server_status = HttpResponse::CONNECTION_UNAVAILABLE; 126 params->response.server_status = HttpResponse::CONNECTION_UNAVAILABLE;
113 return false; 127 return false;
114 } 128 }
115 129
(...skipping 12 matching lines...) Expand all
128 } 142 }
129 bool result = true; 143 bool result = true;
130 EXPECT_TRUE(!store_birthday_sent_ || post.has_store_birthday() || 144 EXPECT_TRUE(!store_birthday_sent_ || post.has_store_birthday() ||
131 post.message_contents() == ClientToServerMessage::AUTHENTICATE); 145 post.message_contents() == ClientToServerMessage::AUTHENTICATE);
132 store_birthday_sent_ = true; 146 store_birthday_sent_ = true;
133 147
134 if (post.message_contents() == ClientToServerMessage::COMMIT) { 148 if (post.message_contents() == ClientToServerMessage::COMMIT) {
135 ProcessCommit(&post, &response); 149 ProcessCommit(&post, &response);
136 } else if (post.message_contents() == ClientToServerMessage::GET_UPDATES) { 150 } else if (post.message_contents() == ClientToServerMessage::GET_UPDATES) {
137 ProcessGetUpdates(&post, &response); 151 ProcessGetUpdates(&post, &response);
138 } else if (post.message_contents() == ClientToServerMessage::AUTHENTICATE) {
139 ProcessAuthenticate(&post, &response, auth_token);
140 } else if (post.message_contents() == ClientToServerMessage::CLEAR_DATA) { 152 } else if (post.message_contents() == ClientToServerMessage::CLEAR_DATA) {
141 ProcessClearData(&post, &response); 153 ProcessClearData(&post, &response);
142 } else { 154 } else {
143 EXPECT_TRUE(false) << "Unknown/unsupported ClientToServerMessage"; 155 EXPECT_TRUE(false) << "Unknown/unsupported ClientToServerMessage";
144 return false; 156 return false;
145 } 157 }
146 if (client_command_.get()) { 158 if (client_command_.get()) {
147 response.mutable_client_command()->CopyFrom(*client_command_.get()); 159 response.mutable_client_command()->CopyFrom(*client_command_.get());
148 } 160 }
149 161
(...skipping 13 matching lines...) Expand all
163 !mid_commit_callback_.is_null()) { 175 !mid_commit_callback_.is_null()) {
164 mid_commit_callback_.Run(); 176 mid_commit_callback_.Run();
165 } 177 }
166 if (mid_commit_observer_) { 178 if (mid_commit_observer_) {
167 mid_commit_observer_->Observe(); 179 mid_commit_observer_->Observe();
168 } 180 }
169 181
170 return result; 182 return result;
171 } 183 }
172 184
173 bool MockConnectionManager::IsServerReachable() {
174 return true;
175 }
176
177 bool MockConnectionManager::IsUserAuthenticated() {
178 return true;
179 }
180
181 sync_pb::GetUpdatesResponse* MockConnectionManager::GetUpdateResponse() { 185 sync_pb::GetUpdatesResponse* MockConnectionManager::GetUpdateResponse() {
182 if (update_queue_.empty()) { 186 if (update_queue_.empty()) {
183 NextUpdateBatch(); 187 NextUpdateBatch();
184 } 188 }
185 return &update_queue_.back(); 189 return &update_queue_.back();
186 } 190 }
187 191
188 void MockConnectionManager::AddDefaultBookmarkData(sync_pb::SyncEntity* entity, 192 void MockConnectionManager::AddDefaultBookmarkData(sync_pb::SyncEntity* entity,
189 bool is_folder) { 193 bool is_folder) {
190 if (use_legacy_bookmarks_protocol_) { 194 if (use_legacy_bookmarks_protocol_) {
(...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after
459 } 463 }
460 464
461 void MockConnectionManager::ProcessClearData(ClientToServerMessage* csm, 465 void MockConnectionManager::ProcessClearData(ClientToServerMessage* csm,
462 ClientToServerResponse* response) { 466 ClientToServerResponse* response) {
463 CHECK(csm->has_clear_user_data()); 467 CHECK(csm->has_clear_user_data());
464 ASSERT_EQ(csm->message_contents(), ClientToServerMessage::CLEAR_DATA); 468 ASSERT_EQ(csm->message_contents(), ClientToServerMessage::CLEAR_DATA);
465 response->clear_user_data(); 469 response->clear_user_data();
466 response->set_error_code(clear_user_data_response_errortype_); 470 response->set_error_code(clear_user_data_response_errortype_);
467 } 471 }
468 472
469 void MockConnectionManager::ProcessAuthenticate(
470 ClientToServerMessage* csm,
471 ClientToServerResponse* response,
472 const std::string& auth_token) {
473 ASSERT_EQ(csm->message_contents(), ClientToServerMessage::AUTHENTICATE);
474 EXPECT_FALSE(auth_token.empty());
475
476 if (auth_token != valid_auth_token_) {
477 response->set_error_code(SyncEnums::AUTH_INVALID);
478 return;
479 }
480
481 response->set_error_code(SyncEnums::SUCCESS);
482 response->mutable_authenticate()->CopyFrom(auth_response_);
483 auth_response_.Clear();
484 }
485
486 void MockConnectionManager::SetAuthenticationResponseInfo(
487 const std::string& valid_auth_token,
488 const std::string& user_display_name,
489 const std::string& user_display_email,
490 const std::string& user_obfuscated_id) {
491 valid_auth_token_ = valid_auth_token;
492 sync_pb::UserIdentification* user = auth_response_.mutable_user();
493 user->set_display_name(user_display_name);
494 user->set_email(user_display_email);
495 user->set_obfuscated_id(user_obfuscated_id);
496 }
497
498 bool MockConnectionManager::ShouldConflictThisCommit() { 473 bool MockConnectionManager::ShouldConflictThisCommit() {
499 bool conflict = false; 474 bool conflict = false;
500 if (conflict_all_commits_) { 475 if (conflict_all_commits_) {
501 conflict = true; 476 conflict = true;
502 } else if (conflict_n_commits_ > 0) { 477 } else if (conflict_n_commits_ > 0) {
503 conflict = true; 478 conflict = true;
504 --conflict_n_commits_; 479 --conflict_n_commits_;
505 } 480 }
506 return conflict; 481 return conflict;
507 } 482 }
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after
640 int data_type_id = syncable::GetExtensionFieldNumberFromModelType(value); 615 int data_type_id = syncable::GetExtensionFieldNumberFromModelType(value);
641 for (int i = 0; i < filter.size(); ++i) { 616 for (int i = 0; i < filter.size(); ++i) {
642 if (filter.Get(i).data_type_id() == data_type_id) { 617 if (filter.Get(i).data_type_id() == data_type_id) {
643 return &(filter.Get(i)); 618 return &(filter.Get(i));
644 } 619 }
645 } 620 }
646 return NULL; 621 return NULL;
647 } 622 }
648 623
649 void MockConnectionManager::SetServerReachable() { 624 void MockConnectionManager::SetServerReachable() {
650 server_status_ = HttpResponse::SERVER_CONNECTION_OK;
651 server_reachable_ = true; 625 server_reachable_ = true;
652
653 FOR_EACH_OBSERVER(ServerConnectionEventListener, listeners_,
654 OnServerConnectionEvent(
655 ServerConnectionEvent(server_status_, server_reachable_)));
656 } 626 }
657 627
658 void MockConnectionManager::SetServerNotReachable() { 628 void MockConnectionManager::SetServerNotReachable() {
659 server_status_ = HttpResponse::CONNECTION_UNAVAILABLE;
660 server_reachable_ = false; 629 server_reachable_ = false;
661
662 FOR_EACH_OBSERVER(ServerConnectionEventListener, listeners_,
663 OnServerConnectionEvent(
664 ServerConnectionEvent(server_status_, server_reachable_)));
665 } 630 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698