forked from mtusi2026/mtusi2026lab7
Prepare verified laboratory 7 implementation and evidence
This commit is contained in:
@@ -0,0 +1,113 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"backend/internal/config"
|
||||
"context"
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestSharedLoginLimitAndRehashCASInRealPostgres(t *testing.T) {
|
||||
if os.Getenv("RUN_SECURITY_DB_TESTS") != "1" {
|
||||
t.Skip("explicit isolated PostgreSQL required")
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
|
||||
defer cancel()
|
||||
dsn, e := config.ReadSecret("/run/secrets/database_url")
|
||||
if e != nil {
|
||||
t.Fatal("database file unavailable")
|
||||
}
|
||||
operator, e := pgxpool.New(ctx, dsn)
|
||||
if e != nil {
|
||||
t.Fatal("operator database unavailable")
|
||||
}
|
||||
defer operator.Close()
|
||||
schema := "security_" + strings.ReplaceAll(uuid.NewString(), "-", "")
|
||||
if _, e = operator.Exec(ctx, "CREATE SCHEMA "+schema); e != nil {
|
||||
t.Fatal("isolated schema creation failed")
|
||||
}
|
||||
defer func() {
|
||||
c, stop := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer stop()
|
||||
if _, e := operator.Exec(c, "DROP SCHEMA "+schema+" CASCADE"); e != nil {
|
||||
t.Error("isolated schema cleanup failed")
|
||||
}
|
||||
}()
|
||||
pc, e := pgxpool.ParseConfig(dsn)
|
||||
if e != nil {
|
||||
t.Fatal("database config unavailable")
|
||||
}
|
||||
pc.MaxConns = 8
|
||||
pc.ConnConfig.RuntimeParams["search_path"] = schema
|
||||
pool, e := pgxpool.NewWithConfig(ctx, pc)
|
||||
if e != nil {
|
||||
t.Fatal("isolated pool unavailable")
|
||||
}
|
||||
defer pool.Close()
|
||||
for _, q := range []string{"CREATE TABLE users(id uuid PRIMARY KEY,username text UNIQUE,password_hash text)", "CREATE TABLE login_limits(bucket smallint PRIMARY KEY,started_at timestamptz NOT NULL DEFAULT 'epoch',attempts smallint NOT NULL DEFAULT 0)", "INSERT INTO login_limits(bucket) SELECT generate_series(0,4095)::smallint"} {
|
||||
if _, e = pool.Exec(ctx, q); e != nil {
|
||||
t.Fatal("isolated fixture creation failed")
|
||||
}
|
||||
}
|
||||
a, b := New(pool, nil), New(pool, nil)
|
||||
var accepted atomic.Int32
|
||||
var failures atomic.Int32
|
||||
var group sync.WaitGroup
|
||||
for i := 0; i < 20; i++ {
|
||||
group.Add(1)
|
||||
s := a
|
||||
if i%2 == 1 {
|
||||
s = b
|
||||
}
|
||||
go func() {
|
||||
defer group.Done()
|
||||
ok, e := s.AllowLogin(ctx, "synthetic_shared")
|
||||
if e != nil {
|
||||
failures.Add(1)
|
||||
}
|
||||
if ok {
|
||||
accepted.Add(1)
|
||||
}
|
||||
}()
|
||||
}
|
||||
group.Wait()
|
||||
if failures.Load() != 0 || accepted.Load() != 10 {
|
||||
t.Fatal("concurrent replicas bypassed atomic ten-attempt limit")
|
||||
}
|
||||
var total, rows int
|
||||
if e = pool.QueryRow(ctx, "SELECT sum(attempts),count(*) FROM login_limits").Scan(&total, &rows); e != nil || total != 10 || rows != 4096 {
|
||||
t.Fatal("shared limiter persistence/cardinality incorrect")
|
||||
}
|
||||
id := uuid.New()
|
||||
if _, e = pool.Exec(ctx, "INSERT INTO users VALUES($1,'legacy','old')", id); e != nil {
|
||||
t.Fatal("user fixture creation failed")
|
||||
}
|
||||
accepted.Store(0)
|
||||
for i := 0; i < 2; i++ {
|
||||
group.Add(1)
|
||||
go func() {
|
||||
defer group.Done()
|
||||
ok, e := a.RehashPassword(ctx, id, "old", "new")
|
||||
if e != nil {
|
||||
failures.Add(1)
|
||||
}
|
||||
if ok {
|
||||
accepted.Add(1)
|
||||
}
|
||||
}()
|
||||
}
|
||||
group.Wait()
|
||||
if failures.Load() != 0 || accepted.Load() != 1 {
|
||||
t.Fatal("rehash CAS allowed duplicate overwrite")
|
||||
}
|
||||
got, hash, e := b.Credentials(ctx, "legacy")
|
||||
if e != nil || got != id || hash != "new" {
|
||||
t.Fatal("CAS replacement was not persisted/read by second replica")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user