// SPDX-License-Identifier: GPL-2.0-or-later // Copyright (C) 2026 Bryan Joshua Pedini package main import ( "database/sql" "slices" "strings" "testing" "time" ) var specRoutes = []string{ "GET /account session account_read", "PUT /account/password session password_change", "GET /zones session zone_list", "POST /zones admin zone_create", "GET /zones/{zone} zone zone_read", "DELETE /zones/{zone} admin zone_delete", "GET /zones/{zone}/records zone record_list", "POST /zones/{zone}/records zone record_create", "PUT /zones/{zone}/records/{id} zone record_update", "DELETE /zones/{zone}/records/{id} zone record_delete", "POST /zones/{zone}/records/delete zone record_bulk_delete", "PUT /zones/{zone}/soa zone soa_update", "GET /zones/{zone}/check zone zone_check", "GET /check session check_all", "GET /zones/{zone}/export zone zone_export", "POST /zones/{zone}/import/preview zone zone_import_preview", "POST /zones/{zone}/import/apply zone zone_import", "GET /zones/{zone}/users admin delegation_list", "PUT /zones/{zone}/users/{username} admin delegation_grant", "DELETE /zones/{zone}/users/{username} admin delegation_revoke", "GET /users admin user_list", "POST /users admin user_create", "GET /users/{username} admin user_read", "PUT /users/{username}/role admin user_role", "PUT /users/{username}/password admin user_password", "DELETE /users/{username} admin user_delete", "GET /audit session audit_read", } func TestRouteTableMatchesSpec(t *testing.T) { var got []string seen := map[string]bool{} for _, r := range apiRoutes { got = append(got, r.Method+" "+r.Path+" "+r.Access+" "+r.Operation) if seen[r.Operation] { t.Errorf("operation %s mapped twice", r.Operation) } seen[r.Operation] = true want := int64(defaultBodyLimit) if r.Operation == opZoneImport || r.Operation == opZoneImportPreview { want = importBodyLimit } if r.BodyLimit != want { t.Errorf("%s body limit %d", r.Operation, r.BodyLimit) } } if !slices.Equal(got, specRoutes) { t.Fatalf("route table differs:\n%s", strings.Join(got, "\n")) } if defaultBodyLimit != 65536 || importBodyLimit != 2097152 { t.Fatal("body limits") } } func TestOperationTable(t *testing.T) { reads := []string{"account_read", "zone_list", "zone_read", "record_list", "zone_check", "check_all", "zone_export", "zone_import_preview", "delegation_list", "user_list", "user_read", "audit_read"} changes := []string{"login", "logout", "password_change", "zone_create", "zone_delete", "record_create", "record_update", "record_delete", "record_bulk_delete", "soa_update", "zone_import", "delegation_grant", "delegation_revoke", "user_create", "user_role", "user_password", "user_delete"} gotReads, gotChanges := operationsOfKind(auditKindRead), operationsOfKind(auditKindChange) slices.Sort(gotReads) slices.Sort(gotChanges) slices.Sort(reads) slices.Sort(changes) if !slices.Equal(gotReads, reads) || !slices.Equal(gotChanges, changes) { t.Fatal(gotReads, gotChanges) } if len(auditChangeOperations) != len(changes) { t.Fatal(auditChangeOperations) } for _, op := range append(reads, changes...) { if !knownOperation(op) || len(op) > auditMaxOperationLen { t.Errorf("%s", op) } } if knownOperation("drop_table") || knownOperation("") { t.Fatal("unknown operation accepted") } for _, r := range apiRoutes { if !knownOperation(r.Operation) { t.Errorf("route operation %s unknown", r.Operation) } } for _, op := range zoneOperations { if !knownOperation(op) { t.Errorf("zone operation %s unknown", op) } } if len(zoneOperations) != 15 { t.Fatal(len(zoneOperations)) } } func TestPageTable(t *testing.T) { want := []string{ "GET /login none", "POST /login none", "POST /logout session", "GET / session", "GET /zones/{zone} zone", "GET /zones/{zone}/import zone", "GET /audit session", "GET /account session", "GET /users admin", "GET /users/{username} admin", "GET /version none", "GET /health none", "GET /static/{hash}/... none", } var got []string for _, r := range pageRoutes { got = append(got, r.Method+" "+r.Path+" "+r.Access) } if !slices.Equal(got, want) { t.Fatal(got) } if !routeHasZone("/zones/{zone}/import") || routeHasZone("/users/{username}") { t.Fatal("routeHasZone") } } func TestAPIAuditEntry(t *testing.T) { p := principal{User: userRow{ID: 4, Username: "alice", Role: roleUser}} e := apiAuditEntry(webNow, p, "192.0.2.1", opZoneRead, testZone) if !e.Time.Equal(webNow) || e.UserID != (sql.NullInt64{Int64: 4, Valid: true}) || e.Actor != "alice" || e.Origin != "192.0.2.1" || e.Operation != opZoneRead || e.Zone.String != testZone || !e.Zone.Valid { t.Fatalf("%+v", e) } if e := apiAuditEntry(webNow, p, "o", opAccountRead, ""); e.Zone.Valid || e.Status.Valid || e.Before != nil { t.Fatalf("%+v", e) } finishAuditEntry(&e, 422, "invalid ttl") if e.Status.Int64 != 422 || e.Error != (sql.NullString{String: "invalid ttl", Valid: true}) { t.Fatalf("%+v", e) } finishAuditEntry(&e, 399, "ignored") if e.Status.Int64 != 399 || e.Error.Valid { t.Fatalf("%+v", e) } } func TestLoginAuditEntry(t *testing.T) { e := loginAuditEntry(webNow, opLogin, "evil\x00\nname"+strings.Repeat("ü", 80), nil, "198.51.100.1") if e.UserID.Valid || e.Actor != "evil name"+strings.Repeat("ü", 54) || len([]rune(e.Actor)) != 64 || e.Origin != "198.51.100.1" || e.Operation != opLogin { t.Fatalf("%+v", e) } u := &userRow{ID: 9, Username: "bob"} if e := loginAuditEntry(webNow, opLogin, "bob", u, "o"); e.UserID.Int64 != 9 || !e.UserID.Valid || e.Actor != "bob" { t.Fatalf("%+v", e) } } func TestCLIAuditEntry(t *testing.T) { created := time.Date(2026, 1, 2, 3, 4, 5, 6000, time.UTC) u := userRow{ID: 3, Username: "bob", Role: roleUser, PasswordHash: "$argon2id$secret", CreatedAt: created} after := u after.Role = roleAdmin e := cliAuditEntry(webNow, opUserRole, u, &u, &after) if e.UserID.Valid || e.Actor != "cli" || e.Origin != "local" || e.Status.Valid || e.Error.Valid || e.TargetUserID.Int64 != 3 || e.Operation != opUserRole { t.Fatalf("%+v", e) } if string(e.Before) != `{"id":3,"username":"bob","role":"user","created_at":"2026-01-02T03:04:05.000006Z","deleted_at":null}` || string(e.After) != `{"id":3,"username":"bob","role":"admin","created_at":"2026-01-02T03:04:05.000006Z","deleted_at":null}` { t.Fatal(string(e.Before), string(e.After)) } if e := cliAuditEntry(webNow, opUserCreate, u, nil, &u); e.Before != nil || e.After == nil { t.Fatal("create entry") } if strings.Contains(string(e.Before)+string(e.After), "secret") { t.Fatal("hash in audit") } } func TestSnapshotBuilders(t *testing.T) { r := recordRow{ID: 5, Zone: testZone, Name: "www", TTL: sql.NullInt64{Int64: 300, Valid: true}, Content: sql.NullString{String: `{"ip":"192.0.2.1"}`, Valid: true}, RecordType: "A"} if got := string(auditRecordJSON(r)); got != `{"id":5,"zone":"example.org.","name":"www","ttl":300,"record_type":"A","content":"{\"ip\":\"192.0.2.1\"}"}` { t.Fatal(got) } broken := recordRow{ID: 6, Zone: testZone, Name: "", RecordType: "a"} if got := string(auditRecordsJSON([]recordRow{broken})); got != `[{"id":6,"zone":"example.org.","name":"","ttl":null,"record_type":"a","content":null}]` { t.Fatal(got) } if got := string(auditRecordsJSON(nil)); got != `[]` { t.Fatal(got) } if got := string(auditDelegationJSON(testZone, "bob")); got != `{"zone":"example.org.","username":"bob"}` { t.Fatal(got) } if got := string(auditDelegationsJSON([]delegationRow{{Zone: testZone, Username: "a", UserID: 1}, {Zone: testZone, Username: "b"}})); got != `[{"zone":"example.org.","username":"a"},{"zone":"example.org.","username":"b"}]` { t.Fatal(got) } deleted := userRow{ID: 1, Username: "x", Role: roleUser, CreatedAt: webNow, DeletedAt: sql.NullTime{Time: webNow.Add(time.Microsecond), Valid: true}} if got := string(auditUserJSON(deleted)); got != `{"id":1,"username":"x","role":"user","created_at":"2026-11-18T12:00:00.000000Z","deleted_at":"2026-11-18T12:00:00.000001Z"}` { t.Fatal(got) } if formatAuditNullTime(sql.NullTime{}) != nil { t.Fatal("null time") } } func TestAuditVisibility(t *testing.T) { admin := principal{User: userRow{ID: 1, Role: roleAdmin}} user := principal{User: userRow{ID: 2, Role: roleUser}} delegated := map[string]bool{testZone: true} own := auditEntry{UserID: sql.NullInt64{Int64: 2, Valid: true}, Zone: sql.NullString{String: otherZone, Valid: true}} zoneRow := auditEntry{UserID: sql.NullInt64{Int64: 3, Valid: true}, Zone: sql.NullString{String: testZone, Valid: true}} foreign := auditEntry{UserID: sql.NullInt64{Int64: 3, Valid: true}, Zone: sql.NullString{String: otherZone, Valid: true}} cli := auditEntry{Actor: "cli"} for _, e := range []auditEntry{own, zoneRow, foreign, cli} { if !auditVisibleTo(admin, e, nil) { t.Fatal("admin sees everything") } } if !auditVisibleTo(user, own, delegated) || !auditVisibleTo(user, zoneRow, delegated) { t.Fatal("user rows hidden") } if auditVisibleTo(user, foreign, delegated) || auditVisibleTo(user, cli, delegated) || auditVisibleTo(user, zoneRow, nil) { t.Fatal("foreign rows visible") } }