| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 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_TEST_LIVE_SYNC_LIVE_SYNC_TEST_H_ | |
| 6 #define CHROME_TEST_LIVE_SYNC_LIVE_SYNC_TEST_H_ | |
| 7 #pragma once | |
| 8 | |
| 9 #include "chrome/test/base/in_process_browser_test.h" | |
| 10 | |
| 11 #include <string> | |
| 12 #include <vector> | |
| 13 | |
| 14 #include "base/basictypes.h" | |
| 15 #include "base/file_util.h" | |
| 16 #include "base/memory/scoped_ptr.h" | |
| 17 #include "base/memory/scoped_vector.h" | |
| 18 #include "base/process_util.h" | |
| 19 #include "chrome/browser/sync/syncable/model_type.h" | |
| 20 #include "net/base/mock_host_resolver.h" | |
| 21 #include "net/test/test_server.h" | |
| 22 | |
| 23 class CommandLine; | |
| 24 class Profile; | |
| 25 class ProfileSyncServiceHarness; | |
| 26 class FakeURLFetcherFactory; | |
| 27 class URLFetcherFactory; | |
| 28 | |
| 29 namespace net { | |
| 30 class ProxyConfig; | |
| 31 class ScopedDefaultHostResolverProc; | |
| 32 class URLRequestContextGetter; | |
| 33 } | |
| 34 | |
| 35 // This is the base class for integration tests for all sync data types. Derived | |
| 36 // classes must be defined for each sync data type. Individual tests are defined | |
| 37 // using the IN_PROC_BROWSER_TEST_F macro. | |
| 38 class LiveSyncTest : public InProcessBrowserTest { | |
| 39 public: | |
| 40 // The different types of live sync tests that can be implemented. | |
| 41 enum TestType { | |
| 42 // Tests where only one client profile is synced with the server. Typically | |
| 43 // sanity level tests. | |
| 44 SINGLE_CLIENT, | |
| 45 | |
| 46 // Tests where two client profiles are synced with the server. Typically | |
| 47 // functionality level tests. | |
| 48 TWO_CLIENT, | |
| 49 | |
| 50 // Tests where three or more client profiles are synced with the server. | |
| 51 // Typically, these tests create client side races and verify that sync | |
| 52 // works. | |
| 53 MULTIPLE_CLIENT, | |
| 54 | |
| 55 // Tests where several client profiles are synced with the server. Only used | |
| 56 // by stress tests. | |
| 57 MANY_CLIENT | |
| 58 }; | |
| 59 | |
| 60 // The type of server we're running against. | |
| 61 enum ServerType { | |
| 62 SERVER_TYPE_UNDECIDED, | |
| 63 LOCAL_PYTHON_SERVER, // The mock python server that runs locally and is | |
| 64 // part of the Chromium checkout. | |
| 65 LOCAL_LIVE_SERVER, // Some other server (maybe the real binary used by | |
| 66 // Google's sync service) that can be started on | |
| 67 // a per-test basis by running a command | |
| 68 EXTERNAL_LIVE_SERVER, // A remote server that the test code has no control | |
| 69 // over whatsoever; cross your fingers that the | |
| 70 // account state is initially clean. | |
| 71 }; | |
| 72 | |
| 73 // A LiveSyncTest must be associated with a particular test type. | |
| 74 explicit LiveSyncTest(TestType test_type); | |
| 75 | |
| 76 virtual ~LiveSyncTest(); | |
| 77 | |
| 78 // Validates command line parameters and creates a local python test server if | |
| 79 // specified. | |
| 80 virtual void SetUp() OVERRIDE; | |
| 81 | |
| 82 // Brings down local python test server if one was created. | |
| 83 virtual void TearDown() OVERRIDE; | |
| 84 | |
| 85 // Sets up command line flags required for sync tests. | |
| 86 virtual void SetUpCommandLine(CommandLine* cl) OVERRIDE; | |
| 87 | |
| 88 // Used to get the number of sync clients used by a test. | |
| 89 int num_clients() WARN_UNUSED_RESULT { return num_clients_; } | |
| 90 | |
| 91 // Returns a pointer to a particular sync profile. Callee owns the object | |
| 92 // and manages its lifetime. | |
| 93 Profile* GetProfile(int index) WARN_UNUSED_RESULT; | |
| 94 | |
| 95 // Returns a pointer to a particular browser. Callee owns the object | |
| 96 // and manages its lifetime. | |
| 97 Browser* GetBrowser(int index) WARN_UNUSED_RESULT; | |
| 98 | |
| 99 // Returns a pointer to a particular sync client. Callee owns the object | |
| 100 // and manages its lifetime. | |
| 101 ProfileSyncServiceHarness* GetClient(int index) WARN_UNUSED_RESULT; | |
| 102 | |
| 103 // Returns a reference to the collection of sync clients. Callee owns the | |
| 104 // object and manages its lifetime. | |
| 105 std::vector<ProfileSyncServiceHarness*>& clients() WARN_UNUSED_RESULT { | |
| 106 return clients_.get(); | |
| 107 } | |
| 108 | |
| 109 // Returns a pointer to the sync profile that is used to verify changes to | |
| 110 // individual sync profiles. Callee owns the object and manages its lifetime. | |
| 111 Profile* verifier() WARN_UNUSED_RESULT; | |
| 112 | |
| 113 // Used to determine whether the verifier profile should be updated or not. | |
| 114 bool use_verifier() WARN_UNUSED_RESULT { return use_verifier_; } | |
| 115 | |
| 116 // After calling this method, changes made to a profile will no longer be | |
| 117 // reflected in the verifier profile. Note: Not all datatypes use this. | |
| 118 // TODO(rsimha): Hook up all datatypes to this mechanism. | |
| 119 void DisableVerifier(); | |
| 120 | |
| 121 // Initializes sync clients and profiles but does not sync any of them. | |
| 122 virtual bool SetupClients() WARN_UNUSED_RESULT; | |
| 123 | |
| 124 // Initializes sync clients and profiles if required and syncs each of them. | |
| 125 virtual bool SetupSync() WARN_UNUSED_RESULT; | |
| 126 | |
| 127 // Enable outgoing network connections for the given profile. | |
| 128 virtual void EnableNetwork(Profile* profile); | |
| 129 | |
| 130 // Disable outgoing network connections for the given profile. | |
| 131 virtual void DisableNetwork(Profile* profile); | |
| 132 | |
| 133 // Encrypts the datatype |type| for profile |index|. | |
| 134 bool EnableEncryption(int index, syncable::ModelType type); | |
| 135 | |
| 136 // Checks if the datatype |type| is encrypted for profile |index|. | |
| 137 bool IsEncrypted(int index, syncable::ModelType type); | |
| 138 | |
| 139 // Blocks until all sync clients have completed their mutual sync cycles. | |
| 140 // Returns true if a quiescent state was successfully reached. | |
| 141 bool AwaitQuiescence(); | |
| 142 | |
| 143 // Returns true if the server being used supports controlling | |
| 144 // notifications. | |
| 145 bool ServerSupportsNotificationControl() const; | |
| 146 | |
| 147 // Disable notifications on the server. This operation is available | |
| 148 // only if ServerSupportsNotificationControl() returned true. | |
| 149 void DisableNotifications(); | |
| 150 | |
| 151 // Enable notifications on the server. This operation is available | |
| 152 // only if ServerSupportsNotificationControl() returned true. | |
| 153 void EnableNotifications(); | |
| 154 | |
| 155 // Trigger a notification to be sent to all clients. This operation | |
| 156 // is available only if ServerSupportsNotificationControl() returned | |
| 157 // true. | |
| 158 void TriggerNotification(const syncable::ModelTypeSet& changed_types); | |
| 159 | |
| 160 // Returns true if the server being used supports injecting errors. | |
| 161 bool ServerSupportsErrorTriggering() const; | |
| 162 | |
| 163 // Triggers a migration for one or more datatypes, and waits | |
| 164 // for the server to complete it. This operation is available | |
| 165 // only if ServerSupportsErrorTriggering() returned true. | |
| 166 void TriggerMigrationDoneError(const syncable::ModelTypeSet& model_types); | |
| 167 | |
| 168 // Triggers the server to set its birthday to a random value thereby | |
| 169 // the server would return a birthday error on next sync. | |
| 170 void TriggerBirthdayError(); | |
| 171 | |
| 172 // Triggers a transient error on the server. Note the server will stay in | |
| 173 // this state until shut down. | |
| 174 void TriggerTransientError(); | |
| 175 | |
| 176 // Triggers setting the sync_tabs field of the nigori node. | |
| 177 void TriggerSetSyncTabs(); | |
| 178 | |
| 179 protected: | |
| 180 // Add custom switches needed for running the test. | |
| 181 virtual void AddTestSwitches(CommandLine* cl); | |
| 182 | |
| 183 // Append the command line switches to enable experimental types that aren't | |
| 184 // on by default yet. | |
| 185 virtual void AddOptionalTypesToCommandLine(CommandLine* cl); | |
| 186 | |
| 187 // InProcessBrowserTest override. Destroys all the sync clients and sync | |
| 188 // profiles created by a test. | |
| 189 virtual void CleanUpOnMainThread() OVERRIDE; | |
| 190 | |
| 191 // InProcessBrowserTest override. Changes behavior of the default host | |
| 192 // resolver to avoid DNS lookup errors. | |
| 193 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE; | |
| 194 | |
| 195 // InProcessBrowserTest override. Resets the host resolver its default | |
| 196 // behavior. | |
| 197 virtual void TearDownInProcessBrowserTestFixture() OVERRIDE; | |
| 198 | |
| 199 // GAIA account used by the test case. | |
| 200 std::string username_; | |
| 201 | |
| 202 // GAIA password used by the test case. | |
| 203 std::string password_; | |
| 204 | |
| 205 // Locally available plain text file in which GAIA credentials are stored. | |
| 206 FilePath password_file_; | |
| 207 | |
| 208 private: | |
| 209 // Helper to ProfileManager::CreateProfile that handles path creation. | |
| 210 static Profile* MakeProfile(const FilePath::StringType name); | |
| 211 | |
| 212 // Helper method used to read GAIA credentials from a local password file | |
| 213 // specified via the "--password-file-for-test" command line switch. | |
| 214 // Note: The password file must be a plain text file with exactly two lines -- | |
| 215 // the username on the first line and the password on the second line. | |
| 216 void ReadPasswordFile(); | |
| 217 | |
| 218 // Helper method that starts up a sync test server if required. | |
| 219 void SetUpTestServerIfRequired(); | |
| 220 | |
| 221 // Helper method used to start up a local python test server. Note: We set up | |
| 222 // an XMPP-only python server if |server_type_| is LOCAL_LIVE_SERVER and mock | |
| 223 // gaia credentials are in use. Returns true if successful. | |
| 224 bool SetUpLocalPythonTestServer(); | |
| 225 | |
| 226 // Helper method used to start up a local sync test server. Returns true if | |
| 227 // successful. | |
| 228 bool SetUpLocalTestServer(); | |
| 229 | |
| 230 // Helper method used to destroy the local python sync test server if one was | |
| 231 // created. Returns true if successful. | |
| 232 bool TearDownLocalPythonTestServer(); | |
| 233 | |
| 234 // Helper method used to destroy the local sync test server if one was | |
| 235 // created. Returns true if successful. | |
| 236 bool TearDownLocalTestServer(); | |
| 237 | |
| 238 // Helper method that waits for up to |time_ms| milliseconds for the test | |
| 239 // server to start. Splits the time into |intervals| intervals, and polls the | |
| 240 // server after each interval to see if it has started. Returns true if | |
| 241 // successful. | |
| 242 bool WaitForTestServerToStart(int time_ms, int intervals); | |
| 243 | |
| 244 // Helper method used to check if the test server is up and running. | |
| 245 bool IsTestServerRunning(); | |
| 246 | |
| 247 // Used to disable and enable network connectivity by providing and | |
| 248 // clearing an invalid proxy configuration. | |
| 249 void SetProxyConfig(net::URLRequestContextGetter* context, | |
| 250 const net::ProxyConfig& proxy_config); | |
| 251 | |
| 252 // Helper method used to set up fake responses for kClientLoginUrl, | |
| 253 // kIssueAuthTokenUrl, kGetUserInfoUrl and kSearchDomainCheckUrl in order to | |
| 254 // mock out calls to GAIA servers. | |
| 255 void SetupMockGaiaResponses(); | |
| 256 | |
| 257 // Test server of type sync, started on demand. | |
| 258 net::TestServer sync_server_; | |
| 259 | |
| 260 // Helper class to whitelist the notification port. | |
| 261 scoped_ptr<net::ScopedPortException> xmpp_port_; | |
| 262 | |
| 263 // Used to differentiate between single-client, two-client, multi-client and | |
| 264 // many-client tests. | |
| 265 TestType test_type_; | |
| 266 | |
| 267 // Tells us what kind of server we're using (some tests run only on certain | |
| 268 // server types). | |
| 269 ServerType server_type_; | |
| 270 | |
| 271 // Number of sync clients that will be created by a test. | |
| 272 int num_clients_; | |
| 273 | |
| 274 // Collection of sync profiles used by a test. A sync profile maintains sync | |
| 275 // data contained within its own subdirectory under the chrome user data | |
| 276 // directory. | |
| 277 ScopedVector<Profile> profiles_; | |
| 278 | |
| 279 // Collection of pointers to the browser objects used by a test. One browser | |
| 280 // instance is created for each sync profile. Browser object lifetime is | |
| 281 // managed by BrowserList, so we don't use a ScopedVector here. | |
| 282 std::vector<Browser*> browsers_; | |
| 283 | |
| 284 // Collection of sync clients used by a test. A sync client is associated with | |
| 285 // a sync profile, and implements methods that sync the contents of the | |
| 286 // profile with the server. | |
| 287 ScopedVector<ProfileSyncServiceHarness> clients_; | |
| 288 | |
| 289 // Sync profile against which changes to individual profiles are verified. We | |
| 290 // don't need a corresponding verifier sync client because the contents of the | |
| 291 // verifier profile are strictly local, and are not meant to be synced. | |
| 292 scoped_ptr<Profile> verifier_; | |
| 293 | |
| 294 // Indicates whether changes to a profile should also change the verifier | |
| 295 // profile or not. | |
| 296 bool use_verifier_; | |
| 297 | |
| 298 // Sync integration tests need to make live DNS requests for access to | |
| 299 // GAIA and sync server URLs under google.com. We use a scoped version | |
| 300 // to override the default resolver while the test is active. | |
| 301 scoped_ptr<net::ScopedDefaultHostResolverProc> mock_host_resolver_override_; | |
| 302 | |
| 303 // Used to start and stop the local test server. | |
| 304 base::ProcessHandle test_server_handle_; | |
| 305 | |
| 306 // Fake URLFetcher factory used to mock out GAIA signin. | |
| 307 scoped_ptr<FakeURLFetcherFactory> fake_factory_; | |
| 308 | |
| 309 // The URLFetcherFactory instance used to instantiate |fake_factory_|. | |
| 310 scoped_ptr<URLFetcherFactory> factory_; | |
| 311 | |
| 312 DISALLOW_COPY_AND_ASSIGN(LiveSyncTest); | |
| 313 }; | |
| 314 | |
| 315 DISABLE_RUNNABLE_METHOD_REFCOUNT(LiveSyncTest); | |
| 316 | |
| 317 #endif // CHROME_TEST_LIVE_SYNC_LIVE_SYNC_TEST_H_ | |
| OLD | NEW |