mirror of
https://github.com/shadow1ng/fscan.git
synced 2026-09-25 20:51:52 +08:00
test: 补充单元测试覆盖率 29.9% → 36.6%
新建 18 个测试文件,追加 30 个已有测试文件,覆盖协议解析、 错误分类、CEL 表达式求值、YAML 反序列化、字节编码等纯函数。
This commit is contained in:
@@ -5,6 +5,7 @@ package services
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
@@ -47,3 +48,74 @@ func TestValidateCQLQueryResponseRejectsErrors(t *testing.T) {
|
||||
t.Fatal("validateCQLQueryResponse() error = nil, want unexpected opcode error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestClassifyCassandraErrorType(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
err error
|
||||
want ErrorType
|
||||
}{
|
||||
{"nil", nil, ErrorTypeUnknown},
|
||||
{"auth error", errors.New("authentication failed"), ErrorTypeAuth},
|
||||
{"bad credentials", errors.New("bad credentials"), ErrorTypeAuth},
|
||||
{"network error", errors.New("connection refused"), ErrorTypeNetwork},
|
||||
{"unknown", errors.New("random cassandra error"), ErrorTypeUnknown},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := classifyCassandraErrorType(tt.err); got != tt.want {
|
||||
t.Errorf("classifyCassandraErrorType() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCqlShortString(t *testing.T) {
|
||||
got := cqlShortString("AB")
|
||||
if len(got) != 4 || binary.BigEndian.Uint16(got[:2]) != 2 || string(got[2:]) != "AB" {
|
||||
t.Errorf("cqlShortString(AB) = %v", got)
|
||||
}
|
||||
empty := cqlShortString("")
|
||||
if len(empty) != 2 || binary.BigEndian.Uint16(empty) != 0 {
|
||||
t.Errorf("cqlShortString empty = %v", empty)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCqlLongString(t *testing.T) {
|
||||
got := cqlLongString("XYZ")
|
||||
if len(got) != 7 || binary.BigEndian.Uint32(got[:4]) != 3 || string(got[4:]) != "XYZ" {
|
||||
t.Errorf("cqlLongString(XYZ) = %v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCqlStringMap(t *testing.T) {
|
||||
m := map[string]string{"k": "v"}
|
||||
got := cqlStringMap(m)
|
||||
if got[0] != 0x00 || got[1] != 0x01 {
|
||||
t.Errorf("count bytes wrong: %v", got[:2])
|
||||
}
|
||||
if !bytes.Contains(got, []byte("k")) || !bytes.Contains(got, []byte("v")) {
|
||||
t.Errorf("missing key/value in %v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExtractClusterName(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
data []byte
|
||||
want string
|
||||
}{
|
||||
{"empty", nil, "unknown"},
|
||||
{"short", []byte{0x01, 0x02}, "unknown"},
|
||||
{"printable", append([]byte{0x00, 0x00, 0x00, 0x01}, []byte("TestCluster")...), "TestCluster"},
|
||||
{"binary prefix", append([]byte{0x00, 0x00, 0x00, 0x00, 0x01, 0x02}, []byte("MyCluster")...), "MyCluster"},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := extractClusterName(tt.data)
|
||||
if !strings.Contains(got, tt.want) && got != tt.want {
|
||||
t.Errorf("extractClusterName() = %q, want %q", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user