mirror of
https://github.com/apple/swift-nio
synced 2026-11-25 04:47:49 +00:00
194 lines
7.1 KiB
Swift
194 lines
7.1 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the SwiftNIO open source project
|
|
//
|
|
// Copyright (c) 2017-2018 Apple Inc. and the SwiftNIO project authors
|
|
// Licensed under Apache License v2.0
|
|
//
|
|
// See LICENSE.txt for license information
|
|
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
import XCTest
|
|
@testable import NIO
|
|
import NIOTLS
|
|
|
|
private class ReadCompletedHandler: ChannelInboundHandler {
|
|
public typealias InboundIn = Any
|
|
public var readCompleteCount: Int
|
|
|
|
init() {
|
|
readCompleteCount = 0
|
|
}
|
|
|
|
public func channelReadComplete(ctx: ChannelHandlerContext) {
|
|
readCompleteCount += 1
|
|
}
|
|
}
|
|
|
|
class ApplicationProtocolNegotiationHandlerTests: XCTestCase {
|
|
private enum EventType {
|
|
case basic
|
|
}
|
|
|
|
func testIgnoresUnknownUserEvents() throws {
|
|
let channel = EmbeddedChannel()
|
|
let loop = channel.eventLoop as! EmbeddedEventLoop
|
|
|
|
let handler = ApplicationProtocolNegotiationHandler { result in
|
|
XCTFail("Negotiation fired")
|
|
return loop.newSucceedFuture(result: ())
|
|
}
|
|
|
|
try channel.pipeline.add(handler: handler).wait()
|
|
|
|
// Fire a pair of events that should be ignored.
|
|
channel.pipeline.fireUserInboundEventTriggered(event: EventType.basic)
|
|
channel.pipeline.fireUserInboundEventTriggered(event: TLSUserEvent.shutdownCompleted)
|
|
|
|
// The channel handler should still be in the pipeline.
|
|
try channel.pipeline.assertContains(handler: handler)
|
|
}
|
|
|
|
private func negotiateTest(event: TLSUserEvent, expectedResult: ALPNResult) throws {
|
|
let channel = EmbeddedChannel()
|
|
let loop = channel.eventLoop as! EmbeddedEventLoop
|
|
let continuePromise: EventLoopPromise<Void> = loop.newPromise()
|
|
|
|
let expectedResult: ALPNResult = .negotiated("h2")
|
|
var called = false
|
|
|
|
let handler = ApplicationProtocolNegotiationHandler { result in
|
|
XCTAssertEqual(expectedResult, result)
|
|
called = true
|
|
return continuePromise.futureResult
|
|
}
|
|
|
|
try channel.pipeline.add(handler: handler).wait()
|
|
|
|
// Fire the handshake complete event.
|
|
channel.pipeline.fireUserInboundEventTriggered(event: TLSUserEvent.handshakeCompleted(negotiatedProtocol: "h2"))
|
|
|
|
// At this time the callback should have fired, but the handler should still be in
|
|
// the pipeline.
|
|
XCTAssertTrue(called)
|
|
try channel.pipeline.assertContains(handler: handler)
|
|
|
|
// Now we fire the future.
|
|
continuePromise.succeed(result: ())
|
|
|
|
// Now the handler should have removed itself from the pipeline.
|
|
try channel.pipeline.assertDoesNotContain(handler: handler)
|
|
}
|
|
|
|
func testCallbackReflectsNotificationResult() throws {
|
|
try negotiateTest(event: .handshakeCompleted(negotiatedProtocol: "h2"), expectedResult: .negotiated("h2"))
|
|
}
|
|
|
|
func testCallbackNotesFallbackForNoNegotiation() throws {
|
|
try negotiateTest(event: .handshakeCompleted(negotiatedProtocol: ""), expectedResult: .fallback)
|
|
}
|
|
|
|
func testNoBufferingBeforeEventFires() throws {
|
|
let channel = EmbeddedChannel()
|
|
let loop = channel.eventLoop as! EmbeddedEventLoop
|
|
|
|
let handler = ApplicationProtocolNegotiationHandler { result in
|
|
XCTFail("Should not be called")
|
|
return loop.newSucceedFuture(result: ())
|
|
}
|
|
|
|
try channel.pipeline.add(handler: handler).wait()
|
|
|
|
// The data we write should not be buffered.
|
|
try channel.writeInbound(data: "hello")
|
|
XCTAssertEqual(channel.readInbound()!, "hello")
|
|
}
|
|
|
|
func testBufferingWhileWaitingForFuture() throws {
|
|
let channel = EmbeddedChannel()
|
|
let loop = channel.eventLoop as! EmbeddedEventLoop
|
|
let continuePromise: EventLoopPromise<Void> = loop.newPromise()
|
|
|
|
let handler = ApplicationProtocolNegotiationHandler { result in
|
|
return continuePromise.futureResult
|
|
}
|
|
|
|
try channel.pipeline.add(handler: handler).wait()
|
|
|
|
// Fire in the event.
|
|
channel.pipeline.fireUserInboundEventTriggered(event: TLSUserEvent.handshakeCompleted(negotiatedProtocol: "h2"))
|
|
|
|
// At this point all writes should be buffered.
|
|
try channel.writeInbound(data: "writes")
|
|
try channel.writeInbound(data: "are")
|
|
try channel.writeInbound(data: "buffered")
|
|
XCTAssertNil(channel.readInbound())
|
|
|
|
// Complete the pipeline swap.
|
|
continuePromise.succeed(result: ())
|
|
|
|
// Now everything should have been unbuffered.
|
|
XCTAssertEqual(channel.readInbound()!, "writes")
|
|
XCTAssertEqual(channel.readInbound()!, "are")
|
|
XCTAssertEqual(channel.readInbound()!, "buffered")
|
|
}
|
|
|
|
func testNothingBufferedDoesNotFireReadCompleted() throws {
|
|
let channel = EmbeddedChannel()
|
|
let loop = channel.eventLoop as! EmbeddedEventLoop
|
|
let continuePromise: EventLoopPromise<Void> = loop.newPromise()
|
|
|
|
let handler = ApplicationProtocolNegotiationHandler { result in
|
|
return continuePromise.futureResult
|
|
}
|
|
let readCompleteHandler = ReadCompletedHandler()
|
|
|
|
try channel.pipeline.add(handler: handler).wait()
|
|
try channel.pipeline.add(handler: readCompleteHandler).wait()
|
|
|
|
// Fire in the event.
|
|
channel.pipeline.fireUserInboundEventTriggered(event: TLSUserEvent.handshakeCompleted(negotiatedProtocol: "h2"))
|
|
|
|
// At this time, readComplete hasn't fired.
|
|
XCTAssertEqual(readCompleteHandler.readCompleteCount, 0)
|
|
|
|
// Now satisfy the future, which forces data unbuffering. As we haven't buffered any data,
|
|
// readComplete should not be fired.
|
|
continuePromise.succeed(result: ())
|
|
XCTAssertEqual(readCompleteHandler.readCompleteCount, 0)
|
|
}
|
|
|
|
func testUnbufferingFiresReadCompleted() throws {
|
|
let channel = EmbeddedChannel()
|
|
let loop = channel.eventLoop as! EmbeddedEventLoop
|
|
let continuePromise: EventLoopPromise<Void> = loop.newPromise()
|
|
|
|
let handler = ApplicationProtocolNegotiationHandler { result in
|
|
return continuePromise.futureResult
|
|
}
|
|
let readCompleteHandler = ReadCompletedHandler()
|
|
|
|
try channel.pipeline.add(handler: handler).wait()
|
|
try channel.pipeline.add(handler: readCompleteHandler).wait()
|
|
|
|
// Fire in the event.
|
|
channel.pipeline.fireUserInboundEventTriggered(event: TLSUserEvent.handshakeCompleted(negotiatedProtocol: "h2"))
|
|
|
|
// Send a write, which is buffered.
|
|
try channel.writeInbound(data: "a write")
|
|
|
|
// At this time, readComplete hasn't fired.
|
|
XCTAssertEqual(readCompleteHandler.readCompleteCount, 1)
|
|
|
|
// Now satisfy the future, which forces data unbuffering. This should fire readComplete.
|
|
continuePromise.succeed(result: ())
|
|
XCTAssertEqual(channel.readInbound()!, "a write")
|
|
|
|
XCTAssertEqual(readCompleteHandler.readCompleteCount, 2)
|
|
}
|
|
}
|