Files
ShadowsocksX-NG/ShadowsocksX-NG2/Tests/BundleManagedPluginProviderTests.swift
T
Qiu Yuzhou 5e13c1716f feat(ng2): managed plugins end to end (#38)
- 受管插件静态事实表(v2ray-plugin v1.3.2,来源/许可证/版本/重签)与
  BundleManagedPluginProvider:受管集内且 bundle 内可执行文件在位才返回
  绝对路径;与 Vendor 清单双向锚定(D6/D10)
- 服务器编辑器插件区:受管选择器(无 + 受管列表),选中受管项才显示
  供应链事实与参数输入;集外引用显式「本版本未提供」并原样保留
- updateServer 插件落盘语义:「无」整体清除、参数写删钥匙串、集外不漂移
- wrapper 监听建立判定:3 秒未建立本地监听记 error 日志,监管继续(D10)
- 诊断导出新增受管插件清单(名称/版本/提供状态,无参数)
- 功能级兼容实测记录(docs/research/plugin-compat-record.md)与端到端
  冒烟:契约插件路径 → sslocal 拉起 → v2ray-plugin 进程建立 → 停止链
2026-09-21 10:38:30 +08:00

116 lines
5.1 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import XCTest
@testable import ShadowsocksX_NG2
/// 受管插件提供(issue #38,D10):受管集内且 bundle 内可执行文件在位才返回
/// 绝对路径;集外引用与文件缺失都归一为「本版本未提供」(nil → 状态机点名
/// 拒绝)。路径只从静态事实表派生,绝不拼接引用原文。
final class BundleManagedPluginProviderTests: XCTestCase {
private var bundleRoot: URL!
private var credentials: InMemoryCredentialStore!
override func setUpWithError() throws {
try super.setUpWithError()
bundleRoot = FileManager.default.temporaryDirectory
.appendingPathComponent("ssxng-plugin-tests-\(UUID().uuidString)", isDirectory: true)
try FileManager.default.createDirectory(
at: bundleRoot.appendingPathComponent("Contents/Helpers/Plugins"),
withIntermediateDirectories: true)
credentials = InMemoryCredentialStore()
}
override func tearDownWithError() throws {
try? FileManager.default.removeItem(at: bundleRoot)
try super.tearDownWithError()
}
private var pluginBinaryName: String { ManagedPluginCatalog.plugins[0].program }
private var pluginURL: URL {
bundleRoot.appendingPathComponent("Contents/Helpers/Plugins/\(pluginBinaryName)")
}
private func writePluginBinary(permissions: Int) throws {
try Data("#!/bin/sh\n".utf8).write(to: pluginURL)
try FileManager.default.setAttributes(
[.posixPermissions: permissions], ofItemAtPath: pluginURL.path)
}
private func makeProvider() -> BundleManagedPluginProvider {
BundleManagedPluginProvider(bundleURL: bundleRoot)
}
func testReturnsBundleAbsolutePathForProvidedManagedProgram() throws {
try writePluginBinary(permissions: 0o755)
let path = try XCTUnwrap(makeProvider().executablePath(forProgram: pluginBinaryName))
XCTAssertEqual(path, pluginURL.path, "配置生成的 plugin 字段是 bundle 内绝对路径")
XCTAssertTrue(path.hasPrefix(bundleRoot.path))
}
func testMissingBinaryIsNotProvided() {
XCTAssertNil(makeProvider().executablePath(forProgram: pluginBinaryName))
}
func testNonExecutableBinaryIsNotProvided() throws {
try writePluginBinary(permissions: 0o644)
XCTAssertNil(makeProvider().executablePath(forProgram: pluginBinaryName))
}
func testUnmanagedProgramIsNeverProvided() throws {
try writePluginBinary(permissions: 0o755)
let provider = makeProvider()
XCTAssertNil(provider.executablePath(forProgram: "obfs-local"), "集外程序名一律未提供")
XCTAssertNil(provider.executablePath(forProgram: "sslocal"), "非插件的 bundle 内二进制不算插件")
XCTAssertNil(provider.executablePath(forProgram: "../Helpers/sslocal"), "路径形态引用不拼接")
XCTAssertNil(provider.executablePath(forProgram: "v2ray-plugin/child"), "子路径引用不拼接")
XCTAssertNil(provider.executablePath(forProgram: ""))
}
/// 组合根事实:宿主 app bundle(Bundle.main,生产默认 provider 的取值来源)
/// 内受管插件在位——防止打包回归静默断开激活链路(编辑器显示提供而激活拒绝)。
func testHostAppBundleProvidesManagedPlugin() throws {
let path = try XCTUnwrap(
BundleManagedPluginProvider().executablePath(forProgram: pluginBinaryName),
"宿主 app bundle 未嵌入受管插件(先跑 fetch-external-binaries.sh)")
XCTAssertTrue(path.hasSuffix("Contents/Helpers/Plugins/\(pluginBinaryName)"))
}
/// 与状态机缝的契约:文件缺失 → provider nil → 激活以 pluginNotProvided
/// 原子拒绝(app 降级丢插件时同一语义)。
func testActivationRejectsMissingPluginByNamedReason() throws {
var catalog = ConfigurationCatalog()
var fields = ActivationFixture.plainFields(remark: "带插件", passwordRef: .fresh())
fields.pluginProgram = pluginBinaryName
try credentials.save("pw", for: fields.passwordRef)
let id = try catalog.addServer(fields)
var machine = ActivationStateMachine()
ActivationFixture.assertThrows(
.invalidLeaf(node: id, reason: .pluginNotProvided(program: pluginBinaryName))
) {
try machine.activate(
id, in: catalog, credentials: credentials, plugins: makeProvider(),
listen: ActivationFixture.listen)
}
}
/// 在位 → 派生文档携带 bundle 内绝对路径(与 opts 解析),激活成功。
func testActivationDerivesBundlePathWhenProvided() throws {
try writePluginBinary(permissions: 0o755)
var catalog = ConfigurationCatalog()
var fields = ActivationFixture.plainFields(remark: "带插件", passwordRef: .fresh())
fields.pluginProgram = pluginBinaryName
try credentials.save("pw", for: fields.passwordRef)
let id = try catalog.addServer(fields)
var machine = ActivationStateMachine()
let configuration = try machine.activate(
id, in: catalog, credentials: credentials, plugins: makeProvider(),
listen: ActivationFixture.listen)
let server = try XCTUnwrap(configuration.document.servers.first)
XCTAssertEqual(server.plugin, pluginURL.path)
XCTAssertNil(server.pluginOpts, "无参数引用时整体省略 pluginOpts")
}
}