forked from holochain/holochain-proto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchain_test.go
285 lines (238 loc) · 7.27 KB
/
chain_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
281
282
283
284
285
package holochain
import (
"bytes"
"fmt"
ic "github.com/libp2p/go-libp2p-crypto"
. "github.com/smartystreets/goconvey/convey"
"reflect"
"testing"
"time"
)
func TestNewChain(t *testing.T) {
Convey("it should make an empty chain", t, func() {
c := NewChain()
So(len(c.Headers), ShouldEqual, 0)
So(len(c.Entries), ShouldEqual, 0)
})
}
func TestNewChainFromFile(t *testing.T) {
d := setupTestDir()
defer cleanupTestDir(d)
h, key, now := chainTestSetup()
var c *Chain
var err error
path := d + "/chain.dat"
Convey("it should make an empty chain with encoder", t, func() {
c, err = NewChainFromFile(h, path)
So(err, ShouldBeNil)
So(c.s, ShouldNotBeNil)
So(fileExists(path), ShouldBeTrue)
})
e := GobEntry{C: "some data1"}
c.AddEntry(h, now, "myData1", &e, key)
e = GobEntry{C: "some other data2"}
c.AddEntry(h, now, "myData2", &e, key)
dump := c.String()
c.s.Close()
c, err = NewChainFromFile(h, path)
Convey("it should load chain data if availble", t, func() {
So(err, ShouldBeNil)
So(c.String(), ShouldEqual, dump)
})
e = GobEntry{C: "yet other data"}
c.AddEntry(h, now, "yourData", &e, key)
dump = c.String()
c.s.Close()
c, err = NewChainFromFile(h, path)
Convey("should continue to append data after reload", t, func() {
So(err, ShouldBeNil)
So(c.String(), ShouldEqual, dump)
})
}
func TestTop(t *testing.T) {
c := NewChain()
Convey("it should return an nil for an empty chain", t, func() {
hd := c.Top()
So(hd, ShouldBeNil)
hd = c.TopType("myData")
So(hd, ShouldBeNil)
})
h, key, now := chainTestSetup()
e := GobEntry{C: "some data"}
c.AddEntry(h, now, "myData", &e, key)
Convey("Top it should return the top header", t, func() {
hd := c.Top()
So(hd, ShouldEqual, c.Headers[0])
})
Convey("TopType should return nil for non existent type", t, func() {
hd := c.TopType("otherData")
So(hd, ShouldBeNil)
})
Convey("TopType should return header for correct type", t, func() {
hd := c.TopType("myData")
So(hd, ShouldEqual, c.Headers[0])
})
c.AddEntry(h, now, "otherData", &e, key)
Convey("TopType should return headers for both types", t, func() {
hd := c.TopType("myData")
So(hd, ShouldEqual, c.Headers[0])
hd = c.TopType("otherData")
So(hd, ShouldEqual, c.Headers[1])
})
}
func TestTopType(t *testing.T) {
c := NewChain()
Convey("it should return nil for an empty chain", t, func() {
hd := c.TopType("myData")
So(hd, ShouldBeNil)
})
Convey("it should return nil for an chain with no entries of the type", t, func() {
})
}
func TestAddEntry(t *testing.T) {
c := NewChain()
h, key, now := chainTestSetup()
Convey("it should add nil to the chain", t, func() {
e := GobEntry{C: "some data"}
hash, err := c.AddEntry(h, now, "myData", &e, key)
So(err, ShouldBeNil)
So(len(c.Headers), ShouldEqual, 1)
So(len(c.Entries), ShouldEqual, 1)
So(c.TypeTops["myData"], ShouldEqual, 0)
So(hash.Equal(&c.Hashes[0]), ShouldBeTrue)
})
}
func TestGet(t *testing.T) {
c := NewChain()
h, key, now := chainTestSetup()
e1 := GobEntry{C: "some data"}
h1, _ := c.AddEntry(h, now, "myData", &e1, key)
hd1, err1 := c.Get(h1)
e2 := GobEntry{C: "some other data"}
h2, _ := c.AddEntry(h, now, "myData", &e2, key)
hd2, err2 := c.Get(h2)
Convey("it should get header by hash or by Entry hash", t, func() {
So(hd1, ShouldEqual, c.Headers[0])
So(err1, ShouldBeNil)
ehd, err := c.GetEntryHeader(hd1.EntryLink)
So(ehd, ShouldEqual, c.Headers[0])
So(err, ShouldBeNil)
So(hd2, ShouldEqual, c.Headers[1])
So(err2, ShouldBeNil)
ehd, err = c.GetEntryHeader(hd2.EntryLink)
So(ehd, ShouldEqual, c.Headers[1])
So(err, ShouldBeNil)
})
Convey("it should get entry by hash", t, func() {
ed, err := c.GetEntry(hd1.EntryLink)
So(err, ShouldBeNil)
So(fmt.Sprintf("%v", &e1), ShouldEqual, fmt.Sprintf("%v", ed))
ed, err = c.GetEntry(hd2.EntryLink)
So(err, ShouldBeNil)
So(fmt.Sprintf("%v", &e2), ShouldEqual, fmt.Sprintf("%v", ed))
})
Convey("it should return nil for non existent hash", t, func() {
hash, _ := NewHash("QmNiCwBNA8MWDADTFVq1BonUEJbS2SvjAoNkZZrhEwcuUi")
hd, err := c.Get(hash)
So(hd, ShouldBeNil)
So(err, ShouldEqual, ErrHashNotFound)
})
}
func TestMarshal(t *testing.T) {
c := NewChain()
h, key, now := chainTestSetup()
e := GobEntry{C: "some data"}
c.AddEntry(h, now, "myData1", &e, key)
e = GobEntry{C: "some other data"}
c.AddEntry(h, now, "myData2", &e, key)
e = GobEntry{C: "and more data"}
c.AddEntry(h, now, "myData3", &e, key)
Convey("it should be able to marshal and unmarshal", t, func() {
var b bytes.Buffer
err := c.MarshalChain(&b)
So(err, ShouldBeNil)
c1, err := UnmarshalChain(&b)
So(err, ShouldBeNil)
So(c1.String(), ShouldEqual, c.String())
// confirm that internal structures are properly set up
for i := 0; i < len(c.Headers); i++ {
So(c.Hashes[i].String(), ShouldEqual, c1.Hashes[i].String())
}
So(reflect.DeepEqual(c.TypeTops, c1.TypeTops), ShouldBeTrue)
So(reflect.DeepEqual(c.Hmap, c1.Hmap), ShouldBeTrue)
So(reflect.DeepEqual(c.Emap, c1.Emap), ShouldBeTrue)
})
}
func TestWalkChain(t *testing.T) {
c := NewChain()
h, key, now := chainTestSetup()
e := GobEntry{C: "some data"}
c.AddEntry(h, now, "myData1", &e, key)
e = GobEntry{C: "some other data"}
c.AddEntry(h, now, "myData2", &e, key)
e = GobEntry{C: "and more data"}
c.AddEntry(h, now, "myData3", &e, key)
Convey("it should walk back from the top through all entries", t, func() {
var x string
var i int
err := c.Walk(func(key *Hash, h *Header, entry Entry) error {
i++
x += fmt.Sprintf("%d:%v ", i, entry.(*GobEntry).C)
return nil
})
So(err, ShouldBeNil)
So(x, ShouldEqual, "1:and more data 2:some other data 3:some data ")
})
}
func TestValidateChain(t *testing.T) {
c := NewChain()
h, key, now := chainTestSetup()
e := GobEntry{C: "some data"}
c.AddEntry(h, now, "myData1", &e, key)
e = GobEntry{C: "some other data"}
c.AddEntry(h, now, "myData1", &e, key)
e = GobEntry{C: "and more data"}
c.AddEntry(h, now, "myData1", &e, key)
Convey("it should validate", t, func() {
So(c.Validate(h), ShouldBeNil)
})
Convey("it should fail to validate if we diddle some bits", t, func() {
c.Entries[0].(*GobEntry).C = "fish"
So(c.Validate(h).Error(), ShouldEqual, "entry hash mismatch at link 0")
c.Entries[0].(*GobEntry).C = "some data"
c.Headers[1].TypeLink = NullHash()
So(c.Validate(h).Error(), ShouldEqual, "header hash mismatch at link 1")
})
}
/*
func TestPersistingChain(t *testing.T) {
c := NewChain()
var b bytes.Buffer
c.encoder = gob.NewEncoder(&b)
h, key, now := chainTestSetup()
e := GobEntry{C: "some data"}
c.AddEntry(h, now, "myData1", &e, key)
e = GobEntry{C: "some other data"}
c.AddEntry(h, now, "myData1", &e, key)
e = GobEntry{C: "and more data"}
c.AddEntry(h, now, "myData1", &e, key)
dec := gob.NewDecoder(&b)
var header *Header
var entry Entry
header, entry, err := readPair(dec)
Convey("it should have added items to the writer", t, func() {
So(err, ShouldBeNil)
So(fmt.Sprintf("%v", header), ShouldEqual, fmt.Sprintf("%v", c.Headers[0]))
So(fmt.Sprintf("%v", entry), ShouldEqual, fmt.Sprintf("%v", c.Entries[0]))
})
}
*/
func chainTestSetup() (hs HashSpec, key ic.PrivKey, now time.Time) {
a, _ := NewAgent(IPFS, "agent id")
key = a.PrivKey()
hc := Holochain{HashType: "sha2-256"}
hP := &hc
hP.PrepareHashType()
hs = hP.hashSpec
return
}