tor-browser

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

test_bug1378385_http1.js (5910B)


      1 // test bug 1378385.
      2 //
      3 // Summary:
      4 // Assume we have 6 http requests in queue, 3 are from the focused window with
      5 // normal priority and the other 3 are from the non-focused window with the
      6 // highest priority.
      7 // We want to test that when "network.http.active_tab_priority" is false,
      8 // the server should receive 3 requests with the highest priority first
      9 // and then receive the rest 3 requests.
     10 //
     11 // Test step:
     12 // 1. Create 6 dummy http requests. Server would not process responses until told
     13 //    all 6 requests.
     14 // 2. Once server receive 6 dummy requests, create 3 http requests with the focused
     15 //    window id and normal priority and 3 requests with non-focused window id and
     16 //    the highrst priority.
     17 //    Note that the requets's id is set to its window id.
     18 // 3. Server starts to process the 6 dummy http requests, so the client can start to
     19 //    process the pending queue. Server will queue those http requests again and wait
     20 //    until get all 6 requests.
     21 // 4. When the server receive all 6 requests, we want to check if 3 requests with higher
     22 //    priority are sent before others.
     23 //    First, we check that if the request id of the first 3 requests in the queue is
     24 //    equal to non focused window id.
     25 //    Second, we check if the request id of the rest requests is equal to focused
     26 //    window id.
     27 
     28 "use strict";
     29 
     30 const { HttpServer } = ChromeUtils.importESModule(
     31  "resource://testing-common/httpd.sys.mjs"
     32 );
     33 
     34 var server = new HttpServer();
     35 server.start(-1);
     36 var baseURL = "http://localhost:" + server.identity.primaryPort + "/";
     37 var maxConnections = 0;
     38 var debug = false;
     39 const FOCUSED_WINDOW_ID = 123;
     40 var NON_FOCUSED_WINDOW_ID;
     41 var FOCUSED_WINDOW_REQUEST_COUNT;
     42 var NON_FOCUSED_WINDOW_REQUEST_COUNT;
     43 
     44 function log(msg) {
     45  if (!debug) {
     46    return;
     47  }
     48 
     49  if (msg) {
     50    dump("TEST INFO | " + msg + "\n");
     51  }
     52 }
     53 
     54 function make_channel(url) {
     55  var request = NetUtil.newChannel({
     56    uri: url,
     57    loadUsingSystemPrincipal: true,
     58  });
     59  request.QueryInterface(Ci.nsIHttpChannel);
     60  return request;
     61 }
     62 
     63 function serverStopListener() {
     64  server.stop();
     65 }
     66 
     67 function createHttpRequest(browserId, requestId, priority) {
     68  let uri = baseURL;
     69  var chan = make_channel(uri);
     70  chan.browserId = browserId;
     71  chan.QueryInterface(Ci.nsISupportsPriority).priority = priority;
     72  var listner = new HttpResponseListener(requestId);
     73  chan.setRequestHeader("X-ID", requestId, false);
     74  chan.setRequestHeader("Cache-control", "no-store", false);
     75  chan.asyncOpen(listner);
     76  log("Create http request id=" + requestId);
     77 }
     78 
     79 function setup_dummyHttpRequests() {
     80  log("setup_dummyHttpRequests");
     81  for (var i = 0; i < maxConnections; i++) {
     82    createHttpRequest(0, i, Ci.nsISupportsPriority.PRIORITY_NORMAL);
     83    do_test_pending();
     84  }
     85 }
     86 
     87 function setup_focusedWindowHttpRequests() {
     88  log("setup_focusedWindowHttpRequests");
     89  for (var i = 0; i < FOCUSED_WINDOW_REQUEST_COUNT; i++) {
     90    createHttpRequest(
     91      FOCUSED_WINDOW_ID,
     92      FOCUSED_WINDOW_ID,
     93      Ci.nsISupportsPriority.PRIORITY_NORMAL
     94    );
     95    do_test_pending();
     96  }
     97 }
     98 
     99 function setup_nonFocusedWindowHttpRequests() {
    100  log("setup_nonFocusedWindowHttpRequests");
    101  for (var i = 0; i < NON_FOCUSED_WINDOW_REQUEST_COUNT; i++) {
    102    createHttpRequest(
    103      NON_FOCUSED_WINDOW_ID,
    104      NON_FOCUSED_WINDOW_ID,
    105      Ci.nsISupportsPriority.PRIORITY_HIGHEST
    106    );
    107    do_test_pending();
    108  }
    109 }
    110 
    111 function HttpResponseListener(id) {
    112  this.id = id;
    113 }
    114 
    115 HttpResponseListener.prototype = {
    116  onStartRequest() {},
    117 
    118  onDataAvailable() {},
    119 
    120  onStopRequest() {
    121    log("STOP id=" + this.id);
    122    do_test_finished();
    123  },
    124 };
    125 
    126 function check_response_id(responses, browserId) {
    127  for (var i = 0; i < responses.length; i++) {
    128    var id = responses[i].getHeader("X-ID");
    129    log("response id=" + id + " browserId=" + browserId);
    130    Assert.equal(id, browserId);
    131  }
    132 }
    133 
    134 var responseQueue = [];
    135 function setup_http_server() {
    136  log("setup_http_server");
    137  maxConnections = Services.prefs.getIntPref(
    138    "network.http.max-persistent-connections-per-server"
    139  );
    140  FOCUSED_WINDOW_REQUEST_COUNT = Math.floor(maxConnections * 0.5);
    141  NON_FOCUSED_WINDOW_REQUEST_COUNT =
    142    maxConnections - FOCUSED_WINDOW_REQUEST_COUNT;
    143  NON_FOCUSED_WINDOW_ID = FOCUSED_WINDOW_ID + FOCUSED_WINDOW_REQUEST_COUNT;
    144 
    145  var allDummyHttpRequestReceived = false;
    146  // Start server; will be stopped at test cleanup time.
    147  server.registerPathHandler("/", function (metadata, response) {
    148    var id = metadata.getHeader("X-ID");
    149    log("Server recived the response id=" + id);
    150 
    151    response.processAsync();
    152    response.setHeader("X-ID", id);
    153    responseQueue.push(response);
    154 
    155    if (
    156      responseQueue.length == maxConnections &&
    157      !allDummyHttpRequestReceived
    158    ) {
    159      log("received all dummy http requets");
    160      allDummyHttpRequestReceived = true;
    161      setup_nonFocusedWindowHttpRequests();
    162      setup_focusedWindowHttpRequests();
    163      processResponses();
    164    } else if (responseQueue.length == maxConnections) {
    165      var nonFocusedWindowResponses = responseQueue.slice(
    166        0,
    167        NON_FOCUSED_WINDOW_REQUEST_COUNT
    168      );
    169      var focusedWindowResponses = responseQueue.slice(
    170        NON_FOCUSED_WINDOW_REQUEST_COUNT,
    171        responseQueue.length
    172      );
    173      check_response_id(nonFocusedWindowResponses, NON_FOCUSED_WINDOW_ID);
    174      check_response_id(focusedWindowResponses, FOCUSED_WINDOW_ID);
    175      processResponses();
    176    }
    177  });
    178 
    179  registerCleanupFunction(function () {
    180    server.stop(serverStopListener);
    181  });
    182 }
    183 
    184 function processResponses() {
    185  while (responseQueue.length) {
    186    var resposne = responseQueue.pop();
    187    resposne.finish();
    188  }
    189 }
    190 
    191 function run_test() {
    192  // Set "network.http.active_tab_priority" to false, so we can expect to
    193  // receive http requests with higher priority first.
    194  Services.prefs.setBoolPref("network.http.active_tab_priority", false);
    195 
    196  setup_http_server();
    197  setup_dummyHttpRequests();
    198 }