blob: 685624fd140ce2378a374e44fc24df28368a0b16 [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]5852edc1b2009-09-10 06:05:2712#include <string>
13#include <vector>
14
[email protected]a66e33a22010-07-16 14:06:1115#include "base/callback.h"
[email protected]55779e62010-07-13 20:27:1116#include "base/scoped_vector.h"
[email protected]5852edc1b2009-09-10 06:05:2717#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]f753af62010-02-11 01:05:3820#include "chrome/browser/sync/syncable/model_type.h"
[email protected]5852edc1b2009-09-10 06:05:2721
[email protected]5852edc1b2009-09-10 06:05:2722namespace syncable {
23class DirectoryManager;
24class ScopedDirLookup;
25}
26namespace browser_sync {
27struct HttpResponse;
28}
29
[email protected]5af80fa2009-09-14 18:34:0730class MockConnectionManager : public browser_sync::ServerConnectionManager {
[email protected]5852edc1b2009-09-10 06:05:2731 public:
[email protected]2186fec42009-09-25 23:09:3632 class MidCommitObserver {
33 public:
34 virtual void Observe() = 0;
35 };
[email protected]5852edc1b2009-09-10 06:05:2736
[email protected]6d201be2009-11-13 19:40:5937 MockConnectionManager(syncable::DirectoryManager* dirmgr,
38 const std::string& name);
[email protected]5852edc1b2009-09-10 06:05:2739 virtual ~MockConnectionManager();
40
41 // Overridden ServerConnectionManager functions.
42 virtual bool PostBufferToPath(const PostBufferParams*,
[email protected]18a797052010-02-09 21:48:0443 const string& path,
44 const string& auth_token,
45 browser_sync::ScopedServerStatusWatcher* watcher);
[email protected]5852edc1b2009-09-10 06:05:2746
47 virtual bool IsServerReachable();
48 virtual bool IsUserAuthenticated();
49
50 // Control of commit response.
[email protected]a66e33a22010-07-16 14:06:1151 void SetMidCommitCallback(Callback0::Type* callback);
[email protected]2186fec42009-09-25 23:09:3652 void SetMidCommitObserver(MidCommitObserver* observer);
[email protected]5852edc1b2009-09-10 06:05:2753
[email protected]5af80fa2009-09-14 18:34:0754 // 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]5852edc1b2009-09-10 06:05:2756 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]55779e62010-07-13 20:27:1162
[email protected]5852edc1b2009-09-10 06:05:2763 // 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]fca88252010-06-30 03:57:4467 // sync, using SetLastXXX() methods and/or GetMutableLastUpdate().
[email protected]5852edc1b2009-09-10 06:05:2768 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]55779e62010-07-13 20:27:11100
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]5852edc1b2009-09-10 06:05:27111 void SetLastUpdateDeleted();
[email protected]7377db592010-02-09 22:00:06112 void SetLastUpdateServerTag(const string& tag);
113 void SetLastUpdateClientTag(const string& tag);
[email protected]5852edc1b2009-09-10 06:05:27114 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]b29384222009-10-23 19:48:23118 void SetChangesRemaining(int64 timestamp);
[email protected]5852edc1b2009-09-10 06:05:27119
[email protected]3c479922009-10-07 23:57:20120 // 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]5852edc1b2009-09-10 06:05:27126 void FailNextPostBufferToPathCall() { fail_next_postbuffer_ = true; }
127
[email protected]1b4ec9a2009-10-16 01:31:55128 // A visitor class to allow a test to change some monitoring state atomically
[email protected]18a797052010-02-09 21:48:04129 // 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]1b4ec9a2009-10-16 01:31:55133 public:
[email protected]18a797052010-02-09 21:48:04134 // Called with response_code_override_lock_ acquired.
135 virtual void OnOverrideComplete() = 0;
[email protected]1b4ec9a2009-10-16 01:31:55136 };
[email protected]18a797052010-02-09 21:48:04137 void ThrottleNextRequest(ResponseCodeOverrideRequestor* visitor);
138 void FailWithAuthInvalid(ResponseCodeOverrideRequestor* visitor);
139 void StopFailingWithAuthInvalid(ResponseCodeOverrideRequestor* visitor);
[email protected]1b4ec9a2009-10-16 01:31:55140 void FailNonPeriodicGetUpdates() { fail_non_periodic_get_updates_ = true; }
141
[email protected]5af80fa2009-09-14 18:34:07142 // Simple inspectors.
[email protected]5852edc1b2009-09-10 06:05:27143 bool client_stuck() const { return client_stuck_; }
144
145 sync_pb::ClientCommand* GetNextClientCommand();
146
[email protected]f753af62010-02-11 01:05:38147 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]55779e62010-07-13 20:27:11151 return commit_messages_.get();
152 }
153 const std::vector<sync_pb::CommitResponse*>& commit_responses() const {
154 return commit_responses_.get();
[email protected]5852edc1b2009-09-10 06:05:27155 }
156 // Retrieve the last sent commit message.
157 const sync_pb::CommitMessage& last_sent_commit() const;
158
[email protected]55779e62010-07-13 20:27:11159 // Retrieve the last returned commit response.
160 const sync_pb::CommitResponse& last_commit_response() const;
161
[email protected]29574e62010-04-01 22:39:59162 // 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]5852edc1b2009-09-10 06:05:27167 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]3273dce2010-01-27 16:08:08177 void set_use_legacy_bookmarks_protocol(bool value) {
178 use_legacy_bookmarks_protocol_ = value;
179 }
180
[email protected]7f8abea2010-07-14 01:31:36181 void set_store_birthday(string new_birthday) {
182 store_birthday_ = new_birthday;
183 }
184
[email protected]f753af62010-02-11 01:05:38185 // 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]894f4a02010-06-29 20:37:06202 void SetServerReachable();
203
204 void SetServerNotReachable();
205
[email protected]55779e62010-07-13 20:27:11206 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]5852edc1b2009-09-10 06:05:27211 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]3c479922009-10-07 23:57:20222 void ProcessAuthenticate(sync_pb::ClientToServerMessage* csm,
223 sync_pb::ClientToServerResponse* response,
224 const std::string& auth_token);
[email protected]5852edc1b2009-09-10 06:05:27225 void ProcessCommit(sync_pb::ClientToServerMessage* csm,
226 sync_pb::ClientToServerResponse* response_buffer);
[email protected]3273dce2010-01-27 16:08:08227
228 void AddDefaultBookmarkData(sync_pb::SyncEntity* entity, bool is_folder);
229
[email protected]5852edc1b2009-09-10 06:05:27230 // Determine if one entry in a commit should be rejected with a conflict.
231 bool ShouldConflictThisCommit();
232
[email protected]5af80fa2009-09-14 18:34:07233 // 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]5852edc1b2009-09-10 06:05:27236 int64 GeneratePositionInParent() {
237 return next_position_in_parent_--;
238 }
239
[email protected]f753af62010-02-11 01:05:38240 // 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]5852edc1b2009-09-10 06:05:27246 // All IDs that have been committed.
[email protected]f753af62010-02-11 01:05:38247 std::vector<syncable::Id> committed_ids_;
[email protected]5852edc1b2009-09-10 06:05:27248
249 // Control of when/if we return conflicts.
250 bool conflict_all_commits_;
251 int conflict_n_commits_;
252
[email protected]55779e62010-07-13 20:27:11253 // 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]5852edc1b2009-09-10 06:05:27256
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]5af80fa2009-09-14 18:34:07266 // Fail on the next call to PostBufferToPath().
[email protected]5852edc1b2009-09-10 06:05:27267 bool fail_next_postbuffer_;
268
269 // Our directory.
[email protected]2186fec42009-09-25 23:09:36270 syncable::DirectoryManager* directory_manager_;
[email protected]6d201be2009-11-13 19:40:59271 std::string directory_name_;
[email protected]5852edc1b2009-09-10 06:05:27272
273 // The updates we'll return to the next request.
274 sync_pb::GetUpdatesResponse updates_;
[email protected]a66e33a22010-07-16 14:06:11275 scoped_ptr<Callback0::Type> mid_commit_callback_;
[email protected]2186fec42009-09-25 23:09:36276 MidCommitObserver* mid_commit_observer_;
[email protected]5852edc1b2009-09-10 06:05:27277
[email protected]3c479922009-10-07 23:57:20278 // 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]1b4ec9a2009-10-16 01:31:55283 // Whether we are faking a server mandating clients to throttle requests.
[email protected]18a797052010-02-09 21:48:04284 // Protected by |response_code_override_lock_|.
[email protected]1b4ec9a2009-10-16 01:31:55285 bool throttling_;
[email protected]18a797052010-02-09 21:48:04286
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]1b4ec9a2009-10-16 01:31:55293
294 // True if we are only accepting GetUpdatesCallerInfo::PERIODIC requests.
295 bool fail_non_periodic_get_updates_;
296
[email protected]5852edc1b2009-09-10 06:05:27297 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]3273dce2010-01-27 16:08:08302 // 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]f753af62010-02-11 01:05:38307 std::bitset<syncable::MODEL_TYPE_COUNT> expected_filter_;
308
309 int num_get_updates_requests_;
310
[email protected]29574e62010-04-01 22:39:59311 sync_pb::ClientToServerMessage last_request_;
312
[email protected]5852edc1b2009-09-10 06:05:27313 DISALLOW_COPY_AND_ASSIGN(MockConnectionManager);
314};
315
[email protected]d07f0c82010-06-25 00:10:32316#endif // CHROME_TEST_SYNC_ENGINE_MOCK_CONNECTION_MANAGER_H_