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