tor-browser

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

file_bug945152_worker.js (2987B)


      1 /* eslint-disable mozilla/no-comparison-or-assignment-inside-ok */
      2 var gData1 = "TEST_DATA_1:ABCDEFGHIJKLMNOPQRSTUVWXYZ";
      3 var gData2 = "TEST_DATA_2:1234567890";
      4 var gPaddingChar = ".";
      5 var gPaddingSize = 10000;
      6 var gPadding = "";
      7 
      8 for (var i = 0; i < gPaddingSize; i++) {
      9  gPadding += gPaddingChar;
     10 }
     11 
     12 function ok(a, msg) {
     13  postMessage({ type: "status", status: !!a, msg });
     14 }
     15 
     16 function is(a, b, msg) {
     17  postMessage({ type: "status", status: a === b, msg });
     18 }
     19 
     20 function checkData(response, data_head, cb) {
     21  ok(response, "Data is non-null");
     22  var str = String.fromCharCode.apply(null, new Uint8Array(response));
     23  ok(str.length == data_head.length + gPaddingSize, "Data size is correct");
     24  ok(str.slice(0, data_head.length) == data_head, "Data head is correct");
     25  ok(str.slice(data_head.length) == gPadding, "Data padding is correct");
     26  cb();
     27 }
     28 
     29 self.onmessage = function onmessage(event) {
     30  var jar = event.data;
     31 
     32  function makeJarURL(entry) {
     33    return "jar:" + jar + "!/" + entry;
     34  }
     35 
     36  function test_mapped_sync() {
     37    var xhr = new XMLHttpRequest({ mozAnon: true, mozSystem: true });
     38    xhr.open("GET", makeJarURL("data_1.txt"), false);
     39    xhr.responseType = "arraybuffer";
     40    xhr.send();
     41    if (xhr.status) {
     42      ok(xhr.status == 200, "Status is 200");
     43      var ct = xhr.getResponseHeader("Content-Type");
     44      ok(ct.includes("mem-mapped"), "Data is memory-mapped");
     45      checkData(xhr.response, gData1, runTests);
     46    }
     47  }
     48 
     49  function test_mapped_async() {
     50    var xhr = new XMLHttpRequest({ mozAnon: true, mozSystem: true });
     51    xhr.open("GET", makeJarURL("data_1.txt"));
     52    xhr.responseType = "arraybuffer";
     53    xhr.onreadystatechange = function () {
     54      if (xhr.readyState !== xhr.DONE) {
     55        return;
     56      }
     57      if (xhr.status) {
     58        ok(xhr.status == 200, "Status is 200");
     59        var ct = xhr.getResponseHeader("Content-Type");
     60        ok(ct.includes("mem-mapped"), "Data is memory-mapped");
     61        checkData(xhr.response, gData1, runTests);
     62      }
     63    };
     64    xhr.send();
     65  }
     66 
     67  // Make sure array buffer retrieved from compressed file in package is
     68  // handled by memory allocation instead of memory mapping.
     69  function test_non_mapped() {
     70    var xhr = new XMLHttpRequest({ mozAnon: true, mozSystem: true });
     71    xhr.open("GET", makeJarURL("data_2.txt"));
     72    xhr.responseType = "arraybuffer";
     73    xhr.onreadystatechange = function () {
     74      if (xhr.readyState !== xhr.DONE) {
     75        return;
     76      }
     77      if (xhr.status) {
     78        ok(xhr.status == 200, "Status is 200");
     79        var ct = xhr.getResponseHeader("Content-Type");
     80        ok(!ct.includes("mem-mapped"), "Data is not memory-mapped");
     81        checkData(xhr.response, gData2, runTests);
     82      }
     83    };
     84    xhr.send();
     85  }
     86 
     87  var tests = [test_mapped_sync, test_mapped_async, test_non_mapped];
     88 
     89  function runTests() {
     90    if (!tests.length) {
     91      postMessage({ type: "finish" });
     92      return;
     93    }
     94 
     95    var test = tests.shift();
     96    test();
     97  }
     98 
     99  runTests();
    100 };