gecko/netwerk/test/unit/test_plaintext_sniff.js

195 lines
5.7 KiB
JavaScript
Raw Normal View History

// Test the plaintext-or-binary sniffer
bug 482085 - fix all xpcshell tests to not reference files from the srcdir. r=bsmedberg,waldo --HG-- rename : toolkit/components/downloads/test/downloads.empty.sqlite => browser/components/privatebrowsing/test/unit/downloads.empty.sqlite rename : dom/src/json/test/fail1.json => dom/src/json/test/unit/fail1.json rename : dom/src/json/test/fail10.json => dom/src/json/test/unit/fail10.json rename : dom/src/json/test/fail11.json => dom/src/json/test/unit/fail11.json rename : dom/src/json/test/fail12.json => dom/src/json/test/unit/fail12.json rename : dom/src/json/test/fail13.json => dom/src/json/test/unit/fail13.json rename : dom/src/json/test/fail14.json => dom/src/json/test/unit/fail14.json rename : dom/src/json/test/fail15.json => dom/src/json/test/unit/fail15.json rename : dom/src/json/test/fail16.json => dom/src/json/test/unit/fail16.json rename : dom/src/json/test/fail17.json => dom/src/json/test/unit/fail17.json rename : dom/src/json/test/fail18.json => dom/src/json/test/unit/fail18.json rename : dom/src/json/test/fail19.json => dom/src/json/test/unit/fail19.json rename : dom/src/json/test/fail2.json => dom/src/json/test/unit/fail2.json rename : dom/src/json/test/fail20.json => dom/src/json/test/unit/fail20.json rename : dom/src/json/test/fail21.json => dom/src/json/test/unit/fail21.json rename : dom/src/json/test/fail22.json => dom/src/json/test/unit/fail22.json rename : dom/src/json/test/fail23.json => dom/src/json/test/unit/fail23.json rename : dom/src/json/test/fail24.json => dom/src/json/test/unit/fail24.json rename : dom/src/json/test/fail25.json => dom/src/json/test/unit/fail25.json rename : dom/src/json/test/fail26.json => dom/src/json/test/unit/fail26.json rename : dom/src/json/test/fail27.json => dom/src/json/test/unit/fail27.json rename : dom/src/json/test/fail28.json => dom/src/json/test/unit/fail28.json rename : dom/src/json/test/fail29.json => dom/src/json/test/unit/fail29.json rename : dom/src/json/test/fail3.json => dom/src/json/test/unit/fail3.json rename : dom/src/json/test/fail30.json => dom/src/json/test/unit/fail30.json rename : dom/src/json/test/fail31.json => dom/src/json/test/unit/fail31.json rename : dom/src/json/test/fail32.json => dom/src/json/test/unit/fail32.json rename : dom/src/json/test/fail33.json => dom/src/json/test/unit/fail33.json rename : dom/src/json/test/fail34.json => dom/src/json/test/unit/fail34.json rename : dom/src/json/test/fail4.json => dom/src/json/test/unit/fail4.json rename : dom/src/json/test/fail5.json => dom/src/json/test/unit/fail5.json rename : dom/src/json/test/fail6.json => dom/src/json/test/unit/fail6.json rename : dom/src/json/test/fail7.json => dom/src/json/test/unit/fail7.json rename : dom/src/json/test/fail8.json => dom/src/json/test/unit/fail8.json rename : dom/src/json/test/fail9.json => dom/src/json/test/unit/fail9.json rename : dom/src/json/test/json2.js => dom/src/json/test/unit/json2.js rename : dom/src/json/test/pass1.json => dom/src/json/test/unit/pass1.json rename : dom/src/json/test/pass3.json => dom/src/json/test/unit/pass3.json rename : testing/xpcshell/example/location_load.js => testing/xpcshell/example/unit/location_load.js rename : toolkit/components/commandlines/test/unit/unix/test_bug410156.js => toolkit/components/commandlines/test/unit_unix/test_bug410156.js rename : toolkit/components/commandlines/test/unit/win/test_bug410156.js => toolkit/components/commandlines/test/unit_win/test_bug410156.js rename : toolkit/mozapps/extensions/test/unit/addons/test_bug257155/install.rdf => toolkit/mozapps/extensions/test/addons/test_bug257155/install.rdf rename : toolkit/mozapps/extensions/test/unit/addons/test_bug299716_2/install.rdf => toolkit/mozapps/extensions/test/addons/test_bug299716_2/install.rdf rename : toolkit/mozapps/extensions/test/unit/addons/test_bug299716_a_1/install.rdf => toolkit/mozapps/extensions/test/addons/test_bug299716_a_1/install.rdf rename : toolkit/mozapps/extensions/test/unit/addons/test_bug299716_a_2/install.rdf => toolkit/mozapps/extensions/test/addons/test_bug299716_a_2/install.rdf rename : toolkit/mozapps/extensions/test/unit/addons/test_bug299716_b_1/install.rdf => toolkit/mozapps/extensions/test/addons/test_bug299716_b_1/install.rdf rename : toolkit/mozapps/extensions/test/unit/addons/test_bug299716_b_2/install.rdf => toolkit/mozapps/extensions/test/addons/test_bug299716_b_2/install.rdf rename : toolkit/mozapps/extensions/test/unit/addons/test_bug299716_c_1/install.rdf => toolkit/mozapps/extensions/test/addons/test_bug299716_c_1/install.rdf rename : toolkit/mozapps/extensions/test/unit/addons/test_bug299716_c_2/install.rdf => toolkit/mozapps/extensions/test/addons/test_bug299716_c_2/install.rdf rename : toolkit/mozapps/extensions/test/unit/addons/test_bug299716_d_1/install.rdf => toolkit/mozapps/extensions/test/addons/test_bug299716_d_1/install.rdf rename : toolkit/mozapps/extensions/test/unit/addons/test_bug299716_d_2/install.rdf => toolkit/mozapps/extensions/test/addons/test_bug299716_d_2/install.rdf rename : toolkit/mozapps/extensions/test/unit/addons/test_bug299716_e_1/install.rdf => toolkit/mozapps/extensions/test/addons/test_bug299716_e_1/install.rdf rename : toolkit/mozapps/extensions/test/unit/addons/test_bug299716_e_2/install.rdf => toolkit/mozapps/extensions/test/addons/test_bug299716_e_2/install.rdf rename : toolkit/mozapps/extensions/test/unit/addons/test_bug299716_f_1/install.rdf => toolkit/mozapps/extensions/test/addons/test_bug299716_f_1/install.rdf rename : toolkit/mozapps/extensions/test/unit/addons/test_bug299716_f_2/install.rdf => toolkit/mozapps/extensions/test/addons/test_bug299716_f_2/install.rdf rename : toolkit/mozapps/extensions/test/unit/addons/test_bug299716_g_1/install.rdf => toolkit/mozapps/extensions/test/addons/test_bug299716_g_1/install.rdf rename : toolkit/mozapps/extensions/test/unit/addons/test_bug299716_g_2/install.rdf => toolkit/mozapps/extensions/test/addons/test_bug299716_g_2/install.rdf rename : toolkit/mozapps/extensions/test/unit/addons/test_bug324121_1/install.rdf => toolkit/mozapps/extensions/test/addons/test_bug324121_1/install.rdf rename : toolkit/mozapps/extensions/test/unit/addons/test_bug324121_2/install.rdf => toolkit/mozapps/extensions/test/addons/test_bug324121_2/install.rdf rename : toolkit/mozapps/extensions/test/unit/addons/test_bug324121_3/install.rdf => toolkit/mozapps/extensions/test/addons/test_bug324121_3/install.rdf rename : toolkit/mozapps/extensions/test/unit/addons/test_bug324121_4/install.rdf => toolkit/mozapps/extensions/test/addons/test_bug324121_4/install.rdf rename : toolkit/mozapps/extensions/test/unit/addons/test_bug324121_5/install.rdf => toolkit/mozapps/extensions/test/addons/test_bug324121_5/install.rdf rename : toolkit/mozapps/extensions/test/unit/addons/test_bug324121_6/install.rdf => toolkit/mozapps/extensions/test/addons/test_bug324121_6/install.rdf rename : toolkit/mozapps/extensions/test/unit/addons/test_bug324121_7/install.rdf => toolkit/mozapps/extensions/test/addons/test_bug324121_7/install.rdf rename : toolkit/mozapps/extensions/test/unit/addons/test_bug324121_8/install.rdf => toolkit/mozapps/extensions/test/addons/test_bug324121_8/install.rdf rename : toolkit/mozapps/extensions/test/unit/addons/test_bug324121_9/install.rdf => toolkit/mozapps/extensions/test/addons/test_bug324121_9/install.rdf rename : toolkit/mozapps/extensions/test/unit/addons/test_bug335238_1/install.rdf => toolkit/mozapps/extensions/test/addons/test_bug335238_1/install.rdf rename : toolkit/mozapps/extensions/test/unit/addons/test_bug335238_2/install.rdf => toolkit/mozapps/extensions/test/addons/test_bug335238_2/install.rdf rename : toolkit/mozapps/extensions/test/unit/addons/test_bug378216_1/install.rdf => toolkit/mozapps/extensions/test/addons/test_bug378216_1/install.rdf rename : toolkit/mozapps/extensions/test/unit/addons/test_bug378216_10/install.rdf => toolkit/mozapps/extensions/test/addons/test_bug378216_10/install.rdf rename : toolkit/mozapps/extensions/test/unit/addons/test_bug378216_11/install.rdf => toolkit/mozapps/extensions/test/addons/test_bug378216_11/install.rdf rename : toolkit/mozapps/extensions/test/unit/addons/test_bug378216_12/install.rdf => toolkit/mozapps/extensions/test/addons/test_bug378216_12/install.rdf rename : toolkit/mozapps/extensions/test/unit/addons/test_bug378216_13/install.rdf => toolkit/mozapps/extensions/test/addons/test_bug378216_13/install.rdf rename : toolkit/mozapps/extensions/test/unit/addons/test_bug378216_2/install.rdf => toolkit/mozapps/extensions/test/addons/test_bug378216_2/install.rdf rename : toolkit/mozapps/extensions/test/unit/addons/test_bug378216_3/install.rdf => toolkit/mozapps/extensions/test/addons/test_bug378216_3/install.rdf rename : toolkit/mozapps/extensions/test/unit/addons/test_bug378216_4/install.rdf => toolkit/mozapps/extensions/test/addons/test_bug378216_4/install.rdf rename : toolkit/mozapps/extensions/test/unit/addons/test_bug378216_5/install.rdf => toolkit/mozapps/extensions/test/addons/test_bug378216_5/install.rdf rename : toolkit/mozapps/extensions/test/unit/addons/test_bug378216_6/install.rdf => toolkit/mozapps/extensions/test/addons/test_bug378216_6/install.rdf rename : toolkit/mozapps/extensions/test/unit/addons/test_bug378216_7/install.rdf => toolkit/mozapps/extensions/test/addons/test_bug378216_7/install.rdf rename : toolkit/mozapps/extensions/test/unit/addons/test_bug378216_8/install.rdf => toolkit/mozapps/extensions/test/addons/test_bug378216_8/install.rdf rename : toolkit/mozapps/extensions/test/unit/addons/test_bug378216_9/install.rdf => toolkit/mozapps/extensions/test/addons/test_bug378216_9/install.rdf rename : toolkit/mozapps/extensions/test/unit/addons/test_bug394300_1/install.rdf => toolkit/mozapps/extensions/test/addons/test_bug394300_1/install.rdf rename : toolkit/mozapps/extensions/test/unit/addons/test_bug394300_2/install.rdf => toolkit/mozapps/extensions/test/addons/test_bug394300_2/install.rdf rename : toolkit/mozapps/extensions/test/unit/addons/test_bug396129_a_1/install.rdf => toolkit/mozapps/extensions/test/addons/test_bug396129_a_1/install.rdf rename : toolkit/mozapps/extensions/test/unit/addons/test_bug396129_a_2/install.rdf => toolkit/mozapps/extensions/test/addons/test_bug396129_a_2/install.rdf rename : toolkit/mozapps/extensions/test/unit/addons/test_bug396129_b_1/install.rdf => toolkit/mozapps/extensions/test/addons/test_bug396129_b_1/install.rdf rename : toolkit/mozapps/extensions/test/unit/addons/test_bug396129_b_2/install.rdf => toolkit/mozapps/extensions/test/addons/test_bug396129_b_2/install.rdf rename : toolkit/mozapps/extensions/test/unit/addons/test_bug396129_c_1/install.rdf => toolkit/mozapps/extensions/test/addons/test_bug396129_c_1/install.rdf rename : toolkit/mozapps/extensions/test/unit/addons/test_bug396129_c_2/install.rdf => toolkit/mozapps/extensions/test/addons/test_bug396129_c_2/install.rdf rename : toolkit/mozapps/extensions/test/unit/addons/test_bug396129_d_1/install.rdf => toolkit/mozapps/extensions/test/addons/test_bug396129_d_1/install.rdf rename : toolkit/mozapps/extensions/test/unit/addons/test_bug396129_d_2/install.rdf => toolkit/mozapps/extensions/test/addons/test_bug396129_d_2/install.rdf rename : toolkit/mozapps/extensions/test/unit/addons/test_bug397778/install.rdf => toolkit/mozapps/extensions/test/addons/test_bug397778/install.rdf rename : toolkit/mozapps/extensions/test/unit/addons/test_bug425657/install.rdf => toolkit/mozapps/extensions/test/addons/test_bug425657/install.rdf rename : toolkit/mozapps/extensions/test/unit/addons/test_bug428341_1/README => toolkit/mozapps/extensions/test/addons/test_bug428341_1/README rename : toolkit/mozapps/extensions/test/unit/addons/test_bug428341_2/install.rdf => toolkit/mozapps/extensions/test/addons/test_bug428341_2/install.rdf rename : toolkit/mozapps/extensions/test/unit/addons/test_bug428341_3/install.rdf => toolkit/mozapps/extensions/test/addons/test_bug428341_3/install.rdf rename : toolkit/mozapps/extensions/test/unit/addons/test_bug428341_4/install.rdf => toolkit/mozapps/extensions/test/addons/test_bug428341_4/install.rdf rename : toolkit/mozapps/extensions/test/unit/addons/test_bug428341_5/install.rdf => toolkit/mozapps/extensions/test/addons/test_bug428341_5/install.rdf rename : toolkit/mozapps/extensions/test/unit/addons/test_bug428341_6/install.rdf => toolkit/mozapps/extensions/test/addons/test_bug428341_6/install.rdf rename : toolkit/mozapps/extensions/test/unit/addons/test_bug428341_7/install.rdf => toolkit/mozapps/extensions/test/addons/test_bug428341_7/install.rdf rename : toolkit/mozapps/extensions/test/unit/addons/test_bug428341_8/install.rdf => toolkit/mozapps/extensions/test/addons/test_bug428341_8/install.rdf rename : toolkit/mozapps/extensions/test/unit/addons/test_bug428341_9/install.rdf => toolkit/mozapps/extensions/test/addons/test_bug428341_9/install.rdf rename : toolkit/mozapps/extensions/test/unit/addons/test_bug436207/install.js => toolkit/mozapps/extensions/test/addons/test_bug436207/install.js rename : toolkit/mozapps/extensions/test/unit/addons/test_bug463819_1/install.rdf => toolkit/mozapps/extensions/test/addons/test_bug463819_1/install.rdf rename : toolkit/mozapps/extensions/test/unit/addons/test_bug463819_2/install.rdf => toolkit/mozapps/extensions/test/addons/test_bug463819_2/install.rdf rename : toolkit/mozapps/extensions/test/unit/addons/test_bug463819_3/install.rdf => toolkit/mozapps/extensions/test/addons/test_bug463819_3/install.rdf rename : toolkit/mozapps/extensions/test/unit/addons/test_bug463819_4/install.rdf => toolkit/mozapps/extensions/test/addons/test_bug463819_4/install.rdf rename : toolkit/mozapps/extensions/test/unit/addons/test_bug463819_5/install.rdf => toolkit/mozapps/extensions/test/addons/test_bug463819_5/install.rdf rename : toolkit/mozapps/extensions/test/unit/addons/test_bug463819_6/install.rdf => toolkit/mozapps/extensions/test/addons/test_bug463819_6/install.rdf rename : toolkit/mozapps/extensions/test/unit/addons/test_bug463819_7/install.rdf => toolkit/mozapps/extensions/test/addons/test_bug463819_7/install.rdf rename : toolkit/mozapps/extensions/test/unit/addons/test_bug463819_8/install.rdf => toolkit/mozapps/extensions/test/addons/test_bug463819_8/install.rdf rename : toolkit/mozapps/extensions/test/unit/addons/test_bug463819_9/install.rdf => toolkit/mozapps/extensions/test/addons/test_bug463819_9/install.rdf
2009-03-21 08:20:00 -07:00
do_load_httpd_js();
// List of Content-Type headers to test. For each header we have an array.
// The first element in the array is the Content-Type header string. The
// second element in the array is a boolean indicating whether we allow
// sniffing for that type.
var contentTypeHeaderList =
[
[ "text/plain", true ],
[ "text/plain; charset=ISO-8859-1", true ],
[ "text/plain; charset=iso-8859-1", true ],
[ "text/plain; charset=UTF-8", true ],
[ "text/plain; charset=unknown", false ],
[ "text/plain; param", false ],
[ "text/plain; charset=ISO-8859-1; param", false ],
[ "text/plain; charset=iso-8859-1; param", false ],
[ "text/plain; charset=UTF-8; param", false ],
[ "text/plain; charset=utf-8", false ],
[ "text/plain; charset=utf8", false ],
[ "text/plain; charset=UTF8", false ],
[ "text/plain; charset=iSo-8859-1", false ]
];
// List of response bodies to test. For each response we have an array. The
// first element in the array is the body string. The second element in the
// array is a boolean indicating whether that string should sniff as binary.
var bodyList =
[
[ "Plaintext", false ]
];
// List of possible BOMs
var BOMList =
[
"\xFE\xFF", // UTF-16BE
"\xFF\xFE", // UTF-16LE
"\xEF\xBB\xBF", // UTF-8
"\x00\x00\xFE\xFF", // UCS-4BE
"\x00\x00\xFF\xFE" // UCS-4LE
];
// Build up bodyList. The things we treat as binary are ASCII codes 0-8,
// 14-26, 28-31. That is, the control char range, except for tab, newline,
// vertical tab, form feed, carriage return, and ESC (this last being used by
// Shift_JIS, apparently).
function isBinaryChar(ch) {
return (0 <= ch && ch <= 8) || (14 <= ch && ch <= 26) ||
(28 <= ch && ch <= 31);
}
// Test chars on their own
var i;
for (i = 0; i <= 127; ++i) {
bodyList.push([ String.fromCharCode(i), isBinaryChar(i) ]);
}
// Test that having a BOM prevents plaintext sniffing
var j;
for (i = 0; i <= 127; ++i) {
for (j = 0; j < BOMList.length; ++j) {
bodyList.push([ BOMList[j] + String.fromCharCode(i, i), false ]);
}
}
// Test that having a BOM requires at least 4 chars to kick in
for (i = 0; i <= 127; ++i) {
for (j = 0; j < BOMList.length; ++j) {
bodyList.push([ BOMList[j] + String.fromCharCode(i),
BOMList[j].length == 2 && isBinaryChar(i) ]);
}
}
function makeChan(headerIdx, bodyIdx) {
var ios = Components.classes["@mozilla.org/network/io-service;1"]
.getService(Components.interfaces.nsIIOService);
var chan =
ios.newChannel("http://localhost:4444/" + headerIdx + "/" + bodyIdx, null,
null)
.QueryInterface(Components.interfaces.nsIHttpChannel);
chan.loadFlags |=
Components.interfaces.nsIChannel.LOAD_CALL_CONTENT_SNIFFERS;
return chan;
}
function makeListener(headerIdx, bodyIdx) {
var listener = {
onStartRequest : function test_onStartR(request, ctx) {
try {
var chan = request.QueryInterface(Components.interfaces.nsIChannel);
2008-01-28 15:23:55 -08:00
do_check_eq(chan.status, Components.results.NS_OK);
var type = chan.contentType;
var expectedType =
contentTypeHeaderList[headerIdx][1] && bodyList[bodyIdx][1] ?
"application/x-vnd.mozilla.guess-from-ext" : "text/plain";
if (expectedType != type) {
do_throw("Unexpected sniffed type '" + type + "'. " +
"Should be '" + expectedType + "'. " +
"Header is ['" +
contentTypeHeaderList[headerIdx][0] + "', " +
contentTypeHeaderList[headerIdx][1] + "]. " +
"Body is ['" +
bodyList[bodyIdx][0].toSource() + "', " +
bodyList[bodyIdx][1] +
"].");
}
do_check_eq(expectedType, type);
} catch (e) {
do_throw("Unexpected exception: " + e);
}
throw Components.results.NS_ERROR_ABORT;
},
onDataAvailable: function test_ODA() {
do_throw("Should not get any data!");
},
onStopRequest: function test_onStopR(request, ctx, status) {
// Advance to next test
++headerIdx;
if (headerIdx == contentTypeHeaderList.length) {
headerIdx = 0;
++bodyIdx;
}
if (bodyIdx == bodyList.length) {
do_test_pending();
httpserv.stop(do_test_finished);
} else {
doTest(headerIdx, bodyIdx);
}
do_test_finished();
}
};
return listener;
}
function doTest(headerIdx, bodyIdx) {
var chan = makeChan(headerIdx, bodyIdx);
var listener = makeListener(headerIdx, bodyIdx);
chan.asyncOpen(listener, null);
do_test_pending();
}
function createResponse(headerIdx, bodyIdx, metadata, response) {
response.setHeader("Content-Type", contentTypeHeaderList[headerIdx][0], false);
response.bodyOutputStream.write(bodyList[bodyIdx][0],
bodyList[bodyIdx][0].length);
}
function makeHandler(headerIdx, bodyIdx) {
var f =
function handlerClosure(metadata, response) {
return createResponse(headerIdx, bodyIdx, metadata, response);
};
return f;
}
var httpserv;
function run_test() {
// disable again for everything for now (causes sporatic oranges)
return;
2008-01-28 15:23:55 -08:00
// disable on Windows for now, because it seems to leak sockets and die.
// Silly operating system!
// This is a really nasty way to detect Windows. I wish we could do better.
if ("@mozilla.org/windows-registry-key;1" in Cc) {
return;
}
2007-12-03 16:23:44 -08:00
httpserv = new nsHttpServer();
for (i = 0; i < contentTypeHeaderList.length; ++i) {
for (j = 0; j < bodyList.length; ++j) {
httpserv.registerPathHandler("/" + i + "/" + j, makeHandler(i, j));
}
}
httpserv.start(4444);
doTest(0, 0);
}