1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/components/mediasniffer/test/unit/test_mediasniffer_ext.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,119 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +const Ci = Components.interfaces; 1.9 +const Cu = Components.utils; 1.10 +const Cc = Components.classes; 1.11 +const CC = Components.Constructor; 1.12 + 1.13 +var BinaryOutputStream = CC("@mozilla.org/binaryoutputstream;1", 1.14 + "nsIBinaryOutputStream", 1.15 + "setOutputStream"); 1.16 + 1.17 +Cu.import("resource://testing-common/httpd.js"); 1.18 + 1.19 +var httpserver = new HttpServer(); 1.20 + 1.21 +var testRan = 0; 1.22 + 1.23 +// The tests files we want to test, and the type we should have after sniffing. 1.24 +const tests = [ 1.25 + // Real webm and mkv files truncated to 512 bytes. 1.26 + { path: "data/file.webm", expected: "video/webm" }, 1.27 + { path: "data/file.mkv", expected: "application/octet-stream" }, 1.28 + // MP3 files with and without id3 headers truncated to 512 bytes. 1.29 + // NB these have 208/209 byte frames, but mp3 can require up to 1.30 + // 1445 bytes to detect with our method. 1.31 + { path: "data/id3tags.mp3", expected: "audio/mpeg" }, 1.32 + { path: "data/notags.mp3", expected: "audio/mpeg" }, 1.33 + // MPEG-2 mp3 files. 1.34 + { path: "data/detodos.mp3", expected: "audio/mpeg" }, 1.35 + // Padding bit flipped in the first header: sniffing should fail. 1.36 + { path: "data/notags-bad.mp3", expected: "application/octet-stream" }, 1.37 + // Garbage before header: sniffing should fail. 1.38 + { path: "data/notags-scan.mp3", expected: "application/octet-stream" }, 1.39 + // VBR from the layer III test patterns. We can't sniff this. 1.40 + { path: "data/he_free.mp3", expected: "application/octet-stream" }, 1.41 + // Make sure we reject mp2, which has a similar header. 1.42 + { path: "data/fl10.mp2", expected: "application/octet-stream" }, 1.43 + // Truncated ff installer regression test for bug 875769. 1.44 + { path: "data/ff-inst.exe", expected: "application/octet-stream" }, 1.45 +]; 1.46 + 1.47 +// A basic listener that reads checks the if we sniffed properly. 1.48 +var listener = { 1.49 + onStartRequest: function(request, context) { 1.50 + do_print("Sniffing " + tests[testRan].path); 1.51 + do_check_eq(request.QueryInterface(Ci.nsIChannel).contentType, tests[testRan].expected); 1.52 + }, 1.53 + 1.54 + onDataAvailable: function(request, context, stream, offset, count) { 1.55 + try { 1.56 + var bis = Components.classes["@mozilla.org/binaryinputstream;1"] 1.57 + .createInstance(Components.interfaces.nsIBinaryInputStream); 1.58 + bis.setInputStream(stream); 1.59 + var array = bis.readByteArray(bis.available()); 1.60 + } catch (ex) { 1.61 + do_throw("Error in onDataAvailable: " + ex); 1.62 + } 1.63 + }, 1.64 + 1.65 + onStopRequest: function(request, context, status) { 1.66 + testRan++; 1.67 + runNext(); 1.68 + } 1.69 +}; 1.70 + 1.71 +function setupChannel(url) { 1.72 + var ios = Components.classes["@mozilla.org/network/io-service;1"]. 1.73 + getService(Ci.nsIIOService); 1.74 + var chan = ios.newChannel("http://localhost:" + 1.75 + httpserver.identity.primaryPort + url, "", null); 1.76 + var httpChan = chan.QueryInterface(Components.interfaces.nsIHttpChannel); 1.77 + return httpChan; 1.78 +} 1.79 + 1.80 +function runNext() { 1.81 + if (testRan == tests.length) { 1.82 + do_test_finished(); 1.83 + return; 1.84 + } 1.85 + var channel = setupChannel("/"); 1.86 + channel.asyncOpen(listener, channel, null); 1.87 +} 1.88 + 1.89 +function getFileContents(aFile) { 1.90 + const PR_RDONLY = 0x01; 1.91 + var fileStream = Cc["@mozilla.org/network/file-input-stream;1"] 1.92 + .createInstance(Ci.nsIFileInputStream); 1.93 + fileStream.init(aFile, 1, -1, null); 1.94 + var bis = Components.classes["@mozilla.org/binaryinputstream;1"] 1.95 + .createInstance(Components.interfaces.nsIBinaryInputStream); 1.96 + bis.setInputStream(fileStream); 1.97 + 1.98 + var data = bis.readByteArray(bis.available()); 1.99 + 1.100 + return data; 1.101 +} 1.102 + 1.103 +function handler(metadata, response) { 1.104 + response.setStatusLine(metadata.httpVersion, 200, "OK"); 1.105 + // Send an empty Content-Type, so we are guaranteed to sniff. 1.106 + response.setHeader("Content-Type", "", false); 1.107 + var body = getFileContents(do_get_file(tests[testRan].path)); 1.108 + var bos = new BinaryOutputStream(response.bodyOutputStream); 1.109 + bos.writeByteArray(body, body.length); 1.110 +} 1.111 + 1.112 +function run_test() { 1.113 + // We use a custom handler so we can change the header to force sniffing. 1.114 + httpserver.registerPathHandler("/", handler); 1.115 + httpserver.start(-1); 1.116 + do_test_pending(); 1.117 + try { 1.118 + runNext(); 1.119 + } catch (e) { 1.120 + print("ERROR - " + e + "\n"); 1.121 + } 1.122 +}