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
|
//
// APIAccessMethodsTests.swift
// MullvadVPNTests
//
// Created by Jon Petersson on 2023-12-13.
// Copyright © 2023 Mullvad VPN AB. All rights reserved.
//
@testable import MullvadSettings
import XCTest
final class APIAccessMethodsTests: XCTestCase {
static let store = InMemorySettingsStore<SettingNotFound>()
override class func setUp() {
SettingsManager.unitTestStore = store
}
override class func tearDown() {
SettingsManager.unitTestStore = nil
}
override func tearDownWithError() throws {
let repository = AccessMethodRepository.shared
repository.fetchAll().forEach {
repository.delete(id: $0.id)
}
}
func testDefaultAccessMethodsExist() throws {
let storedMethods = AccessMethodRepository.shared.fetchAll()
let hasDirectMethod = storedMethods.contains { method in
method.kind == .direct
}
let hasBridgesMethod = storedMethods.contains { method in
method.kind == .bridges
}
XCTAssertEqual(storedMethods.count, 2)
XCTAssertTrue(hasDirectMethod && hasBridgesMethod)
}
func testAddingSocks5AccessMethod() throws {
let uuid = UUID()
let methodToStore = socks5AccessMethod(with: uuid)
AccessMethodRepository.shared.add(methodToStore)
let storedMethod = AccessMethodRepository.shared.fetch(by: uuid)
XCTAssertEqual(methodToStore.id, storedMethod?.id)
}
func testAddingShadowSocksAccessMethod() throws {
let uuid = UUID()
let methodToStore = shadowsocksAccessMethod(with: uuid)
AccessMethodRepository.shared.add(methodToStore)
let storedMethod = AccessMethodRepository.shared.fetch(by: uuid)
XCTAssertEqual(methodToStore.id, storedMethod?.id)
}
func testAddingDuplicateAccessMethodDoesNothing() throws {
let methodToStore = socks5AccessMethod(with: UUID())
AccessMethodRepository.shared.add(methodToStore)
AccessMethodRepository.shared.add(methodToStore)
let storedMethods = AccessMethodRepository.shared.fetchAll()
// Account for .direct and .bridges that are always added by default.
XCTAssertEqual(storedMethods.count, 3)
}
func testUpdatingAccessMethod() throws {
let uuid = UUID()
var methodToStore = socks5AccessMethod(with: uuid)
AccessMethodRepository.shared.add(methodToStore)
let newName = "Renamed method"
methodToStore.name = newName
AccessMethodRepository.shared.update(methodToStore)
let storedMethod = AccessMethodRepository.shared.fetch(by: uuid)
XCTAssertEqual(storedMethod?.name, newName)
}
func testDeletingAccessMethod() throws {
let uuid = UUID()
let methodToStore = socks5AccessMethod(with: uuid)
AccessMethodRepository.shared.add(methodToStore)
AccessMethodRepository.shared.delete(id: uuid)
let storedMethod = AccessMethodRepository.shared.fetch(by: uuid)
XCTAssertNil(storedMethod)
}
}
extension APIAccessMethodsTests {
private func socks5AccessMethod(with uuid: UUID) -> PersistentAccessMethod {
PersistentAccessMethod(
id: uuid,
name: "Method",
isEnabled: true,
proxyConfiguration: .socks5(PersistentProxyConfiguration.SocksConfiguration(
server: .ipv4(.any),
port: 1,
authentication: .noAuthentication
))
)
}
private func shadowsocksAccessMethod(with uuid: UUID) -> PersistentAccessMethod {
PersistentAccessMethod(
id: uuid,
name: "Method",
isEnabled: true,
proxyConfiguration: .shadowsocks(PersistentProxyConfiguration.ShadowsocksConfiguration(
server: .ipv4(.any),
port: 1,
password: "Password",
cipher: .default
))
)
}
}
|