11cb0ef41Sopenharmony_ci// Flags: --expose-internals 21cb0ef41Sopenharmony_ci'use strict'; 31cb0ef41Sopenharmony_ci 41cb0ef41Sopenharmony_ciconst common = require('../common'); 51cb0ef41Sopenharmony_ciconst { 61cb0ef41Sopenharmony_ci ok, 71cb0ef41Sopenharmony_ci strictEqual, 81cb0ef41Sopenharmony_ci throws, 91cb0ef41Sopenharmony_ci} = require('assert'); 101cb0ef41Sopenharmony_ciconst { 111cb0ef41Sopenharmony_ci SocketAddress, 121cb0ef41Sopenharmony_ci} = require('net'); 131cb0ef41Sopenharmony_ci 141cb0ef41Sopenharmony_ciconst { 151cb0ef41Sopenharmony_ci InternalSocketAddress, 161cb0ef41Sopenharmony_ci} = require('internal/socketaddress'); 171cb0ef41Sopenharmony_ciconst { internalBinding } = require('internal/test/binding'); 181cb0ef41Sopenharmony_ciconst { 191cb0ef41Sopenharmony_ci SocketAddress: _SocketAddress, 201cb0ef41Sopenharmony_ci AF_INET 211cb0ef41Sopenharmony_ci} = internalBinding('block_list'); 221cb0ef41Sopenharmony_ci 231cb0ef41Sopenharmony_ci{ 241cb0ef41Sopenharmony_ci const sa = new SocketAddress(); 251cb0ef41Sopenharmony_ci strictEqual(sa.address, '127.0.0.1'); 261cb0ef41Sopenharmony_ci strictEqual(sa.port, 0); 271cb0ef41Sopenharmony_ci strictEqual(sa.family, 'ipv4'); 281cb0ef41Sopenharmony_ci strictEqual(sa.flowlabel, 0); 291cb0ef41Sopenharmony_ci 301cb0ef41Sopenharmony_ci const mc = new MessageChannel(); 311cb0ef41Sopenharmony_ci mc.port1.onmessage = common.mustCall(({ data }) => { 321cb0ef41Sopenharmony_ci ok(SocketAddress.isSocketAddress(data)); 331cb0ef41Sopenharmony_ci 341cb0ef41Sopenharmony_ci strictEqual(data.address, '127.0.0.1'); 351cb0ef41Sopenharmony_ci strictEqual(data.port, 0); 361cb0ef41Sopenharmony_ci strictEqual(data.family, 'ipv4'); 371cb0ef41Sopenharmony_ci strictEqual(data.flowlabel, 0); 381cb0ef41Sopenharmony_ci 391cb0ef41Sopenharmony_ci mc.port1.close(); 401cb0ef41Sopenharmony_ci }); 411cb0ef41Sopenharmony_ci mc.port2.postMessage(sa); 421cb0ef41Sopenharmony_ci} 431cb0ef41Sopenharmony_ci 441cb0ef41Sopenharmony_ci{ 451cb0ef41Sopenharmony_ci const sa = new SocketAddress({}); 461cb0ef41Sopenharmony_ci strictEqual(sa.address, '127.0.0.1'); 471cb0ef41Sopenharmony_ci strictEqual(sa.port, 0); 481cb0ef41Sopenharmony_ci strictEqual(sa.family, 'ipv4'); 491cb0ef41Sopenharmony_ci strictEqual(sa.flowlabel, 0); 501cb0ef41Sopenharmony_ci} 511cb0ef41Sopenharmony_ci 521cb0ef41Sopenharmony_ci{ 531cb0ef41Sopenharmony_ci const sa = new SocketAddress({ 541cb0ef41Sopenharmony_ci address: '123.123.123.123', 551cb0ef41Sopenharmony_ci }); 561cb0ef41Sopenharmony_ci strictEqual(sa.address, '123.123.123.123'); 571cb0ef41Sopenharmony_ci strictEqual(sa.port, 0); 581cb0ef41Sopenharmony_ci strictEqual(sa.family, 'ipv4'); 591cb0ef41Sopenharmony_ci strictEqual(sa.flowlabel, 0); 601cb0ef41Sopenharmony_ci} 611cb0ef41Sopenharmony_ci 621cb0ef41Sopenharmony_ci{ 631cb0ef41Sopenharmony_ci const sa = new SocketAddress({ 641cb0ef41Sopenharmony_ci address: '123.123.123.123', 651cb0ef41Sopenharmony_ci port: 80 661cb0ef41Sopenharmony_ci }); 671cb0ef41Sopenharmony_ci strictEqual(sa.address, '123.123.123.123'); 681cb0ef41Sopenharmony_ci strictEqual(sa.port, 80); 691cb0ef41Sopenharmony_ci strictEqual(sa.family, 'ipv4'); 701cb0ef41Sopenharmony_ci strictEqual(sa.flowlabel, 0); 711cb0ef41Sopenharmony_ci} 721cb0ef41Sopenharmony_ci 731cb0ef41Sopenharmony_ci{ 741cb0ef41Sopenharmony_ci const sa = new SocketAddress({ 751cb0ef41Sopenharmony_ci family: 'ipv6' 761cb0ef41Sopenharmony_ci }); 771cb0ef41Sopenharmony_ci strictEqual(sa.address, '::'); 781cb0ef41Sopenharmony_ci strictEqual(sa.port, 0); 791cb0ef41Sopenharmony_ci strictEqual(sa.family, 'ipv6'); 801cb0ef41Sopenharmony_ci strictEqual(sa.flowlabel, 0); 811cb0ef41Sopenharmony_ci} 821cb0ef41Sopenharmony_ci 831cb0ef41Sopenharmony_ci{ 841cb0ef41Sopenharmony_ci const sa = new SocketAddress({ 851cb0ef41Sopenharmony_ci family: 'ipv6', 861cb0ef41Sopenharmony_ci flowlabel: 1, 871cb0ef41Sopenharmony_ci }); 881cb0ef41Sopenharmony_ci strictEqual(sa.address, '::'); 891cb0ef41Sopenharmony_ci strictEqual(sa.port, 0); 901cb0ef41Sopenharmony_ci strictEqual(sa.family, 'ipv6'); 911cb0ef41Sopenharmony_ci strictEqual(sa.flowlabel, 1); 921cb0ef41Sopenharmony_ci} 931cb0ef41Sopenharmony_ci 941cb0ef41Sopenharmony_ci[1, false, 'hello'].forEach((i) => { 951cb0ef41Sopenharmony_ci throws(() => new SocketAddress(i), { 961cb0ef41Sopenharmony_ci code: 'ERR_INVALID_ARG_TYPE' 971cb0ef41Sopenharmony_ci }); 981cb0ef41Sopenharmony_ci}); 991cb0ef41Sopenharmony_ci 1001cb0ef41Sopenharmony_ci[1, false, {}, [], 'test'].forEach((family) => { 1011cb0ef41Sopenharmony_ci throws(() => new SocketAddress({ family }), { 1021cb0ef41Sopenharmony_ci code: 'ERR_INVALID_ARG_VALUE' 1031cb0ef41Sopenharmony_ci }); 1041cb0ef41Sopenharmony_ci}); 1051cb0ef41Sopenharmony_ci 1061cb0ef41Sopenharmony_ci[1, false, {}, []].forEach((address) => { 1071cb0ef41Sopenharmony_ci throws(() => new SocketAddress({ address }), { 1081cb0ef41Sopenharmony_ci code: 'ERR_INVALID_ARG_TYPE' 1091cb0ef41Sopenharmony_ci }); 1101cb0ef41Sopenharmony_ci}); 1111cb0ef41Sopenharmony_ci 1121cb0ef41Sopenharmony_ci[-1, false, {}, []].forEach((port) => { 1131cb0ef41Sopenharmony_ci throws(() => new SocketAddress({ port }), { 1141cb0ef41Sopenharmony_ci code: 'ERR_SOCKET_BAD_PORT' 1151cb0ef41Sopenharmony_ci }); 1161cb0ef41Sopenharmony_ci}); 1171cb0ef41Sopenharmony_ci 1181cb0ef41Sopenharmony_cithrows(() => new SocketAddress({ flowlabel: -1 }), { 1191cb0ef41Sopenharmony_ci code: 'ERR_OUT_OF_RANGE' 1201cb0ef41Sopenharmony_ci}); 1211cb0ef41Sopenharmony_ci 1221cb0ef41Sopenharmony_ci{ 1231cb0ef41Sopenharmony_ci // Test that the internal helper class InternalSocketAddress correctly 1241cb0ef41Sopenharmony_ci // inherits from SocketAddress and that it does not throw when its properties 1251cb0ef41Sopenharmony_ci // are accessed. 1261cb0ef41Sopenharmony_ci 1271cb0ef41Sopenharmony_ci const address = '127.0.0.1'; 1281cb0ef41Sopenharmony_ci const port = 8080; 1291cb0ef41Sopenharmony_ci const flowlabel = 0; 1301cb0ef41Sopenharmony_ci const handle = new _SocketAddress(address, port, AF_INET, flowlabel); 1311cb0ef41Sopenharmony_ci const addr = new InternalSocketAddress(handle); 1321cb0ef41Sopenharmony_ci ok(addr instanceof SocketAddress); 1331cb0ef41Sopenharmony_ci strictEqual(addr.address, address); 1341cb0ef41Sopenharmony_ci strictEqual(addr.port, port); 1351cb0ef41Sopenharmony_ci strictEqual(addr.family, 'ipv4'); 1361cb0ef41Sopenharmony_ci strictEqual(addr.flowlabel, flowlabel); 1371cb0ef41Sopenharmony_ci} 138