Cache stable executable digests by file identity and algorithm, coalescing concurrent requests within bounded limits. Revalidate opened files and fall back to full hashing on misses or uncertainty. Keep fallback lookups read-only and protect checksum map reads and logging. Add regression coverage for reuse, mutation, failure and contention.
82 lines
2.3 KiB
Go
82 lines
2.3 KiB
Go
//go:build linux
|
|
|
|
package procmon
|
|
|
|
import (
|
|
"bytes"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestChecksumVerifiedFallbackReuse(t *testing.T) {
|
|
path, size := checksumLiveImage(t)
|
|
want := checksumExpected(t, ProcSelfExe, HashMD5)
|
|
seed := checksumFixtureProcess(600, path, ProcSelfExe)
|
|
seed.ComputeChecksum(HashMD5)
|
|
if seed.Checksums[HashMD5] != want {
|
|
t.Fatal("primary seed digest mismatch")
|
|
}
|
|
before := checksumReadChars(t)
|
|
for i := 0; i < 12; i++ {
|
|
p := checksumFixtureProcess(610+i, path, ProcSelfExe+"/unavailable")
|
|
p.RealPath = ProcSelfExe
|
|
p.ComputeChecksum(HashMD5)
|
|
if p.Checksums[HashMD5] != want {
|
|
t.Fatal("verified fallback digest mismatch")
|
|
}
|
|
}
|
|
reads := checksumReadChars(t) - before
|
|
t.Logf("fallback_processes=12 image_bytes=%d read_chars=%d", size, reads)
|
|
if reads > 128*1024 {
|
|
t.Fatalf("verified image fallback rehashed: read_chars=%d image_bytes=%d", reads, size)
|
|
}
|
|
}
|
|
|
|
func TestChecksumUnseededFallbackStillReads(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "unverified-image")
|
|
data := bytes.Repeat([]byte("x"), 2*1024*1024)
|
|
if err := os.WriteFile(path, data, 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// Settle real ctime so this cannot pass merely because a fresh file is refused.
|
|
timer := time.NewTimer(2100 * time.Millisecond)
|
|
<-timer.C
|
|
timer.Stop()
|
|
want := checksumExpected(t, path, HashMD5)
|
|
before := checksumReadChars(t)
|
|
for i := 0; i < 8; i++ {
|
|
p := checksumFixtureProcess(700+i, path, ProcSelfExe+"/unavailable")
|
|
p.RealPath = path
|
|
p.ComputeChecksum(HashMD5)
|
|
if p.Checksums[HashMD5] != want {
|
|
t.Fatal("unverified fallback digest mismatch")
|
|
}
|
|
}
|
|
reads := checksumReadChars(t) - before
|
|
if reads < uint64(8*len(data)) {
|
|
t.Fatal("arbitrary fallback file seeded shared digest cache")
|
|
}
|
|
}
|
|
|
|
func BenchmarkChecksumWarmFallback(b *testing.B) {
|
|
path, size := checksumLiveImage(b)
|
|
seed := checksumFixtureProcess(800, path, ProcSelfExe)
|
|
seed.ComputeChecksum(HashMD5)
|
|
before := checksumReadChars(b)
|
|
b.ReportAllocs()
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
p := checksumFixtureProcess(810+i, path, ProcSelfExe+"/unavailable")
|
|
p.RealPath = ProcSelfExe
|
|
p.ComputeChecksum(HashMD5)
|
|
if len(p.Checksums[HashMD5]) != 32 {
|
|
b.Fatal("missing checksum")
|
|
}
|
|
}
|
|
b.StopTimer()
|
|
reads := checksumReadChars(b) - before
|
|
b.ReportMetric(float64(reads)/float64(b.N), "read-B/op")
|
|
b.ReportMetric(float64(size), "image-B")
|
|
}
|