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