Skip to content

[WebCryptoAPI] Add tests for argument read order#57614

Merged
twiss merged 2 commits intoweb-platform-tests:masterfrom
twiss:webcrypto-argument-read-order
Feb 26, 2026
Merged

[WebCryptoAPI] Add tests for argument read order#57614
twiss merged 2 commits intoweb-platform-tests:masterfrom
twiss:webcrypto-argument-read-order

Conversation

@twiss
Copy link
Copy Markdown
Member

@twiss twiss commented Feb 6, 2026

Add tests for the reading order of arguments in Web Crypto, updated in w3c/webcrypto#426 to match the majority of implementations.

To be merged after w3c/webcrypto#426.

Comment on lines +181 to +185
get name() {
// Alter the buffer back while calling digest
buffer[0] = ~buffer[0];
return alg;
},
Copy link
Copy Markdown
Contributor

@panva panva Feb 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this and all the others using the same pattern truly alter the buffer back instead of continuously switching it back and forth? Otherwise it's dependant on that the name is only accessed once.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, interesting question. It's true that it wasn't my intention for this test to check that name is accessed only once, however, I do think WebIDL and WebCrypto are defined as such, because the property is accessed once in step 4 of normalizing an algorithm, and then never thereafter. So I think it should be fine to rely on this?

Then again, it might make it harder to debug what's the cause of this test failing if it does, and of course this is not quite a proper test of the property being accessed once as it would also pass if it's accessed three times :D So, we could also split that up, if we think the latter is worth testing.

Copy link
Copy Markdown
Contributor

@panva panva Feb 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it might make it harder to debug what's the cause of this test failing if it does

Exactly where i'm coming from. I'm not interested in testing that algorithm properties are only accessed once. Nor in the order itself FWIW but here we are. Optimizations that we have in Node.js rule out this proposed test pattern.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well we might need a separate test then because it sounds like Node.js might not have a conforming bindings implementation?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All i'm asking is that the getter "switches back" instead of "switching back and forth".

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I've updated the tests. We can separately add more tests for the bindings / argument normalization later.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand what you're asking for, but since you're supposed to only call these once and in order, we'd be hiding a bug of sorts by removing coverage for that.

Rather than flipping the data on each call to the argument name getter
function, restore it to the original value. This makes it easier to
distinguish the cause of test failures if the argument name getter is
called multiple times.
Copy link
Copy Markdown
Member

@annevk annevk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By-and-large this seems good, though not sure we needed this many tests. Quality of JS could be improved a bit while here, but I suppose that's largely up to you all.

Comment on lines +181 to +185
get name() {
// Alter the buffer back while calling digest
buffer[0] = ~buffer[0];
return alg;
},
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand what you're asking for, but since you're supposed to only call these once and in order, we'd be hiding a bug of sorts by removing coverage for that.

if (buffer.length > 0) {
if (sourceData[size].length > 0) {
promise_test(function (test) {
var buffer = new Uint8Array(sourceData[size]);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const

// Alter the buffer after calling digest
if (buffer.length > 0) {
if (sourceData[size].length > 0) {
promise_test(function (test) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we are going to reformat anyway, might as well adopt async/await?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't really reformat anything here, I just added an if around it. I can refactor this and other tests in a followup.

.digest({ name: alg, length: length }, buffer)
.then(function (result) {
// Alter the buffer after calling digest
buffer[0] = ~buffer[0];
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also alter it immediately after calling digest. In fact, that would be the superior test as this would be redundant with that.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah this test is wrong, I fixed that in #57616.

@twiss
Copy link
Copy Markdown
Member Author

twiss commented Feb 16, 2026

Fair enough :D I mostly just copy+pasted all the instances of the "modify the buffer after the call" tests and modified them mechanically. I also thought having all the algorithms at least might be useful since some implementations implement some of them synchronously and others not, but it seems the implementations are consistent between algorithms at least, indeed.

@wpt-pr-bot
Copy link
Copy Markdown
Collaborator

There are no reviewers for this pull request besides its author. Please reach out on the chat room to get help with this. Thank you!

@twiss twiss merged commit 42e4732 into web-platform-tests:master Feb 26, 2026
25 checks passed
@twiss twiss deleted the webcrypto-argument-read-order branch February 26, 2026 14:01
kkoyung added a commit to kkoyung/servo that referenced this pull request Mar 1, 2026
The specification of WebCrypto and Modern Algorithms in WebCrypto has
updated to reorder reading algorithm and data arguments in "encrypt",
"decrypt", "sign", "verify", "digest", "importKey", "unwrapKey",
"decapsulateKey" and "decapsulateBits" methods.

WebCrypto:
w3c/webcrypto@5b57233

Modern Algorithms in WebCrypto:
WICG/webcrypto-modern-algos@ae72ee6

This patch updates our implementation accordingly.

Testing: Pass new WPT tests added in
web-platform-tests/wpt#57614 and
servo#42925

Signed-off-by: Kingsley Yung <[email protected]>
github-merge-queue bot pushed a commit to servo/servo that referenced this pull request Mar 1, 2026
…2927)

The specification of WebCrypto and Modern Algorithms in WebCrypto has
updated to reorder reading algorithm and data arguments in "encrypt",
"decrypt", "sign", "verify", "digest", "importKey", "unwrapKey",
"decapsulateKey" and "decapsulateBits" methods. This patch updates our
implementation accordingly.

The relevant commits in specification repositories:
- WebCrypto:
w3c/webcrypto@5b57233
- Modern Algorithms in WebCrypto:
WICG/webcrypto-modern-algos@ae72ee6

Testing: Pass new WPT tests added in
web-platform-tests/wpt#57614 and
#42925

Signed-off-by: Kingsley Yung <[email protected]>
github-merge-queue bot pushed a commit to servo/servo that referenced this pull request Mar 1, 2026
…2927)

The specification of WebCrypto and Modern Algorithms in WebCrypto has
updated to reorder reading algorithm and data arguments in "encrypt",
"decrypt", "sign", "verify", "digest", "importKey", "unwrapKey",
"decapsulateKey" and "decapsulateBits" methods. This patch updates our
implementation accordingly.

The relevant commits in specification repositories:
- WebCrypto:
w3c/webcrypto@5b57233
- Modern Algorithms in WebCrypto:
WICG/webcrypto-modern-algos@ae72ee6

Testing: Pass new WPT tests added in
web-platform-tests/wpt#57614 and
#42925

Signed-off-by: Kingsley Yung <[email protected]>
simonwuelker pushed a commit to simonwuelker/servo that referenced this pull request Mar 3, 2026
…rvo#42927)

The specification of WebCrypto and Modern Algorithms in WebCrypto has
updated to reorder reading algorithm and data arguments in "encrypt",
"decrypt", "sign", "verify", "digest", "importKey", "unwrapKey",
"decapsulateKey" and "decapsulateBits" methods. This patch updates our
implementation accordingly.

The relevant commits in specification repositories:
- WebCrypto:
w3c/webcrypto@5b57233
- Modern Algorithms in WebCrypto:
WICG/webcrypto-modern-algos@ae72ee6

Testing: Pass new WPT tests added in
web-platform-tests/wpt#57614 and
servo#42925

Signed-off-by: Kingsley Yung <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants