blob: 1c19a5bf5150630e0cf99f27ffbca5b7bb00008b [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
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
17using std::string;
18using std::vector;
19
20namespace 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:
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]2186fec42009-09-25 23:09:3634 class MidCommitObserver {
35 public:
36 virtual void Observe() = 0;
37 };
[email protected]5852edc1b2009-09-10 06:05:2738
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]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();
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]5af80fa2009-09-14 18:34:07103 // Prepare to add checksums.
[email protected]5852edc1b2009-09-10 06:05:27104 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]3c479922009-10-07 23:57:20112 // 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]5852edc1b2009-09-10 06:05:27118 void FailNextPostBufferToPathCall() { fail_next_postbuffer_ = true; }
119
[email protected]5af80fa2009-09-14 18:34:07120 // Simple inspectors.
[email protected]5852edc1b2009-09-10 06:05:27121 bool client_stuck() const { return client_stuck_; }
122
123 sync_pb::ClientCommand* GetNextClientCommand();
124
125 const vector<syncable::Id>& committed_ids() const { return committed_ids_; }
126 const vector<sync_pb::CommitMessage*>& commit_messages() const {
127 return commit_messages_;
128 }
129 // Retrieve the last sent commit message.
130 const sync_pb::CommitMessage& last_sent_commit() const;
131
132 void set_conflict_all_commits(bool value) {
133 conflict_all_commits_ = value;
134 }
135 void set_next_new_id(int value) {
136 next_new_id_ = value;
137 }
138 void set_conflict_n_commits(int value) {
139 conflict_n_commits_ = value;
140 }
141
142 private:
143 sync_pb::SyncEntity* AddUpdateFull(syncable::Id id, syncable::Id parentid,
144 string name, int64 version,
145 int64 sync_ts,
146 bool is_dir);
147 sync_pb::SyncEntity* AddUpdateFull(string id, string parentid, string name,
148 int64 version, int64 sync_ts,
149 bool is_dir);
150 // Functions to handle the various types of server request.
151 void ProcessGetUpdates(sync_pb::ClientToServerMessage* csm,
152 sync_pb::ClientToServerResponse* response);
[email protected]3c479922009-10-07 23:57:20153 void ProcessAuthenticate(sync_pb::ClientToServerMessage* csm,
154 sync_pb::ClientToServerResponse* response,
155 const std::string& auth_token);
[email protected]5852edc1b2009-09-10 06:05:27156 void ProcessCommit(sync_pb::ClientToServerMessage* csm,
157 sync_pb::ClientToServerResponse* response_buffer);
158 // Locate the most recent update message for purpose of alteration.
159 sync_pb::SyncEntity* GetMutableLastUpdate();
160
161 // Determine if one entry in a commit should be rejected with a conflict.
162 bool ShouldConflictThisCommit();
163
[email protected]5af80fa2009-09-14 18:34:07164 // Generate a numeric position_in_parent value. We use a global counter
165 // that only decreases; this simulates new objects always being added to the
166 // front of the ordering.
[email protected]5852edc1b2009-09-10 06:05:27167 int64 GeneratePositionInParent() {
168 return next_position_in_parent_--;
169 }
170
171 // All IDs that have been committed.
172 vector<syncable::Id> committed_ids_;
173
174 // Control of when/if we return conflicts.
175 bool conflict_all_commits_;
176 int conflict_n_commits_;
177
178 // Commit messages we've sent
179 vector<sync_pb::CommitMessage*> commit_messages_;
180
181 // The next id the mock will return to a commit.
182 int next_new_id_;
183
184 // The store birthday we send to the client.
185 string store_birthday_;
186 bool store_birthday_sent_;
187 bool client_stuck_;
188 string commit_time_rename_prepended_string_;
189
[email protected]5af80fa2009-09-14 18:34:07190 // Fail on the next call to PostBufferToPath().
[email protected]5852edc1b2009-09-10 06:05:27191 bool fail_next_postbuffer_;
192
193 // Our directory.
[email protected]2186fec42009-09-25 23:09:36194 syncable::DirectoryManager* directory_manager_;
195 PathString directory_name_;
[email protected]5852edc1b2009-09-10 06:05:27196
197 // The updates we'll return to the next request.
198 sync_pb::GetUpdatesResponse updates_;
199 TestCallbackFunction mid_commit_callback_function_;
[email protected]2186fec42009-09-25 23:09:36200 MidCommitObserver* mid_commit_observer_;
[email protected]5852edc1b2009-09-10 06:05:27201
[email protected]3c479922009-10-07 23:57:20202 // The AUTHENTICATE response we'll return for auth requests.
203 sync_pb::AuthenticateResponse auth_response_;
204 // What we use to determine if we should return SUCCESS or BAD_AUTH_TOKEN.
205 std::string valid_auth_token_;
206
[email protected]5852edc1b2009-09-10 06:05:27207 scoped_ptr<sync_pb::ClientCommand> client_command_;
208
209 // The next value to use for the position_in_parent property.
210 int64 next_position_in_parent_;
211
212 DISALLOW_COPY_AND_ASSIGN(MockConnectionManager);
213};
214
215#endif // CHROME_TEST_SYNC_ENGINE_MOCK_SERVER_CONNECTION_H_