utils.go 3.53 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
// SizeSplitterGen creates a generator.
25 26 27 28 29 30
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
31
// GetDAGServ returns a mock DAGService.
32
func GetDAGServ() ipld.DAGService {
33 34 35
	return mdagmock.Mock()
}

Kevin Atkinson's avatar
Kevin Atkinson committed
36
// NodeOpts is used by GetNode, GetEmptyNode and GetRandomNode
37 38 39 40 41 42 43
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
}
44

45 46 47 48 49 50 51
// Some shorthands for NodeOpts.
var (
	UseProtoBufLeaves = NodeOpts{Prefix: mdag.V0CidPrefix()}
	UseRawLeaves      = NodeOpts{Prefix: mdag.V0CidPrefix(), ForceRawLeaves: true, RawLeavesUsed: true}
	UseCidV1          = NodeOpts{Prefix: mdag.V1CidPrefix(), RawLeavesUsed: true}
	UseBlake2b256     NodeOpts
)
52 53 54 55 56 57

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

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

	dbp := h.DagBuilderParams{
		Dagserv:   dserv,
		Maxlinks:  h.DefaultLinksPerBlock,
66 67
		Prefix:    &opts.Prefix,
		RawLeaves: opts.RawLeavesUsed,
68 69
	}

70
	node, err := trickle.Layout(dbp.New(SizeSplitterGen(500)(in)))
71 72 73 74 75 76 77
	if err != nil {
		t.Fatal(err)
	}

	return node
}

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

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

91
	node := GetNode(t, dserv, buf, opts)
92 93 94
	return buf, node
}

95
// ArrComp checks if two byte slices are the same.
96 97 98 99 100 101 102 103 104 105 106 107
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
108
// PrintDag pretty-prints the given dag to stdout.
109
func PrintDag(nd *mdag.ProtoNode, ds ipld.DAGService, indent int) {
110 111 112 113 114 115 116 117 118
	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()))
119
	if len(nd.Links()) > 0 {
120 121
		fmt.Println()
	}
122
	for _, lnk := range nd.Links() {
123 124 125 126
		child, err := lnk.GetNode(context.Background(), ds)
		if err != nil {
			panic(err)
		}
127
		PrintDag(child.(*mdag.ProtoNode), ds, indent+1)
128
	}
129
	if len(nd.Links()) > 0 {
130 131 132 133 134 135
		for i := 0; i < indent; i++ {
			fmt.Print(" ")
		}
	}
	fmt.Println("}")
}