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