Skip to content

Commit

Permalink
test: fix flaky test-webcrypto-encrypt-decrypt-aes
Browse files Browse the repository at this point in the history
* 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: #35586

PR-URL: #37380
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Myles Borins <myles.borins@gmail.com>
  • Loading branch information
RaisinTen authored and targos committed Feb 28, 2021
1 parent 429dffd commit df538eb
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
2 changes: 0 additions & 2 deletions test/parallel/parallel.status
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,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]

Expand Down
9 changes: 7 additions & 2 deletions test/parallel/test-webcrypto-encrypt-decrypt-aes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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'),
Expand Down

0 comments on commit df538eb

Please sign in to comment.