Skip to content

Commit 7727cde

Browse files
committed
PR changes
1 parent 471bc67 commit 7727cde

File tree

1 file changed

+92
-104
lines changed

1 file changed

+92
-104
lines changed

Tests/GRPCNIOTransportHTTP2Tests/HTTP2TransportTLSEnabledTests.swift

Lines changed: 92 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ struct HTTP2TransportTLSEnabledTests {
2828

2929
@Test(
3030
"When using defaults, server does not perform client verification",
31-
arguments: [TransportSecurity.posix],
32-
[TransportSecurity.posix]
31+
arguments: [TransportKind.posix],
32+
[TransportKind.posix]
3333
)
3434
func testRPC_Defaults_OK(
35-
clientTransport: TransportSecurity,
36-
serverTransport: TransportSecurity
35+
clientTransport: TransportKind,
36+
serverTransport: TransportKind
3737
) async throws {
3838
let certificateKeyPairs = try SelfSignedCertificateKeyPairs()
3939
let clientTransportConfig = self.makeDefaultClientTLSConfig(
@@ -46,26 +46,23 @@ struct HTTP2TransportTLSEnabledTests {
4646
)
4747

4848
try await self.withClientAndServer(
49-
clientTransportSecurity: clientTransportConfig,
50-
serverTransportSecurity: serverTransportConfig
49+
clientTLSConfig: clientTransportConfig,
50+
serverTLSConfig: serverTransportConfig
5151
) { 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+
}
5855
}
5956
}
6057

6158
@Test(
6259
"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]
6562
)
6663
func testRPC_mTLS_OK(
67-
clientTransport: TransportSecurity,
68-
serverTransport: TransportSecurity
64+
clientTransport: TransportKind,
65+
serverTransport: TransportKind
6966
) async throws {
7067
let certificateKeyPairs = try SelfSignedCertificateKeyPairs()
7168
let clientTransportConfig = self.makeMTLSClientTLSConfig(
@@ -80,27 +77,24 @@ struct HTTP2TransportTLSEnabledTests {
8077
)
8178

8279
try await self.withClientAndServer(
83-
clientTransportSecurity: clientTransportConfig,
84-
serverTransportSecurity: serverTransportConfig
80+
clientTLSConfig: clientTransportConfig,
81+
serverTLSConfig: serverTransportConfig
8582
) { 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+
}
9286
}
9387
}
9488

9589
@Test(
9690
"Error is surfaced when client fails server verification",
97-
arguments: [TransportSecurity.posix],
98-
[TransportSecurity.posix]
91+
arguments: [TransportKind.posix],
92+
[TransportKind.posix]
9993
)
10094
// Verification should fail because the custom hostname is missing on the client.
10195
func testClientFailsServerValidation(
102-
clientTransport: TransportSecurity,
103-
serverTransport: TransportSecurity
96+
clientTransport: TransportKind,
97+
serverTransport: TransportKind
10498
) async throws {
10599
let certificateKeyPairs = try SelfSignedCertificateKeyPairs()
106100
let clientTransportConfig = self.makeMTLSClientTLSConfig(
@@ -115,49 +109,46 @@ struct HTTP2TransportTLSEnabledTests {
115109
)
116110

117111
try await self.withClientAndServer(
118-
clientTransportSecurity: clientTransportConfig,
119-
serverTransportSecurity: serverTransportConfig
112+
clientTLSConfig: clientTransportConfig,
113+
serverTLSConfig: serverTransportConfig
120114
) { 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+
)
145127

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
147136
}
148-
)
137+
138+
return true
139+
}
149140
}
150141
}
151142

152143
@Test(
153144
"Error is surfaced when server fails client verification",
154-
arguments: [TransportSecurity.posix],
155-
[TransportSecurity.posix]
145+
arguments: [TransportKind.posix],
146+
[TransportKind.posix]
156147
)
157148
// Verification should fail because the server does not have trust roots containing the client cert.
158149
func testServerFailsClientValidation(
159-
clientTransport: TransportSecurity,
160-
serverTransport: TransportSecurity
150+
clientTransport: TransportKind,
151+
serverTransport: TransportKind
161152
) async throws {
162153
let certificateKeyPairs = try SelfSignedCertificateKeyPairs()
163154
let clientTransportConfig = self.makeMTLSClientTLSConfig(
@@ -172,43 +163,40 @@ struct HTTP2TransportTLSEnabledTests {
172163
)
173164

174165
try await self.withClientAndServer(
175-
clientTransportSecurity: clientTransportConfig,
176-
serverTransportSecurity: serverTransportConfig
166+
clientTLSConfig: clientTransportConfig,
167+
serverTLSConfig: serverTransportConfig
177168
) { 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+
)
202181

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
204190
}
205-
)
191+
192+
return true
193+
}
206194
}
207195
}
208196

209197
// - MARK: Test Utilities
210198

211-
enum TransportSecurity: Sendable {
199+
enum TransportKind: Sendable {
212200
case posix
213201
}
214202

@@ -223,7 +211,7 @@ struct HTTP2TransportTLSEnabledTests {
223211
}
224212

225213
func makeDefaultClientTLSConfig(
226-
for transportSecurity: TransportSecurity,
214+
for transportSecurity: TransportKind,
227215
certificateKeyPairs: SelfSignedCertificateKeyPairs
228216
) -> TLSConfig.Client {
229217
switch transportSecurity {
@@ -242,11 +230,11 @@ struct HTTP2TransportTLSEnabledTests {
242230
}
243231

244232
func makeMTLSClientTLSConfig(
245-
for transportSecurity: TransportSecurity,
233+
for transportKind: TransportKind,
246234
certificateKeyPairs: SelfSignedCertificateKeyPairs,
247235
serverHostname: String?
248236
) -> TLSConfig.Client {
249-
switch transportSecurity {
237+
switch transportKind {
250238
case .posix:
251239
return .posix(
252240
.tls(
@@ -265,10 +253,10 @@ struct HTTP2TransportTLSEnabledTests {
265253
}
266254

267255
func makeDefaultServerTLSConfig(
268-
for transportSecurity: TransportSecurity,
256+
for transportKind: TransportKind,
269257
certificateKeyPairs: SelfSignedCertificateKeyPairs
270258
) -> TLSConfig.Server {
271-
switch transportSecurity {
259+
switch transportKind {
272260
case .posix:
273261
return .posix(
274262
.tls(
@@ -282,11 +270,11 @@ struct HTTP2TransportTLSEnabledTests {
282270
}
283271

284272
func makeMTLSServerTLSConfig(
285-
for transportSecurity: TransportSecurity,
273+
for transportKind: TransportKind,
286274
certificateKeyPairs: SelfSignedCertificateKeyPairs,
287275
includeClientCertificateInTrustRoots: Bool
288276
) -> TLSConfig.Server {
289-
switch transportSecurity {
277+
switch transportKind {
290278
case .posix:
291279
return .posix(
292280
.tls(
@@ -306,12 +294,12 @@ struct HTTP2TransportTLSEnabledTests {
306294
}
307295

308296
func withClientAndServer(
309-
clientTransportSecurity: TLSConfig.Client,
310-
serverTransportSecurity: TLSConfig.Server,
297+
clientTLSConfig: TLSConfig.Client,
298+
serverTLSConfig: TLSConfig.Server,
311299
_ test: (ControlClient) async throws -> Void
312300
) async throws {
313301
try await withThrowingDiscardingTaskGroup { group in
314-
let server = self.makeServer(kind: serverTransportSecurity)
302+
let server = self.makeServer(tlsConfig: serverTLSConfig)
315303

316304
group.addTask {
317305
try await server.serve()
@@ -322,7 +310,7 @@ struct HTTP2TransportTLSEnabledTests {
322310
return
323311
}
324312
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)
326314

327315
group.addTask {
328316
try await client.run()
@@ -336,10 +324,10 @@ struct HTTP2TransportTLSEnabledTests {
336324
}
337325
}
338326

339-
private func makeServer(kind: TLSConfig.Server) -> GRPCServer {
327+
private func makeServer(tlsConfig: TLSConfig.Server) -> GRPCServer {
340328
let services = [ControlService()]
341329

342-
switch kind {
330+
switch tlsConfig {
343331
case .posix(let transportSecurity):
344332
let server = GRPCServer(
345333
transport: .http2NIOPosix(
@@ -354,12 +342,12 @@ struct HTTP2TransportTLSEnabledTests {
354342
}
355343

356344
private func makeClient(
357-
kind: TLSConfig.Client,
345+
tlsConfig: TLSConfig.Client,
358346
target: any ResolvableTarget
359347
) throws -> GRPCClient {
360348
let transport: any ClientTransport
361349

362-
switch kind {
350+
switch tlsConfig {
363351
case .posix(let transportSecurity):
364352
transport = try HTTP2ClientTransport.Posix(
365353
target: target,

0 commit comments

Comments
 (0)