@@ -36,21 +36,20 @@ const assert = require('assert');
36
36
const SSL_OP_NO_TICKET = require ( 'crypto' ) . constants . SSL_OP_NO_TICKET ;
37
37
38
38
const pfx = fixtures . readKey ( 'agent1.pfx' ) ;
39
+ const key = fixtures . readKey ( 'agent1-key.pem' ) ;
40
+ const cert = fixtures . readKey ( 'agent1-cert.pem' ) ;
41
+ const ca = fixtures . readKey ( 'ca1-cert.pem' ) ;
39
42
40
43
function test ( testOptions , cb ) {
41
-
42
- const key = fixtures . readKey ( 'agent1-key.pem' ) ;
43
- const cert = fixtures . readKey ( 'agent1-cert.pem' ) ;
44
- const ca = fixtures . readKey ( 'ca1-cert.pem' ) ;
45
44
const options = {
46
45
key,
47
46
cert,
48
47
ca : [ ca ]
49
48
} ;
50
- let requestCount = 0 ;
51
- let clientSecure = 0 ;
52
- let ocspCount = 0 ;
53
- let ocspResponse ;
49
+ const requestCount = testOptions . response ? 0 : 1 ;
50
+
51
+ if ( ! testOptions . ocsp )
52
+ assert . strictEqual ( testOptions . response , undefined ) ;
54
53
55
54
if ( testOptions . pfx ) {
56
55
delete options . key ;
@@ -59,82 +58,56 @@ function test(testOptions, cb) {
59
58
options . passphrase = testOptions . passphrase ;
60
59
}
61
60
62
- const server = tls . createServer ( options , function ( cleartext ) {
61
+ const server = tls . createServer ( options , common . mustCall ( ( cleartext ) => {
63
62
cleartext . on ( 'error' , function ( er ) {
64
63
// We're ok with getting ECONNRESET in this test, but it's
65
64
// timing-dependent, and thus unreliable. Any other errors
66
65
// are just failures, though.
67
66
if ( er . code !== 'ECONNRESET' )
68
67
throw er ;
69
68
} ) ;
70
- ++ requestCount ;
71
69
cleartext . end ( ) ;
72
- } ) ;
73
- server . on ( 'OCSPRequest' , function ( cert , issuer , callback ) {
74
- ++ ocspCount ;
75
- assert . ok ( Buffer . isBuffer ( cert ) ) ;
76
- assert . ok ( Buffer . isBuffer ( issuer ) ) ;
77
-
78
- // Just to check that async really works there
79
- setTimeout ( function ( ) {
80
- callback ( null ,
81
- testOptions . response ? Buffer . from ( testOptions . response ) : null ) ;
82
- } , 100 ) ;
83
- } ) ;
70
+ } , requestCount ) ) ;
71
+
72
+ if ( ! testOptions . ocsp )
73
+ server . on ( 'OCSPRequest' , common . mustNotCall ( ) ) ;
74
+ else
75
+ server . on ( 'OCSPRequest' , common . mustCall ( ( cert , issuer , callback ) => {
76
+ assert . ok ( Buffer . isBuffer ( cert ) ) ;
77
+ assert . ok ( Buffer . isBuffer ( issuer ) ) ;
78
+
79
+ // Callback a little later to ensure that async really works.
80
+ return setTimeout ( callback , 100 , null , testOptions . response ?
81
+ Buffer . from ( testOptions . response ) : null ) ;
82
+ } ) ) ;
83
+
84
84
server . listen ( 0 , function ( ) {
85
85
const client = tls . connect ( {
86
86
port : this . address ( ) . port ,
87
- requestOCSP : testOptions . ocsp !== false ,
88
- secureOptions : testOptions . ocsp === false ?
89
- SSL_OP_NO_TICKET : 0 ,
87
+ requestOCSP : testOptions . ocsp ,
88
+ secureOptions : testOptions . ocsp ? 0 : SSL_OP_NO_TICKET ,
90
89
rejectUnauthorized : false
91
- } , function ( ) {
92
- clientSecure ++ ;
93
- } ) ;
94
- client . on ( 'OCSPResponse' , function ( resp ) {
95
- ocspResponse = resp ;
96
- if ( resp )
90
+ } , common . mustCall ( ( ) => { } , requestCount ) ) ;
91
+
92
+ client . on ( 'OCSPResponse' , common . mustCall ( ( resp ) => {
93
+ if ( testOptions . response ) {
94
+ assert . strictEqual ( resp . toString ( ) , testOptions . response ) ;
97
95
client . destroy ( ) ;
98
- } ) ;
99
- client . on ( 'close' , function ( ) {
100
- server . close ( cb ) ;
101
- } ) ;
102
- } ) ;
96
+ } else {
97
+ assert . strictEqual ( resp , null ) ;
98
+ }
99
+ } , testOptions . ocsp === false ? 0 : 1 ) ) ;
103
100
104
- process . on ( 'exit' , function ( ) {
105
- if ( testOptions . ocsp === false ) {
106
- assert . strictEqual ( requestCount , clientSecure ) ;
107
- assert . strictEqual ( requestCount , 1 ) ;
108
- return ;
109
- }
110
-
111
- if ( testOptions . response ) {
112
- assert . strictEqual ( ocspResponse . toString ( ) , testOptions . response ) ;
113
- } else {
114
- assert . strictEqual ( ocspResponse , null ) ;
115
- }
116
- assert . strictEqual ( requestCount , testOptions . response ? 0 : 1 ) ;
117
- assert . strictEqual ( clientSecure , requestCount ) ;
118
- assert . strictEqual ( ocspCount , 1 ) ;
101
+ client . on ( 'close' , common . mustCall ( ( ) => {
102
+ server . close ( cb ) ;
103
+ } ) ) ;
119
104
} ) ;
120
105
}
121
106
122
- const tests = [
123
- { response : false } ,
124
- { response : 'hello world' } ,
125
- { ocsp : false }
126
- ] ;
107
+ test ( { ocsp : true , response : false } ) ;
108
+ test ( { ocsp : true , response : 'hello world' } ) ;
109
+ test ( { ocsp : false } ) ;
127
110
128
111
if ( ! common . hasFipsCrypto ) {
129
- tests . push ( { pfx : pfx , passphrase : 'sample ' , response : 'hello pfx' } ) ;
112
+ test ( { ocsp : true , response : 'hello pfx ' , pfx : pfx , passphrase : 'sample ' } ) ;
130
113
}
131
-
132
- function runTests ( i ) {
133
- if ( i === tests . length ) return ;
134
-
135
- test ( tests [ i ] , common . mustCall ( function ( ) {
136
- runTests ( i + 1 ) ;
137
- } ) ) ;
138
- }
139
-
140
- runTests ( 0 ) ;
0 commit comments