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