OpenVPNAdapter/OpenVPN Adapter Tests/OpenVPNAdapterTests.swift

168 lines
4.9 KiB
Swift
Raw Normal View History

//
// OpenVPN_Adapter_Tests.swift
// OpenVPN Adapter Tests
//
// Created by Sergey Abramchuk on 09.03.17.
//
//
import XCTest
import NetworkExtension
@testable import OpenVPNAdapter
class OpenVPNAdapterTests: XCTestCase {
2017-03-18 01:12:25 +08:00
enum ExpectationsType {
case connection
}
let customFlow = CustomFlow()
2017-03-18 01:12:25 +08:00
var expectations = [ExpectationsType : XCTestExpectation]()
override func setUp() {
super.setUp()
2017-03-18 01:12:25 +08:00
expectations.removeAll()
}
override func tearDown() {
super.tearDown()
}
2017-05-05 22:01:17 +08:00
func testApplyConfiguration() {
let adapter = OpenVPNAdapter()
let configuration = OpenVPNConfiguration()
configuration.fileContent = ProfileLoader.getVPNProfile(type: .localVPNServer)
configuration.settings = ["auth-user-pass": ""]
let result: OpenVPNProperties
do {
result = try adapter.apply(configuration: configuration)
} catch {
XCTFail("Failed to configure OpenVPN adapted due to error: \(error)")
return
}
XCTAssert(result.remoteHost == "192.168.1.200")
XCTAssert(result.remotePort == 1194)
XCTAssert(result.remoteProto == .UDP)
XCTAssert(result.autologin == false)
}
func testProvideCredentials() {
let adapter = OpenVPNAdapter()
let credentials = OpenVPNCredentials()
credentials.username = "username"
credentials.password = "password"
do {
try adapter.provide(credentials: credentials)
} catch {
XCTFail("Failed to provide credentials. \(error)")
return
}
}
// Test connection with local VPN server
func testLocalConection() {
let adapter = OpenVPNAdapter()
2017-05-05 22:01:17 +08:00
let configuration = OpenVPNConfiguration()
configuration.fileContent = ProfileLoader.getVPNProfile(type: .localVPNServer)
configuration.settings = ["auth-user-pass": ""]
let result: OpenVPNProperties
do {
2017-05-05 22:01:17 +08:00
result = try adapter.apply(configuration: configuration)
} catch {
XCTFail("Failed to configure OpenVPN adapted due to error: \(error)")
2017-05-05 22:01:17 +08:00
return
}
guard !result.autologin else {
XCTFail()
return
}
let credentials = OpenVPNCredentials()
credentials.username = "testuser"
credentials.password = "nonsecure"
do {
try adapter.provide(credentials: credentials)
} catch {
XCTFail("Failed to provide credentials. \(error)")
return
}
2017-05-05 22:01:17 +08:00
expectations[.connection] = expectation(description: "me.ss-abramchuk.openvpn-adapter.connection")
adapter.delegate = self
adapter.connect()
2017-04-15 20:41:59 +08:00
waitForExpectations(timeout: 30.0) { (error) in
adapter.disconnect()
}
}
// Test connection with remote VPN server
func testRemoteConnection() {
let adapter = OpenVPNAdapter()
let configuration = OpenVPNConfiguration()
configuration.fileContent = ProfileLoader.getVPNProfile(type: .remoteVPNServer)
do {
_ = try adapter.apply(configuration: configuration)
} catch {
XCTFail("Failed to configure OpenVPN adapted due to error: \(error)")
return
}
expectations[.connection] = expectation(description: "me.ss-abramchuk.openvpn-adapter.connection")
adapter.delegate = self
adapter.connect()
waitForExpectations(timeout: 30.0) { (error) in
adapter.disconnect()
}
}
}
extension OpenVPNAdapterTests: OpenVPNAdapterDelegate {
2017-10-11 20:39:41 +08:00
func openVPNAdapter(_ openVPNAdapter: OpenVPNAdapter, configureTunnelWithNetworkSettings networkSettings: NEPacketTunnelNetworkSettings, completionHandler: @escaping (NEPacketTunnelFlow?) -> Void) {
completionHandler(customFlow)
}
2017-10-11 20:39:41 +08:00
func openVPNAdapter(_ openVPNAdapter: OpenVPNAdapter, handleEvent event: OpenVPNAdapterEvent, message: String?) {
2017-03-18 01:12:25 +08:00
switch event {
case .connected:
guard let connectionExpectation = expectations[.connection] else { return }
connectionExpectation.fulfill()
case .disconnected:
break
default:
break
}
}
2017-10-11 20:39:41 +08:00
func openVPNAdapter(_ openVPNAdapter: OpenVPNAdapter, handleError error: Error) {
2017-05-05 22:01:17 +08:00
if let connectionExpectation = expectations[.connection] {
XCTFail("Failed to establish conection. \(error.localizedDescription)")
connectionExpectation.fulfill()
}
}
2017-10-11 20:39:41 +08:00
func openVPNAdapter(_ openVPNAdapter: OpenVPNAdapter, handleLogMessage logMessage: String) {
print(logMessage)
2017-03-18 01:12:25 +08:00
}
}