|
5 | 5 | "net/http"
|
6 | 6 | "strings"
|
7 | 7 | "testing"
|
| 8 | + "time" |
8 | 9 |
|
9 | 10 | "github.com/tidwall/gjson"
|
10 | 11 |
|
@@ -174,6 +175,46 @@ func TestUploadKey(t *testing.T) {
|
174 | 175 | })
|
175 | 176 | }
|
176 | 177 |
|
| 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 | + |
177 | 218 | // Tests idempotency of the /keys/upload endpoint.
|
178 | 219 | // Tests that if you upload 4 OTKs then upload the same 4, no error is returned.
|
179 | 220 | func TestUploadKeyIdempotency(t *testing.T) {
|
|
0 commit comments