Skip to content

Commit

Permalink
Add an argument to tc.CreateNewEncryptedRoom for the content of the m…
Browse files Browse the repository at this point in the history
….room.encryption event
  • Loading branch information
andybalaam committed Mar 4, 2024
1 parent 1105bfc commit 4799711
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 24 deletions.
2 changes: 1 addition & 1 deletion tests/device_keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestFailedDeviceKeyDownloadRetries(t *testing.T) {
},
}, func() {
// And Alice and Bob are in an encrypted room together
roomID := tc.CreateNewEncryptedRoom(t, tc.Alice, "private_chat", []string{tc.Bob.UserID})
roomID := tc.CreateNewEncryptedRoom(t, tc.Alice, "private_chat", []string{tc.Bob.UserID}, nil)
tc.Bob.MustJoinRoom(t, roomID, []string{"hs1"})

tc.WithAliceAndBobSyncing(t, func(alice, bob api.Client) {
Expand Down
6 changes: 3 additions & 3 deletions tests/federation_connectivity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestNewUserCannotGetKeysForOfflineServer(t *testing.T) {
Lang: api.ClientTypeRust,
HS: "hs1",
})
roomID := tc.CreateNewEncryptedRoom(t, tc.Alice, "private_chat", []string{tc.Bob.UserID})
roomID := tc.CreateNewEncryptedRoom(t, tc.Alice, "private_chat", []string{tc.Bob.UserID}, nil)
tc.Bob.MustJoinRoom(t, roomID, []string{"hs1"})

tc.WithAliceAndBobSyncing(t, func(alice, bob api.Client) {
Expand Down Expand Up @@ -107,8 +107,8 @@ func TestExistingSessionCannotGetKeysForOfflineServer(t *testing.T) {
Lang: api.ClientTypeRust,
HS: "hs1",
})
roomIDbc := tc.CreateNewEncryptedRoom(t, tc.Charlie, "private_chat", []string{tc.Bob.UserID})
roomIDab := tc.CreateNewEncryptedRoom(t, tc.Alice, "private_chat", []string{tc.Bob.UserID})
roomIDbc := tc.CreateNewEncryptedRoom(t, tc.Charlie, "private_chat", []string{tc.Bob.UserID}, nil)
roomIDab := tc.CreateNewEncryptedRoom(t, tc.Alice, "private_chat", []string{tc.Bob.UserID}, nil)
tc.Bob.MustJoinRoom(t, roomIDab, []string{"hs1"})
tc.Bob.MustJoinRoom(t, roomIDbc, []string{"hs1"})

Expand Down
22 changes: 18 additions & 4 deletions tests/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,11 +239,27 @@ func (c *TestContext) WithAliceBobAndCharlieSyncing(t *testing.T, callback func(
}

// CreateNewEncryptedRoom calls creator.MustCreateRoom with the correct m.room.encryption state event.
func (c *TestContext) CreateNewEncryptedRoom(t *testing.T, creator *client.CSAPI, preset string, invite []string) (roomID string) {

// m_room_encryptionContent specifies the content of the `m.room.encryption`
// event. It may be nil, in which case the standard algorithm
// ("m.megolm.v1.aes-sha2") is used, and the rotation periods are omitted,
// meaning the defaults will be used.
func (c *TestContext) CreateNewEncryptedRoom(
t *testing.T,
creator *client.CSAPI,
preset string,
invite []string,
m_room_encryptionContent map[string]interface{},
) (roomID string) {
t.Helper()
if invite == nil {
invite = []string{} // else synapse 500s
}
if m_room_encryptionContent == nil {
m_room_encryptionContent = map[string]interface{}{
"algorithm": "m.megolm.v1.aes-sha2",
}
}
return creator.MustCreateRoom(t, map[string]interface{}{
"name": t.Name(),
"preset": preset,
Expand All @@ -252,9 +268,7 @@ func (c *TestContext) CreateNewEncryptedRoom(t *testing.T, creator *client.CSAPI
{
"type": "m.room.encryption",
"state_key": "",
"content": map[string]interface{}{
"algorithm": "m.megolm.v1.aes-sha2",
},
"content": m_room_encryptionContent,
},
},
})
Expand Down
12 changes: 6 additions & 6 deletions tests/membership_acls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestAliceBobEncryptionWorks(t *testing.T) {
ClientTypeMatrix(t, func(t *testing.T, clientTypeA, clientTypeB api.ClientType) {
tc := CreateTestContext(t, clientTypeA, clientTypeB)
// Alice invites Bob to the encrypted room
roomID := tc.CreateNewEncryptedRoom(t, tc.Alice, "trusted_private_chat", []string{tc.Bob.UserID})
roomID := tc.CreateNewEncryptedRoom(t, tc.Alice, "trusted_private_chat", []string{tc.Bob.UserID}, nil)
tc.Bob.MustJoinRoom(t, roomID, []string{clientTypeA.HS})

// SDK testing below
Expand Down Expand Up @@ -60,7 +60,7 @@ func TestCanDecryptMessagesAfterInviteButBeforeJoin(t *testing.T) {
ClientTypeMatrix(t, func(t *testing.T, clientTypeA, clientTypeB api.ClientType) {
tc := CreateTestContext(t, clientTypeA, clientTypeB)
// Alice invites Bob to the encrypted room
roomID := tc.CreateNewEncryptedRoom(t, tc.Alice, "trusted_private_chat", []string{tc.Bob.UserID})
roomID := tc.CreateNewEncryptedRoom(t, tc.Alice, "trusted_private_chat", []string{tc.Bob.UserID}, nil)

// SDK testing below
// -----------------
Expand Down Expand Up @@ -123,7 +123,7 @@ func TestBobCanSeeButNotDecryptHistoryInPublicRoom(t *testing.T) {
ClientTypeMatrix(t, func(t *testing.T, clientTypeA, clientTypeB api.ClientType) {
tc := CreateTestContext(t, clientTypeA, clientTypeB)
// shared history visibility
roomID := tc.CreateNewEncryptedRoom(t, tc.Alice, "public_chat", nil)
roomID := tc.CreateNewEncryptedRoom(t, tc.Alice, "public_chat", nil, nil)

// SDK testing below
// -----------------
Expand Down Expand Up @@ -157,7 +157,7 @@ func TestOnRejoinBobCanSeeButNotDecryptHistoryInPublicRoom(t *testing.T) {
ClientTypeMatrix(t, func(t *testing.T, clientTypeA, clientTypeB api.ClientType) {
tc := CreateTestContext(t, clientTypeA, clientTypeB)
// shared history visibility
roomID := tc.CreateNewEncryptedRoom(t, tc.Alice, "public_chat", nil)
roomID := tc.CreateNewEncryptedRoom(t, tc.Alice, "public_chat", nil, nil)
tc.Bob.MustJoinRoom(t, roomID, []string{clientTypeA.HS})

// SDK testing below
Expand Down Expand Up @@ -228,7 +228,7 @@ func TestOnNewDeviceBobCanSeeButNotDecryptHistoryInPublicRoom(t *testing.T) {
ClientTypeMatrix(t, func(t *testing.T, clientTypeA, clientTypeB api.ClientType) {
tc := CreateTestContext(t, clientTypeA, clientTypeB)
// shared history visibility
roomID := tc.CreateNewEncryptedRoom(t, tc.Alice, "public_chat", nil)
roomID := tc.CreateNewEncryptedRoom(t, tc.Alice, "public_chat", nil, nil)
tc.Bob.MustJoinRoom(t, roomID, []string{clientTypeA.HS})

// SDK testing below
Expand Down Expand Up @@ -314,7 +314,7 @@ func TestChangingDeviceAfterInviteReEncrypts(t *testing.T) {
ClientTypeMatrix(t, func(t *testing.T, clientTypeA, clientTypeB api.ClientType) {
tc := CreateTestContext(t, clientTypeA, clientTypeB)
// shared history visibility
roomID := tc.CreateNewEncryptedRoom(t, tc.Alice, "public_chat", nil)
roomID := tc.CreateNewEncryptedRoom(t, tc.Alice, "public_chat", nil, nil)

tc.WithAliceAndBobSyncing(t, func(alice, bob api.Client) {
// Alice invites Bob and then she sends an event
Expand Down
4 changes: 2 additions & 2 deletions tests/one_time_keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func TestFallbackKeyIsUsedIfOneTimeKeysRunOut(t *testing.T) {
fallbackKeyID, fallbackKey = mustClaimFallbackKey(t, otkGobbler, tc.Alice)

// now bob & charlie try to talk to alice, the fallback key should be used
roomID = tc.CreateNewEncryptedRoom(t, tc.Bob, "public_chat", []string{tc.Alice.UserID, tc.Charlie.UserID})
roomID = tc.CreateNewEncryptedRoom(t, tc.Bob, "public_chat", []string{tc.Alice.UserID, tc.Charlie.UserID}, nil)
tc.Charlie.MustJoinRoom(t, roomID, []string{keyConsumerClientType.HS})
tc.Alice.MustJoinRoom(t, roomID, []string{keyConsumerClientType.HS})
charlie.WaitUntilEventInRoom(t, roomID, api.CheckEventHasMembership(alice.UserID(), "join")).Wait(t, 5*time.Second)
Expand Down Expand Up @@ -252,7 +252,7 @@ func TestFailedKeysClaimRetries(t *testing.T) {
defer close()

// make a room which will link the 2 users together when
roomID := tc.CreateNewEncryptedRoom(t, tc.Alice, "public_chat", nil)
roomID := tc.CreateNewEncryptedRoom(t, tc.Alice, "public_chat", nil, nil)
// block /keys/claim and join the room, causing the Olm session to be created
tc.Deployment.WithMITMOptions(t, map[string]interface{}{
"statuscode": map[string]interface{}{
Expand Down
12 changes: 6 additions & 6 deletions tests/room_keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ func sniffToDeviceEvent(t *testing.T, d complement.Deployment, ch chan deploy.Ca
return callbackURL, close
}

// This test ensure we change the m.room_key when a device leaves an E2EE room.
// This test ensures we change the m.room_key when a device leaves an E2EE room.
// If the key is not changed, the left device could potentially decrypt the encrypted
// event if they could get access to it.
func TestRoomKeyIsCycledOnDeviceLogout(t *testing.T) {
ClientTypeMatrix(t, func(t *testing.T, clientTypeA, clientTypeB api.ClientType) {
tc := CreateTestContext(t, clientTypeA, clientTypeB)
roomID := tc.CreateNewEncryptedRoom(t, tc.Alice, "trusted_private_chat", []string{tc.Bob.UserID})
roomID := tc.CreateNewEncryptedRoom(t, tc.Alice, "trusted_private_chat", []string{tc.Bob.UserID}, nil)
tc.Bob.MustJoinRoom(t, roomID, []string{clientTypeA.HS})

// Alice, Alice2 and Bob are in a room.
Expand Down Expand Up @@ -94,7 +94,7 @@ func TestRoomKeyIsCycledOnDeviceLogout(t *testing.T) {
func TestRoomKeyIsCycledOnMemberLeaving(t *testing.T) {
ClientTypeMatrix(t, func(t *testing.T, clientTypeA, clientTypeB api.ClientType) {
tc := CreateTestContext(t, clientTypeA, clientTypeB, clientTypeB)
roomID := tc.CreateNewEncryptedRoom(t, tc.Alice, "trusted_private_chat", []string{tc.Bob.UserID, tc.Charlie.UserID})
roomID := tc.CreateNewEncryptedRoom(t, tc.Alice, "trusted_private_chat", []string{tc.Bob.UserID, tc.Charlie.UserID}, nil)
tc.Bob.MustJoinRoom(t, roomID, []string{clientTypeA.HS})
tc.Charlie.MustJoinRoom(t, roomID, []string{clientTypeA.HS})
// Alice, Bob and Charlie are in a room.
Expand Down Expand Up @@ -149,7 +149,7 @@ func TestRoomKeyIsCycledOnMemberLeaving(t *testing.T) {
func TestRoomKeyIsNotCycled(t *testing.T) {
ClientTypeMatrix(t, func(t *testing.T, clientTypeA, clientTypeB api.ClientType) {
tc := CreateTestContext(t, clientTypeA, clientTypeB)
roomID := tc.CreateNewEncryptedRoom(t, tc.Alice, "trusted_private_chat", []string{tc.Bob.UserID})
roomID := tc.CreateNewEncryptedRoom(t, tc.Alice, "trusted_private_chat", []string{tc.Bob.UserID}, nil)
tc.Bob.MustJoinRoom(t, roomID, []string{clientTypeA.HS})

// Alice, Bob are in a room.
Expand Down Expand Up @@ -288,7 +288,7 @@ func TestRoomKeyIsNotCycledOnClientRestart(t *testing.T) {

func testRoomKeyIsNotCycledOnClientRestartRust(t *testing.T, clientType api.ClientType) {
tc := CreateTestContext(t, clientType, clientType)
roomID := tc.CreateNewEncryptedRoom(t, tc.Alice, "trusted_private_chat", []string{tc.Bob.UserID})
roomID := tc.CreateNewEncryptedRoom(t, tc.Alice, "trusted_private_chat", []string{tc.Bob.UserID}, nil)
tc.Bob.MustJoinRoom(t, roomID, []string{clientType.HS})

tc.WithClientSyncing(t, clientType, tc.Bob, func(bob api.Client) {
Expand Down Expand Up @@ -368,7 +368,7 @@ func testRoomKeyIsNotCycledOnClientRestartRust(t *testing.T, clientType api.Clie

func testRoomKeyIsNotCycledOnClientRestartJS(t *testing.T, clientType api.ClientType) {
tc := CreateTestContext(t, clientType, clientType)
roomID := tc.CreateNewEncryptedRoom(t, tc.Alice, "trusted_private_chat", []string{tc.Bob.UserID})
roomID := tc.CreateNewEncryptedRoom(t, tc.Alice, "trusted_private_chat", []string{tc.Bob.UserID}, nil)
tc.Bob.MustJoinRoom(t, roomID, []string{clientType.HS})

// Alice and Bob are in a room.
Expand Down
4 changes: 2 additions & 2 deletions tests/to_device_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
func TestClientRetriesSendToDevice(t *testing.T) {
ClientTypeMatrix(t, func(t *testing.T, clientTypeA, clientTypeB api.ClientType) {
tc := CreateTestContext(t, clientTypeA, clientTypeB)
roomID := tc.CreateNewEncryptedRoom(t, tc.Alice, "public_chat", nil)
roomID := tc.CreateNewEncryptedRoom(t, tc.Alice, "public_chat", nil, nil)
tc.Bob.MustJoinRoom(t, roomID, []string{clientTypeA.HS})
tc.WithAliceAndBobSyncing(t, func(alice, bob api.Client) {
// lets device keys be exchanged
Expand Down Expand Up @@ -73,7 +73,7 @@ func TestUnprocessedToDeviceMessagesArentLostOnRestart(t *testing.T) {
ForEachClientType(t, func(t *testing.T, clientType api.ClientType) {
// prepare for the test: register all 3 clients and create the room
tc := CreateTestContext(t, clientType, clientType)
roomID := tc.CreateNewEncryptedRoom(t, tc.Alice, "private_chat", []string{tc.Bob.UserID})
roomID := tc.CreateNewEncryptedRoom(t, tc.Alice, "private_chat", []string{tc.Bob.UserID}, nil)
tc.Bob.MustJoinRoom(t, roomID, []string{clientType.HS})
alice2 := tc.Deployment.Login(t, clientType.HS, tc.Alice, helpers.LoginOpts{
DeviceID: "ALICE_TWO",
Expand Down

0 comments on commit 4799711

Please sign in to comment.