blob: 0e4759dc2091978ce05226c02ce725cb0017564b [file] [log] [blame]
[email protected]a66e33a22010-07-16 14:06:111// Copyright (c) 2010 The Chromium Authors. All rights reserved.
[email protected]5852edc1b2009-09-10 06:05:272// 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]5852edc1b2009-09-10 06:05:276
[email protected]d07f0c82010-06-25 00:10:327#ifndef CHROME_TEST_SYNC_ENGINE_MOCK_CONNECTION_MANAGER_H_
8#define CHROME_TEST_SYNC_ENGINE_MOCK_CONNECTION_MANAGER_H_
[email protected]5852edc1b2009-09-10 06:05:279
[email protected]f753af62010-02-11 01:05:3810#include <bitset>
[email protected]5852edc1b2009-09-10 06:05:2711#include <string>
12#include <vector>
13
[email protected]a66e33a22010-07-16 14:06:1114#include "base/callback.h"
[email protected]55779e62010-07-13 20:27:1115#include "base/scoped_vector.h"
[email protected]5852edc1b2009-09-10 06:05:2716#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]f753af62010-02-11 01:05:3819#include "chrome/browser/sync/syncable/model_type.h"
[email protected]5852edc1b2009-09-10 06:05:2720
[email protected]5852edc1b2009-09-10 06:05:2721namespace syncable {
22class DirectoryManager;
23class ScopedDirLookup;
24}
25namespace browser_sync {
26struct HttpResponse;
27}
28
[email protected]5af80fa2009-09-14 18:34:0729class MockConnectionManager : public browser_sync::ServerConnectionManager {
[email protected]5852edc1b2009-09-10 06:05:2730 public:
[email protected]2186fec42009-09-25 23:09:3631 class MidCommitObserver {
32 public:
33 virtual void Observe() = 0;
34 };
[email protected]5852edc1b2009-09-10 06:05:2735
[email protected]6d201be2009-11-13 19:40:5936 MockConnectionManager(syncable::DirectoryManager* dirmgr,
37 const std::string& name);
[email protected]5852edc1b2009-09-10 06:05:2738 virtual ~MockConnectionManager();
39
40 // Overridden ServerConnectionManager functions.
41 virtual bool PostBufferToPath(const PostBufferParams*,
[email protected]18a797052010-02-09 21:48:0442 const string& path,
43 const string& auth_token,
44 browser_sync::ScopedServerStatusWatcher* watcher);
[email protected]5852edc1b2009-09-10 06:05:2745
46 virtual bool IsServerReachable();
47 virtual bool IsUserAuthenticated();
48
49 // Control of commit response.
[email protected]a66e33a22010-07-16 14:06:1150 void SetMidCommitCallback(Callback0::Type* callback);
[email protected]2186fec42009-09-25 23:09:3651 void SetMidCommitObserver(MidCommitObserver* observer);
[email protected]5852edc1b2009-09-10 06:05:2752
[email protected]5af80fa2009-09-14 18:34:0753 // 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]5852edc1b2009-09-10 06:05:2755 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]55779e62010-07-13 20:27:1161
[email protected]5852edc1b2009-09-10 06:05:2762 // 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]fca88252010-06-30 03:57:4466 // sync, using SetLastXXX() methods and/or GetMutableLastUpdate().
[email protected]5852edc1b2009-09-10 06:05:2767 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]55779e62010-07-13 20:27:1199
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]5852edc1b2009-09-10 06:05:27110 void SetLastUpdateDeleted();
[email protected]7377db592010-02-09 22:00:06111 void SetLastUpdateServerTag(const string& tag);
112 void SetLastUpdateClientTag(const string& tag);
[email protected]5852edc1b2009-09-10 06:05:27113 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]b29384222009-10-23 19:48:23117 void SetChangesRemaining(int64 timestamp);
[email protected]5852edc1b2009-09-10 06:05:27118
[email protected]3c479922009-10-07 23:57:20119 // 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]5852edc1b2009-09-10 06:05:27125 void FailNextPostBufferToPathCall() { fail_next_postbuffer_ = true; }
126
[email protected]1b4ec9a2009-10-16 01:31:55127 // A visitor class to allow a test to change some monitoring state atomically
[email protected]18a797052010-02-09 21:48:04128 // 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]1b4ec9a2009-10-16 01:31:55132 public:
[email protected]18a797052010-02-09 21:48:04133 // Called with response_code_override_lock_ acquired.
134 virtual void OnOverrideComplete() = 0;
[email protected]1b4ec9a2009-10-16 01:31:55135 };
[email protected]18a797052010-02-09 21:48:04136 void ThrottleNextRequest(ResponseCodeOverrideRequestor* visitor);
137 void FailWithAuthInvalid(ResponseCodeOverrideRequestor* visitor);
138 void StopFailingWithAuthInvalid(ResponseCodeOverrideRequestor* visitor);
[email protected]1b4ec9a2009-10-16 01:31:55139 void FailNonPeriodicGetUpdates() { fail_non_periodic_get_updates_ = true; }
140
[email protected]5af80fa2009-09-14 18:34:07141 // Simple inspectors.
[email protected]5852edc1b2009-09-10 06:05:27142 bool client_stuck() const { return client_stuck_; }
143
144 sync_pb::ClientCommand* GetNextClientCommand();
145
[email protected]f753af62010-02-11 01:05:38146 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]55779e62010-07-13 20:27:11150 return commit_messages_.get();
151 }
152 const std::vector<sync_pb::CommitResponse*>& commit_responses() const {
153 return commit_responses_.get();
[email protected]5852edc1b2009-09-10 06:05:27154 }
155 // Retrieve the last sent commit message.
156 const sync_pb::CommitMessage& last_sent_commit() const;
157
[email protected]55779e62010-07-13 20:27:11158 // Retrieve the last returned commit response.
159 const sync_pb::CommitResponse& last_commit_response() const;
160
[email protected]29574e62010-04-01 22:39:59161 // 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]5852edc1b2009-09-10 06:05:27166 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]3273dce2010-01-27 16:08:08176 void set_use_legacy_bookmarks_protocol(bool value) {
177 use_legacy_bookmarks_protocol_ = value;
178 }
179
[email protected]7f8abea2010-07-14 01:31:36180 void set_store_birthday(string new_birthday) {
181 store_birthday_ = new_birthday;
182 }
183
[email protected]f753af62010-02-11 01:05:38184 // 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]894f4a02010-06-29 20:37:06201 void SetServerReachable();
202
203 void SetServerNotReachable();
204
[email protected]55779e62010-07-13 20:27:11205 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]5852edc1b2009-09-10 06:05:27210 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]3c479922009-10-07 23:57:20221 void ProcessAuthenticate(sync_pb::ClientToServerMessage* csm,
222 sync_pb::ClientToServerResponse* response,
223 const std::string& auth_token);
[email protected]5852edc1b2009-09-10 06:05:27224 void ProcessCommit(sync_pb::ClientToServerMessage* csm,
225 sync_pb::ClientToServerResponse* response_buffer);
[email protected]3273dce2010-01-27 16:08:08226
227 void AddDefaultBookmarkData(sync_pb::SyncEntity* entity, bool is_folder);
228
[email protected]5852edc1b2009-09-10 06:05:27229 // Determine if one entry in a commit should be rejected with a conflict.
230 bool ShouldConflictThisCommit();
231
[email protected]5af80fa2009-09-14 18:34:07232 // 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]5852edc1b2009-09-10 06:05:27235 int64 GeneratePositionInParent() {
236 return next_position_in_parent_--;
237 }
238
[email protected]f753af62010-02-11 01:05:38239 // 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]5852edc1b2009-09-10 06:05:27245 // All IDs that have been committed.
[email protected]f753af62010-02-11 01:05:38246 std::vector<syncable::Id> committed_ids_;
[email protected]5852edc1b2009-09-10 06:05:27247
248 // Control of when/if we return conflicts.
249 bool conflict_all_commits_;
250 int conflict_n_commits_;
251
[email protected]55779e62010-07-13 20:27:11252 // 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]5852edc1b2009-09-10 06:05:27255
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]5af80fa2009-09-14 18:34:07265 // Fail on the next call to PostBufferToPath().
[email protected]5852edc1b2009-09-10 06:05:27266 bool fail_next_postbuffer_;
267
268 // Our directory.
[email protected]2186fec42009-09-25 23:09:36269 syncable::DirectoryManager* directory_manager_;
[email protected]6d201be2009-11-13 19:40:59270 std::string directory_name_;
[email protected]5852edc1b2009-09-10 06:05:27271
272 // The updates we'll return to the next request.
273 sync_pb::GetUpdatesResponse updates_;
[email protected]a66e33a22010-07-16 14:06:11274 scoped_ptr<Callback0::Type> mid_commit_callback_;
[email protected]2186fec42009-09-25 23:09:36275 MidCommitObserver* mid_commit_observer_;
[email protected]5852edc1b2009-09-10 06:05:27276
[email protected]3c479922009-10-07 23:57:20277 // 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]1b4ec9a2009-10-16 01:31:55282 // Whether we are faking a server mandating clients to throttle requests.
[email protected]18a797052010-02-09 21:48:04283 // Protected by |response_code_override_lock_|.
[email protected]1b4ec9a2009-10-16 01:31:55284 bool throttling_;
[email protected]18a797052010-02-09 21:48:04285
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]1b4ec9a2009-10-16 01:31:55292
293 // True if we are only accepting GetUpdatesCallerInfo::PERIODIC requests.
294 bool fail_non_periodic_get_updates_;
295
[email protected]5852edc1b2009-09-10 06:05:27296 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]3273dce2010-01-27 16:08:08301 // 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]f753af62010-02-11 01:05:38306 std::bitset<syncable::MODEL_TYPE_COUNT> expected_filter_;
307
308 int num_get_updates_requests_;
309
[email protected]29574e62010-04-01 22:39:59310 sync_pb::ClientToServerMessage last_request_;
311
[email protected]5852edc1b2009-09-10 06:05:27312 DISALLOW_COPY_AND_ASSIGN(MockConnectionManager);
313};
314
[email protected]d07f0c82010-06-25 00:10:32315#endif // CHROME_TEST_SYNC_ENGINE_MOCK_CONNECTION_MANAGER_H_