tor-browser

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

file_bug1008126_worker.js (3792B)


      1 /**
      2 * Any copyright is dedicated to the Public Domain.
      3 * http://creativecommons.org/publicdomain/zero/1.0/
      4 */
      5 
      6 /* eslint-disable mozilla/no-comparison-or-assignment-inside-ok */
      7 
      8 var gEntry1 = "data_1.txt";
      9 var gEntry2 = "data_2.txt";
     10 var gEntry3 = "data_big.txt";
     11 var gPaddingChar = ".";
     12 var gPaddingSize = 10000;
     13 var gPadding = "";
     14 for (var i = 0; i < gPaddingSize; i++) {
     15  gPadding += gPaddingChar;
     16 }
     17 var gData1 = "TEST_DATA_1:ABCDEFGHIJKLMNOPQRSTUVWXYZ" + gPadding;
     18 var gData2 = "TEST_DATA_2:1234567890" + gPadding;
     19 
     20 function ok(a, msg) {
     21  postMessage({ type: "status", status: !!a, msg });
     22 }
     23 
     24 function is(a, b, msg) {
     25  postMessage({ type: "status", status: a === b, msg });
     26 }
     27 
     28 function checkData(xhr, data, mapped, cb) {
     29  var ct = xhr.getResponseHeader("Content-Type");
     30  if (mapped) {
     31    ok(ct.includes("mem-mapped"), "Data is memory-mapped");
     32  } else {
     33    ok(!ct.includes("mem-mapped"), "Data is not memory-mapped");
     34  }
     35  ok(xhr.response, "Data is non-null");
     36  var str = String.fromCharCode.apply(null, new Uint8Array(xhr.response));
     37  ok(str == data, "Data is correct");
     38  cb();
     39 }
     40 
     41 self.onmessage = function onmessage(event) {
     42  var jar = event.data;
     43 
     44  function makeJarURL(entry) {
     45    return "jar:" + jar + "!/" + entry;
     46  }
     47 
     48  var xhr = new XMLHttpRequest({ mozAnon: true, mozSystem: true });
     49 
     50  function reset_event_hander() {
     51    xhr.onerror = function (e) {
     52      ok(false, "Error: " + e.error + "\n");
     53    };
     54    xhr.onprogress = null;
     55    xhr.onreadystatechange = null;
     56    xhr.onload = null;
     57    xhr.onloadend = null;
     58  }
     59 
     60  var readystatechangeCount = 0;
     61  var loadCount = 0;
     62  var loadendCount = 0;
     63 
     64  function checkEventCount(cb) {
     65    ok(
     66      readystatechangeCount == 1 && loadCount == 1 && loadendCount == 1,
     67      "Saw all expected events"
     68    );
     69    cb();
     70  }
     71 
     72  function test_multiple_events() {
     73    ok(true, "Test multiple events");
     74    xhr.abort();
     75 
     76    xhr.onreadystatechange = function () {
     77      if (xhr.readyState == xhr.DONE) {
     78        readystatechangeCount++;
     79        checkData(xhr, gData2, false, function () {});
     80      }
     81    };
     82    xhr.onload = function () {
     83      loadCount++;
     84      checkData(xhr, gData2, false, function () {});
     85    };
     86    xhr.onloadend = function () {
     87      loadendCount++;
     88      checkData(xhr, gData2, false, function () {});
     89    };
     90    xhr.open("GET", makeJarURL(gEntry2), false);
     91    xhr.responseType = "arraybuffer";
     92    xhr.send();
     93    checkEventCount(runTests);
     94  }
     95 
     96  function test_sync_xhr_data1() {
     97    ok(true, "Test sync XHR with data1");
     98    xhr.open("GET", makeJarURL(gEntry1), false);
     99    xhr.responseType = "arraybuffer";
    100    xhr.send();
    101    checkData(xhr, gData1, true, runTests);
    102  }
    103 
    104  function test_sync_xhr_data2() {
    105    ok(true, "Test sync XHR with data2");
    106    xhr.open("GET", makeJarURL(gEntry2), false);
    107    xhr.responseType = "arraybuffer";
    108    xhr.send();
    109    checkData(xhr, gData2, false, runTests);
    110  }
    111 
    112  function test_async_xhr_data1() {
    113    ok(true, "Test async XHR with data1");
    114    xhr.onload = function () {
    115      checkData(xhr, gData1, true, runTests);
    116    };
    117    xhr.open("GET", makeJarURL(gEntry1), true);
    118    xhr.responseType = "arraybuffer";
    119    xhr.send();
    120  }
    121 
    122  function test_async_xhr_data2() {
    123    ok(true, "Test async XHR with data2");
    124    xhr.onload = function () {
    125      checkData(xhr, gData2, false, runTests);
    126    };
    127    xhr.open("GET", makeJarURL(gEntry2), true);
    128    xhr.responseType = "arraybuffer";
    129    xhr.send();
    130  }
    131 
    132  var tests = [
    133    test_multiple_events,
    134    test_sync_xhr_data1,
    135    test_sync_xhr_data2,
    136    test_async_xhr_data1,
    137    test_async_xhr_data2,
    138  ];
    139 
    140  function runTests() {
    141    if (!tests.length) {
    142      postMessage({ type: "finish" });
    143      return;
    144    }
    145 
    146    reset_event_hander();
    147 
    148    var test = tests.shift();
    149    test();
    150  }
    151 
    152  runTests();
    153 };