Update adapter tests

This commit is contained in:
Sergey Abramchuk
2017-05-05 17:01:17 +03:00
parent 5488eae00a
commit e9db078ab0
+67 -31
View File
@@ -12,18 +12,10 @@ import NetworkExtension
class OpenVPNAdapterTests: XCTestCase { class OpenVPNAdapterTests: XCTestCase {
enum ConfigurationType {
case withoutCredentials, withCredentials
}
enum ExpectationsType { enum ExpectationsType {
case connection case connection
} }
let configurations: [ConfigurationType : String] = [
.withoutCredentials: "free_openvpn_udp_jp"
]
var expectations = [ExpectationsType : XCTestExpectation]() var expectations = [ExpectationsType : XCTestExpectation]()
override func setUp() { override func setUp() {
@@ -35,18 +27,75 @@ class OpenVPNAdapterTests: XCTestCase {
super.tearDown() super.tearDown()
} }
// Test connection without specifying username and password func testApplyConfiguration() {
func testConectionWithoutCredentials() {
let configuration = getVPNConfiguration(type: .withoutCredentials)
let adapter = OpenVPNAdapter() let adapter = OpenVPNAdapter()
let configuration = OpenVPNConfiguration()
configuration.fileContent = ProfileLoader.getVPNProfile(type: .localVPNServer)
configuration.settings = ["auth-user-pass": ""]
let result: OpenVPNProperties
do { do {
try adapter.configure(using: configuration) result = try adapter.apply(configuration: configuration)
} catch { } catch {
XCTFail("Failed to configure OpenVPN adapted due to error: \(error)") XCTFail("Failed to configure OpenVPN adapted due to error: \(error)")
return
} }
expectations[.connection] = expectation(description: "me.ss-abramchuk.openvpn-adapter.connection-w/o-credentials") 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 without specifying username and password
func testConection() {
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
}
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
}
expectations[.connection] = expectation(description: "me.ss-abramchuk.openvpn-adapter.connection")
adapter.delegate = self adapter.delegate = self
adapter.connect() adapter.connect()
@@ -58,22 +107,6 @@ class OpenVPNAdapterTests: XCTestCase {
} }
extension OpenVPNAdapterTests {
func getVPNConfiguration(type: ConfigurationType) -> Data {
guard
let fileName = configurations[type],
let path = Bundle.current.url(forResource: fileName, withExtension: "ovpn"),
let configuration = try? Data(contentsOf: path)
else {
fatalError("Failed to retrieve OpenVPN configuration")
}
return configuration
}
}
extension OpenVPNAdapterTests: OpenVPNAdapterDelegate { extension OpenVPNAdapterTests: OpenVPNAdapterDelegate {
func configureTunnel(settings: NEPacketTunnelNetworkSettings, callback: @escaping (OpenVPNAdapterPacketFlow?) -> Void) { func configureTunnel(settings: NEPacketTunnelNetworkSettings, callback: @escaping (OpenVPNAdapterPacketFlow?) -> Void) {
@@ -95,7 +128,10 @@ extension OpenVPNAdapterTests: OpenVPNAdapterDelegate {
} }
func handle(error: Error) { func handle(error: Error) {
if let connectionExpectation = expectations[.connection] {
XCTFail("Failed to establish conection. \(error.localizedDescription)")
connectionExpectation.fulfill()
}
} }
func handle(logMessage: String) { func handle(logMessage: String) {