2019-05-01 09:29:10 +00:00
|
|
|
//
|
|
|
|
// RoutingTests.swift
|
2021-11-02 21:52:47 +00:00
|
|
|
// TunnelKitCoreTests
|
2019-05-01 09:29:10 +00:00
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 4/30/19.
|
2023-03-17 15:58:36 +00:00
|
|
|
// Copyright (c) 2023 Davide De Rosa. All rights reserved.
|
2019-05-01 09:29:10 +00:00
|
|
|
//
|
2019-05-14 08:58:47 +00:00
|
|
|
// https://github.com/passepartoutvpn
|
2019-05-01 09:29:10 +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-11-07 20:54:05 +00:00
|
|
|
import CTunnelKitCore
|
2019-05-01 09:29:10 +00:00
|
|
|
|
|
|
|
class RoutingTests: XCTestCase {
|
|
|
|
|
|
|
|
override func 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.
|
|
|
|
}
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2019-05-01 09:29:10 +00:00
|
|
|
func testEntryMatch4() {
|
|
|
|
let entry24 = RoutingTableEntry(iPv4Network: "192.168.1.0/24", gateway: nil, networkInterface: "en0")
|
2019-05-01 14:05:50 +00:00
|
|
|
print(entry24.networkMask()!)
|
2019-05-01 09:29:10 +00:00
|
|
|
for i in 0x0...0xff {
|
|
|
|
XCTAssertTrue(entry24.matchesDestination("192.168.1.\(i)"))
|
|
|
|
}
|
|
|
|
for i in 0x0...0xff {
|
|
|
|
XCTAssertFalse(entry24.matchesDestination("192.168.2.\(i)"))
|
|
|
|
}
|
|
|
|
|
|
|
|
let entry28 = RoutingTableEntry(iPv4Network: "192.168.1.0/28", gateway: nil, networkInterface: "en0")
|
2019-05-01 14:05:50 +00:00
|
|
|
print(entry28.networkMask()!)
|
2019-05-01 09:29:10 +00:00
|
|
|
for i in 0x0...0xf {
|
|
|
|
XCTAssertTrue(entry28.matchesDestination("192.168.1.\(i)"))
|
|
|
|
}
|
|
|
|
for i in 0x10...0x1f {
|
|
|
|
XCTAssertFalse(entry28.matchesDestination("192.168.1.\(i)"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testEntryMatch6() {
|
|
|
|
let entry24 = RoutingTableEntry(iPv6Network: "abcd:efef:1234::/46", gateway: nil, networkInterface: "en0")
|
|
|
|
for i in 0x0...0xf {
|
|
|
|
XCTAssertTrue(entry24.matchesDestination("abcd:efef:1234::\(i)"))
|
|
|
|
}
|
|
|
|
for i in 0x0...0xf {
|
|
|
|
XCTAssertFalse(entry24.matchesDestination("abcd:efef:1233::\(i)"))
|
|
|
|
}
|
|
|
|
}
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2019-05-01 09:29:10 +00:00
|
|
|
func testFindGatewayLAN4() {
|
|
|
|
let table = RoutingTable()
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2019-05-01 09:29:10 +00:00
|
|
|
for entry in table.ipv4() {
|
|
|
|
print(entry)
|
|
|
|
}
|
|
|
|
|
|
|
|
if let defaultGateway = table.defaultGateway4()?.gateway() {
|
|
|
|
print("Default gateway: \(defaultGateway)")
|
|
|
|
if let lan = table.broadestRoute4(matchingDestination: defaultGateway) {
|
|
|
|
print("Gateway LAN: \(lan.network())/\(lan.prefix())")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testFindGatewayLAN6() {
|
|
|
|
let table = RoutingTable()
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2019-05-01 09:29:10 +00:00
|
|
|
for entry in table.ipv6() {
|
|
|
|
print(entry)
|
|
|
|
}
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2019-05-01 09:29:10 +00:00
|
|
|
if let defaultGateway = table.defaultGateway6()?.gateway() {
|
|
|
|
print("Default gateway: \(defaultGateway)")
|
|
|
|
if let lan = table.broadestRoute6(matchingDestination: defaultGateway) {
|
|
|
|
print("Gateway LAN: \(lan.network())/\(lan.prefix())")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2019-05-01 15:07:38 +00:00
|
|
|
func testPartitioning() {
|
|
|
|
let v4 = RoutingTableEntry(iPv4Network: "192.168.1.0/24", gateway: nil, networkInterface: "en0")
|
2020-05-16 13:10:07 +00:00
|
|
|
let v4Boundary = RoutingTableEntry(iPv4Network: "192.168.1.0/31", gateway: nil, networkInterface: "en0")
|
2019-05-01 15:07:38 +00:00
|
|
|
let v6 = RoutingTableEntry(iPv6Network: "abcd:efef:120::/46", gateway: nil, networkInterface: "en0")
|
2020-05-16 13:10:07 +00:00
|
|
|
let v6Boundary = RoutingTableEntry(iPv6Network: "abcd:efef:120::/127", gateway: nil, networkInterface: "en0")
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2020-05-23 22:40:16 +00:00
|
|
|
guard let v4parts = v4.partitioned() else {
|
|
|
|
fatalError()
|
|
|
|
}
|
2019-05-01 15:07:38 +00:00
|
|
|
let v4parts1 = v4parts[0]
|
|
|
|
let v4parts2 = v4parts[1]
|
|
|
|
XCTAssertEqual(v4parts1.network(), "192.168.1.0")
|
|
|
|
XCTAssertEqual(v4parts1.prefix(), 25)
|
|
|
|
XCTAssertEqual(v4parts2.network(), "192.168.1.128")
|
|
|
|
XCTAssertEqual(v4parts2.prefix(), 25)
|
|
|
|
|
2020-05-23 22:40:16 +00:00
|
|
|
guard let v6parts = v6.partitioned() else {
|
|
|
|
fatalError()
|
|
|
|
}
|
2019-05-01 15:07:38 +00:00
|
|
|
let v6parts1 = v6parts[0]
|
|
|
|
let v6parts2 = v6parts[1]
|
|
|
|
XCTAssertEqual(v6parts1.network(), "abcd:efef:120::")
|
|
|
|
XCTAssertEqual(v6parts1.prefix(), 47)
|
|
|
|
XCTAssertEqual(v6parts2.network(), "abcd:efef:122::")
|
|
|
|
XCTAssertEqual(v6parts2.prefix(), 47)
|
2020-05-16 13:10:07 +00:00
|
|
|
|
2020-05-23 22:40:16 +00:00
|
|
|
guard let v4BoundaryParts = v4Boundary.partitioned() else {
|
|
|
|
fatalError()
|
|
|
|
}
|
2020-05-16 13:10:07 +00:00
|
|
|
let v4BoundaryParts1 = v4BoundaryParts[0]
|
|
|
|
let v4BoundaryParts2 = v4BoundaryParts[1]
|
|
|
|
XCTAssertEqual(v4BoundaryParts1.network(), "192.168.1.0")
|
|
|
|
XCTAssertEqual(v4BoundaryParts1.prefix(), 32)
|
|
|
|
XCTAssertEqual(v4BoundaryParts2.network(), "192.168.1.1")
|
|
|
|
XCTAssertEqual(v4BoundaryParts2.prefix(), 32)
|
|
|
|
|
2020-05-23 22:40:16 +00:00
|
|
|
guard let v6BoundaryParts = v6Boundary.partitioned() else {
|
|
|
|
fatalError()
|
|
|
|
}
|
2020-05-16 13:10:07 +00:00
|
|
|
let v6BoundaryParts1 = v6BoundaryParts[0]
|
|
|
|
let v6BoundaryParts2 = v6BoundaryParts[1]
|
|
|
|
XCTAssertEqual(v6BoundaryParts1.network(), "abcd:efef:120::")
|
|
|
|
XCTAssertEqual(v6BoundaryParts1.prefix(), 128)
|
|
|
|
XCTAssertEqual(v6BoundaryParts2.network(), "abcd:efef:120::1")
|
|
|
|
XCTAssertEqual(v6BoundaryParts2.prefix(), 128)
|
2019-05-01 15:07:38 +00:00
|
|
|
}
|
2019-05-01 09:29:10 +00:00
|
|
|
}
|