api_test.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. package whisperv6
  17. import (
  18. "bytes"
  19. "crypto/ecdsa"
  20. "testing"
  21. "time"
  22. "github.com/ethereum/go-ethereum/common"
  23. set "gopkg.in/fatih/set.v0"
  24. )
  25. func TestMultipleTopicCopyInNewMessageFilter(t *testing.T) {
  26. w := &Whisper{
  27. privateKeys: make(map[string]*ecdsa.PrivateKey),
  28. symKeys: make(map[string][]byte),
  29. envelopes: make(map[common.Hash]*Envelope),
  30. expirations: make(map[uint32]*set.SetNonTS),
  31. peers: make(map[*Peer]struct{}),
  32. messageQueue: make(chan *Envelope, messageQueueLimit),
  33. p2pMsgQueue: make(chan *Envelope, messageQueueLimit),
  34. quit: make(chan struct{}),
  35. syncAllowance: DefaultSyncAllowance,
  36. }
  37. w.filters = NewFilters(w)
  38. keyID, err := w.GenerateSymKey()
  39. if err != nil {
  40. t.Fatalf("Error generating symmetric key: %v", err)
  41. }
  42. api := PublicWhisperAPI{
  43. w: w,
  44. lastUsed: make(map[string]time.Time),
  45. }
  46. t1 := [4]byte{0xde, 0xea, 0xbe, 0xef}
  47. t2 := [4]byte{0xca, 0xfe, 0xde, 0xca}
  48. crit := Criteria{
  49. SymKeyID: keyID,
  50. Topics: []TopicType{TopicType(t1), TopicType(t2)},
  51. }
  52. _, err = api.NewMessageFilter(crit)
  53. if err != nil {
  54. t.Fatalf("Error creating the filter: %v", err)
  55. }
  56. found := false
  57. candidates := w.filters.getWatchersByTopic(TopicType(t1))
  58. for _, f := range candidates {
  59. if len(f.Topics) == 2 {
  60. if bytes.Equal(f.Topics[0], t1[:]) && bytes.Equal(f.Topics[1], t2[:]) {
  61. found = true
  62. }
  63. }
  64. }
  65. if !found {
  66. t.Fatalf("Could not find filter with both topics")
  67. }
  68. }