| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 // | |
| 5 // Mock ServerConnectionManager class for use in client unit tests. | |
| 6 | |
| 7 #ifndef CHROME_TEST_SYNC_ENGINE_MOCK_CONNECTION_MANAGER_H_ | |
| 8 #define CHROME_TEST_SYNC_ENGINE_MOCK_CONNECTION_MANAGER_H_ | |
| 9 #pragma once | |
| 10 | |
| 11 #include <bitset> | |
| 12 #include <list> | |
| 13 #include <string> | |
| 14 #include <vector> | |
| 15 | |
| 16 #include "base/callback.h" | |
| 17 #include "base/compiler_specific.h" | |
| 18 #include "base/memory/scoped_vector.h" | |
| 19 #include "chrome/browser/sync/engine/net/server_connection_manager.h" | |
| 20 #include "chrome/browser/sync/protocol/sync.pb.h" | |
| 21 #include "chrome/browser/sync/syncable/directory_manager.h" | |
| 22 #include "chrome/browser/sync/syncable/model_type.h" | |
| 23 #include "chrome/browser/sync/syncable/model_type_payload_map.h" | |
| 24 | |
| 25 namespace syncable { | |
| 26 class DirectoryManager; | |
| 27 class ScopedDirLookup; | |
| 28 } | |
| 29 namespace browser_sync { | |
| 30 struct HttpResponse; | |
| 31 } | |
| 32 | |
| 33 class MockConnectionManager : public browser_sync::ServerConnectionManager { | |
| 34 public: | |
| 35 class MidCommitObserver { | |
| 36 public: | |
| 37 virtual void Observe() = 0; | |
| 38 | |
| 39 protected: | |
| 40 virtual ~MidCommitObserver() {} | |
| 41 }; | |
| 42 | |
| 43 MockConnectionManager(syncable::DirectoryManager* dirmgr, | |
| 44 const std::string& name); | |
| 45 virtual ~MockConnectionManager(); | |
| 46 | |
| 47 // Overridden ServerConnectionManager functions. | |
| 48 virtual bool PostBufferToPath( | |
| 49 PostBufferParams*, | |
| 50 const std::string& path, | |
| 51 const std::string& auth_token, | |
| 52 browser_sync::ScopedServerStatusWatcher* watcher) OVERRIDE; | |
| 53 | |
| 54 virtual bool IsServerReachable() OVERRIDE; | |
| 55 virtual bool IsUserAuthenticated() OVERRIDE; | |
| 56 | |
| 57 // Control of commit response. | |
| 58 void SetMidCommitCallback(Callback0::Type* callback); | |
| 59 void SetMidCommitObserver(MidCommitObserver* observer); | |
| 60 | |
| 61 // Set this if you want commit to perform commit time rename. Will request | |
| 62 // that the client renames all commited entries, prepending this string. | |
| 63 void SetCommitTimeRename(std::string prepend); | |
| 64 | |
| 65 // Generic versions of AddUpdate functions. Tests using these function should | |
| 66 // compile for both the int64 and string id based versions of the server. | |
| 67 // The SyncEntity returned is only valid until the Sync is completed | |
| 68 // (e.g. with SyncShare.) It allows to add further entity properties before | |
| 69 // sync, using SetLastXXX() methods and/or GetMutableLastUpdate(). | |
| 70 sync_pb::SyncEntity* AddUpdateDirectory(syncable::Id id, | |
| 71 syncable::Id parent_id, | |
| 72 std::string name, | |
| 73 int64 version, | |
| 74 int64 sync_ts); | |
| 75 sync_pb::SyncEntity* AddUpdateBookmark(syncable::Id id, | |
| 76 syncable::Id parent_id, | |
| 77 std::string name, | |
| 78 int64 version, | |
| 79 int64 sync_ts); | |
| 80 // Versions of the AddUpdate functions that accept integer IDs. | |
| 81 sync_pb::SyncEntity* AddUpdateDirectory(int id, | |
| 82 int parent_id, | |
| 83 std::string name, | |
| 84 int64 version, | |
| 85 int64 sync_ts); | |
| 86 sync_pb::SyncEntity* AddUpdateBookmark(int id, | |
| 87 int parent_id, | |
| 88 std::string name, | |
| 89 int64 version, | |
| 90 int64 sync_ts); | |
| 91 // New protocol versions of the AddUpdate functions. | |
| 92 sync_pb::SyncEntity* AddUpdateDirectory(std::string id, | |
| 93 std::string parent_id, | |
| 94 std::string name, | |
| 95 int64 version, | |
| 96 int64 sync_ts); | |
| 97 sync_pb::SyncEntity* AddUpdateBookmark(std::string id, | |
| 98 std::string parent_id, | |
| 99 std::string name, | |
| 100 int64 version, | |
| 101 int64 sync_ts); | |
| 102 | |
| 103 // Find the last commit sent by the client, and replay it for the next get | |
| 104 // updates command. This can be used to simulate the GetUpdates that happens | |
| 105 // immediately after a successful commit. | |
| 106 sync_pb::SyncEntity* AddUpdateFromLastCommit(); | |
| 107 | |
| 108 // Add a deleted item. Deletion records typically contain no | |
| 109 // additional information beyond the deletion, and no specifics. | |
| 110 // The server may send the originator fields. | |
| 111 void AddUpdateTombstone(const syncable::Id& id); | |
| 112 | |
| 113 void SetLastUpdateDeleted(); | |
| 114 void SetLastUpdateServerTag(const std::string& tag); | |
| 115 void SetLastUpdateClientTag(const std::string& tag); | |
| 116 void SetLastUpdateOriginatorFields(const std::string& client_id, | |
| 117 const std::string& entry_id); | |
| 118 void SetLastUpdatePosition(int64 position_in_parent); | |
| 119 void SetNewTimestamp(int ts); | |
| 120 void SetChangesRemaining(int64 count); | |
| 121 | |
| 122 // Add a new batch of updates after the current one. Allows multiple | |
| 123 // GetUpdates responses to be buffered up, since the syncer may | |
| 124 // issue multiple requests during a sync cycle. | |
| 125 void NextUpdateBatch(); | |
| 126 | |
| 127 // For AUTHENTICATE responses. | |
| 128 void SetAuthenticationResponseInfo(const std::string& valid_auth_token, | |
| 129 const std::string& user_display_name, | |
| 130 const std::string& user_display_email, | |
| 131 const std::string& user_obfuscated_id); | |
| 132 | |
| 133 void FailNextPostBufferToPathCall() { fail_next_postbuffer_ = true; } | |
| 134 | |
| 135 void SetClearUserDataResponseStatus( | |
| 136 sync_pb::ClientToServerResponse::ErrorType errortype); | |
| 137 | |
| 138 // A visitor class to allow a test to change some monitoring state atomically | |
| 139 // with the action of overriding the response codes sent back to the Syncer | |
| 140 // (for example, so you can say "ThrottleNextRequest, and assert no more | |
| 141 // requests are made once throttling is in effect" in one step. | |
| 142 class ResponseCodeOverrideRequestor { | |
| 143 public: | |
| 144 // Called with response_code_override_lock_ acquired. | |
| 145 virtual void OnOverrideComplete() = 0; | |
| 146 | |
| 147 protected: | |
| 148 virtual ~ResponseCodeOverrideRequestor() {} | |
| 149 }; | |
| 150 void ThrottleNextRequest(ResponseCodeOverrideRequestor* visitor); | |
| 151 void FailWithAuthInvalid(ResponseCodeOverrideRequestor* visitor); | |
| 152 void StopFailingWithAuthInvalid(ResponseCodeOverrideRequestor* visitor); | |
| 153 void FailNonPeriodicGetUpdates() { fail_non_periodic_get_updates_ = true; } | |
| 154 | |
| 155 // Simple inspectors. | |
| 156 bool client_stuck() const { return client_stuck_; } | |
| 157 | |
| 158 sync_pb::ClientCommand* GetNextClientCommand(); | |
| 159 | |
| 160 const std::vector<syncable::Id>& committed_ids() const { | |
| 161 return committed_ids_; | |
| 162 } | |
| 163 const std::vector<sync_pb::CommitMessage*>& commit_messages() const { | |
| 164 return commit_messages_.get(); | |
| 165 } | |
| 166 const std::vector<sync_pb::CommitResponse*>& commit_responses() const { | |
| 167 return commit_responses_.get(); | |
| 168 } | |
| 169 // Retrieve the last sent commit message. | |
| 170 const sync_pb::CommitMessage& last_sent_commit() const; | |
| 171 | |
| 172 // Retrieve the last returned commit response. | |
| 173 const sync_pb::CommitResponse& last_commit_response() const; | |
| 174 | |
| 175 // Retrieve the last request submitted to the server (regardless of type). | |
| 176 const sync_pb::ClientToServerMessage& last_request() const { | |
| 177 return last_request_; | |
| 178 } | |
| 179 | |
| 180 void set_conflict_all_commits(bool value) { | |
| 181 conflict_all_commits_ = value; | |
| 182 } | |
| 183 void set_next_new_id(int value) { | |
| 184 next_new_id_ = value; | |
| 185 } | |
| 186 void set_conflict_n_commits(int value) { | |
| 187 conflict_n_commits_ = value; | |
| 188 } | |
| 189 | |
| 190 void set_use_legacy_bookmarks_protocol(bool value) { | |
| 191 use_legacy_bookmarks_protocol_ = value; | |
| 192 } | |
| 193 | |
| 194 void set_store_birthday(std::string new_birthday) { | |
| 195 // Multiple threads can set store_birthday_ in our tests, need to lock it to | |
| 196 // ensure atomic read/writes and avoid race conditions. | |
| 197 base::AutoLock lock(store_birthday_lock_); | |
| 198 store_birthday_ = new_birthday; | |
| 199 } | |
| 200 | |
| 201 // Retrieve the number of GetUpdates requests that the mock server has | |
| 202 // seen since the last time this function was called. Can be used to | |
| 203 // verify that a GetUpdates actually did or did not happen after running | |
| 204 // the syncer. | |
| 205 int GetAndClearNumGetUpdatesRequests() { | |
| 206 int result = num_get_updates_requests_; | |
| 207 num_get_updates_requests_ = 0; | |
| 208 return result; | |
| 209 } | |
| 210 | |
| 211 // Expect that GetUpdates will request exactly the types indicated in | |
| 212 // the bitset. | |
| 213 void ExpectGetUpdatesRequestTypes( | |
| 214 std::bitset<syncable::MODEL_TYPE_COUNT> expected_filter) { | |
| 215 expected_filter_ = expected_filter; | |
| 216 } | |
| 217 | |
| 218 void ExpectGetUpdatesRequestPayloads( | |
| 219 const syncable::ModelTypePayloadMap& payloads) { | |
| 220 expected_payloads_ = payloads; | |
| 221 } | |
| 222 | |
| 223 void SetServerReachable(); | |
| 224 | |
| 225 void SetServerNotReachable(); | |
| 226 | |
| 227 // Return by copy to be thread-safe. | |
| 228 const std::string store_birthday() { | |
| 229 base::AutoLock lock(store_birthday_lock_); | |
| 230 return store_birthday_; | |
| 231 } | |
| 232 | |
| 233 // Locate the most recent update message for purpose of alteration. | |
| 234 sync_pb::SyncEntity* GetMutableLastUpdate(); | |
| 235 | |
| 236 private: | |
| 237 sync_pb::SyncEntity* AddUpdateFull(syncable::Id id, syncable::Id parentid, | |
| 238 std::string name, int64 version, | |
| 239 int64 sync_ts, | |
| 240 bool is_dir); | |
| 241 sync_pb::SyncEntity* AddUpdateFull(std::string id, | |
| 242 std::string parentid, std::string name, | |
| 243 int64 version, int64 sync_ts, | |
| 244 bool is_dir); | |
| 245 // Functions to handle the various types of server request. | |
| 246 void ProcessGetUpdates(sync_pb::ClientToServerMessage* csm, | |
| 247 sync_pb::ClientToServerResponse* response); | |
| 248 void ProcessAuthenticate(sync_pb::ClientToServerMessage* csm, | |
| 249 sync_pb::ClientToServerResponse* response, | |
| 250 const std::string& auth_token); | |
| 251 void ProcessCommit(sync_pb::ClientToServerMessage* csm, | |
| 252 sync_pb::ClientToServerResponse* response_buffer); | |
| 253 void ProcessClearData(sync_pb::ClientToServerMessage* csm, | |
| 254 sync_pb::ClientToServerResponse* response); | |
| 255 void AddDefaultBookmarkData(sync_pb::SyncEntity* entity, bool is_folder); | |
| 256 | |
| 257 // Determine if one entry in a commit should be rejected with a conflict. | |
| 258 bool ShouldConflictThisCommit(); | |
| 259 | |
| 260 // Generate a numeric position_in_parent value. We use a global counter | |
| 261 // that only decreases; this simulates new objects always being added to the | |
| 262 // front of the ordering. | |
| 263 int64 GeneratePositionInParent() { | |
| 264 return next_position_in_parent_--; | |
| 265 } | |
| 266 | |
| 267 // Get a mutable update response which will eventually be returned to the | |
| 268 // client. | |
| 269 sync_pb::GetUpdatesResponse* GetUpdateResponse(); | |
| 270 void ApplyToken(); | |
| 271 | |
| 272 // Determine whether an progress marker array (like that sent in | |
| 273 // GetUpdates.from_progress_marker) indicates that a particular ModelType | |
| 274 // should be included. | |
| 275 bool IsModelTypePresentInSpecifics( | |
| 276 const google::protobuf::RepeatedPtrField< | |
| 277 sync_pb::DataTypeProgressMarker>& filter, | |
| 278 syncable::ModelType value); | |
| 279 | |
| 280 sync_pb::DataTypeProgressMarker const* GetProgressMarkerForType( | |
| 281 const google::protobuf::RepeatedPtrField< | |
| 282 sync_pb::DataTypeProgressMarker>& filter, | |
| 283 syncable::ModelType value); | |
| 284 | |
| 285 // All IDs that have been committed. | |
| 286 std::vector<syncable::Id> committed_ids_; | |
| 287 | |
| 288 // Control of when/if we return conflicts. | |
| 289 bool conflict_all_commits_; | |
| 290 int conflict_n_commits_; | |
| 291 | |
| 292 // Commit messages we've sent, and responses we've returned. | |
| 293 ScopedVector<sync_pb::CommitMessage> commit_messages_; | |
| 294 ScopedVector<sync_pb::CommitResponse> commit_responses_; | |
| 295 | |
| 296 // The next id the mock will return to a commit. | |
| 297 int next_new_id_; | |
| 298 | |
| 299 // The store birthday we send to the client. | |
| 300 std::string store_birthday_; | |
| 301 base::Lock store_birthday_lock_; | |
| 302 bool store_birthday_sent_; | |
| 303 bool client_stuck_; | |
| 304 std::string commit_time_rename_prepended_string_; | |
| 305 | |
| 306 // Fail on the next call to PostBufferToPath(). | |
| 307 bool fail_next_postbuffer_; | |
| 308 | |
| 309 // Our directory. | |
| 310 syncable::DirectoryManager* directory_manager_; | |
| 311 std::string directory_name_; | |
| 312 | |
| 313 // The updates we'll return to the next request. | |
| 314 std::list<sync_pb::GetUpdatesResponse> update_queue_; | |
| 315 scoped_ptr<Callback0::Type> mid_commit_callback_; | |
| 316 MidCommitObserver* mid_commit_observer_; | |
| 317 | |
| 318 // The clear data response we'll return in the next response | |
| 319 sync_pb::ClientToServerResponse::ErrorType | |
| 320 clear_user_data_response_errortype_; | |
| 321 | |
| 322 // The AUTHENTICATE response we'll return for auth requests. | |
| 323 sync_pb::AuthenticateResponse auth_response_; | |
| 324 // What we use to determine if we should return SUCCESS or BAD_AUTH_TOKEN. | |
| 325 std::string valid_auth_token_; | |
| 326 | |
| 327 // Whether we are faking a server mandating clients to throttle requests. | |
| 328 // Protected by |response_code_override_lock_|. | |
| 329 bool throttling_; | |
| 330 | |
| 331 // Whether we are failing all requests by returning | |
| 332 // ClientToServerResponse::AUTH_INVALID. | |
| 333 // Protected by |response_code_override_lock_|. | |
| 334 bool fail_with_auth_invalid_; | |
| 335 | |
| 336 base::Lock response_code_override_lock_; | |
| 337 | |
| 338 // True if we are only accepting GetUpdatesCallerInfo::PERIODIC requests. | |
| 339 bool fail_non_periodic_get_updates_; | |
| 340 | |
| 341 scoped_ptr<sync_pb::ClientCommand> client_command_; | |
| 342 | |
| 343 // The next value to use for the position_in_parent property. | |
| 344 int64 next_position_in_parent_; | |
| 345 | |
| 346 // The default is to use the newer sync_pb::BookmarkSpecifics-style protocol. | |
| 347 // If this option is set to true, then the MockConnectionManager will | |
| 348 // use the older sync_pb::SyncEntity_BookmarkData-style protocol. | |
| 349 bool use_legacy_bookmarks_protocol_; | |
| 350 | |
| 351 std::bitset<syncable::MODEL_TYPE_COUNT> expected_filter_; | |
| 352 | |
| 353 syncable::ModelTypePayloadMap expected_payloads_; | |
| 354 | |
| 355 int num_get_updates_requests_; | |
| 356 | |
| 357 std::string next_token_; | |
| 358 | |
| 359 sync_pb::ClientToServerMessage last_request_; | |
| 360 | |
| 361 DISALLOW_COPY_AND_ASSIGN(MockConnectionManager); | |
| 362 }; | |
| 363 | |
| 364 #endif // CHROME_TEST_SYNC_ENGINE_MOCK_CONNECTION_MANAGER_H_ | |
| OLD | NEW |