forked from matrix-org/gomatrixserverlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfederationclient_test.go
149 lines (141 loc) · 7.09 KB
/
federationclient_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package gomatrixserverlib_test
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"reflect"
"strings"
"testing"
"github.com/brianathere/gomatrixserverlib"
"golang.org/x/crypto/ed25519"
)
type roundTripper struct {
fn func(*http.Request) (*http.Response, error)
}
func (r *roundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
return r.fn(req)
}
// The purpose of this test is to make sure that if /v2/send_join 404s we automatically
// fallback to /v1/send_join and seamlessly handle the different response format.
func TestSendJoinFallback(t *testing.T) {
serverName := gomatrixserverlib.ServerName("local.server.name")
targetServerName := gomatrixserverlib.ServerName("target.server.name")
keyID := gomatrixserverlib.KeyID("ed25519:auto")
_, privateKey, _ := ed25519.GenerateKey(nil)
roomVer := gomatrixserverlib.RoomVersionV1
// we don't care about the actual contents, just that it ferries data across fine.
retEv := gomatrixserverlib.RawJSON(`{"auth_events":[],"content":{"creator":"@userid:baba.is.you"},"depth":0,"event_id":"$WCraVpPZe5TtHAqs:baba.is.you","hashes":{"sha256":"EehWNbKy+oDOMC0vIvYl1FekdDxMNuabXKUVzV7DG74"},"origin":"baba.is.you","origin_server_ts":0,"prev_events":[],"prev_state":[],"room_id":"!roomid:baba.is.you","sender":"@userid:baba.is.you","signatures":{"baba.is.you":{"ed25519:auto":"08aF4/bYWKrdGPFdXmZCQU6IrOE1ulpevmWBM3kiShJPAbRbZ6Awk7buWkIxlMF6kX3kb4QpbAlZfHLQgncjCw"}},"state_key":"","type":"m.room.create"}`)
wantRes := gomatrixserverlib.RespSendJoin{
StateEvents: gomatrixserverlib.EventJSONs{
retEv,
},
}
v1Res := []interface{}{
200, wantRes,
}
v1ResBytes, err := json.Marshal(v1Res)
if err != nil {
t.Fatalf("failed to marshal RespSendJoin: %s", err)
}
fc := gomatrixserverlib.NewFederationClient(
serverName, keyID, privateKey,
gomatrixserverlib.WithSkipVerify(true),
)
fc.Client = *gomatrixserverlib.NewClient(gomatrixserverlib.WithTransport(
&roundTripper{
fn: func(req *http.Request) (*http.Response, error) {
if strings.HasPrefix(req.URL.Path, "/_matrix/federation/v2/send_join") {
return &http.Response{
StatusCode: 404,
Body: ioutil.NopCloser(strings.NewReader("404 not found")),
}, nil
}
if !strings.HasPrefix(req.URL.Path, "/_matrix/federation/v1/send_join") {
return nil, fmt.Errorf("test: unexpected url path: %s", req.URL.Path)
}
t.Logf("Sending response: %s", string(v1ResBytes))
return &http.Response{
StatusCode: 200,
Body: ioutil.NopCloser(bytes.NewReader(v1ResBytes)),
}, nil
},
},
))
ev, err := gomatrixserverlib.NewEventFromTrustedJSON(
[]byte(`{"auth_events":[["$WCraVpPZe5TtHAqs:baba.is.you",{"sha256":"gBxQI2xzDLMoyIjkrpCJFBXC5NnrSemepc7SninSARI"}]],"content":{"membership":"join"},"depth":1,"event_id":"$fnwGrQEpiOIUoDU2:baba.is.you","hashes":{"sha256":"DqOjdFgvFQ3V/jvQW2j3ygHL4D+t7/LaIPZ/tHTDZtI"},"origin":"baba.is.you","origin_server_ts":0,"prev_events":[["$WCraVpPZe5TtHAqs:baba.is.you",{"sha256":"gBxQI2xzDLMoyIjkrpCJFBXC5NnrSemepc7SninSARI"}]],"prev_state":[],"room_id":"!roomid:baba.is.you","sender":"@userid:baba.is.you","signatures":{"baba.is.you":{"ed25519:auto":"qBWLb42zicQVsbh333YrcKpHfKokcUOM/ytldGlrgSdXqDEDDxvpcFlfadYnyvj3Z/GjA2XZkqKHanNEh575Bw"}},"state_key":"@userid:baba.is.you","type":"m.room.member"}`),
false, roomVer,
)
if err != nil {
t.Fatalf("failed to read event json: %s", err)
}
res, err := fc.SendJoin(context.Background(), targetServerName, ev)
if err != nil {
t.Fatalf("SendJoin returned an error: %s", err)
}
if !reflect.DeepEqual(res.StateEvents, wantRes.StateEvents) {
t.Fatalf("SendJoin response got %+v want %+v", res.StateEvents, wantRes.StateEvents)
}
}
// The purpose of this test is to ensure that the federation client is capable of marshalling into
// a RespSendJoin response type. This is mostly a sanity check that EventJSON and EventJSONs behave
// correctly.
func TestSendJoinJSON(t *testing.T) {
serverName := gomatrixserverlib.ServerName("local.server.name")
targetServerName := gomatrixserverlib.ServerName("target.server.name")
keyID := gomatrixserverlib.KeyID("ed25519:auto")
_, privateKey, _ := ed25519.GenerateKey(nil)
roomVer := gomatrixserverlib.RoomVersionV1
// we don't care about the actual contents, just that it ferries data across fine.
retEv := gomatrixserverlib.RawJSON(`{"auth_events":[],"content":{"creator":"@userid:baba.is.you"},"depth":0,"event_id":"$WCraVpPZe5TtHAqs:baba.is.you","hashes":{"sha256":"EehWNbKy+oDOMC0vIvYl1FekdDxMNuabXKUVzV7DG74"},"origin":"baba.is.you","origin_server_ts":0,"prev_events":[],"prev_state":[],"room_id":"!roomid:baba.is.you","sender":"@userid:baba.is.you","signatures":{"baba.is.you":{"ed25519:auto":"08aF4/bYWKrdGPFdXmZCQU6IrOE1ulpevmWBM3kiShJPAbRbZ6Awk7buWkIxlMF6kX3kb4QpbAlZfHLQgncjCw"}},"state_key":"","type":"m.room.create"}`)
respSendJoinResponseJSON := []byte(fmt.Sprintf(`{
"state": [%s],
"auth_chain": [%s]
}`, string(retEv), string(retEv)))
fc := gomatrixserverlib.NewFederationClient(
serverName, keyID, privateKey,
gomatrixserverlib.WithSkipVerify(true),
)
fc.Client = *gomatrixserverlib.NewClient(gomatrixserverlib.WithTransport(
&roundTripper{
fn: func(req *http.Request) (*http.Response, error) {
if strings.HasPrefix(req.URL.Path, "/_matrix/federation/v2/send_join") {
t.Logf("Sending response: %s", string(respSendJoinResponseJSON))
return &http.Response{
StatusCode: 200,
Body: ioutil.NopCloser(bytes.NewReader(respSendJoinResponseJSON)),
}, nil
}
return &http.Response{
StatusCode: 404,
Body: ioutil.NopCloser(strings.NewReader("404 not found")),
}, nil
},
},
))
ev, err := gomatrixserverlib.NewEventFromTrustedJSON(
[]byte(`{"auth_events":[["$WCraVpPZe5TtHAqs:baba.is.you",{"sha256":"gBxQI2xzDLMoyIjkrpCJFBXC5NnrSemepc7SninSARI"}]],"content":{"membership":"join"},"depth":1,"event_id":"$fnwGrQEpiOIUoDU2:baba.is.you","hashes":{"sha256":"DqOjdFgvFQ3V/jvQW2j3ygHL4D+t7/LaIPZ/tHTDZtI"},"origin":"baba.is.you","origin_server_ts":0,"prev_events":[["$WCraVpPZe5TtHAqs:baba.is.you",{"sha256":"gBxQI2xzDLMoyIjkrpCJFBXC5NnrSemepc7SninSARI"}]],"prev_state":[],"room_id":"!roomid:baba.is.you","sender":"@userid:baba.is.you","signatures":{"baba.is.you":{"ed25519:auto":"qBWLb42zicQVsbh333YrcKpHfKokcUOM/ytldGlrgSdXqDEDDxvpcFlfadYnyvj3Z/GjA2XZkqKHanNEh575Bw"}},"state_key":"@userid:baba.is.you","type":"m.room.member"}`),
false, roomVer,
)
if err != nil {
t.Fatalf("failed to read event json: %s", err)
}
res, err := fc.SendJoin(context.Background(), targetServerName, ev)
if err != nil {
t.Fatalf("SendJoin returned an error: %s", err)
}
wantStateEvents := gomatrixserverlib.EventJSONs{[]byte(retEv)}
wantAuthChain := gomatrixserverlib.EventJSONs{[]byte(retEv)}
if !reflect.DeepEqual(res.StateEvents, wantStateEvents) {
t.Fatalf("SendJoin response got state %+v want %+v", jsonify(res.StateEvents), jsonify(wantStateEvents))
}
if !reflect.DeepEqual(res.AuthEvents, wantAuthChain) {
t.Fatalf("SendJoin response got auth %+v want %+v", jsonify(res.AuthEvents), jsonify(wantAuthChain))
}
}
func jsonify(x interface{}) string {
b, _ := json.Marshal(x)
return string(b)
}