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 (
|
2022-04-08 17:53:24 +01:00
|
|
|
"bytes"
|
2022-04-08 10:12:30 +01:00
|
|
|
"crypto/ed25519"
|
2022-04-08 17:53:24 +01:00
|
|
|
"testing"
|
2022-04-08 10:12:30 +01:00
|
|
|
"time"
|
|
|
|
|
2024-10-17 16:33:45 +01:00
|
|
|
"github.com/element-hq/dendrite/roomserver/types"
|
2022-04-08 10:12:30 +01:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2023-04-19 15:50:33 +01:00
|
|
|
"github.com/matrix-org/gomatrixserverlib/spec"
|
2022-04-08 10:12:30 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type eventMods struct {
|
|
|
|
originServerTS time.Time
|
2023-04-19 15:50:33 +01:00
|
|
|
origin spec.ServerName
|
2022-04-08 10:12:30 +01:00
|
|
|
stateKey *string
|
|
|
|
unsigned interface{}
|
|
|
|
keyID gomatrixserverlib.KeyID
|
|
|
|
privKey ed25519.PrivateKey
|
2022-10-14 08:14:54 +01:00
|
|
|
authEvents []string
|
2022-04-08 10:12:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type eventModifier func(e *eventMods)
|
|
|
|
|
|
|
|
func WithTimestamp(ts time.Time) eventModifier {
|
|
|
|
return func(e *eventMods) {
|
|
|
|
e.originServerTS = ts
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithStateKey(skey string) eventModifier {
|
|
|
|
return func(e *eventMods) {
|
|
|
|
e.stateKey = &skey
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithUnsigned(unsigned interface{}) eventModifier {
|
|
|
|
return func(e *eventMods) {
|
|
|
|
e.unsigned = unsigned
|
|
|
|
}
|
|
|
|
}
|
2022-04-08 17:53:24 +01:00
|
|
|
|
2022-10-14 08:14:54 +01:00
|
|
|
func WithAuthIDs(evs []string) eventModifier {
|
|
|
|
return func(e *eventMods) {
|
|
|
|
e.authEvents = evs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-17 13:23:35 +01:00
|
|
|
func WithKeyID(keyID gomatrixserverlib.KeyID) eventModifier {
|
|
|
|
return func(e *eventMods) {
|
|
|
|
e.keyID = keyID
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithPrivateKey(pkey ed25519.PrivateKey) eventModifier {
|
|
|
|
return func(e *eventMods) {
|
|
|
|
e.privKey = pkey
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-19 15:50:33 +01:00
|
|
|
func WithOrigin(origin spec.ServerName) eventModifier {
|
2022-05-17 13:23:35 +01:00
|
|
|
return func(e *eventMods) {
|
|
|
|
e.origin = origin
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-08 17:53:24 +01:00
|
|
|
// Reverse a list of events
|
2023-04-27 12:54:20 +01:00
|
|
|
func Reversed(in []*types.HeaderedEvent) []*types.HeaderedEvent {
|
|
|
|
out := make([]*types.HeaderedEvent, len(in))
|
2022-04-08 17:53:24 +01:00
|
|
|
for i := 0; i < len(in); i++ {
|
|
|
|
out[i] = in[len(in)-i-1]
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2023-04-27 12:54:20 +01:00
|
|
|
func AssertEventIDsEqual(t *testing.T, gotEventIDs []string, wants []*types.HeaderedEvent) {
|
2022-04-08 17:53:24 +01:00
|
|
|
t.Helper()
|
|
|
|
if len(gotEventIDs) != len(wants) {
|
2022-05-11 13:44:32 +01:00
|
|
|
t.Errorf("length mismatch: got %d events, want %d", len(gotEventIDs), len(wants))
|
|
|
|
return
|
2022-04-08 17:53:24 +01:00
|
|
|
}
|
|
|
|
for i := range wants {
|
|
|
|
w := wants[i].EventID()
|
|
|
|
g := gotEventIDs[i]
|
|
|
|
if w != g {
|
|
|
|
t.Errorf("event at index %d mismatch:\ngot %s\n\nwant %s", i, string(g), string(w))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-27 12:54:20 +01:00
|
|
|
func AssertEventsEqual(t *testing.T, gots, wants []*types.HeaderedEvent) {
|
2022-04-08 17:53:24 +01:00
|
|
|
t.Helper()
|
|
|
|
if len(gots) != len(wants) {
|
|
|
|
t.Fatalf("length mismatch: got %d events, want %d", len(gots), len(wants))
|
|
|
|
}
|
|
|
|
for i := range wants {
|
|
|
|
w := wants[i].JSON()
|
|
|
|
g := gots[i].JSON()
|
|
|
|
if !bytes.Equal(w, g) {
|
|
|
|
t.Errorf("event at index %d mismatch:\ngot %s\n\nwant %s", i, string(g), string(w))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|