| OLD | NEW |
| 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 Loading... |
| 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 Loading... |
| 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_) { |
| 121 |
| 107 fail_next_postbuffer_ = false; | 122 fail_next_postbuffer_ = false; |
| 108 return false; | 123 return false; |
| 109 } | 124 } |
| 110 | 125 |
| 111 if (!server_reachable_) { | 126 if (!server_reachable_) { |
| 112 params->response.server_status = HttpResponse::CONNECTION_UNAVAILABLE; | 127 params->response.server_status = HttpResponse::CONNECTION_UNAVAILABLE; |
| 113 return false; | 128 return false; |
| 114 } | 129 } |
| 115 | 130 |
| 116 // Default to an ok connection. | 131 // Default to an ok connection. |
| (...skipping 11 matching lines...) Expand all Loading... |
| 128 } | 143 } |
| 129 bool result = true; | 144 bool result = true; |
| 130 EXPECT_TRUE(!store_birthday_sent_ || post.has_store_birthday() || | 145 EXPECT_TRUE(!store_birthday_sent_ || post.has_store_birthday() || |
| 131 post.message_contents() == ClientToServerMessage::AUTHENTICATE); | 146 post.message_contents() == ClientToServerMessage::AUTHENTICATE); |
| 132 store_birthday_sent_ = true; | 147 store_birthday_sent_ = true; |
| 133 | 148 |
| 134 if (post.message_contents() == ClientToServerMessage::COMMIT) { | 149 if (post.message_contents() == ClientToServerMessage::COMMIT) { |
| 135 ProcessCommit(&post, &response); | 150 ProcessCommit(&post, &response); |
| 136 } else if (post.message_contents() == ClientToServerMessage::GET_UPDATES) { | 151 } else if (post.message_contents() == ClientToServerMessage::GET_UPDATES) { |
| 137 ProcessGetUpdates(&post, &response); | 152 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) { | 153 } else if (post.message_contents() == ClientToServerMessage::CLEAR_DATA) { |
| 141 ProcessClearData(&post, &response); | 154 ProcessClearData(&post, &response); |
| 142 } else { | 155 } else { |
| 143 EXPECT_TRUE(false) << "Unknown/unsupported ClientToServerMessage"; | 156 EXPECT_TRUE(false) << "Unknown/unsupported ClientToServerMessage"; |
| 144 return false; | 157 return false; |
| 145 } | 158 } |
| 146 if (client_command_.get()) { | 159 if (client_command_.get()) { |
| 147 response.mutable_client_command()->CopyFrom(*client_command_.get()); | 160 response.mutable_client_command()->CopyFrom(*client_command_.get()); |
| 148 } | 161 } |
| 149 | 162 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 163 !mid_commit_callback_.is_null()) { | 176 !mid_commit_callback_.is_null()) { |
| 164 mid_commit_callback_.Run(); | 177 mid_commit_callback_.Run(); |
| 165 } | 178 } |
| 166 if (mid_commit_observer_) { | 179 if (mid_commit_observer_) { |
| 167 mid_commit_observer_->Observe(); | 180 mid_commit_observer_->Observe(); |
| 168 } | 181 } |
| 169 | 182 |
| 170 return result; | 183 return result; |
| 171 } | 184 } |
| 172 | 185 |
| 173 bool MockConnectionManager::IsServerReachable() { | |
| 174 return true; | |
| 175 } | |
| 176 | |
| 177 bool MockConnectionManager::IsUserAuthenticated() { | |
| 178 return true; | |
| 179 } | |
| 180 | |
| 181 sync_pb::GetUpdatesResponse* MockConnectionManager::GetUpdateResponse() { | 186 sync_pb::GetUpdatesResponse* MockConnectionManager::GetUpdateResponse() { |
| 182 if (update_queue_.empty()) { | 187 if (update_queue_.empty()) { |
| 183 NextUpdateBatch(); | 188 NextUpdateBatch(); |
| 184 } | 189 } |
| 185 return &update_queue_.back(); | 190 return &update_queue_.back(); |
| 186 } | 191 } |
| 187 | 192 |
| 188 void MockConnectionManager::AddDefaultBookmarkData(sync_pb::SyncEntity* entity, | 193 void MockConnectionManager::AddDefaultBookmarkData(sync_pb::SyncEntity* entity, |
| 189 bool is_folder) { | 194 bool is_folder) { |
| 190 if (use_legacy_bookmarks_protocol_) { | 195 if (use_legacy_bookmarks_protocol_) { |
| (...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 459 } | 464 } |
| 460 | 465 |
| 461 void MockConnectionManager::ProcessClearData(ClientToServerMessage* csm, | 466 void MockConnectionManager::ProcessClearData(ClientToServerMessage* csm, |
| 462 ClientToServerResponse* response) { | 467 ClientToServerResponse* response) { |
| 463 CHECK(csm->has_clear_user_data()); | 468 CHECK(csm->has_clear_user_data()); |
| 464 ASSERT_EQ(csm->message_contents(), ClientToServerMessage::CLEAR_DATA); | 469 ASSERT_EQ(csm->message_contents(), ClientToServerMessage::CLEAR_DATA); |
| 465 response->clear_user_data(); | 470 response->clear_user_data(); |
| 466 response->set_error_code(clear_user_data_response_errortype_); | 471 response->set_error_code(clear_user_data_response_errortype_); |
| 467 } | 472 } |
| 468 | 473 |
| 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() { | 474 bool MockConnectionManager::ShouldConflictThisCommit() { |
| 499 bool conflict = false; | 475 bool conflict = false; |
| 500 if (conflict_all_commits_) { | 476 if (conflict_all_commits_) { |
| 501 conflict = true; | 477 conflict = true; |
| 502 } else if (conflict_n_commits_ > 0) { | 478 } else if (conflict_n_commits_ > 0) { |
| 503 conflict = true; | 479 conflict = true; |
| 504 --conflict_n_commits_; | 480 --conflict_n_commits_; |
| 505 } | 481 } |
| 506 return conflict; | 482 return conflict; |
| 507 } | 483 } |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 640 int data_type_id = syncable::GetExtensionFieldNumberFromModelType(value); | 616 int data_type_id = syncable::GetExtensionFieldNumberFromModelType(value); |
| 641 for (int i = 0; i < filter.size(); ++i) { | 617 for (int i = 0; i < filter.size(); ++i) { |
| 642 if (filter.Get(i).data_type_id() == data_type_id) { | 618 if (filter.Get(i).data_type_id() == data_type_id) { |
| 643 return &(filter.Get(i)); | 619 return &(filter.Get(i)); |
| 644 } | 620 } |
| 645 } | 621 } |
| 646 return NULL; | 622 return NULL; |
| 647 } | 623 } |
| 648 | 624 |
| 649 void MockConnectionManager::SetServerReachable() { | 625 void MockConnectionManager::SetServerReachable() { |
| 650 server_status_ = HttpResponse::SERVER_CONNECTION_OK; | |
| 651 server_reachable_ = true; | 626 server_reachable_ = true; |
| 652 | |
| 653 FOR_EACH_OBSERVER(ServerConnectionEventListener, listeners_, | |
| 654 OnServerConnectionEvent( | |
| 655 ServerConnectionEvent(server_status_, server_reachable_))); | |
| 656 } | 627 } |
| 657 | 628 |
| 658 void MockConnectionManager::SetServerNotReachable() { | 629 void MockConnectionManager::SetServerNotReachable() { |
| 659 server_status_ = HttpResponse::CONNECTION_UNAVAILABLE; | |
| 660 server_reachable_ = false; | 630 server_reachable_ = false; |
| 631 } |
| 661 | 632 |
| 662 FOR_EACH_OBSERVER(ServerConnectionEventListener, listeners_, | 633 void MockConnectionManager::UpdateConnectionStatus() { |
| 663 OnServerConnectionEvent( | 634 if (!server_reachable_) { |
| 664 ServerConnectionEvent(server_status_, server_reachable_))); | 635 server_status_ = HttpResponse::CONNECTION_UNAVAILABLE; |
| 636 } else { |
| 637 server_status_ = HttpResponse::SERVER_CONNECTION_OK; |
| 638 } |
| 665 } | 639 } |
| OLD | NEW |