Skip to content

Commit

Permalink
Add tests for untested assumptions. (#53)
Browse files Browse the repository at this point in the history
Motivation:

While working on an unwrapping patch, I wrote a few tests to provide an
envelope on the unwrapping behaviour in OpenSSL. Some of these tests
should have counterparts for closure, but do not. This patch brings the
equivalent tests forward.

Modifications:

- Tested the failure mode when receiving gibberish instead of
    CLOSE_NOTIFY.
- Tested what happens to writes that are flushed after close().

Result:

Better test coverage.
  • Loading branch information
Lukasa authored Jan 14, 2019
1 parent 11b78f6 commit 0f3999f
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Tests/NIOOpenSSLTests/OpenSSLIntegrationTest+XCTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ extension OpenSSLIntegrationTest {
("testForcingVerificationFailure", testForcingVerificationFailure),
("testExtractingCertificates", testExtractingCertificates),
("testRepeatedClosure", testRepeatedClosure),
("testReceivingGibberishAfterAttemptingToClose", testReceivingGibberishAfterAttemptingToClose),
("testPendingWritesFailWhenFlushedOnClose", testPendingWritesFailWhenFlushedOnClose),
]
}
}
Expand Down
115 changes: 115 additions & 0 deletions Tests/NIOOpenSSLTests/OpenSSLIntegrationTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1283,4 +1283,119 @@ class OpenSSLIntegrationTest: XCTestCase {
// The closure should have happened.
XCTAssertTrue(closed)
}

func testReceivingGibberishAfterAttemptingToClose() throws {
let serverChannel = EmbeddedChannel()
let clientChannel = EmbeddedChannel()

var clientClosed = false

defer {
XCTAssertThrowsError(try serverChannel.finish())
XCTAssertThrowsError(try clientChannel.finish())
}

let ctx = try assertNoThrowWithValue(configuredSSLContext())

let clientHandler = try assertNoThrowWithValue(OpenSSLClientHandler(context: ctx))
XCTAssertNoThrow(try serverChannel.pipeline.add(handler: OpenSSLServerHandler(context: ctx)).wait())
XCTAssertNoThrow(try clientChannel.pipeline.add(handler: clientHandler).wait())
let handshakeHandler = HandshakeCompletedHandler()
XCTAssertNoThrow(try clientChannel.pipeline.add(handler: handshakeHandler).wait())

// Mark the closure of the client.
clientChannel.closeFuture.whenComplete {
clientClosed = true
}

// Connect. This should lead to a completed handshake.
let addr: SocketAddress = try SocketAddress(unixDomainSocketPath: "/tmp/whatever")
let connectFuture = clientChannel.connect(to: addr)
serverChannel.pipeline.fireChannelActive()
try interactInMemory(clientChannel: clientChannel, serverChannel: serverChannel)
try connectFuture.wait()
XCTAssertTrue(handshakeHandler.handshakeSucceeded)

// Let's close the client connection.
clientChannel.close(promise: nil)
(clientChannel.eventLoop as! EmbeddedEventLoop).run()
XCTAssertFalse(clientClosed)

// Now we're going to simulate the client receiving gibberish data in response, instead
// of a CLOSE_NOTIFY.
var buffer = clientChannel.allocator.buffer(capacity: 1024)
buffer.write(staticString: "GET / HTTP/1.1\r\nHost: localhost\r\nContent-Length: 0\r\n\r\n")

do {
try clientChannel.writeInbound(buffer)
XCTFail("Did not error")
} catch NIOOpenSSLError.shutdownFailed {
// expected
} catch {
XCTFail("Unexpected error: \(error)")
}
(clientChannel.eventLoop as! EmbeddedEventLoop).run()
XCTAssertTrue(clientClosed)

// Clean up by bringing the server up to speed
serverChannel.pipeline.fireChannelInactive()
}

func testPendingWritesFailWhenFlushedOnClose() throws {
let serverChannel = EmbeddedChannel()
let clientChannel = EmbeddedChannel()

defer {
XCTAssertThrowsError(try serverChannel.finish())
XCTAssertThrowsError(try clientChannel.finish())
}

let ctx = try assertNoThrowWithValue(configuredSSLContext())

let clientHandler = try assertNoThrowWithValue(OpenSSLClientHandler(context: ctx))
XCTAssertNoThrow(try serverChannel.pipeline.add(handler: OpenSSLServerHandler(context: ctx)).wait())
XCTAssertNoThrow(try clientChannel.pipeline.add(handler: clientHandler).wait())
let handshakeHandler = HandshakeCompletedHandler()
XCTAssertNoThrow(try clientChannel.pipeline.add(handler: handshakeHandler).wait())

// Connect. This should lead to a completed handshake.
let addr: SocketAddress = try SocketAddress(unixDomainSocketPath: "/tmp/whatever")
let connectFuture = clientChannel.connect(to: addr)
serverChannel.pipeline.fireChannelActive()
try interactInMemory(clientChannel: clientChannel, serverChannel: serverChannel)
try connectFuture.wait()
XCTAssertTrue(handshakeHandler.handshakeSucceeded)

// Queue up a write.
var writeCompleted = false
var buffer = clientChannel.allocator.buffer(capacity: 1024)
buffer.write(staticString: "Hello, world!")
clientChannel.write(buffer).map {
XCTFail("Must not succeed")
}.whenFailure { error in
XCTAssertEqual(error as? ChannelError, .ioOnClosedChannel)
writeCompleted = true
}

// We haven't spun the event loop, so the handlers are still in the pipeline. Now attempt to close.
var closed = false
clientChannel.closeFuture.whenComplete {
closed = true
}

XCTAssertFalse(writeCompleted)
clientChannel.close(promise: nil)
(clientChannel.eventLoop as! EmbeddedEventLoop).run()
XCTAssertFalse(writeCompleted)
XCTAssertFalse(closed)

// Now try to flush the write. This should fail the write early, and take out the connection.
clientChannel.flush()
(clientChannel.eventLoop as! EmbeddedEventLoop).run()
XCTAssertTrue(writeCompleted)
XCTAssertTrue(closed)

// Bring the server up to speed.
serverChannel.pipeline.fireChannelInactive()
}
}

0 comments on commit 0f3999f

Please sign in to comment.