test: fix flaky test-webcrypto-encrypt-decrypt-aes

* Use a copy of plaintext to prevent tampering of the original
* Since subtle.decrypt returns a Promise containing an ArrayBuffer and
  ArrayBuffers cannot be modified directly, create a Buffer from it
  right away so that the modification in the next line works as intended

Fixes: https://github.com/nodejs/node/issues/35586

PR-URL: https://github.com/nodejs/node/pull/37380
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Myles Borins <myles.borins@gmail.com>
This commit is contained in:
Darshan Sen authored and Myles Borins committed 2021-02-18 11:56:23 -05:00
1 parent 38f6e5a789
commit 4411a99dcd
2 files changed
+7 -4

No files matched your search

-2
View File
@@ -27,8 +27,6 @@ test-worker-memory: PASS,FLAKY
test-worker-message-port-transfer-terminate: PASS,FLAKY
[$system==linux]
# https://github.com/nodejs/node/issues/35586
test-webcrypto-encrypt-decrypt-aes: PASS,FLAKY
[$system==macos]
@@ -9,6 +9,9 @@ const assert = require('assert');
const { getRandomValues, subtle } = require('crypto').webcrypto;
async function testEncrypt({ keyBuffer, algorithm, plaintext, result }) {
// Using a copy of plaintext to prevent tampering of the original
plaintext = Buffer.from(plaintext);
const key = await subtle.importKey(
'raw',
keyBuffer,
@@ -23,8 +26,10 @@ async function testEncrypt({ keyBuffer, algorithm, plaintext, result }) {
Buffer.from(output).toString('hex'),
Buffer.from(result).toString('hex'));
const check = await subtle.decrypt(algorithm, key, output);
output[0] = 255 - output[0];
// Converting the returned ArrayBuffer into a Buffer right away,
// so that the next line works
const check = Buffer.from(await subtle.decrypt(algorithm, key, output));
check[0] = 255 - check[0];
assert.strictEqual(
Buffer.from(check).toString('hex'),