tor-browser

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

test_tls_flags_separate_connections.js (3094B)


      1 "use strict";
      2 
      3 const { HttpServer } = ChromeUtils.importESModule(
      4  "resource://testing-common/httpd.sys.mjs"
      5 );
      6 
      7 ChromeUtils.defineLazyGetter(this, "URL", function () {
      8  return "http://localhost:" + httpserv.identity.primaryPort;
      9 });
     10 
     11 // This unit test ensures connections with different tlsFlags have their own
     12 // connection pool. We verify this behavior by opening channels with different
     13 // tlsFlags, and their connection info's hash keys should be different.
     14 
     15 // In the first round of this test, we record the hash key for each connection.
     16 // In the second round, we check if each connection's hash key is consistent
     17 // and different from other connection's hash key.
     18 
     19 let httpserv = null;
     20 let gSecondRoundStarted = false;
     21 
     22 let randomFlagValues = [
     23  0x00000000,
     24 
     25  0xffffffff,
     26 
     27  0x12345678, 0x12345678,
     28 
     29  0x11111111, 0x22222222,
     30 
     31  0xaaaaaaaa, 0x77777777,
     32 
     33  0xbbbbbbbb, 0xcccccccc,
     34 ];
     35 
     36 function handler(metadata, response) {
     37  response.setHeader("Content-Type", "text/plain", false);
     38  response.setHeader("Cache-Control", "no-cache", false);
     39  response.setStatusLine(metadata.httpVersion, 200, "OK");
     40  let body = "0123456789";
     41  response.bodyOutputStream.write(body, body.length);
     42 }
     43 
     44 function makeChan(url, tlsFlags) {
     45  let chan = NetUtil.newChannel({ uri: url, loadUsingSystemPrincipal: true });
     46  chan.QueryInterface(Ci.nsIHttpChannelInternal);
     47  chan.tlsFlags = tlsFlags;
     48 
     49  return chan;
     50 }
     51 
     52 let previousHashKeys = {};
     53 
     54 function Listener(tlsFlags) {
     55  this.tlsFlags = tlsFlags;
     56 }
     57 
     58 let gTestsRun = 0;
     59 Listener.prototype = {
     60  onStartRequest(request) {
     61    request
     62      .QueryInterface(Ci.nsIHttpChannel)
     63      .QueryInterface(Ci.nsIHttpChannelInternal);
     64 
     65    Assert.equal(request.tlsFlags, this.tlsFlags);
     66 
     67    let hashKey = request.connectionInfoHashKey;
     68    if (gSecondRoundStarted) {
     69      // Compare the hash keys with the previous set ones.
     70      // Hash keys should match if and only if their tlsFlags are the same.
     71      for (let tlsFlags of randomFlagValues) {
     72        if (tlsFlags == this.tlsFlags) {
     73          Assert.equal(hashKey, previousHashKeys[tlsFlags]);
     74        } else {
     75          Assert.notEqual(hashKey, previousHashKeys[tlsFlags]);
     76        }
     77      }
     78    } else {
     79      // Set the hash keys in the first round.
     80      previousHashKeys[this.tlsFlags] = hashKey;
     81    }
     82  },
     83  onDataAvailable(request, stream, off, cnt) {
     84    read_stream(stream, cnt);
     85  },
     86  onStopRequest() {
     87    gTestsRun++;
     88    if (gTestsRun == randomFlagValues.length) {
     89      gTestsRun = 0;
     90      if (gSecondRoundStarted) {
     91        // The second round finishes.
     92        httpserv.stop(do_test_finished);
     93      } else {
     94        // The first round finishes. Do the second round.
     95        gSecondRoundStarted = true;
     96        doTest();
     97      }
     98    }
     99  },
    100 };
    101 
    102 function doTest() {
    103  for (let tlsFlags of randomFlagValues) {
    104    let chan = makeChan(URL, tlsFlags);
    105    let listener = new Listener(tlsFlags);
    106    chan.asyncOpen(listener);
    107  }
    108 }
    109 
    110 function run_test() {
    111  do_test_pending();
    112  httpserv = new HttpServer();
    113  httpserv.registerPathHandler("/", handler);
    114  httpserv.start(-1);
    115 
    116  doTest();
    117 }