[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 | |
| 7 | #ifndef CHROME_TEST_SYNC_ENGINE_MOCK_SERVER_CONNECTION_H_ |
| 8 | #define CHROME_TEST_SYNC_ENGINE_MOCK_SERVER_CONNECTION_H_ |
| 9 | |
| 10 | #include <string> |
| 11 | #include <vector> |
| 12 | |
| 13 | #include "chrome/browser/sync/engine/net/server_connection_manager.h" |
| 14 | #include "chrome/browser/sync/protocol/sync.pb.h" |
| 15 | #include "chrome/browser/sync/syncable/directory_manager.h" |
| 16 | |
| 17 | using std::string; |
| 18 | using std::vector; |
| 19 | |
| 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: |
| 30 | // A callback function type. These can be set to be called when server |
| 31 | // activity would normally take place. This aids simulation of race |
| 32 | // conditions. |
| 33 | typedef bool (*TestCallbackFunction)(syncable::Directory* dir); |
[email protected] | 2186fec4 | 2009-09-25 23:09:36 | [diff] [blame] | 34 | class MidCommitObserver { |
| 35 | public: |
| 36 | virtual void Observe() = 0; |
| 37 | }; |
[email protected] | 5852edc1b | 2009-09-10 06:05:27 | [diff] [blame] | 38 | |
| 39 | MockConnectionManager(syncable::DirectoryManager* dirmgr, PathString name); |
| 40 | virtual ~MockConnectionManager(); |
| 41 | |
| 42 | // Overridden ServerConnectionManager functions. |
| 43 | virtual bool PostBufferToPath(const PostBufferParams*, |
| 44 | const string& path, |
| 45 | const string& auth_token); |
| 46 | |
| 47 | virtual bool IsServerReachable(); |
| 48 | virtual bool IsUserAuthenticated(); |
| 49 | |
| 50 | // Control of commit response. |
| 51 | void SetMidCommitCallbackFunction(TestCallbackFunction 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(); |
| 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 |
| 66 | // sync, using AddUpdateExtendedAttributes. |
| 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); |
| 99 | void AddUpdateExtendedAttributes(sync_pb::SyncEntity* ent, |
| 100 | PathString* xattr_key, |
| 101 | syncable::Blob* xattr_value, |
| 102 | int xattr_count); |
[email protected] | 5af80fa | 2009-09-14 18:34:07 | [diff] [blame] | 103 | // Prepare to add checksums. |
[email protected] | 5852edc1b | 2009-09-10 06:05:27 | [diff] [blame] | 104 | void SetLastUpdateDeleted(); |
| 105 | void SetLastUpdateSingletonTag(const string& tag); |
| 106 | void SetLastUpdateOriginatorFields(const string& client_id, |
| 107 | const string& entry_id); |
| 108 | void SetLastUpdatePosition(int64 position_in_parent); |
| 109 | void SetNewTimestamp(int64 ts); |
| 110 | void SetNewestTimestamp(int64 ts); |
| 111 | |
[email protected] | 3c47992 | 2009-10-07 23:57:20 | [diff] [blame] | 112 | // For AUTHENTICATE responses. |
| 113 | void SetAuthenticationResponseInfo(const std::string& valid_auth_token, |
| 114 | const std::string& user_display_name, |
| 115 | const std::string& user_display_email, |
| 116 | const std::string& user_obfuscated_id); |
| 117 | |
[email protected] | 5852edc1b | 2009-09-10 06:05:27 | [diff] [blame] | 118 | void FailNextPostBufferToPathCall() { fail_next_postbuffer_ = true; } |
| 119 | |
[email protected] | 1b4ec9a | 2009-10-16 01:31:55 | [diff] [blame^] | 120 | // A visitor class to allow a test to change some monitoring state atomically |
| 121 | // with the action of throttling requests (for example, so you can say |
| 122 | // "ThrottleNextRequest, and assert no more requests are made once throttling |
| 123 | // is in effect" in one step. |
| 124 | class ThrottleRequestVisitor { |
| 125 | public: |
| 126 | // Called with throttle parameter lock acquired. |
| 127 | virtual void VisitAtomically() = 0; |
| 128 | }; |
| 129 | void ThrottleNextRequest(ThrottleRequestVisitor* visitor); |
| 130 | void FailNonPeriodicGetUpdates() { fail_non_periodic_get_updates_ = true; } |
| 131 | |
[email protected] | 5af80fa | 2009-09-14 18:34:07 | [diff] [blame] | 132 | // Simple inspectors. |
[email protected] | 5852edc1b | 2009-09-10 06:05:27 | [diff] [blame] | 133 | bool client_stuck() const { return client_stuck_; } |
| 134 | |
| 135 | sync_pb::ClientCommand* GetNextClientCommand(); |
| 136 | |
| 137 | const vector<syncable::Id>& committed_ids() const { return committed_ids_; } |
| 138 | const vector<sync_pb::CommitMessage*>& commit_messages() const { |
| 139 | return commit_messages_; |
| 140 | } |
| 141 | // Retrieve the last sent commit message. |
| 142 | const sync_pb::CommitMessage& last_sent_commit() const; |
| 143 | |
| 144 | void set_conflict_all_commits(bool value) { |
| 145 | conflict_all_commits_ = value; |
| 146 | } |
| 147 | void set_next_new_id(int value) { |
| 148 | next_new_id_ = value; |
| 149 | } |
| 150 | void set_conflict_n_commits(int value) { |
| 151 | conflict_n_commits_ = value; |
| 152 | } |
| 153 | |
| 154 | private: |
| 155 | sync_pb::SyncEntity* AddUpdateFull(syncable::Id id, syncable::Id parentid, |
| 156 | string name, int64 version, |
| 157 | int64 sync_ts, |
| 158 | bool is_dir); |
| 159 | sync_pb::SyncEntity* AddUpdateFull(string id, string parentid, string name, |
| 160 | int64 version, int64 sync_ts, |
| 161 | bool is_dir); |
| 162 | // Functions to handle the various types of server request. |
| 163 | void ProcessGetUpdates(sync_pb::ClientToServerMessage* csm, |
| 164 | sync_pb::ClientToServerResponse* response); |
[email protected] | 3c47992 | 2009-10-07 23:57:20 | [diff] [blame] | 165 | void ProcessAuthenticate(sync_pb::ClientToServerMessage* csm, |
| 166 | sync_pb::ClientToServerResponse* response, |
| 167 | const std::string& auth_token); |
[email protected] | 5852edc1b | 2009-09-10 06:05:27 | [diff] [blame] | 168 | void ProcessCommit(sync_pb::ClientToServerMessage* csm, |
| 169 | sync_pb::ClientToServerResponse* response_buffer); |
| 170 | // Locate the most recent update message for purpose of alteration. |
| 171 | sync_pb::SyncEntity* GetMutableLastUpdate(); |
| 172 | |
| 173 | // Determine if one entry in a commit should be rejected with a conflict. |
| 174 | bool ShouldConflictThisCommit(); |
| 175 | |
[email protected] | 5af80fa | 2009-09-14 18:34:07 | [diff] [blame] | 176 | // Generate a numeric position_in_parent value. We use a global counter |
| 177 | // that only decreases; this simulates new objects always being added to the |
| 178 | // front of the ordering. |
[email protected] | 5852edc1b | 2009-09-10 06:05:27 | [diff] [blame] | 179 | int64 GeneratePositionInParent() { |
| 180 | return next_position_in_parent_--; |
| 181 | } |
| 182 | |
| 183 | // All IDs that have been committed. |
| 184 | vector<syncable::Id> committed_ids_; |
| 185 | |
| 186 | // Control of when/if we return conflicts. |
| 187 | bool conflict_all_commits_; |
| 188 | int conflict_n_commits_; |
| 189 | |
| 190 | // Commit messages we've sent |
| 191 | vector<sync_pb::CommitMessage*> commit_messages_; |
| 192 | |
| 193 | // The next id the mock will return to a commit. |
| 194 | int next_new_id_; |
| 195 | |
| 196 | // The store birthday we send to the client. |
| 197 | string store_birthday_; |
| 198 | bool store_birthday_sent_; |
| 199 | bool client_stuck_; |
| 200 | string commit_time_rename_prepended_string_; |
| 201 | |
[email protected] | 5af80fa | 2009-09-14 18:34:07 | [diff] [blame] | 202 | // Fail on the next call to PostBufferToPath(). |
[email protected] | 5852edc1b | 2009-09-10 06:05:27 | [diff] [blame] | 203 | bool fail_next_postbuffer_; |
| 204 | |
| 205 | // Our directory. |
[email protected] | 2186fec4 | 2009-09-25 23:09:36 | [diff] [blame] | 206 | syncable::DirectoryManager* directory_manager_; |
| 207 | PathString directory_name_; |
[email protected] | 5852edc1b | 2009-09-10 06:05:27 | [diff] [blame] | 208 | |
| 209 | // The updates we'll return to the next request. |
| 210 | sync_pb::GetUpdatesResponse updates_; |
| 211 | TestCallbackFunction mid_commit_callback_function_; |
[email protected] | 2186fec4 | 2009-09-25 23:09:36 | [diff] [blame] | 212 | MidCommitObserver* mid_commit_observer_; |
[email protected] | 5852edc1b | 2009-09-10 06:05:27 | [diff] [blame] | 213 | |
[email protected] | 3c47992 | 2009-10-07 23:57:20 | [diff] [blame] | 214 | // The AUTHENTICATE response we'll return for auth requests. |
| 215 | sync_pb::AuthenticateResponse auth_response_; |
| 216 | // What we use to determine if we should return SUCCESS or BAD_AUTH_TOKEN. |
| 217 | std::string valid_auth_token_; |
| 218 | |
[email protected] | 1b4ec9a | 2009-10-16 01:31:55 | [diff] [blame^] | 219 | // Whether we are faking a server mandating clients to throttle requests. |
| 220 | // Protected by |throttle_lock_|. |
| 221 | bool throttling_; |
| 222 | Lock throttle_lock_; |
| 223 | |
| 224 | // True if we are only accepting GetUpdatesCallerInfo::PERIODIC requests. |
| 225 | bool fail_non_periodic_get_updates_; |
| 226 | |
[email protected] | 5852edc1b | 2009-09-10 06:05:27 | [diff] [blame] | 227 | scoped_ptr<sync_pb::ClientCommand> client_command_; |
| 228 | |
| 229 | // The next value to use for the position_in_parent property. |
| 230 | int64 next_position_in_parent_; |
| 231 | |
| 232 | DISALLOW_COPY_AND_ASSIGN(MockConnectionManager); |
| 233 | }; |
| 234 | |
| 235 | #endif // CHROME_TEST_SYNC_ENGINE_MOCK_SERVER_CONNECTION_H_ |