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"
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
FailuresUntilAssumedOffline = 3
|
|
|
|
FailuresUntilBlacklist = 8
|
|
|
|
)
|
|
|
|
|
2023-04-19 15:50:33 +01:00
|
|
|
func (t *testFedClient) QueryKeys(ctx context.Context, origin, s spec.ServerName, keys map[string][]string) (fclient.RespQueryKeys, error) {
|
2023-01-23 17:55:12 +00:00
|
|
|
t.queryKeysCalled = true
|
|
|
|
if t.shouldFail {
|
2023-04-06 09:55:01 +01:00
|
|
|
return fclient.RespQueryKeys{}, fmt.Errorf("Failure")
|
2023-01-23 17:55:12 +00:00
|
|
|
}
|
2023-04-06 09:55:01 +01:00
|
|
|
return fclient.RespQueryKeys{}, nil
|
2023-01-23 17:55:12 +00:00
|
|
|
}
|
|
|
|
|
2023-04-19 15:50:33 +01:00
|
|
|
func (t *testFedClient) ClaimKeys(ctx context.Context, origin, s spec.ServerName, oneTimeKeys map[string]map[string]string) (fclient.RespClaimKeys, error) {
|
2023-01-23 17:55:12 +00:00
|
|
|
t.claimKeysCalled = true
|
|
|
|
if t.shouldFail {
|
2023-04-06 09:55:01 +01:00
|
|
|
return fclient.RespClaimKeys{}, fmt.Errorf("Failure")
|
2023-01-23 17:55:12 +00:00
|
|
|
}
|
2023-04-06 09:55:01 +01:00
|
|
|
return fclient.RespClaimKeys{}, nil
|
2023-01-23 17:55:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFederationClientQueryKeys(t *testing.T) {
|
|
|
|
testDB := test.NewInMemoryFederationDatabase()
|
|
|
|
|
|
|
|
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: "server",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
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 := FederationInternalAPI{
|
|
|
|
db: testDB,
|
|
|
|
cfg: &cfg,
|
|
|
|
statistics: &stats,
|
|
|
|
federation: fedClient,
|
|
|
|
queues: queues,
|
|
|
|
}
|
|
|
|
_, err := fedapi.QueryKeys(context.Background(), "origin", "server", nil)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.True(t, fedClient.queryKeysCalled)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFederationClientQueryKeysBlacklisted(t *testing.T) {
|
|
|
|
testDB := test.NewInMemoryFederationDatabase()
|
|
|
|
testDB.AddServerToBlacklist("server")
|
|
|
|
|
|
|
|
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: "server",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
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 := FederationInternalAPI{
|
|
|
|
db: testDB,
|
|
|
|
cfg: &cfg,
|
|
|
|
statistics: &stats,
|
|
|
|
federation: fedClient,
|
|
|
|
queues: queues,
|
|
|
|
}
|
|
|
|
_, err := fedapi.QueryKeys(context.Background(), "origin", "server", nil)
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
assert.False(t, fedClient.queryKeysCalled)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFederationClientQueryKeysFailure(t *testing.T) {
|
|
|
|
testDB := test.NewInMemoryFederationDatabase()
|
|
|
|
|
|
|
|
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: "server",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
fedClient := &testFedClient{shouldFail: true}
|
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 := FederationInternalAPI{
|
|
|
|
db: testDB,
|
|
|
|
cfg: &cfg,
|
|
|
|
statistics: &stats,
|
|
|
|
federation: fedClient,
|
|
|
|
queues: queues,
|
|
|
|
}
|
|
|
|
_, err := fedapi.QueryKeys(context.Background(), "origin", "server", nil)
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
assert.True(t, fedClient.queryKeysCalled)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFederationClientClaimKeys(t *testing.T) {
|
|
|
|
testDB := test.NewInMemoryFederationDatabase()
|
|
|
|
|
|
|
|
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: "server",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
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 := FederationInternalAPI{
|
|
|
|
db: testDB,
|
|
|
|
cfg: &cfg,
|
|
|
|
statistics: &stats,
|
|
|
|
federation: fedClient,
|
|
|
|
queues: queues,
|
|
|
|
}
|
|
|
|
_, err := fedapi.ClaimKeys(context.Background(), "origin", "server", nil)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.True(t, fedClient.claimKeysCalled)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFederationClientClaimKeysBlacklisted(t *testing.T) {
|
|
|
|
testDB := test.NewInMemoryFederationDatabase()
|
|
|
|
testDB.AddServerToBlacklist("server")
|
|
|
|
|
|
|
|
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: "server",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
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 := FederationInternalAPI{
|
|
|
|
db: testDB,
|
|
|
|
cfg: &cfg,
|
|
|
|
statistics: &stats,
|
|
|
|
federation: fedClient,
|
|
|
|
queues: queues,
|
|
|
|
}
|
|
|
|
_, err := fedapi.ClaimKeys(context.Background(), "origin", "server", nil)
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
assert.False(t, fedClient.claimKeysCalled)
|
|
|
|
}
|