From 89f11b599c595f0b9d7bf559a3d76ca46216aab1 Mon Sep 17 00:00:00 2001 From: Hugh Nimmo-Smith Date: Sat, 4 Mar 2023 08:40:20 +0000 Subject: [PATCH 1/6] Test for transaction ID semantics --- internal/client/client.go | 37 +++++++- tests/csapi/txnid_test.go | 172 +++++++++++++++++++++++++++++++++++++- 2 files changed, 206 insertions(+), 3 deletions(-) diff --git a/internal/client/client.go b/internal/client/client.go index 9a52510d..edd38eaf 100644 --- a/internal/client/client.go +++ b/internal/client/client.go @@ -264,8 +264,14 @@ func (c *CSAPI) SetPushRule(t *testing.T, scope string, kind string, ruleID stri // SendEventUnsynced sends `e` into the room. // Returns the event ID of the sent event. func (c *CSAPI) SendEventUnsynced(t *testing.T, roomID string, e b.Event) string { - t.Helper() txnID := int(atomic.AddInt64(&c.txnID, 1)) + return c.SendEventUnsyncedWithTxnID(t, roomID, e, txnID) +} + +// SendEventUnsynced sends `e` into the room. +// Returns the event ID of the sent event. +func (c *CSAPI) SendEventUnsyncedWithTxnID(t *testing.T, roomID string, e b.Event, txnID int) string { + t.Helper() paths := []string{"_matrix", "client", "v3", "rooms", roomID, "send", e.Type, strconv.Itoa(txnID)} if e.StateKey != nil { paths = []string{"_matrix", "client", "v3", "rooms", roomID, "state", e.Type, *e.StateKey} @@ -438,7 +444,34 @@ func (c *CSAPI) LoginUser(t *testing.T, localpart, password string) (userID, acc return userID, accessToken, deviceID } -//RegisterUser will register the user with given parameters and +// LoginUserWithDeviceID will log in to a homeserver on an existing device +func (c *CSAPI) LoginUserWithDeviceID(t *testing.T, localpart, password, deviceID string) (userID, accessToken string) { + t.Helper() + reqBody := map[string]interface{}{ + "identifier": map[string]interface{}{ + "type": "m.id.user", + "user": localpart, + }, + "device_id": deviceID, + "password": password, + "type": "m.login.password", + } + res := c.MustDoFunc(t, "POST", []string{"_matrix", "client", "v3", "login"}, WithJSONBody(t, reqBody)) + + body, err := ioutil.ReadAll(res.Body) + if err != nil { + t.Fatalf("unable to read response body: %v", err) + } + + userID = gjson.GetBytes(body, "user_id").Str + accessToken = gjson.GetBytes(body, "access_token").Str + if gjson.GetBytes(body, "device_id").Str != deviceID { + t.Fatalf("device_id returned by login does not match the one requested") + } + return userID, accessToken +} + +// RegisterUser will register the user with given parameters and // return user ID & access token, and fail the test on network error func (c *CSAPI) RegisterUser(t *testing.T, localpart, password string) (userID, accessToken, deviceID string) { t.Helper() diff --git a/tests/csapi/txnid_test.go b/tests/csapi/txnid_test.go index 5855d4ff..3c9385aa 100644 --- a/tests/csapi/txnid_test.go +++ b/tests/csapi/txnid_test.go @@ -1,11 +1,12 @@ package csapi_tests import ( + "testing" + "github.com/matrix-org/complement/internal/b" "github.com/matrix-org/complement/internal/client" "github.com/matrix-org/complement/runtime" "github.com/tidwall/gjson" - "testing" ) // TestTxnInEvent checks that the transaction ID is present when getting the event from the /rooms/{roomID}/event/{eventID} endpoint. @@ -39,3 +40,172 @@ func TestTxnInEvent(t *testing.T) { t.Fatalf("Event did not have a 'transaction_id' on the GET /rooms/%s/event/%s response", roomID, eventID) } } + + +func mustHaveTransactionID(t *testing.T, roomID, eventID string) client.SyncCheckOpt { + return client.SyncTimelineHas(roomID, func(r gjson.Result) bool { + if r.Get("event_id").Str == eventID { + if !r.Get("unsigned.transaction_id").Exists() { + t.Fatalf("Event %s in room %s should have a 'transaction_id', but it did not", eventID, roomID) + } + + return true + } + + return false + }) +} + +func mustNotHaveTransactionID(t *testing.T, roomID, eventID string) client.SyncCheckOpt { + return client.SyncTimelineHas(roomID, func(r gjson.Result) bool { + if r.Get("event_id").Str == eventID { + res := r.Get("unsigned.transaction_id") + if res.Exists() { + t.Fatalf("Event %s in room %s should NOT have a 'transaction_id', but it did (%s)", eventID, roomID, res.Str) + } + + return true + } + + return false + }) +} + +// TestTxnScopeOnLocalEcho tests that transaction IDs are scoped to the access token, not the device +// on the sync response +func TestTxnScopeOnLocalEcho(t *testing.T) { + // Conduit scope transaction IDs to the device ID, not the access token. + runtime.SkipIf(t, runtime.Conduit) + + deployment := Deploy(t, b.BlueprintCleanHS) + defer deployment.Destroy(t) + + deployment.RegisterUser(t, "hs1", "alice", "password", false) + + // Create a first client, which allocates a device ID. + c1 := deployment.Client(t, "hs1", "") + c1.UserID, c1.AccessToken, c1.DeviceID = c1.LoginUser(t, "alice", "password") + + // Create a room where we can send events. + roomID := c1.CreateRoom(t, map[string]interface{}{}) + + // Let's send an event, and wait for it to appear in the timeline. + eventID := c1.SendEventUnsynced(t, roomID, b.Event{ + Type: "m.room.message", + Content: map[string]interface{}{ + "msgtype": "m.text", + "body": "first", + }, + }) + + // When syncing, we should find the event and it should have a transaction ID on the first client. + c1.MustSyncUntil(t, client.SyncReq{}, mustHaveTransactionID(t, roomID, eventID)) + + // Create a second client, inheriting the first device ID. + c2 := deployment.Client(t, "hs1", "") + c2.UserID, c2.AccessToken = c2.LoginUserWithDeviceID(t, "alice", "password", c1.DeviceID) + c2.DeviceID = c1.DeviceID + + // When syncing, we should find the event and it should *not* have a transaction ID on the second client. + c2.MustSyncUntil(t, client.SyncReq{}, mustNotHaveTransactionID(t, roomID, eventID)) +} + +// TestTxnIdempotencyScopedToClientSession tests that transaction IDs are scoped to a "client session" +// and behave as expected across multiple clients even if they use the same device ID +func TestTxnIdempotencyScopedToClientSession(t *testing.T) { + // Conduit scope transaction IDs to the device ID, not the client session. + runtime.SkipIf(t, runtime.Conduit) + + deployment := Deploy(t, b.BlueprintCleanHS) + defer deployment.Destroy(t) + + deployment.RegisterUser(t, "hs1", "alice", "password", false) + + // Create a first client, which allocates a device ID. + c1 := deployment.Client(t, "hs1", "") + c1.UserID, c1.AccessToken, c1.DeviceID = c1.LoginUser(t, "alice", "password") + + // Create a room where we can send events. + roomID := c1.CreateRoom(t, map[string]interface{}{}) + + txnId := 1 + event := b.Event{ + Type: "m.room.message", + Content: map[string]interface{}{ + "msgtype": "m.text", + "body": "foo", + }, + } + // send an event with set txnId + eventID1 := c1.SendEventUnsyncedWithTxnID(t, roomID, event, txnId) + + // Create a second client, inheriting the first device ID. + c2 := deployment.Client(t, "hs1", "") + c2.UserID, c2.AccessToken = c2.LoginUserWithDeviceID(t, "alice", "password", c1.DeviceID) + c2.DeviceID = c1.DeviceID + + // send another event with the same txnId + eventID2 := c2.SendEventUnsyncedWithTxnID(t, roomID, event, txnId) + + // the two events should have different event IDs as they came from different clients + if eventID1 == eventID2 { + t.Fatalf("Expected event IDs to be different from two clients sharing the same device ID") + } +} + +// TestTxnIdempotency tests that PUT requests idempotency follows required semantics +func TestTxnIdempotency(t *testing.T) { + deployment := Deploy(t, b.BlueprintCleanHS) + defer deployment.Destroy(t) + + deployment.RegisterUser(t, "hs1", "alice", "password", false) + + // Create a first client, which allocates a device ID. + c1 := deployment.Client(t, "hs1", "") + c1.UserID, c1.AccessToken, c1.DeviceID = c1.LoginUser(t, "alice", "password") + + // Create a room where we can send events. + roomID1 := c1.CreateRoom(t, map[string]interface{}{}) + roomID2 := c1.CreateRoom(t, map[string]interface{}{}) + + // choose a transaction ID + txnId := 1 + event1 := b.Event{ + Type: "m.room.message", + Content: map[string]interface{}{ + "msgtype": "m.text", + "body": "first", + }, + } + event2 := b.Event{ + Type: "m.room.message", + Content: map[string]interface{}{ + "msgtype": "m.text", + "body": "second", + }, + } + + // we send the event and get an event ID back + eventID1 := c1.SendEventUnsyncedWithTxnID(t, roomID1, event1, txnId) + + // we send the identical event again and should get back the same event ID + eventID2 := c1.SendEventUnsyncedWithTxnID(t, roomID1, event1, txnId) + + if eventID1 != eventID2 { + t.Fatalf("Expected event IDs to be the same, but they were not") + } + + // even if we change the content we should still get back the same event ID as transaction ID is the same + eventID3 := c1.SendEventUnsyncedWithTxnID(t, roomID1, event2, txnId) + + if eventID1 != eventID3 { + t.Fatalf("Expected event IDs to be the same even with different content, but they were not") + } + + // if we change the room ID we should be able to use the same transaction ID + eventID4 := c1.SendEventUnsyncedWithTxnID(t, roomID2, event1, txnId) + + if eventID4 == eventID3 { + t.Fatalf("Expected event IDs to be the different, but they were not") + } +} From b6e1929cf6a5a8a8d086976579c7d3bc9e18acfd Mon Sep 17 00:00:00 2001 From: Hugh Nimmo-Smith Date: Mon, 6 Mar 2023 16:52:44 +0000 Subject: [PATCH 2/6] Update internal/client/client.go Co-authored-by: kegsay --- internal/client/client.go | 1 + 1 file changed, 1 insertion(+) diff --git a/internal/client/client.go b/internal/client/client.go index edd38eaf..14bff134 100644 --- a/internal/client/client.go +++ b/internal/client/client.go @@ -264,6 +264,7 @@ func (c *CSAPI) SetPushRule(t *testing.T, scope string, kind string, ruleID stri // SendEventUnsynced sends `e` into the room. // Returns the event ID of the sent event. func (c *CSAPI) SendEventUnsynced(t *testing.T, roomID string, e b.Event) string { + t.Helper() txnID := int(atomic.AddInt64(&c.txnID, 1)) return c.SendEventUnsyncedWithTxnID(t, roomID, e, txnID) } From 4424fb05b429529a2b836ad44039cbb2b3fbd0d3 Mon Sep 17 00:00:00 2001 From: Hugh Nimmo-Smith Date: Thu, 9 Mar 2023 12:55:47 -0500 Subject: [PATCH 3/6] Incorporate review feedback --- internal/client/client.go | 46 +++++++++++-------------------- tests/csapi/txnid_test.go | 57 ++++++++++++++++++++++----------------- 2 files changed, 48 insertions(+), 55 deletions(-) diff --git a/internal/client/client.go b/internal/client/client.go index 14bff134..b857fe35 100644 --- a/internal/client/client.go +++ b/internal/client/client.go @@ -266,14 +266,14 @@ func (c *CSAPI) SetPushRule(t *testing.T, scope string, kind string, ruleID stri func (c *CSAPI) SendEventUnsynced(t *testing.T, roomID string, e b.Event) string { t.Helper() txnID := int(atomic.AddInt64(&c.txnID, 1)) - return c.SendEventUnsyncedWithTxnID(t, roomID, e, txnID) + return c.SendEventUnsyncedWithTxnID(t, roomID, e, strconv.Itoa(txnID)) } // SendEventUnsynced sends `e` into the room. // Returns the event ID of the sent event. -func (c *CSAPI) SendEventUnsyncedWithTxnID(t *testing.T, roomID string, e b.Event, txnID int) string { +func (c *CSAPI) SendEventUnsyncedWithTxnID(t *testing.T, roomID string, e b.Event, txnID string) string { t.Helper() - paths := []string{"_matrix", "client", "v3", "rooms", roomID, "send", e.Type, strconv.Itoa(txnID)} + paths := []string{"_matrix", "client", "v3", "rooms", roomID, "send", e.Type, txnID} if e.StateKey != nil { paths = []string{"_matrix", "client", "v3", "rooms", roomID, "state", e.Type, *e.StateKey} } @@ -421,8 +421,16 @@ func (c *CSAPI) MustSyncUntil(t *testing.T, syncReq SyncReq, checks ...SyncCheck } } +type LoginOpt func(map[string]interface{}) + +func WithDeviceID(deviceID string) LoginOpt { + return func(loginBody map[string]interface{}) { + loginBody["device_id"] = deviceID + } +} + // LoginUser will log in to a homeserver and create a new device on an existing user. -func (c *CSAPI) LoginUser(t *testing.T, localpart, password string) (userID, accessToken, deviceID string) { +func (c *CSAPI) LoginUser(t *testing.T, localpart, password string, opts ...LoginOpt) (userID, accessToken, deviceID string) { t.Helper() reqBody := map[string]interface{}{ "identifier": map[string]interface{}{ @@ -432,31 +440,11 @@ func (c *CSAPI) LoginUser(t *testing.T, localpart, password string) (userID, acc "password": password, "type": "m.login.password", } - res := c.MustDoFunc(t, "POST", []string{"_matrix", "client", "v3", "login"}, WithJSONBody(t, reqBody)) - body, err := ioutil.ReadAll(res.Body) - if err != nil { - t.Fatalf("unable to read response body: %v", err) + for _, opt := range opts { + opt(reqBody) } - userID = gjson.GetBytes(body, "user_id").Str - accessToken = gjson.GetBytes(body, "access_token").Str - deviceID = gjson.GetBytes(body, "device_id").Str - return userID, accessToken, deviceID -} - -// LoginUserWithDeviceID will log in to a homeserver on an existing device -func (c *CSAPI) LoginUserWithDeviceID(t *testing.T, localpart, password, deviceID string) (userID, accessToken string) { - t.Helper() - reqBody := map[string]interface{}{ - "identifier": map[string]interface{}{ - "type": "m.id.user", - "user": localpart, - }, - "device_id": deviceID, - "password": password, - "type": "m.login.password", - } res := c.MustDoFunc(t, "POST", []string{"_matrix", "client", "v3", "login"}, WithJSONBody(t, reqBody)) body, err := ioutil.ReadAll(res.Body) @@ -466,10 +454,8 @@ func (c *CSAPI) LoginUserWithDeviceID(t *testing.T, localpart, password, deviceI userID = gjson.GetBytes(body, "user_id").Str accessToken = gjson.GetBytes(body, "access_token").Str - if gjson.GetBytes(body, "device_id").Str != deviceID { - t.Fatalf("device_id returned by login does not match the one requested") - } - return userID, accessToken + deviceID = gjson.GetBytes(body, "device_id").Str + return userID, accessToken, deviceID } // RegisterUser will register the user with given parameters and diff --git a/tests/csapi/txnid_test.go b/tests/csapi/txnid_test.go index 3c9385aa..1b69b8f0 100644 --- a/tests/csapi/txnid_test.go +++ b/tests/csapi/txnid_test.go @@ -5,6 +5,7 @@ import ( "github.com/matrix-org/complement/internal/b" "github.com/matrix-org/complement/internal/client" + "github.com/matrix-org/complement/internal/must" "github.com/matrix-org/complement/runtime" "github.com/tidwall/gjson" ) @@ -23,30 +24,43 @@ func TestTxnInEvent(t *testing.T) { // Create a room where we can send events. roomID := c.CreateRoom(t, map[string]interface{}{}) + txnId := "abcdefg" // Let's send an event, and wait for it to appear in the timeline. - eventID := c.SendEventSynced(t, roomID, b.Event{ + eventID := c.SendEventUnsyncedWithTxnID(t, roomID, b.Event{ Type: "m.room.message", Content: map[string]interface{}{ "msgtype": "m.text", "body": "first", }, - }) + }, txnId) // The transaction ID should be present on the GET /rooms/{roomID}/event/{eventID} response. res := c.MustDoFunc(t, "GET", []string{"_matrix", "client", "v3", "rooms", roomID, "event", eventID}) body := client.ParseJSON(t, res) result := gjson.ParseBytes(body) if !result.Get("unsigned.transaction_id").Exists() { - t.Fatalf("Event did not have a 'transaction_id' on the GET /rooms/%s/event/%s response", roomID, eventID) + t.Fatalf("Event did not have a 'unsigned.transaction_id' on the GET /rooms/%s/event/%s response", roomID, eventID) + } + + txnIdFromResult:= result.Get("unsigned.transaction_id").Str + + if txnIdFromResult != txnId { + t.Fatalf("Event did not have a 'unsigned.transaction_id' of %s on GET /rooms/%s/event/%s response. Found %s instead", txnId, eventID, roomID, txnIdFromResult) } } -func mustHaveTransactionID(t *testing.T, roomID, eventID string) client.SyncCheckOpt { +func mustHaveTransactionID(t *testing.T, roomID, eventID, expectedTxnId string) client.SyncCheckOpt { return client.SyncTimelineHas(roomID, func(r gjson.Result) bool { if r.Get("event_id").Str == eventID { if !r.Get("unsigned.transaction_id").Exists() { - t.Fatalf("Event %s in room %s should have a 'transaction_id', but it did not", eventID, roomID) + t.Fatalf("Event %s in room %s should have a 'unsigned.transaction_id', but it did not", eventID, roomID) + } + + txnIdFromSync := r.Get("unsigned.transaction_id").Str + + if txnIdFromSync != expectedTxnId { + t.Fatalf("Event %s in room %s should have a 'unsigned.transaction_id' of %s but found %s", eventID, roomID, expectedTxnId, txnIdFromSync) } return true @@ -61,7 +75,7 @@ func mustNotHaveTransactionID(t *testing.T, roomID, eventID string) client.SyncC if r.Get("event_id").Str == eventID { res := r.Get("unsigned.transaction_id") if res.Exists() { - t.Fatalf("Event %s in room %s should NOT have a 'transaction_id', but it did (%s)", eventID, roomID, res.Str) + t.Fatalf("Event %s in room %s should NOT have a 'unsigned.transaction_id', but it did (%s)", eventID, roomID, res.Str) } return true @@ -89,21 +103,22 @@ func TestTxnScopeOnLocalEcho(t *testing.T) { // Create a room where we can send events. roomID := c1.CreateRoom(t, map[string]interface{}{}) + txnId := "abdefgh" // Let's send an event, and wait for it to appear in the timeline. - eventID := c1.SendEventUnsynced(t, roomID, b.Event{ + eventID := c1.SendEventUnsyncedWithTxnID(t, roomID, b.Event{ Type: "m.room.message", Content: map[string]interface{}{ "msgtype": "m.text", "body": "first", }, - }) + }, txnId) // When syncing, we should find the event and it should have a transaction ID on the first client. - c1.MustSyncUntil(t, client.SyncReq{}, mustHaveTransactionID(t, roomID, eventID)) + c1.MustSyncUntil(t, client.SyncReq{}, mustHaveTransactionID(t, roomID, eventID, txnId)) // Create a second client, inheriting the first device ID. c2 := deployment.Client(t, "hs1", "") - c2.UserID, c2.AccessToken = c2.LoginUserWithDeviceID(t, "alice", "password", c1.DeviceID) + c2.UserID, c2.AccessToken, _ = c2.LoginUser(t, "alice", "password", client.WithDeviceID(c1.DeviceID)) c2.DeviceID = c1.DeviceID // When syncing, we should find the event and it should *not* have a transaction ID on the second client. @@ -128,7 +143,7 @@ func TestTxnIdempotencyScopedToClientSession(t *testing.T) { // Create a room where we can send events. roomID := c1.CreateRoom(t, map[string]interface{}{}) - txnId := 1 + txnId := "abcdef" event := b.Event{ Type: "m.room.message", Content: map[string]interface{}{ @@ -141,16 +156,14 @@ func TestTxnIdempotencyScopedToClientSession(t *testing.T) { // Create a second client, inheriting the first device ID. c2 := deployment.Client(t, "hs1", "") - c2.UserID, c2.AccessToken = c2.LoginUserWithDeviceID(t, "alice", "password", c1.DeviceID) + c2.UserID, c2.AccessToken, _ = c2.LoginUser(t, "alice", "password", client.WithDeviceID(c1.DeviceID)) c2.DeviceID = c1.DeviceID // send another event with the same txnId eventID2 := c2.SendEventUnsyncedWithTxnID(t, roomID, event, txnId) // the two events should have different event IDs as they came from different clients - if eventID1 == eventID2 { - t.Fatalf("Expected event IDs to be different from two clients sharing the same device ID") - } + must.NotEqualStr(t, eventID2, eventID1, "Expected eventID1 and eventID2 to be different from two clients sharing the same device ID") } // TestTxnIdempotency tests that PUT requests idempotency follows required semantics @@ -169,7 +182,7 @@ func TestTxnIdempotency(t *testing.T) { roomID2 := c1.CreateRoom(t, map[string]interface{}{}) // choose a transaction ID - txnId := 1 + txnId := "abc" event1 := b.Event{ Type: "m.room.message", Content: map[string]interface{}{ @@ -191,21 +204,15 @@ func TestTxnIdempotency(t *testing.T) { // we send the identical event again and should get back the same event ID eventID2 := c1.SendEventUnsyncedWithTxnID(t, roomID1, event1, txnId) - if eventID1 != eventID2 { - t.Fatalf("Expected event IDs to be the same, but they were not") - } + must.EqualStr(t, eventID2, eventID1, "Expected eventID1 and eventID2 to be the same, but they were not") // even if we change the content we should still get back the same event ID as transaction ID is the same eventID3 := c1.SendEventUnsyncedWithTxnID(t, roomID1, event2, txnId) - if eventID1 != eventID3 { - t.Fatalf("Expected event IDs to be the same even with different content, but they were not") - } + must.EqualStr(t, eventID3, eventID1, "Expected eventID3 and eventID2 to be the same even with different content, but they were not") // if we change the room ID we should be able to use the same transaction ID eventID4 := c1.SendEventUnsyncedWithTxnID(t, roomID2, event1, txnId) - if eventID4 == eventID3 { - t.Fatalf("Expected event IDs to be the different, but they were not") - } + must.NotEqualStr(t, eventID4, eventID3, "Expected eventID4 and eventID3 to be different, but they were not") } From 67f7d146a4e17bde1500b791c2f6f17b49e5a0ef Mon Sep 17 00:00:00 2001 From: Hugh Nimmo-Smith Date: Thu, 9 Mar 2023 13:21:26 -0500 Subject: [PATCH 4/6] Refactor for legibility --- tests/csapi/txnid_test.go | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/tests/csapi/txnid_test.go b/tests/csapi/txnid_test.go index 1b69b8f0..bacf9104 100644 --- a/tests/csapi/txnid_test.go +++ b/tests/csapi/txnid_test.go @@ -1,6 +1,7 @@ package csapi_tests import ( + "fmt" "testing" "github.com/matrix-org/complement/internal/b" @@ -38,30 +39,24 @@ func TestTxnInEvent(t *testing.T) { res := c.MustDoFunc(t, "GET", []string{"_matrix", "client", "v3", "rooms", roomID, "event", eventID}) body := client.ParseJSON(t, res) result := gjson.ParseBytes(body) - if !result.Get("unsigned.transaction_id").Exists() { + unsignedTxnId := result.Get("unsigned.transaction_id") + if !unsignedTxnId.Exists() { t.Fatalf("Event did not have a 'unsigned.transaction_id' on the GET /rooms/%s/event/%s response", roomID, eventID) } - txnIdFromResult:= result.Get("unsigned.transaction_id").Str - - if txnIdFromResult != txnId { - t.Fatalf("Event did not have a 'unsigned.transaction_id' of %s on GET /rooms/%s/event/%s response. Found %s instead", txnId, eventID, roomID, txnIdFromResult) - } + must.EqualStr(t, unsignedTxnId.Str, txnId, fmt.Sprintf("Event did not have a 'unsigned.transaction_id' on GET /rooms/%s/event/%s response", eventID, roomID)) } func mustHaveTransactionID(t *testing.T, roomID, eventID, expectedTxnId string) client.SyncCheckOpt { return client.SyncTimelineHas(roomID, func(r gjson.Result) bool { if r.Get("event_id").Str == eventID { - if !r.Get("unsigned.transaction_id").Exists() { + unsignedTxnId := r.Get("unsigned.transaction_id") + if !unsignedTxnId.Exists() { t.Fatalf("Event %s in room %s should have a 'unsigned.transaction_id', but it did not", eventID, roomID) } - txnIdFromSync := r.Get("unsigned.transaction_id").Str - - if txnIdFromSync != expectedTxnId { - t.Fatalf("Event %s in room %s should have a 'unsigned.transaction_id' of %s but found %s", eventID, roomID, expectedTxnId, txnIdFromSync) - } + must.EqualStr(t, unsignedTxnId.Str, expectedTxnId, fmt.Sprintf("Event %s in room %s should have a 'unsigned.transaction_id'", eventID, roomID)) return true } @@ -73,9 +68,9 @@ func mustHaveTransactionID(t *testing.T, roomID, eventID, expectedTxnId string) func mustNotHaveTransactionID(t *testing.T, roomID, eventID string) client.SyncCheckOpt { return client.SyncTimelineHas(roomID, func(r gjson.Result) bool { if r.Get("event_id").Str == eventID { - res := r.Get("unsigned.transaction_id") - if res.Exists() { - t.Fatalf("Event %s in room %s should NOT have a 'unsigned.transaction_id', but it did (%s)", eventID, roomID, res.Str) + unsignedTxnId := r.Get("unsigned.transaction_id") + if unsignedTxnId.Exists() { + t.Fatalf("Event %s in room %s should NOT have a 'unsigned.transaction_id', but it did (%s)", eventID, roomID, unsignedTxnId.Str) } return true From 17e514ba68072071076b95c2d21c97fe4b52e301 Mon Sep 17 00:00:00 2001 From: Hugh Nimmo-Smith Date: Thu, 9 Mar 2023 13:49:50 -0500 Subject: [PATCH 5/6] Clarify that conduit doesn't pass idempotency tests --- tests/csapi/txnid_test.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/csapi/txnid_test.go b/tests/csapi/txnid_test.go index bacf9104..1be5e3c3 100644 --- a/tests/csapi/txnid_test.go +++ b/tests/csapi/txnid_test.go @@ -163,6 +163,9 @@ func TestTxnIdempotencyScopedToClientSession(t *testing.T) { // TestTxnIdempotency tests that PUT requests idempotency follows required semantics func TestTxnIdempotency(t *testing.T) { + // Conduit appears to be tracking transaction IDs individually rather than combined with the request URI/room ID + runtime.SkipIf(t, runtime.Conduit) + deployment := Deploy(t, b.BlueprintCleanHS) defer deployment.Destroy(t) From cfad90984bceabbac389b3fafe3db7b4c9ca4f0d Mon Sep 17 00:00:00 2001 From: Quentin Gliech Date: Fri, 24 Mar 2023 16:38:26 +0100 Subject: [PATCH 6/6] Tests for MSC3970 --- tests/msc3970_test.go | 109 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 tests/msc3970_test.go diff --git a/tests/msc3970_test.go b/tests/msc3970_test.go new file mode 100644 index 00000000..548ac1d6 --- /dev/null +++ b/tests/msc3970_test.go @@ -0,0 +1,109 @@ +//go:build msc3970 +// +build msc3970 + +// This files contains tests for MSC3970, which changes the scope +// of transaction IDs to be per-device instead of per-access token. +// https://github.com/matrix-org/matrix-spec-proposals/pull/3970 + +package tests + +import ( + "fmt" + "testing" + + "github.com/matrix-org/complement/internal/b" + "github.com/matrix-org/complement/internal/client" + "github.com/matrix-org/complement/internal/must" + "github.com/tidwall/gjson" +) + +func mustHaveTransactionID(t *testing.T, roomID, eventID, expectedTxnId string) client.SyncCheckOpt { + return client.SyncTimelineHas(roomID, func(r gjson.Result) bool { + if r.Get("event_id").Str == eventID { + unsignedTxnId := r.Get("unsigned.transaction_id") + if !unsignedTxnId.Exists() { + t.Fatalf("Event %s in room %s should have a 'unsigned.transaction_id', but it did not", eventID, roomID) + } + + must.EqualStr(t, unsignedTxnId.Str, expectedTxnId, fmt.Sprintf("Event %s in room %s should have a 'unsigned.transaction_id'", eventID, roomID)) + + return true + } + + return false + }) +} + +// TestTxnScopeOnLocalEchoMSC3970 checks that the transaction IDs are scoped to the device, +// and not just the access token, as per MSC3970. +func TestTxnScopeOnLocalEchoMSC3970(t *testing.T) { + deployment := Deploy(t, b.BlueprintCleanHS) + defer deployment.Destroy(t) + + deployment.RegisterUser(t, "hs1", "alice", "password", false) + + // Create a first client, which allocates a device ID. + c1 := deployment.Client(t, "hs1", "") + c1.UserID, c1.AccessToken, c1.DeviceID = c1.LoginUser(t, "alice", "password") + + // Create a room where we can send events. + roomID := c1.CreateRoom(t, map[string]interface{}{}) + + txnId := "abdefgh" + // Let's send an event, and wait for it to appear in the timeline. + eventID := c1.SendEventUnsyncedWithTxnID(t, roomID, b.Event{ + Type: "m.room.message", + Content: map[string]interface{}{ + "msgtype": "m.text", + "body": "first", + }, + }, txnId) + + // When syncing, we should find the event and it should have a transaction ID on the first client. + c1.MustSyncUntil(t, client.SyncReq{}, mustHaveTransactionID(t, roomID, eventID, txnId)) + + // Create a second client, inheriting the first device ID. + c2 := deployment.Client(t, "hs1", "") + c2.UserID, c2.AccessToken, _ = c2.LoginUser(t, "alice", "password", client.WithDeviceID(c1.DeviceID)) + c2.DeviceID = c1.DeviceID + + // When syncing, we should find the event and it should *not* have a transaction ID on the second client. + c2.MustSyncUntil(t, client.SyncReq{}, mustHaveTransactionID(t, roomID, eventID, txnId)) +} + +// TestTxnIdempotencyScopedToClientDeviceMSC3970 tests that transaction IDs are scoped to a device +func TestTxnIdempotencyScopedToDeviceMSC3970(t *testing.T) { + deployment := Deploy(t, b.BlueprintCleanHS) + defer deployment.Destroy(t) + + deployment.RegisterUser(t, "hs1", "alice", "password", false) + + // Create a first client, which allocates a device ID. + c1 := deployment.Client(t, "hs1", "") + c1.UserID, c1.AccessToken, c1.DeviceID = c1.LoginUser(t, "alice", "password") + + // Create a room where we can send events. + roomID := c1.CreateRoom(t, map[string]interface{}{}) + + txnId := "abcdef" + event := b.Event{ + Type: "m.room.message", + Content: map[string]interface{}{ + "msgtype": "m.text", + "body": "foo", + }, + } + // send an event with set txnId + eventID1 := c1.SendEventUnsyncedWithTxnID(t, roomID, event, txnId) + + // Create a second client, inheriting the first device ID. + c2 := deployment.Client(t, "hs1", "") + c2.UserID, c2.AccessToken, _ = c2.LoginUser(t, "alice", "password", client.WithDeviceID(c1.DeviceID)) + c2.DeviceID = c1.DeviceID + + // send another event with the same txnId + eventID2 := c2.SendEventUnsyncedWithTxnID(t, roomID, event, txnId) + + // the two events should have different event IDs as they came from different clients + must.EqualStr(t, eventID2, eventID1, "Expected eventID1 and eventID2 to be equal from two clients sharing the same device ID") +}