testutil.go 2.72 KB
Newer Older
1 2 3
package testutil

import (
4 5 6 7
	"bytes"

	random "github.com/jbenet/go-random"

8 9
	bsmsg "github.com/ipfs/go-bitswap/message"
	"github.com/ipfs/go-bitswap/wantlist"
hannahhoward's avatar
hannahhoward committed
10
	"github.com/ipfs/go-block-format"
11 12 13 14 15 16 17
	cid "github.com/ipfs/go-cid"
	blocksutil "github.com/ipfs/go-ipfs-blocksutil"
	peer "github.com/libp2p/go-libp2p-peer"
)

var blockGenerator = blocksutil.NewBlockGenerator()
var prioritySeq int
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
var seedSeq int64

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

// GenerateBlocksOfSize generates a series of blocks of the given byte size
func GenerateBlocksOfSize(n int, size int64) []blocks.Block {
	generatedBlocks := make([]blocks.Block, 0, n)
	for i := 0; i < n; i++ {
		seedSeq++
		b := blocks.NewBlock(randomBytes(size, seedSeq))
		generatedBlocks = append(generatedBlocks, b)

	}
	return generatedBlocks
}
37

38
// GenerateCids produces n content identifiers.
39 40 41 42 43 44 45 46 47
func GenerateCids(n int) []cid.Cid {
	cids := make([]cid.Cid, 0, n)
	for i := 0; i < n; i++ {
		c := blockGenerator.Next().Cid()
		cids = append(cids, c)
	}
	return cids
}

48
// GenerateWantlist makes a populated wantlist.
49 50 51 52 53 54 55 56 57 58
func GenerateWantlist(n int, ses uint64) *wantlist.ThreadSafe {
	wl := wantlist.NewThreadSafe()
	for i := 0; i < n; i++ {
		prioritySeq++
		entry := wantlist.NewRefEntry(blockGenerator.Next().Cid(), prioritySeq)
		wl.AddEntry(entry, ses)
	}
	return wl
}

59
// GenerateMessageEntries makes fake bitswap message entries.
60
func GenerateMessageEntries(n int, isCancel bool) []*bsmsg.Entry {
61 62 63 64 65 66 67 68 69 70 71 72 73 74
	bsmsgs := make([]*bsmsg.Entry, 0, n)
	for i := 0; i < n; i++ {
		prioritySeq++
		msg := &bsmsg.Entry{
			Entry:  wantlist.NewRefEntry(blockGenerator.Next().Cid(), prioritySeq),
			Cancel: isCancel,
		}
		bsmsgs = append(bsmsgs, msg)
	}
	return bsmsgs
}

var peerSeq int

75
// GeneratePeers creates n peer ids.
76 77 78 79 80 81 82 83 84 85 86 87
func GeneratePeers(n int) []peer.ID {
	peerIds := make([]peer.ID, 0, n)
	for i := 0; i < n; i++ {
		peerSeq++
		p := peer.ID(peerSeq)
		peerIds = append(peerIds, p)
	}
	return peerIds
}

var nextSession uint64

88
// GenerateSessionID make a unit session identifier.
89 90 91 92 93
func GenerateSessionID() uint64 {
	nextSession++
	return uint64(nextSession)
}

94
// ContainsPeer returns true if a peer is found n a list of peers.
95 96 97 98 99 100 101 102
func ContainsPeer(peers []peer.ID, p peer.ID) bool {
	for _, n := range peers {
		if p == n {
			return true
		}
	}
	return false
}
hannahhoward's avatar
hannahhoward committed
103

104 105 106 107 108
// IndexOf returns the index of a given cid in an array of blocks
func IndexOf(blks []blocks.Block, c cid.Cid) int {
	for i, n := range blks {
		if n.Cid() == c {
			return i
hannahhoward's avatar
hannahhoward committed
109 110
		}
	}
111 112 113 114 115 116
	return -1
}

// ContainsBlock returns true if a block is found n a list of blocks
func ContainsBlock(blks []blocks.Block, block blocks.Block) bool {
	return IndexOf(blks, block.Cid()) != -1
hannahhoward's avatar
hannahhoward committed
117
}