2024-10-17 16:16:37 +01:00
|
|
|
// Copyright 2024 New Vector Ltd.
|
2022-04-08 10:12:30 +01:00
|
|
|
// Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
//
|
2024-10-18 15:14:08 +01:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
|
|
|
|
// Please see LICENSE files in the repository root for full details.
|
2022-04-08 10:12:30 +01:00
|
|
|
|
|
|
|
package test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"sync/atomic"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2023-04-19 15:50:33 +01:00
|
|
|
"github.com/matrix-org/gomatrixserverlib/spec"
|
2022-10-14 08:14:54 +01:00
|
|
|
|
2024-10-17 16:33:45 +01:00
|
|
|
"github.com/element-hq/dendrite/internal/eventutil"
|
|
|
|
rstypes "github.com/element-hq/dendrite/roomserver/types"
|
2022-04-08 10:12:30 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type Preset int
|
|
|
|
|
|
|
|
var (
|
|
|
|
PresetNone Preset = 0
|
|
|
|
PresetPrivateChat Preset = 1
|
|
|
|
PresetPublicChat Preset = 2
|
|
|
|
PresetTrustedPrivateChat Preset = 3
|
|
|
|
|
|
|
|
roomIDCounter = int64(0)
|
|
|
|
)
|
|
|
|
|
2023-06-14 15:23:46 +01:00
|
|
|
func UserIDForSender(roomID spec.RoomID, senderID spec.SenderID) (*spec.UserID, error) {
|
2023-06-07 18:14:35 +01:00
|
|
|
return spec.NewUserID(string(senderID), true)
|
2023-06-06 21:55:18 +01:00
|
|
|
}
|
|
|
|
|
2022-04-08 10:12:30 +01:00
|
|
|
type Room struct {
|
2022-12-22 12:05:59 +00:00
|
|
|
ID string
|
|
|
|
Version gomatrixserverlib.RoomVersion
|
|
|
|
preset Preset
|
|
|
|
guestCanJoin bool
|
|
|
|
visibility gomatrixserverlib.HistoryVisibility
|
|
|
|
creator *User
|
2022-04-08 10:12:30 +01:00
|
|
|
|
2022-05-17 13:23:35 +01:00
|
|
|
authEvents gomatrixserverlib.AuthEvents
|
2023-04-27 12:54:20 +01:00
|
|
|
currentState map[string]*rstypes.HeaderedEvent
|
|
|
|
events []*rstypes.HeaderedEvent
|
2022-04-08 10:12:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new test room. Automatically creates the initial create events.
|
|
|
|
func NewRoom(t *testing.T, creator *User, modifiers ...roomModifier) *Room {
|
|
|
|
t.Helper()
|
|
|
|
counter := atomic.AddInt64(&roomIDCounter, 1)
|
2022-05-17 13:23:35 +01:00
|
|
|
if creator.srvName == "" {
|
|
|
|
t.Fatalf("NewRoom: creator doesn't belong to a server: %+v", *creator)
|
|
|
|
}
|
2022-04-08 10:12:30 +01:00
|
|
|
r := &Room{
|
2022-05-17 13:23:35 +01:00
|
|
|
ID: fmt.Sprintf("!%d:%s", counter, creator.srvName),
|
|
|
|
creator: creator,
|
|
|
|
authEvents: gomatrixserverlib.NewAuthEvents(nil),
|
|
|
|
preset: PresetPublicChat,
|
|
|
|
Version: gomatrixserverlib.RoomVersionV9,
|
2023-04-27 12:54:20 +01:00
|
|
|
currentState: make(map[string]*rstypes.HeaderedEvent),
|
2022-08-11 17:23:35 +01:00
|
|
|
visibility: gomatrixserverlib.HistoryVisibilityShared,
|
2022-04-08 10:12:30 +01:00
|
|
|
}
|
|
|
|
for _, m := range modifiers {
|
|
|
|
m(t, r)
|
|
|
|
}
|
|
|
|
r.insertCreateEvents(t)
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2023-05-24 11:14:42 +01:00
|
|
|
func (r *Room) MustGetAuthEventRefsForEvent(t *testing.T, needed gomatrixserverlib.StateNeeded) []string {
|
2022-05-17 13:23:35 +01:00
|
|
|
t.Helper()
|
|
|
|
a, err := needed.AuthEventReferences(&r.authEvents)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("MustGetAuthEvents: %v", err)
|
|
|
|
}
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Room) ForwardExtremities() []string {
|
|
|
|
if len(r.events) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return []string{
|
|
|
|
r.events[len(r.events)-1].EventID(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-08 10:12:30 +01:00
|
|
|
func (r *Room) insertCreateEvents(t *testing.T) {
|
|
|
|
t.Helper()
|
|
|
|
var joinRule gomatrixserverlib.JoinRuleContent
|
|
|
|
var hisVis gomatrixserverlib.HistoryVisibilityContent
|
|
|
|
plContent := eventutil.InitialPowerLevelsContent(r.creator.ID)
|
|
|
|
switch r.preset {
|
|
|
|
case PresetTrustedPrivateChat:
|
|
|
|
fallthrough
|
|
|
|
case PresetPrivateChat:
|
|
|
|
joinRule.JoinRule = "invite"
|
2022-08-11 17:23:35 +01:00
|
|
|
hisVis.HistoryVisibility = gomatrixserverlib.HistoryVisibilityShared
|
2022-04-08 10:12:30 +01:00
|
|
|
case PresetPublicChat:
|
|
|
|
joinRule.JoinRule = "public"
|
2022-08-11 17:23:35 +01:00
|
|
|
hisVis.HistoryVisibility = gomatrixserverlib.HistoryVisibilityShared
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.visibility != "" {
|
|
|
|
hisVis.HistoryVisibility = r.visibility
|
2022-04-08 10:12:30 +01:00
|
|
|
}
|
2022-05-17 13:23:35 +01:00
|
|
|
|
2023-04-19 15:50:33 +01:00
|
|
|
r.CreateAndInsert(t, r.creator, spec.MRoomCreate, map[string]interface{}{
|
2022-04-08 10:12:30 +01:00
|
|
|
"creator": r.creator.ID,
|
|
|
|
"room_version": r.Version,
|
|
|
|
}, WithStateKey(""))
|
2023-04-19 15:50:33 +01:00
|
|
|
r.CreateAndInsert(t, r.creator, spec.MRoomMember, map[string]interface{}{
|
2022-04-08 10:12:30 +01:00
|
|
|
"membership": "join",
|
|
|
|
}, WithStateKey(r.creator.ID))
|
2023-04-19 15:50:33 +01:00
|
|
|
r.CreateAndInsert(t, r.creator, spec.MRoomPowerLevels, plContent, WithStateKey(""))
|
|
|
|
r.CreateAndInsert(t, r.creator, spec.MRoomJoinRules, joinRule, WithStateKey(""))
|
|
|
|
r.CreateAndInsert(t, r.creator, spec.MRoomHistoryVisibility, hisVis, WithStateKey(""))
|
2022-12-22 12:05:59 +00:00
|
|
|
if r.guestCanJoin {
|
2023-04-19 15:50:33 +01:00
|
|
|
r.CreateAndInsert(t, r.creator, spec.MRoomGuestAccess, map[string]string{
|
2022-12-22 12:05:59 +00:00
|
|
|
"guest_access": "can_join",
|
|
|
|
}, WithStateKey(""))
|
|
|
|
}
|
2022-04-08 10:12:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create an event in this room but do not insert it. Does not modify the room in any way (depth, fwd extremities, etc) so is thread-safe.
|
2023-04-27 12:54:20 +01:00
|
|
|
func (r *Room) CreateEvent(t *testing.T, creator *User, eventType string, content interface{}, mods ...eventModifier) *rstypes.HeaderedEvent {
|
2022-04-08 10:12:30 +01:00
|
|
|
t.Helper()
|
|
|
|
depth := 1 + len(r.events) // depth starts at 1
|
|
|
|
|
|
|
|
// possible event modifiers (optional fields)
|
|
|
|
mod := &eventMods{}
|
|
|
|
for _, m := range mods {
|
|
|
|
m(mod)
|
|
|
|
}
|
|
|
|
|
|
|
|
if mod.privKey == nil {
|
2022-05-17 13:23:35 +01:00
|
|
|
mod.privKey = creator.privKey
|
2022-04-08 10:12:30 +01:00
|
|
|
}
|
|
|
|
if mod.keyID == "" {
|
2022-05-17 13:23:35 +01:00
|
|
|
mod.keyID = creator.keyID
|
2022-04-08 10:12:30 +01:00
|
|
|
}
|
|
|
|
if mod.originServerTS.IsZero() {
|
|
|
|
mod.originServerTS = time.Now()
|
|
|
|
}
|
|
|
|
if mod.origin == "" {
|
2022-05-17 13:23:35 +01:00
|
|
|
mod.origin = creator.srvName
|
2022-04-08 10:12:30 +01:00
|
|
|
}
|
|
|
|
|
2023-04-19 15:50:33 +01:00
|
|
|
var unsigned spec.RawJSON
|
2022-04-08 10:12:30 +01:00
|
|
|
var err error
|
|
|
|
if mod.unsigned != nil {
|
|
|
|
unsigned, err = json.Marshal(mod.unsigned)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("CreateEvent[%s]: failed to marshal unsigned field: %s", eventType, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-04 11:17:42 +01:00
|
|
|
builder := gomatrixserverlib.MustGetRoomVersion(r.Version).NewEventBuilderFromProtoEvent(&gomatrixserverlib.ProtoEvent{
|
2023-06-07 18:14:35 +01:00
|
|
|
SenderID: creator.ID,
|
2022-04-08 10:12:30 +01:00
|
|
|
RoomID: r.ID,
|
|
|
|
Type: eventType,
|
|
|
|
StateKey: mod.stateKey,
|
|
|
|
Depth: int64(depth),
|
|
|
|
Unsigned: unsigned,
|
2023-05-04 11:17:42 +01:00
|
|
|
})
|
2022-04-08 10:12:30 +01:00
|
|
|
err = builder.SetContent(content)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("CreateEvent[%s]: failed to SetContent: %s", eventType, err)
|
|
|
|
}
|
|
|
|
if depth > 1 {
|
2023-05-24 11:14:42 +01:00
|
|
|
builder.PrevEvents = []string{r.events[len(r.events)-1].EventID()}
|
2022-04-08 10:12:30 +01:00
|
|
|
}
|
|
|
|
|
2023-05-04 11:17:42 +01:00
|
|
|
err = builder.AddAuthEvents(&r.authEvents)
|
2022-04-08 10:12:30 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("CreateEvent[%s]: failed to AuthEventReferences: %s", eventType, err)
|
|
|
|
}
|
2022-10-14 08:14:54 +01:00
|
|
|
|
|
|
|
if len(mod.authEvents) > 0 {
|
|
|
|
builder.AuthEvents = mod.authEvents
|
|
|
|
}
|
|
|
|
|
2022-04-08 10:12:30 +01:00
|
|
|
ev, err := builder.Build(
|
|
|
|
mod.originServerTS, mod.origin, mod.keyID,
|
2023-05-04 11:17:42 +01:00
|
|
|
mod.privKey,
|
2022-04-08 10:12:30 +01:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("CreateEvent[%s]: failed to build event: %s", eventType, err)
|
|
|
|
}
|
2023-06-06 21:55:18 +01:00
|
|
|
if err = gomatrixserverlib.Allowed(ev, &r.authEvents, UserIDForSender); err != nil {
|
2022-04-08 10:12:30 +01:00
|
|
|
t.Fatalf("CreateEvent[%s]: failed to verify event was allowed: %s", eventType, err)
|
|
|
|
}
|
2023-05-02 15:03:16 +01:00
|
|
|
headeredEvent := &rstypes.HeaderedEvent{PDU: ev}
|
2022-08-11 17:23:35 +01:00
|
|
|
headeredEvent.Visibility = r.visibility
|
|
|
|
return headeredEvent
|
2022-04-08 10:12:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add a new event to this room DAG. Not thread-safe.
|
2023-04-27 12:54:20 +01:00
|
|
|
func (r *Room) InsertEvent(t *testing.T, he *rstypes.HeaderedEvent) {
|
2022-04-08 10:12:30 +01:00
|
|
|
t.Helper()
|
2022-05-17 13:23:35 +01:00
|
|
|
// Add the event to the list of auth/state events
|
2022-04-08 10:12:30 +01:00
|
|
|
r.events = append(r.events, he)
|
|
|
|
if he.StateKey() != nil {
|
2023-05-02 15:03:16 +01:00
|
|
|
err := r.authEvents.AddEvent(he.PDU)
|
2022-04-08 10:12:30 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("InsertEvent: failed to add event to auth events: %s", err)
|
|
|
|
}
|
2022-05-17 13:23:35 +01:00
|
|
|
r.currentState[he.Type()+" "+*he.StateKey()] = he
|
2022-04-08 10:12:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-27 12:54:20 +01:00
|
|
|
func (r *Room) Events() []*rstypes.HeaderedEvent {
|
2022-04-08 10:12:30 +01:00
|
|
|
return r.events
|
|
|
|
}
|
|
|
|
|
2023-04-27 12:54:20 +01:00
|
|
|
func (r *Room) CurrentState() []*rstypes.HeaderedEvent {
|
|
|
|
events := make([]*rstypes.HeaderedEvent, len(r.currentState))
|
2022-05-17 13:23:35 +01:00
|
|
|
i := 0
|
|
|
|
for _, e := range r.currentState {
|
|
|
|
events[i] = e
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
return events
|
|
|
|
}
|
|
|
|
|
2023-04-27 12:54:20 +01:00
|
|
|
func (r *Room) CreateAndInsert(t *testing.T, creator *User, eventType string, content interface{}, mods ...eventModifier) *rstypes.HeaderedEvent {
|
2022-04-08 10:12:30 +01:00
|
|
|
t.Helper()
|
|
|
|
he := r.CreateEvent(t, creator, eventType, content, mods...)
|
|
|
|
r.InsertEvent(t, he)
|
|
|
|
return he
|
|
|
|
}
|
|
|
|
|
|
|
|
// All room modifiers are below
|
|
|
|
|
|
|
|
type roomModifier func(t *testing.T, r *Room)
|
|
|
|
|
|
|
|
func RoomPreset(p Preset) roomModifier {
|
|
|
|
return func(t *testing.T, r *Room) {
|
|
|
|
switch p {
|
|
|
|
case PresetPrivateChat:
|
|
|
|
fallthrough
|
|
|
|
case PresetPublicChat:
|
|
|
|
fallthrough
|
|
|
|
case PresetTrustedPrivateChat:
|
|
|
|
fallthrough
|
|
|
|
case PresetNone:
|
|
|
|
r.preset = p
|
|
|
|
default:
|
|
|
|
t.Errorf("invalid RoomPreset: %v", p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-11 17:23:35 +01:00
|
|
|
func RoomHistoryVisibility(vis gomatrixserverlib.HistoryVisibility) roomModifier {
|
|
|
|
return func(t *testing.T, r *Room) {
|
|
|
|
r.visibility = vis
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-08 10:12:30 +01:00
|
|
|
func RoomVersion(ver gomatrixserverlib.RoomVersion) roomModifier {
|
|
|
|
return func(t *testing.T, r *Room) {
|
|
|
|
r.Version = ver
|
|
|
|
}
|
|
|
|
}
|
2022-12-22 12:05:59 +00:00
|
|
|
|
|
|
|
func GuestsCanJoin(canJoin bool) roomModifier {
|
|
|
|
return func(t *testing.T, r *Room) {
|
|
|
|
r.guestCanJoin = canJoin
|
|
|
|
}
|
|
|
|
}
|