@@ -15,16 +15,48 @@ import WireGuardKitTypes
15
15
16
16
class StartTunnelOperationTests : XCTestCase {
17
17
18
+ //MARK: utility code for setting up tests
19
+
18
20
let testQueue = DispatchQueue ( label: " StartTunnelOperationTests.testQueue " )
21
+ let operationQueue = AsyncOperationQueue ( )
22
+
23
+ let loggedInDeviceState = DeviceState . loggedIn (
24
+ StoredAccountData (
25
+ identifier: " " ,
26
+ number: " " ,
27
+ expiry: . distantFuture
28
+ ) ,
29
+ StoredDeviceData (
30
+ creationDate: Date ( ) ,
31
+ identifier: " " ,
32
+ name: " " ,
33
+ hijackDNS: false ,
34
+ ipv4Address: IPAddressRange ( from: " 127.0.0.1/32 " ) !,
35
+ ipv6Address: IPAddressRange ( from: " ::ff/64 " ) !,
36
+ wgKeyData: StoredWgKeyData ( creationDate: Date ( ) , privateKey: PrivateKey ( ) )
37
+ )
38
+ )
39
+
40
+ func makeInteractor( deviceState: DeviceState , tunnelState: TunnelState ? = nil ) -> MockTunnelInteractor {
41
+ var interactor = MockTunnelInteractor (
42
+ isConfigurationLoaded: true ,
43
+ settings: LatestTunnelSettings ( ) ,
44
+ deviceState: deviceState
45
+ )
46
+ if let tunnelState {
47
+ interactor. tunnelStatus = TunnelStatus ( state: tunnelState)
48
+ }
49
+ return interactor
50
+ }
51
+
52
+ //MARK: the tests
19
53
20
54
func testFailsIfNotLoggedIn( ) throws {
21
- let operationQueue = AsyncOperationQueue ( )
22
55
let settings = LatestTunnelSettings ( )
23
56
let exp = expectation ( description: " Start tunnel operation failed " )
24
57
let operation = StartTunnelOperation (
25
58
dispatchQueue: testQueue,
26
- interactor: MockTunnelInteractor ( isConfigurationLoaded: true , settings: settings, deviceState: . loggedOut) ) { result in
27
-
59
+ interactor: makeInteractor ( deviceState: . loggedOut) ) { result in
28
60
guard case . failure( _) = result else {
29
61
XCTFail ( " Operation returned \( result) , not failure " )
30
62
return
@@ -37,31 +69,9 @@ class StartTunnelOperationTests: XCTestCase {
37
69
}
38
70
39
71
func testSetsReconnectIfDisconnecting( ) {
40
- let operationQueue = AsyncOperationQueue ( )
41
72
let settings = LatestTunnelSettings ( )
42
- var interactor = MockTunnelInteractor (
43
- isConfigurationLoaded: true ,
44
- settings: settings,
45
- deviceState: . loggedIn(
46
- StoredAccountData (
47
- identifier: " " ,
48
- number: " " ,
49
- expiry: . distantFuture
50
- ) ,
51
- StoredDeviceData (
52
- creationDate: Date ( ) ,
53
- identifier: " " ,
54
- name: " " ,
55
- hijackDNS: false ,
56
- ipv4Address: IPAddressRange ( from: " 127.0.0.1/32 " ) !,
57
- ipv6Address: IPAddressRange ( from: " ::ff/64 " ) !,
58
- wgKeyData: StoredWgKeyData ( creationDate: Date ( ) , privateKey: PrivateKey ( ) )
59
- )
60
- )
61
- )
73
+ var interactor = makeInteractor ( deviceState: loggedInDeviceState, tunnelState: . disconnecting( . nothing) )
62
74
var tunnelStatus = TunnelStatus ( )
63
- tunnelStatus. state = . disconnecting( . nothing)
64
- interactor. tunnelStatus = tunnelStatus
65
75
interactor. onUpdateTunnelStatus = { status in tunnelStatus = status }
66
76
let expectation = expectation ( description: " Tunnel status set to reconnect " )
67
77
@@ -74,4 +84,19 @@ class StartTunnelOperationTests: XCTestCase {
74
84
operationQueue. addOperation ( operation)
75
85
wait ( for: [ expectation] , timeout: 1.0 )
76
86
}
87
+
88
+ func testStartsTunnelIfDisconnected( ) {
89
+ let settings = LatestTunnelSettings ( )
90
+ var interactor = makeInteractor ( deviceState: loggedInDeviceState, tunnelState: . disconnected)
91
+ let expectation = expectation ( description: " Make tunnel provider and start tunnel " )
92
+ let operation = StartTunnelOperation (
93
+ dispatchQueue: testQueue,
94
+ interactor: interactor) { result in
95
+ XCTAssertNotNil ( interactor. tunnel)
96
+ XCTAssertNotNil ( interactor. tunnel? . startDate)
97
+ expectation. fulfill ( )
98
+ }
99
+ operationQueue. addOperation ( operation)
100
+ wait ( for: [ expectation] , timeout: 1.0 )
101
+ }
77
102
}
0 commit comments