forked from matrix-org/gomatrixserverlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest_test.go
177 lines (152 loc) · 5.25 KB
/
request_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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package gomatrixserverlib
import (
"bufio"
"bytes"
"encoding/base64"
"net/http"
"testing"
"time"
"golang.org/x/crypto/ed25519"
)
// This GET request is taken from a request made by a synapse run by sytest.
// The headers have been reordered to match the order net/http writes them in.
const exampleGetRequest = "GET /_matrix/federation/v1/query/directory?room_alias=%23test%3Alocalhost%3A44033 HTTP/1.1\r\n" +
"Host: localhost:44033\r\n" +
"Authorization: X-Matrix" +
" origin=\"localhost:8800\"" +
",key=\"ed25519:a_Obwu\"" +
",sig=\"7vt4vP/w8zYB3Zg77nuTPwie3TxEy2OHZQMsSa4nsXZzL4/qw+DguXbyMy3BF77XvSJmBt+Gw+fU6T4HId7fBg\"" +
",destination=\"localhost:44033\"" +
"\r\n" +
"\r\n"
// This PUT request is taken from a request made by a synapse run by sytest.
// The headers have been reordered to match the order net/http writes them in.
const examplePutRequest = "PUT /_matrix/federation/v1/send/1493385816575/ HTTP/1.1\r\n" +
"Host: localhost:44033\r\n" +
"Content-Length: 321\r\n" +
"Authorization: X-Matrix" +
" origin=\"localhost:8800\"" +
",key=\"ed25519:a_Obwu\"" +
",sig=\"+hmW6UjEXx7vMt2+MXO/EImSfdEYdBsZEOmpiz3evYktAgGNpGuNMBYXIA969WGubmceREKA/r1phasUFHBpDg\"" +
",destination=\"localhost:44033\"" +
"\r\n" +
"Content-Type: application/json\r\n" +
"\r\n" +
examplePutContent
const examplePutContent = `{"edus":[{"content":{"device_id":"YHRUBZNPFS",` +
`"keys":{"device_id":"YHRUBZNPFS","device_keys":{},"user_id":` +
`"@ANON-22:localhost:8800"},"prev_id":[],"stream_id":30,"user_id":` +
`"@ANON-22:localhost:8800"},"edu_type":"m.device_list_update"}],"origin"` +
`:"localhost:8800","origin_server_ts":1493385822396,"pdu_failures":[],` +
`"pdus":[]}`
func TestSignGetRequest(t *testing.T) {
request := NewFederationRequest(
"GET", "localhost:8800", "localhost:44033",
"/_matrix/federation/v1/query/directory?room_alias=%23test%3Alocalhost%3A44033",
)
if err := request.Sign("localhost:8800", "ed25519:a_Obwu", privateKey1); err != nil {
t.Fatal(err)
}
hr, err := request.HTTPRequest()
if err != nil {
t.Fatal(err)
}
hr.Header.Set("User-Agent", "")
buf := bytes.NewBuffer(nil)
if err = hr.Write(buf); err != nil {
t.Fatal(err)
}
got := buf.String()
want := exampleGetRequest
if want != got {
t.Errorf("Wanted %q got %q", want, got)
}
}
func TestVerifyGetRequest(t *testing.T) {
hr, err := http.ReadRequest(bufio.NewReader(bytes.NewReader([]byte(exampleGetRequest))))
if err != nil {
t.Fatal(err)
}
request, jsonResp := VerifyHTTPRequest(
hr, time.Unix(1493142432, 96400), "localhost:44033", nil, KeyRing{nil, &testKeyDatabase{}},
)
if request == nil {
t.Fatalf("Wanted non-nil request got nil. (request was %#v, response was %#v)", hr, jsonResp)
}
if request.Method() != "GET" {
t.Errorf("Wanted request.Method() to be \"GET\" got %q", request.Method())
}
if request.Origin() != "localhost:8800" {
t.Errorf("Wanted request.Origin() to be \"localhost:8800\" got %q", request.Origin())
}
if request.Content() != nil {
t.Errorf("Wanted request.Content() to be nil got %q", string(request.Content()))
}
wantPath := "/_matrix/federation/v1/query/directory?room_alias=%23test%3Alocalhost%3A44033"
if request.RequestURI() != wantPath {
t.Errorf("Wanted request.RequestURI() to be %q got %q", wantPath, request.RequestURI())
}
}
func TestSignPutRequest(t *testing.T) {
request := NewFederationRequest(
"PUT", "localhost:8800", "localhost:44033", "/_matrix/federation/v1/send/1493385816575/",
)
if err := request.SetContent(RawJSON([]byte(examplePutContent))); err != nil {
t.Fatal(err)
}
if err := request.Sign("localhost:8800", "ed25519:a_Obwu", privateKey1); err != nil {
t.Fatal(err)
}
hr, err := request.HTTPRequest()
if err != nil {
t.Fatal(err)
}
hr.Header.Set("User-Agent", "")
buf := bytes.NewBuffer(nil)
if err = hr.Write(buf); err != nil {
t.Fatal(err)
}
got := buf.String()
want := examplePutRequest
if want != got {
t.Errorf("Wanted %q got %q", want, got)
}
}
func TestVerifyPutRequest(t *testing.T) {
hr, err := http.ReadRequest(bufio.NewReader(bytes.NewReader([]byte(examplePutRequest))))
if err != nil {
t.Fatal(err)
}
request, jsonResp := VerifyHTTPRequest(
hr, time.Unix(1493142432, 96400), "localhost:44033", nil, KeyRing{nil, &testKeyDatabase{}},
)
if request == nil {
t.Fatalf("Wanted non-nil request got nil. (request was %#v, response was %#v)", hr, jsonResp)
}
if request.Method() != "PUT" {
t.Errorf("Wanted request.Method() to be \"PUT\" got %q", request.Method())
}
if request.Origin() != "localhost:8800" {
t.Errorf("Wanted request.Origin() to be \"localhost:8800\" got %q", request.Origin())
}
if string(request.Content()) != examplePutContent {
t.Errorf("Wanted request.Content() to be %q got %q", examplePutContent, string(request.Content()))
}
wantPath := "/_matrix/federation/v1/send/1493385816575/"
if request.RequestURI() != wantPath {
t.Errorf("Wanted request.RequestURI() to be %q got %q", wantPath, request.RequestURI())
}
}
var privateKey1 = mustLoadPrivateKey(privateKeySeed1)
func mustLoadPrivateKey(seed string) ed25519.PrivateKey {
seedBytes, err := base64.RawStdEncoding.DecodeString(seed)
if err != nil {
panic(err)
}
random := bytes.NewBuffer(seedBytes)
_, privateKey, err := ed25519.GenerateKey(random)
if err != nil {
panic(err)
}
return privateKey
}