2020-07-30 10:15:46 +00:00
|
|
|
package internal
|
2020-07-27 08:19:55 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"reflect"
|
|
|
|
"sort"
|
|
|
|
"testing"
|
|
|
|
|
2020-07-30 13:52:21 +00:00
|
|
|
"github.com/Shopify/sarama"
|
2020-07-28 17:25:16 +00:00
|
|
|
keyapi "github.com/matrix-org/dendrite/keyserver/api"
|
2020-09-04 13:25:01 +00:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/api"
|
2020-07-27 08:19:55 +00:00
|
|
|
"github.com/matrix-org/dendrite/syncapi/types"
|
2020-07-30 17:00:56 +00:00
|
|
|
userapi "github.com/matrix-org/dendrite/userapi/api"
|
2020-07-27 08:19:55 +00:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
syncingUser = "@alice:localhost"
|
2021-01-08 16:59:06 +00:00
|
|
|
emptyToken = types.LogPosition{}
|
|
|
|
newestToken = types.LogPosition{
|
|
|
|
Offset: sarama.OffsetNewest,
|
|
|
|
Partition: 0,
|
2020-12-10 18:57:10 +00:00
|
|
|
}
|
2020-07-27 08:19:55 +00:00
|
|
|
)
|
|
|
|
|
2020-07-28 17:25:16 +00:00
|
|
|
type mockKeyAPI struct{}
|
|
|
|
|
|
|
|
func (k *mockKeyAPI) PerformUploadKeys(ctx context.Context, req *keyapi.PerformUploadKeysRequest, res *keyapi.PerformUploadKeysResponse) {
|
|
|
|
}
|
|
|
|
|
2020-07-30 17:00:56 +00:00
|
|
|
func (k *mockKeyAPI) SetUserAPI(i userapi.UserInternalAPI) {}
|
|
|
|
|
2020-07-28 17:25:16 +00:00
|
|
|
// PerformClaimKeys claims one-time keys for use in pre-key messages
|
|
|
|
func (k *mockKeyAPI) PerformClaimKeys(ctx context.Context, req *keyapi.PerformClaimKeysRequest, res *keyapi.PerformClaimKeysResponse) {
|
|
|
|
}
|
2021-08-18 11:07:09 +00:00
|
|
|
func (k *mockKeyAPI) PerformDeleteKeys(ctx context.Context, req *keyapi.PerformDeleteKeysRequest, res *keyapi.PerformDeleteKeysResponse) {
|
|
|
|
}
|
2021-08-04 16:56:29 +00:00
|
|
|
func (k *mockKeyAPI) PerformUploadDeviceKeys(ctx context.Context, req *keyapi.PerformUploadDeviceKeysRequest, res *keyapi.PerformUploadDeviceKeysResponse) {
|
|
|
|
}
|
|
|
|
func (k *mockKeyAPI) PerformUploadDeviceSignatures(ctx context.Context, req *keyapi.PerformUploadDeviceSignaturesRequest, res *keyapi.PerformUploadDeviceSignaturesResponse) {
|
|
|
|
}
|
2020-07-28 17:25:16 +00:00
|
|
|
func (k *mockKeyAPI) QueryKeys(ctx context.Context, req *keyapi.QueryKeysRequest, res *keyapi.QueryKeysResponse) {
|
|
|
|
}
|
|
|
|
func (k *mockKeyAPI) QueryKeyChanges(ctx context.Context, req *keyapi.QueryKeyChangesRequest, res *keyapi.QueryKeyChangesResponse) {
|
2020-08-03 11:29:58 +00:00
|
|
|
}
|
|
|
|
func (k *mockKeyAPI) QueryOneTimeKeys(ctx context.Context, req *keyapi.QueryOneTimeKeysRequest, res *keyapi.QueryOneTimeKeysResponse) {
|
|
|
|
|
2020-08-04 10:32:14 +00:00
|
|
|
}
|
|
|
|
func (k *mockKeyAPI) QueryDeviceMessages(ctx context.Context, req *keyapi.QueryDeviceMessagesRequest, res *keyapi.QueryDeviceMessagesResponse) {
|
|
|
|
|
2020-08-05 12:41:16 +00:00
|
|
|
}
|
|
|
|
func (k *mockKeyAPI) InputDeviceListUpdate(ctx context.Context, req *keyapi.InputDeviceListUpdateRequest, res *keyapi.InputDeviceListUpdateResponse) {
|
|
|
|
|
2021-08-06 09:13:35 +00:00
|
|
|
}
|
|
|
|
func (k *mockKeyAPI) QuerySignatures(ctx context.Context, req *keyapi.QuerySignaturesRequest, res *keyapi.QuerySignaturesResponse) {
|
2020-07-28 17:25:16 +00:00
|
|
|
}
|
|
|
|
|
2020-09-04 13:25:01 +00:00
|
|
|
type mockRoomserverAPI struct {
|
|
|
|
api.RoomserverInternalAPITrace
|
2020-07-27 08:19:55 +00:00
|
|
|
roomIDToJoinedMembers map[string][]string
|
|
|
|
}
|
|
|
|
|
|
|
|
// QueryRoomsForUser retrieves a list of room IDs matching the given query.
|
2020-09-04 13:25:01 +00:00
|
|
|
func (s *mockRoomserverAPI) QueryRoomsForUser(ctx context.Context, req *api.QueryRoomsForUserRequest, res *api.QueryRoomsForUserResponse) error {
|
2020-07-27 08:19:55 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// QueryBulkStateContent does a bulk query for state event content in the given rooms.
|
2020-09-04 13:25:01 +00:00
|
|
|
func (s *mockRoomserverAPI) QueryBulkStateContent(ctx context.Context, req *api.QueryBulkStateContentRequest, res *api.QueryBulkStateContentResponse) error {
|
2020-07-27 08:19:55 +00:00
|
|
|
res.Rooms = make(map[string]map[gomatrixserverlib.StateKeyTuple]string)
|
|
|
|
if req.AllowWildcards && len(req.StateTuples) == 1 && req.StateTuples[0].EventType == gomatrixserverlib.MRoomMember && req.StateTuples[0].StateKey == "*" {
|
|
|
|
for _, roomID := range req.RoomIDs {
|
|
|
|
res.Rooms[roomID] = make(map[gomatrixserverlib.StateKeyTuple]string)
|
|
|
|
for _, userID := range s.roomIDToJoinedMembers[roomID] {
|
|
|
|
res.Rooms[roomID][gomatrixserverlib.StateKeyTuple{
|
|
|
|
EventType: gomatrixserverlib.MRoomMember,
|
|
|
|
StateKey: userID,
|
|
|
|
}] = "join"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// QuerySharedUsers returns a list of users who share at least 1 room in common with the given user.
|
2020-09-04 13:25:01 +00:00
|
|
|
func (s *mockRoomserverAPI) QuerySharedUsers(ctx context.Context, req *api.QuerySharedUsersRequest, res *api.QuerySharedUsersResponse) error {
|
2020-07-27 08:19:55 +00:00
|
|
|
roomsToQuery := req.IncludeRoomIDs
|
|
|
|
for roomID, members := range s.roomIDToJoinedMembers {
|
|
|
|
exclude := false
|
|
|
|
for _, excludeRoomID := range req.ExcludeRoomIDs {
|
|
|
|
if roomID == excludeRoomID {
|
|
|
|
exclude = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if exclude {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, userID := range members {
|
|
|
|
if userID == req.UserID {
|
|
|
|
roomsToQuery = append(roomsToQuery, roomID)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
res.UserIDsToCount = make(map[string]int)
|
|
|
|
for _, roomID := range roomsToQuery {
|
|
|
|
for _, userID := range s.roomIDToJoinedMembers[roomID] {
|
|
|
|
res.UserIDsToCount[userID]++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type wantCatchup struct {
|
|
|
|
hasNew bool
|
|
|
|
changed []string
|
|
|
|
left []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func assertCatchup(t *testing.T, hasNew bool, syncResponse *types.Response, want wantCatchup) {
|
|
|
|
if hasNew != want.hasNew {
|
|
|
|
t.Errorf("got hasNew=%v want %v", hasNew, want.hasNew)
|
|
|
|
}
|
|
|
|
sort.Strings(syncResponse.DeviceLists.Left)
|
|
|
|
if !reflect.DeepEqual(syncResponse.DeviceLists.Left, want.left) {
|
|
|
|
t.Errorf("device_lists.left got %v want %v", syncResponse.DeviceLists.Left, want.left)
|
|
|
|
}
|
|
|
|
sort.Strings(syncResponse.DeviceLists.Changed)
|
|
|
|
if !reflect.DeepEqual(syncResponse.DeviceLists.Changed, want.changed) {
|
|
|
|
t.Errorf("device_lists.changed got %v want %v", syncResponse.DeviceLists.Changed, want.changed)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func joinResponseWithRooms(syncResponse *types.Response, userID string, roomIDs []string) *types.Response {
|
|
|
|
for _, roomID := range roomIDs {
|
|
|
|
roomEvents := []gomatrixserverlib.ClientEvent{
|
|
|
|
{
|
|
|
|
Type: "m.room.member",
|
|
|
|
StateKey: &userID,
|
|
|
|
EventID: "$something:here",
|
|
|
|
Sender: userID,
|
|
|
|
RoomID: roomID,
|
|
|
|
Content: []byte(`{"membership":"join"}`),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
jr := syncResponse.Rooms.Join[roomID]
|
|
|
|
jr.State.Events = roomEvents
|
|
|
|
syncResponse.Rooms.Join[roomID] = jr
|
|
|
|
}
|
|
|
|
return syncResponse
|
|
|
|
}
|
|
|
|
|
|
|
|
func leaveResponseWithRooms(syncResponse *types.Response, userID string, roomIDs []string) *types.Response {
|
|
|
|
for _, roomID := range roomIDs {
|
|
|
|
roomEvents := []gomatrixserverlib.ClientEvent{
|
|
|
|
{
|
|
|
|
Type: "m.room.member",
|
|
|
|
StateKey: &userID,
|
|
|
|
EventID: "$something:here",
|
|
|
|
Sender: userID,
|
|
|
|
RoomID: roomID,
|
|
|
|
Content: []byte(`{"membership":"leave"}`),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
lr := syncResponse.Rooms.Leave[roomID]
|
|
|
|
lr.Timeline.Events = roomEvents
|
|
|
|
syncResponse.Rooms.Leave[roomID] = lr
|
|
|
|
}
|
|
|
|
return syncResponse
|
|
|
|
}
|
|
|
|
|
|
|
|
// tests that joining a room which results in sharing a new user includes that user in `changed`
|
|
|
|
func TestKeyChangeCatchupOnJoinShareNewUser(t *testing.T) {
|
|
|
|
newShareUser := "@bill:localhost"
|
|
|
|
newlyJoinedRoom := "!TestKeyChangeCatchupOnJoinShareNewUser:bar"
|
2020-07-30 10:15:46 +00:00
|
|
|
syncResponse := types.NewResponse()
|
|
|
|
syncResponse = joinResponseWithRooms(syncResponse, syncingUser, []string{newlyJoinedRoom})
|
|
|
|
|
2020-09-04 13:25:01 +00:00
|
|
|
rsAPI := &mockRoomserverAPI{
|
2020-07-27 08:19:55 +00:00
|
|
|
roomIDToJoinedMembers: map[string][]string{
|
|
|
|
newlyJoinedRoom: {syncingUser, newShareUser},
|
|
|
|
"!another:room": {syncingUser},
|
|
|
|
},
|
2020-09-04 13:25:01 +00:00
|
|
|
}
|
2021-01-08 16:59:06 +00:00
|
|
|
_, hasNew, err := DeviceListCatchup(context.Background(), &mockKeyAPI{}, rsAPI, syncingUser, syncResponse, emptyToken, newestToken)
|
2020-07-27 08:19:55 +00:00
|
|
|
if err != nil {
|
2020-07-30 10:15:46 +00:00
|
|
|
t.Fatalf("DeviceListCatchup returned an error: %s", err)
|
2020-07-27 08:19:55 +00:00
|
|
|
}
|
|
|
|
assertCatchup(t, hasNew, syncResponse, wantCatchup{
|
|
|
|
hasNew: true,
|
|
|
|
changed: []string{newShareUser},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// tests that leaving a room which results in sharing no rooms with a user includes that user in `left`
|
|
|
|
func TestKeyChangeCatchupOnLeaveShareLeftUser(t *testing.T) {
|
|
|
|
removeUser := "@bill:localhost"
|
|
|
|
newlyLeftRoom := "!TestKeyChangeCatchupOnLeaveShareLeftUser:bar"
|
2020-07-30 10:15:46 +00:00
|
|
|
syncResponse := types.NewResponse()
|
|
|
|
syncResponse = leaveResponseWithRooms(syncResponse, syncingUser, []string{newlyLeftRoom})
|
|
|
|
|
2020-09-04 13:25:01 +00:00
|
|
|
rsAPI := &mockRoomserverAPI{
|
2020-07-27 08:19:55 +00:00
|
|
|
roomIDToJoinedMembers: map[string][]string{
|
|
|
|
newlyLeftRoom: {removeUser},
|
|
|
|
"!another:room": {syncingUser},
|
|
|
|
},
|
2020-09-04 13:25:01 +00:00
|
|
|
}
|
2021-01-08 16:59:06 +00:00
|
|
|
_, hasNew, err := DeviceListCatchup(context.Background(), &mockKeyAPI{}, rsAPI, syncingUser, syncResponse, emptyToken, newestToken)
|
2020-07-27 08:19:55 +00:00
|
|
|
if err != nil {
|
2020-07-30 10:15:46 +00:00
|
|
|
t.Fatalf("DeviceListCatchup returned an error: %s", err)
|
2020-07-27 08:19:55 +00:00
|
|
|
}
|
|
|
|
assertCatchup(t, hasNew, syncResponse, wantCatchup{
|
|
|
|
hasNew: true,
|
|
|
|
left: []string{removeUser},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// tests that joining a room which doesn't result in sharing a new user results in no changes.
|
|
|
|
func TestKeyChangeCatchupOnJoinShareNoNewUsers(t *testing.T) {
|
|
|
|
existingUser := "@bob:localhost"
|
|
|
|
newlyJoinedRoom := "!TestKeyChangeCatchupOnJoinShareNoNewUsers:bar"
|
2020-07-30 10:15:46 +00:00
|
|
|
syncResponse := types.NewResponse()
|
|
|
|
syncResponse = joinResponseWithRooms(syncResponse, syncingUser, []string{newlyJoinedRoom})
|
|
|
|
|
2020-09-04 13:25:01 +00:00
|
|
|
rsAPI := &mockRoomserverAPI{
|
2020-07-27 08:19:55 +00:00
|
|
|
roomIDToJoinedMembers: map[string][]string{
|
|
|
|
newlyJoinedRoom: {syncingUser, existingUser},
|
|
|
|
"!another:room": {syncingUser, existingUser},
|
|
|
|
},
|
2020-09-04 13:25:01 +00:00
|
|
|
}
|
2021-01-08 16:59:06 +00:00
|
|
|
_, hasNew, err := DeviceListCatchup(context.Background(), &mockKeyAPI{}, rsAPI, syncingUser, syncResponse, emptyToken, newestToken)
|
2020-07-27 08:19:55 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Catchup returned an error: %s", err)
|
|
|
|
}
|
|
|
|
assertCatchup(t, hasNew, syncResponse, wantCatchup{
|
|
|
|
hasNew: false,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// tests that leaving a room which doesn't result in sharing no rooms with a user results in no changes.
|
|
|
|
func TestKeyChangeCatchupOnLeaveShareNoUsers(t *testing.T) {
|
|
|
|
existingUser := "@bob:localhost"
|
|
|
|
newlyLeftRoom := "!TestKeyChangeCatchupOnLeaveShareNoUsers:bar"
|
2020-07-30 10:15:46 +00:00
|
|
|
syncResponse := types.NewResponse()
|
|
|
|
syncResponse = leaveResponseWithRooms(syncResponse, syncingUser, []string{newlyLeftRoom})
|
|
|
|
|
2020-09-04 13:25:01 +00:00
|
|
|
rsAPI := &mockRoomserverAPI{
|
2020-07-27 08:19:55 +00:00
|
|
|
roomIDToJoinedMembers: map[string][]string{
|
|
|
|
newlyLeftRoom: {existingUser},
|
|
|
|
"!another:room": {syncingUser, existingUser},
|
|
|
|
},
|
2020-09-04 13:25:01 +00:00
|
|
|
}
|
2021-01-08 16:59:06 +00:00
|
|
|
_, hasNew, err := DeviceListCatchup(context.Background(), &mockKeyAPI{}, rsAPI, syncingUser, syncResponse, emptyToken, newestToken)
|
2020-07-27 08:19:55 +00:00
|
|
|
if err != nil {
|
2020-07-30 10:15:46 +00:00
|
|
|
t.Fatalf("DeviceListCatchup returned an error: %s", err)
|
2020-07-27 08:19:55 +00:00
|
|
|
}
|
|
|
|
assertCatchup(t, hasNew, syncResponse, wantCatchup{
|
|
|
|
hasNew: false,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// tests that not joining any rooms (but having messages in the response) do not result in changes.
|
|
|
|
func TestKeyChangeCatchupNoNewJoinsButMessages(t *testing.T) {
|
|
|
|
existingUser := "@bob1:localhost"
|
|
|
|
roomID := "!TestKeyChangeCatchupNoNewJoinsButMessages:bar"
|
|
|
|
syncResponse := types.NewResponse()
|
|
|
|
empty := ""
|
|
|
|
roomStateEvents := []gomatrixserverlib.ClientEvent{
|
|
|
|
{
|
|
|
|
Type: "m.room.name",
|
|
|
|
StateKey: &empty,
|
|
|
|
EventID: "$something:here",
|
|
|
|
Sender: existingUser,
|
|
|
|
RoomID: roomID,
|
|
|
|
Content: []byte(`{"name":"The Room Name"}`),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
roomTimelineEvents := []gomatrixserverlib.ClientEvent{
|
|
|
|
{
|
|
|
|
Type: "m.room.message",
|
|
|
|
EventID: "$something1:here",
|
|
|
|
Sender: existingUser,
|
|
|
|
RoomID: roomID,
|
|
|
|
Content: []byte(`{"body":"Message 1"}`),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "m.room.message",
|
|
|
|
EventID: "$something2:here",
|
|
|
|
Sender: syncingUser,
|
|
|
|
RoomID: roomID,
|
|
|
|
Content: []byte(`{"body":"Message 2"}`),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "m.room.message",
|
|
|
|
EventID: "$something3:here",
|
|
|
|
Sender: existingUser,
|
|
|
|
RoomID: roomID,
|
|
|
|
Content: []byte(`{"body":"Message 3"}`),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
jr := syncResponse.Rooms.Join[roomID]
|
|
|
|
jr.State.Events = roomStateEvents
|
|
|
|
jr.Timeline.Events = roomTimelineEvents
|
|
|
|
syncResponse.Rooms.Join[roomID] = jr
|
|
|
|
|
2020-09-04 13:25:01 +00:00
|
|
|
rsAPI := &mockRoomserverAPI{
|
2020-07-30 10:15:46 +00:00
|
|
|
roomIDToJoinedMembers: map[string][]string{
|
|
|
|
roomID: {syncingUser, existingUser},
|
|
|
|
},
|
2020-09-04 13:25:01 +00:00
|
|
|
}
|
2021-01-08 16:59:06 +00:00
|
|
|
_, hasNew, err := DeviceListCatchup(context.Background(), &mockKeyAPI{}, rsAPI, syncingUser, syncResponse, emptyToken, newestToken)
|
2020-07-27 08:19:55 +00:00
|
|
|
if err != nil {
|
2020-07-30 10:15:46 +00:00
|
|
|
t.Fatalf("DeviceListCatchup returned an error: %s", err)
|
2020-07-27 08:19:55 +00:00
|
|
|
}
|
|
|
|
assertCatchup(t, hasNew, syncResponse, wantCatchup{
|
|
|
|
hasNew: false,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// tests that joining/leaving multiple rooms can result in both `changed` and `left` and they are not duplicated.
|
|
|
|
func TestKeyChangeCatchupChangeAndLeft(t *testing.T) {
|
|
|
|
newShareUser := "@berta:localhost"
|
|
|
|
newShareUser2 := "@bobby:localhost"
|
|
|
|
newlyLeftUser := "@charlie:localhost"
|
|
|
|
newlyLeftUser2 := "@debra:localhost"
|
|
|
|
newlyJoinedRoom := "!join:bar"
|
|
|
|
newlyLeftRoom := "!left:bar"
|
2020-07-30 10:15:46 +00:00
|
|
|
syncResponse := types.NewResponse()
|
|
|
|
syncResponse = joinResponseWithRooms(syncResponse, syncingUser, []string{newlyJoinedRoom})
|
|
|
|
syncResponse = leaveResponseWithRooms(syncResponse, syncingUser, []string{newlyLeftRoom})
|
|
|
|
|
2020-09-04 13:25:01 +00:00
|
|
|
rsAPI := &mockRoomserverAPI{
|
2020-07-27 08:19:55 +00:00
|
|
|
roomIDToJoinedMembers: map[string][]string{
|
|
|
|
newlyJoinedRoom: {syncingUser, newShareUser, newShareUser2},
|
|
|
|
newlyLeftRoom: {newlyLeftUser, newlyLeftUser2},
|
|
|
|
"!another:room": {syncingUser},
|
|
|
|
},
|
2020-09-04 13:25:01 +00:00
|
|
|
}
|
2021-01-08 16:59:06 +00:00
|
|
|
_, hasNew, err := DeviceListCatchup(context.Background(), &mockKeyAPI{}, rsAPI, syncingUser, syncResponse, emptyToken, newestToken)
|
2020-07-27 08:19:55 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Catchup returned an error: %s", err)
|
|
|
|
}
|
|
|
|
assertCatchup(t, hasNew, syncResponse, wantCatchup{
|
|
|
|
hasNew: true,
|
|
|
|
changed: []string{newShareUser, newShareUser2},
|
|
|
|
left: []string{newlyLeftUser, newlyLeftUser2},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// tests that joining/leaving the SAME room puts users in `left` if the final state is leave.
|
|
|
|
// NB: Consider the case:
|
|
|
|
// - Alice and Bob are in a room.
|
|
|
|
// - Alice goes offline, Charlie joins, sends encrypted messages then leaves the room.
|
|
|
|
// - Alice comes back online. Technically nothing has changed in the set of users between those two points in time,
|
|
|
|
// it's still just (Alice,Bob) but then we won't be tracking Charlie -- is this okay though? It's device keys
|
|
|
|
// which are only relevant when actively sending events I think? And if Alice does need the keys she knows
|
|
|
|
// charlie's (user_id, device_id) so can just hit /keys/query - no need to keep updated about it because she
|
|
|
|
// doesn't share any rooms with him.
|
|
|
|
// Ergo, we put them in `left` as it is simpler.
|
|
|
|
func TestKeyChangeCatchupChangeAndLeftSameRoom(t *testing.T) {
|
|
|
|
newShareUser := "@berta:localhost"
|
|
|
|
newShareUser2 := "@bobby:localhost"
|
|
|
|
roomID := "!join:bar"
|
|
|
|
syncResponse := types.NewResponse()
|
|
|
|
roomEvents := []gomatrixserverlib.ClientEvent{
|
|
|
|
{
|
|
|
|
Type: "m.room.member",
|
|
|
|
StateKey: &syncingUser,
|
|
|
|
EventID: "$something:here",
|
|
|
|
Sender: syncingUser,
|
|
|
|
RoomID: roomID,
|
|
|
|
Content: []byte(`{"membership":"join"}`),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "m.room.message",
|
|
|
|
EventID: "$something2:here",
|
|
|
|
Sender: syncingUser,
|
|
|
|
RoomID: roomID,
|
|
|
|
Content: []byte(`{"body":"now I leave you"}`),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "m.room.member",
|
|
|
|
StateKey: &syncingUser,
|
|
|
|
EventID: "$something3:here",
|
|
|
|
Sender: syncingUser,
|
|
|
|
RoomID: roomID,
|
|
|
|
Content: []byte(`{"membership":"leave"}`),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "m.room.member",
|
|
|
|
StateKey: &syncingUser,
|
|
|
|
EventID: "$something4:here",
|
|
|
|
Sender: syncingUser,
|
|
|
|
RoomID: roomID,
|
|
|
|
Content: []byte(`{"membership":"join"}`),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "m.room.message",
|
|
|
|
EventID: "$something5:here",
|
|
|
|
Sender: syncingUser,
|
|
|
|
RoomID: roomID,
|
|
|
|
Content: []byte(`{"body":"now I am back, and I leave you for good"}`),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "m.room.member",
|
|
|
|
StateKey: &syncingUser,
|
|
|
|
EventID: "$something6:here",
|
|
|
|
Sender: syncingUser,
|
|
|
|
RoomID: roomID,
|
|
|
|
Content: []byte(`{"membership":"leave"}`),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
lr := syncResponse.Rooms.Leave[roomID]
|
|
|
|
lr.Timeline.Events = roomEvents
|
|
|
|
syncResponse.Rooms.Leave[roomID] = lr
|
|
|
|
|
2020-09-04 13:25:01 +00:00
|
|
|
rsAPI := &mockRoomserverAPI{
|
2020-07-30 10:15:46 +00:00
|
|
|
roomIDToJoinedMembers: map[string][]string{
|
|
|
|
roomID: {newShareUser, newShareUser2},
|
|
|
|
"!another:room": {syncingUser},
|
|
|
|
},
|
2020-09-04 13:25:01 +00:00
|
|
|
}
|
2021-01-08 16:59:06 +00:00
|
|
|
_, hasNew, err := DeviceListCatchup(
|
2020-09-07 11:38:09 +00:00
|
|
|
context.Background(), &mockKeyAPI{}, rsAPI, syncingUser, syncResponse, emptyToken, newestToken,
|
2020-09-04 13:25:01 +00:00
|
|
|
)
|
2020-07-27 08:19:55 +00:00
|
|
|
if err != nil {
|
2020-07-30 10:15:46 +00:00
|
|
|
t.Fatalf("DeviceListCatchup returned an error: %s", err)
|
2020-07-27 08:19:55 +00:00
|
|
|
}
|
|
|
|
assertCatchup(t, hasNew, syncResponse, wantCatchup{
|
|
|
|
hasNew: true,
|
|
|
|
left: []string{newShareUser, newShareUser2},
|
|
|
|
})
|
|
|
|
}
|