98ccb9366e
build / build (push) Successful in 1m26s
为什么:CI 在 07a9caf 上 format 红(ds02_identity_test.go 未 gofmt),本地那次
make verify 跑在文件生成之前,不能算数;同时 /v1/encode 负控制只有断言、没有
落盘证据,复验时无法回看分类结果。
改了什么:gofmt 两个测试文件;负控制把实际出站条目与被判为 embedding 的差异
写进 raw(negative-control),正例仍只对发布段计数。
验证:make verify 16 项全绿;随后以新 SHA 重跑 DS02 隔离套件。
79 lines
2.5 KiB
Go
79 lines
2.5 KiB
Go
package v071
|
|
|
|
// DS-02 测试进程身份:把"runner 记录的那份源码构建的二进制"与"真正执行的进程"
|
|
// 绑定起来。runner 用 -ldflags 注入三个只在本测试包存在的值(run nonce、完整
|
|
// commit、源码树摘要),并在启动前后各算一次二进制 sha256;这里在进程内读
|
|
// os.Executable 的哈希,与 runner 通过环境变量声明的期望值逐字符比较。
|
|
//
|
|
// 这些值不是产品 metadata:不参与发布、不改 internal/version。
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"os"
|
|
"testing"
|
|
)
|
|
|
|
// 由 runner 的 -ldflags -X 注入;未注入时保留显眼的值,绝不伪装成已知身份。
|
|
var (
|
|
ds02ProvenanceNonce = "unlinked"
|
|
ds02ProvenanceCommit = "unlinked"
|
|
ds02ProvenanceTree = "unlinked"
|
|
)
|
|
|
|
type ds02ProcessIdentity struct {
|
|
Nonce string `json:"nonce"`
|
|
Commit string `json:"commit"`
|
|
SourceTree string `json:"source_tree"`
|
|
Executable string `json:"executable"`
|
|
BinarySHA256 string `json:"binary_sha256"`
|
|
}
|
|
|
|
func TestDS02ProcessIdentity(t *testing.T) {
|
|
expectedNonce := os.Getenv("KG_DS02_EXPECT_NONCE")
|
|
if expectedNonce == "" {
|
|
t.Skip("requires runner-injected expectations (KG_DS02_EXPECT_NONCE)")
|
|
}
|
|
executable, err := os.Executable()
|
|
if err != nil {
|
|
t.Fatalf("resolve own executable: %v", err)
|
|
}
|
|
raw, err := os.ReadFile(executable)
|
|
if err != nil {
|
|
t.Fatalf("read own executable: %v", err)
|
|
}
|
|
sum := sha256.Sum256(raw)
|
|
identity := ds02ProcessIdentity{
|
|
Nonce: ds02ProvenanceNonce,
|
|
Commit: ds02ProvenanceCommit,
|
|
SourceTree: ds02ProvenanceTree,
|
|
Executable: executable,
|
|
BinarySHA256: hex.EncodeToString(sum[:]),
|
|
}
|
|
encoded, err := json.Marshal(identity)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// 单行输出给 runner 解析:证明是"这个进程自报",不是 runner 事后补写。
|
|
t.Logf("DS02-IDENTITY %s", encoded)
|
|
cases := []struct {
|
|
name string
|
|
linked string
|
|
expected string
|
|
}{
|
|
{"nonce", identity.Nonce, expectedNonce},
|
|
{"commit", identity.Commit, os.Getenv("KG_DS02_EXPECT_COMMIT")},
|
|
{"source tree", identity.SourceTree, os.Getenv("KG_DS02_EXPECT_TREE")},
|
|
{"binary sha256", identity.BinarySHA256, os.Getenv("KG_DS02_EXPECT_BINARY_SHA256")},
|
|
}
|
|
for _, item := range cases {
|
|
if item.expected == "" {
|
|
t.Fatalf("%s expectation missing from runner environment", item.name)
|
|
}
|
|
if item.linked != item.expected {
|
|
t.Fatalf("%s mismatch: process has %q, runner expected %q", item.name, item.linked, item.expected)
|
|
}
|
|
}
|
|
}
|