Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / sync / test / fake_server / fake_server.h
blobecc8bd7430b31fec2cd8e498775e68b5a23b03ea
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_
8 #include <map>
9 #include <string>
10 #include <vector>
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.
24 class FakeServer {
25 public:
26 typedef base::Callback<void(int, int, const std::string&)>
27 HandleCommandCallback;
29 class Observer {
30 public:
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;
40 FakeServer();
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
52 // names.
53 scoped_ptr<base::DictionaryValue> GetEntitiesAsDictionaryValue();
55 // Returns all entities stored by the server of the given |model_type|.
56 // This method returns SyncEntity protocol buffer objects (instead of
57 // FakeServerEntity) so that callers can inspect datatype-specific data
58 // (e.g., the URL of a session tab).
59 std::vector<sync_pb::SyncEntity> GetSyncEntitiesByModelType(
60 syncer::ModelType model_type);
62 // Adds the FakeServerEntity* owned by |entity| to the server's collection
63 // of entities. This method makes no guarantees that the added entity will
64 // result in successful server operations.
65 void InjectEntity(scoped_ptr<FakeServerEntity> entity);
67 // Sets a new store birthday so that tests may trigger a NOT_MY_BIRTHDAY
68 // error. If |store_birthday| is the same as |store_birthday_|, false is
69 // returned and this method has no effect.
70 bool SetNewStoreBirthday(const std::string& store_birthday);
72 // Puts the server in a state where it acts as if authentication has
73 // succeeded.
74 void SetAuthenticated();
76 // Puts the server in a state where all commands will fail with an
77 // authentication error.
78 void SetUnauthenticated();
80 // Force the server to return |error_type| in the error_code field of
81 // ClientToServerResponse on all subsequent sync requests. This method should
82 // not be called if TriggerActionableError has previously been called. Returns
83 // true if error triggering was successfully configured.
84 bool TriggerError(const sync_pb::SyncEnums::ErrorType& error_type);
86 // Force the server to return the given data as part of the error field of
87 // ClientToServerResponse on all subsequent sync requests. This method should
88 // not be called if TriggerError has previously been called. Returns true if
89 // error triggering was successfully configured.
90 bool TriggerActionableError(
91 const sync_pb::SyncEnums::ErrorType& error_type,
92 const std::string& description,
93 const std::string& url,
94 const sync_pb::SyncEnums::Action& action);
96 // Instructs the server to send triggered errors on every other request
97 // (starting with the first one after this call). This feature can be used to
98 // test the resiliency of the client when communicating with a problematic
99 // server or flaky network connection. This method should only be called
100 // after a call to TriggerError or TriggerActionableError. Returns true if
101 // triggered error alternating was successful.
102 bool EnableAlternatingTriggeredErrors();
104 // Adds |observer| to FakeServer's observer list. This should be called
105 // before the Profile associated with |observer| is connected to the server.
106 void AddObserver(Observer* observer);
108 // Removes |observer| from the FakeServer's observer list. This method
109 // must be called if AddObserver was ever called with |observer|.
110 void RemoveObserver(Observer* observer);
112 // Undoes the effects of DisableNetwork.
113 void EnableNetwork();
115 // Forces every request to fail in a way that simulates a network failure.
116 // This can be used to trigger exponential backoff in the client.
117 void DisableNetwork();
119 // Returns the entity ID of the Bookmark Bar folder.
120 std::string GetBookmarkBarFolderId() const;
122 private:
123 typedef std::map<std::string, FakeServerEntity*> EntityMap;
125 // Processes a GetUpdates call.
126 bool HandleGetUpdatesRequest(const sync_pb::GetUpdatesMessage& get_updates,
127 sync_pb::GetUpdatesResponse* response);
129 // Processes a Commit call.
130 bool HandleCommitRequest(const sync_pb::CommitMessage& message,
131 const std::string& invalidator_client_id,
132 sync_pb::CommitResponse* response);
134 // Creates and saves a permanent folder for Bookmarks (e.g., Bookmark Bar).
135 bool CreatePermanentBookmarkFolder(const std::string& server_tag,
136 const std::string& name);
138 // Inserts the default permanent items in |entities_|.
139 bool CreateDefaultPermanentItems();
141 // Saves a |entity| to |entities_|.
142 void SaveEntity(FakeServerEntity* entity);
144 // Commits a client-side SyncEntity to the server as a FakeServerEntity.
145 // The client that sent the commit is identified via |client_guid|. The
146 // parent ID string present in |client_entity| should be ignored in favor
147 // of |parent_id|. If the commit is successful, the entity's server ID string
148 // is returned and a new FakeServerEntity is saved in |entities_|.
149 std::string CommitEntity(
150 const sync_pb::SyncEntity& client_entity,
151 sync_pb::CommitResponse_EntryResponse* entry_response,
152 std::string client_guid,
153 std::string parent_id);
155 // Populates |entry_response| based on |entity|. It is assumed that
156 // SaveEntity has already been called on |entity|.
157 void BuildEntryResponseForSuccessfulCommit(
158 sync_pb::CommitResponse_EntryResponse* entry_response,
159 FakeServerEntity* entity);
161 // Determines whether the SyncEntity with id_string |id| is a child of an
162 // entity with id_string |potential_parent_id|.
163 bool IsChild(const std::string& id, const std::string& potential_parent_id);
165 // Creates and saves tombstones for all children of the entity with the given
166 // |id|. A tombstone is not created for the entity itself.
167 bool DeleteChildren(const std::string& id);
169 // Returns whether a triggered error should be sent for the request.
170 bool ShouldSendTriggeredError() const;
172 // This is the last version number assigned to an entity. The next entity will
173 // have a version number of version_ + 1.
174 int64 version_;
176 // The current store birthday value.
177 std::string store_birthday_;
179 // Whether the server should act as if incoming connections are properly
180 // authenticated.
181 bool authenticated_;
183 // All SyncEntity objects saved by the server. The key value is the entity's
184 // id string.
185 EntityMap entities_;
187 // All Keystore keys known to the server.
188 std::vector<std::string> keystore_keys_;
190 // Used as the error_code field of ClientToServerResponse on all responses
191 // except when |triggered_actionable_error_| is set.
192 sync_pb::SyncEnums::ErrorType error_type_;
194 // Used as the error field of ClientToServerResponse when its pointer is not
195 // NULL.
196 scoped_ptr<sync_pb::ClientToServerResponse_Error> triggered_actionable_error_;
198 // These values are used in tandem to return a triggered error (either
199 // |error_type_| or |triggered_actionable_error_|) on every other request.
200 // |alternate_triggered_errors_| is set if this feature is enabled and
201 // |request_counter_| is used to send triggered errors on odd-numbered
202 // requests. Note that |request_counter_| can be reset and is not necessarily
203 // indicative of the total number of requests handled during the object's
204 // lifetime.
205 bool alternate_triggered_errors_;
206 int request_counter_;
208 // FakeServer's observers.
209 ObserverList<Observer, true> observers_;
211 // When true, the server operates normally. When false, a failure is returned
212 // on every request. This is used to simulate a network failure on the client.
213 bool network_enabled_;
216 } // namespace fake_server
218 #endif // SYNC_TEST_FAKE_SERVER_FAKE_SERVER_H_