github.com/bcskill/bcschain/v3@v3.4.9-beta2/whisper/whisperv6/whisper_test.go (about)

     1  // Copyright 2016 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package whisperv6
    18  
    19  import (
    20  	"bytes"
    21  	"context"
    22  	"crypto/ecdsa"
    23  	"crypto/sha256"
    24  	mrand "math/rand"
    25  	"testing"
    26  	"time"
    27  
    28  	"github.com/bcskill/bcschain/v3/common"
    29  	"golang.org/x/crypto/pbkdf2"
    30  )
    31  
    32  func TestWhisperBasic(t *testing.T) {
    33  	w := New(&DefaultConfig)
    34  	p := w.Protocols()
    35  	shh := p[0]
    36  	if shh.Name != ProtocolName {
    37  		t.Fatalf("failed Protocol Name: %v.", shh.Name)
    38  	}
    39  	if uint64(shh.Version) != ProtocolVersion {
    40  		t.Fatalf("failed Protocol Version: %v.", shh.Version)
    41  	}
    42  	if shh.Length != NumberOfMessageCodes {
    43  		t.Fatalf("failed Protocol Length: %v.", shh.Length)
    44  	}
    45  	if shh.Run == nil {
    46  		t.Fatalf("failed shh.Run.")
    47  	}
    48  	if uint64(w.Version()) != ProtocolVersion {
    49  		t.Fatalf("failed whisper Version: %v.", shh.Version)
    50  	}
    51  	if w.GetFilter("non-existent") != nil {
    52  		t.Fatalf("failed GetFilter.")
    53  	}
    54  
    55  	peerID := make([]byte, 64)
    56  	mrand.Read(peerID)
    57  	peer, _ := w.getPeer(peerID)
    58  	if peer != nil {
    59  		t.Fatal("found peer for random key.")
    60  	}
    61  	if err := w.AllowP2PMessagesFromPeer(peerID); err == nil {
    62  		t.Fatalf("failed MarkPeerTrusted.")
    63  	}
    64  	exist := w.HasSymKey("non-existing")
    65  	if exist {
    66  		t.Fatalf("failed HasSymKey.")
    67  	}
    68  	key, err := w.GetSymKey("non-existing")
    69  	if err == nil {
    70  		t.Fatalf("failed GetSymKey(non-existing): false positive.")
    71  	}
    72  	if key != nil {
    73  		t.Fatalf("failed GetSymKey: false positive.")
    74  	}
    75  	mail := w.Envelopes()
    76  	if len(mail) != 0 {
    77  		t.Fatalf("failed w.Envelopes().")
    78  	}
    79  	m := w.Messages("non-existent")
    80  	if len(m) != 0 {
    81  		t.Fatalf("failed w.Messages.")
    82  	}
    83  
    84  	derived := pbkdf2.Key([]byte(peerID), nil, 65356, aesKeyLength, sha256.New)
    85  	if !validateDataIntegrity(derived, aesKeyLength) {
    86  		t.Fatalf("failed validateSymmetricKey with param = %v.", derived)
    87  	}
    88  	if containsOnlyZeros(derived) {
    89  		t.Fatalf("failed containsOnlyZeros with param = %v.", derived)
    90  	}
    91  
    92  	buf := []byte{0xFF, 0xE5, 0x80, 0x2, 0}
    93  	le := bytesToUintLittleEndian(buf)
    94  	be := BytesToUintBigEndian(buf)
    95  	if le != uint64(0x280e5ff) {
    96  		t.Fatalf("failed bytesToIntLittleEndian: %d.", le)
    97  	}
    98  	if be != uint64(0xffe5800200) {
    99  		t.Fatalf("failed BytesToIntBigEndian: %d.", be)
   100  	}
   101  
   102  	id, err := w.NewKeyPair()
   103  	if err != nil {
   104  		t.Fatalf("failed to generate new key pair: %s.", err)
   105  	}
   106  	pk, err := w.GetPrivateKey(id)
   107  	if err != nil {
   108  		t.Fatalf("failed to retrieve new key pair: %s.", err)
   109  	}
   110  	if !validatePrivateKey(pk) {
   111  		t.Fatalf("failed validatePrivateKey: %v.", pk)
   112  	}
   113  	if !ValidatePublicKey(&pk.PublicKey) {
   114  		t.Fatalf("failed ValidatePublicKey: %v.", pk)
   115  	}
   116  }
   117  
   118  func TestWhisperAsymmetricKeyImport(t *testing.T) {
   119  	var (
   120  		w           = New(&DefaultConfig)
   121  		privateKeys []*ecdsa.PrivateKey
   122  	)
   123  
   124  	for i := 0; i < 50; i++ {
   125  		id, err := w.NewKeyPair()
   126  		if err != nil {
   127  			t.Fatalf("could not generate key: %v", err)
   128  		}
   129  
   130  		pk, err := w.GetPrivateKey(id)
   131  		if err != nil {
   132  			t.Fatalf("could not export private key: %v", err)
   133  		}
   134  
   135  		privateKeys = append(privateKeys, pk)
   136  
   137  		if !w.DeleteKeyPair(id) {
   138  			t.Fatalf("could not delete private key")
   139  		}
   140  	}
   141  
   142  	for _, pk := range privateKeys {
   143  		if _, err := w.AddKeyPair(pk); err != nil {
   144  			t.Fatalf("could not import private key: %v", err)
   145  		}
   146  	}
   147  }
   148  
   149  func TestWhisperIdentityManagement(t *testing.T) {
   150  	w := New(&DefaultConfig)
   151  	id1, err := w.NewKeyPair()
   152  	if err != nil {
   153  		t.Fatalf("failed to generate new key pair: %s.", err)
   154  	}
   155  	id2, err := w.NewKeyPair()
   156  	if err != nil {
   157  		t.Fatalf("failed to generate new key pair: %s.", err)
   158  	}
   159  	pk1, err := w.GetPrivateKey(id1)
   160  	if err != nil {
   161  		t.Fatalf("failed to retrieve the key pair: %s.", err)
   162  	}
   163  	pk2, err := w.GetPrivateKey(id2)
   164  	if err != nil {
   165  		t.Fatalf("failed to retrieve the key pair: %s.", err)
   166  	}
   167  
   168  	if !w.HasKeyPair(id1) {
   169  		t.Fatalf("failed HasIdentity(pk1).")
   170  	}
   171  	if !w.HasKeyPair(id2) {
   172  		t.Fatalf("failed HasIdentity(pk2).")
   173  	}
   174  	if pk1 == nil {
   175  		t.Fatalf("failed GetIdentity(pk1).")
   176  	}
   177  	if pk2 == nil {
   178  		t.Fatalf("failed GetIdentity(pk2).")
   179  	}
   180  
   181  	if !validatePrivateKey(pk1) {
   182  		t.Fatalf("pk1 is invalid.")
   183  	}
   184  	if !validatePrivateKey(pk2) {
   185  		t.Fatalf("pk2 is invalid.")
   186  	}
   187  
   188  	// Delete one identity
   189  	done := w.DeleteKeyPair(id1)
   190  	if !done {
   191  		t.Fatalf("failed to delete id1.")
   192  	}
   193  	pk1, err = w.GetPrivateKey(id1)
   194  	if err == nil {
   195  		t.Fatalf("retrieve the key pair: false positive.")
   196  	}
   197  	pk2, err = w.GetPrivateKey(id2)
   198  	if err != nil {
   199  		t.Fatalf("failed to retrieve the key pair: %s.", err)
   200  	}
   201  	if w.HasKeyPair(id1) {
   202  		t.Fatalf("failed DeleteIdentity(pub1): still exist.")
   203  	}
   204  	if !w.HasKeyPair(id2) {
   205  		t.Fatalf("failed DeleteIdentity(pub1): pub2 does not exist.")
   206  	}
   207  	if pk1 != nil {
   208  		t.Fatalf("failed DeleteIdentity(pub1): first key still exist.")
   209  	}
   210  	if pk2 == nil {
   211  		t.Fatalf("failed DeleteIdentity(pub1): second key does not exist.")
   212  	}
   213  
   214  	// Delete again non-existing identity
   215  	done = w.DeleteKeyPair(id1)
   216  	if done {
   217  		t.Fatalf("delete id1: false positive.")
   218  	}
   219  	pk1, err = w.GetPrivateKey(id1)
   220  	if err == nil {
   221  		t.Fatalf("retrieve the key pair: false positive.")
   222  	}
   223  	pk2, err = w.GetPrivateKey(id2)
   224  	if err != nil {
   225  		t.Fatalf("failed to retrieve the key pair: %s.", err)
   226  	}
   227  	if w.HasKeyPair(id1) {
   228  		t.Fatalf("failed delete non-existing identity: exist.")
   229  	}
   230  	if !w.HasKeyPair(id2) {
   231  		t.Fatalf("failed delete non-existing identity: pub2 does not exist.")
   232  	}
   233  	if pk1 != nil {
   234  		t.Fatalf("failed delete non-existing identity: first key exist.")
   235  	}
   236  	if pk2 == nil {
   237  		t.Fatalf("failed delete non-existing identity: second key does not exist.")
   238  	}
   239  
   240  	// Delete second identity
   241  	done = w.DeleteKeyPair(id2)
   242  	if !done {
   243  		t.Fatalf("failed to delete id2.")
   244  	}
   245  	pk1, err = w.GetPrivateKey(id1)
   246  	if err == nil {
   247  		t.Fatalf("retrieve the key pair: false positive.")
   248  	}
   249  	pk2, err = w.GetPrivateKey(id2)
   250  	if err == nil {
   251  		t.Fatalf("retrieve the key pair: false positive.")
   252  	}
   253  	if w.HasKeyPair(id1) {
   254  		t.Fatalf("failed delete second identity: first identity exist.")
   255  	}
   256  	if w.HasKeyPair(id2) {
   257  		t.Fatalf("failed delete second identity: still exist.")
   258  	}
   259  	if pk1 != nil {
   260  		t.Fatalf("failed delete second identity: first key exist.")
   261  	}
   262  	if pk2 != nil {
   263  		t.Fatalf("failed delete second identity: second key exist.")
   264  	}
   265  }
   266  
   267  func TestWhisperSymKeyManagement(t *testing.T) {
   268  	InitSingleTest()
   269  
   270  	var err error
   271  	var k1, k2 []byte
   272  	w := New(&DefaultConfig)
   273  	id1 := string("arbitrary-string-1")
   274  	id2 := string("arbitrary-string-2")
   275  
   276  	id1, err = w.GenerateSymKey()
   277  	if err != nil {
   278  		t.Fatalf("failed GenerateSymKey with seed %d: %s.", seed, err)
   279  	}
   280  
   281  	k1, err = w.GetSymKey(id1)
   282  	if err != nil {
   283  		t.Fatalf("failed GetSymKey(id1).")
   284  	}
   285  	k2, err = w.GetSymKey(id2)
   286  	if err == nil {
   287  		t.Fatalf("failed GetSymKey(id2): false positive.")
   288  	}
   289  	if !w.HasSymKey(id1) {
   290  		t.Fatalf("failed HasSymKey(id1).")
   291  	}
   292  	if w.HasSymKey(id2) {
   293  		t.Fatalf("failed HasSymKey(id2): false positive.")
   294  	}
   295  	if k1 == nil {
   296  		t.Fatalf("first key does not exist.")
   297  	}
   298  	if k2 != nil {
   299  		t.Fatalf("second key still exist.")
   300  	}
   301  
   302  	// add existing id, nothing should change
   303  	randomKey := make([]byte, aesKeyLength)
   304  	mrand.Read(randomKey)
   305  	id1, err = w.AddSymKeyDirect(randomKey)
   306  	if err != nil {
   307  		t.Fatalf("failed AddSymKey with seed %d: %s.", seed, err)
   308  	}
   309  
   310  	k1, err = w.GetSymKey(id1)
   311  	if err != nil {
   312  		t.Fatalf("failed w.GetSymKey(id1).")
   313  	}
   314  	k2, err = w.GetSymKey(id2)
   315  	if err == nil {
   316  		t.Fatalf("failed w.GetSymKey(id2): false positive.")
   317  	}
   318  	if !w.HasSymKey(id1) {
   319  		t.Fatalf("failed w.HasSymKey(id1).")
   320  	}
   321  	if w.HasSymKey(id2) {
   322  		t.Fatalf("failed w.HasSymKey(id2): false positive.")
   323  	}
   324  	if k1 == nil {
   325  		t.Fatalf("first key does not exist.")
   326  	}
   327  	if !bytes.Equal(k1, randomKey) {
   328  		t.Fatalf("k1 != randomKey.")
   329  	}
   330  	if k2 != nil {
   331  		t.Fatalf("second key already exist.")
   332  	}
   333  
   334  	id2, err = w.AddSymKeyDirect(randomKey)
   335  	if err != nil {
   336  		t.Fatalf("failed AddSymKey(id2) with seed %d: %s.", seed, err)
   337  	}
   338  	k1, err = w.GetSymKey(id1)
   339  	if err != nil {
   340  		t.Fatalf("failed w.GetSymKey(id1).")
   341  	}
   342  	k2, err = w.GetSymKey(id2)
   343  	if err != nil {
   344  		t.Fatalf("failed w.GetSymKey(id2).")
   345  	}
   346  	if !w.HasSymKey(id1) {
   347  		t.Fatalf("HasSymKey(id1) failed.")
   348  	}
   349  	if !w.HasSymKey(id2) {
   350  		t.Fatalf("HasSymKey(id2) failed.")
   351  	}
   352  	if k1 == nil {
   353  		t.Fatalf("k1 does not exist.")
   354  	}
   355  	if k2 == nil {
   356  		t.Fatalf("k2 does not exist.")
   357  	}
   358  	if !bytes.Equal(k1, k2) {
   359  		t.Fatalf("k1 != k2.")
   360  	}
   361  	if !bytes.Equal(k1, randomKey) {
   362  		t.Fatalf("k1 != randomKey.")
   363  	}
   364  	if len(k1) != aesKeyLength {
   365  		t.Fatalf("wrong length of k1.")
   366  	}
   367  	if len(k2) != aesKeyLength {
   368  		t.Fatalf("wrong length of k2.")
   369  	}
   370  
   371  	w.DeleteSymKey(id1)
   372  	k1, err = w.GetSymKey(id1)
   373  	if err == nil {
   374  		t.Fatalf("failed w.GetSymKey(id1): false positive.")
   375  	}
   376  	if k1 != nil {
   377  		t.Fatalf("failed GetSymKey(id1): false positive.")
   378  	}
   379  	k2, err = w.GetSymKey(id2)
   380  	if err != nil {
   381  		t.Fatalf("failed w.GetSymKey(id2).")
   382  	}
   383  	if w.HasSymKey(id1) {
   384  		t.Fatalf("failed to delete first key: still exist.")
   385  	}
   386  	if !w.HasSymKey(id2) {
   387  		t.Fatalf("failed to delete first key: second key does not exist.")
   388  	}
   389  	if k1 != nil {
   390  		t.Fatalf("failed to delete first key.")
   391  	}
   392  	if k2 == nil {
   393  		t.Fatalf("failed to delete first key: second key is nil.")
   394  	}
   395  
   396  	w.DeleteSymKey(id1)
   397  	w.DeleteSymKey(id2)
   398  	k1, err = w.GetSymKey(id1)
   399  	if err == nil {
   400  		t.Fatalf("failed w.GetSymKey(id1): false positive.")
   401  	}
   402  	k2, err = w.GetSymKey(id2)
   403  	if err == nil {
   404  		t.Fatalf("failed w.GetSymKey(id2): false positive.")
   405  	}
   406  	if k1 != nil || k2 != nil {
   407  		t.Fatalf("k1 or k2 is not nil")
   408  	}
   409  	if w.HasSymKey(id1) {
   410  		t.Fatalf("failed to delete second key: first key exist.")
   411  	}
   412  	if w.HasSymKey(id2) {
   413  		t.Fatalf("failed to delete second key: still exist.")
   414  	}
   415  	if k1 != nil {
   416  		t.Fatalf("failed to delete second key: first key is not nil.")
   417  	}
   418  	if k2 != nil {
   419  		t.Fatalf("failed to delete second key: second key is not nil.")
   420  	}
   421  
   422  	randomKey = make([]byte, aesKeyLength+1)
   423  	mrand.Read(randomKey)
   424  	_, err = w.AddSymKeyDirect(randomKey)
   425  	if err == nil {
   426  		t.Fatalf("added the key with wrong size, seed %d.", seed)
   427  	}
   428  
   429  	const password = "arbitrary data here"
   430  	id1, err = w.AddSymKeyFromPassword(password)
   431  	if err != nil {
   432  		t.Fatalf("failed AddSymKeyFromPassword(id1) with seed %d: %s.", seed, err)
   433  	}
   434  	id2, err = w.AddSymKeyFromPassword(password)
   435  	if err != nil {
   436  		t.Fatalf("failed AddSymKeyFromPassword(id2) with seed %d: %s.", seed, err)
   437  	}
   438  	k1, err = w.GetSymKey(id1)
   439  	if err != nil {
   440  		t.Fatalf("failed w.GetSymKey(id1).")
   441  	}
   442  	k2, err = w.GetSymKey(id2)
   443  	if err != nil {
   444  		t.Fatalf("failed w.GetSymKey(id2).")
   445  	}
   446  	if !w.HasSymKey(id1) {
   447  		t.Fatalf("HasSymKey(id1) failed.")
   448  	}
   449  	if !w.HasSymKey(id2) {
   450  		t.Fatalf("HasSymKey(id2) failed.")
   451  	}
   452  	if !validateDataIntegrity(k2, aesKeyLength) {
   453  		t.Fatalf("key validation failed.")
   454  	}
   455  	if !bytes.Equal(k1, k2) {
   456  		t.Fatalf("k1 != k2.")
   457  	}
   458  }
   459  
   460  func TestExpiry(t *testing.T) {
   461  	InitSingleTest()
   462  
   463  	w := New(&DefaultConfig)
   464  	w.SetMinimumPowTest(context.Background(), 0.0000001)
   465  	defer w.SetMinimumPowTest(context.Background(), DefaultMinimumPoW)
   466  	w.Start(nil)
   467  	defer w.Stop()
   468  
   469  	params, err := generateMessageParams()
   470  	if err != nil {
   471  		t.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err)
   472  	}
   473  
   474  	params.TTL = 1
   475  	msg, err := NewSentMessage(params)
   476  	if err != nil {
   477  		t.Fatalf("failed to create new message with seed %d: %s.", seed, err)
   478  	}
   479  	env, err := msg.Wrap(params)
   480  	if err != nil {
   481  		t.Fatalf("failed Wrap with seed %d: %s.", seed, err)
   482  	}
   483  
   484  	err = w.Send(env)
   485  	if err != nil {
   486  		t.Fatalf("failed to send envelope with seed %d: %s.", seed, err)
   487  	}
   488  
   489  	// wait till received or timeout
   490  	var received, expired bool
   491  	for j := 0; j < 20; j++ {
   492  		time.Sleep(100 * time.Millisecond)
   493  		if len(w.Envelopes()) > 0 {
   494  			received = true
   495  			break
   496  		}
   497  	}
   498  
   499  	if !received {
   500  		t.Fatalf("did not receive the sent envelope, seed: %d.", seed)
   501  	}
   502  
   503  	// wait till expired or timeout
   504  	for j := 0; j < 20; j++ {
   505  		time.Sleep(100 * time.Millisecond)
   506  		if len(w.Envelopes()) == 0 {
   507  			expired = true
   508  			break
   509  		}
   510  	}
   511  
   512  	if !expired {
   513  		t.Fatalf("expire failed, seed: %d.", seed)
   514  	}
   515  }
   516  
   517  func TestCustomization(t *testing.T) {
   518  	InitSingleTest()
   519  
   520  	w := New(&DefaultConfig)
   521  	defer w.SetMinimumPowTest(context.Background(), DefaultMinimumPoW)
   522  	defer w.SetMaxMessageSize(DefaultMaxMessageSize)
   523  	w.Start(nil)
   524  	defer w.Stop()
   525  
   526  	const smallPoW = 0.00001
   527  
   528  	f, err := generateFilter(t, true)
   529  	if err != nil {
   530  		t.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err)
   531  	}
   532  	params, err := generateMessageParams()
   533  	if err != nil {
   534  		t.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err)
   535  	}
   536  
   537  	params.KeySym = f.KeySym
   538  	params.Topic = BytesToTopic(f.Topics[2])
   539  	params.PoW = smallPoW
   540  	params.TTL = 3600 * 24 // one day
   541  	msg, err := NewSentMessage(params)
   542  	if err != nil {
   543  		t.Fatalf("failed to create new message with seed %d: %s.", seed, err)
   544  	}
   545  	env, err := msg.Wrap(params)
   546  	if err != nil {
   547  		t.Fatalf("failed Wrap with seed %d: %s.", seed, err)
   548  	}
   549  
   550  	err = w.Send(env)
   551  	if err == nil {
   552  		t.Fatalf("successfully sent envelope with PoW %.06f, false positive (seed %d).", env.PoW(), seed)
   553  	}
   554  
   555  	w.SetMinimumPowTest(context.Background(), smallPoW/2)
   556  	err = w.Send(env)
   557  	if err != nil {
   558  		t.Fatalf("failed to send envelope with seed %d: %s.", seed, err)
   559  	}
   560  
   561  	params.TTL++
   562  	msg, err = NewSentMessage(params)
   563  	if err != nil {
   564  		t.Fatalf("failed to create new message with seed %d: %s.", seed, err)
   565  	}
   566  	env, err = msg.Wrap(params)
   567  	if err != nil {
   568  		t.Fatalf("failed Wrap with seed %d: %s.", seed, err)
   569  	}
   570  	w.SetMaxMessageSize(uint32(env.size() - 1))
   571  	err = w.Send(env)
   572  	if err == nil {
   573  		t.Fatalf("successfully sent oversized envelope (seed %d): false positive.", seed)
   574  	}
   575  
   576  	w.SetMaxMessageSize(DefaultMaxMessageSize)
   577  	err = w.Send(env)
   578  	if err != nil {
   579  		t.Fatalf("failed to send second envelope with seed %d: %s.", seed, err)
   580  	}
   581  
   582  	// wait till received or timeout
   583  	var received bool
   584  	for j := 0; j < 20; j++ {
   585  		time.Sleep(100 * time.Millisecond)
   586  		if len(w.Envelopes()) > 1 {
   587  			received = true
   588  			break
   589  		}
   590  	}
   591  
   592  	if !received {
   593  		t.Fatalf("did not receive the sent envelope, seed: %d.", seed)
   594  	}
   595  
   596  	// check w.messages()
   597  	id, err := w.Subscribe(f)
   598  	if err != nil {
   599  		t.Fatalf("failed subscribe with seed %d: %s.", seed, err)
   600  	}
   601  	time.Sleep(5 * time.Millisecond)
   602  	mail := f.Retrieve()
   603  	if len(mail) > 0 {
   604  		t.Fatalf("received premature mail")
   605  	}
   606  
   607  	mail = w.Messages(id)
   608  	if len(mail) != 2 {
   609  		t.Fatalf("failed to get whisper messages")
   610  	}
   611  }
   612  
   613  func TestSymmetricSendCycle(t *testing.T) {
   614  	InitSingleTest()
   615  
   616  	w := New(&DefaultConfig)
   617  	defer w.SetMinimumPowTest(context.Background(), DefaultMinimumPoW)
   618  	defer w.SetMaxMessageSize(DefaultMaxMessageSize)
   619  	w.Start(nil)
   620  	defer w.Stop()
   621  
   622  	filter1, err := generateFilter(t, true)
   623  	if err != nil {
   624  		t.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err)
   625  	}
   626  	filter1.PoW = DefaultMinimumPoW
   627  
   628  	// Copy the first filter since some of its fields
   629  	// are randomly gnerated.
   630  	filter2 := &Filter{
   631  		KeySym:   filter1.KeySym,
   632  		Topics:   filter1.Topics,
   633  		PoW:      filter1.PoW,
   634  		AllowP2P: filter1.AllowP2P,
   635  		Messages: make(map[common.Hash]*ReceivedMessage),
   636  	}
   637  
   638  	params, err := generateMessageParams()
   639  	if err != nil {
   640  		t.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err)
   641  	}
   642  
   643  	filter1.Src = &params.Src.PublicKey
   644  	filter2.Src = &params.Src.PublicKey
   645  
   646  	params.KeySym = filter1.KeySym
   647  	params.Topic = BytesToTopic(filter1.Topics[2])
   648  	params.PoW = filter1.PoW
   649  	params.WorkTime = 10
   650  	params.TTL = 50
   651  	msg, err := NewSentMessage(params)
   652  	if err != nil {
   653  		t.Fatalf("failed to create new message with seed %d: %s.", seed, err)
   654  	}
   655  	env, err := msg.Wrap(params)
   656  	if err != nil {
   657  		t.Fatalf("failed Wrap with seed %d: %s.", seed, err)
   658  	}
   659  
   660  	_, err = w.Subscribe(filter1)
   661  	if err != nil {
   662  		t.Fatalf("failed subscribe 1 with seed %d: %s.", seed, err)
   663  	}
   664  
   665  	_, err = w.Subscribe(filter2)
   666  	if err != nil {
   667  		t.Fatalf("failed subscribe 2 with seed %d: %s.", seed, err)
   668  	}
   669  
   670  	err = w.Send(env)
   671  	if err != nil {
   672  		t.Fatalf("Failed sending envelope with PoW %.06f (seed %d): %s", env.PoW(), seed, err)
   673  	}
   674  
   675  	// wait till received or timeout
   676  	var received bool
   677  	for j := 0; j < 200; j++ {
   678  		time.Sleep(10 * time.Millisecond)
   679  		if len(w.Envelopes()) > 0 {
   680  			received = true
   681  			break
   682  		}
   683  	}
   684  
   685  	if !received {
   686  		t.Fatalf("did not receive the sent envelope, seed: %d.", seed)
   687  	}
   688  
   689  	// check w.messages()
   690  	time.Sleep(5 * time.Millisecond)
   691  	mail1 := filter1.Retrieve()
   692  	mail2 := filter2.Retrieve()
   693  	if len(mail2) == 0 {
   694  		t.Fatalf("did not receive any email for filter 2")
   695  	}
   696  	if len(mail1) == 0 {
   697  		t.Fatalf("did not receive any email for filter 1")
   698  	}
   699  
   700  }
   701  
   702  func TestSymmetricSendWithoutAKey(t *testing.T) {
   703  	InitSingleTest()
   704  
   705  	w := New(&DefaultConfig)
   706  	defer w.SetMinimumPowTest(context.Background(), DefaultMinimumPoW)
   707  	defer w.SetMaxMessageSize(DefaultMaxMessageSize)
   708  	w.Start(nil)
   709  	defer w.Stop()
   710  
   711  	filter, err := generateFilter(t, true)
   712  	if err != nil {
   713  		t.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err)
   714  	}
   715  	filter.PoW = DefaultMinimumPoW
   716  
   717  	params, err := generateMessageParams()
   718  	if err != nil {
   719  		t.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err)
   720  	}
   721  
   722  	filter.Src = nil
   723  
   724  	params.KeySym = filter.KeySym
   725  	params.Topic = BytesToTopic(filter.Topics[2])
   726  	params.PoW = filter.PoW
   727  	params.WorkTime = 10
   728  	params.TTL = 50
   729  	msg, err := NewSentMessage(params)
   730  	if err != nil {
   731  		t.Fatalf("failed to create new message with seed %d: %s.", seed, err)
   732  	}
   733  	env, err := msg.Wrap(params)
   734  	if err != nil {
   735  		t.Fatalf("failed Wrap with seed %d: %s.", seed, err)
   736  	}
   737  
   738  	_, err = w.Subscribe(filter)
   739  	if err != nil {
   740  		t.Fatalf("failed subscribe 1 with seed %d: %s.", seed, err)
   741  	}
   742  
   743  	err = w.Send(env)
   744  	if err != nil {
   745  		t.Fatalf("Failed sending envelope with PoW %.06f (seed %d): %s", env.PoW(), seed, err)
   746  	}
   747  
   748  	// wait till received or timeout
   749  	var received bool
   750  	for j := 0; j < 200; j++ {
   751  		time.Sleep(10 * time.Millisecond)
   752  		if len(w.Envelopes()) > 0 {
   753  			received = true
   754  			break
   755  		}
   756  	}
   757  
   758  	if !received {
   759  		t.Fatalf("did not receive the sent envelope, seed: %d.", seed)
   760  	}
   761  
   762  	// check w.messages()
   763  	time.Sleep(5 * time.Millisecond)
   764  	mail := filter.Retrieve()
   765  	if len(mail) == 0 {
   766  		t.Fatalf("did not receive message in spite of not setting a public key")
   767  	}
   768  }
   769  
   770  func TestSymmetricSendKeyMismatch(t *testing.T) {
   771  	InitSingleTest()
   772  
   773  	w := New(&DefaultConfig)
   774  	defer w.SetMinimumPowTest(context.Background(), DefaultMinimumPoW)
   775  	defer w.SetMaxMessageSize(DefaultMaxMessageSize)
   776  	w.Start(nil)
   777  	defer w.Stop()
   778  
   779  	filter, err := generateFilter(t, true)
   780  	if err != nil {
   781  		t.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err)
   782  	}
   783  	filter.PoW = DefaultMinimumPoW
   784  
   785  	params, err := generateMessageParams()
   786  	if err != nil {
   787  		t.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err)
   788  	}
   789  
   790  	params.KeySym = filter.KeySym
   791  	params.Topic = BytesToTopic(filter.Topics[2])
   792  	params.PoW = filter.PoW
   793  	params.WorkTime = 10
   794  	params.TTL = 50
   795  	msg, err := NewSentMessage(params)
   796  	if err != nil {
   797  		t.Fatalf("failed to create new message with seed %d: %s.", seed, err)
   798  	}
   799  	env, err := msg.Wrap(params)
   800  	if err != nil {
   801  		t.Fatalf("failed Wrap with seed %d: %s.", seed, err)
   802  	}
   803  
   804  	_, err = w.Subscribe(filter)
   805  	if err != nil {
   806  		t.Fatalf("failed subscribe 1 with seed %d: %s.", seed, err)
   807  	}
   808  
   809  	err = w.Send(env)
   810  	if err != nil {
   811  		t.Fatalf("Failed sending envelope with PoW %.06f (seed %d): %s", env.PoW(), seed, err)
   812  	}
   813  
   814  	// wait till received or timeout
   815  	var received bool
   816  	for j := 0; j < 200; j++ {
   817  		time.Sleep(10 * time.Millisecond)
   818  		if len(w.Envelopes()) > 0 {
   819  			received = true
   820  			break
   821  		}
   822  	}
   823  
   824  	if !received {
   825  		t.Fatalf("did not receive the sent envelope, seed: %d.", seed)
   826  	}
   827  
   828  	// check w.messages()
   829  	time.Sleep(5 * time.Millisecond)
   830  	mail := filter.Retrieve()
   831  	if len(mail) > 0 {
   832  		t.Fatalf("received a message when keys weren't matching")
   833  	}
   834  }
   835  
   836  func TestBloom(t *testing.T) {
   837  	topic := TopicType{0, 0, 255, 6}
   838  	b := TopicToBloom(topic)
   839  	x := make([]byte, bloomFilterSize)
   840  	x[0] = byte(1)
   841  	x[32] = byte(1)
   842  	x[bloomFilterSize-1] = byte(128)
   843  	if !bloomFilterMatch(x, b) || !bloomFilterMatch(b, x) {
   844  		t.Fatalf("bloom filter does not match the mask")
   845  	}
   846  
   847  	_, err := mrand.Read(b)
   848  	if err != nil {
   849  		t.Fatalf("math rand error")
   850  	}
   851  	_, err = mrand.Read(x)
   852  	if err != nil {
   853  		t.Fatalf("math rand error")
   854  	}
   855  	if !bloomFilterMatch(b, b) {
   856  		t.Fatalf("bloom filter does not match self")
   857  	}
   858  	x = addBloom(x, b)
   859  	if !bloomFilterMatch(x, b) {
   860  		t.Fatalf("bloom filter does not match combined bloom")
   861  	}
   862  	if !isFullNode(nil) {
   863  		t.Fatalf("isFullNode did not recognize nil as full node")
   864  	}
   865  	x[17] = 254
   866  	if isFullNode(x) {
   867  		t.Fatalf("isFullNode false positive")
   868  	}
   869  	for i := 0; i < bloomFilterSize; i++ {
   870  		b[i] = byte(255)
   871  	}
   872  	if !isFullNode(b) {
   873  		t.Fatalf("isFullNode false negative")
   874  	}
   875  	if bloomFilterMatch(x, b) {
   876  		t.Fatalf("bloomFilterMatch false positive")
   877  	}
   878  	if !bloomFilterMatch(b, x) {
   879  		t.Fatalf("bloomFilterMatch false negative")
   880  	}
   881  
   882  	w := New(&DefaultConfig)
   883  	f := w.BloomFilter()
   884  	if f != nil {
   885  		t.Fatalf("wrong bloom on creation")
   886  	}
   887  	err = w.SetBloomFilter(context.Background(), x)
   888  	if err != nil {
   889  		t.Fatalf("failed to set bloom filter: %s", err)
   890  	}
   891  	f = w.BloomFilter()
   892  	if !bloomFilterMatch(f, x) || !bloomFilterMatch(x, f) {
   893  		t.Fatalf("retireved wrong bloom filter")
   894  	}
   895  }