summaryrefslogtreecommitdiffstats
path: root/dom/tests/mochitest/general/workerStoragePrevented.js
blob: 467cc091136a1f574966cc6b97ea5f76f1ddc95b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Unfortunately, workers can't share the code from storagePermissionsUtils.
// These are basic mechanisms for communicating to the test runner.

function ok(condition, text) {
  if (!condition) {
    self.postMessage("FAILURE: " + text);
  } else {
    self.postMessage(text);
  }
}

function finishTest() {
  self.postMessage("done");
  self.close();
}

// Workers don't have access to localstorage or sessionstorage
ok(typeof self.localStorage == "undefined", "localStorage should be undefined");
ok(
  typeof self.sessionStorage == "undefined",
  "sessionStorage should be undefined"
);

// Make sure that we can't access indexedDB
try {
  indexedDB;
  ok(false, "WORKER getting indexedDB should have thrown");
} catch (e) {
  ok(true, "WORKER getting indexedDB threw");
}

// Make sure that we can't access caches
try {
  var promise = caches.keys();
  ok(true, "WORKER getting caches didn't throw");

  promise.then(
    function () {
      ok(false, "WORKER The promise should have rejected");
      workerTest();
    },
    function () {
      ok(true, "WORKER The promise was rejected");
      workerTest();
    }
  );
} catch (e) {
  ok(
    location.protocol !== "https:",
    "WORKER getting caches should not have thrown"
  );
  workerTest();
}

// Try to spawn an inner worker, and make sure that it also can't access storage
function workerTest() {
  if (location.hash == "#inner") {
    // Don't recurse infinitely, if we are the inner worker, don't spawn another
    finishTest();
    return;
  }
  // Create the inner worker, and listen for test messages from it
  var worker = new Worker("workerStoragePrevented.js#inner");
  worker.addEventListener("message", function (e) {
    if (e.data == "done") {
      finishTest();
      return;
    }

    ok(
      !e.data.match(/^FAILURE/),
      e.data + " (WORKER = workerStoragePrevented.js#inner)"
    );
  });
}