@@ -3,45 +3,58 @@ const common = require('../common');
3
3
const assert = require ( 'assert' ) ;
4
4
const dgram = require ( 'dgram' ) ;
5
5
6
- // IPv4 Test
7
- const socket_ipv4 = dgram . createSocket ( 'udp4' ) ;
8
- const family_ipv4 = 'IPv4' ;
9
-
10
- socket_ipv4 . on ( 'listening' , function ( ) {
11
- const address_ipv4 = socket_ipv4 . address ( ) ;
12
- assert . strictEqual ( address_ipv4 . address , common . localhostIPv4 ) ;
13
- assert . strictEqual ( typeof address_ipv4 . port , 'number' ) ;
14
- assert . ok ( isFinite ( address_ipv4 . port ) ) ;
15
- assert . ok ( address_ipv4 . port > 0 ) ;
16
- assert . strictEqual ( address_ipv4 . family , family_ipv4 ) ;
17
- socket_ipv4 . close ( ) ;
18
- } ) ;
19
-
20
- socket_ipv4 . on ( 'error' , function ( e ) {
21
- console . log ( 'Error on udp4 socket. ' + e . toString ( ) ) ;
22
- socket_ipv4 . close ( ) ;
23
- } ) ;
24
-
25
- socket_ipv4 . bind ( 0 , common . localhostIPv4 ) ;
26
-
27
- // IPv6 Test
28
- const localhost_ipv6 = '::1' ;
29
- const socket_ipv6 = dgram . createSocket ( 'udp6' ) ;
30
- const family_ipv6 = 'IPv6' ;
31
-
32
- socket_ipv6 . on ( 'listening' , function ( ) {
33
- const address_ipv6 = socket_ipv6 . address ( ) ;
34
- assert . strictEqual ( address_ipv6 . address , localhost_ipv6 ) ;
35
- assert . strictEqual ( typeof address_ipv6 . port , 'number' ) ;
36
- assert . ok ( isFinite ( address_ipv6 . port ) ) ;
37
- assert . ok ( address_ipv6 . port > 0 ) ;
38
- assert . strictEqual ( address_ipv6 . family , family_ipv6 ) ;
39
- socket_ipv6 . close ( ) ;
40
- } ) ;
41
-
42
- socket_ipv6 . on ( 'error' , function ( e ) {
43
- console . log ( 'Error on udp6 socket. ' + e . toString ( ) ) ;
44
- socket_ipv6 . close ( ) ;
45
- } ) ;
46
-
47
- socket_ipv6 . bind ( 0 , localhost_ipv6 ) ;
6
+ {
7
+ // IPv4 Test
8
+ const socket = dgram . createSocket ( 'udp4' ) ;
9
+
10
+ socket . on ( 'listening' , common . mustCall ( ( ) => {
11
+ const address = socket . address ( ) ;
12
+
13
+ assert . strictEqual ( address . address , common . localhostIPv4 ) ;
14
+ assert . strictEqual ( typeof address . port , 'number' ) ;
15
+ assert . ok ( isFinite ( address . port ) ) ;
16
+ assert . ok ( address . port > 0 ) ;
17
+ assert . strictEqual ( address . family , 'IPv4' ) ;
18
+ socket . close ( ) ;
19
+ } ) ) ;
20
+
21
+ socket . on ( 'error' , ( err ) => {
22
+ socket . close ( ) ;
23
+ common . fail ( `Unexpected error on udp4 socket. ${ err . toString ( ) } ` ) ;
24
+ } ) ;
25
+
26
+ socket . bind ( 0 , common . localhostIPv4 ) ;
27
+ }
28
+
29
+ {
30
+ // IPv6 Test
31
+ const socket = dgram . createSocket ( 'udp6' ) ;
32
+ const localhost = '::1' ;
33
+
34
+ socket . on ( 'listening' , common . mustCall ( ( ) => {
35
+ const address = socket . address ( ) ;
36
+
37
+ assert . strictEqual ( address . address , localhost ) ;
38
+ assert . strictEqual ( typeof address . port , 'number' ) ;
39
+ assert . ok ( isFinite ( address . port ) ) ;
40
+ assert . ok ( address . port > 0 ) ;
41
+ assert . strictEqual ( address . family , 'IPv6' ) ;
42
+ socket . close ( ) ;
43
+ } ) ) ;
44
+
45
+ socket . on ( 'error' , ( err ) => {
46
+ socket . close ( ) ;
47
+ common . fail ( `Unexpected error on udp6 socket. ${ err . toString ( ) } ` ) ;
48
+ } ) ;
49
+
50
+ socket . bind ( 0 , localhost ) ;
51
+ }
52
+
53
+ {
54
+ // Verify that address() throws if the socket is not bound.
55
+ const socket = dgram . createSocket ( 'udp4' ) ;
56
+
57
+ assert . throws ( ( ) => {
58
+ socket . address ( ) ;
59
+ } , / ^ E r r o r : g e t s o c k n a m e E I N V A L $ / ) ;
60
+ }
0 commit comments