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