summaryrefslogtreecommitdiffstats
path: root/dom/tests/mochitest/general/workerStoragePrevented.js
blob: 7d232d65df132923a4e14605d64ac42e5e631cc2 (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// 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 access indexedDB handle
try {
  indexedDB;
  ok(true, "WORKER getting indexedDB didn't throw");
} catch (e) {
  ok(false, "WORKER getting indexedDB threw");
}

// Make sure that we cannot access indexedDB methods
idbOpenTest();

// Make sure that we can't access indexedDB deleteDatabase
function idbDeleteTest() {
  try {
    indexedDB.deleteDatabase("door");
    ok(false, "WORKER deleting indexedDB database succeeded");
  } catch (e) {
    ok(true, "WORKER deleting indexedDB database failed");
    ok(
      e.name == "SecurityError",
      "WORKER deleting indexedDB database threw a security error"
    );
  } finally {
    cacheTest();
  }
}

// Make sure that we can't access indexedDB databases
function idbDatabasesTest() {
  indexedDB
    .databases()
    .then(() => {
      ok(false, "WORKER querying indexedDB databases succeeded");
    })
    .catch(e => {
      ok(true, "WORKER querying indexedDB databases failed");
      ok(
        e.name == "SecurityError",
        "WORKER querying indexedDB databases threw a security error"
      );
    })
    .finally(idbDeleteTest);
}

// Make sure that we can't access indexedDB open
function idbOpenTest() {
  try {
    indexedDB.open("door");
    ok(false, "WORKER opening indexedDB database succeeded");
  } catch (e) {
    ok(true, "WORKER opening indexedDB database failed");
    ok(
      e.name == "SecurityError",
      "WORKER opening indexedDB database threw a security error"
    );
  } finally {
    idbDatabasesTest();
  }
}

// Make sure that we can't access caches
function cacheTest() {
  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 != "#outer") {
    // 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) {
    const isFail = e.data.match(/^FAILURE/);
    ok(!isFail, e.data + " (WORKER = workerStoragePrevented.js#inner)");

    if (e.data == "done" || isFail) {
      finishTest();
    }
  });
  worker.addEventListener("error", function (e) {
    ok(false, e.data + " (WORKER = workerStoragePrevented.js#inner)");

    finishTest();
  });
}