2020-09-02 11:13:15 -05:00
|
|
|
// Copyright 2020 The Matrix.org Foundation C.I.C.
|
2017-11-27 04:20:00 -06:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2020-09-02 11:13:15 -05:00
|
|
|
package query
|
2017-11-27 04:20:00 -06:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"testing"
|
2023-05-18 14:41:47 -05:00
|
|
|
"time"
|
2017-11-27 04:20:00 -06:00
|
|
|
|
2023-05-18 14:41:47 -05:00
|
|
|
"github.com/matrix-org/dendrite/internal/caching"
|
|
|
|
"github.com/matrix-org/dendrite/internal/sqlutil"
|
|
|
|
"github.com/matrix-org/dendrite/roomserver/storage"
|
2017-11-27 04:20:00 -06:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/types"
|
2023-05-18 14:41:47 -05:00
|
|
|
"github.com/matrix-org/dendrite/setup/config"
|
2022-05-17 07:23:35 -05:00
|
|
|
"github.com/matrix-org/dendrite/test"
|
2017-11-27 04:20:00 -06:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2023-05-18 14:41:47 -05:00
|
|
|
"github.com/matrix-org/gomatrixserverlib/spec"
|
2017-11-27 04:20:00 -06:00
|
|
|
)
|
|
|
|
|
2020-05-01 04:48:17 -05:00
|
|
|
// used to implement RoomserverInternalAPIEventDB to test getAuthChain
|
2017-11-27 04:20:00 -06:00
|
|
|
type getEventDB struct {
|
2023-05-03 04:21:27 -05:00
|
|
|
eventMap map[string]gomatrixserverlib.PDU
|
2017-11-27 04:20:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func createEventDB() *getEventDB {
|
|
|
|
return &getEventDB{
|
2023-05-03 04:21:27 -05:00
|
|
|
eventMap: make(map[string]gomatrixserverlib.PDU),
|
2017-11-27 04:20:00 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Adds a fake event to the storage with given auth events.
|
|
|
|
func (db *getEventDB) addFakeEvent(eventID string, authIDs []string) error {
|
2023-05-24 05:14:42 -05:00
|
|
|
authEvents := make([]any, 0, len(authIDs))
|
2017-11-27 04:20:00 -06:00
|
|
|
for _, authID := range authIDs {
|
2023-05-24 05:14:42 -05:00
|
|
|
authEvents = append(authEvents, []any{authID, struct{}{}})
|
2017-11-27 04:20:00 -06:00
|
|
|
}
|
|
|
|
builder := map[string]interface{}{
|
|
|
|
"event_id": eventID,
|
|
|
|
"auth_events": authEvents,
|
|
|
|
}
|
|
|
|
|
|
|
|
eventJSON, err := json.Marshal(&builder)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-04-21 11:06:29 -05:00
|
|
|
event, err := gomatrixserverlib.MustGetRoomVersion(gomatrixserverlib.RoomVersionV1).NewEventFromTrustedJSON(
|
2023-04-20 13:07:31 -05:00
|
|
|
eventJSON, false,
|
2020-03-27 11:28:22 -05:00
|
|
|
)
|
2017-11-27 04:20:00 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
db.eventMap[eventID] = event
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Adds multiple events at once, each entry in the map is an eventID and set of
|
|
|
|
// auth events that are converted to an event and added.
|
|
|
|
func (db *getEventDB) addFakeEvents(graph map[string][]string) error {
|
|
|
|
for eventID, authIDs := range graph {
|
|
|
|
err := db.addFakeEvent(eventID, authIDs)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-01 04:48:17 -05:00
|
|
|
// EventsFromIDs implements RoomserverInternalAPIEventDB
|
2023-03-01 10:06:47 -06:00
|
|
|
func (db *getEventDB) EventsFromIDs(ctx context.Context, roomInfo *types.RoomInfo, eventIDs []string) (res []types.Event, err error) {
|
2017-11-27 04:20:00 -06:00
|
|
|
for _, evID := range eventIDs {
|
|
|
|
res = append(res, types.Event{
|
|
|
|
EventNID: 0,
|
2023-05-02 09:03:16 -05:00
|
|
|
PDU: db.eventMap[evID],
|
2017-11-27 04:20:00 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetAuthChainSingle(t *testing.T) {
|
|
|
|
db := createEventDB()
|
|
|
|
|
|
|
|
err := db.addFakeEvents(map[string][]string{
|
|
|
|
"a": {},
|
|
|
|
"b": {"a"},
|
|
|
|
"c": {"a", "b"},
|
|
|
|
"d": {"b", "c"},
|
|
|
|
"e": {"a", "d"},
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to add events to db: %v", err)
|
|
|
|
}
|
|
|
|
|
2023-03-01 10:06:47 -06:00
|
|
|
result, err := GetAuthChain(context.TODO(), db.EventsFromIDs, nil, []string{"e"})
|
2017-11-27 04:20:00 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("getAuthChain failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var returnedIDs []string
|
|
|
|
for _, event := range result {
|
|
|
|
returnedIDs = append(returnedIDs, event.EventID())
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedIDs := []string{"a", "b", "c", "d", "e"}
|
|
|
|
|
2018-07-26 11:34:39 -05:00
|
|
|
if !test.UnsortedStringSliceEqual(expectedIDs, returnedIDs) {
|
2017-11-27 04:20:00 -06:00
|
|
|
t.Fatalf("returnedIDs got '%v', expected '%v'", returnedIDs, expectedIDs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetAuthChainMultiple(t *testing.T) {
|
|
|
|
db := createEventDB()
|
|
|
|
|
|
|
|
err := db.addFakeEvents(map[string][]string{
|
|
|
|
"a": {},
|
|
|
|
"b": {"a"},
|
|
|
|
"c": {"a", "b"},
|
|
|
|
"d": {"b", "c"},
|
|
|
|
"e": {"a", "d"},
|
|
|
|
"f": {"a", "b", "c"},
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to add events to db: %v", err)
|
|
|
|
}
|
|
|
|
|
2023-03-01 10:06:47 -06:00
|
|
|
result, err := GetAuthChain(context.TODO(), db.EventsFromIDs, nil, []string{"e", "f"})
|
2017-11-27 04:20:00 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("getAuthChain failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var returnedIDs []string
|
|
|
|
for _, event := range result {
|
|
|
|
returnedIDs = append(returnedIDs, event.EventID())
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedIDs := []string{"a", "b", "c", "d", "e", "f"}
|
|
|
|
|
2018-07-26 11:34:39 -05:00
|
|
|
if !test.UnsortedStringSliceEqual(expectedIDs, returnedIDs) {
|
2017-11-27 04:20:00 -06:00
|
|
|
t.Fatalf("returnedIDs got '%v', expected '%v'", returnedIDs, expectedIDs)
|
|
|
|
}
|
|
|
|
}
|
2023-05-18 14:41:47 -05:00
|
|
|
|
|
|
|
func mustCreateDatabase(t *testing.T, dbType test.DBType) (storage.Database, func()) {
|
|
|
|
conStr, close := test.PrepareDBConnectionString(t, dbType)
|
|
|
|
caches := caching.NewRistrettoCache(8*1024*1024, time.Hour, caching.DisableMetrics)
|
|
|
|
cm := sqlutil.NewConnectionManager(nil, config.DatabaseOptions{})
|
|
|
|
db, err := storage.Open(context.Background(), cm, &config.DatabaseOptions{ConnectionString: config.DataSource(conStr)}, caches)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to create Database: %v", err)
|
|
|
|
}
|
|
|
|
return db, close
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCurrentEventIsNil(t *testing.T) {
|
|
|
|
test.WithAllDatabases(t, func(t *testing.T, dbType test.DBType) {
|
|
|
|
db, close := mustCreateDatabase(t, dbType)
|
|
|
|
defer close()
|
|
|
|
querier := Queryer{
|
|
|
|
DB: db,
|
|
|
|
}
|
|
|
|
|
|
|
|
roomID, _ := spec.NewRoomID("!room:server")
|
|
|
|
event, _ := querier.CurrentStateEvent(context.Background(), *roomID, spec.MRoomMember, "@user:server")
|
|
|
|
if event != nil {
|
|
|
|
t.Fatal("Event should equal nil, most likely this is failing because the interface type is not nil, but the value is.")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|