Bug 1443942 - Test for blocking midflight redirects in media elements. r=jya

Test that playback works if we don't block, doesn't if we do block, and does
if we do block and CORS is used.

MozReview-Commit-ID: 9PTZXLOdHIU

--HG--
extra : rebase_source : db7f0c61b64990623ef035b266cf052c45df1c76
This commit is contained in:
Chris Pearce 2018-03-06 14:44:26 +13:00
Родитель 259a33e836
Коммит e8c1b1b7f1
3 изменённых файлов: 168 добавлений и 0 удалений

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

@ -0,0 +1,71 @@
function parseQuery(query, key) {
for (let p of query.split('&')) {
if (p == key) {
return true;
}
if (p.startsWith(key + "=")) {
return p.substring(key.length + 1);
}
}
}
// Return the first few bytes in a short byte range response. When Firefox
// requests subsequent bytes in a second range request, respond with a
// redirect. Requests after the first redirected are serviced as expected.
function handleRequest(request, response)
{
var query = request.queryString;
var resource = parseQuery(query, "resource");
var redirected = parseQuery(query, "redirected") || false;
var useCors = parseQuery(query, "cors") || false;
var file = Components.classes["@mozilla.org/file/directory_service;1"].
getService(Components.interfaces.nsIProperties).
get("CurWorkD", Components.interfaces.nsIFile);
var fis = Components.classes['@mozilla.org/network/file-input-stream;1'].
createInstance(Components.interfaces.nsIFileInputStream);
var bis = Components.classes["@mozilla.org/binaryinputstream;1"].
createInstance(Components.interfaces.nsIBinaryInputStream);
var paths = "tests/dom/media/test/" + resource;
var split = paths.split("/");
for (var i = 0; i < split.length; ++i) {
file.append(split[i]);
}
fis.init(file, -1, -1, false);
bis.setInputStream(fis);
var bytes = bis.readBytes(bis.available());
let [from, to] = request.getHeader("range").split("=")[1].split("-").map(s => parseInt(s));
if (!redirected && from > 0) {
var origin = request.host == "mochi.test" ? "example.org" : "mochi.test:8888";
response.setStatusLine(request.httpVersion, 303, "See Other");
let url = "http://" + origin +
"/tests/dom/media/test/midflight-redirect.sjs?redirected&" + query;
response.setHeader("Location", url);
response.setHeader("Content-Type", "text/html");
return;
}
if (from == 0 && !redirected) {
to = Math.min(bytes.length / 4, 200);
} else {
to = to || Math.max(from, bytes.length - 1);
}
byterange = bytes.substring(from, to + 1);
let contentRange = "bytes "+ from +"-"+ to +"/"+ bytes.length;
let contentLength = (to - from + 1).toString();
response.setStatusLine(request.httpVersion, 206, "Partial Content");
response.setHeader("Content-Range", contentRange);
response.setHeader("Content-Length", contentLength, false);
response.setHeader("Content-Type", "video/ogg", false);
response.setHeader("Accept-Ranges", "bytes", false);
if (redirected && useCors) {
response.setHeader("Access-Control-Allow-Origin", "*");
}
response.write(byterange, byterange.length);
bis.close();
}

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

@ -495,6 +495,7 @@ support-files =
invalid-preskip.webm^headers^
long.vtt
manifest.js
midflight-redirect.sjs
multiple-bos.ogg
multiple-bos.ogg^headers^
multiple-bos-more-header-fileds.ogg
@ -950,6 +951,7 @@ skip-if = android_version == '17' # android(bug 1232305)
[test_mediatrack_replay_from_end.html]
skip-if = toolkit == 'android' # android(bug 1232305)
[test_metadata.html]
[test_midflight_redirect_blocked.html]
[test_mixed_principals.html]
skip-if = toolkit == 'android' # bug 1309814, android(bug 1232305)
[test_mozHasAudio.html]

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

@ -0,0 +1,95 @@
<!DOCTYPE HTML>
<html>
<head>
<title>Test mid-flight cross site redirects are blocked</title>
<script type="text/javascript" src="/MochiKit/MochiKit.js"></script>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
<script type="text/javascript" src="manifest.js"></script>
</head>
<body>
<pre id='test'>
<script class="testbody" type='application/javascript'>
function testIfLoadsToMetadata(test, useCors) {
return new Promise(function(resolve, reject) {
var elemType = getMajorMimeType(test.type);
var element = document.createElement(elemType);
if (useCors) {
element.crossOrigin = "anonymous";
}
// Log events for debugging.
[
"suspend", "play", "canplay", "canplaythrough", "loadstart",
"loadedmetadata", "loadeddata", "playing", "ended", "error",
"stalled", "emptied", "abort", "waiting", "pause"
].forEach((eventName) => {
element.addEventListener(eventName, (event)=> {
info(test.name + " " + event.type);
});
});
element.addEventListener("loadedmetadata", ()=>{
resolve(true);
removeNodeAndSource(element);
}, false);
element.addEventListener("error", ()=>{
resolve(false);
removeNodeAndSource(element);
}, false);
let suffix = useCors ? "&cors" : "";
element.src = "midflight-redirect.sjs?resource=" + test.name + suffix;
element.preload = "metadata";
document.body.appendChild(element);
element.load()
});
}
let v = document.createElement("video");
const testCases = gSmallTests.filter(t => v.canPlayType(t.type));
function testMediaLoad(expectedToLoad, message, useCors) {
return new Promise(async function(resolve, reject) {
for (let test of testCases) {
let loaded = await testIfLoadsToMetadata(test, useCors);
is(loaded, expectedToLoad, test.name + " " + message);
}
resolve();
});
}
async function runTest() {
try {
SimpleTest.info("Allowing midflight redirects...");
await SpecialPowers.pushPrefEnv({'set': [["media.block-midflight-redirects", false]]});
SimpleTest.info("Test that all media plays...");
await testMediaLoad(true, "expected to load", false);
SimpleTest.info("Blocking midflight redirects...");
await SpecialPowers.pushPrefEnv({'set': [["media.block-midflight-redirects", true]]});
SimpleTest.info("Test that all media no longer play...");
await testMediaLoad(false, "expected to be blocked", false);
SimpleTest.info("Test that all media play if CORS used...");
await testMediaLoad(true, "expected to play with CORS", true);
} catch (e) {
info("Exception " + e.message);
ok(false, "Threw exception " + e.message);
}
SimpleTest.finish();
}
SimpleTest.waitForExplicitFinish();
addLoadEvent(runTest);
</script>
</pre>
</body>
</html>