forked from matrix-org/gomatrixserverlib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheventV1_test.go
More file actions
136 lines (113 loc) · 4.77 KB
/
Copy patheventV1_test.go
File metadata and controls
136 lines (113 loc) · 4.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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) {
now := time.Now()
// Happy path
ev := makeStickyEvent(t, 20000, now.UnixMilli(), nil)
assert.True(t, ev.IsSticky(now, now))
// Origin before now
ev = makeStickyEvent(t, 20000, now.UnixMilli()-10000, nil)
assert.True(t, ev.IsSticky(now, now)) // should use the -10s time from origin as the start time
// Origin in the future
ev = makeStickyEvent(t, 20000, now.UnixMilli()+30000, nil)
assert.True(t, ev.IsSticky(now, 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, now.UnixMilli()-30000, nil)
assert.False(t, ev.IsSticky(now, now))
// State events can also be sticky
stateKey := "state_key"
ev = makeStickyEvent(t, 20000, now.UnixMilli(), &stateKey)
assert.True(t, ev.IsSticky(now, now))
// Not a sticky event
ev = makeStickyEvent(t, -1, now.UnixMilli(), nil) // -1 creates a non-sticky event
assert.False(t, ev.IsSticky(now, 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))
// State events can also be sticky
stateKey := "state_key"
ev = makeStickyEvent(t, 20000, nowTS, &stateKey)
assert.Equal(t, now.Add(20*time.Second), 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))
}