-
Notifications
You must be signed in to change notification settings - Fork 28
/
store_metadata_test.go
792 lines (598 loc) · 28.2 KB
/
store_metadata_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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
package weshnet
import (
"bytes"
"context"
crand "crypto/rand"
"fmt"
mrand "math/rand"
"testing"
"time"
datastore "github.com/ipfs/go-datastore"
ds_sync "github.com/ipfs/go-datastore/sync"
"github.com/libp2p/go-libp2p/core/crypto"
mocknet "github.com/libp2p/go-libp2p/p2p/net/mock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
"berty.tech/weshnet/v2/pkg/ipfsutil"
"berty.tech/weshnet/v2/pkg/protocoltypes"
"berty.tech/weshnet/v2/pkg/testutil"
)
func TestMetadataStoreSecret_Basic(t *testing.T) {
t.Skip("skipping as we don't care about this code now")
// TODO: handle more cases (more members, more devices...)
memberCount := 2
deviceCount := 1
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
peers, groupSK, cleanup := CreatePeersWithGroupTest(ctx, t, "/tmp/secrets_test", memberCount, deviceCount)
defer cleanup()
secretsAdded := make(chan struct{})
msA := peers[0].GC.MetadataStore()
msB := peers[1].GC.MetadataStore()
go waitForBertyEventType(ctx, t, msA, protocoltypes.EventType_EventTypeGroupDeviceChainKeyAdded, 2, secretsAdded)
go waitForBertyEventType(ctx, t, msB, protocoltypes.EventType_EventTypeGroupDeviceChainKeyAdded, 2, secretsAdded)
inviteAllPeersToGroup(ctx, t, peers, groupSK)
devPkA := peers[0].GC.DevicePubKey()
devPkB := peers[1].GC.DevicePubKey()
<-secretsAdded
<-secretsAdded
_ = devPkA
_ = devPkB
}
func TestMetadataStoreMember(t *testing.T) {
t.Skip("skipping as we don't care about this code now")
// If seed is not set, it will default to 1, explicitly setting it and displaying it if the test fails
seed := time.Now().UTC().UnixNano()
mrand.Seed(seed)
for _, tc := range []struct {
memberCount int
deviceCount int
slow bool
}{
{memberCount: 1, deviceCount: 1, slow: false},
{memberCount: 1, deviceCount: 3, slow: false},
{memberCount: 3, deviceCount: 1, slow: false},
// TODO: fix pubsub issues
// {memberCount: 3, deviceCount: 3, slow: false},
} {
t.Run(fmt.Sprintf("testMemberStore seed: %d, memberCount: %d, deviceCount: %d", seed, tc.memberCount, tc.deviceCount), func(t *testing.T) {
if tc.slow {
// TODO: re-enable this test
t.Skip()
testutil.FilterSpeed(t, testutil.Slow)
}
testMemberStore(t, tc.memberCount, tc.deviceCount)
})
}
}
func testMemberStore(t *testing.T, memberCount, deviceCount int) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Creates N members with M devices each within the same group
peers, groupSK, cleanup := CreatePeersWithGroupTest(ctx, t, "/tmp/member_test", memberCount, deviceCount)
defer cleanup()
done := make(chan struct{})
for _, peer := range peers {
go waitForBertyEventType(ctx, t, peer.GC.MetadataStore(), protocoltypes.EventType_EventTypeGroupMemberDeviceAdded, len(peers), done)
}
for i, peer := range peers {
if _, err := peer.GC.MetadataStore().AddDeviceToGroup(ctx); err != nil {
t.Fatal(err)
}
if i == 0 {
if _, err := peer.GC.MetadataStore().ClaimGroupOwnership(ctx, groupSK); err != nil {
t.Fatal(err)
}
}
}
// Wait for all events to be received in all peers's member log (or timeout)
for i := range peers {
<-done
t.Logf("got all members for member %d", i)
}
// Test if everything was replicated and indexed correctly
for i, peer := range peers {
ms := peer.GC.MetadataStore()
// Test list functions (only length, checking all entries would be too long)
memberList := ms.ListMembers()
if len(memberList) != memberCount {
t.Fatalf("%d member(s) missing from peer %d member list (%d/%d)", memberCount-len(memberList), i, len(memberList), memberCount)
}
deviceList := ms.ListDevices()
if len(deviceList) != memberCount*deviceCount {
t.Fatalf("%d device(s) missing from peer %d device list (%d/%d)", memberCount*deviceCount-len(deviceList), i, len(deviceList), memberCount*deviceCount)
}
// Test entries getter functions
for j, peerDev := range peers {
if _, err := ms.GetDevicesForMember(peerDev.GC.MemberPubKey()); err != nil {
t.Fatalf("member of peer %d is missing from peer %d member map: %v", j, i, err)
}
if _, err := ms.GetMemberByDevice(peerDev.GC.MemberPubKey()); err != nil {
t.Fatalf("device of peer %d is missing from peer %d device map: %v", j, i, err)
}
}
}
}
func ipfsAPIUsingMockNet(ctx context.Context, t *testing.T) ipfsutil.ExtendedCoreAPI {
ipfsopts := &ipfsutil.TestingAPIOpts{
Logger: zap.NewNop(),
Mocknet: mocknet.New(),
Datastore: ds_sync.MutexWrap(datastore.NewMapDatastore()),
}
node := ipfsutil.TestingCoreAPIUsingMockNet(ctx, t, ipfsopts)
return node.API()
}
func TestMetadataRendezvousPointLifecycle(t *testing.T) {
testutil.FilterSpeed(t, testutil.Fast)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Creates N members with M devices each within the same group
peers, _, cleanup := CreatePeersWithGroupTest(ctx, t, "/tmp/member_test", 1, 1)
defer cleanup()
api := ipfsAPIUsingMockNet(ctx, t)
ownCG, err := peers[0].DB.openAccountGroup(ctx, nil, api)
assert.NoError(t, err)
meta := ownCG.MetadataStore()
_, accountMemberDevice, err := peers[0].SecretStore.GetGroupForAccount()
assert.NoError(t, err)
accPK, err := accountMemberDevice.Member().Raw()
assert.NoError(t, err)
enabled, shareableContact := meta.GetIncomingContactRequestsStatus()
assert.False(t, enabled)
assert.NotNil(t, shareableContact)
// reset rdv seed
_, err = meta.ContactRequestReferenceReset(ctx)
assert.NoError(t, err)
// get set shareable contact, not enabled
enabled, shareableContact = meta.GetIncomingContactRequestsStatus()
assert.False(t, enabled)
assert.NotNil(t, shareableContact)
assert.Equal(t, accPK, shareableContact.Pk)
assert.Equal(t, 32, len(shareableContact.PublicRendezvousSeed))
_, err = meta.ContactRequestEnable(ctx)
assert.NoError(t, err)
enabled, shareableContact = meta.GetIncomingContactRequestsStatus()
assert.True(t, enabled)
assert.NotNil(t, shareableContact)
// Force reindex to check both enabled and seed
err = meta.Load(ctx, -1)
assert.NoError(t, err)
enabled, shareableContact = meta.GetIncomingContactRequestsStatus()
assert.True(t, enabled)
assert.NotNil(t, shareableContact)
assert.Equal(t, accPK, shareableContact.Pk)
assert.Equal(t, 32, len(shareableContact.PublicRendezvousSeed))
// Disable incoming contact requests
_, err = meta.ContactRequestDisable(ctx)
assert.NoError(t, err)
enabled, shareableContact = meta.GetIncomingContactRequestsStatus()
assert.False(t, enabled)
assert.NotNil(t, shareableContact)
assert.Equal(t, accPK, shareableContact.Pk)
assert.Equal(t, 32, len(shareableContact.PublicRendezvousSeed))
}
func TestMetadataContactLifecycle(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
peersCount := 4
// Creates N members with M devices each within the same group
peers, _, cleanup := CreatePeersWithGroupTest(ctx, t, "/tmp/member_test", peersCount, 1)
defer cleanup()
var (
err error
meta = make([]*MetadataStore, peersCount)
ownCG = make([]*GroupContext, peersCount)
contacts = make([]*protocoltypes.ShareableContact, peersCount)
)
api := ipfsAPIUsingMockNet(ctx, t)
for i, p := range peers {
ownCG[i], err = p.DB.openAccountGroup(ctx, nil, api)
require.NoError(t, err)
meta[i] = ownCG[i].MetadataStore()
_, err = meta[i].ContactRequestReferenceReset(ctx)
require.NoError(t, err)
_, contacts[i] = meta[i].GetIncomingContactRequestsStatus()
require.NotNil(t, contacts[i])
contacts[i].Metadata = []byte(fmt.Sprintf("own meta %d", i))
}
_, randPK, err := crypto.GenerateEd25519Key(crand.Reader)
require.NoError(t, err)
// no contacts
require.Equal(t, len(meta[0].Index().(*metadataStoreIndex).contacts), 0)
_, err = meta[0].ContactRequestReferenceReset(ctx)
require.NoError(t, err)
contact2PK := contacts[1].Pk
contact2RDVS := contacts[1].PublicRendezvousSeed
// Enqueuing outgoing
_, err = meta[0].ContactRequestOutgoingEnqueue(ctx, contacts[0], contacts[0].Metadata)
require.Error(t, err)
_, err = meta[0].ContactRequestOutgoingEnqueue(ctx, contacts[1], contacts[0].Metadata)
require.NoError(t, err)
_, err = meta[0].ContactRequestOutgoingEnqueue(ctx, contacts[1], contacts[0].Metadata)
require.NoError(t, err)
require.Equal(t, len(meta[0].Index().(*metadataStoreIndex).contacts), 1)
require.NotNil(t, meta[0].Index().(*metadataStoreIndex).contacts[string(contact2PK)])
require.Equal(t, meta[0].Index().(*metadataStoreIndex).contacts[string(contact2PK)].state, protocoltypes.ContactState_ContactStateToRequest)
require.Equal(t, meta[0].Index().(*metadataStoreIndex).contacts[string(contact2PK)].contact.Pk, contact2PK)
require.Equal(t, meta[0].Index().(*metadataStoreIndex).contacts[string(contact2PK)].contact.PublicRendezvousSeed, contact2RDVS)
require.Equal(t, contacts[0].Metadata, meta[0].Index().(*metadataStoreIndex).contactRequestMetadata[string(contact2PK)])
contacts[1].PublicRendezvousSeed = nil
_, err = meta[0].ContactRequestOutgoingEnqueue(ctx, contacts[1], contacts[0].Metadata)
require.Error(t, err)
contacts[1].PublicRendezvousSeed = []byte("too_short")
_, err = meta[0].ContactRequestOutgoingEnqueue(ctx, contacts[1], contacts[0].Metadata)
require.Error(t, err)
contacts[1].Pk = nil
contacts[1].PublicRendezvousSeed = contact2RDVS
_, err = meta[0].ContactRequestOutgoingEnqueue(ctx, contacts[1], contacts[0].Metadata)
require.Error(t, err)
contacts[1].Pk = []byte("invalid")
_, err = meta[0].ContactRequestOutgoingEnqueue(ctx, contacts[1], contacts[0].Metadata)
require.Error(t, err)
require.Equal(t, 1, len(meta[0].Index().(*metadataStoreIndex).contacts))
require.NotNil(t, meta[0].Index().(*metadataStoreIndex).contacts[string(contact2PK)])
require.Equal(t, protocoltypes.ContactState_ContactStateToRequest, meta[0].Index().(*metadataStoreIndex).contacts[string(contact2PK)].state)
require.Equal(t, contact2PK, meta[0].Index().(*metadataStoreIndex).contacts[string(contact2PK)].contact.Pk)
require.Equal(t, contact2RDVS, meta[0].Index().(*metadataStoreIndex).contacts[string(contact2PK)].contact.PublicRendezvousSeed)
require.Equal(t, contacts[0].Metadata, meta[0].Index().(*metadataStoreIndex).contactRequestMetadata[string(contact2PK)])
contacts[1].Pk = contact2PK
contacts[1].PublicRendezvousSeed = contact2RDVS
meta[0].Index().(*metadataStoreIndex).contactRequestMetadata[string(contacts[1].Pk)] = contacts[0].Metadata
// Marking as sent
_, err = meta[0].ContactRequestOutgoingSent(ctx, nil)
require.Error(t, err)
_, err = meta[0].ContactRequestOutgoingSent(ctx, randPK)
require.Error(t, err)
_, err = meta[0].ContactRequestOutgoingSent(ctx, ownCG[0].MemberPubKey())
require.Error(t, err)
meta[0].Index().(*metadataStoreIndex).contacts[string(contacts[1].Pk)].state = protocoltypes.ContactState_ContactStateAdded
_, err = meta[0].ContactRequestOutgoingSent(ctx, ownCG[1].MemberPubKey())
require.Error(t, err)
meta[0].Index().(*metadataStoreIndex).contacts[string(contacts[1].Pk)].state = protocoltypes.ContactState_ContactStateToRequest
_, err = meta[0].ContactRequestOutgoingSent(ctx, ownCG[1].MemberPubKey())
require.NoError(t, err)
require.Equal(t, len(meta[0].Index().(*metadataStoreIndex).contacts), 1)
require.NotNil(t, meta[0].Index().(*metadataStoreIndex).contacts[string(contacts[1].Pk)])
require.Equal(t, protocoltypes.ContactState_ContactStateAdded, meta[0].Index().(*metadataStoreIndex).contacts[string(contacts[1].Pk)].state)
require.Equal(t, contacts[1].Pk, meta[0].Index().(*metadataStoreIndex).contacts[string(contacts[1].Pk)].contact.Pk)
require.Equal(t, contacts[1].PublicRendezvousSeed, meta[0].Index().(*metadataStoreIndex).contacts[string(contacts[1].Pk)].contact.PublicRendezvousSeed)
// Marking as received
_, err = meta[1].ContactRequestIncomingReceived(ctx, &protocoltypes.ShareableContact{})
require.Error(t, err)
_, err = meta[1].ContactRequestIncomingReceived(ctx, &protocoltypes.ShareableContact{Pk: []byte("invalid"), PublicRendezvousSeed: []byte("invalid")})
require.Error(t, err)
_, err = meta[1].ContactRequestIncomingReceived(ctx, &protocoltypes.ShareableContact{Pk: []byte("invalid"), PublicRendezvousSeed: contacts[0].PublicRendezvousSeed})
require.Error(t, err)
_, err = meta[1].ContactRequestIncomingReceived(ctx, &protocoltypes.ShareableContact{Pk: contacts[0].Pk, PublicRendezvousSeed: []byte("invalid")})
require.Error(t, err)
_, err = meta[1].ContactRequestIncomingReceived(ctx, contacts[1])
require.Error(t, err)
_, err = meta[1].ContactRequestIncomingReceived(ctx, contacts[0])
require.NoError(t, err)
require.Equal(t, len(meta[1].Index().(*metadataStoreIndex).contacts), 1)
require.NotNil(t, meta[1].Index().(*metadataStoreIndex).contacts[string(contacts[0].Pk)])
require.Equal(t, protocoltypes.ContactState_ContactStateReceived, meta[1].Index().(*metadataStoreIndex).contacts[string(contacts[0].Pk)].state)
require.Equal(t, contacts[0].Pk, meta[1].Index().(*metadataStoreIndex).contacts[string(contacts[0].Pk)].contact.Pk)
require.Equal(t, contacts[0].PublicRendezvousSeed, meta[1].Index().(*metadataStoreIndex).contacts[string(contacts[0].Pk)].contact.PublicRendezvousSeed)
require.Equal(t, contacts[0].Metadata, meta[1].Index().(*metadataStoreIndex).contacts[string(contacts[0].Pk)].contact.Metadata)
// Accepting received
_, err = meta[1].ContactRequestIncomingAccept(ctx, nil)
require.Error(t, err)
_, err = meta[1].ContactRequestIncomingAccept(ctx, randPK)
require.Error(t, err)
_, err = meta[1].ContactRequestIncomingAccept(ctx, ownCG[1].MemberPubKey())
require.Error(t, err)
_, err = meta[1].ContactRequestIncomingAccept(ctx, ownCG[0].MemberPubKey())
require.NoError(t, err)
require.Equal(t, len(meta[1].Index().(*metadataStoreIndex).contacts), 1)
require.NotNil(t, meta[1].Index().(*metadataStoreIndex).contacts[string(contacts[0].Pk)])
require.Equal(t, protocoltypes.ContactState_ContactStateAdded, meta[1].Index().(*metadataStoreIndex).contacts[string(contacts[0].Pk)].state)
require.Equal(t, contacts[0].Pk, meta[1].Index().(*metadataStoreIndex).contacts[string(contacts[0].Pk)].contact.Pk)
require.Equal(t, contacts[0].PublicRendezvousSeed, meta[1].Index().(*metadataStoreIndex).contacts[string(contacts[0].Pk)].contact.PublicRendezvousSeed)
_, err = meta[1].ContactRequestIncomingReceived(ctx, contacts[0])
require.Error(t, err)
// Auto accept when receiving an invitation from a contact you were waiting to send an invitation to
_, err = meta[1].ContactRequestOutgoingEnqueue(ctx, contacts[3], contacts[1].Metadata)
require.NoError(t, err)
_, err = meta[1].ContactRequestIncomingReceived(ctx, contacts[3])
require.NoError(t, err)
require.Equal(t, 2, len(meta[1].Index().(*metadataStoreIndex).contacts))
require.Equal(t, meta[1].Index().(*metadataStoreIndex).contacts[string(contacts[3].Pk)].state, protocoltypes.ContactState_ContactStateAdded)
// Refuse contact
_, err = meta[1].ContactRequestIncomingReceived(ctx, contacts[2])
require.NoError(t, err)
require.Equal(t, len(meta[1].Index().(*metadataStoreIndex).contacts), 3)
require.Equal(t, meta[1].Index().(*metadataStoreIndex).contacts[string(contacts[2].Pk)].state, protocoltypes.ContactState_ContactStateReceived)
_, err = meta[1].ContactRequestIncomingDiscard(ctx, nil)
require.Error(t, err)
_, err = meta[1].ContactRequestIncomingDiscard(ctx, randPK)
require.Error(t, err)
_, err = meta[1].ContactRequestIncomingDiscard(ctx, ownCG[0].MemberPubKey())
require.Error(t, err)
_, err = meta[1].ContactRequestIncomingDiscard(ctx, ownCG[1].MemberPubKey())
require.Error(t, err)
_, err = meta[1].ContactRequestIncomingDiscard(ctx, ownCG[2].MemberPubKey())
require.NoError(t, err)
require.Equal(t, len(meta[1].Index().(*metadataStoreIndex).contacts), 3)
require.Equal(t, meta[1].Index().(*metadataStoreIndex).contacts[string(contacts[2].Pk)].state, protocoltypes.ContactState_ContactStateDiscarded)
// Allow receiving requests again after discarded
_, err = meta[1].ContactRequestIncomingReceived(ctx, contacts[2])
require.NoError(t, err)
require.Equal(t, len(meta[1].Index().(*metadataStoreIndex).contacts), 3)
require.Equal(t, meta[1].Index().(*metadataStoreIndex).contacts[string(contacts[2].Pk)].state, protocoltypes.ContactState_ContactStateReceived)
_, err = meta[1].ContactRequestOutgoingEnqueue(ctx, contacts[2], contacts[1].Metadata)
require.NoError(t, err)
require.Equal(t, len(meta[1].Index().(*metadataStoreIndex).contacts), 3)
require.Equal(t, meta[1].Index().(*metadataStoreIndex).contacts[string(contacts[2].Pk)].state, protocoltypes.ContactState_ContactStateAdded)
// Auto accept discarded requests
meta[1].Index().(*metadataStoreIndex).contacts[string(contacts[2].Pk)].state = protocoltypes.ContactState_ContactStateDiscarded
_, err = meta[1].ContactRequestOutgoingEnqueue(ctx, contacts[2], contacts[1].Metadata)
require.NoError(t, err)
require.Equal(t, len(meta[1].Index().(*metadataStoreIndex).contacts), 3)
require.Equal(t, meta[1].Index().(*metadataStoreIndex).contacts[string(contacts[2].Pk)].state, protocoltypes.ContactState_ContactStateAdded)
// Block contact
require.Equal(t, len(meta[2].Index().(*metadataStoreIndex).contacts), 0)
_, err = meta[2].ContactBlock(ctx, ownCG[2].MemberPubKey())
require.Error(t, err)
require.Equal(t, len(meta[2].Index().(*metadataStoreIndex).contacts), 0)
_, err = meta[2].ContactBlock(ctx, ownCG[0].MemberPubKey())
require.NoError(t, err)
require.Equal(t, len(meta[2].Index().(*metadataStoreIndex).contacts), 1)
require.Equal(t, meta[2].Index().(*metadataStoreIndex).contacts[string(contacts[0].Pk)].state, protocoltypes.ContactState_ContactStateBlocked)
_, err = meta[2].ContactBlock(ctx, ownCG[0].MemberPubKey())
require.Error(t, err)
require.Equal(t, len(meta[2].Index().(*metadataStoreIndex).contacts), 1)
require.Equal(t, meta[2].Index().(*metadataStoreIndex).contacts[string(contacts[0].Pk)].state, protocoltypes.ContactState_ContactStateBlocked)
// Unblock contact
_, err = meta[2].ContactUnblock(ctx, ownCG[1].MemberPubKey())
require.Error(t, err)
require.Equal(t, len(meta[2].Index().(*metadataStoreIndex).contacts), 1)
require.Equal(t, meta[2].Index().(*metadataStoreIndex).contacts[string(contacts[0].Pk)].state, protocoltypes.ContactState_ContactStateBlocked)
_, err = meta[2].ContactUnblock(ctx, ownCG[0].MemberPubKey())
require.NoError(t, err)
require.Equal(t, len(meta[2].Index().(*metadataStoreIndex).contacts), 1)
require.Equal(t, meta[2].Index().(*metadataStoreIndex).contacts[string(contacts[0].Pk)].state, protocoltypes.ContactState_ContactStateRemoved)
_, err = meta[2].ContactUnblock(ctx, ownCG[0].MemberPubKey())
require.Error(t, err)
require.Equal(t, len(meta[2].Index().(*metadataStoreIndex).contacts), 1)
require.Equal(t, meta[2].Index().(*metadataStoreIndex).contacts[string(contacts[0].Pk)].state, protocoltypes.ContactState_ContactStateRemoved)
}
func TestMetadataAliasLifecycle(t *testing.T) {
testutil.FilterSpeed(t, testutil.Slow)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
peersCount := 4
// Creates N members with M devices each within the same group
peers, _, cleanup := CreatePeersWithGroupTest(ctx, t, "/tmp/member_test", peersCount, 1)
defer cleanup()
// disclose
_, err := peers[0].GC.MetadataStore().ContactSendAliasKey(ctx)
require.Error(t, err)
g, err := peers[0].SecretStore.GetGroupForContact(peers[1].GC.MemberPubKey())
require.NoError(t, err)
cg0, err := peers[0].DB.OpenGroup(ctx, g, nil)
require.NoError(t, err)
defer cg0.Close()
require.False(t, cg0.MetadataStore().Index().(*metadataStoreIndex).ownAliasKeySent)
_, err = cg0.MetadataStore().AddDeviceToGroup(ctx)
require.NoError(t, err)
_, err = cg0.MetadataStore().ContactSendAliasKey(ctx)
require.NoError(t, err)
require.Empty(t, cg0.MetadataStore().Index().(*metadataStoreIndex).otherAliasKey)
require.True(t, cg0.MetadataStore().Index().(*metadataStoreIndex).ownAliasKeySent)
g, err = peers[1].SecretStore.GetGroupForContact(peers[0].GC.MemberPubKey())
require.NoError(t, err)
cg1, err := peers[1].DB.OpenGroup(ctx, g, nil)
require.NoError(t, err)
defer cg1.Close()
_ = cg1
// TODO: receive key on cg1 from cg0
// TODO: match received alias proof with previously disclosed key
}
func TestMetadataGroupsLifecycle(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Creates N members with M devices each within the same group
peers, _, cleanup := CreatePeersWithGroupTest(ctx, t, "/tmp/member_test", 1, 1)
defer cleanup()
api := ipfsAPIUsingMockNet(ctx, t)
ownCG, err := peers[0].DB.openAccountGroup(ctx, nil, api)
assert.NoError(t, err)
g1, _, err := NewGroupMultiMember()
require.NoError(t, err)
g2, _, err := NewGroupMultiMember()
require.NoError(t, err)
g3, _, err := NewGroupMultiMember()
require.NoError(t, err)
g1PK, err := g1.GetPubKey()
require.NoError(t, err)
g3PK, err := g3.GetPubKey()
require.NoError(t, err)
_, err = ownCG.MetadataStore().GroupJoin(ctx, g1)
require.NoError(t, err)
groups := ownCG.MetadataStore().ListMultiMemberGroups()
require.Len(t, groups, 1)
require.Equal(t, groups[0].PublicKey, g1.PublicKey)
require.Equal(t, groups[0].Secret, g1.Secret)
require.Equal(t, groups[0].SecretSig, g1.SecretSig)
require.Equal(t, groups[0].GroupType, g1.GroupType)
_, err = ownCG.MetadataStore().GroupJoin(ctx, g2)
require.NoError(t, err)
groups = ownCG.MetadataStore().ListMultiMemberGroups()
require.Len(t, groups, 2)
first, second := 0, 1
if bytes.Compare(groups[1].PublicKey, g1.PublicKey) == 0 {
first, second = 1, 0
}
require.Equal(t, groups[first].PublicKey, g1.PublicKey)
require.Equal(t, groups[first].Secret, g1.Secret)
require.Equal(t, groups[first].SecretSig, g1.SecretSig)
require.Equal(t, groups[first].GroupType, g1.GroupType)
require.Equal(t, groups[second].PublicKey, g2.PublicKey)
require.Equal(t, groups[second].Secret, g2.Secret)
require.Equal(t, groups[second].SecretSig, g2.SecretSig)
require.Equal(t, groups[second].GroupType, g2.GroupType)
_, err = ownCG.MetadataStore().GroupJoin(ctx, &protocoltypes.Group{
PublicKey: []byte("invalid_pk"),
Secret: g3.Secret,
SecretSig: g3.SecretSig,
GroupType: protocoltypes.GroupType_GroupTypeMultiMember,
})
require.Error(t, err)
groups = ownCG.MetadataStore().ListMultiMemberGroups()
require.Len(t, groups, 2)
_, err = ownCG.MetadataStore().GroupJoin(ctx, &protocoltypes.Group{
PublicKey: g3.PublicKey,
Secret: nil,
SecretSig: g3.SecretSig,
GroupType: protocoltypes.GroupType_GroupTypeMultiMember,
})
require.Error(t, err)
_, err = ownCG.MetadataStore().GroupJoin(ctx, &protocoltypes.Group{
PublicKey: g3.PublicKey,
Secret: g3.Secret,
SecretSig: []byte("invalid_sig"),
GroupType: protocoltypes.GroupType_GroupTypeMultiMember,
})
require.Error(t, err)
_, err = ownCG.MetadataStore().GroupJoin(ctx, g1)
require.Error(t, err)
groups = ownCG.MetadataStore().ListMultiMemberGroups()
require.Len(t, groups, 2)
_, err = ownCG.MetadataStore().GroupLeave(ctx, nil)
require.Error(t, err)
groups = ownCG.MetadataStore().ListMultiMemberGroups()
require.Len(t, groups, 2)
_, err = ownCG.MetadataStore().GroupLeave(ctx, g1PK)
require.NoError(t, err)
groups = ownCG.MetadataStore().ListMultiMemberGroups()
require.Len(t, groups, 1)
_, err = ownCG.MetadataStore().GroupLeave(ctx, g1PK)
require.Error(t, err)
groups = ownCG.MetadataStore().ListMultiMemberGroups()
require.Len(t, groups, 1)
_, err = ownCG.MetadataStore().GroupLeave(ctx, g3PK)
require.Error(t, err)
groups = ownCG.MetadataStore().ListMultiMemberGroups()
require.Len(t, groups, 1)
require.Equal(t, groups[0].PublicKey, g2.PublicKey)
require.Equal(t, groups[0].Secret, g2.Secret)
require.Equal(t, groups[0].SecretSig, g2.SecretSig)
require.Equal(t, groups[0].GroupType, g2.GroupType)
}
func TestFlappyMultiDevices_Basic(t *testing.T) {
testutil.FilterStabilityAndSpeed(t, testutil.Flappy, testutil.Slow)
memberCount := 2
deviceCount := 3
totalDevices := memberCount * deviceCount
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
defer cancel()
peers, _, cleanup := CreatePeersWithGroupTest(ctx, t, "/tmp/multidevices_test", memberCount, deviceCount)
defer cleanup()
api := ipfsAPIUsingMockNet(ctx, t)
// make peer index
pi := [][]int{}
for i := 0; i < memberCount; i++ {
pi = append(pi, []int{})
for j := 0; j < deviceCount; j++ {
pi[i] = append(pi[i], i*deviceCount+j)
}
}
var (
err error
meta = make([]*MetadataStore, totalDevices)
ownCG = make([]*GroupContext, totalDevices)
contacts = make([]*protocoltypes.ShareableContact, totalDevices)
listContacts map[string]*AccountContact
groups []*protocoltypes.Group
)
// Activate account group + contact request
for i, p := range peers {
// except for the latest peer devices
if (i % deviceCount) == (deviceCount - 1) {
continue
}
ownCG[i], err = p.DB.openAccountGroup(ctx, nil, api)
require.NoError(t, err)
meta[i] = ownCG[i].MetadataStore()
_, err = meta[i].ContactRequestEnable(ctx)
assert.NoError(t, err)
_, err = meta[i].ContactRequestReferenceReset(ctx)
require.NoError(t, err)
_, contacts[i] = meta[i].GetIncomingContactRequestsStatus()
require.NotNil(t, contacts[i])
contacts[i].Metadata = []byte(fmt.Sprintf("own meta %d", i))
}
syncChan := make(chan struct{})
go waitForBertyEventType(ctx, t, meta[pi[0][0]], protocoltypes.EventType_EventTypeAccountContactRequestOutgoingEnqueued, 1, syncChan)
go waitForBertyEventType(ctx, t, meta[pi[0][1]], protocoltypes.EventType_EventTypeAccountContactRequestOutgoingEnqueued, 1, syncChan)
go waitForBertyEventType(ctx, t, meta[pi[1][0]], protocoltypes.EventType_EventTypeAccountContactRequestIncomingReceived, 1, syncChan)
go waitForBertyEventType(ctx, t, meta[pi[1][1]], protocoltypes.EventType_EventTypeAccountContactRequestIncomingReceived, 1, syncChan)
// Add peers to contact
// Enqueuing outgoing
_, err = meta[pi[0][0]].ContactRequestOutgoingEnqueue(ctx, contacts[pi[1][0]], contacts[pi[0][0]].Metadata)
require.NoError(t, err)
// Marking as sent
_, err = meta[pi[0][0]].ContactRequestOutgoingSent(ctx, ownCG[pi[1][0]].MemberPubKey())
require.NoError(t, err)
// Marking as received
_, err = meta[pi[1][0]].ContactRequestIncomingReceived(ctx, contacts[pi[0][0]])
require.NoError(t, err)
// Accepting received
_, err = meta[pi[1][0]].ContactRequestIncomingAccept(ctx, ownCG[pi[0][0]].MemberPubKey())
require.NoError(t, err)
for i := 0; i < 4; i++ {
select {
case <-syncChan:
case <-ctx.Done():
require.NoError(t, ctx.Err())
}
}
// test if contact is established
listContacts = meta[pi[0][0]].ListContacts()
require.Equal(t, 1, len(listContacts))
require.NotNil(t, listContacts[string(contacts[pi[1][0]].Pk)])
listContacts = meta[pi[1][0]].ListContacts()
require.Equal(t, 1, len(listContacts))
require.NotNil(t, listContacts[string(contacts[pi[0][0]].Pk)])
// test if 2nd devices have also the contact
listContacts = meta[pi[0][1]].ListContacts()
require.Equal(t, 1, len(listContacts))
require.NotNil(t, listContacts[string(contacts[pi[1][0]].Pk)])
listContacts = meta[pi[1][1]].ListContacts()
require.Equal(t, 1, len(listContacts))
require.NotNil(t, listContacts[string(contacts[pi[0][0]].Pk)])
// Activate group for 2nd peer's 1st device
groups = meta[pi[1][0]].ListMultiMemberGroups()
require.Len(t, groups, 0)
go waitForBertyEventType(ctx, t, meta[pi[1][0]], protocoltypes.EventType_EventTypeAccountGroupJoined, 1, syncChan)
go waitForBertyEventType(ctx, t, meta[pi[1][1]], protocoltypes.EventType_EventTypeAccountGroupJoined, 1, syncChan)
_, err = meta[pi[1][0]].GroupJoin(ctx, peers[pi[1][0]].GC.group)
require.NoError(t, err)
for i := 0; i < 2; i++ {
select {
case <-syncChan:
case <-ctx.Done():
require.NoError(t, ctx.Err())
}
}
groups = meta[pi[1][0]].ListMultiMemberGroups()
require.Len(t, groups, 1)
// Test if other devices have the group too
groups = meta[pi[1][1]].ListMultiMemberGroups()
require.Len(t, groups, 1)
// Check if a account group activate after the contact request is synchronized
// Activate the 2nd peer's latest device account group
ownCG[pi[1][2]], err = peers[pi[1][2]].DB.openAccountGroup(ctx, nil, api)
require.NoError(t, err)
meta[pi[1][2]] = ownCG[pi[1][2]].MetadataStore()
// wait for replication db
<-time.After(time.Second * 2)
listContacts = meta[pi[1][2]].ListContacts()
require.Equal(t, 1, len(listContacts))
require.NotNil(t, listContacts[string(contacts[0].Pk)])
// Test for group
groups = meta[pi[1][2]].ListMultiMemberGroups()
require.Len(t, groups, 1)
}