-
Notifications
You must be signed in to change notification settings - Fork 384
/
Copy pathAPIAccessMethodsTests.swift
143 lines (109 loc) · 3.98 KB
/
APIAccessMethodsTests.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
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
//
// APIAccessMethodsTests.swift
// MullvadVPNTests
//
// Created by Jon Petersson on 2023-12-13.
// Copyright © 2023 Mullvad VPN AB. All rights reserved.
//
import Combine
@testable import MullvadSettings
import XCTest
final class APIAccessMethodsTests: XCTestCase {
static let store = InMemorySettingsStore<SettingNotFound>()
override static func setUp() {
SettingsManager.unitTestStore = store
}
override static func tearDown() {
SettingsManager.unitTestStore = nil
}
override func tearDownWithError() throws {
let repository = AccessMethodRepository()
repository.fetchAll().forEach {
repository.delete(id: $0.id)
}
}
func testDefaultAccessMethodsExist() throws {
let repository = AccessMethodRepository()
let storedMethods = repository.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 repository = AccessMethodRepository()
let uuid = UUID()
let methodToStore = socks5AccessMethod(with: uuid)
repository.save(methodToStore)
let storedMethod = repository.fetch(by: uuid)
XCTAssertEqual(methodToStore.id, storedMethod?.id)
}
func testAddingShadowSocksAccessMethod() throws {
let repository = AccessMethodRepository()
let uuid = UUID()
let methodToStore = shadowsocksAccessMethod(with: uuid)
repository.save(methodToStore)
let storedMethod = repository.fetch(by: uuid)
XCTAssertEqual(methodToStore.id, storedMethod?.id)
}
func testAddingDuplicateAccessMethodDoesNothing() throws {
let repository = AccessMethodRepository()
let methodToStore = socks5AccessMethod(with: UUID())
repository.save(methodToStore)
repository.save(methodToStore)
let storedMethods = repository.fetchAll()
// Account for .direct and .bridges that are always added by default.
XCTAssertEqual(storedMethods.count, 3)
}
func testUpdatingAccessMethod() throws {
let repository = AccessMethodRepository()
let uuid = UUID()
var methodToStore = socks5AccessMethod(with: uuid)
repository.save(methodToStore)
let newName = "Renamed method"
methodToStore.name = newName
repository.save(methodToStore)
let storedMethod = repository.fetch(by: uuid)
XCTAssertEqual(storedMethod?.name, newName)
}
func testDeletingAccessMethod() throws {
let repository = AccessMethodRepository()
let uuid = UUID()
let methodToStore = socks5AccessMethod(with: uuid)
repository.save(methodToStore)
repository.delete(id: uuid)
let storedMethod = repository.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
))
)
}
}