summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/test/xpcshell/test_ext_indexedDB_principal.js
blob: f355b1d43a99ed1ad5a5f314568d12622af45362 (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
"use strict";

AddonTestUtils.init(this);
AddonTestUtils.createAppInfo(
  "xpcshell@tests.mozilla.org",
  "XPCShell",
  "1",
  "42"
);

add_task(async function test_indexedDB_principal() {
  Services.prefs.setBoolPref("privacy.firstparty.isolate", true);

  await AddonTestUtils.promiseStartupManager();

  let extension = ExtensionTestUtils.loadExtension({
    useAddonManager: "temporary",
    manifest: {},
    async background() {
      browser.test.onMessage.addListener(async msg => {
        if (msg == "create-storage") {
          let request = window.indexedDB.open("TestDatabase");
          request.onupgradeneeded = function (e) {
            let db = e.target.result;
            db.createObjectStore("TestStore");
          };
          request.onsuccess = function (e) {
            let db = e.target.result;
            let tx = db.transaction("TestStore", "readwrite");
            let store = tx.objectStore("TestStore");
            tx.oncomplete = () => browser.test.sendMessage("storage-created");
            store.add("foo", "bar");
            tx.onerror = function (e) {
              browser.test.fail(`Failed with error ${tx.error.message}`);
              // Don't wait for timeout
              browser.test.sendMessage("storage-created");
            };
          };
          request.onerror = function (e) {
            browser.test.fail(`Failed with error ${request.error.message}`);
            // Don't wait for timeout
            browser.test.sendMessage("storage-created");
          };
          return;
        }
        if (msg == "check-storage") {
          let dbRequest = window.indexedDB.open("TestDatabase");
          dbRequest.onupgradeneeded = function () {
            browser.test.fail("Database should exist");
            browser.test.notifyFail("done");
          };
          dbRequest.onsuccess = function (e) {
            let db = e.target.result;
            let transaction = db.transaction("TestStore");
            transaction.onerror = function (e) {
              browser.test.fail(
                `Failed with error ${transaction.error.message}`
              );
              browser.test.notifyFail("done");
            };
            let objectStore = transaction.objectStore("TestStore");
            let request = objectStore.get("bar");
            request.onsuccess = function (event) {
              browser.test.assertEq(
                request.result,
                "foo",
                "Got the expected data"
              );
              browser.test.notifyPass("done");
            };
            request.onerror = function (e) {
              browser.test.fail(`Failed with error ${request.error.message}`);
              browser.test.notifyFail("done");
            };
          };
          dbRequest.onerror = function (e) {
            browser.test.fail(`Failed with error ${dbRequest.error.message}`);
            browser.test.notifyFail("done");
          };
        }
      });
    },
  });

  await extension.startup();
  extension.sendMessage("create-storage");
  await extension.awaitMessage("storage-created");

  await extension.addon.disable();

  Services.prefs.setBoolPref("privacy.firstparty.isolate", false);

  await extension.addon.enable();
  await extension.awaitStartup();

  extension.sendMessage("check-storage");
  await extension.awaitFinish("done");

  await extension.unload();
  Services.prefs.clearUserPref("privacy.firstparty.isolate");
});

add_task(async function test_indexedDB_ext_privateBrowsing() {
  Services.prefs.setBoolPref("dom.indexedDB.privateBrowsing.enabled", true);

  let extension = ExtensionTestUtils.loadExtension({
    useAddonManager: "temporary",
    incognitoOverride: "spanning",
    files: {
      "extpage.html": `<!DOCTYPE><script src="extpage.js"></script>`,
      "extpage.js": async function () {
        try {
          const request = window.indexedDB.open("TestDatabasePrivateBrowsing");
          await new Promise((resolve, reject) => {
            request.onupgradeneeded = resolve;
            request.onsuccess = resolve;
            request.onerror = () => {
              reject(request.error);
            };
          });
          browser.test.notifyFail("indexedDB-expect-error-on-open");
        } catch (err) {
          browser.test.assertEq(
            "InvalidStateError",
            err.name,
            "Expect an error raised on openeing indexedDB"
          );
          browser.test.notifyPass("indexedDB-expect-error-on-open");
        }
      },
    },
  });

  await extension.startup();

  const page = await ExtensionTestUtils.loadContentPage(
    extension.extension.baseURI.resolve("extpage.html"),
    { privateBrowsing: true }
  );

  await extension.awaitFinish("indexedDB-expect-error-on-open");

  await page.close();
  await extension.unload();

  Services.prefs.clearUserPref("dom.indexedDB.privateBrowsing.enabled");
});