mirror of
https://github.com/nodejs/node.git
synced 2026-11-09 15:11:11 -05:00
Replace the internal assertion with ERR_INVALID_ARG_VALUE when the output encoding changes between Cipher or Decipher calls. Document the encoding requirement and update the corresponding tests. Signed-off-by: Archkon <180910180+Archkon@users.noreply.github.com> PR-URL: https://github.com/nodejs/node/pull/64692 Fixes: https://github.com/nodejs/node/issues/64689 Reviewed-By: René <contact.9a5d6388@renegade334.me.uk> Reviewed-By: Tim Perry <pimterry@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Filip Skokan <panva.ip@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Aviv Keller <me@aviv.sh>
59 lines
1.2 KiB
JavaScript
59 lines
1.2 KiB
JavaScript
'use strict';
|
|
const common = require('../common');
|
|
if (!common.hasCrypto)
|
|
common.skip('missing crypto');
|
|
|
|
// This test checks if error is thrown in case of wrong encoding provided into cipher.
|
|
|
|
const assert = require('assert');
|
|
const { createCipheriv, randomBytes } = require('crypto');
|
|
|
|
const createCipher = () => {
|
|
return createCipheriv('aes-256-cbc', randomBytes(32), randomBytes(16));
|
|
};
|
|
|
|
const encodingChangeError = {
|
|
code: 'ERR_INVALID_ARG_VALUE',
|
|
name: 'TypeError',
|
|
message: /cannot be changed from 'utf8'/,
|
|
};
|
|
|
|
{
|
|
const cipher = createCipher();
|
|
cipher.update('test', 'utf-8', 'utf-8');
|
|
|
|
assert.throws(
|
|
() => cipher.update('666f6f', 'hex', 'hex'),
|
|
encodingChangeError
|
|
);
|
|
}
|
|
|
|
{
|
|
const cipher = createCipher();
|
|
cipher.update('test', 'utf-8', 'utf-8');
|
|
|
|
assert.throws(
|
|
() => cipher.final('hex'),
|
|
encodingChangeError
|
|
);
|
|
}
|
|
|
|
{
|
|
const cipher = createCipher();
|
|
cipher.update('test', 'utf-8', 'utf-8');
|
|
|
|
assert.throws(
|
|
() => cipher.final('bad2'),
|
|
{ message: /^Unknown encoding: bad2$/, code: 'ERR_UNKNOWN_ENCODING' }
|
|
);
|
|
}
|
|
|
|
{
|
|
const cipher = createCipher();
|
|
|
|
assert.throws(
|
|
() => cipher.update('test', 'utf-8', 'bad3'),
|
|
{ message: /^Unknown encoding: bad3$/, code: 'ERR_UNKNOWN_ENCODING' }
|
|
);
|
|
}
|