github.com/SmartMeshFoundation/Spectrum@v0.0.0-20220621030607-452a266fee1e/whisper/whisperv6/whisper_test.go (about)

     1  // Copyright 2016 The Spectrum Authors
     2  // This file is part of the Spectrum library.
     3  //
     4  // The Spectrum 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 Spectrum 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 Spectrum 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/SmartMeshFoundation/Spectrum/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 !validateSymmetricKey(derived) {
    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 k1 == nil {
   452  		t.Fatalf("k1 does not exist.")
   453  	}
   454  	if k2 == nil {
   455  		t.Fatalf("k2 does not exist.")
   456  	}
   457  	if !bytes.Equal(k1, k2) {
   458  		t.Fatalf("k1 != k2.")
   459  	}
   460  	if len(k1) != aesKeyLength {
   461  		t.Fatalf("wrong length of k1.")
   462  	}
   463  	if len(k2) != aesKeyLength {
   464  		t.Fatalf("wrong length of k2.")
   465  	}
   466  	if !validateSymmetricKey(k2) {
   467  		t.Fatalf("key validation failed.")
   468  	}
   469  }
   470  
   471  func TestExpiry(t *testing.T) {
   472  	InitSingleTest()
   473  
   474  	w := New(&DefaultConfig)
   475  	w.SetMinimumPowTest(0.0000001)
   476  	defer w.SetMinimumPowTest(DefaultMinimumPoW)
   477  	w.Start(nil)
   478  	defer w.Stop()
   479  
   480  	params, err := generateMessageParams()
   481  	if err != nil {
   482  		t.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err)
   483  	}
   484  
   485  	params.TTL = 1
   486  	msg, err := NewSentMessage(params)
   487  	if err != nil {
   488  		t.Fatalf("failed to create new message with seed %d: %s.", seed, err)
   489  	}
   490  	env, err := msg.Wrap(params)
   491  	if err != nil {
   492  		t.Fatalf("failed Wrap with seed %d: %s.", seed, err)
   493  	}
   494  
   495  	err = w.Send(env)
   496  	if err != nil {
   497  		t.Fatalf("failed to send envelope with seed %d: %s.", seed, err)
   498  	}
   499  
   500  	// wait till received or timeout
   501  	var received, expired bool
   502  	for j := 0; j < 20; j++ {
   503  		time.Sleep(100 * time.Millisecond)
   504  		if len(w.Envelopes()) > 0 {
   505  			received = true
   506  			break
   507  		}
   508  	}
   509  
   510  	if !received {
   511  		t.Fatalf("did not receive the sent envelope, seed: %d.", seed)
   512  	}
   513  
   514  	// wait till expired or timeout
   515  	for j := 0; j < 20; j++ {
   516  		time.Sleep(100 * time.Millisecond)
   517  		if len(w.Envelopes()) == 0 {
   518  			expired = true
   519  			break
   520  		}
   521  	}
   522  
   523  	if !expired {
   524  		t.Fatalf("expire failed, seed: %d.", seed)
   525  	}
   526  }
   527  
   528  func TestCustomization(t *testing.T) {
   529  	InitSingleTest()
   530  
   531  	w := New(&DefaultConfig)
   532  	defer w.SetMinimumPowTest(DefaultMinimumPoW)
   533  	defer w.SetMaxMessageSize(DefaultMaxMessageSize)
   534  	w.Start(nil)
   535  	defer w.Stop()
   536  
   537  	const smallPoW = 0.00001
   538  
   539  	f, err := generateFilter(t, true)
   540  	if err != nil {
   541  		t.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err)
   542  	}
   543  	params, err := generateMessageParams()
   544  	if err != nil {
   545  		t.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err)
   546  	}
   547  
   548  	params.KeySym = f.KeySym
   549  	params.Topic = BytesToTopic(f.Topics[2])
   550  	params.PoW = smallPoW
   551  	params.TTL = 3600 * 24 // one day
   552  	msg, err := NewSentMessage(params)
   553  	if err != nil {
   554  		t.Fatalf("failed to create new message with seed %d: %s.", seed, err)
   555  	}
   556  	env, err := msg.Wrap(params)
   557  	if err != nil {
   558  		t.Fatalf("failed Wrap with seed %d: %s.", seed, err)
   559  	}
   560  
   561  	err = w.Send(env)
   562  	if err == nil {
   563  		t.Fatalf("successfully sent envelope with PoW %.06f, false positive (seed %d).", env.PoW(), seed)
   564  	}
   565  
   566  	w.SetMinimumPowTest(smallPoW / 2)
   567  	err = w.Send(env)
   568  	if err != nil {
   569  		t.Fatalf("failed to send envelope with seed %d: %s.", seed, err)
   570  	}
   571  
   572  	params.TTL++
   573  	msg, err = NewSentMessage(params)
   574  	if err != nil {
   575  		t.Fatalf("failed to create new message with seed %d: %s.", seed, err)
   576  	}
   577  	env, err = msg.Wrap(params)
   578  	if err != nil {
   579  		t.Fatalf("failed Wrap with seed %d: %s.", seed, err)
   580  	}
   581  	w.SetMaxMessageSize(uint32(env.size() - 1))
   582  	err = w.Send(env)
   583  	if err == nil {
   584  		t.Fatalf("successfully sent oversized envelope (seed %d): false positive.", seed)
   585  	}
   586  
   587  	w.SetMaxMessageSize(DefaultMaxMessageSize)
   588  	err = w.Send(env)
   589  	if err != nil {
   590  		t.Fatalf("failed to send second envelope with seed %d: %s.", seed, err)
   591  	}
   592  
   593  	// wait till received or timeout
   594  	var received bool
   595  	for j := 0; j < 20; j++ {
   596  		time.Sleep(100 * time.Millisecond)
   597  		if len(w.Envelopes()) > 1 {
   598  			received = true
   599  			break
   600  		}
   601  	}
   602  
   603  	if !received {
   604  		t.Fatalf("did not receive the sent envelope, seed: %d.", seed)
   605  	}
   606  
   607  	// check w.messages()
   608  	id, err := w.Subscribe(f)
   609  	if err != nil {
   610  		t.Fatalf("failed subscribe with seed %d: %s.", seed, err)
   611  	}
   612  	time.Sleep(5 * time.Millisecond)
   613  	mail := f.Retrieve()
   614  	if len(mail) > 0 {
   615  		t.Fatalf("received premature mail")
   616  	}
   617  
   618  	mail = w.Messages(id)
   619  	if len(mail) != 2 {
   620  		t.Fatalf("failed to get whisper messages")
   621  	}
   622  }
   623  
   624  func TestSymmetricSendCycle(t *testing.T) {
   625  	InitSingleTest()
   626  
   627  	w := New(&DefaultConfig)
   628  	defer w.SetMinimumPowTest(DefaultMinimumPoW)
   629  	defer w.SetMaxMessageSize(DefaultMaxMessageSize)
   630  	w.Start(nil)
   631  	defer w.Stop()
   632  
   633  	filter1, err := generateFilter(t, true)
   634  	if err != nil {
   635  		t.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err)
   636  	}
   637  	filter1.PoW = DefaultMinimumPoW
   638  
   639  	// Copy the first filter since some of its fields
   640  	// are randomly gnerated.
   641  	filter2 := &Filter{
   642  		KeySym:   filter1.KeySym,
   643  		Topics:   filter1.Topics,
   644  		PoW:      filter1.PoW,
   645  		AllowP2P: filter1.AllowP2P,
   646  		Messages: make(map[common.Hash]*ReceivedMessage),
   647  	}
   648  
   649  	params, err := generateMessageParams()
   650  	if err != nil {
   651  		t.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err)
   652  	}
   653  
   654  	filter1.Src = &params.Src.PublicKey
   655  	filter2.Src = &params.Src.PublicKey
   656  
   657  	params.KeySym = filter1.KeySym
   658  	params.Topic = BytesToTopic(filter1.Topics[2])
   659  	params.PoW = filter1.PoW
   660  	params.WorkTime = 10
   661  	params.TTL = 50
   662  	msg, err := NewSentMessage(params)
   663  	if err != nil {
   664  		t.Fatalf("failed to create new message with seed %d: %s.", seed, err)
   665  	}
   666  	env, err := msg.Wrap(params)
   667  	if err != nil {
   668  		t.Fatalf("failed Wrap with seed %d: %s.", seed, err)
   669  	}
   670  
   671  	_, err = w.Subscribe(filter1)
   672  	if err != nil {
   673  		t.Fatalf("failed subscribe 1 with seed %d: %s.", seed, err)
   674  	}
   675  
   676  	_, err = w.Subscribe(filter2)
   677  	if err != nil {
   678  		t.Fatalf("failed subscribe 2 with seed %d: %s.", seed, err)
   679  	}
   680  
   681  	err = w.Send(env)
   682  	if err != nil {
   683  		t.Fatalf("Failed sending envelope with PoW %.06f (seed %d): %s", env.PoW(), seed, err)
   684  	}
   685  
   686  	// wait till received or timeout
   687  	var received bool
   688  	for j := 0; j < 200; j++ {
   689  		time.Sleep(10 * time.Millisecond)
   690  		if len(w.Envelopes()) > 0 {
   691  			received = true
   692  			break
   693  		}
   694  	}
   695  
   696  	if !received {
   697  		t.Fatalf("did not receive the sent envelope, seed: %d.", seed)
   698  	}
   699  
   700  	// check w.messages()
   701  	time.Sleep(5 * time.Millisecond)
   702  	mail1 := filter1.Retrieve()
   703  	mail2 := filter2.Retrieve()
   704  	if len(mail2) == 0 {
   705  		t.Fatalf("did not receive any email for filter 2")
   706  	}
   707  	if len(mail1) == 0 {
   708  		t.Fatalf("did not receive any email for filter 1")
   709  	}
   710  
   711  }
   712  
   713  func TestSymmetricSendWithoutAKey(t *testing.T) {
   714  	InitSingleTest()
   715  
   716  	w := New(&DefaultConfig)
   717  	defer w.SetMinimumPowTest(DefaultMinimumPoW)
   718  	defer w.SetMaxMessageSize(DefaultMaxMessageSize)
   719  	w.Start(nil)
   720  	defer w.Stop()
   721  
   722  	filter, err := generateFilter(t, true)
   723  	if err != nil {
   724  		t.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err)
   725  	}
   726  	filter.PoW = DefaultMinimumPoW
   727  
   728  	params, err := generateMessageParams()
   729  	if err != nil {
   730  		t.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err)
   731  	}
   732  
   733  	filter.Src = nil
   734  
   735  	params.KeySym = filter.KeySym
   736  	params.Topic = BytesToTopic(filter.Topics[2])
   737  	params.PoW = filter.PoW
   738  	params.WorkTime = 10
   739  	params.TTL = 50
   740  	msg, err := NewSentMessage(params)
   741  	if err != nil {
   742  		t.Fatalf("failed to create new message with seed %d: %s.", seed, err)
   743  	}
   744  	env, err := msg.Wrap(params)
   745  	if err != nil {
   746  		t.Fatalf("failed Wrap with seed %d: %s.", seed, err)
   747  	}
   748  
   749  	_, err = w.Subscribe(filter)
   750  	if err != nil {
   751  		t.Fatalf("failed subscribe 1 with seed %d: %s.", seed, err)
   752  	}
   753  
   754  	err = w.Send(env)
   755  	if err != nil {
   756  		t.Fatalf("Failed sending envelope with PoW %.06f (seed %d): %s", env.PoW(), seed, err)
   757  	}
   758  
   759  	// wait till received or timeout
   760  	var received bool
   761  	for j := 0; j < 200; j++ {
   762  		time.Sleep(10 * time.Millisecond)
   763  		if len(w.Envelopes()) > 0 {
   764  			received = true
   765  			break
   766  		}
   767  	}
   768  
   769  	if !received {
   770  		t.Fatalf("did not receive the sent envelope, seed: %d.", seed)
   771  	}
   772  
   773  	// check w.messages()
   774  	time.Sleep(5 * time.Millisecond)
   775  	mail := filter.Retrieve()
   776  	if len(mail) == 0 {
   777  		t.Fatalf("did not receive message in spite of not setting a public key")
   778  	}
   779  }
   780  
   781  func TestSymmetricSendKeyMismatch(t *testing.T) {
   782  	InitSingleTest()
   783  
   784  	w := New(&DefaultConfig)
   785  	defer w.SetMinimumPowTest(DefaultMinimumPoW)
   786  	defer w.SetMaxMessageSize(DefaultMaxMessageSize)
   787  	w.Start(nil)
   788  	defer w.Stop()
   789  
   790  	filter, err := generateFilter(t, true)
   791  	if err != nil {
   792  		t.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err)
   793  	}
   794  	filter.PoW = DefaultMinimumPoW
   795  
   796  	params, err := generateMessageParams()
   797  	if err != nil {
   798  		t.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err)
   799  	}
   800  
   801  	params.KeySym = filter.KeySym
   802  	params.Topic = BytesToTopic(filter.Topics[2])
   803  	params.PoW = filter.PoW
   804  	params.WorkTime = 10
   805  	params.TTL = 50
   806  	msg, err := NewSentMessage(params)
   807  	if err != nil {
   808  		t.Fatalf("failed to create new message with seed %d: %s.", seed, err)
   809  	}
   810  	env, err := msg.Wrap(params)
   811  	if err != nil {
   812  		t.Fatalf("failed Wrap with seed %d: %s.", seed, err)
   813  	}
   814  
   815  	_, err = w.Subscribe(filter)
   816  	if err != nil {
   817  		t.Fatalf("failed subscribe 1 with seed %d: %s.", seed, err)
   818  	}
   819  
   820  	err = w.Send(env)
   821  	if err != nil {
   822  		t.Fatalf("Failed sending envelope with PoW %.06f (seed %d): %s", env.PoW(), seed, err)
   823  	}
   824  
   825  	// wait till received or timeout
   826  	var received bool
   827  	for j := 0; j < 200; j++ {
   828  		time.Sleep(10 * time.Millisecond)
   829  		if len(w.Envelopes()) > 0 {
   830  			received = true
   831  			break
   832  		}
   833  	}
   834  
   835  	if !received {
   836  		t.Fatalf("did not receive the sent envelope, seed: %d.", seed)
   837  	}
   838  
   839  	// check w.messages()
   840  	time.Sleep(5 * time.Millisecond)
   841  	mail := filter.Retrieve()
   842  	if len(mail) > 0 {
   843  		t.Fatalf("received a message when keys weren't matching")
   844  	}
   845  }