11const assert = require ( 'assert' ) ;
22const net = require ( 'net' ) ;
3+ const os = require ( 'os' ) ;
34const helpers = require ( '../lib/helpers' ) ;
45const consts = require ( '../lib/constants' ) ;
56const CacheServer = require ( '../lib/server/server' ) ;
@@ -67,6 +68,57 @@ describe("Server common", function() {
6768 } ) ;
6869 } ) ;
6970
71+ describe ( "Ipv6" , function ( ) {
72+ const ipv6Server = new CacheServer ( cache , { port : 0 , allowIpv6 : true } ) ;
73+
74+ before ( function ( ) {
75+ var interfaces = os . networkInterfaces ( ) ;
76+ var ipv6Available = false ;
77+ Object . keys ( interfaces ) . forEach ( function ( interfaceName ) {
78+ interfaces [ interfaceName ] . forEach ( function ( address ) {
79+ if ( address . family === "IPv6" ) {
80+ ipv6Available = true ;
81+ }
82+ } ) ;
83+ } ) ;
84+
85+ if ( ! ipv6Available ) {
86+ console . log ( "Skipping IPv6 tests because IPv6 is not available on this machine" ) ;
87+ this . skip ( ) ;
88+ }
89+
90+ return ipv6Server . start ( err => assert ( ! err , `Cache Server reported error! ${ err } ` ) ) ;
91+ } ) ;
92+
93+ after ( function ( ) {
94+ ipv6Server . stop ( ) ;
95+ } ) ;
96+
97+ it ( "should bind to ipv6 when allowed" , function ( done ) {
98+ var serverAddress = ipv6Server . _server . address ( ) ;
99+ assert . strictEqual ( serverAddress . family , "IPv6" ) ;
100+ done ( ) ;
101+ } ) ;
102+
103+ } ) ;
104+ describe ( "Ipv4" , function ( ) {
105+ const ipv4Server = new CacheServer ( cache , { port : 0 , allowIpv6 : false } ) ;
106+
107+ before ( function ( ) {
108+ return ipv4Server . start ( err => assert ( ! err , `Cache Server reported error! ${ err } ` ) ) ;
109+ } ) ;
110+
111+ after ( function ( ) {
112+ ipv4Server . stop ( ) ;
113+ } ) ;
114+
115+ it ( "should bind to ipv4 when ipv6 not allowed" , function ( done ) {
116+ var serverAddress = ipv4Server . _server . address ( ) ;
117+ assert . strictEqual ( serverAddress . family , "IPv4" ) ;
118+ done ( ) ;
119+ } ) ;
120+ } ) ;
121+
70122 describe ( "Other" , function ( ) {
71123 it ( "should force close the socket when a quit (q) command is received" , function ( done ) {
72124 client = net . connect ( { port : server . port } , function ( err ) {
@@ -92,6 +144,7 @@ describe("Server common", function() {
92144 client . write ( helpers . encodeInt32 ( consts . PROTOCOL_VERSION ) ) ;
93145 client . write ( 'xx' ) ;
94146 } ) ;
95- } )
147+ } ) ;
96148 } )
97- } ) ;
149+ } ) ;
150+
0 commit comments