tor-browser

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

test_cache2-29e-concurrent_read_half-non-206-response.js (4138B)


      1 /*
      2 
      3 Checkes if the concurrent cache read/write works when the write is interrupted because of max-entry-size limits.
      4 This is enhancement of 29c test, this test checks that a corrupted 206 response is correctly handled (no crashes or asserion failures)
      5 This test is using a resumable response.
      6 - with a profile, set max-entry-size to 1 (=1024 bytes)
      7 - first channel makes a request for a resumable response
      8 - second channel makes a request for the same resource, concurrent read happens
      9 - first channel sets predicted data size on the entry with every chunk, it's doomed on 1024
     10 - second channel now must engage interrupted concurrent write algorithm and read the rest of the content from the network
     11 - the response to the range request is plain 200
     12 - the first must deliver full content w/o errors
     13 - the second channel must correctly fail
     14 
     15 */
     16 "use strict";
     17 
     18 const { HttpServer } = ChromeUtils.importESModule(
     19  "resource://testing-common/httpd.sys.mjs"
     20 );
     21 
     22 var httpProtocolHandler = Cc[
     23  "@mozilla.org/network/protocol;1?name=http"
     24 ].getService(Ci.nsIHttpProtocolHandler);
     25 
     26 ChromeUtils.defineLazyGetter(this, "URL", function () {
     27  return "http://localhost:" + httpServer.identity.primaryPort;
     28 });
     29 
     30 var httpServer = null;
     31 
     32 function make_channel(url) {
     33  return NetUtil.newChannel({ uri: url, loadUsingSystemPrincipal: true });
     34 }
     35 
     36 // need something bigger than 1024 bytes
     37 const responseBody =
     38  "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" +
     39  "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" +
     40  "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" +
     41  "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" +
     42  "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" +
     43  "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" +
     44  "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" +
     45  "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" +
     46  "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" +
     47  "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" +
     48  "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef";
     49 
     50 function contentHandler(metadata, response) {
     51  response.setHeader("Content-Type", "text/plain");
     52  response.setHeader("ETag", "Just testing");
     53  response.setHeader("Cache-Control", "max-age=99999");
     54  response.setHeader("Accept-Ranges", "bytes");
     55  response.setHeader("Content-Length", "" + responseBody.length);
     56  response.bodyOutputStream.write(responseBody, responseBody.length);
     57 }
     58 
     59 function run_test() {
     60  // Static check
     61  Assert.greater(responseBody.length, 1024);
     62 
     63  do_get_profile();
     64 
     65  Services.prefs.setIntPref("browser.cache.disk.max_entry_size", 1);
     66  Services.prefs.setBoolPref("network.http.rcwn.enabled", false);
     67 
     68  httpServer = new HttpServer();
     69  httpServer.registerPathHandler("/content", contentHandler);
     70  httpServer.start(-1);
     71 
     72  httpProtocolHandler.EnsureHSTSDataReady().then(function () {
     73    var chan1 = make_channel(URL + "/content");
     74    chan1.asyncOpen(new ChannelListener(firstTimeThrough, null));
     75    var chan2 = make_channel(URL + "/content");
     76    chan2.asyncOpen(
     77      new ChannelListener(secondTimeThrough, null, CL_EXPECT_FAILURE)
     78    );
     79  });
     80 
     81  do_test_pending();
     82 }
     83 
     84 function firstTimeThrough(request, buffer) {
     85  Assert.equal(buffer, responseBody);
     86 }
     87 
     88 function secondTimeThrough() {
     89  httpServer.stop(do_test_finished);
     90 }