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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
|
//
// MigrationManagerTests.swift
// MullvadVPNTests
//
// Created by Marco Nikic on 2023-10-17.
// Copyright © 2025 Mullvad VPN AB. All rights reserved.
//
import XCTest
@testable import MullvadMockData
@testable import MullvadREST
@testable import MullvadSettings
@testable import MullvadTypes
final class MigrationManagerTests: XCTestCase, @unchecked Sendable {
static let store = InMemorySettingsStore<SettingNotFound>()
var manager: MigrationManager!
var testFileURL: URL!
override static func setUp() {
SettingsManager.unitTestStore = store
}
override static func tearDown() {
store.reset()
}
override func setUpWithError() throws {
testFileURL = FileManager.default.temporaryDirectory
.appendingPathComponent("MigrationManagerTests-\(UUID().uuidString)", isDirectory: true)
try FileManager.default.createDirectory(at: testFileURL, withIntermediateDirectories: true)
manager = MigrationManager(cacheDirectory: testFileURL)
}
override func tearDownWithError() throws {
try FileManager.default.removeItem(at: testFileURL)
}
func testNothingToMigrate() throws {
let store = Self.store
let settings = LatestTunnelSettings()
try SettingsManager.writeSettings(settings)
let nothingToMigrateExpectation = expectation(description: "No migration")
manager.migrateSettings(store: store) { result in
if case .nothing = result {
nothingToMigrateExpectation.fulfill()
}
}
wait(for: [nothingToMigrateExpectation], timeout: .UnitTest.timeout)
}
func testNothingToMigrateWhenSettingsAreNotFound() throws {
let store = InMemorySettingsStore<KeychainError>()
SettingsManager.unitTestStore = store
let nothingToMigrateExpectation = expectation(description: "No migration")
manager.migrateSettings(store: store) { result in
if case .nothing = result {
nothingToMigrateExpectation.fulfill()
}
}
wait(for: [nothingToMigrateExpectation], timeout: .UnitTest.timeout)
// Reset the `SettingsManager` unit test store to avoid affecting other tests
// since it's a globally shared instance
SettingsManager.unitTestStore = Self.store
}
func testFailedMigration() throws {
let store = Self.store
let failedMigrationExpectation = expectation(description: "Failed migration")
manager.migrateSettings(store: store) { result in
if case .failure = result {
failedMigrationExpectation.fulfill()
}
}
wait(for: [failedMigrationExpectation], timeout: .UnitTest.timeout)
}
func testFailedMigrationResetsSettings() throws {
let store = Self.store
let data = Data("Migration test".utf8)
try store.write(data, for: .settings)
try store.write(data, for: .deviceState)
// Failed migration should reset settings and device state keys
manager.migrateSettings(store: store) { _ in }
let assertDeletionFor: (SettingsKey) throws -> Void = { key in
try XCTAssertThrowsError(store.read(key: key)) { thrownError in
XCTAssertTrue(thrownError is SettingNotFound)
}
}
try assertDeletionFor(.deviceState)
try assertDeletionFor(.lastUsedAccount)
}
func testFailedMigrationIfRecordedSettingsVersionHigherThanLatestSettings() throws {
let store = Self.store
let settings = FutureVersionSettings()
try write(settings: settings, version: Int.max - 1, in: store)
manager.migrateSettings(store: store) { _ in }
let assertDeletionFor: (SettingsKey) throws -> Void = { key in
try XCTAssertThrowsError(store.read(key: key)) { thrownError in
XCTAssertTrue(thrownError is SettingNotFound)
}
}
try assertDeletionFor(.deviceState)
try assertDeletionFor(.lastUsedAccount)
}
func testFailedMigrationCorruptedSchemaResetsSettings() throws {
let store = Self.store
let settings = FutureVersionSettings()
try write(settings: settings, version: -42, in: store)
let failedMigrationExpectation = expectation(description: "Failed migration")
manager.migrateSettings(store: store) { result in
if case .failure = result {
failedMigrationExpectation.fulfill()
}
}
wait(for: [failedMigrationExpectation], timeout: .UnitTest.timeout)
}
func testSuccessfulMigrationFromV6ToLatest() throws {
var settingsV6 = TunnelSettingsV6()
let relayConstraints = RelayConstraints(
exitLocations: .only(UserSelectedRelays(locations: [.city("jp", "osa")]))
)
settingsV6.relayConstraints = relayConstraints
settingsV6.tunnelQuantumResistance = .off
settingsV6.wireGuardObfuscation = WireGuardObfuscationSettings(
state: .off,
udpOverTcpPort: .automatic
)
settingsV6.tunnelMultihopState = .off
settingsV6.daita = .init(daitaState: .on)
try migrateToLatest(settingsV6, version: .v6)
// Once the migration is done, settings should have been updated to the latest available version
// Verify that the old settings are still valid
let latestSettings = try SettingsManager.readSettings()
XCTAssertEqual(settingsV6.relayConstraints, latestSettings.relayConstraints)
XCTAssertEqual(settingsV6.tunnelQuantumResistance, latestSettings.tunnelQuantumResistance)
XCTAssertEqual(settingsV6.wireGuardObfuscation, latestSettings.wireGuardObfuscation)
XCTAssertEqual(settingsV6.tunnelMultihopState, latestSettings.tunnelMultihopState)
XCTAssertEqual(settingsV6.daita, latestSettings.daita)
}
func testSuccessfulMigrationFromV5ToLatest() throws {
var settingsV5 = TunnelSettingsV5()
let relayConstraints = RelayConstraints(
exitLocations: .only(UserSelectedRelays(locations: [.city("jp", "osa")]))
)
settingsV5.relayConstraints = relayConstraints
settingsV5.tunnelQuantumResistance = .off
settingsV5.wireGuardObfuscation = WireGuardObfuscationSettings(
state: .off,
udpOverTcpPort: .automatic
)
settingsV5.tunnelMultihopState = .off
try migrateToLatest(settingsV5, version: .v5)
// Once the migration is done, settings should have been updated to the latest available version
// Verify that the old settings are still valid
let latestSettings = try SettingsManager.readSettings()
XCTAssertEqual(settingsV5.relayConstraints, latestSettings.relayConstraints)
XCTAssertEqual(settingsV5.tunnelQuantumResistance, latestSettings.tunnelQuantumResistance)
XCTAssertEqual(settingsV5.wireGuardObfuscation, latestSettings.wireGuardObfuscation)
XCTAssertEqual(settingsV5.tunnelMultihopState, latestSettings.tunnelMultihopState)
}
func testSuccessfulMigrationFromV4ToLatest() throws {
var settingsV4 = TunnelSettingsV4()
let relayConstraints = RelayConstraints(
exitLocations: .only(UserSelectedRelays(locations: [.city("jp", "osa")]))
)
settingsV4.relayConstraints = relayConstraints
settingsV4.tunnelQuantumResistance = .off
settingsV4.wireGuardObfuscation = WireGuardObfuscationSettings(
state: .off,
udpOverTcpPort: .automatic
)
try migrateToLatest(settingsV4, version: .v4)
// Once the migration is done, settings should have been updated to the latest available version
// Verify that the old settings are still valid
let latestSettings = try SettingsManager.readSettings()
XCTAssertEqual(settingsV4.relayConstraints, latestSettings.relayConstraints)
XCTAssertEqual(settingsV4.tunnelQuantumResistance, latestSettings.tunnelQuantumResistance)
XCTAssertEqual(settingsV4.wireGuardObfuscation, latestSettings.wireGuardObfuscation)
}
func testSuccessfulMigrationFromV3ToLatest() throws {
var settingsV3 = TunnelSettingsV3()
let relayConstraints = RelayConstraints(
exitLocations: .only(UserSelectedRelays(locations: [.city("jp", "osa")]))
)
settingsV3.relayConstraints = relayConstraints
settingsV3.dnsSettings = DNSSettings()
settingsV3.wireGuardObfuscation = WireGuardObfuscationSettings(
state: .udpOverTcp,
udpOverTcpPort: .port80
)
try migrateToLatest(settingsV3, version: .v3)
// Once the migration is done, settings should have been updated to the latest available version
// Verify that the old settings are still valid
let latestSettings = try SettingsManager.readSettings()
XCTAssertEqual(settingsV3.relayConstraints, latestSettings.relayConstraints)
XCTAssertEqual(settingsV3.wireGuardObfuscation, latestSettings.wireGuardObfuscation)
}
func testSuccessfulMigrationFromV2ToLatest() throws {
var settingsV2 = TunnelSettingsV2()
let osakaRelayConstraints = RelayConstraints(
exitLocations: .only(UserSelectedRelays(locations: [.city("jp", "osa")]))
)
settingsV2.relayConstraints = osakaRelayConstraints
try migrateToLatest(settingsV2, version: .v2)
let latestSettings = try SettingsManager.readSettings()
XCTAssertEqual(osakaRelayConstraints, latestSettings.relayConstraints)
}
func testSuccessfulMigrationFromV1ToLatest() throws {
var settingsV1 = TunnelSettingsV1()
let osakaRelayConstraints = RelayConstraints(
exitLocations: .only(UserSelectedRelays(locations: [.city("jp", "osa")]))
)
settingsV1.relayConstraints = osakaRelayConstraints
try migrateToLatest(settingsV1, version: .v1)
// Once the migration is done, settings should have been updated to the latest available version
// Verify that the old settings are still valid
let latestSettings = try SettingsManager.readSettings()
XCTAssertEqual(osakaRelayConstraints, latestSettings.relayConstraints)
}
private func migrateToLatest(_ settings: any TunnelSettings, version: SchemaVersion) throws {
let store = Self.store
try write(settings: settings, version: version.rawValue, in: store)
let successfulMigrationExpectation = expectation(description: "Successful migration")
manager.migrateSettings(store: store) { result in
if case .success = result {
successfulMigrationExpectation.fulfill()
}
}
wait(for: [successfulMigrationExpectation], timeout: .UnitTest.timeout)
}
func write(settings: any TunnelSettings, version: Int, in store: SettingsStore) throws {
let parser = SettingsParser(decoder: JSONDecoder(), encoder: JSONEncoder())
let payload = try parser.producePayload(settings, version: version)
try store.write(payload, for: .settings)
}
}
private struct FutureVersionSettings: TunnelSettings {
func upgradeToNextVersion() -> TunnelSettings { self }
}
struct SettingNotFound: Error, Instantiable {}
extension KeychainError: Instantiable {
init() {
self = KeychainError.itemNotFound
}
}
|