2023-01-23 17:55:12 +00:00
|
|
|
// Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-07-06 16:15:24 +01:00
|
|
|
"crypto/ed25519"
|
2023-01-23 17:55:12 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/matrix-org/dendrite/federationapi/api"
|
|
|
|
"github.com/matrix-org/dendrite/federationapi/queue"
|
|
|
|
"github.com/matrix-org/dendrite/federationapi/statistics"
|
|
|
|
"github.com/matrix-org/dendrite/setup/config"
|
|
|
|
"github.com/matrix-org/dendrite/setup/process"
|
|
|
|
"github.com/matrix-org/dendrite/test"
|
2023-04-06 09:55:01 +01:00
|
|
|
"github.com/matrix-org/gomatrixserverlib/fclient"
|
2023-04-19 15:50:33 +01:00
|
|
|
"github.com/matrix-org/gomatrixserverlib/spec"
|
2023-01-23 17:55:12 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
type testFedClient struct {
|
2023-04-24 17:23:25 +01:00
|
|
|
fclient.FederationClient
|
2023-01-23 17:55:12 +00:00
|
|
|
queryKeysCalled bool
|
|
|
|
claimKeysCalled bool
|
|
|
|
shouldFail bool
|
|
|
|
}
|
|
|
|
|
2023-04-19 15:50:33 +01:00
|
|
|
func (t *testFedClient) LookupRoomAlias(ctx context.Context, origin, s spec.ServerName, roomAlias string) (res fclient.RespDirectory, err error) {
|
2023-04-06 09:55:01 +01:00
|
|
|
return fclient.RespDirectory{}, nil
|
2023-01-23 17:55:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPerformWakeupServers(t *testing.T) {
|
|
|
|
testDB := test.NewInMemoryFederationDatabase()
|
|
|
|
|
2023-04-19 15:50:33 +01:00
|
|
|
server := spec.ServerName("wakeup")
|
2023-01-23 17:55:12 +00:00
|
|
|
testDB.AddServerToBlacklist(server)
|
|
|
|
testDB.SetServerAssumedOffline(context.Background(), server)
|
|
|
|
blacklisted, err := testDB.IsServerBlacklisted(server)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, blacklisted)
|
|
|
|
offline, err := testDB.IsServerAssumedOffline(context.Background(), server)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, offline)
|
|
|
|
|
2023-07-06 16:15:24 +01:00
|
|
|
_, key, err := ed25519.GenerateKey(nil)
|
|
|
|
assert.NoError(t, err)
|
2023-01-23 17:55:12 +00:00
|
|
|
cfg := config.FederationAPI{
|
|
|
|
Matrix: &config.Global{
|
2023-04-06 09:55:01 +01:00
|
|
|
SigningIdentity: fclient.SigningIdentity{
|
2023-01-23 17:55:12 +00:00
|
|
|
ServerName: "relay",
|
2023-07-06 16:15:24 +01:00
|
|
|
KeyID: "ed25519:1",
|
|
|
|
PrivateKey: key,
|
2023-01-23 17:55:12 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
fedClient := &testFedClient{}
|
2024-02-28 19:59:34 +00:00
|
|
|
stats := statistics.NewStatistics(testDB, FailuresUntilBlacklist, FailuresUntilAssumedOffline, true)
|
2023-01-23 17:55:12 +00:00
|
|
|
queues := queue.NewOutgoingQueues(
|
|
|
|
testDB, process.NewProcessContext(),
|
|
|
|
false,
|
2023-11-22 14:38:04 +00:00
|
|
|
cfg.Matrix.ServerName, fedClient, &stats,
|
2023-01-23 17:55:12 +00:00
|
|
|
nil,
|
|
|
|
)
|
|
|
|
fedAPI := NewFederationInternalAPI(
|
|
|
|
testDB, &cfg, nil, fedClient, &stats, nil, queues, nil,
|
|
|
|
)
|
|
|
|
|
|
|
|
req := api.PerformWakeupServersRequest{
|
2023-04-19 15:50:33 +01:00
|
|
|
ServerNames: []spec.ServerName{server},
|
2023-01-23 17:55:12 +00:00
|
|
|
}
|
|
|
|
res := api.PerformWakeupServersResponse{}
|
|
|
|
err = fedAPI.PerformWakeupServers(context.Background(), &req, &res)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
blacklisted, err = testDB.IsServerBlacklisted(server)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.False(t, blacklisted)
|
|
|
|
offline, err = testDB.IsServerAssumedOffline(context.Background(), server)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.False(t, offline)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestQueryRelayServers(t *testing.T) {
|
|
|
|
testDB := test.NewInMemoryFederationDatabase()
|
|
|
|
|
2023-04-19 15:50:33 +01:00
|
|
|
server := spec.ServerName("wakeup")
|
|
|
|
relayServers := []spec.ServerName{"relay1", "relay2"}
|
2023-01-23 17:55:12 +00:00
|
|
|
err := testDB.P2PAddRelayServersForServer(context.Background(), server, relayServers)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2023-07-06 16:15:24 +01:00
|
|
|
_, key, err := ed25519.GenerateKey(nil)
|
|
|
|
assert.NoError(t, err)
|
2023-01-23 17:55:12 +00:00
|
|
|
cfg := config.FederationAPI{
|
|
|
|
Matrix: &config.Global{
|
2023-04-06 09:55:01 +01:00
|
|
|
SigningIdentity: fclient.SigningIdentity{
|
2023-01-23 17:55:12 +00:00
|
|
|
ServerName: "relay",
|
2023-07-06 16:15:24 +01:00
|
|
|
KeyID: "ed25519:1",
|
|
|
|
PrivateKey: key,
|
2023-01-23 17:55:12 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
fedClient := &testFedClient{}
|
2024-02-28 19:59:34 +00:00
|
|
|
stats := statistics.NewStatistics(testDB, FailuresUntilBlacklist, FailuresUntilAssumedOffline, false)
|
2023-01-23 17:55:12 +00:00
|
|
|
queues := queue.NewOutgoingQueues(
|
|
|
|
testDB, process.NewProcessContext(),
|
|
|
|
false,
|
2023-11-22 14:38:04 +00:00
|
|
|
cfg.Matrix.ServerName, fedClient, &stats,
|
2023-01-23 17:55:12 +00:00
|
|
|
nil,
|
|
|
|
)
|
|
|
|
fedAPI := NewFederationInternalAPI(
|
|
|
|
testDB, &cfg, nil, fedClient, &stats, nil, queues, nil,
|
|
|
|
)
|
|
|
|
|
|
|
|
req := api.P2PQueryRelayServersRequest{
|
|
|
|
Server: server,
|
|
|
|
}
|
|
|
|
res := api.P2PQueryRelayServersResponse{}
|
|
|
|
err = fedAPI.P2PQueryRelayServers(context.Background(), &req, &res)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, len(relayServers), len(res.RelayServers))
|
|
|
|
}
|
|
|
|
|
2023-01-28 23:27:53 +00:00
|
|
|
func TestRemoveRelayServers(t *testing.T) {
|
|
|
|
testDB := test.NewInMemoryFederationDatabase()
|
|
|
|
|
2023-04-19 15:50:33 +01:00
|
|
|
server := spec.ServerName("wakeup")
|
|
|
|
relayServers := []spec.ServerName{"relay1", "relay2"}
|
2023-01-28 23:27:53 +00:00
|
|
|
err := testDB.P2PAddRelayServersForServer(context.Background(), server, relayServers)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2023-07-06 16:15:24 +01:00
|
|
|
_, key, err := ed25519.GenerateKey(nil)
|
|
|
|
assert.NoError(t, err)
|
2023-01-28 23:27:53 +00:00
|
|
|
cfg := config.FederationAPI{
|
|
|
|
Matrix: &config.Global{
|
2023-04-06 09:55:01 +01:00
|
|
|
SigningIdentity: fclient.SigningIdentity{
|
2023-01-28 23:27:53 +00:00
|
|
|
ServerName: "relay",
|
2023-07-06 16:15:24 +01:00
|
|
|
KeyID: "ed25519:1",
|
|
|
|
PrivateKey: key,
|
2023-01-28 23:27:53 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
fedClient := &testFedClient{}
|
2024-02-28 19:59:34 +00:00
|
|
|
stats := statistics.NewStatistics(testDB, FailuresUntilBlacklist, FailuresUntilAssumedOffline, false)
|
2023-01-28 23:27:53 +00:00
|
|
|
queues := queue.NewOutgoingQueues(
|
|
|
|
testDB, process.NewProcessContext(),
|
|
|
|
false,
|
2023-11-22 14:38:04 +00:00
|
|
|
cfg.Matrix.ServerName, fedClient, &stats,
|
2023-01-28 23:27:53 +00:00
|
|
|
nil,
|
|
|
|
)
|
|
|
|
fedAPI := NewFederationInternalAPI(
|
|
|
|
testDB, &cfg, nil, fedClient, &stats, nil, queues, nil,
|
|
|
|
)
|
|
|
|
|
|
|
|
req := api.P2PRemoveRelayServersRequest{
|
|
|
|
Server: server,
|
2023-04-19 15:50:33 +01:00
|
|
|
RelayServers: []spec.ServerName{"relay1"},
|
2023-01-28 23:27:53 +00:00
|
|
|
}
|
|
|
|
res := api.P2PRemoveRelayServersResponse{}
|
|
|
|
err = fedAPI.P2PRemoveRelayServers(context.Background(), &req, &res)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
finalRelays, err := testDB.P2PGetRelayServersForServer(context.Background(), server)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 1, len(finalRelays))
|
2023-04-19 15:50:33 +01:00
|
|
|
assert.Equal(t, spec.ServerName("relay2"), finalRelays[0])
|
2023-01-28 23:27:53 +00:00
|
|
|
}
|
|
|
|
|
2023-01-23 17:55:12 +00:00
|
|
|
func TestPerformDirectoryLookup(t *testing.T) {
|
|
|
|
testDB := test.NewInMemoryFederationDatabase()
|
|
|
|
|
2023-07-06 16:15:24 +01:00
|
|
|
_, key, err := ed25519.GenerateKey(nil)
|
|
|
|
assert.NoError(t, err)
|
2023-01-23 17:55:12 +00:00
|
|
|
cfg := config.FederationAPI{
|
|
|
|
Matrix: &config.Global{
|
2023-04-06 09:55:01 +01:00
|
|
|
SigningIdentity: fclient.SigningIdentity{
|
2023-01-23 17:55:12 +00:00
|
|
|
ServerName: "relay",
|
2023-07-06 16:15:24 +01:00
|
|
|
KeyID: "ed25519:1",
|
|
|
|
PrivateKey: key,
|
2023-01-23 17:55:12 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
fedClient := &testFedClient{}
|
2024-02-28 19:59:34 +00:00
|
|
|
stats := statistics.NewStatistics(testDB, FailuresUntilBlacklist, FailuresUntilAssumedOffline, false)
|
2023-01-23 17:55:12 +00:00
|
|
|
queues := queue.NewOutgoingQueues(
|
|
|
|
testDB, process.NewProcessContext(),
|
|
|
|
false,
|
2023-11-22 14:38:04 +00:00
|
|
|
cfg.Matrix.ServerName, fedClient, &stats,
|
2023-01-23 17:55:12 +00:00
|
|
|
nil,
|
|
|
|
)
|
|
|
|
fedAPI := NewFederationInternalAPI(
|
|
|
|
testDB, &cfg, nil, fedClient, &stats, nil, queues, nil,
|
|
|
|
)
|
|
|
|
|
|
|
|
req := api.PerformDirectoryLookupRequest{
|
|
|
|
RoomAlias: "room",
|
|
|
|
ServerName: "server",
|
|
|
|
}
|
|
|
|
res := api.PerformDirectoryLookupResponse{}
|
2023-07-06 16:15:24 +01:00
|
|
|
err = fedAPI.PerformDirectoryLookup(context.Background(), &req, &res)
|
2023-01-23 17:55:12 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPerformDirectoryLookupRelaying(t *testing.T) {
|
|
|
|
testDB := test.NewInMemoryFederationDatabase()
|
|
|
|
|
2023-04-19 15:50:33 +01:00
|
|
|
server := spec.ServerName("wakeup")
|
2023-01-23 17:55:12 +00:00
|
|
|
testDB.SetServerAssumedOffline(context.Background(), server)
|
2023-04-19 15:50:33 +01:00
|
|
|
testDB.P2PAddRelayServersForServer(context.Background(), server, []spec.ServerName{"relay"})
|
2023-01-23 17:55:12 +00:00
|
|
|
|
2023-07-06 16:15:24 +01:00
|
|
|
_, key, err := ed25519.GenerateKey(nil)
|
|
|
|
assert.NoError(t, err)
|
2023-01-23 17:55:12 +00:00
|
|
|
cfg := config.FederationAPI{
|
|
|
|
Matrix: &config.Global{
|
2023-04-06 09:55:01 +01:00
|
|
|
SigningIdentity: fclient.SigningIdentity{
|
2023-07-06 16:15:24 +01:00
|
|
|
ServerName: "relay",
|
|
|
|
KeyID: "ed25519:1",
|
|
|
|
PrivateKey: key,
|
2023-01-23 17:55:12 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
fedClient := &testFedClient{}
|
2024-02-28 19:59:34 +00:00
|
|
|
stats := statistics.NewStatistics(testDB, FailuresUntilBlacklist, FailuresUntilAssumedOffline, true)
|
2023-01-23 17:55:12 +00:00
|
|
|
queues := queue.NewOutgoingQueues(
|
|
|
|
testDB, process.NewProcessContext(),
|
|
|
|
false,
|
2023-11-22 14:38:04 +00:00
|
|
|
cfg.Matrix.ServerName, fedClient, &stats,
|
2023-01-23 17:55:12 +00:00
|
|
|
nil,
|
|
|
|
)
|
|
|
|
fedAPI := NewFederationInternalAPI(
|
|
|
|
testDB, &cfg, nil, fedClient, &stats, nil, queues, nil,
|
|
|
|
)
|
|
|
|
|
|
|
|
req := api.PerformDirectoryLookupRequest{
|
|
|
|
RoomAlias: "room",
|
|
|
|
ServerName: server,
|
|
|
|
}
|
|
|
|
res := api.PerformDirectoryLookupResponse{}
|
2023-07-06 16:15:24 +01:00
|
|
|
err = fedAPI.PerformDirectoryLookup(context.Background(), &req, &res)
|
2023-01-23 17:55:12 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
}
|