summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadVPNTests/MullvadREST/Relay/RelaySelectorWrapperTests.swift
blob: 6352ade1935d71ca88ec10c8b53bf7ec77efe160 (plain)
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
//
//  RelaySelectorWrapperTests.swift
//  MullvadVPNTests
//
//  Created by Jon Petersson on 2024-06-10.
//  Copyright © 2025 Mullvad VPN AB. All rights reserved.
//

import MullvadMockData
import XCTest

@testable import MullvadREST
@testable import MullvadSettings
@testable import MullvadTypes

class RelaySelectorWrapperTests: XCTestCase {
    let multihopWithDaitaConstraints = RelayConstraints(
        entryLocations: .only(UserSelectedRelays(locations: [.country("es")])),  // Relay with DAITA.
        exitLocations: .only(UserSelectedRelays(locations: [.country("us")]))
    )

    let multihopWithoutDaitaConstraints = RelayConstraints(
        entryLocations: .only(UserSelectedRelays(locations: [.country("se")])),  // Relay without DAITA.
        exitLocations: .only(UserSelectedRelays(locations: [.country("us")]))
    )

    let singlehopWithoutDaitaConstraints = RelayConstraints(
        exitLocations: .only(UserSelectedRelays(locations: [.country("se")]))  // Relay without DAITA.
    )

    let singlehopWithDaitaConstraints = RelayConstraints(
        exitLocations: .only(UserSelectedRelays(locations: [.country("es")]))  // Relay with DAITA.
    )

    var relayCache: RelayCache!
    override func setUpWithError() throws {
        let fileCache = MockFileCache(
            initialState: .exists(
                try StoredRelays(
                    rawData: try REST.Coding.makeJSONEncoder().encode(ServerRelaysResponseStubs.sampleRelays),
                    updatedAt: .distantPast
                ))
        )

        relayCache = RelayCache(fileCache: fileCache)
    }

    func testSelectRelayWithMultihopOff() throws {
        let wrapper = RelaySelectorWrapper(relayCache: relayCache)

        let settings = LatestTunnelSettings(
            relayConstraints: singlehopWithoutDaitaConstraints,
            tunnelMultihopState: .off,
            daita: DAITASettings(daitaState: .off)
        )

        let selectedRelays = try wrapper.selectRelays(tunnelSettings: settings, connectionAttemptCount: 0)
        XCTAssertNil(selectedRelays.entry)
    }

    func testSelectRelayWithMultihopOn() throws {
        let wrapper = RelaySelectorWrapper(relayCache: relayCache)

        let settings = LatestTunnelSettings(
            relayConstraints: multihopWithDaitaConstraints,
            tunnelMultihopState: .on,
            daita: DAITASettings(daitaState: .off)
        )

        let selectedRelays = try wrapper.selectRelays(tunnelSettings: settings, connectionAttemptCount: 0)
        XCTAssertNotNil(selectedRelays.entry)
    }

    func testCanSelectRelayWithMultihopOnAndDaitaOn() throws {
        let wrapper = RelaySelectorWrapper(relayCache: relayCache)

        let settings = LatestTunnelSettings(
            relayConstraints: multihopWithDaitaConstraints,
            tunnelMultihopState: .on,
            daita: DAITASettings(daitaState: .on)
        )

        XCTAssertNoThrow(try wrapper.selectRelays(tunnelSettings: settings, connectionAttemptCount: 0))
    }

    func testCannotSelectRelayWithMultihopOnDaitaOnDirectOnlyOn() throws {
        let wrapper = RelaySelectorWrapper(relayCache: relayCache)

        let settings = LatestTunnelSettings(
            relayConstraints: multihopWithoutDaitaConstraints,
            tunnelMultihopState: .on,
            daita: DAITASettings(daitaState: .on, directOnlyState: .on)
        )

        XCTAssertThrowsError(try wrapper.selectRelays(tunnelSettings: settings, connectionAttemptCount: 0))
    }

    func testCanSelectRelayWithMultihopOffAndDaitaOn() throws {
        let wrapper = RelaySelectorWrapper(relayCache: relayCache)

        let settings = LatestTunnelSettings(
            relayConstraints: singlehopWithDaitaConstraints,
            tunnelMultihopState: .off,
            daita: DAITASettings(daitaState: .on)
        )

        let selectedRelays = try wrapper.selectRelays(tunnelSettings: settings, connectionAttemptCount: 0)
        XCTAssertNotNil(selectedRelays.exit)
    }

    // If DAITA is enabled and no supported relays are found, we should try to find the nearest
    // available relay that supports DAITA and use it as entry in a multihop selection.
    func testCanSelectRelayWithMultihopOffDaitaOnThroughMultihop() throws {
        let wrapper = RelaySelectorWrapper(relayCache: relayCache)

        let settings = LatestTunnelSettings(
            relayConstraints: singlehopWithoutDaitaConstraints,
            tunnelMultihopState: .off,
            daita: DAITASettings(daitaState: .on)
        )

        let selectedRelays = try wrapper.selectRelays(tunnelSettings: settings, connectionAttemptCount: 0)
        XCTAssertNotNil(selectedRelays.entry)
    }

    func testValidWireguardPortDoesNotThrow() throws {
        let wrapper = RelaySelectorWrapper(relayCache: relayCache)

        let settings = LatestTunnelSettings(
            relayConstraints: .init(
                port:
                    .only(
                        ServerRelaysResponseStubs.sampleRelays.wireguard.portRanges.first!.first!
                    )
            )
        )

        XCTAssertNoThrow(
            try wrapper
                .selectRelays(tunnelSettings: settings, connectionAttemptCount: 0)
        )
    }

    func testInvalidWireguardPortThrows() throws {
        let wrapper = RelaySelectorWrapper(relayCache: relayCache)

        var settings = LatestTunnelSettings(
            relayConstraints: .init(port: .only(1)),
            wireGuardObfuscation: .init(state: .automatic)
        )

        XCTAssertThrowsError(
            try wrapper
                .selectRelays(tunnelSettings: settings, connectionAttemptCount: 0)
        )

        settings = LatestTunnelSettings(
            relayConstraints: .init(port: .only(1)),
            wireGuardObfuscation: .init(state: .off)
        )

        XCTAssertThrowsError(
            try wrapper
                .selectRelays(tunnelSettings: settings, connectionAttemptCount: 0)
        )
    }

    func testInvalidWireguardPortDoesNotThrowWhenObfuscated() throws {
        let wrapper = RelaySelectorWrapper(relayCache: relayCache)

        var settings = LatestTunnelSettings(
            relayConstraints: .init(port: .only(1)),
            wireGuardObfuscation: .init(state: .quic)
        )

        XCTAssertNoThrow(
            try wrapper
                .selectRelays(tunnelSettings: settings, connectionAttemptCount: 0)
        )

        settings = LatestTunnelSettings(
            relayConstraints: .init(port: .only(1)),
            wireGuardObfuscation: .init(state: .udpOverTcp)
        )

        XCTAssertNoThrow(
            try wrapper
                .selectRelays(tunnelSettings: settings, connectionAttemptCount: 0)
        )

        settings = LatestTunnelSettings(
            relayConstraints: .init(port: .only(1)),
            wireGuardObfuscation: .init(state: .shadowsocks)
        )

        XCTAssertNoThrow(
            try wrapper
                .selectRelays(tunnelSettings: settings, connectionAttemptCount: 0)
        )
    }
}