11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciif (!common.hasCrypto)
41cb0ef41Sopenharmony_ci  common.skip('missing crypto');
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciconst assert = require('assert');
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ciconst {
91cb0ef41Sopenharmony_ci  generateKey,
101cb0ef41Sopenharmony_ci  generateKeySync
111cb0ef41Sopenharmony_ci} = require('crypto');
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ci[1, true, [], {}, Infinity, null, undefined].forEach((i) => {
141cb0ef41Sopenharmony_ci  assert.throws(() => generateKey(i, 1, common.mustNotCall()), {
151cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_ARG_TYPE',
161cb0ef41Sopenharmony_ci    message: /The "type" argument must be /
171cb0ef41Sopenharmony_ci  });
181cb0ef41Sopenharmony_ci  assert.throws(() => generateKeySync(i, 1), {
191cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_ARG_TYPE',
201cb0ef41Sopenharmony_ci    message: /The "type" argument must be /
211cb0ef41Sopenharmony_ci  });
221cb0ef41Sopenharmony_ci});
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ci['', true, [], null, undefined].forEach((i) => {
251cb0ef41Sopenharmony_ci  assert.throws(() => generateKey('aes', i, common.mustNotCall()), {
261cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_ARG_TYPE',
271cb0ef41Sopenharmony_ci    message: /The "options" argument must be /
281cb0ef41Sopenharmony_ci  });
291cb0ef41Sopenharmony_ci  assert.throws(() => generateKeySync('aes', i), {
301cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_ARG_TYPE',
311cb0ef41Sopenharmony_ci    message: /The "options" argument must be /
321cb0ef41Sopenharmony_ci  });
331cb0ef41Sopenharmony_ci});
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ci['', true, {}, [], null, undefined].forEach((length) => {
361cb0ef41Sopenharmony_ci  assert.throws(() => generateKey('hmac', { length }, common.mustNotCall()), {
371cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_ARG_TYPE',
381cb0ef41Sopenharmony_ci    message: /The "options\.length" property must be /
391cb0ef41Sopenharmony_ci  });
401cb0ef41Sopenharmony_ci  assert.throws(() => generateKeySync('hmac', { length }), {
411cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_ARG_TYPE',
421cb0ef41Sopenharmony_ci    message: /The "options\.length" property must be /
431cb0ef41Sopenharmony_ci  });
441cb0ef41Sopenharmony_ci});
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ciassert.throws(() => generateKey('aes', { length: 256 }), {
471cb0ef41Sopenharmony_ci  code: 'ERR_INVALID_ARG_TYPE'
481cb0ef41Sopenharmony_ci});
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ciassert.throws(() => generateKey('hmac', { length: -1 }, common.mustNotCall()), {
511cb0ef41Sopenharmony_ci  code: 'ERR_OUT_OF_RANGE'
521cb0ef41Sopenharmony_ci});
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ciassert.throws(() => generateKey('hmac', { length: 4 }, common.mustNotCall()), {
551cb0ef41Sopenharmony_ci  code: 'ERR_OUT_OF_RANGE'
561cb0ef41Sopenharmony_ci});
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ciassert.throws(() => generateKey('hmac', { length: 7 }, common.mustNotCall()), {
591cb0ef41Sopenharmony_ci  code: 'ERR_OUT_OF_RANGE'
601cb0ef41Sopenharmony_ci});
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_ciassert.throws(
631cb0ef41Sopenharmony_ci  () => generateKey('hmac', { length: 2 ** 31 }, common.mustNotCall()), {
641cb0ef41Sopenharmony_ci    code: 'ERR_OUT_OF_RANGE'
651cb0ef41Sopenharmony_ci  });
661cb0ef41Sopenharmony_ci
671cb0ef41Sopenharmony_ciassert.throws(() => generateKeySync('hmac', { length: -1 }), {
681cb0ef41Sopenharmony_ci  code: 'ERR_OUT_OF_RANGE'
691cb0ef41Sopenharmony_ci});
701cb0ef41Sopenharmony_ci
711cb0ef41Sopenharmony_ciassert.throws(() => generateKeySync('hmac', { length: 4 }), {
721cb0ef41Sopenharmony_ci  code: 'ERR_OUT_OF_RANGE'
731cb0ef41Sopenharmony_ci});
741cb0ef41Sopenharmony_ci
751cb0ef41Sopenharmony_ciassert.throws(() => generateKeySync('hmac', { length: 7 }), {
761cb0ef41Sopenharmony_ci  code: 'ERR_OUT_OF_RANGE'
771cb0ef41Sopenharmony_ci});
781cb0ef41Sopenharmony_ci
791cb0ef41Sopenharmony_ciassert.throws(
801cb0ef41Sopenharmony_ci  () => generateKeySync('hmac', { length: 2 ** 31 }), {
811cb0ef41Sopenharmony_ci    code: 'ERR_OUT_OF_RANGE'
821cb0ef41Sopenharmony_ci  });
831cb0ef41Sopenharmony_ci
841cb0ef41Sopenharmony_ciassert.throws(() => generateKeySync('aes', { length: 123 }), {
851cb0ef41Sopenharmony_ci  code: 'ERR_INVALID_ARG_VALUE',
861cb0ef41Sopenharmony_ci  message: /The property 'options\.length' must be one of: 128, 192, 256/
871cb0ef41Sopenharmony_ci});
881cb0ef41Sopenharmony_ci
891cb0ef41Sopenharmony_ci{
901cb0ef41Sopenharmony_ci  const key = generateKeySync('aes', { length: 128 });
911cb0ef41Sopenharmony_ci  assert(key);
921cb0ef41Sopenharmony_ci  const keybuf = key.export();
931cb0ef41Sopenharmony_ci  assert.strictEqual(keybuf.byteLength, 128 / 8);
941cb0ef41Sopenharmony_ci
951cb0ef41Sopenharmony_ci  generateKey('aes', { length: 128 }, common.mustSucceed((key) => {
961cb0ef41Sopenharmony_ci    assert(key);
971cb0ef41Sopenharmony_ci    const keybuf = key.export();
981cb0ef41Sopenharmony_ci    assert.strictEqual(keybuf.byteLength, 128 / 8);
991cb0ef41Sopenharmony_ci  }));
1001cb0ef41Sopenharmony_ci}
1011cb0ef41Sopenharmony_ci
1021cb0ef41Sopenharmony_ci{
1031cb0ef41Sopenharmony_ci  const key = generateKeySync('aes', { length: 256 });
1041cb0ef41Sopenharmony_ci  assert(key);
1051cb0ef41Sopenharmony_ci  const keybuf = key.export();
1061cb0ef41Sopenharmony_ci  assert.strictEqual(keybuf.byteLength, 256 / 8);
1071cb0ef41Sopenharmony_ci
1081cb0ef41Sopenharmony_ci  generateKey('aes', { length: 256 }, common.mustSucceed((key) => {
1091cb0ef41Sopenharmony_ci    assert(key);
1101cb0ef41Sopenharmony_ci    const keybuf = key.export();
1111cb0ef41Sopenharmony_ci    assert.strictEqual(keybuf.byteLength, 256 / 8);
1121cb0ef41Sopenharmony_ci  }));
1131cb0ef41Sopenharmony_ci}
1141cb0ef41Sopenharmony_ci
1151cb0ef41Sopenharmony_ci{
1161cb0ef41Sopenharmony_ci  const key = generateKeySync('hmac', { length: 123 });
1171cb0ef41Sopenharmony_ci  assert(key);
1181cb0ef41Sopenharmony_ci  const keybuf = key.export();
1191cb0ef41Sopenharmony_ci  assert.strictEqual(keybuf.byteLength, Math.floor(123 / 8));
1201cb0ef41Sopenharmony_ci
1211cb0ef41Sopenharmony_ci  generateKey('hmac', { length: 123 }, common.mustSucceed((key) => {
1221cb0ef41Sopenharmony_ci    assert(key);
1231cb0ef41Sopenharmony_ci    const keybuf = key.export();
1241cb0ef41Sopenharmony_ci    assert.strictEqual(keybuf.byteLength, Math.floor(123 / 8));
1251cb0ef41Sopenharmony_ci  }));
1261cb0ef41Sopenharmony_ci}
1271cb0ef41Sopenharmony_ci
1281cb0ef41Sopenharmony_ciassert.throws(
1291cb0ef41Sopenharmony_ci  () => generateKey('unknown', { length: 123 }, common.mustNotCall()), {
1301cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_ARG_VALUE',
1311cb0ef41Sopenharmony_ci    message: /The argument 'type' must be a supported key type/
1321cb0ef41Sopenharmony_ci  });
1331cb0ef41Sopenharmony_ci
1341cb0ef41Sopenharmony_ciassert.throws(() => generateKeySync('unknown', { length: 123 }), {
1351cb0ef41Sopenharmony_ci  code: 'ERR_INVALID_ARG_VALUE',
1361cb0ef41Sopenharmony_ci  message: /The argument 'type' must be a supported key type/
1371cb0ef41Sopenharmony_ci});
138