-
Notifications
You must be signed in to change notification settings - Fork 384
/
Copy pathMigrationManagerTests.swift
222 lines (179 loc) · 8.3 KB
/
MigrationManagerTests.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
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
//
// MigrationManagerTests.swift
// MullvadVPNTests
//
// Created by Marco Nikic on 2023-10-17.
// Copyright © 2023 Mullvad VPN AB. All rights reserved.
//
@testable import MullvadMockData
@testable import MullvadREST
@testable import MullvadSettings
@testable import MullvadTypes
import XCTest
final class MigrationManagerTests: XCTestCase {
static let store = InMemorySettingsStore<SettingNotFound>()
var manager: MigrationManager!
override static func setUp() {
SettingsManager.unitTestStore = store
}
override static func tearDown() {
SettingsManager.unitTestStore = nil
}
override func setUpWithError() throws {
manager = MigrationManager()
}
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 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, port: .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: .on, port: .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)
}
private 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
}
}