1'use strict'; 2 3const common = require('../common'); 4const { 5 ok, 6 strictEqual, 7 throws, 8} = require('assert'); 9const { 10 SocketAddress, 11} = require('net'); 12const { 13 MessageChannel, 14} = require('worker_threads'); 15 16{ 17 const sa = new SocketAddress(); 18 strictEqual(sa.address, '127.0.0.1'); 19 strictEqual(sa.port, 0); 20 strictEqual(sa.family, 'ipv4'); 21 strictEqual(sa.flowlabel, 0); 22 23 const mc = new MessageChannel(); 24 mc.port1.onmessage = common.mustCall(({ data }) => { 25 ok(SocketAddress.isSocketAddress(data)); 26 27 strictEqual(data.address, '127.0.0.1'); 28 strictEqual(data.port, 0); 29 strictEqual(data.family, 'ipv4'); 30 strictEqual(data.flowlabel, 0); 31 32 mc.port1.close(); 33 }); 34 mc.port2.postMessage(sa); 35} 36 37{ 38 const sa = new SocketAddress({}); 39 strictEqual(sa.address, '127.0.0.1'); 40 strictEqual(sa.port, 0); 41 strictEqual(sa.family, 'ipv4'); 42 strictEqual(sa.flowlabel, 0); 43} 44 45{ 46 const sa = new SocketAddress({ 47 address: '123.123.123.123', 48 }); 49 strictEqual(sa.address, '123.123.123.123'); 50 strictEqual(sa.port, 0); 51 strictEqual(sa.family, 'ipv4'); 52 strictEqual(sa.flowlabel, 0); 53} 54 55{ 56 const sa = new SocketAddress({ 57 address: '123.123.123.123', 58 port: 80 59 }); 60 strictEqual(sa.address, '123.123.123.123'); 61 strictEqual(sa.port, 80); 62 strictEqual(sa.family, 'ipv4'); 63 strictEqual(sa.flowlabel, 0); 64} 65 66{ 67 const sa = new SocketAddress({ 68 family: 'ipv6' 69 }); 70 strictEqual(sa.address, '::'); 71 strictEqual(sa.port, 0); 72 strictEqual(sa.family, 'ipv6'); 73 strictEqual(sa.flowlabel, 0); 74} 75 76{ 77 const sa = new SocketAddress({ 78 family: 'ipv6', 79 flowlabel: 1, 80 }); 81 strictEqual(sa.address, '::'); 82 strictEqual(sa.port, 0); 83 strictEqual(sa.family, 'ipv6'); 84 strictEqual(sa.flowlabel, 1); 85} 86 87[1, false, 'hello'].forEach((i) => { 88 throws(() => new SocketAddress(i), { 89 code: 'ERR_INVALID_ARG_TYPE' 90 }); 91}); 92 93[1, false, {}, [], 'test'].forEach((family) => { 94 throws(() => new SocketAddress({ family }), { 95 code: 'ERR_INVALID_ARG_VALUE' 96 }); 97}); 98 99[1, false, {}, []].forEach((address) => { 100 throws(() => new SocketAddress({ address }), { 101 code: 'ERR_INVALID_ARG_TYPE' 102 }); 103}); 104 105[-1, false, {}, []].forEach((port) => { 106 throws(() => new SocketAddress({ port }), { 107 code: 'ERR_SOCKET_BAD_PORT' 108 }); 109}); 110 111throws(() => new SocketAddress({ flowlabel: -1 }), { 112 code: 'ERR_OUT_OF_RANGE' 113}); 114