11cb0ef41Sopenharmony_ci// Flags: --experimental-global-webcrypto 21cb0ef41Sopenharmony_ci'use strict'; 31cb0ef41Sopenharmony_ci 41cb0ef41Sopenharmony_ciconst common = require('../common'); 51cb0ef41Sopenharmony_ci 61cb0ef41Sopenharmony_ciif (!common.hasCrypto) 71cb0ef41Sopenharmony_ci common.skip('missing crypto'); 81cb0ef41Sopenharmony_ci 91cb0ef41Sopenharmony_ciconst assert = require('assert'); 101cb0ef41Sopenharmony_ci 111cb0ef41Sopenharmony_ci// Test CryptoKey constructor 121cb0ef41Sopenharmony_ci{ 131cb0ef41Sopenharmony_ci assert.throws(() => new CryptoKey(), { 141cb0ef41Sopenharmony_ci name: 'TypeError', message: 'Illegal constructor', code: 'ERR_ILLEGAL_CONSTRUCTOR' 151cb0ef41Sopenharmony_ci }); 161cb0ef41Sopenharmony_ci} 171cb0ef41Sopenharmony_ci 181cb0ef41Sopenharmony_ci// Test SubtleCrypto constructor 191cb0ef41Sopenharmony_ci{ 201cb0ef41Sopenharmony_ci assert.throws(() => new SubtleCrypto(), { 211cb0ef41Sopenharmony_ci name: 'TypeError', message: 'Illegal constructor', code: 'ERR_ILLEGAL_CONSTRUCTOR' 221cb0ef41Sopenharmony_ci }); 231cb0ef41Sopenharmony_ci} 241cb0ef41Sopenharmony_ci 251cb0ef41Sopenharmony_ci// Test Crypto constructor 261cb0ef41Sopenharmony_ci{ 271cb0ef41Sopenharmony_ci assert.throws(() => new Crypto(), { 281cb0ef41Sopenharmony_ci name: 'TypeError', message: 'Illegal constructor', code: 'ERR_ILLEGAL_CONSTRUCTOR' 291cb0ef41Sopenharmony_ci }); 301cb0ef41Sopenharmony_ci} 311cb0ef41Sopenharmony_ci 321cb0ef41Sopenharmony_ciconst notCrypto = Reflect.construct(function() {}, [], Crypto); 331cb0ef41Sopenharmony_ciconst notSubtle = Reflect.construct(function() {}, [], SubtleCrypto); 341cb0ef41Sopenharmony_ci 351cb0ef41Sopenharmony_ci// Test Crypto.prototype.subtle 361cb0ef41Sopenharmony_ci{ 371cb0ef41Sopenharmony_ci assert.throws(() => notCrypto.subtle, { 381cb0ef41Sopenharmony_ci name: 'TypeError', code: 'ERR_INVALID_THIS', 391cb0ef41Sopenharmony_ci }); 401cb0ef41Sopenharmony_ci} 411cb0ef41Sopenharmony_ci 421cb0ef41Sopenharmony_ci// Test Crypto.prototype.randomUUID 431cb0ef41Sopenharmony_ci{ 441cb0ef41Sopenharmony_ci assert.throws(() => notCrypto.randomUUID(), { 451cb0ef41Sopenharmony_ci name: 'TypeError', code: 'ERR_INVALID_THIS', 461cb0ef41Sopenharmony_ci }); 471cb0ef41Sopenharmony_ci} 481cb0ef41Sopenharmony_ci 491cb0ef41Sopenharmony_ci// Test Crypto.prototype.getRandomValues 501cb0ef41Sopenharmony_ci{ 511cb0ef41Sopenharmony_ci assert.throws(() => notCrypto.getRandomValues(new Uint8Array(12)), { 521cb0ef41Sopenharmony_ci name: 'TypeError', code: 'ERR_INVALID_THIS', 531cb0ef41Sopenharmony_ci }); 541cb0ef41Sopenharmony_ci} 551cb0ef41Sopenharmony_ci 561cb0ef41Sopenharmony_ci// Test SubtleCrypto.prototype.encrypt 571cb0ef41Sopenharmony_ci{ 581cb0ef41Sopenharmony_ci assert.rejects(() => notSubtle.encrypt(), { 591cb0ef41Sopenharmony_ci name: 'TypeError', code: 'ERR_INVALID_THIS', 601cb0ef41Sopenharmony_ci }).then(common.mustCall()); 611cb0ef41Sopenharmony_ci} 621cb0ef41Sopenharmony_ci 631cb0ef41Sopenharmony_ci// Test SubtleCrypto.prototype.decrypt 641cb0ef41Sopenharmony_ci{ 651cb0ef41Sopenharmony_ci assert.rejects(() => notSubtle.decrypt(), { 661cb0ef41Sopenharmony_ci name: 'TypeError', code: 'ERR_INVALID_THIS', 671cb0ef41Sopenharmony_ci }).then(common.mustCall()); 681cb0ef41Sopenharmony_ci} 691cb0ef41Sopenharmony_ci 701cb0ef41Sopenharmony_ci// Test SubtleCrypto.prototype.sign 711cb0ef41Sopenharmony_ci{ 721cb0ef41Sopenharmony_ci assert.rejects(() => notSubtle.sign(), { 731cb0ef41Sopenharmony_ci name: 'TypeError', code: 'ERR_INVALID_THIS', 741cb0ef41Sopenharmony_ci }).then(common.mustCall()); 751cb0ef41Sopenharmony_ci} 761cb0ef41Sopenharmony_ci 771cb0ef41Sopenharmony_ci// Test SubtleCrypto.prototype.verify 781cb0ef41Sopenharmony_ci{ 791cb0ef41Sopenharmony_ci assert.rejects(() => notSubtle.verify(), { 801cb0ef41Sopenharmony_ci name: 'TypeError', code: 'ERR_INVALID_THIS', 811cb0ef41Sopenharmony_ci }).then(common.mustCall()); 821cb0ef41Sopenharmony_ci} 831cb0ef41Sopenharmony_ci 841cb0ef41Sopenharmony_ci// Test SubtleCrypto.prototype.digest 851cb0ef41Sopenharmony_ci{ 861cb0ef41Sopenharmony_ci assert.rejects(() => notSubtle.digest(), { 871cb0ef41Sopenharmony_ci name: 'TypeError', code: 'ERR_INVALID_THIS', 881cb0ef41Sopenharmony_ci }).then(common.mustCall()); 891cb0ef41Sopenharmony_ci} 901cb0ef41Sopenharmony_ci 911cb0ef41Sopenharmony_ci// Test SubtleCrypto.prototype.generateKey 921cb0ef41Sopenharmony_ci{ 931cb0ef41Sopenharmony_ci assert.rejects(() => notSubtle.generateKey(), { 941cb0ef41Sopenharmony_ci name: 'TypeError', code: 'ERR_INVALID_THIS', 951cb0ef41Sopenharmony_ci }).then(common.mustCall()); 961cb0ef41Sopenharmony_ci} 971cb0ef41Sopenharmony_ci 981cb0ef41Sopenharmony_ci// Test SubtleCrypto.prototype.deriveKey 991cb0ef41Sopenharmony_ci{ 1001cb0ef41Sopenharmony_ci assert.rejects(() => notSubtle.deriveKey(), { 1011cb0ef41Sopenharmony_ci name: 'TypeError', code: 'ERR_INVALID_THIS', 1021cb0ef41Sopenharmony_ci }).then(common.mustCall()); 1031cb0ef41Sopenharmony_ci} 1041cb0ef41Sopenharmony_ci 1051cb0ef41Sopenharmony_ci// Test SubtleCrypto.prototype.deriveBits 1061cb0ef41Sopenharmony_ci{ 1071cb0ef41Sopenharmony_ci assert.rejects(() => notSubtle.deriveBits(), { 1081cb0ef41Sopenharmony_ci name: 'TypeError', code: 'ERR_INVALID_THIS', 1091cb0ef41Sopenharmony_ci }).then(common.mustCall()); 1101cb0ef41Sopenharmony_ci} 1111cb0ef41Sopenharmony_ci 1121cb0ef41Sopenharmony_ci// Test SubtleCrypto.prototype.importKey 1131cb0ef41Sopenharmony_ci{ 1141cb0ef41Sopenharmony_ci assert.rejects(() => notSubtle.importKey(), { 1151cb0ef41Sopenharmony_ci name: 'TypeError', code: 'ERR_INVALID_THIS', 1161cb0ef41Sopenharmony_ci }).then(common.mustCall()); 1171cb0ef41Sopenharmony_ci} 1181cb0ef41Sopenharmony_ci 1191cb0ef41Sopenharmony_ci// Test SubtleCrypto.prototype.exportKey 1201cb0ef41Sopenharmony_ci{ 1211cb0ef41Sopenharmony_ci assert.rejects(() => notSubtle.exportKey(), { 1221cb0ef41Sopenharmony_ci name: 'TypeError', code: 'ERR_INVALID_THIS', 1231cb0ef41Sopenharmony_ci }).then(common.mustCall()); 1241cb0ef41Sopenharmony_ci} 1251cb0ef41Sopenharmony_ci 1261cb0ef41Sopenharmony_ci// Test SubtleCrypto.prototype.wrapKey 1271cb0ef41Sopenharmony_ci{ 1281cb0ef41Sopenharmony_ci assert.rejects(() => notSubtle.wrapKey(), { 1291cb0ef41Sopenharmony_ci name: 'TypeError', code: 'ERR_INVALID_THIS', 1301cb0ef41Sopenharmony_ci }).then(common.mustCall()); 1311cb0ef41Sopenharmony_ci} 1321cb0ef41Sopenharmony_ci 1331cb0ef41Sopenharmony_ci// Test SubtleCrypto.prototype.unwrapKey 1341cb0ef41Sopenharmony_ci{ 1351cb0ef41Sopenharmony_ci assert.rejects(() => notSubtle.unwrapKey(), { 1361cb0ef41Sopenharmony_ci name: 'TypeError', code: 'ERR_INVALID_THIS', 1371cb0ef41Sopenharmony_ci }).then(common.mustCall()); 1381cb0ef41Sopenharmony_ci} 1391cb0ef41Sopenharmony_ci 1401cb0ef41Sopenharmony_ci{ 1411cb0ef41Sopenharmony_ci globalThis.crypto.subtle.importKey( 1421cb0ef41Sopenharmony_ci 'raw', 1431cb0ef41Sopenharmony_ci globalThis.crypto.getRandomValues(new Uint8Array(4)), 1441cb0ef41Sopenharmony_ci 'PBKDF2', 1451cb0ef41Sopenharmony_ci false, 1461cb0ef41Sopenharmony_ci ['deriveKey'], 1471cb0ef41Sopenharmony_ci ).then((key) => { 1481cb0ef41Sopenharmony_ci globalThis.crypto.subtle.importKey = common.mustNotCall(); 1491cb0ef41Sopenharmony_ci return globalThis.crypto.subtle.deriveKey({ 1501cb0ef41Sopenharmony_ci name: 'PBKDF2', 1511cb0ef41Sopenharmony_ci hash: 'SHA-512', 1521cb0ef41Sopenharmony_ci salt: globalThis.crypto.getRandomValues(new Uint8Array()), 1531cb0ef41Sopenharmony_ci iterations: 5, 1541cb0ef41Sopenharmony_ci }, key, { 1551cb0ef41Sopenharmony_ci name: 'AES-GCM', 1561cb0ef41Sopenharmony_ci length: 256 1571cb0ef41Sopenharmony_ci }, true, ['encrypt', 'decrypt']); 1581cb0ef41Sopenharmony_ci }).then(common.mustCall()); 1591cb0ef41Sopenharmony_ci} 160