aboutsummaryrefslogtreecommitdiffstats
path: root/whisper/whisperv6/whisper_test.go
blob: 99e5f0bbb439bfb798ce173e85fcf442ca38899d (plain) (blame)
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
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
// Copyright 2016 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

package whisperv6

import (
    "bytes"
    "crypto/ecdsa"
    "crypto/sha256"
    mrand "math/rand"
    "testing"
    "time"

    "github.com/ethereum/go-ethereum/common"
    "golang.org/x/crypto/pbkdf2"
)

func TestWhisperBasic(t *testing.T) {
    w := New(&DefaultConfig)
    p := w.Protocols()
    shh := p[0]
    if shh.Name != ProtocolName {
        t.Fatalf("failed Protocol Name: %v.", shh.Name)
    }
    if uint64(shh.Version) != ProtocolVersion {
        t.Fatalf("failed Protocol Version: %v.", shh.Version)
    }
    if shh.Length != NumberOfMessageCodes {
        t.Fatalf("failed Protocol Length: %v.", shh.Length)
    }
    if shh.Run == nil {
        t.Fatalf("failed shh.Run.")
    }
    if uint64(w.Version()) != ProtocolVersion {
        t.Fatalf("failed whisper Version: %v.", shh.Version)
    }
    if w.GetFilter("non-existent") != nil {
        t.Fatalf("failed GetFilter.")
    }

    peerID := make([]byte, 64)
    mrand.Read(peerID)
    peer, _ := w.getPeer(peerID)
    if peer != nil {
        t.Fatal("found peer for random key.")
    }
    if err := w.AllowP2PMessagesFromPeer(peerID); err == nil {
        t.Fatalf("failed MarkPeerTrusted.")
    }
    exist := w.HasSymKey("non-existing")
    if exist {
        t.Fatalf("failed HasSymKey.")
    }
    key, err := w.GetSymKey("non-existing")
    if err == nil {
        t.Fatalf("failed GetSymKey(non-existing): false positive.")
    }
    if key != nil {
        t.Fatalf("failed GetSymKey: false positive.")
    }
    mail := w.Envelopes()
    if len(mail) != 0 {
        t.Fatalf("failed w.Envelopes().")
    }
    m := w.Messages("non-existent")
    if len(m) != 0 {
        t.Fatalf("failed w.Messages.")
    }

    derived := pbkdf2.Key([]byte(peerID), nil, 65356, aesKeyLength, sha256.New)
    if !validateDataIntegrity(derived, aesKeyLength) {
        t.Fatalf("failed validateSymmetricKey with param = %v.", derived)
    }
    if containsOnlyZeros(derived) {
        t.Fatalf("failed containsOnlyZeros with param = %v.", derived)
    }

    buf := []byte{0xFF, 0xE5, 0x80, 0x2, 0}
    le := bytesToUintLittleEndian(buf)
    be := BytesToUintBigEndian(buf)
    if le != uint64(0x280e5ff) {
        t.Fatalf("failed bytesToIntLittleEndian: %d.", le)
    }
    if be != uint64(0xffe5800200) {
        t.Fatalf("failed BytesToIntBigEndian: %d.", be)
    }

    id, err := w.NewKeyPair()
    if err != nil {
        t.Fatalf("failed to generate new key pair: %s.", err)
    }
    pk, err := w.GetPrivateKey(id)
    if err != nil {
        t.Fatalf("failed to retrieve new key pair: %s.", err)
    }
    if !validatePrivateKey(pk) {
        t.Fatalf("failed validatePrivateKey: %v.", pk)
    }
    if !ValidatePublicKey(&pk.PublicKey) {
        t.Fatalf("failed ValidatePublicKey: %v.", pk)
    }
}

func TestWhisperAsymmetricKeyImport(t *testing.T) {
    var (
        w           = New(&DefaultConfig)
        privateKeys []*ecdsa.PrivateKey
    )

    for i := 0; i < 50; i++ {
        id, err := w.NewKeyPair()
        if err != nil {
            t.Fatalf("could not generate key: %v", err)
        }

        pk, err := w.GetPrivateKey(id)
        if err != nil {
            t.Fatalf("could not export private key: %v", err)
        }

        privateKeys = append(privateKeys, pk)

        if !w.DeleteKeyPair(id) {
            t.Fatalf("could not delete private key")
        }
    }

    for _, pk := range privateKeys {
        if _, err := w.AddKeyPair(pk); err != nil {
            t.Fatalf("could not import private key: %v", err)
        }
    }
}

func TestWhisperIdentityManagement(t *testing.T) {
    w := New(&DefaultConfig)
    id1, err := w.NewKeyPair()
    if err != nil {
        t.Fatalf("failed to generate new key pair: %s.", err)
    }
    id2, err := w.NewKeyPair()
    if err != nil {
        t.Fatalf("failed to generate new key pair: %s.", err)
    }
    pk1, err := w.GetPrivateKey(id1)
    if err != nil {
        t.Fatalf("failed to retrieve the key pair: %s.", err)
    }
    pk2, err := w.GetPrivateKey(id2)
    if err != nil {
        t.Fatalf("failed to retrieve the key pair: %s.", err)
    }

    if !w.HasKeyPair(id1) {
        t.Fatalf("failed HasIdentity(pk1).")
    }
    if !w.HasKeyPair(id2) {
        t.Fatalf("failed HasIdentity(pk2).")
    }
    if pk1 == nil {
        t.Fatalf("failed GetIdentity(pk1).")
    }
    if pk2 == nil {
        t.Fatalf("failed GetIdentity(pk2).")
    }

    if !validatePrivateKey(pk1) {
        t.Fatalf("pk1 is invalid.")
    }
    if !validatePrivateKey(pk2) {
        t.Fatalf("pk2 is invalid.")
    }

    // Delete one identity
    done := w.DeleteKeyPair(id1)
    if !done {
        t.Fatalf("failed to delete id1.")
    }
    pk1, err = w.GetPrivateKey(id1)
    if err == nil {
        t.Fatalf("retrieve the key pair: false positive.")
    }
    pk2, err = w.GetPrivateKey(id2)
    if err != nil {
        t.Fatalf("failed to retrieve the key pair: %s.", err)
    }
    if w.HasKeyPair(id1) {
        t.Fatalf("failed DeleteIdentity(pub1): still exist.")
    }
    if !w.HasKeyPair(id2) {
        t.Fatalf("failed DeleteIdentity(pub1): pub2 does not exist.")
    }
    if pk1 != nil {
        t.Fatalf("failed DeleteIdentity(pub1): first key still exist.")
    }
    if pk2 == nil {
        t.Fatalf("failed DeleteIdentity(pub1): second key does not exist.")
    }

    // Delete again non-existing identity
    done = w.DeleteKeyPair(id1)
    if done {
        t.Fatalf("delete id1: false positive.")
    }
    pk1, err = w.GetPrivateKey(id1)
    if err == nil {
        t.Fatalf("retrieve the key pair: false positive.")
    }
    pk2, err = w.GetPrivateKey(id2)
    if err != nil {
        t.Fatalf("failed to retrieve the key pair: %s.", err)
    }
    if w.HasKeyPair(id1) {
        t.Fatalf("failed delete non-existing identity: exist.")
    }
    if !w.HasKeyPair(id2) {
        t.Fatalf("failed delete non-existing identity: pub2 does not exist.")
    }
    if pk1 != nil {
        t.Fatalf("failed delete non-existing identity: first key exist.")
    }
    if pk2 == nil {
        t.Fatalf("failed delete non-existing identity: second key does not exist.")
    }

    // Delete second identity
    done = w.DeleteKeyPair(id2)
    if !done {
        t.Fatalf("failed to delete id2.")
    }
    pk1, err = w.GetPrivateKey(id1)
    if err == nil {
        t.Fatalf("retrieve the key pair: false positive.")
    }
    pk2, err = w.GetPrivateKey(id2)
    if err == nil {
        t.Fatalf("retrieve the key pair: false positive.")
    }
    if w.HasKeyPair(id1) {
        t.Fatalf("failed delete second identity: first identity exist.")
    }
    if w.HasKeyPair(id2) {
        t.Fatalf("failed delete second identity: still exist.")
    }
    if pk1 != nil {
        t.Fatalf("failed delete second identity: first key exist.")
    }
    if pk2 != nil {
        t.Fatalf("failed delete second identity: second key exist.")
    }
}

func TestWhisperSymKeyManagement(t *testing.T) {
    InitSingleTest()

    var err error
    var k1, k2 []byte
    w := New(&DefaultConfig)
    id1 := string("arbitrary-string-1")
    id2 := string("arbitrary-string-2")

    id1, err = w.GenerateSymKey()
    if err != nil {
        t.Fatalf("failed GenerateSymKey with seed %d: %s.", seed, err)
    }

    k1, err = w.GetSymKey(id1)
    if err != nil {
        t.Fatalf("failed GetSymKey(id1).")
    }
    k2, err = w.GetSymKey(id2)
    if err == nil {
        t.Fatalf("failed GetSymKey(id2): false positive.")
    }
    if !w.HasSymKey(id1) {
        t.Fatalf("failed HasSymKey(id1).")
    }
    if w.HasSymKey(id2) {
        t.Fatalf("failed HasSymKey(id2): false positive.")
    }
    if k1 == nil {
        t.Fatalf("first key does not exist.")
    }
    if k2 != nil {
        t.Fatalf("second key still exist.")
    }

    // add existing id, nothing should change
    randomKey := make([]byte, aesKeyLength)
    mrand.Read(randomKey)
    id1, err = w.AddSymKeyDirect(randomKey)
    if err != nil {
        t.Fatalf("failed AddSymKey with seed %d: %s.", seed, err)
    }

    k1, err = w.GetSymKey(id1)
    if err != nil {
        t.Fatalf("failed w.GetSymKey(id1).")
    }
    k2, err = w.GetSymKey(id2)
    if err == nil {
        t.Fatalf("failed w.GetSymKey(id2): false positive.")
    }
    if !w.HasSymKey(id1) {
        t.Fatalf("failed w.HasSymKey(id1).")
    }
    if w.HasSymKey(id2) {
        t.Fatalf("failed w.HasSymKey(id2): false positive.")
    }
    if k1 == nil {
        t.Fatalf("first key does not exist.")
    }
    if !bytes.Equal(k1, randomKey) {
        t.Fatalf("k1 != randomKey.")
    }
    if k2 != nil {
        t.Fatalf("second key already exist.")
    }

    id2, err = w.AddSymKeyDirect(randomKey)
    if err != nil {
        t.Fatalf("failed AddSymKey(id2) with seed %d: %s.", seed, err)
    }
    k1, err = w.GetSymKey(id1)
    if err != nil {
        t.Fatalf("failed w.GetSymKey(id1).")
    }
    k2, err = w.GetSymKey(id2)
    if err != nil {
        t.Fatalf("failed w.GetSymKey(id2).")
    }
    if !w.HasSymKey(id1) {
        t.Fatalf("HasSymKey(id1) failed.")
    }
    if !w.HasSymKey(id2) {
        t.Fatalf("HasSymKey(id2) failed.")
    }
    if k1 == nil {
        t.Fatalf("k1 does not exist.")
    }
    if k2 == nil {
        t.Fatalf("k2 does not exist.")
    }
    if !bytes.Equal(k1, k2) {
        t.Fatalf("k1 != k2.")
    }
    if !bytes.Equal(k1, randomKey) {
        t.Fatalf("k1 != randomKey.")
    }
    if len(k1) != aesKeyLength {
        t.Fatalf("wrong length of k1.")
    }
    if len(k2) != aesKeyLength {
        t.Fatalf("wrong length of k2.")
    }

    w.DeleteSymKey(id1)
    k1, err = w.GetSymKey(id1)
    if err == nil {
        t.Fatalf("failed w.GetSymKey(id1): false positive.")
    }
    if k1 != nil {
        t.Fatalf("failed GetSymKey(id1): false positive.")
    }
    k2, err = w.GetSymKey(id2)
    if err != nil {
        t.Fatalf("failed w.GetSymKey(id2).")
    }
    if w.HasSymKey(id1) {
        t.Fatalf("failed to delete first key: still exist.")
    }
    if !w.HasSymKey(id2) {
        t.Fatalf("failed to delete first key: second key does not exist.")
    }
    if k1 != nil {
        t.Fatalf("failed to delete first key.")
    }
    if k2 == nil {
        t.Fatalf("failed to delete first key: second key is nil.")
    }

    w.DeleteSymKey(id1)
    w.DeleteSymKey(id2)
    k1, err = w.GetSymKey(id1)
    if err == nil {
        t.Fatalf("failed w.GetSymKey(id1): false positive.")
    }
    k2, err = w.GetSymKey(id2)
    if err == nil {
        t.Fatalf("failed w.GetSymKey(id2): false positive.")
    }
    if k1 != nil || k2 != nil {
        t.Fatalf("k1 or k2 is not nil")
    }
    if w.HasSymKey(id1) {
        t.Fatalf("failed to delete second key: first key exist.")
    }
    if w.HasSymKey(id2) {
        t.Fatalf("failed to delete second key: still exist.")
    }
    if k1 != nil {
        t.Fatalf("failed to delete second key: first key is not nil.")
    }
    if k2 != nil {
        t.Fatalf("failed to delete second key: second key is not nil.")
    }

    randomKey = make([]byte, aesKeyLength+1)
    mrand.Read(randomKey)
    _, err = w.AddSymKeyDirect(randomKey)
    if err == nil {
        t.Fatalf("added the key with wrong size, seed %d.", seed)
    }

    const password = "arbitrary data here"
    id1, err = w.AddSymKeyFromPassword(password)
    if err != nil {
        t.Fatalf("failed AddSymKeyFromPassword(id1) with seed %d: %s.", seed, err)
    }
    id2, err = w.AddSymKeyFromPassword(password)
    if err != nil {
        t.Fatalf("failed AddSymKeyFromPassword(id2) with seed %d: %s.", seed, err)
    }
    k1, err = w.GetSymKey(id1)
    if err != nil {
        t.Fatalf("failed w.GetSymKey(id1).")
    }
    k2, err = w.GetSymKey(id2)
    if err != nil {
        t.Fatalf("failed w.GetSymKey(id2).")
    }
    if !w.HasSymKey(id1) {
        t.Fatalf("HasSymKey(id1) failed.")
    }
    if !w.HasSymKey(id2) {
        t.Fatalf("HasSymKey(id2) failed.")
    }
    if !validateDataIntegrity(k2, aesKeyLength) {
        t.Fatalf("key validation failed.")
    }
    if !bytes.Equal(k1, k2) {
        t.Fatalf("k1 != k2.")
    }
}

func TestExpiry(t *testing.T) {
    InitSingleTest()

    w := New(&DefaultConfig)
    w.SetMinimumPowTest(0.0000001)
    defer w.SetMinimumPowTest(DefaultMinimumPoW)
    w.Start(nil)
    defer w.Stop()

    params, err := generateMessageParams()
    if err != nil {
        t.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err)
    }

    params.TTL = 1
    msg, err := NewSentMessage(params)
    if err != nil {
        t.Fatalf("failed to create new message with seed %d: %s.", seed, err)
    }
    env, err := msg.Wrap(params)
    if err != nil {
        t.Fatalf("failed Wrap with seed %d: %s.", seed, err)
    }

    err = w.Send(env)
    if err != nil {
        t.Fatalf("failed to send envelope with seed %d: %s.", seed, err)
    }

    // wait till received or timeout
    var received, expired bool
    for j := 0; j < 20; j++ {
        time.Sleep(100 * time.Millisecond)
        if len(w.Envelopes()) > 0 {
            received = true
            break
        }
    }

    if !received {
        t.Fatalf("did not receive the sent envelope, seed: %d.", seed)
    }

    // wait till expired or timeout
    for j := 0; j < 20; j++ {
        time.Sleep(100 * time.Millisecond)
        if len(w.Envelopes()) == 0 {
            expired = true
            break
        }
    }

    if !expired {
        t.Fatalf("expire failed, seed: %d.", seed)
    }
}

func TestCustomization(t *testing.T) {
    InitSingleTest()

    w := New(&DefaultConfig)
    defer w.SetMinimumPowTest(DefaultMinimumPoW)
    defer w.SetMaxMessageSize(DefaultMaxMessageSize)
    w.Start(nil)
    defer w.Stop()

    const smallPoW = 0.00001

    f, err := generateFilter(t, true)
    if err != nil {
        t.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err)
    }
    params, err := generateMessageParams()
    if err != nil {
        t.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err)
    }

    params.KeySym = f.KeySym
    params.Topic = BytesToTopic(f.Topics[2])
    params.PoW = smallPoW
    params.TTL = 3600 * 24 // one day
    msg, err := NewSentMessage(params)
    if err != nil {
        t.Fatalf("failed to create new message with seed %d: %s.", seed, err)
    }
    env, err := msg.Wrap(params)
    if err != nil {
        t.Fatalf("failed Wrap with seed %d: %s.", seed, err)
    }

    err = w.Send(env)
    if err == nil {
        t.Fatalf("successfully sent envelope with PoW %.06f, false positive (seed %d).", env.PoW(), seed)
    }

    w.SetMinimumPowTest(smallPoW / 2)
    err = w.Send(env)
    if err != nil {
        t.Fatalf("failed to send envelope with seed %d: %s.", seed, err)
    }

    params.TTL++
    msg, err = NewSentMessage(params)
    if err != nil {
        t.Fatalf("failed to create new message with seed %d: %s.", seed, err)
    }
    env, err = msg.Wrap(params)
    if err != nil {
        t.Fatalf("failed Wrap with seed %d: %s.", seed, err)
    }
    w.SetMaxMessageSize(uint32(env.size() - 1))
    err = w.Send(env)
    if err == nil {
        t.Fatalf("successfully sent oversized envelope (seed %d): false positive.", seed)
    }

    w.SetMaxMessageSize(DefaultMaxMessageSize)
    err = w.Send(env)
    if err != nil {
        t.Fatalf("failed to send second envelope with seed %d: %s.", seed, err)
    }

    // wait till received or timeout
    var received bool
    for j := 0; j < 20; j++ {
        time.Sleep(100 * time.Millisecond)
        if len(w.Envelopes()) > 1 {
            received = true
            break
        }
    }

    if !received {
        t.Fatalf("did not receive the sent envelope, seed: %d.", seed)
    }

    // check w.messages()
    id, err := w.Subscribe(f)
    if err != nil {
        t.Fatalf("failed subscribe with seed %d: %s.", seed, err)
    }
    time.Sleep(5 * time.Millisecond)
    mail := f.Retrieve()
    if len(mail) > 0 {
        t.Fatalf("received premature mail")
    }

    mail = w.Messages(id)
    if len(mail) != 2 {
        t.Fatalf("failed to get whisper messages")
    }
}

func TestSymmetricSendCycle(t *testing.T) {
    InitSingleTest()

    w := New(&DefaultConfig)
    defer w.SetMinimumPowTest(DefaultMinimumPoW)
    defer w.SetMaxMessageSize(DefaultMaxMessageSize)
    w.Start(nil)
    defer w.Stop()

    filter1, err := generateFilter(t, true)
    if err != nil {
        t.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err)
    }
    filter1.PoW = DefaultMinimumPoW

    // Copy the first filter since some of its fields
    // are randomly gnerated.
    filter2 := &Filter{
        KeySym:   filter1.KeySym,
        Topics:   filter1.Topics,
        PoW:      filter1.PoW,
        AllowP2P: filter1.AllowP2P,
        Messages: make(map[common.Hash]*ReceivedMessage),
    }

    params, err := generateMessageParams()
    if err != nil {
        t.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err)
    }

    filter1.Src = &params.Src.PublicKey
    filter2.Src = &params.Src.PublicKey

    params.KeySym = filter1.KeySym
    params.Topic = BytesToTopic(filter1.Topics[2])
    params.PoW = filter1.PoW
    params.WorkTime = 10
    params.TTL = 50
    msg, err := NewSentMessage(params)
    if err != nil {
        t.Fatalf("failed to create new message with seed %d: %s.", seed, err)
    }
    env, err := msg.Wrap(params)
    if err != nil {
        t.Fatalf("failed Wrap with seed %d: %s.", seed, err)
    }

    _, err = w.Subscribe(filter1)
    if err != nil {
        t.Fatalf("failed subscribe 1 with seed %d: %s.", seed, err)
    }

    _, err = w.Subscribe(filter2)
    if err != nil {
        t.Fatalf("failed subscribe 2 with seed %d: %s.", seed, err)
    }

    err = w.Send(env)
    if err != nil {
        t.Fatalf("Failed sending envelope with PoW %.06f (seed %d): %s", env.PoW(), seed, err)
    }

    // wait till received or timeout
    var received bool
    for j := 0; j < 200; j++ {
        time.Sleep(10 * time.Millisecond)
        if len(w.Envelopes()) > 0 {
            received = true
            break
        }
    }

    if !received {
        t.Fatalf("did not receive the sent envelope, seed: %d.", seed)
    }

    // check w.messages()
    time.Sleep(5 * time.Millisecond)
    mail1 := filter1.Retrieve()
    mail2 := filter2.Retrieve()
    if len(mail2) == 0 {
        t.Fatalf("did not receive any email for filter 2")
    }
    if len(mail1) == 0 {
        t.Fatalf("did not receive any email for filter 1")
    }

}

func TestSymmetricSendWithoutAKey(t *testing.T) {
    InitSingleTest()

    w := New(&DefaultConfig)
    defer w.SetMinimumPowTest(DefaultMinimumPoW)
    defer w.SetMaxMessageSize(DefaultMaxMessageSize)
    w.Start(nil)
    defer w.Stop()

    filter, err := generateFilter(t, true)
    if err != nil {
        t.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err)
    }
    filter.PoW = DefaultMinimumPoW

    params, err := generateMessageParams()
    if err != nil {
        t.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err)
    }

    filter.Src = nil

    params.KeySym = filter.KeySym
    params.Topic = BytesToTopic(filter.Topics[2])
    params.PoW = filter.PoW
    params.WorkTime = 10
    params.TTL = 50
    msg, err := NewSentMessage(params)
    if err != nil {
        t.Fatalf("failed to create new message with seed %d: %s.", seed, err)
    }
    env, err := msg.Wrap(params)
    if err != nil {
        t.Fatalf("failed Wrap with seed %d: %s.", seed, err)
    }

    _, err = w.Subscribe(filter)
    if err != nil {
        t.Fatalf("failed subscribe 1 with seed %d: %s.", seed, err)
    }

    err = w.Send(env)
    if err != nil {
        t.Fatalf("Failed sending envelope with PoW %.06f (seed %d): %s", env.PoW(), seed, err)
    }

    // wait till received or timeout
    var received bool
    for j := 0; j < 200; j++ {
        time.Sleep(10 * time.Millisecond)
        if len(w.Envelopes()) > 0 {
            received = true
            break
        }
    }

    if !received {
        t.Fatalf("did not receive the sent envelope, seed: %d.", seed)
    }

    // check w.messages()
    time.Sleep(5 * time.Millisecond)
    mail := filter.Retrieve()
    if len(mail) == 0 {
        t.Fatalf("did not receive message in spite of not setting a public key")
    }
}

func TestSymmetricSendKeyMismatch(t *testing.T) {
    InitSingleTest()

    w := New(&DefaultConfig)
    defer w.SetMinimumPowTest(DefaultMinimumPoW)
    defer w.SetMaxMessageSize(DefaultMaxMessageSize)
    w.Start(nil)
    defer w.Stop()

    filter, err := generateFilter(t, true)
    if err != nil {
        t.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err)
    }
    filter.PoW = DefaultMinimumPoW

    params, err := generateMessageParams()
    if err != nil {
        t.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err)
    }

    params.KeySym = filter.KeySym
    params.Topic = BytesToTopic(filter.Topics[2])
    params.PoW = filter.PoW
    params.WorkTime = 10
    params.TTL = 50
    msg, err := NewSentMessage(params)
    if err != nil {
        t.Fatalf("failed to create new message with seed %d: %s.", seed, err)
    }
    env, err := msg.Wrap(params)
    if err != nil {
        t.Fatalf("failed Wrap with seed %d: %s.", seed, err)
    }

    _, err = w.Subscribe(filter)
    if err != nil {
        t.Fatalf("failed subscribe 1 with seed %d: %s.", seed, err)
    }

    err = w.Send(env)
    if err != nil {
        t.Fatalf("Failed sending envelope with PoW %.06f (seed %d): %s", env.PoW(), seed, err)
    }

    // wait till received or timeout
    var received bool
    for j := 0; j < 200; j++ {
        time.Sleep(10 * time.Millisecond)
        if len(w.Envelopes()) > 0 {
            received = true
            break
        }
    }

    if !received {
        t.Fatalf("did not receive the sent envelope, seed: %d.", seed)
    }

    // check w.messages()
    time.Sleep(5 * time.Millisecond)
    mail := filter.Retrieve()
    if len(mail) > 0 {
        t.Fatalf("received a message when keys weren't matching")
    }
}

func TestBloom(t *testing.T) {
    topic := TopicType{0, 0, 255, 6}
    b := TopicToBloom(topic)
    x := make([]byte, bloomFilterSize)
    x[0] = byte(1)
    x[32] = byte(1)
    x[bloomFilterSize-1] = byte(128)
    if !bloomFilterMatch(x, b) || !bloomFilterMatch(b, x) {
        t.Fatalf("bloom filter does not match the mask")
    }

    _, err := mrand.Read(b)
    if err != nil {
        t.Fatalf("math rand error")
    }
    _, err = mrand.Read(x)
    if err != nil {
        t.Fatalf("math rand error")
    }
    if !bloomFilterMatch(b, b) {
        t.Fatalf("bloom filter does not match self")
    }
    x = addBloom(x, b)
    if !bloomFilterMatch(x, b) {
        t.Fatalf("bloom filter does not match combined bloom")
    }
    if !isFullNode(nil) {
        t.Fatalf("isFullNode did not recognize nil as full node")
    }
    x[17] = 254
    if isFullNode(x) {
        t.Fatalf("isFullNode false positive")
    }
    for i := 0; i < bloomFilterSize; i++ {
        b[i] = byte(255)
    }
    if !isFullNode(b) {
        t.Fatalf("isFullNode false negative")
    }
    if bloomFilterMatch(x, b) {
        t.Fatalf("bloomFilterMatch false positive")
    }
    if !bloomFilterMatch(b, x) {
        t.Fatalf("bloomFilterMatch false negative")
    }

    w := New(&DefaultConfig)
    f := w.BloomFilter()
    if f != nil {
        t.Fatalf("wrong bloom on creation")
    }
    err = w.SetBloomFilter(x)
    if err != nil {
        t.Fatalf("failed to set bloom filter: %s", err)
    }
    f = w.BloomFilter()
    if !bloomFilterMatch(f, x) || !bloomFilterMatch(x, f) {
        t.Fatalf("retireved wrong bloom filter")
    }
}