5 Commits
Author SHA1 Message Date
clawbot ba79d6de02 Stop allowing credentialed cross-origin requests (closes #261)
check / check (push) Failing after 3s
CORS() allowed every origin with credentials, so a page on another origin of the same site could read what the signed-in user sees.

CORS() now uses the CONVENTIONS.md template's options (AllowedOrigins "*", AllowCredentials false) and covers only the two public, read-only routes, GET /api/v1/server and GET /.well-known/healthcheck.json. Every other route sends no Access-Control-Allow-* header. A preflight to the public routes gets 405 like any other path.

Deviation: CORS() is not in the global middleware chain CONVENTIONS.md lists, because REPO_POLICIES.md forbids * on authenticated routes.

Model: opus-5-5
2026-11-11 01:22:38 +01:00
clawbot ed5125606a Stop the slow test packages waiting (closes #263)
check / check (push) Failing after 1m9s
The slowest test packages spent their time waiting rather than testing.

db.Params gains an optional PasswordCost (zero means bcrypt.DefaultCost), and the db, ircserver and handlers tests hash at bcrypt.MinCost. A test IRC server's relay checks its queue on broker notification instead of polling. The service and handlers tests build config.Config directly and run in parallel; config exports DefaultTrustedProxies and ParseTrustedProxies for them. Fixed sleeps give way to waits for the event. Production defaults are unchanged.

What still takes time is CPU work under -race or a timeout a test is about.

Model: opus-5-5
2026-11-11 01:15:43 +01:00
clawbot 847f5c6fec Let ircserver and db tests pass under heavy host load (closes #265)
check / check (push) Failing after 4s
Tests in internal/ircserver and internal/db failed under heavy host load and passed on a rerun.

testTimeout, the failure limit on every read and wait in internal/ircserver, goes from 5 to 30 seconds; passing tests take no longer. TestQuietClientNotAnsweringPingIsClosed gets its own 4-second ping interval and measures its upper bound from the end of registration, a second before a two-interval read deadline would close the connection, leaving room for scheduling delay. endlessQueryTimeout in internal/db goes from 100 ms to 2 seconds, so the cancelled-query tests interrupt a running statement.

Judgement call: the closed test no longer checks PING timing; TestQuietClientAnsweringPingStaysConnected still does.

Model: opus-5-5
2026-11-11 01:07:47 +01:00
sneak 142f0a1c4b Merge branch 'main' into next
check / check (push) Failing after 3s
2026-11-11 00:49:06 +01:00
sneak 12a232ee1b next → main: utility commands, IRC listener and audit fixes, build fixes (#111)
check / check (push) Canceled after 0s
Reviewed-on: #111
2026-10-22 18:27:05 +02:00
19 changed files with 567 additions and 190 deletions

No files matched your search

+10 -8
View File
@@ -168,8 +168,10 @@ either.
- Request bodies for `POST /api/v1/session`, `POST /api/v1/login` and - Request bodies for `POST /api/v1/session`, `POST /api/v1/login` and
`POST /api/v1/messages` are limited to `MAX_MESSAGE_SIZE` bytes (default `POST /api/v1/messages` are limited to `MAX_MESSAGE_SIZE` bytes (default
`4096`). A larger body gets `400` `invalid request body`. `4096`). A larger body gets `400` `invalid request body`.
- The server allows cross-origin requests from any origin, with credentials. The - `GET /api/v1/server` and `GET /.well-known/healthcheck.json` answer a request
cookie is `SameSite=Strict`, so a browser sends it only from the same site. from any origin with `Access-Control-Allow-Origin: *`. Every other route sends
no `Access-Control-Allow-*` header, so a page on another origin cannot read
its responses.
### The auth cookie ### The auth cookie
@@ -1893,14 +1895,14 @@ The IRC listener has no TLS.
seconds. A request still running after 60 seconds is cancelled, and the seconds. A request still running after 60 seconds is cancelled, and the
connection closes without a response. Headers may be up to 1 MiB. connection closes without a response. Headers may be up to 1 MiB.
- A long poll waits at most 30 seconds. - A long poll waits at most 30 seconds.
- Every response to a well-formed request, except to a cross-origin preflight, - Every response to a well-formed request carries a `Content-Security-Policy`
carries a `Content-Security-Policy` header: header:
`default-src 'self'; script-src 'self'; style-src 'self'; connect-src 'self'; img-src 'self'; font-src 'self'; object-src 'none'; frame-ancestors 'none'; base-uri 'self'; form-action 'self'`. `default-src 'self'; script-src 'self'; style-src 'self'; connect-src 'self'; img-src 'self'; font-src 'self'; object-src 'none'; frame-ancestors 'none'; base-uri 'self'; form-action 'self'`.
The web client works within it. The web client works within it.
- Cross-origin requests are allowed from any origin, with credentials. The - `GET /api/v1/server` and `GET /.well-known/healthcheck.json` answer a request
allowed methods are `GET`, `POST`, `PUT`, `DELETE` and `OPTIONS`. The allowed from any origin with `Access-Control-Allow-Origin: *`. Every other route sends
request headers are `Accept`, `Content-Type` and `X-CSRF-Token`. The cookie's no `Access-Control-Allow-*` header, so a page on another origin cannot read
`SameSite=Strict` still keeps it to the same site. its responses.
### Logging ### Logging
+24 -18
View File
@@ -36,9 +36,9 @@ Type /help for available commands.`
const DefaultDBURL = "file:///var/lib/neoirc/state.db" + const DefaultDBURL = "file:///var/lib/neoirc/state.db" +
"?_pragma=journal_mode(WAL)&_pragma=busy_timeout(5000)" "?_pragma=journal_mode(WAL)&_pragma=busy_timeout(5000)"
// defaultTrustedProxies is TRUSTED_PROXIES when it is not set: the // DefaultTrustedProxies is TRUSTED_PROXIES when it is not set: the
// private address ranges of RFC 1918. // private address ranges of RFC 1918.
const defaultTrustedProxies = "10.0.0.0/8,172.16.0.0/12,192.168.0.0/16" const DefaultTrustedProxies = "10.0.0.0/8,172.16.0.0/12,192.168.0.0/16"
var errOutOfRange = errors.New("setting out of range") var errOutOfRange = errors.New("setting out of range")
@@ -117,7 +117,7 @@ func New(
viper.SetDefault("LOGIN_RATE_LIMIT", "1") viper.SetDefault("LOGIN_RATE_LIMIT", "1")
viper.SetDefault("LOGIN_RATE_BURST", "5") viper.SetDefault("LOGIN_RATE_BURST", "5")
viper.SetDefault("IRC_LISTEN_ADDR", ":6667") viper.SetDefault("IRC_LISTEN_ADDR", ":6667")
viper.SetDefault("TRUSTED_PROXIES", defaultTrustedProxies) viper.SetDefault("TRUSTED_PROXIES", DefaultTrustedProxies)
err := viper.ReadInConfig() err := viper.ReadInConfig()
if err != nil { if err != nil {
@@ -219,9 +219,7 @@ func parseStringAllowEmptyEnv(key string, value *string) error {
return parseString(key, value) return parseString(key, value)
} }
// parseTrustedProxies sets *value to the addresses in TRUSTED_PROXIES, a // parseTrustedProxies sets *value to the addresses in TRUSTED_PROXIES.
// comma-separated list of networks such as 10.0.0.0/8 and single
// addresses such as 192.0.2.1. The empty string trusts nothing.
func parseTrustedProxies(value **netipx.IPSet) error { func parseTrustedProxies(value **netipx.IPSet) error {
var raw string var raw string
@@ -230,18 +228,7 @@ func parseTrustedProxies(value **netipx.IPSet) error {
return err return err
} }
var builder netipx.IPSetBuilder ipSet, err := ParseTrustedProxies(raw)
if strings.TrimSpace(raw) != "" {
for entry := range strings.SplitSeq(raw, ",") {
err = addTrustedProxy(&builder, strings.TrimSpace(entry))
if err != nil {
return fmt.Errorf("cannot parse TRUSTED_PROXIES: %w", err)
}
}
}
ipSet, err := builder.IPSet()
if err != nil { if err != nil {
return fmt.Errorf("cannot parse TRUSTED_PROXIES: %w", err) return fmt.Errorf("cannot parse TRUSTED_PROXIES: %w", err)
} }
@@ -251,6 +238,25 @@ func parseTrustedProxies(value **netipx.IPSet) error {
return nil return nil
} }
// ParseTrustedProxies returns the addresses in list, which has the form
// of TRUSTED_PROXIES: a comma-separated list of networks such as
// 10.0.0.0/8 and single addresses such as 192.0.2.1. The empty string
// trusts nothing.
func ParseTrustedProxies(list string) (*netipx.IPSet, error) {
var builder netipx.IPSetBuilder
if strings.TrimSpace(list) != "" {
for entry := range strings.SplitSeq(list, ",") {
err := addTrustedProxy(&builder, strings.TrimSpace(entry))
if err != nil {
return nil, err
}
}
}
return builder.IPSet()
}
// addTrustedProxy adds entry, a network with a slash or a single // addTrustedProxy adds entry, a network with a slash or a single
// address, to builder. An IPv4 address written in IPv6 form, such as // address, to builder. An IPv4 address written in IPv6 form, such as
// ::ffff:192.0.2.1, is added as IPv4. A network in that form and an // ::ffff:192.0.2.1, is added as IPv4. A network in that form and an
+1 -1
View File
@@ -22,7 +22,7 @@ func (database *Database) SetPassword(
password string, password string,
) error { ) error {
hash, err := bcrypt.GenerateFromPassword( hash, err := bcrypt.GenerateFromPassword(
[]byte(password), bcrypt.DefaultCost, []byte(password), database.passwordCost,
) )
if err != nil { if err != nil {
return fmt.Errorf("hash password: %w", err) return fmt.Errorf("hash password: %w", err)
+13
View File
@@ -15,6 +15,7 @@ import (
"strings" "strings"
"go.uber.org/fx" "go.uber.org/fx"
"golang.org/x/crypto/bcrypt"
"sneak.berlin/go/neoirc/internal/config" "sneak.berlin/go/neoirc/internal/config"
"sneak.berlin/go/neoirc/internal/logger" "sneak.berlin/go/neoirc/internal/logger"
@@ -39,6 +40,11 @@ type Params struct {
Logger *logger.Logger Logger *logger.Logger
Config *config.Config Config *config.Config
// PasswordCost is the bcrypt cost at which SetPassword hashes
// a password. The server leaves it unset, which means
// bcrypt.DefaultCost. Tests set a lower cost to save time.
PasswordCost int `optional:"true"`
} }
// Database manages the SQLite connection and migrations. // Database manages the SQLite connection and migrations.
@@ -46,6 +52,7 @@ type Database struct {
conn *sql.DB conn *sql.DB
log *slog.Logger log *slog.Logger
params *Params params *Params
passwordCost int
} }
// New creates a new Database and registers lifecycle hooks. // New creates a new Database and registers lifecycle hooks.
@@ -53,9 +60,15 @@ func New(
lifecycle fx.Lifecycle, lifecycle fx.Lifecycle,
params Params, params Params,
) (*Database, error) { ) (*Database, error) {
passwordCost := params.PasswordCost
if passwordCost == 0 {
passwordCost = bcrypt.DefaultCost
}
database := &Database{ //nolint:exhaustruct // conn set in OnStart database := &Database{ //nolint:exhaustruct // conn set in OnStart
params: &params, params: &params,
log: params.Logger.Get(), log: params.Logger.Get(),
passwordCost: passwordCost,
} }
database.log.Info("Database instantiated") database.log.Info("Database instantiated")
+80
View File
@@ -10,6 +10,7 @@ import (
"time" "time"
"go.uber.org/fx/fxtest" "go.uber.org/fx/fxtest"
"golang.org/x/crypto/bcrypt"
"sneak.berlin/go/neoirc/internal/config" "sneak.berlin/go/neoirc/internal/config"
"sneak.berlin/go/neoirc/internal/db" "sneak.berlin/go/neoirc/internal/db"
"sneak.berlin/go/neoirc/internal/dbtest" "sneak.berlin/go/neoirc/internal/dbtest"
@@ -240,6 +241,85 @@ func TestDBURLSettingForeignKeysFailsStartup(t *testing.T) {
} }
} }
// TestPasswordCost checks the bcrypt cost of the hash that SetPassword
// stores: bcrypt.DefaultCost when db.Params leaves PasswordCost unset,
// as the server does, and PasswordCost when it is set.
func TestPasswordCost(t *testing.T) {
t.Parallel()
tests := []struct {
name string
passwordCost int
wantCost int
}{
{name: "unset", wantCost: bcrypt.DefaultCost},
{
name: "bcrypt.MinCost",
passwordCost: bcrypt.MinCost,
wantCost: bcrypt.MinCost,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
lifecycle := fxtest.NewLifecycle(t)
log, err := logger.New(lifecycle, logger.Params{
Globals: &globals.Globals{},
})
if err != nil {
t.Fatal(err)
}
database, err := db.New(lifecycle, db.Params{
Logger: log,
Config: &config.Config{DBURL: dbtest.NewURL(t)},
PasswordCost: test.passwordCost,
})
if err != nil {
t.Fatal(err)
}
lifecycle.RequireStart()
t.Cleanup(lifecycle.RequireStop)
sessionID, _, _, err := database.CreateSession(
t.Context(), "costuser", "", "", "",
)
if err != nil {
t.Fatal(err)
}
err = database.SetPassword(t.Context(), sessionID, "password123")
if err != nil {
t.Fatal(err)
}
var hash string
err = database.GetDB().QueryRowContext(
t.Context(),
"SELECT password_hash FROM sessions WHERE id = ?",
sessionID,
).Scan(&hash)
if err != nil {
t.Fatal(err)
}
cost, err := bcrypt.Cost([]byte(hash))
if err != nil {
t.Fatal(err)
}
if cost != test.wantCost {
t.Errorf("cost = %d, want %d", cost, test.wantCost)
}
})
}
}
// startDatabase starts a Database for dbURL the way the server does. // startDatabase starts a Database for dbURL the way the server does.
// It returns the error that stopped it from starting, if any. // It returns the error that stopped it from starting, if any.
func startDatabase(t *testing.T, dbURL string) (*db.Database, error) { func startDatabase(t *testing.T, dbURL string) (*db.Database, error) {
+4 -1
View File
@@ -8,10 +8,12 @@ import (
"testing" "testing"
"time" "time"
"golang.org/x/crypto/bcrypt"
"sneak.berlin/go/neoirc/internal/dbtest" "sneak.berlin/go/neoirc/internal/dbtest"
) )
// NewTestDatabase creates an in-memory database for testing. // NewTestDatabase creates an in-memory database for testing. It
// hashes passwords at bcrypt.MinCost.
func NewTestDatabase(tb testing.TB) (*Database, error) { func NewTestDatabase(tb testing.TB) (*Database, error) {
tb.Helper() tb.Helper()
@@ -25,6 +27,7 @@ func NewTestDatabase(tb testing.TB) (*Database, error) {
database := &Database{ //nolint:exhaustruct // test helper, params not needed database := &Database{ //nolint:exhaustruct // test helper, params not needed
conn: conn, conn: conn,
log: slog.Default(), log: slog.Default(),
passwordCost: bcrypt.MinCost,
} }
err = database.runMigrations(context.Background()) err = database.runMigrations(context.Background())
+144 -82
View File
@@ -145,34 +145,46 @@ type testServer struct {
handlers *handlers.Handlers handlers *handlers.Handlers
} }
// viperMu makes tests start their servers one at a time, const (
// because config.New writes the global viper instance and // maxMessageSize is MAX_MESSAGE_SIZE on every test server, the
// the server reads it as it starts. // setting's default.
// maxMessageSize = 4096
//nolint:gochecknoglobals // guards a global, so must be one
var viperMu sync.Mutex // loginRateLimit is LOGIN_RATE_LIMIT on every test server, the
// setting's default.
loginRateLimit = 1
// loginRateBurst is LOGIN_RATE_BURST on every test server, the
// setting's default.
loginRateBurst = 5
)
func newTestServer( func newTestServer(
t *testing.T, t *testing.T,
) *testServer { ) *testServer {
t.Helper() t.Helper()
return newTestServerWith(t, 0, "", "") return newTestServerWith(t, 0, "", "", config.DefaultTrustedProxies)
} }
func newTestServerWith( func newTestServerWith(
t *testing.T, t *testing.T,
hashcashBits int, hashcashBits int,
operName, operPassword string, operName, operPassword, trustedProxies string,
) *testServer { ) *testServer {
t.Helper() t.Helper()
viperMu.Lock()
defer viperMu.Unlock()
dbURL := dbtest.NewURL(t) dbURL := dbtest.NewURL(t)
copyTemplateDB(t, dbURL) copyTemplateDB(t, dbURL)
trusted, err := config.ParseTrustedProxies(trustedProxies)
if err != nil {
t.Fatalf("parse trusted proxies: %v", err)
}
// Each duration setting defaults to 30 days.
const thirtyDays = "720h"
var ( var (
srv *server.Server srv *server.Server
brk *broker.Broker brk *broker.Broker
@@ -184,31 +196,25 @@ func newTestServerWith(
fx.Provide( fx.Provide(
newTestGlobals, newTestGlobals,
logger.New, logger.New,
func( // Built here rather than by config.New, which writes
lifecycle fx.Lifecycle, // the global viper instance, so servers start in
globs *globals.Globals, // parallel.
log *logger.Logger, func() *config.Config {
) (*config.Config, error) { return &config.Config{
cfg, err := config.New( DBURL: dbURL,
lifecycle, config.Params{ //nolint:exhaustruct
Globals: globs, Logger: log,
},
)
if err != nil {
return nil, fmt.Errorf(
"test config: %w", err,
)
}
cfg.DBURL = dbURL
cfg.Port = 0
// One line: each line is a database write per new session. // One line: each line is a database write per new session.
cfg.MOTD = "neoirc test server" MOTD: "neoirc test server",
cfg.HashcashBits = hashcashBits MaxMessageSize: maxMessageSize,
cfg.OperName = operName HashcashBits: hashcashBits,
cfg.OperPassword = operPassword OperName: operName,
OperPassword: operPassword,
return cfg, nil MessageMaxAge: thirtyDays,
QueueMaxAge: thirtyDays,
SessionIdleTimeout: thirtyDays,
LoginRateLimit: loginRateLimit,
LoginRateBurst: loginRateBurst,
TrustedProxies: trusted,
}
}, },
newTestDB, newTestDB,
broker.New, broker.New,
@@ -255,7 +261,7 @@ func newTestDB(
cfg *config.Config, cfg *config.Config,
) (*db.Database, error) { ) (*db.Database, error) {
database, err := db.New(lifecycle, db.Params{ //nolint:exhaustruct database, err := db.New(lifecycle, db.Params{ //nolint:exhaustruct
Logger: log, Config: cfg, Logger: log, Config: cfg, PasswordCost: bcrypt.MinCost,
}) })
if err != nil { if err != nil {
return nil, fmt.Errorf("test db: %w", err) return nil, fmt.Errorf("test db: %w", err)
@@ -830,10 +836,7 @@ func (tserver *testServer) postBody(
func TestRequestBodyLimit(t *testing.T) { func TestRequestBodyLimit(t *testing.T) {
t.Parallel() t.Parallel()
const ( const nick = "alice_bodylimit"
maxMessageSize = 4096 // the default MAX_MESSAGE_SIZE
nick = "alice_bodylimit"
)
endpoints := []struct { endpoints := []struct {
name, path, value string name, path, value string
@@ -1750,18 +1753,34 @@ func TestConcurrentQuitsSendOneQuit(t *testing.T) {
// that the second session then holds one QUIT from the // that the second session then holds one QUIT from the
// first, and that the first session is deleted: its tokens // first, and that the first session is deleted: its tokens
// are refused and its nick is free. It does all this // are refused and its nick is free. It does all this
// quitRaceTrials times, each on a new server, because each // quitRaceTrials times, at once, each on a new server, because
// trial logs in and logins are rate limited. // each trial logs in and logins are rate limited.
func checkQuitRace( func checkQuitRace(
t *testing.T, t *testing.T,
leave func(tserver *testServer, tokens []string), leave func(tserver *testServer, tokens []string),
) { ) {
t.Helper() t.Helper()
for trial := range quitRaceTrials {
t.Run(fmt.Sprintf("trial %d", trial), func(t *testing.T) {
t.Parallel()
checkQuitRaceOnce(t, newTestServer(t), leave)
})
}
}
// checkQuitRaceOnce runs one trial of checkQuitRace on
// tserver.
func checkQuitRaceOnce(
t *testing.T,
tserver *testServer,
leave func(tserver *testServer, tokens []string),
) {
t.Helper()
const nick = "racingquitter" const nick = "racingquitter"
for trial := range quitRaceTrials {
tserver := newTestServer(t)
firstToken := tserver.createSession(nick) firstToken := tserver.createSession(nick)
observerToken := tserver.createSession("raceobserver") observerToken := tserver.createSession("raceobserver")
@@ -1783,19 +1802,18 @@ func checkQuitRace(
quits := countQuits(msgs, nick) quits := countQuits(msgs, nick)
if quits != 1 { if quits != 1 {
t.Errorf("trial %d: %d QUITs, want 1: %v", trial, quits, msgs) t.Errorf("%d QUITs, want 1: %v", quits, msgs)
} }
for _, token := range tokens { for _, token := range tokens {
status, _ := tserver.getState(token) status, _ := tserver.getState(token)
if status != http.StatusUnauthorized { if status != http.StatusUnauthorized {
t.Errorf("trial %d: state got %d, want 401", trial, status) t.Errorf("state got %d, want 401", status)
} }
} }
// createSession fails the test if the nick is taken. // createSession fails the test if the nick is taken.
tserver.createSession(nick) tserver.createSession(nick)
}
} }
// TestLogoutOfOneClientKeepsSession checks that when one of // TestLogoutOfOneClientKeepsSession checks that when one of
@@ -1933,11 +1951,28 @@ const (
// return once the session has left. // return once the session has left.
quitWakeDeadline = 5 * time.Second quitWakeDeadline = 5 * time.Second
// pollWaitStep is how often checkQuitWakesPoll checks // pollWaitStep is how often waitForLongPoll checks
// whether the long poll is waiting yet. // whether the long poll is waiting yet.
pollWaitStep = 10 * time.Millisecond pollWaitStep = 10 * time.Millisecond
) )
// waitForLongPoll waits until a long poll of the session
// sessionID is waiting, and fails the test if none is within
// quitWakeDeadline.
func (tserver *testServer) waitForLongPoll(sessionID int64) {
tserver.t.Helper()
deadline := time.Now().Add(quitWakeDeadline)
for tserver.broker.ListenerCount(sessionID) == 0 {
if time.Now().After(deadline) {
tserver.t.Fatal("the long poll did not start waiting")
}
time.Sleep(pollWaitStep)
}
}
// TestQuitWakesPeerLongPoll checks that a session's QUIT // TestQuitWakesPeerLongPoll checks that a session's QUIT
// wakes the long poll of a user who shares a channel with // wakes the long poll of a user who shares a channel with
// it. // it.
@@ -2012,15 +2047,7 @@ func checkQuitWakesPoll(
msgs, pollErr = tserver.longPoll(peerToken, lastID, quitWakePollTimeout) msgs, pollErr = tserver.longPoll(peerToken, lastID, quitWakePollTimeout)
}() }()
deadline := time.Now().Add(quitWakeDeadline) tserver.waitForLongPoll(peerID)
for tserver.broker.ListenerCount(peerID) == 0 {
if time.Now().After(deadline) {
t.Fatal("the long poll did not start waiting")
}
time.Sleep(pollWaitStep)
}
leave(tserver, token) leave(tserver, token)
@@ -2850,7 +2877,12 @@ func TestLongPoll(t *testing.T) {
pollMsgs = result.Messages pollMsgs = result.Messages
}() }()
time.Sleep(200 * time.Millisecond) bobID, err := tserver.database.GetSessionByNick(t.Context(), "lp_bob")
if err != nil {
t.Fatalf("get session of lp_bob: %v", err)
}
tserver.waitForLongPoll(bobID)
tserver.sendCommand(aliceToken, map[string]any{ tserver.sendCommand(aliceToken, map[string]any{
commandKey: privmsgCmd, commandKey: privmsgCmd,
@@ -3009,18 +3041,18 @@ func TestGetMessagesRemovesWaitChannel(t *testing.T) {
longTimeout = 10 longTimeout = 10
// shortTimeout, in seconds, is the timeout of the // shortTimeout, in seconds, is the timeout of the
// requests that wait it out. // request that waits it out.
shortTimeout = 1 shortTimeout = 1
) )
// queued is whether a message is queued before the request, // queued is whether a message is queued before the request,
// whose timeout is in seconds. failingRead is as for // whose timeout is in seconds. failingRead and notified are
// getMessagesFailingRead. wantReads is how many times the // as for getMessagesFailingRead. wantReads is how many times
// handler reads the queue: once if it answers at once, // the handler reads the queue: once if it answers at once,
// twice if it waits first. // twice if it waits first.
tests := []struct { tests := []struct {
name string name string
queued bool queued, notified bool
timeout, failingRead int timeout, failingRead int
wantReads, wantStatus int wantReads, wantStatus int
}{ }{
@@ -3041,9 +3073,9 @@ func TestGetMessagesRemovesWaitChannel(t *testing.T) {
wantReads: 1, wantStatus: http.StatusInternalServerError, wantReads: 1, wantStatus: http.StatusInternalServerError,
}, },
{ {
name: "read after the wait failed", timeout: shortTimeout, name: "read after the wait failed", notified: true,
failingRead: 2, wantReads: 2, timeout: handlers.MaxLongPollTimeout, failingRead: 2,
wantStatus: http.StatusInternalServerError, wantReads: 2, wantStatus: http.StatusInternalServerError,
}, },
} }
@@ -3075,7 +3107,7 @@ func TestGetMessagesRemovesWaitChannel(t *testing.T) {
} }
status, reads := tserver.getMessagesFailingRead( status, reads := tserver.getMessagesFailingRead(
token, lastID, tc.timeout, tc.failingRead, token, sessionID, lastID, tc.timeout, tc.failingRead, tc.notified,
) )
if status != tc.wantStatus { if status != tc.wantStatus {
t.Errorf("status %d, want %d", status, tc.wantStatus) t.Errorf("status %d, want %d", status, tc.wantStatus)
@@ -3092,14 +3124,17 @@ func TestGetMessagesRemovesWaitChannel(t *testing.T) {
} }
// getMessagesFailingRead sends GET /api/v1/messages for the // getMessagesFailingRead sends GET /api/v1/messages for the
// client token with timeout and after afterID. The handler's // client token of the session sessionID, with timeout and
// read of the queue numbered failingRead, counting from 1, // after afterID. The handler's read of the queue numbered
// fails; with 0, none does. It returns the response status // failingRead, counting from 1, fails; with 0, none does.
// and how many times the handler read the queue. // With notified, it notifies the broker once the request
// waits. It returns the response status and how many times
// the handler read the queue.
func (tserver *testServer) getMessagesFailingRead( func (tserver *testServer) getMessagesFailingRead(
token string, token string,
afterID int64, sessionID, afterID int64,
timeout, failingRead int, timeout, failingRead int,
notified bool,
) (int, int) { ) (int, int) {
tserver.t.Helper() tserver.t.Helper()
@@ -3121,20 +3156,45 @@ func (tserver *testServer) getMessagesFailingRead(
) )
}) })
resp, err := doRequestAuth( var (
status int
err error
)
answered := make(chan struct{})
go func() {
defer close(answered)
var resp *http.Response
resp, err = doRequestAuth(
tserver.t, http.MethodGet, tserver.t, http.MethodGet,
tserver.url(fmt.Sprintf( tserver.url(fmt.Sprintf(
"%s?timeout=%d&after=%d", apiMessages, timeout, afterID, "%s?timeout=%d&after=%d", apiMessages, timeout, afterID,
)), )),
token, nil, token, nil,
) )
if err != nil {
return
}
status = resp.StatusCode
_ = resp.Body.Close()
}()
if notified {
tserver.waitForLongPoll(sessionID)
tserver.broker.Notify(sessionID)
}
<-answered
if err != nil { if err != nil {
tserver.t.Fatalf("get messages: %v", err) tserver.t.Fatalf("get messages: %v", err)
} }
_ = resp.Body.Close() return status, reads
return resp.StatusCode, reads
} }
// TestLongPollRemovesWaitChannelWhenClientGivesUp checks that // TestLongPollRemovesWaitChannelWhenClientGivesUp checks that
@@ -4343,12 +4403,11 @@ func TestLoginRateLimitIgnoresForwardedHeaders(t *testing.T) {
} }
func TestLoginRateLimitPerClientBehindTrustedProxy(t *testing.T) { func TestLoginRateLimitPerClientBehindTrustedProxy(t *testing.T) {
// The test client connects from 127.0.0.1. Trusted as a proxy, it t.Parallel()
// passes each client's address in X-Forwarded-For. t.Setenv rules
// out t.Parallel.
t.Setenv("TRUSTED_PROXIES", "127.0.0.1")
tserver := newTestServer(t) // The test client connects from 127.0.0.1. Trusted as a proxy, it
// passes each client's address in X-Forwarded-For.
tserver := newTestServerWith(t, 0, "", "", "127.0.0.1")
// An empty JSON object is refused after the rate limit counts the // An empty JSON object is refused after the rate limit counts the
// attempt, so no attempt checks a password or looks up a hostname. // attempt, so no attempt checks a password or looks up a hostname.
@@ -5079,7 +5138,9 @@ func TestRefusedSessionLeavesStampUnspent(t *testing.T) {
badNick = "bad nick" badNick = "bad nick"
) )
tserver := newTestServerWith(t, bits, "", "") tserver := newTestServerWith(
t, bits, "", "", config.DefaultTrustedProxies,
)
// A server with no SERVER_NAME takes stamps for "neoirc". // A server with no SERVER_NAME takes stamps for "neoirc".
status := postSessionWithStamp( status := postSessionWithStamp(
@@ -5285,6 +5346,7 @@ func newTestServerWithOper(
return newTestServerWith( return newTestServerWith(
t, 0, testOperName, testOperPassword, t, 0, testOperName, testOperPassword,
config.DefaultTrustedProxies,
) )
} }
+4
View File
@@ -6,6 +6,10 @@ import (
"sneak.berlin/go/neoirc/internal/db" "sneak.berlin/go/neoirc/internal/db"
) )
// MaxLongPollTimeout is the longest HandleGetMessages waits, in
// seconds.
const MaxLongPollTimeout = maxLongPollTimeout
// SetPollMessages makes HandleGetMessages read the client // SetPollMessages makes HandleGetMessages read the client
// output queue with poll in place of Database.PollMessages. // output queue with poll in place of Database.PollMessages.
func (hdlr *Handlers) SetPollMessages( func (hdlr *Handlers) SetPollMessages(
+7
View File
@@ -70,6 +70,10 @@ type Conn struct {
closed bool closed bool
killed bool killed bool
// pollInterval is how often the relay checks the client
// output queue when no broker notification arrives.
pollInterval time.Duration
// quitReason is the reason in the QUIT that peers are // quitReason is the reason in the QUIT that peers are
// sent when the connection ends. The client's QUIT // sent when the connection ends. The client's QUIT
// replaces it. // replaces it.
@@ -83,6 +87,7 @@ func newConn(
brk *broker.Broker, brk *broker.Broker,
cfg *config.Config, cfg *config.Config,
svc *service.Service, svc *service.Service,
poll time.Duration,
) *Conn { ) *Conn {
host, _, _ := net.SplitHostPort(tcpConn.RemoteAddr().String()) host, _, _ := net.SplitHostPort(tcpConn.RemoteAddr().String())
@@ -101,6 +106,8 @@ func newConn(
serverSfx: srvName, serverSfx: srvName,
remoteIP: host, remoteIP: host,
quitReason: "Connection closed", quitReason: "Connection closed",
pollInterval: poll,
} }
conn.commands = conn.buildCommandMap() conn.commands = conn.buildCommandMap()
+13 -9
View File
@@ -14,16 +14,16 @@ import (
) )
// NewTestServer creates a Server suitable for testing, whose // NewTestServer creates a Server suitable for testing, whose
// connections use interval and deadline in place of // connections use interval, deadline and poll in place of
// pingInterval and pongDeadline, and resolve in place of // pingInterval, pongDeadline and pollInterval, and resolve in
// resolveHost. // place of resolveHost.
// The caller must call Stop() when finished. // The caller must call Stop() when finished.
func NewTestServer( func NewTestServer(
log *slog.Logger, log *slog.Logger,
cfg *config.Config, cfg *config.Config,
database *db.Database, database *db.Database,
brk *broker.Broker, brk *broker.Broker,
interval, deadline time.Duration, interval, deadline, poll time.Duration,
resolve func(ctx context.Context, addr string) string, resolve func(ctx context.Context, addr string) string,
) *Server { ) *Server {
globs := &globals.Globals{ globs := &globals.Globals{
@@ -46,10 +46,18 @@ func NewTestServer(
pingInterval: interval, pingInterval: interval,
pongDeadline: deadline, pongDeadline: deadline,
pollInterval: poll,
stopTimeout: stopTimeout,
resolveHost: resolve, resolveHost: resolve,
} }
} }
// SetStopTimeout makes Stop wait at most timeout for the
// connections' cleanup.
func (s *Server) SetStopTimeout(timeout time.Duration) {
s.stopTimeout = timeout
}
// ResolveHost is the reverse DNS lookup that a server made by // ResolveHost is the reverse DNS lookup that a server made by
// New does for each client. // New does for each client.
func ResolveHost(ctx context.Context, addr string) string { func ResolveHost(ctx context.Context, addr string) string {
@@ -99,10 +107,6 @@ const PingInterval = pingInterval
// for a line before it closes the connection. // for a line before it closes the connection.
const PongDeadline = pongDeadline const PongDeadline = pongDeadline
// StopTimeout is the longest Stop waits for the connections'
// cleanup.
const StopTimeout = stopTimeout
// NewTestConn returns a Conn for nick on tcpConn, without a // NewTestConn returns a Conn for nick on tcpConn, without a
// server, database or service behind it. // server, database or service behind it.
func NewTestConn( func NewTestConn(
@@ -111,7 +115,7 @@ func NewTestConn(
tcpConn net.Conn, tcpConn net.Conn,
nick string, nick string,
) *Conn { ) *Conn {
conn := newConn(tcpConn, log, nil, nil, cfg, nil) conn := newConn(tcpConn, log, nil, nil, cfg, nil, pollInterval)
conn.nick = nick conn.nick = nick
return conn return conn
-4
View File
@@ -3,7 +3,6 @@ package ircserver_test
import ( import (
"strings" "strings"
"testing" "testing"
"time"
"sneak.berlin/go/neoirc/internal/config" "sneak.berlin/go/neoirc/internal/config"
) )
@@ -1508,9 +1507,6 @@ func TestIntegrationThirdClientObserver(t *testing.T) {
bob.joinAndDrain("#trio") bob.joinAndDrain("#trio")
carol.joinAndDrain("#trio") carol.joinAndDrain("#trio")
// Drain join notifications.
time.Sleep(100 * time.Millisecond)
// alice sends; both bob and carol should receive. // alice sends; both bob and carol should receive.
alice.send("PRIVMSG #trio :hello trio") alice.send("PRIVMSG #trio :hello trio")
+1 -1
View File
@@ -17,7 +17,7 @@ import (
func (c *Conn) relayMessages(ctx context.Context) { func (c *Conn) relayMessages(ctx context.Context) {
// Use a ticker as a fallback; primary wakeup is via // Use a ticker as a fallback; primary wakeup is via
// broker notification. // broker notification.
ticker := time.NewTicker(pollInterval) ticker := time.NewTicker(c.pollInterval)
defer ticker.Stop() defer ticker.Stop()
for { for {
+63 -1
View File
@@ -9,6 +9,7 @@ import (
"time" "time"
"golang.org/x/crypto/bcrypt" "golang.org/x/crypto/bcrypt"
"sneak.berlin/go/neoirc/internal/config"
"sneak.berlin/go/neoirc/internal/dbtest" "sneak.berlin/go/neoirc/internal/dbtest"
"sneak.berlin/go/neoirc/internal/ircserver" "sneak.berlin/go/neoirc/internal/ircserver"
"sneak.berlin/go/neoirc/internal/service" "sneak.berlin/go/neoirc/internal/service"
@@ -58,7 +59,11 @@ func (env *testEnv) queueMessage(
func TestRelayKeepsOneWaitChannel(t *testing.T) { func TestRelayKeepsOneWaitChannel(t *testing.T) {
t.Parallel() t.Parallel()
env := newTestEnv(t) env := newTestEnvWithResolver(
t, &config.Config{ServerName: testServerName},
ircserver.PingInterval, ircserver.PongDeadline,
ircserver.PollInterval, ircserver.ResolveHost,
)
client := env.dial(t) client := env.dial(t)
client.register("idle") client.register("idle")
@@ -119,6 +124,63 @@ func (env *testEnv) checkOneWaitChannel(
} }
} }
// TestRelayDeliversAfterFailedRead checks that when the relay
// cannot read the client output queue, it waits for the next
// notification, and delivers once the queue can be read again.
func TestRelayDeliversAfterFailedRead(t *testing.T) {
t.Parallel()
env := newTestEnv(t)
client := env.dial(t)
client.register("reader")
sessionID, err := env.database.GetSessionByNick(t.Context(), "reader")
if err != nil {
t.Fatalf("get session of reader: %v", err)
}
dbtest.Exec(t, env.dbURL,
"ALTER TABLE client_queues RENAME TO hidden_queues")
// Each pass of the relay takes a wait channel and then reads
// the queue. Once it holds a channel after the second
// notification, the read after the first one has failed.
for range 2 {
env.waitForWaitChannel(t, sessionID)
env.brk.Notify(sessionID)
}
env.waitForWaitChannel(t, sessionID)
dbtest.Exec(t, env.dbURL,
"ALTER TABLE hidden_queues RENAME TO client_queues")
env.queueMessage(
t, "reader", "PRIVMSG", "carol", "reader", []string{"readable"},
)
client.readUntil(func(l string) bool {
return strings.Contains(l, "readable")
})
}
// waitForWaitChannel waits until the session sessionID holds a
// broker wait channel, and fails the test if it does not
// within testTimeout.
func (env *testEnv) waitForWaitChannel(t *testing.T, sessionID int64) {
t.Helper()
deadline := time.Now().Add(testTimeout)
for env.brk.ListenerCount(sessionID) == 0 {
if time.Now().After(deadline) {
t.Fatal("the relay is not waiting for a notification")
}
time.Sleep(relayCheckStep)
}
}
// TestRelayMultiLineBody checks that each line of a PRIVMSG // TestRelayMultiLineBody checks that each line of a PRIVMSG
// or NOTICE body sent over the HTTP API reaches an IRC // or NOTICE body sent over the HTTP API reaches an IRC
// listener user as its own line. // listener user as its own line.
+10 -3
View File
@@ -53,6 +53,11 @@ type Server struct {
pingInterval time.Duration pingInterval time.Duration
pongDeadline time.Duration pongDeadline time.Duration
// pollInterval and stopTimeout are the constants of the
// same names, except in tests, which change them.
pollInterval time.Duration
stopTimeout time.Duration
// resolveHost is the function of the same name, except in // resolveHost is the function of the same name, except in
// tests, which replace it with one that blocks. // tests, which replace it with one that blocks.
resolveHost func(ctx context.Context, addr string) string resolveHost func(ctx context.Context, addr string) string
@@ -79,6 +84,8 @@ func New(
pingInterval: pingInterval, pingInterval: pingInterval,
pongDeadline: pongDeadline, pongDeadline: pongDeadline,
pollInterval: pollInterval,
stopTimeout: stopTimeout,
resolveHost: resolveHost, resolveHost: resolveHost,
} }
@@ -154,10 +161,10 @@ func (s *Server) stop() {
select { select {
case <-done: case <-done:
case <-time.After(stopTimeout): case <-time.After(s.stopTimeout):
s.log.Error( s.log.Error(
"irc connections not cleaned up before stop timeout", "irc connections not cleaned up before stop timeout",
"timeout", stopTimeout, "timeout", s.stopTimeout,
) )
} }
} }
@@ -181,7 +188,7 @@ func (s *Server) acceptLoop(ctx context.Context) {
client := newConn( client := newConn(
tcpConn, s.log, tcpConn, s.log,
s.database, s.brk, s.cfg, s.svc, s.database, s.brk, s.cfg, s.svc, s.pollInterval,
) )
s.mu.Lock() s.mu.Lock()
+24 -11
View File
@@ -18,6 +18,7 @@ import (
"time" "time"
"go.uber.org/fx/fxtest" "go.uber.org/fx/fxtest"
"golang.org/x/crypto/bcrypt"
"sneak.berlin/go/neoirc/internal/broker" "sneak.berlin/go/neoirc/internal/broker"
"sneak.berlin/go/neoirc/internal/config" "sneak.berlin/go/neoirc/internal/config"
"sneak.berlin/go/neoirc/internal/db" "sneak.berlin/go/neoirc/internal/db"
@@ -35,6 +36,12 @@ import (
// enough for a heavily loaded host. // enough for a heavily loaded host.
const testTimeout = 30 * time.Second const testTimeout = 30 * time.Second
// testPollInterval is how often the relay of a test server
// checks the client output queue without a broker
// notification. It is so long that the relay delivers only on
// notification, and a test fails when one is missing.
const testPollInterval = time.Hour
// testEnv holds the shared test infrastructure. // testEnv holds the shared test infrastructure.
type testEnv struct { type testEnv struct {
dbURL string dbURL string
@@ -94,16 +101,19 @@ func newTestEnvWithPingTimes(
t.Helper() t.Helper()
return newTestEnvWithResolver( return newTestEnvWithResolver(
t, cfg, interval, deadline, ircserver.ResolveHost, t, cfg, interval, deadline, testPollInterval,
ircserver.ResolveHost,
) )
} }
// newTestEnvWithResolver is newTestEnvWithPingTimes, except // newTestEnvWithResolver is newTestEnvWithPingTimes, except
// that the server looks up a client's hostname with resolve. // that the relay checks the client output queue every poll
// without a broker notification, and the server looks up a
// client's hostname with resolve.
func newTestEnvWithResolver( func newTestEnvWithResolver(
t *testing.T, t *testing.T,
cfg *config.Config, cfg *config.Config,
interval, deadline time.Duration, interval, deadline, poll time.Duration,
resolve func(ctx context.Context, addr string) string, resolve func(ctx context.Context, addr string) string,
) *testEnv { ) *testEnv {
t.Helper() t.Helper()
@@ -121,6 +131,7 @@ func newTestEnvWithResolver(
database, err := db.New(lifecycle, db.Params{ database, err := db.New(lifecycle, db.Params{
Logger: dbLog, Logger: dbLog,
Config: &config.Config{DBURL: dbURL}, Config: &config.Config{DBURL: dbURL},
PasswordCost: bcrypt.MinCost,
}) })
if err != nil { if err != nil {
t.Fatalf("new database: %v", err) t.Fatalf("new database: %v", err)
@@ -137,7 +148,7 @@ func newTestEnvWithResolver(
)) ))
srv := ircserver.NewTestServer( srv := ircserver.NewTestServer(
log, cfg, database, brk, interval, deadline, resolve, log, cfg, database, brk, interval, deadline, poll, resolve,
) )
// Port 0 makes the system pick a free port. dial reads it // Port 0 makes the system pick a free port. dial reads it
@@ -433,7 +444,8 @@ func TestRegistrationWhileAnotherLookupIsPending(t *testing.T) {
env := newTestEnvWithResolver( env := newTestEnvWithResolver(
t, &config.Config{ServerName: testServerName}, t, &config.Config{ServerName: testServerName},
ircserver.PingInterval, ircserver.PongDeadline, resolve, ircserver.PingInterval, ircserver.PongDeadline,
testPollInterval, resolve,
) )
env.dial(t) env.dial(t)
@@ -1064,12 +1076,16 @@ func TestStopEndsSessions(t *testing.T) {
} }
// TestStopReturnsWhileCleanupIsStuck checks that Stop stops // TestStopReturnsWhileCleanupIsStuck checks that Stop stops
// waiting for a connection's cleanup after StopTimeout, so a // waiting for a connection's cleanup after its stop timeout,
// cleanup that cannot finish does not hold up stopping. // so a cleanup that cannot finish does not hold up stopping.
func TestStopReturnsWhileCleanupIsStuck(t *testing.T) { func TestStopReturnsWhileCleanupIsStuck(t *testing.T) {
t.Parallel() t.Parallel()
const stopTimeout = 100 * time.Millisecond
env := newTestEnv(t) env := newTestEnv(t)
env.srv.SetStopTimeout(stopTimeout)
client := env.dial(t) client := env.dial(t)
client.register("stuck") client.register("stuck")
@@ -1095,7 +1111,7 @@ func TestStopReturnsWhileCleanupIsStuck(t *testing.T) {
select { select {
case <-stopped: case <-stopped:
case <-time.After(ircserver.StopTimeout + testTimeout): case <-time.After(stopTimeout + testTimeout):
t.Error("Stop did not return while a cleanup was stuck") t.Error("Stop did not return while a cleanup was stuck")
} }
@@ -1147,9 +1163,6 @@ func TestDirectMessage(t *testing.T) {
receiver := env.dial(t) receiver := env.dial(t)
receiver.register("dmreceiver") receiver.register("dmreceiver")
// Give relay goroutines time to start.
time.Sleep(100 * time.Millisecond)
sender.send("PRIVMSG dmreceiver :hello privately") sender.send("PRIVMSG dmreceiver :hello privately")
lines := receiver.readUntil(func(line string) bool { lines := receiver.readUntil(func(line string) bool {
+5 -10
View File
@@ -200,24 +200,19 @@ func (mware *Middleware) Logging() func(http.Handler) http.Handler {
} }
} }
// CORS returns middleware that handles Cross-Origin Resource Sharing. // CORS returns middleware that lets a page on any origin read the
// AllowCredentials is true so browsers include cookies in // response. Use it only on public, read-only routes.
// cross-origin API requests.
func (mware *Middleware) CORS() func(http.Handler) http.Handler { func (mware *Middleware) CORS() func(http.Handler) http.Handler {
return cors.Handler(cors.Options{ //nolint:exhaustruct // optional fields return cors.Handler(cors.Options{ //nolint:exhaustruct // optional fields
AllowOriginFunc: func( AllowedOrigins: []string{"*"},
_ *http.Request, _ string,
) bool {
return true
},
AllowedMethods: []string{ AllowedMethods: []string{
"GET", "POST", "PUT", "DELETE", "OPTIONS", "GET", "POST", "PUT", "DELETE", "OPTIONS",
}, },
AllowedHeaders: []string{ AllowedHeaders: []string{
"Accept", "Content-Type", "X-CSRF-Token", "Accept", "Authorization", "Content-Type", "X-CSRF-Token",
}, },
ExposedHeaders: []string{"Link"}, ExposedHeaders: []string{"Link"},
AllowCredentials: true, AllowCredentials: false,
MaxAge: corsMaxAge, MaxAge: corsMaxAge,
}) })
} }
+2 -3
View File
@@ -30,7 +30,6 @@ func (srv *Server) SetupRoutes() {
srv.router.Use(srv.mw.Metrics()) srv.router.Use(srv.mw.Metrics())
} }
srv.router.Use(srv.mw.CORS())
srv.router.Use(srv.mw.CSP()) srv.router.Use(srv.mw.CSP())
srv.router.Use(middleware.Timeout(routeTimeout)) srv.router.Use(middleware.Timeout(routeTimeout))
@@ -45,7 +44,7 @@ func (srv *Server) SetupRoutes() {
} }
// Health check. // Health check.
srv.router.Get( srv.router.With(srv.mw.CORS()).Get(
"/.well-known/healthcheck.json", "/.well-known/healthcheck.json",
srv.handlers.HandleHealthCheck(), srv.handlers.HandleHealthCheck(),
) )
@@ -69,7 +68,7 @@ func (srv *Server) SetupRoutes() {
} }
func (srv *Server) setupAPIv1(router chi.Router) { func (srv *Server) setupAPIv1(router chi.Router) {
router.Get( router.With(srv.mw.CORS()).Get(
"/server", "/server",
srv.handlers.HandleServerInfo(), srv.handlers.HandleServerInfo(),
) )
+84
View File
@@ -6,8 +6,10 @@ package server_test
import ( import (
"fmt" "fmt"
"maps"
"net" "net"
"net/http" "net/http"
"net/http/httptest"
"strconv" "strconv"
"strings" "strings"
"testing" "testing"
@@ -188,6 +190,88 @@ func TestServeAfterStopDoesNotStopApp(t *testing.T) {
} }
} }
// TestCORSHeaders checks the Access-Control-Allow-* headers on the
// answers to requests from another origin. The public, read-only
// routes answer with Access-Control-Allow-Origin: * alone. Routes
// that need the cookie, and a preflight to one, answer with none.
func TestCORSHeaders(t *testing.T) {
var srv *server.Server
startApp(t, fx.Populate(&srv))
tests := []struct {
name string
method string
path string
requestMethod string
wantHeaders map[string]string
}{
{
name: "server information",
method: http.MethodGet,
path: "/api/v1/server",
wantHeaders: map[string]string{"Access-Control-Allow-Origin": "*"},
},
{
name: "health check",
method: http.MethodGet,
path: "/.well-known/healthcheck.json",
wantHeaders: map[string]string{"Access-Control-Allow-Origin": "*"},
},
{
name: "send",
method: http.MethodPost,
path: "/api/v1/messages",
},
{
name: "state",
method: http.MethodGet,
path: "/api/v1/state",
},
{
name: "preflight for send",
method: http.MethodOptions,
path: "/api/v1/messages",
requestMethod: http.MethodPost,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
request := httptest.NewRequestWithContext(
t.Context(), test.method,
"http://chat.example.com"+test.path, nil,
)
request.Header.Set("Origin", "http://blog.example.com")
if test.requestMethod != "" {
request.Header.Set(
"Access-Control-Request-Method", test.requestMethod,
)
}
recorder := httptest.NewRecorder()
srv.ServeHTTP(recorder, request)
gotHeaders := map[string]string{}
for name := range recorder.Header() {
if strings.HasPrefix(name, "Access-Control-Allow-") {
gotHeaders[name] = recorder.Header().Get(name)
}
}
if !maps.Equal(gotHeaders, test.wantHeaders) {
t.Errorf(
"Access-Control-Allow-* headers are %v, want %v",
gotHeaders, test.wantHeaders,
)
}
})
}
}
// startApp starts the app newApp builds, with the HTTP port // startApp starts the app newApp builds, with the HTTP port
// set to 0, so the system gives the server a free port. // set to 0, so the system gives the server a free port.
func startApp( func startApp(
+78 -38
View File
@@ -1,7 +1,3 @@
// Tests use a global viper instance for configuration,
// making parallel execution unsafe.
//
//nolint:paralleltest
package service_test package service_test
import ( import (
@@ -59,28 +55,12 @@ func newTestEnv(t *testing.T) *testEnv {
} }
}, },
logger.New, logger.New,
func( func() *config.Config {
lifecycle fx.Lifecycle, return &config.Config{
globs *globals.Globals, DBURL: dbURL,
log *logger.Logger, OperName: "admin",
) (*config.Config, error) { OperPassword: "secret",
cfg, err := config.New(
lifecycle, config.Params{ //nolint:exhaustruct
Globals: globs, Logger: log,
},
)
if err != nil {
return nil, fmt.Errorf(
"test config: %w", err,
)
} }
cfg.DBURL = dbURL
cfg.Port = 0
cfg.OperName = "admin"
cfg.OperPassword = "secret"
return cfg, nil
}, },
func( func(
lifecycle fx.Lifecycle, lifecycle fx.Lifecycle,
@@ -136,6 +116,8 @@ func createSession(
// environment starts on an empty database while another one // environment starts on an empty database while another one
// is still open. // is still open.
func TestEachTestEnvHasItsOwnDatabase(t *testing.T) { func TestEachTestEnvHasItsOwnDatabase(t *testing.T) {
t.Parallel()
ctx := t.Context() ctx := t.Context()
first := newTestEnv(t) first := newTestEnv(t)
@@ -146,6 +128,8 @@ func TestEachTestEnvHasItsOwnDatabase(t *testing.T) {
} }
func TestFanOut(t *testing.T) { func TestFanOut(t *testing.T) {
t.Parallel()
env := newTestEnv(t) env := newTestEnv(t)
ctx := t.Context() ctx := t.Context()
@@ -173,6 +157,8 @@ func TestFanOut(t *testing.T) {
} }
func TestJoinChannel(t *testing.T) { func TestJoinChannel(t *testing.T) {
t.Parallel()
env := newTestEnv(t) env := newTestEnv(t)
ctx := t.Context() ctx := t.Context()
@@ -213,6 +199,8 @@ func TestJoinChannel(t *testing.T) {
} }
func TestPartChannel(t *testing.T) { func TestPartChannel(t *testing.T) {
t.Parallel()
env := newTestEnv(t) env := newTestEnv(t)
ctx := t.Context() ctx := t.Context()
@@ -313,6 +301,8 @@ func joinConcurrently(
// concurrent JOINs of a +l 2 channel with one member, one // concurrent JOINs of a +l 2 channel with one member, one
// gets in and the other gets ERR_CHANNELISFULL. // gets in and the other gets ERR_CHANNELISFULL.
func TestConcurrentJoinsRespectUserLimit(t *testing.T) { func TestConcurrentJoinsRespectUserLimit(t *testing.T) {
t.Parallel()
const userLimit = 2 const userLimit = 2
env := newTestEnv(t) env := newTestEnv(t)
@@ -375,6 +365,8 @@ func TestConcurrentJoinsRespectUserLimit(t *testing.T) {
// two concurrent JOINs that create a channel, exactly one // two concurrent JOINs that create a channel, exactly one
// makes its session the operator. // makes its session the operator.
func TestConcurrentFirstJoinsMakeOneOperator(t *testing.T) { func TestConcurrentFirstJoinsMakeOneOperator(t *testing.T) {
t.Parallel()
env := newTestEnv(t) env := newTestEnv(t)
ctx := t.Context() ctx := t.Context()
@@ -426,6 +418,8 @@ func TestConcurrentFirstJoinsMakeOneOperator(t *testing.T) {
// either joins a channel that still exists or gets // either joins a channel that still exists or gets
// ERR_NOSUCHCHANNEL, and never fails otherwise. // ERR_NOSUCHCHANNEL, and never fails otherwise.
func TestJoinConcurrentWithLastPart(t *testing.T) { func TestJoinConcurrentWithLastPart(t *testing.T) {
t.Parallel()
env := newTestEnv(t) env := newTestEnv(t)
ctx := t.Context() ctx := t.Context()
@@ -491,6 +485,8 @@ func TestJoinConcurrentWithLastPart(t *testing.T) {
// quits, gets in only once the channel is deleted, into a // quits, gets in only once the channel is deleted, into a
// new channel of that name with no key. // new channel of that name with no key.
func TestJoinConcurrentWithLastQuit(t *testing.T) { func TestJoinConcurrentWithLastQuit(t *testing.T) {
t.Parallel()
env := newTestEnv(t) env := newTestEnv(t)
ctx := t.Context() ctx := t.Context()
@@ -569,15 +565,32 @@ const quitCrowdSize = 10
// ahead of that JOIN. The quitting session is also on a // ahead of that JOIN. The quitting session is also on a
// channel with quitCrowdSize other sessions, made before the // channel with quitCrowdSize other sessions, made before the
// watcher's channel, so that its QUIT reaches the watcher // watcher's channel, so that its QUIT reaches the watcher
// last. // last. Each trial runs on a database of its own.
func TestQuitQueuedAheadOfNewNickHoldersJoin(t *testing.T) { func TestQuitQueuedAheadOfNewNickHoldersJoin(t *testing.T) {
env := newTestEnv(t) t.Parallel()
ctx := t.Context()
for trial := range concurrentJoinTrials { for trial := range concurrentJoinTrials {
nick := fmt.Sprintf("reused%d", trial) t.Run(fmt.Sprintf("trial %d", trial), func(t *testing.T) {
crowd := fmt.Sprintf("#crowd%d", trial) t.Parallel()
shared := fmt.Sprintf("#shared%d", trial)
checkQuitQueuedAheadOfJoin(t, newTestEnv(t))
})
}
}
// checkQuitQueuedAheadOfJoin runs one trial of
// TestQuitQueuedAheadOfNewNickHoldersJoin on env.
func checkQuitQueuedAheadOfJoin(t *testing.T, env *testEnv) {
t.Helper()
const (
nick = "reused"
crowd = "#crowd"
shared = "#shared"
watcherNick = "watcher"
)
ctx := t.Context()
quitterSID := createSession(ctx, t, env.db, nick) quitterSID := createSession(ctx, t, env.db, nick)
for _, channel := range []string{crowd, shared} { for _, channel := range []string{crowd, shared} {
@@ -588,7 +601,7 @@ func TestQuitQueuedAheadOfNewNickHoldersJoin(t *testing.T) {
} }
for member := range quitCrowdSize { for member := range quitCrowdSize {
memberNick := fmt.Sprintf("crowd%dx%d", trial, member) memberNick := fmt.Sprintf("crowd%d", member)
memberSID := createSession(ctx, t, env.db, memberNick) memberSID := createSession(ctx, t, env.db, memberNick)
_, err := env.svc.JoinChannel(ctx, memberSID, 0, memberNick, crowd, "") _, err := env.svc.JoinChannel(ctx, memberSID, 0, memberNick, crowd, "")
@@ -597,8 +610,6 @@ func TestQuitQueuedAheadOfNewNickHoldersJoin(t *testing.T) {
} }
} }
watcherNick := fmt.Sprintf("watcher%d", trial)
watcherSID, watcherClientID, _, err := env.db.CreateSession( watcherSID, watcherClientID, _, err := env.db.CreateSession(
ctx, watcherNick, watcherNick, "localhost", "127.0.0.1", ctx, watcherNick, watcherNick, "localhost", "127.0.0.1",
) )
@@ -627,7 +638,7 @@ func TestQuitQueuedAheadOfNewNickHoldersJoin(t *testing.T) {
}, },
) )
if errs[1] != nil { if errs[1] != nil {
t.Fatalf("trial %d: new session: %v", trial, errs[1]) t.Fatalf("new session: %v", errs[1])
} }
msgs, _, err := env.db.PollMessages( msgs, _, err := env.db.PollMessages(
@@ -640,11 +651,10 @@ func TestQuitQueuedAheadOfNewNickHoldersJoin(t *testing.T) {
commands := commandsFrom(msgs, nick) commands := commandsFrom(msgs, nick)
if !slices.Equal(commands, []string{irc.CmdQuit, irc.CmdJoin}) { if !slices.Equal(commands, []string{irc.CmdQuit, irc.CmdJoin}) {
t.Errorf( t.Errorf(
"trial %d: watcher got %v from %s, want QUIT then JOIN", "watcher got %v from %s, want QUIT then JOIN",
trial, commands, nick, commands, nick,
) )
} }
}
} }
// takeNickAndJoin creates a session with nick, trying again // takeNickAndJoin creates a session with nick, trying again
@@ -691,6 +701,8 @@ func commandsFrom(msgs []db.IRCMessage, nick string) []string {
// into a +i channel uses the invite up, so that a JOIN // into a +i channel uses the invite up, so that a JOIN
// after leaving gets ERR_INVITEONLYCHAN. // after leaving gets ERR_INVITEONLYCHAN.
func TestJoinUsesUpInvite(t *testing.T) { func TestJoinUsesUpInvite(t *testing.T) {
t.Parallel()
const channel = "#club" const channel = "#club"
env := newTestEnv(t) env := newTestEnv(t)
@@ -736,6 +748,8 @@ func TestJoinUsesUpInvite(t *testing.T) {
// session's nick, username and hostname refuses its JOIN // session's nick, username and hostname refuses its JOIN
// with ERR_BANNEDFROMCHAN. // with ERR_BANNEDFROMCHAN.
func TestJoinRefusedByHostmaskBan(t *testing.T) { func TestJoinRefusedByHostmaskBan(t *testing.T) {
t.Parallel()
const channel = "#club" const channel = "#club"
env := newTestEnv(t) env := newTestEnv(t)
@@ -774,6 +788,8 @@ func TestJoinRefusedByHostmaskBan(t *testing.T) {
// keeps a channel other members are still on and deletes // keeps a channel other members are still on and deletes
// the channel when its last member parts. // the channel when its last member parts.
func TestPartDeletesChannelOnlyWhenEmpty(t *testing.T) { func TestPartDeletesChannelOnlyWhenEmpty(t *testing.T) {
t.Parallel()
const channel = "#room" const channel = "#room"
env := newTestEnv(t) env := newTestEnv(t)
@@ -821,6 +837,8 @@ func TestPartDeletesChannelOnlyWhenEmpty(t *testing.T) {
// TestKickOfLastMemberDeletesChannel checks that a KICK // TestKickOfLastMemberDeletesChannel checks that a KICK
// that leaves a channel with no members deletes it. // that leaves a channel with no members deletes it.
func TestKickOfLastMemberDeletesChannel(t *testing.T) { func TestKickOfLastMemberDeletesChannel(t *testing.T) {
t.Parallel()
const channel = "#room" const channel = "#room"
env := newTestEnv(t) env := newTestEnv(t)
@@ -845,6 +863,8 @@ func TestKickOfLastMemberDeletesChannel(t *testing.T) {
} }
func TestSendChannelMessage(t *testing.T) { func TestSendChannelMessage(t *testing.T) {
t.Parallel()
env := newTestEnv(t) env := newTestEnv(t)
ctx := t.Context() ctx := t.Context()
@@ -892,6 +912,8 @@ func TestSendChannelMessage(t *testing.T) {
} }
func TestBroadcastQuit(t *testing.T) { func TestBroadcastQuit(t *testing.T) {
t.Parallel()
env := newTestEnv(t) env := newTestEnv(t)
ctx := t.Context() ctx := t.Context()
@@ -929,6 +951,8 @@ func TestBroadcastQuit(t *testing.T) {
// error as a DM to an unknown nick. The trigger deletes the // error as a DM to an unknown nick. The trigger deletes the
// target's session just before the DM is stored. // target's session just before the DM is stored.
func TestSendDirectMessageTargetQuits(t *testing.T) { func TestSendDirectMessageTargetQuits(t *testing.T) {
t.Parallel()
env := newTestEnv(t) env := newTestEnv(t)
ctx := t.Context() ctx := t.Context()
@@ -960,6 +984,8 @@ func TestSendDirectMessageTargetQuits(t *testing.T) {
// trigger deletes the channel just before the message is // trigger deletes the channel just before the message is
// stored. // stored.
func TestSendChannelMessageChannelDeleted(t *testing.T) { func TestSendChannelMessageChannelDeleted(t *testing.T) {
t.Parallel()
env := newTestEnv(t) env := newTestEnv(t)
ctx := t.Context() ctx := t.Context()
@@ -991,6 +1017,8 @@ func TestSendChannelMessageChannelDeleted(t *testing.T) {
} }
func TestSendChannelMessage_Moderated(t *testing.T) { func TestSendChannelMessage_Moderated(t *testing.T) {
t.Parallel()
env := newTestEnv(t) env := newTestEnv(t)
ctx := t.Context() ctx := t.Context()
@@ -1042,6 +1070,8 @@ func TestSendChannelMessage_Moderated(t *testing.T) {
// not spend its hashcash stamp: the operator can then send // not spend its hashcash stamp: the operator can then send
// the same message with it. // the same message with it.
func TestRefusedChannelMessageLeavesStampUnspent(t *testing.T) { func TestRefusedChannelMessageLeavesStampUnspent(t *testing.T) {
t.Parallel()
const ( const (
channel = "#paid" channel = "#paid"
bits = 2 bits = 2
@@ -1067,6 +1097,8 @@ func TestRefusedChannelMessageLeavesStampUnspent(t *testing.T) {
for _, refusal := range refusals { for _, refusal := range refusals {
t.Run(refusal.name, func(t *testing.T) { t.Run(refusal.name, func(t *testing.T) {
t.Parallel()
env := newTestEnv(t) env := newTestEnv(t)
ctx := t.Context() ctx := t.Context()
@@ -1119,6 +1151,8 @@ func TestRefusedChannelMessageLeavesStampUnspent(t *testing.T) {
} }
func TestQueryUserMode(t *testing.T) { func TestQueryUserMode(t *testing.T) {
t.Parallel()
env := newTestEnv(t) env := newTestEnv(t)
ctx := t.Context() ctx := t.Context()
@@ -1169,6 +1203,8 @@ func TestQueryUserMode(t *testing.T) {
} }
func TestUserhostReply(t *testing.T) { func TestUserhostReply(t *testing.T) {
t.Parallel()
env := newTestEnv(t) env := newTestEnv(t)
ctx := t.Context() ctx := t.Context()
@@ -1274,10 +1310,14 @@ func applyUserModeTests() []applyUserModeTest {
} }
func TestApplyUserMode(t *testing.T) { func TestApplyUserMode(t *testing.T) {
t.Parallel()
env := newTestEnv(t) env := newTestEnv(t)
for i, test := range applyUserModeTests() { for i, test := range applyUserModeTests() {
t.Run(test.modeStr, func(t *testing.T) { t.Run(test.modeStr, func(t *testing.T) {
t.Parallel()
ctx := t.Context() ctx := t.Context()
sid := createSession( sid := createSession(
ctx, t, env.db, fmt.Sprintf("user%d", i), ctx, t, env.db, fmt.Sprintf("user%d", i),