mirror of
https://github.com/shadowsocks/ShadowsocksX-NG.git
synced 2026-11-26 15:53:48 +00:00
Implements issue #71 with ADR-0020 (supersedes ADR-0019's endpoint-signature cleanup, startup cleanup, and health-gate cleanup). - New ShadowsocksX-NG2SystemProxyHelper target: app-bundled root LaunchDaemon (SMAppService.daemon, on-demand via MachServices) that mechanically applies a closed typed configuration or executes an unconditional clear. Clients are accepted by code-signing bundle identifier only; requests run serially on a shared queue (last-writer-wins). - SystemProxyConfiguration is now the closed typed apply payload (Codable over JSON XPC): explicit SOCKS/HTTP/HTTPS enable + endpoints, PAC and auto-discovery disabled with URL/JS removed, explicit simple-hostname exclusion, complete ExceptionsList. Unmodeled Proxies keys are preserved and equivalent values skip the commit. - Cleanup is a side effect of a persisted system-proxy intent on-to-off transition only: the switch itself, or the agent-off cascade (persist both intents off, clear, then stop listeners; failed cascade persistence records no transition and clears nothing). Launch with the switch off, health-gate closure, invalid targets, and passive events never clear. Failed clears are surfaced without deferred retry. - Endpoint-signature store, ownership migration, and direct Authorization Services writes are removed; old signature files stay inert. Helper unavailability or pending approval keeps an enabled intent pending and surfaces the Login Items approval path (HomeView button + menu label).
146 lines
5.9 KiB
Swift
146 lines
5.9 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 setUp() async throws {
|
|
try await super.setUp()
|
|
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 tearDown() async throws {
|
|
try? FileManager.default.removeItem(at: runtime.directory)
|
|
try await super.tearDown()
|
|
}
|
|
|
|
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())
|
|
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()
|
|
) -> ProxyRuntimeController {
|
|
let runtimeFileStore = RuntimeFileStore(fileURL: runtime.contract)
|
|
agent.onRegister = { [runtimeFileStore] in
|
|
guard let document = runtimeFileStore.loadDocument() else { return }
|
|
try? runtimeFileStore.writeRuntimeReceipt(for: document, processID: 42)
|
|
}
|
|
return ProxyRuntimeController(
|
|
catalogSnapshotReader: catalogSnapshotReader,
|
|
activationFileStore: ActivationStateFileStore(fileURL: activationFileURL),
|
|
runtimeFileStore: runtimeFileStore,
|
|
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,
|
|
systemProxy: systemProxy,
|
|
systemProxyHelper: ProxyRuntimeFixture.FakeSystemProxyHelperService(),
|
|
firewallExecutableURLs: [URL(fileURLWithPath: "/bundle/Helpers/sslocal")],
|
|
firewallPollIntervalNanoseconds: 1_000_000,
|
|
launchHealthTimeoutSeconds: 0.05,
|
|
sendSignal: { _, _ in 0 },
|
|
processIsAlive: { $0 == 42 })
|
|
}
|
|
|
|
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
|
|
}
|
|
}
|