summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadVPNTests/RelaySelectorTests.swift
blob: fb17f7eaf49461cf3e70035bafeae2606a04d2af (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
//
//  RelaySelectorTests.swift
//  RelaySelectorTests
//
//  Created by pronebird on 07/11/2019.
//  Copyright © 2019 Mullvad VPN AB. All rights reserved.
//

import XCTest
import Network

class RelaySelectorTests: XCTestCase {

    func testCountryConstraint() {
        let relaySelector = RelaySelector(relays: sampleRelays)
        let constraints = RelayConstraints(location: .only(.country("es")))

        let result = relaySelector.evaluate(with: constraints)

        XCTAssertEqual(result?.relay.hostname, "es1-wireguard")
    }

    func testCityConstraint() {
        let relaySelector = RelaySelector(relays: sampleRelays)
        let constraints = RelayConstraints(location: .only(.city("se", "got")))

        let result = relaySelector.evaluate(with: constraints)

        XCTAssertEqual(result?.relay.hostname, "se10-wireguard")
    }

    func testHostnameConstraint() {
        let relaySelector = RelaySelector(relays: sampleRelays)
        let constraints = RelayConstraints(location: .only(.hostname("se", "sto", "se6-wireguard")))

        let result = relaySelector.evaluate(with: constraints)

        XCTAssertEqual(result?.relay.hostname, "se6-wireguard")
    }

}

private let sampleRelays = ServerRelaysResponse(
    locations: [
        "es-mad": ServerLocation(
            country: "Spain",
            city: "Madrid",
            latitude: 40.408566,
            longitude: -3.69222
        ),
        "se-got": ServerLocation(
            country: "Sweden",
            city: "Gothenburg",
            latitude: 57.70887,
            longitude: 11.97456
        ),
        "se-sto": ServerLocation(
            country: "Sweden",
            city: "Stockholm",
            latitude: 59.3289,
            longitude: 18.0649
        )
    ],
    wireguard: ServerWireguardTunnels(
        ipv4Gateway: .loopback,
        ipv6Gateway: .loopback,
        portRanges: [53...53],
        relays: [
            ServerRelay(
                hostname: "es1-wireguard",
                active: true,
                owned: true,
                location: "es-mad",
                provider: "",
                weight: 500,
                ipv4AddrIn: .loopback,
                ipv6AddrIn: .loopback,
                publicKey: Data(),
                includeInCountry: true
            ),
            ServerRelay(
                hostname: "se10-wireguard",
                active: true,
                owned: true,
                location: "se-got",
                provider: "",
                weight: 1000,
                ipv4AddrIn: .loopback,
                ipv6AddrIn: .loopback,
                publicKey: Data(),
                includeInCountry: true
            ),
            ServerRelay(
                hostname: "se2-wireguard",
                active: true,
                owned: true,
                location: "se-sto",
                provider: "",
                weight: 50,
                ipv4AddrIn: .loopback,
                ipv6AddrIn: .loopback,
                publicKey: Data(),
                includeInCountry: true
            ),
            ServerRelay(
                hostname: "se6-wireguard",
                active: true,
                owned: true,
                location: "se-sto",
                provider: "",
                weight: 100,
                ipv4AddrIn: .loopback,
                ipv6AddrIn: .loopback,
                publicKey: Data(),
                includeInCountry: true
            )
    ])
)