This commit is contained in:
toom1996
2026-09-16 00:56:15 +08:00
parent 5e4803b97f
commit 7dd3fda73c
10 changed files with 184 additions and 54 deletions

View File

@ -73,3 +73,39 @@ func TestTyped(t *testing.T) {
t.Fatal("长度不足 2 的 typed 串应返回错误")
}
}
// TestTypedWithType 验证新编码(类型进密码、无前缀):
// - 往返严格成立 DecodeWithType(EncodeWithType(n, typ), typ) == n;
// - 同数字不同类别得到完全不相关的串(runway#5 ≠ snap#5),且串本身不含类型字母;
// - 用错误 typ 解码得到错误主键(≠ n),从而查库自然 404。
func TestTypedWithType(t *testing.T) {
Init("test-salt")
// 往返
for _, typ := range []byte{TypeRunway, TypeSnap, TypeRunwayImage, TypeSnapImage} {
for _, n := range []uint32{0, 1, 5, 999, 1 << 20, (1 << 32) - 1} {
got, err := DecodeWithType(EncodeWithType(n, typ), typ)
if err != nil || got != n {
t.Fatalf("往返不一致: typ=%c n=%d got=%d err=%v", typ, n, got, err)
}
}
}
// 同数字不同类别:串不同且不相关,且都不含类型字母
r := EncodeWithType(5, TypeRunway)
s := EncodeWithType(5, TypeSnap)
if r == s {
t.Fatalf("同数字不同类别应得到不同串: runway=%q snap=%q", r, s)
}
if r[0] == TypeRunway || r[0] == TypeSnap || s[0] == TypeRunway || s[0] == TypeSnap {
t.Fatalf("新编码不应含类型字母前缀: runway=%q snap=%q", r, s)
}
// 用错误类别解码得不到原主键
if wrong, err := DecodeWithType(r, TypeSnap); err != nil || wrong == 5 {
t.Fatalf("用错误类别解码应得不到原主键: got=%d err=%v", wrong, err)
}
// 空串报错
if _, err := DecodeWithType("", TypeRunway); err == nil {
t.Fatal("空串应返回错误")
}
}