11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ciconst common = require('../common'); 41cb0ef41Sopenharmony_ciif (!common.hasCrypto) 51cb0ef41Sopenharmony_ci common.skip('missing crypto'); 61cb0ef41Sopenharmony_ci 71cb0ef41Sopenharmony_ciconst assert = require('assert'); 81cb0ef41Sopenharmony_ciconst tls = require('tls'); 91cb0ef41Sopenharmony_ci 101cb0ef41Sopenharmony_ciassert.throws( 111cb0ef41Sopenharmony_ci () => tls.createSecureContext({ ciphers: 1 }), 121cb0ef41Sopenharmony_ci { 131cb0ef41Sopenharmony_ci code: 'ERR_INVALID_ARG_TYPE', 141cb0ef41Sopenharmony_ci name: 'TypeError', 151cb0ef41Sopenharmony_ci message: 'The "options.ciphers" property must be of type string.' + 161cb0ef41Sopenharmony_ci ' Received type number (1)' 171cb0ef41Sopenharmony_ci }); 181cb0ef41Sopenharmony_ci 191cb0ef41Sopenharmony_ciassert.throws( 201cb0ef41Sopenharmony_ci () => tls.createServer({ ciphers: 1 }), 211cb0ef41Sopenharmony_ci { 221cb0ef41Sopenharmony_ci code: 'ERR_INVALID_ARG_TYPE', 231cb0ef41Sopenharmony_ci name: 'TypeError', 241cb0ef41Sopenharmony_ci message: 'The "options.ciphers" property must be of type string.' + 251cb0ef41Sopenharmony_ci ' Received type number (1)' 261cb0ef41Sopenharmony_ci }); 271cb0ef41Sopenharmony_ci 281cb0ef41Sopenharmony_ciassert.throws( 291cb0ef41Sopenharmony_ci () => tls.createSecureContext({ key: 'dummykey', passphrase: 1 }), 301cb0ef41Sopenharmony_ci { 311cb0ef41Sopenharmony_ci code: 'ERR_INVALID_ARG_TYPE', 321cb0ef41Sopenharmony_ci name: 'TypeError', 331cb0ef41Sopenharmony_ci message: /The "options\.passphrase" property must be of type string/ 341cb0ef41Sopenharmony_ci }); 351cb0ef41Sopenharmony_ci 361cb0ef41Sopenharmony_ciassert.throws( 371cb0ef41Sopenharmony_ci () => tls.createServer({ key: 'dummykey', passphrase: 1 }), 381cb0ef41Sopenharmony_ci { 391cb0ef41Sopenharmony_ci code: 'ERR_INVALID_ARG_TYPE', 401cb0ef41Sopenharmony_ci name: 'TypeError', 411cb0ef41Sopenharmony_ci message: /The "options\.passphrase" property must be of type string/ 421cb0ef41Sopenharmony_ci }); 431cb0ef41Sopenharmony_ci 441cb0ef41Sopenharmony_ciassert.throws( 451cb0ef41Sopenharmony_ci () => tls.createServer({ ecdhCurve: 1 }), 461cb0ef41Sopenharmony_ci { 471cb0ef41Sopenharmony_ci code: 'ERR_INVALID_ARG_TYPE', 481cb0ef41Sopenharmony_ci name: 'TypeError', 491cb0ef41Sopenharmony_ci message: /The "options\.ecdhCurve" property must be of type string/ 501cb0ef41Sopenharmony_ci }); 511cb0ef41Sopenharmony_ci 521cb0ef41Sopenharmony_ciassert.throws( 531cb0ef41Sopenharmony_ci () => tls.createServer({ handshakeTimeout: 'abcd' }), 541cb0ef41Sopenharmony_ci { 551cb0ef41Sopenharmony_ci code: 'ERR_INVALID_ARG_TYPE', 561cb0ef41Sopenharmony_ci name: 'TypeError', 571cb0ef41Sopenharmony_ci message: 'The "options.handshakeTimeout" property must be of type number.' + 581cb0ef41Sopenharmony_ci " Received type string ('abcd')" 591cb0ef41Sopenharmony_ci } 601cb0ef41Sopenharmony_ci); 611cb0ef41Sopenharmony_ci 621cb0ef41Sopenharmony_ciassert.throws( 631cb0ef41Sopenharmony_ci () => tls.createServer({ sessionTimeout: 'abcd' }), 641cb0ef41Sopenharmony_ci { 651cb0ef41Sopenharmony_ci code: 'ERR_INVALID_ARG_TYPE', 661cb0ef41Sopenharmony_ci name: 'TypeError', 671cb0ef41Sopenharmony_ci message: /The "options\.sessionTimeout" property must be of type number/ 681cb0ef41Sopenharmony_ci }); 691cb0ef41Sopenharmony_ci 701cb0ef41Sopenharmony_ciassert.throws( 711cb0ef41Sopenharmony_ci () => tls.createServer({ ticketKeys: 'abcd' }), 721cb0ef41Sopenharmony_ci { 731cb0ef41Sopenharmony_ci code: 'ERR_INVALID_ARG_TYPE', 741cb0ef41Sopenharmony_ci name: 'TypeError', 751cb0ef41Sopenharmony_ci message: /The "options\.ticketKeys" property must be an instance of/ 761cb0ef41Sopenharmony_ci }); 771cb0ef41Sopenharmony_ci 781cb0ef41Sopenharmony_ciassert.throws(() => tls.createServer({ ticketKeys: Buffer.alloc(0) }), { 791cb0ef41Sopenharmony_ci code: 'ERR_INVALID_ARG_VALUE', 801cb0ef41Sopenharmony_ci message: /The property 'options\.ticketKeys' must be exactly 48 bytes/ 811cb0ef41Sopenharmony_ci}); 821cb0ef41Sopenharmony_ci 831cb0ef41Sopenharmony_ciassert.throws( 841cb0ef41Sopenharmony_ci () => tls.createSecurePair({}), 851cb0ef41Sopenharmony_ci { 861cb0ef41Sopenharmony_ci message: 'context must be a SecureContext', 871cb0ef41Sopenharmony_ci code: 'ERR_TLS_INVALID_CONTEXT', 881cb0ef41Sopenharmony_ci name: 'TypeError', 891cb0ef41Sopenharmony_ci } 901cb0ef41Sopenharmony_ci); 911cb0ef41Sopenharmony_ci 921cb0ef41Sopenharmony_ci{ 931cb0ef41Sopenharmony_ci const buffer = Buffer.from('abcd'); 941cb0ef41Sopenharmony_ci const out = {}; 951cb0ef41Sopenharmony_ci tls.convertALPNProtocols(buffer, out); 961cb0ef41Sopenharmony_ci out.ALPNProtocols.write('efgh'); 971cb0ef41Sopenharmony_ci assert(buffer.equals(Buffer.from('abcd'))); 981cb0ef41Sopenharmony_ci assert(out.ALPNProtocols.equals(Buffer.from('efgh'))); 991cb0ef41Sopenharmony_ci} 1001cb0ef41Sopenharmony_ci 1011cb0ef41Sopenharmony_ci{ 1021cb0ef41Sopenharmony_ci const arrayBufferViewStr = 'abcd'; 1031cb0ef41Sopenharmony_ci const inputBuffer = Buffer.from(arrayBufferViewStr.repeat(8), 'utf8'); 1041cb0ef41Sopenharmony_ci for (const expectView of common.getArrayBufferViews(inputBuffer)) { 1051cb0ef41Sopenharmony_ci const out = {}; 1061cb0ef41Sopenharmony_ci const expected = Buffer.from(expectView.buffer.slice(), 1071cb0ef41Sopenharmony_ci expectView.byteOffset, 1081cb0ef41Sopenharmony_ci expectView.byteLength); 1091cb0ef41Sopenharmony_ci tls.convertALPNProtocols(expectView, out); 1101cb0ef41Sopenharmony_ci assert(out.ALPNProtocols.equals(expected)); 1111cb0ef41Sopenharmony_ci } 1121cb0ef41Sopenharmony_ci} 1131cb0ef41Sopenharmony_ci 1141cb0ef41Sopenharmony_ci{ 1151cb0ef41Sopenharmony_ci const protocols = [(new String('a')).repeat(500)]; 1161cb0ef41Sopenharmony_ci const out = {}; 1171cb0ef41Sopenharmony_ci assert.throws( 1181cb0ef41Sopenharmony_ci () => tls.convertALPNProtocols(protocols, out), 1191cb0ef41Sopenharmony_ci { 1201cb0ef41Sopenharmony_ci code: 'ERR_OUT_OF_RANGE', 1211cb0ef41Sopenharmony_ci message: 'The byte length of the protocol at index 0 exceeds the ' + 1221cb0ef41Sopenharmony_ci 'maximum length. It must be <= 255. Received 500' 1231cb0ef41Sopenharmony_ci } 1241cb0ef41Sopenharmony_ci ); 1251cb0ef41Sopenharmony_ci} 1261cb0ef41Sopenharmony_ci 1271cb0ef41Sopenharmony_ciassert.throws(() => { tls.createSecureContext({ minVersion: 'fhqwhgads' }); }, 1281cb0ef41Sopenharmony_ci { 1291cb0ef41Sopenharmony_ci code: 'ERR_TLS_INVALID_PROTOCOL_VERSION', 1301cb0ef41Sopenharmony_ci name: 'TypeError' 1311cb0ef41Sopenharmony_ci }); 1321cb0ef41Sopenharmony_ci 1331cb0ef41Sopenharmony_ciassert.throws(() => { tls.createSecureContext({ maxVersion: 'fhqwhgads' }); }, 1341cb0ef41Sopenharmony_ci { 1351cb0ef41Sopenharmony_ci code: 'ERR_TLS_INVALID_PROTOCOL_VERSION', 1361cb0ef41Sopenharmony_ci name: 'TypeError' 1371cb0ef41Sopenharmony_ci }); 138