-
Notifications
You must be signed in to change notification settings - Fork 241
/
messenger_raw_message_resend_test.go
412 lines (365 loc) · 14.1 KB
/
messenger_raw_message_resend_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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
package api
import (
"context"
"errors"
"path/filepath"
"testing"
"time"
"github.com/cenkalti/backoff/v3"
"go.uber.org/zap"
"github.com/status-im/status-go/eth-node/types"
m_common "github.com/status-im/status-go/multiaccounts/common"
"github.com/status-im/status-go/params"
"github.com/status-im/status-go/protocol"
"github.com/status-im/status-go/protocol/common"
"github.com/status-im/status-go/protocol/common/shard"
"github.com/status-im/status-go/protocol/communities"
"github.com/status-im/status-go/protocol/protobuf"
"github.com/status-im/status-go/protocol/requests"
"github.com/status-im/status-go/protocol/tt"
"github.com/status-im/status-go/services/utils"
"github.com/status-im/status-go/wakuv2"
"github.com/stretchr/testify/suite"
)
type MessengerRawMessageResendTest struct {
suite.Suite
aliceBackend *GethStatusBackend
bobBackend *GethStatusBackend
aliceMessenger *protocol.Messenger
bobMessenger *protocol.Messenger
// add exchangeBootNode to ensure alice and bob can find each other.
// If relying on in the fleet, the test will likely be flaky
exchangeBootNode *wakuv2.Waku
}
func TestMessengerRawMessageResendTestSuite(t *testing.T) {
suite.Run(t, new(MessengerRawMessageResendTest))
}
func (s *MessengerRawMessageResendTest) SetupTest() {
logger, err := zap.NewDevelopment()
s.Require().NoError(err)
exchangeNodeConfig := &wakuv2.Config{
Port: 0,
EnableDiscV5: true,
EnablePeerExchangeServer: true,
ClusterID: 16,
UseShardAsDefaultTopic: true,
DefaultShardPubsubTopic: shard.DefaultShardPubsubTopic(),
}
s.exchangeBootNode, err = wakuv2.New("", "", exchangeNodeConfig, logger.Named("pxServerNode"), nil, nil, nil, nil)
s.Require().NoError(err)
s.Require().NoError(s.exchangeBootNode.Start())
s.createAliceBobBackendAndLogin()
community := s.createTestCommunity(s.aliceMessenger, protobuf.CommunityPermissions_MANUAL_ACCEPT)
s.addMutualContact()
advertiseCommunityToUserOldWay(&s.Suite, community, s.aliceMessenger, s.bobMessenger)
requestBob := &requests.RequestToJoinCommunity{
CommunityID: community.ID(),
}
joinOnRequestCommunity(&s.Suite, community, s.aliceMessenger, s.bobMessenger, requestBob)
}
func (s *MessengerRawMessageResendTest) TearDownTest() {
// Initialize a map to keep track of the operation status.
operationStatus := map[string]bool{
"Alice Logout": false,
"Bob Logout": false,
"Boot Node Stop": false,
}
done := make(chan string, 3) // Buffered channel to receive the names of the completed operations
errs := make(chan error, 3) // Channel to receive errs from operations
// Asynchronously perform operations and report completion or errs.
go func() {
err := s.aliceBackend.Logout()
if err != nil {
errs <- err
}
done <- "Alice Logout"
}()
go func() {
err := s.bobBackend.Logout()
if err != nil {
errs <- err
}
done <- "Bob Logout"
}()
go func() {
err := s.exchangeBootNode.Stop()
if err != nil {
errs <- err
}
done <- "Boot Node Stop"
}()
timeout := time.After(30 * time.Second)
operationsCompleted := 0
for operationsCompleted < 3 {
select {
case opName := <-done:
s.T().Logf("%s completed successfully.", opName)
operationStatus[opName] = true
operationsCompleted++
case err := <-errs:
s.Require().NoError(err)
case <-timeout:
// If a timeout occurs, check which operations have not reported completion.
s.T().Errorf("Timeout occurred, the following operations did not complete in time:")
for opName, completed := range operationStatus {
if !completed {
s.T().Errorf("%s is still pending.", opName)
}
}
s.T().FailNow()
}
}
}
func (s *MessengerRawMessageResendTest) createAliceBobBackendAndLogin() {
pxServerNodeENR, err := s.exchangeBootNode.GetNodeENRString()
s.Require().NoError(err)
// we don't support multiple logger instances, so just share the log dir
shareLogDir := filepath.Join(s.T().TempDir(), "logs")
s.T().Logf("shareLogDir: %s", shareLogDir)
s.createBackendAndLogin(&s.aliceBackend, &s.aliceMessenger, "alice66", pxServerNodeENR, shareLogDir)
s.createBackendAndLogin(&s.bobBackend, &s.bobMessenger, "bob66", pxServerNodeENR, shareLogDir)
aliceWaku := s.aliceBackend.StatusNode().WakuV2Service()
bobWaku := s.bobBackend.StatusNode().WakuV2Service()
// NOTE: default MaxInterval is 10s, which is too short for the test
// TODO(frank) figure out why it takes so long for the peers to know each other
err = tt.RetryWithBackOff(func() error {
if len(aliceWaku.Peerstore().Addrs(bobWaku.PeerID())) > 0 {
return nil
}
s.T().Logf("alice don't know bob's addresses")
return errors.New("alice don't know bob's addresses")
}, func(b *backoff.ExponentialBackOff) { b.MaxInterval = 20 * time.Second })
s.Require().NoError(err)
err = tt.RetryWithBackOff(func() error {
if len(bobWaku.Peerstore().Addrs(aliceWaku.PeerID())) > 0 {
return nil
}
s.T().Logf("bob don't know alice's addresses")
return errors.New("bob don't know alice's addresses")
}, func(b *backoff.ExponentialBackOff) { b.MaxInterval = 20 * time.Second })
s.Require().NoError(err)
}
func (s *MessengerRawMessageResendTest) createBackendAndLogin(backend **GethStatusBackend, messenger **protocol.Messenger, displayName, pxServerNodeENR, shareLogDir string) {
*backend = NewGethStatusBackend()
rootDir := filepath.Join(s.T().TempDir())
s.T().Logf("%s rootDir: %s", displayName, rootDir)
createAccountRequest := s.setCreateAccountRequest(displayName, rootDir, shareLogDir)
_, err := (*backend).CreateAccountAndLogin(createAccountRequest,
params.WithDiscV5BootstrapNodes([]string{pxServerNodeENR}),
// override fleet nodes
params.WithWakuNodes([]string{}))
s.Require().NoError(err)
*messenger = (*backend).Messenger()
s.Require().NotNil(messenger)
_, err = (*messenger).Start()
s.Require().NoError(err)
}
func (s *MessengerRawMessageResendTest) setCreateAccountRequest(displayName, backupDisabledDataDir, logFilePath string) *requests.CreateAccount {
nameServer := "1.1.1.1"
verifyENSContractAddress := "0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e"
verifyTransactionChainID := int64(1)
verifyURL := "https://eth-archival.rpc.grove.city/v1/3ef2018191814b7e1009b8d9"
logLevel := "DEBUG"
networkID := uint64(1)
password := "qwerty"
return &requests.CreateAccount{
UpstreamConfig: verifyURL,
WakuV2Nameserver: &nameServer,
VerifyENSContractAddress: &verifyENSContractAddress,
BackupDisabledDataDir: backupDisabledDataDir,
Password: password,
DisplayName: displayName,
LogEnabled: true,
VerifyTransactionChainID: &verifyTransactionChainID,
VerifyTransactionURL: &verifyURL,
VerifyENSURL: &verifyURL,
LogLevel: &logLevel,
LogFilePath: logFilePath,
NetworkID: &networkID,
CustomizationColor: string(m_common.CustomizationColorPrimary),
}
}
// TestMessageSent tests if ApplicationMetadataMessage_COMMUNITY_REQUEST_TO_JOIN is in state `sent` without resending
func (s *MessengerRawMessageResendTest) TestMessageSent() {
ids, err := s.bobMessenger.RawMessagesIDsByType(protobuf.ApplicationMetadataMessage_COMMUNITY_REQUEST_TO_JOIN)
s.Require().NoError(err)
s.Require().Len(ids, 1)
err = tt.RetryWithBackOff(func() error {
rawMessage, err := s.bobMessenger.RawMessageByID(ids[0])
s.Require().NoError(err)
s.Require().NotNil(rawMessage)
if rawMessage.Sent {
return nil
}
return errors.New("raw message should be sent finally")
})
s.Require().NoError(err)
}
// TestMessageResend tests if ApplicationMetadataMessage_COMMUNITY_REQUEST_TO_JOIN is resent
func (s *MessengerRawMessageResendTest) TestMessageResend() {
ids, err := s.bobMessenger.RawMessagesIDsByType(protobuf.ApplicationMetadataMessage_COMMUNITY_REQUEST_TO_JOIN)
s.Require().NoError(err)
s.Require().Len(ids, 1)
rawMessage, err := s.bobMessenger.RawMessageByID(ids[0])
s.Require().NoError(err)
s.Require().NotNil(rawMessage)
s.Require().NoError(s.bobMessenger.UpdateRawMessageSent(rawMessage.ID, false, 0))
err = tt.RetryWithBackOff(func() error {
rawMessage, err := s.bobMessenger.RawMessageByID(ids[0])
s.Require().NoError(err)
s.Require().NotNil(rawMessage)
if !rawMessage.Sent {
return errors.New("message ApplicationMetadataMessage_COMMUNITY_REQUEST_TO_JOIN was not resent yet")
}
return nil
})
s.Require().NoError(err)
waitOnMessengerResponse(&s.Suite, func(r *protocol.MessengerResponse) error {
if len(r.RequestsToJoinCommunity()) > 0 {
return nil
}
return errors.New("community request to join not received")
}, s.aliceMessenger)
}
// To be removed in https://github.com/status-im/status-go/issues/4437
func advertiseCommunityToUserOldWay(s *suite.Suite, community *communities.Community, alice *protocol.Messenger, bob *protocol.Messenger) {
chat := protocol.CreateOneToOneChat(bob.IdentityPublicKeyString(), bob.IdentityPublicKey(), bob.GetTransport())
inputMessage := common.NewMessage()
inputMessage.ChatId = chat.ID
inputMessage.Text = "some text"
inputMessage.CommunityID = community.IDString()
err := alice.SaveChat(chat)
s.Require().NoError(err)
_, err = alice.SendChatMessage(context.Background(), inputMessage)
s.Require().NoError(err)
// Ensure community is received
response, err := protocol.WaitOnMessengerResponse(
bob,
func(r *protocol.MessengerResponse) bool {
return len(r.Communities()) > 0
},
"bob did not receive community request to join",
)
s.Require().NoError(err)
communityInResponse := response.Communities()[0]
s.Require().Equal(community.ID(), communityInResponse.ID())
}
func (s *MessengerRawMessageResendTest) addMutualContact() {
bobPubkey := s.bobMessenger.IdentityPublicKeyCompressed()
bobZQ3ID, err := utils.SerializePublicKey(bobPubkey)
s.Require().NoError(err)
mr, err := s.aliceMessenger.AddContact(context.Background(), &requests.AddContact{
ID: bobZQ3ID,
DisplayName: "bob666",
})
s.Require().NoError(err)
s.Require().Len(mr.Messages(), 2)
var contactRequest *common.Message
waitOnMessengerResponse(&s.Suite, func(r *protocol.MessengerResponse) error {
for _, m := range r.Messages() {
if m.GetContentType() == protobuf.ChatMessage_CONTACT_REQUEST {
contactRequest = m
return nil
}
}
return errors.New("contact request not received")
}, s.bobMessenger)
mr, err = s.bobMessenger.AcceptContactRequest(context.Background(), &requests.AcceptContactRequest{
ID: types.FromHex(contactRequest.ID),
})
s.Require().NoError(err)
s.Require().Len(mr.Contacts, 1)
waitOnMessengerResponse(&s.Suite, func(r *protocol.MessengerResponse) error {
if len(r.Contacts) > 0 {
return nil
}
return errors.New("contact accepted not received")
}, s.aliceMessenger)
}
type MessageResponseValidator func(*protocol.MessengerResponse) error
func waitOnMessengerResponse(s *suite.Suite, fnWait MessageResponseValidator, user *protocol.Messenger) {
_, err := protocol.WaitOnMessengerResponse(
user,
func(r *protocol.MessengerResponse) bool {
err := fnWait(r)
if err != nil {
s.T().Logf("response error: %s", err.Error())
}
return err == nil
},
"MessengerResponse data not received",
)
s.Require().NoError(err)
}
func requestToJoinCommunity(s *suite.Suite, controlNode *protocol.Messenger, user *protocol.Messenger, request *requests.RequestToJoinCommunity) types.HexBytes {
response, err := user.RequestToJoinCommunity(request)
s.Require().NoError(err)
s.Require().NotNil(response)
s.Require().Len(response.RequestsToJoinCommunity(), 1)
requestToJoin := response.RequestsToJoinCommunity()[0]
s.Require().Equal(requestToJoin.PublicKey, user.IdentityPublicKeyString())
_, err = protocol.WaitOnMessengerResponse(
controlNode,
func(r *protocol.MessengerResponse) bool {
if len(r.RequestsToJoinCommunity()) == 0 {
return false
}
for _, resultRequest := range r.RequestsToJoinCommunity() {
if resultRequest.PublicKey == user.IdentityPublicKeyString() {
return true
}
}
return false
},
"control node did not receive community request to join",
)
s.Require().NoError(err)
return requestToJoin.ID
}
func joinOnRequestCommunity(s *suite.Suite, community *communities.Community, controlNode *protocol.Messenger, user *protocol.Messenger, request *requests.RequestToJoinCommunity) {
// Request to join the community
requestToJoinID := requestToJoinCommunity(s, controlNode, user, request)
// accept join request
acceptRequestToJoin := &requests.AcceptRequestToJoinCommunity{ID: requestToJoinID}
response, err := controlNode.AcceptRequestToJoinCommunity(acceptRequestToJoin)
s.Require().NoError(err)
s.Require().NotNil(response)
updatedCommunity := response.Communities()[0]
s.Require().NotNil(updatedCommunity)
s.Require().True(updatedCommunity.HasMember(user.IdentityPublicKey()))
// receive request to join response
_, err = protocol.WaitOnMessengerResponse(
user,
func(r *protocol.MessengerResponse) bool {
return len(r.Communities()) > 0 && r.Communities()[0].HasMember(user.IdentityPublicKey())
},
"user did not receive request to join response",
)
s.Require().NoError(err)
userCommunity, err := user.GetCommunityByID(community.ID())
s.Require().NoError(err)
s.Require().True(userCommunity.HasMember(user.IdentityPublicKey()))
_, err = protocol.WaitOnMessengerResponse(
controlNode,
func(r *protocol.MessengerResponse) bool {
return len(r.Communities()) > 0 && r.Communities()[0].HasMember(user.IdentityPublicKey())
},
"control node did not receive request to join response",
)
s.Require().NoError(err)
}
func (s *MessengerRawMessageResendTest) createTestCommunity(controlNode *protocol.Messenger, membershipType protobuf.CommunityPermissions_Access) *communities.Community {
description := &requests.CreateCommunity{
Membership: membershipType,
Name: "status",
Color: "#ffffff",
Description: "status community description",
PinMessageAllMembersEnabled: false,
}
response, err := controlNode.CreateCommunity(description, true)
s.Require().NoError(err)
s.Require().NotNil(response)
s.Require().Len(response.Communities(), 1)
s.Require().Len(response.Chats(), 1)
return response.Communities()[0]
}