Skip to content

Commit 5400d08

Browse files
refactor(crypto test): use wpt to test crypto api (#5471)
Closes #4268 Co-authored-by: Agnès Toulet <35176601+AgnesToulet@users.noreply.github.com>
1 parent 239f267 commit 5400d08

3 files changed

Lines changed: 59 additions & 267 deletions

File tree

internal/js/modules/k6/webcrypto/tests/crypto_test.go

Lines changed: 0 additions & 267 deletions
This file was deleted.

internal/js/modules/k6/webcrypto/tests/subtle_crypto_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,20 @@ func TestWebPlatformTestSuite(t *testing.T) {
3030
// if empty, no function will be called
3131
callFn string
3232
}{
33+
// test cases for crypto api
34+
{
35+
catalog: "",
36+
files: []string{
37+
"getRandomValues.any.js",
38+
},
39+
},
40+
{
41+
catalog: "",
42+
files: []string{
43+
"randomUUID.https.any.js",
44+
},
45+
},
46+
// test cases for subtle crypto api
3347
{
3448
catalog: "digest",
3549
files: []string{
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
diff --git a/WebCryptoAPI/getRandomValues.any.js b/WebCryptoAPI/getRandomValues.any.js
2+
index 574134eb7..128c805a5 100644
3+
--- a/WebCryptoAPI/getRandomValues.any.js
4+
+++ b/WebCryptoAPI/getRandomValues.any.js
5+
@@ -1,14 +1,31 @@
6+
+// k6 throws GoError instead of DOMException, so we need to monkey patch.
7+
+const originalAssertThrowsDom = self.assert_throws_dom;
8+
+self.assert_throws_dom = function(expectedErrorName, func, description) {
9+
+ try {
10+
+ func();
11+
+ } catch (e) {
12+
+ const isGoError = (typeof e === "object") && (e.name === "GoError");
13+
+ const msg = isGoError ? (e.message || "") : e.toString();
14+
+ if (msg.includes(expectedErrorName)) {
15+
+ return; // Treat as correct
16+
+ }
17+
+ }
18+
+
19+
+ return originalAssertThrowsDom(expectedErrorName, func, description);
20+
+}
21+
+
22+
// Step 1.
23+
-test(function() {
24+
- assert_throws_dom("TypeMismatchError", function() {
25+
- self.crypto.getRandomValues(new Float16Array(6))
26+
- }, "Float16Array")
27+
+// Float16Array is not supported in k6
28+
+// test(function() {
29+
+// assert_throws_dom("TypeMismatchError", function() {
30+
+// self.crypto.getRandomValues(new Float16Array(6))
31+
+// }, "Float16Array")
32+
33+
- assert_throws_dom("TypeMismatchError", function() {
34+
- const len = 65536 / Float16Array.BYTES_PER_ELEMENT + 1;
35+
- self.crypto.getRandomValues(new Float16Array(len));
36+
- }, "Float16Array (too long)")
37+
-}, "Float16 arrays");
38+
+// assert_throws_dom("TypeMismatchError", function() {
39+
+// const len = 65536 / Float16Array.BYTES_PER_ELEMENT + 1;
40+
+// self.crypto.getRandomValues(new Float16Array(len));
41+
+// }, "Float16Array (too long)")
42+
+// }, "Float16 arrays");
43+
44+
test(function() {
45+
assert_throws_dom("TypeMismatchError", function() {

0 commit comments

Comments
 (0)