mirror of
https://github.com/shadowsocks/ShadowsocksX-NG.git
synced 2026-11-22 13:34:00 +00:00
Tests/ 根层原有 138 个平铺测试文件,无法从目录读出测试覆盖的功能领域。 按功能领域重组为 17 个目录(Catalog/Workflow 与 Rules/Custom 两处嵌套,分别 对应 App/Application/CatalogWorkflow 与 Domain/Rules/Custom):Architecture / Support / Agent / Activation / Catalog / Subscription / Rules / ProxyRuntime / ProxyControl / Runtime / Settings / SystemProxy / Plugin / Diagnostics / Credentials / Workspace / Servers;Smoke/ 保持独立 target 不动。138 个移动中 132 个是 100% 相似度的纯重命名,其余 6 个在重命名之上带路径反推改写(见下)。 本次不是纯搬移:测试代码比生产代码多一层位置耦合,有 7 处用固定层数的 deletingLastPathComponent() 从 #filePath 反推仓库路径(都假定文件就在 Tests/ 根层)。这 7 处改走新增的 Tests/Support/TestSourceTree.swift——从调用点沿父 目录上溯找到含 project.yml 的目录,因此不再依赖父目录层数,以后移动测试文件 也不会静默指错。 配套改动: - 新增 Tests 布局守卫 TestsLayoutTests,断言 Tests/ 根层无平铺 .swift。比 Domain 那条弱一档——根层的 Info.plist(info.path 生成物)与 Fixtures/RuleSnapshots 下的 JSON(folder resource)都必须保留,不能用 「树下只有 .swift」覆盖。 - project.yml:Smoke target 按文件引用的 Tests/AppArtifact.swift 改指 Tests/Support/;两处注释里写死的 Tests/SkeletonTests.swift 随移动更新。 excludes 的裸名 "Smoke" / "Fixtures" 仍匹配 Tests/ 直属子目录,无需改动。 - README 的 Tests 条目展开为目录说明,并修掉 Domain 条目里写死的 Tests/SkeletonTests.swift。 - 顺带删去 DomainLayoutTests 文档注释里「整理前 62 个文件」这类易漂移的数字。 验证:xcodegen generate 后单测 target 为 139 个源文件(全部非 Smoke 文件,0 个 Smoke 文件混入)且仍以 Fixtures/RuleSnapshots 为资源,Smoke target 为 8 个 Smoke 文件加 AppArtifact;依赖路径反推的 6 个测试类全绿(路径指错即失败); task format 未改写任何被移动文件;task lint 与整理前同为 21 条内容型 warning; build 0 error;task test 1025 用例全通过(整理前 1024,+1 为新守卫); TestsLayoutTests 经变异验证(根层投放探针即失败);packaging-gate ALL CHECKS PASSED。
283 lines
11 KiB
Swift
283 lines
11 KiB
Swift
import XCTest
|
|
|
|
@testable import ShadowsocksX_NG2
|
|
|
|
/// SIP-008 v1 + 私有扩展解析(issue #35 验收:解码/schema/重复 ID/环/记录校验
|
|
/// 失败逐项覆盖;扩展缺失/未知/无效回退标准列表)。
|
|
final class SubscriptionDocumentParserTests: XCTestCase {
|
|
private let subscriptionID = NodeID(rawValue: "sub-test")
|
|
|
|
// MARK: 文档夹具
|
|
|
|
private func flatDoc(serverCount: Int = 2) -> Data {
|
|
let servers = (1...serverCount).map { index in
|
|
"""
|
|
{"id": "00000000-0000-4000-8000-00000000000\(index)",
|
|
"remarks": "节点 \(index)",
|
|
"server": "203.0.113.\(index)", "server_port": \(8387 + index),
|
|
"password": "pw\(index)", "method": "aes-256-gcm"}
|
|
"""
|
|
}.joined(separator: ",")
|
|
return Data(
|
|
"""
|
|
{"version": 1, "servers": [\(servers)]}
|
|
""".utf8)
|
|
}
|
|
|
|
private func treeDoc(
|
|
schemaVersion: Int = 1,
|
|
serverRefIDs: [String] = ["aaaaaaaa-0000-4000-8000-00000000000a"],
|
|
groups: String? = nil,
|
|
rootGroupID: String = "root"
|
|
) -> Data {
|
|
let groupJSON: String
|
|
if let groups {
|
|
groupJSON = groups
|
|
} else {
|
|
let refs =
|
|
serverRefIDs
|
|
.map { #"{"type": "server", "id": "\#($0)"}"# }
|
|
.joined(separator: ",")
|
|
groupJSON = """
|
|
{"id": "root", "name": "Example subscription", "children": [
|
|
{"type": "group", "id": "jp"}\(refs.isEmpty ? "" : ", \(refs)")]},
|
|
{"id": "jp", "name": "Japan", "children": [{"type": "server", "id": "bbbbbbbb-0000-4000-8000-00000000000b"}]}
|
|
"""
|
|
}
|
|
return Data(
|
|
"""
|
|
{"version": 1,
|
|
"servers": [
|
|
{"id": "aaaaaaaa-0000-4000-8000-00000000000a", "server": "203.0.113.1", "server_port": 8388,
|
|
"password": "p1", "method": "aes-256-gcm"},
|
|
{"id": "bbbbbbbb-0000-4000-8000-00000000000b", "server": "203.0.113.2", "server_port": 8389,
|
|
"password": "p2", "method": "aes-256-gcm"}],
|
|
"x_shadowsocksx_ng": {"schema_version": \(schemaVersion), "root_group_id": "\(rootGroupID)",
|
|
"groups": [\(groupJSON)]}}
|
|
""".utf8)
|
|
}
|
|
|
|
func parse(_ data: Data) throws -> SubscriptionSnapshot {
|
|
try SubscriptionDocumentParser.parse(data, subscriptionID: subscriptionID)
|
|
}
|
|
|
|
private func serverIDs(_ group: SubscriptionSnapshot.Group) -> [String] {
|
|
group.children.compactMap {
|
|
if case .server(let leaf) = $0 { return leaf.id.rawValue }
|
|
return nil
|
|
}
|
|
}
|
|
|
|
private func serverRecords(_ group: SubscriptionSnapshot.Group) -> [RemoteServerRecord] {
|
|
group.children.compactMap {
|
|
if case .server(let leaf) = $0 { return leaf.record }
|
|
return nil
|
|
}
|
|
}
|
|
|
|
// MARK: 标准列表与扩展树
|
|
|
|
func testFlatDocumentParsesServersInArrayOrder() throws {
|
|
let snapshot = try parse(flatDoc())
|
|
|
|
XCTAssertEqual(
|
|
snapshot.root.children.filter { if case .group = $0 { return true } else { return false } }
|
|
.count, 0, "扁平回退无嵌套分组")
|
|
XCTAssertEqual(serverIDs(snapshot.root).count, 2)
|
|
XCTAssertTrue(serverIDs(snapshot.root).allSatisfy { $0.hasPrefix("sub-test:id:") })
|
|
let records = serverRecords(snapshot.root)
|
|
XCTAssertEqual(records[0].address, "203.0.113.1")
|
|
XCTAssertEqual(records[0].port, 8388)
|
|
XCTAssertEqual(records[0].remark, "节点 1")
|
|
XCTAssertEqual(records[1].password, "pw2")
|
|
}
|
|
|
|
func testExtensionTreeParsesNestedGroupsAndOrder() throws {
|
|
let snapshot = try parse(treeDoc())
|
|
|
|
XCTAssertEqual(snapshot.root.name, "Example subscription")
|
|
// 根分组子序 = [嵌套组 Japan, 服务器 a](远端交错顺序原样保留)。
|
|
XCTAssertEqual(snapshot.root.children.count, 2)
|
|
guard case .group(let japan) = snapshot.root.children[0] else {
|
|
return XCTFail("首子应为嵌套组")
|
|
}
|
|
guard case .server(let rootServer) = snapshot.root.children[1] else {
|
|
return XCTFail("次子应为服务器")
|
|
}
|
|
XCTAssertEqual(japan.name, "Japan")
|
|
XCTAssertEqual(rootServer.record.address, "203.0.113.1")
|
|
XCTAssertEqual(serverRecords(japan).map(\.address), ["203.0.113.2"])
|
|
// 分组身份按订阅作用域限定且跨刷新稳定。
|
|
XCTAssertTrue(japan.id.rawValue.hasPrefix("sub-test:g:jp"))
|
|
}
|
|
|
|
func testEmptyServersIsValidEmptySnapshot() throws {
|
|
let snapshot = try parse(Data(#"{"version": 1, "servers": []}"#.utf8))
|
|
|
|
XCTAssertEqual(snapshot.root.children.count, 0)
|
|
}
|
|
|
|
func testPluginFieldsCarriedVerbatim() throws {
|
|
let data = Data(
|
|
"""
|
|
{"version": 1, "servers": [
|
|
{"id": "aaaaaaaa-0000-4000-8000-00000000000a", "server": "203.0.113.1", "server_port": 443,
|
|
"password": "p", "method": "aes-256-gcm",
|
|
"plugin": "v2ray-plugin", "plugin_opts": "mode=websocket;host=example.com"}]}
|
|
""".utf8)
|
|
|
|
let snapshot = try parse(data)
|
|
|
|
let records = serverRecords(snapshot.root)
|
|
XCTAssertEqual(records[0].pluginProgram, "v2ray-plugin")
|
|
XCTAssertEqual(records[0].pluginOptions, "mode=websocket;host=example.com")
|
|
}
|
|
|
|
// MARK: 扩展缺失/未知/无效 → 回退扁平
|
|
|
|
func testMissingExtensionFallsBackToFlat() throws {
|
|
let snapshot = try parse(flatDoc())
|
|
|
|
XCTAssertEqual(snapshot.root.name, "", "扁平回退无根名(由调用方以 host 兜底)")
|
|
XCTAssertEqual(serverIDs(snapshot.root).count, 2)
|
|
}
|
|
|
|
func testUnknownSchemaVersionFallsBackToFlat() throws {
|
|
let snapshot = try parse(treeDoc(schemaVersion: 2))
|
|
|
|
XCTAssertEqual(snapshot.root.name, "")
|
|
XCTAssertEqual(serverIDs(snapshot.root).count, 2, "标准服务器不因扩展版本未知而丢失")
|
|
XCTAssertEqual(
|
|
snapshot.root.children.filter { if case .group = $0 { return true } else { return false } }
|
|
.count, 0)
|
|
}
|
|
|
|
func testExtensionNotAnObjectFallsBackToFlat() throws {
|
|
let data = Data(
|
|
"""
|
|
{"version": 1, "servers": [], "x_shadowsocksx_ng": "broken"}
|
|
""".utf8)
|
|
|
|
let snapshot = try parse(data)
|
|
|
|
XCTAssertEqual(serverIDs(snapshot.root).count, 0)
|
|
}
|
|
|
|
func testUnknownServerRefFallsBackToFlat() throws {
|
|
let snapshot = try parse(treeDoc(serverRefIDs: ["ghost"]))
|
|
|
|
XCTAssertEqual(snapshot.root.name, "")
|
|
XCTAssertEqual(serverIDs(snapshot.root).count, 2, "引用损坏时保留标准服务器")
|
|
}
|
|
|
|
func testCycleFallsBackToFlat() throws {
|
|
let groups = """
|
|
{"id": "root", "name": "R", "children": [{"type": "group", "id": "jp"}]},
|
|
{"id": "jp", "name": "J", "children": [{"type": "group", "id": "root"}]}
|
|
"""
|
|
let snapshot = try parse(treeDoc(groups: groups))
|
|
|
|
XCTAssertEqual(snapshot.root.name, "", "环使扩展无效 → 回退扁平")
|
|
XCTAssertEqual(serverIDs(snapshot.root).count, 2)
|
|
XCTAssertEqual(
|
|
snapshot.root.children.filter { if case .group = $0 { return true } else { return false } }
|
|
.count, 0)
|
|
}
|
|
|
|
func testSharedParentFallsBackToFlat() throws {
|
|
let groups = """
|
|
{"id": "root", "name": "R", "children": [
|
|
{"type": "group", "id": "x"}, {"type": "group", "id": "y"}]},
|
|
{"id": "x", "name": "X", "children": [{"type": "group", "id": "shared"}]},
|
|
{"id": "y", "name": "Y", "children": [{"type": "group", "id": "shared"}]},
|
|
{"id": "shared", "name": "S", "children": []}
|
|
"""
|
|
let snapshot = try parse(treeDoc(groups: groups))
|
|
|
|
XCTAssertEqual(snapshot.root.name, "", "同一分组被两个父引用 → 扩展无效")
|
|
}
|
|
|
|
func testDuplicateGroupIDFallsBackToFlat() throws {
|
|
let groups = """
|
|
{"id": "root", "name": "R", "children": []},
|
|
{"id": "root", "name": "R2", "children": []}
|
|
"""
|
|
let snapshot = try parse(treeDoc(groups: groups))
|
|
|
|
XCTAssertEqual(snapshot.root.name, "")
|
|
}
|
|
|
|
func testGroupIDCollidingWithServerIDFallsBackToFlat() throws {
|
|
let groups = """
|
|
{"id": "aaaaaaaa-0000-4000-8000-00000000000a", "name": "冒充服务器 ID", "children": []},
|
|
{"id": "root", "name": "R", "children": [{"type": "group", "id": "aaaaaaaa-0000-4000-8000-00000000000a"}]}
|
|
"""
|
|
let snapshot = try parse(treeDoc(groups: groups))
|
|
|
|
XCTAssertEqual(snapshot.root.name, "")
|
|
}
|
|
|
|
func testMissingRootGroupFallsBackToFlat() throws {
|
|
let snapshot = try parse(treeDoc(rootGroupID: "ghost"))
|
|
|
|
XCTAssertEqual(snapshot.root.name, "")
|
|
XCTAssertEqual(serverIDs(snapshot.root).count, 2)
|
|
}
|
|
|
|
func testUnreferencedServersAppendedToRootInArrayOrder() throws {
|
|
let groups = """
|
|
{"id": "root", "name": "R", "children": [{"type": "server", "id": "aaaaaaaa-0000-4000-8000-00000000000a"}]}
|
|
"""
|
|
let data = Data(
|
|
"""
|
|
{"version": 1,
|
|
"servers": [
|
|
{"id": "bbbbbbbb-0000-4000-8000-00000000000b", "server": "203.0.113.2", "server_port": 8389,
|
|
"password": "p2", "method": "m"},
|
|
{"id": "aaaaaaaa-0000-4000-8000-00000000000a", "server": "203.0.113.1", "server_port": 8388,
|
|
"password": "p1", "method": "m"}],
|
|
"x_shadowsocksx_ng": {"schema_version": 1, "root_group_id": "root",
|
|
"groups": [\(groups)]}}
|
|
""".utf8)
|
|
|
|
let snapshot = try parse(data)
|
|
|
|
XCTAssertEqual(serverIDs(snapshot.root).count, 2, "未引用服务器不丢弃")
|
|
XCTAssertEqual(serverRecords(snapshot.root)[0].address, "203.0.113.1", "树引用在前")
|
|
XCTAssertEqual(serverRecords(snapshot.root)[1].address, "203.0.113.2", "未引用按数组序追加")
|
|
}
|
|
|
|
// MARK: 身份延续规则(无稳定 ID 按地址端口精确匹配延续)
|
|
|
|
func testIdLessRecordIdentityIsStableForIdenticalContent() throws {
|
|
let record =
|
|
#"{"server": "203.0.113.1", "server_port": 8388, "password": "p", "method": "m"}"#
|
|
let first = try parse(Data("{\"version\": 1, \"servers\": [\(record)]}".utf8))
|
|
let second = try parse(Data("{\"version\": 1, \"servers\": [\(record)]}".utf8))
|
|
|
|
XCTAssertEqual(serverIDs(first.root)[0], serverIDs(second.root)[0])
|
|
}
|
|
|
|
func testIdLessRecordIdentitySurvivesRemoteRename() throws {
|
|
let base =
|
|
#"{"server": "203.0.113.1", "server_port": 8388, "password": "p", "method": "m", "remarks": "r"}"#
|
|
let original = try parse(Data("{\"version\": 1, \"servers\": [\(base)]}".utf8))
|
|
// 仅改 remarks(远端改名):显示元数据不参与端点指纹。
|
|
let renamed =
|
|
#"{"server": "203.0.113.1", "server_port": 8388, "password": "p", "method": "m", "remarks": "r2"}"#
|
|
let changed = try parse(Data("{\"version\": 1, \"servers\": [\(renamed)]}".utf8))
|
|
|
|
XCTAssertEqual(serverIDs(original.root)[0], serverIDs(changed.root)[0])
|
|
}
|
|
|
|
func testIdentityScopedPerSubscription() throws {
|
|
let data = flatDoc()
|
|
let other = try SubscriptionDocumentParser.parse(
|
|
data, subscriptionID: NodeID(rawValue: "sub-other"))
|
|
|
|
XCTAssertTrue(serverIDs(other.root).allSatisfy { $0.hasPrefix("sub-other:id:") })
|
|
let baseline = try parse(data)
|
|
XCTAssertNotEqual(serverIDs(other.root)[0], serverIDs(baseline.root)[0])
|
|
}
|
|
}
|