| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef SYNC_TEST_FAKE_SERVER_FAKE_SERVER_H_ | |
| 6 #define SYNC_TEST_FAKE_SERVER_FAKE_SERVER_H_ | |
| 7 | |
| 8 #include <stdint.h> | |
| 9 | |
| 10 #include <map> | |
| 11 #include <memory> | |
| 12 #include <string> | |
| 13 #include <vector> | |
| 14 | |
| 15 #include "base/callback.h" | |
| 16 #include "base/observer_list.h" | |
| 17 #include "base/threading/thread_checker.h" | |
| 18 #include "base/values.h" | |
| 19 #include "sync/internal_api/public/base/model_type.h" | |
| 20 #include "sync/protocol/sync.pb.h" | |
| 21 #include "sync/test/fake_server/fake_server_entity.h" | |
| 22 | |
| 23 namespace fake_server { | |
| 24 | |
| 25 // A fake version of the Sync server used for testing. This class is not thread | |
| 26 // safe. | |
| 27 class FakeServer { | |
| 28 public: | |
| 29 class Observer { | |
| 30 public: | |
| 31 virtual ~Observer() {} | |
| 32 | |
| 33 // Called after FakeServer has processed a successful commit. The types | |
| 34 // updated as part of the commit are passed in |committed_model_types|. | |
| 35 virtual void OnCommit( | |
| 36 const std::string& committer_id, | |
| 37 syncer::ModelTypeSet committed_model_types) = 0; | |
| 38 }; | |
| 39 | |
| 40 FakeServer(); | |
| 41 virtual ~FakeServer(); | |
| 42 | |
| 43 // Handles a /command POST (with the given |request|) to the server. Three | |
| 44 // output arguments, |error_code|, |response_code|, and |response|, are used | |
| 45 // to pass data back to the caller. The command has failed if the value | |
| 46 // pointed to by |error_code| is nonzero. |completion_closure| will be called | |
| 47 // immediately before return. | |
| 48 void HandleCommand(const std::string& request, | |
| 49 const base::Closure& completion_closure, | |
| 50 int* error_code, | |
| 51 int* response_code, | |
| 52 std::string* response); | |
| 53 | |
| 54 // Helpers for fetching the last Commit or GetUpdates messages, respectively. | |
| 55 // Returns true if the specified message existed, and false if no message has | |
| 56 // been received. | |
| 57 bool GetLastCommitMessage(sync_pb::ClientToServerMessage* message); | |
| 58 bool GetLastGetUpdatesMessage(sync_pb::ClientToServerMessage* message); | |
| 59 | |
| 60 // Creates a DicionaryValue representation of all entities present in the | |
| 61 // server. The dictionary keys are the strings generated by ModelTypeToString | |
| 62 // and the values are ListValues containing StringValue versions of entity | |
| 63 // names. | |
| 64 std::unique_ptr<base::DictionaryValue> GetEntitiesAsDictionaryValue(); | |
| 65 | |
| 66 // Returns all entities stored by the server of the given |model_type|. | |
| 67 // This method returns SyncEntity protocol buffer objects (instead of | |
| 68 // FakeServerEntity) so that callers can inspect datatype-specific data | |
| 69 // (e.g., the URL of a session tab). | |
| 70 std::vector<sync_pb::SyncEntity> GetSyncEntitiesByModelType( | |
| 71 syncer::ModelType model_type); | |
| 72 | |
| 73 // Adds |entity| to the server's collection of entities. This method makes no | |
| 74 // guarantees that the added entity will result in successful server | |
| 75 // operations. | |
| 76 void InjectEntity(std::unique_ptr<FakeServerEntity> entity); | |
| 77 | |
| 78 // Modifies the entity on the server with the given |id|. The entity's | |
| 79 // EntitySpecifics are replaced with |updated_specifics| and its version is | |
| 80 // updated. If the given |id| does not exist or the ModelType of | |
| 81 // |updated_specifics| does not match the entity, false is returned. | |
| 82 // Otherwise, true is returned to represent a successful modification. | |
| 83 // | |
| 84 // This method sometimes updates entity data beyond EntitySpecifics. For | |
| 85 // example, in the case of a bookmark, changing the BookmarkSpecifics title | |
| 86 // field will modify the top-level entity's name field. | |
| 87 bool ModifyEntitySpecifics(const std::string& id, | |
| 88 const sync_pb::EntitySpecifics& updated_specifics); | |
| 89 | |
| 90 bool ModifyBookmarkEntity(const std::string& id, | |
| 91 const std::string& parent_id, | |
| 92 const sync_pb::EntitySpecifics& updated_specifics); | |
| 93 | |
| 94 // Clears server data simulating a "dashboard stop and clear" and sets a new | |
| 95 // store birthday. | |
| 96 void ClearServerData(); | |
| 97 | |
| 98 // Puts the server in a state where it acts as if authentication has | |
| 99 // succeeded. | |
| 100 void SetAuthenticated(); | |
| 101 | |
| 102 // Puts the server in a state where all commands will fail with an | |
| 103 // authentication error. | |
| 104 void SetUnauthenticated(); | |
| 105 | |
| 106 // Force the server to return |error_type| in the error_code field of | |
| 107 // ClientToServerResponse on all subsequent sync requests. This method should | |
| 108 // not be called if TriggerActionableError has previously been called. Returns | |
| 109 // true if error triggering was successfully configured. | |
| 110 bool TriggerError(const sync_pb::SyncEnums::ErrorType& error_type); | |
| 111 | |
| 112 // Force the server to return the given data as part of the error field of | |
| 113 // ClientToServerResponse on all subsequent sync requests. This method should | |
| 114 // not be called if TriggerError has previously been called. Returns true if | |
| 115 // error triggering was successfully configured. | |
| 116 bool TriggerActionableError( | |
| 117 const sync_pb::SyncEnums::ErrorType& error_type, | |
| 118 const std::string& description, | |
| 119 const std::string& url, | |
| 120 const sync_pb::SyncEnums::Action& action); | |
| 121 | |
| 122 // Instructs the server to send triggered errors on every other request | |
| 123 // (starting with the first one after this call). This feature can be used to | |
| 124 // test the resiliency of the client when communicating with a problematic | |
| 125 // server or flaky network connection. This method should only be called | |
| 126 // after a call to TriggerError or TriggerActionableError. Returns true if | |
| 127 // triggered error alternating was successful. | |
| 128 bool EnableAlternatingTriggeredErrors(); | |
| 129 | |
| 130 // Adds |observer| to FakeServer's observer list. This should be called | |
| 131 // before the Profile associated with |observer| is connected to the server. | |
| 132 void AddObserver(Observer* observer); | |
| 133 | |
| 134 // Removes |observer| from the FakeServer's observer list. This method | |
| 135 // must be called if AddObserver was ever called with |observer|. | |
| 136 void RemoveObserver(Observer* observer); | |
| 137 | |
| 138 // Undoes the effects of DisableNetwork. | |
| 139 void EnableNetwork(); | |
| 140 | |
| 141 // Forces every request to fail in a way that simulates a network failure. | |
| 142 // This can be used to trigger exponential backoff in the client. | |
| 143 void DisableNetwork(); | |
| 144 | |
| 145 // Returns the entity ID of the Bookmark Bar folder. | |
| 146 std::string GetBookmarkBarFolderId() const; | |
| 147 | |
| 148 // Returns the current FakeServer as a WeakPtr. | |
| 149 base::WeakPtr<FakeServer> AsWeakPtr(); | |
| 150 | |
| 151 private: | |
| 152 using EntityMap = std::map<std::string, std::unique_ptr<FakeServerEntity>>; | |
| 153 | |
| 154 // Gets FakeServer ready for syncing. | |
| 155 void Init(); | |
| 156 | |
| 157 // Processes a GetUpdates call. | |
| 158 bool HandleGetUpdatesRequest(const sync_pb::GetUpdatesMessage& get_updates, | |
| 159 sync_pb::GetUpdatesResponse* response); | |
| 160 | |
| 161 // Processes a Commit call. | |
| 162 bool HandleCommitRequest(const sync_pb::CommitMessage& message, | |
| 163 const std::string& invalidator_client_id, | |
| 164 sync_pb::CommitResponse* response); | |
| 165 | |
| 166 // Creates and saves a permanent folder for Bookmarks (e.g., Bookmark Bar). | |
| 167 bool CreatePermanentBookmarkFolder(const std::string& server_tag, | |
| 168 const std::string& name); | |
| 169 | |
| 170 // Inserts the default permanent items in |entities_|. | |
| 171 bool CreateDefaultPermanentItems(); | |
| 172 | |
| 173 // Saves a |entity| to |entities_|. | |
| 174 void SaveEntity(std::unique_ptr<FakeServerEntity> entity); | |
| 175 | |
| 176 // Commits a client-side SyncEntity to the server as a FakeServerEntity. | |
| 177 // The client that sent the commit is identified via |client_guid|. The | |
| 178 // parent ID string present in |client_entity| should be ignored in favor | |
| 179 // of |parent_id|. If the commit is successful, the entity's server ID string | |
| 180 // is returned and a new FakeServerEntity is saved in |entities_|. | |
| 181 std::string CommitEntity( | |
| 182 const sync_pb::SyncEntity& client_entity, | |
| 183 sync_pb::CommitResponse_EntryResponse* entry_response, | |
| 184 const std::string& client_guid, | |
| 185 const std::string& parent_id); | |
| 186 | |
| 187 // Populates |entry_response| based on the stored entity identified by | |
| 188 // |entity_id|. It is assumed that the entity identified by |entity_id| has | |
| 189 // already been stored using SaveEntity. | |
| 190 void BuildEntryResponseForSuccessfulCommit( | |
| 191 const std::string& entity_id, | |
| 192 sync_pb::CommitResponse_EntryResponse* entry_response); | |
| 193 | |
| 194 // Determines whether the SyncEntity with id_string |id| is a child of an | |
| 195 // entity with id_string |potential_parent_id|. | |
| 196 bool IsChild(const std::string& id, const std::string& potential_parent_id); | |
| 197 | |
| 198 // Creates and saves tombstones for all children of the entity with the given | |
| 199 // |id|. A tombstone is not created for the entity itself. | |
| 200 void DeleteChildren(const std::string& id); | |
| 201 | |
| 202 // Returns whether a triggered error should be sent for the request. | |
| 203 bool ShouldSendTriggeredError() const; | |
| 204 | |
| 205 // Updates the |entity| to a new version and increments the version counter | |
| 206 // that the server uses to assign versions. | |
| 207 void UpdateEntityVersion(FakeServerEntity* entity); | |
| 208 | |
| 209 // Returns the store birthday. | |
| 210 std::string GetStoreBirthday() const; | |
| 211 | |
| 212 // This is the last version number assigned to an entity. The next entity will | |
| 213 // have a version number of version_ + 1. | |
| 214 int64_t version_; | |
| 215 | |
| 216 // The current store birthday value. | |
| 217 int64_t store_birthday_; | |
| 218 | |
| 219 // Whether the server should act as if incoming connections are properly | |
| 220 // authenticated. | |
| 221 bool authenticated_; | |
| 222 | |
| 223 // All SyncEntity objects saved by the server. The key value is the entity's | |
| 224 // id string. | |
| 225 EntityMap entities_; | |
| 226 | |
| 227 // All Keystore keys known to the server. | |
| 228 std::vector<std::string> keystore_keys_; | |
| 229 | |
| 230 // Used as the error_code field of ClientToServerResponse on all responses | |
| 231 // except when |triggered_actionable_error_| is set. | |
| 232 sync_pb::SyncEnums::ErrorType error_type_; | |
| 233 | |
| 234 // Used as the error field of ClientToServerResponse when its pointer is not | |
| 235 // NULL. | |
| 236 std::unique_ptr<sync_pb::ClientToServerResponse_Error> | |
| 237 triggered_actionable_error_; | |
| 238 | |
| 239 // These values are used in tandem to return a triggered error (either | |
| 240 // |error_type_| or |triggered_actionable_error_|) on every other request. | |
| 241 // |alternate_triggered_errors_| is set if this feature is enabled and | |
| 242 // |request_counter_| is used to send triggered errors on odd-numbered | |
| 243 // requests. Note that |request_counter_| can be reset and is not necessarily | |
| 244 // indicative of the total number of requests handled during the object's | |
| 245 // lifetime. | |
| 246 bool alternate_triggered_errors_; | |
| 247 int request_counter_; | |
| 248 | |
| 249 // FakeServer's observers. | |
| 250 base::ObserverList<Observer, true> observers_; | |
| 251 | |
| 252 // When true, the server operates normally. When false, a failure is returned | |
| 253 // on every request. This is used to simulate a network failure on the client. | |
| 254 bool network_enabled_; | |
| 255 | |
| 256 // The last received client to server messages. | |
| 257 sync_pb::ClientToServerMessage last_commit_message_; | |
| 258 sync_pb::ClientToServerMessage last_getupdates_message_; | |
| 259 | |
| 260 // Used to verify that FakeServer is only used from one thread. | |
| 261 base::ThreadChecker thread_checker_; | |
| 262 | |
| 263 // Creates WeakPtr versions of the current FakeServer. This must be the last | |
| 264 // data member! | |
| 265 base::WeakPtrFactory<FakeServer> weak_ptr_factory_; | |
| 266 }; | |
| 267 | |
| 268 } // namespace fake_server | |
| 269 | |
| 270 #endif // SYNC_TEST_FAKE_SERVER_FAKE_SERVER_H_ | |
| OLD | NEW |