Home | History | Annotate | Download | only in fake_server
      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 <map>
      9 #include <string>
     10 #include <vector>
     11 
     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"
     20 
     21 namespace fake_server {
     22 
     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;
     28 
     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   // 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);
     48 
     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();
     54 
     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);
     59 
     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);
     64 
     65   // Puts the server in a state where it acts as if authentication has
     66   // succeeded.
     67   void SetAuthenticated();
     68 
     69   // Puts the server in a state where all commands will fail with an
     70   // authentication error.
     71   void SetUnauthenticated();
     72 
     73   // Return |error_type| on next sync request.
     74   void TriggerError(const sync_pb::SyncEnums::ErrorType& error_type);
     75 
     76   // Adds |observer| to FakeServer's observer list. This should be called
     77   // before the Profile associated with |observer| is connected to the server.
     78   void AddObserver(Observer* observer);
     79 
     80   // Removes |observer| from the FakeServer's observer list. This method
     81   // must be called if AddObserver was ever called with |observer|.
     82   void RemoveObserver(Observer* observer);
     83 
     84  private:
     85   typedef std::map<std::string, FakeServerEntity*> EntityMap;
     86 
     87   // Processes a GetUpdates call.
     88   bool HandleGetUpdatesRequest(const sync_pb::GetUpdatesMessage& get_updates,
     89                                sync_pb::GetUpdatesResponse* response);
     90 
     91   // Processes a Commit call.
     92   bool HandleCommitRequest(const sync_pb::CommitMessage& message,
     93                            const std::string& invalidator_client_id,
     94                            sync_pb::CommitResponse* response);
     95 
     96   // Inserts the default permanent items in |entities_|.
     97   bool CreateDefaultPermanentItems();
     98 
     99   // Inserts the mobile bookmarks folder entity in |entities_|.
    100   bool CreateMobileBookmarksPermanentItem();
    101 
    102   // Saves a |entity| to |entities_|.
    103   void SaveEntity(FakeServerEntity* entity);
    104 
    105   // Commits a client-side SyncEntity to the server as a FakeServerEntity.
    106   // The client that sent the commit is identified via |client_guid|. The
    107   // parent ID string present in |client_entity| should be ignored in favor
    108   // of |parent_id|. If the commit is successful, the entity's server ID string
    109   // is returned and a new FakeServerEntity is saved in |entities_|.
    110   std::string CommitEntity(
    111       const sync_pb::SyncEntity& client_entity,
    112       sync_pb::CommitResponse_EntryResponse* entry_response,
    113       std::string client_guid,
    114       std::string parent_id);
    115 
    116   // Populates |entry_response| based on |entity|. It is assumed that
    117   // SaveEntity has already been called on |entity|.
    118   void BuildEntryResponseForSuccessfulCommit(
    119       sync_pb::CommitResponse_EntryResponse* entry_response,
    120       FakeServerEntity* entity);
    121 
    122   // Determines whether the SyncEntity with id_string |id| is a child of an
    123   // entity with id_string |potential_parent_id|.
    124   bool IsChild(const std::string& id, const std::string& potential_parent_id);
    125 
    126   // Creates and saves tombstones for all children of the entity with the given
    127   // |id|. A tombstone is not created for the entity itself.
    128   bool DeleteChildren(const std::string& id);
    129 
    130   // This is the last version number assigned to an entity. The next entity will
    131   // have a version number of version_ + 1.
    132   int64 version_;
    133 
    134   // The current store birthday value.
    135   std::string store_birthday_;
    136 
    137   // Whether the server should act as if incoming connections are properly
    138   // authenticated.
    139   bool authenticated_;
    140 
    141   // All SyncEntity objects saved by the server. The key value is the entity's
    142   // id string.
    143   EntityMap entities_;
    144 
    145   // All Keystore keys known to the server.
    146   std::vector<std::string> keystore_keys_;
    147 
    148   sync_pb::SyncEnums::ErrorType error_type_;
    149 
    150   // FakeServer's observers.
    151   ObserverList<Observer, true> observers_;
    152 };
    153 
    154 }  // namespace fake_server
    155 
    156 #endif  // SYNC_TEST_FAKE_SERVER_FAKE_SERVER_H_
    157