mirror of
https://github.com/shadowsocks/ShadowsocksX-NG.git
synced 2026-11-22 13:34:00 +00:00
Every catalog commit re-expanded the active target and ran the full deploy cycle unconditionally: the status face flashed back to starting, the launch health gate re-probed both local inbounds, and system proxy converged again — even when the derived contract was byte-identical to what was already deployed. Remark edits went further: remarks were part of SslocalServerDocument, so renaming the active server rewrote the contract and signalled sslocal for a change with no runtime effect. The reload predicate is now contract byte equality, shared by both layers (CONTEXT.md: a committed edit that leaves the runtime configuration file's bytes unchanged triggers no runtime convergence work). remarks leave the contract — upstream treats them as optional metadata and the GUI resolves display names from the catalog — so remark-only edits and no-op re-saves derive byte-identical documents. The catalogDidCommit deploy path skips write, signal, health gate and system proxy converge when ProxyRuntimePlan.actions returns empty (registered agent, live wrapper, on-disk contract equals the derived document); disk drift or a dead wrapper keeps the self-healing deploy. Catalog revalidation (candidate skips, target invalidation) still runs on every commit, and explicit intents (activation, switches, settings, launch resync) are untouched. The remark-edit runtime test — which encoded the old behavior as the example of a value change — is replaced by four catalog-convergence cases: remark-only edit and outside-subtree edit stay untouched, group member and active server value edits rewrite and signal. Also repairs the smoke suite, which failed on HEAD before this change: its ACL documents used a sslocal-active.acl path while RuntimeFileStore.write validates against acl-active.ini, so every ACL-bearing case threw before spawning the wrapper. Smoke ACL paths now bind to the store-validated location, and the suite passes 11/11 against real sslocal, confirming upstream accepts the contract without remarks.
138 lines
7.1 KiB
Swift
138 lines
7.1 KiB
Swift
import XCTest
|
||
|
||
@testable import ShadowsocksX_NG2
|
||
|
||
/// 目录提交 → 运行时收敛语义(D3/D5 与 CONTEXT.md「有效值未变的提交不做
|
||
/// 运行时收敛」):与主测试类同构扩展,按既有模式独立成文件。
|
||
extension ProxyRuntimeControllerTests {
|
||
// MARK: 目录重展开消费(D3/D5)
|
||
|
||
/// 模拟 wrapper 在跑(pid 指向本测试进程,kill(pid, 0) 判活通过)。
|
||
private func simulateRunningWrapper() throws {
|
||
try Data("\(ProcessInfo.processInfo.processIdentifier)".utf8).write(to: runtime.pidFile)
|
||
}
|
||
|
||
private func committedCatalog() throws -> ConfigurationCatalog {
|
||
try CatalogFileStore(fileURL: catalogFileURL).load().catalog
|
||
}
|
||
|
||
private func replacedFields(
|
||
_ fields: ServerFields, address: String? = nil, remark: String? = nil
|
||
) -> ServerFields {
|
||
ServerFields(
|
||
address: address ?? fields.address,
|
||
port: fields.port,
|
||
encryptionMethod: fields.encryptionMethod,
|
||
passwordRef: fields.passwordRef,
|
||
remark: remark ?? fields.remark,
|
||
pluginProgram: fields.pluginProgram,
|
||
pluginOptionsRef: fields.pluginOptionsRef)
|
||
}
|
||
|
||
func testCatalogValueEditOfActiveTargetRewritesAndSignalsRunningWrapper() async throws {
|
||
let seeded = try makeSeededCatalog()
|
||
let controller = makeController(probe: ProxyRuntimeFixture.FakeProbe.reachable())
|
||
try await controller.activate(seeded.server)
|
||
await controller.setAgentEnabled(true)
|
||
try simulateRunningWrapper()
|
||
|
||
// 修改激活服务器的连接地址(契约有效值变化),提交目录变更。
|
||
var catalog = try CatalogFileStore(fileURL: catalogFileURL).load().catalog
|
||
let fields = try ActivationFixture.serverFields(of: seeded.server, in: catalog)
|
||
try catalog.updateServer(
|
||
seeded.server, with: replacedFields(fields, address: "203.0.113.9"))
|
||
try CatalogFileStore(fileURL: catalogFileURL).save(CatalogDocument(catalog: catalog))
|
||
|
||
// 协调器生产适配入口(issue #40):以刚提交的内存快照重展开。
|
||
_ = await controller.catalogDidCommit(snapshot: try committedCatalog())
|
||
|
||
XCTAssertEqual(controller.state, .running)
|
||
let onDisk = try XCTUnwrap(RuntimeFileStore(fileURL: runtime.contract).loadDocument())
|
||
XCTAssertEqual(onDisk.servers.first?.server, "203.0.113.9", "运行时文档已原子更新")
|
||
let signalsSent = signals.signalsSent.filter { $0.signal != 0 }
|
||
XCTAssertEqual(signalsSent.count, 1, "代理运行中:重写后应发一次 SIGUSR1")
|
||
XCTAssertEqual(signalsSent.first?.signal, SIGUSR1)
|
||
}
|
||
|
||
func testCatalogRemarkOnlyEditOfActiveTargetKeepsRuntimeUntouched() async throws {
|
||
let seeded = try makeSeededCatalog()
|
||
let probe = ProxyRuntimeFixture.FakeProbe.reachable()
|
||
let controller = makeController(probe: probe)
|
||
try await controller.activate(seeded.server)
|
||
await controller.setAgentEnabled(true)
|
||
try simulateRunningWrapper()
|
||
let contractDataBefore = try Data(contentsOf: runtime.contract)
|
||
|
||
// 修改激活服务器的备注:显示名是目录元数据,不进契约 → 零收敛
|
||
// (不写盘、不发信号、不闪 starting、不重走健康门)。
|
||
var catalog = try CatalogFileStore(fileURL: catalogFileURL).load().catalog
|
||
let fields = try ActivationFixture.serverFields(of: seeded.server, in: catalog)
|
||
try catalog.updateServer(
|
||
seeded.server, with: replacedFields(fields, remark: "新加坡 01"))
|
||
try CatalogFileStore(fileURL: catalogFileURL).save(CatalogDocument(catalog: catalog))
|
||
|
||
let outcome = await controller.catalogDidCommit(snapshot: try committedCatalog())
|
||
|
||
XCTAssertEqual(outcome, .converged(skippedServers: []))
|
||
XCTAssertEqual(controller.state, .running, "状态面原地不动,不闪回 starting")
|
||
XCTAssertEqual(try Data(contentsOf: runtime.contract), contractDataBefore, "契约逐字节未变")
|
||
XCTAssertEqual(
|
||
signals.signalsSent.filter { $0.signal != 0 }.count, 0, "不向 wrapper 发信号")
|
||
XCTAssertEqual(probe.callCount, 2, "不重走健康门(初始部署探测过 SOCKS+HTTP 各一次)")
|
||
}
|
||
|
||
func testCatalogValueEditOutsideActiveSubtreeKeepsRuntimeUntouched() async throws {
|
||
var catalog = ConfigurationCatalog()
|
||
let activeServer = try ActivationFixture.addPlainServer(
|
||
"香港 01", in: &catalog, credentials: credentials)
|
||
let idleServer = try ActivationFixture.addPlainServer(
|
||
"备用 02", in: &catalog, credentials: credentials)
|
||
try CatalogFileStore(fileURL: catalogFileURL).save(CatalogDocument(catalog: catalog))
|
||
let probe = ProxyRuntimeFixture.FakeProbe.reachable()
|
||
let controller = makeController(probe: probe)
|
||
try await controller.activate(activeServer)
|
||
await controller.setAgentEnabled(true)
|
||
try simulateRunningWrapper()
|
||
let contractDataBefore = try Data(contentsOf: runtime.contract)
|
||
|
||
// 修改目标子树之外服务器的连接地址:派生契约不变 → 零收敛。
|
||
var working = try CatalogFileStore(fileURL: catalogFileURL).load().catalog
|
||
let fields = try ActivationFixture.serverFields(of: idleServer, in: working)
|
||
try working.updateServer(idleServer, with: replacedFields(fields, address: "198.51.100.9"))
|
||
try CatalogFileStore(fileURL: catalogFileURL).save(CatalogDocument(catalog: working))
|
||
|
||
let outcome = await controller.catalogDidCommit(snapshot: try committedCatalog())
|
||
|
||
XCTAssertEqual(outcome, .converged(skippedServers: []))
|
||
XCTAssertEqual(controller.state, .running, "状态面原地不动,不闪回 starting")
|
||
XCTAssertEqual(try Data(contentsOf: runtime.contract), contractDataBefore, "契约逐字节未变")
|
||
XCTAssertEqual(signals.signalsSent.filter { $0.signal != 0 }.count, 0, "不向 wrapper 发信号")
|
||
XCTAssertEqual(probe.callCount, 2, "不重走健康门")
|
||
}
|
||
|
||
func testCatalogGroupMemberValueEditRewritesAndSignalsRunningWrapper() async throws {
|
||
var catalog = ConfigurationCatalog()
|
||
let group = try catalog.addGroup("分组")
|
||
let member = try ActivationFixture.addPlainServer(
|
||
"香港 01", to: group, in: &catalog, credentials: credentials)
|
||
try CatalogFileStore(fileURL: catalogFileURL).save(CatalogDocument(catalog: catalog))
|
||
let controller = makeController(probe: ProxyRuntimeFixture.FakeProbe.reachable())
|
||
try await controller.activate(group)
|
||
await controller.setAgentEnabled(true)
|
||
try simulateRunningWrapper()
|
||
|
||
// 激活目标是分组:成员连接值变化是活动子树的真实值变化 → 正常收敛。
|
||
var working = try CatalogFileStore(fileURL: catalogFileURL).load().catalog
|
||
let fields = try ActivationFixture.serverFields(of: member, in: working)
|
||
try working.updateServer(member, with: replacedFields(fields, address: "203.0.113.9"))
|
||
try CatalogFileStore(fileURL: catalogFileURL).save(CatalogDocument(catalog: working))
|
||
|
||
_ = await controller.catalogDidCommit(snapshot: try committedCatalog())
|
||
|
||
XCTAssertEqual(controller.state, .running)
|
||
let onDisk = try XCTUnwrap(RuntimeFileStore(fileURL: runtime.contract).loadDocument())
|
||
XCTAssertEqual(onDisk.servers.first?.server, "203.0.113.9", "成员值变化进入运行时文档")
|
||
XCTAssertEqual(signals.signalsSent.filter { $0.signal != 0 }.count, 1, "转发一次 SIGUSR1")
|
||
}
|
||
}
|