Update tests in order to use VPNProfile

This commit is contained in:
Sergey Abramchuk
2018-09-27 17:31:45 +03:00
parent 1beeda999e
commit df7cdf7c1d
+14 -50
View File
@@ -30,10 +30,12 @@ class OpenVPNAdapterTests: XCTestCase {
} }
func testApplyConfiguration() { func testApplyConfiguration() {
guard let vpnConfiguration = VPNProfile.configuration.data(using: .utf8) else { fatalError() }
let adapter = OpenVPNAdapter() let adapter = OpenVPNAdapter()
let configuration = OpenVPNConfiguration() let configuration = OpenVPNConfiguration()
configuration.fileContent = ProfileLoader.getVPNProfile(type: .localVPNServer) configuration.fileContent = vpnConfiguration
configuration.settings = ["auth-user-pass": ""] configuration.settings = ["auth-user-pass": ""]
let result: OpenVPNProperties let result: OpenVPNProperties
@@ -44,9 +46,8 @@ class OpenVPNAdapterTests: XCTestCase {
return return
} }
XCTAssert(result.remoteHost == "192.168.1.200") XCTAssert(result.remoteHost == VPNProfile.remoteHost)
XCTAssert(result.remotePort == 1194) XCTAssert(result.remotePort == VPNProfile.remotePort)
XCTAssert(result.remoteProto == .UDP)
XCTAssert(result.autologin == false) XCTAssert(result.autologin == false)
} }
@@ -65,54 +66,15 @@ class OpenVPNAdapterTests: XCTestCase {
} }
} }
// Test connection with local VPN server
func testLocalConection() { // Test connection to the VPN server
func testConnection() {
guard let vpnConfiguration = VPNProfile.configuration.data(using: .utf8) else { fatalError() }
let adapter = OpenVPNAdapter() let adapter = OpenVPNAdapter()
let configuration = OpenVPNConfiguration() let configuration = OpenVPNConfiguration()
configuration.fileContent = ProfileLoader.getVPNProfile(type: .localVPNServer) configuration.fileContent = vpnConfiguration
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.connect()
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 { do {
_ = try adapter.apply(configuration: configuration) _ = try adapter.apply(configuration: configuration)
@@ -155,9 +117,11 @@ extension OpenVPNAdapterTests: OpenVPNAdapterDelegate {
func openVPNAdapter(_ openVPNAdapter: OpenVPNAdapter, handleError error: Error) { func openVPNAdapter(_ openVPNAdapter: OpenVPNAdapter, handleError error: Error) {
if let connectionExpectation = expectations[.connection] { if let connectionExpectation = expectations[.connection] {
XCTFail("Failed to establish conection. \(error.localizedDescription)") XCTFail("Failed to establish conection.")
connectionExpectation.fulfill() connectionExpectation.fulfill()
} }
dump(error)
} }
func openVPNAdapter(_ openVPNAdapter: OpenVPNAdapter, handleLogMessage logMessage: String) { func openVPNAdapter(_ openVPNAdapter: OpenVPNAdapter, handleLogMessage logMessage: String) {