Files
ShadowsocksX-NG/ShadowsocksX-NG2/Tests/RuntimeLogRedactionTests.swift
T
Qiu Yuzhou a6825aaafa feat(ng2): diagnostics log viewer and redacted export (#34)
主窗口诊断区(spec #21 D5/D11):

- RuntimeLog 增加事件接收缝,GUI 侧接入 RuntimeEventStore 内存环形缓冲
  (容量 500),wrapper 侧不变(stderr → agent.log 收敛)
- 主窗口侧栏新增「服务器/诊断」分区切换;诊断区日志查看器双源:GUI 事件
  实时呈现 + agent.log 尾部(1s 轮询),均可复制
- DiagnosticReportBuilder 显式触发的脱敏诊断导出:只含状态、存在性、权限、
  大小/时间、数量与脱敏元数据;不读 Keychain 与契约/目录内容;原始错误
  detail 不入导出,家目录前缀改写为「~」;服务器地址与备注不进入任何输出
- 投毒夹具测试验证导出不含任何敏感类目(密码/插件参数/地址/备注/token/JSON)

Closes #34
2026-09-20 22:41:28 +08:00

90 lines
3.0 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
/// 日志脱敏基线(spec #21 D5):封闭事件枚举由构造保证不携带敏感值;脱敏
/// 工具按敏感信息定义收敛 URL 与运行时文档摘要。
final class RuntimeLogRedactionTests: XCTestCase {
/// 投毒夹具:敏感值埋进文档各字段,任何一条日志事件都不得带出。
private let poisoned = ProxyRuntimeFixture.makeDocument(
serverAddress: "203.0.113.7",
password: "SECRET-PASSWORD",
pluginOpts: "obfs=http;obfs-host=SECRET-HOST"
)
private let secrets = [
"SECRET-PASSWORD",
"SECRET-HOST",
"203.0.113.7",
"香港 01",
]
private func assertNoSecrets(
_ text: String, file: StaticString = #filePath, line: UInt = #line
) {
for secret in secrets {
XCTAssertFalse(
text.contains(secret),
"日志不得包含敏感值「\(secret)」\n实际:\(text)",
file: file, line: line)
}
XCTAssertFalse(text.contains("{"), "日志不得携带 JSON 文档内容", file: file, line: line)
}
func testDocumentSummaryExposesOnlyCountsProtocolsAndMode() {
let summary = Redactor.documentSummary(poisoned)
XCTAssertEqual(summary, "servers=1 protocols=socks mode=tcp_only")
assertNoSecrets(summary)
}
func testRemoteURLKeepsOnlySchemeAndHost() {
XCTAssertEqual(
Redactor.remoteURL("https://user:SECRET@example.com/sub/CLASH-SECRET?token=abc&x=1"),
"https://example.com/…",
"完整订阅 URL 与 URL token 永不入日志(D5)")
}
func testRemoteURLRedactsUnparseableAndEmptyValues() {
for bad in ["", "not a url", "https://", "ftp://"] {
XCTAssertEqual(
Redactor.remoteURL(bad), "<redacted-url>",
"无法安全归约的 URL 一律占位:\(bad)")
}
}
func testEveryRuntimeLogEventIsFreeOfSecretsAndJSONDumps() {
let events: [RuntimeLogEvent] = [
.contractWritten(serverCount: poisoned.servers.count),
.contractUnchanged,
.runtimeFilesDeleted,
.runtimePersistFailed(detail: "errno 13"),
.agentRegistered,
.agentRegisterFailed(detail: "SMAppService error 9"),
.agentUnregistered,
.agentUnregisterFailed(detail: "SMAppService error 9"),
.sslocalSpawned(pid: 42),
.sslocalSpawnFailed,
.sslocalExitedUnexpectedly(status: 78),
.sslocalStopRequested,
.pacStarted(port: 1089),
.pacStartFailed(port: 1089, detail: "Address already in use"),
.pacStopped,
.contractMissing,
.contractInvalidRemoved,
.reloadForwarded,
.reloadRestarted,
.endpointProbeFailed(host: "127.0.0.1", port: 1086, detail: "Connection refused"),
.activationFailed(
reason: ActivationFailure.invalidLeaf(
node: NodeID(rawValue: "leaf-1"),
reason: .pluginNotProvided(program: "simple-obfs")
).presentedReason),
.diagnosticsExported,
]
let joined = events.map(\.description).joined(separator: "\n")
assertNoSecrets(joined)
}
}