Test connections without credentials

This commit is contained in:
Sergey Abramchuk
2017-03-17 20:12:25 +03:00
parent 42c692b225
commit 6dc4f7c0b7
@@ -12,23 +12,32 @@ import NetworkExtension
class OpenVPNAdapterTests: XCTestCase { class OpenVPNAdapterTests: XCTestCase {
let vpnConfiguration = "free_openvpn_udp" enum ConfigurationType {
case withoutCredentials, withCredentials
}
var vpnAdapterExpectation: XCTestExpectation? enum ExpectationsType {
case connection
}
let configurations: [ConfigurationType : String] = [
.withoutCredentials: "free_openvpn_udp"
]
var expectations = [ExpectationsType : XCTestExpectation]()
override func setUp() { override func setUp() {
super.setUp() super.setUp()
expectations.removeAll()
} }
override func tearDown() { override func tearDown() {
vpnAdapterExpectation = nil
super.tearDown() super.tearDown()
} }
// Test connection without specifying username and password // Test connection without specifying username and password
func testConectionWithoutCredentials() { func testConectionWithoutCredentials() {
let configuration = getVPNConfiguration() let configuration = getVPNConfiguration(type: .withoutCredentials)
let adapter = OpenVPNAdapter() let adapter = OpenVPNAdapter()
do { do {
@@ -37,7 +46,7 @@ class OpenVPNAdapterTests: XCTestCase {
XCTFail("Failed to configure OpenVPN adapted due to error: \(error)") XCTFail("Failed to configure OpenVPN adapted due to error: \(error)")
} }
vpnAdapterExpectation = expectation(description: "me.ss-abramchuk.openvpn-adapter.connection-w/o-credentials") expectations[.connection] = expectation(description: "me.ss-abramchuk.openvpn-adapter.connection-w/o-credentials")
adapter.delegate = self adapter.delegate = self
adapter.connect() adapter.connect()
@@ -51,9 +60,10 @@ class OpenVPNAdapterTests: XCTestCase {
extension OpenVPNAdapterTests { extension OpenVPNAdapterTests {
func getVPNConfiguration() -> Data { func getVPNConfiguration(type: ConfigurationType) -> Data {
guard guard
let path = Bundle.current.url(forResource: vpnConfiguration, withExtension: "ovpn"), let fileName = configurations[type],
let path = Bundle.current.url(forResource: fileName, withExtension: "ovpn"),
let configuration = try? Data(contentsOf: path) let configuration = try? Data(contentsOf: path)
else { else {
fatalError("Failed to retrieve OpenVPN configuration") fatalError("Failed to retrieve OpenVPN configuration")
@@ -71,13 +81,27 @@ extension OpenVPNAdapterTests: OpenVPNAdapterDelegate {
} }
func handle(event: OpenVPNEvent, message: String?) { func handle(event: OpenVPNEvent, message: String?) {
switch event {
case .connected:
guard let connectionExpectation = expectations[.connection] else { return }
connectionExpectation.fulfill()
case .disconnected:
break
default:
break
}
} }
func handle(error: Error) { func handle(error: Error) {
} }
func handle(logMessage: String) {
print("\(logMessage)")
}
} }
extension OpenVPNAdapterTests: OpenVPNAdapterPacketFlow { extension OpenVPNAdapterTests: OpenVPNAdapterPacketFlow {