1 // Copyright (c) 2012 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 #include "chrome/browser/sync/test/integration/sync_test.h"
9 #include "base/basictypes.h"
10 #include "base/bind.h"
11 #include "base/command_line.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/message_loop/message_loop.h"
15 #include "base/path_service.h"
16 #include "base/process/launch.h"
17 #include "base/strings/string_split.h"
18 #include "base/strings/string_util.h"
19 #include "base/strings/stringprintf.h"
20 #include "base/strings/utf_string_conversions.h"
21 #include "base/synchronization/waitable_event.h"
22 #include "base/test/test_timeouts.h"
23 #include "base/threading/platform_thread.h"
24 #include "base/values.h"
25 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
26 #include "chrome/browser/chrome_notification_types.h"
27 #include "chrome/browser/history/history_service_factory.h"
28 #include "chrome/browser/invalidation/profile_invalidation_provider_factory.h"
29 #include "chrome/browser/lifetime/application_lifetime.h"
30 #include "chrome/browser/profiles/profile_manager.h"
31 #include "chrome/browser/search_engines/template_url_service_factory.h"
32 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
33 #include "chrome/browser/signin/signin_manager_factory.h"
34 #include "chrome/browser/sync/glue/invalidation_helper.h"
35 #include "chrome/browser/sync/profile_sync_service.h"
36 #include "chrome/browser/sync/profile_sync_service_factory.h"
37 #include "chrome/browser/sync/test/integration/fake_server_invalidation_service.h"
38 #include "chrome/browser/sync/test/integration/p2p_invalidation_forwarder.h"
39 #include "chrome/browser/sync/test/integration/p2p_sync_refresher.h"
40 #include "chrome/browser/sync/test/integration/profile_sync_service_harness.h"
41 #include "chrome/browser/sync/test/integration/single_client_status_change_checker.h"
42 #include "chrome/browser/sync/test/integration/sync_datatype_helper.h"
43 #include "chrome/browser/sync/test/integration/sync_integration_test_util.h"
44 #include "chrome/browser/ui/browser.h"
45 #include "chrome/browser/ui/browser_finder.h"
46 #include "chrome/browser/ui/host_desktop.h"
47 #include "chrome/browser/ui/tabs/tab_strip_model.h"
48 #include "chrome/browser/ui/webui/signin/login_ui_service.h"
49 #include "chrome/browser/ui/webui/signin/login_ui_service_factory.h"
50 #include "chrome/common/chrome_constants.h"
51 #include "chrome/common/chrome_paths.h"
52 #include "chrome/common/chrome_switches.h"
53 #include "chrome/test/base/testing_browser_process.h"
54 #include "chrome/test/base/ui_test_utils.h"
55 #include "components/bookmarks/test/bookmark_test_helpers.h"
56 #include "components/google/core/browser/google_url_tracker.h"
57 #include "components/invalidation/impl/invalidation_switches.h"
58 #include "components/invalidation/impl/p2p_invalidation_service.h"
59 #include "components/invalidation/impl/p2p_invalidator.h"
60 #include "components/invalidation/impl/profile_invalidation_provider.h"
61 #include "components/invalidation/public/invalidation_service.h"
62 #include "components/keyed_service/core/keyed_service.h"
63 #include "components/os_crypt/os_crypt.h"
64 #include "components/search_engines/template_url_service.h"
65 #include "components/signin/core/browser/profile_identity_provider.h"
66 #include "components/signin/core/browser/signin_manager.h"
67 #include "content/public/browser/notification_service.h"
68 #include "content/public/browser/web_contents.h"
69 #include "content/public/test/test_browser_thread.h"
70 #include "google_apis/gaia/gaia_urls.h"
71 #include "net/base/escape.h"
72 #include "net/base/load_flags.h"
73 #include "net/base/network_change_notifier.h"
74 #include "net/base/port_util.h"
75 #include "net/cookies/cookie_monster.h"
76 #include "net/test/spawned_test_server/spawned_test_server.h"
77 #include "net/url_request/test_url_fetcher_factory.h"
78 #include "net/url_request/url_fetcher.h"
79 #include "net/url_request/url_fetcher_delegate.h"
80 #include "net/url_request/url_request_context.h"
81 #include "net/url_request/url_request_context_getter.h"
82 #include "sync/engine/sync_scheduler_impl.h"
83 #include "sync/protocol/sync.pb.h"
84 #include "sync/test/fake_server/fake_server.h"
85 #include "sync/test/fake_server/fake_server_network_resources.h"
88 #if defined(OS_CHROMEOS)
89 #include "chromeos/chromeos_switches.h"
92 using content::BrowserThread
;
95 const char kPasswordFileForTest
[] = "password-file-for-test";
96 const char kSyncUserForTest
[] = "sync-user-for-test";
97 const char kSyncPasswordForTest
[] = "sync-password-for-test";
98 const char kSyncServerCommandLine
[] = "sync-server-command-line";
103 // Helper class that checks whether a sync test server is running or not.
104 class SyncServerStatusChecker
: public net::URLFetcherDelegate
{
106 SyncServerStatusChecker() : running_(false) {}
108 void OnURLFetchComplete(const net::URLFetcher
* source
) override
{
110 source
->GetResponseAsString(&data
);
112 (source
->GetStatus().status() == net::URLRequestStatus::SUCCESS
&&
113 source
->GetResponseCode() == 200 && data
.find("ok") == 0);
114 base::MessageLoop::current()->Quit();
117 bool running() const { return running_
; }
123 bool IsEncryptionComplete(const ProfileSyncService
* service
) {
124 return service
->EncryptEverythingEnabled() && !service
->encryption_pending();
127 // Helper class to wait for encryption to complete.
128 class EncryptionChecker
: public SingleClientStatusChangeChecker
{
130 explicit EncryptionChecker(ProfileSyncService
* service
)
131 : SingleClientStatusChangeChecker(service
) {}
133 bool IsExitConditionSatisfied() override
{
134 return IsEncryptionComplete(service());
137 std::string
GetDebugMessage() const override
{ return "Encryption"; }
140 void SetupNetworkCallback(
141 base::WaitableEvent
* done
,
142 net::URLRequestContextGetter
* url_request_context_getter
) {
143 url_request_context_getter
->GetURLRequestContext()->
144 set_cookie_store(new net::CookieMonster(NULL
, NULL
));
148 scoped_ptr
<KeyedService
> BuildFakeServerProfileInvalidationProvider(
149 content::BrowserContext
* context
) {
150 return make_scoped_ptr(new invalidation::ProfileInvalidationProvider(
151 scoped_ptr
<invalidation::InvalidationService
>(
152 new fake_server::FakeServerInvalidationService
)));
155 scoped_ptr
<KeyedService
> BuildP2PProfileInvalidationProvider(
156 content::BrowserContext
* context
,
157 syncer::P2PNotificationTarget notification_target
) {
158 Profile
* profile
= static_cast<Profile
*>(context
);
159 return make_scoped_ptr(new invalidation::ProfileInvalidationProvider(
160 scoped_ptr
<invalidation::InvalidationService
>(
161 new invalidation::P2PInvalidationService(
162 scoped_ptr
<IdentityProvider
>(new ProfileIdentityProvider(
163 SigninManagerFactory::GetForProfile(profile
),
164 ProfileOAuth2TokenServiceFactory::GetForProfile(profile
),
165 LoginUIServiceFactory::GetShowLoginPopupCallbackForProfile(
167 profile
->GetRequestContext(), notification_target
))));
170 scoped_ptr
<KeyedService
> BuildSelfNotifyingP2PProfileInvalidationProvider(
171 content::BrowserContext
* context
) {
172 return BuildP2PProfileInvalidationProvider(context
, syncer::NOTIFY_ALL
);
175 scoped_ptr
<KeyedService
> BuildRealisticP2PProfileInvalidationProvider(
176 content::BrowserContext
* context
) {
177 return BuildP2PProfileInvalidationProvider(context
, syncer::NOTIFY_OTHERS
);
182 SyncTest::SyncTest(TestType test_type
)
183 : test_type_(test_type
),
184 server_type_(SERVER_TYPE_UNDECIDED
),
187 notifications_enabled_(true) {
188 sync_datatype_helper::AssociateWithTest(this);
189 switch (test_type_
) {
191 case SINGLE_CLIENT_LEGACY
: {
196 case TWO_CLIENT_LEGACY
: {
200 case MULTIPLE_CLIENT
: {
207 SyncTest::~SyncTest() {}
209 void SyncTest::SetUp() {
210 base::CommandLine
* cl
= base::CommandLine::ForCurrentProcess();
211 if (cl
->HasSwitch(switches::kPasswordFileForTest
)) {
213 } else if (cl
->HasSwitch(switches::kSyncUserForTest
) &&
214 cl
->HasSwitch(switches::kSyncPasswordForTest
)) {
215 username_
= cl
->GetSwitchValueASCII(switches::kSyncUserForTest
);
216 password_
= cl
->GetSwitchValueASCII(switches::kSyncPasswordForTest
);
218 username_
= "user@gmail.com";
219 password_
= "password";
222 if (username_
.empty() || password_
.empty())
223 LOG(FATAL
) << "Cannot run sync tests without GAIA credentials.";
225 // Sets |server_type_| if it wasn't specified by the test.
228 // Mock the Mac Keychain service. The real Keychain can block on user input.
229 #if defined(OS_MACOSX)
230 OSCrypt::UseMockKeychain(true);
233 // Start up a sync test server if one is needed and setup mock gaia responses.
234 // Note: This must be done prior to the call to SetupClients() because we want
235 // the mock gaia responses to be available before GaiaUrls is initialized.
236 SetUpTestServerIfRequired();
238 // Yield control back to the InProcessBrowserTest framework.
239 InProcessBrowserTest::SetUp();
242 void SyncTest::TearDown() {
243 // Clear any mock gaia responses that might have been set.
244 ClearMockGaiaResponses();
246 // Allow the InProcessBrowserTest framework to perform its tear down.
247 InProcessBrowserTest::TearDown();
249 // Stop the local python test server. This is a no-op if one wasn't started.
250 TearDownLocalPythonTestServer();
252 // Stop the local sync test server. This is a no-op if one wasn't started.
253 TearDownLocalTestServer();
255 fake_server_
.reset();
258 void SyncTest::SetUpCommandLine(base::CommandLine
* cl
) {
260 AddOptionalTypesToCommandLine(cl
);
262 #if defined(OS_CHROMEOS)
263 cl
->AppendSwitch(chromeos::switches::kIgnoreUserProfileMappingForTests
);
267 void SyncTest::AddTestSwitches(base::CommandLine
* cl
) {
268 // Disable non-essential access of external network resources.
269 if (!cl
->HasSwitch(switches::kDisableBackgroundNetworking
))
270 cl
->AppendSwitch(switches::kDisableBackgroundNetworking
);
272 if (!cl
->HasSwitch(switches::kSyncShortInitialRetryOverride
))
273 cl
->AppendSwitch(switches::kSyncShortInitialRetryOverride
);
276 void SyncTest::AddOptionalTypesToCommandLine(base::CommandLine
* cl
) {}
278 // Called when the ProfileManager has created a profile.
280 void SyncTest::CreateProfileCallback(const base::Closure
& quit_closure
,
282 Profile::CreateStatus status
) {
283 EXPECT_TRUE(profile
);
284 EXPECT_NE(Profile::CREATE_STATUS_LOCAL_FAIL
, status
);
285 EXPECT_NE(Profile::CREATE_STATUS_REMOTE_FAIL
, status
);
286 // This will be called multiple times. Wait until the profile is initialized
287 // fully to quit the loop.
288 if (status
== Profile::CREATE_STATUS_INITIALIZED
)
292 // TODO(shadi): Ideally creating a new profile should not depend on signin
293 // process. We should try to consolidate MakeProfileForUISignin() and
294 // MakeProfile(). Major differences are profile paths and creation methods. For
295 // UI signin we need profiles in unique user data dir's and we need to use
296 // ProfileManager::CreateProfileAsync() for proper profile creation.
298 Profile
* SyncTest::MakeProfileForUISignin(
299 const base::FilePath::StringType name
) {
300 // For multi profile UI signin, profile paths should be outside user data dir.
301 // Otherwise, we get an error that the profile has already signed in on this
303 // Note that prefix |name| is implemented only on Win. On other platforms the
304 // com.google.Chrome.XXXXXX prefix is used.
305 base::FilePath profile_path
;
306 CHECK(base::CreateNewTempDirectory(name
, &profile_path
));
308 ProfileManager
* profile_manager
= g_browser_process
->profile_manager();
309 base::RunLoop run_loop
;
310 ProfileManager::CreateCallback create_callback
= base::Bind(
311 &CreateProfileCallback
, run_loop
.QuitClosure());
312 profile_manager
->CreateProfileAsync(profile_path
,
318 return profile_manager
->GetProfileByPath(profile_path
);
321 Profile
* SyncTest::MakeProfile(const base::FilePath::StringType name
) {
323 // Create new profiles in user data dir so that other profiles can know about
324 // it. This is needed in tests such as supervised user cases which assume
325 // browser->profile() as the custodian profile.
326 PathService::Get(chrome::DIR_USER_DATA
, &path
);
327 path
= path
.Append(name
);
329 if (!base::PathExists(path
))
330 CHECK(base::CreateDirectory(path
));
332 if (!preexisting_preferences_file_contents_
.empty()) {
333 base::FilePath
pref_path(path
.Append(chrome::kPreferencesFilename
));
334 const char* contents
= preexisting_preferences_file_contents_
.c_str();
335 size_t contents_length
= preexisting_preferences_file_contents_
.size();
336 if (!base::WriteFile(pref_path
, contents
, contents_length
)) {
337 LOG(FATAL
) << "Preexisting Preferences file could not be written.";
342 Profile::CreateProfile(path
, NULL
, Profile::CREATE_MODE_SYNCHRONOUS
);
343 g_browser_process
->profile_manager()->RegisterTestingProfile(profile
,
349 Profile
* SyncTest::GetProfile(int index
) {
350 if (profiles_
.empty())
351 LOG(FATAL
) << "SetupClients() has not yet been called.";
352 if (index
< 0 || index
>= static_cast<int>(profiles_
.size()))
353 LOG(FATAL
) << "GetProfile(): Index is out of bounds.";
354 return profiles_
[index
];
357 Browser
* SyncTest::GetBrowser(int index
) {
358 if (browsers_
.empty())
359 LOG(FATAL
) << "SetupClients() has not yet been called.";
360 if (index
< 0 || index
>= static_cast<int>(browsers_
.size()))
361 LOG(FATAL
) << "GetBrowser(): Index is out of bounds.";
362 return browsers_
[index
];
365 ProfileSyncServiceHarness
* SyncTest::GetClient(int index
) {
366 if (clients_
.empty())
367 LOG(FATAL
) << "SetupClients() has not yet been called.";
368 if (index
< 0 || index
>= static_cast<int>(clients_
.size()))
369 LOG(FATAL
) << "GetClient(): Index is out of bounds.";
370 return clients_
[index
];
373 ProfileSyncService
* SyncTest::GetSyncService(int index
) {
374 return ProfileSyncServiceFactory::GetForProfile(GetProfile(index
));
377 std::vector
<ProfileSyncService
*> SyncTest::GetSyncServices() {
378 std::vector
<ProfileSyncService
*> services
;
379 for (int i
= 0; i
< num_clients(); ++i
) {
380 services
.push_back(GetSyncService(i
));
385 Profile
* SyncTest::verifier() {
387 LOG(FATAL
) << "Verifier account is disabled.";
388 if (verifier_
== NULL
)
389 LOG(FATAL
) << "SetupClients() has not yet been called.";
393 void SyncTest::DisableVerifier() {
394 use_verifier_
= false;
397 bool SyncTest::SetupClients() {
398 if (num_clients_
<= 0)
399 LOG(FATAL
) << "num_clients_ incorrectly initialized.";
400 if (!profiles_
.empty() || !browsers_
.empty() || !clients_
.empty())
401 LOG(FATAL
) << "SetupClients() has already been called.";
403 // Create the required number of sync profiles, browsers and clients.
404 profiles_
.resize(num_clients_
);
405 browsers_
.resize(num_clients_
);
406 clients_
.resize(num_clients_
);
407 invalidation_forwarders_
.resize(num_clients_
);
408 sync_refreshers_
.resize(num_clients_
);
409 fake_server_invalidation_services_
.resize(num_clients_
);
410 for (int i
= 0; i
< num_clients_
; ++i
) {
411 InitializeInstance(i
);
414 // Verifier account is not useful when running against external servers.
415 if (UsingExternalServers())
418 // Create the verifier profile.
420 verifier_
= MakeProfile(FILE_PATH_LITERAL("Verifier"));
421 bookmarks::test::WaitForBookmarkModelToLoad(
422 BookmarkModelFactory::GetForProfile(verifier()));
423 ui_test_utils::WaitForHistoryToLoad(HistoryServiceFactory::GetForProfile(
424 verifier(), ServiceAccessType::EXPLICIT_ACCESS
));
425 ui_test_utils::WaitForTemplateURLServiceToLoad(
426 TemplateURLServiceFactory::GetForProfile(verifier()));
428 // Error cases are all handled by LOG(FATAL) messages. So there is not really
429 // a case that returns false. In case we failed to create a verifier profile,
430 // any call to the verifier() would fail.
434 void SyncTest::InitializeInstance(int index
) {
435 base::FilePath::StringType profile_name
=
436 base::StringPrintf(FILE_PATH_LITERAL("Profile%d"), index
);
437 // If running against an EXTERNAL_LIVE_SERVER, we need to signin profiles
438 // using real GAIA server. This requires creating profiles with no test hooks.
439 if (UsingExternalServers()) {
440 profiles_
[index
] = MakeProfileForUISignin(profile_name
);
442 // Without need of real GAIA authentication, we create new test profiles.
443 profiles_
[index
] = MakeProfile(profile_name
);
446 EXPECT_FALSE(GetProfile(index
) == NULL
) << "Could not create Profile "
449 // CheckInitialState() assumes that no windows are open at startup.
450 browsers_
[index
] = new Browser(Browser::CreateParams(
451 GetProfile(index
), chrome::GetActiveDesktop()));
453 EXPECT_FALSE(GetBrowser(index
) == NULL
) << "Could not create Browser "
456 // Make sure the ProfileSyncService has been created before creating the
457 // ProfileSyncServiceHarness - some tests expect the ProfileSyncService to
459 ProfileSyncService
* profile_sync_service
=
460 ProfileSyncServiceFactory::GetForProfile(GetProfile(index
));
462 SetupNetwork(GetProfile(index
)->GetRequestContext());
464 if (server_type_
== IN_PROCESS_FAKE_SERVER
) {
465 // TODO(pvalenzuela): Run the fake server via EmbeddedTestServer.
466 profile_sync_service
->OverrideNetworkResourcesForTest(
467 make_scoped_ptr
<syncer::NetworkResources
>(
468 new fake_server::FakeServerNetworkResources(
469 fake_server_
->AsWeakPtr())));
472 ProfileSyncServiceHarness::SigninType singin_type
= UsingExternalServers()
473 ? ProfileSyncServiceHarness::SigninType::UI_SIGNIN
474 : ProfileSyncServiceHarness::SigninType::FAKE_SIGNIN
;
477 ProfileSyncServiceHarness::Create(GetProfile(index
),
481 EXPECT_FALSE(GetClient(index
) == NULL
) << "Could not create Client "
483 InitializeInvalidations(index
);
485 bookmarks::test::WaitForBookmarkModelToLoad(
486 BookmarkModelFactory::GetForProfile(GetProfile(index
)));
487 ui_test_utils::WaitForHistoryToLoad(HistoryServiceFactory::GetForProfile(
488 GetProfile(index
), ServiceAccessType::EXPLICIT_ACCESS
));
489 ui_test_utils::WaitForTemplateURLServiceToLoad(
490 TemplateURLServiceFactory::GetForProfile(GetProfile(index
)));
493 void SyncTest::InitializeInvalidations(int index
) {
494 if (UsingExternalServers()) {
495 // DO NOTHING. External live sync servers use GCM to notify profiles of any
496 // invalidations in sync'ed data. In this case, to notify other profiles of
497 // invalidations, we use sync refresh notifications instead.
498 } else if (server_type_
== IN_PROCESS_FAKE_SERVER
) {
499 CHECK(fake_server_
.get());
500 fake_server::FakeServerInvalidationService
* invalidation_service
=
501 static_cast<fake_server::FakeServerInvalidationService
*>(
502 static_cast<invalidation::ProfileInvalidationProvider
*>(
503 invalidation::ProfileInvalidationProviderFactory::
504 GetInstance()->SetTestingFactoryAndUse(
506 BuildFakeServerProfileInvalidationProvider
))->
507 GetInvalidationService());
508 fake_server_
->AddObserver(invalidation_service
);
509 if (TestUsesSelfNotifications()) {
510 invalidation_service
->EnableSelfNotifications();
512 invalidation_service
->DisableSelfNotifications();
514 fake_server_invalidation_services_
[index
] = invalidation_service
;
516 invalidation::P2PInvalidationService
* p2p_invalidation_service
=
517 static_cast<invalidation::P2PInvalidationService
*>(
518 static_cast<invalidation::ProfileInvalidationProvider
*>(
519 invalidation::ProfileInvalidationProviderFactory::
520 GetInstance()->SetTestingFactoryAndUse(
522 TestUsesSelfNotifications() ?
523 BuildSelfNotifyingP2PProfileInvalidationProvider
:
524 BuildRealisticP2PProfileInvalidationProvider
))->
525 GetInvalidationService());
526 p2p_invalidation_service
->UpdateCredentials(username_
, password_
);
527 // Start listening for and emitting notifications of commits.
528 invalidation_forwarders_
[index
] =
529 new P2PInvalidationForwarder(clients_
[index
]->service(),
530 p2p_invalidation_service
);
534 bool SyncTest::SetupSync() {
535 // Create sync profiles and clients if they haven't already been created.
536 if (profiles_
.empty()) {
538 LOG(FATAL
) << "SetupClients() failed.";
541 // Sync each of the profiles.
542 for (int i
= 0; i
< num_clients_
; ++i
) {
543 if (!GetClient(i
)->SetupSync())
544 LOG(FATAL
) << "SetupSync() failed.";
547 // Because clients may modify sync data as part of startup (for example local
548 // session-releated data is rewritten), we need to ensure all startup-based
549 // changes have propagated between the clients.
551 // Tests that don't use self-notifications can't await quiescense. They'll
552 // have to find their own way of waiting for an initial state if they really
553 // need such guarantees.
554 if (TestUsesSelfNotifications()) {
558 // SyncRefresher is used instead of invalidations to notify other profiles to
559 // do a sync refresh on committed data sets. This is only needed when running
560 // tests against external live server, otherwise invalidation service is used.
561 // With external live servers, the profiles commit data on first sync cycle
562 // automatically after signing in. To avoid misleading sync commit
563 // notifications at start up, we start the SyncRefresher observers post
565 if (UsingExternalServers()) {
566 for (int i
= 0; i
< num_clients_
; ++i
) {
567 sync_refreshers_
[i
] = new P2PSyncRefresher(clients_
[i
]->service());
570 // OneClickSigninSyncStarter observer is created with a real user sign in.
571 // It is deleted on certain conditions which are not satisfied by our tests,
572 // and this causes the SigninTracker observer to stay hanging at shutdown.
573 // Calling LoginUIService::SyncConfirmationUIClosed forces the observer to
574 // be removed. http://crbug.com/484388
575 for (int i
= 0; i
< num_clients_
; ++i
) {
576 LoginUIServiceFactory::GetForProfile(GetProfile(i
))->
577 SyncConfirmationUIClosed(false /* configure_sync_first */);
584 void SyncTest::TearDownOnMainThread() {
585 for (size_t i
= 0; i
< clients_
.size(); ++i
) {
586 clients_
[i
]->service()->RequestStop(ProfileSyncService::CLEAR_DATA
);
589 content::WindowedNotificationObserver
observer(
590 chrome::NOTIFICATION_BROWSER_CLOSED
,
591 content::NotificationService::AllSources());
592 chrome::CloseAllBrowsers();
594 // Waiting for a single notification mitigates flakiness (related to not all
595 // browsers being closed). If further flakiness is seen
596 // (GetTotalBrowserCount() > 0 after this call), GetTotalBrowserCount()
597 // notifications should be waited on.
600 if (fake_server_
.get()) {
601 std::vector
<fake_server::FakeServerInvalidationService
*>::const_iterator it
;
602 for (it
= fake_server_invalidation_services_
.begin();
603 it
!= fake_server_invalidation_services_
.end(); ++it
) {
604 fake_server_
->RemoveObserver(*it
);
608 // All browsers should be closed at this point, or else we could see memory
609 // corruption in QuitBrowser().
610 CHECK_EQ(0U, chrome::GetTotalBrowserCount());
611 invalidation_forwarders_
.clear();
612 sync_refreshers_
.clear();
613 fake_server_invalidation_services_
.clear();
617 void SyncTest::SetUpInProcessBrowserTestFixture() {
618 // We don't take a reference to |resolver|, but mock_host_resolver_override_
619 // does, so effectively assumes ownership.
620 net::RuleBasedHostResolverProc
* resolver
=
621 new net::RuleBasedHostResolverProc(host_resolver());
622 resolver
->AllowDirectLookup("*.google.com");
624 // Allow connection to googleapis.com for oauth token requests in E2E tests.
625 resolver
->AllowDirectLookup("*.googleapis.com");
627 // On Linux, we use Chromium's NSS implementation which uses the following
628 // hosts for certificate verification. Without these overrides, running the
629 // integration tests on Linux causes error as we make external DNS lookups.
630 resolver
->AllowDirectLookup("*.thawte.com");
631 resolver
->AllowDirectLookup("*.geotrust.com");
632 resolver
->AllowDirectLookup("*.gstatic.com");
633 mock_host_resolver_override_
.reset(
634 new net::ScopedDefaultHostResolverProc(resolver
));
637 void SyncTest::TearDownInProcessBrowserTestFixture() {
638 mock_host_resolver_override_
.reset();
641 void SyncTest::ReadPasswordFile() {
642 base::CommandLine
* cl
= base::CommandLine::ForCurrentProcess();
643 password_file_
= cl
->GetSwitchValuePath(switches::kPasswordFileForTest
);
644 if (password_file_
.empty())
645 LOG(FATAL
) << "Can't run live server test without specifying --"
646 << switches::kPasswordFileForTest
<< "=<filename>";
647 std::string file_contents
;
648 base::ReadFileToString(password_file_
, &file_contents
);
649 ASSERT_NE(file_contents
, "") << "Password file \""
650 << password_file_
.value() << "\" does not exist.";
651 std::vector
<std::string
> tokens
= base::SplitString(
652 file_contents
, "\r\n", base::KEEP_WHITESPACE
, base::SPLIT_WANT_NONEMPTY
);
653 ASSERT_EQ(2U, tokens
.size()) << "Password file \""
654 << password_file_
.value()
655 << "\" must contain exactly two lines of text.";
656 username_
= tokens
[0];
657 password_
= tokens
[1];
660 void SyncTest::SetupMockGaiaResponses() {
661 factory_
.reset(new net::URLFetcherImplFactory());
662 fake_factory_
.reset(new net::FakeURLFetcherFactory(factory_
.get()));
663 fake_factory_
->SetFakeResponse(
664 GaiaUrls::GetInstance()->get_user_info_url(),
665 "email=user@gmail.com\ndisplayEmail=user@gmail.com",
667 net::URLRequestStatus::SUCCESS
);
668 fake_factory_
->SetFakeResponse(
669 GaiaUrls::GetInstance()->issue_auth_token_url(),
672 net::URLRequestStatus::SUCCESS
);
673 fake_factory_
->SetFakeResponse(
674 GURL(GoogleURLTracker::kSearchDomainCheckURL
),
677 net::URLRequestStatus::SUCCESS
);
678 fake_factory_
->SetFakeResponse(
679 GaiaUrls::GetInstance()->client_login_to_oauth2_url(),
682 net::URLRequestStatus::SUCCESS
);
683 fake_factory_
->SetFakeResponse(
684 GaiaUrls::GetInstance()->oauth2_token_url(),
686 " \"refresh_token\": \"rt1\","
687 " \"access_token\": \"at1\","
688 " \"expires_in\": 3600,"
689 " \"token_type\": \"Bearer\""
692 net::URLRequestStatus::SUCCESS
);
693 fake_factory_
->SetFakeResponse(
694 GaiaUrls::GetInstance()->oauth_user_info_url(),
699 net::URLRequestStatus::SUCCESS
);
700 fake_factory_
->SetFakeResponse(
701 GaiaUrls::GetInstance()->oauth1_login_url(),
702 "SID=sid\nLSID=lsid\nAuth=auth_token",
704 net::URLRequestStatus::SUCCESS
);
705 fake_factory_
->SetFakeResponse(
706 GaiaUrls::GetInstance()->oauth2_revoke_url(),
709 net::URLRequestStatus::SUCCESS
);
712 void SyncTest::SetOAuth2TokenResponse(const std::string
& response_data
,
713 net::HttpStatusCode response_code
,
714 net::URLRequestStatus::Status status
) {
715 ASSERT_TRUE(NULL
!= fake_factory_
.get());
716 fake_factory_
->SetFakeResponse(GaiaUrls::GetInstance()->oauth2_token_url(),
717 response_data
, response_code
, status
);
720 void SyncTest::ClearMockGaiaResponses() {
721 // Clear any mock gaia responses that might have been set.
723 fake_factory_
->ClearFakeResponses();
724 fake_factory_
.reset();
727 // Cancel any outstanding URL fetches and destroy the URLFetcherImplFactory we
729 net::URLFetcher::CancelAll();
733 void SyncTest::DecideServerType() {
734 // Only set |server_type_| if it hasn't already been set. This allows for
735 // tests to explicitly set this value in each test class if needed.
736 if (server_type_
== SERVER_TYPE_UNDECIDED
) {
737 base::CommandLine
* cl
= base::CommandLine::ForCurrentProcess();
738 if (!cl
->HasSwitch(switches::kSyncServiceURL
) &&
739 !cl
->HasSwitch(switches::kSyncServerCommandLine
)) {
740 // If neither a sync server URL nor a sync server command line is
741 // provided, start up a local sync test server and point Chrome
742 // to its URL. This is the most common configuration, and the only
743 // one that makes sense for most developers. FakeServer is the
744 // current solution but some scenarios are only supported by the
745 // legacy python server.
746 switch (test_type_
) {
749 case MULTIPLE_CLIENT
:
750 server_type_
= IN_PROCESS_FAKE_SERVER
;
753 server_type_
= LOCAL_PYTHON_SERVER
;
755 } else if (cl
->HasSwitch(switches::kSyncServiceURL
) &&
756 cl
->HasSwitch(switches::kSyncServerCommandLine
)) {
757 // If a sync server URL and a sync server command line are provided,
758 // start up a local sync server by running the command line. Chrome
759 // will connect to the server at the URL that was provided.
760 server_type_
= LOCAL_LIVE_SERVER
;
761 } else if (cl
->HasSwitch(switches::kSyncServiceURL
) &&
762 !cl
->HasSwitch(switches::kSyncServerCommandLine
)) {
763 // If a sync server URL is provided, but not a server command line,
764 // it is assumed that the server is already running. Chrome will
765 // automatically connect to it at the URL provided. There is nothing
767 server_type_
= EXTERNAL_LIVE_SERVER
;
769 // If a sync server command line is provided, but not a server URL,
771 LOG(FATAL
) << "Can't figure out how to run a server.";
776 // Start up a local sync server based on the value of server_type_, which
777 // was determined from the command line parameters.
778 void SyncTest::SetUpTestServerIfRequired() {
779 if (UsingExternalServers()) {
780 // Nothing to do; we'll just talk to the URL we were given.
781 } else if (server_type_
== LOCAL_PYTHON_SERVER
) {
782 if (!SetUpLocalPythonTestServer())
783 LOG(FATAL
) << "Failed to set up local python sync and XMPP servers";
784 SetupMockGaiaResponses();
785 } else if (server_type_
== LOCAL_LIVE_SERVER
) {
786 // Using mock gaia credentials requires the use of a mock XMPP server.
787 if (username_
== "user@gmail.com" && !SetUpLocalPythonTestServer())
788 LOG(FATAL
) << "Failed to set up local python XMPP server";
789 if (!SetUpLocalTestServer())
790 LOG(FATAL
) << "Failed to set up local test server";
791 } else if (server_type_
== IN_PROCESS_FAKE_SERVER
) {
792 fake_server_
.reset(new fake_server::FakeServer());
793 SetupMockGaiaResponses();
795 LOG(FATAL
) << "Don't know which server environment to run test in.";
799 bool SyncTest::SetUpLocalPythonTestServer() {
800 EXPECT_TRUE(sync_server_
.Start())
801 << "Could not launch local python test server.";
803 base::CommandLine
* cl
= base::CommandLine::ForCurrentProcess();
804 if (server_type_
== LOCAL_PYTHON_SERVER
) {
805 std::string sync_service_url
= sync_server_
.GetURL("chromiumsync").spec();
806 cl
->AppendSwitchASCII(switches::kSyncServiceURL
, sync_service_url
);
807 DVLOG(1) << "Started local python sync server at " << sync_service_url
;
811 if (!sync_server_
.server_data().GetInteger("xmpp_port", &xmpp_port
)) {
812 LOG(ERROR
) << "Could not find valid xmpp_port value";
815 if ((xmpp_port
<= 0) || (xmpp_port
> kuint16max
)) {
816 LOG(ERROR
) << "Invalid xmpp port: " << xmpp_port
;
820 net::HostPortPair
xmpp_host_port_pair(sync_server_
.host_port_pair());
821 xmpp_host_port_pair
.set_port(xmpp_port
);
822 xmpp_port_
.reset(new net::ScopedPortException(xmpp_port
));
824 if (!cl
->HasSwitch(invalidation::switches::kSyncNotificationHostPort
)) {
825 cl
->AppendSwitchASCII(invalidation::switches::kSyncNotificationHostPort
,
826 xmpp_host_port_pair
.ToString());
827 // The local XMPP server only supports insecure connections.
828 cl
->AppendSwitch(invalidation::switches::kSyncAllowInsecureXmppConnection
);
830 DVLOG(1) << "Started local python XMPP server at "
831 << xmpp_host_port_pair
.ToString();
836 bool SyncTest::SetUpLocalTestServer() {
837 base::CommandLine
* cl
= base::CommandLine::ForCurrentProcess();
838 base::CommandLine::StringType server_cmdline_string
=
839 cl
->GetSwitchValueNative(switches::kSyncServerCommandLine
);
840 base::CommandLine::StringVector server_cmdline_vector
= base::SplitString(
841 server_cmdline_string
, FILE_PATH_LITERAL(" "),
842 base::KEEP_WHITESPACE
, base::SPLIT_WANT_NONEMPTY
);
843 base::CommandLine
server_cmdline(server_cmdline_vector
);
844 base::LaunchOptions options
;
846 options
.start_hidden
= true;
848 test_server_
= base::LaunchProcess(server_cmdline
, options
);
849 if (!test_server_
.IsValid())
850 LOG(ERROR
) << "Could not launch local test server.";
852 const base::TimeDelta kMaxWaitTime
= TestTimeouts::action_max_timeout();
853 const int kNumIntervals
= 15;
854 if (WaitForTestServerToStart(kMaxWaitTime
, kNumIntervals
)) {
855 DVLOG(1) << "Started local test server at "
856 << cl
->GetSwitchValueASCII(switches::kSyncServiceURL
) << ".";
859 LOG(ERROR
) << "Could not start local test server at "
860 << cl
->GetSwitchValueASCII(switches::kSyncServiceURL
) << ".";
865 bool SyncTest::TearDownLocalPythonTestServer() {
866 if (!sync_server_
.Stop()) {
867 LOG(ERROR
) << "Could not stop local python test server.";
874 bool SyncTest::TearDownLocalTestServer() {
875 if (test_server_
.IsValid()) {
876 EXPECT_TRUE(test_server_
.Terminate(0, false))
877 << "Could not stop local test server.";
878 test_server_
.Close();
883 bool SyncTest::WaitForTestServerToStart(base::TimeDelta wait
, int intervals
) {
884 for (int i
= 0; i
< intervals
; ++i
) {
885 if (IsTestServerRunning())
887 base::PlatformThread::Sleep(wait
/ intervals
);
892 bool SyncTest::IsTestServerRunning() {
893 base::CommandLine
* cl
= base::CommandLine::ForCurrentProcess();
894 std::string sync_url
= cl
->GetSwitchValueASCII(switches::kSyncServiceURL
);
895 GURL
sync_url_status(sync_url
.append("/healthz"));
896 SyncServerStatusChecker delegate
;
897 scoped_ptr
<net::URLFetcher
> fetcher
=
898 net::URLFetcher::Create(sync_url_status
, net::URLFetcher::GET
, &delegate
);
899 fetcher
->SetLoadFlags(net::LOAD_DISABLE_CACHE
|
900 net::LOAD_DO_NOT_SEND_COOKIES
|
901 net::LOAD_DO_NOT_SAVE_COOKIES
);
902 fetcher
->SetRequestContext(g_browser_process
->system_request_context());
904 content::RunMessageLoop();
905 return delegate
.running();
908 bool SyncTest::TestUsesSelfNotifications() {
912 bool SyncTest::EnableEncryption(int index
) {
913 ProfileSyncService
* service
= GetClient(index
)->service();
915 if (::IsEncryptionComplete(service
))
918 service
->EnableEncryptEverything();
920 // In order to kick off the encryption we have to reconfigure. Just grab the
921 // currently synced types and use them.
922 const syncer::ModelTypeSet synced_datatypes
=
923 service
->GetPreferredDataTypes();
924 bool sync_everything
= synced_datatypes
.Equals(syncer::ModelTypeSet::All());
925 service
->OnUserChoseDatatypes(sync_everything
, synced_datatypes
);
927 return AwaitEncryptionComplete(index
);
930 bool SyncTest::IsEncryptionComplete(int index
) {
931 return ::IsEncryptionComplete(GetClient(index
)->service());
934 bool SyncTest::AwaitEncryptionComplete(int index
) {
935 ProfileSyncService
* service
= GetClient(index
)->service();
936 EncryptionChecker
checker(service
);
938 return !checker
.TimedOut();
941 bool SyncTest::AwaitQuiescence() {
942 return ProfileSyncServiceHarness::AwaitQuiescence(clients());
945 bool SyncTest::UsingExternalServers() {
946 return server_type_
== EXTERNAL_LIVE_SERVER
;
949 bool SyncTest::ServerSupportsNotificationControl() const {
950 EXPECT_NE(SERVER_TYPE_UNDECIDED
, server_type_
);
952 // Supported only if we're using the python testserver.
953 return server_type_
== LOCAL_PYTHON_SERVER
;
956 void SyncTest::DisableNotificationsImpl() {
957 ASSERT_TRUE(ServerSupportsNotificationControl());
958 std::string path
= "chromiumsync/disablenotifications";
959 ui_test_utils::NavigateToURL(browser(), sync_server_
.GetURL(path
));
960 ASSERT_EQ("Notifications disabled",
962 browser()->tab_strip_model()->GetActiveWebContents()->
966 void SyncTest::DisableNotifications() {
967 DisableNotificationsImpl();
968 notifications_enabled_
= false;
971 void SyncTest::EnableNotificationsImpl() {
972 ASSERT_TRUE(ServerSupportsNotificationControl());
973 std::string path
= "chromiumsync/enablenotifications";
974 ui_test_utils::NavigateToURL(browser(), sync_server_
.GetURL(path
));
975 ASSERT_EQ("Notifications enabled",
977 browser()->tab_strip_model()->GetActiveWebContents()->
981 void SyncTest::EnableNotifications() {
982 EnableNotificationsImpl();
983 notifications_enabled_
= true;
986 void SyncTest::TriggerNotification(syncer::ModelTypeSet changed_types
) {
987 ASSERT_TRUE(ServerSupportsNotificationControl());
988 const std::string
& data
=
989 syncer::P2PNotificationData(
992 syncer::ObjectIdInvalidationMap::InvalidateAll(
993 syncer::ModelTypeSetToObjectIdSet(changed_types
))).ToString();
994 const std::string
& path
=
995 std::string("chromiumsync/sendnotification?channel=") +
996 syncer::kSyncP2PNotificationChannel
+ "&data=" + data
;
997 ui_test_utils::NavigateToURL(browser(), sync_server_
.GetURL(path
));
998 ASSERT_EQ("Notification sent",
1000 browser()->tab_strip_model()->GetActiveWebContents()->
1004 bool SyncTest::ServerSupportsErrorTriggering() const {
1005 EXPECT_NE(SERVER_TYPE_UNDECIDED
, server_type_
);
1007 // Supported only if we're using the python testserver.
1008 return server_type_
== LOCAL_PYTHON_SERVER
;
1011 void SyncTest::TriggerMigrationDoneError(syncer::ModelTypeSet model_types
) {
1012 ASSERT_TRUE(ServerSupportsErrorTriggering());
1013 std::string path
= "chromiumsync/migrate";
1015 for (syncer::ModelTypeSet::Iterator it
= model_types
.First();
1016 it
.Good(); it
.Inc()) {
1019 "%ctype=%d", joiner
,
1020 syncer::GetSpecificsFieldNumberFromModelType(it
.Get())));
1023 ui_test_utils::NavigateToURL(browser(), sync_server_
.GetURL(path
));
1024 ASSERT_EQ("Migration: 200",
1026 browser()->tab_strip_model()->GetActiveWebContents()->
1030 void SyncTest::TriggerXmppAuthError() {
1031 ASSERT_TRUE(ServerSupportsErrorTriggering());
1032 std::string path
= "chromiumsync/xmppcred";
1033 ui_test_utils::NavigateToURL(browser(), sync_server_
.GetURL(path
));
1036 void SyncTest::TriggerCreateSyncedBookmarks() {
1037 ASSERT_TRUE(ServerSupportsErrorTriggering());
1038 std::string path
= "chromiumsync/createsyncedbookmarks";
1039 ui_test_utils::NavigateToURL(browser(), sync_server_
.GetURL(path
));
1040 ASSERT_EQ("Synced Bookmarks",
1042 browser()->tab_strip_model()->GetActiveWebContents()->
1046 void SyncTest::SetupNetwork(net::URLRequestContextGetter
* context_getter
) {
1047 base::WaitableEvent
done(false, false);
1048 BrowserThread::PostTask(
1049 BrowserThread::IO
, FROM_HERE
,
1050 base::Bind(&SetupNetworkCallback
, &done
,
1051 make_scoped_refptr(context_getter
)));
1055 fake_server::FakeServer
* SyncTest::GetFakeServer() const {
1056 return fake_server_
.get();
1059 void SyncTest::TriggerSyncForModelTypes(int index
,
1060 syncer::ModelTypeSet model_types
) {
1061 content::NotificationService::current()->Notify(
1062 chrome::NOTIFICATION_SYNC_REFRESH_LOCAL
,
1063 content::Source
<Profile
>(GetProfile(index
)),
1064 content::Details
<const syncer::ModelTypeSet
>(&model_types
));
1067 void SyncTest::SetPreexistingPreferencesFileContents(
1068 const std::string
& contents
) {
1069 preexisting_preferences_file_contents_
= contents
;