Skip to content

Commit

Permalink
test: update hr-time web platform tests
Browse files Browse the repository at this point in the history
PR-URL: #44100
Reviewed-By: Feng Yu <F3n67u@outlook.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
  • Loading branch information
anonrig authored and nodejs-github-bot committed Aug 8, 2022
1 parent 118f001 commit 5465886
Show file tree
Hide file tree
Showing 13 changed files with 170 additions and 38 deletions.
2 changes: 1 addition & 1 deletion test/fixtures/wpt/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Last update:
- dom/events: https://github.com/web-platform-tests/wpt/tree/f8821adb28/dom/events
- encoding: https://github.com/web-platform-tests/wpt/tree/c1b24fce6e/encoding
- FileAPI: https://github.com/web-platform-tests/wpt/tree/3b279420d4/FileAPI
- hr-time: https://github.com/web-platform-tests/wpt/tree/9910784394/hr-time
- hr-time: https://github.com/web-platform-tests/wpt/tree/34cafd797e/hr-time
- html/webappapis/atob: https://github.com/web-platform-tests/wpt/tree/f267e1dca6/html/webappapis/atob
- html/webappapis/microtask-queuing: https://github.com/web-platform-tests/wpt/tree/2c5c3c4c27/html/webappapis/microtask-queuing
- html/webappapis/structured-clone: https://github.com/web-platform-tests/wpt/tree/47d3fb280c/html/webappapis/structured-clone
Expand Down
14 changes: 14 additions & 0 deletions test/fixtures/wpt/hr-time/clamped-time-origin-isolated.https.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!doctype html>
<html>
<head>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/clamped-time-origin.js"></script>
</head>
<body>
<script>
// Isolated contexts should be clamped to 5 microseconds.
run_test(/*isolated=*/true);
</script>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Cross-Origin-Embedder-Policy: require-corp
Cross-Origin-Opener-Policy: same-origin
14 changes: 14 additions & 0 deletions test/fixtures/wpt/hr-time/clamped-time-origin.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!doctype html>
<html>
<head>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/clamped-time-origin.js"></script>
</head>
<body>
<script>
// Non-isolated contexts should be clamped to 100 microseconds.
run_test(/*isolated=*/false);
</script>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>window.performance.now should not enable timing attacks</title>
<link rel="author" title="W3C" href="http://www.w3.org/" />
<link rel="help" href="http://w3c.github.io/hr-time/#privacy-security"/>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/timing-attack.js"></script>
<script>
run_test(/*isolated=*/true);
</script>
</head>
<body>
<h1>Description</h1>
<p>The recommended minimum resolution of the Performance interface should be set to 5 microseconds.</p>

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

</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Cross-Origin-Embedder-Policy: require-corp
Cross-Origin-Opener-Policy: same-origin
2 changes: 2 additions & 0 deletions test/fixtures/wpt/hr-time/idlharness-shadowrealm.window.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// META: script=/resources/idlharness-shadowrealm.js
idl_test_shadowrealm(["hr-time"], ["html", "dom"]);
33 changes: 33 additions & 0 deletions test/fixtures/wpt/hr-time/navigation-start-post-before-unload.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<!doctype html>
<html>
<head>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
</head>
<body>
<script>
function busyWait(millis) {
const start = performance.now();
while (performance.now() < start + millis) {}
}
promise_test(async t => {
const delay = 3000;
const iframe = document.createElement('iframe');
iframe.src = './resources/now_frame.html';
document.body.appendChild(iframe);
await new Promise(resolve => iframe.addEventListener('load', resolve));
iframe.contentWindow.addEventListener('beforeunload', () => {
busyWait(delay);
});
iframe.src = './resources/post.html';
await new Promise(resolve => this.addEventListener('message', ({data}) => {
if (data === 'done')
resolve();
}));

const entry = iframe.contentWindow.performance.getEntriesByType('navigation')[0];
assert_less_than(entry.fetchStart, delay);
}, 'timeOrigin should be set after beforeunload');
</script>
</body>
</html>
30 changes: 30 additions & 0 deletions test/fixtures/wpt/hr-time/resources/clamped-time-origin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const run_test = isolated => {
// Multiplier to convert the clamped timestamps to microseconds.
const multiplier = 1000;
const windowOrigin = performance.timeOrigin;
// Clamp to at least 5 microseconds in isolated contexts and at least 100 in
// non-isolated ones.
const resolution = isolated ? 5 : 100;

const create_worker = () => {
return new Promise(resolve => {
const workerScript = 'postMessage({timeOrigin: performance.timeOrigin})';
const blob = new Blob([workerScript]);
const worker = new Worker(URL.createObjectURL(blob));
worker.addEventListener('message', event => {
resolve(event.data.timeOrigin);
});
});
};
promise_test(async t => {
assert_equals(self.crossOriginIsolated, isolated,
"crossOriginIsolated is properly set");
let prev = windowOrigin;
let current;
for (let i = 1; i < 100; ++i) {
current = await create_worker();
assert_true(current === prev || current - prev > resolution / 1000);
prev = current;
}
}, 'timeOrigins are clamped.');
};
4 changes: 4 additions & 0 deletions test/fixtures/wpt/hr-time/resources/post.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<!DOCTYPE HTML>
<script>
window.parent.postMessage('done');
</script>
42 changes: 42 additions & 0 deletions test/fixtures/wpt/hr-time/resources/timing-attack.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
function run_test(isolated) {
let resolution = 100;
if (isolated) {
resolution = 5;
}
test(function() {
function check_resolutions(times, length) {
const end = length - 2;

// we compare each value with the following ones
for (let i = 0; i < end; i++) {
const h1 = times[i];
for (let j = i+1; j < end; j++) {
const h2 = times[j];
const diff = h2 - h1;
assert_true((diff === 0) || ((diff * 1000) >= resolution),
"Differences smaller than ' + resolution + ' microseconds: " + diff);
}
}
return true;
}

const times = new Array(10);
let index = 0;
let hrt1, hrt2, hrt;
assert_equals(self.crossOriginIsolated, isolated, "Document cross-origin isolated value matches");

// rapid firing of performance.now
hrt1 = performance.now();
hrt2 = performance.now();
times[index++] = hrt1;
times[index++] = hrt2;

// ensure that we get performance.now() to return a different value
do {
hrt = performance.now();
times[index++] = hrt;
} while ((hrt - hrt1) === 0);

assert_true(check_resolutions(times, index), 'Difference should be at least ' + resolution + ' microseconds.');
}, 'The recommended minimum resolution of the Performance interface has been set to ' + resolution + ' microseconds for cross-origin isolated contexts.');
}
39 changes: 3 additions & 36 deletions test/fixtures/wpt/hr-time/timing-attack.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,47 +7,14 @@
<link rel="help" href="http://w3c.github.io/hr-time/#privacy-security"/>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/timing-attack.js"></script>
<script>
test(function() {
function check_resolutions(times, length) {
var end = length - 2;

// we compare each value with the following ones
for (var i = 0; i < end; i++) {
var h1 = times[i];
for (var j = i+1; j < end; j++) {
var h2 = times[j];
var diff = h2 - h1;
assert_true((diff === 0) || ((diff * 1000) >= 5),
"Differences smaller than 5 microseconds: " + diff);
}
}
return true;
}

var times = new Array(10);
var index = 0;
var hrt1, hrt2, hrt;

// rapid firing of performance.now
hrt1 = performance.now();
hrt2 = performance.now();
times[index++] = hrt1;
times[index++] = hrt2;

// ensure that we get performance.now() to return a different value
do {
hrt = performance.now();
times[index++] = hrt;
} while ((hrt - hrt1) === 0);

assert_true(check_resolutions(times, index), 'Difference should be at least 5 microseconds.');
}, 'The recommended minimum resolution of the Performance interface has been set to 5 microseconds');
run_test(/*isolated=*/false);
</script>
</head>
<body>
<h1>Description</h1>
<p>The recommended minimum resolution of the Performance interface should be set to 5 microseconds.</p>
<p>The recommended minimum resolution of the Performance interface should be set to 100 microseconds for non-isolated contexts.</p>

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

Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/wpt/versions.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"path": "FileAPI"
},
"hr-time": {
"commit": "9910784394858a8e34d9eb4e5d00788765abf837",
"commit": "34cafd797e58dad280d20040eee012d49ccfa91f",
"path": "hr-time"
},
"html/webappapis/atob": {
Expand Down

0 comments on commit 5465886

Please sign in to comment.