package blockcache import ( "os" "testing" "git.smesh.lol/nostrino/pkg/crypto/chainhash" ) // hashOf is a deterministic block hash for the tests. func hashOf(tag byte) chainhash.Hash { return chainhash.HashH([]byte{tag}) } // blockOf is a block of the requested size, distinguished by the tag. func blockOf(tag byte, size int) []byte { out := make([]byte, size) out[0] = tag return out } func openTestCache(t *testing.T, maxBytes int64) (*Cache, string) { t.Helper() dir := t.TempDir() cache, err := Open(Config{Dir: dir, MaxBytes: maxBytes}) if err != nil { t.Fatal(err) } t.Cleanup(func() { _ = cache.Close() }) return cache, dir } // TestRoundTrip: a block that went in comes out byte for byte. func TestRoundTrip(t *testing.T) { cache, _ := openTestCache(t, 1<<20) if err := cache.Put("btc", hashOf(1), blockOf(1, 100)); err != nil { t.Fatal(err) } raw, ok, err := cache.Get("btc", hashOf(1)) if err != nil { t.Fatal(err) } if !ok { t.Fatal("a block that was just stored is a miss") } if len(raw) != 100 || raw[0] != 1 { t.Fatalf("the block came back as %d bytes starting %d", len(raw), raw[0]) } } // TestAMissIsNotAnError: the cache is asked for blocks it does not hold, and that is the // normal answer rather than a failure. func TestAMissIsNotAnError(t *testing.T) { cache, _ := openTestCache(t, 1<<20) _, ok, err := cache.Get("btc", hashOf(9)) if err != nil { t.Fatalf("a miss was reported as an error: %v", err) } if ok { t.Fatal("a block that was never stored was found") } } // TestTheOldestGoesFirst: the cache is bounded, and the blocks that go are the ones stored // first, which is where the requests are not. func TestTheOldestGoesFirst(t *testing.T) { cache, _ := openTestCache(t, 250) for tag := byte(1); tag <= 3; tag++ { if err := cache.Put("btc", hashOf(tag), blockOf(tag, 100)); err != nil { t.Fatal(err) } } count, bytes := cache.Len() if bytes > 250 { t.Fatalf("cache holds %d bytes, bound is 250", bytes) } if count != 2 { t.Fatalf("cache holds %d blocks, want the two newest", count) } for tag := byte(2); tag <= 3; tag++ { if _, ok, _ := cache.Get("btc", hashOf(tag)); !ok { t.Fatalf("block %d was evicted before block 1", tag) } } if _, ok, _ := cache.Get("btc", hashOf(1)); ok { t.Fatal("the oldest block survived a full cache") } } // TestTheIndexSurvivesAReopen: a sidecar restarts and the blocks it paid to fetch are still // there. func TestTheIndexSurvivesAReopen(t *testing.T) { dir := t.TempDir() first, err := Open(Config{Dir: dir, MaxBytes: 1 << 20}) if err != nil { t.Fatal(err) } if err := first.Put("blake", hashOf(4), blockOf(4, 64)); err != nil { t.Fatal(err) } if err := first.Close(); err != nil { t.Fatal(err) } second, err := Open(Config{Dir: dir, MaxBytes: 1 << 20}) if err != nil { t.Fatal(err) } defer second.Close() raw, ok, err := second.Get("blake", hashOf(4)) if err != nil || !ok { t.Fatalf("the block did not survive the reopen: ok=%v err=%v", ok, err) } if len(raw) != 64 { t.Fatalf("the block came back as %d bytes", len(raw)) } count, bytes := second.Len() if count != 1 || bytes != 64 { t.Fatalf("index says %d blocks and %d bytes", count, bytes) } } // TestAVanishedFileIsDropped: the index can outlive a file, and an entry with no bytes behind // it is not a block. func TestAVanishedFileIsDropped(t *testing.T) { dir := t.TempDir() first, err := Open(Config{Dir: dir, MaxBytes: 1 << 20}) if err != nil { t.Fatal(err) } if err := first.Put("btc", hashOf(5), blockOf(5, 32)); err != nil { t.Fatal(err) } if err := first.Close(); err != nil { t.Fatal(err) } path, err := first.path("btc", hashOf(5).String()) if err != nil { t.Fatal(err) } if err := os.Remove(path); err != nil { t.Fatal(err) } second, err := Open(Config{Dir: dir, MaxBytes: 1 << 20}) if err != nil { t.Fatal(err) } defer second.Close() if count, _ := second.Len(); count != 0 { t.Fatalf("index kept %d entries whose files are gone", count) } } // TestANameThatIsAPathIsRefused: the chain name and the hash come from a request, so neither is // allowed to be a path. func TestANameThatIsAPathIsRefused(t *testing.T) { cache, _ := openTestCache(t, 1<<20) if _, _, err := cache.Get("../etc", hashOf(1)); err == nil { t.Fatal("a chain name with a path in it was accepted") } if _, err := cache.path("btc", "../../etc/passwd"); err == nil { t.Fatal("a block hash that is a path was accepted") } if err := cache.Put("../etc", hashOf(1), blockOf(1, 8)); err == nil { t.Fatal("a chain name with a path in it was accepted on write") } }