Commit da05d4bb authored by Karan Bhatia's avatar Karan Bhatia Committed by Commit Bot

CTS: Make ExtensionAPITest.ActiveTab work with runtime host permissions enabled.

Currently the ExtensionAPITest.ActiveTab fails with the RuntimeHostPermissions
feature since it assumes that the active tab access grant is revoked when the
page is navigated. However, with the runtime host permissions feature enabled,
the active tab grant is only revoked when the page navigates to a different
origin. Fix the test and also test the behavior with RuntimeHostPermissions
enabled.

This is in preparation of enabling RuntimeHostPermissions for field trial
testing.

BUG=888158

Change-Id: I46c2c1f92d273eff80395642d0fe2733528cdf70
Reviewed-on: https://chromium-review.googlesource.com/1239210
Commit-Queue: Karan Bhatia <karandeepb@chromium.org>
Reviewed-by: default avatarDevlin <rdevlin.cronin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#593691}
parent 12dbd16e
......@@ -5,6 +5,8 @@
#include <memory>
#include "base/logging.h"
#include "base/macros.h"
#include "base/test/scoped_feature_list.h"
#include "chrome/browser/extensions/extension_action_runner.h"
#include "chrome/browser/extensions/extension_apitest.h"
#include "chrome/browser/extensions/extension_service.h"
......@@ -20,9 +22,11 @@
#include "extensions/browser/test_extension_registry_observer.h"
#include "extensions/common/constants.h"
#include "extensions/common/extension.h"
#include "extensions/common/extension_features.h"
#include "extensions/test/extension_test_message_listener.h"
#include "extensions/test/result_catcher.h"
#include "net/base/filename_util.h"
#include "net/dns/mock_host_resolver.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#if defined(OS_CHROMEOS)
......@@ -33,21 +37,68 @@
namespace extensions {
namespace {
IN_PROC_BROWSER_TEST_F(ExtensionApiTest, ActiveTab) {
class ExtensionActiveTabTest : public ExtensionApiTest,
public testing::WithParamInterface<bool> {
public:
ExtensionActiveTabTest() = default;
// ExtensionApiTest override:
void SetUp() override {
if (ShouldEnableRuntimeHostPermissions()) {
scoped_feature_list_.InitAndEnableFeature(
extensions_features::kRuntimeHostPermissions);
} else {
scoped_feature_list_.InitAndDisableFeature(
extensions_features::kRuntimeHostPermissions);
}
ExtensionApiTest::SetUp();
}
void SetUpOnMainThread() override {
ExtensionApiTest::SetUpOnMainThread();
// Map all hosts to localhost.
host_resolver()->AddRule("*", "127.0.0.1");
ASSERT_EQ(ShouldEnableRuntimeHostPermissions(),
base::FeatureList::IsEnabled(
extensions_features::kRuntimeHostPermissions));
const char* runtime_host_permissions_arg =
ShouldEnableRuntimeHostPermissions() ? "RuntimeHostPermissionsEnabled"
: "RuntimeHostPermissionsDisabled";
SetCustomArg(runtime_host_permissions_arg);
}
private:
bool ShouldEnableRuntimeHostPermissions() const { return GetParam(); }
base::test::ScopedFeatureList scoped_feature_list_;
DISALLOW_COPY_AND_ASSIGN(ExtensionActiveTabTest);
};
IN_PROC_BROWSER_TEST_P(ExtensionActiveTabTest, ActiveTab) {
ASSERT_TRUE(StartEmbeddedTestServer());
ExtensionTestMessageListener background_page_ready("ready",
false /*will_reply*/);
const Extension* extension =
LoadExtension(test_data_dir_.AppendASCII("active_tab"));
ASSERT_TRUE(extension);
ASSERT_TRUE(background_page_ready.WaitUntilSatisfied());
// Shouldn't be initially granted based on activeTab.
{
ExtensionTestMessageListener navigation_count_listener(
"1", false /*will_reply*/);
ResultCatcher catcher;
ui_test_utils::NavigateToURL(
browser(),
embedded_test_server()->GetURL(
"/extensions/api_test/active_tab/page.html"));
"google.com", "/extensions/api_test/active_tab/page.html"));
EXPECT_TRUE(catcher.GetNextResult()) << message_;
EXPECT_TRUE(navigation_count_listener.WaitUntilSatisfied());
}
// Do one pass of BrowserAction without granting activeTab permission,
......@@ -91,17 +142,39 @@ IN_PROC_BROWSER_TEST_F(ExtensionApiTest, ActiveTab) {
}
#endif
// Changing page should go back to it not having access.
// Navigating to a different page on the same origin should revoke extension's
// access to the tab, unless the runtime host permissions feature is enabled.
{
ExtensionTestMessageListener navigation_count_listener(
"2", false /*will_reply*/);
ResultCatcher catcher;
ui_test_utils::NavigateToURL(
browser(),
embedded_test_server()->GetURL(
"/extensions/api_test/active_tab/final_page.html"));
"google.com", "/extensions/api_test/active_tab/final_page.html"));
EXPECT_TRUE(catcher.GetNextResult()) << message_;
EXPECT_TRUE(navigation_count_listener.WaitUntilSatisfied());
}
// Navigating to a different origin should revoke extension's access to the
// tab.
{
ExtensionTestMessageListener navigation_count_listener(
"3", false /*will_reply*/);
ResultCatcher catcher;
ui_test_utils::NavigateToURL(
browser(),
embedded_test_server()->GetURL(
"example.com", "/extensions/api_test/active_tab/final_page.html"));
EXPECT_TRUE(catcher.GetNextResult()) << message_;
EXPECT_TRUE(navigation_count_listener.WaitUntilSatisfied());
}
}
INSTANTIATE_TEST_CASE_P(ExtensionActiveTabTest,
ExtensionActiveTabTest,
testing::Bool());
// Tests the behavior of activeTab and its relation to an extension's ability to
// xhr file urls and inject scripts in file frames.
IN_PROC_BROWSER_TEST_F(ExtensionApiTest, FileURLs) {
......
......@@ -42,8 +42,9 @@ var injectIframe =
'iframe.src = "' + iframeUrl + '";\n' +
'document.body.appendChild(iframe);\n';
var runCount = 0;
var testConfig; // Populated in response to chrome.test.getConfig().
var runCount = 0;
chrome.browserAction.onClicked.addListener(function(tab) {
runCount++;
if (runCount == 1) {
......@@ -54,8 +55,8 @@ chrome.browserAction.onClicked.addListener(function(tab) {
return;
} else if (runCount == 3) {
// Third pass is done in a public session, and activeTab permission is
// granted to the extension. URL should be scrubbed down to the origin here
// (tested at the C++ side).
// granted to the extension. URL should be scrubbed down to the origin
// here (tested at the C++ side).
chrome.test.sendMessage(tab.url);
chrome.test.succeed();
return;
......@@ -65,7 +66,7 @@ chrome.browserAction.onClicked.addListener(function(tab) {
iframeDone = chrome.test.callbackAdded();
cachedUrl = tab.url;
chrome.tabs.executeScript({ code: injectIframe }, callbackPass());
chrome.tabs.executeScript({code: injectIframe}, callbackPass());
assertTrue(canXhr(tab.url));
chrome.automation.getTree(callbackPass(function(rootNode) {
......@@ -74,15 +75,44 @@ chrome.browserAction.onClicked.addListener(function(tab) {
}));
});
var navigationCount = 0;
chrome.webNavigation.onCompleted.addListener(function(details) {
chrome.tabs.executeScript({ code: 'true' }, callbackFail(
'Cannot access contents of the page. ' +
'Extension manifest must request permission to access the ' +
'respective host.'));
if (!details.url.endsWith('page.html'))
return;
assertTrue(!!testConfig);
assertTrue(
testConfig.customArg == 'RuntimeHostPermissionsEnabled' ||
testConfig.customArg == 'RuntimeHostPermissionsDisabled');
navigationCount++;
chrome.test.sendMessage(navigationCount.toString());
var expectHasAccess = navigationCount === 2 &&
testConfig.customArg === 'RuntimeHostPermissionsEnabled';
if (expectHasAccess) {
chrome.tabs.executeScript({code: 'true'}, callbackPass());
chrome.automation.getTree(callbackPass());
assertTrue(canXhr(details.url));
return;
}
chrome.tabs.executeScript(
{code: 'true'},
callbackFail(
'Cannot access contents of the page. ' +
'Extension manifest must request permission to access the ' +
'respective host.'));
chrome.automation.getTree(callbackFail(
'Cannot request automation tree on url "' + details.url +
'". Extension manifest must request permission to access this host.'));
'". Extension manifest must request permission to access this host.'));
assertFalse(canXhr(details.url));
});
chrome.test.getConfig(function(config) {
testConfig = config;
chrome.test.sendMessage('ready');
});
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment