summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/generic-sensor/generic-sensor-feature-policy-test.sub.js
blob: 9bc46ae9364be41fdb4dc2cf47d8a231b04f6081 (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
const feature_policies = {
  "AmbientLightSensor" : ["ambient-light-sensor"],
  "Accelerometer" : ["accelerometer"],
  "LinearAccelerationSensor" : ["accelerometer"],
  "GravitySensor" : ["accelerometer"],
  "Gyroscope" : ["gyroscope"],
  "GeolocationSensor" : ["geolocation"],
  "Magnetometer" : ["magnetometer"],
  "UncalibratedMagnetometer" : ["magnetometer"],
  "AbsoluteOrientationSensor" : ["accelerometer", "gyroscope", "magnetometer"],
  "RelativeOrientationSensor" : ["accelerometer", "gyroscope"]
};

const same_origin_src =
  "/feature-policy/resources/feature-policy-generic-sensor.html#";
const cross_origin_src =
  "https://{{domains[www]}}:{{ports[https][0]}}" + same_origin_src;
const base_src = "/feature-policy/resources/redirect-on-load.html#";

function get_feature_policies_for_sensor(sensorType) {
  return feature_policies[sensorType];
}

function run_fp_tests_disabled(sensorName) {
  const sensorType = self[sensorName];
  const featureNameList = feature_policies[sensorName];
  const header = "Feature-Policy header " + featureNameList.join(" 'none';") + " 'none'";
  const desc = "'new " + sensorName + "()'";

  test(() => {
    assert_implements(sensorName in self, `${sensorName} is not supported.`);
    assert_throws_dom("SecurityError", () => {new sensorType()});
  }, `${sensorName}: ${header} disallows the top-level document.`);

  async_test(t => {
    assert_implements(sensorName in self, `${sensorName} is not supported.`);
    test_feature_availability(
      desc,
      t,
      same_origin_src + sensorName,
      expect_feature_unavailable_default
    );
  }, `${sensorName}: ${header} disallows same-origin iframes.`);

  async_test(t => {
    assert_implements(sensorName in self, `${sensorName} is not supported.`);
    test_feature_availability(
      desc,
      t,
      cross_origin_src + sensorName,
      expect_feature_unavailable_default
    );
  }, `${sensorName}: ${header} disallows cross-origin iframes.`);
}

function run_fp_tests_enabled(sensorName) {
  const featureNameList = feature_policies[sensorName];
  const header = "Feature-Policy header " + featureNameList.join(" *;") + " *";
  const desc = "'new " + sensorName + "()'";

  test(() => {
    assert_implements(sensorName in self, `${sensorName} is not supported.`);
  }, `${sensorName}: ${header} allows the top-level document.`);

  async_test(t => {
    assert_implements(sensorName in self, `${sensorName} is not supported.`);
    test_feature_availability(
      desc,
      t,
      same_origin_src + sensorName,
      expect_feature_available_default
    );
  }, `${sensorName}: ${header} allows same-origin iframes.`);

  // Set allow="feature;feature;..." on iframe element to delegate features
  // under test to cross origin subframe.
  async_test(t => {
    assert_implements(sensorName in self, `${sensorName} is not supported.`);
    test_feature_availability(
      desc,
      t,
      cross_origin_src + sensorName,
      expect_feature_available_default,
      feature_policies[sensorName].join(";")
    );
  }, `${sensorName}: ${header} allows cross-origin iframes.`);
}

function run_fp_tests_enabled_by_attribute(sensorName) {
  const featureNameList = feature_policies[sensorName];
  const header = "Feature-Policy allow='" + featureNameList.join(" ") + "' attribute";
  const desc = "'new " + sensorName + "()'";

  async_test(t => {
    assert_implements(sensorName in self, `${sensorName} is not supported.`);
    test_feature_availability(
      desc,
      t,
      same_origin_src + sensorName,
      expect_feature_available_default,
      featureNameList.join(";")
    );
  }, `${sensorName}: ${header} allows same-origin iframe`);

  async_test(t => {
    assert_implements(sensorName in self, `${sensorName} is not supported.`);
    test_feature_availability(
      desc,
      t,
      cross_origin_src + sensorName,
      expect_feature_available_default,
      featureNameList.join(";")
    );
  }, `${sensorName}: ${header} allows cross-origin iframe`);
}

function run_fp_tests_enabled_by_attribute_redirect_on_load(sensorName) {
  const featureNameList = feature_policies[sensorName];
  const header = "Feature-Policy allow='" + featureNameList.join(" ") + "' attribute";
  const desc = "'new " + sensorName + "()'";

  async_test(t => {
    assert_implements(sensorName in self, `${sensorName} is not supported.`);
    test_feature_availability(
      desc,
      t,
      base_src + same_origin_src + sensorName,
      expect_feature_available_default,
      featureNameList.join(";")
    );
  }, `${sensorName}: ${header} allows same-origin relocation`);

  async_test(t => {
    assert_implements(sensorName in self, `${sensorName} is not supported.`);
    test_feature_availability(
      desc,
      t,
      base_src + cross_origin_src + sensorName,
      expect_feature_unavailable_default,
      featureNameList.join(";")
    );
  }, `${sensorName}: ${header} disallows cross-origin relocation`);
}

function run_fp_tests_enabled_on_self_origin(sensorName) {
  const featureNameList = feature_policies[sensorName];
  const header = "Feature-Policy header " + featureNameList.join(" 'self';") + " 'self'";
  const desc = "'new " + sensorName + "()'";

  test(() => {
    assert_implements(sensorName in self, `${sensorName} is not supported.`);
  }, `${sensorName}: ${header} allows the top-level document.`);

  async_test(t => {
    assert_implements(sensorName in self, `${sensorName} is not supported.`);
    test_feature_availability(
      desc,
      t,
      same_origin_src + sensorName,
      expect_feature_available_default
    );
  }, `${sensorName}: ${header} allows same-origin iframes.`);

  async_test(t => {
    assert_implements(sensorName in self, `${sensorName} is not supported.`);
    test_feature_availability(
      desc,
      t,
      cross_origin_src + sensorName,
      expect_feature_unavailable_default
    );
  }, `${sensorName}: ${header} disallows cross-origin iframes.`);
}