|
| 1 | +/* |
| 2 | + * Copyright 2020, gRPC Authors All rights reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +import Dispatch |
| 17 | +import Foundation |
| 18 | +import NIO |
| 19 | +import NIOSSL |
| 20 | +import NIOTransportServices |
| 21 | +import GRPC |
| 22 | +import GRPCSampleData |
| 23 | +import EchoModel |
| 24 | +import EchoImplementation |
| 25 | +import XCTest |
| 26 | + |
| 27 | +final class ZeroLengthWriteTests: GRPCTestCase { |
| 28 | + func clientBuilder(group: EventLoopGroup, secure: Bool, debugInitializer: @escaping (Channel) -> EventLoopFuture<Void>) -> ClientConnection.Builder { |
| 29 | + if secure { |
| 30 | + return ClientConnection.secure(group: group) |
| 31 | + .withTLS(trustRoots: .certificates([SampleCertificate.ca.certificate])) |
| 32 | + .withDebugChannelInitializer(debugInitializer) |
| 33 | + } else { |
| 34 | + return ClientConnection.insecure(group: group) |
| 35 | + .withDebugChannelInitializer(debugInitializer) |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + func serverBuilder(group: EventLoopGroup, secure: Bool, debugInitializer: @escaping (Channel) -> EventLoopFuture<Void>) -> Server.Builder { |
| 40 | + if secure { |
| 41 | + return Server.secure( |
| 42 | + group: group, |
| 43 | + certificateChain: [SampleCertificate.server.certificate], |
| 44 | + privateKey: SamplePrivateKey.server |
| 45 | + ).withTLS(trustRoots: .certificates([SampleCertificate.ca.certificate])) |
| 46 | + .withDebugChannelInitializer(debugInitializer) |
| 47 | + } else { |
| 48 | + return Server.insecure(group: group) |
| 49 | + .withDebugChannelInitializer(debugInitializer) |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + func makeServer(group: EventLoopGroup, secure: Bool, debugInitializer: @escaping (Channel) -> EventLoopFuture<Void>) throws -> Server { |
| 54 | + return try self.serverBuilder(group: group, secure: secure, debugInitializer: debugInitializer) |
| 55 | + .withServiceProviders([self.makeEchoProvider()]) |
| 56 | + .withLogger(self.serverLogger) |
| 57 | + .bind(host: "localhost", port: 0) |
| 58 | + .wait() |
| 59 | + } |
| 60 | + |
| 61 | + func makeClientConnection(group: EventLoopGroup, secure: Bool, port: Int, debugInitializer: @escaping (Channel) -> EventLoopFuture<Void>) throws -> ClientConnection { |
| 62 | + return self.clientBuilder(group: group, secure: secure, debugInitializer: debugInitializer) |
| 63 | + .withBackgroundActivityLogger(self.clientLogger) |
| 64 | + .connect(host: "localhost", port: port) |
| 65 | + } |
| 66 | + |
| 67 | + func makeEchoProvider() -> Echo_EchoProvider { return EchoProvider() } |
| 68 | + |
| 69 | + func makeEchoClient(group: EventLoopGroup, secure: Bool, port: Int, debugInitializer: @escaping (Channel) -> EventLoopFuture<Void>) throws -> Echo_EchoClient { |
| 70 | + return Echo_EchoClient( |
| 71 | + channel: try self.makeClientConnection(group: group, secure: secure, port: port, debugInitializer: debugInitializer), |
| 72 | + defaultCallOptions: self.callOptionsWithLogger |
| 73 | + ) |
| 74 | + } |
| 75 | + |
| 76 | + func zeroLengthWriteExpectation() -> XCTestExpectation { |
| 77 | + let expectation = self.expectation(description: "Expecting zero length write workaround") |
| 78 | + expectation.expectedFulfillmentCount = 1 |
| 79 | + expectation.assertForOverFulfill = true |
| 80 | + return expectation |
| 81 | + } |
| 82 | + |
| 83 | + func noZeroLengthWriteExpectation() -> XCTestExpectation { |
| 84 | + let expectation = self.expectation(description: "Not expecting zero length write workaround") |
| 85 | + expectation.isInverted = true |
| 86 | + return expectation |
| 87 | + } |
| 88 | + |
| 89 | + func debugPipelineExpectation(_ expectation: XCTestExpectation) -> (Channel) -> EventLoopFuture<Void> { |
| 90 | + return { channel in |
| 91 | + channel.pipeline.handler(type: NIOFilterEmptyWritesHandler.self).map { _ in |
| 92 | + expectation.fulfill() |
| 93 | + }.recover { _ in () } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + private func _runTest(networkPreference: NetworkPreference, secure: Bool, clientExpectation: XCTestExpectation, serverExpectation: XCTestExpectation) { |
| 98 | + // We can only run this test on platforms where the zero-length write workaround _could_ be added. |
| 99 | + #if canImport(Network) |
| 100 | + guard #available(OSX 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *) else { return } |
| 101 | + let group = PlatformSupport.makeEventLoopGroup( |
| 102 | + loopCount: 1, |
| 103 | + networkPreference: networkPreference) |
| 104 | + let server = try! self.makeServer(group: group, secure: secure, debugInitializer: self.debugPipelineExpectation(serverExpectation)) |
| 105 | + |
| 106 | + defer { |
| 107 | + XCTAssertNoThrow(try server.close().wait()) |
| 108 | + XCTAssertNoThrow(try group.syncShutdownGracefully()) |
| 109 | + } |
| 110 | + |
| 111 | + let port = server.channel.localAddress!.port! |
| 112 | + let client = try! self.makeEchoClient(group: group, secure: secure, port: port, debugInitializer: self.debugPipelineExpectation(clientExpectation)) |
| 113 | + defer { |
| 114 | + XCTAssertNoThrow(try client.channel.close().wait()) |
| 115 | + } |
| 116 | + |
| 117 | + // We need to wait here to confirm that the RPC completes. All expectations should have completed by then. |
| 118 | + let call = client.get(Echo_EchoRequest(text: "foo bar baz")) |
| 119 | + XCTAssertNoThrow(try call.status.wait()) |
| 120 | + self.waitForExpectations(timeout: 1.0) |
| 121 | + #endif |
| 122 | + } |
| 123 | + |
| 124 | + func testZeroLengthWriteTestPosixSecure() throws { |
| 125 | + // We can only run this test on platforms where the zero-length write workaround _could_ be added. |
| 126 | + #if canImport(Network) |
| 127 | + guard #available(OSX 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *) else { return } |
| 128 | + |
| 129 | + let serverExpectation = self.noZeroLengthWriteExpectation() |
| 130 | + let clientExpectation = self.noZeroLengthWriteExpectation() |
| 131 | + self._runTest(networkPreference: .userDefined(.posix), secure: true, clientExpectation: clientExpectation, serverExpectation: serverExpectation) |
| 132 | + #endif |
| 133 | + } |
| 134 | + |
| 135 | + func testZeroLengthWriteTestPosixInsecure() throws { |
| 136 | + // We can only run this test on platforms where the zero-length write workaround _could_ be added. |
| 137 | + #if canImport(Network) |
| 138 | + guard #available(OSX 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *) else { return } |
| 139 | + |
| 140 | + let serverExpectation = self.noZeroLengthWriteExpectation() |
| 141 | + let clientExpectation = self.noZeroLengthWriteExpectation() |
| 142 | + self._runTest(networkPreference: .userDefined(.posix), secure: false, clientExpectation: clientExpectation, serverExpectation: serverExpectation) |
| 143 | + #endif |
| 144 | + } |
| 145 | + |
| 146 | + func testZeroLengthWriteTestNetworkFrameworkSecure() throws { |
| 147 | + // We can only run this test on platforms where the zero-length write workaround _could_ be added. |
| 148 | + #if canImport(Network) |
| 149 | + guard #available(OSX 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *) else { return } |
| 150 | + |
| 151 | + let serverExpectation = self.noZeroLengthWriteExpectation() |
| 152 | + let clientExpectation = self.noZeroLengthWriteExpectation() |
| 153 | + self._runTest(networkPreference: .userDefined(.networkFramework), secure: true, clientExpectation: clientExpectation, serverExpectation: serverExpectation) |
| 154 | + #endif |
| 155 | + } |
| 156 | + |
| 157 | + func testZeroLengthWriteTestNetworkFrameworkInsecure() throws { |
| 158 | + // We can only run this test on platforms where the zero-length write workaround _could_ be added. |
| 159 | + #if canImport(Network) |
| 160 | + guard #available(OSX 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *) else { return } |
| 161 | + |
| 162 | + let serverExpectation = self.zeroLengthWriteExpectation() |
| 163 | + let clientExpectation = self.zeroLengthWriteExpectation() |
| 164 | + self._runTest(networkPreference: .userDefined(.networkFramework), secure: false, clientExpectation: clientExpectation, serverExpectation: serverExpectation) |
| 165 | + #endif |
| 166 | + } |
| 167 | +} |
0 commit comments