-
Notifications
You must be signed in to change notification settings - Fork 212
/
testutil.go
135 lines (120 loc) · 3.04 KB
/
testutil.go
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
129
130
131
132
133
134
135
package types
import (
"math/rand"
)
// RandomBytes generates random data in bytes for testing.
func RandomBytes(size int) []byte {
b := make([]byte, size)
_, err := rand.Read(b)
if err != nil {
return nil
}
return b
}
// RandomHash generates random Hash32 for testing.
func RandomHash() Hash32 {
var h Hash32
h.SetBytes(RandomBytes(Hash32Length))
return h
}
// RandomBeacon generates random beacon in bytes for testing.
func RandomBeacon() Beacon {
return BytesToBeacon(RandomBytes(BeaconSize))
}
// RandomActiveSet generates a random set of ATXIDs of the specified size.
func RandomActiveSet(size int) []ATXID {
ids := make([]ATXID, 0, size)
for i := 0; i < size; i++ {
ids = append(ids, RandomATXID())
}
return ids
}
// RandomTXSet generates a random set of TransactionID of the specified size.
func RandomTXSet(size int) []TransactionID {
ids := make([]TransactionID, 0, size)
for i := 0; i < size; i++ {
ids = append(ids, RandomTransactionID())
}
return ids
}
// RandomATXID generates a random ATXID for testing.
func RandomATXID() ATXID {
var b [ATXIDSize]byte
_, err := rand.Read(b[:])
if err != nil {
return EmptyATXID
}
return ATXID(b)
}
// RandomNodeID generates a random NodeID for testing.
func RandomNodeID() NodeID {
var b [NodeIDSize]byte
_, err := rand.Read(b[:])
if err != nil {
return EmptyNodeID
}
return NodeID(b)
}
// RandomBallotID generates a random BallotID for testing.
func RandomBallotID() BallotID {
var b [hash20Length]byte // TODO(mafa): BallotIDSize is 32???
_, err := rand.Read(b[:])
if err != nil {
return EmptyBallotID
}
return BallotID(b)
}
// RandomProposalID generates a random ProposalID for testing.
func RandomProposalID() ProposalID {
var b [hash20Length]byte // TODO(mafa): ProposalIDSize is 32???
_, err := rand.Read(b[:])
if err != nil {
return ProposalID{}
}
return ProposalID(b)
}
// RandomBlockID generates a random ProposalID for testing.
func RandomBlockID() BlockID {
return BlockID(RandomProposalID())
}
// RandomTransactionID generates a random TransactionID for testing.
func RandomTransactionID() TransactionID {
var b [TransactionIDSize]byte
_, err := rand.Read(b[:])
if err != nil {
return TransactionID{}
}
return TransactionID(b)
}
// RandomBallot generates a Ballot with random content for testing.
func RandomBallot() *Ballot {
return &Ballot{
InnerBallot: InnerBallot{
Layer: LayerID(10),
AtxID: RandomATXID(),
RefBallot: RandomBallotID(),
},
Votes: Votes{
Base: RandomBallotID(),
Support: []Vote{{ID: RandomBlockID()}, {ID: RandomBlockID()}},
},
}
}
// RandomEdSignature generates a random (not necessarily valid) EdSignature for testing.
func RandomEdSignature() EdSignature {
var b [EdSignatureSize]byte
_, err := rand.Read(b[:])
if err != nil {
return EdSignature{}
}
return EdSignature(b)
}
// RandomVrfSignature generates a random VrfSignature for testing.
func RandomVrfSignature() VrfSignature {
var b [VrfSignatureSize]byte
_, err := rand.Read(b[:])
if err != nil {
return VrfSignature{}
}
return VrfSignature(b)
}