1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
package whisper
import (
"fmt"
"testing"
"time"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/p2p/nat"
)
type testNode struct {
server *p2p.Server
client *Whisper
}
func startNodes(n int) ([]*testNode, error) {
cluster := make([]*testNode, 0, n)
for i := 0; i < n; i++ {
shh := New()
// Generate the node identity
key, err := crypto.GenerateKey()
if err != nil {
return nil, err
}
name := common.MakeName(fmt.Sprintf("whisper-go-test-%d", i), "1.0")
// Create an Ethereum server to communicate through
server := &p2p.Server{
PrivateKey: key,
MaxPeers: 10,
Name: name,
Protocols: []p2p.Protocol{shh.Protocol()},
ListenAddr: fmt.Sprintf(":%d", 30300+i),
NAT: nat.Any(),
}
if err := server.Start(); err != nil {
return nil, err
}
// Peer online, store and iterate
cluster = append(cluster, &testNode{
server: server,
client: shh,
})
}
return cluster, nil
}
func stopNodes(cluster []*testNode) {
for _, node := range cluster {
node.server.Stop()
}
}
func TestSelfMessage(t *testing.T) {
cluster, err := startNodes(1)
if err != nil {
t.Fatalf("failed to boot test cluster: %v", err)
}
defer stopNodes(cluster)
client := cluster[0].client
// Start watching for self messages, signal any arrivals
self := client.NewIdentity()
done := make(chan struct{})
client.Watch(Filter{
To: &self.PublicKey,
Fn: func(msg *Message) {
close(done)
},
})
// Send a dummy message to oneself
msg := NewMessage([]byte("hello whisper"))
envelope, err := msg.Wrap(DefaultProofOfWork, Options{
From: self,
To: &self.PublicKey,
TTL: DefaultTimeToLive,
})
if err != nil {
t.Fatalf("failed to wrap message: %v", err)
}
// Dump the message into the system and wait for it to pop back out
if err := client.Send(envelope); err != nil {
t.Fatalf("failed to send self-message: %v", err)
}
select {
case <-done:
case <-time.After(time.Second):
t.Fatalf("self-message receive timeout")
}
}
func TestDirectMessage(t *testing.T) {
cluster, err := startNodes(2)
if err != nil {
t.Fatalf("failed to boot test cluster: %v", err)
}
defer stopNodes(cluster)
sender := cluster[0].client
senderId := sender.NewIdentity()
recipient := cluster[1].client
recipientId := recipient.NewIdentity()
// Watch for arriving messages on the recipient
done := make(chan struct{})
recipient.Watch(Filter{
To: &recipientId.PublicKey,
Fn: func(msg *Message) {
close(done)
},
})
// Send a dummy message from the sender
msg := NewMessage([]byte("hello whisper"))
envelope, err := msg.Wrap(DefaultProofOfWork, Options{
From: senderId,
To: &recipientId.PublicKey,
TTL: DefaultTimeToLive,
})
if err != nil {
t.Fatalf("failed to wrap message: %v", err)
}
if err := sender.Send(envelope); err != nil {
t.Fatalf("failed to send direct message: %v", err)
}
// Wait for an arrival or a timeout
select {
case <-done:
case <-time.After(time.Second):
t.Fatalf("direct message receive timeout")
}
}
|