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

import (
Jakub Sztandera's avatar
Jakub Sztandera committed
4
	"math/rand"
5

6 7
	bsmsg "github.com/ipfs/go-bitswap/message"
	"github.com/ipfs/go-bitswap/wantlist"
8
	blocks "github.com/ipfs/go-block-format"
9 10
	cid "github.com/ipfs/go-cid"
	blocksutil "github.com/ipfs/go-ipfs-blocksutil"
Raúl Kripalani's avatar
Raúl Kripalani committed
11
	peer "github.com/libp2p/go-libp2p-core/peer"
12 13 14 15
)

var blockGenerator = blocksutil.NewBlockGenerator()
var prioritySeq int
16 17 18 19 20

// 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++ {
Jakub Sztandera's avatar
Jakub Sztandera committed
21
		// rand.Read never errors
Steven Allen's avatar
Steven Allen committed
22
		buf := make([]byte, size)
Jakub Sztandera's avatar
Jakub Sztandera committed
23 24
		rand.Read(buf)
		b := blocks.NewBlock(buf)
25 26 27 28 29
		generatedBlocks = append(generatedBlocks, b)

	}
	return generatedBlocks
}
30

31
// GenerateCids produces n content identifiers.
32 33 34 35 36 37 38 39 40
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
}

41
// GenerateWantlist makes a populated wantlist.
42 43
func GenerateWantlist(n int, ses uint64) *wantlist.SessionTrackedWantlist {
	wl := wantlist.NewSessionTrackedWantlist()
44 45 46 47 48 49 50 51
	for i := 0; i < n; i++ {
		prioritySeq++
		entry := wantlist.NewRefEntry(blockGenerator.Next().Cid(), prioritySeq)
		wl.AddEntry(entry, ses)
	}
	return wl
}

52
// GenerateMessageEntries makes fake bitswap message entries.
53 54
func GenerateMessageEntries(n int, isCancel bool) []bsmsg.Entry {
	bsmsgs := make([]bsmsg.Entry, 0, n)
55 56
	for i := 0; i < n; i++ {
		prioritySeq++
57
		msg := bsmsg.Entry{
58 59 60 61 62 63 64 65 66 67
			Entry:  wantlist.NewRefEntry(blockGenerator.Next().Cid(), prioritySeq),
			Cancel: isCancel,
		}
		bsmsgs = append(bsmsgs, msg)
	}
	return bsmsgs
}

var peerSeq int

68
// GeneratePeers creates n peer ids.
69 70 71 72 73 74 75 76 77 78 79 80
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

81
// GenerateSessionID make a unit session identifier.
82 83 84 85 86
func GenerateSessionID() uint64 {
	nextSession++
	return uint64(nextSession)
}

87
// ContainsPeer returns true if a peer is found n a list of peers.
88 89 90 91 92 93 94 95
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
96

97 98 99 100 101
// 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
102 103
		}
	}
104 105 106 107 108 109
	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
110
}