testselector.go 2.77 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
package testselector

import (
	"bytes"
	"errors"

	cid "github.com/ipfs/go-cid"
	gsselector "github.com/ipfs/go-graphsync/selector"
	ipld "github.com/ipfs/go-ipld-format"
	"github.com/jbenet/go-random"
	mh "github.com/multiformats/go-multihash"
)

var seedSeq int64

const (
	blockSize = 512
)

func randomBytes(n int64, seed int64) []byte {
	data := new(bytes.Buffer)
	random.WritePseudoRandomBytes(n, data, seed)
	return data.Bytes()
}

type byteNode struct {
	data    []byte
	builder cid.Builder
}

var v0CidPrefix = cid.Prefix{
	Codec:    cid.DagProtobuf,
	MhLength: -1,
	MhType:   mh.SHA2_256,
	Version:  0,
}

// ErrEmptyNode is just a simple error code for stubbing out IPLD Node interface
// methods
var ErrEmptyNode = errors.New("dummy node")

func (n *byteNode) Resolve([]string) (interface{}, []string, error) {
	return nil, nil, ErrEmptyNode
}

func (n *byteNode) Tree(string, int) []string {
	return nil
}

func (n *byteNode) ResolveLink([]string) (*ipld.Link, []string, error) {
	return nil, nil, ErrEmptyNode
}

func (n *byteNode) Copy() ipld.Node {
	return &byteNode{}
}

func (n *byteNode) Cid() cid.Cid {
	c, err := n.builder.Sum(n.RawData())
	if err != nil {
		return cid.Cid{}
	}
	return c
}

func (n *byteNode) Links() []*ipld.Link {
	return nil
}

func (n *byteNode) Loggable() map[string]interface{} {
	return nil
}

func (n *byteNode) String() string {
	return string(n.data)
}

func (n *byteNode) RawData() []byte {
	return n.data
}

func (n *byteNode) Size() (uint64, error) {
	return 0, nil
}

func (n *byteNode) Stat() (*ipld.NodeStat, error) {
	return &ipld.NodeStat{}, nil
}

func newNode(data []byte) *byteNode {
	return &byteNode{
		data:    data,
		builder: v0CidPrefix,
	}
}

// MockDecodeSelectorFunc decodes raw data to a type that satisfies
// the Selector interface
func MockDecodeSelectorFunc(data []byte) gsselector.Selector {
	return newNode(data)
}

// MockDecodeSelectionResponseFunc decodes raw data to a type that
// satisfies the SelectionResponse interface
func MockDecodeSelectionResponseFunc(data []byte) gsselector.SelectionResponse {
	return newNode(data)
}

// MockDecodeRootNodeFunc decodes raw data to a type that satisfies an
// IPLD node interface
func MockDecodeRootNodeFunc(data []byte) ipld.Node {
	return newNode(data)
}

// GenerateSelector returns a new mock Selector
func GenerateSelector() gsselector.Selector {
	node := newNode(randomBytes(blockSize, seedSeq))
	seedSeq++
	return node
}

// GenerateSelectionResponse returns a new mock SelectionResponse
func GenerateSelectionResponse() gsselector.SelectionResponse {
	node := newNode(randomBytes(blockSize, seedSeq))
	seedSeq++
	return node
}

129 130
// GenerateRootCid generates a new mock CID to serve as a root
func GenerateRootCid() cid.Cid {
131 132
	node := newNode(randomBytes(blockSize, seedSeq))
	seedSeq++
133
	return node.Cid()
134
}