ServiceWorker: Consolidate version manipulation functions in SWProviderContext
[chromium-blink-merge.git] / sync / test / fake_server / fake_server.h
blob83b38f5ed9e9dbce5ff0269a1f75783f187dbfee
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 // 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
66 // succeeded.
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();
112 private:
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 bool CreatePermanentBookmarkFolder(const char* server_tag, const char* name);
126 // Inserts the default permanent items in |entities_|.
127 bool CreateDefaultPermanentItems();
129 // Inserts the mobile bookmarks folder entity in |entities_|.
130 bool CreateMobileBookmarksPermanentItem();
132 // Saves a |entity| to |entities_|.
133 void SaveEntity(FakeServerEntity* entity);
135 // Commits a client-side SyncEntity to the server as a FakeServerEntity.
136 // The client that sent the commit is identified via |client_guid|. The
137 // parent ID string present in |client_entity| should be ignored in favor
138 // of |parent_id|. If the commit is successful, the entity's server ID string
139 // is returned and a new FakeServerEntity is saved in |entities_|.
140 std::string CommitEntity(
141 const sync_pb::SyncEntity& client_entity,
142 sync_pb::CommitResponse_EntryResponse* entry_response,
143 std::string client_guid,
144 std::string parent_id);
146 // Populates |entry_response| based on |entity|. It is assumed that
147 // SaveEntity has already been called on |entity|.
148 void BuildEntryResponseForSuccessfulCommit(
149 sync_pb::CommitResponse_EntryResponse* entry_response,
150 FakeServerEntity* entity);
152 // Determines whether the SyncEntity with id_string |id| is a child of an
153 // entity with id_string |potential_parent_id|.
154 bool IsChild(const std::string& id, const std::string& potential_parent_id);
156 // Creates and saves tombstones for all children of the entity with the given
157 // |id|. A tombstone is not created for the entity itself.
158 bool DeleteChildren(const std::string& id);
160 // Returns whether a triggered error should be sent for the request.
161 bool ShouldSendTriggeredError() const;
163 // This is the last version number assigned to an entity. The next entity will
164 // have a version number of version_ + 1.
165 int64 version_;
167 // The current store birthday value.
168 std::string store_birthday_;
170 // Whether the server should act as if incoming connections are properly
171 // authenticated.
172 bool authenticated_;
174 // All SyncEntity objects saved by the server. The key value is the entity's
175 // id string.
176 EntityMap entities_;
178 // All Keystore keys known to the server.
179 std::vector<std::string> keystore_keys_;
181 // Used as the error_code field of ClientToServerResponse on all responses
182 // except when |triggered_actionable_error_| is set.
183 sync_pb::SyncEnums::ErrorType error_type_;
185 // Used as the error field of ClientToServerResponse when its pointer is not
186 // NULL.
187 scoped_ptr<sync_pb::ClientToServerResponse_Error> triggered_actionable_error_;
189 // These values are used in tandem to return a triggered error (either
190 // |error_type_| or |triggered_actionable_error_|) on every other request.
191 // |alternate_triggered_errors_| is set if this feature is enabled and
192 // |request_counter_| is used to send triggered errors on odd-numbered
193 // requests. Note that |request_counter_| can be reset and is not necessarily
194 // indicative of the total number of requests handled during the object's
195 // lifetime.
196 bool alternate_triggered_errors_;
197 int request_counter_;
199 // FakeServer's observers.
200 ObserverList<Observer, true> observers_;
202 // When true, the server operates normally. When false, a failure is returned
203 // on every request. This is used to simulate a network failure on the client.
204 bool network_enabled_;
207 } // namespace fake_server
209 #endif // SYNC_TEST_FAKE_SERVER_FAKE_SERVER_H_