@@ -5,33 +5,43 @@ if (!common.hasCrypto)
5
5
common . skip ( 'missing crypto' ) ;
6
6
7
7
if ( ! common . opensslCli )
8
- common . skip ( 'node compiled without OpenSSL CLI. ' ) ;
8
+ common . skip ( 'node compiled without OpenSSL CLI' ) ;
9
9
10
10
const net = require ( 'net' ) ;
11
11
const tls = require ( 'tls' ) ;
12
12
const fixtures = require ( '../common/fixtures' ) ;
13
13
14
+ let clientClosed = false ;
15
+ let errorReceived = false ;
16
+ function canCloseServer ( ) {
17
+ return clientClosed && errorReceived ;
18
+ }
19
+
14
20
function loadPEM ( n ) {
15
- return fixtures . readKey ( `${ n } .pem` ) ;
21
+ return fixtures . readKey ( `${ n } .pem` , 'utf-8' ) ;
16
22
}
17
23
18
24
const opts = {
19
25
key : loadPEM ( 'agent2-key' ) ,
20
26
cert : loadPEM ( 'agent2-cert' )
21
27
} ;
28
+
22
29
const max_iter = 20 ;
23
30
let iter = 0 ;
24
31
25
- const server = tls . createServer ( opts , function ( s ) {
26
- s . pipe ( s ) ;
27
- s . on ( 'error' , function ( ) {
28
- // ignore error
29
- } ) ;
32
+ const errorHandler = common . mustCall ( ( ) => {
33
+ errorReceived = true ;
34
+ if ( canCloseServer ( ) )
35
+ server . close ( ) ;
30
36
} ) ;
37
+ const server = tls . createServer ( opts , common . mustCall ( function ( s ) {
38
+ s . pipe ( s ) ;
39
+ s . on ( 'error' , errorHandler ) ;
40
+ } , 2 ) ) ;
31
41
32
- server . listen ( 0 , function ( ) {
42
+ server . listen ( 0 , common . mustCall ( function ( ) {
33
43
sendClient ( ) ;
34
- } ) ;
44
+ } ) ) ;
35
45
36
46
37
47
function sendClient ( ) {
@@ -45,15 +55,14 @@ function sendClient() {
45
55
return ;
46
56
}
47
57
client . end ( ) ;
48
- server . close ( ) ;
49
58
} , max_iter ) ) ;
50
59
client . write ( 'a' ) ;
51
- client . on ( 'error' , function ( ) {
52
- // ignore error
53
- } ) ;
54
- client . on ( 'close' , function ( ) {
55
- server . close ( ) ;
56
- } ) ;
60
+ client . on ( 'error' , common . mustNotCall ( ) ) ;
61
+ client . on ( 'close' , common . mustCall ( function ( ) {
62
+ clientClosed = true ;
63
+ if ( canCloseServer ( ) )
64
+ server . close ( ) ;
65
+ } ) ) ;
57
66
}
58
67
59
68
@@ -63,11 +72,9 @@ function sendBADTLSRecord() {
63
72
const client = tls . connect ( {
64
73
socket : socket ,
65
74
rejectUnauthorized : false
66
- } , function ( ) {
75
+ } , common . mustCall ( function ( ) {
67
76
socket . write ( BAD_RECORD ) ;
68
77
socket . end ( ) ;
69
- } ) ;
70
- client . on ( 'error' , function ( ) {
71
- // ignore error
72
- } ) ;
78
+ } ) ) ;
79
+ client . on ( 'error' , common . mustCall ( ) ) ;
73
80
}
0 commit comments