Skip to content

Commit ec15cf2

Browse files
authored
Add test for the order in which OTKs are issued (#744)
Per MSC4225, they should be issued in the same order they were uploaded.
1 parent fc63446 commit ec15cf2

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

tests/csapi/upload_keys_test.go

+41
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"net/http"
66
"strings"
77
"testing"
8+
"time"
89

910
"github.com/tidwall/gjson"
1011

@@ -174,6 +175,46 @@ func TestUploadKey(t *testing.T) {
174175
})
175176
}
176177

178+
// Per MSC4225, keys must be issued in the same order they are uploaded
179+
func TestKeyClaimOrdering(t *testing.T) {
180+
deployment := complement.Deploy(t, 1)
181+
defer deployment.Destroy(t)
182+
alice := deployment.Register(t, "hs1", helpers.RegistrationOpts{})
183+
_, oneTimeKeys := alice.MustGenerateOneTimeKeys(t, 2)
184+
185+
// first upload key 1, sleep a bit, then upload key 0.
186+
otk1 := map[string]interface{}{"signed_curve25519:1": oneTimeKeys["signed_curve25519:1"]}
187+
alice.MustDo(t, "POST", []string{"_matrix", "client", "v3", "keys", "upload"},
188+
client.WithJSONBody(t, map[string]interface{}{"one_time_keys": otk1}))
189+
190+
// Ensure that there is a difference in timestamp between the two upload requests.
191+
time.Sleep(1 * time.Second)
192+
193+
otk0 := map[string]interface{}{"signed_curve25519:0": oneTimeKeys["signed_curve25519:0"]}
194+
alice.MustDo(t, "POST", []string{"_matrix", "client", "v3", "keys", "upload"},
195+
client.WithJSONBody(t, map[string]interface{}{"one_time_keys": otk0}))
196+
197+
// Now claim the keys, and check they come back in the right order
198+
reqBody := client.WithJSONBody(t, map[string]interface{}{
199+
"one_time_keys": map[string]interface{}{
200+
alice.UserID: map[string]string{
201+
alice.DeviceID: "signed_curve25519",
202+
},
203+
},
204+
})
205+
otksField := "one_time_keys." + client.GjsonEscape(alice.UserID) + "." + client.GjsonEscape(alice.DeviceID)
206+
resp := alice.MustDo(t, "POST", []string{"_matrix", "client", "v3", "keys", "claim"}, reqBody)
207+
must.MatchResponse(t, resp, match.HTTPResponse{
208+
StatusCode: http.StatusOK,
209+
JSON: []match.JSON{match.JSONKeyEqual(otksField, otk1)},
210+
})
211+
resp = alice.MustDo(t, "POST", []string{"_matrix", "client", "v3", "keys", "claim"}, reqBody)
212+
must.MatchResponse(t, resp, match.HTTPResponse{
213+
StatusCode: http.StatusOK,
214+
JSON: []match.JSON{match.JSONKeyEqual(otksField, otk0)},
215+
})
216+
}
217+
177218
// Tests idempotency of the /keys/upload endpoint.
178219
// Tests that if you upload 4 OTKs then upload the same 4, no error is returned.
179220
func TestUploadKeyIdempotency(t *testing.T) {

0 commit comments

Comments
 (0)