From df538ebc8e07cdee439b7743b8c405220d758eab Mon Sep 17 00:00:00 2001 From: Darshan Sen Date: Mon, 15 Feb 2021 18:50:54 +0530 Subject: [PATCH] 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 Reviewed-By: Myles Borins --- test/parallel/parallel.status | 2 -- test/parallel/test-webcrypto-encrypt-decrypt-aes.js | 9 +++++++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/test/parallel/parallel.status b/test/parallel/parallel.status index 75d543420dea53..8e070688b1f250 100644 --- a/test/parallel/parallel.status +++ b/test/parallel/parallel.status @@ -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] diff --git a/test/parallel/test-webcrypto-encrypt-decrypt-aes.js b/test/parallel/test-webcrypto-encrypt-decrypt-aes.js index 38d2b70bcb0567..7adb18918d2205 100644 --- a/test/parallel/test-webcrypto-encrypt-decrypt-aes.js +++ b/test/parallel/test-webcrypto-encrypt-decrypt-aes.js @@ -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'),