2022-12-12 07:20:59 +00:00
|
|
|
package shared_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-06-12 11:45:42 +01:00
|
|
|
"crypto/ed25519"
|
2022-12-12 07:20:59 +00:00
|
|
|
"testing"
|
2023-02-24 08:40:20 +00:00
|
|
|
"time"
|
2022-12-12 07:20:59 +00:00
|
|
|
|
2023-02-24 08:40:20 +00:00
|
|
|
"github.com/matrix-org/dendrite/internal/caching"
|
2023-06-13 15:28:41 +01:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/types"
|
2023-06-12 11:45:42 +01:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
|
|
|
"github.com/matrix-org/gomatrixserverlib/spec"
|
2022-12-12 07:20:59 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2023-06-12 11:45:42 +01:00
|
|
|
ed255192 "golang.org/x/crypto/ed25519"
|
2022-12-12 07:20:59 +00:00
|
|
|
|
|
|
|
"github.com/matrix-org/dendrite/internal/sqlutil"
|
|
|
|
"github.com/matrix-org/dendrite/roomserver/storage/postgres"
|
|
|
|
"github.com/matrix-org/dendrite/roomserver/storage/shared"
|
|
|
|
"github.com/matrix-org/dendrite/roomserver/storage/sqlite3"
|
|
|
|
"github.com/matrix-org/dendrite/roomserver/storage/tables"
|
|
|
|
"github.com/matrix-org/dendrite/setup/config"
|
|
|
|
"github.com/matrix-org/dendrite/test"
|
|
|
|
)
|
|
|
|
|
|
|
|
func mustCreateRoomserverDatabase(t *testing.T, dbType test.DBType) (*shared.Database, func()) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
connStr, clearDB := test.PrepareDBConnectionString(t, dbType)
|
|
|
|
dbOpts := &config.DatabaseOptions{ConnectionString: config.DataSource(connStr)}
|
|
|
|
|
2023-06-12 11:45:42 +01:00
|
|
|
writer := sqlutil.NewExclusiveWriter()
|
|
|
|
db, err := sqlutil.Open(dbOpts, writer)
|
2022-12-12 07:20:59 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
var membershipTable tables.Membership
|
|
|
|
var stateKeyTable tables.EventStateKeys
|
2023-06-12 11:45:42 +01:00
|
|
|
var userRoomKeys tables.UserRoomKeys
|
|
|
|
var roomsTable tables.Rooms
|
2022-12-12 07:20:59 +00:00
|
|
|
switch dbType {
|
|
|
|
case test.DBTypePostgres:
|
2023-06-12 11:45:42 +01:00
|
|
|
err = postgres.CreateRoomsTable(db)
|
|
|
|
assert.NoError(t, err)
|
2022-12-12 07:20:59 +00:00
|
|
|
err = postgres.CreateEventStateKeysTable(db)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = postgres.CreateMembershipTable(db)
|
|
|
|
assert.NoError(t, err)
|
2023-06-12 11:45:42 +01:00
|
|
|
err = postgres.CreateUserRoomKeysTable(db)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
roomsTable, err = postgres.PrepareRoomsTable(db)
|
|
|
|
assert.NoError(t, err)
|
2022-12-12 07:20:59 +00:00
|
|
|
membershipTable, err = postgres.PrepareMembershipTable(db)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
stateKeyTable, err = postgres.PrepareEventStateKeysTable(db)
|
2023-06-12 11:45:42 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
userRoomKeys, err = postgres.PrepareUserRoomKeysTable(db)
|
2022-12-12 07:20:59 +00:00
|
|
|
case test.DBTypeSQLite:
|
2023-06-12 11:45:42 +01:00
|
|
|
err = sqlite3.CreateRoomsTable(db)
|
|
|
|
assert.NoError(t, err)
|
2022-12-12 07:20:59 +00:00
|
|
|
err = sqlite3.CreateEventStateKeysTable(db)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = sqlite3.CreateMembershipTable(db)
|
|
|
|
assert.NoError(t, err)
|
2023-06-12 11:45:42 +01:00
|
|
|
err = sqlite3.CreateUserRoomKeysTable(db)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
roomsTable, err = sqlite3.PrepareRoomsTable(db)
|
|
|
|
assert.NoError(t, err)
|
2022-12-12 07:20:59 +00:00
|
|
|
membershipTable, err = sqlite3.PrepareMembershipTable(db)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
stateKeyTable, err = sqlite3.PrepareEventStateKeysTable(db)
|
2023-06-12 11:45:42 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
userRoomKeys, err = sqlite3.PrepareUserRoomKeysTable(db)
|
2022-12-12 07:20:59 +00:00
|
|
|
}
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2023-02-24 08:40:20 +00:00
|
|
|
cache := caching.NewRistrettoCache(8*1024*1024, time.Hour, false)
|
|
|
|
|
2023-06-12 11:45:42 +01:00
|
|
|
evDb := shared.EventDatabase{EventStateKeysTable: stateKeyTable, Cache: cache, Writer: writer}
|
2023-03-01 16:06:47 +00:00
|
|
|
|
2022-12-12 07:20:59 +00:00
|
|
|
return &shared.Database{
|
2023-06-12 11:45:42 +01:00
|
|
|
DB: db,
|
|
|
|
EventDatabase: evDb,
|
|
|
|
MembershipTable: membershipTable,
|
|
|
|
UserRoomKeyTable: userRoomKeys,
|
|
|
|
RoomsTable: roomsTable,
|
|
|
|
Writer: writer,
|
|
|
|
Cache: cache,
|
2022-12-12 07:20:59 +00:00
|
|
|
}, func() {
|
|
|
|
clearDB()
|
|
|
|
err = db.Close()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_GetLeftUsers(t *testing.T) {
|
|
|
|
alice := test.NewUser(t)
|
|
|
|
bob := test.NewUser(t)
|
|
|
|
charlie := test.NewUser(t)
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
test.WithAllDatabases(t, func(t *testing.T, dbType test.DBType) {
|
|
|
|
db, close := mustCreateRoomserverDatabase(t, dbType)
|
|
|
|
defer close()
|
|
|
|
|
|
|
|
// Create dummy entries
|
|
|
|
for _, user := range []*test.User{alice, bob, charlie} {
|
|
|
|
nid, err := db.EventStateKeysTable.InsertEventStateKeyNID(ctx, nil, user.ID)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = db.MembershipTable.InsertMembership(ctx, nil, 1, nid, true)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
// We must update the membership with a non-zero event NID or it will get filtered out in later queries
|
|
|
|
membershipNID := tables.MembershipStateLeaveOrBan
|
|
|
|
if user == alice {
|
|
|
|
membershipNID = tables.MembershipStateJoin
|
|
|
|
}
|
|
|
|
_, err = db.MembershipTable.UpdateMembership(ctx, nil, 1, nid, nid, membershipNID, 1, false)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now try to get the left users, this should be Bob and Charlie, since they have a "leave" membership
|
|
|
|
expectedUserIDs := []string{bob.ID, charlie.ID}
|
|
|
|
leftUsers, err := db.GetLeftUsers(context.Background(), []string{alice.ID, bob.ID, charlie.ID})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.ElementsMatch(t, expectedUserIDs, leftUsers)
|
|
|
|
})
|
|
|
|
}
|
2023-06-12 11:45:42 +01:00
|
|
|
|
|
|
|
func TestUserRoomKeys(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
alice := test.NewUser(t)
|
|
|
|
room := test.NewRoom(t, alice)
|
|
|
|
|
|
|
|
userID, err := spec.NewUserID(alice.ID, true)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
roomID, err := spec.NewRoomID(room.ID)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
test.WithAllDatabases(t, func(t *testing.T, dbType test.DBType) {
|
|
|
|
db, close := mustCreateRoomserverDatabase(t, dbType)
|
|
|
|
defer close()
|
|
|
|
|
|
|
|
// create a room NID so we can query the room
|
|
|
|
_, err = db.RoomsTable.InsertRoomNID(ctx, nil, roomID.String(), gomatrixserverlib.RoomVersionV10)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
doesNotExist, err := spec.NewRoomID("!doesnotexist:localhost")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
_, err = db.RoomsTable.InsertRoomNID(ctx, nil, doesNotExist.String(), gomatrixserverlib.RoomVersionV10)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
_, key, err := ed25519.GenerateKey(nil)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
gotKey, err := db.InsertUserRoomPrivatePublicKey(ctx, *userID, *roomID, key)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, gotKey, key)
|
|
|
|
|
|
|
|
// again, this shouldn't result in an error, but return the existing key
|
|
|
|
_, key2, err := ed25519.GenerateKey(nil)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
gotKey, err = db.InsertUserRoomPrivatePublicKey(ctx, *userID, *roomID, key2)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, gotKey, key)
|
|
|
|
|
|
|
|
gotKey, err = db.SelectUserRoomPrivateKey(context.Background(), *userID, *roomID)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, key, gotKey)
|
2023-06-14 15:23:46 +01:00
|
|
|
pubKey, err := db.SelectUserRoomPublicKey(context.Background(), *userID, *roomID)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, key.Public(), pubKey)
|
2023-06-12 11:45:42 +01:00
|
|
|
|
|
|
|
// Key doesn't exist, we shouldn't get anything back
|
|
|
|
gotKey, err = db.SelectUserRoomPrivateKey(context.Background(), *userID, *doesNotExist)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Nil(t, gotKey)
|
2023-06-14 15:23:46 +01:00
|
|
|
pubKey, err = db.SelectUserRoomPublicKey(context.Background(), *userID, *doesNotExist)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Nil(t, pubKey)
|
2023-06-12 11:45:42 +01:00
|
|
|
|
|
|
|
queryUserIDs := map[spec.RoomID][]ed25519.PublicKey{
|
|
|
|
*roomID: {key.Public().(ed25519.PublicKey)},
|
|
|
|
}
|
|
|
|
|
|
|
|
userIDs, err := db.SelectUserIDsForPublicKeys(ctx, queryUserIDs)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
wantKeys := map[spec.RoomID]map[string]string{
|
|
|
|
*roomID: {
|
2023-06-14 17:11:27 +01:00
|
|
|
spec.Base64Bytes(key.Public().(ed25519.PublicKey)).Encode(): userID.String(),
|
2023-06-12 11:45:42 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
assert.Equal(t, wantKeys, userIDs)
|
|
|
|
|
|
|
|
// insert key that came in over federation
|
|
|
|
var gotPublicKey, key4 ed255192.PublicKey
|
|
|
|
key4, _, err = ed25519.GenerateKey(nil)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
gotPublicKey, err = db.InsertUserRoomPublicKey(context.Background(), *userID, *doesNotExist, key4)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, key4, gotPublicKey)
|
|
|
|
|
|
|
|
// test invalid room
|
|
|
|
reallyDoesNotExist, err := spec.NewRoomID("!reallydoesnotexist:localhost")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
_, err = db.InsertUserRoomPublicKey(context.Background(), *userID, *reallyDoesNotExist, key4)
|
|
|
|
assert.Error(t, err)
|
|
|
|
_, err = db.InsertUserRoomPrivatePublicKey(context.Background(), *userID, *reallyDoesNotExist, key)
|
|
|
|
assert.Error(t, err)
|
|
|
|
})
|
|
|
|
}
|
2023-06-13 15:28:41 +01:00
|
|
|
|
|
|
|
func TestAssignRoomNID(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
alice := test.NewUser(t)
|
|
|
|
room := test.NewRoom(t, alice)
|
|
|
|
|
|
|
|
roomID, err := spec.NewRoomID(room.ID)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
test.WithAllDatabases(t, func(t *testing.T, dbType test.DBType) {
|
|
|
|
db, close := mustCreateRoomserverDatabase(t, dbType)
|
|
|
|
defer close()
|
|
|
|
|
|
|
|
nid, err := db.AssignRoomNID(ctx, *roomID, room.Version)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Greater(t, nid, types.EventNID(0))
|
|
|
|
|
|
|
|
_, err = db.AssignRoomNID(ctx, spec.RoomID{}, "notaroomversion")
|
|
|
|
assert.Error(t, err)
|
|
|
|
})
|
|
|
|
}
|