test: require review fix assertions
- test: require review fix assertions - test: cover zone repair and imported dns records - test: wait for dns and tighten assertions - fix(api): drop the record id of a rolled back record creation - test(audit): run every api route through the real handlers in an audit matrix - test(api): run the authorization matrix through the real zone handlers - test: repair duplicate apex soa through ui
This commit is contained in:
1 parent
a884219512
commit
9251c85fd8
14 files changed
+1631
-123
No files matched your search
@@ -0,0 +1,322 @@
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
// Copyright (C) 2026 Bryan Joshua Pedini
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"database/sql/driver"
|
||||
"errors"
|
||||
"maps"
|
||||
"slices"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
type matrixFault struct {
|
||||
match []string
|
||||
err error
|
||||
result *fdbResponse
|
||||
times int
|
||||
}
|
||||
|
||||
type matrixWorld struct {
|
||||
state fakeWebState
|
||||
records []recordRow
|
||||
nextID int64
|
||||
}
|
||||
|
||||
type matrixSQL struct {
|
||||
t *testing.T
|
||||
store *fakeWebStore
|
||||
mu sync.Mutex
|
||||
records []recordRow
|
||||
nextID int64
|
||||
working *matrixWorld
|
||||
fdb *fdbState
|
||||
faults []*matrixFault
|
||||
unknown []string
|
||||
}
|
||||
|
||||
func newMatrixSQL(t *testing.T, store *fakeWebStore) *matrixSQL {
|
||||
return &matrixSQL{t: t, store: store}
|
||||
}
|
||||
|
||||
func (m *matrixSQL) open(t *testing.T) *sql.DB {
|
||||
db, state := newFakeDB(t, m.query)
|
||||
m.fdb = state
|
||||
state.onBegin = m.begin
|
||||
state.onCommit = m.commit
|
||||
state.onEnd = m.end
|
||||
return db
|
||||
}
|
||||
|
||||
func (m *matrixSQL) fail(err error, times int, match ...string) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
m.faults = append(m.faults, &matrixFault{match: match, err: err, times: times})
|
||||
}
|
||||
|
||||
func (m *matrixSQL) answer(result fdbResponse, match ...string) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
m.faults = append(m.faults, &matrixFault{match: match, result: &result, times: -1})
|
||||
}
|
||||
|
||||
func (m *matrixSQL) addRecord(r recordRow) recordRow {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
if r.ID == 0 {
|
||||
m.nextID++
|
||||
r.ID = m.nextID
|
||||
}
|
||||
m.nextID = max(m.nextID, r.ID)
|
||||
m.records = append(m.records, r)
|
||||
m.store.mu.Lock()
|
||||
defer m.store.mu.Unlock()
|
||||
matrixSyncZones(&m.store.state, m.records)
|
||||
return r
|
||||
}
|
||||
|
||||
func (m *matrixSQL) rows() []recordRow {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
return slices.Clone(m.records)
|
||||
}
|
||||
|
||||
func (m *matrixSQL) begin() {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
m.store.mu.Lock()
|
||||
defer m.store.mu.Unlock()
|
||||
m.working = &matrixWorld{state: m.store.state.clone(), records: slices.Clone(m.records), nextID: m.nextID}
|
||||
}
|
||||
|
||||
func (m *matrixSQL) commit() error {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
if m.working == nil {
|
||||
return errors.New("commit without transaction")
|
||||
}
|
||||
m.store.mu.Lock()
|
||||
defer m.store.mu.Unlock()
|
||||
m.records, m.nextID = m.working.records, m.working.nextID
|
||||
m.store.state = m.working.state
|
||||
matrixSyncZones(&m.store.state, m.records)
|
||||
m.store.commits++
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *matrixSQL) end() {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
m.working = nil
|
||||
}
|
||||
|
||||
func matrixSyncZones(state *fakeWebState, records []recordRow) {
|
||||
state.zones = map[string]bool{}
|
||||
for _, r := range records {
|
||||
state.zones[zoneAPITestKey(r.Zone)] = true
|
||||
}
|
||||
}
|
||||
|
||||
func matrixMatches(query string, parts []string) bool {
|
||||
for _, p := range parts {
|
||||
if !strings.Contains(query, p) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (m *matrixSQL) query(query string, args []any) fdbResponse {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
for _, f := range m.faults {
|
||||
if f.times != 0 && matrixMatches(query, f.match) {
|
||||
if f.times > 0 {
|
||||
f.times--
|
||||
}
|
||||
if f.result != nil {
|
||||
return *f.result
|
||||
}
|
||||
return fdbResponse{err: f.err}
|
||||
}
|
||||
}
|
||||
m.store.mu.Lock()
|
||||
defer m.store.mu.Unlock()
|
||||
w := m.working
|
||||
if w == nil {
|
||||
w = &matrixWorld{state: m.store.state, records: m.records, nextID: m.nextID}
|
||||
defer func() {
|
||||
if m.working == nil {
|
||||
m.records, m.nextID = w.records, w.nextID
|
||||
m.store.state = w.state
|
||||
}
|
||||
}()
|
||||
}
|
||||
return m.exec(w, query, args)
|
||||
}
|
||||
|
||||
func (m *matrixSQL) userValues(u userRow) []driver.Value {
|
||||
var lastLogin, deleted driver.Value
|
||||
if u.LastLoginAt.Valid {
|
||||
lastLogin = u.LastLoginAt.Time
|
||||
}
|
||||
if u.DeletedAt.Valid {
|
||||
deleted = u.DeletedAt.Time
|
||||
}
|
||||
return []driver.Value{u.ID, u.Username, u.Role, u.PasswordHash, u.CreatedAt, u.UpdatedAt, lastLogin, deleted}
|
||||
}
|
||||
|
||||
func (m *matrixSQL) exec(w *matrixWorld, query string, args []any) fdbResponse {
|
||||
has := func(v string) bool { return strings.Contains(query, v) }
|
||||
switch {
|
||||
case has("GET_LOCK"), has("RELEASE_LOCK"):
|
||||
return fdbResponse{rows: fdbRow(int64(1))}
|
||||
case has("FROM `coredns_webui_users` u WHERE u.`id` = ?"):
|
||||
id := args[0].(int64)
|
||||
for _, u := range w.state.users {
|
||||
if u.ID == id {
|
||||
return fdbResponse{rows: fdbRow(m.userValues(u)...)}
|
||||
}
|
||||
}
|
||||
return fdbResponse{}
|
||||
case has("SELECT 1 FROM `coredns_webui_zone_delegations` WHERE `zone` = ? AND `user_id` = ?"):
|
||||
if _, ok := w.state.delegations[delegationKey{args[0].(string), args[1].(int64)}]; ok {
|
||||
return fdbResponse{rows: fdbRow(int64(1))}
|
||||
}
|
||||
return fdbResponse{}
|
||||
case has("GROUP BY `zone_key`"):
|
||||
return m.zoneSummaries(w, has("d.`user_id` = ?"), args)
|
||||
case has("SELECT `id`") && has(zoneRowsWhere):
|
||||
var rows [][]driver.Value
|
||||
for _, r := range w.records {
|
||||
if zoneAPITestKey(r.Zone) == args[0] {
|
||||
rows = append(rows, zoneAPITestValues(r))
|
||||
}
|
||||
}
|
||||
return fdbResponse{rows: rows}
|
||||
case has("SELECT `id`") && has("WHERE `id` = ?"):
|
||||
for _, r := range w.records {
|
||||
if r.ID == args[0] {
|
||||
return fdbResponse{rows: fdbRow(zoneAPITestValues(r)...)}
|
||||
}
|
||||
}
|
||||
return fdbResponse{}
|
||||
case has("FOR UPDATE OF d"):
|
||||
var out []delegationRow
|
||||
for k, d := range w.state.delegations {
|
||||
if k.zone == args[0] {
|
||||
d.Username = w.state.users[k.userID-1].Username
|
||||
out = append(out, d)
|
||||
}
|
||||
}
|
||||
slices.SortFunc(out, func(a, b delegationRow) int { return strings.Compare(a.Username, b.Username) })
|
||||
var rows [][]driver.Value
|
||||
for _, d := range out {
|
||||
rows = append(rows, []driver.Value{d.Zone, d.UserID, d.Username, d.CreatedAt, d.CreatedBy})
|
||||
}
|
||||
return fdbResponse{rows: rows}
|
||||
case has("DELETE FROM `coredns_webui_zone_delegations` WHERE `zone` = ?"):
|
||||
var n int64
|
||||
for k := range w.state.delegations {
|
||||
if k.zone == args[0] {
|
||||
delete(w.state.delegations, k)
|
||||
n++
|
||||
}
|
||||
}
|
||||
return fdbResponse{rowsAffected: n}
|
||||
case has("INSERT INTO `coredns_records`"):
|
||||
w.nextID++
|
||||
w.records = append(w.records, recordRow{
|
||||
ID: w.nextID, Zone: args[0].(string), Name: args[1].(string), TTL: matrixNullInt(args[2]),
|
||||
Content: matrixNullString(args[3]), RecordType: args[4].(string),
|
||||
})
|
||||
return fdbResponse{lastInsertID: w.nextID, rowsAffected: 1}
|
||||
case has("UPDATE `coredns_records`"):
|
||||
i := slices.IndexFunc(w.records, func(r recordRow) bool { return r.ID == args[5] })
|
||||
if i < 0 {
|
||||
return fdbResponse{}
|
||||
}
|
||||
w.records[i] = recordRow{
|
||||
ID: args[5].(int64), Zone: args[0].(string), Name: args[1].(string), TTL: matrixNullInt(args[2]),
|
||||
Content: matrixNullString(args[3]), RecordType: args[4].(string),
|
||||
}
|
||||
return fdbResponse{rowsAffected: 1}
|
||||
case has("DELETE FROM `coredns_records` WHERE `id` = ?"):
|
||||
n := len(w.records)
|
||||
w.records = slices.DeleteFunc(w.records, func(r recordRow) bool { return r.ID == args[0] })
|
||||
return fdbResponse{rowsAffected: int64(n - len(w.records))}
|
||||
case has("DELETE FROM `coredns_records`" + zoneRowsWhere):
|
||||
n := len(w.records)
|
||||
w.records = slices.DeleteFunc(w.records, func(r recordRow) bool { return zoneAPITestKey(r.Zone) == args[0] })
|
||||
return fdbResponse{rowsAffected: int64(n - len(w.records))}
|
||||
case has("INSERT INTO `coredns_webui_audit`"):
|
||||
e := auditEntry{
|
||||
Time: args[0].(time.Time), UserID: matrixNullInt(args[1]), Actor: args[2].(string), Origin: args[3].(string),
|
||||
Operation: args[4].(string), Status: matrixNullInt(args[5]), Error: matrixNullString(args[6]),
|
||||
Zone: matrixNullString(args[7]), RecordID: matrixNullInt(args[8]), TargetUserID: matrixNullInt(args[9]),
|
||||
Before: matrixBytes(args[10]), After: matrixBytes(args[11]),
|
||||
}
|
||||
e.ID = int64(len(w.state.audit) + 1)
|
||||
w.state.audit = append(w.state.audit, fakeAuditInsert{entry: e, inTx: m.working != nil})
|
||||
return fdbResponse{lastInsertID: e.ID, rowsAffected: 1}
|
||||
}
|
||||
m.unknown = append(m.unknown, query)
|
||||
return fdbResponse{err: errors.New("matrix backend: unexpected SQL")}
|
||||
}
|
||||
|
||||
func (m *matrixSQL) zoneSummaries(w *matrixWorld, ofUser bool, args []any) fdbResponse {
|
||||
counts := map[string]int64{}
|
||||
soa := map[string]recordRow{}
|
||||
for _, r := range w.records {
|
||||
key := zoneAPITestKey(r.Zone)
|
||||
if ofUser {
|
||||
if _, ok := w.state.delegations[delegationKey{key, args[0].(int64)}]; !ok {
|
||||
continue
|
||||
}
|
||||
}
|
||||
counts[key]++
|
||||
if r.RecordType == "SOA" {
|
||||
if cur, ok := soa[key]; !ok || (cur.Name != "" && r.Name == "") {
|
||||
soa[key] = r
|
||||
}
|
||||
}
|
||||
}
|
||||
var rows [][]driver.Value
|
||||
for _, key := range slices.Sorted(maps.Keys(counts)) {
|
||||
var content driver.Value
|
||||
if r, ok := soa[key]; ok && r.Content.Valid {
|
||||
content = r.Content.String
|
||||
}
|
||||
rows = append(rows, []driver.Value{key, counts[key], content})
|
||||
}
|
||||
return fdbResponse{rows: rows}
|
||||
}
|
||||
|
||||
func matrixNullInt(v any) sql.NullInt64 {
|
||||
if n, ok := v.(int64); ok {
|
||||
return sql.NullInt64{Int64: n, Valid: true}
|
||||
}
|
||||
return sql.NullInt64{}
|
||||
}
|
||||
|
||||
func matrixNullString(v any) sql.NullString {
|
||||
if s, ok := v.(string); ok {
|
||||
return sql.NullString{String: s, Valid: true}
|
||||
}
|
||||
return sql.NullString{}
|
||||
}
|
||||
|
||||
func matrixBytes(v any) []byte {
|
||||
switch x := v.(type) {
|
||||
case string:
|
||||
return []byte(x)
|
||||
case []byte:
|
||||
return x
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in new issue
Block a user