gecko-dev/caps/tests/mochitest/test_extensionURL.html

172 строки
7.2 KiB
HTML
Исходник Обычный вид История

2015-07-20 05:23:55 +03:00
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1161831
-->
<head>
<meta charset="utf-8">
<title>Test for Bug 1161831</title>
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
<script type="application/javascript">
/** Test for Bug 1161831 **/
SimpleTest.waitForExplicitFinish();
var aps = SpecialPowers.Cc["@mozilla.org/addons/policy-service;1"]
.getService(SpecialPowers.Ci.nsIAddonPolicyService).wrappedJSObject;
var oldLoadCallback = aps.setExtensionURILoadCallback(null);
var oldMapCallback = aps.setExtensionURIToAddonIdCallback(null);
var resourceHandler = SpecialPowers.Services.io.getProtocolHandler("resource")
.QueryInterface(SpecialPowers.Ci.nsISubstitutingProtocolHandler);
var extensionHandler = SpecialPowers.Services.io.getProtocolHandler("moz-extension")
.QueryInterface(SpecialPowers.Ci.nsISubstitutingProtocolHandler);
SimpleTest.registerCleanupFunction(function() {
extensionHandler.setSubstitution('cherise', null);
extensionHandler.setSubstitution('liebchen', null);
aps.setExtensionURILoadCallback(oldLoadCallback);
aps.setExtensionURIToAddonIdCallback(oldMapCallback);
});
addLoadEvent(function() {
// First, get a file:// URI to something - open to suggestions on how to do
// this more easily.
var resURI = SpecialPowers.Services.io.newURI('resource://testing-common/resource_test_file.html', null, null);
var filePath = resourceHandler.resolveURI(resURI);
ok(filePath.startsWith('file://'), 'resource:// URI resolves where we expect: ' + filePath);
var fileURI = SpecialPowers.Services.io.newURI(filePath, null, null);
// Register a moz-extension:// URI.
extensionHandler.setSubstitution('cherise', fileURI);
// Alias the above.
extensionHandler.setSubstitution('liebchen', SpecialPowers.Services.io.newURI('moz-extension://cherise', null, null));
//
// Make sure that non-file:// URIs don't work.
//
// resource://
try {
extensionHandler.setSubstitution('interdit', resURI);
ok(false, "Should have thrown for mapping moz-extension to resource");
} catch (e) {
ok(true, "Threw correctly: " + e);
}
// chrome://
try {
var chromeURI = SpecialPowers.Services.io.newURI('chrome://global/content/mozilla.xhtml', null, null);
extensionHandler.setSubstitution('verboten', chromeURI);
ok(false, "Should have thrown for mapping moz-extension to chrome");
} catch (e) {
ok(true, "Threw correctly: " + e);
}
function navigateWithLocation(ifr, url) { ifr.contentWindow.location = url; }
function navigateWithSrc(ifr, url) { ifr.setAttribute('src', url); }
function navigateFromChromeWithLocation(ifr, url) { SpecialPowers.wrap(ifr).contentWindow.location = url; }
function navigateFromChromeWithWebNav(ifr, url) {
SpecialPowers.wrap(ifr).contentWindow
.QueryInterface(SpecialPowers.Ci.nsIInterfaceRequestor)
.getInterface(SpecialPowers.Ci.nsIWebNavigation)
.loadURI(url, 0, null, null, null);
}
function setWhitelistCallback(rgxp) {
var cb = SpecialPowers.wrapCallback(function(uri) { return rgxp.test(uri.spec); });
aps.setExtensionURILoadCallback(cb);
}
aps.setExtensionURIToAddonIdCallback(SpecialPowers.wrapCallback(function (uri) { return 'imaginaryaddon-' + uri.host[0]; }));
function testLoad(url, navigate, shouldThrow) {
var ifr = document.createElement('iframe');
var p = new Promise(function(resolve, reject) {
ifr.onload = function() {
ok(true, 'Loaded ' + url);
var prin = SpecialPowers.wrap(ifr.contentWindow).document.nodePrincipal;
function stripTrailingSlash(s) { return s.replace(/\/$/, ''); };
is(stripTrailingSlash(prin.URI.spec), url, 'Principal uri is correct: ' + url);
function stripPath(s) { return s.replace(/(.*\/\/.+)\/.*/, '$1'); };
is(prin.originNoSuffix, stripPath(url), 'Principal origin is correct: ' + prin.originNoSuffix);
2015-07-20 05:23:55 +03:00
is(prin.originAttributes.addonId, 'imaginaryaddon-' + url[url.indexOf('/') + 2], 'addonId is correct');
if (/_blank/.test(url)) {
is(SpecialPowers.wrap(ifr.contentWindow).document.documentElement.innerHTML,
'<head></head><body></body>', 'blank document looks right');
} else {
is(SpecialPowers.wrap(ifr.contentWindow).document.title, 'resource test file',
'document looks right');
}
2015-07-20 05:23:55 +03:00
ifr.remove();
resolve();
};
document.body.appendChild(ifr);
var threw = false;
try {
navigate(ifr, url);
} catch (e) {
ifr.remove();
threw = true;
ok(/denied|insecure/.test(e), "exception correct: " + e);
2015-07-20 05:23:55 +03:00
}
is(threw, !!shouldThrow, "Correct throwing behavior for: " + url);
!threw || resolve();
});
return p;
}
2015-10-01 05:28:58 +03:00
function testXHR(url, shouldThrow) {
return new Promise(function(resolve, reject) {
var xhr = new XMLHttpRequest();
xhr.addEventListener("load", () => { ok(!shouldThrow, "XHR succeeded for " + url); resolve(); });
xhr.addEventListener("error", () => { ok(false, "Unexpected XHR error: " + url); resolve(); });
try {
xhr.open("GET", url, true);
xhr.send();
} catch (e) {
ok(shouldThrow, "XHR threw for " + url);
resolve();
}
});
}
2015-07-20 05:23:55 +03:00
//
// Perform some loads and make sure they work correctly.
//
testLoad.bind(null, 'moz-extension://cherise', navigateFromChromeWithLocation)()
.then(testLoad.bind(null, 'moz-extension://cherise', navigateFromChromeWithWebNav))
.then(testLoad.bind(null, 'moz-extension://cherise', navigateWithLocation, /* shouldThrow = */ true))
2015-10-01 05:28:58 +03:00
.then(testXHR.bind(null, 'moz-extension://cherise', /* shouldThrow = */ true))
2015-07-20 05:23:55 +03:00
.then(setWhitelistCallback.bind(null, /cherise/))
.then(testLoad.bind(null, 'moz-extension://cherise', navigateWithLocation))
2015-10-01 05:28:58 +03:00
.then(testXHR.bind(null, 'moz-extension://cherise'))
2015-07-20 05:23:55 +03:00
.then(testLoad.bind(null, 'moz-extension://liebchen', navigateWithLocation, /* shouldThrow = */ true))
2015-10-01 05:28:58 +03:00
.then(testXHR.bind(null, 'moz-extension://liebchen', /* shouldThrow = */ true))
2015-07-20 05:23:55 +03:00
.then(setWhitelistCallback.bind(null, /cherise|liebchen/))
.then(testLoad.bind(null, 'moz-extension://liebchen', navigateWithLocation))
.then(testLoad.bind(null, 'moz-extension://liebchen', navigateWithSrc))
.then(testLoad.bind(null, 'moz-extension://cherise', navigateWithSrc))
.then(testLoad.bind(null, 'moz-extension://cherise/_blank.html', navigateWithSrc))
2015-07-20 05:23:55 +03:00
.then(SimpleTest.finish.bind(SimpleTest),
function(e) { ok(false, "rejected promise: " + e); SimpleTest.finish() }
);
});
</script>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1161831">Mozilla Bug 1161831</a>
<p id="display"></p>
<div id="content" style="display: none">
</div>
<pre id="test">
</pre>
</body>
</html>