mirror of
https://github.com/shadowsocks/ShadowsocksX-NG.git
synced 2026-11-22 05:25:41 +00:00
Remove the PAC product feature entirely: PAC HTTP service, port, health probe, runtime extension, PAC URL system proxy projection, PAC rule editing, and GFWList URL settings. The product now exposes rule / global / direct modes only, all projected as a local SOCKS system-proxy target. First-run defaults are rule + proxyWhenUnmatched, agent on, system proxy off. Homepage and status menu share one ProxyControlSnapshot showing the three modes and rule sub-options. Manual servers, subscriptions, Legacy import, and independent SOCKS/HTTP inbounds are retained. Fix: include verbose in SslocalListenFingerprint so a verbose-only contract change restarts sslocal instead of leaving RUST_LOG stale. Revise CONTEXT.md vocabulary, ADR-0007, and ADR-0009. Record GFWList exception shadowing, IPv6 system-exception, DNS, and traffic-coverage boundaries. Add host acceptance evidence document. Closes #67
127 lines
4.4 KiB
Swift
127 lines
4.4 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 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)
|
|
}
|
|
|
|
func testResetRemovesSettingsAndLeavesFactoryDefaultsAvailable() throws {
|
|
try store.save(ProxySettings())
|
|
try store.reset()
|
|
|
|
XCTAssertFalse(FileManager.default.fileExists(atPath: store.fileURL.path))
|
|
XCTAssertEqual(try store.load(), ProxySettings())
|
|
}
|
|
}
|