gecko-dev/netwerk/test/unit/test_bug482601.js
Michal Novotny ee8f43208a Bug 1448476 - Cache entry corruption after writing the alternate data. r=honzab
When writing to alt-data output stream fails for whatever reason, we now try to
truncate alternative data and keep the original data instead of dooming the
whole entry. The patch also changes how is the predicted size passed to the
cache. Instead of a dedicated method it's now an argument of openOutputStream
and openAlternativeOutputStream methods which fail in case the entry would
exceed the allowed limit.
2018-04-25 07:01:00 +03:00

234 lines
7.3 KiB
JavaScript

ChromeUtils.import("resource://testing-common/httpd.js");
ChromeUtils.import("resource://gre/modules/NetUtil.jsm");
var httpserv = null;
var test_nr = 0;
var observers_called = "";
var handlers_called = "";
var buffer = "";
var observer = {
QueryInterface: function (aIID) {
if (aIID.equals(Ci.nsISupports) ||
aIID.equals(Ci.nsIObserver))
return this;
throw Cr.NS_ERROR_NO_INTERFACE;
},
observe: function(subject, topic, data) {
if (observers_called.length)
observers_called += ",";
observers_called += topic;
}
};
var listener = {
onStartRequest: function (request, ctx) {
buffer = "";
},
onDataAvailable: function (request, ctx, stream, offset, count) {
buffer = buffer.concat(read_stream(stream, count));
},
onStopRequest: function (request, ctx, status) {
Assert.equal(status, Cr.NS_OK);
Assert.equal(buffer, "0123456789");
Assert.equal(observers_called, results[test_nr]);
test_nr++;
do_timeout(0, do_test);
}
};
function run_test() {
httpserv = new HttpServer();
httpserv.registerPathHandler("/bug482601/nocache", bug482601_nocache);
httpserv.registerPathHandler("/bug482601/partial", bug482601_partial);
httpserv.registerPathHandler("/bug482601/cached", bug482601_cached);
httpserv.registerPathHandler("/bug482601/only_from_cache", bug482601_only_from_cache);
httpserv.start(-1);
var obs = Cc["@mozilla.org/observer-service;1"].getService();
obs = obs.QueryInterface(Ci.nsIObserverService);
obs.addObserver(observer, "http-on-examine-response");
obs.addObserver(observer, "http-on-examine-merged-response");
obs.addObserver(observer, "http-on-examine-cached-response");
do_timeout(0, do_test);
do_test_pending();
}
function do_test() {
if (test_nr < tests.length) {
tests[test_nr]();
}
else {
Assert.equal(handlers_called, "nocache,partial,cached");
httpserv.stop(do_test_finished);
}
}
var tests = [test_nocache,
test_partial,
test_cached,
test_only_from_cache];
var results = ["http-on-examine-response",
"http-on-examine-response,http-on-examine-merged-response",
"http-on-examine-response,http-on-examine-merged-response",
"http-on-examine-cached-response"];
function makeChan(url) {
return NetUtil.newChannel({uri: url, loadUsingSystemPrincipal: true})
.QueryInterface(Ci.nsIHttpChannel);
}
function storeCache(aCacheEntry, aResponseHeads, aContent) {
aCacheEntry.setMetaDataElement("request-method", "GET");
aCacheEntry.setMetaDataElement("response-head", aResponseHeads);
aCacheEntry.setMetaDataElement("charset", "ISO-8859-1");
var oStream = aCacheEntry.openOutputStream(0, aContent.length);
var written = oStream.write(aContent, aContent.length);
if (written != aContent.length) {
do_throw("oStream.write has not written all data!\n" +
" Expected: " + written + "\n" +
" Actual: " + aContent.length + "\n");
}
oStream.close();
aCacheEntry.close();
}
function test_nocache() {
observers_called = "";
var chan = makeChan("http://localhost:" + httpserv.identity.primaryPort +
"/bug482601/nocache");
chan.asyncOpen2(listener);
}
function test_partial() {
asyncOpenCacheEntry("http://localhost:" + httpserv.identity.primaryPort +
"/bug482601/partial",
"disk", Ci.nsICacheStorage.OPEN_NORMALLY, null,
test_partial2);
}
function test_partial2(status, entry) {
Assert.equal(status, Cr.NS_OK);
storeCache(entry,
"HTTP/1.1 200 OK\r\n" +
"Date: Thu, 1 Jan 2009 00:00:00 GMT\r\n" +
"Server: httpd.js\r\n" +
"Last-Modified: Thu, 1 Jan 2009 00:00:00 GMT\r\n" +
"Accept-Ranges: bytes\r\n" +
"Content-Length: 10\r\n" +
"Content-Type: text/plain\r\n",
"0123");
observers_called = "";
var chan = makeChan("http://localhost:" + httpserv.identity.primaryPort +
"/bug482601/partial");
chan.asyncOpen2(listener);
}
function test_cached() {
asyncOpenCacheEntry("http://localhost:" + httpserv.identity.primaryPort +
"/bug482601/cached",
"disk", Ci.nsICacheStorage.OPEN_NORMALLY, null,
test_cached2);
}
function test_cached2(status, entry) {
Assert.equal(status, Cr.NS_OK);
storeCache(entry,
"HTTP/1.1 200 OK\r\n" +
"Date: Thu, 1 Jan 2009 00:00:00 GMT\r\n" +
"Server: httpd.js\r\n" +
"Last-Modified: Thu, 1 Jan 2009 00:00:00 GMT\r\n" +
"Accept-Ranges: bytes\r\n" +
"Content-Length: 10\r\n" +
"Content-Type: text/plain\r\n",
"0123456789");
observers_called = "";
var chan = makeChan("http://localhost:" + httpserv.identity.primaryPort +
"/bug482601/cached");
chan.loadFlags = Ci.nsIRequest.VALIDATE_ALWAYS;
chan.asyncOpen2(listener);
}
function test_only_from_cache() {
asyncOpenCacheEntry("http://localhost:" + httpserv.identity.primaryPort +
"/bug482601/only_from_cache",
"disk", Ci.nsICacheStorage.OPEN_NORMALLY, null,
test_only_from_cache2);
}
function test_only_from_cache2(status, entry) {
Assert.equal(status, Cr.NS_OK);
storeCache(entry,
"HTTP/1.1 200 OK\r\n" +
"Date: Thu, 1 Jan 2009 00:00:00 GMT\r\n" +
"Server: httpd.js\r\n" +
"Last-Modified: Thu, 1 Jan 2009 00:00:00 GMT\r\n" +
"Accept-Ranges: bytes\r\n" +
"Content-Length: 10\r\n" +
"Content-Type: text/plain\r\n",
"0123456789");
observers_called = "";
var chan = makeChan("http://localhost:" + httpserv.identity.primaryPort +
"/bug482601/only_from_cache");
chan.loadFlags = Ci.nsICachingChannel.LOAD_ONLY_FROM_CACHE;
chan.asyncOpen2(listener);
}
// PATHS
// /bug482601/nocache
function bug482601_nocache(metadata, response) {
response.setHeader("Content-Type", "text/plain", false);
var body = "0123456789";
response.bodyOutputStream.write(body, body.length);
handlers_called += "nocache";
}
// /bug482601/partial
function bug482601_partial(metadata, response) {
Assert.ok(metadata.hasHeader("If-Range"));
Assert.equal(metadata.getHeader("If-Range"),
"Thu, 1 Jan 2009 00:00:00 GMT");
Assert.ok(metadata.hasHeader("Range"));
Assert.equal(metadata.getHeader("Range"), "bytes=4-");
response.setStatusLine(metadata.httpVersion, 206, "Partial Content");
response.setHeader("Content-Range", "bytes 4-9/10", false);
response.setHeader("Content-Type", "text/plain", false);
response.setHeader("Last-Modified", "Thu, 1 Jan 2009 00:00:00 GMT");
var body = "456789";
response.bodyOutputStream.write(body, body.length);
handlers_called += ",partial";
}
// /bug482601/cached
function bug482601_cached(metadata, response) {
Assert.ok(metadata.hasHeader("If-Modified-Since"));
Assert.equal(metadata.getHeader("If-Modified-Since"),
"Thu, 1 Jan 2009 00:00:00 GMT");
response.setStatusLine(metadata.httpVersion, 304, "Not Modified");
handlers_called += ",cached";
}
// /bug482601/only_from_cache
function bug482601_only_from_cache(metadata, response) {
do_throw("This should not be reached");
}