From eb3635184bd0a6dcbde03aa27fe5f3cf9eb5e0bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Nie=C3=9Fen?= Date: Thu, 15 Sep 2022 18:13:03 +0200 Subject: [PATCH] lib,test: fix bug in InternalSocketAddress MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit InternalSocketAddress must set [kDetails] in order for the inherited properties to function correctly. Co-authored-by: Tobias Nießen PR-URL: https://github.com/nodejs/node/pull/44618 Reviewed-By: Joyee Cheung Reviewed-By: Antoine du Hamel --- lib/internal/socketaddress.js | 6 ++++++ test/parallel/test-socketaddress.js | 27 +++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/lib/internal/socketaddress.js b/lib/internal/socketaddress.js index 9697a1e7380eac..506f397180e6a1 100644 --- a/lib/internal/socketaddress.js +++ b/lib/internal/socketaddress.js @@ -142,6 +142,12 @@ class InternalSocketAddress extends JSTransferable { constructor(handle) { super(); this[kHandle] = handle; + this[kDetail] = this[kHandle]?.detail({ + address: undefined, + port: undefined, + family: undefined, + flowlabel: undefined, + }); } } diff --git a/test/parallel/test-socketaddress.js b/test/parallel/test-socketaddress.js index 4caf46dd3ee9ad..b6d9946271fa52 100644 --- a/test/parallel/test-socketaddress.js +++ b/test/parallel/test-socketaddress.js @@ -1,3 +1,4 @@ +// Flags: --expose-internals 'use strict'; const common = require('../common'); @@ -10,6 +11,15 @@ const { SocketAddress, } = require('net'); +const { + InternalSocketAddress, +} = require('internal/socketaddress'); +const { internalBinding } = require('internal/test/binding'); +const { + SocketAddress: _SocketAddress, + AF_INET +} = internalBinding('block_list'); + { const sa = new SocketAddress(); strictEqual(sa.address, '127.0.0.1'); @@ -108,3 +118,20 @@ const { throws(() => new SocketAddress({ flowlabel: -1 }), { code: 'ERR_OUT_OF_RANGE' }); + +{ + // Test that the internal helper class InternalSocketAddress correctly + // inherits from SocketAddress and that it does not throw when its properties + // are accessed. + + const address = '127.0.0.1'; + const port = 8080; + const flowlabel = 0; + const handle = new _SocketAddress(address, port, AF_INET, flowlabel); + const addr = new InternalSocketAddress(handle); + ok(addr instanceof SocketAddress); + strictEqual(addr.address, address); + strictEqual(addr.port, port); + strictEqual(addr.family, 'ipv4'); + strictEqual(addr.flowlabel, flowlabel); +}