utils.go 3.41 KB
Newer Older
1 2 3 4
package testu

import (
	"bytes"
5
	"context"
6 7 8 9 10 11
	"fmt"
	"io"
	"io/ioutil"
	"testing"

	"github.com/ipfs/go-ipfs/importer/chunk"
12 13
	h "github.com/ipfs/go-ipfs/importer/helpers"
	trickle "github.com/ipfs/go-ipfs/importer/trickle"
14 15 16 17
	mdag "github.com/ipfs/go-ipfs/merkledag"
	mdagmock "github.com/ipfs/go-ipfs/merkledag/test"
	ft "github.com/ipfs/go-ipfs/unixfs"

Steven Allen's avatar
Steven Allen committed
18 19 20
	u "gx/ipfs/QmNiJuT8Ja3hMVpBHXv3Q6dwmperaQ6JjLtpMQgMCD7xvx/go-ipfs-util"
	mh "gx/ipfs/QmZyZDi491cCNTLfAhwcaDii2Kg4pwKRkhqQzURGDvY6ua/go-multihash"
	cid "gx/ipfs/QmcZfnkapfECQGcLZaf9B79NRg7cRa9EnZh4LSbkCzwNvY/go-cid"
21
	ipld "gx/ipfs/Qme5bWv7wtjUNGsK2BNGVUFPKiuxWrsqrtvYwCLRw8YFES/go-ipld-format"
22 23 24 25 26 27 28 29
)

func SizeSplitterGen(size int64) chunk.SplitterGen {
	return func(r io.Reader) chunk.Splitter {
		return chunk.NewSizeSplitter(r, size)
	}
}

Steven Allen's avatar
Steven Allen committed
30
// GetDAGServ returns a mock DAGService.
31
func GetDAGServ() ipld.DAGService {
32 33 34
	return mdagmock.Mock()
}

Kevin Atkinson's avatar
Kevin Atkinson committed
35
// NodeOpts is used by GetNode, GetEmptyNode and GetRandomNode
36 37 38 39 40 41 42
type NodeOpts struct {
	Prefix cid.Prefix
	// ForceRawLeaves if true will force the use of raw leaves
	ForceRawLeaves bool
	// RawLeavesUsed is true if raw leaves or either implicitly or explicitly enabled
	RawLeavesUsed bool
}
43

44 45 46
var UseProtoBufLeaves = NodeOpts{Prefix: mdag.V0CidPrefix()}
var UseRawLeaves = NodeOpts{Prefix: mdag.V0CidPrefix(), ForceRawLeaves: true, RawLeavesUsed: true}
var UseCidV1 = NodeOpts{Prefix: mdag.V1CidPrefix(), RawLeavesUsed: true}
47 48 49 50 51 52 53
var UseBlake2b256 NodeOpts

func init() {
	UseBlake2b256 = UseCidV1
	UseBlake2b256.Prefix.MhType = mh.Names["blake2b-256"]
	UseBlake2b256.Prefix.MhLength = -1
}
54

Steven Allen's avatar
Steven Allen committed
55
// GetNode returns a unixfs file node with the specified data.
56
func GetNode(t testing.TB, dserv ipld.DAGService, data []byte, opts NodeOpts) ipld.Node {
57
	in := bytes.NewReader(data)
58 59 60 61

	dbp := h.DagBuilderParams{
		Dagserv:   dserv,
		Maxlinks:  h.DefaultLinksPerBlock,
62 63
		Prefix:    &opts.Prefix,
		RawLeaves: opts.RawLeavesUsed,
64 65 66
	}

	node, err := trickle.TrickleLayout(dbp.New(SizeSplitterGen(500)(in)))
67 68 69 70 71 72 73
	if err != nil {
		t.Fatal(err)
	}

	return node
}

Steven Allen's avatar
Steven Allen committed
74
// GetEmptyNode returns an empty unixfs file node.
75
func GetEmptyNode(t testing.TB, dserv ipld.DAGService, opts NodeOpts) ipld.Node {
76
	return GetNode(t, dserv, []byte{}, opts)
77 78
}

Steven Allen's avatar
Steven Allen committed
79
// GetRandomNode returns a random unixfs file node.
80
func GetRandomNode(t testing.TB, dserv ipld.DAGService, size int64, opts NodeOpts) ([]byte, ipld.Node) {
81 82 83 84 85 86
	in := io.LimitReader(u.NewTimeSeededRand(), size)
	buf, err := ioutil.ReadAll(in)
	if err != nil {
		t.Fatal(err)
	}

87
	node := GetNode(t, dserv, buf, opts)
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
	return buf, node
}

func ArrComp(a, b []byte) error {
	if len(a) != len(b) {
		return fmt.Errorf("Arrays differ in length. %d != %d", len(a), len(b))
	}
	for i, v := range a {
		if v != b[i] {
			return fmt.Errorf("Arrays differ at index: %d", i)
		}
	}
	return nil
}

Steven Allen's avatar
Steven Allen committed
103
// PrintDag pretty-prints the given dag to stdout.
104
func PrintDag(nd *mdag.ProtoNode, ds ipld.DAGService, indent int) {
105 106 107 108 109 110 111 112 113
	pbd, err := ft.FromBytes(nd.Data())
	if err != nil {
		panic(err)
	}

	for i := 0; i < indent; i++ {
		fmt.Print(" ")
	}
	fmt.Printf("{size = %d, type = %s, children = %d", pbd.GetFilesize(), pbd.GetType().String(), len(pbd.GetBlocksizes()))
114
	if len(nd.Links()) > 0 {
115 116
		fmt.Println()
	}
117
	for _, lnk := range nd.Links() {
118 119 120 121
		child, err := lnk.GetNode(context.Background(), ds)
		if err != nil {
			panic(err)
		}
122
		PrintDag(child.(*mdag.ProtoNode), ds, indent+1)
123
	}
124
	if len(nd.Links()) > 0 {
125 126 127 128 129 130
		for i := 0; i < indent; i++ {
			fmt.Print(" ")
		}
	}
	fmt.Println("}")
}