summaryrefslogtreecommitdiff
path: root/test/parallel/test-crypto-encoding-validation-error.js
blob: 0e921ac2862f49ea44553ba5e093b1db1aa197fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
'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 cipher = createCipher();
  cipher.update('test', 'utf-8', 'utf-8');

  assert.throws(
    () => cipher.update('666f6f', 'hex', 'hex'),
    { message: /Cannot change encoding/ }
  );
}

{
  const cipher = createCipher();
  cipher.update('test', 'utf-8', 'utf-8');

  assert.throws(
    () => cipher.final('hex'),
    { message: /Cannot change encoding/ }
  );
}

{
  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' }
  );
}