Skip to content

Commit

Permalink
WIP TestBobCanSeeButNotDecryptHistoryInPublicRoom
Browse files Browse the repository at this point in the history
  • Loading branch information
kegsay committed Nov 14, 2023
1 parent dc8aede commit 8225472
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 45 deletions.
2 changes: 2 additions & 0 deletions internal/api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ type Client interface {
WaitUntilEventInRoom(t *testing.T, roomID string, checker func(e Event) bool) Waiter
// Backpaginate in this room by `count` events.
MustBackpaginate(t *testing.T, roomID string, count int)
// MustGetEvent will return the client's view of this event, or fail the test if the event cannot be found.
MustGetEvent(t *testing.T, roomID, eventID string) Event
// Log something to stdout and the underlying client log file
Logf(t *testing.T, format string, args ...interface{})
// The user for this client
Expand Down
4 changes: 4 additions & 0 deletions internal/api/js.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,10 @@ func (c *JSClient) UserID() string {
return c.userID
}

func (c *JSClient) MustGetEvent(t *testing.T, roomID, eventID string) Event {
return Event{}
}

// StartSyncing to begin syncing from sync v2 / sliding sync.
// Tests should call stopSyncing() at the end of the test.
func (c *JSClient) StartSyncing(t *testing.T) (stopSyncing func()) {
Expand Down
6 changes: 5 additions & 1 deletion internal/api/rust.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ func (c *RustClient) Close(t *testing.T) {
c.FFIClient.Destroy()
}

func (c *RustClient) MustGetEvent(t *testing.T, roomID, eventID string) Event {
return Event{}
}

// StartSyncing to begin syncing from sync v2 / sliding sync.
// Tests should call stopSyncing() at the end of the test.
func (c *RustClient) StartSyncing(t *testing.T) (stopSyncing func()) {
Expand Down Expand Up @@ -214,7 +218,7 @@ func (c *RustClient) ensureListening(t *testing.T, roomID string) *matrix_sdk_ff
return r
}

t.Logf("[%s]AddTimelineListenerBlocking[%s]", c.userID, roomID)
t.Logf("[%s]AddTimelineListener[%s]", c.userID, roomID)
// we need a timeline listener before we can send messages
r.AddTimelineListener(&timelineListener{fn: func(diff []*matrix_sdk_ffi.TimelineDiff) {
timeline := c.rooms[roomID].timeline
Expand Down
63 changes: 19 additions & 44 deletions tests/membership_acls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,6 @@ func TestCanDecryptMessagesAfterInviteButBeforeJoin(t *testing.T) {
})
}

/*
// In a public, `shared` history visibility room, a new user Bob cannot decrypt earlier messages prior to his join,
// despite being able to see the events. Subsequent messages are decryptable.
func TestBobCanSeeButNotDecryptHistoryInPublicRoom(t *testing.T) {
Expand All @@ -198,7 +197,7 @@ func TestBobCanSeeButNotDecryptHistoryInPublicRoom(t *testing.T) {
})
roomID := csapiAlice.MustCreateRoom(t, map[string]interface{}{
"name": "TestBobCanSeeButNotDecryptHistoryInPublicRoom",
"preset": "public_chat",
"preset": "public_chat", // shared history visibility
"initial_state": []map[string]interface{}{
{
"type": "m.room.encryption",
Expand All @@ -209,12 +208,12 @@ func TestBobCanSeeButNotDecryptHistoryInPublicRoom(t *testing.T) {
},
},
})
// TODO csapiBob.MustJoinRoom(t, roomID, []string{"hs1"})
ss := deployment.SlidingSyncURL(t)

// SDK testing below
// -----------------
// Alice and Bob are present with keys uploaded etc

// login both clients first, so OTKs etc are uploaded.
alice := MustLoginClient(t, clientTypeA, api.FromComplementClient(csapiAlice, "complement-crypto-password"), ss)
defer alice.Close(t)
bob := MustLoginClient(t, clientTypeB, api.FromComplementClient(csapiBob, "complement-crypto-password"), ss)
Expand All @@ -225,47 +224,23 @@ func TestBobCanSeeButNotDecryptHistoryInPublicRoom(t *testing.T) {
defer aliceStopSyncing()
bobStopSyncing := bob.StartSyncing(t)
defer bobStopSyncing()
time.Sleep(time.Second) // TODO: find another way to wait until initial sync is done
undecryptableBody := "Bob cannot decrypt this"
// Check the room is in fact encrypted
isEncrypted, err := alice.IsRoomEncrypted(t, roomID)
must.NotError(t, "failed to check if room is encrypted", err)
must.Equal(t, isEncrypted, true, "room is not encrypted when it should be")
// Alice sends a message to herself in this public room
aliceWaiter := alice.WaitUntilEventInRoom(t, roomID, undecryptableBody)
alice.SendMessage(t, roomID, undecryptableBody)
t.Logf("alice (%s) waiting for event", alice.Type())
aliceWaiter.Wait(t, 5*time.Second)
// Bob joins the room
csapiBob.JoinRoom(t, roomID, []string{"hs1"})
time.Sleep(time.Second) // TODO alice waits until she sees bob's join
// Alice sends a new message which Bob should be able to decrypt
decryptableBody := "Bob can decrypt this"
aliceWaiter = alice.WaitUntilEventInRoom(t, roomID, decryptableBody)
// Rust SDK listener doesn't seem to always catch this unless we are listening before the message is sent
bobWaiter := bob.WaitUntilEventInRoom(t, roomID, decryptableBody)
alice.SendMessage(t, roomID, decryptableBody)
t.Logf("alice (%s) waiting for event", alice.Type())
aliceWaiter.Wait(t, 5*time.Second)

isEncrypted, err = bob.IsRoomEncrypted(t, roomID)
must.NotError(t, "failed to check if room is encrypted", err)
must.Equal(t, isEncrypted, true, "room is not encrypted")
t.Logf("bob room encrypted = %v", isEncrypted)
// Bob receives the decryptable message
t.Logf("bob (%s) waiting for event", bob.Type())
bobWaiter.Wait(t, 5*time.Second)
// Bob attempts to backpaginate to see the older message
bob.MustBackpaginate(t, roomID, 5) // arbitrary, must be >2
// Alice sends a message which Bob should not be able to decrypt
beforeJoinBody := "Before Bob joins"
waiter := alice.WaitUntilEventInRoom(t, roomID, api.CheckEventHasBody(beforeJoinBody))
evID := alice.SendMessage(t, roomID, beforeJoinBody)
t.Logf("alice (%s) waiting for event %s", alice.Type(), evID)
waiter.Wait(t, 5*time.Second)

// TODO Ensure Bob cannot see the undecrypted content, find the event by event ID to confirm
// now bob joins the room
csapiBob.MustJoinRoom(t, roomID, []string{"hs1"})
time.Sleep(time.Second) // wait for it to appear on the client else rust crashes if it cannot find the room FIXME
waiter = bob.WaitUntilEventInRoom(t, roomID, api.CheckEventHasMembership(bob.UserID(), "join"))
waiter.Wait(t, 5*time.Second)

// bob hits scrollback and should see but not be able to decrypt the message
bob.MustBackpaginate(t, roomID, 5)
ev := bob.MustGetEvent(t, roomID, evID) // TODO
must.NotEqual(t, ev.Text, beforeJoinBody, "bob was able to decrypt a message from before he was joined")
})
} */
}

0 comments on commit 8225472

Please sign in to comment.