summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/shared-storage/resources/util.js
blob: 838885a56570fca26e08e168b8a078928e131b62 (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
'use strict';

async function IsSharedStorageSelectUrlAllowedByPermissionsPolicy() {
  const errorMessage = 'The \"shared-storage-select-url\" Permissions Policy denied the usage of window.sharedStorage.selectURL().';
  let allowedByPermissionsPolicy = true;
  try {
    // Run selectURL() with without addModule() and this should always fail.
    // Check the error message to distinguish between the permissions policy
    // error and the missing addModule() error.
    await sharedStorage.selectURL("operation", [{url: "1.html"}]);
    assert_unreached("did not fail");
  } catch (e) {
    if (e.message === errorMessage) {
      allowedByPermissionsPolicy = false;
    }
  }

  return allowedByPermissionsPolicy;
}

// Execute all shared storage methods and capture their errors. Return true if
// the permissions policy allows all of them; return false if the permissions
// policy disallows all of them. Precondition: only these two outcomes are
// possible.
async function AreSharedStorageMethodsAllowedByPermissionsPolicy() {
  let permissionsPolicyDeniedCount = 0;
  const errorMessage = 'The \"shared-storage\" Permissions Policy denied the method on window.sharedStorage.';

  try {
    await window.sharedStorage.worklet.addModule('/shared-storage/resources/simple-module.js');
  } catch (e) {
    assert_equals(e.message, errorMessage);
    ++permissionsPolicyDeniedCount;
  }

  try {
    await window.sharedStorage.run('operation');
  } catch (e) {
    assert_equals(e.message, errorMessage);
    ++permissionsPolicyDeniedCount;
  }

  try {
    // Run selectURL() with without addModule() and this should always fail.
    // Check the error message to distinguish between the permissions policy
    // error and the missing addModule() error.
    await sharedStorage.selectURL("operation", [{url: "1.html"}]);
    assert_unreached("did not fail");
  } catch (e) {
    if (e.message === errorMessage) {
      ++permissionsPolicyDeniedCount;
    }
  }

  try {
    await window.sharedStorage.set('a', 'b');
  } catch (e) {
    assert_equals(e.message, errorMessage);
    ++permissionsPolicyDeniedCount;
  }

  try {
    await window.sharedStorage.append('a', 'b');
  } catch (e) {
    assert_equals(e.message, errorMessage);
    ++permissionsPolicyDeniedCount;
  }

  try {
    await window.sharedStorage.clear();
  } catch (e) {
    assert_equals(e.message, errorMessage);
    ++permissionsPolicyDeniedCount;
  }

  try {
    await window.sharedStorage.delete('a');
  } catch (e) {
    assert_equals(e.message, errorMessage);
    ++permissionsPolicyDeniedCount;
  }

  if (permissionsPolicyDeniedCount === 0)
    return true;

  return false;
}