Skip to content

Commit 711622b

Browse files
authored
Fix test for misbehaving server (#379)
The misbehaving SOCKS server test was previously passing, but not for the right reason. The invalid data we were sending was never making it off the server thanks to the SOCKSServerHandshakeHandler. In fact, as long as SOCKSServerHandshakeHandler is present, it isn't possible to send invalid data. So to test the client now we have to add a completely new handler, which exists only to write some nonsense bytes. The test still passes, but for the right reason, and we now assert the type of error the client throws too.
1 parent 3fd0658 commit 711622b

File tree

2 files changed

+35
-21
lines changed

2 files changed

+35
-21
lines changed

Tests/AsyncHTTPClientTests/HTTPClient+SOCKSTests.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,10 @@ class HTTPClientSOCKSTests: XCTestCase {
131131
XCTAssertNoThrow(try socksBin.shutdown())
132132
}
133133

134-
// the server will send a bogus message in response to the clients request
135-
XCTAssertThrowsError(try localClient.get(url: "http://localhost/socks/test").wait())
134+
// the server will send a bogus message in response to the clients greeting
135+
// this will be first picked up as an invalid protocol
136+
XCTAssertThrowsError(try localClient.get(url: "http://localhost/socks/test").wait()) { e in
137+
XCTAssertTrue(e is SOCKSError.InvalidProtocolVersion)
138+
}
136139
}
137140
}

Tests/AsyncHTTPClientTests/SOCKSTestUtils.swift

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,40 @@ struct MockSOCKSError: Error, Hashable {
2222
var description: String
2323
}
2424

25+
class TestSOCKSBadServerHandler: ChannelInboundHandler {
26+
typealias InboundIn = ByteBuffer
27+
28+
func channelRead(context: ChannelHandlerContext, data: NIOAny) {
29+
// just write some nonsense bytes
30+
let buffer = context.channel.allocator.buffer(bytes: [0xAA, 0xBB, 0xCC, 0xDD, 0xEE])
31+
context.writeAndFlush(.init(buffer), promise: nil)
32+
}
33+
}
34+
2535
class MockSOCKSServer {
2636
let channel: Channel
2737

2838
init(expectedURL: String, expectedResponse: String, misbehave: Bool = false, file: String = #file, line: UInt = #line) throws {
2939
let elg = MultiThreadedEventLoopGroup(numberOfThreads: 1)
30-
let bootstrap = ServerBootstrap(group: elg)
31-
.serverChannelOption(ChannelOptions.socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1)
32-
.childChannelInitializer { channel in
33-
let handshakeHandler = SOCKSServerHandshakeHandler()
34-
return channel.pipeline.addHandlers([
35-
handshakeHandler,
36-
SOCKSTestHandler(handshakeHandler: handshakeHandler, misbehave: misbehave),
37-
TestHTTPServer(expectedURL: expectedURL, expectedResponse: expectedResponse, file: file, line: line),
38-
])
39-
}
40+
let bootstrap: ServerBootstrap
41+
if misbehave {
42+
bootstrap = ServerBootstrap(group: elg)
43+
.serverChannelOption(ChannelOptions.socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1)
44+
.childChannelInitializer { channel in
45+
channel.pipeline.addHandler(TestSOCKSBadServerHandler())
46+
}
47+
} else {
48+
bootstrap = ServerBootstrap(group: elg)
49+
.serverChannelOption(ChannelOptions.socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1)
50+
.childChannelInitializer { channel in
51+
let handshakeHandler = SOCKSServerHandshakeHandler()
52+
return channel.pipeline.addHandlers([
53+
handshakeHandler,
54+
SOCKSTestHandler(handshakeHandler: handshakeHandler),
55+
TestHTTPServer(expectedURL: expectedURL, expectedResponse: expectedResponse, file: file, line: line),
56+
])
57+
}
58+
}
4059
self.channel = try bootstrap.bind(host: "localhost", port: 1080).wait()
4160
}
4261

@@ -49,11 +68,9 @@ class SOCKSTestHandler: ChannelInboundHandler, RemovableChannelHandler {
4968
typealias InboundIn = ClientMessage
5069

5170
let handshakeHandler: SOCKSServerHandshakeHandler
52-
let misbehave: Bool
5371

54-
init(handshakeHandler: SOCKSServerHandshakeHandler, misbehave: Bool) {
72+
init(handshakeHandler: SOCKSServerHandshakeHandler) {
5573
self.handshakeHandler = handshakeHandler
56-
self.misbehave = misbehave
5774
}
5875

5976
func channelRead(context: ChannelHandlerContext, data: NIOAny) {
@@ -69,12 +86,6 @@ class SOCKSTestHandler: ChannelInboundHandler, RemovableChannelHandler {
6986
case .authenticationData:
7087
context.fireErrorCaught(MockSOCKSError(description: "Received authentication data but didn't receive any."))
7188
case .request(let request):
72-
guard !self.misbehave else {
73-
context.writeAndFlush(
74-
.init(ServerMessage.authenticationData(context.channel.allocator.buffer(string: "bad server!"), complete: true)), promise: nil
75-
)
76-
return
77-
}
7889
context.writeAndFlush(.init(
7990
ServerMessage.response(.init(reply: .succeeded, boundAddress: request.addressType))), promise: nil)
8091
context.channel.pipeline.addHandlers([

0 commit comments

Comments
 (0)