mirror of
https://github.com/shadowsocks/ShadowsocksX-NG.git
synced 2026-11-27 08:13:46 +00:00
GUI → LaunchAgent → wrapper → sslocal full chain (spec #21 D2/D5): - ShadowsocksX-NG2Agent: Swift BundleProgram tool target embedded at Contents/MacOS, supervised by launchd via SMAppService agent plist (Contents/Library/LaunchAgents, bundle-relative ProgramArguments, KeepAlive={SuccessfulExit:false}). Wrapper reads the cross-process contract v2/sslocal-active.json, spawns sslocal -c <absolute path> and supervises it: SIGTERM chain stop, SIGUSR1 forward for server-only changes vs graceful restart on listen fingerprint changes, non-zero exit on unexpected child loss for KeepAlive replay, invalid/missing contract → clean exit with plain unlink. - Runtime file protocol: RuntimeFileStore reuses AtomicFileWriter (0700 dir self-heal, 0600 temp at creation, atomic replace); explicit-stop cleanup unlinks contract, agent pid file and stale temporaries. Shared read-side validation via SslocalRuntimeDocument.decodeValidated so GUI and wrapper cannot drift. Wrapper pid file bridges SMAppService's missing pid. - GUI: ProxyRuntimeController consumes ActivationStateMachine effects through pure ProxyRuntimePlan decision table (ordered actions, idempotent skip of identical contract); proxy toggle and launch failure presentation (naming endpoint:port) in the status menu; launch resync reconciles agent registration, wrapper liveness and on-disk contract; catalog commits re-expand immediately. - Log sanitization baseline: closed RuntimeLogEvent enum plus Redactor, wrapper/sslocal output converged to a 0600 agent.log with RUST_LOG defaulting to warn. Tests (126 total, all green): stub-sslocal driven wrapper lifecycle (start/stop order, SIGTERM chain, crash replay, cleanup, reload protocol), real sslocal smoke (SOCKS port binding + full SOCKS5 handshake), plan decision table, controller flows with fakes, runtime file permissions/atomic replace, endpoint probe, redaction. Manual release-gate items (real launchd/SMAppService): agent registration approval flow, GUI-quit persistence, bootout behaviors. Closes #27
120 lines
3.5 KiB
Swift
120 lines
3.5 KiB
Swift
import XCTest
|
|
|
|
@testable import ShadowsocksX_NG2
|
|
|
|
/// 代理运行时测试共享夹具(票 #27):临时 v2 目录、契约文档构造、LaunchAgent
|
|
/// 与探测替身。
|
|
enum ProxyRuntimeFixture {
|
|
/// 在临时目录中建一套隔离的运行时文件组(不触碰真实 ~/Library)。
|
|
struct TemporaryV2 {
|
|
let directory: URL
|
|
let contract: URL
|
|
let pidFile: URL
|
|
}
|
|
|
|
static func makeTemporaryV2(file: StaticString = #filePath, line: UInt = #line)
|
|
-> TemporaryV2
|
|
{
|
|
let directory = FileManager.default.temporaryDirectory
|
|
.appendingPathComponent("ssxng-tests-\(UUID().uuidString)", isDirectory: true)
|
|
try? FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true)
|
|
return TemporaryV2(
|
|
directory: directory,
|
|
contract: directory.appendingPathComponent("sslocal-active.json"),
|
|
pidFile: directory.appendingPathComponent("agent.pid"))
|
|
}
|
|
|
|
static func makeDocument(
|
|
serverAddress: String = "203.0.113.7",
|
|
password: String = "resolved-password",
|
|
pluginOpts: String? = nil,
|
|
localAddress: String = "127.0.0.1",
|
|
localPort: Int = 1086,
|
|
inboundProtocol: String = "socks",
|
|
mode: String = "tcp_only"
|
|
) -> SslocalRuntimeDocument {
|
|
SslocalRuntimeDocument(
|
|
servers: [
|
|
SslocalServerDocument(
|
|
id: "server-1",
|
|
remarks: "香港 01",
|
|
server: serverAddress,
|
|
serverPort: 8388,
|
|
password: password,
|
|
method: "aes-256-gcm",
|
|
plugin: nil,
|
|
pluginOpts: pluginOpts
|
|
)
|
|
],
|
|
localAddress: localAddress,
|
|
localPort: localPort,
|
|
inboundProtocol: inboundProtocol,
|
|
mode: mode
|
|
)
|
|
}
|
|
|
|
/// LaunchAgent 注册态可编程替身,记录全部调用。
|
|
final class FakeLaunchAgent: LaunchAgentControlling {
|
|
private(set) var currentStatus: LaunchAgentStatus
|
|
private(set) var registerCount = 0
|
|
private(set) var unregisterCount = 0
|
|
var registerError: Error?
|
|
/// register() 成功后自动迁移到的状态(模拟 launchd 拉起)。
|
|
var statusAfterRegister: LaunchAgentStatus = .registered
|
|
|
|
init(initialStatus: LaunchAgentStatus = .notRegistered) {
|
|
currentStatus = initialStatus
|
|
}
|
|
|
|
var status: LaunchAgentStatus { currentStatus }
|
|
|
|
func setStatus(_ status: LaunchAgentStatus) {
|
|
currentStatus = status
|
|
}
|
|
|
|
func register() throws {
|
|
registerCount += 1
|
|
if let registerError {
|
|
currentStatus = .registered
|
|
throw registerError
|
|
}
|
|
currentStatus = statusAfterRegister
|
|
}
|
|
|
|
func unregister() throws {
|
|
unregisterCount += 1
|
|
currentStatus = .notRegistered
|
|
}
|
|
}
|
|
|
|
/// 探测替身:按预设序列返回,序列耗尽后停留在最后一项。
|
|
final class FakeProbe: EndpointProbing {
|
|
private let lock = NSLock()
|
|
private var outcomes: [EndpointHealthProbe.Outcome]
|
|
private(set) var callCount = 0
|
|
|
|
init(outcomes: [EndpointHealthProbe.Outcome]) {
|
|
self.outcomes = outcomes
|
|
}
|
|
|
|
static func reachable() -> FakeProbe {
|
|
FakeProbe(outcomes: [.reachable])
|
|
}
|
|
|
|
static func refusing() -> FakeProbe {
|
|
FakeProbe(outcomes: [.refused(detail: "Connection refused")])
|
|
}
|
|
|
|
func probe(host: String, port: Int, timeout: TimeInterval) -> EndpointHealthProbe.Outcome {
|
|
lock.lock()
|
|
defer { lock.unlock() }
|
|
callCount += 1
|
|
guard let outcome = outcomes.first else { return .timedOut }
|
|
if outcomes.count > 1 {
|
|
outcomes.removeFirst()
|
|
}
|
|
return outcome
|
|
}
|
|
}
|
|
}
|