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