forked from holochain/holochain-proto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_test.go
237 lines (202 loc) · 6.44 KB
/
node_test.go
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package holochain
import (
"bytes"
"context"
"fmt"
ic "github.com/libp2p/go-libp2p-crypto"
net "github.com/libp2p/go-libp2p-net"
pstore "github.com/libp2p/go-libp2p-peerstore"
. "github.com/smartystreets/goconvey/convey"
// peer "gx/ipfs/QmZcUPvPhD1Xvk6mwijYF8AfR3mG31S1YsEfHG4khrFPRr/go-libp2p-peer"
"io/ioutil"
"strings"
"testing"
"time"
)
func TestNewNode(t *testing.T) {
node, err := makeNode(1234, "")
defer node.Close()
Convey("It should create a node", t, func() {
So(err, ShouldBeNil)
So(node.NetAddr.String(), ShouldEqual, "/ip4/127.0.0.1/tcp/1234")
So(node.HashAddr.Pretty(), ShouldEqual, "QmNN6oDiV4GsfKDXPVmGLdBLLXCM28Jnm7pz7WD63aiwSG")
})
Convey("It should send between nodes", t, func() {
node2, err := makeNode(4321, "node2")
So(err, ShouldBeNil)
defer node2.Close()
node.Host.Peerstore().AddAddr(node2.HashAddr, node2.NetAddr, pstore.PermanentAddrTTL)
var payload string
node2.Host.SetStreamHandler("/testprotocol/1.0.0", func(s net.Stream) {
defer s.Close()
buf := make([]byte, 1024)
n, err := s.Read(buf)
if err != nil {
payload = err.Error()
} else {
payload = string(buf[:n])
}
_, err = s.Write([]byte("I got: " + payload))
if err != nil {
panic(err)
}
})
s, err := node.Host.NewStream(context.Background(), node2.HashAddr, "/testprotocol/1.0.0")
So(err, ShouldBeNil)
_, err = s.Write([]byte("greetings"))
So(err, ShouldBeNil)
out, err := ioutil.ReadAll(s)
So(err, ShouldBeNil)
So(payload, ShouldEqual, "greetings")
So(string(out), ShouldEqual, "I got: greetings")
})
}
func TestNewMessage(t *testing.T) {
node, err := makeNode(1234, "node1")
if err != nil {
panic(err)
}
defer node.Close()
Convey("It should create a new message", t, func() {
now := time.Now()
m := node.NewMessage(PUT_REQUEST, "fish")
So(now.Before(m.Time), ShouldBeTrue) // poor check, but at least makes sure the time was set to something just after the NewMessage call was made
So(m.Type, ShouldEqual, PUT_REQUEST)
So(m.Body, ShouldEqual, "fish")
So(m.From, ShouldEqual, node.HashAddr)
})
}
func TestNodeSend(t *testing.T) {
d := setupTestDir()
defer cleanupTestDir(d)
node1, err := makeNode(1234, "node1")
if err != nil {
panic(err)
}
defer node1.Close()
node2, err := makeNode(1235, "node2")
if err != nil {
panic(err)
}
defer node2.Close()
var h Holochain
h.path = d
h.node = node1
h.dht = NewDHT(&h)
Convey("It should start the DHT protocol", t, func() {
err := h.dht.StartDHT()
So(err, ShouldBeNil)
})
Convey("It should start the Src protocol", t, func() {
err := node2.StartSrc(&h)
So(err, ShouldBeNil)
})
node1.Host.Peerstore().AddAddr(node2.HashAddr, node2.NetAddr, pstore.PermanentAddrTTL)
node2.Host.Peerstore().AddAddr(node1.HashAddr, node1.NetAddr, pstore.PermanentAddrTTL)
Convey("It should fail on messages without a source", t, func() {
m := Message{Type: PUT_REQUEST, Body: "fish"}
r, err := node2.Send(DHTProtocol, node1.HashAddr, &m)
So(err, ShouldBeNil)
So(r.Type, ShouldEqual, ERROR_RESPONSE)
So(r.From, ShouldEqual, node1.HashAddr) // response comes from who we sent to
So(r.Body, ShouldEqual, "message must have a source")
})
Convey("It should fail on incorrect message types", t, func() {
m := node1.NewMessage(PUT_REQUEST, "fish")
r, err := node1.Send(SourceProtocol, node2.HashAddr, m)
So(err, ShouldBeNil)
So(r.Type, ShouldEqual, ERROR_RESPONSE)
So(r.From, ShouldEqual, node2.HashAddr) // response comes from who we sent to
So(r.Body, ShouldEqual, "message type 2 not in holochain-src protocol")
})
Convey("It should respond with queued on valid PUT_REQUESTS", t, func() {
hash, _ := NewHash("QmY8Mzg9F69e5P9AoQPYat6x5HEhc1TVGs11tmfNSzkqh2")
m := node2.NewMessage(PUT_REQUEST, hash)
r, err := node2.Send(DHTProtocol, node1.HashAddr, m)
So(err, ShouldBeNil)
So(r.Type, ShouldEqual, OK_RESPONSE)
So(r.From, ShouldEqual, node1.HashAddr) // response comes from who we sent to
So(r.Body, ShouldEqual, "queued")
})
}
func TestMessageCoding(t *testing.T) {
node, err := makeNode(1234, "node1")
if err != nil {
panic(err)
}
defer node.Close()
m := node.NewMessage(PUT_REQUEST, "fish")
var d []byte
Convey("It should encode and decode messages", t, func() {
d, err = m.Encode()
So(err, ShouldBeNil)
var m2 Message
r := bytes.NewReader(d)
err = m2.Decode(r)
So(err, ShouldBeNil)
So(fmt.Sprintf("%v", m), ShouldEqual, fmt.Sprintf("%v", &m2))
})
}
func TestSrcReceiver(t *testing.T) {
d, _, h := prepareTestChain("test")
defer cleanupTestDir(d)
Convey("SRC_VALIDATE should fail if body isn't a hash", t, func() {
m := h.node.NewMessage(SRC_VALIDATE, "fish")
_, err := SrcReceiver(h, m)
So(err.Error(), ShouldEqual, "expected hash")
})
Convey("SRC_VALIDATE should fail if hash doesn't exist", t, func() {
hash, _ := NewHash("QmY8Mzg9F69e5P9AoQPYat6x5HEhc1TVGs11tmfNSzkqh2")
m := h.node.NewMessage(SRC_VALIDATE, hash)
_, err := SrcReceiver(h, m)
So(err, ShouldNotBeNil)
So(err.Error(), ShouldEqual, "hash not found")
})
Convey("SRC_VALIDATE should return header or entry by hash", t, func() {
entry := GobEntry{C: "bogus entry data"}
h2, hd, err := h.NewEntry(time.Now(), "myData", &entry)
m := h.node.NewMessage(SRC_VALIDATE, h2)
r, err := SrcReceiver(h, m)
So(err, ShouldBeNil)
So(fmt.Sprintf("%v", r), ShouldEqual, fmt.Sprintf("%v", hd))
m = h.node.NewMessage(SRC_VALIDATE, hd.EntryLink)
r, err = SrcReceiver(h, m)
So(err, ShouldBeNil)
So(fmt.Sprintf("%v", r), ShouldEqual, fmt.Sprintf("%v", &entry))
})
}
/*
func TestFindPeer(t *testing.T) {
node1, err := makeNode(1234, "node1")
if err != nil {
panic(err)
}
defer node1.Close()
// generate a new unknown peerID
r := strings.NewReader("1234567890123456789012345678901234567890x")
key, _, err := ic.GenerateEd25519Key(r)
if err != nil {
panic(err)
}
pid, err := peer.IDFromPrivateKey(key)
if err != nil {
panic(err)
}
Convey("sending to an unknown peer should fail with no route to peer", t, func() {
m := Message{Type: PUT_REQUEST, Body: "fish"}
_, err := node1.Send(DHTProtocol, pid, &m)
//So(r, ShouldBeNil)
So(err, ShouldEqual, "fish")
})
}
*/
func makeNode(port int, id string) (*Node, error) {
listenaddr := fmt.Sprintf("/ip4/127.0.0.1/tcp/%d", port)
// use a constant reader so the key will be the same each time for the test...
r := strings.NewReader(id + "1234567890123456789012345678901234567890")
key, _, err := ic.GenerateEd25519Key(r)
if err != nil {
panic(err)
}
return NewNode(listenaddr, key)
}