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