1'use strict';
2const common = require('../common');
3if (!common.hasCrypto)
4  common.skip('missing crypto');
5
6// This test checks if error is thrown in case of wrong encoding provided into cipher.
7
8const assert = require('assert');
9const { createCipheriv, randomBytes } = require('crypto');
10
11const createCipher = () => {
12  return createCipheriv('aes-256-cbc', randomBytes(32), randomBytes(16));
13};
14
15{
16  const cipher = createCipher();
17  cipher.update('test', 'utf-8', 'utf-8');
18
19  assert.throws(
20    () => cipher.update('666f6f', 'hex', 'hex'),
21    { message: /Cannot change encoding/ }
22  );
23}
24
25{
26  const cipher = createCipher();
27  cipher.update('test', 'utf-8', 'utf-8');
28
29  assert.throws(
30    () => cipher.final('hex'),
31    { message: /Cannot change encoding/ }
32  );
33}
34
35{
36  const cipher = createCipher();
37  cipher.update('test', 'utf-8', 'utf-8');
38
39  assert.throws(
40    () => cipher.final('bad2'),
41    { message: /^Unknown encoding: bad2$/, code: 'ERR_UNKNOWN_ENCODING' }
42  );
43}
44
45{
46  const cipher = createCipher();
47
48  assert.throws(
49    () => cipher.update('test', 'utf-8', 'bad3'),
50    { message: /^Unknown encoding: bad3$/, code: 'ERR_UNKNOWN_ENCODING' }
51  );
52}
53