Files
ShadowsocksX-NG/ShadowsocksX-NG2/Tests/SystemProxyTests.swift
T
Qiu Yuzhou 43ea00cb44 fix(system-proxy): use BundleProgram in daemon plist and refresh stale registration
launchd 对 ProgramArguments[0] 的相对路径按 _PATH_STDPATH(PATH)解析而非
bundle:系统域 daemon 的 job 因此永远 spawn 失败(EX_CONFIG 节流循环,
launchctl print 显示 job state = spawn failed、runs 持续累加),客户端 XPC
调用只能等到超时。agent 之所以正常,是 GUI 域导入时被解析成绝对路径,system
域 submitted job 没有这一步。SMAppService 清单的正确键是 BundleProgram
(bundle 相对,launchd.plist(5)),改用之。

launchd 同时沿用注册提交时的定义快照:app 更新改动 LaunchDaemon 清单后旧
定义不会自动刷新(register() 对已注册服务返回 alreadyRegistered),必须注销
重注。门禁在已批准分支按清单内容 SHA-256 指纹(UserDefaults)检测漂移,漂移
才注销重注——审批与 bundle 签名绑定,重注不重新弹授权;首次注册成功即记指
纹,失败不记(下次启动重试),指纹一致为一次哈希比对的 no-op。
2026-09-30 18:54:19 +08:00

243 lines
11 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import XCTest
@testable import ShadowsocksX_NG2
/// System proxy mode mapping and declarative planning are pure/testable seams;
/// no test in this file writes the host's real SystemConfiguration state.
/// issue #71:closed typed 配置、显式 HTTPS/PAC/自动发现/简单主机名/例外列表、
/// 无条件清理计划与 helper XPC 契约(含序列化 last-writer)都在此验证。
final class SystemProxyTests: XCTestCase {
func testSupportedModesProjectTheSameLocalEndpoints() throws {
let document = ProxyRuntimeFixture.makeDocument(
listenerMode: .allIPv4Interfaces, localPort: 2086)
let expected = SystemProxyConfiguration(
socks: .init(host: "127.0.0.1", port: 2086),
http: .init(host: "127.0.0.1", port: SslocalListenSettings.defaultHTTPPort),
https: .init(host: "127.0.0.1", port: SslocalListenSettings.defaultHTTPPort),
exceptions: FixedLocalProxyRanges.systemProxyExceptions)
XCTAssertEqual(
try ProxyMode.rule.systemProxyConfiguration(for: document),
expected)
XCTAssertEqual(
try ProxyMode.global.systemProxyConfiguration(for: document),
expected,
"全局模式的系统例外使用固定本地范围,与 ACL 安全策略一致")
}
func testIPv6OnlyModeProjectsSystemProxyToIPv6Loopback() throws {
let document = SslocalRuntimeDocument(
servers: [], listen: SslocalListenSettings(listenerMode: .allIPv6Interfaces))
let configuration = try ProxyMode.rule.systemProxyConfiguration(for: document)
XCTAssertEqual(configuration.socks.host, "::1")
XCTAssertEqual(configuration.http.host, "::1")
XCTAssertEqual(configuration.https.host, "::1", "HTTPS 显式赋值,与 HTTP 同端点")
}
func testMissingHTTPInboundIsRejectedInsteadOfPointingAtAWildcardPort() throws {
// 防御路径:well-formed 文档允许缺 HTTP 入站;此时不得把系统代理指向
// 无效端口 0,而应按模式错误拒绝。
let json = """
{"servers":[],"locals":[{"protocol":"socks","local_address":"127.0.0.1",\
"local_port":11086,"mode":"tcp_and_udp"}],\
"x_shadowsocksx_ng_listen":{"listener_mode":"localhost",\
"bind_address":"127.0.0.1"}}
"""
let document = try XCTUnwrap(SslocalRuntimeDocument.decodeValidated(Data(json.utf8)))
XCTAssertThrowsError(try ProxyMode.rule.systemProxyConfiguration(for: document)) { error in
XCTAssertEqual(error as? ProxyModeError, .invalidHTTPPort(0))
}
}
func testModeProjectionCarriesClosedTypedPolicyDefaults() throws {
let document = ProxyRuntimeFixture.makeDocument(localPort: 2086)
let configuration = try ProxyMode.rule.systemProxyConfiguration(
for: document, exceptions: ["example.com"])
XCTAssertTrue(configuration.pacEnabled == false, "PAC 显式关闭")
XCTAssertTrue(configuration.autoDiscoveryEnabled == false, "自动发现显式关闭")
XCTAssertTrue(configuration.excludeSimpleHostnames, "简单主机名排除由 GUI 拥有,默认开")
XCTAssertEqual(
configuration.exceptions,
FixedLocalProxyRanges.systemProxyExceptions(including: ["example.com"]),
"例外列表 = 固定本地范围 + 用户补充项,全量给出")
}
// MARK: - Property-list projection
private let configuration = SystemProxyConfiguration(
socks: .init(host: "127.0.0.1", port: 1086),
http: .init(host: "127.0.0.1", port: 1087),
https: .init(host: "127.0.0.1", port: 1087),
exceptions: ["localhost", "127.0.0.1"])
func testSystemConfigurationProjectionEnablesSOCKSHTTPAndHTTPS() {
let original: [String: Any] = [
SystemProxyPropertyList.httpEnabled: 1,
SystemProxyPropertyList.httpsEnabled: 1,
SystemProxyPropertyList.socksEnabled: 1,
SystemProxyPropertyList.pacEnabled: 1,
SystemProxyPropertyList.pacURL: "http://127.0.0.1:1089/v1/proxy.pac",
"ExceptionsList": ["stale.example"],
]
let applied = SystemProxyPropertyList.applying(configuration, to: original)
XCTAssertEqual(applied[SystemProxyPropertyList.pacEnabled] as? Int, 0)
XCTAssertNil(applied[SystemProxyPropertyList.pacURL])
XCTAssertNil(applied[SystemProxyPropertyList.pacJavaScript])
XCTAssertEqual(applied[SystemProxyPropertyList.socksEnabled] as? Int, 1)
XCTAssertEqual(applied[SystemProxyPropertyList.socksProxy] as? String, "127.0.0.1")
XCTAssertEqual(applied[SystemProxyPropertyList.socksPort] as? Int, 1086)
XCTAssertEqual(applied[SystemProxyPropertyList.httpEnabled] as? Int, 1)
XCTAssertEqual(applied[SystemProxyPropertyList.httpProxy] as? String, "127.0.0.1")
XCTAssertEqual(applied[SystemProxyPropertyList.httpPort] as? Int, 1087)
XCTAssertEqual(applied[SystemProxyPropertyList.httpsEnabled] as? Int, 1)
XCTAssertEqual(applied[SystemProxyPropertyList.httpsProxy] as? String, "127.0.0.1")
XCTAssertEqual(applied[SystemProxyPropertyList.httpsPort] as? Int, 1087)
XCTAssertEqual(applied[SystemProxyPropertyList.excludeSimpleHostnames] as? Int, 1)
XCTAssertEqual(applied[SystemProxyPropertyList.autoDiscoveryEnabled] as? Int, 0)
XCTAssertEqual(
applied[SystemProxyPropertyList.exceptionsList] as? [String],
["localhost", "127.0.0.1"],
"例外列表全量覆盖,不保留旧值")
}
/// 显式简单主机名排除值:GUI 拥有该策略(当前恒开),helper 只翻译。
func testSimpleHostnameExclusionFollowsExplicitFlag() {
let withFlagOn = SystemProxyPropertyList.applying(configuration, to: [:])
XCTAssertEqual(withFlagOn[SystemProxyPropertyList.excludeSimpleHostnames] as? Int, 1)
var flagOff = configuration
flagOff.excludeSimpleHostnames = false
let withFlagOff = SystemProxyPropertyList.applying(
flagOff, to: [SystemProxyPropertyList.excludeSimpleHostnames: 1])
XCTAssertEqual(withFlagOff[SystemProxyPropertyList.excludeSimpleHostnames] as? Int, 0)
}
/// 空例外列表也是显式值:写入空数组而非保留旧列表(issue #71 AC29)。
func testEmptyExceptionListIsWrittenExplicitly() {
var withoutExceptions = configuration
withoutExceptions.exceptions = []
let applied = SystemProxyPropertyList.applying(
withoutExceptions, to: ["ExceptionsList": ["stale.example"]])
XCTAssertEqual(applied[SystemProxyPropertyList.exceptionsList] as? [String], [])
}
/// 未建模键保留(issue #71 AC30):typed 字段集之外的 Proxies 键不被擦除。
func testUnmodeledKeysArePreservedOnApply() {
let original: [String: Any] = [
"ThirdPartyKey": "third-party value",
SystemProxyPropertyList.socksEnabled: 0,
]
let applied = SystemProxyPropertyList.applying(configuration, to: original)
XCTAssertEqual(applied["ThirdPartyKey"] as? String, "third-party value")
}
/// PAC 显式关闭时移除 URL 与 JavaScript 残值(issue #71 AC27)。
func testDisabledPACRemovesURLAndJavaScriptValues() {
let original: [String: Any] = [
SystemProxyPropertyList.pacEnabled: 1,
SystemProxyPropertyList.pacURL: "http://proxy.example/proxy.pac",
SystemProxyPropertyList.pacJavaScript: "function FindProxyForURL() {}",
]
let applied = SystemProxyPropertyList.applying(configuration, to: original)
XCTAssertEqual(applied[SystemProxyPropertyList.pacEnabled] as? Int, 0)
XCTAssertNil(applied[SystemProxyPropertyList.pacURL])
XCTAssertNil(applied[SystemProxyPropertyList.pacJavaScript])
}
// MARK: - Declarative planner
private func serialized(_ dictionary: [String: Any]) throws -> Data {
try PropertyListSerialization.data(fromPropertyList: dictionary, format: .binary, options: 0)
}
private func service(
_ id: String, location: String = "location-1", configuration data: Data?
) -> SystemProxyServiceState {
SystemProxyServiceState(
identifier: SystemProxyServiceIdentifier(locationID: location, serviceID: id),
configuration: data)
}
func testApplyPlannerWritesEveryActiveServiceToDesiredValues() throws {
let applied = try serialized(SystemProxyPropertyList.applying(configuration, to: [:]))
let foreign = try serialized(["HTTPEnable": 1, "HTTPProxy": "proxy.example"])
let plan = try SystemProxyPlanner.makeApplyPlan(
services: [
service("service-1", configuration: applied),
service("service-2", configuration: foreign),
], configuration: configuration)
XCTAssertEqual(plan.writes.map(\.identifier.serviceID), ["service-2"], "等值服务零写入")
XCTAssertEqual(plan.outcome, .written)
let projected = try XCTUnwrap(plan.writes.first?.configuration)
XCTAssertTrue(SystemProxyPlanner.equivalent(projected, applied))
}
func testApplyPlannerIsIdempotentWhenAllServicesMatch() throws {
let applied = try serialized(SystemProxyPropertyList.applying(configuration, to: [:]))
let plan = try SystemProxyPlanner.makeApplyPlan(
services: [service("service-1", configuration: applied)], configuration: configuration)
XCTAssertTrue(plan.writes.isEmpty)
XCTAssertEqual(plan.outcome, .unchanged, "重试不产生无谓的 SC 写入(issue #71 AC31)")
}
/// 清理计划无条件:凡持有 Proxies 实体的服务(跨位置、不校验端点或来源)
/// 都整字典移除(issue #71 AC6)。
func testCleanupPlannerClearsWholeDictionaryEverywhereUnconditionally() throws {
let ng2Values = try serialized(SystemProxyPropertyList.applying(configuration, to: [:]))
let foreignValues = try serialized([
"HTTPEnable": 1, "HTTPProxy": "proxy.example", "HTTPPort": 9090,
])
let services = [
service("same-id", location: "location-a", configuration: ng2Values),
service("same-id", location: "location-b", configuration: foreignValues),
service("bare-service", location: "location-b", configuration: nil),
]
let plan = SystemProxyPlanner.makeClearPlan(services: services)
XCTAssertEqual(
Set(plan.writes.map(\.identifier)),
[services[0].identifier, services[1].identifier],
"不校验来源:NG2 与其他应用写入的服务都被清空")
XCTAssertTrue(plan.writes.allSatisfy { $0.configuration == nil }, "清除完整 Proxies 字典")
XCTAssertFalse(
plan.writes.contains { $0.identifier == services[2].identifier },
"没有 Proxies 实体的服务无需写入")
}
// MARK: - 注册清单漂移指纹(issue #71 后续)
/// launchd 沿用注册提交时的 job 定义快照,清单变更须注销重注才生效;
/// 指纹检测决定是否重注。
func testRegistrationStampDriftDetection() {
let data = Data("plist-content".utf8)
XCTAssertTrue(
SystemProxyHelperRegistrationStamp.drifted(plistData: data, storedStamp: nil),
"无指纹(首次带检测代码启动)视为漂移")
XCTAssertTrue(
SystemProxyHelperRegistrationStamp.drifted(plistData: data, storedStamp: "outdated"),
"清单内容变更视为漂移")
XCTAssertFalse(
SystemProxyHelperRegistrationStamp.drifted(
plistData: data, storedStamp: SystemProxyHelperRegistrationStamp.stamp(data)),
"指纹一致不漂移")
XCTAssertFalse(
SystemProxyHelperRegistrationStamp.drifted(plistData: nil, storedStamp: nil),
"清单不可读无基准,不判漂移")
XCTAssertNotEqual(
SystemProxyHelperRegistrationStamp.stamp(Data("a".utf8)),
SystemProxyHelperRegistrationStamp.stamp(Data("b".utf8)),
"不同清单内容指纹不同")
}
}