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