Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(808)

Side by Side Diff: components/sync/test/fake_server/fake_server.h

Issue 2887343002: wip 3 (Closed)
Patch Set: cleanup Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef COMPONENTS_SYNC_TEST_FAKE_SERVER_FAKE_SERVER_H_ 5 #ifndef COMPONENTS_SYNC_TEST_FAKE_SERVER_FAKE_SERVER_H_
6 #define COMPONENTS_SYNC_TEST_FAKE_SERVER_FAKE_SERVER_H_ 6 #define COMPONENTS_SYNC_TEST_FAKE_SERVER_FAKE_SERVER_H_
7 7
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <map> 10 #include <map>
11 #include <memory> 11 #include <memory>
12 #include <string> 12 #include <string>
13 #include <vector> 13 #include <vector>
14 14
15 #include "base/callback.h" 15 #include "base/callback.h"
16 #include "base/files/scoped_temp_dir.h"
16 #include "base/observer_list.h" 17 #include "base/observer_list.h"
17 #include "base/threading/thread_checker.h" 18 #include "base/threading/thread_checker.h"
18 #include "base/values.h" 19 #include "base/values.h"
19 #include "components/sync/base/model_type.h" 20 #include "components/sync/base/model_type.h"
21 #include "components/sync/engine_impl/loopback_server/loopback_server.h"
22 #include "components/sync/engine_impl/loopback_server/loopback_server_entity.h"
23 #include "components/sync/engine_impl/loopback_server/persistent_bookmark_entity .h"
24 #include "components/sync/engine_impl/loopback_server/persistent_tombstone_entit y.h"
25 #include "components/sync/engine_impl/loopback_server/persistent_unique_client_e ntity.h"
20 #include "components/sync/protocol/sync.pb.h" 26 #include "components/sync/protocol/sync.pb.h"
21 #include "components/sync/test/fake_server/fake_server_entity.h"
22 27
23 namespace fake_server { 28 namespace fake_server {
24 29
25 // A fake version of the Sync server used for testing. This class is not thread 30 // A fake version of the Sync server used for testing. This class is not thread
26 // safe. 31 // safe.
27 class FakeServer { 32 class FakeServer : public syncer::LoopbackServer::ObserverForTests {
28 public: 33 public:
29 class Observer { 34 class Observer {
30 public: 35 public:
31 virtual ~Observer() {} 36 virtual ~Observer() {}
32 37
33 // Called after FakeServer has processed a successful commit. The types 38 // Called after FakeServer has processed a successful commit. The types
34 // updated as part of the commit are passed in |committed_model_types|. 39 // updated as part of the commit are passed in |committed_model_types|.
35 virtual void OnCommit(const std::string& committer_id, 40 virtual void OnCommit(const std::string& committer_id,
36 syncer::ModelTypeSet committed_model_types) = 0; 41 syncer::ModelTypeSet committed_model_types) = 0;
37 }; 42 };
38 43
39 FakeServer(); 44 FakeServer();
40 virtual ~FakeServer(); 45 ~FakeServer() override;
41 46
42 // Handles a /command POST (with the given |request|) to the server. Three 47 // Handles a /command POST (with the given |request|) to the server. Three
43 // output arguments, |error_code|, |response_code|, and |response|, are used 48 // output arguments, |error_code|, |response_code|, and |response|, are used
44 // to pass data back to the caller. The command has failed if the value 49 // to pass data back to the caller. The command has failed if the value
45 // pointed to by |error_code| is nonzero. |completion_closure| will be called 50 // pointed to by |error_code| is nonzero. |completion_closure| will be called
46 // immediately before return. 51 // immediately before return.
47 void HandleCommand(const std::string& request, 52 void HandleCommand(const std::string& request,
48 const base::Closure& completion_closure, 53 const base::Closure& completion_closure,
49 int* error_code, 54 int* error_code,
50 int* response_code, 55 int* response_code,
51 std::string* response); 56 std::string* response);
52 57
53 // Helpers for fetching the last Commit or GetUpdates messages, respectively. 58 // Helpers for fetching the last Commit or GetUpdates messages, respectively.
54 // Returns true if the specified message existed, and false if no message has 59 // Returns true if the specified message existed, and false if no message has
55 // been received. 60 // been received.
56 bool GetLastCommitMessage(sync_pb::ClientToServerMessage* message); 61 bool GetLastCommitMessage(sync_pb::ClientToServerMessage* message);
57 bool GetLastGetUpdatesMessage(sync_pb::ClientToServerMessage* message); 62 bool GetLastGetUpdatesMessage(sync_pb::ClientToServerMessage* message);
58 63
59 // Creates a DicionaryValue representation of all entities present in the 64 // Creates a DicionaryValue representation of all entities present in the
60 // server. The dictionary keys are the strings generated by ModelTypeToString 65 // server. The dictionary keys are the strings generated by ModelTypeToString
61 // and the values are ListValues containing StringValue versions of entity 66 // and the values are ListValues containing StringValue versions of entity
62 // names. 67 // names.
63 std::unique_ptr<base::DictionaryValue> GetEntitiesAsDictionaryValue(); 68 std::unique_ptr<base::DictionaryValue> GetEntitiesAsDictionaryValue();
64 69
65 // Returns all entities stored by the server of the given |model_type|. 70 // Returns all entities stored by the server of the given |model_type|.
66 // This method returns SyncEntity protocol buffer objects (instead of 71 // This method returns SyncEntity protocol buffer objects (instead of
67 // FakeServerEntity) so that callers can inspect datatype-specific data 72 // LoopbackServerEntity) so that callers can inspect datatype-specific data
68 // (e.g., the URL of a session tab). 73 // (e.g., the URL of a session tab).
69 std::vector<sync_pb::SyncEntity> GetSyncEntitiesByModelType( 74 std::vector<sync_pb::SyncEntity> GetSyncEntitiesByModelType(
70 syncer::ModelType model_type); 75 syncer::ModelType model_type);
71 76
72 // Adds |entity| to the server's collection of entities. This method makes no 77 // Adds |entity| to the server's collection of entities. This method makes no
73 // guarantees that the added entity will result in successful server 78 // guarantees that the added entity will result in successful server
74 // operations. 79 // operations.
75 void InjectEntity(std::unique_ptr<FakeServerEntity> entity); 80 void InjectEntity(std::unique_ptr<syncer::LoopbackServerEntity> entity);
76 81
77 // Modifies the entity on the server with the given |id|. The entity's 82 // Modifies the entity on the server with the given |id|. The entity's
78 // EntitySpecifics are replaced with |updated_specifics| and its version is 83 // EntitySpecifics are replaced with |updated_specifics| and its version is
79 // updated. If the given |id| does not exist or the ModelType of 84 // updated. If the given |id| does not exist or the ModelType of
80 // |updated_specifics| does not match the entity, false is returned. 85 // |updated_specifics| does not match the entity, false is returned.
81 // Otherwise, true is returned to represent a successful modification. 86 // Otherwise, true is returned to represent a successful modification.
82 // 87 //
83 // This method sometimes updates entity data beyond EntitySpecifics. For 88 // This method sometimes updates entity data beyond EntitySpecifics. For
84 // example, in the case of a bookmark, changing the BookmarkSpecifics title 89 // example, in the case of a bookmark, changing the BookmarkSpecifics title
85 // field will modify the top-level entity's name field. 90 // field will modify the top-level entity's name field.
86 bool ModifyEntitySpecifics(const std::string& id, 91 bool ModifyEntitySpecifics(const std::string& id,
87 const sync_pb::EntitySpecifics& updated_specifics); 92 const sync_pb::EntitySpecifics& updated_specifics);
88 93
89 bool ModifyBookmarkEntity(const std::string& id,
90 const std::string& parent_id,
91 const sync_pb::EntitySpecifics& updated_specifics);
92
93 // Clears server data simulating a "dashboard stop and clear" and sets a new 94 // Clears server data simulating a "dashboard stop and clear" and sets a new
94 // store birthday. 95 // store birthday.
95 void ClearServerData(); 96 void ClearServerData();
96 97
97 // Puts the server in a state where it acts as if authentication has 98 // Puts the server in a state where it acts as if authentication has
98 // succeeded. 99 // succeeded.
99 void SetAuthenticated(); 100 void SetAuthenticated();
100 101
101 // Puts the server in a state where all commands will fail with an 102 // Puts the server in a state where all commands will fail with an
102 // authentication error. 103 // authentication error.
(...skipping 30 matching lines...) Expand all
133 // must be called if AddObserver was ever called with |observer|. 134 // must be called if AddObserver was ever called with |observer|.
134 void RemoveObserver(Observer* observer); 135 void RemoveObserver(Observer* observer);
135 136
136 // Undoes the effects of DisableNetwork. 137 // Undoes the effects of DisableNetwork.
137 void EnableNetwork(); 138 void EnableNetwork();
138 139
139 // Forces every request to fail in a way that simulates a network failure. 140 // Forces every request to fail in a way that simulates a network failure.
140 // This can be used to trigger exponential backoff in the client. 141 // This can be used to trigger exponential backoff in the client.
141 void DisableNetwork(); 142 void DisableNetwork();
142 143
143 // Returns the entity ID of the Bookmark Bar folder. 144 // Implement LoopbackServer::ObserverForTests:
144 std::string GetBookmarkBarFolderId() const; 145 void OnCommit(const std::string& committer_id,
146 syncer::ModelTypeSet committed_model_types) override;
145 147
146 // Returns the current FakeServer as a WeakPtr. 148 // Returns the current FakeServer as a WeakPtr.
147 base::WeakPtr<FakeServer> AsWeakPtr(); 149 base::WeakPtr<FakeServer> AsWeakPtr();
148 150
149 private: 151 private:
150 using EntityMap = std::map<std::string, std::unique_ptr<FakeServerEntity>>;
151
152 // Gets FakeServer ready for syncing.
153 void Init();
154
155 // Processes a GetUpdates call.
156 bool HandleGetUpdatesRequest(const sync_pb::GetUpdatesMessage& get_updates,
157 sync_pb::GetUpdatesResponse* response);
158
159 // Processes a Commit call.
160 bool HandleCommitRequest(const sync_pb::CommitMessage& message,
161 const std::string& invalidator_client_id,
162 sync_pb::CommitResponse* response);
163
164 // Creates and saves a permanent folder for Bookmarks (e.g., Bookmark Bar).
165 bool CreatePermanentBookmarkFolder(const std::string& server_tag,
166 const std::string& name);
167
168 // Inserts the default permanent items in |entities_|.
169 bool CreateDefaultPermanentItems();
170
171 // Saves a |entity| to |entities_|.
172 void SaveEntity(std::unique_ptr<FakeServerEntity> entity);
173
174 // Commits a client-side SyncEntity to the server as a FakeServerEntity.
175 // The client that sent the commit is identified via |client_guid|. The
176 // parent ID string present in |client_entity| should be ignored in favor
177 // of |parent_id|. If the commit is successful, the entity's server ID string
178 // is returned and a new FakeServerEntity is saved in |entities_|.
179 std::string CommitEntity(
180 const sync_pb::SyncEntity& client_entity,
181 sync_pb::CommitResponse_EntryResponse* entry_response,
182 const std::string& client_guid,
183 const std::string& parent_id);
184
185 // Populates |entry_response| based on the stored entity identified by
186 // |entity_id|. It is assumed that the entity identified by |entity_id| has
187 // already been stored using SaveEntity.
188 void BuildEntryResponseForSuccessfulCommit(
189 const std::string& entity_id,
190 sync_pb::CommitResponse_EntryResponse* entry_response);
191
192 // Determines whether the SyncEntity with id_string |id| is a child of an
193 // entity with id_string |potential_parent_id|.
194 bool IsChild(const std::string& id, const std::string& potential_parent_id);
195
196 // Creates and saves tombstones for all children of the entity with the given
197 // |id|. A tombstone is not created for the entity itself.
198 void DeleteChildren(const std::string& id);
199
200 // Returns whether a triggered error should be sent for the request. 152 // Returns whether a triggered error should be sent for the request.
201 bool ShouldSendTriggeredError() const; 153 bool ShouldSendTriggeredError() const;
202 154
203 // Updates the |entity| to a new version and increments the version counter
204 // that the server uses to assign versions.
205 void UpdateEntityVersion(FakeServerEntity* entity);
206
207 // Returns the store birthday.
208 std::string GetStoreBirthday() const;
209
210 // This is the last version number assigned to an entity. The next entity will
211 // have a version number of version_ + 1.
212 int64_t version_;
213
214 // The current store birthday value.
215 int64_t store_birthday_;
216
217 // Whether the server should act as if incoming connections are properly 155 // Whether the server should act as if incoming connections are properly
218 // authenticated. 156 // authenticated.
219 bool authenticated_; 157 bool authenticated_;
220 158
221 // All SyncEntity objects saved by the server. The key value is the entity's
222 // id string.
223 EntityMap entities_;
224
225 // All Keystore keys known to the server. 159 // All Keystore keys known to the server.
226 std::vector<std::string> keystore_keys_; 160 std::vector<std::string> keystore_keys_;
227 161
228 // Used as the error_code field of ClientToServerResponse on all responses 162 // Used as the error_code field of ClientToServerResponse on all responses
229 // except when |triggered_actionable_error_| is set. 163 // except when |triggered_actionable_error_| is set.
230 sync_pb::SyncEnums::ErrorType error_type_; 164 sync_pb::SyncEnums::ErrorType error_type_;
231 165
232 // Used as the error field of ClientToServerResponse when its pointer is not 166 // Used as the error field of ClientToServerResponse when its pointer is not
233 // null. 167 // null.
234 std::unique_ptr<sync_pb::ClientToServerResponse_Error> 168 std::unique_ptr<sync_pb::ClientToServerResponse_Error>
(...skipping 16 matching lines...) Expand all
251 // on every request. This is used to simulate a network failure on the client. 185 // on every request. This is used to simulate a network failure on the client.
252 bool network_enabled_; 186 bool network_enabled_;
253 187
254 // The last received client to server messages. 188 // The last received client to server messages.
255 sync_pb::ClientToServerMessage last_commit_message_; 189 sync_pb::ClientToServerMessage last_commit_message_;
256 sync_pb::ClientToServerMessage last_getupdates_message_; 190 sync_pb::ClientToServerMessage last_getupdates_message_;
257 191
258 // Used to verify that FakeServer is only used from one thread. 192 // Used to verify that FakeServer is only used from one thread.
259 base::ThreadChecker thread_checker_; 193 base::ThreadChecker thread_checker_;
260 194
195 std::unique_ptr<syncer::LoopbackServer> loopback_server_;
196 std::unique_ptr<base::ScopedTempDir> loopback_server_storage_;
197
261 // Creates WeakPtr versions of the current FakeServer. This must be the last 198 // Creates WeakPtr versions of the current FakeServer. This must be the last
262 // data member! 199 // data member!
263 base::WeakPtrFactory<FakeServer> weak_ptr_factory_; 200 base::WeakPtrFactory<FakeServer> weak_ptr_factory_;
264 }; 201 };
265 202
266 } // namespace fake_server 203 } // namespace fake_server
267 204
268 #endif // COMPONENTS_SYNC_TEST_FAKE_SERVER_FAKE_SERVER_H_ 205 #endif // COMPONENTS_SYNC_TEST_FAKE_SERVER_FAKE_SERVER_H_
OLDNEW
« no previous file with comments | « components/sync/test/fake_server/bookmark_entity_builder.cc ('k') | components/sync/test/fake_server/fake_server.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698