mirror of
https://github.com/deneraraujo/OpenVPNAdapter.git
synced 2026-04-24 00:00:05 +08:00
Update configuration tests
This commit is contained in:
@@ -9,8 +9,6 @@
|
|||||||
import XCTest
|
import XCTest
|
||||||
@testable import OpenVPNAdapter
|
@testable import OpenVPNAdapter
|
||||||
|
|
||||||
// TODO: Test getting/setting of all properties of OpenVPNConfiguration
|
|
||||||
|
|
||||||
class OpenVPNConfigurationTests: XCTestCase {
|
class OpenVPNConfigurationTests: XCTestCase {
|
||||||
|
|
||||||
override func setUp() {
|
override func setUp() {
|
||||||
@@ -23,135 +21,148 @@ class OpenVPNConfigurationTests: XCTestCase {
|
|||||||
super.tearDown()
|
super.tearDown()
|
||||||
}
|
}
|
||||||
|
|
||||||
func testGetSetProfile() {
|
func testEvaluateEmptyConfig() {
|
||||||
guard let originalProfile = VPNProfile.general.configuration.data(using: .utf8) else { fatalError() }
|
|
||||||
|
|
||||||
let configuration = OpenVPNConfiguration()
|
let configuration = OpenVPNConfiguration()
|
||||||
|
|
||||||
guard configuration.fileContent == nil else {
|
do {
|
||||||
XCTFail("Empty file content should return nil")
|
let _ = try OpenVPNAdapter.evaluate(configuration: configuration)
|
||||||
return
|
XCTFail("We shouldn't be here, evaluation should fail.")
|
||||||
|
} catch {
|
||||||
|
guard let fatal = (error as NSError).userInfo[OpenVPNAdapterErrorFatalKey] as? Bool else {
|
||||||
|
XCTFail("Error should contain OpenVPNAdapterErrorFatalKey.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
XCTAssert(fatal)
|
||||||
}
|
}
|
||||||
|
|
||||||
configuration.fileContent = originalProfile
|
|
||||||
|
|
||||||
guard let returnedProfile = configuration.fileContent else {
|
|
||||||
XCTFail("Returned file content should not be nil")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
XCTAssert(originalProfile.elementsEqual(returnedProfile))
|
|
||||||
|
|
||||||
configuration.fileContent = nil
|
|
||||||
XCTAssert(configuration.fileContent == nil, "Empty file content should return nil")
|
|
||||||
|
|
||||||
configuration.fileContent = Data()
|
|
||||||
XCTAssert(configuration.fileContent == nil, "Empty file content should return nil")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func testGetSetSettings() {
|
func testEvaluateManuallyPopulatedConfig() {
|
||||||
let originalSettings = [
|
let configuration = OpenVPNConfiguration()
|
||||||
|
|
||||||
|
guard let caURL = Bundle.current.url(forResource: "ca", withExtension: "crt"),
|
||||||
|
let caContent = try? String(contentsOf: caURL, encoding: .utf8)
|
||||||
|
else {
|
||||||
|
fatalError("Failed to get ca.crt, check its existance in the Resources folder.")
|
||||||
|
}
|
||||||
|
|
||||||
|
guard let certURL = Bundle.current.url(forResource: "client", withExtension: "crt"),
|
||||||
|
let certContent = try? String(contentsOf: certURL, encoding: .utf8)
|
||||||
|
else {
|
||||||
|
fatalError("Failed to get client.crt, check its existance in the Resources folder.")
|
||||||
|
}
|
||||||
|
|
||||||
|
guard let keyURL = Bundle.current.url(forResource: "client", withExtension: "key"),
|
||||||
|
let keyContent = try? String(contentsOf: keyURL, encoding: .utf8)
|
||||||
|
else {
|
||||||
|
fatalError("Failed to get client.key, check its existance in the Resources folder.")
|
||||||
|
}
|
||||||
|
|
||||||
|
configuration.settings = [
|
||||||
"client": "",
|
"client": "",
|
||||||
"dev": "tun",
|
"dev": "tun",
|
||||||
"remote-cert-tls" : "server"
|
"proto": "udp",
|
||||||
|
"remote": "my-server.com 1194",
|
||||||
|
"resolv-retry": "infinite",
|
||||||
|
"nobind": "",
|
||||||
|
"auth-user-pass": "",
|
||||||
|
"cipher": "AES-256-CBC",
|
||||||
|
"comp-lzo": "",
|
||||||
|
"verb": "3",
|
||||||
|
"ca": caContent.replacingOccurrences(of: "\n", with: "\\n"),
|
||||||
|
"cert": certContent.replacingOccurrences(of: "\n", with: "\\n"),
|
||||||
|
"key": keyContent.replacingOccurrences(of: "\n", with: "\\n")
|
||||||
]
|
]
|
||||||
|
|
||||||
|
let evaluation: OpenVPNConfigurationEvaluation
|
||||||
|
do {
|
||||||
|
evaluation = try OpenVPNAdapter.evaluate(configuration: configuration)
|
||||||
|
} catch {
|
||||||
|
XCTFail("Evaluation failed due to error: \(error)")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
XCTAssert(evaluation.remoteHost == "my-server.com")
|
||||||
|
XCTAssert(evaluation.remotePort == 1194)
|
||||||
|
|
||||||
|
XCTAssert(
|
||||||
|
!evaluation.autologin, "Username and password are required so autologin should be false."
|
||||||
|
)
|
||||||
|
|
||||||
|
XCTAssert(
|
||||||
|
!evaluation.externalPki,
|
||||||
|
"Key and cert were provided to the configuration so it shouldn't be External PKI profile."
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
func testEvaluateConfigFromFile() {
|
||||||
let configuration = OpenVPNConfiguration()
|
let configuration = OpenVPNConfiguration()
|
||||||
|
|
||||||
guard configuration.settings == nil else {
|
guard let configURL = Bundle.current.url(forResource: "client", withExtension: "ovpn"),
|
||||||
XCTFail("Empty settings should return nil")
|
let configContent = try? Data(contentsOf: configURL)
|
||||||
|
else {
|
||||||
|
fatalError("Failed to get client.ovpn, check its existance in the Resources folder.")
|
||||||
|
}
|
||||||
|
|
||||||
|
configuration.fileContent = configContent
|
||||||
|
|
||||||
|
let evaluation: OpenVPNConfigurationEvaluation
|
||||||
|
do {
|
||||||
|
evaluation = try OpenVPNAdapter.evaluate(configuration: configuration)
|
||||||
|
} catch {
|
||||||
|
XCTFail("Evaluation failed due to error: \(error)")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
configuration.settings = originalSettings
|
XCTAssert(evaluation.remoteHost == "my-server.com")
|
||||||
|
XCTAssert(evaluation.remotePort == 1194)
|
||||||
|
|
||||||
guard let returnedSettings = configuration.settings else {
|
XCTAssert(
|
||||||
XCTFail("Returned settings should not be nil")
|
!evaluation.autologin, "Username and password required so autologin should be false."
|
||||||
return
|
)
|
||||||
}
|
|
||||||
|
|
||||||
let equals = originalSettings.allSatisfy { (key, value) in
|
XCTAssert(
|
||||||
returnedSettings[key] == value
|
evaluation.externalPki,
|
||||||
}
|
"Key and cert were not provided to the configuration so it should be External PKI profile."
|
||||||
|
)
|
||||||
XCTAssert(equals)
|
|
||||||
|
|
||||||
configuration.settings = [:]
|
|
||||||
XCTAssert(configuration.settings == nil, "Empty settings should return nil")
|
|
||||||
|
|
||||||
configuration.settings = nil
|
|
||||||
XCTAssert(configuration.settings == nil, "Empty settings should return nil")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func testGetSetRemote() {
|
func testSetConfigurationProperties() {
|
||||||
guard let originalProfile = VPNProfile.general.configuration.data(using: .utf8) else { fatalError() }
|
|
||||||
|
|
||||||
let originalServer = "192.168.1.200"
|
|
||||||
let originalPort: UInt = 12000
|
|
||||||
|
|
||||||
let configuration = OpenVPNConfiguration()
|
|
||||||
configuration.fileContent = originalProfile
|
|
||||||
|
|
||||||
XCTAssertNil(configuration.server)
|
|
||||||
XCTAssertEqual(configuration.port, 0)
|
|
||||||
|
|
||||||
configuration.server = originalServer
|
|
||||||
configuration.port = originalPort
|
|
||||||
|
|
||||||
XCTAssertNotNil(configuration.server)
|
|
||||||
XCTAssertEqual(configuration.server, originalServer)
|
|
||||||
XCTAssertEqual(configuration.port, originalPort)
|
|
||||||
}
|
|
||||||
|
|
||||||
func testGetSetProto() {
|
|
||||||
let originalOption: OpenVPNTransportProtocol = .UDP
|
|
||||||
|
|
||||||
let configuration = OpenVPNConfiguration()
|
let configuration = OpenVPNConfiguration()
|
||||||
|
|
||||||
guard configuration.proto == .default else {
|
guard let configURL = Bundle.current.url(forResource: "client", withExtension: "ovpn"),
|
||||||
XCTFail("proto option should return default value")
|
let configContent = try? Data(contentsOf: configURL)
|
||||||
return
|
else {
|
||||||
|
fatalError("Failed to get client.ovpn, check its existance in the Resources folder.")
|
||||||
}
|
}
|
||||||
|
|
||||||
configuration.proto = originalOption
|
configuration.fileContent = configContent
|
||||||
guard configuration.proto == originalOption else {
|
configuration.settings = [
|
||||||
XCTFail("proto option should be equal to original value (enabled)")
|
"persist-key": "",
|
||||||
return
|
"persist-tun": ""
|
||||||
}
|
]
|
||||||
|
|
||||||
|
configuration.server = "another-server.com"
|
||||||
|
configuration.port = 5000
|
||||||
|
|
||||||
|
XCTAssert(configuration.proto == .default)
|
||||||
|
|
||||||
|
configuration.proto = .adaptive
|
||||||
|
configuration.tlsCertProfile = .preferred
|
||||||
|
|
||||||
|
XCTAssert(configuration.fileContent?.elementsEqual(configContent) == true)
|
||||||
|
XCTAssert(configuration.settings?["persist-key"] != nil)
|
||||||
|
XCTAssert(configuration.settings?["persist-tun"] != nil)
|
||||||
|
|
||||||
|
XCTAssert(configuration.server == "another-server.com")
|
||||||
|
XCTAssert(configuration.port == 5000)
|
||||||
|
|
||||||
|
XCTAssert(configuration.proto == .adaptive)
|
||||||
|
XCTAssert(configuration.tlsCertProfile == .preferred)
|
||||||
|
|
||||||
|
XCTAssert(configuration.ipv6 == .default)
|
||||||
|
|
||||||
|
configuration.ipv6 = .enabled
|
||||||
|
XCTAssert(configuration.ipv6 == .enabled)
|
||||||
}
|
}
|
||||||
|
|
||||||
func testGetSetIPv6() {
|
|
||||||
let originalOption: OpenVPNIPv6Preference = .enabled
|
|
||||||
|
|
||||||
let configuration = OpenVPNConfiguration()
|
|
||||||
|
|
||||||
guard configuration.ipv6 == .default else {
|
|
||||||
XCTFail("IPv6 option should return default value")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
configuration.ipv6 = originalOption
|
|
||||||
guard configuration.ipv6 == originalOption else {
|
|
||||||
XCTFail("IPv6 option should be equal to original value (enabled)")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func testGetSetTLSCertProfile() {
|
|
||||||
let originalOption: OpenVPNTLSCertProfile = .preferred
|
|
||||||
|
|
||||||
let configuration = OpenVPNConfiguration()
|
|
||||||
|
|
||||||
guard configuration.tlsCertProfile == .default else {
|
|
||||||
XCTFail("TLS Cert Profile option should return default value")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
configuration.tlsCertProfile = originalOption
|
|
||||||
guard configuration.tlsCertProfile == originalOption else {
|
|
||||||
XCTFail("TLS Cert Profile option should be equal to original value (preferred)")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user