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

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

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

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

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

	}
	return generatedBlocks
}
31

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

42
// GenerateMessageEntries makes fake bitswap message entries.
43 44
func GenerateMessageEntries(n int, isCancel bool) []bsmsg.Entry {
	bsmsgs := make([]bsmsg.Entry, 0, n)
45 46
	for i := 0; i < n; i++ {
		prioritySeq++
47
		msg := bsmsg.Entry{
48 49 50 51 52 53 54 55 56 57
			Entry:  wantlist.NewRefEntry(blockGenerator.Next().Cid(), prioritySeq),
			Cancel: isCancel,
		}
		bsmsgs = append(bsmsgs, msg)
	}
	return bsmsgs
}

var peerSeq int

58
// GeneratePeers creates n peer ids.
59 60 61 62
func GeneratePeers(n int) []peer.ID {
	peerIds := make([]peer.ID, 0, n)
	for i := 0; i < n; i++ {
		peerSeq++
63
		p := peer.ID(fmt.Sprint(i))
64 65 66 67 68 69 70
		peerIds = append(peerIds, p)
	}
	return peerIds
}

var nextSession uint64

71
// GenerateSessionID make a unit session identifier.
72 73 74 75 76
func GenerateSessionID() uint64 {
	nextSession++
	return uint64(nextSession)
}

77
// ContainsPeer returns true if a peer is found n a list of peers.
78 79 80 81 82 83 84 85
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
86

87 88 89 90 91
// 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
92 93
		}
	}
94 95 96 97 98 99
	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
100
}
dirkmc's avatar
dirkmc committed
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 129 130 131 132 133 134 135 136 137 138 139 140

// ContainsKey returns true if a key is found n a list of CIDs.
func ContainsKey(ks []cid.Cid, c cid.Cid) bool {
	for _, k := range ks {
		if c == k {
			return true
		}
	}
	return false
}

// MatchKeysIgnoreOrder returns true if the lists of CIDs match (even if
// they're in a different order)
func MatchKeysIgnoreOrder(ks1 []cid.Cid, ks2 []cid.Cid) bool {
	if len(ks1) != len(ks2) {
		return false
	}

	for _, k := range ks1 {
		if !ContainsKey(ks2, k) {
			return false
		}
	}
	return true
}

// MatchPeersIgnoreOrder returns true if the lists of peers match (even if
// they're in a different order)
func MatchPeersIgnoreOrder(ps1 []peer.ID, ps2 []peer.ID) bool {
	if len(ps1) != len(ps2) {
		return false
	}

	for _, p := range ps1 {
		if !ContainsPeer(ps2, p) {
			return false
		}
	}
	return true
}