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.
5 #ifndef SYNC_TEST_FAKE_SERVER_FAKE_SERVER_H_
6 #define SYNC_TEST_FAKE_SERVER_FAKE_SERVER_H_
12 #include "base/basictypes.h"
13 #include "base/callback.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/observer_list.h"
16 #include "base/values.h"
17 #include "sync/internal_api/public/base/model_type.h"
18 #include "sync/protocol/sync.pb.h"
19 #include "sync/test/fake_server/fake_server_entity.h"
21 namespace fake_server {
23 // A fake version of the Sync server used for testing.
26 typedef base::Callback<void(int, int, const std::string&)>
27 HandleCommandCallback;
31 virtual ~Observer() {}
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;
41 virtual ~FakeServer();
43 // Asynchronously handles a /command POST to the server. If the error_code is
44 // passed to |callback| is 0 (success), the POST's response code and content
45 // will also be passed.
46 void HandleCommand(const std::string& request,
47 const HandleCommandCallback& callback);
49 // Creates a DicionaryValue representation of all entities present in the
50 // server. The dictionary keys are the strings generated by ModelTypeToString
51 // and the values are ListValues containing StringValue versions of entity
53 scoped_ptr<base::DictionaryValue> GetEntitiesAsDictionaryValue();
55 // Adds the FakeServerEntity* owned by |entity| to the server's collection
56 // of entities. This method makes no guarantees that the added entity will
57 // result in successful server operations.
58 void InjectEntity(scoped_ptr<FakeServerEntity> entity);
60 // Sets a new store birthday so that tests may trigger a NOT_MY_BIRTHDAY
61 // error. If |store_birthday| is the same as |store_birthday_|, false is
62 // returned and this method has no effect.
63 bool SetNewStoreBirthday(const std::string& store_birthday);
65 // Puts the server in a state where it acts as if authentication has
67 void SetAuthenticated();
69 // Puts the server in a state where all commands will fail with an
70 // authentication error.
71 void SetUnauthenticated();
73 // Force the server to return |error_type| in the error_code field of
74 // ClientToServerResponse on all subsequent sync requests. This method should
75 // not be called if TriggerActionableError has previously been called. Returns
76 // true if error triggering was successfully configured.
77 bool TriggerError(const sync_pb::SyncEnums::ErrorType& error_type);
79 // Force the server to return the given data as part of the error field of
80 // ClientToServerResponse on all subsequent sync requests. This method should
81 // not be called if TriggerError has previously been called. Returns true if
82 // error triggering was successfully configured.
83 bool TriggerActionableError(
84 const sync_pb::SyncEnums::ErrorType& error_type,
85 const std::string& description,
86 const std::string& url,
87 const sync_pb::SyncEnums::Action& action);
89 // Instructs the server to send triggered errors on every other request
90 // (starting with the first one after this call). This feature can be used to
91 // test the resiliency of the client when communicating with a problematic
92 // server or flaky network connection. This method should only be called
93 // after a call to TriggerError or TriggerActionableError. Returns true if
94 // triggered error alternating was successful.
95 bool EnableAlternatingTriggeredErrors();
97 // Adds |observer| to FakeServer's observer list. This should be called
98 // before the Profile associated with |observer| is connected to the server.
99 void AddObserver(Observer* observer);
101 // Removes |observer| from the FakeServer's observer list. This method
102 // must be called if AddObserver was ever called with |observer|.
103 void RemoveObserver(Observer* observer);
105 // Undoes the effects of DisableNetwork.
106 void EnableNetwork();
108 // Forces every request to fail in a way that simulates a network failure.
109 // This can be used to trigger exponential backoff in the client.
110 void DisableNetwork();
113 typedef std::map<std::string, FakeServerEntity*> EntityMap;
115 // Processes a GetUpdates call.
116 bool HandleGetUpdatesRequest(const sync_pb::GetUpdatesMessage& get_updates,
117 sync_pb::GetUpdatesResponse* response);
119 // Processes a Commit call.
120 bool HandleCommitRequest(const sync_pb::CommitMessage& message,
121 const std::string& invalidator_client_id,
122 sync_pb::CommitResponse* response);
124 // Inserts the default permanent items in |entities_|.
125 bool CreateDefaultPermanentItems();
127 // Inserts the mobile bookmarks folder entity in |entities_|.
128 bool CreateMobileBookmarksPermanentItem();
130 // Saves a |entity| to |entities_|.
131 void SaveEntity(FakeServerEntity* entity);
133 // Commits a client-side SyncEntity to the server as a FakeServerEntity.
134 // The client that sent the commit is identified via |client_guid|. The
135 // parent ID string present in |client_entity| should be ignored in favor
136 // of |parent_id|. If the commit is successful, the entity's server ID string
137 // is returned and a new FakeServerEntity is saved in |entities_|.
138 std::string CommitEntity(
139 const sync_pb::SyncEntity& client_entity,
140 sync_pb::CommitResponse_EntryResponse* entry_response,
141 std::string client_guid,
142 std::string parent_id);
144 // Populates |entry_response| based on |entity|. It is assumed that
145 // SaveEntity has already been called on |entity|.
146 void BuildEntryResponseForSuccessfulCommit(
147 sync_pb::CommitResponse_EntryResponse* entry_response,
148 FakeServerEntity* entity);
150 // Determines whether the SyncEntity with id_string |id| is a child of an
151 // entity with id_string |potential_parent_id|.
152 bool IsChild(const std::string& id, const std::string& potential_parent_id);
154 // Creates and saves tombstones for all children of the entity with the given
155 // |id|. A tombstone is not created for the entity itself.
156 bool DeleteChildren(const std::string& id);
158 // Returns whether a triggered error should be sent for the request.
159 bool ShouldSendTriggeredError() const;
161 // This is the last version number assigned to an entity. The next entity will
162 // have a version number of version_ + 1.
165 // The current store birthday value.
166 std::string store_birthday_;
168 // Whether the server should act as if incoming connections are properly
172 // All SyncEntity objects saved by the server. The key value is the entity's
176 // All Keystore keys known to the server.
177 std::vector<std::string> keystore_keys_;
179 // Used as the error_code field of ClientToServerResponse on all responses
180 // except when |triggered_actionable_error_| is set.
181 sync_pb::SyncEnums::ErrorType error_type_;
183 // Used as the error field of ClientToServerResponse when its pointer is not
185 scoped_ptr<sync_pb::ClientToServerResponse_Error> triggered_actionable_error_;
187 // These values are used in tandem to return a triggered error (either
188 // |error_type_| or |triggered_actionable_error_|) on every other request.
189 // |alternate_triggered_errors_| is set if this feature is enabled and
190 // |request_counter_| is used to send triggered errors on odd-numbered
191 // requests. Note that |request_counter_| can be reset and is not necessarily
192 // indicative of the total number of requests handled during the object's
194 bool alternate_triggered_errors_;
195 int request_counter_;
197 // FakeServer's observers.
198 ObserverList<Observer, true> observers_;
200 // When true, the server operates normally. When false, a failure is returned
201 // on every request. This is used to simulate a network failure on the client.
202 bool network_enabled_;
205 } // namespace fake_server
207 #endif // SYNC_TEST_FAKE_SERVER_FAKE_SERVER_H_