| OLD | NEW |
| 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" | |
| 17 #include "base/observer_list.h" | 16 #include "base/observer_list.h" |
| 18 #include "base/threading/thread_checker.h" | 17 #include "base/threading/thread_checker.h" |
| 19 #include "base/values.h" | 18 #include "base/values.h" |
| 20 #include "components/sync/base/model_type.h" | 19 #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" | |
| 26 #include "components/sync/protocol/sync.pb.h" | 20 #include "components/sync/protocol/sync.pb.h" |
| 21 #include "components/sync/test/fake_server/fake_server_entity.h" |
| 27 | 22 |
| 28 namespace fake_server { | 23 namespace fake_server { |
| 29 | 24 |
| 30 // A fake version of the Sync server used for testing. This class is not thread | 25 // A fake version of the Sync server used for testing. This class is not thread |
| 31 // safe. | 26 // safe. |
| 32 class FakeServer : public syncer::LoopbackServer::ObserverForTests { | 27 class FakeServer { |
| 33 public: | 28 public: |
| 34 class Observer { | 29 class Observer { |
| 35 public: | 30 public: |
| 36 virtual ~Observer() {} | 31 virtual ~Observer() {} |
| 37 | 32 |
| 38 // Called after FakeServer has processed a successful commit. The types | 33 // Called after FakeServer has processed a successful commit. The types |
| 39 // updated as part of the commit are passed in |committed_model_types|. | 34 // updated as part of the commit are passed in |committed_model_types|. |
| 40 virtual void OnCommit(const std::string& committer_id, | 35 virtual void OnCommit(const std::string& committer_id, |
| 41 syncer::ModelTypeSet committed_model_types) = 0; | 36 syncer::ModelTypeSet committed_model_types) = 0; |
| 42 }; | 37 }; |
| 43 | 38 |
| 44 FakeServer(); | 39 FakeServer(); |
| 45 ~FakeServer() override; | 40 virtual ~FakeServer(); |
| 46 | 41 |
| 47 // Handles a /command POST (with the given |request|) to the server. Three | 42 // Handles a /command POST (with the given |request|) to the server. Three |
| 48 // output arguments, |error_code|, |response_code|, and |response|, are used | 43 // output arguments, |error_code|, |response_code|, and |response|, are used |
| 49 // to pass data back to the caller. The command has failed if the value | 44 // to pass data back to the caller. The command has failed if the value |
| 50 // pointed to by |error_code| is nonzero. |completion_closure| will be called | 45 // pointed to by |error_code| is nonzero. |completion_closure| will be called |
| 51 // immediately before return. | 46 // immediately before return. |
| 52 void HandleCommand(const std::string& request, | 47 void HandleCommand(const std::string& request, |
| 53 const base::Closure& completion_closure, | 48 const base::Closure& completion_closure, |
| 54 int* error_code, | 49 int* error_code, |
| 55 int* response_code, | 50 int* response_code, |
| 56 std::string* response); | 51 std::string* response); |
| 57 | 52 |
| 58 // Helpers for fetching the last Commit or GetUpdates messages, respectively. | 53 // Helpers for fetching the last Commit or GetUpdates messages, respectively. |
| 59 // Returns true if the specified message existed, and false if no message has | 54 // Returns true if the specified message existed, and false if no message has |
| 60 // been received. | 55 // been received. |
| 61 bool GetLastCommitMessage(sync_pb::ClientToServerMessage* message); | 56 bool GetLastCommitMessage(sync_pb::ClientToServerMessage* message); |
| 62 bool GetLastGetUpdatesMessage(sync_pb::ClientToServerMessage* message); | 57 bool GetLastGetUpdatesMessage(sync_pb::ClientToServerMessage* message); |
| 63 | 58 |
| 64 // Creates a DicionaryValue representation of all entities present in the | 59 // Creates a DicionaryValue representation of all entities present in the |
| 65 // server. The dictionary keys are the strings generated by ModelTypeToString | 60 // server. The dictionary keys are the strings generated by ModelTypeToString |
| 66 // and the values are ListValues containing StringValue versions of entity | 61 // and the values are ListValues containing StringValue versions of entity |
| 67 // names. | 62 // names. |
| 68 std::unique_ptr<base::DictionaryValue> GetEntitiesAsDictionaryValue(); | 63 std::unique_ptr<base::DictionaryValue> GetEntitiesAsDictionaryValue(); |
| 69 | 64 |
| 70 // Returns all entities stored by the server of the given |model_type|. | 65 // Returns all entities stored by the server of the given |model_type|. |
| 71 // This method returns SyncEntity protocol buffer objects (instead of | 66 // This method returns SyncEntity protocol buffer objects (instead of |
| 72 // LoopbackServerEntity) so that callers can inspect datatype-specific data | 67 // FakeServerEntity) so that callers can inspect datatype-specific data |
| 73 // (e.g., the URL of a session tab). | 68 // (e.g., the URL of a session tab). |
| 74 std::vector<sync_pb::SyncEntity> GetSyncEntitiesByModelType( | 69 std::vector<sync_pb::SyncEntity> GetSyncEntitiesByModelType( |
| 75 syncer::ModelType model_type); | 70 syncer::ModelType model_type); |
| 76 | 71 |
| 77 // Adds |entity| to the server's collection of entities. This method makes no | 72 // Adds |entity| to the server's collection of entities. This method makes no |
| 78 // guarantees that the added entity will result in successful server | 73 // guarantees that the added entity will result in successful server |
| 79 // operations. | 74 // operations. |
| 80 void InjectEntity(std::unique_ptr<syncer::LoopbackServerEntity> entity); | 75 void InjectEntity(std::unique_ptr<FakeServerEntity> entity); |
| 81 | 76 |
| 82 // Modifies the entity on the server with the given |id|. The entity's | 77 // Modifies the entity on the server with the given |id|. The entity's |
| 83 // EntitySpecifics are replaced with |updated_specifics| and its version is | 78 // EntitySpecifics are replaced with |updated_specifics| and its version is |
| 84 // updated. If the given |id| does not exist or the ModelType of | 79 // updated. If the given |id| does not exist or the ModelType of |
| 85 // |updated_specifics| does not match the entity, false is returned. | 80 // |updated_specifics| does not match the entity, false is returned. |
| 86 // Otherwise, true is returned to represent a successful modification. | 81 // Otherwise, true is returned to represent a successful modification. |
| 87 // | 82 // |
| 88 // This method sometimes updates entity data beyond EntitySpecifics. For | 83 // This method sometimes updates entity data beyond EntitySpecifics. For |
| 89 // example, in the case of a bookmark, changing the BookmarkSpecifics title | 84 // example, in the case of a bookmark, changing the BookmarkSpecifics title |
| 90 // field will modify the top-level entity's name field. | 85 // field will modify the top-level entity's name field. |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 138 // must be called if AddObserver was ever called with |observer|. | 133 // must be called if AddObserver was ever called with |observer|. |
| 139 void RemoveObserver(Observer* observer); | 134 void RemoveObserver(Observer* observer); |
| 140 | 135 |
| 141 // Undoes the effects of DisableNetwork. | 136 // Undoes the effects of DisableNetwork. |
| 142 void EnableNetwork(); | 137 void EnableNetwork(); |
| 143 | 138 |
| 144 // Forces every request to fail in a way that simulates a network failure. | 139 // Forces every request to fail in a way that simulates a network failure. |
| 145 // This can be used to trigger exponential backoff in the client. | 140 // This can be used to trigger exponential backoff in the client. |
| 146 void DisableNetwork(); | 141 void DisableNetwork(); |
| 147 | 142 |
| 148 // Implement LoopbackServer::ObserverForTests: | 143 // Returns the entity ID of the Bookmark Bar folder. |
| 149 void OnCommit(const std::string& committer_id, | 144 std::string GetBookmarkBarFolderId() const; |
| 150 syncer::ModelTypeSet committed_model_types) override; | |
| 151 | 145 |
| 152 // Returns the current FakeServer as a WeakPtr. | 146 // Returns the current FakeServer as a WeakPtr. |
| 153 base::WeakPtr<FakeServer> AsWeakPtr(); | 147 base::WeakPtr<FakeServer> AsWeakPtr(); |
| 154 | 148 |
| 155 private: | 149 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 |
| 156 // Returns whether a triggered error should be sent for the request. | 200 // Returns whether a triggered error should be sent for the request. |
| 157 bool ShouldSendTriggeredError() const; | 201 bool ShouldSendTriggeredError() const; |
| 158 | 202 |
| 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 |
| 159 // Whether the server should act as if incoming connections are properly | 217 // Whether the server should act as if incoming connections are properly |
| 160 // authenticated. | 218 // authenticated. |
| 161 bool authenticated_; | 219 bool authenticated_; |
| 162 | 220 |
| 221 // All SyncEntity objects saved by the server. The key value is the entity's |
| 222 // id string. |
| 223 EntityMap entities_; |
| 224 |
| 163 // All Keystore keys known to the server. | 225 // All Keystore keys known to the server. |
| 164 std::vector<std::string> keystore_keys_; | 226 std::vector<std::string> keystore_keys_; |
| 165 | 227 |
| 166 // Used as the error_code field of ClientToServerResponse on all responses | 228 // Used as the error_code field of ClientToServerResponse on all responses |
| 167 // except when |triggered_actionable_error_| is set. | 229 // except when |triggered_actionable_error_| is set. |
| 168 sync_pb::SyncEnums::ErrorType error_type_; | 230 sync_pb::SyncEnums::ErrorType error_type_; |
| 169 | 231 |
| 170 // Used as the error field of ClientToServerResponse when its pointer is not | 232 // Used as the error field of ClientToServerResponse when its pointer is not |
| 171 // null. | 233 // null. |
| 172 std::unique_ptr<sync_pb::ClientToServerResponse_Error> | 234 std::unique_ptr<sync_pb::ClientToServerResponse_Error> |
| (...skipping 16 matching lines...) Expand all Loading... |
| 189 // on every request. This is used to simulate a network failure on the client. | 251 // on every request. This is used to simulate a network failure on the client. |
| 190 bool network_enabled_; | 252 bool network_enabled_; |
| 191 | 253 |
| 192 // The last received client to server messages. | 254 // The last received client to server messages. |
| 193 sync_pb::ClientToServerMessage last_commit_message_; | 255 sync_pb::ClientToServerMessage last_commit_message_; |
| 194 sync_pb::ClientToServerMessage last_getupdates_message_; | 256 sync_pb::ClientToServerMessage last_getupdates_message_; |
| 195 | 257 |
| 196 // Used to verify that FakeServer is only used from one thread. | 258 // Used to verify that FakeServer is only used from one thread. |
| 197 base::ThreadChecker thread_checker_; | 259 base::ThreadChecker thread_checker_; |
| 198 | 260 |
| 199 std::unique_ptr<syncer::LoopbackServer> loopback_server_; | |
| 200 std::unique_ptr<base::ScopedTempDir> loopback_server_storage_; | |
| 201 | |
| 202 // Creates WeakPtr versions of the current FakeServer. This must be the last | 261 // Creates WeakPtr versions of the current FakeServer. This must be the last |
| 203 // data member! | 262 // data member! |
| 204 base::WeakPtrFactory<FakeServer> weak_ptr_factory_; | 263 base::WeakPtrFactory<FakeServer> weak_ptr_factory_; |
| 205 }; | 264 }; |
| 206 | 265 |
| 207 } // namespace fake_server | 266 } // namespace fake_server |
| 208 | 267 |
| 209 #endif // COMPONENTS_SYNC_TEST_FAKE_SERVER_FAKE_SERVER_H_ | 268 #endif // COMPONENTS_SYNC_TEST_FAKE_SERVER_FAKE_SERVER_H_ |
| OLD | NEW |