19
19
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20
20
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21
21
22
+ // Test that the family option of net.connect is honored.
23
+
22
24
'use strict' ;
23
25
const common = require ( '../common' ) ;
24
26
if ( ! common . hasIPv6 )
@@ -27,63 +29,39 @@ if (!common.hasIPv6)
27
29
const assert = require ( 'assert' ) ;
28
30
const net = require ( 'net' ) ;
29
31
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' ;
34
34
35
- const server = net . createServer ( { allowHalfOpen : true } , function ( socket ) {
35
+ const server = net . createServer ( { allowHalfOpen : true } , ( socket ) => {
36
36
socket . resume ( ) ;
37
37
socket . on ( 'end' , common . mustCall ( ) ) ;
38
38
socket . end ( ) ;
39
39
} ) ;
40
40
41
- server . listen ( 0 , '::1' , tryConnect ) ;
42
-
43
41
function tryConnect ( ) {
44
- const client = net . connect ( {
45
- host : host ,
42
+ const connectOpt = {
43
+ host : HOSTNAME ,
46
44
port : server . address ( ) . port ,
47
45
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 , ( ) => {
51
55
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.
53
58
setTimeout ( function ( ) {
54
59
assert ( client . writable ) ;
55
60
client . end ( ) ;
56
61
} , 10 ) ;
57
- } ) ) ;
58
- client . on ( 'close' , function ( ) {
59
- server . close ( ) ;
60
62
} ) ;
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 ( ) ) ;
88
64
} ) ;
89
65
}
66
+
67
+ server . listen ( 0 , hostAddrIPv6 , tryConnect ) ;
0 commit comments