-
Notifications
You must be signed in to change notification settings - Fork 55
Add sticky event parsing #462
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
turt2live
wants to merge
3
commits into
main
Choose a base branch
from
travis/msc4354-sticky
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+194
−0
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
package gomatrixserverlib | ||
|
||
import ( | ||
"crypto/sha256" | ||
"encoding/base64" | ||
"encoding/json" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/tidwall/sjson" | ||
) | ||
|
||
func makeStickyEvent(t *testing.T, durationMS int64, originTS int64, stateKey *string) PDU { | ||
verImpl := MustGetRoomVersion(RoomVersionV12) | ||
|
||
m := map[string]interface{}{ | ||
"sticky": map[string]int64{ | ||
"duration_ms": durationMS, | ||
}, | ||
"room_id": "!L6nFTAu28CEi9yn9up1SUiKtTNnKt2yomgy2JFRT2Zk", | ||
"type": "m.room.message", | ||
"sender": "@user:localhost", | ||
"content": map[string]interface{}{ | ||
"body": "Hello, World!", | ||
"msgtype": "m.text", | ||
}, | ||
"origin_server_ts": originTS, | ||
"unsigned": make(map[string]interface{}), | ||
"depth": 1, | ||
"origin": "localhost", | ||
"prev_events": []string{"$65vISquU7WNlFCaJeJ5uohlX4LVEPx5yEkAc1hpRf44"}, | ||
"auth_events": []string{"$65vISquU7WNlFCaJeJ5uohlX4LVEPx5yEkAc1hpRf44"}, | ||
"hashes": map[string]string{ | ||
"sha256": "1234567890", | ||
}, | ||
"signatures": map[string]interface{}{ | ||
"localhost": map[string]string{ | ||
"ed25519:localhost": "doesn't matter because it's not checked", | ||
}, | ||
}, | ||
} | ||
if stateKey != nil { | ||
m["state_key"] = *stateKey | ||
} | ||
if durationMS < 0 { | ||
delete(m, "sticky") | ||
} | ||
|
||
b, err := json.Marshal(m) | ||
assert.NoError(t, err, "failed to marshal sticky message event") | ||
|
||
// we need to add hashes manually so we don't cause our event to become redacted | ||
cj, err := CanonicalJSON(b) | ||
assert.NoError(t, err, "failed to canonicalize sticky message event") | ||
for _, key := range []string{"signatures", "unsigned", "hashes"} { | ||
cj, err = sjson.DeleteBytes(cj, key) | ||
assert.NoErrorf(t, err, "failed to delete %s from sticky message event", key) | ||
} | ||
sum := sha256.Sum256(cj) | ||
b, err = sjson.SetBytes(b, "hashes.sha256", base64.RawURLEncoding.EncodeToString(sum[:])) | ||
assert.NoError(t, err, "failed to set sha256 hash on sticky message event") | ||
|
||
ev, err := verImpl.NewEventFromUntrustedJSON(b) | ||
assert.NoError(t, err, "failed to create new untrusted sticky message event") | ||
assert.NotNil(t, ev) | ||
return ev | ||
} | ||
|
||
func TestIsSticky(t *testing.T) { | ||
// Note: IsSticky internally uses `time.Now()`, so we can't play with the time too much. | ||
|
||
// Happy path | ||
ev := makeStickyEvent(t, 20000, time.Now().UnixMilli(), nil) | ||
assert.True(t, ev.IsSticky(time.Now())) | ||
|
||
// Origin before now | ||
ev = makeStickyEvent(t, 20000, time.Now().UnixMilli()-10000, nil) | ||
assert.True(t, ev.IsSticky(time.Now())) // should use the -10s time from origin as the start time | ||
|
||
// Origin in the future | ||
ev = makeStickyEvent(t, 20000, time.Now().UnixMilli()+30000, nil) | ||
assert.True(t, ev.IsSticky(time.Now())) // This will switch to using Now() instead of the 30s future, so should be in range | ||
|
||
// Origin is well before now, leading to expiration upon receipt | ||
ev = makeStickyEvent(t, 20000, time.Now().UnixMilli()-30000, nil) | ||
assert.False(t, ev.IsSticky(time.Now())) | ||
|
||
// Not a message event | ||
stateKey := "state_key" | ||
ev = makeStickyEvent(t, 20000, time.Now().UnixMilli(), &stateKey) | ||
assert.False(t, ev.IsSticky(time.Now())) | ||
|
||
// Not a sticky event | ||
ev = makeStickyEvent(t, -1, time.Now().UnixMilli(), nil) // -1 creates a non-sticky event | ||
assert.False(t, ev.IsSticky(time.Now())) | ||
} | ||
|
||
func TestStickyEndTime(t *testing.T) { | ||
now := time.Now().UTC().Truncate(time.Millisecond) | ||
nowTS := now.UnixMilli() | ||
received := now | ||
|
||
// Happy path: event is a message event, and origin and duration are within range | ||
ev := makeStickyEvent(t, 20000, nowTS, nil) | ||
assert.Equal(t, now.Add(20*time.Second), ev.StickyEndTime(received)) | ||
|
||
// Origin before now, but duration still within range | ||
ev = makeStickyEvent(t, 20000, nowTS-10000, nil) | ||
assert.Equal(t, now.Add(10*time.Second), ev.StickyEndTime(received)) // +10 s because origin is -10s with a duration of 20s | ||
|
||
// Origin and duration before now | ||
ev = makeStickyEvent(t, 20000, nowTS-30000, nil) | ||
assert.Equal(t, received.Add(-10*time.Second), ev.StickyEndTime(received)) // 10s before received (-30+20 = -10) | ||
|
||
// Origin in the future (using received time instead), duration still within range | ||
ev = makeStickyEvent(t, 20000, nowTS+10000, nil) | ||
assert.Equal(t, now.Add(20*time.Second), ev.StickyEndTime(received)) // +20s because we'll use the received time as a start time | ||
|
||
// Origin is in the future, which places the start time before the origin | ||
ev = makeStickyEvent(t, 20000, nowTS+30000, nil) | ||
assert.Equal(t, received.Add(20*time.Second), ev.StickyEndTime(received)) // The origin is ignored, so +20s for the duration | ||
|
||
// Duration is more than an hour | ||
ev = makeStickyEvent(t, 3699999, nowTS, nil) | ||
assert.Equal(t, now.Add(1*time.Hour), ev.StickyEndTime(received)) | ||
|
||
// Not a message event | ||
stateKey := "state_key" | ||
ev = makeStickyEvent(t, 20000, nowTS, &stateKey) | ||
assert.Equal(t, time.Time{}, ev.StickyEndTime(received)) | ||
|
||
// Not a sticky event | ||
ev = makeStickyEvent(t, -1, nowTS, nil) // -1 creates a non-sticky event | ||
assert.Equal(t, time.Time{}, ev.StickyEndTime(received)) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be applied to
eventV1
?At a glance, I would think this only applies to
eventV3
or newer. But I haven't been following gmsl or that MSC so maybe I'm wrong.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not tied to a room version, so v1 probably makes sense.