summaryrefslogtreecommitdiffstats
path: root/devtools/server/tests/browser/storage-secured-iframe.html
blob: c2fe4ed485ecb19bf5869f61287472c76605e107 (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
<!DOCTYPE HTML>
<html>
<!--
Iframe for testing multiple host detetion in storage actor
-->
<head>
  <meta charset="utf-8">
</head>
<body>
<script type="application/javascript">
"use strict";
document.cookie = "sc1=foobar;";
localStorage.setItem("iframe-s-ls1", "foobar");
sessionStorage.setItem("iframe-s-ss1", "foobar-2");

const idbGenerator = async function () {
  let request = indexedDB.open("idb-s1", 1);
  request.onerror = function() {
    throw new Error("error opening db connection");
  };
  const db = await new Promise(done => {
    request.onupgradeneeded = event => {
      const dbResult = event.target.result;
      const store1 = dbResult.createObjectStore("obj-s1", { keyPath: "id" });
      store1.transaction.oncomplete = () => {
        done(dbResult);
      };
    };
  });
  await new Promise(done => {
    request.onsuccess = done;
  });

  let transaction = db.transaction(["obj-s1"], "readwrite");
  const store1 = transaction.objectStore("obj-s1");
  store1.add({id: 6, name: "foo", email: "foo@bar.com"});
  store1.add({id: 7, name: "foo2", email: "foo2@bar.com"});
  await new Promise(success => {
    transaction.oncomplete = success;
  });

  db.close();

  request = indexedDB.open("idb-s2", 1);
  const db2 = await new Promise(done => {
    request.onupgradeneeded = event => {
      const db2Result = event.target.result;
      const store3 =
        db2Result.createObjectStore("obj-s2", { keyPath: "id3", autoIncrement: true });
      store3.createIndex("name2", "name2", { unique: true });
      store3.transaction.oncomplete = () => {
        done(db2Result);
      };
    };
  });
  await new Promise(done => {
    request.onsuccess = done;
  });

  transaction = db2.transaction(["obj-s2"], "readwrite");
  const store3 = transaction.objectStore("obj-s2");
  store3.add({id3: 16, name2: "foo", email: "foo@bar.com"});
  await new Promise(success => {
    transaction.oncomplete = success;
  });

  db2.close();
  dump("added cookies and stuff from secured iframe\n");
}

function deleteDB(dbName) {
  return new Promise(resolve => {
    dump("removing database " + dbName + " from " + document.location + "\n");
    indexedDB.deleteDatabase(dbName).onsuccess = resolve;
  });
}

window.setup = async function () {
  await idbGenerator();
};

window.clear = async function () {
  document.cookie = "sc1=; expires=Thu, 01 Jan 1970 00:00:00 GMT";

  localStorage.clear();

  await deleteDB("idb-s1");
  await deleteDB("idb-s2");

  console.log("removed cookies and stuff from secured iframe");
}
</script>
</body>
</html>