github.com/zhiqiangxu/go-ethereum@v1.9.16-0.20210824055606-be91cfdebc48/whisper/whisperv6/api_test.go (about)

     1  // Copyright 2018 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  	"testing"
    22  	"time"
    23  )
    24  
    25  func TestMultipleTopicCopyInNewMessageFilter(t *testing.T) {
    26  	w := New(nil)
    27  
    28  	keyID, err := w.GenerateSymKey()
    29  	if err != nil {
    30  		t.Fatalf("Error generating symmetric key: %v", err)
    31  	}
    32  	api := PublicWhisperAPI{
    33  		w:        w,
    34  		lastUsed: make(map[string]time.Time),
    35  	}
    36  
    37  	t1 := [4]byte{0xde, 0xea, 0xbe, 0xef}
    38  	t2 := [4]byte{0xca, 0xfe, 0xde, 0xca}
    39  
    40  	crit := Criteria{
    41  		SymKeyID: keyID,
    42  		Topics:   []TopicType{TopicType(t1), TopicType(t2)},
    43  	}
    44  
    45  	_, err = api.NewMessageFilter(crit)
    46  	if err != nil {
    47  		t.Fatalf("Error creating the filter: %v", err)
    48  	}
    49  
    50  	found := false
    51  	candidates := w.filters.getWatchersByTopic(TopicType(t1))
    52  	for _, f := range candidates {
    53  		if len(f.Topics) == 2 {
    54  			if bytes.Equal(f.Topics[0], t1[:]) && bytes.Equal(f.Topics[1], t2[:]) {
    55  				found = true
    56  			}
    57  		}
    58  	}
    59  
    60  	if !found {
    61  		t.Fatalf("Could not find filter with both topics")
    62  	}
    63  }