first commit

This commit is contained in:
libroot 2026-11-19 04:40:59 +02:00
commit 8c4dcae475
Signed by: libroot
GPG key ID: 2AC68C029EBE9ACE
89 changed files with 24623 additions and 0 deletions

126
tests/probes_test.go Normal file
View file

@ -0,0 +1,126 @@
package main
// probes_test.go - the instruments that catch what reading the code cannot:
// data races, the parameter space behind each route, and header injection.
// Each of these exists because a review that only read the source missed
// something a probe found in seconds.
import (
"fmt"
"net/http"
"net/http/httptest"
"os"
"sort"
"strings"
"sync"
"testing"
"time"
)
// Concurrency: the archive claims to be read-only while serving. Hammer the
// handlers from many goroutines with -race and see.
func TestProbeConcurrent(t *testing.T) {
if os.Getenv("SNOWCHIVE_BENCH_DATA") == "" {
buildFixture(t)
}
paths := []string{"/", "/browse", "/browse/program", "/browse/tag?sort=count",
"/search?q=prism&do_search=1", "/explore/ids", "/api/facets", "/glossary",
"/doc/prism-slides", "/articles", "/sitemap.xml"}
mux, _ := buildFixture(t)
var wg sync.WaitGroup
for i := 0; i < 24; i++ {
wg.Add(1)
go func(n int) {
defer wg.Done()
for j := 0; j < 20; j++ {
p := paths[(n+j)%len(paths)]
r := httptest.NewRequest("GET", p, nil)
if j%3 == 0 {
r.AddCookie(&http.Cookie{Name: "pref_theme", Value: "dark"})
}
w := httptest.NewRecorder()
mux.ServeHTTP(w, r)
if w.Code != 200 {
t.Errorf("%s: %d", p, w.Code)
return
}
}
}(i)
}
wg.Wait()
}
// Input space: every route crossed with the parameters it accepts. The
// 80 ms page hid behind ?sort=count - a path-only sweep never saw it.
// Input space: every route crossed with the parameters it accepts. The
// 80 ms page hid behind ?sort=count - a path-only sweep never saw it.
func TestProbeParamSweep(t *testing.T) {
buildFixture(t)
base := []string{"/", "/browse", "/browse/program", "/browse/tag", "/browse/docdate",
"/browse/program/PRISM", "/browse/docdate/2013", "/search", "/explore/ids",
"/explore/terms", "/explore/pages", "/explore/outlets", "/articles",
"/api/docs", "/api/search", "/glossary", "/pinned", "/preferences"}
params := []string{"", "?sort=count", "?sort=count&order=asc", "?sort=zzz", "?order=desc",
"?view=table", "?view=card", "?p=2", "?p=-1", "?p=99999999999999999999",
"?per_page=all", "?per_page=0", "?per_page=-5", "?type=REPORTED", "?type=NOPE",
"?coll=news", "?coll=news&coll=foia", "?missing=1", "?q=prism&do_search=1",
"?q=%22a+b%22~5&do_search=1", "?q=-prism&do_search=1", "?hist=foia,sidtoday-archive",
"?date_doc_start=2013&date_doc_end=2013", "?fqv=x&fqf=ZZ&fqc=xx&do_search=1",
"?sort=name&order=desc&p=1&type=FOIA&coll=foia"}
type slow struct {
path string
d time.Duration
}
var slows []slow
bad := 0
for _, b := range base {
for _, p := range params {
u := b + p
start := time.Now()
w := get(t, u)
d := time.Since(start)
if w.Code != 200 && w.Code != 400 && w.Code != 404 {
bad++
t.Errorf("%s: status %d", u, w.Code)
}
slows = append(slows, slow{u, d})
}
}
sort.Slice(slows, func(i, j int) bool { return slows[i].d > slows[j].d })
fmt.Printf("%d combinations, %d unexpected statuses; slowest on the fixture:\n", len(slows), bad)
for _, s := range slows[:5] {
fmt.Printf(" %8s %s\n", s.d.Round(time.Microsecond), s.path)
}
}
// Header injection: the Onion-Location header echoes the request URI.
// Header injection: the Onion-Location header echoes the request URI.
func TestProbeHeaderInjection(t *testing.T) {
buildFixture(t)
for _, raw := range []string{
"/browse?x=1%0d%0aX-Injected:+yes",
"/browse?x=%0aSet-Cookie:+a=b",
"/doc/" + strings.Repeat("a", 200),
} {
r := httptest.NewRequest("GET", raw, nil)
w := httptest.NewRecorder()
mux, _ := buildFixture(t)
mux.ServeHTTP(w, r)
h := w.Header()
if h.Get("X-Injected") != "" || h.Get("Set-Cookie") != "" {
t.Errorf("%s: header injected: %v", raw, h)
}
for k, vs := range h {
for _, v := range vs {
if strings.ContainsAny(v, "\r\n") {
t.Errorf("%s: header %s carries a newline: %q", raw, k, v)
}
}
}
}
}
// Pathological documents: the biggest thing in the corpus is what breaks
// first. What IS the biggest thing?