Skip to content

Commit 537d52f

Browse files
anonrigruyadorno
authored andcommitted
test: update hr-time web platform tests
PR-URL: #44100 Reviewed-By: Feng Yu <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
1 parent 79445cb commit 537d52f

13 files changed

+170
-38
lines changed

test/fixtures/wpt/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Last update:
1616
- dom/events: https://github.com/web-platform-tests/wpt/tree/f8821adb28/dom/events
1717
- encoding: https://github.com/web-platform-tests/wpt/tree/c1b24fce6e/encoding
1818
- FileAPI: https://github.com/web-platform-tests/wpt/tree/3b279420d4/FileAPI
19-
- hr-time: https://github.com/web-platform-tests/wpt/tree/9910784394/hr-time
19+
- hr-time: https://github.com/web-platform-tests/wpt/tree/34cafd797e/hr-time
2020
- html/webappapis/atob: https://github.com/web-platform-tests/wpt/tree/f267e1dca6/html/webappapis/atob
2121
- html/webappapis/microtask-queuing: https://github.com/web-platform-tests/wpt/tree/2c5c3c4c27/html/webappapis/microtask-queuing
2222
- html/webappapis/structured-clone: https://github.com/web-platform-tests/wpt/tree/47d3fb280c/html/webappapis/structured-clone
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<script src="/resources/testharness.js"></script>
5+
<script src="/resources/testharnessreport.js"></script>
6+
<script src="resources/clamped-time-origin.js"></script>
7+
</head>
8+
<body>
9+
<script>
10+
// Isolated contexts should be clamped to 5 microseconds.
11+
run_test(/*isolated=*/true);
12+
</script>
13+
</body>
14+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Cross-Origin-Embedder-Policy: require-corp
2+
Cross-Origin-Opener-Policy: same-origin
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<script src="/resources/testharness.js"></script>
5+
<script src="/resources/testharnessreport.js"></script>
6+
<script src="resources/clamped-time-origin.js"></script>
7+
</head>
8+
<body>
9+
<script>
10+
// Non-isolated contexts should be clamped to 100 microseconds.
11+
run_test(/*isolated=*/false);
12+
</script>
13+
</body>
14+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8" />
5+
<title>window.performance.now should not enable timing attacks</title>
6+
<link rel="author" title="W3C" href="http://www.w3.org/" />
7+
<link rel="help" href="http://w3c.github.io/hr-time/#privacy-security"/>
8+
<script src="/resources/testharness.js"></script>
9+
<script src="/resources/testharnessreport.js"></script>
10+
<script src="resources/timing-attack.js"></script>
11+
<script>
12+
run_test(/*isolated=*/true);
13+
</script>
14+
</head>
15+
<body>
16+
<h1>Description</h1>
17+
<p>The recommended minimum resolution of the Performance interface should be set to 5 microseconds.</p>
18+
19+
<div id="log"></div>
20+
21+
</body>
22+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Cross-Origin-Embedder-Policy: require-corp
2+
Cross-Origin-Opener-Policy: same-origin
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// META: script=/resources/idlharness-shadowrealm.js
2+
idl_test_shadowrealm(["hr-time"], ["html", "dom"]);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<script src="/resources/testharness.js"></script>
5+
<script src="/resources/testharnessreport.js"></script>
6+
</head>
7+
<body>
8+
<script>
9+
function busyWait(millis) {
10+
const start = performance.now();
11+
while (performance.now() < start + millis) {}
12+
}
13+
promise_test(async t => {
14+
const delay = 3000;
15+
const iframe = document.createElement('iframe');
16+
iframe.src = './resources/now_frame.html';
17+
document.body.appendChild(iframe);
18+
await new Promise(resolve => iframe.addEventListener('load', resolve));
19+
iframe.contentWindow.addEventListener('beforeunload', () => {
20+
busyWait(delay);
21+
});
22+
iframe.src = './resources/post.html';
23+
await new Promise(resolve => this.addEventListener('message', ({data}) => {
24+
if (data === 'done')
25+
resolve();
26+
}));
27+
28+
const entry = iframe.contentWindow.performance.getEntriesByType('navigation')[0];
29+
assert_less_than(entry.fetchStart, delay);
30+
}, 'timeOrigin should be set after beforeunload');
31+
</script>
32+
</body>
33+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const run_test = isolated => {
2+
// Multiplier to convert the clamped timestamps to microseconds.
3+
const multiplier = 1000;
4+
const windowOrigin = performance.timeOrigin;
5+
// Clamp to at least 5 microseconds in isolated contexts and at least 100 in
6+
// non-isolated ones.
7+
const resolution = isolated ? 5 : 100;
8+
9+
const create_worker = () => {
10+
return new Promise(resolve => {
11+
const workerScript = 'postMessage({timeOrigin: performance.timeOrigin})';
12+
const blob = new Blob([workerScript]);
13+
const worker = new Worker(URL.createObjectURL(blob));
14+
worker.addEventListener('message', event => {
15+
resolve(event.data.timeOrigin);
16+
});
17+
});
18+
};
19+
promise_test(async t => {
20+
assert_equals(self.crossOriginIsolated, isolated,
21+
"crossOriginIsolated is properly set");
22+
let prev = windowOrigin;
23+
let current;
24+
for (let i = 1; i < 100; ++i) {
25+
current = await create_worker();
26+
assert_true(current === prev || current - prev > resolution / 1000);
27+
prev = current;
28+
}
29+
}, 'timeOrigins are clamped.');
30+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<!DOCTYPE HTML>
2+
<script>
3+
window.parent.postMessage('done');
4+
</script>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
function run_test(isolated) {
2+
let resolution = 100;
3+
if (isolated) {
4+
resolution = 5;
5+
}
6+
test(function() {
7+
function check_resolutions(times, length) {
8+
const end = length - 2;
9+
10+
// we compare each value with the following ones
11+
for (let i = 0; i < end; i++) {
12+
const h1 = times[i];
13+
for (let j = i+1; j < end; j++) {
14+
const h2 = times[j];
15+
const diff = h2 - h1;
16+
assert_true((diff === 0) || ((diff * 1000) >= resolution),
17+
"Differences smaller than ' + resolution + ' microseconds: " + diff);
18+
}
19+
}
20+
return true;
21+
}
22+
23+
const times = new Array(10);
24+
let index = 0;
25+
let hrt1, hrt2, hrt;
26+
assert_equals(self.crossOriginIsolated, isolated, "Document cross-origin isolated value matches");
27+
28+
// rapid firing of performance.now
29+
hrt1 = performance.now();
30+
hrt2 = performance.now();
31+
times[index++] = hrt1;
32+
times[index++] = hrt2;
33+
34+
// ensure that we get performance.now() to return a different value
35+
do {
36+
hrt = performance.now();
37+
times[index++] = hrt;
38+
} while ((hrt - hrt1) === 0);
39+
40+
assert_true(check_resolutions(times, index), 'Difference should be at least ' + resolution + ' microseconds.');
41+
}, 'The recommended minimum resolution of the Performance interface has been set to ' + resolution + ' microseconds for cross-origin isolated contexts.');
42+
}

test/fixtures/wpt/hr-time/timing-attack.html

+3-36
Original file line numberDiff line numberDiff line change
@@ -7,47 +7,14 @@
77
<link rel="help" href="http://w3c.github.io/hr-time/#privacy-security"/>
88
<script src="/resources/testharness.js"></script>
99
<script src="/resources/testharnessreport.js"></script>
10+
<script src="resources/timing-attack.js"></script>
1011
<script>
11-
test(function() {
12-
function check_resolutions(times, length) {
13-
var end = length - 2;
14-
15-
// we compare each value with the following ones
16-
for (var i = 0; i < end; i++) {
17-
var h1 = times[i];
18-
for (var j = i+1; j < end; j++) {
19-
var h2 = times[j];
20-
var diff = h2 - h1;
21-
assert_true((diff === 0) || ((diff * 1000) >= 5),
22-
"Differences smaller than 5 microseconds: " + diff);
23-
}
24-
}
25-
return true;
26-
}
27-
28-
var times = new Array(10);
29-
var index = 0;
30-
var hrt1, hrt2, hrt;
31-
32-
// rapid firing of performance.now
33-
hrt1 = performance.now();
34-
hrt2 = performance.now();
35-
times[index++] = hrt1;
36-
times[index++] = hrt2;
37-
38-
// ensure that we get performance.now() to return a different value
39-
do {
40-
hrt = performance.now();
41-
times[index++] = hrt;
42-
} while ((hrt - hrt1) === 0);
43-
44-
assert_true(check_resolutions(times, index), 'Difference should be at least 5 microseconds.');
45-
}, 'The recommended minimum resolution of the Performance interface has been set to 5 microseconds');
12+
run_test(/*isolated=*/false);
4613
</script>
4714
</head>
4815
<body>
4916
<h1>Description</h1>
50-
<p>The recommended minimum resolution of the Performance interface should be set to 5 microseconds.</p>
17+
<p>The recommended minimum resolution of the Performance interface should be set to 100 microseconds for non-isolated contexts.</p>
5118

5219
<div id="log"></div>
5320

test/fixtures/wpt/versions.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"path": "FileAPI"
2525
},
2626
"hr-time": {
27-
"commit": "9910784394858a8e34d9eb4e5d00788765abf837",
27+
"commit": "34cafd797e58dad280d20040eee012d49ccfa91f",
2828
"path": "hr-time"
2929
},
3030
"html/webappapis/atob": {

0 commit comments

Comments
 (0)