test: update hr-time web platform tests

PR-URL: https://github.com/nodejs/node/pull/44100
Reviewed-By: Feng Yu <F3n67u@outlook.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
This commit is contained in:
Yagiz Nizipli
2022-08-02 10:17:09 -04:00
committed by Node.js GitHub Bot
parent 118f001b67
commit 5465886d5d
13 changed files with 170 additions and 38 deletions

View File

@@ -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

View File

@@ -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>

View File

@@ -0,0 +1,2 @@
Cross-Origin-Embedder-Policy: require-corp
Cross-Origin-Opener-Policy: same-origin

View File

@@ -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>

View File

@@ -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>

View File

@@ -0,0 +1,2 @@
Cross-Origin-Embedder-Policy: require-corp
Cross-Origin-Opener-Policy: same-origin

View File

@@ -0,0 +1,2 @@
// META: script=/resources/idlharness-shadowrealm.js
idl_test_shadowrealm(["hr-time"], ["html", "dom"]);

View File

@@ -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>

View File

@@ -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.');
};

View File

@@ -0,0 +1,4 @@
<!DOCTYPE HTML>
<script>
window.parent.postMessage('done');
</script>

View File

@@ -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.');
}

View File

@@ -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>

View File

@@ -24,7 +24,7 @@
"path": "FileAPI"
},
"hr-time": {
"commit": "9910784394858a8e34d9eb4e5d00788765abf837",
"commit": "34cafd797e58dad280d20040eee012d49ccfa91f",
"path": "hr-time"
},
"html/webappapis/atob": {