Test ExtendedTunnel (#902)

Decouple .connectionStatus to TunnelStatus.withEnvironment() for easy
testing.
This commit is contained in:
Davide 2024-11-21 16:48:12 +01:00 committed by GitHub
parent a93fcd4c66
commit 2478fb204b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 177 additions and 26 deletions

View File

@ -77,19 +77,7 @@ extension ExtendedTunnel {
}
public var connectionStatus: TunnelStatus {
var status = tunnel.status
if status == .active, let environmentConnectionStatus {
if environmentConnectionStatus == .connected {
status = .active
} else {
status = .activating
}
}
return status
}
private var environmentConnectionStatus: ConnectionStatus? {
value(forKey: TunnelEnvironmentKeys.connectionStatus)
tunnel.status.withEnvironment(environment)
}
}
@ -196,8 +184,24 @@ private extension ExtendedTunnel {
func processedProfile(_ profile: Profile) throws -> Profile {
if let processor {
return try processor.willConnect(to: profile)
return try processor.willInstall(profile)
}
return profile
}
}
// MARK: - Helpers
extension TunnelStatus {
public func withEnvironment(_ environment: TunnelEnvironment) -> TunnelStatus {
var status = self
if status == .active, let connectionStatus = environment.environmentValue(forKey: TunnelEnvironmentKeys.connectionStatus) {
if connectionStatus == .connected {
status = .active
} else {
status = .activating
}
}
return status
}
}

View File

@ -35,7 +35,7 @@ public final class InAppProcessor: ObservableObject, Sendable {
private nonisolated let _willRebuild: (IAPManager, Profile.Builder) throws -> Profile.Builder
private nonisolated let _willConnect: (IAPManager, Profile) throws -> Profile
private nonisolated let _willInstall: (IAPManager, Profile) throws -> Profile
private nonisolated let _verify: (IAPManager, Profile) -> Set<AppFeature>?
@ -44,14 +44,14 @@ public final class InAppProcessor: ObservableObject, Sendable {
title: @escaping (Profile) -> String,
isIncluded: @escaping (IAPManager, Profile) -> Bool,
willRebuild: @escaping (IAPManager, Profile.Builder) throws -> Profile.Builder,
willConnect: @escaping (IAPManager, Profile) throws -> Profile,
willInstall: @escaping (IAPManager, Profile) throws -> Profile,
verify: @escaping (IAPManager, Profile) -> Set<AppFeature>?
) {
self.iapManager = iapManager
_title = title
_isIncluded = isIncluded
_willRebuild = willRebuild
_willConnect = willConnect
_willInstall = willInstall
_verify = verify
}
}
@ -79,7 +79,7 @@ extension InAppProcessor: ProfileProcessor {
// MARK: - TunnelProcessor
extension InAppProcessor: TunnelProcessor {
public func willConnect(to profile: Profile) throws -> Profile {
try _willConnect(iapManager, profile)
public func willInstall(_ profile: Profile) throws -> Profile {
try _willInstall(iapManager, profile)
}
}

View File

@ -29,5 +29,5 @@ import PassepartoutKit
public protocol TunnelProcessor {
func title(for profile: Profile) -> String
func willConnect(to profile: Profile) throws -> Profile
func willInstall(_ profile: Profile) throws -> Profile
}

View File

@ -55,7 +55,7 @@ extension AppContext {
willRebuild: { _, builder in
builder
},
willConnect: { _, profile in
willInstall: { _, profile in
try profile.withProviderModules()
},
verify: { _, _ in

View File

@ -23,12 +23,14 @@
// along with Passepartout. If not, see <http://www.gnu.org/licenses/>.
//
import Combine
@testable import CommonLibrary
import Foundation
import PassepartoutKit
import XCTest
final class ExtendedTunnelTests: XCTestCase {
private var subscriptions: Set<AnyCancellable> = []
}
@MainActor
@ -40,11 +42,23 @@ extension ExtendedTunnelTests {
let module = try DNSModule.Builder().tryBuild()
let profile = try Profile.Builder(modules: [module], activatingModules: true).tryBuild()
try await tunnel.install(profile, connect: true, title: \.name)
try await sut.connect(with: profile)
env.setEnvironmentValue(.crypto, forKey: TunnelEnvironmentKeys.lastErrorCode)
let exp = expectation(description: "Last error code")
var didCall = false
sut
.$lastErrorCode
.sink {
if !didCall, $0 != nil {
didCall = true
exp.fulfill()
}
}
.store(in: &subscriptions)
try await tunnel.disconnect()
try await Task.sleep(for: .milliseconds(200))
await fulfillment(of: [exp], timeout: 0.5)
XCTAssertEqual(sut.lastErrorCode, .crypto)
}
@ -55,14 +69,103 @@ extension ExtendedTunnelTests {
let module = try DNSModule.Builder().tryBuild()
let profile = try Profile.Builder(modules: [module], activatingModules: true).tryBuild()
try await tunnel.install(profile, connect: false, title: \.name)
try await sut.install(profile)
let dataCount = DataCount(500, 700)
env.setEnvironmentValue(dataCount, forKey: TunnelEnvironmentKeys.dataCount)
XCTAssertEqual(sut.dataCount, nil)
let exp = expectation(description: "Data count")
var didCall = false
sut
.$dataCount
.sink {
if !didCall, $0 != nil {
didCall = true
exp.fulfill()
}
}
.store(in: &subscriptions)
try await tunnel.install(profile, connect: true, title: \.name)
try await Task.sleep(for: .milliseconds(300))
await fulfillment(of: [exp], timeout: 0.5)
XCTAssertEqual(sut.dataCount, dataCount)
}
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)
}
}
}

View File

@ -0,0 +1,44 @@
//
// MockTunnelProcessor.swift
// Passepartout
//
// Created by Davide De Rosa on 11/21/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/>.
//
import CommonLibrary
import Foundation
import PassepartoutKit
final class MockTunnelProcessor: TunnelProcessor {
var titleCount = 0
var willInstallCount = 0
func title(for profile: Profile) -> String {
titleCount += 1
return ""
}
func willInstall(_ profile: Profile) throws -> Profile {
willInstallCount += 1
return profile
}
}

View File

@ -47,7 +47,7 @@ extension IAPManager {
willRebuild: { _, builder in
builder
},
willConnect: { iap, profile in
willInstall: { iap, profile in
try iap.verify(profile)
// validate provider modules