summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/storage/opaque-origin.https.window.js
blob: cc1d31fdf2c68322f72a40b1e84ec05ab1930779 (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
// META: title=StorageManager API and opaque origins

function load_iframe(src, sandbox) {
  return new Promise(resolve => {
    const iframe = document.createElement('iframe');
    iframe.onload = () => { resolve(iframe); };
    if (sandbox)
      iframe.sandbox = sandbox;
    iframe.srcdoc = src;
    iframe.style.display = 'none';
    document.documentElement.appendChild(iframe);
  });
}

function wait_for_message(iframe) {
  return new Promise(resolve => {
    self.addEventListener('message', function listener(e) {
      if (e.source === iframe.contentWindow && "result" in e.data) {
        resolve(e.data);
        self.removeEventListener('message', listener);
      }
    });
  });
}

function make_script(snippet) {
  return '<script src="/resources/testharness.js"></script>' +
         '<script>' +
         '  window.onmessage = () => {' +
         '    try {' +
         '      (' + snippet + ')' +
         '        .then(' +
         '          result => {' +
         '            window.parent.postMessage({result: "no rejection"}, "*");' +
         '          }, ' +
         '          error => {' +
         '            try {' +
         '              assert_throws_js(TypeError, () => { throw error; });' +
         '              window.parent.postMessage({result: "correct rejection"}, "*");' +
         '            } catch (e) {' +
         '              window.parent.postMessage({result: "incorrect rejection"}, "*");' +
         '            }' +
         '          });' +
         '    } catch (ex) {' +
         // Report if not implemented/exposed, rather than time out.
         '      window.parent.postMessage({result: "API access threw"}, "*");' +
         '    }' +
         '  };' +
         '<\/script>';
}

['navigator.storage.persisted()',
 'navigator.storage.estimate()',
 // persist() can prompt, so make sure we test that last
 'navigator.storage.persist()',
].forEach(snippet => {
  promise_test(t => {
    return load_iframe(make_script(snippet))
      .then(iframe => {
        iframe.contentWindow.postMessage({}, '*');
        return wait_for_message(iframe);
      })
      .then(message => {
        assert_equals(message.result, 'no rejection',
                      `${snippet} should not reject`);
      });
  }, `${snippet} in non-sandboxed iframe should not reject`);

  promise_test(t => {
    return load_iframe(make_script(snippet), 'allow-scripts')
      .then(iframe => {
        iframe.contentWindow.postMessage({}, '*');
        return wait_for_message(iframe);
      })
      .then(message => {
        assert_equals(message.result, 'correct rejection',
                      `${snippet} should reject with TypeError`);
      });
  }, `${snippet} in sandboxed iframe should reject with TypeError`);
});