toolkit/components/mediasniffer/test/unit/test_mediasniffer_ext.js

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 const Ci = Components.interfaces;
michael@0 6 const Cu = Components.utils;
michael@0 7 const Cc = Components.classes;
michael@0 8 const CC = Components.Constructor;
michael@0 9
michael@0 10 var BinaryOutputStream = CC("@mozilla.org/binaryoutputstream;1",
michael@0 11 "nsIBinaryOutputStream",
michael@0 12 "setOutputStream");
michael@0 13
michael@0 14 Cu.import("resource://testing-common/httpd.js");
michael@0 15
michael@0 16 var httpserver = new HttpServer();
michael@0 17
michael@0 18 var testRan = 0;
michael@0 19
michael@0 20 // The tests files we want to test, and the type we should have after sniffing.
michael@0 21 const tests = [
michael@0 22 // Real webm and mkv files truncated to 512 bytes.
michael@0 23 { path: "data/file.webm", expected: "video/webm" },
michael@0 24 { path: "data/file.mkv", expected: "application/octet-stream" },
michael@0 25 // MP3 files with and without id3 headers truncated to 512 bytes.
michael@0 26 // NB these have 208/209 byte frames, but mp3 can require up to
michael@0 27 // 1445 bytes to detect with our method.
michael@0 28 { path: "data/id3tags.mp3", expected: "audio/mpeg" },
michael@0 29 { path: "data/notags.mp3", expected: "audio/mpeg" },
michael@0 30 // MPEG-2 mp3 files.
michael@0 31 { path: "data/detodos.mp3", expected: "audio/mpeg" },
michael@0 32 // Padding bit flipped in the first header: sniffing should fail.
michael@0 33 { path: "data/notags-bad.mp3", expected: "application/octet-stream" },
michael@0 34 // Garbage before header: sniffing should fail.
michael@0 35 { path: "data/notags-scan.mp3", expected: "application/octet-stream" },
michael@0 36 // VBR from the layer III test patterns. We can't sniff this.
michael@0 37 { path: "data/he_free.mp3", expected: "application/octet-stream" },
michael@0 38 // Make sure we reject mp2, which has a similar header.
michael@0 39 { path: "data/fl10.mp2", expected: "application/octet-stream" },
michael@0 40 // Truncated ff installer regression test for bug 875769.
michael@0 41 { path: "data/ff-inst.exe", expected: "application/octet-stream" },
michael@0 42 ];
michael@0 43
michael@0 44 // A basic listener that reads checks the if we sniffed properly.
michael@0 45 var listener = {
michael@0 46 onStartRequest: function(request, context) {
michael@0 47 do_print("Sniffing " + tests[testRan].path);
michael@0 48 do_check_eq(request.QueryInterface(Ci.nsIChannel).contentType, tests[testRan].expected);
michael@0 49 },
michael@0 50
michael@0 51 onDataAvailable: function(request, context, stream, offset, count) {
michael@0 52 try {
michael@0 53 var bis = Components.classes["@mozilla.org/binaryinputstream;1"]
michael@0 54 .createInstance(Components.interfaces.nsIBinaryInputStream);
michael@0 55 bis.setInputStream(stream);
michael@0 56 var array = bis.readByteArray(bis.available());
michael@0 57 } catch (ex) {
michael@0 58 do_throw("Error in onDataAvailable: " + ex);
michael@0 59 }
michael@0 60 },
michael@0 61
michael@0 62 onStopRequest: function(request, context, status) {
michael@0 63 testRan++;
michael@0 64 runNext();
michael@0 65 }
michael@0 66 };
michael@0 67
michael@0 68 function setupChannel(url) {
michael@0 69 var ios = Components.classes["@mozilla.org/network/io-service;1"].
michael@0 70 getService(Ci.nsIIOService);
michael@0 71 var chan = ios.newChannel("http://localhost:" +
michael@0 72 httpserver.identity.primaryPort + url, "", null);
michael@0 73 var httpChan = chan.QueryInterface(Components.interfaces.nsIHttpChannel);
michael@0 74 return httpChan;
michael@0 75 }
michael@0 76
michael@0 77 function runNext() {
michael@0 78 if (testRan == tests.length) {
michael@0 79 do_test_finished();
michael@0 80 return;
michael@0 81 }
michael@0 82 var channel = setupChannel("/");
michael@0 83 channel.asyncOpen(listener, channel, null);
michael@0 84 }
michael@0 85
michael@0 86 function getFileContents(aFile) {
michael@0 87 const PR_RDONLY = 0x01;
michael@0 88 var fileStream = Cc["@mozilla.org/network/file-input-stream;1"]
michael@0 89 .createInstance(Ci.nsIFileInputStream);
michael@0 90 fileStream.init(aFile, 1, -1, null);
michael@0 91 var bis = Components.classes["@mozilla.org/binaryinputstream;1"]
michael@0 92 .createInstance(Components.interfaces.nsIBinaryInputStream);
michael@0 93 bis.setInputStream(fileStream);
michael@0 94
michael@0 95 var data = bis.readByteArray(bis.available());
michael@0 96
michael@0 97 return data;
michael@0 98 }
michael@0 99
michael@0 100 function handler(metadata, response) {
michael@0 101 response.setStatusLine(metadata.httpVersion, 200, "OK");
michael@0 102 // Send an empty Content-Type, so we are guaranteed to sniff.
michael@0 103 response.setHeader("Content-Type", "", false);
michael@0 104 var body = getFileContents(do_get_file(tests[testRan].path));
michael@0 105 var bos = new BinaryOutputStream(response.bodyOutputStream);
michael@0 106 bos.writeByteArray(body, body.length);
michael@0 107 }
michael@0 108
michael@0 109 function run_test() {
michael@0 110 // We use a custom handler so we can change the header to force sniffing.
michael@0 111 httpserver.registerPathHandler("/", handler);
michael@0 112 httpserver.start(-1);
michael@0 113 do_test_pending();
michael@0 114 try {
michael@0 115 runNext();
michael@0 116 } catch (e) {
michael@0 117 print("ERROR - " + e + "\n");
michael@0 118 }
michael@0 119 }

mercurial