@@ -28,12 +28,12 @@ struct HTTP2TransportTLSEnabledTests {
28
28
29
29
@Test (
30
30
" When using defaults, server does not perform client verification " ,
31
- arguments: [ TransportSecurity . posix] ,
32
- [ TransportSecurity . posix]
31
+ arguments: [ TransportKind . posix] ,
32
+ [ TransportKind . posix]
33
33
)
34
34
func testRPC_Defaults_OK(
35
- clientTransport: TransportSecurity ,
36
- serverTransport: TransportSecurity
35
+ clientTransport: TransportKind ,
36
+ serverTransport: TransportKind
37
37
) async throws {
38
38
let certificateKeyPairs = try SelfSignedCertificateKeyPairs ( )
39
39
let clientTransportConfig = self . makeDefaultClientTLSConfig (
@@ -46,26 +46,23 @@ struct HTTP2TransportTLSEnabledTests {
46
46
)
47
47
48
48
try await self . withClientAndServer (
49
- clientTransportSecurity : clientTransportConfig,
50
- serverTransportSecurity : serverTransportConfig
49
+ clientTLSConfig : clientTransportConfig,
50
+ serverTLSConfig : serverTransportConfig
51
51
) { control in
52
- await #expect(
53
- throws: Never . self,
54
- performing: {
55
- try await self . executeUnaryRPC ( control: control)
56
- }
57
- )
52
+ await #expect( throws: Never . self) {
53
+ try await self . executeUnaryRPC ( control: control)
54
+ }
58
55
}
59
56
}
60
57
61
58
@Test (
62
59
" When using mTLS defaults, both client and server verify each others' certificates " ,
63
- arguments: [ TransportSecurity . posix] ,
64
- [ TransportSecurity . posix]
60
+ arguments: [ TransportKind . posix] ,
61
+ [ TransportKind . posix]
65
62
)
66
63
func testRPC_mTLS_OK(
67
- clientTransport: TransportSecurity ,
68
- serverTransport: TransportSecurity
64
+ clientTransport: TransportKind ,
65
+ serverTransport: TransportKind
69
66
) async throws {
70
67
let certificateKeyPairs = try SelfSignedCertificateKeyPairs ( )
71
68
let clientTransportConfig = self . makeMTLSClientTLSConfig (
@@ -80,27 +77,24 @@ struct HTTP2TransportTLSEnabledTests {
80
77
)
81
78
82
79
try await self . withClientAndServer (
83
- clientTransportSecurity : clientTransportConfig,
84
- serverTransportSecurity : serverTransportConfig
80
+ clientTLSConfig : clientTransportConfig,
81
+ serverTLSConfig : serverTransportConfig
85
82
) { control in
86
- await #expect(
87
- throws: Never . self,
88
- performing: {
89
- try await self . executeUnaryRPC ( control: control)
90
- }
91
- )
83
+ await #expect( throws: Never . self) {
84
+ try await self . executeUnaryRPC ( control: control)
85
+ }
92
86
}
93
87
}
94
88
95
89
@Test (
96
90
" Error is surfaced when client fails server verification " ,
97
- arguments: [ TransportSecurity . posix] ,
98
- [ TransportSecurity . posix]
91
+ arguments: [ TransportKind . posix] ,
92
+ [ TransportKind . posix]
99
93
)
100
94
// Verification should fail because the custom hostname is missing on the client.
101
95
func testClientFailsServerValidation(
102
- clientTransport: TransportSecurity ,
103
- serverTransport: TransportSecurity
96
+ clientTransport: TransportKind ,
97
+ serverTransport: TransportKind
104
98
) async throws {
105
99
let certificateKeyPairs = try SelfSignedCertificateKeyPairs ( )
106
100
let clientTransportConfig = self . makeMTLSClientTLSConfig (
@@ -115,49 +109,46 @@ struct HTTP2TransportTLSEnabledTests {
115
109
)
116
110
117
111
try await self . withClientAndServer (
118
- clientTransportSecurity : clientTransportConfig,
119
- serverTransportSecurity : serverTransportConfig
112
+ clientTLSConfig : clientTransportConfig,
113
+ serverTLSConfig : serverTransportConfig
120
114
) { control in
121
- await #expect(
122
- performing: {
123
- try await self . executeUnaryRPC ( control: control)
124
- } ,
125
- throws: { error in
126
- guard let rootError = error as? RPCError else {
127
- Issue . record ( " Should be an RPC error " )
128
- return false
129
- }
130
- #expect( rootError. code == . unavailable)
131
- #expect(
132
- rootError. message
133
- == " The server accepted the TCP connection but closed the connection before completing the HTTP/2 connection preface. "
134
- )
135
-
136
- guard
137
- let sslError = rootError. cause as? NIOSSLExtraError ,
138
- case . failedToValidateHostname = sslError
139
- else {
140
- Issue . record (
141
- " Should be a NIOSSLExtraError.failedToValidateHostname error, but was: \( String ( describing: rootError. cause) ) "
142
- )
143
- return false
144
- }
115
+ await #expect {
116
+ try await self . executeUnaryRPC ( control: control)
117
+ } throws: { error in
118
+ guard let rootError = error as? RPCError else {
119
+ Issue . record ( " Should be an RPC error " )
120
+ return false
121
+ }
122
+ #expect( rootError. code == . unavailable)
123
+ #expect(
124
+ rootError. message
125
+ == " The server accepted the TCP connection but closed the connection before completing the HTTP/2 connection preface. "
126
+ )
145
127
146
- return true
128
+ guard
129
+ let sslError = rootError. cause as? NIOSSLExtraError ,
130
+ case . failedToValidateHostname = sslError
131
+ else {
132
+ Issue . record (
133
+ " Should be a NIOSSLExtraError.failedToValidateHostname error, but was: \( String ( describing: rootError. cause) ) "
134
+ )
135
+ return false
147
136
}
148
- )
137
+
138
+ return true
139
+ }
149
140
}
150
141
}
151
142
152
143
@Test (
153
144
" Error is surfaced when server fails client verification " ,
154
- arguments: [ TransportSecurity . posix] ,
155
- [ TransportSecurity . posix]
145
+ arguments: [ TransportKind . posix] ,
146
+ [ TransportKind . posix]
156
147
)
157
148
// Verification should fail because the server does not have trust roots containing the client cert.
158
149
func testServerFailsClientValidation(
159
- clientTransport: TransportSecurity ,
160
- serverTransport: TransportSecurity
150
+ clientTransport: TransportKind ,
151
+ serverTransport: TransportKind
161
152
) async throws {
162
153
let certificateKeyPairs = try SelfSignedCertificateKeyPairs ( )
163
154
let clientTransportConfig = self . makeMTLSClientTLSConfig (
@@ -172,43 +163,40 @@ struct HTTP2TransportTLSEnabledTests {
172
163
)
173
164
174
165
try await self . withClientAndServer (
175
- clientTransportSecurity : clientTransportConfig,
176
- serverTransportSecurity : serverTransportConfig
166
+ clientTLSConfig : clientTransportConfig,
167
+ serverTLSConfig : serverTransportConfig
177
168
) { control in
178
- await #expect(
179
- performing: {
180
- try await self . executeUnaryRPC ( control: control)
181
- } ,
182
- throws: { error in
183
- guard let rootError = error as? RPCError else {
184
- Issue . record ( " Should be an RPC error " )
185
- return false
186
- }
187
- #expect( rootError. code == . unavailable)
188
- #expect(
189
- rootError. message
190
- == " The server accepted the TCP connection but closed the connection before completing the HTTP/2 connection preface. "
191
- )
192
-
193
- guard
194
- let sslError = rootError. cause as? NIOSSL . BoringSSLError ,
195
- case . sslError = sslError
196
- else {
197
- Issue . record (
198
- " Should be a NIOSSL.sslError error, but was: \( String ( describing: rootError. cause) ) "
199
- )
200
- return false
201
- }
169
+ await #expect {
170
+ try await self . executeUnaryRPC ( control: control)
171
+ } throws: { error in
172
+ guard let rootError = error as? RPCError else {
173
+ Issue . record ( " Should be an RPC error " )
174
+ return false
175
+ }
176
+ #expect( rootError. code == . unavailable)
177
+ #expect(
178
+ rootError. message
179
+ == " The server accepted the TCP connection but closed the connection before completing the HTTP/2 connection preface. "
180
+ )
202
181
203
- return true
182
+ guard
183
+ let sslError = rootError. cause as? NIOSSL . BoringSSLError ,
184
+ case . sslError = sslError
185
+ else {
186
+ Issue . record (
187
+ " Should be a NIOSSL.sslError error, but was: \( String ( describing: rootError. cause) ) "
188
+ )
189
+ return false
204
190
}
205
- )
191
+
192
+ return true
193
+ }
206
194
}
207
195
}
208
196
209
197
// - MARK: Test Utilities
210
198
211
- enum TransportSecurity : Sendable {
199
+ enum TransportKind : Sendable {
212
200
case posix
213
201
}
214
202
@@ -223,7 +211,7 @@ struct HTTP2TransportTLSEnabledTests {
223
211
}
224
212
225
213
func makeDefaultClientTLSConfig(
226
- for transportSecurity: TransportSecurity ,
214
+ for transportSecurity: TransportKind ,
227
215
certificateKeyPairs: SelfSignedCertificateKeyPairs
228
216
) -> TLSConfig . Client {
229
217
switch transportSecurity {
@@ -242,11 +230,11 @@ struct HTTP2TransportTLSEnabledTests {
242
230
}
243
231
244
232
func makeMTLSClientTLSConfig(
245
- for transportSecurity : TransportSecurity ,
233
+ for transportKind : TransportKind ,
246
234
certificateKeyPairs: SelfSignedCertificateKeyPairs ,
247
235
serverHostname: String ?
248
236
) -> TLSConfig . Client {
249
- switch transportSecurity {
237
+ switch transportKind {
250
238
case . posix:
251
239
return . posix(
252
240
. tls(
@@ -265,10 +253,10 @@ struct HTTP2TransportTLSEnabledTests {
265
253
}
266
254
267
255
func makeDefaultServerTLSConfig(
268
- for transportSecurity : TransportSecurity ,
256
+ for transportKind : TransportKind ,
269
257
certificateKeyPairs: SelfSignedCertificateKeyPairs
270
258
) -> TLSConfig . Server {
271
- switch transportSecurity {
259
+ switch transportKind {
272
260
case . posix:
273
261
return . posix(
274
262
. tls(
@@ -282,11 +270,11 @@ struct HTTP2TransportTLSEnabledTests {
282
270
}
283
271
284
272
func makeMTLSServerTLSConfig(
285
- for transportSecurity : TransportSecurity ,
273
+ for transportKind : TransportKind ,
286
274
certificateKeyPairs: SelfSignedCertificateKeyPairs ,
287
275
includeClientCertificateInTrustRoots: Bool
288
276
) -> TLSConfig . Server {
289
- switch transportSecurity {
277
+ switch transportKind {
290
278
case . posix:
291
279
return . posix(
292
280
. tls(
@@ -306,12 +294,12 @@ struct HTTP2TransportTLSEnabledTests {
306
294
}
307
295
308
296
func withClientAndServer(
309
- clientTransportSecurity : TLSConfig . Client ,
310
- serverTransportSecurity : TLSConfig . Server ,
297
+ clientTLSConfig : TLSConfig . Client ,
298
+ serverTLSConfig : TLSConfig . Server ,
311
299
_ test: ( ControlClient ) async throws -> Void
312
300
) async throws {
313
301
try await withThrowingDiscardingTaskGroup { group in
314
- let server = self . makeServer ( kind : serverTransportSecurity )
302
+ let server = self . makeServer ( tlsConfig : serverTLSConfig )
315
303
316
304
group. addTask {
317
305
try await server. serve ( )
@@ -322,7 +310,7 @@ struct HTTP2TransportTLSEnabledTests {
322
310
return
323
311
}
324
312
let target : any ResolvableTarget = . ipv4( host: address. host, port: address. port)
325
- let client = try self . makeClient ( kind : clientTransportSecurity , target: target)
313
+ let client = try self . makeClient ( tlsConfig : clientTLSConfig , target: target)
326
314
327
315
group. addTask {
328
316
try await client. run ( )
@@ -336,10 +324,10 @@ struct HTTP2TransportTLSEnabledTests {
336
324
}
337
325
}
338
326
339
- private func makeServer( kind : TLSConfig . Server ) -> GRPCServer {
327
+ private func makeServer( tlsConfig : TLSConfig . Server ) -> GRPCServer {
340
328
let services = [ ControlService ( ) ]
341
329
342
- switch kind {
330
+ switch tlsConfig {
343
331
case . posix( let transportSecurity) :
344
332
let server = GRPCServer (
345
333
transport: . http2NIOPosix(
@@ -354,12 +342,12 @@ struct HTTP2TransportTLSEnabledTests {
354
342
}
355
343
356
344
private func makeClient(
357
- kind : TLSConfig . Client ,
345
+ tlsConfig : TLSConfig . Client ,
358
346
target: any ResolvableTarget
359
347
) throws -> GRPCClient {
360
348
let transport : any ClientTransport
361
349
362
- switch kind {
350
+ switch tlsConfig {
363
351
case . posix( let transportSecurity) :
364
352
transport = try HTTP2ClientTransport . Posix (
365
353
target: target,
0 commit comments