blob: 245c00581b365ce3dc60f129c07ac78ef5cc211b [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]32b76ef2010-07-26 23:08:249#pragma once
[email protected]5852edc1b2009-09-10 06:05:2710
[email protected]f753af62010-02-11 01:05:3811#include <bitset>
[email protected]a26691932011-01-13 21:00:0812#include <list>
[email protected]5852edc1b2009-09-10 06:05:2713#include <string>
14#include <vector>
15
[email protected]a66e33a22010-07-16 14:06:1116#include "base/callback.h"
[email protected]55779e62010-07-13 20:27:1117#include "base/scoped_vector.h"
[email protected]5852edc1b2009-09-10 06:05:2718#include "chrome/browser/sync/engine/net/server_connection_manager.h"
19#include "chrome/browser/sync/protocol/sync.pb.h"
[email protected]0a9c9062011-02-28 23:54:5820#include "chrome/browser/sync/sessions/session_state.h"
[email protected]5852edc1b2009-09-10 06:05:2721#include "chrome/browser/sync/syncable/directory_manager.h"
[email protected]f753af62010-02-11 01:05:3822#include "chrome/browser/sync/syncable/model_type.h"
[email protected]5852edc1b2009-09-10 06:05:2723
[email protected]5852edc1b2009-09-10 06:05:2724namespace syncable {
25class DirectoryManager;
26class ScopedDirLookup;
27}
28namespace browser_sync {
29struct HttpResponse;
30}
31
[email protected]5af80fa2009-09-14 18:34:0732class MockConnectionManager : public browser_sync::ServerConnectionManager {
[email protected]5852edc1b2009-09-10 06:05:2733 public:
[email protected]2186fec42009-09-25 23:09:3634 class MidCommitObserver {
35 public:
36 virtual void Observe() = 0;
[email protected]8e4f0102010-07-30 23:42:4337
38 protected:
39 virtual ~MidCommitObserver() {}
[email protected]2186fec42009-09-25 23:09:3640 };
[email protected]5852edc1b2009-09-10 06:05:2741
[email protected]6d201be2009-11-13 19:40:5942 MockConnectionManager(syncable::DirectoryManager* dirmgr,
43 const std::string& name);
[email protected]5852edc1b2009-09-10 06:05:2744 virtual ~MockConnectionManager();
45
46 // Overridden ServerConnectionManager functions.
[email protected]ca318f92011-01-25 22:49:0247 virtual bool PostBufferToPath(
48 const PostBufferParams*,
49 const std::string& path,
50 const std::string& auth_token,
[email protected]18a797052010-02-09 21:48:0451 browser_sync::ScopedServerStatusWatcher* watcher);
[email protected]5852edc1b2009-09-10 06:05:2752
53 virtual bool IsServerReachable();
54 virtual bool IsUserAuthenticated();
55
56 // Control of commit response.
[email protected]a66e33a22010-07-16 14:06:1157 void SetMidCommitCallback(Callback0::Type* callback);
[email protected]2186fec42009-09-25 23:09:3658 void SetMidCommitObserver(MidCommitObserver* observer);
[email protected]5852edc1b2009-09-10 06:05:2759
[email protected]5af80fa2009-09-14 18:34:0760 // Set this if you want commit to perform commit time rename. Will request
61 // that the client renames all commited entries, prepending this string.
[email protected]ca318f92011-01-25 22:49:0262 void SetCommitTimeRename(std::string prepend);
[email protected]5852edc1b2009-09-10 06:05:2763
[email protected]5852edc1b2009-09-10 06:05:2764 // Generic versions of AddUpdate functions. Tests using these function should
65 // compile for both the int64 and string id based versions of the server.
66 // The SyncEntity returned is only valid until the Sync is completed
67 // (e.g. with SyncShare.) It allows to add further entity properties before
[email protected]fca88252010-06-30 03:57:4468 // sync, using SetLastXXX() methods and/or GetMutableLastUpdate().
[email protected]5852edc1b2009-09-10 06:05:2769 sync_pb::SyncEntity* AddUpdateDirectory(syncable::Id id,
70 syncable::Id parent_id,
[email protected]ca318f92011-01-25 22:49:0271 std::string name,
[email protected]5852edc1b2009-09-10 06:05:2772 int64 version,
73 int64 sync_ts);
74 sync_pb::SyncEntity* AddUpdateBookmark(syncable::Id id,
75 syncable::Id parent_id,
[email protected]ca318f92011-01-25 22:49:0276 std::string name,
[email protected]5852edc1b2009-09-10 06:05:2777 int64 version,
78 int64 sync_ts);
79 // Versions of the AddUpdate functions that accept integer IDs.
80 sync_pb::SyncEntity* AddUpdateDirectory(int id,
81 int parent_id,
[email protected]ca318f92011-01-25 22:49:0282 std::string name,
[email protected]5852edc1b2009-09-10 06:05:2783 int64 version,
84 int64 sync_ts);
85 sync_pb::SyncEntity* AddUpdateBookmark(int id,
86 int parent_id,
[email protected]ca318f92011-01-25 22:49:0287 std::string name,
[email protected]5852edc1b2009-09-10 06:05:2788 int64 version,
89 int64 sync_ts);
90 // New protocol versions of the AddUpdate functions.
[email protected]ca318f92011-01-25 22:49:0291 sync_pb::SyncEntity* AddUpdateDirectory(std::string id,
92 std::string parent_id,
93 std::string name,
[email protected]5852edc1b2009-09-10 06:05:2794 int64 version,
95 int64 sync_ts);
[email protected]ca318f92011-01-25 22:49:0296 sync_pb::SyncEntity* AddUpdateBookmark(std::string id,
97 std::string parent_id,
98 std::string name,
[email protected]5852edc1b2009-09-10 06:05:2799 int64 version,
100 int64 sync_ts);
[email protected]55779e62010-07-13 20:27:11101
102 // Find the last commit sent by the client, and replay it for the next get
103 // updates command. This can be used to simulate the GetUpdates that happens
104 // immediately after a successful commit.
105 sync_pb::SyncEntity* AddUpdateFromLastCommit();
106
107 // Add a deleted item. Deletion records typically contain no
108 // additional information beyond the deletion, and no specifics.
109 // The server may send the originator fields.
110 void AddUpdateTombstone(const syncable::Id& id);
111
[email protected]5852edc1b2009-09-10 06:05:27112 void SetLastUpdateDeleted();
[email protected]ca318f92011-01-25 22:49:02113 void SetLastUpdateServerTag(const std::string& tag);
114 void SetLastUpdateClientTag(const std::string& tag);
115 void SetLastUpdateOriginatorFields(const std::string& client_id,
116 const std::string& entry_id);
[email protected]5852edc1b2009-09-10 06:05:27117 void SetLastUpdatePosition(int64 position_in_parent);
[email protected]a26691932011-01-13 21:00:08118 void SetNewTimestamp(int ts);
119 void SetChangesRemaining(int64 count);
120
121 // Add a new batch of updates after the current one. Allows multiple
122 // GetUpdates responses to be buffered up, since the syncer may
123 // issue multiple requests during a sync cycle.
124 void NextUpdateBatch();
[email protected]5852edc1b2009-09-10 06:05:27125
[email protected]3c479922009-10-07 23:57:20126 // For AUTHENTICATE responses.
127 void SetAuthenticationResponseInfo(const std::string& valid_auth_token,
128 const std::string& user_display_name,
129 const std::string& user_display_email,
130 const std::string& user_obfuscated_id);
131
[email protected]5852edc1b2009-09-10 06:05:27132 void FailNextPostBufferToPathCall() { fail_next_postbuffer_ = true; }
133
[email protected]689a7a72010-09-29 19:56:41134 void SetClearUserDataResponseStatus(
135 sync_pb::ClientToServerResponse::ErrorType errortype);
[email protected]1ce52aa2010-09-08 19:07:36136
[email protected]1b4ec9a2009-10-16 01:31:55137 // A visitor class to allow a test to change some monitoring state atomically
[email protected]18a797052010-02-09 21:48:04138 // with the action of overriding the response codes sent back to the Syncer
139 // (for example, so you can say "ThrottleNextRequest, and assert no more
140 // requests are made once throttling is in effect" in one step.
141 class ResponseCodeOverrideRequestor {
[email protected]1b4ec9a2009-10-16 01:31:55142 public:
[email protected]18a797052010-02-09 21:48:04143 // Called with response_code_override_lock_ acquired.
144 virtual void OnOverrideComplete() = 0;
[email protected]8e4f0102010-07-30 23:42:43145
146 protected:
147 virtual ~ResponseCodeOverrideRequestor() {}
[email protected]1b4ec9a2009-10-16 01:31:55148 };
[email protected]18a797052010-02-09 21:48:04149 void ThrottleNextRequest(ResponseCodeOverrideRequestor* visitor);
150 void FailWithAuthInvalid(ResponseCodeOverrideRequestor* visitor);
151 void StopFailingWithAuthInvalid(ResponseCodeOverrideRequestor* visitor);
[email protected]1b4ec9a2009-10-16 01:31:55152 void FailNonPeriodicGetUpdates() { fail_non_periodic_get_updates_ = true; }
153
[email protected]5af80fa2009-09-14 18:34:07154 // Simple inspectors.
[email protected]5852edc1b2009-09-10 06:05:27155 bool client_stuck() const { return client_stuck_; }
156
157 sync_pb::ClientCommand* GetNextClientCommand();
158
[email protected]f753af62010-02-11 01:05:38159 const std::vector<syncable::Id>& committed_ids() const {
160 return committed_ids_;
161 }
162 const std::vector<sync_pb::CommitMessage*>& commit_messages() const {
[email protected]55779e62010-07-13 20:27:11163 return commit_messages_.get();
164 }
165 const std::vector<sync_pb::CommitResponse*>& commit_responses() const {
166 return commit_responses_.get();
[email protected]5852edc1b2009-09-10 06:05:27167 }
168 // Retrieve the last sent commit message.
169 const sync_pb::CommitMessage& last_sent_commit() const;
170
[email protected]55779e62010-07-13 20:27:11171 // Retrieve the last returned commit response.
172 const sync_pb::CommitResponse& last_commit_response() const;
173
[email protected]29574e62010-04-01 22:39:59174 // Retrieve the last request submitted to the server (regardless of type).
175 const sync_pb::ClientToServerMessage& last_request() const {
176 return last_request_;
177 }
178
[email protected]5852edc1b2009-09-10 06:05:27179 void set_conflict_all_commits(bool value) {
180 conflict_all_commits_ = value;
181 }
182 void set_next_new_id(int value) {
183 next_new_id_ = value;
184 }
185 void set_conflict_n_commits(int value) {
186 conflict_n_commits_ = value;
187 }
188
[email protected]3273dce2010-01-27 16:08:08189 void set_use_legacy_bookmarks_protocol(bool value) {
190 use_legacy_bookmarks_protocol_ = value;
191 }
192
[email protected]ca318f92011-01-25 22:49:02193 void set_store_birthday(std::string new_birthday) {
[email protected]ca2fcc22010-09-07 23:52:04194 // Multiple threads can set store_birthday_ in our tests, need to lock it to
195 // ensure atomic read/writes and avoid race conditions.
[email protected]20305ec2011-01-21 04:55:52196 base::AutoLock lock(store_birthday_lock_);
[email protected]7f8abea2010-07-14 01:31:36197 store_birthday_ = new_birthday;
198 }
199
[email protected]f753af62010-02-11 01:05:38200 // Retrieve the number of GetUpdates requests that the mock server has
201 // seen since the last time this function was called. Can be used to
202 // verify that a GetUpdates actually did or did not happen after running
203 // the syncer.
204 int GetAndClearNumGetUpdatesRequests() {
205 int result = num_get_updates_requests_;
206 num_get_updates_requests_ = 0;
207 return result;
208 }
209
210 // Expect that GetUpdates will request exactly the types indicated in
211 // the bitset.
212 void ExpectGetUpdatesRequestTypes(
213 std::bitset<syncable::MODEL_TYPE_COUNT> expected_filter) {
214 expected_filter_ = expected_filter;
215 }
216
[email protected]0a9c9062011-02-28 23:54:58217 void ExpectGetUpdatesRequestPayloads(
218 const browser_sync::sessions::TypePayloadMap& payloads) {
219 expected_payloads_ = payloads;
220 }
221
[email protected]894f4a02010-06-29 20:37:06222 void SetServerReachable();
223
224 void SetServerNotReachable();
225
[email protected]ca2fcc22010-09-07 23:52:04226 // Const necessary to avoid any hidden copy-on-write issues that would break
227 // in multithreaded scenarios (see |set_store_birthday|).
228 const std::string& store_birthday() {
[email protected]20305ec2011-01-21 04:55:52229 base::AutoLock lock(store_birthday_lock_);
[email protected]ca2fcc22010-09-07 23:52:04230 return store_birthday_;
231 }
[email protected]55779e62010-07-13 20:27:11232
233 // Locate the most recent update message for purpose of alteration.
234 sync_pb::SyncEntity* GetMutableLastUpdate();
235
[email protected]5852edc1b2009-09-10 06:05:27236 private:
237 sync_pb::SyncEntity* AddUpdateFull(syncable::Id id, syncable::Id parentid,
[email protected]ca318f92011-01-25 22:49:02238 std::string name, int64 version,
[email protected]5852edc1b2009-09-10 06:05:27239 int64 sync_ts,
240 bool is_dir);
[email protected]ca318f92011-01-25 22:49:02241 sync_pb::SyncEntity* AddUpdateFull(std::string id,
242 std::string parentid, std::string name,
[email protected]5852edc1b2009-09-10 06:05:27243 int64 version, int64 sync_ts,
244 bool is_dir);
245 // Functions to handle the various types of server request.
246 void ProcessGetUpdates(sync_pb::ClientToServerMessage* csm,
247 sync_pb::ClientToServerResponse* response);
[email protected]3c479922009-10-07 23:57:20248 void ProcessAuthenticate(sync_pb::ClientToServerMessage* csm,
249 sync_pb::ClientToServerResponse* response,
250 const std::string& auth_token);
[email protected]5852edc1b2009-09-10 06:05:27251 void ProcessCommit(sync_pb::ClientToServerMessage* csm,
252 sync_pb::ClientToServerResponse* response_buffer);
[email protected]1ce52aa2010-09-08 19:07:36253 void ProcessClearData(sync_pb::ClientToServerMessage* csm,
254 sync_pb::ClientToServerResponse* response);
[email protected]3273dce2010-01-27 16:08:08255 void AddDefaultBookmarkData(sync_pb::SyncEntity* entity, bool is_folder);
256
[email protected]5852edc1b2009-09-10 06:05:27257 // Determine if one entry in a commit should be rejected with a conflict.
258 bool ShouldConflictThisCommit();
259
[email protected]5af80fa2009-09-14 18:34:07260 // Generate a numeric position_in_parent value. We use a global counter
261 // that only decreases; this simulates new objects always being added to the
262 // front of the ordering.
[email protected]5852edc1b2009-09-10 06:05:27263 int64 GeneratePositionInParent() {
264 return next_position_in_parent_--;
265 }
266
[email protected]a26691932011-01-13 21:00:08267 // Get a mutable update response which will eventually be returned to the
268 // client.
269 sync_pb::GetUpdatesResponse* GetUpdateResponse();
270 void ApplyToken();
271
272 // Determine whether an progress marker array (like that sent in
273 // GetUpdates.from_progress_marker) indicates that a particular ModelType
[email protected]f753af62010-02-11 01:05:38274 // should be included.
[email protected]a26691932011-01-13 21:00:08275 bool IsModelTypePresentInSpecifics(
276 const google::protobuf::RepeatedPtrField<
277 sync_pb::DataTypeProgressMarker>& filter,
[email protected]f753af62010-02-11 01:05:38278 syncable::ModelType value);
279
[email protected]0a9c9062011-02-28 23:54:58280 sync_pb::DataTypeProgressMarker const* GetProgressMarkerForType(
281 const google::protobuf::RepeatedPtrField<
282 sync_pb::DataTypeProgressMarker>& filter,
283 syncable::ModelType value);
284
[email protected]5852edc1b2009-09-10 06:05:27285 // All IDs that have been committed.
[email protected]f753af62010-02-11 01:05:38286 std::vector<syncable::Id> committed_ids_;
[email protected]5852edc1b2009-09-10 06:05:27287
288 // Control of when/if we return conflicts.
289 bool conflict_all_commits_;
290 int conflict_n_commits_;
291
[email protected]55779e62010-07-13 20:27:11292 // Commit messages we've sent, and responses we've returned.
293 ScopedVector<sync_pb::CommitMessage> commit_messages_;
294 ScopedVector<sync_pb::CommitResponse> commit_responses_;
[email protected]5852edc1b2009-09-10 06:05:27295
296 // The next id the mock will return to a commit.
297 int next_new_id_;
298
299 // The store birthday we send to the client.
[email protected]ca318f92011-01-25 22:49:02300 std::string store_birthday_;
[email protected]20305ec2011-01-21 04:55:52301 base::Lock store_birthday_lock_;
[email protected]5852edc1b2009-09-10 06:05:27302 bool store_birthday_sent_;
303 bool client_stuck_;
[email protected]ca318f92011-01-25 22:49:02304 std::string commit_time_rename_prepended_string_;
[email protected]5852edc1b2009-09-10 06:05:27305
[email protected]5af80fa2009-09-14 18:34:07306 // Fail on the next call to PostBufferToPath().
[email protected]5852edc1b2009-09-10 06:05:27307 bool fail_next_postbuffer_;
308
309 // Our directory.
[email protected]2186fec42009-09-25 23:09:36310 syncable::DirectoryManager* directory_manager_;
[email protected]6d201be2009-11-13 19:40:59311 std::string directory_name_;
[email protected]5852edc1b2009-09-10 06:05:27312
313 // The updates we'll return to the next request.
[email protected]a26691932011-01-13 21:00:08314 std::list<sync_pb::GetUpdatesResponse> update_queue_;
[email protected]a66e33a22010-07-16 14:06:11315 scoped_ptr<Callback0::Type> mid_commit_callback_;
[email protected]2186fec42009-09-25 23:09:36316 MidCommitObserver* mid_commit_observer_;
[email protected]5852edc1b2009-09-10 06:05:27317
[email protected]1ce52aa2010-09-08 19:07:36318 // The clear data response we'll return in the next response
[email protected]689a7a72010-09-29 19:56:41319 sync_pb::ClientToServerResponse::ErrorType
320 clear_user_data_response_errortype_;
[email protected]1ce52aa2010-09-08 19:07:36321
[email protected]3c479922009-10-07 23:57:20322 // The AUTHENTICATE response we'll return for auth requests.
323 sync_pb::AuthenticateResponse auth_response_;
324 // What we use to determine if we should return SUCCESS or BAD_AUTH_TOKEN.
325 std::string valid_auth_token_;
326
[email protected]1b4ec9a2009-10-16 01:31:55327 // Whether we are faking a server mandating clients to throttle requests.
[email protected]18a797052010-02-09 21:48:04328 // Protected by |response_code_override_lock_|.
[email protected]1b4ec9a2009-10-16 01:31:55329 bool throttling_;
[email protected]18a797052010-02-09 21:48:04330
331 // Whether we are failing all requests by returning
332 // ClientToServerResponse::AUTH_INVALID.
333 // Protected by |response_code_override_lock_|.
334 bool fail_with_auth_invalid_;
335
[email protected]20305ec2011-01-21 04:55:52336 base::Lock response_code_override_lock_;
[email protected]1b4ec9a2009-10-16 01:31:55337
338 // True if we are only accepting GetUpdatesCallerInfo::PERIODIC requests.
339 bool fail_non_periodic_get_updates_;
340
[email protected]5852edc1b2009-09-10 06:05:27341 scoped_ptr<sync_pb::ClientCommand> client_command_;
342
343 // The next value to use for the position_in_parent property.
344 int64 next_position_in_parent_;
345
[email protected]3273dce2010-01-27 16:08:08346 // The default is to use the newer sync_pb::BookmarkSpecifics-style protocol.
347 // If this option is set to true, then the MockConnectionManager will
348 // use the older sync_pb::SyncEntity_BookmarkData-style protocol.
349 bool use_legacy_bookmarks_protocol_;
350
[email protected]f753af62010-02-11 01:05:38351 std::bitset<syncable::MODEL_TYPE_COUNT> expected_filter_;
352
[email protected]0a9c9062011-02-28 23:54:58353 browser_sync::sessions::TypePayloadMap expected_payloads_;
354
[email protected]f753af62010-02-11 01:05:38355 int num_get_updates_requests_;
356
[email protected]a26691932011-01-13 21:00:08357 std::string next_token_;
358
[email protected]29574e62010-04-01 22:39:59359 sync_pb::ClientToServerMessage last_request_;
360
[email protected]5852edc1b2009-09-10 06:05:27361 DISALLOW_COPY_AND_ASSIGN(MockConnectionManager);
362};
363
[email protected]d07f0c82010-06-25 00:10:32364#endif // CHROME_TEST_SYNC_ENGINE_MOCK_CONNECTION_MANAGER_H_