utils.go 2.86 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"

18
	cid "gx/ipfs/QmNp85zy9RLrQ5oQD4hPyS39ezrrXpcaa7R4Y9kxdWQLLQ/go-cid"
19
	node "gx/ipfs/QmPN7cwmpcc4DWXb4KTB9dNAJgjuPY69h3npsMfhRrQL9c/go-ipld-format"
20
	u "gx/ipfs/QmSU6eubNdhXjFBJBSksTp8kv8YRub8mGAPv8tVJHmL2EU/go-ipfs-util"
21 22 23 24 25 26 27 28 29 30 31 32
)

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

func GetDAGServ() mdag.DAGService {
	return mdagmock.Mock()
}

33 34 35 36 37 38 39
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
}
40

41 42 43
var UseProtoBufLeaves = NodeOpts{Prefix: mdag.V0CidPrefix()}
var UseRawLeaves = NodeOpts{Prefix: mdag.V0CidPrefix(), ForceRawLeaves: true, RawLeavesUsed: true}
var UseCidV1 = NodeOpts{Prefix: mdag.V1CidPrefix(), RawLeavesUsed: true}
44

45
func GetNode(t testing.TB, dserv mdag.DAGService, data []byte, opts NodeOpts) node.Node {
46
	in := bytes.NewReader(data)
47 48 49 50

	dbp := h.DagBuilderParams{
		Dagserv:   dserv,
		Maxlinks:  h.DefaultLinksPerBlock,
51 52
		Prefix:    &opts.Prefix,
		RawLeaves: opts.RawLeavesUsed,
53 54 55
	}

	node, err := trickle.TrickleLayout(dbp.New(SizeSplitterGen(500)(in)))
56 57 58 59 60 61 62
	if err != nil {
		t.Fatal(err)
	}

	return node
}

63 64
func GetEmptyNode(t testing.TB, dserv mdag.DAGService, opts NodeOpts) node.Node {
	return GetNode(t, dserv, []byte{}, opts)
65 66
}

67
func GetRandomNode(t testing.TB, dserv mdag.DAGService, size int64, opts NodeOpts) ([]byte, node.Node) {
68 69 70 71 72 73
	in := io.LimitReader(u.NewTimeSeededRand(), size)
	buf, err := ioutil.ReadAll(in)
	if err != nil {
		t.Fatal(err)
	}

74
	node := GetNode(t, dserv, buf, opts)
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
	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
}

90
func PrintDag(nd *mdag.ProtoNode, ds mdag.DAGService, indent int) {
91 92 93 94 95 96 97 98 99
	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()))
100
	if len(nd.Links()) > 0 {
101 102
		fmt.Println()
	}
103
	for _, lnk := range nd.Links() {
104 105 106 107
		child, err := lnk.GetNode(context.Background(), ds)
		if err != nil {
			panic(err)
		}
108
		PrintDag(child.(*mdag.ProtoNode), ds, indent+1)
109
	}
110
	if len(nd.Links()) > 0 {
111 112 113 114 115 116
		for i := 0; i < indent; i++ {
			fmt.Print(" ")
		}
	}
	fmt.Println("}")
}