trustopen_test.go raw

   1  package store_test
   2  
   3  import (
   4  	"os"
   5  	"path/filepath"
   6  	"testing"
   7  
   8  	"git.smesh.lol/nostrino/pkg/crypto/chainhash"
   9  
  10  	"git.smesh.lol/btcd-lite/internal/store"
  11  )
  12  
  13  // TestTrustingOpenDoesNotReadTheStore: the trusting open must not read a single
  14  // stored body, which is the whole point on a dataset that is hours of reads.
  15  func TestTrustingOpenDoesNotReadTheStore(t *testing.T) {
  16  	dir := t.TempDir()
  17  	s, err := store.NewBlockStore(dir)
  18  	if err != nil {
  19  		t.Fatal(err)
  20  	}
  21  	raw := make([]byte, 81)
  22  	raw[0] = 1
  23  	h := chainhash.DoubleHashH(raw[:80])
  24  	if err := s.PutBlock(h, raw); err != nil {
  25  		t.Fatal(err)
  26  	}
  27  	if err := s.Close(); err != nil {
  28  		t.Fatal(err)
  29  	}
  30  	// A handle that counts reads: truncating the body file would make a reading
  31  	// open fail, so the file is made unreadable instead.
  32  	body := filepath.Join(dir, "00000000.blk")
  33  	if err := os.Chmod(body, 0o000); err != nil {
  34  		t.Skipf("cannot chmod: %v", err)
  35  	}
  36  	defer os.Chmod(body, 0o644)
  37  	if _, err := store.NewBlockStoreTrusting(dir); err != nil {
  38  		t.Fatalf("the trusting open read the body: %v", err)
  39  	}
  40  	if _, err := store.NewBlockStore(dir); err == nil {
  41  		t.Fatal("the verifying open did not read the body")
  42  	}
  43  }
  44