Files
async-http-client/Tests/AsyncHTTPClientTests/SSLContextCacheTests.swift
T
Fabian FettandGitHub eab2a84b1c Use explicit NIO imports (#407)
* Use explicit NIO imports for `NIOCore`, `NIOPosix` and `NIOEmbedded`
* Updated dependencies
2021-08-19 21:11:49 +02:00

83 lines
3.4 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the AsyncHTTPClient open source project
//
// Copyright (c) 2021 Apple Inc. and the AsyncHTTPClient project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of AsyncHTTPClient project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
@testable import AsyncHTTPClient
import NIOCore
import NIOPosix
import NIOSSL
import XCTest
final class SSLContextCacheTests: XCTestCase {
func testRequestingSSLContextWorks() {
let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
let eventLoop = group.next()
let cache = SSLContextCache()
defer {
XCTAssertNoThrow(try group.syncShutdownGracefully())
}
XCTAssertNoThrow(try cache.sslContext(tlsConfiguration: .makeClientConfiguration(),
eventLoop: eventLoop,
logger: HTTPClient.loggingDisabled).wait())
}
func testCacheWorks() {
let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
let eventLoop = group.next()
let cache = SSLContextCache()
defer {
XCTAssertNoThrow(try group.syncShutdownGracefully())
}
var firstContext: NIOSSLContext?
var secondContext: NIOSSLContext?
XCTAssertNoThrow(firstContext = try cache.sslContext(tlsConfiguration: .makeClientConfiguration(),
eventLoop: eventLoop,
logger: HTTPClient.loggingDisabled).wait())
XCTAssertNoThrow(secondContext = try cache.sslContext(tlsConfiguration: .makeClientConfiguration(),
eventLoop: eventLoop,
logger: HTTPClient.loggingDisabled).wait())
XCTAssertNotNil(firstContext)
XCTAssertNotNil(secondContext)
XCTAssert(firstContext === secondContext)
}
func testCacheDoesNotReturnWrongEntry() {
let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
let eventLoop = group.next()
let cache = SSLContextCache()
defer {
XCTAssertNoThrow(try group.syncShutdownGracefully())
}
var firstContext: NIOSSLContext?
var secondContext: NIOSSLContext?
XCTAssertNoThrow(firstContext = try cache.sslContext(tlsConfiguration: .makeClientConfiguration(),
eventLoop: eventLoop,
logger: HTTPClient.loggingDisabled).wait())
// Second one has a _different_ TLSConfiguration.
var testTLSConfig = TLSConfiguration.makeClientConfiguration()
testTLSConfig.certificateVerification = .none
XCTAssertNoThrow(secondContext = try cache.sslContext(tlsConfiguration: testTLSConfig,
eventLoop: eventLoop,
logger: HTTPClient.loggingDisabled).wait())
XCTAssertNotNil(firstContext)
XCTAssertNotNil(secondContext)
XCTAssert(firstContext !== secondContext)
}
}