11cb0ef41Sopenharmony_ci// Flags: --expose-internals
21cb0ef41Sopenharmony_ci'use strict';
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ciconst common = require('../common');
51cb0ef41Sopenharmony_ciif (!common.hasCrypto)
61cb0ef41Sopenharmony_ci  common.skip('missing crypto');
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ciconst assert = require('assert');
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ciconst webidl = require('internal/crypto/webidl');
111cb0ef41Sopenharmony_ciconst { subtle } = require('node:crypto').webcrypto;
121cb0ef41Sopenharmony_ciconst { generateKeySync } = require('crypto');
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ciconst { converters } = webidl;
151cb0ef41Sopenharmony_ciconst prefix = "Failed to execute 'fn' on 'interface'";
161cb0ef41Sopenharmony_ciconst context = '1st argument';
171cb0ef41Sopenharmony_ciconst opts = { prefix, context };
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ci// Required arguments.length
201cb0ef41Sopenharmony_ci{
211cb0ef41Sopenharmony_ci  assert.throws(() => webidl.requiredArguments(0, 3, { prefix }), {
221cb0ef41Sopenharmony_ci    code: 'ERR_MISSING_ARGS',
231cb0ef41Sopenharmony_ci    name: 'TypeError',
241cb0ef41Sopenharmony_ci    message: `${prefix}: 3 arguments required, but only 0 present.`
251cb0ef41Sopenharmony_ci  });
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci  assert.throws(() => webidl.requiredArguments(0, 1, { prefix }), {
281cb0ef41Sopenharmony_ci    code: 'ERR_MISSING_ARGS',
291cb0ef41Sopenharmony_ci    name: 'TypeError',
301cb0ef41Sopenharmony_ci    message: `${prefix}: 1 argument required, but only 0 present.`
311cb0ef41Sopenharmony_ci  });
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci  // Does not throw when extra are added
341cb0ef41Sopenharmony_ci  webidl.requiredArguments(4, 3, { prefix });
351cb0ef41Sopenharmony_ci}
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ci// boolean
381cb0ef41Sopenharmony_ci{
391cb0ef41Sopenharmony_ci  assert.strictEqual(converters.boolean(0), false);
401cb0ef41Sopenharmony_ci  assert.strictEqual(converters.boolean(NaN), false);
411cb0ef41Sopenharmony_ci  assert.strictEqual(converters.boolean(undefined), false);
421cb0ef41Sopenharmony_ci  assert.strictEqual(converters.boolean(null), false);
431cb0ef41Sopenharmony_ci  assert.strictEqual(converters.boolean(false), false);
441cb0ef41Sopenharmony_ci  assert.strictEqual(converters.boolean(''), false);
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci  assert.strictEqual(converters.boolean(1), true);
471cb0ef41Sopenharmony_ci  assert.strictEqual(converters.boolean(Number.POSITIVE_INFINITY), true);
481cb0ef41Sopenharmony_ci  assert.strictEqual(converters.boolean(Number.NEGATIVE_INFINITY), true);
491cb0ef41Sopenharmony_ci  assert.strictEqual(converters.boolean('1'), true);
501cb0ef41Sopenharmony_ci  assert.strictEqual(converters.boolean('0'), true);
511cb0ef41Sopenharmony_ci  assert.strictEqual(converters.boolean('false'), true);
521cb0ef41Sopenharmony_ci  assert.strictEqual(converters.boolean(function() {}), true);
531cb0ef41Sopenharmony_ci  assert.strictEqual(converters.boolean(Symbol()), true);
541cb0ef41Sopenharmony_ci  assert.strictEqual(converters.boolean([]), true);
551cb0ef41Sopenharmony_ci  assert.strictEqual(converters.boolean({}), true);
561cb0ef41Sopenharmony_ci}
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ci// int conversion
591cb0ef41Sopenharmony_ci// https://webidl.spec.whatwg.org/#abstract-opdef-converttoint
601cb0ef41Sopenharmony_ci{
611cb0ef41Sopenharmony_ci  for (const [converter, max] of [
621cb0ef41Sopenharmony_ci    [converters.octet, Math.pow(2, 8) - 1],
631cb0ef41Sopenharmony_ci    [converters['unsigned short'], Math.pow(2, 16) - 1],
641cb0ef41Sopenharmony_ci    [converters['unsigned long'], Math.pow(2, 32) - 1],
651cb0ef41Sopenharmony_ci  ]) {
661cb0ef41Sopenharmony_ci    assert.strictEqual(converter(0), 0);
671cb0ef41Sopenharmony_ci    assert.strictEqual(converter(max), max);
681cb0ef41Sopenharmony_ci    assert.strictEqual(converter('' + 0), 0);
691cb0ef41Sopenharmony_ci    assert.strictEqual(converter('' + max), max);
701cb0ef41Sopenharmony_ci    assert.strictEqual(converter(3), 3);
711cb0ef41Sopenharmony_ci    assert.strictEqual(converter('' + 3), 3);
721cb0ef41Sopenharmony_ci    assert.strictEqual(converter(3.1), 3);
731cb0ef41Sopenharmony_ci    assert.strictEqual(converter(3.7), 3);
741cb0ef41Sopenharmony_ci
751cb0ef41Sopenharmony_ci    assert.strictEqual(converter(max + 1), 0);
761cb0ef41Sopenharmony_ci    assert.strictEqual(converter(max + 2), 1);
771cb0ef41Sopenharmony_ci    assert.throws(() => converter(max + 1, { ...opts, enforceRange: true }), {
781cb0ef41Sopenharmony_ci      name: 'TypeError',
791cb0ef41Sopenharmony_ci      code: 'ERR_OUT_OF_RANGE',
801cb0ef41Sopenharmony_ci      message: `${prefix}: ${context} is outside the expected range of 0 to ${max}.`,
811cb0ef41Sopenharmony_ci    });
821cb0ef41Sopenharmony_ci
831cb0ef41Sopenharmony_ci    assert.strictEqual(converter({}), 0);
841cb0ef41Sopenharmony_ci    assert.strictEqual(converter(NaN), 0);
851cb0ef41Sopenharmony_ci    assert.strictEqual(converter(false), 0);
861cb0ef41Sopenharmony_ci    assert.strictEqual(converter(true), 1);
871cb0ef41Sopenharmony_ci    assert.strictEqual(converter('1'), 1);
881cb0ef41Sopenharmony_ci    assert.strictEqual(converter('0'), 0);
891cb0ef41Sopenharmony_ci    assert.strictEqual(converter('{}'), 0);
901cb0ef41Sopenharmony_ci    assert.strictEqual(converter({}), 0);
911cb0ef41Sopenharmony_ci    assert.strictEqual(converter([]), 0);
921cb0ef41Sopenharmony_ci    assert.strictEqual(converter(function() {}), 0);
931cb0ef41Sopenharmony_ci
941cb0ef41Sopenharmony_ci    assert.throws(() => converter(Symbol(), opts), {
951cb0ef41Sopenharmony_ci      name: 'TypeError',
961cb0ef41Sopenharmony_ci      code: 'ERR_INVALID_ARG_TYPE',
971cb0ef41Sopenharmony_ci      message: `${prefix}: ${context} is a Symbol and cannot be converted to a number.`
981cb0ef41Sopenharmony_ci    });
991cb0ef41Sopenharmony_ci    assert.throws(() => converter(0n, opts), {
1001cb0ef41Sopenharmony_ci      name: 'TypeError',
1011cb0ef41Sopenharmony_ci      code: 'ERR_INVALID_ARG_TYPE',
1021cb0ef41Sopenharmony_ci      message: `${prefix}: ${context} is a BigInt and cannot be converted to a number.`
1031cb0ef41Sopenharmony_ci    });
1041cb0ef41Sopenharmony_ci  }
1051cb0ef41Sopenharmony_ci}
1061cb0ef41Sopenharmony_ci
1071cb0ef41Sopenharmony_ci// DOMString
1081cb0ef41Sopenharmony_ci{
1091cb0ef41Sopenharmony_ci  assert.strictEqual(converters.DOMString(1), '1');
1101cb0ef41Sopenharmony_ci  assert.strictEqual(converters.DOMString(1n), '1');
1111cb0ef41Sopenharmony_ci  assert.strictEqual(converters.DOMString(false), 'false');
1121cb0ef41Sopenharmony_ci  assert.strictEqual(converters.DOMString(true), 'true');
1131cb0ef41Sopenharmony_ci  assert.strictEqual(converters.DOMString(undefined), 'undefined');
1141cb0ef41Sopenharmony_ci  assert.strictEqual(converters.DOMString(NaN), 'NaN');
1151cb0ef41Sopenharmony_ci  assert.strictEqual(converters.DOMString({}), '[object Object]');
1161cb0ef41Sopenharmony_ci  assert.strictEqual(converters.DOMString({ foo: 'bar' }), '[object Object]');
1171cb0ef41Sopenharmony_ci  assert.strictEqual(converters.DOMString([]), '');
1181cb0ef41Sopenharmony_ci  assert.strictEqual(converters.DOMString([1, 2]), '1,2');
1191cb0ef41Sopenharmony_ci
1201cb0ef41Sopenharmony_ci  assert.throws(() => converters.DOMString(Symbol(), opts), {
1211cb0ef41Sopenharmony_ci    name: 'TypeError',
1221cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_ARG_TYPE',
1231cb0ef41Sopenharmony_ci    message: `${prefix}: ${context} is a Symbol and cannot be converted to a string.`
1241cb0ef41Sopenharmony_ci  });
1251cb0ef41Sopenharmony_ci}
1261cb0ef41Sopenharmony_ci
1271cb0ef41Sopenharmony_ci// object
1281cb0ef41Sopenharmony_ci{
1291cb0ef41Sopenharmony_ci  for (const good of [{}, [], new Array(), function() {}]) {
1301cb0ef41Sopenharmony_ci    assert.deepStrictEqual(converters.object(good), good);
1311cb0ef41Sopenharmony_ci  }
1321cb0ef41Sopenharmony_ci
1331cb0ef41Sopenharmony_ci  for (const bad of [undefined, null, NaN, false, true, 0, 1, '', 'foo', Symbol(), 9n]) {
1341cb0ef41Sopenharmony_ci    assert.throws(() => converters.object(bad, opts), {
1351cb0ef41Sopenharmony_ci      name: 'TypeError',
1361cb0ef41Sopenharmony_ci      code: 'ERR_INVALID_ARG_TYPE',
1371cb0ef41Sopenharmony_ci      message: `${prefix}: ${context} is not an object.`
1381cb0ef41Sopenharmony_ci    });
1391cb0ef41Sopenharmony_ci  }
1401cb0ef41Sopenharmony_ci}
1411cb0ef41Sopenharmony_ci
1421cb0ef41Sopenharmony_ci// Uint8Array
1431cb0ef41Sopenharmony_ci{
1441cb0ef41Sopenharmony_ci  for (const good of [Buffer.alloc(0), new Uint8Array()]) {
1451cb0ef41Sopenharmony_ci    assert.deepStrictEqual(converters.Uint8Array(good), good);
1461cb0ef41Sopenharmony_ci  }
1471cb0ef41Sopenharmony_ci
1481cb0ef41Sopenharmony_ci  for (const bad of [new ArrayBuffer(), new SharedArrayBuffer(), [], null, 'foo', undefined, true]) {
1491cb0ef41Sopenharmony_ci    assert.throws(() => converters.Uint8Array(bad, opts), {
1501cb0ef41Sopenharmony_ci      name: 'TypeError',
1511cb0ef41Sopenharmony_ci      code: 'ERR_INVALID_ARG_TYPE',
1521cb0ef41Sopenharmony_ci      message: `${prefix}: ${context} is not an Uint8Array object.`
1531cb0ef41Sopenharmony_ci    });
1541cb0ef41Sopenharmony_ci  }
1551cb0ef41Sopenharmony_ci
1561cb0ef41Sopenharmony_ci  assert.throws(() => converters.Uint8Array(new Uint8Array(new SharedArrayBuffer()), opts), {
1571cb0ef41Sopenharmony_ci    name: 'TypeError',
1581cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_ARG_TYPE',
1591cb0ef41Sopenharmony_ci    message: `${prefix}: ${context} is a view on a SharedArrayBuffer, which is not allowed.`
1601cb0ef41Sopenharmony_ci  });
1611cb0ef41Sopenharmony_ci}
1621cb0ef41Sopenharmony_ci
1631cb0ef41Sopenharmony_ci// BufferSource
1641cb0ef41Sopenharmony_ci{
1651cb0ef41Sopenharmony_ci  for (const good of [
1661cb0ef41Sopenharmony_ci    Buffer.alloc(0),
1671cb0ef41Sopenharmony_ci    new Uint8Array(),
1681cb0ef41Sopenharmony_ci    new ArrayBuffer(),
1691cb0ef41Sopenharmony_ci    new DataView(new ArrayBuffer()),
1701cb0ef41Sopenharmony_ci    new BigInt64Array(),
1711cb0ef41Sopenharmony_ci    new BigUint64Array(),
1721cb0ef41Sopenharmony_ci    new Float32Array(),
1731cb0ef41Sopenharmony_ci    new Float64Array(),
1741cb0ef41Sopenharmony_ci    new Int8Array(),
1751cb0ef41Sopenharmony_ci    new Int16Array(),
1761cb0ef41Sopenharmony_ci    new Int32Array(),
1771cb0ef41Sopenharmony_ci    new Uint8ClampedArray(),
1781cb0ef41Sopenharmony_ci    new Uint16Array(),
1791cb0ef41Sopenharmony_ci    new Uint32Array(),
1801cb0ef41Sopenharmony_ci  ]) {
1811cb0ef41Sopenharmony_ci    assert.deepStrictEqual(converters.BufferSource(good), good);
1821cb0ef41Sopenharmony_ci  }
1831cb0ef41Sopenharmony_ci
1841cb0ef41Sopenharmony_ci  for (const bad of [new SharedArrayBuffer(), [], null, 'foo', undefined, true]) {
1851cb0ef41Sopenharmony_ci    assert.throws(() => converters.BufferSource(bad, opts), {
1861cb0ef41Sopenharmony_ci      name: 'TypeError',
1871cb0ef41Sopenharmony_ci      code: 'ERR_INVALID_ARG_TYPE',
1881cb0ef41Sopenharmony_ci      message: `${prefix}: ${context} is not instance of ArrayBuffer, Buffer, TypedArray, or DataView.`
1891cb0ef41Sopenharmony_ci    });
1901cb0ef41Sopenharmony_ci  }
1911cb0ef41Sopenharmony_ci
1921cb0ef41Sopenharmony_ci  assert.throws(() => converters.BufferSource(new Uint8Array(new SharedArrayBuffer()), opts), {
1931cb0ef41Sopenharmony_ci    name: 'TypeError',
1941cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_ARG_TYPE',
1951cb0ef41Sopenharmony_ci    message: `${prefix}: ${context} is a view on a SharedArrayBuffer, which is not allowed.`
1961cb0ef41Sopenharmony_ci  });
1971cb0ef41Sopenharmony_ci}
1981cb0ef41Sopenharmony_ci
1991cb0ef41Sopenharmony_ci// CryptoKey
2001cb0ef41Sopenharmony_ci{
2011cb0ef41Sopenharmony_ci
2021cb0ef41Sopenharmony_ci  subtle.generateKey({ name: 'AES-CBC', length: 128 }, false, ['encrypt']).then((key) => {
2031cb0ef41Sopenharmony_ci    assert.deepStrictEqual(converters.CryptoKey(key), key);
2041cb0ef41Sopenharmony_ci  }).then(common.mustCall());
2051cb0ef41Sopenharmony_ci
2061cb0ef41Sopenharmony_ci  for (const bad of [
2071cb0ef41Sopenharmony_ci    generateKeySync('aes', { length: 128 }),
2081cb0ef41Sopenharmony_ci    undefined, null, 1, {}, Symbol(), true, false, [],
2091cb0ef41Sopenharmony_ci  ]) {
2101cb0ef41Sopenharmony_ci    assert.throws(() => converters.CryptoKey(bad, opts), {
2111cb0ef41Sopenharmony_ci      name: 'TypeError',
2121cb0ef41Sopenharmony_ci      code: 'ERR_INVALID_ARG_TYPE',
2131cb0ef41Sopenharmony_ci      message: `${prefix}: ${context} is not of type CryptoKey.`
2141cb0ef41Sopenharmony_ci    });
2151cb0ef41Sopenharmony_ci  }
2161cb0ef41Sopenharmony_ci}
2171cb0ef41Sopenharmony_ci
2181cb0ef41Sopenharmony_ci// AlgorithmIdentifier (Union for (object or DOMString))
2191cb0ef41Sopenharmony_ci{
2201cb0ef41Sopenharmony_ci  assert.strictEqual(converters.AlgorithmIdentifier('foo'), 'foo');
2211cb0ef41Sopenharmony_ci  assert.deepStrictEqual(converters.AlgorithmIdentifier({ name: 'foo' }), { name: 'foo' });
2221cb0ef41Sopenharmony_ci}
2231cb0ef41Sopenharmony_ci
2241cb0ef41Sopenharmony_ci// JsonWebKey
2251cb0ef41Sopenharmony_ci{
2261cb0ef41Sopenharmony_ci  for (const good of [
2271cb0ef41Sopenharmony_ci    {},
2281cb0ef41Sopenharmony_ci    { use: 'sig' },
2291cb0ef41Sopenharmony_ci    { key_ops: ['sign'] },
2301cb0ef41Sopenharmony_ci    { ext: true },
2311cb0ef41Sopenharmony_ci    { oth: [] },
2321cb0ef41Sopenharmony_ci    { oth: [{ r: '', d: '', t: '' }] },
2331cb0ef41Sopenharmony_ci  ]) {
2341cb0ef41Sopenharmony_ci    assert.deepStrictEqual(converters.JsonWebKey(good), good);
2351cb0ef41Sopenharmony_ci    assert.deepStrictEqual(converters.JsonWebKey({ ...good, filtered: 'out' }), good);
2361cb0ef41Sopenharmony_ci  }
2371cb0ef41Sopenharmony_ci}
2381cb0ef41Sopenharmony_ci
2391cb0ef41Sopenharmony_ci// KeyFormat
2401cb0ef41Sopenharmony_ci{
2411cb0ef41Sopenharmony_ci  for (const good of ['jwk', 'spki', 'pkcs8', 'raw']) {
2421cb0ef41Sopenharmony_ci    assert.strictEqual(converters.KeyFormat(good), good);
2431cb0ef41Sopenharmony_ci  }
2441cb0ef41Sopenharmony_ci
2451cb0ef41Sopenharmony_ci  for (const bad of ['foo', 1, false]) {
2461cb0ef41Sopenharmony_ci    assert.throws(() => converters.KeyFormat(bad, opts), {
2471cb0ef41Sopenharmony_ci      name: 'TypeError',
2481cb0ef41Sopenharmony_ci      code: 'ERR_INVALID_ARG_VALUE',
2491cb0ef41Sopenharmony_ci      message: `${prefix}: ${context} value '${bad}' is not a valid enum value of type KeyFormat.`,
2501cb0ef41Sopenharmony_ci    });
2511cb0ef41Sopenharmony_ci  }
2521cb0ef41Sopenharmony_ci}
2531cb0ef41Sopenharmony_ci
2541cb0ef41Sopenharmony_ci// KeyUsage
2551cb0ef41Sopenharmony_ci{
2561cb0ef41Sopenharmony_ci  for (const good of [
2571cb0ef41Sopenharmony_ci    'encrypt',
2581cb0ef41Sopenharmony_ci    'decrypt',
2591cb0ef41Sopenharmony_ci    'sign',
2601cb0ef41Sopenharmony_ci    'verify',
2611cb0ef41Sopenharmony_ci    'deriveKey',
2621cb0ef41Sopenharmony_ci    'deriveBits',
2631cb0ef41Sopenharmony_ci    'wrapKey',
2641cb0ef41Sopenharmony_ci    'unwrapKey',
2651cb0ef41Sopenharmony_ci  ]) {
2661cb0ef41Sopenharmony_ci    assert.strictEqual(converters.KeyUsage(good), good);
2671cb0ef41Sopenharmony_ci  }
2681cb0ef41Sopenharmony_ci
2691cb0ef41Sopenharmony_ci  for (const bad of ['foo', 1, false]) {
2701cb0ef41Sopenharmony_ci    assert.throws(() => converters.KeyUsage(bad, opts), {
2711cb0ef41Sopenharmony_ci      name: 'TypeError',
2721cb0ef41Sopenharmony_ci      code: 'ERR_INVALID_ARG_VALUE',
2731cb0ef41Sopenharmony_ci      message: `${prefix}: ${context} value '${bad}' is not a valid enum value of type KeyUsage.`,
2741cb0ef41Sopenharmony_ci    });
2751cb0ef41Sopenharmony_ci  }
2761cb0ef41Sopenharmony_ci}
2771cb0ef41Sopenharmony_ci
2781cb0ef41Sopenharmony_ci// Algorithm
2791cb0ef41Sopenharmony_ci{
2801cb0ef41Sopenharmony_ci  const good = { name: 'RSA-PSS' };
2811cb0ef41Sopenharmony_ci  assert.deepStrictEqual(converters.Algorithm({ ...good, filtered: 'out' }, opts), good);
2821cb0ef41Sopenharmony_ci
2831cb0ef41Sopenharmony_ci  assert.throws(() => converters.Algorithm({}, opts), {
2841cb0ef41Sopenharmony_ci    name: 'TypeError',
2851cb0ef41Sopenharmony_ci    code: 'ERR_MISSING_OPTION',
2861cb0ef41Sopenharmony_ci    message: `${prefix}: ${context} can not be converted to 'Algorithm' because 'name' is required in 'Algorithm'.`,
2871cb0ef41Sopenharmony_ci  });
2881cb0ef41Sopenharmony_ci}
2891cb0ef41Sopenharmony_ci
2901cb0ef41Sopenharmony_ci// RsaHashedKeyGenParams
2911cb0ef41Sopenharmony_ci{
2921cb0ef41Sopenharmony_ci  for (const good of [
2931cb0ef41Sopenharmony_ci    {
2941cb0ef41Sopenharmony_ci      name: 'RSA-OAEP',
2951cb0ef41Sopenharmony_ci      hash: { name: 'SHA-1' },
2961cb0ef41Sopenharmony_ci      modulusLength: 2048,
2971cb0ef41Sopenharmony_ci      publicExponent: new Uint8Array([1, 0, 1]),
2981cb0ef41Sopenharmony_ci    },
2991cb0ef41Sopenharmony_ci    {
3001cb0ef41Sopenharmony_ci      name: 'RSA-OAEP',
3011cb0ef41Sopenharmony_ci      hash: 'SHA-1',
3021cb0ef41Sopenharmony_ci      modulusLength: 2048,
3031cb0ef41Sopenharmony_ci      publicExponent: new Uint8Array([1, 0, 1]),
3041cb0ef41Sopenharmony_ci    },
3051cb0ef41Sopenharmony_ci  ]) {
3061cb0ef41Sopenharmony_ci    assert.deepStrictEqual(converters.RsaHashedKeyGenParams({ ...good, filtered: 'out' }, opts), good);
3071cb0ef41Sopenharmony_ci    for (const required of ['hash', 'publicExponent', 'modulusLength']) {
3081cb0ef41Sopenharmony_ci      assert.throws(() => converters.RsaHashedKeyGenParams({ ...good, [required]: undefined }, opts), {
3091cb0ef41Sopenharmony_ci        name: 'TypeError',
3101cb0ef41Sopenharmony_ci        code: 'ERR_MISSING_OPTION',
3111cb0ef41Sopenharmony_ci        message: `${prefix}: ${context} can not be converted to 'RsaHashedKeyGenParams' because '${required}' is required in 'RsaHashedKeyGenParams'.`,
3121cb0ef41Sopenharmony_ci      });
3131cb0ef41Sopenharmony_ci    }
3141cb0ef41Sopenharmony_ci  }
3151cb0ef41Sopenharmony_ci}
3161cb0ef41Sopenharmony_ci
3171cb0ef41Sopenharmony_ci// RsaHashedImportParams
3181cb0ef41Sopenharmony_ci{
3191cb0ef41Sopenharmony_ci  for (const good of [
3201cb0ef41Sopenharmony_ci    { name: 'RSA-OAEP', hash: { name: 'SHA-1' } },
3211cb0ef41Sopenharmony_ci    { name: 'RSA-OAEP', hash: 'SHA-1' },
3221cb0ef41Sopenharmony_ci  ]) {
3231cb0ef41Sopenharmony_ci    assert.deepStrictEqual(converters.RsaHashedImportParams({ ...good, filtered: 'out' }, opts), good);
3241cb0ef41Sopenharmony_ci    assert.throws(() => converters.RsaHashedImportParams({ ...good, hash: undefined }, opts), {
3251cb0ef41Sopenharmony_ci      name: 'TypeError',
3261cb0ef41Sopenharmony_ci      code: 'ERR_MISSING_OPTION',
3271cb0ef41Sopenharmony_ci      message: `${prefix}: ${context} can not be converted to 'RsaHashedImportParams' because 'hash' is required in 'RsaHashedImportParams'.`,
3281cb0ef41Sopenharmony_ci    });
3291cb0ef41Sopenharmony_ci  }
3301cb0ef41Sopenharmony_ci}
3311cb0ef41Sopenharmony_ci
3321cb0ef41Sopenharmony_ci// RsaPssParams
3331cb0ef41Sopenharmony_ci{
3341cb0ef41Sopenharmony_ci  const good = { name: 'RSA-PSS', saltLength: 20 };
3351cb0ef41Sopenharmony_ci  assert.deepStrictEqual(converters.RsaPssParams({ ...good, filtered: 'out' }, opts), good);
3361cb0ef41Sopenharmony_ci
3371cb0ef41Sopenharmony_ci  assert.throws(() => converters.RsaPssParams({ ...good, saltLength: undefined }, opts), {
3381cb0ef41Sopenharmony_ci    name: 'TypeError',
3391cb0ef41Sopenharmony_ci    code: 'ERR_MISSING_OPTION',
3401cb0ef41Sopenharmony_ci    message: `${prefix}: ${context} can not be converted to 'RsaPssParams' because 'saltLength' is required in 'RsaPssParams'.`,
3411cb0ef41Sopenharmony_ci  });
3421cb0ef41Sopenharmony_ci}
3431cb0ef41Sopenharmony_ci
3441cb0ef41Sopenharmony_ci// RsaOaepParams
3451cb0ef41Sopenharmony_ci{
3461cb0ef41Sopenharmony_ci  for (const good of [{ name: 'RSA-OAEP' }, { name: 'RSA-OAEP', label: Buffer.alloc(0) }]) {
3471cb0ef41Sopenharmony_ci    assert.deepStrictEqual(converters.RsaOaepParams({ ...good, filtered: 'out' }, opts), good);
3481cb0ef41Sopenharmony_ci  }
3491cb0ef41Sopenharmony_ci}
3501cb0ef41Sopenharmony_ci
3511cb0ef41Sopenharmony_ci// EcKeyImportParams, EcKeyGenParams
3521cb0ef41Sopenharmony_ci{
3531cb0ef41Sopenharmony_ci  for (const name of ['EcKeyImportParams', 'EcKeyGenParams']) {
3541cb0ef41Sopenharmony_ci    const { [name]: converter } = converters;
3551cb0ef41Sopenharmony_ci
3561cb0ef41Sopenharmony_ci    const good = { name: 'ECDSA', namedCurve: 'P-256' };
3571cb0ef41Sopenharmony_ci    assert.deepStrictEqual(converter({ ...good, filtered: 'out' }, opts), good);
3581cb0ef41Sopenharmony_ci
3591cb0ef41Sopenharmony_ci    assert.throws(() => converter({ ...good, namedCurve: undefined }, opts), {
3601cb0ef41Sopenharmony_ci      name: 'TypeError',
3611cb0ef41Sopenharmony_ci      code: 'ERR_MISSING_OPTION',
3621cb0ef41Sopenharmony_ci      message: `${prefix}: ${context} can not be converted to '${name}' because 'namedCurve' is required in '${name}'.`,
3631cb0ef41Sopenharmony_ci    });
3641cb0ef41Sopenharmony_ci  }
3651cb0ef41Sopenharmony_ci}
3661cb0ef41Sopenharmony_ci
3671cb0ef41Sopenharmony_ci// EcdsaParams
3681cb0ef41Sopenharmony_ci{
3691cb0ef41Sopenharmony_ci  for (const good of [
3701cb0ef41Sopenharmony_ci    { name: 'ECDSA', hash: { name: 'SHA-1' } },
3711cb0ef41Sopenharmony_ci    { name: 'ECDSA', hash: 'SHA-1' },
3721cb0ef41Sopenharmony_ci  ]) {
3731cb0ef41Sopenharmony_ci    assert.deepStrictEqual(converters.EcdsaParams({ ...good, filtered: 'out' }, opts), good);
3741cb0ef41Sopenharmony_ci    assert.throws(() => converters.EcdsaParams({ ...good, hash: undefined }, opts), {
3751cb0ef41Sopenharmony_ci      name: 'TypeError',
3761cb0ef41Sopenharmony_ci      code: 'ERR_MISSING_OPTION',
3771cb0ef41Sopenharmony_ci      message: `${prefix}: ${context} can not be converted to 'EcdsaParams' because 'hash' is required in 'EcdsaParams'.`,
3781cb0ef41Sopenharmony_ci    });
3791cb0ef41Sopenharmony_ci  }
3801cb0ef41Sopenharmony_ci}
3811cb0ef41Sopenharmony_ci
3821cb0ef41Sopenharmony_ci// HmacKeyGenParams, HmacImportParams
3831cb0ef41Sopenharmony_ci{
3841cb0ef41Sopenharmony_ci  for (const name of ['HmacKeyGenParams', 'HmacImportParams']) {
3851cb0ef41Sopenharmony_ci    const { [name]: converter } = converters;
3861cb0ef41Sopenharmony_ci
3871cb0ef41Sopenharmony_ci    for (const good of [
3881cb0ef41Sopenharmony_ci      { name: 'HMAC', hash: { name: 'SHA-1' } },
3891cb0ef41Sopenharmony_ci      { name: 'HMAC', hash: { name: 'SHA-1' }, length: 20 },
3901cb0ef41Sopenharmony_ci      { name: 'HMAC', hash: 'SHA-1' },
3911cb0ef41Sopenharmony_ci      { name: 'HMAC', hash: 'SHA-1', length: 20 },
3921cb0ef41Sopenharmony_ci    ]) {
3931cb0ef41Sopenharmony_ci      assert.deepStrictEqual(converter({ ...good, filtered: 'out' }, opts), good);
3941cb0ef41Sopenharmony_ci      assert.throws(() => converter({ ...good, hash: undefined }, opts), {
3951cb0ef41Sopenharmony_ci        name: 'TypeError',
3961cb0ef41Sopenharmony_ci        code: 'ERR_MISSING_OPTION',
3971cb0ef41Sopenharmony_ci        message: `${prefix}: ${context} can not be converted to '${name}' because 'hash' is required in '${name}'.`,
3981cb0ef41Sopenharmony_ci      });
3991cb0ef41Sopenharmony_ci    }
4001cb0ef41Sopenharmony_ci  }
4011cb0ef41Sopenharmony_ci}
4021cb0ef41Sopenharmony_ci
4031cb0ef41Sopenharmony_ci// AesKeyGenParams, AesDerivedKeyParams
4041cb0ef41Sopenharmony_ci{
4051cb0ef41Sopenharmony_ci  for (const name of ['AesKeyGenParams', 'AesDerivedKeyParams']) {
4061cb0ef41Sopenharmony_ci    const { [name]: converter } = converters;
4071cb0ef41Sopenharmony_ci
4081cb0ef41Sopenharmony_ci    const good = { name: 'AES-CBC', length: 128 };
4091cb0ef41Sopenharmony_ci    assert.deepStrictEqual(converter({ ...good, filtered: 'out' }, opts), good);
4101cb0ef41Sopenharmony_ci
4111cb0ef41Sopenharmony_ci    assert.throws(() => converter({ ...good, length: undefined }, opts), {
4121cb0ef41Sopenharmony_ci      name: 'TypeError',
4131cb0ef41Sopenharmony_ci      code: 'ERR_MISSING_OPTION',
4141cb0ef41Sopenharmony_ci      message: `${prefix}: ${context} can not be converted to '${name}' because 'length' is required in '${name}'.`,
4151cb0ef41Sopenharmony_ci    });
4161cb0ef41Sopenharmony_ci  }
4171cb0ef41Sopenharmony_ci}
4181cb0ef41Sopenharmony_ci
4191cb0ef41Sopenharmony_ci// HkdfParams
4201cb0ef41Sopenharmony_ci{
4211cb0ef41Sopenharmony_ci  for (const good of [
4221cb0ef41Sopenharmony_ci    { name: 'HKDF', hash: { name: 'SHA-1' }, salt: Buffer.alloc(0), info: Buffer.alloc(0) },
4231cb0ef41Sopenharmony_ci    { name: 'HKDF', hash: 'SHA-1', salt: Buffer.alloc(0), info: Buffer.alloc(0) },
4241cb0ef41Sopenharmony_ci  ]) {
4251cb0ef41Sopenharmony_ci    assert.deepStrictEqual(converters.HkdfParams({ ...good, filtered: 'out' }, opts), good);
4261cb0ef41Sopenharmony_ci    for (const required of ['hash', 'salt', 'info']) {
4271cb0ef41Sopenharmony_ci      assert.throws(() => converters.HkdfParams({ ...good, [required]: undefined }, opts), {
4281cb0ef41Sopenharmony_ci        name: 'TypeError',
4291cb0ef41Sopenharmony_ci        code: 'ERR_MISSING_OPTION',
4301cb0ef41Sopenharmony_ci        message: `${prefix}: ${context} can not be converted to 'HkdfParams' because '${required}' is required in 'HkdfParams'.`,
4311cb0ef41Sopenharmony_ci      });
4321cb0ef41Sopenharmony_ci    }
4331cb0ef41Sopenharmony_ci  }
4341cb0ef41Sopenharmony_ci}
4351cb0ef41Sopenharmony_ci
4361cb0ef41Sopenharmony_ci// Pbkdf2Params
4371cb0ef41Sopenharmony_ci{
4381cb0ef41Sopenharmony_ci  for (const good of [
4391cb0ef41Sopenharmony_ci    { name: 'PBKDF2', hash: { name: 'SHA-1' }, iterations: 5, salt: Buffer.alloc(0) },
4401cb0ef41Sopenharmony_ci    { name: 'PBKDF2', hash: 'SHA-1', iterations: 5, salt: Buffer.alloc(0) },
4411cb0ef41Sopenharmony_ci  ]) {
4421cb0ef41Sopenharmony_ci    assert.deepStrictEqual(converters.Pbkdf2Params({ ...good, filtered: 'out' }, opts), good);
4431cb0ef41Sopenharmony_ci    for (const required of ['hash', 'iterations', 'salt']) {
4441cb0ef41Sopenharmony_ci      assert.throws(() => converters.Pbkdf2Params({ ...good, [required]: undefined }, opts), {
4451cb0ef41Sopenharmony_ci        name: 'TypeError',
4461cb0ef41Sopenharmony_ci        code: 'ERR_MISSING_OPTION',
4471cb0ef41Sopenharmony_ci        message: `${prefix}: ${context} can not be converted to 'Pbkdf2Params' because '${required}' is required in 'Pbkdf2Params'.`,
4481cb0ef41Sopenharmony_ci      });
4491cb0ef41Sopenharmony_ci    }
4501cb0ef41Sopenharmony_ci  }
4511cb0ef41Sopenharmony_ci}
4521cb0ef41Sopenharmony_ci
4531cb0ef41Sopenharmony_ci// AesCbcParams
4541cb0ef41Sopenharmony_ci{
4551cb0ef41Sopenharmony_ci  const good = { name: 'AES-CBC', iv: Buffer.alloc(0) };
4561cb0ef41Sopenharmony_ci  assert.deepStrictEqual(converters.AesCbcParams({ ...good, filtered: 'out' }, opts), good);
4571cb0ef41Sopenharmony_ci
4581cb0ef41Sopenharmony_ci  assert.throws(() => converters.AesCbcParams({ ...good, iv: undefined }, opts), {
4591cb0ef41Sopenharmony_ci    name: 'TypeError',
4601cb0ef41Sopenharmony_ci    code: 'ERR_MISSING_OPTION',
4611cb0ef41Sopenharmony_ci    message: `${prefix}: ${context} can not be converted to 'AesCbcParams' because 'iv' is required in 'AesCbcParams'.`,
4621cb0ef41Sopenharmony_ci  });
4631cb0ef41Sopenharmony_ci}
4641cb0ef41Sopenharmony_ci
4651cb0ef41Sopenharmony_ci// AesGcmParams
4661cb0ef41Sopenharmony_ci{
4671cb0ef41Sopenharmony_ci  for (const good of [
4681cb0ef41Sopenharmony_ci    { name: 'AES-GCM', iv: Buffer.alloc(0) },
4691cb0ef41Sopenharmony_ci    { name: 'AES-GCM', iv: Buffer.alloc(0), tagLength: 16 },
4701cb0ef41Sopenharmony_ci    { name: 'AES-GCM', iv: Buffer.alloc(0), tagLength: 16, additionalData: Buffer.alloc(0) },
4711cb0ef41Sopenharmony_ci  ]) {
4721cb0ef41Sopenharmony_ci    assert.deepStrictEqual(converters.AesGcmParams({ ...good, filtered: 'out' }, opts), good);
4731cb0ef41Sopenharmony_ci
4741cb0ef41Sopenharmony_ci    assert.throws(() => converters.AesGcmParams({ ...good, iv: undefined }, opts), {
4751cb0ef41Sopenharmony_ci      name: 'TypeError',
4761cb0ef41Sopenharmony_ci      code: 'ERR_MISSING_OPTION',
4771cb0ef41Sopenharmony_ci      message: `${prefix}: ${context} can not be converted to 'AesGcmParams' because 'iv' is required in 'AesGcmParams'.`,
4781cb0ef41Sopenharmony_ci    });
4791cb0ef41Sopenharmony_ci  }
4801cb0ef41Sopenharmony_ci}
4811cb0ef41Sopenharmony_ci
4821cb0ef41Sopenharmony_ci// AesCtrParams
4831cb0ef41Sopenharmony_ci{
4841cb0ef41Sopenharmony_ci  const good = { name: 'AES-CTR', counter: Buffer.alloc(0), length: 20 };
4851cb0ef41Sopenharmony_ci  assert.deepStrictEqual(converters.AesCtrParams({ ...good, filtered: 'out' }, opts), good);
4861cb0ef41Sopenharmony_ci
4871cb0ef41Sopenharmony_ci  for (const required of ['counter', 'length']) {
4881cb0ef41Sopenharmony_ci    assert.throws(() => converters.AesCtrParams({ ...good, [required]: undefined }, opts), {
4891cb0ef41Sopenharmony_ci      name: 'TypeError',
4901cb0ef41Sopenharmony_ci      code: 'ERR_MISSING_OPTION',
4911cb0ef41Sopenharmony_ci      message: `${prefix}: ${context} can not be converted to 'AesCtrParams' because '${required}' is required in 'AesCtrParams'.`,
4921cb0ef41Sopenharmony_ci    });
4931cb0ef41Sopenharmony_ci  }
4941cb0ef41Sopenharmony_ci}
4951cb0ef41Sopenharmony_ci
4961cb0ef41Sopenharmony_ci// EcdhKeyDeriveParams
4971cb0ef41Sopenharmony_ci{
4981cb0ef41Sopenharmony_ci  subtle.generateKey({ name: 'ECDH', namedCurve: 'P-256' }, false, ['deriveBits']).then((kp) => {
4991cb0ef41Sopenharmony_ci    const good = { name: 'ECDH', public: kp.publicKey };
5001cb0ef41Sopenharmony_ci    assert.deepStrictEqual(converters.EcdhKeyDeriveParams({ ...good, filtered: 'out' }, opts), good);
5011cb0ef41Sopenharmony_ci
5021cb0ef41Sopenharmony_ci    assert.throws(() => converters.EcdhKeyDeriveParams({ ...good, public: undefined }, opts), {
5031cb0ef41Sopenharmony_ci      name: 'TypeError',
5041cb0ef41Sopenharmony_ci      code: 'ERR_MISSING_OPTION',
5051cb0ef41Sopenharmony_ci      message: `${prefix}: ${context} can not be converted to 'EcdhKeyDeriveParams' because 'public' is required in 'EcdhKeyDeriveParams'.`,
5061cb0ef41Sopenharmony_ci    });
5071cb0ef41Sopenharmony_ci  }).then(common.mustCall());
5081cb0ef41Sopenharmony_ci}
5091cb0ef41Sopenharmony_ci
5101cb0ef41Sopenharmony_ci// Ed448Params
5111cb0ef41Sopenharmony_ci{
5121cb0ef41Sopenharmony_ci  for (const good of [
5131cb0ef41Sopenharmony_ci    { name: 'Ed448', context: new Uint8Array() },
5141cb0ef41Sopenharmony_ci    { name: 'Ed448' },
5151cb0ef41Sopenharmony_ci  ]) {
5161cb0ef41Sopenharmony_ci    assert.deepStrictEqual(converters.Ed448Params({ ...good, filtered: 'out' }, opts), good);
5171cb0ef41Sopenharmony_ci  }
5181cb0ef41Sopenharmony_ci}
519