forked from grpc/grpc-swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGRPCServerPipelineConfiguratorTests.swift
271 lines (228 loc) · 9.83 KB
/
GRPCServerPipelineConfiguratorTests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*
* Copyright 2020, gRPC Authors All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
@testable import GRPC
import NIOCore
import NIOEmbedded
import NIOHTTP2
import NIOTLS
import XCTest
class GRPCServerPipelineConfiguratorTests: GRPCTestCase {
private var channel: EmbeddedChannel!
private func assertConfigurator(isPresent: Bool) {
assertThat(
try self.channel.pipeline.handler(type: GRPCServerPipelineConfigurator.self).wait(),
isPresent ? .doesNotThrow() : .throws()
)
}
private func assertHTTP2Handler(isPresent: Bool) {
assertThat(
try self.channel.pipeline.handler(type: NIOHTTP2Handler.self).wait(),
isPresent ? .doesNotThrow() : .throws()
)
}
private func assertGRPCWebToHTTP2Handler(isPresent: Bool) {
assertThat(
try self.channel.pipeline.handler(type: GRPCWebToHTTP2ServerCodec.self).wait(),
isPresent ? .doesNotThrow() : .throws()
)
}
private func setUp(tls: Bool, requireALPN: Bool = true) {
self.channel = EmbeddedChannel()
var configuration = Server.Configuration.default(
target: .unixDomainSocket("/ignored"),
eventLoopGroup: self.channel.eventLoop,
serviceProviders: []
)
configuration.logger = self.serverLogger
if tls {
#if canImport(NIOSSL)
configuration.tlsConfiguration = .makeServerConfigurationBackedByNIOSSL(
certificateChain: [],
privateKey: .file("not used"),
requireALPN: requireALPN
)
#else
fatalError("TLS enabled for a test when NIOSSL is not available")
#endif
}
let handler = GRPCServerPipelineConfigurator(configuration: configuration)
assertThat(try self.channel.pipeline.addHandler(handler).wait(), .doesNotThrow())
}
#if canImport(NIOSSL)
func testHTTP2SetupViaALPN() {
self.setUp(tls: true, requireALPN: true)
let event = TLSUserEvent.handshakeCompleted(negotiatedProtocol: "h2")
self.channel.pipeline.fireUserInboundEventTriggered(event)
self.assertConfigurator(isPresent: false)
self.assertHTTP2Handler(isPresent: true)
}
func testGRPCExpSetupViaALPN() {
self.setUp(tls: true, requireALPN: true)
let event = TLSUserEvent.handshakeCompleted(negotiatedProtocol: "grpc-exp")
self.channel.pipeline.fireUserInboundEventTriggered(event)
self.assertConfigurator(isPresent: false)
self.assertHTTP2Handler(isPresent: true)
}
func testHTTP1Dot1SetupViaALPN() {
self.setUp(tls: true, requireALPN: true)
let event = TLSUserEvent.handshakeCompleted(negotiatedProtocol: "http/1.1")
self.channel.pipeline.fireUserInboundEventTriggered(event)
self.assertConfigurator(isPresent: false)
self.assertGRPCWebToHTTP2Handler(isPresent: true)
}
func testUnrecognisedALPNCloses() {
self.setUp(tls: true, requireALPN: true)
let event = TLSUserEvent.handshakeCompleted(negotiatedProtocol: "unsupported")
self.channel.pipeline.fireUserInboundEventTriggered(event)
self.channel.embeddedEventLoop.run()
assertThat(try self.channel.closeFuture.wait(), .doesNotThrow())
}
func testNoNegotiatedProtocolCloses() {
self.setUp(tls: true, requireALPN: true)
let event = TLSUserEvent.handshakeCompleted(negotiatedProtocol: nil)
self.channel.pipeline.fireUserInboundEventTriggered(event)
self.channel.embeddedEventLoop.run()
assertThat(try self.channel.closeFuture.wait(), .doesNotThrow())
}
func testNoNegotiatedProtocolFallbackToBytesWhenALPNNotRequired() throws {
self.setUp(tls: true, requireALPN: false)
// Require ALPN is disabled, so this is a no-op.
let event = TLSUserEvent.handshakeCompleted(negotiatedProtocol: nil)
self.channel.pipeline.fireUserInboundEventTriggered(event)
// Configure via bytes.
let bytes = ByteBuffer(staticString: "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n")
assertThat(try self.channel.writeInbound(bytes), .doesNotThrow())
self.assertConfigurator(isPresent: false)
self.assertHTTP2Handler(isPresent: true)
}
#endif // canImport(NIOSSL)
func testHTTP2SetupViaBytes() {
self.setUp(tls: false)
let bytes = ByteBuffer(staticString: "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n")
assertThat(try self.channel.writeInbound(bytes), .doesNotThrow())
self.assertConfigurator(isPresent: false)
self.assertHTTP2Handler(isPresent: true)
}
func testHTTP2SetupViaBytesDripFed() {
self.setUp(tls: false)
var bytes = ByteBuffer(staticString: "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n")
var head = bytes.readSlice(length: bytes.readableBytes - 1)!
let tail = bytes.readSlice(length: 1)!
while let slice = head.readSlice(length: 1) {
assertThat(try self.channel.writeInbound(slice), .doesNotThrow())
self.assertConfigurator(isPresent: true)
self.assertHTTP2Handler(isPresent: false)
}
// Final byte.
assertThat(try self.channel.writeInbound(tail), .doesNotThrow())
self.assertConfigurator(isPresent: false)
self.assertHTTP2Handler(isPresent: true)
}
func testHTTP1Dot1SetupViaBytes() {
self.setUp(tls: false)
let bytes = ByteBuffer(staticString: "GET http://www.foo.bar HTTP/1.1\r\n")
assertThat(try self.channel.writeInbound(bytes), .doesNotThrow())
self.assertConfigurator(isPresent: false)
self.assertGRPCWebToHTTP2Handler(isPresent: true)
}
func testHTTP1Dot1SetupViaBytesDripFed() {
self.setUp(tls: false)
var bytes = ByteBuffer(staticString: "GET http://www.foo.bar HTTP/1.1\r\n")
var head = bytes.readSlice(length: bytes.readableBytes - 1)!
let tail = bytes.readSlice(length: 1)!
while let slice = head.readSlice(length: 1) {
assertThat(try self.channel.writeInbound(slice), .doesNotThrow())
self.assertConfigurator(isPresent: true)
self.assertGRPCWebToHTTP2Handler(isPresent: false)
}
// Final byte.
assertThat(try self.channel.writeInbound(tail), .doesNotThrow())
self.assertConfigurator(isPresent: false)
self.assertGRPCWebToHTTP2Handler(isPresent: true)
}
func testUnexpectedInputClosesEventuallyWhenDripFed() {
self.setUp(tls: false)
var bytes = ByteBuffer(repeating: UInt8(ascii: "a"), count: 2048)
while let slice = bytes.readSlice(length: 1) {
assertThat(try self.channel.writeInbound(slice), .doesNotThrow())
self.assertConfigurator(isPresent: true)
self.assertHTTP2Handler(isPresent: false)
self.assertGRPCWebToHTTP2Handler(isPresent: false)
}
self.channel.embeddedEventLoop.run()
assertThat(try self.channel.closeFuture.wait(), .doesNotThrow())
}
func testReadsAreUnbufferedAfterConfiguration() throws {
self.setUp(tls: false)
var bytes = ByteBuffer(staticString: "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n")
// A SETTINGS frame MUST follow the connection preface. Append one so that the HTTP/2 handler
// responds with its initial settings (and we validate that we forward frames once configuring).
let emptySettingsFrameBytes: [UInt8] = [
0x00, 0x00, 0x00, // 3-byte payload length (0 bytes)
0x04, // 1-byte frame type (SETTINGS)
0x00, // 1-byte flags (none)
0x00, 0x00, 0x00, 0x00, // 4-byte stream identifier
]
bytes.writeBytes(emptySettingsFrameBytes)
// Do the setup.
assertThat(try self.channel.writeInbound(bytes), .doesNotThrow())
self.assertConfigurator(isPresent: false)
self.assertHTTP2Handler(isPresent: true)
// We expect the server to respond with a SETTINGS frame now.
let ioData = try channel.readOutbound(as: IOData.self)
switch ioData {
case var .some(.byteBuffer(buffer)):
if let frame = buffer.readBytes(length: 9) {
// Just check it's a SETTINGS frame.
assertThat(frame[3], .is(0x04))
} else {
XCTFail("Expected more bytes")
}
default:
XCTFail("Expected ByteBuffer but got \(String(describing: ioData))")
}
}
#if canImport(NIOSSL)
func testALPNIsPreferredOverBytes() throws {
self.setUp(tls: true, requireALPN: true)
// Write in an HTTP/1 request line. This should just be buffered.
let bytes = ByteBuffer(staticString: "GET http://www.foo.bar HTTP/1.1\r\n")
assertThat(try self.channel.writeInbound(bytes), .doesNotThrow())
self.assertConfigurator(isPresent: true)
self.assertHTTP2Handler(isPresent: false)
self.assertGRPCWebToHTTP2Handler(isPresent: false)
// Now configure HTTP/2 with ALPN. This should be used to configure the pipeline.
let event = TLSUserEvent.handshakeCompleted(negotiatedProtocol: "h2")
self.channel.pipeline.fireUserInboundEventTriggered(event)
self.assertConfigurator(isPresent: false)
self.assertGRPCWebToHTTP2Handler(isPresent: false)
self.assertHTTP2Handler(isPresent: true)
}
func testALPNFallbackToAlreadyBufferedBytes() throws {
self.setUp(tls: true, requireALPN: false)
// Write in an HTTP/2 connection preface. This should just be buffered.
let bytes = ByteBuffer(staticString: "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n")
assertThat(try self.channel.writeInbound(bytes), .doesNotThrow())
self.assertConfigurator(isPresent: true)
self.assertHTTP2Handler(isPresent: false)
// Complete the handshake with no protocol negotiated, we should fallback to the buffered bytes.
let event = TLSUserEvent.handshakeCompleted(negotiatedProtocol: nil)
self.channel.pipeline.fireUserInboundEventTriggered(event)
self.assertConfigurator(isPresent: false)
self.assertHTTP2Handler(isPresent: true)
}
#endif // canImport(NIOSSL)
}