mirror of
https://github.com/shadowsocks/ShadowsocksX-NG.git
synced 2026-11-22 05:25:41 +00:00
Not every preference warrants a reset; future resets are per configuration item, added only where a field truly needs one (tracked in #69). The whole chain goes: the 恢复默认 button and its confirmation alert in SettingsView, the SettingsWorkflow reset/confirm/cancel commands and the confirmation machinery they existed for (the SettingsConfirmation enum had a single case), the SettingsCommitting.resetPreferences seam entry, the runtime resetPreferences() and ProxySettingsFileStore.reset() with its file-rollback helpers and the rollbackFailed store error. CONTEXT.md drops the three stale reset phrases and now records the domain decision: no restore-all-factory-defaults action for preferences; a reset, where it exists, is scoped to one configuration item. ADR-0004/0005/0011 carry amendment notes since their reset sentences describe a removed capability (0004's also predate the PAC removal).
133 lines
4.6 KiB
Swift
133 lines
4.6 KiB
Swift
import Foundation
|
|
import XCTest
|
|
|
|
@testable import ShadowsocksX_NG2
|
|
|
|
/// #33 偏好持久化缝:设置文件只保存公开偏好;重置不触碰配置目录与活动目标
|
|
/// 使用的其他文件。
|
|
final class ProxySettingsFileStoreTests: XCTestCase {
|
|
private var directory: URL!
|
|
private var store: ProxySettingsFileStore!
|
|
|
|
override func setUpWithError() throws {
|
|
try super.setUpWithError()
|
|
directory = FileManager.default.temporaryDirectory
|
|
.appendingPathComponent("ssxng-settings-tests-\(UUID().uuidString)", isDirectory: true)
|
|
store = ProxySettingsFileStore(
|
|
fileURL: directory.appendingPathComponent("settings.json"),
|
|
legacyListenFileURL: directory.appendingPathComponent("listen-settings.json"))
|
|
}
|
|
|
|
override func tearDownWithError() throws {
|
|
try? FileManager.default.removeItem(at: directory)
|
|
try super.tearDownWithError()
|
|
}
|
|
|
|
private func writeRaw(_ text: String) throws {
|
|
try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true)
|
|
try Data(text.utf8).write(to: store.fileURL)
|
|
}
|
|
|
|
private func makeWriteFailingStore() throws -> ProxySettingsFileStore {
|
|
try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true)
|
|
let blockedParent = directory.appendingPathComponent("blocked")
|
|
try Data("not a directory".utf8).write(to: blockedParent)
|
|
return ProxySettingsFileStore(
|
|
fileURL: blockedParent.appendingPathComponent("settings.json"),
|
|
legacyListenFileURL: directory.appendingPathComponent("listen-settings.json"))
|
|
}
|
|
|
|
func testRoundTripKeepsPreferences() throws {
|
|
var settings = ProxySettings()
|
|
settings.listen.scope = .host(advertisedAddress: "192.168.2.89")
|
|
settings.listen.socksPort = 2086
|
|
settings.listen.httpPort = 2087
|
|
settings.timeoutSeconds = 120
|
|
settings.verboseLogging = true
|
|
settings.proxyExceptions = "localhost, 127.0.0.1"
|
|
settings.preferredMode = .direct
|
|
|
|
try store.save(settings)
|
|
|
|
XCTAssertEqual(try store.load(), settings)
|
|
let raw = try String(contentsOf: store.fileURL, encoding: .utf8)
|
|
XCTAssertFalse(raw.contains("udpRelayEnabled"))
|
|
XCTAssertEqual(try store.load().listen.mode, "tcp_and_udp")
|
|
}
|
|
|
|
func testLoadPreservesPersistedLegacyDefaultPortsWithoutImplicitMigration() throws {
|
|
var settings = ProxySettings()
|
|
settings.listen.socksPort = 1086
|
|
settings.listen.httpPort = 1087
|
|
try store.save(settings)
|
|
|
|
let loaded = try store.load()
|
|
|
|
XCTAssertEqual(loaded.listen.socksPort, 1086)
|
|
XCTAssertEqual(loaded.listen.httpPort, 1087)
|
|
}
|
|
|
|
func testMissingPortFieldsUseNewFactoryDefaults() throws {
|
|
try writeRaw("{}")
|
|
|
|
XCTAssertEqual(try store.load(), ProxySettings())
|
|
}
|
|
|
|
func testUnknownEnabledModesIsIgnoredAndNeverWrittenBack() throws {
|
|
try writeRaw("{\"enabledModes\":[\"global\"],\"preferredMode\":\"global\"}")
|
|
|
|
XCTAssertEqual(try store.load().preferredMode, .global)
|
|
try store.save(ProxySettings())
|
|
let raw = try String(contentsOf: store.fileURL, encoding: .utf8)
|
|
XCTAssertFalse(raw.contains("enabledModes"))
|
|
}
|
|
|
|
func testLegacyPACPreferredModeFallsBackToRuleWithoutLosingOtherFields() throws {
|
|
try writeRaw(
|
|
"""
|
|
{"preferredMode":"pac","socksPort":2086,"httpPort":2087,"timeoutSeconds":120,
|
|
"pacUserRules":"@@example.com","gfwListURLConfigured":true,"pacPort":2089}
|
|
""")
|
|
|
|
let loaded = try store.load()
|
|
XCTAssertEqual(loaded.preferredMode, .rule)
|
|
XCTAssertEqual(loaded.listen.socksPort, 2086)
|
|
XCTAssertEqual(loaded.listen.httpPort, 2087)
|
|
XCTAssertEqual(loaded.timeoutSeconds, 120)
|
|
}
|
|
|
|
func testInvalidSaveDoesNotTouchExistingSettings() throws {
|
|
try store.save(ProxySettings())
|
|
var invalid = ProxySettings()
|
|
invalid.timeoutSeconds = 0
|
|
|
|
XCTAssertThrowsError(try store.save(invalid)) { error in
|
|
XCTAssertEqual(
|
|
error as? ProxySettingsStoreError,
|
|
.invalid([.invalidTimeout(0)]))
|
|
}
|
|
XCTAssertEqual(try store.load(), ProxySettings())
|
|
}
|
|
|
|
func testSaveFailureIsTypedIOFailure() throws {
|
|
store = try makeWriteFailingStore()
|
|
|
|
XCTAssertThrowsError(try store.save(ProxySettings())) { error in
|
|
guard case .ioFailure = error as? ProxySettingsStoreError else {
|
|
return XCTFail("文件写入失败应保留为 typed io failure,实际为 \(error)")
|
|
}
|
|
}
|
|
}
|
|
|
|
func testMissingNewFileMigratesLegacyListenSettingsAndUsesNewDefaults() throws {
|
|
var legacy = SslocalListenSettings()
|
|
legacy.socksPort = 2086
|
|
try ListenSettingsFileStore(fileURL: store.legacyListenFileURL).save(legacy)
|
|
|
|
let loaded = try store.load()
|
|
|
|
XCTAssertEqual(loaded.listen, legacy)
|
|
XCTAssertEqual(loaded.timeoutSeconds, 60)
|
|
}
|
|
}
|