tor-browser

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

test_immutable.js (5922B)


      1 "use strict";
      2 
      3 var prefs;
      4 var http2pref;
      5 var origin;
      6 var rcwnpref;
      7 
      8 function run_test() {
      9  var h2Port = Services.env.get("MOZHTTP2_PORT");
     10  Assert.notEqual(h2Port, null);
     11  Assert.notEqual(h2Port, "");
     12 
     13  // Set to allow the cert presented by our H2 server
     14  do_get_profile();
     15  prefs = Services.prefs;
     16 
     17  http2pref = prefs.getBoolPref("network.http.http2.enabled");
     18  rcwnpref = prefs.getBoolPref("network.http.rcwn.enabled");
     19 
     20  prefs.setBoolPref("network.http.http2.enabled", true);
     21  prefs.setCharPref(
     22    "network.dns.localDomains",
     23    "foo.example.com, bar.example.com"
     24  );
     25  // Disable rcwn to make cache behavior deterministic.
     26  prefs.setBoolPref("network.http.rcwn.enabled", false);
     27 
     28  // The moz-http2 cert is for foo.example.com and is signed by http2-ca.pem
     29  // so add that cert to the trust list as a signing cert.  // the foo.example.com domain name.
     30  let certdb = Cc["@mozilla.org/security/x509certdb;1"].getService(
     31    Ci.nsIX509CertDB
     32  );
     33  addCertFromFile(certdb, "http2-ca.pem", "CTu,u,u");
     34 
     35  origin = "https://foo.example.com:" + h2Port;
     36  dump("origin - " + origin + "\n");
     37  doTest1();
     38 }
     39 
     40 function resetPrefs() {
     41  prefs.setBoolPref("network.http.http2.enabled", http2pref);
     42  prefs.setBoolPref("network.http.rcwn.enabled", rcwnpref);
     43  prefs.clearUserPref("network.dns.localDomains");
     44 }
     45 
     46 function makeChan(path) {
     47  return NetUtil.newChannel({
     48    uri: origin + path,
     49    loadUsingSystemPrincipal: true,
     50  }).QueryInterface(Ci.nsIHttpChannel);
     51 }
     52 
     53 var nextTest;
     54 var expectPass = true;
     55 var expectConditional = false;
     56 
     57 var Listener = function () {};
     58 Listener.prototype = {
     59  onStartRequest: function testOnStartRequest(request) {
     60    Assert.ok(request instanceof Ci.nsIHttpChannel);
     61 
     62    if (expectPass) {
     63      if (!Components.isSuccessCode(request.status)) {
     64        do_throw(
     65          "Channel should have a success code! (" + request.status + ")"
     66        );
     67      }
     68      Assert.equal(request.responseStatus, 200);
     69    } else {
     70      Assert.equal(Components.isSuccessCode(request.status), false);
     71    }
     72  },
     73 
     74  onDataAvailable: function testOnDataAvailable(request, stream, off, cnt) {
     75    read_stream(stream, cnt);
     76  },
     77 
     78  onStopRequest: function testOnStopRequest(request) {
     79    if (expectConditional) {
     80      Assert.equal(request.getResponseHeader("x-conditional"), "true");
     81    } else {
     82      try {
     83        Assert.notEqual(request.getResponseHeader("x-conditional"), "true");
     84      } catch (e) {
     85        Assert.ok(true);
     86      }
     87    }
     88    nextTest();
     89    do_test_finished();
     90  },
     91 };
     92 
     93 function testsDone() {
     94  dump("testDone\n");
     95  resetPrefs();
     96 }
     97 
     98 function doTest1() {
     99  dump("execute doTest1 - resource without immutable. initial request\n");
    100  do_test_pending();
    101  expectConditional = false;
    102  var chan = makeChan("/immutable-test-without-attribute");
    103  var listener = new Listener();
    104  nextTest = doTest2;
    105  chan.asyncOpen(listener);
    106 }
    107 
    108 function doTest2() {
    109  dump("execute doTest2 - resource without immutable. reload\n");
    110  do_test_pending();
    111  expectConditional = true;
    112  var chan = makeChan("/immutable-test-without-attribute");
    113  var listener = new Listener();
    114  nextTest = doTest3;
    115  chan.loadFlags = Ci.nsIRequest.VALIDATE_ALWAYS;
    116  chan.asyncOpen(listener);
    117 }
    118 
    119 function doTest3() {
    120  dump("execute doTest3 - resource without immutable. shift reload\n");
    121  do_test_pending();
    122  expectConditional = false;
    123  var chan = makeChan("/immutable-test-without-attribute");
    124  var listener = new Listener();
    125  nextTest = doTest4;
    126  chan.loadFlags = Ci.nsIRequest.LOAD_BYPASS_CACHE;
    127  chan.asyncOpen(listener);
    128 }
    129 
    130 function doTest4() {
    131  dump("execute doTest4 - resource with immutable. initial request\n");
    132  do_test_pending();
    133  expectConditional = false;
    134  var chan = makeChan("/immutable-test-with-attribute");
    135  var listener = new Listener();
    136  nextTest = doTest5;
    137  chan.asyncOpen(listener);
    138 }
    139 
    140 function doTest5() {
    141  dump("execute doTest5 - resource with immutable. reload\n");
    142  do_test_pending();
    143  expectConditional = false;
    144  var chan = makeChan("/immutable-test-with-attribute");
    145  var listener = new Listener();
    146  nextTest = doTest6;
    147  chan.loadFlags = Ci.nsIRequest.VALIDATE_ALWAYS;
    148  chan.asyncOpen(listener);
    149 }
    150 
    151 function doTest6() {
    152  dump("execute doTest6 - resource with immutable. shift reload\n");
    153  do_test_pending();
    154  expectConditional = false;
    155  var chan = makeChan("/immutable-test-with-attribute");
    156  var listener = new Listener();
    157  nextTest = doTest7;
    158  chan.loadFlags = Ci.nsIRequest.LOAD_BYPASS_CACHE;
    159  chan.asyncOpen(listener);
    160 }
    161 
    162 function doTest7() {
    163  dump("execute doTest7 - expired resource with immutable. initial request\n");
    164  do_test_pending();
    165  expectConditional = false;
    166  var chan = makeChan("/immutable-test-expired-with-Expires-header");
    167  var listener = new Listener();
    168  nextTest = doTest8;
    169  chan.asyncOpen(listener);
    170 }
    171 
    172 function doTest8() {
    173  dump("execute doTest8 - expired resource with immutable. reload\n");
    174  do_test_pending();
    175  expectConditional = true;
    176  var chan = makeChan("/immutable-test-expired-with-Expires-header");
    177  var listener = new Listener();
    178  nextTest = doTest9;
    179  chan.loadFlags = Ci.nsIRequest.VALIDATE_ALWAYS;
    180  chan.asyncOpen(listener);
    181 }
    182 
    183 function doTest9() {
    184  dump(
    185    "execute doTest9 - expired resource with immutable cache extension and Last modified header. initial request\n"
    186  );
    187  do_test_pending();
    188  expectConditional = false;
    189  var chan = makeChan("/immutable-test-expired-with-last-modified-header");
    190  var listener = new Listener();
    191  nextTest = doTest10;
    192  chan.asyncOpen(listener);
    193 }
    194 
    195 function doTest10() {
    196  dump(
    197    "execute doTest10 - expired resource with immutable cache extension and Last modified heder. reload\n"
    198  );
    199  do_test_pending();
    200  expectConditional = true;
    201  var chan = makeChan("/immutable-test-expired-with-last-modified-header");
    202  var listener = new Listener();
    203  nextTest = testsDone;
    204  chan.loadFlags = Ci.nsIRequest.VALIDATE_ALWAYS;
    205  chan.asyncOpen(listener);
    206 }