forked from holochain/holochain-proto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdht_test.go
280 lines (236 loc) · 7.91 KB
/
dht_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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package holochain
import (
"fmt"
peer "github.com/libp2p/go-libp2p-peer"
. "github.com/smartystreets/goconvey/convey"
"testing"
"time"
)
func TestNewDHT(t *testing.T) {
d := setupTestDir()
defer cleanupTestDir(d)
var h Holochain
h.path = d
dht := NewDHT(&h)
Convey("It should initialize the DHT struct", t, func() {
So(dht.h, ShouldEqual, &h)
So(fileExists(h.path+"/dht.db"), ShouldBeTrue)
})
}
func TestPutGet(t *testing.T) {
d, _, h := prepareTestChain("test")
defer cleanupTestDir(d)
dht := h.dht
var id peer.ID = h.node.HashAddr
Convey("It should store and retrieve", t, func() {
hash, _ := NewHash("QmY8Mzg9F69e5P9AoQPYat655HEhc1TVGs11tmfNSzkqh2")
err := dht.put(hash, id, []byte("some value"))
So(err, ShouldBeNil)
data, err := dht.get(hash)
So(err, ShouldBeNil)
So(string(data), ShouldEqual, "some value")
hash, _ = NewHash("QmY8Mzg9F69e5P9AoQPYat6x5HEhc1TVGs11tmfNSzkqh2")
data, err = dht.get(hash)
So(data, ShouldBeNil)
So(err, ShouldEqual, ErrHashNotFound)
})
}
func TestPutGetMeta(t *testing.T) {
d := setupTestDir()
defer cleanupTestDir(d)
var h Holochain
h.path = d
dht := NewDHT(&h)
hash, _ := NewHash("QmY8Mzg9F69e5P9AoQPYat655HEhc1TVGs11tmfNSzkqh2")
metaHash1, _ := NewHash("QmY8Mzg9F69e5P9AoQPYat655HEhc1TVGs11tmfNSzkqh3")
metaHash2, _ := NewHash("QmY8Mzg9F69e5P9AoQPYat655HEhc1TVGs11tmfNSzkqh4")
Convey("It should fail if hash doesn't exist", t, func() {
e1 := GobEntry{C: "some data"}
err := dht.putMeta(hash, metaHash1, "someType", &e1)
So(err, ShouldEqual, ErrHashNotFound)
v, err := dht.getMeta(hash, "someType")
So(v, ShouldBeNil)
So(err, ShouldEqual, ErrHashNotFound)
})
var id peer.ID
err := dht.put(hash, id, []byte("some value"))
if err != nil {
panic(err)
}
Convey("It should store and retrieve meta values on a hash", t, func() {
data, err := dht.getMeta(hash, "someType")
So(err, ShouldNotBeNil)
So(err.Error(), ShouldEqual, "No values for someType")
e1 := GobEntry{C: "value 1"}
err = dht.putMeta(hash, metaHash1, "someType", &e1)
So(err, ShouldBeNil)
e2 := GobEntry{C: "value 2"}
err = dht.putMeta(hash, metaHash2, "someType", &e2)
So(err, ShouldBeNil)
e3 := GobEntry{C: "value 3"}
err = dht.putMeta(hash, metaHash1, "otherType", &e3)
So(err, ShouldBeNil)
data, err = dht.getMeta(hash, "someType")
So(err, ShouldBeNil)
So(len(data), ShouldEqual, 2)
m := data[0]
So(m.Content(), ShouldEqual, "value 1")
m = data[1]
So(m.Content(), ShouldEqual, "value 2")
data, err = dht.getMeta(hash, "otherType")
So(err, ShouldBeNil)
So(len(data), ShouldEqual, 1)
So(data[0].Content(), ShouldEqual, "value 3")
})
}
func TestFindNodeForHash(t *testing.T) {
d, _, h := prepareTestChain("test")
defer cleanupTestDir(d)
Convey("It should find a node", t, func() {
// for now the node it finds is ourself for any hash because we haven't implemented
// anything about neighborhoods or other nodes...
self := h.node.HashAddr
hash, err := NewHash("QmY8Mzg9F69e5P9AoQPYat655HEhc1TVGs11tmfNSzkqh2")
if err != nil {
panic(err)
}
node, err := h.dht.FindNodeForHash(hash)
So(err, ShouldBeNil)
So(node.HashAddr.Pretty(), ShouldEqual, self.Pretty())
})
}
func TestSend(t *testing.T) {
d, _, h := prepareTestChain("test")
defer cleanupTestDir(d)
node, err := NewNode("/ip4/127.0.0.1/tcp/1234", h.Agent().PrivKey())
if err != nil {
panic(err)
}
defer node.Close()
hash, _ := NewHash("QmY8Mzg9F69e5P9AoQPYat655HEhc1TVGs11tmfNSzkqh2")
Convey("send GET_REQUEST message for non existent hash should get error", t, func() {
r, err := h.dht.send(node.HashAddr, GET_REQUEST, hash)
So(r, ShouldBeNil)
So(err, ShouldEqual, ErrHashNotFound)
})
now := time.Unix(1, 1) // pick a constant time so the test will always work
e := GobEntry{C: "some data"}
_, hd, err := h.NewEntry(now, "myData", &e)
if err != nil {
panic(err)
}
// publish the entry data to the dht
hash = hd.EntryLink
Convey("before send PUT_REQUEST message queue should be empty", t, func() {
So(h.dht.Queue.Len(), ShouldEqual, 0)
})
Convey("after send PUT_REQUEST message queue should have the message in it", t, func() {
r, err := h.dht.send(node.HashAddr, PUT_REQUEST, hash)
So(err, ShouldBeNil)
So(r, ShouldEqual, "queued")
So(h.dht.Queue.Len(), ShouldEqual, 1)
messages, err := h.dht.Queue.Get(1)
So(err, ShouldBeNil)
m := messages[0].(*Message)
So(m.Type, ShouldEqual, PUT_REQUEST)
hs := m.Body.(Hash)
So(hs.Equal(&hash), ShouldBeTrue)
})
Convey("after a handled PUT_REQUEST data should be stored in DHT", t, func() {
r, err := h.dht.send(node.HashAddr, PUT_REQUEST, hash)
So(err, ShouldBeNil)
So(r, ShouldEqual, "queued")
h.dht.handlePutReqs()
hd, _ := h.chain.GetEntryHeader(hash)
So(hd.EntryLink.Equal(&hash), ShouldBeTrue)
})
Convey("send GET_REQUEST message should return content", t, func() {
r, err := h.dht.send(node.HashAddr, GET_REQUEST, hash)
So(err, ShouldBeNil)
So(fmt.Sprintf("%v", r), ShouldEqual, fmt.Sprintf("%v", &e))
})
}
func TestDHTReceiver(t *testing.T) {
d, _, h := prepareTestChain("test")
defer cleanupTestDir(d)
Convey("PUT_REQUEST should fail if body isn't a hash", t, func() {
m := h.node.NewMessage(PUT_REQUEST, "fish")
_, err := DHTReceiver(h, m)
So(err.Error(), ShouldEqual, "expected hash")
})
Convey("PUTMETA_REQUEST should fail if body not a good put meta request", t, func() {
m := h.node.NewMessage(PUTMETA_REQUEST, "fish")
_, err := DHTReceiver(h, m)
So(err.Error(), ShouldEqual, "expected meta struct")
})
hash, _ := NewHash("QmY8Mzg9F69e5P9AoQPYat655HEhc1TVGs11tmfNSzkqh2")
Convey("PUTMETA_REQUEST should fail if hash doesn't exist", t, func() {
me := MetaReq{O: hash, M: hash, T: "myMetaType"}
m := h.node.NewMessage(PUTMETA_REQUEST, me)
_, err := DHTReceiver(h, m)
So(err.Error(), ShouldEqual, "hash not found")
})
now := time.Unix(1, 1) // pick a constant time so the test will always work
e := GobEntry{C: "some data"}
_, hd, _ := h.NewEntry(now, "myData", &e)
hash = hd.EntryLink
Convey("PUT_REQUEST should queue a valid message", t, func() {
m := h.node.NewMessage(PUT_REQUEST, hash)
r, err := DHTReceiver(h, m)
So(err, ShouldBeNil)
So(r, ShouldEqual, "queued")
})
if err := h.dht.handlePutReqs(); err != nil {
panic(err)
}
Convey("GET_REQUEST should return the value of the has", t, func() {
m := h.node.NewMessage(GET_REQUEST, hash)
r, err := DHTReceiver(h, m)
So(err, ShouldBeNil)
So(fmt.Sprintf("%v", r), ShouldEqual, fmt.Sprintf("%v", &e))
})
e = GobEntry{C: "some meta data"}
_, hd, _ = h.NewEntry(now, "myMetaData", &e)
Convey("PUTMETA_REQUEST should store meta values", t, func() {
me := MetaReq{O: hash, M: hd.EntryLink, T: "myMetaType"}
m := h.node.NewMessage(PUTMETA_REQUEST, me)
r, err := DHTReceiver(h, m)
So(err, ShouldBeNil)
So(r, ShouldEqual, "queued")
// fake the handleputreqs
err = h.dht.handlePutReqs()
So(err, ShouldBeNil)
// check that it got put
meta, err := h.dht.getMeta(hash, "myMetaType")
So(err, ShouldBeNil)
So(meta[0].Content(), ShouldEqual, "some meta data")
})
Convey("GETMETA_REQUEST should retrieve meta values", t, func() {
mq := MetaQuery{H: hash, T: "myMetaType"}
m := h.node.NewMessage(GETMETA_REQUEST, mq)
r, err := DHTReceiver(h, m)
So(err, ShouldBeNil)
results := r.([]Entry)
So(results[0].Content(), ShouldEqual, "some meta data")
})
}
func TestHandlePutReqs(t *testing.T) {
d, _, h := prepareTestChain("test")
defer cleanupTestDir(d)
now := time.Unix(1, 1) // pick a constant time so the test will always work
e := GobEntry{C: "some data"}
_, hd, err := h.NewEntry(now, "myData", &e)
if err != nil {
panic(err)
}
m := h.node.NewMessage(PUT_REQUEST, hd.EntryLink)
err = h.dht.Queue.Put(m)
Convey("handle put request should pull data from source and verify it", t, func() {
err := h.dht.handlePutReqs()
So(err, ShouldBeNil)
data, err := h.dht.get(hd.EntryLink)
So(err, ShouldBeNil)
b, _ := e.Marshal()
So(fmt.Sprintf("%v", data), ShouldEqual, fmt.Sprintf("%v", b))
})
}