Skip to content

Commit

Permalink
test: add test for CarOffsetWriter dag traverse order
Browse files Browse the repository at this point in the history
  • Loading branch information
rvagg committed Aug 3, 2022
1 parent 3fe1cc0 commit 141961e
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions car/car_offset_writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"math/rand"
"testing"

blocks "github.com/ipfs/go-block-format"
"github.com/ipfs/go-blockservice"
"github.com/ipfs/go-cidutil"
"github.com/ipfs/go-datastore"
Expand All @@ -15,13 +16,61 @@ import (
chunk "github.com/ipfs/go-ipfs-chunker"
format "github.com/ipfs/go-ipld-format"
"github.com/ipfs/go-merkledag"
merkledagpb "github.com/ipfs/go-merkledag/pb"
"github.com/ipfs/go-unixfs/importer/balanced"
"github.com/ipfs/go-unixfs/importer/helpers"
"github.com/ipld/go-car"
mh "github.com/multiformats/go-multihash"
"github.com/stretchr/testify/require"
)

func TestCarOffsetWriterDagOrder(t *testing.T) {
ctx := context.Background()
ds := dss.MutexWrap(datastore.NewMapDatastore())
bs := bstore.NewBlockstore(ds)
bserv := blockservice.New(bs, nil)
aaaa := "aaaa"
aaaaBlk := merkledag.NewRawNode([]byte(aaaa))
require.NoError(t, bserv.AddBlock(ctx, aaaaBlk))
bbbb := "bbbb"
bbbbBlk := merkledag.NewRawNode([]byte(bbbb))
require.NoError(t, bserv.AddBlock(ctx, bbbbBlk))
pbn := &merkledagpb.PBNode{
Links: []*merkledagpb.PBLink{
{Hash: bbbbBlk.Cid().Bytes(), Name: &bbbb},
{Hash: aaaaBlk.Cid().Bytes(), Name: &aaaa},
},
}
rootByts, err := pbn.Marshal()
rootBlk := blocks.NewBlock(rootByts)
require.NoError(t, err)
require.NoError(t, bserv.AddBlock(ctx, rootBlk))

fullCarCow := NewCarOffsetWriter(rootBlk.Cid(), bs, NewBlockInfoCache())
var fullBuff bytes.Buffer
require.NoError(t, fullCarCow.Write(context.Background(), &fullBuff, 0))

reader, err := car.NewCarReader(bytes.NewReader(fullBuff.Bytes()))
require.NoError(t, err)

// header
require.Equal(t, 1, len(reader.Header.Roots))
require.Equal(t, rootBlk.Cid(), reader.Header.Roots[0])
blk, err := reader.Next()
require.NoError(t, err)

// 3 blocks, in the correct, unsorted order - root, bbbb, aaaa
require.Equal(t, blk.Cid(), rootBlk.Cid())
blk, err = reader.Next()
require.NoError(t, err)
require.Equal(t, blk.Cid(), bbbbBlk.Cid())
blk, err = reader.Next()
require.NoError(t, err)
require.Equal(t, blk.Cid(), aaaaBlk.Cid())
_, err = reader.Next()
require.Error(t, err)
}

func TestCarOffsetWriter(t *testing.T) {
ds := dss.MutexWrap(datastore.NewMapDatastore())
bs := bstore.NewBlockstore(ds)
Expand Down

0 comments on commit 141961e

Please sign in to comment.