tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_bug812167.js (4238B)


      1 "use strict";
      2 
      3 const { HttpServer } = ChromeUtils.importESModule(
      4  "resource://testing-common/httpd.sys.mjs"
      5 );
      6 
      7 /*
      8 - get 302 with Cache-control: no-store
      9 - check cache entry for the 302 response is cached only in memory device
     10 - get 302 with Expires: -1
     11 - check cache entry for the 302 response is not cached at all
     12 */
     13 
     14 var httpserver = null;
     15 // Need to randomize, because apparently no one clears our cache
     16 var randomPath1 = "/redirect-no-store/" + Math.random();
     17 
     18 ChromeUtils.defineLazyGetter(this, "randomURI1", function () {
     19  return "http://localhost:" + httpserver.identity.primaryPort + randomPath1;
     20 });
     21 
     22 var randomPath2 = "/redirect-expires-past/" + Math.random();
     23 
     24 ChromeUtils.defineLazyGetter(this, "randomURI2", function () {
     25  return "http://localhost:" + httpserver.identity.primaryPort + randomPath2;
     26 });
     27 
     28 function make_channel(url) {
     29  return NetUtil.newChannel({ uri: url, loadUsingSystemPrincipal: true });
     30 }
     31 
     32 const responseBody = "response body";
     33 
     34 var redirectHandler_NoStore_calls = 0;
     35 function redirectHandler_NoStore(metadata, response) {
     36  response.setStatusLine(metadata.httpVersion, 302, "Found");
     37  response.setHeader(
     38    "Location",
     39    "http://localhost:" + httpserver.identity.primaryPort + "/content",
     40    false
     41  );
     42  response.setHeader("Cache-control", "no-store");
     43  ++redirectHandler_NoStore_calls;
     44 }
     45 
     46 var redirectHandler_ExpiresInPast_calls = 0;
     47 function redirectHandler_ExpiresInPast(metadata, response) {
     48  response.setStatusLine(metadata.httpVersion, 302, "Found");
     49  response.setHeader(
     50    "Location",
     51    "http://localhost:" + httpserver.identity.primaryPort + "/content",
     52    false
     53  );
     54  response.setHeader("Expires", "-1");
     55  ++redirectHandler_ExpiresInPast_calls;
     56 }
     57 
     58 function contentHandler(metadata, response) {
     59  response.setHeader("Content-Type", "text/plain");
     60  response.bodyOutputStream.write(responseBody, responseBody.length);
     61 }
     62 
     63 function check_response(
     64  path,
     65  request,
     66  buffer,
     67  expectedExpiration,
     68  continuation
     69 ) {
     70  Assert.equal(buffer, responseBody);
     71 
     72  // Entry is always there, old cache wrapping code does session->SetDoomEntriesIfExpired(false),
     73  // just check it's not persisted or is expired (dep on the test).
     74  asyncOpenCacheEntry(
     75    path,
     76    "disk",
     77    Ci.nsICacheStorage.OPEN_READONLY,
     78    null,
     79    function (status, entry) {
     80      Assert.equal(status, 0);
     81 
     82      // Expired entry is on disk, no-store entry is in memory
     83      Assert.equal(entry.persistent, expectedExpiration);
     84 
     85      // Do the request again and check the server handler is called appropriately
     86      var chan = make_channel(path);
     87      chan.asyncOpen(
     88        new ChannelListener(function (request1, buffer1) {
     89          Assert.equal(buffer1, responseBody);
     90 
     91          if (expectedExpiration) {
     92            // Handler had to be called second time
     93            Assert.equal(redirectHandler_ExpiresInPast_calls, 2);
     94          } else {
     95            // Handler had to be called second time (no-store forces validate),
     96            // and we are just in memory
     97            Assert.equal(redirectHandler_NoStore_calls, 2);
     98            Assert.ok(!entry.persistent);
     99          }
    100 
    101          continuation();
    102        }, null)
    103      );
    104    }
    105  );
    106 }
    107 
    108 function run_test_no_store() {
    109  var chan = make_channel(randomURI1);
    110  chan.asyncOpen(
    111    new ChannelListener(function (request, buffer) {
    112      // Cache-control: no-store response should only be found in the memory cache.
    113      check_response(randomURI1, request, buffer, false, run_test_expires_past);
    114    }, null)
    115  );
    116 }
    117 
    118 function run_test_expires_past() {
    119  var chan = make_channel(randomURI2);
    120  chan.asyncOpen(
    121    new ChannelListener(function (request, buffer) {
    122      // Expires: -1 response should not be found in any cache.
    123      check_response(randomURI2, request, buffer, true, finish_test);
    124    }, null)
    125  );
    126 }
    127 
    128 function finish_test() {
    129  httpserver.stop(do_test_finished);
    130 }
    131 
    132 function run_test() {
    133  do_get_profile();
    134 
    135  httpserver = new HttpServer();
    136  httpserver.registerPathHandler(randomPath1, redirectHandler_NoStore);
    137  httpserver.registerPathHandler(randomPath2, redirectHandler_ExpiresInPast);
    138  httpserver.registerPathHandler("/content", contentHandler);
    139  httpserver.start(-1);
    140 
    141  run_test_no_store();
    142  do_test_pending();
    143 }