sync_test.h revision 46d4c2bc3267f3f028f39e7e311b0f89aba2e4fd
1// Copyright 2013 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 CHROME_BROWSER_SYNC_TEST_INTEGRATION_SYNC_TEST_H_
6#define CHROME_BROWSER_SYNC_TEST_INTEGRATION_SYNC_TEST_H_
7
8#include <string>
9#include <vector>
10
11#include "base/basictypes.h"
12#include "base/compiler_specific.h"
13#include "base/memory/scoped_ptr.h"
14#include "base/memory/scoped_vector.h"
15#include "chrome/test/base/in_process_browser_test.h"
16#include "net/dns/mock_host_resolver.h"
17#include "net/http/http_status_code.h"
18#include "net/url_request/url_request_status.h"
19#include "sync/internal_api/public/base/model_type.h"
20#include "sync/protocol/sync_protocol_error.h"
21#include "sync/test/fake_server/fake_server.h"
22#include "sync/test/local_sync_test_server.h"
23
24class Profile;
25class ProfileSyncService;
26class ProfileSyncServiceHarness;
27class P2PInvalidationForwarder;
28
29namespace base {
30class CommandLine;
31}
32
33namespace fake_server {
34class FakeServer;
35class FakeServerInvalidationService;
36}
37
38namespace net {
39class FakeURLFetcherFactory;
40class ProxyConfig;
41class ScopedDefaultHostResolverProc;
42class URLFetcherImplFactory;
43class URLRequestContextGetter;
44}
45
46// This is the base class for integration tests for all sync data types. Derived
47// classes must be defined for each sync data type. Individual tests are defined
48// using the IN_PROC_BROWSER_TEST_F macro.
49class SyncTest : public InProcessBrowserTest {
50 public:
51  // The different types of live sync tests that can be implemented.
52  enum TestType {
53    // Tests where only one client profile is synced with the server. Typically
54    // sanity level tests.
55    SINGLE_CLIENT,
56
57    // Tests that use one client profile and are not compatible with
58    // FakeServer.
59    // TODO(pvalenzuela): Delete this value when all SINGLE_CLIENT_LEGACY tests
60    // are compatible with FakeServer and switched to SINGLE_CLIENT. See
61    // crbug.com/323265.
62    SINGLE_CLIENT_LEGACY,
63
64    // Tests where two client profiles are synced with the server. Typically
65    // functionality level tests.
66    TWO_CLIENT,
67
68    // Tests that use two client profiles and are not compatible with
69    // FakeServer.
70    // TODO(pvalenzuela): Delete this value when all TWO_CLIENT_LEGACY tests are
71    // compatible with FakeServer and switched to TWO_CLIENT. See
72    // crbug.com/323265.
73    TWO_CLIENT_LEGACY,
74
75    // Tests where three or more client profiles are synced with the server.
76    // Typically, these tests create client side races and verify that sync
77    // works.
78    MULTIPLE_CLIENT
79  };
80
81  // The type of server we're running against.
82  enum ServerType {
83    SERVER_TYPE_UNDECIDED,
84    LOCAL_PYTHON_SERVER,    // The mock python server that runs locally and is
85                            // part of the Chromium checkout.
86    LOCAL_LIVE_SERVER,      // Some other server (maybe the real binary used by
87                            // Google's sync service) that can be started on
88                            // a per-test basis by running a command
89    EXTERNAL_LIVE_SERVER,   // A remote server that the test code has no control
90                            // over whatsoever; cross your fingers that the
91                            // account state is initially clean.
92    IN_PROCESS_FAKE_SERVER, // The fake Sync server (FakeServer) running
93                            // in-process (bypassing HTTP calls). This
94                            // ServerType will eventually replace
95                            // LOCAL_PYTHON_SERVER.
96  };
97
98  // NOTE: IMPORTANT the enum here should match with
99  // the enum defined on the chromiumsync.py test server impl.
100  enum SyncErrorFrequency {
101    // Uninitialized state.
102    ERROR_FREQUENCY_NONE,
103
104    // Server sends the error on all requests.
105    ERROR_FREQUENCY_ALWAYS,
106
107    // Server sends the error on two thirds of the request.
108    // Note this is not random. The server would send the
109    // error on the first 2 requests of every 3 requests.
110    ERROR_FREQUENCY_TWO_THIRDS
111  };
112
113  // A SyncTest must be associated with a particular test type.
114  explicit SyncTest(TestType test_type);
115
116  virtual ~SyncTest();
117
118  // Validates command line parameters and creates a local python test server if
119  // specified.
120  virtual void SetUp() OVERRIDE;
121
122  // Brings down local python test server if one was created.
123  virtual void TearDown() OVERRIDE;
124
125  // Sets up command line flags required for sync tests.
126  virtual void SetUpCommandLine(base::CommandLine* cl) OVERRIDE;
127
128  // Used to get the number of sync clients used by a test.
129  int num_clients() WARN_UNUSED_RESULT { return num_clients_; }
130
131  // Returns a pointer to a particular sync profile. Callee owns the object
132  // and manages its lifetime.
133  Profile* GetProfile(int index) WARN_UNUSED_RESULT;
134
135  // Returns a pointer to a particular browser. Callee owns the object
136  // and manages its lifetime.
137  Browser* GetBrowser(int index) WARN_UNUSED_RESULT;
138
139  // Returns a pointer to a particular sync client. Callee owns the object
140  // and manages its lifetime.
141  ProfileSyncServiceHarness* GetClient(int index) WARN_UNUSED_RESULT;
142
143  // Returns a reference to the collection of sync clients. Callee owns the
144  // object and manages its lifetime.
145  std::vector<ProfileSyncServiceHarness*>& clients() WARN_UNUSED_RESULT {
146    return clients_.get();
147  }
148
149  // Returns a ProfileSyncService at the given index.
150  ProfileSyncService* GetSyncService(int index);
151
152  // Returns the set of ProfileSyncServices.
153  std::vector<ProfileSyncService*> GetSyncServices();
154
155  // Returns a pointer to the sync profile that is used to verify changes to
156  // individual sync profiles. Callee owns the object and manages its lifetime.
157  Profile* verifier() WARN_UNUSED_RESULT;
158
159  // Used to determine whether the verifier profile should be updated or not.
160  bool use_verifier() WARN_UNUSED_RESULT { return use_verifier_; }
161
162  // After calling this method, changes made to a profile will no longer be
163  // reflected in the verifier profile. Note: Not all datatypes use this.
164  // TODO(rsimha): Hook up all datatypes to this mechanism.
165  void DisableVerifier();
166
167  // Initializes sync clients and profiles but does not sync any of them.
168  virtual bool SetupClients() WARN_UNUSED_RESULT;
169
170  // Initializes sync clients and profiles if required and syncs each of them.
171  virtual bool SetupSync() WARN_UNUSED_RESULT;
172
173  // Enable outgoing network connections for the given profile.
174  virtual void EnableNetwork(Profile* profile);
175
176  // Disable outgoing network connections for the given profile.
177  virtual void DisableNetwork(Profile* profile);
178
179  // Sets whether or not the sync clients in this test should respond to
180  // notifications of their own commits.  Real sync clients do not do this, but
181  // many test assertions require this behavior.
182  //
183  // Default is to return true.  Test should override this if they require
184  // different behavior.
185  virtual bool TestUsesSelfNotifications();
186
187  // Kicks off encryption for profile |index|.
188  bool EnableEncryption(int index);
189
190  // Checks if encryption is complete for profile |index|.
191  bool IsEncryptionComplete(int index);
192
193  // Blocks until all sync clients have completed their mutual sync cycles.
194  // Returns true if a quiescent state was successfully reached.
195  bool AwaitQuiescence();
196
197  // Returns true if the server being used supports controlling
198  // notifications.
199  bool ServerSupportsNotificationControl() const;
200
201  // Disable notifications on the server.  This operation is available
202  // only if ServerSupportsNotificationControl() returned true.
203  void DisableNotifications();
204
205  // Enable notifications on the server.  This operation is available
206  // only if ServerSupportsNotificationControl() returned true.
207  void EnableNotifications();
208
209  // Sets the mock gaia response for when an OAuth2 token is requested.
210  // Each call to this method will overwrite responses that were previously set.
211  void SetOAuth2TokenResponse(const std::string& response_data,
212                              net::HttpStatusCode response_code,
213                              net::URLRequestStatus::Status status);
214
215  // Trigger a notification to be sent to all clients.  This operation
216  // is available only if ServerSupportsNotificationControl() returned
217  // true.
218  void TriggerNotification(syncer::ModelTypeSet changed_types);
219
220  // Returns true if the server being used supports injecting errors.
221  bool ServerSupportsErrorTriggering() const;
222
223  // Triggers a migration for one or more datatypes, and waits
224  // for the server to complete it.  This operation is available
225  // only if ServerSupportsErrorTriggering() returned true.
226  void TriggerMigrationDoneError(syncer::ModelTypeSet model_types);
227
228  // Triggers a transient error on the server. Note the server will stay in
229  // this state until shut down.
230  void TriggerTransientError();
231
232  // Triggers an XMPP auth error on the server.  Note the server will
233  // stay in this state until shut down.
234  void TriggerXmppAuthError();
235
236  // Triggers a sync error on the server.
237  //   error: The error the server is expected to return.
238  //   frequency: Frequency with which the error is returned.
239  void TriggerSyncError(const syncer::SyncProtocolError& error,
240                        SyncErrorFrequency frequency);
241
242  // Triggers the creation the Synced Bookmarks folder on the server.
243  void TriggerCreateSyncedBookmarks();
244
245  // Returns the FakeServer being used for the test or NULL if FakeServer is
246  // not being used.
247  fake_server::FakeServer* GetFakeServer() const;
248
249 protected:
250  // Add custom switches needed for running the test.
251  virtual void AddTestSwitches(base::CommandLine* cl);
252
253  // Append the command line switches to enable experimental types that aren't
254  // on by default yet.
255  virtual void AddOptionalTypesToCommandLine(base::CommandLine* cl);
256
257  // InProcessBrowserTest override. Destroys all the sync clients and sync
258  // profiles created by a test.
259  virtual void CleanUpOnMainThread() OVERRIDE;
260
261  // InProcessBrowserTest override. Changes behavior of the default host
262  // resolver to avoid DNS lookup errors.
263  virtual void SetUpInProcessBrowserTestFixture() OVERRIDE;
264
265  // InProcessBrowserTest override. Resets the host resolver its default
266  // behavior.
267  virtual void TearDownInProcessBrowserTestFixture() OVERRIDE;
268
269  // Creates Profile, Browser and ProfileSyncServiceHarness instances for
270  // |index|. Used by SetupClients().
271  virtual void InitializeInstance(int index);
272
273  // Implementations of the EnableNotifications() and DisableNotifications()
274  // functions defined above.
275  void DisableNotificationsImpl();
276  void EnableNotificationsImpl();
277
278  // GAIA account used by the test case.
279  std::string username_;
280
281  // GAIA password used by the test case.
282  std::string password_;
283
284  // Locally available plain text file in which GAIA credentials are stored.
285  base::FilePath password_file_;
286
287  // The FakeServer used in tests with server type IN_PROCESS_FAKE_SERVER.
288  scoped_ptr<fake_server::FakeServer> fake_server_;
289
290 private:
291  // Helper to ProfileManager::CreateProfile that handles path creation.
292  static Profile* MakeProfile(const base::FilePath::StringType name);
293
294  // Helper method used to read GAIA credentials from a local password file
295  // specified via the "--password-file-for-test" command line switch.
296  // Note: The password file must be a plain text file with exactly two lines --
297  // the username on the first line and the password on the second line.
298  void ReadPasswordFile();
299
300  // Helper method that starts up a sync test server if required.
301  void SetUpTestServerIfRequired();
302
303  // Helper method used to start up a local python test server. Note: We set up
304  // an XMPP-only python server if |server_type_| is LOCAL_LIVE_SERVER and mock
305  // gaia credentials are in use. Returns true if successful.
306  bool SetUpLocalPythonTestServer();
307
308  // Helper method used to start up a local sync test server. Returns true if
309  // successful.
310  bool SetUpLocalTestServer();
311
312  // Helper method used to destroy the local python sync test server if one was
313  // created. Returns true if successful.
314  bool TearDownLocalPythonTestServer();
315
316  // Helper method used to destroy the local sync test server if one was
317  // created. Returns true if successful.
318  bool TearDownLocalTestServer();
319
320  // Helper method that waits for up to |wait| for the test server
321  // to start. Splits the time into |intervals| intervals, and polls the
322  // server after each interval to see if it has started. Returns true if
323  // successful.
324  bool WaitForTestServerToStart(base::TimeDelta wait, int intervals);
325
326  // Helper method used to check if the test server is up and running.
327  bool IsTestServerRunning();
328
329  // Used to disable and enable network connectivity by providing and
330  // clearing an invalid proxy configuration.
331  void SetProxyConfig(net::URLRequestContextGetter* context,
332                      const net::ProxyConfig& proxy_config);
333
334  // Helper method used to set up fake responses for kClientLoginUrl,
335  // kIssueAuthTokenUrl, kGetUserInfoUrl and kSearchDomainCheckUrl in order to
336  // mock out calls to GAIA servers.
337  void SetupMockGaiaResponses();
338
339  // Helper method used to clear any fake responses that might have been set for
340  // various gaia URLs, cancel any outstanding URL requests, and return to using
341  // the default URLFetcher creation mechanism.
342  void ClearMockGaiaResponses();
343
344  // Decide which sync server implementation to run against based on the type
345  // of test being run and command line args passed in.
346  void DecideServerType();
347
348  // Sets up the client-side invalidations infrastructure depending on the
349  // value of |server_type_|.
350  void InitializeInvalidations(int index);
351
352  // Python sync test server, started on demand.
353  syncer::LocalSyncTestServer sync_server_;
354
355  // Helper class to whitelist the notification port.
356  scoped_ptr<net::ScopedPortException> xmpp_port_;
357
358  // Used to differentiate between single-client, two-client, multi-client and
359  // many-client tests.
360  TestType test_type_;
361
362  // Tells us what kind of server we're using (some tests run only on certain
363  // server types).
364  ServerType server_type_;
365
366  // Number of sync clients that will be created by a test.
367  int num_clients_;
368
369  // Collection of sync profiles used by a test. A sync profile maintains sync
370  // data contained within its own subdirectory under the chrome user data
371  // directory. Profiles are owned by the ProfileManager.
372  std::vector<Profile*> profiles_;
373
374  // Collection of pointers to the browser objects used by a test. One browser
375  // instance is created for each sync profile. Browser object lifetime is
376  // managed by BrowserList, so we don't use a ScopedVector here.
377  std::vector<Browser*> browsers_;
378
379  // Collection of sync clients used by a test. A sync client is associated with
380  // a sync profile, and implements methods that sync the contents of the
381  // profile with the server.
382  ScopedVector<ProfileSyncServiceHarness> clients_;
383
384  // A set of objects to listen for commit activity and broadcast notifications
385  // of this activity to its peer sync clients.
386  ScopedVector<P2PInvalidationForwarder> invalidation_forwarders_;
387
388  // Collection of pointers to FakeServerInvalidation objects for each profile.
389  std::vector<fake_server::FakeServerInvalidationService*>
390      fake_server_invalidation_services_;
391
392  // Sync profile against which changes to individual profiles are verified. We
393  // don't need a corresponding verifier sync client because the contents of the
394  // verifier profile are strictly local, and are not meant to be synced.
395  Profile* verifier_;
396
397  // Indicates whether changes to a profile should also change the verifier
398  // profile or not.
399  bool use_verifier_;
400
401  // Indicates whether or not notifications were explicitly enabled/disabled.
402  // Defaults to true.
403  bool notifications_enabled_;
404
405  // Sync integration tests need to make live DNS requests for access to
406  // GAIA and sync server URLs under google.com. We use a scoped version
407  // to override the default resolver while the test is active.
408  scoped_ptr<net::ScopedDefaultHostResolverProc> mock_host_resolver_override_;
409
410  // Used to start and stop the local test server.
411  base::ProcessHandle test_server_handle_;
412
413  // Fake URLFetcher factory used to mock out GAIA signin.
414  scoped_ptr<net::FakeURLFetcherFactory> fake_factory_;
415
416  // The URLFetcherImplFactory instance used to instantiate |fake_factory_|.
417  scoped_ptr<net::URLFetcherImplFactory> factory_;
418
419  DISALLOW_COPY_AND_ASSIGN(SyncTest);
420};
421
422#endif  // CHROME_BROWSER_SYNC_TEST_INTEGRATION_SYNC_TEST_H_
423