2023-01-23 18:55:12 +01: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 routing_test
|
2020-05-06 15:27:02 +02:00
|
|
|
|
|
|
|
import (
|
2023-01-23 18:55:12 +01:00
|
|
|
"encoding/hex"
|
2020-05-06 15:27:02 +02:00
|
|
|
"encoding/json"
|
2023-01-23 18:55:12 +01:00
|
|
|
"net/http/httptest"
|
2020-05-06 15:27:02 +02:00
|
|
|
"testing"
|
|
|
|
|
2023-01-23 18:55:12 +01:00
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/matrix-org/dendrite/cmd/dendrite-demo-yggdrasil/signing"
|
|
|
|
fedAPI "github.com/matrix-org/dendrite/federationapi"
|
|
|
|
"github.com/matrix-org/dendrite/federationapi/routing"
|
2023-03-22 09:21:32 +01:00
|
|
|
"github.com/matrix-org/dendrite/internal/caching"
|
2023-01-23 18:55:12 +01:00
|
|
|
"github.com/matrix-org/dendrite/internal/httputil"
|
2023-03-22 09:21:32 +01:00
|
|
|
"github.com/matrix-org/dendrite/internal/sqlutil"
|
|
|
|
"github.com/matrix-org/dendrite/setup/jetstream"
|
2022-05-17 14:23:35 +02:00
|
|
|
"github.com/matrix-org/dendrite/test"
|
2023-01-23 18:55:12 +01:00
|
|
|
"github.com/matrix-org/dendrite/test/testrig"
|
2020-05-06 15:27:02 +02:00
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2023-04-19 16:50:33 +02:00
|
|
|
"github.com/matrix-org/gomatrixserverlib/fclient"
|
|
|
|
"github.com/matrix-org/gomatrixserverlib/spec"
|
2023-01-23 18:55:12 +01:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"golang.org/x/crypto/ed25519"
|
2020-05-06 15:27:02 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2023-04-19 16:50:33 +02:00
|
|
|
testOrigin = spec.ServerName("kaer.morhen")
|
2020-05-06 15:27:02 +02:00
|
|
|
)
|
|
|
|
|
2023-01-23 18:55:12 +01:00
|
|
|
type sendContent struct {
|
|
|
|
PDUs []json.RawMessage `json:"pdus"`
|
|
|
|
EDUs []gomatrixserverlib.EDU `json:"edus"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHandleSend(t *testing.T) {
|
|
|
|
test.WithAllDatabases(t, func(t *testing.T, dbType test.DBType) {
|
2023-03-22 09:21:32 +01:00
|
|
|
cfg, processCtx, close := testrig.CreateConfig(t, dbType)
|
|
|
|
cm := sqlutil.NewConnectionManager(processCtx, cfg.Global.DatabaseOptions)
|
|
|
|
routers := httputil.NewRouters()
|
2023-01-23 18:55:12 +01:00
|
|
|
defer close()
|
|
|
|
|
|
|
|
fedMux := mux.NewRouter().SkipClean(true).PathPrefix(httputil.PublicFederationPathPrefix).Subrouter().UseEncodedPath()
|
2023-03-22 09:21:32 +01:00
|
|
|
natsInstance := jetstream.NATSInstance{}
|
|
|
|
routers.Federation = fedMux
|
|
|
|
cfg.FederationAPI.Matrix.SigningIdentity.ServerName = testOrigin
|
|
|
|
cfg.FederationAPI.Matrix.Metrics.Enabled = false
|
|
|
|
fedapi := fedAPI.NewInternalAPI(processCtx, cfg, cm, &natsInstance, nil, nil, nil, nil, true)
|
2023-01-23 18:55:12 +01:00
|
|
|
serverKeyAPI := &signing.YggdrasilKeys{}
|
|
|
|
keyRing := serverKeyAPI.KeyRing()
|
2023-11-09 08:43:27 +01:00
|
|
|
|
|
|
|
routing.Setup(routers, cfg, nil, fedapi, keyRing, nil, nil, &cfg.MSCs, nil, caching.DisableMetrics)
|
2023-01-23 18:55:12 +01:00
|
|
|
|
|
|
|
handler := fedMux.Get(routing.SendRouteName).GetHandler().ServeHTTP
|
|
|
|
_, sk, _ := ed25519.GenerateKey(nil)
|
|
|
|
keyID := signing.KeyID
|
|
|
|
pk := sk.Public().(ed25519.PublicKey)
|
2023-04-19 16:50:33 +02:00
|
|
|
serverName := spec.ServerName(hex.EncodeToString(pk))
|
|
|
|
req := fclient.NewFederationRequest("PUT", serverName, testOrigin, "/send/1234")
|
2023-01-23 18:55:12 +01:00
|
|
|
content := sendContent{}
|
|
|
|
err := req.SetContent(content)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error: %s", err.Error())
|
2020-05-12 17:24:28 +02:00
|
|
|
}
|
2023-01-23 18:55:12 +01:00
|
|
|
req.Sign(serverName, gomatrixserverlib.KeyID(keyID), sk)
|
|
|
|
httpReq, err := req.HTTPRequest()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error: %s", err.Error())
|
2020-05-12 17:24:28 +02:00
|
|
|
}
|
2023-01-23 18:55:12 +01:00
|
|
|
vars := map[string]string{"txnID": "1234"}
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
httpReq = mux.SetURLVars(httpReq, vars)
|
|
|
|
handler(w, httpReq)
|
2020-05-06 19:03:25 +02:00
|
|
|
|
2023-01-23 18:55:12 +01:00
|
|
|
res := w.Result()
|
|
|
|
assert.Equal(t, 200, res.StatusCode)
|
|
|
|
})
|
2020-05-06 19:03:25 +02:00
|
|
|
}
|