Bug 1319070 - Test matching against the principal. r=kmag

MozReview-Commit-ID: LjuiizBh1OK
This commit is contained in:
Tomislav Jovanovic 2017-03-15 10:06:43 +01:00
Родитель 00c1af4647
Коммит 2ad8230ba4
3 изменённых файлов: 169 добавлений и 0 удалений

Просмотреть файл

@ -142,6 +142,76 @@ add_task(function* testBadPermissions() {
yield BrowserTestUtils.removeTab(tab1);
});
add_task(function* testMatchDataURI() {
const target = ExtensionTestUtils.loadExtension({
files: {
"page.html": `<!DOCTYPE html>
<meta charset="utf-8">
<script src="page.js"></script>
<iframe id="inherited" src="data:text/html;charset=utf-8,inherited"></iframe>
`,
"page.js": function() {
browser.test.onMessage.addListener((msg, url) => {
window.location.href = url;
});
},
},
background() {
browser.tabs.create({active: true, url: browser.runtime.getURL("page.html")});
},
});
const scripts = ExtensionTestUtils.loadExtension({
manifest: {
permissions: ["<all_urls>", "webNavigation"],
},
background() {
browser.webNavigation.onCompleted.addListener(({url, frameId}) => {
browser.test.log(`Document loading complete: ${url}`);
if (frameId === 0) {
browser.test.sendMessage("tab-ready", url);
}
});
browser.test.onMessage.addListener(async msg => {
browser.test.assertRejects(
browser.tabs.executeScript({
code: "location.href;",
allFrames: true,
}),
/No window matching/,
"Should not execute in `data:` frame");
browser.test.sendMessage("done");
});
},
});
yield scripts.startup();
yield target.startup();
// Test extension page with a data: iframe.
const page = yield scripts.awaitMessage("tab-ready");
ok(page.endsWith("page.html"), "Extension page loaded into a tab");
scripts.sendMessage("execute");
yield scripts.awaitMessage("done");
// Test extension tab navigated to a data: URI.
const data = "data:text/html;charset=utf-8,also-inherits";
target.sendMessage("navigate", data);
const url = yield scripts.awaitMessage("tab-ready");
is(url, data, "Extension tab navigated to a data: URI");
scripts.sendMessage("execute");
yield scripts.awaitMessage("done");
yield BrowserTestUtils.removeTab(gBrowser.selectedTab);
yield scripts.unload();
yield target.unload();
});
add_task(function* testBadURL() {
async function background() {
let promises = [

Просмотреть файл

@ -18,6 +18,7 @@ skip-if = (os == 'android') # android doesn't have devtools
[test_chrome_ext_background_page.html]
skip-if = (toolkit == 'android') # android doesn't have devtools
[test_chrome_ext_eventpage_warning.html]
[test_chrome_ext_contentscript_data_uri.html]
[test_chrome_ext_contentscript_unrecognizedprop_warning.html]
[test_chrome_ext_hybrid_addons.html]
[test_chrome_ext_trustworthy_origin.html]

Просмотреть файл

@ -0,0 +1,98 @@
<!DOCTYPE html>
<html>
<head>
<title>Test content script matching a data: URI</title>
<script src="chrome://mochikit/content/tests/SimpleTest/SpawnTask.js"></script>
<script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
<script src="chrome://mochikit/content/tests/SimpleTest/ExtensionTestUtils.js"></script>
<script src="head.js"></script>
<link rel="stylesheet" href="chrome://mochikit/contents/tests/SimpleTest/test.css"/>
</head>
<body>
<script>
"use strict";
add_task(function* test_contentscript_data_uri() {
const target = ExtensionTestUtils.loadExtension({
files: {
"page.html": `<!DOCTYPE html>
<meta charset="utf-8">
<iframe id="inherited" src="data:text/html;charset=utf-8,inherited"></iframe>
`,
},
background() {
browser.test.sendMessage("page", browser.runtime.getURL("page.html"));
},
});
const scripts = ExtensionTestUtils.loadExtension({
manifest: {
permissions: ["webNavigation"],
content_scripts: [{
all_frames: true,
matches: ["<all_urls>"],
run_at: "document_start",
css: ["all_urls.css"],
js: ["all_urls.js"],
}],
},
files: {
"all_urls.css": `
body { background: yellow; }
`,
"all_urls.js": function() {
document.body.style.color = "red";
browser.test.assertTrue(location.protocol !== "data:",
`Matched document not a data URI: ${location.href}`);
},
},
background() {
browser.webNavigation.onCompleted.addListener(({url, frameId}) => {
browser.test.log(`Document loading complete: ${url}`);
if (frameId === 0) {
browser.test.sendMessage("tab-ready", url);
}
});
},
});
yield target.startup();
yield scripts.startup();
// Test extension page with a data: iframe.
const page = yield target.awaitMessage("page");
const win = window.open(page);
yield scripts.awaitMessage("tab-ready");
is(win.location.href, page, "Extension page loaded into a tab");
is(win.document.readyState, "complete", "Page finished loading");
const iframe = win.document.getElementById("inherited").contentWindow;
is(iframe.document.readyState, "complete", "iframe finished loading");
const style1 = iframe.getComputedStyle(iframe.document.body);
is(style1.color, "rgb(0, 0, 0)", "iframe text color is unmodified");
is(style1.backgroundColor, "rgba(0, 0, 0, 0)", "iframe background unmodified");
// Test extension tab navigated to a data: URI.
const data = "data:text/html;charset=utf-8,also-inherits";
win.location.href = data;
yield scripts.awaitMessage("tab-ready");
is(win.location.href, data, "Extension tab navigated to a data: URI");
is(win.document.readyState, "complete", "Tab finished loading");
const style2 = win.getComputedStyle(win.document.body);
is(style2.color, "rgb(0, 0, 0)", "Tab text color is unmodified");
is(style2.backgroundColor, "rgba(0, 0, 0, 0)", "Tab background unmodified");
win.close();
yield target.unload();
yield scripts.unload();
});
</script>
</body>
</html>