mirror of
https://github.com/shadow1ng/fscan.git
synced 2026-09-22 03:10:42 +08:00
* add CVE-2026-24061 detect logic (#562) * add CVE-2026-24061 detect logic * fix(telnet): 修复 errcheck 警告,统一错误处理风格 --------- Co-authored-by: ZacharyZcR <[email protected]> * fix: 修复 Hub 广播 data race 和端口扫描潜在死锁,清理死代码 - hub.go: broadcast 路径 RLock 改 Lock,修复并发 delete/close 竞争 - port_scan.go: pool.Invoke 失败时释放 wg 和 semaphore,防止死锁 - web_scanner.go: 删除只写不读的 fingerprintCache - webtitle.go: 移除对已删除 SetFingerprints 的调用 - keylogger.go: 删除未使用的 stopChan 和 isRunning 字段 * refactor: context 穿透扫描生命周期,修复长驻插件阻塞和 Web Stop 无效 - RunScan 接受 context.Context,创建可取消上下文并穿透到所有策略和插件 - 长驻插件(forwardshell/socks5proxy/reverseshell)不再进入 scan WaitGroup, 通过 ctx.Done() 管理生命周期,解除 wg.Wait() 死锁 - Web Stop API 从 stopChan 改为 context.CancelFunc,取消信号真正传播到扫描链路 - ExecuteScanTasks 和 executeScanTask 支持 context 取消检查,停止分发新任务 - CLI 模式传 context.Background(),行为完全不变 * fix: 修复 Web Stop 信号等待阻塞和 SMB 响应解析越界 panic - scanner.go: 长驻插件等待信号时同时监听 ctx.Done(),Web Stop 可正常返回 - smb_protocol.go: 响应长度检查修正为 47,远端偏移量全部做边界校验 * fix: POC 扫描接入调用方 context,修复 cachedPocPath 竞争和 ProxyStats data race - webscan/web_scan.go: WebScan 接受 ctx 参数,替换 context.Background(); sync.Once 改为 sync.Mutex 保护 POC 加载,消除 cachedPocPath 并发写竞争 - webtitle.go: ctx 从 Scan 穿透到 identifyFingerprintsMulti → triggerPocScan → WebScan - webpoc.go: 传递 ctx 到 WebScan - proxy/types.go: ProxyStats 增加 sync.Mutex - proxy/manager.go: LastConnectTime/LastError/AverageConnectTime 读写加锁 * fix: 修复 ProxyStats 含 mutex 导致的 copylocks 告警 Stats() 方法改为手动构造副本,避免值拷贝复制 sync.Mutex * fix: 补全 HTTP/TLS proxy stats 加锁,修复 RPC/SMB 解析越界和 POC 加载逻辑 - httpdialer.go/tlsdialer.go: LastError/LastConnectTime/AverageConnectTime 加 mutex - findnet.go: RPC 响应结束标记位置 < 4 时跳过截断,防止负数切片 panic - ms17010.go: SMB 会话响应最小长度改为 45,sessionSetupResponse 加长度校验 - web_scan.go: POC 加载失败时不标记 pocLoaded,允许后续重试 - Eval.go: DNSLog 配置去掉 sync.Once,允许多次扫描更新配置 * fix: Web 全局状态同步、字典文件错误提示、长驻插件连接可取消 - scan.go: Web API 构建 config/state 后同步到全局实例 - config_builder.go: 用户名/密码/URL 文件读取失败时输出错误日志 - reverseshell.go: 读命令设 1s 超时,超时后检查 ctx 实现可取消 - forwardshell.go: handleClient 接受 ctx,取消时关闭连接解除阻塞 - socks5proxy.go: handleClient 接受 ctx,取消时关闭连接解除 IO 阻塞 * refactor: 引入 ScanSession,替代全局状态穿透扫描管道 (Phase 1-3) - 新增 common/session.go: ScanSession 结构体封装 Config/State/Params/Dialer - RunScan/Strategy/ExecuteScanTasks/executeScanTask 全部接收 session - Plugin 接口从 Scan(ctx, info, config, state) 改为 Scan(ctx, info, session) - 48 个插件实现统一更新签名 - Web API 构建 ScanSession 传给 RunScan - CLI 模式通过 Initialize() 创建 session * refactor: 全量替换 WrapperTcpWithTimeout 为 session.DialTCP (Phase 4) - core/port_scan.go: EnhancedPortScan/connectWithRetry/scanSinglePort 接入 session - core/service_probe.go: SmartPortInfoScanner 持有 session,重连走 session.DialTCP - core/icmp.go: CheckLive/tcpProbeAlive 接入 session - 17 个 service 插件: 内部 helper 函数全部穿透 ctx+session - 移除插件中冗余的手动 TCP 计数(DialTCP 内部已处理) - plugins/core 下已无 WrapperTcpWithTimeout/SafeTCPDial 调用残留 * refactor: 清除 core/plugins 全局状态依赖,ProgressManager 缓存引用 (Phase 5) - core/alive_scanner.go: GetFlagVars() → session.Params - core/service_scanner.go: GetFlagVars() → session.Params 和 config.Target.Ports - common/progress_manager.go: 缓存 State 和 NoColor 到字段,不再运行时读全局 - common/output_api.go: SaveResult 改用 GetGlobalConfig().Output.DisableSave - common/network.go: WrapperTcpWithTimeout 标记 Deprecated - core/ 和 plugins/ 下已无全局状态调用残留 * fix: 修复 dialer timeout 锁死、CVE 检测绕过 session 和误报问题 * fix: 修复 pocDNSLog data race,穿透 ctx 到全链路,消除残余 net.DialTimeout 绕过 * perf: CVE-2026-24061 检测改并发执行,消除硬 sleep 用 deadline 替代 * feat: 项目缓存系统,跨扫描合并资产,缓存 host:port 避免漏报 * perf: 三阶段性能优化,ICMP 并发提升+TCP 并行探测,端口扫描退避调整,服务探测超时减半 * fix: 修复凭据测试清理 goroutine 无限阻塞导致的 goroutine 泄漏 * fix: 凭据测试连续网络错误短路、resultChan 缓冲防阻塞、timer 泄漏修复 * perf: 大规模扫描网段预筛,按 /24 探活跳过空子网,B 段扫描从 2h+ 降至 2min * fix: 网段预筛从抽样改全覆盖,每台主机发 1 个探测包,消除漏报 * perf: 网段预筛增加网关启发式,.1/.254 多端口优先探测,命中即跳过逐主机兜底 * fix: MSSQL 连接加 encrypt=disable 修复无 TLS 环境扫描失败,Web API 参数校验负数 * feat: Release 增加 armv5 架构支持 * chore: bump version to 2.1.3 * fix: 锁定 golangci-lint 版本为 v2.12.1 修复 CI checksum 校验失败 * fix: golangci-lint 改用 go install 安装,绕过上游安装脚本 checksum 校验问题 * feat: -silent 模式输出 NDJSON 到 stdout,支持 AI agent 管道消费 - 新增 StdoutNDJSONWriter,silent 模式下每条扫描结果实时输出一行 JSON - LogWithProgress 层拦截人类可读日志,绕过 logger sync.Once 初始化时序问题 - 支持 fscan -h xxx -silent | jq 管道用法 * fix rdp invalid random panic (#573) * restore ms17010 legacy detection and exploit (#574) * fix ms17010 legacy packet decoding (#574) * fix csv web title output (#575) * fix web result protocol output (#577) * feat: add -ntp flag to disable TCP supplementary probe * fix: skip TCP supplementary probe in icmp mode * feat: add -debug flag with file logging to fscan_debug.log * fix: resolve golangci-lint errcheck and staticcheck warnings * fix: skip proxy deep verification for SOCKS5 connections (#579) SOCKS5 protocol validates connection reachability at protocol level, deep verification was incorrectly rejecting non-banner services like SMB(445), RPC(139) and Kerberos(88). * fix: exclude timeout from scan failure rate calculation (#578) Timeout is a normal scan result when firewalls drop packets, not a scan failure. Only resource exhaustion errors count toward failure rate. * feat: flatten NDJSON output for AI agent consumption and add SKILL.md * perf: 端口扫描自适应超时,基于 RTT 采样动态调整连接超时 * perf: 四项扫描性能优化 - SO_LINGER=0 快速释放连接,减少 TIME_WAIT 堆积 - 服务探测超时自适应,RTT 采样约束读超时上限 - 端口扫描结果流式传递,pipeline 并行端口扫描和插件执行 - ICMP 批量预构建包和地址,减少发送循环开销 * perf: 六项性能优化 - DNS 解析缓存:sync.Map 缓存避免重复系统调用 - 凭据测试 TCP 预检:不可达目标直接跳过全部凭据 - Web 探测 HTTP Client 复用:全局共享连接池 - 端口扫描 Bloom Filter 去重:替代 map 降低内存 - 进度条 atomic 累加 + 50ms 节流渲染:消除锁竞争 - 服务探针预解码:Init 时预编译,运行时零解码开销 * refactor: replace bloom filter with map for deduplication Bloom filter has false positive risk which can silently drop valid scan results. Map provides exact deduplication with negligible memory overhead at the scale of open ports (typically thousands, not millions). * fix: credential TCP precheck bypass proxy and pipeline goroutine leak - Skip TCP precheck when proxy is enabled, net.DialTimeout cannot reach targets behind SOCKS5/HTTP proxy - Drain stream channel on ctx cancellation to prevent EnhancedPortScan goroutine from blocking on a full channel * fix: stream channel 提前返回未关闭导致 goroutine 泄漏,服务探测超时下限 500ms * fix: resolve golangci-lint errcheck and staticcheck warnings --------- Co-authored-by: r00t <[email protected]>
476 lines
12 KiB
Go
476 lines
12 KiB
Go
package output
|
|
|
|
import (
|
|
"fmt"
|
|
"sync"
|
|
"testing"
|
|
)
|
|
|
|
/*
|
|
buffer_test.go - ResultBuffer 高价值测试
|
|
|
|
测试重点:
|
|
1. 去重逻辑 - 不同结果类型的去重策略差异
|
|
2. 完整度评分 - 决定是否替换已有服务记录
|
|
3. 并发安全 - 多goroutine同时Add
|
|
*/
|
|
|
|
// =============================================================================
|
|
// 基本去重测试
|
|
// =============================================================================
|
|
|
|
// TestResultBuffer_HostDeduplication 测试主机去重
|
|
func TestResultBuffer_HostDeduplication(t *testing.T) {
|
|
buf := NewResultBuffer()
|
|
|
|
// 添加相同主机多次
|
|
for i := 0; i < 10; i++ {
|
|
buf.Add(&ScanResult{
|
|
Type: TypeHost,
|
|
Target: "192.168.1.1",
|
|
Status: "alive",
|
|
})
|
|
}
|
|
|
|
hosts, _, _, _ := buf.Summary()
|
|
if hosts != 1 {
|
|
t.Errorf("主机应去重为1个,实际 %d", hosts)
|
|
}
|
|
}
|
|
|
|
// TestResultBuffer_PortDeduplication 测试端口去重
|
|
func TestResultBuffer_PortDeduplication(t *testing.T) {
|
|
buf := NewResultBuffer()
|
|
|
|
// 相同IP:Port应去重
|
|
for i := 0; i < 5; i++ {
|
|
buf.Add(&ScanResult{
|
|
Type: TypePort,
|
|
Target: "192.168.1.1",
|
|
Details: map[string]interface{}{"port": 80},
|
|
})
|
|
}
|
|
|
|
// 不同端口不去重
|
|
buf.Add(&ScanResult{
|
|
Type: TypePort,
|
|
Target: "192.168.1.1",
|
|
Details: map[string]interface{}{"port": 443},
|
|
})
|
|
|
|
_, ports, _, _ := buf.Summary()
|
|
if ports != 2 {
|
|
t.Errorf("端口应有2个(80和443),实际 %d", ports)
|
|
}
|
|
}
|
|
|
|
// TestResultBuffer_ServiceDeduplication 测试服务去重
|
|
func TestResultBuffer_ServiceDeduplication(t *testing.T) {
|
|
buf := NewResultBuffer()
|
|
|
|
// 相同Target的服务应去重
|
|
buf.Add(&ScanResult{
|
|
Type: TypeService,
|
|
Target: "192.168.1.1:80",
|
|
Status: "http",
|
|
})
|
|
buf.Add(&ScanResult{
|
|
Type: TypeService,
|
|
Target: "192.168.1.1:80",
|
|
Status: "nginx",
|
|
})
|
|
|
|
_, _, services, _ := buf.Summary()
|
|
if services != 1 {
|
|
t.Errorf("相同Target的服务应去重为1个,实际 %d", services)
|
|
}
|
|
}
|
|
|
|
// TestResultBuffer_VulnDeduplication 测试漏洞去重
|
|
func TestResultBuffer_VulnDeduplication(t *testing.T) {
|
|
buf := NewResultBuffer()
|
|
|
|
// 相同Target+Status的漏洞应去重
|
|
for i := 0; i < 3; i++ {
|
|
buf.Add(&ScanResult{
|
|
Type: TypeVuln,
|
|
Target: "192.168.1.1:445",
|
|
Status: "MS17-010",
|
|
})
|
|
}
|
|
|
|
// 不同漏洞不去重
|
|
buf.Add(&ScanResult{
|
|
Type: TypeVuln,
|
|
Target: "192.168.1.1:445",
|
|
Status: "CVE-2020-0796",
|
|
})
|
|
|
|
_, _, _, vulns := buf.Summary()
|
|
if vulns != 2 {
|
|
t.Errorf("漏洞应有2个,实际 %d", vulns)
|
|
}
|
|
}
|
|
|
|
// =============================================================================
|
|
// 完整度评分测试
|
|
// =============================================================================
|
|
|
|
// TestResultBuffer_CompletenessScore 测试完整度评分
|
|
func TestResultBuffer_CompletenessScore(t *testing.T) {
|
|
buf := NewResultBuffer()
|
|
|
|
tests := []struct {
|
|
name string
|
|
result *ScanResult
|
|
expectedScore int
|
|
}{
|
|
{
|
|
name: "空Details",
|
|
result: &ScanResult{Details: nil},
|
|
expectedScore: 0,
|
|
},
|
|
{
|
|
name: "只有status",
|
|
result: &ScanResult{Details: map[string]interface{}{"status": 200}},
|
|
expectedScore: 2,
|
|
},
|
|
{
|
|
name: "有server",
|
|
result: &ScanResult{Details: map[string]interface{}{"server": "nginx/1.18.0"}},
|
|
expectedScore: 2,
|
|
},
|
|
{
|
|
name: "有title",
|
|
result: &ScanResult{Details: map[string]interface{}{"title": "Welcome"}},
|
|
expectedScore: 1,
|
|
},
|
|
{
|
|
name: "有指纹-[]string",
|
|
result: &ScanResult{Details: map[string]interface{}{"fingerprints": []string{"nginx"}}},
|
|
expectedScore: 3,
|
|
},
|
|
{
|
|
name: "有指纹-[]interface{}",
|
|
result: &ScanResult{Details: map[string]interface{}{"fingerprints": []interface{}{"apache", "php"}}},
|
|
expectedScore: 3,
|
|
},
|
|
{
|
|
name: "有banner",
|
|
result: &ScanResult{Details: map[string]interface{}{"banner": "SSH-2.0-OpenSSH"}},
|
|
expectedScore: 1,
|
|
},
|
|
{
|
|
name: "完整记录",
|
|
result: &ScanResult{
|
|
Details: map[string]interface{}{
|
|
"status": 200,
|
|
"server": "nginx",
|
|
"title": "Home",
|
|
"fingerprints": []string{"nginx", "php"},
|
|
"banner": "test",
|
|
},
|
|
},
|
|
expectedScore: 9, // 2+2+1+3+1
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
score := buf.CalculateCompleteness(tt.result)
|
|
if score != tt.expectedScore {
|
|
t.Errorf("完整度评分 = %d, 期望 %d", score, tt.expectedScore)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestResultBuffer_ServiceUpdate 测试服务记录更新
|
|
//
|
|
// 当新记录比旧记录更完整时,应该替换
|
|
func TestResultBuffer_ServiceUpdate(t *testing.T) {
|
|
buf := NewResultBuffer()
|
|
|
|
// 先添加简单记录
|
|
buf.Add(&ScanResult{
|
|
Type: TypeService,
|
|
Target: "192.168.1.1:80",
|
|
Status: "http",
|
|
Details: map[string]interface{}{},
|
|
})
|
|
|
|
// 再添加更完整的记录
|
|
buf.Add(&ScanResult{
|
|
Type: TypeService,
|
|
Target: "192.168.1.1:80",
|
|
Status: "http",
|
|
Details: map[string]interface{}{
|
|
"status": 200,
|
|
"server": "nginx/1.18.0",
|
|
"title": "Welcome",
|
|
"fingerprints": []string{"nginx", "php"},
|
|
},
|
|
})
|
|
|
|
_, _, services, _ := buf.Summary()
|
|
if services != 1 {
|
|
t.Fatal("服务数量应为1")
|
|
}
|
|
|
|
// 验证是更完整的记录
|
|
if buf.ServiceResults[0].Details == nil {
|
|
t.Fatal("Details不应为nil")
|
|
}
|
|
if buf.ServiceResults[0].Details["server"] != "nginx/1.18.0" {
|
|
t.Error("应保留更完整的记录")
|
|
}
|
|
}
|
|
|
|
func TestResultBuffer_ServiceUpdateMergesDetails(t *testing.T) {
|
|
buf := NewResultBuffer()
|
|
|
|
buf.Add(&ScanResult{
|
|
Type: TypeService,
|
|
Target: "192.168.1.1:80",
|
|
Status: "identified",
|
|
Details: map[string]interface{}{
|
|
"service": "http",
|
|
"banner": "HTTP/1.1 200 OK",
|
|
},
|
|
})
|
|
buf.Add(&ScanResult{
|
|
Type: TypeService,
|
|
Target: "192.168.1.1:80",
|
|
Status: "web",
|
|
Details: map[string]interface{}{
|
|
"title": "Home",
|
|
"status": 200,
|
|
"server": "nginx",
|
|
},
|
|
})
|
|
|
|
if len(buf.ServiceResults) != 1 {
|
|
t.Fatalf("期望1条服务记录,实际 %d", len(buf.ServiceResults))
|
|
}
|
|
details := buf.ServiceResults[0].Details
|
|
for _, key := range []string{"service", "banner", "title", "status", "server"} {
|
|
if _, ok := details[key]; !ok {
|
|
t.Errorf("合并后的服务记录缺少字段 %q: %#v", key, details)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestResultBuffer_ServiceNoDowngrade 测试不降级服务记录
|
|
//
|
|
// 当新记录不如旧记录完整时,不应替换
|
|
func TestResultBuffer_ServiceNoDowngrade(t *testing.T) {
|
|
buf := NewResultBuffer()
|
|
|
|
// 先添加完整记录
|
|
buf.Add(&ScanResult{
|
|
Type: TypeService,
|
|
Target: "192.168.1.1:80",
|
|
Status: "http",
|
|
Details: map[string]interface{}{
|
|
"status": 200,
|
|
"server": "nginx/1.18.0",
|
|
"fingerprints": []string{"nginx"},
|
|
},
|
|
})
|
|
|
|
// 再添加简单记录
|
|
buf.Add(&ScanResult{
|
|
Type: TypeService,
|
|
Target: "192.168.1.1:80",
|
|
Status: "http",
|
|
Details: map[string]interface{}{},
|
|
})
|
|
|
|
// 验证仍保留完整记录
|
|
if buf.ServiceResults[0].Details["server"] != "nginx/1.18.0" {
|
|
t.Error("不应降级到不完整的记录")
|
|
}
|
|
}
|
|
|
|
// =============================================================================
|
|
// 并发安全测试
|
|
// =============================================================================
|
|
|
|
// TestResultBuffer_ConcurrentAdd 测试并发添加
|
|
func TestResultBuffer_ConcurrentAdd(t *testing.T) {
|
|
buf := NewResultBuffer()
|
|
|
|
const goroutines = 100
|
|
const resultsPerGoroutine = 100
|
|
|
|
var wg sync.WaitGroup
|
|
wg.Add(goroutines)
|
|
|
|
for i := 0; i < goroutines; i++ {
|
|
go func(id int) {
|
|
defer wg.Done()
|
|
for j := 0; j < resultsPerGoroutine; j++ {
|
|
// 每个goroutine添加不同类型的结果
|
|
switch j % 4 {
|
|
case 0:
|
|
buf.Add(&ScanResult{
|
|
Type: TypeHost,
|
|
Target: fmt.Sprintf("192.168.%d.%d", id, j),
|
|
})
|
|
case 1:
|
|
buf.Add(&ScanResult{
|
|
Type: TypePort,
|
|
Target: fmt.Sprintf("192.168.%d.%d", id, j),
|
|
Details: map[string]interface{}{"port": j},
|
|
})
|
|
case 2:
|
|
buf.Add(&ScanResult{
|
|
Type: TypeService,
|
|
Target: fmt.Sprintf("192.168.%d.%d:%d", id, j, j),
|
|
})
|
|
case 3:
|
|
buf.Add(&ScanResult{
|
|
Type: TypeVuln,
|
|
Target: fmt.Sprintf("192.168.%d.%d", id, j),
|
|
Status: fmt.Sprintf("CVE-%d", j),
|
|
})
|
|
}
|
|
}
|
|
}(i)
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
// 验证没有panic,数据完整
|
|
hosts, ports, services, vulns := buf.Summary()
|
|
total := hosts + ports + services + vulns
|
|
|
|
if total == 0 {
|
|
t.Error("并发添加后应有结果")
|
|
}
|
|
|
|
t.Logf("并发测试完成: %d hosts, %d ports, %d services, %d vulns",
|
|
hosts, ports, services, vulns)
|
|
}
|
|
|
|
// TestResultBuffer_ConcurrentSummary 测试并发获取摘要
|
|
func TestResultBuffer_ConcurrentSummary(t *testing.T) {
|
|
buf := NewResultBuffer()
|
|
|
|
// 预填充一些数据
|
|
for i := 0; i < 100; i++ {
|
|
buf.Add(&ScanResult{
|
|
Type: TypeHost,
|
|
Target: fmt.Sprintf("192.168.1.%d", i),
|
|
})
|
|
}
|
|
|
|
var wg sync.WaitGroup
|
|
wg.Add(100)
|
|
|
|
for i := 0; i < 100; i++ {
|
|
go func() {
|
|
defer wg.Done()
|
|
// 同时获取摘要和添加
|
|
buf.Summary()
|
|
buf.Add(&ScanResult{
|
|
Type: TypeHost,
|
|
Target: "10.0.0.1",
|
|
})
|
|
}()
|
|
}
|
|
|
|
wg.Wait()
|
|
// 没有panic即为成功
|
|
}
|
|
|
|
// =============================================================================
|
|
// 边界情况测试
|
|
// =============================================================================
|
|
|
|
// TestResultBuffer_NilResult 测试nil结果
|
|
func TestResultBuffer_NilResult(t *testing.T) {
|
|
buf := NewResultBuffer()
|
|
buf.Add(nil) // 不应panic
|
|
|
|
hosts, ports, services, vulns := buf.Summary()
|
|
if hosts+ports+services+vulns != 0 {
|
|
t.Error("添加nil后应无结果")
|
|
}
|
|
}
|
|
|
|
// TestResultBuffer_PortWithoutDetails 测试无Details的端口
|
|
func TestResultBuffer_PortWithoutDetails(t *testing.T) {
|
|
buf := NewResultBuffer()
|
|
|
|
buf.Add(&ScanResult{
|
|
Type: TypePort,
|
|
Target: "192.168.1.1",
|
|
Details: nil,
|
|
})
|
|
|
|
_, ports, _, _ := buf.Summary()
|
|
if ports != 1 {
|
|
t.Error("无Details的端口也应被添加")
|
|
}
|
|
}
|
|
|
|
// TestResultBuffer_Clear 测试清空
|
|
func TestResultBuffer_Clear(t *testing.T) {
|
|
buf := NewResultBuffer()
|
|
|
|
// 添加各类结果
|
|
buf.Add(&ScanResult{Type: TypeHost, Target: "192.168.1.1"})
|
|
buf.Add(&ScanResult{Type: TypePort, Target: "192.168.1.1", Details: map[string]interface{}{"port": 80}})
|
|
buf.Add(&ScanResult{Type: TypeService, Target: "192.168.1.1:80"})
|
|
buf.Add(&ScanResult{Type: TypeVuln, Target: "192.168.1.1", Status: "CVE-2021-1234"})
|
|
|
|
// 清空
|
|
buf.Clear()
|
|
|
|
hosts, ports, services, vulns := buf.Summary()
|
|
if hosts+ports+services+vulns != 0 {
|
|
t.Error("Clear后应无结果")
|
|
}
|
|
|
|
// 验证可以继续添加
|
|
buf.Add(&ScanResult{Type: TypeHost, Target: "10.0.0.1"})
|
|
hosts, _, _, _ = buf.Summary()
|
|
if hosts != 1 {
|
|
t.Error("Clear后应能继续添加")
|
|
}
|
|
}
|
|
|
|
// TestResultBuffer_EmptyFingerprints 测试空指纹数组
|
|
func TestResultBuffer_EmptyFingerprints(t *testing.T) {
|
|
buf := NewResultBuffer()
|
|
|
|
// 空字符串数组
|
|
score1 := buf.CalculateCompleteness(&ScanResult{
|
|
Details: map[string]interface{}{"fingerprints": []string{}},
|
|
})
|
|
if score1 != 0 {
|
|
t.Errorf("空指纹数组不应加分,实际 %d", score1)
|
|
}
|
|
|
|
// 空interface数组
|
|
score2 := buf.CalculateCompleteness(&ScanResult{
|
|
Details: map[string]interface{}{"fingerprints": []interface{}{}},
|
|
})
|
|
if score2 != 0 {
|
|
t.Errorf("空interface数组不应加分,实际 %d", score2)
|
|
}
|
|
}
|
|
|
|
// TestResultBuffer_StatusZero 测试status为0
|
|
func TestResultBuffer_StatusZero(t *testing.T) {
|
|
buf := NewResultBuffer()
|
|
|
|
score := buf.CalculateCompleteness(&ScanResult{
|
|
Details: map[string]interface{}{"status": 0},
|
|
})
|
|
if score != 0 {
|
|
t.Errorf("status为0不应加分,实际 %d", score)
|
|
}
|
|
}
|