Files
ShadowsocksX-NG/ShadowsocksX-NG2/Tests/SystemProxyHelperXPCRoundTripTests.swift
T
Qiu Yuzhou 7d0ebc79b4 refactor(tests): drop TEST_HOST by linking tests against app-source static lib
单测不再以 app 可执行为宿主:新增 ShadowsocksX-NG2Core 静态库 target 同源
编译 App/(除 @main)与 Domain/,模块名沿用 ShadowsocksX_NG2 使全部测试的
@testable import 零改动;app target 模块改名 ShadowsocksX_NG2App 避免共享
Products 目录 swiftmodule 撞名。两个测试 target 改链 Core 后 xcodegen 不再
生成 TEST_HOST,xcodebuild test 只构建不启动 app——与安装版运行实例零接触
(pgrep 全程验证无 app 进程)。同源双编译沿用 Agent/Helper 共享 Domain 文件
的既定模式,Core 只被测试 target 依赖,发布链不编译它。

随之删除 MainApp.swift 的整套测试宿主脚手架(isUnitTesting/makeTesting/
Noop 服务/Ephemeral 存储,-142 行),app 在测试下不再启动,守卫失去存在
理由。宿主移除暴露三处 Bundle.main 依赖,改为显式缝:控制器新增 appBundle
注入(内置规则快照 BuiltinRuleCatalog 与 LaunchDaemon 清单指纹
launchDaemonPlistData(in:) 均按 bundle 读取,生产默认 .main 不变);XPC
listener delegate 期望客户端身份改为可注入(测试按本进程 SecCode 自查)。
测试经 AppArtifact.bundle(沿测试包落盘位置向上查找 app 产物,兼容
TEST_HOST 内嵌与平级两种布局)统一取产物。

665 单测 + 11 冒烟全绿,task build 生产构建不受影响。
2026-09-30 20:44:13 +08:00

88 lines
3.6 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 Foundation
import Security
import XCTest
@testable import ShadowsocksX_NG2
/// 真实 XPC 装配层的契约 round-trip(issue #71):匿名监听器挂真实 listener
/// delegate(注入假特权写入器,不触真实系统设置)+ listenerEndpoint 连接,覆盖
/// fakes 与纯逻辑单测覆盖不到的层:NSXPCInterface 方法签名、客户端签名
/// identifier 校验、Data payload 传输与 XPC 内部队列应答路径。去宿主化
/// (ADR 0021)后连接方是 xctest runner 本进程,delegate 期望身份按本进程
/// 实际签名 identifier 注入,无需 launchd。
/// 注意:NSXPCListener.delegate 是弱引用,delegate 必须由测试强持有。
final class SystemProxyHelperXPCRoundTripTests: XCTestCase {
func testTypedApplyAndClearRoundTripThroughRealXPCConnection() async throws {
let engine = SystemProxyHelperEngine(perform: { request in
switch request {
case .apply: return .applied(.written)
case .clear: return .cleared
}
})
let delegate = SystemProxyHelperListenerDelegate(
engine: engine,
expectedClientIdentifier: try XCTUnwrap(Self.currentProcessSigningIdentifier()))
let listener = NSXPCListener.anonymous()
listener.delegate = delegate
listener.resume()
defer { listener.invalidate() }
let connection = NSXPCConnection(listenerEndpoint: listener.endpoint)
connection.remoteObjectInterface = NSXPCInterface(with: SystemProxyHelperControlling.self)
connection.resume()
defer { connection.invalidate() }
let proxy = try XCTUnwrap(
connection.remoteObjectProxyWithErrorHandler { (error: Error) in
XCTFail("XPC 错误回调不应触发:\(error)")
} as? SystemProxyHelperControlling)
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"])
let payload = try SystemProxyHelperWire.encodeConfiguration(configuration)
let applyReplied = expectation(description: "apply reply")
var appliedResponse: SystemProxyHelperResponse?
proxy.apply(payload) { data in
do {
appliedResponse = try SystemProxyHelperWire.decode(data)
} catch {
XCTFail("apply 应答无法解码:\(error)")
}
applyReplied.fulfill()
}
await fulfillment(of: [applyReplied], timeout: 10)
XCTAssertEqual(appliedResponse, .applied(.written))
let clearReplied = expectation(description: "clear reply")
var clearedResponse: SystemProxyHelperResponse?
proxy.clear { data in
do {
clearedResponse = try SystemProxyHelperWire.decode(data)
} catch {
XCTFail("clear 应答无法解码:\(error)")
}
clearReplied.fulfill()
}
await fulfillment(of: [clearReplied], timeout: 10)
XCTAssertEqual(clearedResponse, .cleared)
}
/// 本进程的代码签名 identifier(与生产 SystemProxyClientValidator 同一
/// SecCode 查询路径,对自身用 SecCodeCopySelf 取 code)。
private static func currentProcessSigningIdentifier() -> String? {
var code: SecCode?
guard SecCodeCopySelf([], &code) == errSecSuccess, let code else { return nil }
var staticCode: SecStaticCode?
guard SecCodeCopyStaticCode(code, [], &staticCode) == errSecSuccess, let staticCode
else { return nil }
var information: CFDictionary?
guard SecCodeCopySigningInformation(staticCode, SecCSFlags(), &information) == errSecSuccess,
let information
else { return nil }
return (information as NSDictionary)[kSecCodeInfoIdentifier] as? String
}
}