This commit is contained in:
David Evans 2021-06-17 14:08:58 +01:00
parent b66a111d4d
commit b0ee587401
3 changed files with 35 additions and 7 deletions

View File

@ -69,10 +69,9 @@ public final class SOCKSServerHandshakeHandler: ChannelDuplexHandler, RemovableC
} }
public func write(context: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise<Void>?) { public func write(context: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise<Void>?) {
do {
let message = self.unwrapOutboundIn(data) let message = self.unwrapOutboundIn(data)
let outboundBuffer: ByteBuffer let outboundBuffer: ByteBuffer
do {
switch message { switch message {
case .selectedAuthenticationMethod(let method): case .selectedAuthenticationMethod(let method):
outboundBuffer = try self.handleWriteSelectedAuthenticationMethod(method, context: context) outboundBuffer = try self.handleWriteSelectedAuthenticationMethod(method, context: context)

View File

@ -32,9 +32,10 @@ extension SOCKSServerHandlerTests {
("testOutboundErrorsAreHandled", testOutboundErrorsAreHandled), ("testOutboundErrorsAreHandled", testOutboundErrorsAreHandled),
("testFlushOnHandlerRemoved", testFlushOnHandlerRemoved), ("testFlushOnHandlerRemoved", testFlushOnHandlerRemoved),
("testForceHandlerRemovalAfterAuth", testForceHandlerRemovalAfterAuth), ("testForceHandlerRemovalAfterAuth", testForceHandlerRemovalAfterAuth),
("testAutoAuthentictionComplete", testAutoAuthentictionComplete), ("testAutoAuthenticationComplete", testAutoAuthenticationComplete),
("testAutoAuthentictionCompleteWithManuallyCompletion", testAutoAuthentictionCompleteWithManuallyCompletion), ("testAutoAuthenticationCompleteWithManualCompletion", testAutoAuthenticationCompleteWithManualCompletion),
("testEagerClientRequestBeforeAuthenticationComplete", testEagerClientRequestBeforeAuthenticationComplete), ("testEagerClientRequestBeforeAuthenticationComplete", testEagerClientRequestBeforeAuthenticationComplete),
("testManualAuthenticationFailureExtraBytes", testManualAuthenticationFailureExtraBytes),
] ]
} }
} }

View File

@ -230,7 +230,7 @@ class SOCKSServerHandlerTests: XCTestCase {
XCTAssertThrowsError(try self.channel.writeOutbound(ServerMessage.authenticationData(ByteBuffer(string: "hello, world!"), complete: false))) XCTAssertThrowsError(try self.channel.writeOutbound(ServerMessage.authenticationData(ByteBuffer(string: "hello, world!"), complete: false)))
} }
func testAutoAuthentictionComplete() { func testAutoAuthenticationComplete() {
// server selects none-required, this should mean we can continue without // server selects none-required, this should mean we can continue without
// having to manually inform the state machine // having to manually inform the state machine
@ -246,7 +246,7 @@ class SOCKSServerHandlerTests: XCTestCase {
self.assertOutputBuffer([0x05, 0x00, 0x00, 0x01, 127, 0, 0, 1, 0, 80]) self.assertOutputBuffer([0x05, 0x00, 0x00, 0x01, 127, 0, 0, 1, 0, 80])
} }
func testAutoAuthentictionCompleteWithManuallyCompletion() { func testAutoAuthenticationCompleteWithManualCompletion() {
// server selects none-required, this should mean we can continue without // server selects none-required, this should mean we can continue without
// having to manually inform the state machine. However, informing the state // having to manually inform the state machine. However, informing the state
@ -283,6 +283,34 @@ class SOCKSServerHandlerTests: XCTestCase {
// server will read those as authentication bytes // server will read those as authentication bytes
self.writeInbound([0x05, 0x01, 0x00, 0x01, 127, 0, 0, 1, 0, 80]) self.writeInbound([0x05, 0x01, 0x00, 0x01, 127, 0, 0, 1, 0, 80])
self.assertInbound(.authenticationData(ByteBuffer(bytes: [0x05, 0x01, 0x00, 0x01, 127, 0, 0, 1, 0, 80]))) self.assertInbound(.authenticationData(ByteBuffer(bytes: [0x05, 0x01, 0x00, 0x01, 127, 0, 0, 1, 0, 80])))
}
func testManualAuthenticationFailureExtraBytes() {
// server selects none-required, this should mean we can continue without
// having to manually inform the state machine. However, informing the state
// machine manually shouldn't break anything.
self.writeInbound([0x05, 0x01, 0x00])
self.writeOutbound(.selectedAuthenticationMethod(.init(method: .noneRequired)))
self.assertOutputBuffer([0x05, 0x00])
// invalid authentication completion
// we've selection `noneRequired`, so no
// bytes should be written
XCTAssertThrowsError(try self.channel.writeOutbound(ServerMessage.authenticationData(ByteBuffer(bytes: [0x00]), complete: true)))
}
func testManualAuthenticationFailureInvalidCompletion() {
// server selects none-required, this should mean we can continue without
// having to manually inform the state machine. However, informing the state
// machine manually shouldn't break anything.
self.writeInbound([0x05, 0x01, 0x00])
self.writeOutbound(.selectedAuthenticationMethod(.init(method: .noneRequired)))
self.assertOutputBuffer([0x05, 0x00])
// invalid authentication completion
// authentication should have already completed
// as we selected `noneRequired`, so sending
// `complete = false` should be an error
XCTAssertThrowsError(try self.channel.writeOutbound(ServerMessage.authenticationData(ByteBuffer(bytes: []), complete: false)))
} }
} }