@@ -69,114 +69,106 @@ const SNIContexts = {
69
69
}
70
70
} ;
71
71
72
- const clientsOptions = [ {
72
+ test ( {
73
73
port : undefined ,
74
74
key : loadPEM ( 'agent1-key' ) ,
75
75
cert : loadPEM ( 'agent1-cert' ) ,
76
76
ca : [ loadPEM ( 'ca1-cert' ) ] ,
77
77
servername : 'a.example.com' ,
78
78
rejectUnauthorized : false
79
- } , {
79
+ } ,
80
+ true ,
81
+ { sni : 'a.example.com' , authorized : false } ,
82
+ null ,
83
+ null ) ;
84
+
85
+ test ( {
80
86
port : undefined ,
81
87
key : loadPEM ( 'agent4-key' ) ,
82
88
cert : loadPEM ( 'agent4-cert' ) ,
83
89
ca : [ loadPEM ( 'ca1-cert' ) ] ,
84
90
servername : 'a.example.com' ,
85
91
rejectUnauthorized : false
86
- } , {
92
+ } ,
93
+ true ,
94
+ { sni : 'a.example.com' , authorized : true } ,
95
+ null ,
96
+ null ) ;
97
+
98
+ test ( {
87
99
port : undefined ,
88
100
key : loadPEM ( 'agent2-key' ) ,
89
101
cert : loadPEM ( 'agent2-cert' ) ,
90
102
ca : [ loadPEM ( 'ca2-cert' ) ] ,
91
103
servername : 'b.example.com' ,
92
104
rejectUnauthorized : false
93
- } , {
105
+ } ,
106
+ true ,
107
+ { sni : 'b.example.com' , authorized : false } ,
108
+ null ,
109
+ null ) ;
110
+
111
+ test ( {
94
112
port : undefined ,
95
113
key : loadPEM ( 'agent3-key' ) ,
96
114
cert : loadPEM ( 'agent3-cert' ) ,
97
115
ca : [ loadPEM ( 'ca1-cert' ) ] ,
98
116
servername : 'c.wrong.com' ,
99
117
rejectUnauthorized : false
100
- } , {
118
+ } ,
119
+ false ,
120
+ { sni : 'c.wrong.com' , authorized : false } ,
121
+ null ,
122
+ null ) ;
123
+
124
+ test ( {
101
125
port : undefined ,
102
126
key : loadPEM ( 'agent3-key' ) ,
103
127
cert : loadPEM ( 'agent3-cert' ) ,
104
128
ca : [ loadPEM ( 'ca1-cert' ) ] ,
105
129
servername : 'c.another.com' ,
106
130
rejectUnauthorized : false
107
- } ] ;
108
-
109
- const serverResults = [ ] ;
110
- const clientResults = [ ] ;
111
- const serverErrors = [ ] ;
112
- const clientErrors = [ ] ;
113
- let serverError ;
114
- let clientError ;
115
-
116
- const server = tls . createServer ( serverOptions , function ( c ) {
117
- serverResults . push ( { sni : c . servername , authorized : c . authorized } ) ;
118
- c . end ( ) ;
119
- } ) ;
120
-
121
- server . on ( 'tlsClientError' , function ( err ) {
122
- serverResults . push ( null ) ;
123
- serverError = err . message ;
124
- } ) ;
125
-
126
- server . listen ( 0 , startTest ) ;
131
+ } ,
132
+ false ,
133
+ null ,
134
+ 'Client network socket disconnected before secure TLS ' +
135
+ 'connection was established' ,
136
+ 'Invalid SNI context' ) ;
137
+
138
+ function test ( options , clientResult , serverResult , clientError , serverError ) {
139
+ const server = tls . createServer ( serverOptions , ( c ) => {
140
+ assert . deepStrictEqual (
141
+ { sni : c . servername , authorized : c . authorized } ,
142
+ serverResult
143
+ ) ;
144
+ } ) ;
127
145
128
- function startTest ( ) {
129
- function connectClient ( i , callback ) {
130
- const options = clientsOptions [ i ] ;
131
- clientError = null ;
132
- serverError = null ;
146
+ if ( serverResult ) {
147
+ assert ( ! serverError ) ;
148
+ server . on ( 'tlsClientError' , common . mustNotCall ( ) ) ;
149
+ } else {
150
+ assert ( serverError ) ;
151
+ server . on ( 'tlsClientError' , common . mustCall ( ( err ) => {
152
+ assert . strictEqual ( err . message , serverError ) ;
153
+ } ) ) ;
154
+ }
133
155
156
+ server . listen ( 0 , ( ) => {
134
157
options . port = server . address ( ) . port ;
135
- const client = tls . connect ( options , function ( ) {
136
- clientResults . push (
137
- client . authorizationError &&
138
- ( client . authorizationError === 'ERR_TLS_CERT_ALTNAME_INVALID' ) ) ;
139
-
140
- next ( ) ;
141
- } ) ;
142
-
143
- client . on ( 'error' , function ( err ) {
144
- clientResults . push ( false ) ;
145
- clientError = err . message ;
146
- next ( ) ;
158
+ const client = tls . connect ( options , ( ) => {
159
+ const result = client . authorizationError &&
160
+ ( client . authorizationError === 'ERR_TLS_CERT_ALTNAME_INVALID' ) ;
161
+ assert . strictEqual ( result , clientResult ) ;
162
+ client . end ( ) ;
147
163
} ) ;
148
164
149
- function next ( ) {
150
- clientErrors . push ( clientError ) ;
151
- serverErrors . push ( serverError ) ;
152
-
153
- if ( i === clientsOptions . length - 1 )
154
- callback ( ) ;
155
- else
156
- connectClient ( i + 1 , callback ) ;
157
- }
158
- }
165
+ client . on ( 'close' , common . mustCall ( ( ) => server . close ( ) ) ) ;
159
166
160
- connectClient ( 0 , function ( ) {
161
- server . close ( ) ;
167
+ if ( clientError )
168
+ client . on ( 'error' , common . mustCall ( ( err ) => {
169
+ assert . strictEqual ( err . message , clientError ) ;
170
+ } ) ) ;
171
+ else
172
+ client . on ( 'error' , common . mustNotCall ( ) ) ;
162
173
} ) ;
163
174
}
164
-
165
- process . on ( 'exit' , function ( ) {
166
- assert . deepStrictEqual ( serverResults , [
167
- { sni : 'a.example.com' , authorized : false } ,
168
- { sni : 'a.example.com' , authorized : true } ,
169
- { sni : 'b.example.com' , authorized : false } ,
170
- { sni : 'c.wrong.com' , authorized : false } ,
171
- null
172
- ] ) ;
173
- assert . deepStrictEqual ( clientResults , [ true , true , true , false , false ] ) ;
174
- assert . deepStrictEqual ( clientErrors , [
175
- null , null , null , null ,
176
- 'Client network socket disconnected before secure TLS ' +
177
- 'connection was established'
178
- ] ) ;
179
- assert . deepStrictEqual ( serverErrors , [
180
- null , null , null , null , 'Invalid SNI context'
181
- ] ) ;
182
- } ) ;
0 commit comments