-
Notifications
You must be signed in to change notification settings - Fork 384
/
Copy pathTunnelManagerTests.swift
182 lines (154 loc) · 6.65 KB
/
TunnelManagerTests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
//
// TunnelManagerTests.swift
// MullvadVPNTests
//
// Created by Marco Nikic on 2023-10-02.
// Copyright © 2023 Mullvad VPN AB. All rights reserved.
//
import MullvadREST
@testable import MullvadMockData
@testable import MullvadSettings
@testable import MullvadTypes
@testable import WireGuardKitTypes
import XCTest
class TunnelManagerTests: XCTestCase {
static let store = InMemorySettingsStore<SettingNotFound>()
private var tunnelObserver: TunnelObserver!
var application: UIApplicationStub!
var relayCacheTracker: RelayCacheTrackerStub!
var accountProxy: AccountsProxyStub!
var accessTokenManager: AccessTokenManagerStub!
var devicesProxy: DevicesProxyStub!
var apiProxy: APIProxyStub!
var transportProvider: TransportProvider!
override static func setUp() {
SettingsManager.unitTestStore = store
}
override static func tearDown() {
SettingsManager.unitTestStore = nil
}
override func setUp() async throws {
application = UIApplicationStub()
relayCacheTracker = RelayCacheTrackerStub()
accountProxy = AccountsProxyStub()
accessTokenManager = AccessTokenManagerStub()
devicesProxy = DevicesProxyStub(deviceResult: .success(Device.mock(publicKey: PrivateKey().publicKey)))
apiProxy = APIProxyStub()
transportProvider = TransportProvider(
urlSessionTransport: URLSessionTransport(urlSession: REST.makeURLSession()),
addressCache: REST.AddressCache(
canWriteToCache: true,
cacheDirectory: FileManager.default.temporaryDirectory
),
transportStrategy: TransportStrategy(
datasource: AccessMethodRepositoryStub(accessMethods: [PersistentAccessMethod(
id: UUID(),
name: "direct",
isEnabled: true,
proxyConfiguration: .direct
)]),
shadowsocksLoader: ShadowsocksLoader(
cache: ShadowsocksConfigurationCacheStub(),
relaySelector: ShadowsocksRelaySelectorStub(relays: .mock()),
constraintsUpdater: RelayConstraintsUpdater(),
multihopUpdater: MultihopUpdater(listener: MultihopStateListener())
)
)
)
try SettingsManager.writeSettings(LatestTunnelSettings())
}
override func tearDown() async throws {
application = nil
relayCacheTracker = nil
accountProxy = nil
accessTokenManager = nil
devicesProxy = nil
apiProxy = nil
transportProvider = nil
tunnelObserver = nil
}
func testLogInStartsKeyRotations() async throws {
accountProxy.createAccountResult = .success(REST.NewAccountData.mockValue())
let tunnelManager = TunnelManager(
application: application,
tunnelStore: TunnelStoreStub(backgroundTaskProvider: application),
relayCacheTracker: relayCacheTracker,
accountsProxy: accountProxy,
devicesProxy: devicesProxy,
apiProxy: apiProxy,
accessTokenManager: accessTokenManager,
relaySelector: RelaySelectorStub.nonFallible()
)
_ = try await tunnelManager.setNewAccount()
XCTAssertEqual(tunnelManager.isRunningPeriodicPrivateKeyRotation, true)
}
func testLogOutStopsKeyRotations() async throws {
accountProxy.createAccountResult = .success(REST.NewAccountData.mockValue())
let tunnelManager = TunnelManager(
application: application,
tunnelStore: TunnelStoreStub(backgroundTaskProvider: application),
relayCacheTracker: relayCacheTracker,
accountsProxy: accountProxy,
devicesProxy: devicesProxy,
apiProxy: apiProxy,
accessTokenManager: accessTokenManager,
relaySelector: RelaySelectorStub.nonFallible()
)
_ = try await tunnelManager.setNewAccount()
await tunnelManager.unsetAccount()
XCTAssertEqual(tunnelManager.isRunningPeriodicPrivateKeyRotation, false)
}
/// This test verifies tunnel gets out of `blockedState` after constraints are satisfied.
func testExitBlockedStateAfterSatisfyingConstraints() async throws {
let blockedExpectation = expectation(description: "Relay constraints aren't satisfied!")
let connectedExpectation = expectation(description: "Connected!")
accountProxy.createAccountResult = .success(REST.NewAccountData.mockValue())
let relaySelector = RelaySelectorStub { _, _ in
try RelaySelectorStub.unsatisfied().selectRelays(with: RelayConstraints(), connectionAttemptCount: 0)
}
let tunnelManager = TunnelManager(
application: application,
tunnelStore: TunnelStore(application: application),
relayCacheTracker: relayCacheTracker,
accountsProxy: accountProxy,
devicesProxy: devicesProxy,
apiProxy: apiProxy,
accessTokenManager: accessTokenManager,
relaySelector: relaySelector
)
let simulatorTunnelProviderHost = SimulatorTunnelProviderHost(
relaySelector: relaySelector,
transportProvider: transportProvider
)
SimulatorTunnelProvider.shared.delegate = simulatorTunnelProviderHost
let tunnelObserver = TunnelBlockObserver(
didUpdateTunnelStatus: { _, tunnelStatus in
switch tunnelStatus.state {
case let .error(blockedStateReason) where blockedStateReason == .noRelaysSatisfyingConstraints:
blockedExpectation.fulfill()
relaySelector.selectedRelaysResult = { relayConstraints, connectionAttemptCount in
try RelaySelectorStub.nonFallible().selectRelays(
with: relayConstraints,
connectionAttemptCount: connectionAttemptCount
)
}
tunnelManager.reconnectTunnel(selectNewRelay: true)
case .connected:
connectedExpectation.fulfill()
default:
return
}
}
)
self.tunnelObserver = tunnelObserver
tunnelManager.addObserver(tunnelObserver)
_ = try await tunnelManager.setNewAccount()
XCTAssertTrue(tunnelManager.deviceState.isLoggedIn)
tunnelManager.startTunnel()
await fulfillment(
of: [blockedExpectation, connectedExpectation],
timeout: .UnitTest.timeout,
enforceOrder: true
)
}
}