netwerk/test/unit/test_bug482601.js

Thu, 15 Jan 2015 15:59:08 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:59:08 +0100
branch
TOR_BUG_9701
changeset 10
ac0c01689b40
permissions
-rw-r--r--

Implement a real Private Browsing Mode condition by changing the API/ABI;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 Cu.import("resource://testing-common/httpd.js");
michael@0 2
michael@0 3 var httpserv = null;
michael@0 4 var test_nr = 0;
michael@0 5 var observers_called = "";
michael@0 6 var handlers_called = "";
michael@0 7 var buffer = "";
michael@0 8
michael@0 9 var observer = {
michael@0 10 QueryInterface: function (aIID) {
michael@0 11 if (aIID.equals(Ci.nsISupports) ||
michael@0 12 aIID.equals(Ci.nsIObserver))
michael@0 13 return this;
michael@0 14 throw Cr.NS_ERROR_NO_INTERFACE;
michael@0 15 },
michael@0 16
michael@0 17 observe: function(subject, topic, data) {
michael@0 18 if (observers_called.length)
michael@0 19 observers_called += ",";
michael@0 20
michael@0 21 observers_called += topic;
michael@0 22 }
michael@0 23 };
michael@0 24
michael@0 25 var listener = {
michael@0 26 onStartRequest: function (request, ctx) {
michael@0 27 buffer = "";
michael@0 28 },
michael@0 29
michael@0 30 onDataAvailable: function (request, ctx, stream, offset, count) {
michael@0 31 buffer = buffer.concat(read_stream(stream, count));
michael@0 32 },
michael@0 33
michael@0 34 onStopRequest: function (request, ctx, status) {
michael@0 35 do_check_eq(status, Cr.NS_OK);
michael@0 36 do_check_eq(buffer, "0123456789");
michael@0 37 do_check_eq(observers_called, results[test_nr]);
michael@0 38 test_nr++;
michael@0 39 do_timeout(0, do_test);
michael@0 40 }
michael@0 41 };
michael@0 42
michael@0 43 function run_test() {
michael@0 44 httpserv = new HttpServer();
michael@0 45 httpserv.registerPathHandler("/bug482601/nocache", bug482601_nocache);
michael@0 46 httpserv.registerPathHandler("/bug482601/partial", bug482601_partial);
michael@0 47 httpserv.registerPathHandler("/bug482601/cached", bug482601_cached);
michael@0 48 httpserv.registerPathHandler("/bug482601/only_from_cache", bug482601_only_from_cache);
michael@0 49 httpserv.start(-1);
michael@0 50
michael@0 51 var obs = Cc["@mozilla.org/observer-service;1"].getService();
michael@0 52 obs = obs.QueryInterface(Ci.nsIObserverService);
michael@0 53 obs.addObserver(observer, "http-on-examine-response", false);
michael@0 54 obs.addObserver(observer, "http-on-examine-merged-response", false);
michael@0 55 obs.addObserver(observer, "http-on-examine-cached-response", false);
michael@0 56
michael@0 57 do_timeout(0, do_test);
michael@0 58 do_test_pending();
michael@0 59 }
michael@0 60
michael@0 61 function do_test() {
michael@0 62 if (test_nr < tests.length) {
michael@0 63 tests[test_nr]();
michael@0 64 }
michael@0 65 else {
michael@0 66 do_check_eq(handlers_called, "nocache,partial,cached");
michael@0 67 httpserv.stop(do_test_finished);
michael@0 68 }
michael@0 69 }
michael@0 70
michael@0 71 var tests = [test_nocache,
michael@0 72 test_partial,
michael@0 73 test_cached,
michael@0 74 test_only_from_cache];
michael@0 75
michael@0 76 var results = ["http-on-examine-response",
michael@0 77 "http-on-examine-response,http-on-examine-merged-response",
michael@0 78 "http-on-examine-response,http-on-examine-merged-response",
michael@0 79 "http-on-examine-cached-response"];
michael@0 80
michael@0 81 function makeChan(url) {
michael@0 82 var ios = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
michael@0 83 var chan = ios.newChannel(url, null, null).QueryInterface(Ci.nsIHttpChannel);
michael@0 84 return chan;
michael@0 85 }
michael@0 86
michael@0 87 function storeCache(aCacheEntry, aResponseHeads, aContent) {
michael@0 88 aCacheEntry.setMetaDataElement("request-method", "GET");
michael@0 89 aCacheEntry.setMetaDataElement("response-head", aResponseHeads);
michael@0 90 aCacheEntry.setMetaDataElement("charset", "ISO-8859-1");
michael@0 91
michael@0 92 var oStream = aCacheEntry.openOutputStream(0);
michael@0 93 var written = oStream.write(aContent, aContent.length);
michael@0 94 if (written != aContent.length) {
michael@0 95 do_throw("oStream.write has not written all data!\n" +
michael@0 96 " Expected: " + written + "\n" +
michael@0 97 " Actual: " + aContent.length + "\n");
michael@0 98 }
michael@0 99 oStream.close();
michael@0 100 aCacheEntry.close();
michael@0 101 }
michael@0 102
michael@0 103 function test_nocache() {
michael@0 104 observers_called = "";
michael@0 105
michael@0 106 var chan = makeChan("http://localhost:" + httpserv.identity.primaryPort +
michael@0 107 "/bug482601/nocache");
michael@0 108 chan.asyncOpen(listener, null);
michael@0 109 }
michael@0 110
michael@0 111 function test_partial() {
michael@0 112 asyncOpenCacheEntry("http://localhost:" + httpserv.identity.primaryPort +
michael@0 113 "/bug482601/partial",
michael@0 114 "disk", Ci.nsICacheStorage.OPEN_NORMALLY, null,
michael@0 115 test_partial2);
michael@0 116 }
michael@0 117
michael@0 118 function test_partial2(status, entry) {
michael@0 119 do_check_eq(status, Cr.NS_OK);
michael@0 120 storeCache(entry,
michael@0 121 "HTTP/1.1 200 OK\r\n" +
michael@0 122 "Date: Thu, 1 Jan 2009 00:00:00 GMT\r\n" +
michael@0 123 "Server: httpd.js\r\n" +
michael@0 124 "Last-Modified: Thu, 1 Jan 2009 00:00:00 GMT\r\n" +
michael@0 125 "Accept-Ranges: bytes\r\n" +
michael@0 126 "Content-Length: 10\r\n" +
michael@0 127 "Content-Type: text/plain\r\n",
michael@0 128 "0123");
michael@0 129
michael@0 130 observers_called = "";
michael@0 131
michael@0 132 var chan = makeChan("http://localhost:" + httpserv.identity.primaryPort +
michael@0 133 "/bug482601/partial");
michael@0 134 chan.asyncOpen(listener, null);
michael@0 135 }
michael@0 136
michael@0 137 function test_cached() {
michael@0 138 asyncOpenCacheEntry("http://localhost:" + httpserv.identity.primaryPort +
michael@0 139 "/bug482601/cached",
michael@0 140 "disk", Ci.nsICacheStorage.OPEN_NORMALLY, null,
michael@0 141 test_cached2);
michael@0 142 }
michael@0 143
michael@0 144 function test_cached2(status, entry) {
michael@0 145 do_check_eq(status, Cr.NS_OK);
michael@0 146 storeCache(entry,
michael@0 147 "HTTP/1.1 200 OK\r\n" +
michael@0 148 "Date: Thu, 1 Jan 2009 00:00:00 GMT\r\n" +
michael@0 149 "Server: httpd.js\r\n" +
michael@0 150 "Last-Modified: Thu, 1 Jan 2009 00:00:00 GMT\r\n" +
michael@0 151 "Accept-Ranges: bytes\r\n" +
michael@0 152 "Content-Length: 10\r\n" +
michael@0 153 "Content-Type: text/plain\r\n",
michael@0 154 "0123456789");
michael@0 155
michael@0 156 observers_called = "";
michael@0 157
michael@0 158 var chan = makeChan("http://localhost:" + httpserv.identity.primaryPort +
michael@0 159 "/bug482601/cached");
michael@0 160 chan.loadFlags = Ci.nsIRequest.VALIDATE_ALWAYS;
michael@0 161 chan.asyncOpen(listener, null);
michael@0 162 }
michael@0 163
michael@0 164 function test_only_from_cache() {
michael@0 165 asyncOpenCacheEntry("http://localhost:" + httpserv.identity.primaryPort +
michael@0 166 "/bug482601/only_from_cache",
michael@0 167 "disk", Ci.nsICacheStorage.OPEN_NORMALLY, null,
michael@0 168 test_only_from_cache2);
michael@0 169 }
michael@0 170
michael@0 171 function test_only_from_cache2(status, entry) {
michael@0 172 do_check_eq(status, Cr.NS_OK);
michael@0 173 storeCache(entry,
michael@0 174 "HTTP/1.1 200 OK\r\n" +
michael@0 175 "Date: Thu, 1 Jan 2009 00:00:00 GMT\r\n" +
michael@0 176 "Server: httpd.js\r\n" +
michael@0 177 "Last-Modified: Thu, 1 Jan 2009 00:00:00 GMT\r\n" +
michael@0 178 "Accept-Ranges: bytes\r\n" +
michael@0 179 "Content-Length: 10\r\n" +
michael@0 180 "Content-Type: text/plain\r\n",
michael@0 181 "0123456789");
michael@0 182
michael@0 183 observers_called = "";
michael@0 184
michael@0 185 var chan = makeChan("http://localhost:" + httpserv.identity.primaryPort +
michael@0 186 "/bug482601/only_from_cache");
michael@0 187 chan.loadFlags = Ci.nsICachingChannel.LOAD_ONLY_FROM_CACHE;
michael@0 188 chan.asyncOpen(listener, null);
michael@0 189 }
michael@0 190
michael@0 191
michael@0 192 // PATHS
michael@0 193
michael@0 194 // /bug482601/nocache
michael@0 195 function bug482601_nocache(metadata, response) {
michael@0 196 response.setHeader("Content-Type", "text/plain", false);
michael@0 197 var body = "0123456789";
michael@0 198 response.bodyOutputStream.write(body, body.length);
michael@0 199 handlers_called += "nocache";
michael@0 200 }
michael@0 201
michael@0 202 // /bug482601/partial
michael@0 203 function bug482601_partial(metadata, response) {
michael@0 204 do_check_true(metadata.hasHeader("If-Range"));
michael@0 205 do_check_eq(metadata.getHeader("If-Range"),
michael@0 206 "Thu, 1 Jan 2009 00:00:00 GMT");
michael@0 207 do_check_true(metadata.hasHeader("Range"));
michael@0 208 do_check_eq(metadata.getHeader("Range"), "bytes=4-");
michael@0 209
michael@0 210 response.setStatusLine(metadata.httpVersion, 206, "Partial Content");
michael@0 211 response.setHeader("Content-Range", "bytes 4-9/10", false);
michael@0 212 response.setHeader("Content-Type", "text/plain", false);
michael@0 213 response.setHeader("Last-Modified", "Thu, 1 Jan 2009 00:00:00 GMT");
michael@0 214
michael@0 215 var body = "456789";
michael@0 216 response.bodyOutputStream.write(body, body.length);
michael@0 217 handlers_called += ",partial";
michael@0 218 }
michael@0 219
michael@0 220 // /bug482601/cached
michael@0 221 function bug482601_cached(metadata, response) {
michael@0 222 do_check_true(metadata.hasHeader("If-Modified-Since"));
michael@0 223 do_check_eq(metadata.getHeader("If-Modified-Since"),
michael@0 224 "Thu, 1 Jan 2009 00:00:00 GMT");
michael@0 225
michael@0 226 response.setStatusLine(metadata.httpVersion, 304, "Not Modified");
michael@0 227 handlers_called += ",cached";
michael@0 228 }
michael@0 229
michael@0 230 // /bug482601/only_from_cache
michael@0 231 function bug482601_only_from_cache(metadata, response) {
michael@0 232 do_throw("This should not be reached");
michael@0 233 }

mercurial