-
Notifications
You must be signed in to change notification settings - Fork 384
/
Copy pathRelaySelectorStub.swift
64 lines (56 loc) · 1.96 KB
/
RelaySelectorStub.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
//
// RelaySelectorStub.swift
// PacketTunnelCoreTests
//
// Created by pronebird on 05/09/2023.
// Copyright © 2023 Mullvad VPN AB. All rights reserved.
//
import MullvadREST
import MullvadTypes
import WireGuardKitTypes
/// Relay selector stub that accepts a block that can be used to provide custom implementation.
public final class RelaySelectorStub: RelaySelectorProtocol {
var selectedRelaysResult: (UInt) throws -> SelectedRelays
init(selectedRelaysResult: @escaping (UInt) throws -> SelectedRelays) {
self.selectedRelaysResult = selectedRelaysResult
}
public func selectRelays(connectionAttemptCount: UInt) throws -> SelectedRelays {
return try selectedRelaysResult(connectionAttemptCount)
}
}
extension RelaySelectorStub {
/// Returns a relay selector that never fails.
public static func nonFallible() -> RelaySelectorStub {
let publicKey = PrivateKey().publicKey.rawValue
return RelaySelectorStub { _ in
let cityRelay = SelectedRelay(
endpoint: MullvadEndpoint(
ipv4Relay: IPv4Endpoint(ip: .loopback, port: 1300),
ipv4Gateway: .loopback,
ipv6Gateway: .loopback,
publicKey: publicKey
),
hostname: "se-got",
location: Location(
country: "",
countryCode: "se",
city: "",
cityCode: "got",
latitude: 0,
longitude: 0
)
)
return SelectedRelays(
entry: cityRelay,
exit: cityRelay,
retryAttempt: 0
)
}
}
/// Returns a relay selector that cannot satisfy constraints .
public static func unsatisfied() -> RelaySelectorStub {
return RelaySelectorStub { _ in
throw NoRelaysSatisfyingConstraintsError()
}
}
}