blob: aaade7e66686aa22a3ea03d0cfaf9026c6b65432 [file] [log] [blame]
[email protected]5852edc1b2009-09-10 06:05:271// 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]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
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]f753af62010-02-11 01:05:3817#include "chrome/browser/sync/syncable/model_type.h"
[email protected]82b1cd9fc2009-11-20 01:41:3418#include "chrome/browser/sync/util/closure.h"
[email protected]5852edc1b2009-09-10 06:05:2719
[email protected]5852edc1b2009-09-10 06:05:2720namespace syncable {
21class DirectoryManager;
22class ScopedDirLookup;
23}
24namespace browser_sync {
25struct HttpResponse;
26}
27
[email protected]5af80fa2009-09-14 18:34:0728class MockConnectionManager : public browser_sync::ServerConnectionManager {
[email protected]5852edc1b2009-09-10 06:05:2729 public:
[email protected]2186fec42009-09-25 23:09:3630 class MidCommitObserver {
31 public:
32 virtual void Observe() = 0;
33 };
[email protected]5852edc1b2009-09-10 06:05:2734
[email protected]6d201be2009-11-13 19:40:5935 MockConnectionManager(syncable::DirectoryManager* dirmgr,
36 const std::string& name);
[email protected]5852edc1b2009-09-10 06:05:2737 virtual ~MockConnectionManager();
38
39 // Overridden ServerConnectionManager functions.
40 virtual bool PostBufferToPath(const PostBufferParams*,
[email protected]18a797052010-02-09 21:48:0441 const string& path,
42 const string& auth_token,
43 browser_sync::ScopedServerStatusWatcher* watcher);
[email protected]5852edc1b2009-09-10 06:05:2744
45 virtual bool IsServerReachable();
46 virtual bool IsUserAuthenticated();
47
48 // Control of commit response.
[email protected]82b1cd9fc2009-11-20 01:41:3449 void SetMidCommitCallback(Closure* callback);
[email protected]2186fec42009-09-25 23:09:3650 void SetMidCommitObserver(MidCommitObserver* observer);
[email protected]5852edc1b2009-09-10 06:05:2751
[email protected]5af80fa2009-09-14 18:34:0752 // 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]5852edc1b2009-09-10 06:05:2754 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]6d201be2009-11-13 19:40:5998 std::string* xattr_key,
[email protected]5852edc1b2009-09-10 06:05:2799 syncable::Blob* xattr_value,
100 int xattr_count);
[email protected]3273dce2010-01-27 16:08:08101
[email protected]5852edc1b2009-09-10 06:05:27102 void SetLastUpdateDeleted();
[email protected]7377db592010-02-09 22:00:06103 void SetLastUpdateServerTag(const string& tag);
104 void SetLastUpdateClientTag(const string& tag);
[email protected]5852edc1b2009-09-10 06:05:27105 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]b29384222009-10-23 19:48:23109 void SetChangesRemaining(int64 timestamp);
[email protected]5852edc1b2009-09-10 06:05:27110
[email protected]3c479922009-10-07 23:57:20111 // 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]5852edc1b2009-09-10 06:05:27117 void FailNextPostBufferToPathCall() { fail_next_postbuffer_ = true; }
118
[email protected]1b4ec9a2009-10-16 01:31:55119 // A visitor class to allow a test to change some monitoring state atomically
[email protected]18a797052010-02-09 21:48:04120 // 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]1b4ec9a2009-10-16 01:31:55124 public:
[email protected]18a797052010-02-09 21:48:04125 // Called with response_code_override_lock_ acquired.
126 virtual void OnOverrideComplete() = 0;
[email protected]1b4ec9a2009-10-16 01:31:55127 };
[email protected]18a797052010-02-09 21:48:04128 void ThrottleNextRequest(ResponseCodeOverrideRequestor* visitor);
129 void FailWithAuthInvalid(ResponseCodeOverrideRequestor* visitor);
130 void StopFailingWithAuthInvalid(ResponseCodeOverrideRequestor* visitor);
[email protected]1b4ec9a2009-10-16 01:31:55131 void FailNonPeriodicGetUpdates() { fail_non_periodic_get_updates_ = true; }
132
[email protected]5af80fa2009-09-14 18:34:07133 // Simple inspectors.
[email protected]5852edc1b2009-09-10 06:05:27134 bool client_stuck() const { return client_stuck_; }
135
136 sync_pb::ClientCommand* GetNextClientCommand();
137
[email protected]f753af62010-02-11 01:05:38138 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]5852edc1b2009-09-10 06:05:27142 return commit_messages_;
143 }
144 // Retrieve the last sent commit message.
145 const sync_pb::CommitMessage& last_sent_commit() const;
146
[email protected]29574e62010-04-01 22:39:59147 // 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]5852edc1b2009-09-10 06:05:27152 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]3273dce2010-01-27 16:08:08162 void set_use_legacy_bookmarks_protocol(bool value) {
163 use_legacy_bookmarks_protocol_ = value;
164 }
165
[email protected]f753af62010-02-11 01:05:38166 // 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]5852edc1b2009-09-10 06:05:27183 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]3c479922009-10-07 23:57:20194 void ProcessAuthenticate(sync_pb::ClientToServerMessage* csm,
195 sync_pb::ClientToServerResponse* response,
196 const std::string& auth_token);
[email protected]5852edc1b2009-09-10 06:05:27197 void ProcessCommit(sync_pb::ClientToServerMessage* csm,
198 sync_pb::ClientToServerResponse* response_buffer);
[email protected]3273dce2010-01-27 16:08:08199
200 void AddDefaultBookmarkData(sync_pb::SyncEntity* entity, bool is_folder);
201
[email protected]5852edc1b2009-09-10 06:05:27202 // 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]5af80fa2009-09-14 18:34:07208 // 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]5852edc1b2009-09-10 06:05:27211 int64 GeneratePositionInParent() {
212 return next_position_in_parent_--;
213 }
214
[email protected]f753af62010-02-11 01:05:38215 // 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]5852edc1b2009-09-10 06:05:27221 // All IDs that have been committed.
[email protected]f753af62010-02-11 01:05:38222 std::vector<syncable::Id> committed_ids_;
[email protected]5852edc1b2009-09-10 06:05:27223
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]f753af62010-02-11 01:05:38229 std::vector<sync_pb::CommitMessage*> commit_messages_;
[email protected]5852edc1b2009-09-10 06:05:27230
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]5af80fa2009-09-14 18:34:07240 // Fail on the next call to PostBufferToPath().
[email protected]5852edc1b2009-09-10 06:05:27241 bool fail_next_postbuffer_;
242
243 // Our directory.
[email protected]2186fec42009-09-25 23:09:36244 syncable::DirectoryManager* directory_manager_;
[email protected]6d201be2009-11-13 19:40:59245 std::string directory_name_;
[email protected]5852edc1b2009-09-10 06:05:27246
247 // The updates we'll return to the next request.
248 sync_pb::GetUpdatesResponse updates_;
[email protected]82b1cd9fc2009-11-20 01:41:34249 Closure* mid_commit_callback_;
[email protected]2186fec42009-09-25 23:09:36250 MidCommitObserver* mid_commit_observer_;
[email protected]5852edc1b2009-09-10 06:05:27251
[email protected]3c479922009-10-07 23:57:20252 // 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]1b4ec9a2009-10-16 01:31:55257 // Whether we are faking a server mandating clients to throttle requests.
[email protected]18a797052010-02-09 21:48:04258 // Protected by |response_code_override_lock_|.
[email protected]1b4ec9a2009-10-16 01:31:55259 bool throttling_;
[email protected]18a797052010-02-09 21:48:04260
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]1b4ec9a2009-10-16 01:31:55267
268 // True if we are only accepting GetUpdatesCallerInfo::PERIODIC requests.
269 bool fail_non_periodic_get_updates_;
270
[email protected]5852edc1b2009-09-10 06:05:27271 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]3273dce2010-01-27 16:08:08276 // 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]f753af62010-02-11 01:05:38281 std::bitset<syncable::MODEL_TYPE_COUNT> expected_filter_;
282
283 int num_get_updates_requests_;
284
[email protected]29574e62010-04-01 22:39:59285 sync_pb::ClientToServerMessage last_request_;
286
[email protected]5852edc1b2009-09-10 06:05:27287 DISALLOW_COPY_AND_ASSIGN(MockConnectionManager);
288};
289
[email protected]d07f0c82010-06-25 00:10:32290#endif // CHROME_TEST_SYNC_ENGINE_MOCK_CONNECTION_MANAGER_H_