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

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

Steven Allen's avatar
Steven Allen committed
6
	bsmsg "github.com/ipfs/go-bitswap/message"
7
	"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
)

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

var peerSeq int

57
// GeneratePeers creates n peer ids.
58 59 60 61 62 63 64 65 66 67 68 69
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

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

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

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

// 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
}