2020-07-28 17:38:30 +01:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-08-05 11:01:37 +01:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
2020-07-28 17:38:30 +01:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
2020-07-30 14:52:21 +01:00
|
|
|
|
2020-08-03 17:07:06 +01:00
|
|
|
"github.com/matrix-org/dendrite/keyserver/api"
|
2022-02-04 13:08:13 +00:00
|
|
|
"github.com/matrix-org/dendrite/keyserver/types"
|
2020-12-02 17:41:00 +00:00
|
|
|
"github.com/matrix-org/dendrite/setup/config"
|
2020-07-28 17:38:30 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var ctx = context.Background()
|
|
|
|
|
2020-08-05 11:01:37 +01:00
|
|
|
func MustCreateDatabase(t *testing.T) (Database, func()) {
|
|
|
|
tmpfile, err := ioutil.TempFile("", "keyserver_storage_test")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
t.Logf("Database %s", tmpfile.Name())
|
2022-05-03 16:35:06 +01:00
|
|
|
db, err := NewDatabase(nil, &config.DatabaseOptions{
|
2020-08-10 14:18:04 +01:00
|
|
|
ConnectionString: config.DataSource(fmt.Sprintf("file://%s", tmpfile.Name())),
|
|
|
|
})
|
2020-08-05 11:01:37 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to NewDatabase: %s", err)
|
|
|
|
}
|
|
|
|
return db, func() {
|
|
|
|
os.Remove(tmpfile.Name())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-28 17:38:30 +01:00
|
|
|
func MustNotError(t *testing.T, err error) {
|
|
|
|
t.Helper()
|
|
|
|
if err == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.Fatalf("operation failed: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestKeyChanges(t *testing.T) {
|
2020-08-05 11:01:37 +01:00
|
|
|
db, clean := MustCreateDatabase(t)
|
|
|
|
defer clean()
|
2022-01-21 09:56:06 +00:00
|
|
|
_, err := db.StoreKeyChange(ctx, "@alice:localhost")
|
|
|
|
MustNotError(t, err)
|
|
|
|
deviceChangeIDB, err := db.StoreKeyChange(ctx, "@bob:localhost")
|
|
|
|
MustNotError(t, err)
|
|
|
|
deviceChangeIDC, err := db.StoreKeyChange(ctx, "@charlie:localhost")
|
|
|
|
MustNotError(t, err)
|
2022-02-04 13:08:13 +00:00
|
|
|
userIDs, latest, err := db.KeyChanges(ctx, deviceChangeIDB, types.OffsetNewest)
|
2020-07-28 17:38:30 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to KeyChanges: %s", err)
|
|
|
|
}
|
2022-01-21 09:56:06 +00:00
|
|
|
if latest != deviceChangeIDC {
|
|
|
|
t.Fatalf("KeyChanges: got latest=%d want %d", latest, deviceChangeIDC)
|
2020-07-28 17:38:30 +01:00
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(userIDs, []string{"@charlie:localhost"}) {
|
|
|
|
t.Fatalf("KeyChanges: wrong user_ids: %v", userIDs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestKeyChangesNoDupes(t *testing.T) {
|
2020-08-05 11:01:37 +01:00
|
|
|
db, clean := MustCreateDatabase(t)
|
|
|
|
defer clean()
|
2022-01-21 09:56:06 +00:00
|
|
|
deviceChangeIDA, err := db.StoreKeyChange(ctx, "@alice:localhost")
|
|
|
|
MustNotError(t, err)
|
|
|
|
deviceChangeIDB, err := db.StoreKeyChange(ctx, "@alice:localhost")
|
|
|
|
MustNotError(t, err)
|
|
|
|
if deviceChangeIDA == deviceChangeIDB {
|
|
|
|
t.Fatalf("Expected change ID to be different even when inserting key change for the same user, got %d for both changes", deviceChangeIDA)
|
|
|
|
}
|
|
|
|
deviceChangeID, err := db.StoreKeyChange(ctx, "@alice:localhost")
|
|
|
|
MustNotError(t, err)
|
2022-02-04 13:08:13 +00:00
|
|
|
userIDs, latest, err := db.KeyChanges(ctx, 0, types.OffsetNewest)
|
2020-07-28 17:38:30 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to KeyChanges: %s", err)
|
|
|
|
}
|
2022-01-21 09:56:06 +00:00
|
|
|
if latest != deviceChangeID {
|
|
|
|
t.Fatalf("KeyChanges: got latest=%d want %d", latest, deviceChangeID)
|
2020-07-28 17:38:30 +01:00
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(userIDs, []string{"@alice:localhost"}) {
|
|
|
|
t.Fatalf("KeyChanges: wrong user_ids: %v", userIDs)
|
|
|
|
}
|
|
|
|
}
|
2020-07-30 14:52:21 +01:00
|
|
|
|
|
|
|
func TestKeyChangesUpperLimit(t *testing.T) {
|
2020-08-05 11:01:37 +01:00
|
|
|
db, clean := MustCreateDatabase(t)
|
|
|
|
defer clean()
|
2022-01-21 09:56:06 +00:00
|
|
|
deviceChangeIDA, err := db.StoreKeyChange(ctx, "@alice:localhost")
|
|
|
|
MustNotError(t, err)
|
|
|
|
deviceChangeIDB, err := db.StoreKeyChange(ctx, "@bob:localhost")
|
|
|
|
MustNotError(t, err)
|
|
|
|
_, err = db.StoreKeyChange(ctx, "@charlie:localhost")
|
|
|
|
MustNotError(t, err)
|
|
|
|
userIDs, latest, err := db.KeyChanges(ctx, deviceChangeIDA, deviceChangeIDB)
|
2020-07-30 14:52:21 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to KeyChanges: %s", err)
|
|
|
|
}
|
2022-01-21 09:56:06 +00:00
|
|
|
if latest != deviceChangeIDB {
|
|
|
|
t.Fatalf("KeyChanges: got latest=%d want %d", latest, deviceChangeIDB)
|
2020-07-30 14:52:21 +01:00
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(userIDs, []string{"@bob:localhost"}) {
|
|
|
|
t.Fatalf("KeyChanges: wrong user_ids: %v", userIDs)
|
|
|
|
}
|
|
|
|
}
|
2020-08-03 17:07:06 +01:00
|
|
|
|
|
|
|
// The purpose of this test is to make sure that the storage layer is generating sequential stream IDs per user,
|
|
|
|
// and that they are returned correctly when querying for device keys.
|
|
|
|
func TestDeviceKeysStreamIDGeneration(t *testing.T) {
|
2020-08-05 11:01:37 +01:00
|
|
|
var err error
|
|
|
|
db, clean := MustCreateDatabase(t)
|
|
|
|
defer clean()
|
2020-08-03 17:07:06 +01:00
|
|
|
alice := "@alice:TestDeviceKeysStreamIDGeneration"
|
|
|
|
bob := "@bob:TestDeviceKeysStreamIDGeneration"
|
|
|
|
msgs := []api.DeviceMessage{
|
|
|
|
{
|
2021-08-11 09:44:14 +01:00
|
|
|
Type: api.TypeDeviceKeyUpdate,
|
|
|
|
DeviceKeys: &api.DeviceKeys{
|
2020-08-03 17:07:06 +01:00
|
|
|
DeviceID: "AAA",
|
|
|
|
UserID: alice,
|
|
|
|
KeyJSON: []byte(`{"key":"v1"}`),
|
|
|
|
},
|
|
|
|
// StreamID: 1
|
|
|
|
},
|
|
|
|
{
|
2021-08-11 09:44:14 +01:00
|
|
|
Type: api.TypeDeviceKeyUpdate,
|
|
|
|
DeviceKeys: &api.DeviceKeys{
|
2020-08-03 17:07:06 +01:00
|
|
|
DeviceID: "AAA",
|
|
|
|
UserID: bob,
|
|
|
|
KeyJSON: []byte(`{"key":"v1"}`),
|
|
|
|
},
|
|
|
|
// StreamID: 1 as this is a different user
|
|
|
|
},
|
|
|
|
{
|
2021-08-11 09:44:14 +01:00
|
|
|
Type: api.TypeDeviceKeyUpdate,
|
|
|
|
DeviceKeys: &api.DeviceKeys{
|
2020-08-03 17:07:06 +01:00
|
|
|
DeviceID: "another_device",
|
|
|
|
UserID: alice,
|
|
|
|
KeyJSON: []byte(`{"key":"v1"}`),
|
|
|
|
},
|
|
|
|
// StreamID: 2 as this is a 2nd device key
|
|
|
|
},
|
|
|
|
}
|
2020-08-05 13:41:16 +01:00
|
|
|
MustNotError(t, db.StoreLocalDeviceKeys(ctx, msgs))
|
2020-08-03 17:07:06 +01:00
|
|
|
if msgs[0].StreamID != 1 {
|
2020-08-05 13:41:16 +01:00
|
|
|
t.Fatalf("Expected StoreLocalDeviceKeys to set StreamID=1 but got %d", msgs[0].StreamID)
|
2020-08-03 17:07:06 +01:00
|
|
|
}
|
|
|
|
if msgs[1].StreamID != 1 {
|
2020-08-05 13:41:16 +01:00
|
|
|
t.Fatalf("Expected StoreLocalDeviceKeys to set StreamID=1 (different user) but got %d", msgs[1].StreamID)
|
2020-08-03 17:07:06 +01:00
|
|
|
}
|
|
|
|
if msgs[2].StreamID != 2 {
|
2020-08-05 13:41:16 +01:00
|
|
|
t.Fatalf("Expected StoreLocalDeviceKeys to set StreamID=2 (another device) but got %d", msgs[2].StreamID)
|
2020-08-03 17:07:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// updating a device sets the next stream ID for that user
|
|
|
|
msgs = []api.DeviceMessage{
|
|
|
|
{
|
2021-08-11 09:44:14 +01:00
|
|
|
Type: api.TypeDeviceKeyUpdate,
|
|
|
|
DeviceKeys: &api.DeviceKeys{
|
2020-08-03 17:07:06 +01:00
|
|
|
DeviceID: "AAA",
|
|
|
|
UserID: alice,
|
|
|
|
KeyJSON: []byte(`{"key":"v2"}`),
|
|
|
|
},
|
|
|
|
// StreamID: 3
|
|
|
|
},
|
|
|
|
}
|
2020-08-05 13:41:16 +01:00
|
|
|
MustNotError(t, db.StoreLocalDeviceKeys(ctx, msgs))
|
2020-08-03 17:07:06 +01:00
|
|
|
if msgs[0].StreamID != 3 {
|
2020-08-05 13:41:16 +01:00
|
|
|
t.Fatalf("Expected StoreLocalDeviceKeys to set StreamID=3 (new key same device) but got %d", msgs[0].StreamID)
|
2020-08-03 17:07:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Querying for device keys returns the latest stream IDs
|
2022-02-18 11:31:05 +00:00
|
|
|
msgs, err = db.DeviceKeysForUser(ctx, alice, []string{"AAA", "another_device"}, false)
|
2020-08-03 17:07:06 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("DeviceKeysForUser returned error: %s", err)
|
|
|
|
}
|
2022-03-10 13:17:28 +00:00
|
|
|
wantStreamIDs := map[string]int64{
|
2020-08-03 17:07:06 +01:00
|
|
|
"AAA": 3,
|
|
|
|
"another_device": 2,
|
|
|
|
}
|
|
|
|
if len(msgs) != len(wantStreamIDs) {
|
|
|
|
t.Fatalf("DeviceKeysForUser: wrong number of devices, got %d want %d", len(msgs), len(wantStreamIDs))
|
|
|
|
}
|
|
|
|
for _, m := range msgs {
|
|
|
|
if m.StreamID != wantStreamIDs[m.DeviceID] {
|
|
|
|
t.Errorf("DeviceKeysForUser: wrong returned stream ID for key, got %d want %d", m.StreamID, wantStreamIDs[m.DeviceID])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|