Skip to content

Commit

Permalink
Add test for the order in which OTKs are issued
Browse files Browse the repository at this point in the history
Per MSC4225, they should be issued in the same order they were uploaded.
  • Loading branch information
richvdh committed Nov 7, 2024
1 parent c246a09 commit 97c1592
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions tests/csapi/upload_keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/http"
"strings"
"testing"
"time"

"github.com/tidwall/gjson"

Expand Down Expand Up @@ -174,6 +175,45 @@ func TestUploadKey(t *testing.T) {
})
}

// Per MSC4225, keys must be issued in the same order they are uploaded
func TestKeyClaimOrdering(t *testing.T) {
deployment := complement.Deploy(t, 1)
defer deployment.Destroy(t)
alice := deployment.Register(t, "hs1", helpers.RegistrationOpts{})
_, oneTimeKeys := alice.MustGenerateOneTimeKeys(t, 2)

// first upload key 1, sleep a bit, then upload key 0.
otk1 := map[string]interface{}{"signed_curve25519:1": oneTimeKeys["signed_curve25519:1"]}
alice.MustDo(t, "POST", []string{"_matrix", "client", "v3", "keys", "upload"},
client.WithJSONBody(t, map[string]interface{}{"one_time_keys": otk1}))

time.Sleep(1 * time.Second)

otk0 := map[string]interface{}{"signed_curve25519:0": oneTimeKeys["signed_curve25519:0"]}
alice.MustDo(t, "POST", []string{"_matrix", "client", "v3", "keys", "upload"},
client.WithJSONBody(t, map[string]interface{}{"one_time_keys": otk0}))

// Now claim the keys, and check they come back in the right order
reqBody := client.WithJSONBody(t, map[string]interface{}{
"one_time_keys": map[string]interface{}{
alice.UserID: map[string]string{
alice.DeviceID: "signed_curve25519",
},
},
})
otksField := "one_time_keys." + client.GjsonEscape(alice.UserID) + "." + client.GjsonEscape(alice.DeviceID)
resp := alice.MustDo(t, "POST", []string{"_matrix", "client", "v3", "keys", "claim"}, reqBody)
must.MatchResponse(t, resp, match.HTTPResponse{
StatusCode: http.StatusOK,
JSON: []match.JSON{match.JSONKeyEqual(otksField, otk1)},
})
resp = alice.MustDo(t, "POST", []string{"_matrix", "client", "v3", "keys", "claim"}, reqBody)
must.MatchResponse(t, resp, match.HTTPResponse{
StatusCode: http.StatusOK,
JSON: []match.JSON{match.JSONKeyEqual(otksField, otk0)},
})
}

// Tests idempotency of the /keys/upload endpoint.
// Tests that if you upload 4 OTKs then upload the same 4, no error is returned.
func TestUploadKeyIdempotency(t *testing.T) {
Expand Down

0 comments on commit 97c1592

Please sign in to comment.