mirror of
https://github.com/shadowsocks/ShadowsocksX-NG.git
synced 2026-11-27 16:50:37 +00:00
激活状态机与派生层(spec #21 D3 激活族 + D5 派生): - ActivationStateMachine:持久化活动目标(服务器或分组);激活原子成功 或点名失败(保留原目标与运行状态);目录提交后立即重展开,失效即清除 并发出停止意图(删除/禁用/变空/无效叶子各路径,无静默回退) - 组激活按显式子序递归展开有效启用叶子;组目标保持组 UUID - SslocalRuntimeDocument:完整 sslocal JSON 契约文档,凭据派生时解析, 插件字段无插件时整体省略;ManagedPluginProviding 缝按「未提供」参与 无效判定(真实受管集 #38 接通) - ActivationStateFileStore:activation.json 持久化(0700/0600、原子替换, 损坏按安全侧恢复为无活动目标);与 CatalogFileStore 共享 AtomicFileWriter
147 lines
4.9 KiB
Swift
147 lines
4.9 KiB
Swift
import XCTest
|
|
|
|
@testable import ShadowsocksX_NG2
|
|
|
|
/// 激活缝测试共享夹具(票 #26):受管插件/凭据存储替身、无插件叶子、
|
|
/// 激活与重展开的便捷调用、原子失败断言。
|
|
enum ActivationFixture {
|
|
/// 受管插件测试集:只提供表内程序,值即写入文档的 bundle 内路径。
|
|
struct TestManagedPlugins: ManagedPluginProviding {
|
|
let pathsByProgram: [String: String]
|
|
|
|
func executablePath(forProgram program: String) -> String? {
|
|
pathsByProgram[program]
|
|
}
|
|
}
|
|
|
|
/// 读取即失败的凭据存储(模拟 Keychain 读取错误)。
|
|
struct FailingCredentialStore: CredentialStoring {
|
|
func save(_ secret: String, for reference: CredentialReference) throws {}
|
|
func secret(for reference: CredentialReference) throws -> String? {
|
|
throw CredentialStoreError.keychainStatus(errSecAuthFailed)
|
|
}
|
|
func delete(_ reference: CredentialReference) throws {}
|
|
}
|
|
|
|
static let plugins = TestManagedPlugins(pathsByProgram: [
|
|
"v2ray-plugin": "/bundle/Helpers/Plugins/v2ray-plugin"
|
|
])
|
|
|
|
static let listen = SslocalListenSettings()
|
|
|
|
/// 无插件叶子字段(插件语义单独覆盖)。
|
|
static func plainFields(
|
|
remark: String, passwordRef: CredentialReference = .fresh()
|
|
) -> ServerFields {
|
|
ServerFields(
|
|
address: "203.0.113.7",
|
|
port: 8388,
|
|
encryptionMethod: "aes-256-gcm",
|
|
passwordRef: passwordRef,
|
|
remark: remark,
|
|
pluginProgram: nil,
|
|
pluginOptionsRef: nil
|
|
)
|
|
}
|
|
|
|
/// 加入无插件叶子并把其密码存入凭据存储,返回节点身份。
|
|
@discardableResult
|
|
static func addPlainServer(
|
|
_ remark: String,
|
|
to parent: NodeID? = nil,
|
|
in catalog: inout ConfigurationCatalog,
|
|
credentials: InMemoryCredentialStore
|
|
) throws -> NodeID {
|
|
let passwordRef = CredentialReference(rawValue: "ref-pw-\(remark)")
|
|
try credentials.save("pw-\(remark)", for: passwordRef)
|
|
return try catalog.addServer(plainFields(remark: remark, passwordRef: passwordRef), to: parent)
|
|
}
|
|
|
|
static func makeCredentials(
|
|
_ secrets: [CredentialReference: String]
|
|
) throws -> InMemoryCredentialStore {
|
|
let store = InMemoryCredentialStore()
|
|
for (reference, secret) in secrets {
|
|
try store.save(secret, for: reference)
|
|
}
|
|
return store
|
|
}
|
|
|
|
static func activate(
|
|
_ machine: inout ActivationStateMachine,
|
|
_ target: NodeID,
|
|
in catalog: ConfigurationCatalog,
|
|
credentials: CredentialStoring
|
|
) throws -> RuntimeConfiguration {
|
|
try machine.activate(
|
|
target, in: catalog, credentials: credentials, plugins: plugins, listen: listen)
|
|
}
|
|
|
|
static func commit(
|
|
_ machine: inout ActivationStateMachine,
|
|
_ catalog: ConfigurationCatalog,
|
|
credentials: CredentialStoring
|
|
) -> ActivationEffect? {
|
|
machine.catalogDidCommit(
|
|
catalog, credentials: credentials, plugins: plugins, listen: listen)
|
|
}
|
|
|
|
/// 断言激活以指定原因原子失败(错误类型与值都点名)。
|
|
static func assertThrows(
|
|
_ expected: ActivationFailure,
|
|
_ activation: () throws -> RuntimeConfiguration,
|
|
file: StaticString = #filePath,
|
|
line: UInt = #line
|
|
) {
|
|
do {
|
|
_ = try activation()
|
|
XCTFail("应被拒绝:\(expected)", file: file, line: line)
|
|
} catch let failure as ActivationFailure {
|
|
XCTAssertEqual(failure, expected, file: file, line: line)
|
|
} catch {
|
|
XCTFail("错误应为 ActivationFailure,实际 \(error)", file: file, line: line)
|
|
}
|
|
}
|
|
|
|
enum FixtureError: Error {
|
|
case unexpectedEffect
|
|
}
|
|
|
|
/// 解包「原子更新」效应;否则 XCTFail 并抛错终止用例。
|
|
static func requireDeployed(
|
|
_ effect: ActivationEffect?, file: StaticString = #filePath, line: UInt = #line
|
|
) throws -> RuntimeConfiguration {
|
|
guard case .deployed(let configuration)? = effect else {
|
|
XCTFail("应产出原子更新,实际 \(String(describing: effect))", file: file, line: line)
|
|
throw FixtureError.unexpectedEffect
|
|
}
|
|
return configuration
|
|
}
|
|
|
|
/// 解包「清除并停止」效应的点名原因;否则 XCTFail 并抛错终止用例。
|
|
static func requireCleared(
|
|
_ effect: ActivationEffect?, file: StaticString = #filePath, line: UInt = #line
|
|
) throws -> ActivationFailure {
|
|
guard case .clearedAndStopped(let failure)? = effect else {
|
|
XCTFail("应清除并停止,实际 \(String(describing: effect))", file: file, line: line)
|
|
throw FixtureError.unexpectedEffect
|
|
}
|
|
return failure
|
|
}
|
|
|
|
/// 取叶子当前的服务器字段(非叶子即夹具错误)。
|
|
static func serverFields(
|
|
of leaf: NodeID,
|
|
in catalog: ConfigurationCatalog,
|
|
file: StaticString = #filePath,
|
|
line: UInt = #line
|
|
) throws -> ServerFields {
|
|
let kind = try XCTUnwrap(catalog.entry(for: leaf)?.kind, file: file, line: line)
|
|
guard case .server(let fields) = kind else {
|
|
XCTFail("夹具应是服务器叶子", file: file, line: line)
|
|
throw FixtureError.unexpectedEffect
|
|
}
|
|
return fields
|
|
}
|
|
}
|