-
Notifications
You must be signed in to change notification settings - Fork 45
/
car_test.go
299 lines (266 loc) · 6.59 KB
/
car_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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package car_test
import (
"bytes"
"context"
"encoding/hex"
"io"
"strings"
"testing"
"github.com/ipfs/go-cid"
format "github.com/ipfs/go-ipld-format"
"github.com/ipfs/go-merkledag"
dstest "github.com/ipfs/go-merkledag/test"
car "github.com/ipld/go-car"
)
func assertAddNodes(t *testing.T, ds format.DAGService, nds ...format.Node) {
for _, nd := range nds {
if err := ds.Add(context.Background(), nd); err != nil {
t.Fatal(err)
}
}
}
func TestRoundtrip(t *testing.T) {
ctx := context.Background()
dserv := dstest.Mock()
a := merkledag.NewRawNode([]byte("aaaa"))
b := merkledag.NewRawNode([]byte("bbbb"))
c := merkledag.NewRawNode([]byte("cccc"))
nd1 := &merkledag.ProtoNode{}
nd1.AddNodeLink("cat", a)
nd2 := &merkledag.ProtoNode{}
nd2.AddNodeLink("first", nd1)
nd2.AddNodeLink("dog", b)
nd3 := &merkledag.ProtoNode{}
nd3.AddNodeLink("second", nd2)
nd3.AddNodeLink("bear", c)
assertAddNodes(t, dserv, a, b, c, nd1, nd2, nd3)
buf := new(bytes.Buffer)
if err := car.WriteCar(context.Background(), dserv, []cid.Cid{nd3.Cid()}, buf); err != nil {
t.Fatal(err)
}
bserv := dstest.Bserv()
ch, err := car.LoadCar(ctx, bserv.Blockstore(), buf)
if err != nil {
t.Fatal(err)
}
if len(ch.Roots) != 1 {
t.Fatal("should have one root")
}
if !ch.Roots[0].Equals(nd3.Cid()) {
t.Fatal("got wrong cid")
}
bs := bserv.Blockstore()
for _, nd := range []format.Node{a, b, c, nd1, nd2, nd3} {
has, err := bs.Has(ctx, nd.Cid())
if err != nil {
t.Fatal(err)
}
if !has {
t.Fatal("should have cid in blockstore")
}
}
}
// fixture is a clean single-block, single-root CAR
const fixtureStr = "3aa265726f6f747381d82a58250001711220151fe9e73c6267a7060c6f6c4cca943c236f4b196723489608edb42a8b8fa80b6776657273696f6e012c01711220151fe9e73c6267a7060c6f6c4cca943c236f4b196723489608edb42a8b8fa80ba165646f646779f5"
func TestEOFHandling(t *testing.T) {
fixture, err := hex.DecodeString(fixtureStr)
if err != nil {
t.Fatal(err)
}
load := func(t *testing.T, byts []byte) *car.CarReader {
cr, err := car.NewCarReader(bytes.NewReader(byts))
if err != nil {
t.Fatal(err)
}
blk, err := cr.Next()
if err != nil {
t.Fatal(err)
}
if blk.Cid().String() != "bafyreiavd7u6opdcm6tqmddpnrgmvfb4enxuwglhenejmchnwqvixd5ibm" {
t.Fatal("unexpected CID")
}
return cr
}
t.Run("CleanEOF", func(t *testing.T) {
cr := load(t, fixture)
blk, err := cr.Next()
if err != io.EOF {
t.Fatal("Didn't get expected EOF")
}
if blk != nil {
t.Fatal("EOF returned expected block")
}
})
t.Run("BadVarint", func(t *testing.T) {
fixtureBadVarint := append(fixture, 160)
cr := load(t, fixtureBadVarint)
blk, err := cr.Next()
if err != io.ErrUnexpectedEOF {
t.Fatal("Didn't get unexpected EOF")
}
if blk != nil {
t.Fatal("EOF returned unexpected block")
}
})
t.Run("TruncatedBlock", func(t *testing.T) {
fixtureTruncatedBlock := append(fixture, 100, 0, 0)
cr := load(t, fixtureTruncatedBlock)
blk, err := cr.Next()
if err != io.ErrUnexpectedEOF {
t.Fatal("Didn't get unexpected EOF")
}
if blk != nil {
t.Fatal("EOF returned unexpected block")
}
})
}
func TestEmptyRootsInHeader(t *testing.T) {
testCases := []struct {
name string
errorOnEmptyRoots bool
hex string
errStr string // either the whole error string
}{
{
"errorOnEmptyRoots:true",
true,
"0aa16776657273696f6e01",
"empty car, no roots",
}, {
"errorOnEmptyRoots:false",
false,
"0aa16776657273696f6e01",
"",
},
}
makeCar := func(t *testing.T, byts string, flag bool) error {
fixture, err := hex.DecodeString(byts)
if err != nil {
t.Fatal(err)
}
_, err = car.NewCarReaderWithOptions(bytes.NewReader(fixture), car.WithErrorOnEmptyRoots(flag))
return err
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err := makeCar(t, tc.hex, tc.errorOnEmptyRoots)
if err == nil && tc.errorOnEmptyRoots {
t.Fatal("expected error from empty roots, didn't get one")
}
if err != nil && !tc.errorOnEmptyRoots {
t.Fatal("expected no error from empty roots, but got one")
}
if tc.errStr != "" {
if err.Error() != tc.errStr {
t.Fatalf("bad error: %v", err)
}
}
})
}
}
func TestBadHeaders(t *testing.T) {
testCases := []struct {
name string
hex string
errStr string // either the whole error string
errPfx string // or just the prefix
}{
{
"{version:2}",
"0aa16776657273696f6e02",
"invalid car version: 2",
"",
},
{
// an unfortunate error because we don't use a pointer
"{roots:[baeaaaa3bmjrq]}",
"13a165726f6f747381d82a480001000003616263",
"invalid car version: 0",
"",
}, {
"{version:\"1\",roots:[baeaaaa3bmjrq]}",
"1da265726f6f747381d82a4800010000036162636776657273696f6e6131",
"", "invalid header: ",
}, {
"{version:1}",
"0aa16776657273696f6e01",
"empty car, no roots",
"",
}, {
"{version:1,roots:{cid:baeaaaa3bmjrq}}",
"20a265726f6f7473a163636964d82a4800010000036162636776657273696f6e01",
"",
"invalid header: ",
}, {
"{version:1,roots:[baeaaaa3bmjrq],blip:true}",
"22a364626c6970f565726f6f747381d82a4800010000036162636776657273696f6e01",
"",
"invalid header: ",
}, {
"[1,[]]",
"03820180",
"",
"invalid header: ",
}, {
// this is an unfortunate error, it'd be nice to catch it better but it's
// very unlikely we'd ever see this in practice
"null",
"01f6",
"",
"invalid car version: 0",
},
}
makeCar := func(t *testing.T, byts string) error {
fixture, err := hex.DecodeString(byts)
if err != nil {
t.Fatal(err)
}
_, err = car.NewCarReader(bytes.NewReader(fixture))
return err
}
t.Run("Sanity check {version:1,roots:[baeaaaa3bmjrq]}", func(t *testing.T) {
err := makeCar(t, "1ca265726f6f747381d82a4800010000036162636776657273696f6e01")
if err != nil {
t.Fatal(err)
}
})
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err := makeCar(t, tc.hex)
if err == nil {
t.Fatal("expected error from bad header, didn't get one")
}
if tc.errStr != "" {
if err.Error() != tc.errStr {
t.Fatalf("bad error: %v", err)
}
} else {
if !strings.HasPrefix(err.Error(), tc.errPfx) {
t.Fatalf("bad error: %v", err)
}
}
})
}
}
func BenchmarkNewCarReader_small(b *testing.B) {
b.ReportAllocs()
fixture, err := hex.DecodeString(fixtureStr)
if err != nil {
b.Fatal(err)
}
for i := 0; i < b.N; i++ {
cr, err := car.NewCarReader(bytes.NewReader(fixture))
if err != nil {
b.Fatal(err)
}
for {
_, err := cr.Next()
if err == io.EOF {
break
}
if err != nil {
b.Fatal(err)
}
}
}
}