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