-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmessenger_identity_image_test.go
566 lines (468 loc) · 18.8 KB
/
messenger_identity_image_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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
package protocol
import (
"context"
"crypto/ecdsa"
"errors"
"testing"
"time"
"github.com/cenkalti/backoff/v3"
"github.com/stretchr/testify/suite"
"go.uber.org/zap"
gethbridge "github.com/status-im/status-go/eth-node/bridge/geth"
"github.com/status-im/status-go/eth-node/crypto"
"github.com/status-im/status-go/eth-node/types"
"github.com/status-im/status-go/images"
"github.com/status-im/status-go/multiaccounts"
"github.com/status-im/status-go/multiaccounts/settings"
"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/waku"
)
func TestMessengerProfilePictureHandlerSuite(t *testing.T) {
suite.Run(t, new(MessengerProfilePictureHandlerSuite))
}
type MessengerProfilePictureHandlerSuite struct {
suite.Suite
alice *Messenger // client instance of Messenger
bob *Messenger // server instance of Messenger
aliceKey *ecdsa.PrivateKey // private key for the alice instance of Messenger
bobKey *ecdsa.PrivateKey // private key for the bob instance of Messenger
// If one wants to send messages between different instances of Messenger,
// a single Waku service should be shared.
shh types.Waku
logger *zap.Logger
}
func (s *MessengerProfilePictureHandlerSuite) SetupSuite() {
s.logger = tt.MustCreateTestLogger()
}
func (s *MessengerProfilePictureHandlerSuite) setup() {
var err error
// Setup Waku things
config := waku.DefaultConfig
config.MinimumAcceptedPoW = 0
wakuLogger := s.logger.Named("Waku")
shh := waku.New(&config, wakuLogger)
s.shh = gethbridge.NewGethWakuWrapper(shh)
s.Require().NoError(shh.Start())
// Generate private keys for Alice and Bob
s.aliceKey, err = crypto.GenerateKey()
s.Require().NoError(err)
s.bobKey, err = crypto.GenerateKey()
s.Require().NoError(err)
// Generate Alice Messenger
aliceLogger := s.logger.Named("Alice messenger")
s.alice, err = newMessengerWithKey(s.shh, s.aliceKey, aliceLogger, []Option{})
s.Require().NoError(err)
s.logger.Debug("alice messenger created")
// Generate Bob Messenger
bobLogger := s.logger.Named("Bob messenger")
s.bob, err = newMessengerWithKey(s.shh, s.bobKey, bobLogger, []Option{})
s.Require().NoError(err)
s.logger.Debug("bob messenger created")
// Setup MultiAccount for Alice Messenger
s.logger.Debug("alice setupMultiAccount before")
s.setupMultiAccount(s.alice)
s.logger.Debug("alice setupMultiAccount after")
}
func (s *MessengerProfilePictureHandlerSuite) setupTest() {
s.logger.Debug("setupTest fired")
s.setup()
s.logger.Debug("setupTest completed")
}
func (s *MessengerProfilePictureHandlerSuite) SetupTest() {
s.logger.Debug("SetupTest fired")
s.setup()
s.logger.Debug("SetupTest completed")
}
func (s *MessengerProfilePictureHandlerSuite) tearDown() {
// Shutdown messengers
s.NoError(s.alice.Shutdown())
s.alice = nil
s.NoError(s.bob.Shutdown())
s.bob = nil
_ = s.logger.Sync()
//time.Sleep(2 * time.Second)
}
func (s *MessengerProfilePictureHandlerSuite) tearDownTest() {
s.logger.Debug("tearDownTest fired")
s.tearDown()
s.logger.Debug("tearDownTest completed")
}
func (s *MessengerProfilePictureHandlerSuite) TearDownTest() {
s.logger.Debug("TearDownTest fired")
s.tearDown()
s.logger.Debug("TearDownTest completed")
}
func (s *MessengerProfilePictureHandlerSuite) generateKeyUID(publicKey *ecdsa.PublicKey) string {
return types.EncodeHex(crypto.FromECDSAPub(publicKey))
}
func (s *MessengerProfilePictureHandlerSuite) setupMultiAccount(m *Messenger) {
keyUID := s.generateKeyUID(&m.identity.PublicKey)
m.account = &multiaccounts.Account{KeyUID: keyUID}
err := m.multiAccounts.SaveAccount(multiaccounts.Account{Name: "string", KeyUID: keyUID})
s.NoError(err)
}
func (s *MessengerProfilePictureHandlerSuite) generateAndStoreIdentityImages(m *Messenger) []images.IdentityImage {
keyUID := s.generateKeyUID(&m.identity.PublicKey)
iis := images.SampleIdentityImages()
s.Require().NoError(m.multiAccounts.StoreIdentityImages(keyUID, iis, false))
return iis
}
func (s *MessengerProfilePictureHandlerSuite) TestChatIdentity() {
iis := s.generateAndStoreIdentityImages(s.alice)
ci, err := s.alice.createChatIdentity(privateChat)
s.Require().NoError(err)
s.Require().Exactly(len(iis), len(ci.Images))
}
func (s *MessengerProfilePictureHandlerSuite) TestEncryptDecryptIdentityImagesWithContactPubKeys() {
smPayload := "hello small image"
lgPayload := "hello large image"
ci := protobuf.ChatIdentity{
Clock: uint64(time.Now().Unix()),
Images: map[string]*protobuf.IdentityImage{
"small": {
Payload: []byte(smPayload),
},
"large": {
Payload: []byte(lgPayload),
},
},
}
// Make contact keys and Contacts, set the Contacts to added
contactKeys := make([]*ecdsa.PrivateKey, 10)
for i := range contactKeys {
contactKey, err := crypto.GenerateKey()
s.Require().NoError(err)
contactKeys[i] = contactKey
contact, err := BuildContactFromPublicKey(&contactKey.PublicKey)
s.Require().NoError(err)
contact.Added = true
s.alice.allContacts.Store(contact.ID, contact)
}
// Test EncryptIdentityImagesWithContactPubKeys
err := EncryptIdentityImagesWithContactPubKeys(ci.Images, s.alice)
s.Require().NoError(err)
for _, ii := range ci.Images {
s.Require().Equal(s.alice.allContacts.Len(), len(ii.EncryptionKeys))
}
s.Require().NotEqual([]byte(smPayload), ci.Images["small"].Payload)
s.Require().NotEqual([]byte(lgPayload), ci.Images["large"].Payload)
s.Require().True(ci.Images["small"].Encrypted)
s.Require().True(ci.Images["large"].Encrypted)
// Test DecryptIdentityImagesWithIdentityPrivateKey
err = DecryptIdentityImagesWithIdentityPrivateKey(ci.Images, contactKeys[2], &s.alice.identity.PublicKey)
s.Require().NoError(err)
s.Require().Equal(smPayload, string(ci.Images["small"].Payload))
s.Require().Equal(lgPayload, string(ci.Images["large"].Payload))
s.Require().False(ci.Images["small"].Encrypted)
s.Require().False(ci.Images["large"].Encrypted)
// RESET Messenger identity, Contacts and IdentityImage.EncryptionKeys
s.alice.allContacts = new(contactMap)
ci.Images["small"].EncryptionKeys = nil
ci.Images["large"].EncryptionKeys = nil
// Test EncryptIdentityImagesWithContactPubKeys with no contacts
err = EncryptIdentityImagesWithContactPubKeys(ci.Images, s.alice)
s.Require().NoError(err)
for _, ii := range ci.Images {
s.Require().Equal(0, len(ii.EncryptionKeys))
}
s.Require().NotEqual([]byte(smPayload), ci.Images["small"].Payload)
s.Require().NotEqual([]byte(lgPayload), ci.Images["large"].Payload)
s.Require().True(ci.Images["small"].Encrypted)
s.Require().True(ci.Images["large"].Encrypted)
// Test DecryptIdentityImagesWithIdentityPrivateKey with no valid identity
err = DecryptIdentityImagesWithIdentityPrivateKey(ci.Images, contactKeys[2], &s.alice.identity.PublicKey)
s.Require().NoError(err)
s.Require().NotEqual([]byte(smPayload), ci.Images["small"].Payload)
s.Require().NotEqual([]byte(lgPayload), ci.Images["large"].Payload)
s.Require().True(ci.Images["small"].Encrypted)
s.Require().True(ci.Images["large"].Encrypted)
}
func (s *MessengerProfilePictureHandlerSuite) TestPictureInPrivateChatOneSided() {
s.setupTest()
err := s.bob.settings.SaveSettingField(settings.ProfilePicturesVisibility, settings.ProfilePicturesShowToEveryone)
s.Require().NoError(err)
err = s.alice.settings.SaveSettingField(settings.ProfilePicturesVisibility, settings.ProfilePicturesShowToEveryone)
s.Require().NoError(err)
bChat := CreateOneToOneChat(s.generateKeyUID(&s.aliceKey.PublicKey), &s.aliceKey.PublicKey, s.alice.transport)
err = s.bob.SaveChat(bChat)
s.Require().NoError(err)
_, err = s.bob.Join(bChat)
s.Require().NoError(err)
// Alice sends a message to the public chat
message := buildTestMessage(*bChat)
response, err := s.bob.SendChatMessage(context.Background(), message)
s.Require().NoError(err)
s.Require().NotNil(response)
options := func(b *backoff.ExponentialBackOff) {
b.MaxElapsedTime = 2 * time.Second
}
err = tt.RetryWithBackOff(func() error {
response, err = s.alice.RetrieveAll()
if err != nil {
return err
}
s.Require().NotNil(response)
contacts := response.Contacts
s.logger.Debug("RetryWithBackOff contact data", zap.Any("contacts", contacts))
if len(contacts) > 0 && len(contacts[0].Images) > 0 {
s.logger.Debug("", zap.Any("contacts", contacts))
return nil
}
return errors.New("no new contacts with images received")
}, options)
}
func (s *MessengerProfilePictureHandlerSuite) TestE2eSendingReceivingProfilePicture() {
profilePicShowSettings := map[string]settings.ProfilePicturesShowToType{
"ShowToContactsOnly": settings.ProfilePicturesShowToContactsOnly,
"ShowToEveryone": settings.ProfilePicturesShowToEveryone,
"ShowToNone": settings.ProfilePicturesShowToNone,
}
profilePicViewSettings := map[string]settings.ProfilePicturesVisibilityType{
"ViewFromContactsOnly": settings.ProfilePicturesVisibilityContactsOnly,
"ViewFromEveryone": settings.ProfilePicturesVisibilityEveryone,
"ViewFromNone": settings.ProfilePicturesVisibilityNone,
}
isContactFor := map[string][]bool{
"alice": {true, false},
"bob": {true, false},
}
chatContexts := []chatContext{
publicChat,
privateChat,
}
// TODO see if possible to push each test scenario into a go routine
for _, cc := range chatContexts {
for sn, ss := range profilePicShowSettings {
for vn, vs := range profilePicViewSettings {
for _, ac := range isContactFor["alice"] {
for _, bc := range isContactFor["bob"] {
s.logger.Debug("top of the loop")
s.setupTest()
s.logger.Info("testing with criteria:",
zap.String("chat context type", string(cc)),
zap.String("profile picture Show Settings", sn),
zap.String("profile picture View Settings", vn),
zap.Bool("bob in Alice's Contacts", ac),
zap.Bool("alice in Bob's Contacts", bc),
)
expectPicture, err := resultExpected(ss, vs, ac, bc)
s.logger.Debug("expect to receive a profile pic?",
zap.Bool("result", expectPicture),
zap.Error(err))
// Setting up Bob
s.logger.Debug("Setting up test criteria for Bob")
s.logger.Debug("Save bob profile-pictures-visibility setting before")
err = s.bob.settings.SaveSettingField(settings.ProfilePicturesVisibility, vs)
s.Require().NoError(err)
s.logger.Debug("Save bob profile-pictures-visibility setting after")
s.logger.Debug("bob add contact before")
if bc {
s.logger.Debug("bob has contact to add")
_, err = s.bob.AddContact(context.Background(), &requests.AddContact{ID: types.Hex2Bytes(s.generateKeyUID(&s.alice.identity.PublicKey))})
s.Require().NoError(err)
s.logger.Debug("bob add contact after")
}
s.logger.Debug("bob add contact after after")
// Create Bob's chats
switch cc {
case publicChat:
s.logger.Debug("making publicChats for bob")
// Bob opens up the public chat and joins it
bChat := CreatePublicChat("status", s.alice.transport)
err = s.bob.SaveChat(bChat)
s.Require().NoError(err)
_, err = s.bob.Join(bChat)
s.Require().NoError(err)
case privateChat:
s.logger.Debug("making privateChats for bob")
s.logger.Debug("Bob making one to one chat with alice")
bChat := CreateOneToOneChat(s.generateKeyUID(&s.aliceKey.PublicKey), &s.aliceKey.PublicKey, s.alice.transport)
s.logger.Debug("Bob saving one to one chat with alice")
err = s.bob.SaveChat(bChat)
s.Require().NoError(err)
s.logger.Debug("Bob saved one to one chat with alice")
s.logger.Debug("Bob joining one to one chat with alice")
_, err = s.bob.Join(bChat)
s.Require().NoError(err)
s.logger.Debug("Bob joined one to one chat with alice")
default:
s.Failf("unexpected chat context type", "%s", string(cc))
}
// Setting up Alice
s.logger.Debug("Setting up test criteria for Alice")
s.logger.Debug("Save alice profile-pictures-show-to setting before")
err = s.alice.settings.SaveSettingField(settings.ProfilePicturesShowTo, ss)
s.Require().NoError(err)
s.logger.Debug("Save alice profile-pictures-show-to setting after")
s.logger.Debug("alice add contact before")
if ac {
s.logger.Debug("alice has contact to add")
_, err = s.alice.AddContact(context.Background(), &requests.AddContact{ID: types.Hex2Bytes(s.generateKeyUID(&s.bob.identity.PublicKey))})
s.Require().NoError(err)
s.logger.Debug("alice add contact after")
}
s.logger.Debug("alice add contact after after")
s.logger.Debug("generateAndStoreIdentityImages before")
iis := s.generateAndStoreIdentityImages(s.alice)
s.logger.Debug("generateAndStoreIdentityImages after")
s.logger.Debug("Before making chat for alice")
// Create chats
var aChat *Chat
switch cc {
case publicChat:
s.logger.Debug("making publicChats for alice")
// Alice opens creates a public chat
aChat = CreatePublicChat("status", s.alice.transport)
err = s.alice.SaveChat(aChat)
s.Require().NoError(err)
case privateChat:
s.logger.Debug("making privateChats for alice")
s.logger.Debug("Alice making one to one chat with bob")
aChat = CreateOneToOneChat(s.generateKeyUID(&s.bobKey.PublicKey), &s.bobKey.PublicKey, s.bob.transport)
s.logger.Debug("Alice saving one to one chat with bob")
err = s.alice.SaveChat(aChat)
s.Require().NoError(err)
s.logger.Debug("Alice saved one to one chat with bob")
s.logger.Debug("Alice joining one to one chat with bob")
_, err = s.alice.Join(aChat)
s.Require().NoError(err)
s.logger.Debug("Alice joined one to one chat with bob")
s.logger.Debug("alice before manually triggering publishContactCode")
err = s.alice.publishContactCode()
s.logger.Debug("alice after manually triggering publishContactCode",
zap.Error(err))
s.Require().NoError(err)
default:
s.Failf("unexpected chat context type", "%s", string(cc))
}
s.logger.Debug("Build and send a chat from alice")
// Alice sends a message to the public chat
message := buildTestMessage(*aChat)
response, err := s.alice.SendChatMessage(context.Background(), message)
s.Require().NoError(err)
s.Require().NotNil(response)
// Poll bob to see if he got the chatIdentity
// Retrieve ChatIdentity
var contacts []*Contact
s.logger.Debug("Checking Bob to see if he got the chatIdentity")
options := func(b *backoff.ExponentialBackOff) {
b.MaxElapsedTime = 2 * time.Second
}
err = tt.RetryWithBackOff(func() error {
response, err = s.bob.RetrieveAll()
if err != nil {
return err
}
contacts = response.Contacts
s.logger.Debug("RetryWithBackOff contact data", zap.Any("contacts", contacts))
if len(contacts) > 0 && len(contacts[0].Images) > 0 {
s.logger.Debug("", zap.Any("contacts", contacts))
return nil
}
return errors.New("no new contacts with images received")
}, options)
s.logger.Debug("Finished RetryWithBackOff got Bob",
zap.Any("contacts", contacts),
zap.Error(err))
if expectPicture {
s.logger.Debug("expecting a contact with images")
s.Require().NoError(err)
s.Require().NotNil(contacts)
} else {
s.logger.Debug("expecting no contacts with images")
s.Require().EqualError(err, "no new contacts with images received")
s.logger.Info("Completed testing with criteria:",
zap.String("chat context type", string(cc)),
zap.String("profile picture Show Settings", sn),
zap.String("profile picture View Settings", vn),
zap.Bool("bob in Alice's Contacts", ac),
zap.Bool("alice in Bob's Contacts", bc),
)
s.tearDownTest()
continue
}
s.logger.Debug("checking alice's contact")
// Check if alice's contact data with profile picture is there
var contact *Contact
for _, c := range contacts {
if c.ID == s.generateKeyUID(&s.alice.identity.PublicKey) {
contact = c
}
}
s.Require().NotNil(contact)
s.logger.Debug("checked alice's contact info all good")
// Check that Bob now has Alice's profile picture(s)
switch cc {
case publicChat:
// In public chat context we only need the images.SmallDimName, but also may have the large
s.Require().GreaterOrEqual(len(contact.Images), 1)
// Check if the result matches expectation
smImg, ok := contact.Images[images.SmallDimName]
s.Require().True(ok, "contact images must contain the images.SmallDimName")
for _, ii := range iis {
if ii.Name == images.SmallDimName {
s.Require().Equal(ii.Payload, smImg.Payload)
}
}
case privateChat:
s.Require().Equal(len(contact.Images), 2)
s.logger.Info("private chat chat images", zap.Any("iis", iis))
// Check if the result matches expectation
smImg, ok := contact.Images[images.SmallDimName]
s.Require().True(ok, "contact images must contain the images.SmallDimName")
lgImg, ok := contact.Images[images.LargeDimName]
s.Require().True(ok, "contact images must contain the images.LargeDimName")
for _, ii := range iis {
switch ii.Name {
case images.SmallDimName:
s.Require().Equal(ii.Payload, smImg.Payload)
case images.LargeDimName:
s.Require().Equal(ii.Payload, lgImg.Payload)
}
}
}
s.logger.Info("Completed testing with criteria:",
zap.String("chat context type", string(cc)),
zap.String("profile picture Show Settings", sn),
zap.String("profile picture View Settings", vn),
zap.Bool("bob in Alice's Contacts", ac),
zap.Bool("alice in Bob's Contacts", bc),
)
s.tearDownTest()
}
}
}
}
}
s.setupTest()
}
func resultExpected(ss settings.ProfilePicturesShowToType, vs settings.ProfilePicturesVisibilityType, ac, bc bool) (bool, error) {
switch ss {
case settings.ProfilePicturesShowToContactsOnly:
if ac {
return resultExpectedVS(vs, bc)
}
return false, nil
case settings.ProfilePicturesShowToEveryone:
return resultExpectedVS(vs, bc)
case settings.ProfilePicturesShowToNone:
return false, nil
default:
return false, errors.New("unknown ProfilePicturesShowToType")
}
}
func resultExpectedVS(vs settings.ProfilePicturesVisibilityType, bc bool) (bool, error) {
switch vs {
case settings.ProfilePicturesVisibilityContactsOnly:
return true, nil
case settings.ProfilePicturesVisibilityEveryone:
return true, nil
case settings.ProfilePicturesVisibilityNone:
// If we are contacts, we save the image regardless
return bc, nil
default:
return false, errors.New("unknown ProfilePicturesVisibilityType")
}
}