103 lines
3.2 KiB
Go
Executable File
103 lines
3.2 KiB
Go
Executable File
package tomb
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func TestIngestStoreVerify(t *testing.T) {
|
|
tmp := t.TempDir()
|
|
tomb := New(tmp)
|
|
|
|
// Create a fake source file to ingest.
|
|
srcBlob := filepath.Join(tmp, "src.txt")
|
|
if err := os.WriteFile(srcBlob, []byte("hello world"), 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
hash, err := tomb.IngestBlob(srcBlob)
|
|
if err != nil {
|
|
t.Fatalf("IngestBlob: %v", err)
|
|
}
|
|
if hash == "" {
|
|
t.Fatal("expected non-empty hash")
|
|
}
|
|
|
|
sarc := &Sarcophagus{
|
|
SpellName: "test",
|
|
Version: "1.0",
|
|
Arch: "x86_64",
|
|
Files: map[string]string{"/usr/bin/test": hash},
|
|
}
|
|
if err := tomb.Store(sarc); err != nil {
|
|
t.Fatalf("Store: %v", err)
|
|
}
|
|
if sarc.EssenceID == "" {
|
|
t.Fatal("Store should set EssenceID")
|
|
}
|
|
|
|
// VerifyRoot must pass.
|
|
if err := tomb.VerifyRoot(sarc.EssenceID); err != nil {
|
|
t.Fatalf("VerifyRoot: %v", err)
|
|
}
|
|
|
|
// VerifyBlobs must pass.
|
|
if err := tomb.VerifyBlobs(sarc.EssenceID); err != nil {
|
|
t.Fatalf("VerifyBlobs: %v", err)
|
|
}
|
|
|
|
// Tamper with the blob — VerifyBlobs must fail.
|
|
blobPath := tomb.blobPath(hash)
|
|
if err := os.WriteFile(blobPath, []byte("tampered"), 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := tomb.VerifyBlobs(sarc.EssenceID); err == nil {
|
|
t.Fatal("VerifyBlobs must detect tampering")
|
|
} else {
|
|
t.Logf("✓ correctly detected bit-rot: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestDedup(t *testing.T) {
|
|
tmp := t.TempDir()
|
|
tomb := New(tmp)
|
|
|
|
// Two identical source files.
|
|
src1 := filepath.Join(tmp, "a.txt")
|
|
src2 := filepath.Join(tmp, "b.txt")
|
|
_ = os.WriteFile(src1, []byte("same content"), 0644)
|
|
_ = os.WriteFile(src2, []byte("same content"), 0644)
|
|
|
|
h1, _ := tomb.IngestBlob(src1)
|
|
h2, _ := tomb.IngestBlob(src2)
|
|
if h1 != h2 {
|
|
t.Fatalf("dedup failed: %s != %s", h1, h2)
|
|
}
|
|
t.Logf("✓ dedup hit: %s", h1)
|
|
}
|
|
|
|
func TestFindByVariant(t *testing.T) {
|
|
tmp := t.TempDir()
|
|
tomb := New(tmp)
|
|
sarc := &Sarcophagus{
|
|
VariantHash: "v-1",
|
|
SpellName: "demo",
|
|
Version: "1.0",
|
|
Files: map[string]string{"/usr/bin/demo": "h1"},
|
|
}
|
|
if err := tomb.Store(sarc); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if id, err := tomb.FindByVariant("v-1"); err != nil {
|
|
t.Fatalf("FindByVariant: %v", err)
|
|
} else if id != sarc.EssenceID {
|
|
t.Fatalf("FindByVariant returned %s, want %s", id, sarc.EssenceID)
|
|
}
|
|
if id, err := tomb.FindByVariant("missing"); err != nil {
|
|
t.Fatalf("FindByVariant: %v", err)
|
|
} else if id != "" {
|
|
t.Fatalf("FindByVariant should return empty for unknown variant, got %s", id)
|
|
}
|
|
}
|