2018-11-10 09:54:50 +00:00
|
|
|
//
|
|
|
|
// ConfigurationParserTests.swift
|
2021-11-02 21:52:47 +00:00
|
|
|
// TunnelKitOpenVPNTests
|
2018-11-10 09:54:50 +00:00
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 11/10/18.
|
2021-01-03 17:55:56 +00:00
|
|
|
// Copyright (c) 2021 Davide De Rosa. All rights reserved.
|
2018-11-10 09:54:50 +00:00
|
|
|
//
|
2019-05-14 08:58:47 +00:00
|
|
|
// https://github.com/passepartoutvpn
|
2018-11-10 09:54:50 +00:00
|
|
|
//
|
|
|
|
// This file is part of TunnelKit.
|
|
|
|
//
|
|
|
|
// TunnelKit is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// TunnelKit is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with TunnelKit. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
//
|
|
|
|
|
|
|
|
import XCTest
|
2021-10-25 14:27:27 +00:00
|
|
|
import TunnelKitCore
|
2021-11-07 20:54:05 +00:00
|
|
|
import TunnelKitOpenVPNCore
|
2018-11-10 09:54:50 +00:00
|
|
|
|
|
|
|
class ConfigurationParserTests: XCTestCase {
|
|
|
|
override func setUp() {
|
|
|
|
super.setUp()
|
|
|
|
// Put setup code here. This method is called before the invocation of each test method in the class.
|
|
|
|
}
|
|
|
|
|
|
|
|
override func tearDown() {
|
|
|
|
// Put teardown code here. This method is called after the invocation of each test method in the class.
|
|
|
|
super.tearDown()
|
|
|
|
}
|
|
|
|
|
2019-04-04 10:40:28 +00:00
|
|
|
// from lines
|
|
|
|
|
|
|
|
func testCompression() throws {
|
2019-05-19 12:04:41 +00:00
|
|
|
XCTAssertNil(try OpenVPN.ConfigurationParser.parsed(fromLines: ["comp-lzo"]).warning)
|
|
|
|
XCTAssertNoThrow(try OpenVPN.ConfigurationParser.parsed(fromLines: ["comp-lzo no"]))
|
|
|
|
XCTAssertNoThrow(try OpenVPN.ConfigurationParser.parsed(fromLines: ["comp-lzo yes"]))
|
|
|
|
// XCTAssertThrowsError(try OpenVPN.ConfigurationParser.parsed(fromLines: ["comp-lzo yes"]))
|
2019-04-04 10:40:28 +00:00
|
|
|
|
2019-05-19 12:04:41 +00:00
|
|
|
XCTAssertNoThrow(try OpenVPN.ConfigurationParser.parsed(fromLines: ["compress"]))
|
|
|
|
XCTAssertNoThrow(try OpenVPN.ConfigurationParser.parsed(fromLines: ["compress lzo"]))
|
2019-04-04 10:40:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func testDHCPOption() throws {
|
2019-04-25 11:06:22 +00:00
|
|
|
let lines = [
|
2019-04-12 05:47:27 +00:00
|
|
|
"dhcp-option DNS 8.8.8.8",
|
|
|
|
"dhcp-option DNS6 ffff::1",
|
2020-12-18 18:09:50 +00:00
|
|
|
"dhcp-option DOMAIN fake-main.net",
|
|
|
|
"dhcp-option DOMAIN-SEARCH one.com",
|
|
|
|
"dhcp-option DOMAIN-SEARCH two.com",
|
|
|
|
"dhcp-option DOMAIN main.net",
|
2019-04-12 05:47:27 +00:00
|
|
|
"dhcp-option PROXY_HTTP 1.2.3.4 8081",
|
2019-04-13 17:03:27 +00:00
|
|
|
"dhcp-option PROXY_HTTPS 7.8.9.10 8082",
|
2019-10-21 19:47:45 +00:00
|
|
|
"dhcp-option PROXY_AUTO_CONFIG_URL https://pac/",
|
2019-04-13 17:03:27 +00:00
|
|
|
"dhcp-option PROXY_BYPASS foo.com bar.org net.chat"
|
2019-04-12 05:47:27 +00:00
|
|
|
]
|
2019-05-19 13:50:30 +00:00
|
|
|
XCTAssertNoThrow(try OpenVPN.ConfigurationParser.parsed(fromLines: lines))
|
2019-04-04 10:40:28 +00:00
|
|
|
|
2019-05-19 13:50:30 +00:00
|
|
|
let parsed = try! OpenVPN.ConfigurationParser.parsed(fromLines: lines).configuration
|
2019-04-04 10:40:28 +00:00
|
|
|
XCTAssertEqual(parsed.dnsServers, ["8.8.8.8", "ffff::1"])
|
2020-12-18 18:09:50 +00:00
|
|
|
XCTAssertEqual(parsed.searchDomains, ["main.net", "one.com", "two.com"])
|
2019-04-12 05:47:27 +00:00
|
|
|
XCTAssertEqual(parsed.httpProxy?.address, "1.2.3.4")
|
|
|
|
XCTAssertEqual(parsed.httpProxy?.port, 8081)
|
|
|
|
XCTAssertEqual(parsed.httpsProxy?.address, "7.8.9.10")
|
|
|
|
XCTAssertEqual(parsed.httpsProxy?.port, 8082)
|
2019-10-22 19:03:25 +00:00
|
|
|
XCTAssertEqual(parsed.proxyAutoConfigurationURL?.absoluteString, "https://pac/")
|
2019-04-13 17:03:27 +00:00
|
|
|
XCTAssertEqual(parsed.proxyBypassDomains, ["foo.com", "bar.org", "net.chat"])
|
2019-04-04 10:40:28 +00:00
|
|
|
}
|
|
|
|
|
2019-04-25 11:06:22 +00:00
|
|
|
func testRedirectGateway() throws {
|
2019-05-19 13:50:30 +00:00
|
|
|
var parsed: OpenVPN.Configuration
|
2019-04-25 11:06:22 +00:00
|
|
|
|
2019-05-19 13:50:30 +00:00
|
|
|
parsed = try! OpenVPN.ConfigurationParser.parsed(fromLines: []).configuration
|
2019-04-25 11:06:22 +00:00
|
|
|
XCTAssertEqual(parsed.routingPolicies, nil)
|
|
|
|
XCTAssertNotEqual(parsed.routingPolicies, [])
|
2019-05-19 13:50:30 +00:00
|
|
|
parsed = try! OpenVPN.ConfigurationParser.parsed(fromLines: ["redirect-gateway ipv4 block-local"]).configuration
|
2019-05-19 00:08:49 +00:00
|
|
|
XCTAssertEqual(Set(parsed.routingPolicies!), Set([.IPv4, .blockLocal]))
|
2019-04-25 11:06:22 +00:00
|
|
|
}
|
|
|
|
|
2019-04-04 10:40:28 +00:00
|
|
|
func testConnectionBlock() throws {
|
2019-04-25 11:06:22 +00:00
|
|
|
let lines = ["<connection>", "</connection>"]
|
2019-05-19 13:50:30 +00:00
|
|
|
XCTAssertThrowsError(try OpenVPN.ConfigurationParser.parsed(fromLines: lines))
|
2019-04-04 10:40:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// from file
|
|
|
|
|
2018-11-10 09:54:50 +00:00
|
|
|
func testPIA() throws {
|
2019-05-19 13:50:30 +00:00
|
|
|
let file = try OpenVPN.ConfigurationParser.parsed(fromURL: url(withName: "pia-hungary"))
|
2019-04-04 09:06:00 +00:00
|
|
|
XCTAssertEqual(file.configuration.hostname, "hungary.privateinternetaccess.com")
|
2018-11-10 09:54:50 +00:00
|
|
|
XCTAssertEqual(file.configuration.cipher, .aes128cbc)
|
|
|
|
XCTAssertEqual(file.configuration.digest, .sha1)
|
2019-04-04 09:06:00 +00:00
|
|
|
XCTAssertEqual(file.configuration.endpointProtocols, [
|
2018-11-10 09:54:50 +00:00
|
|
|
EndpointProtocol(.udp, 1198),
|
|
|
|
EndpointProtocol(.tcp, 502)
|
|
|
|
])
|
|
|
|
}
|
|
|
|
|
|
|
|
func testStripped() throws {
|
2019-05-19 13:50:30 +00:00
|
|
|
let lines = try OpenVPN.ConfigurationParser.parsed(fromURL: url(withName: "pia-hungary"), returnsStripped: true).strippedLines!
|
2018-11-10 09:54:50 +00:00
|
|
|
let stripped = lines.joined(separator: "\n")
|
|
|
|
print(stripped)
|
|
|
|
}
|
|
|
|
|
2019-03-25 14:51:43 +00:00
|
|
|
func testEncryptedCertificateKey() throws {
|
2019-04-01 22:09:18 +00:00
|
|
|
try privateTestEncryptedCertificateKey(pkcs: "1")
|
2019-04-01 22:33:20 +00:00
|
|
|
try privateTestEncryptedCertificateKey(pkcs: "8")
|
2019-04-01 22:09:18 +00:00
|
|
|
}
|
|
|
|
|
2019-09-03 20:11:53 +00:00
|
|
|
func testXOR() throws {
|
|
|
|
let cfg = try OpenVPN.ConfigurationParser.parsed(fromLines: ["scramble xormask F"])
|
|
|
|
XCTAssertNil(cfg.warning)
|
|
|
|
XCTAssertEqual(cfg.configuration.xorMask, Character("F").asciiValue)
|
|
|
|
|
|
|
|
let cfg2 = try OpenVPN.ConfigurationParser.parsed(fromLines: ["scramble xormask FFFF"])
|
|
|
|
XCTAssertNil(cfg.warning)
|
|
|
|
XCTAssertNil(cfg2.configuration.xorMask)
|
|
|
|
}
|
|
|
|
|
2019-04-01 22:09:18 +00:00
|
|
|
private func privateTestEncryptedCertificateKey(pkcs: String) throws {
|
2019-04-02 16:58:30 +00:00
|
|
|
let cfgURL = url(withName: "tunnelbear.enc.\(pkcs)")
|
2019-05-19 13:50:30 +00:00
|
|
|
XCTAssertThrowsError(try OpenVPN.ConfigurationParser.parsed(fromURL: cfgURL))
|
|
|
|
XCTAssertNoThrow(try OpenVPN.ConfigurationParser.parsed(fromURL: cfgURL, passphrase: "foobar"))
|
2019-04-01 22:55:44 +00:00
|
|
|
}
|
|
|
|
|
2018-11-10 09:54:50 +00:00
|
|
|
private func url(withName name: String) -> URL {
|
2021-10-25 14:27:27 +00:00
|
|
|
return Bundle.module.url(forResource: name, withExtension: "ovpn")!
|
2018-11-10 09:54:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|