2024-09-23 13:02:26 +00:00
|
|
|
//
|
2024-11-01 08:47:50 +00:00
|
|
|
// ExtendedTunnelTests.swift
|
2024-09-23 13:02:26 +00:00
|
|
|
// Passepartout
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 9/12/24.
|
|
|
|
// Copyright (c) 2024 Davide De Rosa. All rights reserved.
|
|
|
|
//
|
|
|
|
// https://github.com/passepartoutvpn
|
|
|
|
//
|
|
|
|
// This file is part of Passepartout.
|
|
|
|
//
|
|
|
|
// Passepartout 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.
|
|
|
|
//
|
|
|
|
// Passepartout 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 Passepartout. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
//
|
|
|
|
|
2024-11-21 15:48:12 +00:00
|
|
|
import Combine
|
2024-11-03 12:16:13 +00:00
|
|
|
@testable import CommonLibrary
|
2024-09-23 13:02:26 +00:00
|
|
|
import Foundation
|
|
|
|
import PassepartoutKit
|
|
|
|
import XCTest
|
|
|
|
|
2024-11-01 08:47:50 +00:00
|
|
|
final class ExtendedTunnelTests: XCTestCase {
|
2024-11-21 15:48:12 +00:00
|
|
|
private var subscriptions: Set<AnyCancellable> = []
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@MainActor
|
2024-11-01 08:47:50 +00:00
|
|
|
extension ExtendedTunnelTests {
|
2024-09-23 13:02:26 +00:00
|
|
|
func test_givenTunnel_whenDisconnectWithError_thenPublishesLastErrorCode() async throws {
|
|
|
|
let env = InMemoryEnvironment()
|
|
|
|
let tunnel = Tunnel(strategy: FakeTunnelStrategy(environment: env))
|
2024-11-01 08:47:50 +00:00
|
|
|
let sut = ExtendedTunnel(tunnel: tunnel, environment: env, interval: 0.1)
|
2024-09-23 13:02:26 +00:00
|
|
|
|
2024-11-01 19:01:18 +00:00
|
|
|
let module = try DNSModule.Builder().tryBuild()
|
|
|
|
let profile = try Profile.Builder(modules: [module], activatingModules: true).tryBuild()
|
2024-11-21 15:48:12 +00:00
|
|
|
try await sut.connect(with: profile)
|
2024-09-23 13:02:26 +00:00
|
|
|
env.setEnvironmentValue(.crypto, forKey: TunnelEnvironmentKeys.lastErrorCode)
|
|
|
|
|
2024-11-21 15:48:12 +00:00
|
|
|
let exp = expectation(description: "Last error code")
|
|
|
|
var didCall = false
|
|
|
|
sut
|
|
|
|
.$lastErrorCode
|
|
|
|
.sink {
|
|
|
|
if !didCall, $0 != nil {
|
|
|
|
didCall = true
|
|
|
|
exp.fulfill()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.store(in: &subscriptions)
|
|
|
|
|
2024-09-23 13:02:26 +00:00
|
|
|
try await tunnel.disconnect()
|
2024-11-26 11:33:28 +00:00
|
|
|
await fulfillment(of: [exp], timeout: CommonLibraryTests.timeout)
|
2024-09-23 13:02:26 +00:00
|
|
|
XCTAssertEqual(sut.lastErrorCode, .crypto)
|
|
|
|
}
|
|
|
|
|
|
|
|
func test_givenTunnel_whenConnect_thenPublishesDataCount() async throws {
|
|
|
|
let env = InMemoryEnvironment()
|
|
|
|
let tunnel = Tunnel(strategy: FakeTunnelStrategy(environment: env))
|
2024-11-01 08:47:50 +00:00
|
|
|
let sut = ExtendedTunnel(tunnel: tunnel, environment: env, interval: 0.1)
|
2024-09-23 13:02:26 +00:00
|
|
|
|
2024-11-01 19:01:18 +00:00
|
|
|
let module = try DNSModule.Builder().tryBuild()
|
|
|
|
let profile = try Profile.Builder(modules: [module], activatingModules: true).tryBuild()
|
2024-11-21 15:48:12 +00:00
|
|
|
try await sut.install(profile)
|
2024-09-23 13:02:26 +00:00
|
|
|
|
|
|
|
let dataCount = DataCount(500, 700)
|
|
|
|
env.setEnvironmentValue(dataCount, forKey: TunnelEnvironmentKeys.dataCount)
|
|
|
|
XCTAssertEqual(sut.dataCount, nil)
|
|
|
|
|
2024-11-21 15:48:12 +00:00
|
|
|
let exp = expectation(description: "Data count")
|
|
|
|
var didCall = false
|
|
|
|
sut
|
|
|
|
.$dataCount
|
|
|
|
.sink {
|
|
|
|
if !didCall, $0 != nil {
|
|
|
|
didCall = true
|
|
|
|
exp.fulfill()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.store(in: &subscriptions)
|
|
|
|
|
2024-09-30 12:56:20 +00:00
|
|
|
try await tunnel.install(profile, connect: true, title: \.name)
|
2024-11-26 11:33:28 +00:00
|
|
|
await fulfillment(of: [exp], timeout: CommonLibraryTests.timeout)
|
2024-09-23 13:02:26 +00:00
|
|
|
XCTAssertEqual(sut.dataCount, dataCount)
|
|
|
|
}
|
2024-11-21 15:48:12 +00:00
|
|
|
|
|
|
|
func test_givenTunnelAndProcessor_whenInstall_thenProcessesProfile() async throws {
|
|
|
|
let env = InMemoryEnvironment()
|
|
|
|
let tunnel = Tunnel(strategy: FakeTunnelStrategy(environment: env))
|
|
|
|
let processor = MockTunnelProcessor()
|
|
|
|
let sut = ExtendedTunnel(tunnel: tunnel, environment: env, processor: processor, interval: 0.1)
|
|
|
|
|
|
|
|
let module = try DNSModule.Builder().tryBuild()
|
|
|
|
let profile = try Profile.Builder(modules: [module], activatingModules: true).tryBuild()
|
|
|
|
try await sut.install(profile)
|
|
|
|
|
|
|
|
XCTAssertEqual(tunnel.currentProfile?.id, profile.id)
|
|
|
|
// XCTAssertEqual(processor.titleCount, 1) // unused by FakeTunnelStrategy
|
|
|
|
XCTAssertEqual(processor.willInstallCount, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func test_givenTunnel_whenStatusChanges_thenConnectionStatusIsExpected() async throws {
|
|
|
|
let env = InMemoryEnvironment()
|
|
|
|
let tunnel = Tunnel(strategy: FakeTunnelStrategy(environment: env))
|
|
|
|
let processor = MockTunnelProcessor()
|
|
|
|
let sut = ExtendedTunnel(tunnel: tunnel, environment: env, processor: processor, interval: 0.1)
|
|
|
|
|
|
|
|
let module = try DNSModule.Builder().tryBuild()
|
|
|
|
let profile = try Profile.Builder(modules: [module], activatingModules: true).tryBuild()
|
|
|
|
try await sut.install(profile)
|
|
|
|
|
|
|
|
XCTAssertEqual(tunnel.currentProfile?.id, profile.id)
|
|
|
|
// XCTAssertEqual(processor.titleCount, 1) // unused by FakeTunnelStrategy
|
|
|
|
XCTAssertEqual(processor.willInstallCount, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func test_givenTunnelStatus_thenConnectionStatusIsExpected() async throws {
|
|
|
|
let allTunnelStatuses: [TunnelStatus] = [
|
|
|
|
.inactive,
|
|
|
|
.activating,
|
|
|
|
.active,
|
|
|
|
.deactivating
|
|
|
|
]
|
|
|
|
let allConnectionStatuses: [ConnectionStatus] = [
|
|
|
|
.disconnected,
|
|
|
|
.connecting,
|
|
|
|
.connected,
|
|
|
|
.disconnecting
|
|
|
|
]
|
|
|
|
|
|
|
|
let env = InMemoryEnvironment()
|
|
|
|
let key = TunnelEnvironmentKeys.connectionStatus
|
|
|
|
|
|
|
|
// no connection status, tunnel status unaffected
|
|
|
|
allTunnelStatuses.forEach {
|
|
|
|
XCTAssertEqual($0.withEnvironment(env), $0)
|
|
|
|
}
|
|
|
|
|
|
|
|
// has connection status
|
|
|
|
|
|
|
|
// affected if .active
|
|
|
|
let tunnelActive: TunnelStatus = .active
|
|
|
|
env.setEnvironmentValue(ConnectionStatus.connected, forKey: key)
|
|
|
|
XCTAssertEqual(tunnelActive.withEnvironment(env), .active)
|
|
|
|
allConnectionStatuses
|
|
|
|
.filter {
|
|
|
|
$0 != .connected
|
|
|
|
}
|
|
|
|
.forEach {
|
|
|
|
env.setEnvironmentValue($0, forKey: key)
|
|
|
|
XCTAssertEqual(tunnelActive.withEnvironment(env), .activating)
|
|
|
|
}
|
|
|
|
|
|
|
|
// unaffected otherwise
|
|
|
|
allTunnelStatuses
|
|
|
|
.filter {
|
|
|
|
$0 != .active
|
|
|
|
}
|
|
|
|
.forEach {
|
|
|
|
XCTAssertEqual($0.withEnvironment(env), $0)
|
|
|
|
}
|
|
|
|
}
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|