Files
ShadowsocksX-NG/ShadowsocksX-NG2/Tests/ProxyRuntimeControllerModeTests.swift
T
Qiu Yuzhou 398e8d84b6 feat(ng2): split agent and system proxy switches
实现 #60(ACL 1/8):agent 意图与系统代理意图拆为两个独立开关,
首次运行默认 agent 开、系统代理关;显式关闭 agent 持久化并在 GUI
重启后生效;关闭系统代理只恢复 NG2 持有的系统设置,本地 SOCKS/HTTP
继续监听;关闭 agent 先恢复系统设置再停止监听。无活动目标时 agent
以空服务器列表契约提供本地监听(isWellFormed 放行空 servers,上游
sslocal v1.25.0 接受);需要代理出口的模式在活动目标缺失或端点不健
康时系统代理保持待应用,条件恢复后随下次收敛自动应用。首页、状态菜
单与侧栏状态卡经同一代理控制快照展示/操作 agent 意图/运行状态与系
统代理意图/实际应用及 typed 错误。测试覆盖启动顺序、持久化、健康门
禁、ownership 冲突、无效活动目标与 GUI 重同步(含真实 sslocal 空监
听回环证据);替身测试不表述为真实系统写入验证。
2026-09-26 00:38:28 +08:00

141 lines
5.6 KiB
Swift

import XCTest
@testable import ShadowsocksX_NG2
/// Mode restoration and commands share the Domain availability policy without
/// starting a real runtime or touching host proxy settings.
@MainActor
extension ProxyRuntimeControllerTests {
func testRuntimeFactsProjectControllerStateAtStableSeam() async {
let controller = makeController(probe: ProxyRuntimeFixture.FakeProbe.reachable())
XCTAssertEqual(
controller.runtimeFacts,
ProxyRuntimeFacts(status: .off, isOn: false))
await controller.resyncOnLaunch()
XCTAssertEqual(
controller.runtimeFacts,
ProxyRuntimeFacts(status: .running, isOn: true),
"默认 agent 意图开启:重同步后无目标也进入空列表监听")
}
}
/// Coordinator/controller integration for the shared committed catalog source.
@MainActor
final class CatalogRuntimeSnapshotIntegrationTests: XCTestCase {
private var runtime: ProxyRuntimeFixture.TemporaryRuntime!
private var catalogFileURL: URL!
private var activationFileURL: URL!
private var credentials: InMemoryCredentialStore!
private var agent: ProxyRuntimeFixture.FakeLaunchAgent!
private var systemProxy: ProxyRuntimeFixture.FakeSystemProxy!
override func setUpWithError() throws {
try super.setUpWithError()
runtime = ProxyRuntimeFixture.makeTemporaryRuntime()
catalogFileURL = runtime.directory.appendingPathComponent("catalog.json")
activationFileURL = runtime.directory.appendingPathComponent("activation.json")
credentials = InMemoryCredentialStore()
agent = ProxyRuntimeFixture.FakeLaunchAgent()
systemProxy = ProxyRuntimeFixture.FakeSystemProxy()
}
override func tearDownWithError() throws {
try? FileManager.default.removeItem(at: runtime.directory)
try super.tearDownWithError()
}
func testCommitWithoutActiveTargetFeedsLaterActivationThroughSharedSnapshot() async throws {
let fileStore = CatalogFileStore(fileURL: catalogFileURL)
let bootstrap = CatalogCommitCoordinator.bootstrap(fileStore: fileStore)
let controller = makeController(
catalogSnapshotReader: bootstrap.catalogSnapshotReader,
settings: ProxySettings(listen: ActivationFixture.listen, agentEnabled: false))
let coordinator = CatalogCommitCoordinator(
fileStore: fileStore,
runtime: ProxyRuntimeSyncAdapter(controller: controller),
bootstrap: bootstrap)
let server = try coordinator.commit { catalog, _ in
try ActivationFixture.addPlainServer(
"提交后激活", in: &catalog, credentials: credentials)
}
XCTAssertEqual(coordinator.syncStatus, .idle, "无目标时目录只发布,不触发运行时")
XCTAssertNil(controller.activeTargetID)
try await controller.activate(server)
XCTAssertEqual(controller.activeTargetID, server, "激活读取协调器刚发布的唯一快照")
XCTAssertEqual(controller.state, .off, "agent 意图关闭时该命令仅选择目标,不部署")
}
func testCoordinatorUsesProductionAdapterToClearRemovedActiveTarget() async throws {
let server = try makeSeededCatalog()
try ActivationStateFileStore(fileURL: activationFileURL).save(activeTargetID: server)
let fileStore = CatalogFileStore(fileURL: catalogFileURL)
let bootstrap = CatalogCommitCoordinator.bootstrap(fileStore: fileStore)
let controller = makeController(
catalogSnapshotReader: bootstrap.catalogSnapshotReader,
probe: ProxyRuntimeFixture.FakeProbe.reachable(),
pacProbe: ProxyRuntimeFixture.FakePACProbe())
let coordinator = CatalogCommitCoordinator(
fileStore: fileStore,
runtime: ProxyRuntimeSyncAdapter(controller: controller),
bootstrap: bootstrap)
try coordinator.commit { catalog, _ in try catalog.remove(server) }
await waitUntilRuntimeSettles(
{
if case .finished = coordinator.syncStatus { return true }
return false
}())
guard case .finished(_, .clearedAndStopped) = coordinator.syncStatus else {
return XCTFail("生产 adapter 应以刚提交快照清理无效目标")
}
XCTAssertNil(controller.activeTargetID)
XCTAssertNil(
try ActivationStateFileStore(fileURL: activationFileURL).loadActiveTargetID(),
"adapter 收敛后持久化清除失效目标")
}
private func makeController(
catalogSnapshotReader: RuntimeCatalogSnapshotReading,
settings: ProxySettings? = nil,
probe: EndpointProbing = SystemEndpointProbe(),
pacProbe: PACHealthProbing = SystemPACHealthProbe()
) -> ProxyRuntimeController {
ProxyRuntimeController(
catalogSnapshotReader: catalogSnapshotReader,
activationFileStore: ActivationStateFileStore(fileURL: activationFileURL),
runtimeFileStore: RuntimeFileStore(fileURL: runtime.contract),
credentials: credentials,
plugins: ActivationFixture.plugins,
listenRestore: RestoredListenSettings(
settings: ActivationFixture.listen, unreadableError: nil),
settingsStore: InMemoryProxySettingsStore(),
settingsRestore: RestoredProxySettings(
settings: settings ?? ProxySettings(listen: ActivationFixture.listen),
unreadableError: nil),
agent: agent,
probe: probe,
pacProbe: pacProbe,
systemProxy: systemProxy,
firewallExecutableURLs: [URL(fileURLWithPath: "/bundle/Helpers/sslocal")],
firewallPollIntervalNanoseconds: 1_000_000,
sendSignal: { _, _ in 0 })
}
private func makeSeededCatalog() throws -> NodeID {
var catalog = ConfigurationCatalog()
let server = try ActivationFixture.addPlainServer(
"待删除", in: &catalog, credentials: credentials)
try CatalogFileStore(fileURL: catalogFileURL).save(CatalogDocument(catalog: catalog))
return server
}
}