Every time in UTC, and every day as YYYY-MM-DD (closes #313)
check / check (push) Waiting to run

AbuseIPDB's daily count keeps its day as a civil.Date, taken from the UTC
clock and written to reputation.json as "YYYY-MM-DD"; it was a time.Time
at midnight. Every time read from a state file is converted to UTC, so a
time an admin writes with an offset is written back in UTC.
TestMainRunsDiscriminator runs sww discriminator with TZ 14 hours ahead
of UTC and checks that the process line, the request line and
clients.json are in UTC. SPEC.md states the rule near its start;
AGENTS.md makes it an iron rule.

Model: opus-5-5
This commit is contained in:
sneak committed 2026-11-11 01:20:44 +00:00
1 parent ad36dd1775
commit 2bdecf2f34
11 files changed
+256 -22

No files matched your search

+51 -3
View File
@@ -9,10 +9,14 @@ import (
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"syscall"
"testing"
"time"
// The time zone database, so that the process TestMainRunsDiscriminator
// starts finds the zone TZ names on any host.
_ "time/tzdata"
"sneak.berlin/go/smallwebwaf/internal/discriminator"
"sneak.berlin/go/smallwebwaf/internal/sww"
@@ -37,6 +41,14 @@ func TestMain(m *testing.M) {
func TestMainRunsDiscriminator(t *testing.T) {
t.Parallel()
// Kiritimati is 14 hours ahead of UTC; every time sww gives stays in UTC.
const zone = "Pacific/Kiritimati"
_, err := time.LoadLocation(zone)
if err != nil {
t.Fatalf("load %s: %v", zone, err)
}
binary, err := os.Executable()
if err != nil {
t.Fatalf("find the test binary: %v", err)
@@ -45,14 +57,16 @@ func TestMainRunsDiscriminator(t *testing.T) {
ctx, cancel := context.WithTimeout(t.Context(), 3*waitLimit)
defer cancel()
dir := t.TempDir()
out := &output{}
cmd := exec.CommandContext(ctx, binary) //nolint:gosec // this test binary
cmd.Stdout = out
cmd.Env = append(os.Environ(),
"TZ="+zone,
commandForMain+"="+discriminatorCommand,
listenAddr+"="+localhost+":0",
stateDir+"="+t.TempDir(),
stateDir+"="+dir,
rulesDir+"="+t.TempDir(),
lookupSource+"=off")
@@ -66,9 +80,11 @@ func TestMainRunsDiscriminator(t *testing.T) {
_ = cmd.Wait()
})
addr, _ := out.line(t, "msg", "starting")["address"].(string)
starting := out.line(t, "msg", "starting")
addr, _ := starting["address"].(string)
ask(t, addr, "/", http.StatusOK, discriminator.DispositionAllow)
// The files are written as sww stops.
err = cmd.Process.Signal(syscall.SIGTERM)
if err != nil {
t.Fatalf("signal: %v", err)
@@ -76,7 +92,39 @@ func TestMainRunsDiscriminator(t *testing.T) {
err = cmd.Wait()
if err != nil {
t.Errorf("exit: %v; output:\n%s", err, out.text())
t.Fatalf("exit: %v; output:\n%s", err, out.text())
}
wantUTC(t, "the process line", starting["time"])
wantUTC(t, "the request line", out.line(t, "type", "request")["time"])
path := filepath.Join(dir, "clients.json")
clients, err := os.ReadFile(path) //nolint:gosec // a file in the test's directory
if err != nil {
t.Fatalf("read clients.json: %v", err)
}
stamp := regexp.MustCompile(`"\d{4}-\d\d-\d\dT[^"]*"`)
times := stamp.FindAllString(string(clients), -1)
if len(times) == 0 {
t.Errorf("clients.json holds no time:\n%s", clients)
}
for _, stored := range times {
wantUTC(t, "clients.json", strings.Trim(stored, `"`))
}
}
// wantUTC checks that value, a time read from the log line or file that
// where names, is in UTC.
func wantUTC(t *testing.T, where string, value any) {
t.Helper()
text, _ := value.(string)
if !strings.HasSuffix(text, "Z") {
t.Errorf("%s gives the time %v, want one in UTC", where, value)
}
}