package service_test import ( "encoding/json" "net/http/httptest" "testing" "git.smesh.lol/btcd-lite/internal/chunk" "git.smesh.lol/btcd-lite/internal/service" ) // build fills an index with three block chunks and two filter chunks. func build(t *testing.T) (*service.Index, []chunk.Chunk) { t.Helper() idx := service.NewIndex() var blocks []chunk.Chunk for h := uint32(0); h < 30; h += 10 { blocks = append(blocks, chunk.Chunk{Kind: chunk.Blocks, Chain: "btc", From: h, To: h + 10, Raw: []byte{byte(h), byte(h + 1)}}) } if err := idx.AddRun(chunk.Blocks, "btc", 0, 30, blocks); err != nil { t.Fatal(err) } filters := []chunk.Chunk{ {Kind: chunk.Filters, Chain: "btc", From: 0, To: 15, Raw: []byte("f0")}, {Kind: chunk.Filters, Chain: "btc", From: 15, To: 30, Raw: []byte("f1")}, } if err := idx.AddRun(chunk.Filters, "btc", 0, 30, filters); err != nil { t.Fatal(err) } return idx, blocks } // TestStatus: the status document names the chain and its coverage, and the // block count. func TestStatus(t *testing.T) { idx, _ := build(t) h := service.Handler(idx) w := httptest.NewRecorder() h.ServeHTTP(w, httptest.NewRequest("GET", "/v1/status", nil)) if w.Code != 200 { t.Fatalf("status %d", w.Code) } var st service.Status if err := json.Unmarshal(w.Body.Bytes(), &st); err != nil { t.Fatal(err) } btc := st.Chains["btc"] if btc.Start != 0 || btc.Tip != 30 { t.Fatalf("coverage %+v", btc) } if st.Blocks != 3 { t.Fatalf("blocks %d", st.Blocks) } } // TestManifestEndpoint: an instance that publishes a manifest serves it at // /v1/manifest and one that does not says so. func TestManifestEndpoint(t *testing.T) { idx, _ := build(t) h := service.Handler(idx) w := httptest.NewRecorder() h.ServeHTTP(w, httptest.NewRequest("GET", "/v1/manifest", nil)) if w.Code != 404 { t.Fatalf("an index with no manifest answered %d", w.Code) } } // TestRangeAndChunk: a range names its chunks, and each name serves the bytes // that hash to it. func TestRangeAndChunk(t *testing.T) { idx, blocks := build(t) h := service.Handler(idx) w := httptest.NewRecorder() h.ServeHTTP(w, httptest.NewRequest("GET", "/v1/chains/btc/blk/5-25", nil)) if w.Code != 200 { t.Fatalf("range status %d: %s", w.Code, w.Body) } var listed struct { Chunks []string `json:"chunks"` } if err := json.Unmarshal(w.Body.Bytes(), &listed); err != nil { t.Fatal(err) } // 5..25 is inside chunks 0-10, 10-20, 20-30: three chunks. if len(listed.Chunks) != 3 { t.Fatalf("%d chunks for 5-25", len(listed.Chunks)) } for i, name := range listed.Chunks { w2 := httptest.NewRecorder() h.ServeHTTP(w2, httptest.NewRequest("GET", "/v1/chunks/"+name, nil)) if w2.Code != 200 { t.Fatalf("chunk %d status %d", i, w2.Code) } if got := chunk.Name(w2.Body.Bytes()).String(); got != name { t.Fatalf("chunk %d served bytes that hash to %s", i, got) } } // A range the instance does not hold is refused, not guessed at. w3 := httptest.NewRecorder() h.ServeHTTP(w3, httptest.NewRequest("GET", "/v1/chains/btc/blk/0-40", nil)) if w3.Code == 200 { t.Fatal("a range past the coverage was served") } // A chunk nobody filed is not served. w4 := httptest.NewRecorder() h.ServeHTTP(w4, httptest.NewRequest("GET", "/v1/chunks/0000000000000000000000000000000000000000000000000000000000000000", nil)) if w4.Code != 404 { t.Fatalf("an unknown chunk gave %d", w4.Code) } _ = blocks }