forked from berty/weshnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
store_message_test.go
220 lines (171 loc) · 5.68 KB
/
store_message_test.go
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package weshnet
import (
"container/ring"
"context"
"fmt"
"testing"
"time"
"github.com/libp2p/go-libp2p/p2p/host/eventbus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
ipfslog "berty.tech/go-ipfs-log"
"berty.tech/weshnet/v2/pkg/protocoltypes"
"berty.tech/weshnet/v2/pkg/testutil"
)
func countEntries(out <-chan *protocoltypes.GroupMessageEvent) int {
found := 0
for range out {
found++
}
return found
}
func Test_AddMessage_ListMessages_manually_supplying_secrets(t *testing.T) {
testutil.FilterSpeed(t, testutil.Slow)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
memberCount := 2
deviceCount := 1
entriesCount := 25
testMsg1 := []byte("first message")
peers, _, cleanup := CreatePeersWithGroupTest(ctx, t, "/tmp/message_test", memberCount, deviceCount)
defer cleanup()
dPK0 := peers[0].GC.DevicePubKey()
ds0For1, err := peers[0].SecretStore.GetShareableChainKey(ctx, peers[0].GC.Group(), peers[1].GC.MemberPubKey())
require.NoError(t, err)
require.NotNil(t, ds0For1)
err = peers[1].SecretStore.RegisterChainKey(ctx, peers[0].GC.Group(), dPK0, ds0For1)
require.NoError(t, err)
_, err = peers[0].GC.MessageStore().AddMessage(ctx, testMsg1)
require.NoError(t, err)
<-time.After(time.Millisecond * 500)
out, err := peers[0].GC.MessageStore().ListEvents(ctx, nil, nil, false)
require.NoError(t, err)
require.Equal(t, 1, countEntries(out))
watcherCtx, watcherCancel := context.WithTimeout(ctx, time.Second*5)
chSub, err := peers[1].GC.MessageStore().EventBus().Subscribe(new(*protocoltypes.GroupMessageEvent))
require.NoError(t, err)
defer chSub.Close()
go func() {
for {
select {
case <-chSub.Out():
case <-watcherCtx.Done():
return
}
c, err := peers[1].GC.MessageStore().ListEvents(watcherCtx, nil, nil, false)
if !assert.NoError(t, err) {
watcherCancel()
break
}
if countEntries(c) == entriesCount+1 {
watcherCancel()
break
}
}
}()
for i := 0; i < entriesCount; i++ {
payload := []byte(fmt.Sprintf("test message %d", i))
_, err = peers[0].GC.MessageStore().AddMessage(ctx, payload)
require.NoError(t, err)
}
<-watcherCtx.Done()
out, err = peers[1].GC.MessageStore().ListEvents(ctx, nil, nil, false)
require.NoError(t, err)
<-time.After(time.Second)
require.Equal(t, 1+entriesCount, countEntries(out))
// TODO: check that ListEvents can be called multiple times with the same output
// TODO: check that message are correctly ordered
// TODO: check that message are correctly decrypted
// TODO: check that message sender is correct
// TODO: check that message parents IDs are valid
// TODO: check that message IDs are valid
}
func bufferCount(buffer *ring.Ring) int {
count := 0
buffer.Do(func(f interface{}) {
if _, ok := f.(ipfslog.Entry); ok {
count++
}
})
return count
}
func Test_Add_Messages_To_Cache(t *testing.T) {
testutil.FilterSpeed(t, testutil.Fast)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
memberCount := 2
deviceCount := 1
entriesCount := 50
testMsg1 := []byte("last message")
peers, _, cleanup := CreatePeersWithGroupTest(ctx, t, "/tmp/message_test", memberCount, deviceCount)
defer cleanup()
dPK0 := peers[0].GC.DevicePubKey()
dPK0Raw, err := dPK0.Raw()
require.NoError(t, err)
ds0For1, err := peers[0].SecretStore.GetShareableChainKey(ctx, peers[0].GC.Group(), peers[1].GC.MemberPubKey())
require.NoError(t, err)
require.NotNil(t, ds0For1)
cevent, err := peers[0].GC.MessageStore().EventBus().Subscribe(
new(*protocoltypes.GroupMessageEvent), eventbus.BufSize(entriesCount))
require.NoError(t, err)
cadded, err := peers[1].GC.MessageStore().EventBus().Subscribe(
new(messageItem), eventbus.BufSize(entriesCount))
require.NoError(t, err)
for i := 0; i < entriesCount; i++ {
payload := []byte(fmt.Sprintf("test message %d", i))
_, err = peers[0].GC.MessageStore().AddMessage(ctx, payload)
require.NoError(t, err)
}
// check that all events has been received on peer 1
for i := 0; i < entriesCount; i++ {
select {
case <-cevent.Out():
case <-time.After(time.Second):
require.FailNow(t, "timeout while waiting for group message event")
return
}
}
cevent.Close()
clist, err := peers[0].GC.MessageStore().ListEvents(ctx, nil, nil, false)
require.NoError(t, err)
count := countEntries(clist)
require.Equal(t, entriesCount, count)
// check that messages has been replicated on peer 2
for i := 0; i < entriesCount; i++ {
select {
case <-cadded.Out():
case <-time.After(time.Second * 5):
require.FailNow(t, "timeout while waiting for replicated event")
return
}
}
cadded.Close()
// time.Sleep(time.Millisecond * 500)
size, ok := peers[1].GC.MessageStore().CacheSizeForDevicePK(dPK0Raw)
require.True(t, ok)
require.Equal(t, entriesCount, size)
err = peers[1].SecretStore.RegisterChainKey(ctx, peers[0].GC.Group(), dPK0, ds0For1)
require.NoError(t, err)
cevent, err = peers[1].GC.MessageStore().EventBus().Subscribe(
new(*protocoltypes.GroupMessageEvent), eventbus.BufSize(entriesCount))
require.NoError(t, err)
peers[1].GC.MessageStore().ProcessMessageQueueForDevicePK(ctx, dPK0Raw)
// check that all events has been received on peer 2
for i := 0; i < entriesCount; i++ {
select {
case <-cevent.Out():
case <-time.After(time.Second):
require.FailNow(t, "timeout while waiting for group message event")
return
}
}
cevent.Close()
size, ok = peers[1].GC.MessageStore().CacheSizeForDevicePK(dPK0Raw)
require.True(t, ok)
require.Equal(t, 0, size)
_, err = peers[0].GC.MessageStore().AddMessage(ctx, testMsg1)
require.NoError(t, err)
size, ok = peers[1].GC.MessageStore().CacheSizeForDevicePK(dPK0Raw)
require.True(t, ok)
require.Equal(t, 0, size)
}