whisper_test.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // Copyright 2014 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 whisper
  17. import (
  18. "testing"
  19. "time"
  20. "github.com/ethereum/go-ethereum/p2p"
  21. "github.com/ethereum/go-ethereum/p2p/discover"
  22. )
  23. func startTestCluster(n int) []*Whisper {
  24. // Create the batch of simulated peers
  25. nodes := make([]*p2p.Peer, n)
  26. for i := 0; i < n; i++ {
  27. nodes[i] = p2p.NewPeer(discover.NodeID{}, "", nil)
  28. }
  29. whispers := make([]*Whisper, n)
  30. for i := 0; i < n; i++ {
  31. whispers[i] = New()
  32. whispers[i].Start(nil)
  33. }
  34. // Wire all the peers to the root one
  35. for i := 1; i < n; i++ {
  36. src, dst := p2p.MsgPipe()
  37. go whispers[0].handlePeer(nodes[i], src)
  38. go whispers[i].handlePeer(nodes[0], dst)
  39. }
  40. return whispers
  41. }
  42. func TestSelfMessage(t *testing.T) {
  43. // Start the single node cluster
  44. client := startTestCluster(1)[0]
  45. // Start watching for self messages, signal any arrivals
  46. self := client.NewIdentity()
  47. done := make(chan struct{})
  48. client.Watch(Filter{
  49. To: &self.PublicKey,
  50. Fn: func(msg *Message) {
  51. close(done)
  52. },
  53. })
  54. // Send a dummy message to oneself
  55. msg := NewMessage([]byte("self whisper"))
  56. envelope, err := msg.Wrap(DefaultPoW, Options{
  57. From: self,
  58. To: &self.PublicKey,
  59. TTL: DefaultTTL,
  60. })
  61. if err != nil {
  62. t.Fatalf("failed to wrap message: %v", err)
  63. }
  64. // Dump the message into the system and wait for it to pop back out
  65. if err := client.Send(envelope); err != nil {
  66. t.Fatalf("failed to send self-message: %v", err)
  67. }
  68. select {
  69. case <-done:
  70. case <-time.After(time.Second):
  71. t.Fatalf("self-message receive timeout")
  72. }
  73. }
  74. func TestDirectMessage(t *testing.T) {
  75. // Start the sender-recipient cluster
  76. cluster := startTestCluster(2)
  77. sender := cluster[0]
  78. senderId := sender.NewIdentity()
  79. recipient := cluster[1]
  80. recipientId := recipient.NewIdentity()
  81. // Watch for arriving messages on the recipient
  82. done := make(chan struct{})
  83. recipient.Watch(Filter{
  84. To: &recipientId.PublicKey,
  85. Fn: func(msg *Message) {
  86. close(done)
  87. },
  88. })
  89. // Send a dummy message from the sender
  90. msg := NewMessage([]byte("direct whisper"))
  91. envelope, err := msg.Wrap(DefaultPoW, Options{
  92. From: senderId,
  93. To: &recipientId.PublicKey,
  94. TTL: DefaultTTL,
  95. })
  96. if err != nil {
  97. t.Fatalf("failed to wrap message: %v", err)
  98. }
  99. if err := sender.Send(envelope); err != nil {
  100. t.Fatalf("failed to send direct message: %v", err)
  101. }
  102. // Wait for an arrival or a timeout
  103. select {
  104. case <-done:
  105. case <-time.After(time.Second):
  106. t.Fatalf("direct message receive timeout")
  107. }
  108. }
  109. func TestAnonymousBroadcast(t *testing.T) {
  110. testBroadcast(true, t)
  111. }
  112. func TestIdentifiedBroadcast(t *testing.T) {
  113. testBroadcast(false, t)
  114. }
  115. func testBroadcast(anonymous bool, t *testing.T) {
  116. // Start the single sender multi recipient cluster
  117. cluster := startTestCluster(3)
  118. sender := cluster[1]
  119. targets := cluster[1:]
  120. for _, target := range targets {
  121. if !anonymous {
  122. target.NewIdentity()
  123. }
  124. }
  125. // Watch for arriving messages on the recipients
  126. dones := make([]chan struct{}, len(targets))
  127. for i := 0; i < len(targets); i++ {
  128. done := make(chan struct{}) // need for the closure
  129. dones[i] = done
  130. targets[i].Watch(Filter{
  131. Topics: NewFilterTopicsFromStringsFlat("broadcast topic"),
  132. Fn: func(msg *Message) {
  133. close(done)
  134. },
  135. })
  136. }
  137. // Send a dummy message from the sender
  138. msg := NewMessage([]byte("broadcast whisper"))
  139. envelope, err := msg.Wrap(DefaultPoW, Options{
  140. Topics: NewTopicsFromStrings("broadcast topic"),
  141. TTL: DefaultTTL,
  142. })
  143. if err != nil {
  144. t.Fatalf("failed to wrap message: %v", err)
  145. }
  146. if err := sender.Send(envelope); err != nil {
  147. t.Fatalf("failed to send broadcast message: %v", err)
  148. }
  149. // Wait for an arrival on each recipient, or timeouts
  150. timeout := time.After(time.Second)
  151. for _, done := range dones {
  152. select {
  153. case <-done:
  154. case <-timeout:
  155. t.Fatalf("broadcast message receive timeout")
  156. }
  157. }
  158. }
  159. func TestMessageExpiration(t *testing.T) {
  160. // Start the single node cluster and inject a dummy message
  161. node := startTestCluster(1)[0]
  162. message := NewMessage([]byte("expiring message"))
  163. envelope, err := message.Wrap(DefaultPoW, Options{TTL: time.Second})
  164. if err != nil {
  165. t.Fatalf("failed to wrap message: %v", err)
  166. }
  167. if err := node.Send(envelope); err != nil {
  168. t.Fatalf("failed to inject message: %v", err)
  169. }
  170. // Check that the message is inside the cache
  171. node.poolMu.RLock()
  172. _, found := node.messages[envelope.Hash()]
  173. node.poolMu.RUnlock()
  174. if !found {
  175. t.Fatalf("message not found in cache")
  176. }
  177. // Wait for expiration and check cache again
  178. time.Sleep(time.Second) // wait for expiration
  179. time.Sleep(2 * expirationCycle) // wait for cleanup cycle
  180. node.poolMu.RLock()
  181. _, found = node.messages[envelope.Hash()]
  182. node.poolMu.RUnlock()
  183. if found {
  184. t.Fatalf("message not expired from cache")
  185. }
  186. // Check that adding an expired envelope doesn't do anything.
  187. node.add(envelope)
  188. node.poolMu.RLock()
  189. _, found = node.messages[envelope.Hash()]
  190. node.poolMu.RUnlock()
  191. if found {
  192. t.Fatalf("message was added to cache")
  193. }
  194. }