manifest_test.go raw

   1  package serve
   2  
   3  import (
   4  	"crypto/ed25519"
   5  	"net/http/httptest"
   6  	"testing"
   7  
   8  	"git.smesh.lol/btcd-lite/internal/manifest"
   9  	"git.smesh.lol/btcd-lite/internal/service"
  10  	"git.smesh.lol/btcd-lite/internal/store"
  11  )
  12  
  13  // TestManifestWithKey: a view with a key publishes a manifest of its block
  14  // coverage, and a client verifying it with the same key's public half accepts
  15  // it.
  16  func TestManifestWithKey(t *testing.T) {
  17  	dir := t.TempDir()
  18  	b, hdr := putBlocks(t, dir, 20)
  19  	defer b.Close()
  20  	defer hdr.Close()
  21  	v := New(map[string]*store.HeaderStore{"btc": hdr}, b)
  22  	v.Key = ed25519.NewKeyFromSeed(make([]byte, 32))
  23  	h := service.Handler(v)
  24  	w := httptest.NewRecorder()
  25  	h.ServeHTTP(w, httptest.NewRequest("GET", "/v1/manifest", nil))
  26  	if w.Code != 200 {
  27  		t.Fatalf("manifest status %d: %s", w.Code, w.Body)
  28  	}
  29  	pub := v.Key.Public().(ed25519.PublicKey)
  30  	m, err := manifest.Verify(w.Body.Bytes(), pub)
  31  	if err != nil {
  32  		t.Fatalf("verify: %v", err)
  33  	}
  34  	cov, ok := m.ChainTip("btc")
  35  	if !ok || cov.Tip != 20 {
  36  		t.Fatalf("coverage %+v", cov)
  37  	}
  38  }
  39