CI / Test (ubuntu-latest) (push) Canceled after 0s
CI / Test (windows-latest) (push) Canceled after 0s
CI / Storage tests on PostgreSQL (push) Canceled after 0s
CI / Complement (federation off) (push) Canceled after 0s
CI / golangci-lint (push) Canceled after 0s
CI / Smoke test the binary (push) Canceled after 0s
Implements PLAN.md Phase 3: the roomserver, the room half of storage, the Client-Server room endpoints and the admin room endpoints. A client can now create a room, invite, join, leave, kick, ban, send messages and state, redact, name a room with an alias and find one in the public directory. Nothing talks to another server; every operation that would need to is refused with a message that says so rather than a bare M_FORBIDDEN. roomserver: one input pipeline every event goes through, whatever sent it — validate, authorise, resolve, persist, emit. The rules are not reimplemented here; internal/matrix is pure library code and this is what connects it to a database and to a room's history. The Client-Server handlers do no authorisation of their own, deliberately: whether Alice may kick Bob is a question about the room's power levels, and the one place that is answered correctly is the authorisation rules inside the pipeline. A mutex per room serialises writers. Two events racing into one room would each read the same forward extremities, both become one, and fork the room for no reason — which would then need state resolution to put back together. State resolution is wired in anyway for the multi-extremity case. It is unreachable from outside in this phase, and the alternative is a pipeline that authorises against one arbitrary branch of a fork without saying so. Room versions 3 to 12 all work and are each tested end to end. Version 12 earns that coverage: its room ID is the create event's own ID, its create event carries no room_id, and the create event is excluded from every other event's auth_events — so the state auth.Check is given is the event's auth_events plus the create event, which that version identifies by room ID rather than citing. createRoom sends the spec's events in the spec's order, each authorised against what the ones before it produced. A failure part-way leaves an incomplete room, which is the lesser evil: the alternative is holding every event of a new room in memory and writing them in one transaction, making the pipeline that authorises them a special case. The one failure a second client can cause at any time, an alias clash, takes the room row back out. Redactions stay two decisions, as the spec keeps them. The rules let almost any redaction into the room; whether it applies to its target is a separate test, run inside the same transaction as the redaction itself so that a client holding an event ID can always read the stripped event. storage: events keyed by event ID rather than by a surrogate, since an event ID appears in prev_events, auth_events, state snapshots and client requests alike. Stream ordering comes from a counter row bumped inside the write transaction, because a PostgreSQL sequence is explicitly not transactional and a rolled-back write would leave a hole in the stream that /sync would have to be taught to skip. State snapshots are stored per event and deduplicated by content hash, so the table grows with state changes rather than with traffic — and that is what makes "the state after a forward extremity" answerable at all. The room's listing columns are denormalised onto its row because the public directory orders every published room by member count and pages the result, which cannot be done by reading each room's state in turn. Federation-disabled behaviour from PLAN.md section 3 is enforced here. Restricted joins are the exception that works: the rule needs a signature from the authorising user's server verified, and while federation is off that server is always this one. adminapi: room list and detail, purge, force-leave, and room and event counters on the dashboard. clientapi gains its own rate-limit buckets for createRoom and for joining, both of which write events rather than read them. Tested against the real event core and a real database rather than fakes: what is under test is whether an event this server signs is one the authorisation rules accept, and a fake of either would only prove that the pipeline calls what the fake expects. The storage tests run against both SQL drivers. docs/rooms.md describes the pipeline, the storage decisions and the known gaps — chief among them that history visibility is room-level rather than per-event, and that /sync still reports no rooms, both of which belong with the event stream in Phase 4. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
327 lines
12 KiB
Go
327 lines
12 KiB
Go
package roomserver_test
|
|
|
|
import (
|
|
"errors"
|
|
"testing"
|
|
|
|
"github.com/dariuszjaniak/warp/server/internal/roomserver"
|
|
)
|
|
|
|
func TestAliasLifecycle(t *testing.T) {
|
|
f := newFixture(t, nil)
|
|
resp := f.createRoom(roomserver.CreateRoomRequest{Preset: roomserver.PresetPublicChat})
|
|
const alias = "#chat:" + testServerName
|
|
|
|
if err := f.api.CreateAlias(t.Context(), alias, resp.RoomID, alice); err != nil {
|
|
t.Fatalf("CreateAlias: %v", err)
|
|
}
|
|
resolved, err := f.api.ResolveAlias(t.Context(), alias)
|
|
if err != nil {
|
|
t.Fatalf("ResolveAlias: %v", err)
|
|
}
|
|
if resolved.RoomID != resp.RoomID {
|
|
t.Errorf("the alias resolves to %q, want %q", resolved.RoomID, resp.RoomID)
|
|
}
|
|
// A client needs somewhere to join through, which with federation off
|
|
// is this server.
|
|
if len(resolved.Servers) != 1 || resolved.Servers[0] != testServerName {
|
|
t.Errorf("the alias names servers %v, want [%s]", resolved.Servers, testServerName)
|
|
}
|
|
|
|
aliases, err := f.api.RoomAliases(t.Context(), resp.RoomID, alice)
|
|
if err != nil {
|
|
t.Fatalf("RoomAliases: %v", err)
|
|
}
|
|
if len(aliases) != 1 || aliases[0] != alias {
|
|
t.Errorf("RoomAliases = %v, want [%s]", aliases, alias)
|
|
}
|
|
|
|
// The creator of an alias may remove it.
|
|
if err := f.api.DeleteAlias(t.Context(), alias, alice, false); err != nil {
|
|
t.Fatalf("DeleteAlias: %v", err)
|
|
}
|
|
if _, err := f.api.ResolveAlias(t.Context(), alias); !errors.Is(err, roomserver.ErrNoSuchAlias) {
|
|
t.Errorf("the deleted alias still resolves: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestAliasPermissions(t *testing.T) {
|
|
f := newFixture(t, nil)
|
|
resp := f.createRoom(roomserver.CreateRoomRequest{Preset: roomserver.PresetPublicChat})
|
|
|
|
// Only a member may name a room, so that a stranger cannot squat names
|
|
// against rooms they cannot see.
|
|
err := f.api.CreateAlias(t.Context(), "#squat:"+testServerName, resp.RoomID, bob)
|
|
if !errors.Is(err, roomserver.ErrForbidden) {
|
|
t.Errorf("a stranger creating an alias = %v, want ErrForbidden", err)
|
|
}
|
|
|
|
if _, err := f.api.JoinRoom(t.Context(), roomserver.JoinRequest{
|
|
RoomID: resp.RoomID, User: bob,
|
|
}); err != nil {
|
|
t.Fatalf("bob joining: %v", err)
|
|
}
|
|
const bobsAlias = "#bobs:" + testServerName
|
|
if err := f.api.CreateAlias(t.Context(), bobsAlias, resp.RoomID, bob); err != nil {
|
|
t.Fatalf("CreateAlias as a member: %v", err)
|
|
}
|
|
|
|
// carol is nobody here and may not delete it.
|
|
if err := f.api.DeleteAlias(t.Context(), bobsAlias, carol, false); !errors.Is(err, roomserver.ErrForbidden) {
|
|
t.Errorf("a stranger deleting an alias = %v, want ErrForbidden", err)
|
|
}
|
|
// alice can, because she could set the room's canonical alias — which
|
|
// is what lets a room's moderators clean up after a departed member.
|
|
if err := f.api.DeleteAlias(t.Context(), bobsAlias, alice, false); err != nil {
|
|
t.Errorf("a moderator deleting a member's alias: %v", err)
|
|
}
|
|
|
|
// An alias of another server is not ours to create or resolve.
|
|
err = f.api.CreateAlias(t.Context(), "#elsewhere:other.example", resp.RoomID, alice)
|
|
if !errors.Is(err, roomserver.ErrForbidden) {
|
|
t.Errorf("creating a remote alias = %v, want ErrForbidden", err)
|
|
}
|
|
_, err = f.api.ResolveAlias(t.Context(), "#elsewhere:other.example")
|
|
if !errors.Is(err, roomserver.ErrFederationDisabled) {
|
|
t.Errorf("resolving a remote alias = %v, want ErrFederationDisabled", err)
|
|
}
|
|
|
|
// Malformed aliases are refused before anything is looked up.
|
|
for _, bad := range []string{"", "general", "#", "#general", "#:" + testServerName} {
|
|
if err := f.api.CreateAlias(t.Context(), bad, resp.RoomID, alice); !errors.Is(err, roomserver.ErrInvalidContent) {
|
|
t.Errorf("CreateAlias(%q) = %v, want ErrInvalidContent", bad, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestPublicRoomDirectory(t *testing.T) {
|
|
f := newFixture(t, nil)
|
|
|
|
private := f.createRoom(roomserver.CreateRoomRequest{Name: "Private"})
|
|
public := f.createRoom(roomserver.CreateRoomRequest{
|
|
Preset: roomserver.PresetPublicChat,
|
|
Visibility: roomserver.VisibilityPublic,
|
|
Name: "Public Room",
|
|
Topic: "Anybody may join",
|
|
})
|
|
|
|
resp, err := f.api.PublicRooms(t.Context(), roomserver.PublicRoomsRequest{})
|
|
if err != nil {
|
|
t.Fatalf("PublicRooms: %v", err)
|
|
}
|
|
if resp.TotalRoomCountEstimate != 1 || len(resp.Chunk) != 1 {
|
|
t.Fatalf("the directory has %d of %d rooms, want 1 of 1",
|
|
len(resp.Chunk), resp.TotalRoomCountEstimate)
|
|
}
|
|
entry := resp.Chunk[0]
|
|
if entry.RoomID != public.RoomID {
|
|
t.Errorf("the directory lists %q, want %q", entry.RoomID, public.RoomID)
|
|
}
|
|
if entry.Name != "Public Room" || entry.Topic != "Anybody may join" {
|
|
t.Errorf("the directory entry is %+v", entry)
|
|
}
|
|
if entry.NumJoinedMembers != 1 {
|
|
t.Errorf("num_joined_members = %d, want 1", entry.NumJoinedMembers)
|
|
}
|
|
if entry.WorldReadable {
|
|
t.Error("a shared-history room is reported as world-readable")
|
|
}
|
|
// The public_chat preset makes the room joinable by anyone with an
|
|
// account and closed to guests. The two are independent, which is
|
|
// exactly why the pairing is worth pinning down.
|
|
if entry.JoinRule != "public" {
|
|
t.Errorf("join_rule = %q, want public", entry.JoinRule)
|
|
}
|
|
if entry.GuestCanJoin {
|
|
t.Error("a public_chat room reports that guests may join")
|
|
}
|
|
|
|
// Publishing the private room puts it in, and the two then order by
|
|
// member count.
|
|
if err := f.api.SetRoomVisibility(t.Context(), private.RoomID, alice,
|
|
roomserver.VisibilityPublic, false); err != nil {
|
|
t.Fatalf("SetRoomVisibility: %v", err)
|
|
}
|
|
if visibility, err := f.api.RoomVisibility(t.Context(), private.RoomID); err != nil {
|
|
t.Fatalf("RoomVisibility: %v", err)
|
|
} else if visibility != roomserver.VisibilityPublic {
|
|
t.Errorf("visibility = %q, want public", visibility)
|
|
}
|
|
|
|
resp, err = f.api.PublicRooms(t.Context(), roomserver.PublicRoomsRequest{})
|
|
if err != nil {
|
|
t.Fatalf("PublicRooms: %v", err)
|
|
}
|
|
if resp.TotalRoomCountEstimate != 2 {
|
|
t.Errorf("the directory has %d rooms, want 2", resp.TotalRoomCountEstimate)
|
|
}
|
|
|
|
// The search looks at the name.
|
|
resp, err = f.api.PublicRooms(t.Context(), roomserver.PublicRoomsRequest{Search: "public"})
|
|
if err != nil {
|
|
t.Fatalf("PublicRooms with a search: %v", err)
|
|
}
|
|
if len(resp.Chunk) != 1 || resp.Chunk[0].RoomID != public.RoomID {
|
|
t.Errorf("searching for \"public\" returned %+v", resp.Chunk)
|
|
}
|
|
|
|
// Withdrawing a room takes it back out.
|
|
if err := f.api.SetRoomVisibility(t.Context(), public.RoomID, alice,
|
|
roomserver.VisibilityPrivate, false); err != nil {
|
|
t.Fatalf("SetRoomVisibility: %v", err)
|
|
}
|
|
resp, err = f.api.PublicRooms(t.Context(), roomserver.PublicRoomsRequest{})
|
|
if err != nil {
|
|
t.Fatalf("PublicRooms: %v", err)
|
|
}
|
|
if resp.TotalRoomCountEstimate != 1 {
|
|
t.Errorf("the directory has %d rooms after a withdrawal, want 1", resp.TotalRoomCountEstimate)
|
|
}
|
|
}
|
|
|
|
func TestPublicRoomDirectoryPaging(t *testing.T) {
|
|
f := newFixture(t, nil)
|
|
for range 5 {
|
|
f.createRoom(roomserver.CreateRoomRequest{
|
|
Preset: roomserver.PresetPublicChat,
|
|
Visibility: roomserver.VisibilityPublic,
|
|
})
|
|
}
|
|
|
|
first, err := f.api.PublicRooms(t.Context(), roomserver.PublicRoomsRequest{Limit: 2})
|
|
if err != nil {
|
|
t.Fatalf("PublicRooms: %v", err)
|
|
}
|
|
if len(first.Chunk) != 2 || first.NextBatch == "" || first.PrevBatch != "" {
|
|
t.Fatalf("the first page is %d rooms, next=%q prev=%q",
|
|
len(first.Chunk), first.NextBatch, first.PrevBatch)
|
|
}
|
|
|
|
second, err := f.api.PublicRooms(t.Context(), roomserver.PublicRoomsRequest{
|
|
Limit: 2, Since: first.NextBatch,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("PublicRooms page two: %v", err)
|
|
}
|
|
if len(second.Chunk) != 2 || second.PrevBatch == "" {
|
|
t.Fatalf("the second page is %d rooms, prev=%q", len(second.Chunk), second.PrevBatch)
|
|
}
|
|
if second.Chunk[0].RoomID == first.Chunk[0].RoomID {
|
|
t.Error("the second page repeats the first")
|
|
}
|
|
|
|
last, err := f.api.PublicRooms(t.Context(), roomserver.PublicRoomsRequest{
|
|
Limit: 2, Since: second.NextBatch,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("PublicRooms page three: %v", err)
|
|
}
|
|
if len(last.Chunk) != 1 || last.NextBatch != "" {
|
|
t.Errorf("the last page is %d rooms, next=%q", len(last.Chunk), last.NextBatch)
|
|
}
|
|
|
|
// A token this server did not issue is refused rather than clamped: a
|
|
// client that made one up should hear about it.
|
|
if _, err := f.api.PublicRooms(t.Context(), roomserver.PublicRoomsRequest{
|
|
Since: "not-a-token",
|
|
}); !errors.Is(err, roomserver.ErrInvalidContent) {
|
|
t.Errorf("an invented pagination token = %v, want ErrInvalidContent", err)
|
|
}
|
|
}
|
|
|
|
func TestPublicRoomDirectoryPermissions(t *testing.T) {
|
|
f := newFixture(t, nil)
|
|
resp := f.createRoom(roomserver.CreateRoomRequest{Preset: roomserver.PresetPublicChat})
|
|
|
|
if _, err := f.api.JoinRoom(t.Context(), roomserver.JoinRequest{
|
|
RoomID: resp.RoomID, User: bob,
|
|
}); err != nil {
|
|
t.Fatalf("bob joining: %v", err)
|
|
}
|
|
|
|
// An ordinary member cannot publish the room: a room published by a
|
|
// passer-by cannot be taken back by its moderators.
|
|
err := f.api.SetRoomVisibility(t.Context(), resp.RoomID, bob,
|
|
roomserver.VisibilityPublic, false)
|
|
if !errors.Is(err, roomserver.ErrForbidden) {
|
|
t.Errorf("a member publishing a room = %v, want ErrForbidden", err)
|
|
}
|
|
// An admin can, whoever they are.
|
|
if err := f.api.SetRoomVisibility(t.Context(), resp.RoomID, bob,
|
|
roomserver.VisibilityPublic, true); err != nil {
|
|
t.Errorf("an admin publishing a room: %v", err)
|
|
}
|
|
|
|
// A value that is neither is a bad request, not a silent no-op.
|
|
err = f.api.SetRoomVisibility(t.Context(), resp.RoomID, alice, "secret", false)
|
|
if !errors.Is(err, roomserver.ErrInvalidContent) {
|
|
t.Errorf("an invented visibility = %v, want ErrInvalidContent", err)
|
|
}
|
|
|
|
// Another server's directory needs federation.
|
|
_, err = f.api.PublicRooms(t.Context(), roomserver.PublicRoomsRequest{Server: "other.example"})
|
|
if !errors.Is(err, roomserver.ErrFederationDisabled) {
|
|
t.Errorf("reading a remote directory = %v, want ErrFederationDisabled", err)
|
|
}
|
|
}
|
|
|
|
func TestAdminRoomListAndPurge(t *testing.T) {
|
|
f := newFixture(t, nil)
|
|
resp := f.createRoom(roomserver.CreateRoomRequest{
|
|
Preset: roomserver.PresetPublicChat,
|
|
Name: "Doomed",
|
|
RoomAliasName: "doomed",
|
|
})
|
|
if _, err := f.api.JoinRoom(t.Context(), roomserver.JoinRequest{
|
|
RoomID: resp.RoomID, User: bob,
|
|
}); err != nil {
|
|
t.Fatalf("bob joining: %v", err)
|
|
}
|
|
if _, err := f.sendMessage(resp.RoomID, alice, "hello", "m1"); err != nil {
|
|
t.Fatalf("sending a message: %v", err)
|
|
}
|
|
|
|
details, err := f.api.RoomDetails(t.Context(), resp.RoomID)
|
|
if err != nil {
|
|
t.Fatalf("RoomDetails: %v", err)
|
|
}
|
|
if details.Name != "Doomed" || details.JoinedMembers != 2 || details.Members != 2 {
|
|
t.Errorf("RoomDetails = %+v", details)
|
|
}
|
|
if len(details.Aliases) != 1 {
|
|
t.Errorf("RoomDetails lists %v aliases, want one", details.Aliases)
|
|
}
|
|
// A room filled by one server has a single forward extremity; more
|
|
// than one would mean it had forked.
|
|
if len(details.ForwardExtremities) != 1 {
|
|
t.Errorf("the room has %d forward extremities, want 1", len(details.ForwardExtremities))
|
|
}
|
|
|
|
stats, err := f.api.Stats(t.Context())
|
|
if err != nil {
|
|
t.Fatalf("Stats: %v", err)
|
|
}
|
|
if stats.Rooms != 1 || stats.Published != 0 || stats.Events == 0 {
|
|
t.Errorf("Stats = %+v", stats)
|
|
}
|
|
|
|
if err := f.api.PurgeRoom(t.Context(), resp.RoomID); err != nil {
|
|
t.Fatalf("PurgeRoom: %v", err)
|
|
}
|
|
if _, err := f.api.RoomDetails(t.Context(), resp.RoomID); !errors.Is(err, roomserver.ErrNoSuchRoom) {
|
|
t.Errorf("the purged room is still there: %v", err)
|
|
}
|
|
// The alias went with it, so the name can be used again.
|
|
if _, err := f.api.ResolveAlias(t.Context(), resp.Alias); !errors.Is(err, roomserver.ErrNoSuchAlias) {
|
|
t.Errorf("the purged room's alias still resolves: %v", err)
|
|
}
|
|
if rooms, err := f.api.JoinedRooms(t.Context(), bob); err != nil {
|
|
t.Fatalf("JoinedRooms: %v", err)
|
|
} else if len(rooms) != 0 {
|
|
t.Errorf("bob is still joined to %v after the purge", rooms)
|
|
}
|
|
if err := f.api.PurgeRoom(t.Context(), resp.RoomID); !errors.Is(err, roomserver.ErrNoSuchRoom) {
|
|
t.Errorf("purging twice = %v, want ErrNoSuchRoom", err)
|
|
}
|
|
}
|