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. 4 5 #include <string> 6 7 #include "base/strings/string16.h" 8 #include "base/strings/stringprintf.h" 9 #include "base/strings/utf_string_conversions.h" 10 #include "chrome/browser/extensions/api/push_messaging/sync_setup_helper.h" 11 #include "chrome/browser/extensions/extension_apitest.h" 12 #include "chrome/browser/extensions/extension_service.h" 13 #include "chrome/browser/ui/browser.h" 14 #include "chrome/browser/ui/tabs/tab_strip_model.h" 15 #include "chrome/common/chrome_switches.h" 16 #include "chrome/test/base/ui_test_utils.h" 17 #include "content/public/browser/render_frame_host.h" 18 #include "extensions/common/extension_set.h" 19 #include "net/dns/mock_host_resolver.h" 20 21 namespace { 22 const char kTestExtensionId[] = "mfaehphpebmlbfdiegjnpidmibldjbjk"; 23 const char kPasswordFileForTest[] = "password-file-for-test"; 24 const char kOverrideUserDataDir[] = "override-user-data-dir"; 25 } // namespace 26 27 namespace extensions { 28 29 // This class provides tests specific to the push messaging 30 // canary test server. These tests require network access, 31 // and should not be run by normal buildbots as part of the normal 32 // checkin process. 33 class PushMessagingCanaryTest : public ExtensionApiTest { 34 public: 35 PushMessagingCanaryTest() { 36 sync_setup_helper_.reset(new SyncSetupHelper()); 37 } 38 39 virtual ~PushMessagingCanaryTest() { 40 } 41 42 virtual void SetUp() OVERRIDE { 43 CommandLine* command_line = CommandLine::ForCurrentProcess(); 44 45 ASSERT_TRUE(command_line->HasSwitch(kPasswordFileForTest)); 46 base::FilePath password_file = 47 command_line->GetSwitchValuePath(kPasswordFileForTest); 48 ASSERT_TRUE(sync_setup_helper_->ReadPasswordFile(password_file)); 49 50 // The test framework overrides any command line user-data-dir 51 // argument with a /tmp/.org.chromium.Chromium.XXXXXX directory. 52 // That happens in the ChromeTestLauncherDelegate, and affects 53 // all unit tests (no opt out available). It intentionally erases 54 // any --user-data-dir switch if present and appends a new one. 55 // Re-override the default data dir for our test so we can persist 56 // the profile for this particular test so we can persist the max 57 // invalidation version between runs. 58 const base::FilePath& override_user_data_dir = 59 command_line->GetSwitchValuePath(kOverrideUserDataDir); 60 ASSERT_TRUE(!override_user_data_dir.empty()); 61 command_line->AppendSwitchPath(switches::kUserDataDir, 62 base::FilePath(override_user_data_dir)); 63 LOG(INFO) << "command line file override switch is " 64 << override_user_data_dir.value(); 65 66 ExtensionApiTest::SetUp(); 67 } 68 69 void InitializeSync() { 70 ASSERT_TRUE(sync_setup_helper_->InitializeSync(profile())); 71 } 72 73 // InProcessBrowserTest override. Destroys the sync client and sync 74 // profile created by the test. We must clean up ProfileSyncServiceHarness 75 // now before the profile is cleaned up. 76 virtual void CleanUpOnMainThread() OVERRIDE { 77 sync_setup_helper_.reset(); 78 } 79 80 const SyncSetupHelper* sync_setup_helper() const { 81 return sync_setup_helper_.get(); 82 } 83 84 protected: 85 // Override InProcessBrowserTest. Change behavior of the default host 86 // resolver to avoid DNS lookup errors, so we can make network calls. 87 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE { 88 // The resolver object lifetime is managed by sync_test_setup, not here. 89 EnableDNSLookupForThisTest( 90 new net::RuleBasedHostResolverProc(host_resolver())); 91 } 92 93 virtual void TearDownInProcessBrowserTestFixture() OVERRIDE { 94 DisableDNSLookupForThisTest(); 95 } 96 97 98 // Change behavior of the default host resolver to allow DNS lookup 99 // to proceed instead of being blocked by the test infrastructure. 100 void EnableDNSLookupForThisTest( 101 net::RuleBasedHostResolverProc* host_resolver) { 102 // mock_host_resolver_override_ takes ownership of the resolver. 103 scoped_refptr<net::RuleBasedHostResolverProc> resolver = 104 new net::RuleBasedHostResolverProc(host_resolver); 105 resolver->AllowDirectLookup("*.google.com"); 106 // On Linux, we use Chromium's NSS implementation which uses the following 107 // hosts for certificate verification. Without these overrides, running the 108 // integration tests on Linux causes error as we make external DNS lookups. 109 resolver->AllowDirectLookup("*.thawte.com"); 110 resolver->AllowDirectLookup("*.geotrust.com"); 111 resolver->AllowDirectLookup("*.gstatic.com"); 112 resolver->AllowDirectLookup("*.googleapis.com"); 113 mock_host_resolver_override_.reset( 114 new net::ScopedDefaultHostResolverProc(resolver.get())); 115 } 116 117 // We need to reset the DNS lookup when we finish, or the test will fail. 118 void DisableDNSLookupForThisTest() { 119 mock_host_resolver_override_.reset(); 120 } 121 122 private: 123 scoped_ptr<SyncSetupHelper> sync_setup_helper_; 124 125 // This test needs to make live DNS requests for access to 126 // GAIA and sync server URLs under google.com. We use a scoped version 127 // to override the default resolver while the test is active. 128 scoped_ptr<net::ScopedDefaultHostResolverProc> mock_host_resolver_override_; 129 }; 130 131 // Test that a push can make a round trip through the servers. 132 // This test is disabled to keep it from running on trybots since 133 // it requires network access, and it is not a good idea to run 134 // this test as part of a checkin or nightly test. 135 IN_PROC_BROWSER_TEST_F(PushMessagingCanaryTest, MANUAL_ReceivesPush) { 136 InitializeSync(); 137 138 const ExtensionSet* installed_extensions = extension_service()->extensions(); 139 if (!installed_extensions->Contains(kTestExtensionId)) { 140 const Extension* extension = 141 LoadExtension(test_data_dir_.AppendASCII("push_messaging_canary")); 142 ASSERT_TRUE(extension); 143 } 144 ASSERT_TRUE(installed_extensions->Contains(kTestExtensionId)); 145 146 ResultCatcher catcher; 147 catcher.RestrictToProfile(profile()); 148 149 const Extension* extension = 150 extension_service()->extensions()->GetByID(kTestExtensionId); 151 ASSERT_TRUE(extension); 152 ui_test_utils::NavigateToURL( 153 browser(), extension->GetResourceURL("push_messaging_canary.html")); 154 155 const std::string& client_id = sync_setup_helper()->client_id(); 156 const std::string& client_secret = sync_setup_helper()->client_secret(); 157 const std::string& refresh_token = sync_setup_helper()->refresh_token(); 158 159 const base::string16& script_string = base::UTF8ToUTF16(base::StringPrintf( 160 "startTestWithCredentials('%s', '%s', '%s');", 161 client_id.c_str(), client_secret.c_str(), refresh_token.c_str())); 162 163 browser()->tab_strip_model()->GetActiveWebContents()->GetMainFrame()-> 164 ExecuteJavaScript(script_string); 165 166 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); 167 } 168 169 } // namespace extensions 170