fe6e218d9c
build / build (push) Successful in 1m17s
候选归档 79 版 / 56MB blob 上,overview、storage、snapshots 三个只读接口 都在 10s 请求期限后返回 503。分段计时定位到唯一瓶颈是按版本重读全部 blob: Open 0ms、LoadManifest×79 11~22ms、ContentUsage 15~25ms、 Usage(逐版 Load 验字节)13.75~14.11s;单版 Load 261~290ms。 - Usage 拆成 Usage(逐版复核字节,同步/恢复与运维 CLI 用)与 UsageFromManifests(同一份去重实现,只做清单级校验),列举路径改用后者; - library_storage.go 每版两次 Load、snapshots 每版一次 Load 改为一次清单读取; - wiki/文档差异只读所需部件,被读部件仍由 OpenPart 复核摘要; - LoadManifest 与 Load 共用同一套清单判据(严格解码 + 重新编码校验); - 列举响应带 verification=manifest / verified=false,详情整版复核后才是 content, 避免把"清单可读"读成"完整性已验证"; - libraryWithDeadline 去掉超时后再写已结束响应的 goroutine 写法 (候选环境实测 http: superfluous response.WriteHeader call)。 回归:多版本共享大 blob 时枚举不读 blob 字节(篡改后枚举仍成功), 而 Load/OpenPart/Usage 仍拒绝坏 blob;坏清单两条路径都整条报错。 make verify 15 项全绿(local 档案)。
145 lines
5.2 KiB
Go
145 lines
5.2 KiB
Go
package librarysnapshot
|
|
|
|
import (
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// 枚举/盘点与执行走两条读取路径,这个文件把它们的分工钉死:
|
|
//
|
|
// - 枚举(UsageFromManifests / ContentUsage)只校验清单自身,**不读 blob 字节**
|
|
// —— 否则列版本就退化成 O(全部版本字节),实测 79 版 13.8s 打到接口超时;
|
|
// - 执行(Load / OpenPart)仍然逐部件复核字节,坏 blob 必须被拒绝;
|
|
// - 坏清单在两条路径上都必须如实报错,不能因为性能修复出现"假完整"。
|
|
|
|
// corruptBlob 把某个 blob 的内容改成长度不变的另一串字节:
|
|
// 文件大小与 0600 权限都合法,只有摘要对不上——这正是"被篡改"的形状。
|
|
func corruptBlob(t *testing.T, root, sha string) {
|
|
t.Helper()
|
|
path := filepath.Join(root, "blobs", sha)
|
|
info, err := os.Stat(path)
|
|
if err != nil {
|
|
t.Fatalf("blob %s: %v", sha, err)
|
|
}
|
|
body := make([]byte, info.Size())
|
|
for i := range body {
|
|
body[i] = 'x'
|
|
}
|
|
if err = os.WriteFile(path, body, 0600); err != nil {
|
|
t.Fatalf("write blob %s: %v", sha, err)
|
|
}
|
|
}
|
|
|
|
// 健康归档上两条路径必须给出同一份数字:清单级盘点的去重口径不能因为不读字节而变形。
|
|
func TestUsageFromManifestsMatchesVerifiedUsage(t *testing.T) {
|
|
archive, _ := fixtureArchive(t)
|
|
sealFixtureVersion(t, archive, "internal", time.Date(2026, 10, 28, 1, 0, 0, 0, time.UTC), 4)
|
|
sealFixtureVersion(t, archive, "internal", time.Date(2026, 10, 28, 2, 0, 0, 0, time.UTC), 4)
|
|
verified, err := archive.Usage()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
listed, err := archive.UsageFromManifests()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(verified) != len(listed) || len(listed) != 2 {
|
|
t.Fatalf("versions verified=%d listed=%d, want 2", len(verified), len(listed))
|
|
}
|
|
for i := range verified {
|
|
if verified[i] != listed[i] {
|
|
t.Fatalf("version %d: verified %+v != listed %+v", i, verified[i], listed[i])
|
|
}
|
|
// 两个版本内容完全一致 ⇒ 每个 blob 都被两版共享,共享字节不得为 0。
|
|
if listed[i].LogicalBytes <= 0 || listed[i].SharedBytes <= 0 || listed[i].UniqueBytes <= 0 {
|
|
t.Fatalf("listed usage must keep real accounting: %+v", listed[i])
|
|
}
|
|
}
|
|
}
|
|
|
|
// blob 被篡改后:枚举仍然给出全部版本(证明它没有按版本重读所有 blob),
|
|
// 而任何真正要用这个版本的路径都必须失败(证明完整性门槛没被移除)。
|
|
func TestListingSkipsBlobBytesWhileExecutionStillRejects(t *testing.T) {
|
|
archive, root := fixtureArchive(t)
|
|
first := sealFixtureVersion(t, archive, "internal", time.Date(2026, 10, 28, 1, 0, 0, 0, time.UTC), 4)
|
|
second := sealFixtureVersion(t, archive, "internal", time.Date(2026, 10, 28, 2, 0, 0, 0, time.UTC), 4)
|
|
|
|
manifest, err := archive.LoadManifest(first)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// 挑最大的部件来篡改,确保受影响的不是某个空表。
|
|
target := manifest.Parts[0]
|
|
for _, part := range manifest.Parts {
|
|
if part.Bytes > target.Bytes {
|
|
target = part
|
|
}
|
|
}
|
|
corruptBlob(t, root, target.SHA256)
|
|
|
|
listed, err := archive.UsageFromManifests()
|
|
if err != nil {
|
|
t.Fatalf("listing must not depend on blob bytes: %v", err)
|
|
}
|
|
if len(listed) != 2 || listed[0].VersionID != first || listed[1].VersionID != second {
|
|
t.Fatalf("listing lost versions: %+v", listed)
|
|
}
|
|
total, err := archive.ContentUsage()
|
|
if err != nil {
|
|
t.Fatalf("content accounting must not depend on blob bytes: %v", err)
|
|
}
|
|
if total.Versions != 2 || total.ContentBytes <= 0 {
|
|
t.Fatalf("content accounting = %+v", total)
|
|
}
|
|
|
|
// 执行路径:整版 Load、逐部件 OpenPart、逐版 Usage 都必须拒绝被篡改的字节。
|
|
if _, err = archive.Load(first); err == nil {
|
|
t.Fatal("Load must reject a tampered blob")
|
|
}
|
|
if _, err = archive.Load(second); err == nil {
|
|
t.Fatal("Load must reject a version sharing the tampered blob")
|
|
}
|
|
if _, err = archive.OpenPart(target); err == nil {
|
|
t.Fatal("OpenPart must reject a tampered blob")
|
|
}
|
|
if _, err = archive.Usage(); err == nil {
|
|
t.Fatal("verified Usage must reject a tampered blob")
|
|
}
|
|
}
|
|
|
|
// 清单被改过(内容与文件名摘要对不上)时,枚举必须整条失败:
|
|
// 性能修复不允许把"清单坏了"降级成"看起来还好"。
|
|
func TestListingRejectsTamperedManifest(t *testing.T) {
|
|
archive, root := fixtureArchive(t)
|
|
id := sealFixtureVersion(t, archive, "internal", time.Date(2026, 10, 28, 1, 0, 0, 0, time.UTC), 4)
|
|
path := filepath.Join(root, "versions", id+".json")
|
|
file, err := os.OpenFile(path, os.O_RDWR, 0600)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
body, err := io.ReadAll(file)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// 只改一个字节:JSON 仍然可解析,但内容摘要与文件名不再一致。
|
|
if _, err = file.WriteAt([]byte{' '}, 0); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
file.Close()
|
|
if string(body) == "" {
|
|
t.Fatal("empty manifest fixture")
|
|
}
|
|
if _, err = archive.LoadManifest(id); err == nil {
|
|
t.Fatal("LoadManifest must reject a manifest whose digest does not match its name")
|
|
}
|
|
if _, err = archive.UsageFromManifests(); err == nil {
|
|
t.Fatal("listing must fail closed on a tampered manifest")
|
|
}
|
|
if _, err = archive.ContentUsage(); err == nil {
|
|
t.Fatal("content accounting must fail closed on a tampered manifest")
|
|
}
|
|
}
|