From c4777bed39a0567e9d3cb4d801348acaa4de5351 Mon Sep 17 00:00:00 2001 From: Eric Eastwood Date: Wed, 10 Dec 2025 17:15:24 -0600 Subject: [PATCH 1/2] Non-working: Test fake homeserver is contactable from host-side > Perhaps the fake homeserver that Complement is mocking isn't running > properly. As a sanity check, we could send a request on the host-side to > the fake server that's also on the host-side that we expect to be > responding. --- federation/handle.go | 20 ++++++++++++++++++++ tests/federation_room_join_test.go | 18 ++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/federation/handle.go b/federation/handle.go index eaaba9b7..a91c8cd0 100644 --- a/federation/handle.go +++ b/federation/handle.go @@ -15,6 +15,26 @@ import ( "github.com/matrix-org/util" ) +// HandleVersionRequests is an option which makes the server process `/_matrix/federation/v1/version` requests. +func HandleVersionRequests() func(*Server) { + return func(srv *Server) { + mux := srv.mux.PathPrefix("/_matrix/federation/v1").Subrouter() + + handlerFn := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { + w.WriteHeader(200) + b, _ := json.Marshal(map[string]interface{}{ + "server": map[string]interface{}{ + "name": "complement-federation-test-server", + "version": "0.1.0", + }, + }) + w.Write(b) + }) + + mux.Handle("/version", handlerFn).Methods("GET") + } +} + // EXPERIMENTAL // MakeJoinRequestsHandler is the http.Handler implementation for the make_join part of // HandleMakeSendJoinRequests. diff --git a/tests/federation_room_join_test.go b/tests/federation_room_join_test.go index 03b12ee0..bf67799b 100644 --- a/tests/federation_room_join_test.go +++ b/tests/federation_room_join_test.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + "io" "net/http" "net/url" "strings" @@ -24,6 +25,7 @@ import ( "github.com/matrix-org/complement/client" "github.com/matrix-org/complement/federation" "github.com/matrix-org/complement/helpers" + "github.com/matrix-org/complement/internal" "github.com/matrix-org/complement/match" "github.com/matrix-org/complement/must" "github.com/matrix-org/complement/runtime" @@ -296,12 +298,17 @@ func TestJoinFederatedRoomWithUnverifiableEvents(t *testing.T) { }) } +func VersionRequestHandler(s *federation.Server, w http.ResponseWriter, req *http.Request) { + // Send it +} + // This test checks that users cannot circumvent the auth checks via send_join. func TestBannedUserCannotSendJoin(t *testing.T) { deployment := complement.Deploy(t, 1) defer deployment.Destroy(t) srv := federation.NewServer(t, deployment, + federation.HandleVersionRequests(), federation.HandleKeyRequests(), federation.HandleTransactionRequests(nil, nil), ) @@ -309,6 +316,17 @@ func TestBannedUserCannotSendJoin(t *testing.T) { origin := srv.ServerName() defer cancel() + // XXX: Sanity check that the server is listening and responding (at-least from the host perspective) + splitPieces := strings.SplitN(string(srv.ServerName()), ":", 2) + port := splitPieces[1] + res, err := http.Get("https://localhost:" + port + "/_matrix/federation/v1/version") + if err != nil { + t.Fatalf("Failed to GET: %s", err) + } + defer internal.CloseIO(res.Body, "server response body") + resBody, err := io.ReadAll(res.Body) + t.Logf("asdf request %d %s", res.StatusCode, resBody) + fedClient := srv.FederationClient(deployment) charlie := srv.UserID("charlie") From 772ec93516adf5a21e1938cd063cbffd1f1e2615 Mon Sep 17 00:00:00 2001 From: Eric Eastwood Date: Wed, 10 Dec 2025 17:59:19 -0600 Subject: [PATCH 2/2] Working request --- tests/federation_room_join_test.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/tests/federation_room_join_test.go b/tests/federation_room_join_test.go index bf67799b..34735679 100644 --- a/tests/federation_room_join_test.go +++ b/tests/federation_room_join_test.go @@ -2,6 +2,7 @@ package tests import ( "context" + "crypto/tls" "encoding/json" "fmt" "io" @@ -319,7 +320,13 @@ func TestBannedUserCannotSendJoin(t *testing.T) { // XXX: Sanity check that the server is listening and responding (at-least from the host perspective) splitPieces := strings.SplitN(string(srv.ServerName()), ":", 2) port := splitPieces[1] - res, err := http.Get("https://localhost:" + port + "/_matrix/federation/v1/version") + // Ignore HTTPS/TLS verification for localhost + httpTransport := &http.Transport{ + TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, + } + httpClient := &http.Client{Transport: httpTransport} + // Make the request + res, err := httpClient.Get("https://localhost:" + port + "/_matrix/federation/v1/version") if err != nil { t.Fatalf("Failed to GET: %s", err) }