-
Notifications
You must be signed in to change notification settings - Fork 392
/
Copy pathStartTunnelOperationTests.swift
101 lines (90 loc) · 3.38 KB
/
StartTunnelOperationTests.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
//
// StartTunnelOperationTests.swift
// MullvadVPNTests
//
// Created by Andrew Bulhak on 2024-02-02.
// Copyright © 2024 Mullvad VPN AB. All rights reserved.
//
import MullvadSettings
@testable import MullvadVPN
import Network
import Operations
import WireGuardKitTypes
import XCTest
class StartTunnelOperationTests: XCTestCase {
// MARK: utility code for setting up tests
let testQueue = DispatchQueue(label: "StartTunnelOperationTests.testQueue")
let operationQueue = AsyncOperationQueue()
let loggedInDeviceState = DeviceState.loggedIn(
StoredAccountData(
identifier: "",
number: "",
expiry: .distantFuture
),
StoredDeviceData(
creationDate: Date(),
identifier: "",
name: "",
hijackDNS: false,
ipv4Address: IPAddressRange(from: "127.0.0.1/32")!,
ipv6Address: IPAddressRange(from: "::ff/64")!,
wgKeyData: StoredWgKeyData(creationDate: Date(), privateKey: PrivateKey())
)
)
func makeInteractor(deviceState: DeviceState, tunnelState: TunnelState? = nil) -> MockTunnelInteractor {
let interactor = MockTunnelInteractor(
isConfigurationLoaded: true,
settings: LatestTunnelSettings(),
deviceState: deviceState
)
if let tunnelState {
interactor.tunnelStatus = TunnelStatus(state: tunnelState)
}
return interactor
}
// MARK: the tests
func testFailsIfNotLoggedIn() throws {
let expectation = expectation(description: "Start tunnel operation failed")
let operation = StartTunnelOperation(
dispatchQueue: testQueue,
interactor: makeInteractor(deviceState: .loggedOut)
) { result in
guard case .failure = result else {
XCTFail("Operation returned \(result), not failure")
return
}
expectation.fulfill()
}
operationQueue.addOperation(operation)
wait(for: [expectation], timeout: 1.0)
}
func testSetsReconnectIfDisconnecting() {
let interactor = makeInteractor(deviceState: loggedInDeviceState, tunnelState: .disconnecting(.nothing))
var tunnelStatus = TunnelStatus()
interactor.onUpdateTunnelStatus = { status in tunnelStatus = status }
let expectation = expectation(description: "Tunnel status set to reconnect")
let operation = StartTunnelOperation(
dispatchQueue: testQueue,
interactor: interactor
) { _ in
XCTAssertEqual(tunnelStatus.state, .disconnecting(.reconnect))
expectation.fulfill()
}
operationQueue.addOperation(operation)
wait(for: [expectation], timeout: 1.0)
}
func testStartsTunnelIfDisconnected() {
let interactor = makeInteractor(deviceState: loggedInDeviceState, tunnelState: .disconnected)
let expectation = expectation(description: "Make tunnel provider and start tunnel")
let operation = StartTunnelOperation(
dispatchQueue: testQueue,
interactor: interactor
) { _ in
XCTAssertNotNil(interactor.tunnel)
XCTAssertNotNil(interactor.tunnel?.startDate)
expectation.fulfill()
}
operationQueue.addOperation(operation)
wait(for: [expectation], timeout: 1.0)
}
}