tor-browser

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

test_bug1527293.js (3965B)


      1 // Test bug 1527293
      2 //
      3 // Summary:
      4 // The purpose of this test is to check that a cache entry is doomed and not
      5 // reused when we don't write the content due to max entry size limit.
      6 //
      7 // Test step:
      8 // 1. Create http request for an entry whose size is bigger than we allow to
      9 //    cache. The response must contain Content-Range header so the content size
     10 //    is known in advance, but it must not contain Content-Length header because
     11 //    the bug isn't reproducible with it.
     12 // 2. After receiving and checking the content do the same request again.
     13 // 3. Check that the request isn't conditional, i.e. the entry from previous
     14 //    load was doomed.
     15 
     16 "use strict";
     17 
     18 const { HttpServer } = ChromeUtils.importESModule(
     19  "resource://testing-common/httpd.sys.mjs"
     20 );
     21 
     22 ChromeUtils.defineLazyGetter(this, "URL", function () {
     23  return "http://localhost:" + httpServer.identity.primaryPort;
     24 });
     25 
     26 var httpServer = null;
     27 
     28 function make_channel(url) {
     29  return NetUtil.newChannel({ uri: url, loadUsingSystemPrincipal: true });
     30 }
     31 
     32 // need something bigger than 1024 bytes
     33 const responseBody =
     34  "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" +
     35  "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" +
     36  "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" +
     37  "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" +
     38  "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" +
     39  "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" +
     40  "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" +
     41  "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" +
     42  "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" +
     43  "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" +
     44  "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef";
     45 
     46 function contentHandler(metadata, response) {
     47  response.setHeader("Content-Type", "text/plain");
     48  response.setHeader("ETag", "Just testing");
     49  response.setHeader("Cache-Control", "max-age=99999");
     50 
     51  Assert.throws(
     52    () => {
     53      metadata.getHeader("If-None-Match");
     54    },
     55    /NS_ERROR_NOT_AVAILABLE/,
     56    "conditional request not expected"
     57  );
     58 
     59  response.setHeader("Accept-Ranges", "bytes");
     60  let len = responseBody.length;
     61  response.setHeader("Content-Range", "0-" + (len - 1) + "/" + len);
     62  response.bodyOutputStream.write(responseBody, responseBody.length);
     63 }
     64 
     65 function run_test() {
     66  // Static check
     67  Assert.greater(responseBody.length, 1024);
     68 
     69  do_get_profile();
     70 
     71  Services.prefs.setIntPref("browser.cache.disk.max_entry_size", 1);
     72  Services.prefs.setBoolPref("network.http.rcwn.enabled", false);
     73 
     74  httpServer = new HttpServer();
     75  httpServer.registerPathHandler("/content", contentHandler);
     76  httpServer.start(-1);
     77 
     78  var chan = make_channel(URL + "/content");
     79  chan.asyncOpen(new ChannelListener(firstTimeThrough, null));
     80 
     81  do_test_pending();
     82 }
     83 
     84 function firstTimeThrough(request, buffer) {
     85  Assert.equal(buffer, responseBody);
     86 
     87  var chan = make_channel(URL + "/content");
     88  chan.asyncOpen(new ChannelListener(secondTimeThrough, null));
     89 }
     90 
     91 function secondTimeThrough(request, buffer) {
     92  Assert.equal(buffer, responseBody);
     93  httpServer.stop(do_test_finished);
     94 }