-
Notifications
You must be signed in to change notification settings - Fork 2
/
carstream_test.go
227 lines (212 loc) · 6.14 KB
/
carstream_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
package frisbii_test
import (
"bytes"
"context"
"crypto/rand"
"io"
"testing"
blocks "github.com/ipfs/go-block-format"
"github.com/ipfs/go-cid"
gstestutil "github.com/ipfs/go-graphsync/testutil"
"github.com/ipfs/go-unixfsnode"
unixfs "github.com/ipfs/go-unixfsnode/testutil"
"github.com/ipld/frisbii"
"github.com/ipld/go-car/v2"
"github.com/ipld/go-ipld-prime/datamodel"
"github.com/ipld/go-ipld-prime/linking"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
"github.com/ipld/go-ipld-prime/storage/memstore"
trustlessutils "github.com/ipld/go-trustless-utils"
trustlesstestutil "github.com/ipld/go-trustless-utils/testutil"
"github.com/stretchr/testify/require"
)
func TestStreamCar(t *testing.T) {
ctx := context.Background()
chainLsys := makeLsys()
tbc := gstestutil.SetupBlockChain(ctx, t, chainLsys, 1000, 100)
allChainBlocks := tbc.AllBlocks()
fileLsys := makeLsys()
fileEnt := unixfs.GenerateFile(t, &fileLsys, rand.Reader, 1<<20)
dirLsys := makeLsys()
var dirEnt unixfs.DirEntry
for {
dirEnt = GenerateNoDupes(func() unixfs.DirEntry { return unixfs.GenerateDirectory(t, &dirLsys, rand.Reader, 4<<20, false) })
if len(dirEnt.Children) > 2 { // we want at least 3 children to test the path subset selector
break
}
}
shardedDirLsys := makeLsys()
var shardedDirEnt unixfs.DirEntry
for {
shardedDirEnt = GenerateNoDupes(func() unixfs.DirEntry { return unixfs.GenerateDirectory(t, &shardedDirLsys, rand.Reader, 4<<20, true) })
if len(dirEnt.Children) > 2 { // we want at least 3 children to test the path subset selector
break
}
}
testCases := []struct {
name string
path datamodel.Path
scope trustlessutils.DagScope
root cid.Cid
lsys linking.LinkSystem
validate func(t *testing.T, r io.Reader)
expectedErr string
}{
{
name: "chain: all blocks",
scope: trustlessutils.DagScopeAll,
root: tbc.TipLink.(cidlink.Link).Cid,
lsys: chainLsys,
validate: func(t *testing.T, r io.Reader) {
root, blks := carToBlocks(t, r)
require.Equal(t, tbc.TipLink.(cidlink.Link).Cid, root)
require.Equal(t, allChainBlocks, blks)
},
},
{
name: "chain: just root",
scope: trustlessutils.DagScopeBlock,
root: tbc.TipLink.(cidlink.Link).Cid,
lsys: chainLsys,
validate: func(t *testing.T, r io.Reader) {
root, blks := carToBlocks(t, r)
require.Equal(t, tbc.TipLink.(cidlink.Link).Cid, root)
require.Equal(t, []blocks.Block{allChainBlocks[0]}, blks)
},
},
{
name: "unixfs file",
scope: trustlessutils.DagScopeAll,
root: fileEnt.Root,
lsys: fileLsys,
validate: func(t *testing.T, r io.Reader) {
root, blks := carToBlocks(t, r)
require.Equal(t, fileEnt.Root, root)
require.ElementsMatch(t, fileEnt.SelfCids, blkCids(blks))
},
},
{
name: "unixfs directory",
scope: trustlessutils.DagScopeAll,
root: dirEnt.Root,
lsys: dirLsys,
validate: func(t *testing.T, r io.Reader) {
root, blks := carToBlocks(t, r)
require.Equal(t, dirEnt.Root, root)
require.ElementsMatch(t, entCids(dirEnt), blkCids(blks))
},
},
{
name: "unixfs sharded directory",
scope: trustlessutils.DagScopeAll,
root: shardedDirEnt.Root,
lsys: shardedDirLsys,
validate: func(t *testing.T, r io.Reader) {
root, blks := carToBlocks(t, r)
require.Equal(t, shardedDirEnt.Root, root)
require.ElementsMatch(t, entCids(shardedDirEnt), blkCids(blks))
},
},
{
// path that (probably) doesn't exist, shouldn't error but shouldn't
// return much (this ought to be tested better with a fixture)
name: "unixfs sharded directory, no such path",
scope: trustlessutils.DagScopeAll,
path: datamodel.ParsePath(shardedDirEnt.Children[0].Path + "/nope"),
root: shardedDirEnt.Root,
lsys: shardedDirLsys,
validate: func(t *testing.T, r io.Reader) {
root, blks := carToBlocks(t, r)
require.Equal(t, shardedDirEnt.Root, root)
cids := blkCids(blks)
require.Contains(t, cids, shardedDirEnt.Root)
require.NotEqual(t, len(entCids(shardedDirEnt)), cids) // shouldn't contain the full thing!
},
},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
req := require.New(t)
var buf bytes.Buffer
err := frisbii.StreamCar(ctx, tc.lsys, &buf, trustlessutils.Request{Root: tc.root, Path: tc.path.String(), Scope: tc.scope, Duplicates: false})
if tc.expectedErr != "" {
req.EqualError(err, tc.expectedErr)
} else {
req.NoError(err)
tc.validate(t, &buf)
}
})
}
}
func carToBlocks(t *testing.T, r io.Reader) (cid.Cid, []blocks.Block) {
rdr, err := car.NewBlockReader(r)
require.NoError(t, err)
require.Len(t, rdr.Roots, 1)
blks := make([]blocks.Block, 0)
for {
blk, err := rdr.Next()
if err == io.EOF {
break
}
require.NoError(t, err)
blks = append(blks, blk)
}
return rdr.Roots[0], blks
}
func blkCids(blks []blocks.Block) []cid.Cid {
cids := make([]cid.Cid, len(blks))
for i, blk := range blks {
cids[i] = blk.Cid()
}
return cids
}
func entCids(ent unixfs.DirEntry) []cid.Cid {
cids := make([]cid.Cid, 0)
var _entCids func(ent unixfs.DirEntry)
_entCids = func(ent unixfs.DirEntry) {
cids = append(cids, ent.SelfCids...)
for _, c := range ent.Children {
_entCids(c)
}
}
_entCids(ent)
return cids
}
func makeLsys() linking.LinkSystem {
store := &trustlesstestutil.CorrectedMemStore{ParentStore: &memstore.Store{
Bag: make(map[string][]byte),
}}
lsys := cidlink.DefaultLinkSystem()
lsys.SetReadStorage(store)
lsys.SetWriteStorage(store)
unixfsnode.AddUnixFSReificationToLinkSystem(&lsys)
return lsys
}
// TODO: this should probably be an option in unixfsnode/testutil, for
// generators to strictly not return a DAG with duplicates
func GenerateNoDupes(gen func() unixfs.DirEntry) unixfs.DirEntry {
var check func(unixfs.DirEntry) bool
var seen map[cid.Cid]struct{}
check = func(e unixfs.DirEntry) bool {
for _, c := range e.SelfCids {
if _, ok := seen[c]; ok {
return false
}
seen[c] = struct{}{}
}
for _, c := range e.Children {
if !check(c) {
return false
}
}
return true
}
for {
seen = make(map[cid.Cid]struct{})
gend := gen()
if check(gend) {
return gend
}
}
}