aboutsummaryrefslogtreecommitdiff
path: root/internal/api/server_hardening_test.go
blob: 97eae0e265aadc8904477ed4d70f206056f675db (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package api

import (
	"encoding/json"
	"github.com/Fuwn/plutia/internal/config"
	"github.com/Fuwn/plutia/internal/storage"
	"github.com/Fuwn/plutia/internal/types"
	"io"
	"net/http"
	"net/http/httptest"
	"strings"
	"testing"
	"time"
)

func TestResolveRateLimitPerIP(t *testing.T) {
	store, err := storage.OpenPebble(t.TempDir())

	if err != nil {
		t.Fatalf("open pebble: %v", err)
	}

	defer store.Close()

	if err := store.PutState(types.StateV1{Version: 1, DID: "did:plc:alice", DIDDocument: []byte(`{"id":"did:plc:alice"}`), ChainTipHash: "tip", LatestOpSeq: 1, UpdatedAt: time.Now().UTC()}); err != nil {
		t.Fatalf("put state: %v", err)
	}

	cfg := config.Default()
	cfg.RequestTimeout = 10 * time.Second
	cfg.RateLimit.ResolveRPS = 1
	cfg.RateLimit.ResolveBurst = 1
	cfg.RateLimit.ProofRPS = 1
	cfg.RateLimit.ProofBurst = 1
	h := NewServer(cfg, store, nil, nil).Handler()
	req1 := httptest.NewRequest(http.MethodGet, "/did/did:plc:alice", nil)
	req1.RemoteAddr = "203.0.113.7:12345"
	rr1 := httptest.NewRecorder()

	h.ServeHTTP(rr1, req1)

	if rr1.Code != http.StatusOK {
		t.Fatalf("first request status: got %d want %d", rr1.Code, http.StatusOK)
	}

	req2 := httptest.NewRequest(http.MethodGet, "/did/did:plc:alice", nil)
	req2.RemoteAddr = "203.0.113.7:12345"
	rr2 := httptest.NewRecorder()

	h.ServeHTTP(rr2, req2)

	if rr2.Code != http.StatusTooManyRequests {
		t.Fatalf("second request status: got %d want %d", rr2.Code, http.StatusTooManyRequests)
	}
}

func TestStatusIncludesBuildInfo(t *testing.T) {
	store, err := storage.OpenPebble(t.TempDir())

	if err != nil {
		t.Fatalf("open pebble: %v", err)
	}

	defer store.Close()

	h := NewServer(config.Default(), store, nil, nil, WithBuildInfo(BuildInfo{
		Version:   "v0.1.0",
		Commit:    "abc123",
		BuildDate: "2026-02-26T00:00:00Z",
		GoVersion: "go1.test",
	})).Handler()
	req := httptest.NewRequest(http.MethodGet, "/status", nil)
	rr := httptest.NewRecorder()

	h.ServeHTTP(rr, req)

	if rr.Code != http.StatusOK {
		t.Fatalf("status code: got %d want %d", rr.Code, http.StatusOK)
	}

	var payload map[string]any

	if err := json.Unmarshal(rr.Body.Bytes(), &payload); err != nil {
		t.Fatalf("decode status: %v", err)
	}

	build, ok := payload["build"].(map[string]any)

	if !ok {
		t.Fatalf("missing build section: %v", payload)
	}

	if got := build["version"]; got != "v0.1.0" {
		t.Fatalf("unexpected build version: %v", got)
	}

	if got := build["commit"]; got != "abc123" {
		t.Fatalf("unexpected build commit: %v", got)
	}
}

func TestMetricsExposeRequiredSeries(t *testing.T) {
	store, err := storage.OpenPebble(t.TempDir())

	if err != nil {
		t.Fatalf("open pebble: %v", err)
	}

	defer store.Close()

	h := NewServer(config.Default(), store, nil, nil).Handler()
	req := httptest.NewRequest(http.MethodGet, "/metrics", nil)
	rr := httptest.NewRecorder()

	h.ServeHTTP(rr, req)

	if rr.Code != http.StatusOK {
		t.Fatalf("metrics status: got %d want %d", rr.Code, http.StatusOK)
	}

	body, _ := io.ReadAll(rr.Body)
	text := string(body)

	for _, metric := range []string{
		"ingest_ops_total",
		"ingest_ops_per_second",
		"ingest_lag_ops",
		"verify_failures_total",
		"checkpoint_duration_seconds",
		"checkpoint_sequence",
		"disk_bytes_total",
		"did_count",
		"thin_cache_hits_total",
		"thin_cache_misses_total",
		"thin_cache_entries",
		"thin_cache_evictions_total",
	} {
		if !strings.Contains(text, metric) {
			t.Fatalf("metrics output missing %q", metric)
		}
	}
}