Skip to content

[Tests] Add some basic IPv6 tests #483

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Tests/AsyncHTTPClientTests/HTTPClientTestUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import Foundation
import Logging
import NIOConcurrencyHelpers
import NIOCore
import NIOEmbedded
import NIOHPACK
import NIOHTTP1
import NIOHTTP2
Expand Down Expand Up @@ -46,6 +47,18 @@ func getDefaultEventLoopGroup(numberOfThreads: Int) -> EventLoopGroup {
return MultiThreadedEventLoopGroup(numberOfThreads: numberOfThreads)
}

let canBindIPv6Loopback: Bool = {
let elg = MultiThreadedEventLoopGroup(numberOfThreads: 1)
defer { try! elg.syncShutdownGracefully() }
let serverChannel = try? ServerBootstrap(group: elg)
.serverChannelOption(ChannelOptions.socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1)
.bind(host: "::1", port: 0)
.wait()
let didBind = (serverChannel != nil)
try! serverChannel?.close().wait()
return didBind
}()

class TestHTTPDelegate: HTTPClientResponseDelegate {
typealias Response = Void

Expand Down Expand Up @@ -266,6 +279,7 @@ internal final class HTTPBin<RequestHandler: ChannelInboundHandler> where
enum BindTarget {
case unixDomainSocket(String)
case localhostIPv4RandomPort
case localhostIPv6RandomPort
}

enum Mode {
Expand Down Expand Up @@ -331,6 +345,8 @@ internal final class HTTPBin<RequestHandler: ChannelInboundHandler> where
switch bindTarget {
case .localhostIPv4RandomPort:
socketAddress = try! SocketAddress(ipAddress: "127.0.0.1", port: 0)
case .localhostIPv6RandomPort:
socketAddress = try! SocketAddress(ipAddress: "::1", port: 0)
case .unixDomainSocket(let path):
socketAddress = try! SocketAddress(unixDomainSocketPath: path)
}
Expand Down
2 changes: 2 additions & 0 deletions Tests/AsyncHTTPClientTests/HTTPClientTests+XCTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ extension HTTPClientTests {
("testGetHttps", testGetHttps),
("testGetHttpsWithIP", testGetHttpsWithIP),
("testGetHTTPSWorksOnMTELGWithIP", testGetHTTPSWorksOnMTELGWithIP),
("testGetHttpsWithIPv6", testGetHttpsWithIPv6),
("testGetHTTPSWorksOnMTELGWithIPv6", testGetHTTPSWorksOnMTELGWithIPv6),
("testPostHttps", testPostHttps),
("testHttpRedirect", testHttpRedirect),
("testHttpHostRedirect", testHttpHostRedirect),
Expand Down
34 changes: 34 additions & 0 deletions Tests/AsyncHTTPClientTests/HTTPClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,40 @@ class HTTPClientTests: XCTestCase {
XCTAssertEqual(.ok, response.status)
}

func testGetHttpsWithIPv6() throws {
try XCTSkipUnless(canBindIPv6Loopback, "Requires IPv6")
let localHTTPBin = HTTPBin(.http1_1(ssl: true), bindTarget: .localhostIPv6RandomPort)
let localClient = HTTPClient(eventLoopGroupProvider: .shared(self.clientGroup),
configuration: HTTPClient.Configuration(certificateVerification: .none))
defer {
XCTAssertNoThrow(try localClient.syncShutdown())
XCTAssertNoThrow(try localHTTPBin.shutdown())
}
var response: HTTPClient.Response?
XCTAssertNoThrow(response = try localClient.get(url: "https://[::1]:\(localHTTPBin.port)/get").wait())
XCTAssertEqual(.ok, response?.status)
}

func testGetHTTPSWorksOnMTELGWithIPv6() throws {
try XCTSkipUnless(canBindIPv6Loopback, "Requires IPv6")
// Same test as above but this one will use NIO on Sockets even on Apple platforms, just to make sure
// this works.
let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
defer {
XCTAssertNoThrow(try group.syncShutdownGracefully())
}
let localHTTPBin = HTTPBin(.http1_1(ssl: true), bindTarget: .localhostIPv6RandomPort)
let localClient = HTTPClient(eventLoopGroupProvider: .shared(group),
configuration: HTTPClient.Configuration(certificateVerification: .none))
defer {
XCTAssertNoThrow(try localClient.syncShutdown())
XCTAssertNoThrow(try localHTTPBin.shutdown())
}
var response: HTTPClient.Response?
XCTAssertNoThrow(response = try localClient.get(url: "https://[::1]:\(localHTTPBin.port)/get").wait())
XCTAssertEqual(.ok, response?.status)
}

func testPostHttps() throws {
let localHTTPBin = HTTPBin(.http1_1(ssl: true))
let localClient = HTTPClient(eventLoopGroupProvider: .shared(self.clientGroup),
Expand Down