11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciif (!common.hasCrypto)
41cb0ef41Sopenharmony_ci  common.skip('missing crypto');
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ci// This test checks if error is thrown in case of wrong encoding provided into cipher.
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ciconst assert = require('assert');
91cb0ef41Sopenharmony_ciconst { createCipheriv, randomBytes } = require('crypto');
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ciconst createCipher = () => {
121cb0ef41Sopenharmony_ci  return createCipheriv('aes-256-cbc', randomBytes(32), randomBytes(16));
131cb0ef41Sopenharmony_ci};
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ci{
161cb0ef41Sopenharmony_ci  const cipher = createCipher();
171cb0ef41Sopenharmony_ci  cipher.update('test', 'utf-8', 'utf-8');
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ci  assert.throws(
201cb0ef41Sopenharmony_ci    () => cipher.update('666f6f', 'hex', 'hex'),
211cb0ef41Sopenharmony_ci    { message: /Cannot change encoding/ }
221cb0ef41Sopenharmony_ci  );
231cb0ef41Sopenharmony_ci}
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci{
261cb0ef41Sopenharmony_ci  const cipher = createCipher();
271cb0ef41Sopenharmony_ci  cipher.update('test', 'utf-8', 'utf-8');
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ci  assert.throws(
301cb0ef41Sopenharmony_ci    () => cipher.final('hex'),
311cb0ef41Sopenharmony_ci    { message: /Cannot change encoding/ }
321cb0ef41Sopenharmony_ci  );
331cb0ef41Sopenharmony_ci}
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ci{
361cb0ef41Sopenharmony_ci  const cipher = createCipher();
371cb0ef41Sopenharmony_ci  cipher.update('test', 'utf-8', 'utf-8');
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ci  assert.throws(
401cb0ef41Sopenharmony_ci    () => cipher.final('bad2'),
411cb0ef41Sopenharmony_ci    { message: /^Unknown encoding: bad2$/, code: 'ERR_UNKNOWN_ENCODING' }
421cb0ef41Sopenharmony_ci  );
431cb0ef41Sopenharmony_ci}
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ci{
461cb0ef41Sopenharmony_ci  const cipher = createCipher();
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ci  assert.throws(
491cb0ef41Sopenharmony_ci    () => cipher.update('test', 'utf-8', 'bad3'),
501cb0ef41Sopenharmony_ci    { message: /^Unknown encoding: bad3$/, code: 'ERR_UNKNOWN_ENCODING' }
511cb0ef41Sopenharmony_ci  );
521cb0ef41Sopenharmony_ci}
53