1919// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
2020// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121
22+ // Test that the family option of net.connect is honored.
23+
2224'use strict' ;
2325const common = require ( '../common' ) ;
2426if ( ! common . hasIPv6 )
@@ -27,63 +29,39 @@ if (!common.hasIPv6)
2729const assert = require ( 'assert' ) ;
2830const net = require ( 'net' ) ;
2931
30- const hosts = common . localIPv6Hosts ;
31- let hostIdx = 0 ;
32- let host = hosts [ hostIdx ] ;
33- let localhostTries = 10 ;
32+ const hostAddrIPv6 = '::1' ;
33+ const HOSTNAME = 'dummy' ;
3434
35- const server = net . createServer ( { allowHalfOpen : true } , function ( socket ) {
35+ const server = net . createServer ( { allowHalfOpen : true } , ( socket ) => {
3636 socket . resume ( ) ;
3737 socket . on ( 'end' , common . mustCall ( ) ) ;
3838 socket . end ( ) ;
3939} ) ;
4040
41- server . listen ( 0 , '::1' , tryConnect ) ;
42-
4341function tryConnect ( ) {
44- const client = net . connect ( {
45- host : host ,
42+ const connectOpt = {
43+ host : HOSTNAME ,
4644 port : server . address ( ) . port ,
4745 family : 6 ,
48- allowHalfOpen : true
49- } , function ( ) {
50- console . error ( 'client connect cb' ) ;
46+ allowHalfOpen : true ,
47+ lookup : common . mustCall ( ( addr , opt , cb ) => {
48+ assert . strictEqual ( addr , HOSTNAME ) ;
49+ assert . strictEqual ( opt . family , 6 ) ;
50+ cb ( null , hostAddrIPv6 , opt . family ) ;
51+ } )
52+ } ;
53+ // No `mustCall`, since test could skip, and it's the only path to `close`.
54+ const client = net . connect ( connectOpt , ( ) => {
5155 client . resume ( ) ;
52- client . on ( 'end' , common . mustCall ( function ( ) {
56+ client . on ( 'end' , ( ) => {
57+ // Wait for next uv tick and make sure the socket stream is writable.
5358 setTimeout ( function ( ) {
5459 assert ( client . writable ) ;
5560 client . end ( ) ;
5661 } , 10 ) ;
57- } ) ) ;
58- client . on ( 'close' , function ( ) {
59- server . close ( ) ;
6062 } ) ;
61- } ) . on ( 'error' , function ( err ) {
62- // ENOTFOUND means we don't have the requested address. In this
63- // case we try the next one in the list and if we run out of
64- // candidates we assume IPv6 is not supported on the
65- // machine and skip the test.
66- // EAI_AGAIN means we tried to remotely resolve the address and
67- // timed out or hit some intermittent connectivity issue with the
68- // dns server. Although we are looking for local loopback addresses
69- // we may go remote since the list we search includes addresses that
70- // cover more than is available on any one distribution. The
71- // net is that if we get an EAI_AGAIN we were looking for an
72- // address which does not exist in this distribution so the error
73- // is not significant and we should just move on and try the
74- // next address in the list.
75- if ( ( err . syscall === 'getaddrinfo' ) && ( ( err . code === 'ENOTFOUND' ) ||
76- ( err . code === 'EAI_AGAIN' ) ) ) {
77- if ( host !== 'localhost' || -- localhostTries === 0 )
78- host = hosts [ ++ hostIdx ] ;
79- if ( host )
80- tryConnect ( ) ;
81- else {
82- server . close ( ) ;
83- common . skip ( 'no IPv6 localhost support' ) ;
84- }
85- return ;
86- }
87- throw err ;
63+ client . on ( 'close' , ( ) => server . close ( ) ) ;
8864 } ) ;
8965}
66+
67+ server . listen ( 0 , hostAddrIPv6 , tryConnect ) ;
0 commit comments