Skip to content

Commit a3996b8

Browse files
committed
fix: add cross-impl shard test
Ref: ipfs/js-ipfs-unixfs#358
1 parent 7db26c0 commit a3996b8

File tree

1 file changed

+45
-5
lines changed

1 file changed

+45
-5
lines changed

data/builder/dir_test.go

+45-5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package builder
33
import (
44
"bytes"
55
"fmt"
6+
"io"
67
"os"
78
"path/filepath"
89
"testing"
@@ -12,18 +13,15 @@ import (
1213
dagpb "github.com/ipld/go-codec-dagpb"
1314
"github.com/ipld/go-ipld-prime"
1415
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
16+
"github.com/multiformats/go-multihash"
1517
"github.com/stretchr/testify/require"
1618
)
1719

1820
func mkEntries(cnt int, ls *ipld.LinkSystem) ([]dagpb.PBLink, error) {
1921
entries := make([]dagpb.PBLink, 0, cnt)
2022
for i := 0; i < cnt; i++ {
2123
r := bytes.NewBufferString(fmt.Sprintf("%d", i))
22-
f, s, err := BuildUnixFSFile(r, "", ls)
23-
if err != nil {
24-
return nil, err
25-
}
26-
e, err := BuildUnixFSDirectoryEntry(fmt.Sprintf("file %d", i), int64(s), f)
24+
e, err := mkEntry(r, fmt.Sprintf("file %d", i), ls)
2725
if err != nil {
2826
return nil, err
2927
}
@@ -32,6 +30,48 @@ func mkEntries(cnt int, ls *ipld.LinkSystem) ([]dagpb.PBLink, error) {
3230
return entries, nil
3331
}
3432

33+
func mkEntry(r io.Reader, name string, ls *ipld.LinkSystem) (dagpb.PBLink, error) {
34+
f, s, err := BuildUnixFSFile(r, "", ls)
35+
if err != nil {
36+
return nil, err
37+
}
38+
return BuildUnixFSDirectoryEntry(name, int64(s), f)
39+
}
40+
41+
// Cross-impl reference test: directory of files with single character
42+
// names, starting from ' ' and ending with '~', but excluding the special
43+
// characters '/' and '.'. Each file should contain a single byte with the
44+
// same value as the character in its name. Files are added to a sharded
45+
// directory with a fanout of 16, using CIDv1 throughout, and should result
46+
// in the root CID of:
47+
//
48+
// bafybeihnipspiyy3dctpcx7lv655qpiuy52d7b2fzs52dtrjqwmvbiux44
49+
func TestBuildUnixFSDirectoryShardAltFanout_Reference(t *testing.T) {
50+
ls := cidlink.DefaultLinkSystem()
51+
storage := cidlink.Memory{}
52+
ls.StorageReadOpener = storage.OpenRead
53+
ls.StorageWriteOpener = storage.OpenWrite
54+
entries := make([]dagpb.PBLink, 0)
55+
for ch := ' '; ch <= '~'; ch++ {
56+
if ch == '/' || ch == '.' {
57+
continue
58+
}
59+
s := string(ch)
60+
r := bytes.NewBuffer([]byte(s))
61+
e, err := mkEntry(r, s, &ls)
62+
require.NoError(t, err)
63+
entries = append(entries, e)
64+
}
65+
lnk, sz, err := BuildUnixFSShardedDirectory(16, multihash.MURMUR3X64_64, entries, &ls)
66+
require.NoError(t, err)
67+
var totStored int
68+
for _, blk := range storage.Bag {
69+
totStored += len(blk)
70+
}
71+
require.Equal(t, totStored, int(sz))
72+
require.Equal(t, "bafybeihnipspiyy3dctpcx7lv655qpiuy52d7b2fzs52dtrjqwmvbiux44", lnk.String())
73+
}
74+
3575
func TestBuildUnixFSDirectory(t *testing.T) {
3676
ls := cidlink.DefaultLinkSystem()
3777
storage := cidlink.Memory{}

0 commit comments

Comments
 (0)