tor-browser

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

worker_helper.js (2266B)


      1 /**
      2 * Any copyright is dedicated to the Public Domain.
      3 * http://creativecommons.org/publicdomain/zero/1.0/
      4 */
      5 
      6 var _tests = [];
      7 function addTest(test) {
      8  _tests.push(test);
      9 }
     10 
     11 function addAsyncTest(fn) {
     12  _tests.push(() => fn().catch(ok.bind(null, false)));
     13 }
     14 
     15 function runNextTest() {
     16  if (!_tests.length) {
     17    SimpleTest.finish();
     18    return;
     19  }
     20  const fn = _tests.shift();
     21  try {
     22    fn();
     23  } catch (ex) {
     24    info(
     25      "Test function " +
     26        (fn.name ? "'" + fn.name + "' " : "") +
     27        "threw an exception: " +
     28        ex
     29    );
     30  }
     31 }
     32 
     33 /**
     34 * Helper to perform an XHR then blob response to create worker
     35 */
     36 function doXHRGetBlob(uri) {
     37  return new Promise(resolve => {
     38    const xhr = new XMLHttpRequest();
     39    xhr.open("GET", uri);
     40    xhr.responseType = "blob";
     41    xhr.addEventListener("load", function () {
     42      is(
     43        xhr.status,
     44        200,
     45        "doXHRGetBlob load uri='" + uri + "' status=" + xhr.status
     46      );
     47      resolve(xhr.response);
     48    });
     49    xhr.send();
     50  });
     51 }
     52 
     53 function removeObserver(observer) {
     54  SpecialPowers.removeObserver(observer, "specialpowers-http-notify-request");
     55  SpecialPowers.removeObserver(observer, "csp-on-violate-policy");
     56 }
     57 
     58 /**
     59 * Helper to perform an assert to check if the request should be blocked or
     60 * allowed by CSP
     61 */
     62 function assertCSPBlock(url, shouldBlock) {
     63  return new Promise((resolve, reject) => {
     64    let observer = {
     65      observe(subject, topic, data) {
     66        if (topic === "specialpowers-http-notify-request") {
     67          if (data == url) {
     68            is(shouldBlock, false, "Should allow request uri='" + url);
     69            removeObserver(observer);
     70            resolve();
     71          }
     72        }
     73 
     74        if (topic === "csp-on-violate-policy") {
     75          let asciiSpec = SpecialPowers.getPrivilegedProps(
     76            SpecialPowers.do_QueryInterface(subject, "nsIURI"),
     77            "asciiSpec"
     78          );
     79          if (asciiSpec == url) {
     80            is(shouldBlock, true, "Should block request uri='" + url);
     81            removeObserver(observer);
     82            resolve();
     83          }
     84        }
     85      },
     86    };
     87 
     88    SpecialPowers.addObserver(observer, "csp-on-violate-policy");
     89    SpecialPowers.addObserver(observer, "specialpowers-http-notify-request");
     90  });
     91 }