| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444 |
- /*
- * 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.
- */
- import Atomics
- import EchoImplementation
- import EchoModel
- import GRPC
- import HelloWorldModel
- import NIOCore
- import NIOHPACK
- import NIOPosix
- import SwiftProtobuf
- import XCTest
- class InterceptorsTests: GRPCTestCase {
- private var group: EventLoopGroup!
- private var server: Server!
- private var connection: ClientConnection!
- private var echo: Echo_EchoNIOClient!
- private let onCloseCounter = ManagedAtomic<Int>(0)
- override func setUp() {
- super.setUp()
- self.group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
- self.server = try! Server.insecure(group: self.group)
- .withServiceProviders([
- EchoProvider(interceptors: CountOnCloseInterceptors(counter: self.onCloseCounter)),
- HelloWorldProvider(interceptors: HelloWorldServerInterceptorFactory()),
- ])
- .withLogger(self.serverLogger)
- .bind(host: "localhost", port: 0)
- .wait()
- self.connection = ClientConnection.insecure(group: self.group)
- .withBackgroundActivityLogger(self.clientLogger)
- .connect(host: "localhost", port: self.server.channel.localAddress!.port!)
- self.echo = Echo_EchoNIOClient(
- channel: self.connection,
- defaultCallOptions: CallOptions(logger: self.clientLogger),
- interceptors: ReversingInterceptors()
- )
- }
- override func tearDown() {
- super.tearDown()
- XCTAssertNoThrow(try self.connection.close().wait())
- XCTAssertNoThrow(try self.server.close().wait())
- XCTAssertNoThrow(try self.group.syncShutdownGracefully())
- }
- func testEcho() {
- let get = self.echo.get(.with { $0.text = "hello" })
- assertThat(try get.response.wait(), .is(.with { $0.text = "hello :teg ohce tfiwS" }))
- assertThat(try get.status.wait(), .hasCode(.ok))
- XCTAssertEqual(self.onCloseCounter.load(ordering: .sequentiallyConsistent), 1)
- }
- func testCollect() {
- let collect = self.echo.collect()
- collect.sendMessage(.with { $0.text = "1 2" }, promise: nil)
- collect.sendMessage(.with { $0.text = "3 4" }, promise: nil)
- collect.sendEnd(promise: nil)
- assertThat(try collect.response.wait(), .is(.with { $0.text = "3 4 1 2 :tcelloc ohce tfiwS" }))
- assertThat(try collect.status.wait(), .hasCode(.ok))
- XCTAssertEqual(self.onCloseCounter.load(ordering: .sequentiallyConsistent), 1)
- }
- func testExpand() {
- let expand = self.echo.expand(.with { $0.text = "hello" }) { response in
- // Expand splits on spaces, so we only expect one response.
- assertThat(response, .is(.with { $0.text = "hello :)0( dnapxe ohce tfiwS" }))
- }
- assertThat(try expand.status.wait(), .hasCode(.ok))
- XCTAssertEqual(self.onCloseCounter.load(ordering: .sequentiallyConsistent), 1)
- }
- func testUpdate() {
- let update = self.echo.update { response in
- // We'll just send the one message, so only expect one response.
- assertThat(response, .is(.with { $0.text = "hello :)0( etadpu ohce tfiwS" }))
- }
- update.sendMessage(.with { $0.text = "hello" }, promise: nil)
- update.sendEnd(promise: nil)
- assertThat(try update.status.wait(), .hasCode(.ok))
- XCTAssertEqual(self.onCloseCounter.load(ordering: .sequentiallyConsistent), 1)
- }
- func testSayHello() {
- var greeter = Helloworld_GreeterNIOClient(
- channel: self.connection,
- defaultCallOptions: CallOptions(logger: self.clientLogger)
- )
- // Make a call without interceptors.
- let notAuthed = greeter.sayHello(.with { $0.name = "World" })
- assertThat(try notAuthed.response.wait(), .throws())
- assertThat(
- try notAuthed.trailingMetadata.wait(),
- .contains("www-authenticate", ["Magic"])
- )
- assertThat(try notAuthed.status.wait(), .hasCode(.unauthenticated))
- // Add an interceptor factory.
- greeter.interceptors = HelloWorldClientInterceptorFactory(client: greeter)
- // Make sure we break the reference cycle.
- defer {
- greeter.interceptors = nil
- }
- // Try again with the not-really-auth interceptor:
- let hello = greeter.sayHello(.with { $0.name = "PanCakes" })
- assertThat(
- try hello.response.map { $0.message }.wait(),
- .is(.equalTo("Hello, PanCakes, you're authorized!"))
- )
- assertThat(try hello.status.wait(), .hasCode(.ok))
- }
- }
- // MARK: - Helpers
- class HelloWorldProvider: Helloworld_GreeterProvider {
- var interceptors: Helloworld_GreeterServerInterceptorFactoryProtocol?
- init(interceptors: Helloworld_GreeterServerInterceptorFactoryProtocol? = nil) {
- self.interceptors = interceptors
- }
- func sayHello(
- request: Helloworld_HelloRequest,
- context: StatusOnlyCallContext
- ) -> EventLoopFuture<Helloworld_HelloReply> {
- // Since we're auth'd, the 'userInfo' should have some magic set.
- assertThat(context.userInfo.magic, .is("Magic"))
- let response = Helloworld_HelloReply.with {
- $0.message = "Hello, \(request.name), you're authorized!"
- }
- return context.eventLoop.makeSucceededFuture(response)
- }
- }
- extension HelloWorldClientInterceptorFactory: @unchecked Sendable {}
- private class HelloWorldClientInterceptorFactory:
- Helloworld_GreeterClientInterceptorFactoryProtocol
- {
- var client: Helloworld_GreeterNIOClient
- init(client: Helloworld_GreeterNIOClient) {
- self.client = client
- }
- func makeSayHelloInterceptors() -> [ClientInterceptor<
- Helloworld_HelloRequest, Helloworld_HelloReply
- >] {
- return [NotReallyAuthClientInterceptor(client: self.client)]
- }
- }
- class RemoteAddressExistsInterceptor<Request, Response>:
- ServerInterceptor<Request, Response>, @unchecked Sendable
- {
- override func receive(
- _ part: GRPCServerRequestPart<Request>,
- context: ServerInterceptorContext<Request, Response>
- ) {
- XCTAssertNotNil(context.remoteAddress)
- super.receive(part, context: context)
- }
- }
- class NotReallyAuthServerInterceptor<Request: Message, Response: Message>:
- ServerInterceptor<Request, Response>,
- @unchecked Sendable
- {
- override func receive(
- _ part: GRPCServerRequestPart<Request>,
- context: ServerInterceptorContext<Request, Response>
- ) {
- switch part {
- case let .metadata(headers):
- if let auth = headers.first(name: "authorization"), auth == "Magic" {
- context.userInfo.magic = auth
- context.receive(part)
- } else {
- // Not auth'd. Fail the RPC.
- let status = GRPCStatus(code: .unauthenticated, message: "You need some magic auth!")
- let trailers = HPACKHeaders([("www-authenticate", "Magic")])
- context.send(.end(status, trailers), promise: nil)
- }
- case .message, .end:
- context.receive(part)
- }
- }
- }
- final class HelloWorldServerInterceptorFactory: Helloworld_GreeterServerInterceptorFactoryProtocol {
- func makeSayHelloInterceptors() -> [ServerInterceptor<
- Helloworld_HelloRequest, Helloworld_HelloReply
- >] {
- return [RemoteAddressExistsInterceptor(), NotReallyAuthServerInterceptor()]
- }
- }
- class NotReallyAuthClientInterceptor<Request: Message, Response: Message>:
- ClientInterceptor<Request, Response>, @unchecked Sendable
- {
- private let client: Helloworld_GreeterNIOClient
- private enum State {
- // We're trying the call, these are the parts we've sent so far.
- case trying([GRPCClientRequestPart<Request>])
- // We're retrying using this call.
- case retrying(Call<Request, Response>)
- }
- private var state: State = .trying([])
- init(client: Helloworld_GreeterNIOClient) {
- self.client = client
- }
- override func cancel(
- promise: EventLoopPromise<Void>?,
- context: ClientInterceptorContext<Request, Response>
- ) {
- switch self.state {
- case .trying:
- context.cancel(promise: promise)
- case let .retrying(call):
- call.cancel(promise: promise)
- context.cancel(promise: nil)
- }
- }
- override func send(
- _ part: GRPCClientRequestPart<Request>,
- promise: EventLoopPromise<Void>?,
- context: ClientInterceptorContext<Request, Response>
- ) {
- switch self.state {
- case var .trying(parts):
- // Record the part, incase we need to retry.
- parts.append(part)
- self.state = .trying(parts)
- // Forward the request part.
- context.send(part, promise: promise)
- case let .retrying(call):
- // We're retrying, send the part to the retry call.
- call.send(part, promise: promise)
- }
- }
- override func receive(
- _ part: GRPCClientResponsePart<Response>,
- context: ClientInterceptorContext<Request, Response>
- ) {
- switch self.state {
- case var .trying(parts):
- switch part {
- // If 'authentication' fails this is the only part we expect, we can forward everything else.
- case let .end(status, trailers) where status.code == .unauthenticated:
- // We only know how to deal with magic.
- guard trailers.first(name: "www-authenticate") == "Magic" else {
- // We can't handle this, fail.
- context.receive(part)
- return
- }
- // We know how to handle this: make a new call.
- let call: Call<Request, Response> = self.client.channel.makeCall(
- path: context.path,
- type: context.type,
- callOptions: context.options,
- // We could grab interceptors from the client, but we don't need to.
- interceptors: []
- )
- // We're retying the call now.
- self.state = .retrying(call)
- // Invoke the call and redirect responses here.
- call.invoke(onError: context.errorCaught(_:), onResponsePart: context.receive(_:))
- // Parts must contain the metadata as the first item if we got that first response.
- if case var .some(.metadata(metadata)) = parts.first {
- metadata.replaceOrAdd(name: "authorization", value: "Magic")
- parts[0] = .metadata(metadata)
- }
- // Now replay any requests on the retry call.
- for part in parts {
- call.send(part, promise: nil)
- }
- default:
- context.receive(part)
- }
- case .retrying:
- // Ignore anything we receive on the original call.
- ()
- }
- }
- }
- final class EchoReverseInterceptor: ClientInterceptor<Echo_EchoRequest, Echo_EchoResponse>,
- @unchecked Sendable
- {
- override func send(
- _ part: GRPCClientRequestPart<Echo_EchoRequest>,
- promise: EventLoopPromise<Void>?,
- context: ClientInterceptorContext<Echo_EchoRequest, Echo_EchoResponse>
- ) {
- switch part {
- case .message(var request, let metadata):
- request.text = String(request.text.reversed())
- context.send(.message(request, metadata), promise: promise)
- default:
- context.send(part, promise: promise)
- }
- }
- override func receive(
- _ part: GRPCClientResponsePart<Echo_EchoResponse>,
- context: ClientInterceptorContext<Echo_EchoRequest, Echo_EchoResponse>
- ) {
- switch part {
- case var .message(response):
- response.text = String(response.text.reversed())
- context.receive(.message(response))
- default:
- context.receive(part)
- }
- }
- }
- final class ReversingInterceptors: Echo_EchoClientInterceptorFactoryProtocol {
- // This interceptor is stateless, let's just share it.
- private let interceptors = [EchoReverseInterceptor()]
- func makeGetInterceptors() -> [ClientInterceptor<Echo_EchoRequest, Echo_EchoResponse>] {
- return self.interceptors
- }
- func makeExpandInterceptors() -> [ClientInterceptor<Echo_EchoRequest, Echo_EchoResponse>] {
- return self.interceptors
- }
- func makeCollectInterceptors() -> [ClientInterceptor<Echo_EchoRequest, Echo_EchoResponse>] {
- return self.interceptors
- }
- func makeUpdateInterceptors() -> [ClientInterceptor<Echo_EchoRequest, Echo_EchoResponse>] {
- return self.interceptors
- }
- }
- final class CountOnCloseInterceptors: Echo_EchoServerInterceptorFactoryProtocol {
- // This interceptor is stateless, let's just share it.
- private let interceptors: [ServerInterceptor<Echo_EchoRequest, Echo_EchoResponse>]
- init(counter: ManagedAtomic<Int>) {
- self.interceptors = [CountOnCloseServerInterceptor(counter: counter)]
- }
- func makeGetInterceptors() -> [ServerInterceptor<Echo_EchoRequest, Echo_EchoResponse>] {
- return self.interceptors
- }
- func makeExpandInterceptors() -> [ServerInterceptor<Echo_EchoRequest, Echo_EchoResponse>] {
- return self.interceptors
- }
- func makeCollectInterceptors() -> [ServerInterceptor<Echo_EchoRequest, Echo_EchoResponse>] {
- return self.interceptors
- }
- func makeUpdateInterceptors() -> [ServerInterceptor<Echo_EchoRequest, Echo_EchoResponse>] {
- return self.interceptors
- }
- }
- final class CountOnCloseServerInterceptor: ServerInterceptor<Echo_EchoRequest, Echo_EchoResponse>,
- @unchecked Sendable
- {
- private let counter: ManagedAtomic<Int>
- init(counter: ManagedAtomic<Int>) {
- self.counter = counter
- }
- override func receive(
- _ part: GRPCServerRequestPart<Echo_EchoRequest>,
- context: ServerInterceptorContext<Echo_EchoRequest, Echo_EchoResponse>
- ) {
- switch part {
- case .metadata:
- context.closeFuture.whenComplete { _ in
- self.counter.wrappingIncrement(ordering: .sequentiallyConsistent)
- }
- default:
- ()
- }
- context.receive(part)
- }
- }
- private enum MagicKey: UserInfo.Key {
- typealias Value = String
- }
- extension UserInfo {
- fileprivate var magic: MagicKey.Value? {
- get {
- return self[MagicKey.self]
- }
- set {
- self[MagicKey.self] = newValue
- }
- }
- }
|