github.com/linapex/ethereum-dpos-chinese@v0.0.0-20190316121959-b78b3a4a1ece/whisper/whisperv6/api_test.go (about)

     1  
     2  //<developer>
     3  //    <name>linapex 曹一峰</name>
     4  //    <email>linapex@163.com</email>
     5  //    <wx>superexc</wx>
     6  //    <qqgroup>128148617</qqgroup>
     7  //    <url>https://jsq.ink</url>
     8  //    <role>pku engineer</role>
     9  //    <date>2019-03-16 12:09:51</date>
    10  //</624342689906692096>
    11  
    12  //
    13  //
    14  //
    15  //
    16  //
    17  //
    18  //
    19  //
    20  //
    21  //
    22  //
    23  //
    24  //
    25  //
    26  //
    27  
    28  package whisperv6
    29  
    30  import (
    31  	"bytes"
    32  	"crypto/ecdsa"
    33  	"testing"
    34  	"time"
    35  
    36  	mapset "github.com/deckarep/golang-set"
    37  	"github.com/ethereum/go-ethereum/common"
    38  )
    39  
    40  func TestMultipleTopicCopyInNewMessageFilter(t *testing.T) {
    41  	w := &Whisper{
    42  		privateKeys:   make(map[string]*ecdsa.PrivateKey),
    43  		symKeys:       make(map[string][]byte),
    44  		envelopes:     make(map[common.Hash]*Envelope),
    45  		expirations:   make(map[uint32]mapset.Set),
    46  		peers:         make(map[*Peer]struct{}),
    47  		messageQueue:  make(chan *Envelope, messageQueueLimit),
    48  		p2pMsgQueue:   make(chan *Envelope, messageQueueLimit),
    49  		quit:          make(chan struct{}),
    50  		syncAllowance: DefaultSyncAllowance,
    51  	}
    52  	w.filters = NewFilters(w)
    53  
    54  	keyID, err := w.GenerateSymKey()
    55  	if err != nil {
    56  		t.Fatalf("Error generating symmetric key: %v", err)
    57  	}
    58  	api := PublicWhisperAPI{
    59  		w:        w,
    60  		lastUsed: make(map[string]time.Time),
    61  	}
    62  
    63  	t1 := [4]byte{0xde, 0xea, 0xbe, 0xef}
    64  	t2 := [4]byte{0xca, 0xfe, 0xde, 0xca}
    65  
    66  	crit := Criteria{
    67  		SymKeyID: keyID,
    68  		Topics:   []TopicType{TopicType(t1), TopicType(t2)},
    69  	}
    70  
    71  	_, err = api.NewMessageFilter(crit)
    72  	if err != nil {
    73  		t.Fatalf("Error creating the filter: %v", err)
    74  	}
    75  
    76  	found := false
    77  	candidates := w.filters.getWatchersByTopic(TopicType(t1))
    78  	for _, f := range candidates {
    79  		if len(f.Topics) == 2 {
    80  			if bytes.Equal(f.Topics[0], t1[:]) && bytes.Equal(f.Topics[1], t2[:]) {
    81  				found = true
    82  			}
    83  		}
    84  	}
    85  
    86  	if !found {
    87  		t.Fatalf("Could not find filter with both topics")
    88  	}
    89  }
    90