1 Commits
Author SHA1 Message Date
sneak 748709afbf Decode the API's actual fields in neoirc-cli (closes #146)
check / check (push) Canceled after 0s
The CLI read the poll cursor from `lastId` while the server sends
`last_id`, so the cursor stayed at 0 and every poll returned the
whole queue at once. It now reads `last_id`.

/names decodes the member objects the server sends and prints each
nick with `@` for an operator or `+` for a voiced member. /list
prints each channel's name and topic, since the server sends no
member count. StateResponse.Channels decodes channel objects.

/topic with no text prints a usage line and sends nothing, because
the HTTP API has no topic query and a TOPIC without text gets 461.
The help says "Set topic".

The client tests serve the server's real response shapes.

Model: opus-5-5
2026-10-20 23:23:21 +00:00
9 changed files with 195 additions and 1349 deletions

No files matched your search

+4 -7
View File
@@ -2569,13 +2569,10 @@ issues) and simpler than UUIDs (integer comparison vs. string comparison).
- **Sessions**: Both anonymous and password-protected sessions are deleted on - **Sessions**: Both anonymous and password-protected sessions are deleted on
`QUIT` or when the last client logs out (`POST /api/v1/logout` with no `QUIT` or when the last client logs out (`POST /api/v1/logout` with no
remaining clients triggers session cleanup). There is no distinction between remaining clients triggers session cleanup). There is no distinction between
session types in the cleanup path: the session is parted from its channels, session types in the cleanup path — `handleQuit` and `cleanupUser` both call
deleted, and its QUIT queued to its channel peers in one database transaction, `DeleteSession` unconditionally. Idle clients are automatically removed after
so when several QUITs or logouts end one session at once only the one that `SESSION_IDLE_TIMEOUT` (default 30 days) without an authenticated HTTP API
deletes it broadcasts QUIT, and whatever a new holder of the nick sends request or, on the IRC listener, a command — the server runs a background
reaches those peers after the QUIT. Idle clients are automatically removed
after `SESSION_IDLE_TIMEOUT` (default 30 days) without an authenticated HTTP
API request or, on the IRC listener, a command — the server runs a background
cleanup loop that, for a session left with no clients, parts the user from all cleanup loop that, for a session left with no clients, parts the user from all
channels, broadcasts QUIT, and releases the nick. channels, broadcasts QUIT, and releases the nick.
- **Clients**: Individual client auth cookies are invalidated on - **Clients**: Individual client auth cookies are invalidated on
+13 -195
View File
@@ -2,28 +2,20 @@ package neoircapi_test
import ( import (
"context" "context"
"encoding/json"
"fmt"
"io" "io"
"net" "net"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"net/url" "net/url"
"reflect"
"slices" "slices"
"strconv" "strconv"
"strings" "strings"
"testing" "testing"
api "sneak.berlin/go/neoirc/internal/cli/api" api "sneak.berlin/go/neoirc/internal/cli/api"
"sneak.berlin/go/neoirc/internal/hashcash"
) )
const ( const cookieValue = "opaque-value"
cookieValue = "opaque-value"
testNick = "alice"
testChannel = "#general"
)
// newSessionServer starts a plain-HTTP server that, like // newSessionServer starts a plain-HTTP server that, like
// neoircd, sets a Secure auth cookie when a session is // neoircd, sets a Secure auth cookie when a session is
@@ -34,16 +26,10 @@ func newSessionServer(t *testing.T) *httptest.Server {
mux := http.NewServeMux() mux := http.NewServeMux()
// Without hashcash the server sends no hashcash_bits.
mux.HandleFunc("GET /api/v1/server", func( mux.HandleFunc("GET /api/v1/server", func(
writer http.ResponseWriter, _ *http.Request, writer http.ResponseWriter, _ *http.Request,
) { ) {
_, _ = io.WriteString(writer, `{ _, _ = io.WriteString(writer, `{}`)
"name": "My NeoIRC Server",
"version": "0.1.0",
"motd": "Welcome! Be nice.",
"users": 42
}`)
}) })
mux.HandleFunc("POST /api/v1/session", func( mux.HandleFunc("POST /api/v1/session", func(
@@ -107,12 +93,12 @@ func newClientForPath(t *testing.T, path, body string) *api.Client {
return api.NewClient(server.URL) return api.NewClient(server.URL)
} }
func TestGetStateDecodesFields(t *testing.T) { func TestGetStateDecodesChannels(t *testing.T) {
t.Parallel() t.Parallel()
client := api.NewClient(newSessionServer(t).URL) client := api.NewClient(newSessionServer(t).URL)
_, err := client.CreateSession(testNick) _, err := client.CreateSession("alice")
if err != nil { if err != nil {
t.Fatalf("create session: %v", err) t.Fatalf("create session: %v", err)
} }
@@ -122,117 +108,12 @@ func TestGetStateDecodesFields(t *testing.T) {
t.Fatalf("state: %v", err) t.Fatalf("state: %v", err)
} }
want := api.StateResponse{ want := []api.Channel{
ID: 1, {Name: "#general", Topic: "Welcome!"},
Nick: testNick,
Channels: []api.Channel{
{Name: testChannel, Topic: "Welcome!"},
{Name: "#dev", Topic: ""}, {Name: "#dev", Topic: ""},
},
} }
if !reflect.DeepEqual(*state, want) { if !slices.Equal(state.Channels, want) {
t.Fatalf("state: got %+v, want %+v", *state, want) t.Fatalf("channels: got %+v, want %+v", state.Channels, want)
}
}
func TestGetServerInfoDecodesFields(t *testing.T) {
t.Parallel()
client := newClientForPath(t, "/api/v1/server", `{
"name": "My NeoIRC Server",
"version": "0.1.0",
"motd": "Welcome! Be nice.",
"users": 42,
"hashcash_bits": 20
}`)
info, err := client.GetServerInfo()
if err != nil {
t.Fatalf("server info: %v", err)
}
want := api.ServerInfo{
Name: "My NeoIRC Server",
MOTD: "Welcome! Be nice.",
Version: "0.1.0",
HashcashBits: 20,
}
if *info != want {
t.Fatalf("server info: got %+v, want %+v", *info, want)
}
}
// TestCreateSessionSendsNickAndStamp serves a server that, like
// neoircd with hashcash on, advertises hashcash_bits and creates
// the session only for a pow_token the server's validator in
// internal/hashcash accepts.
func TestCreateSessionSendsNickAndStamp(t *testing.T) {
t.Parallel()
const (
serverName = "My NeoIRC Server"
stampBits = 8
sessionID = 7
)
mux := http.NewServeMux()
mux.HandleFunc("GET /api/v1/server", func(
writer http.ResponseWriter, _ *http.Request,
) {
_, _ = fmt.Fprintf(writer, `{
"name": %q,
"version": "0.1.0",
"motd": "Welcome! Be nice.",
"users": 42,
"hashcash_bits": %d
}`, serverName, stampBits)
})
validator := hashcash.NewValidator(serverName)
mux.HandleFunc("POST /api/v1/session", func(
writer http.ResponseWriter, request *http.Request,
) {
// Decoded with the field names handleCreateSession uses.
var payload struct {
Nick string `json:"nick"`
Hashcash string `json:"pow_token"` //nolint:tagliatelle
}
err := json.NewDecoder(request.Body).Decode(&payload)
if err != nil {
writer.WriteHeader(http.StatusBadRequest)
return
}
err = validator.Validate(payload.Hashcash, stampBits)
if err != nil {
writer.WriteHeader(http.StatusPaymentRequired)
return
}
writer.WriteHeader(http.StatusCreated)
_ = json.NewEncoder(writer).Encode(map[string]any{
"id": sessionID,
"nick": payload.Nick,
})
})
server := httptest.NewServer(mux)
t.Cleanup(server.Close)
session, err := api.NewClient(server.URL).CreateSession(testNick)
if err != nil {
t.Fatalf("create session: %v", err)
}
want := api.SessionResponse{ID: sessionID, Nick: testNick}
if *session != want {
t.Fatalf("session: got %+v, want %+v", *session, want)
} }
} }
@@ -250,7 +131,7 @@ func TestListChannelsDecodesNamesAndTopics(t *testing.T) {
} }
want := []api.Channel{ want := []api.Channel{
{Name: testChannel, Topic: "Welcome!"}, {Name: "#general", Topic: "Welcome!"},
{Name: "#dev", Topic: "Development discussion"}, {Name: "#dev", Topic: "Development discussion"},
} }
if !slices.Equal(channels, want) { if !slices.Equal(channels, want) {
@@ -282,13 +163,13 @@ func TestGetMembersDecodesMemberObjects(t *testing.T) {
} }
]`) ]`)
members, err := client.GetMembers(testChannel) members, err := client.GetMembers("#general")
if err != nil { if err != nil {
t.Fatalf("get members: %v", err) t.Fatalf("get members: %v", err)
} }
want := []api.Member{ want := []api.Member{
{Nick: testNick, IsOperator: true, IsVoiced: false}, {Nick: "alice", IsOperator: true, IsVoiced: false},
{Nick: "bob", IsOperator: false, IsVoiced: true}, {Nick: "bob", IsOperator: false, IsVoiced: true},
{Nick: "Carol", IsOperator: false, IsVoiced: false}, {Nick: "Carol", IsOperator: false, IsVoiced: false},
} }
@@ -363,69 +244,6 @@ func TestPollMessagesAdvancesCursor(t *testing.T) {
} }
} }
// TestPollMessagesDecodesMessageFields serves a relayed PRIVMSG
// with the sender's meta and a numeric, as the server queues
// them.
func TestPollMessagesDecodesMessageFields(t *testing.T) {
t.Parallel()
client := newClientForPath(t, "/api/v1/messages", `{
"messages": [
{
"id": "b7c8210f-849c-4b90-9ee8-d99c8889358e",
"command": "PRIVMSG",
"from": "alice",
"to": "#general",
"body": ["signed message"],
"ts": "2026-02-10T20:00:01.234567891Z",
"meta": {"sig": "base64...", "alg": "ed25519"}
},
{
"id": "7f5a04f8-eab4-4d2e-be55-f5cfcfaf43c5",
"command": "RPL_LUSEROP",
"code": 252,
"from": "neoirc",
"to": "alice",
"params": ["0"],
"body": ["operator(s) online"],
"ts": "2026-02-10T20:00:02Z",
"meta": {}
}
],
"last_id": 42
}`)
result, err := client.PollMessages(0, 0)
if err != nil {
t.Fatalf("poll: %v", err)
}
want := []api.Message{
{
ID: "b7c8210f-849c-4b90-9ee8-d99c8889358e",
Command: "PRIVMSG",
From: testNick,
To: testChannel,
Body: []any{"signed message"},
TS: "2026-02-10T20:00:01.234567891Z",
Meta: map[string]any{"sig": "base64...", "alg": "ed25519"},
},
{
ID: "7f5a04f8-eab4-4d2e-be55-f5cfcfaf43c5",
Command: "RPL_LUSEROP",
From: "neoirc",
To: testNick,
Params: []string{"0"},
Body: []any{"operator(s) online"},
TS: "2026-02-10T20:00:02Z",
Meta: map[string]any{},
},
}
if !reflect.DeepEqual(result.Messages, want) {
t.Fatalf("messages: got %+v, want %+v", result.Messages, want)
}
}
func TestClientKeepsSessionOverPlainHTTPOnLocalhost(t *testing.T) { func TestClientKeepsSessionOverPlainHTTPOnLocalhost(t *testing.T) {
t.Parallel() t.Parallel()
@@ -444,7 +262,7 @@ func TestClientKeepsSessionOverPlainHTTPOnLocalhost(t *testing.T) {
"http://" + net.JoinHostPort(host, serverURL.Port()), "http://" + net.JoinHostPort(host, serverURL.Port()),
) )
_, err := client.CreateSession(testNick) _, err := client.CreateSession("alice")
if err != nil { if err != nil {
t.Fatalf("create session: %v", err) t.Fatalf("create session: %v", err)
} }
@@ -477,7 +295,7 @@ func TestClientWithholdsCookieOverPlainHTTPFromOtherHosts(t *testing.T) {
}, },
} }
_, err := client.CreateSession(testNick) _, err := client.CreateSession("alice")
if err != nil { if err != nil {
t.Fatalf("create session: %v", err) t.Fatalf("create session: %v", err)
} }
-51
View File
@@ -1,51 +0,0 @@
package cli
import (
"testing"
api "sneak.berlin/go/neoirc/internal/cli/api"
)
func TestMemberPrefix(t *testing.T) {
t.Parallel()
tests := []struct {
name string
member api.Member
want string
}{
{
name: "operator",
member: api.Member{Nick: "alice", IsOperator: true},
want: "@",
},
{
name: "voiced member",
member: api.Member{Nick: "bob", IsVoiced: true},
want: "+",
},
{
name: "voiced operator",
member: api.Member{
Nick: "Carol", IsOperator: true, IsVoiced: true,
},
want: "@",
},
{
name: "plain member",
member: api.Member{Nick: "dave"},
want: "",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
got := memberPrefix(test.member)
if got != test.want {
t.Fatalf("got %q, want %q", got, test.want)
}
})
}
}
+85 -241
View File
@@ -854,6 +854,42 @@ func (database *Database) SetChannelTopicLocked(
return nil return nil
} }
// PartChannel removes a session from a channel.
func (database *Database) PartChannel(
ctx context.Context,
channelID, sessionID int64,
) error {
_, err := database.conn.ExecContext(ctx,
`DELETE FROM channel_members
WHERE channel_id = ? AND session_id = ?`,
channelID, sessionID)
if err != nil {
return fmt.Errorf("part channel: %w", err)
}
return nil
}
// DeleteChannelIfEmpty removes a channel with no members.
func (database *Database) DeleteChannelIfEmpty(
ctx context.Context,
channelID int64,
) error {
_, err := database.conn.ExecContext(ctx,
`DELETE FROM channels WHERE id = ?
AND NOT EXISTS
(SELECT 1 FROM channel_members
WHERE channel_id = ?)`,
channelID, channelID)
if err != nil {
return fmt.Errorf(
"delete channel if empty: %w", err,
)
}
return nil
}
// PartChannelAndDeleteIfEmpty removes a session from a // PartChannelAndDeleteIfEmpty removes a session from a
// channel and deletes the channel if that left it with no // channel and deletes the channel if that left it with no
// members, in one transaction, so that no JOIN lands // members, in one transaction, so that no JOIN lands
@@ -869,29 +905,7 @@ func (database *Database) PartChannelAndDeleteIfEmpty(
defer func() { _ = transaction.Rollback() }() defer func() { _ = transaction.Rollback() }()
err = partChannelAndDeleteIfEmptyInTx( _, err = transaction.ExecContext(ctx,
ctx, transaction, channelID, sessionID,
)
if err != nil {
return err
}
err = transaction.Commit()
if err != nil {
return fmt.Errorf("commit part: %w", err)
}
return nil
}
// partChannelAndDeleteIfEmptyInTx is
// PartChannelAndDeleteIfEmpty, writing through transaction.
func partChannelAndDeleteIfEmptyInTx(
ctx context.Context,
transaction *sql.Tx,
channelID, sessionID int64,
) error {
_, err := transaction.ExecContext(ctx,
`DELETE FROM channel_members `DELETE FROM channel_members
WHERE channel_id = ? AND session_id = ?`, WHERE channel_id = ? AND session_id = ?`,
channelID, sessionID) channelID, sessionID)
@@ -911,6 +925,11 @@ func partChannelAndDeleteIfEmptyInTx(
) )
} }
err = transaction.Commit()
if err != nil {
return fmt.Errorf("commit part: %w", err)
}
return nil return nil
} }
@@ -1137,40 +1156,6 @@ func (database *Database) InsertMessage(
params json.RawMessage, params json.RawMessage,
body json.RawMessage, body json.RawMessage,
meta json.RawMessage, meta json.RawMessage,
) (int64, string, error) {
transaction, err := database.conn.BeginTx(ctx, nil)
if err != nil {
return 0, "", fmt.Errorf("begin insert message: %w", err)
}
defer func() { _ = transaction.Rollback() }()
dbID, msgUUID, err := insertMessageInTx(
ctx, transaction, command, from, target,
toSessionID, toChannelID, params, body, meta,
)
if err != nil {
return 0, "", err
}
err = transaction.Commit()
if err != nil {
return 0, "", fmt.Errorf("commit insert message: %w", err)
}
return dbID, msgUUID, nil
}
// insertMessageInTx is InsertMessage, writing through
// transaction.
func insertMessageInTx(
ctx context.Context,
transaction *sql.Tx,
command, from, target string,
toSessionID, toChannelID int64,
params json.RawMessage,
body json.RawMessage,
meta json.RawMessage,
) (int64, string, error) { ) (int64, string, error) {
msgUUID := uuid.New().String() msgUUID := uuid.New().String()
now := time.Now().UTC() now := time.Now().UTC()
@@ -1187,7 +1172,7 @@ func insertMessageInTx(
meta = json.RawMessage("{}") meta = json.RawMessage("{}")
} }
res, err := transaction.ExecContext(ctx, res, err := database.conn.ExecContext(ctx,
`INSERT INTO messages `INSERT INTO messages
(uuid, command, msg_from, msg_to, (uuid, command, msg_from, msg_to,
to_session_id, to_channel_id, to_session_id, to_channel_id,
@@ -1214,34 +1199,7 @@ func (database *Database) EnqueueToSession(
ctx context.Context, ctx context.Context,
sessionID, messageID int64, sessionID, messageID int64,
) error { ) error {
transaction, err := database.conn.BeginTx(ctx, nil) _, err := database.conn.ExecContext(ctx,
if err != nil {
return fmt.Errorf("begin enqueue: %w", err)
}
defer func() { _ = transaction.Rollback() }()
err = enqueueToSessionInTx(ctx, transaction, sessionID, messageID)
if err != nil {
return err
}
err = transaction.Commit()
if err != nil {
return fmt.Errorf("commit enqueue: %w", err)
}
return nil
}
// enqueueToSessionInTx is EnqueueToSession, writing through
// transaction.
func enqueueToSessionInTx(
ctx context.Context,
transaction *sql.Tx,
sessionID, messageID int64,
) error {
_, err := transaction.ExecContext(ctx,
`INSERT OR IGNORE INTO client_queues `INSERT OR IGNORE INTO client_queues
(client_id, message_id, created_at) (client_id, message_id, created_at)
SELECT c.id, ?, ? SELECT c.id, ?, ?
@@ -1512,175 +1470,38 @@ func (database *Database) SetTopic(
return nil return nil
} }
// QuitSession ends a session in one transaction: it parts // DeleteSession removes a session and all its data.
// the session from its channels, deleting each channel this func (database *Database) DeleteSession(
// leaves with no members, deletes the session with its
// clients, and stores a QUIT from nick with body and queues
// it to the other sessions that were on those channels. It
// returns those sessions, for the caller to wake once the
// QUIT is committed. Of several calls for one session at
// once, only the first finds the session; the others change
// nothing and return none.
func (database *Database) QuitSession(
ctx context.Context, ctx context.Context,
sessionID int64, sessionID int64,
nick string, ) error {
body json.RawMessage, _, err := database.conn.ExecContext(
) ([]int64, error) { ctx,
transaction, err := database.conn.BeginTx(ctx, nil) "DELETE FROM sessions WHERE id = ?",
if err != nil {
return nil, fmt.Errorf("begin quit: %w", err)
}
defer func() { _ = transaction.Rollback() }()
peerIDs, err := quitSessionInTx(
ctx, transaction, sessionID, nick, body,
)
if err != nil {
return nil, err
}
err = transaction.Commit()
if err != nil {
return nil, fmt.Errorf("commit quit: %w", err)
}
return peerIDs, nil
}
// LogoutClient deletes a client of a session and, if that
// leaves the session with no clients, does what QuitSession
// does, in one transaction. It returns what QuitSession
// returns, or none if the session keeps a client.
func (database *Database) LogoutClient(
ctx context.Context,
sessionID, clientID int64,
nick string,
body json.RawMessage,
) ([]int64, error) {
transaction, err := database.conn.BeginTx(ctx, nil)
if err != nil {
return nil, fmt.Errorf("begin logout: %w", err)
}
defer func() { _ = transaction.Rollback() }()
_, err = transaction.ExecContext(ctx,
"DELETE FROM clients WHERE id = ?", clientID)
if err != nil {
return nil, fmt.Errorf("delete client: %w", err)
}
var remaining int64
err = transaction.QueryRowContext(ctx,
"SELECT COUNT(*) FROM clients WHERE session_id = ?",
sessionID, sessionID,
).Scan(&remaining)
if err != nil {
return nil, fmt.Errorf("count clients: %w", err)
}
var peerIDs []int64
if remaining == 0 {
peerIDs, err = quitSessionInTx(
ctx, transaction, sessionID, nick, body,
) )
if err != nil { if err != nil {
return nil, err return fmt.Errorf("delete session: %w", err)
}
} }
err = transaction.Commit() return nil
if err != nil {
return nil, fmt.Errorf("commit logout: %w", err)
}
return peerIDs, nil
} }
// quitSessionInTx is QuitSession, writing through // DeleteClient removes a single client record by ID.
// transaction. func (database *Database) DeleteClient(
func quitSessionInTx(
ctx context.Context, ctx context.Context,
transaction *sql.Tx, clientID int64,
sessionID int64, ) error {
nick string, _, err := database.conn.ExecContext(
body json.RawMessage, ctx,
) ([]int64, error) { "DELETE FROM clients WHERE id = ?",
rows, err := transaction.QueryContext(ctx, clientID,
`SELECT DISTINCT session_id FROM channel_members
WHERE session_id != ? AND channel_id IN
(SELECT channel_id FROM channel_members
WHERE session_id = ?)`,
sessionID, sessionID)
if err != nil {
return nil, fmt.Errorf("get channel peers: %w", err)
}
peerIDs, err := scanInt64s(rows)
if err != nil {
return nil, err
}
rows, err = transaction.QueryContext(ctx,
`SELECT channel_id FROM channel_members
WHERE session_id = ?`, sessionID)
if err != nil {
return nil, fmt.Errorf(
"get session channel ids: %w", err,
)
}
channelIDs, err := scanInt64s(rows)
if err != nil {
return nil, err
}
for _, channelID := range channelIDs {
err = partChannelAndDeleteIfEmptyInTx(
ctx, transaction, channelID, sessionID,
) )
if err != nil { if err != nil {
return nil, err return fmt.Errorf("delete client: %w", err)
}
} }
result, err := transaction.ExecContext(ctx, return nil
"DELETE FROM sessions WHERE id = ?", sessionID)
if err != nil {
return nil, fmt.Errorf("delete session: %w", err)
}
deletedRows, err := result.RowsAffected()
if err != nil {
return nil, fmt.Errorf("delete session: %w", err)
}
if deletedRows == 0 || len(peerIDs) == 0 {
return nil, nil
}
messageID, _, err := insertMessageInTx(
ctx, transaction, irc.CmdQuit, nick, "", 0, 0,
nil, body, nil,
)
if err != nil {
return nil, err
}
for _, peerID := range peerIDs {
err = enqueueToSessionInTx(
ctx, transaction, peerID, messageID,
)
if err != nil {
return nil, err
}
}
return peerIDs, nil
} }
// GetUserCount returns the number of active users. // GetUserCount returns the number of active users.
@@ -1722,6 +1543,29 @@ func (database *Database) GetOperCount(
return count, nil return count, nil
} }
// ClientCountForSession returns the number of clients
// belonging to a session.
func (database *Database) ClientCountForSession(
ctx context.Context,
sessionID int64,
) (int64, error) {
var count int64
err := database.conn.QueryRowContext(
ctx,
`SELECT COUNT(*) FROM clients
WHERE session_id = ?`,
sessionID,
).Scan(&count)
if err != nil {
return 0, fmt.Errorf(
"client count for session: %w", err,
)
}
return count, nil
}
// DeleteStaleUsers removes clients not seen since the // DeleteStaleUsers removes clients not seen since the
// cutoff and cleans up orphaned users (sessions). // cutoff and cleans up orphaned users (sessions).
func (database *Database) DeleteStaleUsers( func (database *Database) DeleteStaleUsers(
+26 -123
View File
@@ -2,7 +2,6 @@ package db_test
import ( import (
"encoding/json" "encoding/json"
"slices"
"testing" "testing"
"sneak.berlin/go/neoirc/internal/db" "sneak.berlin/go/neoirc/internal/db"
@@ -412,7 +411,7 @@ func TestJoinAndPart(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
err = database.PartChannelAndDeleteIfEmpty(ctx, chID, sid) err = database.PartChannel(ctx, chID, sid)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@@ -423,10 +422,7 @@ func TestJoinAndPart(t *testing.T) {
} }
} }
// TestPartChannelAndDeleteIfEmpty checks that a part keeps func TestDeleteChannelIfEmpty(t *testing.T) {
// a channel another session is still on and deletes the
// channel when its last member parts.
func TestPartChannelAndDeleteIfEmpty(t *testing.T) {
t.Parallel() t.Parallel()
database := setupTestDB(t) database := setupTestDB(t)
@@ -439,13 +435,9 @@ func TestPartChannelAndDeleteIfEmpty(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
nicks := []string{"temp1", "temp2"} sid, _, _, err := database.CreateSession(ctx, "temp", "", "", "")
sids := make([]int64, 0, len(nicks)) if err != nil {
t.Fatal(err)
for _, nick := range nicks {
sid, _, _, createErr := database.CreateSession(ctx, nick, "", "", "")
if createErr != nil {
t.Fatal(createErr)
} }
err = database.JoinChannel(ctx, chID, sid) err = database.JoinChannel(ctx, chID, sid)
@@ -453,20 +445,12 @@ func TestPartChannelAndDeleteIfEmpty(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
sids = append(sids, sid) err = database.PartChannel(ctx, chID, sid)
}
err = database.PartChannelAndDeleteIfEmpty(ctx, chID, sids[0])
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
_, err = database.GetChannelByName(ctx, "#empty") err = database.DeleteChannelIfEmpty(ctx, chID)
if err != nil {
t.Fatalf("channel deleted while a member is on it: %v", err)
}
err = database.PartChannelAndDeleteIfEmpty(ctx, chID, sids[1])
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@@ -793,11 +777,6 @@ func TestGetHistoryReusedNames(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
err = database.JoinChannel(ctx, oldChID, oldSID)
if err != nil {
t.Fatal(err)
}
body := json.RawMessage(`["hi"]`) body := json.RawMessage(`["hi"]`)
_, _, err = database.InsertMessage( _, _, err = database.InsertMessage(
@@ -820,9 +799,12 @@ func TestGetHistoryReusedNames(t *testing.T) {
t.Fatal("expected one DM and one channel message") t.Fatal("expected one DM and one channel message")
} }
// The session is the channel's only member, so quitting err = database.DeleteSession(ctx, oldSID)
// deletes the channel too. if err != nil {
_, err = database.QuitSession(ctx, oldSID, "reused", nil) t.Fatal(err)
}
err = database.DeleteChannelIfEmpty(ctx, oldChID)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@@ -844,42 +826,34 @@ func TestGetHistoryReusedNames(t *testing.T) {
} }
} }
// TestQuitSession checks that QuitSession deletes the func TestDeleteSession(t *testing.T) {
// session, keeps a channel another session is still on,
// deletes a channel it leaves with no members, and queues
// one QUIT to the other session; and that a second call for
// the same session changes nothing.
func TestQuitSession(t *testing.T) {
t.Parallel() t.Parallel()
database := setupTestDB(t) database := setupTestDB(t)
ctx := t.Context() ctx := t.Context()
sid, sharedID, _ := createSessionWithChannels( sid, _, _, err := database.CreateSession(
t, database, "deleteme", "#shared", "#alone", ctx, "deleteme", "", "", "",
)
peerSID, peerClientID, _, err := database.CreateSession(
ctx, "peer", "", "", "",
) )
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
err = database.JoinChannel(ctx, sharedID, peerSID) chID, err := database.GetOrCreateChannel(
ctx, "#delchan",
)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
body := json.RawMessage(`["bye"]`) err = database.JoinChannel(ctx, chID, sid)
peerIDs, err := database.QuitSession(ctx, sid, "deleteme", body)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if !slices.Equal(peerIDs, []int64{peerSID}) { err = database.DeleteSession(ctx, sid)
t.Fatalf("got peers %v, want [%d]", peerIDs, peerSID) if err != nil {
t.Fatal(err)
} }
_, err = database.GetSessionByNick(ctx, "deleteme") _, err = database.GetSessionByNick(ctx, "deleteme")
@@ -887,80 +861,9 @@ func TestQuitSession(t *testing.T) {
t.Fatal("session should be deleted") t.Fatal("session should be deleted")
} }
ids, _ := database.GetChannelMemberIDs(ctx, sharedID) ids, _ := database.GetChannelMemberIDs(ctx, chID)
if !slices.Equal(ids, []int64{peerSID}) { if len(ids) != 0 {
t.Fatalf("#shared members: got %v, want [%d]", ids, peerSID) t.Fatal("expected no members after deletion")
}
_, err = database.GetChannelByName(ctx, "#alone")
if err == nil {
t.Fatal("expected the emptied channel to be deleted")
}
checkOneQuitQueued(t, database, peerClientID, "deleteme")
peerIDs, err = database.QuitSession(ctx, sid, "deleteme", body)
if err != nil || len(peerIDs) != 0 {
t.Fatalf("second quit: got %v, %v; want none", peerIDs, err)
}
checkOneQuitQueued(t, database, peerClientID, "deleteme")
}
// checkOneQuitQueued fails the test unless the queue of the
// client clientID holds one message, a QUIT from nick.
func checkOneQuitQueued(
t *testing.T,
database *db.Database,
clientID int64,
nick string,
) {
t.Helper()
msgs, _, err := database.PollMessages(t.Context(), clientID, 0, 0)
if err != nil {
t.Fatal(err)
}
if len(msgs) != 1 || msgs[0].Command != "QUIT" || msgs[0].From != nick {
t.Fatalf("queue: got %v, want one QUIT from %s", msgs, nick)
}
}
// TestLogoutClientFailureKeepsClient checks that when the
// session of a logout's last client cannot be deleted,
// LogoutClient fails and the client stays logged in.
func TestLogoutClientFailureKeepsClient(t *testing.T) {
t.Parallel()
database := setupTestDB(t)
ctx := t.Context()
sid, clientID, token, err := database.CreateSession(
ctx, "stuck", "", "", "",
)
if err != nil {
t.Fatal(err)
}
_, err = database.GetDB().ExecContext(ctx,
`CREATE TRIGGER keep_sessions
BEFORE DELETE ON sessions
BEGIN
SELECT RAISE(ABORT, 'sessions are kept');
END`)
if err != nil {
t.Fatal(err)
}
_, err = database.LogoutClient(ctx, sid, clientID, "stuck", nil)
if err == nil {
t.Fatal("logout succeeded although the session was kept")
}
_, _, nick, err := database.GetSessionByToken(ctx, token)
if err != nil || nick != "stuck" {
t.Fatalf("client gone after a failed logout: %q, %v", nick, err)
} }
} }
+24 -3
View File
@@ -3395,11 +3395,15 @@ func (hdlr *Handlers) HandleLogout() http.HandlerFunc {
return return
} }
err := hdlr.svc.Logout( ctx := request.Context()
request.Context(), sessionID, clientID, nick,
err := hdlr.params.Database.DeleteClient(
ctx, clientID,
) )
if err != nil { if err != nil {
hdlr.log.Error("logout failed", "error", err) hdlr.log.Error(
"delete client failed", "error", err,
)
hdlr.respondError( hdlr.respondError(
writer, request, writer, request,
"internal error", "internal error",
@@ -3409,6 +3413,23 @@ func (hdlr *Handlers) HandleLogout() http.HandlerFunc {
return return
} }
// If no clients remain, clean up the user fully:
// part all channels (notifying members) and
// delete the session.
remaining, err := hdlr.params.Database.
ClientCountForSession(ctx, sessionID)
if err != nil {
hdlr.log.Error(
"client count check failed", "error", err,
)
}
if remaining == 0 {
hdlr.cleanupUser(
ctx, sessionID, nick,
)
}
hdlr.clearAuthCookie(writer) hdlr.clearAuthCookie(writer)
hdlr.respondJSON(writer, request, hdlr.respondJSON(writer, request,
+3 -493
View File
@@ -126,7 +126,6 @@ const (
privmsgCmd = "PRIVMSG" privmsgCmd = "PRIVMSG"
joinCmd = "JOIN" joinCmd = "JOIN"
apiMessages = "/api/v1/messages" apiMessages = "/api/v1/messages"
apiLogout = "/api/v1/logout"
apiSession = "/api/v1/session" apiSession = "/api/v1/session"
apiState = "/api/v1/state" apiState = "/api/v1/state"
authCookieName = "neoirc_auth" authCookieName = "neoirc_auth"
@@ -137,8 +136,6 @@ type testServer struct {
httpServer *httptest.Server httpServer *httptest.Server
t *testing.T t *testing.T
fxApp *fxtest.App fxApp *fxtest.App
broker *broker.Broker
database *db.Database
} }
func newTestServer( func newTestServer(
@@ -165,11 +162,7 @@ func newTestServerWith(
) )
copyTemplateDB(t, dbURL) copyTemplateDB(t, dbURL)
var ( var srv *server.Server
srv *server.Server
brk *broker.Broker
database *db.Database
)
app := fxtest.New(t, app := fxtest.New(t,
fx.Provide( fx.Provide(
@@ -202,14 +195,13 @@ func newTestServerWith(
return cfg, nil return cfg, nil
}, },
newTestDB, newTestDB,
broker.New,
stats.New, stats.New,
newTestHealthcheck, newTestHealthcheck,
newTestMiddleware, newTestMiddleware,
newTestHandlers, newTestHandlers,
newTestServerFx, newTestServerFx,
), ),
fx.Populate(&srv, &brk, &database), fx.Populate(&srv),
) )
app.RequireStart() app.RequireStart()
@@ -225,8 +217,6 @@ func newTestServerWith(
httpServer: httpSrv, httpServer: httpSrv,
t: t, t: t,
fxApp: app, fxApp: app,
broker: brk,
database: database,
} }
} }
@@ -301,8 +291,8 @@ func newTestHandlers(
database *db.Database, database *db.Database,
hcheck *healthcheck.Healthcheck, hcheck *healthcheck.Healthcheck,
tracker *stats.Tracker, tracker *stats.Tracker,
brk *broker.Broker,
) (*handlers.Handlers, error) { ) (*handlers.Handlers, error) {
brk := broker.New()
svc := service.New(service.Params{ //nolint:exhaustruct svc := service.New(service.Params{ //nolint:exhaustruct
Logger: log, Logger: log,
Config: cfg, Config: cfg,
@@ -1405,486 +1395,6 @@ func TestQuit(t *testing.T) {
} }
} }
// quitRaceTrials is how many times checkQuitRace repeats
// its race.
const quitRaceTrials = 5
// TestConcurrentLogoutsSendOneQuit checks that when both
// clients of a session log out at once, the session's QUIT
// is sent once.
func TestConcurrentLogoutsSendOneQuit(t *testing.T) {
checkQuitRace(t, func(tserver *testServer, tokens []string) {
tserver.postConcurrently(apiLogout, nil, tokens...)
})
}
// TestConcurrentQuitsSendOneQuit checks that when both
// clients of a session send QUIT at once, the session's
// QUIT is sent once.
func TestConcurrentQuitsSendOneQuit(t *testing.T) {
quit, err := json.Marshal(map[string]any{commandKey: irc.CmdQuit})
if err != nil {
t.Fatal(err)
}
checkQuitRace(t, func(tserver *testServer, tokens []string) {
tserver.postConcurrently(apiMessages, quit, tokens...)
})
}
// checkQuitRace gives a session a password and two
// clients, puts it on a channel with a second session, and
// calls leave with the tokens of the two clients. It checks
// that the second session then holds one QUIT from the
// first, and that the first session is deleted: its tokens
// are refused and its nick is free. It does all this
// quitRaceTrials times, each on a new server, because each
// trial logs in and logins are rate limited.
func checkQuitRace(
t *testing.T,
leave func(tserver *testServer, tokens []string),
) {
t.Helper()
const nick = "racingquitter"
for trial := range quitRaceTrials {
tserver := newTestServer(t)
firstToken := tserver.createSession(nick)
observerToken := tserver.createSession("raceobserver")
tserver.sendCommand(firstToken, map[string]any{
commandKey: irc.CmdPass, bodyKey: []string{testPassword},
})
tokens := []string{firstToken, tserver.login(nick)}
for _, token := range []string{firstToken, observerToken} {
tserver.sendCommand(token, map[string]any{
commandKey: joinCmd, toKey: "#quitrace",
})
}
_, lastID := tserver.pollMessages(observerToken, 0)
leave(tserver, tokens)
msgs, _ := tserver.pollMessages(observerToken, lastID)
quits := countQuits(msgs, nick)
if quits != 1 {
t.Errorf("trial %d: %d QUITs, want 1: %v", trial, quits, msgs)
}
for _, token := range tokens {
status, _ := tserver.getState(token)
if status != http.StatusUnauthorized {
t.Errorf("trial %d: state got %d, want 401", trial, status)
}
}
// createSession fails the test if the nick is taken.
tserver.createSession(nick)
}
}
// TestLogoutOfOneClientKeepsSession checks that when one of
// a session's two clients logs out, the other client's
// token still works, the session stays on its channel and
// its channel peer gets no QUIT, and that the other
// client's logout then sends one QUIT.
func TestLogoutOfOneClientKeepsSession(t *testing.T) {
const (
nick = "twoclients"
channel = "#logouts"
)
tserver := newTestServer(t)
firstToken := tserver.createSession(nick)
observerToken := tserver.createSession("logoutobserver")
tserver.sendCommand(firstToken, map[string]any{
commandKey: irc.CmdPass, bodyKey: []string{testPassword},
})
secondToken := tserver.login(nick)
for _, token := range []string{firstToken, observerToken} {
tserver.sendCommand(token, map[string]any{
commandKey: joinCmd, toKey: channel,
})
}
_, lastID := tserver.pollMessages(observerToken, 0)
tserver.logout(firstToken)
status, _ := tserver.getState(firstToken)
if status != http.StatusUnauthorized {
t.Errorf("logged-out client's state: got %d, want 401", status)
}
status, state := tserver.getState(secondToken)
if status != http.StatusOK {
t.Fatalf("other client's state: got %d, want 200", status)
}
names := channelNames(state)
if !slices.Equal(names, []string{channel}) {
t.Errorf("session's channels: got %v, want [%s]", names, channel)
}
msgs, lastID := tserver.pollMessages(observerToken, lastID)
quits := countQuits(msgs, nick)
if quits != 0 {
t.Errorf("after the first logout: %d QUITs, want 0: %v", quits, msgs)
}
tserver.logout(secondToken)
msgs, _ = tserver.pollMessages(observerToken, lastID)
quits = countQuits(msgs, nick)
if quits != 1 {
t.Errorf("after the second logout: %d QUITs, want 1: %v", quits, msgs)
}
}
// TestFailedLogoutChangesNothing checks that a logout whose
// session cannot be deleted answers 500 and changes
// nothing: the client's token still works, the session is
// still on its channel and its channel peer has no QUIT.
func TestFailedLogoutChangesNothing(t *testing.T) {
const (
nick = "stuckleaver"
channel = "#stuck"
)
tserver := newTestServer(t)
token := tserver.createSession(nick)
observerToken := tserver.createSession("stuckobserver")
for _, joiner := range []string{token, observerToken} {
tserver.sendCommand(joiner, map[string]any{
commandKey: joinCmd, toKey: channel,
})
}
_, lastID := tserver.pollMessages(observerToken, 0)
_, err := tserver.database.GetDB().ExecContext(t.Context(),
`CREATE TRIGGER keep_sessions
BEFORE DELETE ON sessions
BEGIN
SELECT RAISE(ABORT, 'sessions are kept');
END`)
if err != nil {
t.Fatal(err)
}
resp, err := doRequestAuth(
t, http.MethodPost, tserver.url(apiLogout), token, nil,
)
if err != nil {
t.Fatalf("logout: %v", err)
}
_ = resp.Body.Close()
if resp.StatusCode != http.StatusInternalServerError {
t.Errorf("logout: got %d, want 500", resp.StatusCode)
}
status, state := tserver.getState(token)
if status != http.StatusOK {
t.Fatalf("state after the failed logout: got %d, want 200", status)
}
names := channelNames(state)
if !slices.Equal(names, []string{channel}) {
t.Errorf("session's channels: got %v, want [%s]", names, channel)
}
msgs, _ := tserver.pollMessages(observerToken, lastID)
quits := countQuits(msgs, nick)
if quits != 0 {
t.Errorf("%d QUITs, want 0: %v", quits, msgs)
}
}
const (
// quitWakePollTimeout is the timeout, in seconds, of the
// long poll in checkQuitWakesPoll.
quitWakePollTimeout = 10
// quitWakeDeadline is how long checkQuitWakesPoll waits
// for that long poll to start waiting, and then to
// return once the session has left.
quitWakeDeadline = 5 * time.Second
// pollWaitStep is how often checkQuitWakesPoll checks
// whether the long poll is waiting yet.
pollWaitStep = 10 * time.Millisecond
)
// TestQuitWakesPeerLongPoll checks that a session's QUIT
// wakes the long poll of a user who shares a channel with
// it.
func TestQuitWakesPeerLongPoll(t *testing.T) {
const reason = "gone fishing"
checkQuitWakesPoll(t, reason, func(tserver *testServer, token string) {
tserver.sendCommand(token, map[string]any{
commandKey: irc.CmdQuit, bodyKey: []string{reason},
})
})
}
// TestLastLogoutWakesPeerLongPoll checks that the logout of
// a session's last client wakes the long poll of a user who
// shares a channel with it.
func TestLastLogoutWakesPeerLongPoll(t *testing.T) {
checkQuitWakesPoll(t, "Connection closed", func(tserver *testServer, token string) {
tserver.logout(token)
})
}
// checkQuitWakesPoll puts a session and a peer on a
// channel, and calls leave with the session's token while
// the peer long-polls with a timeout of quitWakePollTimeout
// seconds. It checks that the poll returns within
// quitWakeDeadline with the session's QUIT, whose body is
// reason.
func checkQuitWakesPoll(
t *testing.T,
reason string,
leave func(tserver *testServer, token string),
) {
t.Helper()
const (
nick = "wakequitter"
peerNick = "wakepeer"
)
tserver := newTestServer(t)
token := tserver.createSession(nick)
peerToken := tserver.createSession(peerNick)
for _, joiner := range []string{token, peerToken} {
tserver.sendCommand(joiner, map[string]any{
commandKey: joinCmd, toKey: "#wake",
})
}
_, lastID := tserver.pollMessages(peerToken, 0)
peerID, err := tserver.database.GetSessionByNick(t.Context(), peerNick)
if err != nil {
t.Fatalf("get session of %s: %v", peerNick, err)
}
var (
msgs []map[string]any
pollErr error
)
polled := make(chan struct{})
go func() {
defer close(polled)
msgs, pollErr = tserver.longPoll(peerToken, lastID, quitWakePollTimeout)
}()
deadline := time.Now().Add(quitWakeDeadline)
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)
select {
case <-polled:
case <-time.After(quitWakeDeadline):
t.Fatalf("the long poll did not return within %v", quitWakeDeadline)
}
if pollErr != nil {
t.Fatalf("long poll: %v", pollErr)
}
if len(msgs) != 1 || countQuits(msgs, nick) != 1 {
t.Fatalf("long poll: got %v, want one QUIT from %s", msgs, nick)
}
lines, _ := msgs[0][bodyKey].([]any)
if len(lines) != 1 || lines[0] != reason {
t.Errorf("QUIT body: got %v, want [%s]", lines, reason)
}
}
// channelNames returns the names of the channels in state,
// a response to GET /api/v1/state.
func channelNames(state map[string]any) []string {
channels, _ := state["channels"].([]any)
names := make([]string, 0, len(channels))
for _, entry := range channels {
info, _ := entry.(map[string]any)
name, _ := info["name"].(string)
names = append(names, name)
}
return names
}
// countQuits returns how many of msgs are a QUIT from nick.
func countQuits(msgs []map[string]any, nick string) int {
quits := 0
for _, msg := range msgs {
if msg[commandKey] == irc.CmdQuit && msg["from"] == nick {
quits++
}
}
return quits
}
// logout logs out the client token and fails the test
// unless the server answers 200.
func (tserver *testServer) logout(token string) {
tserver.t.Helper()
resp, err := doRequestAuth(
tserver.t, http.MethodPost,
tserver.url(apiLogout), token, nil,
)
if err != nil {
tserver.t.Fatalf("logout: %v", err)
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != http.StatusOK {
tserver.t.Fatalf("logout: status %d", resp.StatusCode)
}
}
// longPoll returns the messages after afterID that the
// client token has been sent, waiting up to timeout seconds
// for one if there are none yet. It returns errors instead
// of failing the test, so a goroutine can call it.
func (tserver *testServer) longPoll(
token string, afterID int64, timeout int,
) ([]map[string]any, error) {
resp, err := doRequestAuth(
tserver.t, http.MethodGet,
tserver.url(fmt.Sprintf(
"%s?timeout=%d&after=%d", apiMessages, timeout, afterID,
)),
token, nil,
)
if err != nil {
return nil, err
}
defer func() { _ = resp.Body.Close() }()
var result struct {
Messages []map[string]any `json:"messages"`
}
err = json.NewDecoder(resp.Body).Decode(&result)
if err != nil {
return nil, fmt.Errorf("decode poll: %w", err)
}
return result.Messages, nil
}
// login logs in to the session nick with testPassword and
// returns the new client's token.
func (tserver *testServer) login(nick string) string {
tserver.t.Helper()
body, err := json.Marshal(map[string]string{
nickKey: nick, passwordKey: testPassword,
})
if err != nil {
tserver.t.Fatalf("marshal login: %v", err)
}
resp, err := doRequest(
tserver.t, http.MethodPost,
tserver.url("/api/v1/login"), bytes.NewReader(body),
)
if err != nil {
tserver.t.Fatalf("login: %v", err)
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != http.StatusOK {
tserver.t.Fatalf("login: status %d", resp.StatusCode)
}
for _, cookie := range resp.Cookies() {
if cookie.Name == authCookieName {
return cookie.Value
}
}
tserver.t.Fatal("no auth cookie in login response")
return ""
}
// postConcurrently POSTs body to path once with each of
// tokens, all at once.
func (tserver *testServer) postConcurrently(
path string, body []byte, tokens ...string,
) {
tserver.t.Helper()
start := make(chan struct{})
var waitGroup sync.WaitGroup
for _, token := range tokens {
waitGroup.Add(1)
go func() {
defer waitGroup.Done()
<-start
resp, err := doRequestAuth(
tserver.t, http.MethodPost, tserver.url(path),
token, bytes.NewReader(body),
)
if err != nil {
tserver.t.Error(err)
return
}
_ = resp.Body.Close()
}()
}
close(start)
waitGroup.Wait()
}
func TestUnknownCommand(t *testing.T) { func TestUnknownCommand(t *testing.T) {
tserver := newTestServer(t) tserver := newTestServer(t)
token := tserver.createSession("cmdtest") token := tserver.createSession("cmdtest")
+40 -34
View File
@@ -826,55 +826,61 @@ func (s *Service) ChangeNick(
return nil return nil
} }
// BroadcastQuit parts the session from all its channels, // BroadcastQuit broadcasts a QUIT to all channel peers,
// deletes it, and queues a QUIT to the users who shared a // parts all channels, and deletes the session. Uses the
// channel with it, in one transaction, so that the QUIT is // FanOut pattern: one message row fanned out to all unique
// queued before anyone else can take the nick, and then // peer sessions.
// wakes those users' long polls. Of several calls for one
// session at once, only the one that deletes it sends the
// QUIT.
func (s *Service) BroadcastQuit( func (s *Service) BroadcastQuit(
ctx context.Context, ctx context.Context,
sessionID int64, sessionID int64,
nick, reason string, nick, reason string,
) { ) {
body, _ := json.Marshal([]string{reason}) //nolint:errchkjson channels, err := s.db.GetSessionChannels(
ctx, sessionID,
peerIDs, err := s.db.QuitSession(ctx, sessionID, nick, body) )
if err != nil { if err != nil {
s.log.Error("quit failed", "error", err)
return return
} }
for _, peerID := range peerIDs { notified := make(map[int64]bool)
s.broker.Notify(peerID)
}
}
// Logout deletes a client of the session and, if that was for _, ch := range channels {
// the session's last client, does what BroadcastQuit does. memberIDs, memErr := s.db.GetChannelMemberIDs(
// If that fails, nothing changes and Logout returns the ctx, ch.ID,
// error.
func (s *Service) Logout(
ctx context.Context,
sessionID, clientID int64,
nick string,
) error {
body, _ := json.Marshal([]string{"Connection closed"}) //nolint:errchkjson
peerIDs, err := s.db.LogoutClient(
ctx, sessionID, clientID, nick, body,
) )
if err != nil { if memErr != nil {
return fmt.Errorf("logout: %w", err) continue
} }
for _, peerID := range peerIDs { for _, mid := range memberIDs {
s.broker.Notify(peerID) if mid == sessionID || notified[mid] {
continue
} }
return nil notified[mid] = true
}
}
if len(notified) > 0 {
recipients := make([]int64, 0, len(notified))
for sid := range notified {
recipients = append(recipients, sid)
}
body, _ := json.Marshal([]string{reason}) //nolint:errchkjson
_, _, _ = s.FanOut(
ctx, irc.CmdQuit, nick, "", 0, 0,
nil, body, nil, recipients,
)
}
for _, ch := range channels {
s.db.PartChannel(ctx, ch.ID, sessionID) //nolint:errcheck,gosec
s.db.DeleteChannelIfEmpty(ctx, ch.ID) //nolint:errcheck,gosec
}
s.db.DeleteSession(ctx, sessionID) //nolint:errcheck,gosec
} }
// SetAway sets or clears the away message. Returns true // SetAway sets or clears the away message. Returns true
-202
View File
@@ -495,208 +495,6 @@ func TestJoinConcurrentWithLastPart(t *testing.T) {
} }
} }
// TestJoinConcurrentWithLastQuit checks that a user who
// sends JOIN without the key to a +k channel, again after
// each ERR_BADCHANNELKEY, while the channel's only member
// quits, gets in only once the channel is deleted, into a
// new channel of that name with no key.
func TestJoinConcurrentWithLastQuit(t *testing.T) {
env := newTestEnv(t)
ctx := t.Context()
for trial := range concurrentJoinTrials {
channel := fmt.Sprintf("#keyed%d", trial)
leaverNick := fmt.Sprintf("leaver%d", trial)
leaverSID := createSession(ctx, t, env.db, leaverNick)
joinerNick := fmt.Sprintf("joiner%d", trial)
joinerSID := createSession(ctx, t, env.db, joinerNick)
created, err := env.svc.JoinChannel(
ctx, leaverSID, leaverNick, channel, "",
)
if err != nil {
t.Fatalf("join %s: %v", leaverNick, err)
}
err = env.db.SetChannelKey(ctx, created.ChannelID, "secret")
if err != nil {
t.Fatalf("set +k: %v", err)
}
var joined *service.JoinResult
errs := runConcurrently(
func() error {
env.svc.BroadcastQuit(ctx, leaverSID, leaverNick, "")
return nil
},
// The database has one connection, so retried
// JOINs take turns with the QUIT's statements
// and land between any two that are not in one
// transaction. A JOIN after the QUIT gets in, so
// the loop ends.
func() error {
for {
var (
joinErr error
ircErr *service.IRCError
)
joined, joinErr = env.svc.JoinChannel(
ctx, joinerSID, joinerNick, channel, "",
)
if !errors.As(joinErr, &ircErr) ||
ircErr.Code != irc.ErrBadChannelKey {
return joinErr
}
}
},
)
if errs[1] != nil {
t.Fatalf("trial %d: JOIN: %v", trial, errs[1])
}
key, _ := env.db.GetChannelKey(ctx, joined.ChannelID)
if key != "" {
t.Errorf(
"trial %d: %s got into +k %s without the key",
trial, joinerNick, channel,
)
}
}
}
// quitCrowdSize is how many sessions share a second channel
// with the quitting session in
// TestQuitQueuedAheadOfNewNickHoldersJoin.
const quitCrowdSize = 10
// TestQuitQueuedAheadOfNewNickHoldersJoin checks that when
// a new session takes a quitting session's nick and joins a
// channel the quitting session shared with a watcher, while
// the QUIT is going out, the watcher has the QUIT queued
// ahead of that JOIN. The quitting session is also on a
// channel with quitCrowdSize other sessions, made before the
// watcher's channel, so that its QUIT reaches the watcher
// last.
func TestQuitQueuedAheadOfNewNickHoldersJoin(t *testing.T) {
env := newTestEnv(t)
ctx := t.Context()
for trial := range concurrentJoinTrials {
nick := fmt.Sprintf("reused%d", trial)
crowd := fmt.Sprintf("#crowd%d", trial)
shared := fmt.Sprintf("#shared%d", trial)
quitterSID := createSession(ctx, t, env.db, nick)
for _, channel := range []string{crowd, shared} {
_, err := env.svc.JoinChannel(ctx, quitterSID, nick, channel, "")
if err != nil {
t.Fatalf("join %s: %v", channel, err)
}
}
for member := range quitCrowdSize {
memberNick := fmt.Sprintf("crowd%dx%d", trial, member)
memberSID := createSession(ctx, t, env.db, memberNick)
_, err := env.svc.JoinChannel(ctx, memberSID, memberNick, crowd, "")
if err != nil {
t.Fatalf("join %s: %v", memberNick, err)
}
}
watcherNick := fmt.Sprintf("watcher%d", trial)
watcherSID, watcherClientID, _, err := env.db.CreateSession(
ctx, watcherNick, watcherNick, "localhost", "127.0.0.1",
)
if err != nil {
t.Fatalf("create session %s: %v", watcherNick, err)
}
_, err = env.svc.JoinChannel(ctx, watcherSID, watcherNick, shared, "")
if err != nil {
t.Fatalf("join %s: %v", watcherNick, err)
}
_, lastQueueID, err := env.db.PollMessages(ctx, watcherClientID, 0, 0)
if err != nil {
t.Fatalf("poll: %v", err)
}
errs := runConcurrently(
func() error {
env.svc.BroadcastQuit(ctx, quitterSID, nick, "")
return nil
},
func() error {
return takeNickAndJoin(ctx, env, nick, shared)
},
)
if errs[1] != nil {
t.Fatalf("trial %d: new session: %v", trial, errs[1])
}
msgs, _, err := env.db.PollMessages(
ctx, watcherClientID, lastQueueID, 0,
)
if err != nil {
t.Fatalf("poll: %v", err)
}
commands := commandsFrom(msgs, nick)
if !slices.Equal(commands, []string{irc.CmdQuit, irc.CmdJoin}) {
t.Errorf(
"trial %d: watcher got %v from %s, want QUIT then JOIN",
trial, commands, nick,
)
}
}
}
// takeNickAndJoin creates a session with nick, trying again
// while another session holds the nick, and joins it to
// channel.
func takeNickAndJoin(
ctx context.Context,
env *testEnv,
nick, channel string,
) error {
for {
sessionID, _, _, err := env.db.CreateSession(
ctx, nick, nick, "localhost", "127.0.0.1",
)
if db.IsUniqueConstraintError(err) {
continue
}
if err != nil {
return err
}
_, err = env.svc.JoinChannel(ctx, sessionID, nick, channel, "")
return err
}
}
// commandsFrom returns the commands of the messages in msgs
// that nick sent, in order.
func commandsFrom(msgs []db.IRCMessage, nick string) []string {
commands := make([]string, 0, len(msgs))
for _, msg := range msgs {
if msg.From == nick {
commands = append(commands, msg.Command)
}
}
return commands
}
// TestJoinUsesUpInvite checks that the JOIN an invite let // TestJoinUsesUpInvite checks that the JOIN an invite let
// 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.