service_test.go raw
1 package service_test
2
3 import (
4 "encoding/json"
5 "net/http/httptest"
6 "testing"
7
8 "git.smesh.lol/btcd-lite/internal/chunk"
9 "git.smesh.lol/btcd-lite/internal/service"
10 )
11
12 // build fills an index with three block chunks and two filter chunks.
13 func build(t *testing.T) (*service.Index, []chunk.Chunk) {
14 t.Helper()
15 idx := service.NewIndex()
16 var blocks []chunk.Chunk
17 for h := uint32(0); h < 30; h += 10 {
18 blocks = append(blocks, chunk.Chunk{Kind: chunk.Blocks, Chain: "btc", From: h, To: h + 10,
19 Raw: []byte{byte(h), byte(h + 1)}})
20 }
21 if err := idx.AddRun(chunk.Blocks, "btc", 0, 30, blocks); err != nil {
22 t.Fatal(err)
23 }
24 filters := []chunk.Chunk{
25 {Kind: chunk.Filters, Chain: "btc", From: 0, To: 15, Raw: []byte("f0")},
26 {Kind: chunk.Filters, Chain: "btc", From: 15, To: 30, Raw: []byte("f1")},
27 }
28 if err := idx.AddRun(chunk.Filters, "btc", 0, 30, filters); err != nil {
29 t.Fatal(err)
30 }
31 return idx, blocks
32 }
33
34 // TestStatus: the status document names the chain and its coverage, and the
35 // block count.
36 func TestStatus(t *testing.T) {
37 idx, _ := build(t)
38 h := service.Handler(idx)
39 w := httptest.NewRecorder()
40 h.ServeHTTP(w, httptest.NewRequest("GET", "/v1/status", nil))
41 if w.Code != 200 {
42 t.Fatalf("status %d", w.Code)
43 }
44 var st service.Status
45 if err := json.Unmarshal(w.Body.Bytes(), &st); err != nil {
46 t.Fatal(err)
47 }
48 btc := st.Chains["btc"]
49 if btc.Start != 0 || btc.Tip != 30 {
50 t.Fatalf("coverage %+v", btc)
51 }
52 if st.Blocks != 3 {
53 t.Fatalf("blocks %d", st.Blocks)
54 }
55 }
56
57 // TestManifestEndpoint: an instance that publishes a manifest serves it at
58 // /v1/manifest and one that does not says so.
59 func TestManifestEndpoint(t *testing.T) {
60 idx, _ := build(t)
61 h := service.Handler(idx)
62 w := httptest.NewRecorder()
63 h.ServeHTTP(w, httptest.NewRequest("GET", "/v1/manifest", nil))
64 if w.Code != 404 {
65 t.Fatalf("an index with no manifest answered %d", w.Code)
66 }
67 }
68
69 // TestRangeAndChunk: a range names its chunks, and each name serves the bytes
70 // that hash to it.
71 func TestRangeAndChunk(t *testing.T) {
72 idx, blocks := build(t)
73 h := service.Handler(idx)
74 w := httptest.NewRecorder()
75 h.ServeHTTP(w, httptest.NewRequest("GET", "/v1/chains/btc/blk/5-25", nil))
76 if w.Code != 200 {
77 t.Fatalf("range status %d: %s", w.Code, w.Body)
78 }
79 var listed struct {
80 Chunks []string `json:"chunks"`
81 }
82 if err := json.Unmarshal(w.Body.Bytes(), &listed); err != nil {
83 t.Fatal(err)
84 }
85 // 5..25 is inside chunks 0-10, 10-20, 20-30: three chunks.
86 if len(listed.Chunks) != 3 {
87 t.Fatalf("%d chunks for 5-25", len(listed.Chunks))
88 }
89 for i, name := range listed.Chunks {
90 w2 := httptest.NewRecorder()
91 h.ServeHTTP(w2, httptest.NewRequest("GET", "/v1/chunks/"+name, nil))
92 if w2.Code != 200 {
93 t.Fatalf("chunk %d status %d", i, w2.Code)
94 }
95 if got := chunk.Name(w2.Body.Bytes()).String(); got != name {
96 t.Fatalf("chunk %d served bytes that hash to %s", i, got)
97 }
98 }
99 // A range the instance does not hold is refused, not guessed at.
100 w3 := httptest.NewRecorder()
101 h.ServeHTTP(w3, httptest.NewRequest("GET", "/v1/chains/btc/blk/0-40", nil))
102 if w3.Code == 200 {
103 t.Fatal("a range past the coverage was served")
104 }
105 // A chunk nobody filed is not served.
106 w4 := httptest.NewRecorder()
107 h.ServeHTTP(w4, httptest.NewRequest("GET", "/v1/chunks/0000000000000000000000000000000000000000000000000000000000000000", nil))
108 if w4.Code != 404 {
109 t.Fatalf("an unknown chunk gave %d", w4.Code)
110 }
111 _ = blocks
112 }
113