-
Notifications
You must be signed in to change notification settings - Fork 5
/
rand.go
300 lines (271 loc) · 8.94 KB
/
rand.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package protorand
import (
"fmt"
"math/rand"
"reflect"
"time"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/types/dynamicpb"
"google.golang.org/protobuf/types/known/durationpb"
"google.golang.org/protobuf/types/known/timestamppb"
)
var (
// Chars is the set of characters used to generate random strings.
Chars = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
)
// ProtoRand is a source of random values for protobuf fields.
type ProtoRand struct {
rand *rand.Rand
MaxCollectionElements int
// MaxDepth is the maximum stack depth for recursion, which prevents stack overflows if a message is (directly or
// transitively) self-referential.
MaxDepth int
}
// New creates a new ProtoRand.
func New() *ProtoRand {
return &ProtoRand{
rand: rand.New(rand.NewSource(time.Now().UnixNano())),
MaxCollectionElements: 10,
MaxDepth: 5,
}
}
// Seed sets the seed of the random generator.
func (p *ProtoRand) Seed(seed int64) {
p.rand = rand.New(rand.NewSource(seed))
}
// Gen generates a new proto.Message having random values in its fields.
// The input is used to specify the type of the generated message.
// The input itself is immutable.
func (p *ProtoRand) Gen(in proto.Message) (proto.Message, error) {
mds := in.ProtoReflect().Descriptor()
dm, err := p.NewDynamicProtoRand(mds)
if err != nil {
return nil, err
}
out := reflect.New(reflect.ValueOf(in).Elem().Type()).Interface().(proto.Message)
proto.Merge(out, dm)
return out, nil
}
// NewDynamicProtoRand creates dynamicpb with assigning random values to a proto.
func (p *ProtoRand) NewDynamicProtoRand(mds protoreflect.MessageDescriptor) (*dynamicpb.Message, error) {
return p.newDynamicProtoRand(mds, p.MaxDepth)
}
func (p *ProtoRand) newDynamicProtoRand(mds protoreflect.MessageDescriptor, allowedDepth int) (*dynamicpb.Message, error) {
getRandValue := func(fd protoreflect.FieldDescriptor) (protoreflect.Value, error) {
switch fd.FullName() {
case "google.protobuf.Timestamp":
const minTimestamp = -62135596800 // Seconds between 1970-01-01T00:00:00Z and 0001-01-01T00:00:00Z, inclusive
const maxTimestamp = +253402300799 // Seconds between 1970-01-01T00:00:00Z and 9999-12-31T23:59:59Z, inclusive
const maxNanos = 1e9 // exclusive
t := ×tamppb.Timestamp{
Seconds: p.rand.Int63n(maxTimestamp-minTimestamp+1) + minTimestamp,
Nanos: p.rand.Int31n(maxNanos),
}
return protoreflect.ValueOf(t), nil
case "google.protobuf.Duration":
const absDuration = 315576000000 // 10000yr * 365.25day/yr * 24hr/day * 60min/hr * 60sec/min
const maxNanos = 1e9 // exclusive
s := p.rand.Int63n(2*absDuration+1) - absDuration
n := int32(0)
switch {
case s == 0:
n = p.rand.Int31n(2*maxNanos+1) - maxNanos
case s > 0:
n = p.rand.Int31n(maxNanos + 1)
case s < 0:
n = p.rand.Int31n(maxNanos+1) - maxNanos
}
d := &durationpb.Duration{
Seconds: s,
Nanos: n,
}
return protoreflect.ValueOf(d), nil
}
switch fd.Kind() {
case protoreflect.Int32Kind:
return protoreflect.ValueOfInt32(p.randInt32()), nil
case protoreflect.Int64Kind:
return protoreflect.ValueOfInt64(p.randInt64()), nil
case protoreflect.Sint32Kind:
return protoreflect.ValueOfInt32(p.randInt32()), nil
case protoreflect.Sint64Kind:
return protoreflect.ValueOfInt64(p.randInt64()), nil
case protoreflect.Uint32Kind:
return protoreflect.ValueOfUint32(p.randUint32()), nil
case protoreflect.Uint64Kind:
return protoreflect.ValueOfUint64(p.randUint64()), nil
case protoreflect.Fixed32Kind:
return protoreflect.ValueOfUint32(p.randUint32()), nil
case protoreflect.Fixed64Kind:
return protoreflect.ValueOfUint64(p.randUint64()), nil
case protoreflect.Sfixed32Kind:
return protoreflect.ValueOfInt32(p.randInt32()), nil
case protoreflect.Sfixed64Kind:
return protoreflect.ValueOfInt64(p.randInt64()), nil
case protoreflect.FloatKind:
return protoreflect.ValueOfFloat32(p.randFloat32()), nil
case protoreflect.DoubleKind:
return protoreflect.ValueOfFloat64(p.randFloat64()), nil
case protoreflect.StringKind:
return protoreflect.ValueOfString(p.randString()), nil
case protoreflect.BoolKind:
return protoreflect.ValueOfBool(p.randBool()), nil
case protoreflect.EnumKind:
return protoreflect.ValueOfEnum(p.chooseEnumValueRandomly(fd.Enum().Values())), nil
case protoreflect.BytesKind:
return protoreflect.ValueOfBytes(p.randBytes()), nil
case protoreflect.MessageKind:
msg := fd.Message()
switch msg.FullName() {
case "google.protobuf.Timestamp":
const minTimestamp = -62135596800 // Seconds between 1970-01-01T00:00:00Z and 0001-01-01T00:00:00Z, inclusive
const maxTimestamp = +253402300799 // Seconds between 1970-01-01T00:00:00Z and 9999-12-31T23:59:59Z, inclusive
const maxNanos = 1e9 // exclusive
t := ×tamppb.Timestamp{
Seconds: p.rand.Int63n(maxTimestamp-minTimestamp+1) + minTimestamp,
Nanos: p.rand.Int31n(maxNanos),
}
return protoreflect.ValueOf(t.ProtoReflect()), nil
case "google.protobuf.Duration":
const absDuration = 315576000000 // 10000yr * 365.25day/yr * 24hr/day * 60min/hr * 60sec/min
const maxNanos = 1e9 // exclusive
s := p.rand.Int63n(2*absDuration+1) - absDuration
n := int32(0)
switch {
case s == 0:
n = p.rand.Int31n(2*maxNanos+1) - maxNanos
case s > 0:
n = p.rand.Int31n(maxNanos + 1)
case s < 0:
n = p.rand.Int31n(maxNanos+1) - maxNanos
}
d := &durationpb.Duration{
Seconds: s,
Nanos: n,
}
return protoreflect.ValueOf(d.ProtoReflect()), nil
}
// process recursively (if we have more stacks to give...)
if allowedDepth > 0 {
rm, err := p.newDynamicProtoRand(msg, allowedDepth-1)
if err != nil {
return protoreflect.Value{}, err
}
return protoreflect.ValueOfMessage(rm), nil
}
// recursed too deep; just return nil
return protoreflect.Value{}, nil
default:
return protoreflect.Value{}, fmt.Errorf("unexpected type: %v", fd.Kind())
}
}
// decide which fields in each OneOf will be populated in advance
populatedOneOfField := map[protoreflect.Name]protoreflect.FieldNumber{}
oneOfs := mds.Oneofs()
for i := 0; i < oneOfs.Len(); i++ {
oneOf := oneOfs.Get(i)
populatedOneOfField[oneOf.Name()] = p.chooseOneOfFieldRandomly(oneOf).Number()
}
dm := dynamicpb.NewMessage(mds)
fds := mds.Fields()
for k := 0; k < fds.Len(); k++ {
fd := fds.Get(k)
// If a field is in OneOf, check if the field should be populated
if oneOf := fd.ContainingOneof(); oneOf != nil {
populatedFieldNum := populatedOneOfField[oneOf.Name()]
if populatedFieldNum != fd.Number() {
continue
}
}
if fd.IsList() {
list := dm.Mutable(fd).List()
n := p.rand.Intn(p.MaxCollectionElements) + 1
for i := 0; i < n; i++ {
value, err := getRandValue(fd)
if err != nil {
return nil, err
}
if value.Interface() != nil {
list.Append(value)
}
}
dm.Set(fd, protoreflect.ValueOfList(list))
continue
}
if fd.IsMap() {
mp := dm.Mutable(fd).Map()
n := p.rand.Intn(p.MaxCollectionElements) + 1
for i := 0; i < n; i++ {
key, err := getRandValue(fd.MapKey())
if err != nil {
return nil, err
}
value, err := getRandValue(fd.MapValue())
if err != nil {
return nil, err
}
if key.Interface() != nil && value.Interface() != nil {
mp.Set(protoreflect.MapKey(key), protoreflect.Value(value))
}
}
dm.Set(fd, protoreflect.ValueOfMap(mp))
continue
}
value, err := getRandValue(fd)
if err != nil {
return nil, err
}
if value.Interface() != nil {
dm.Set(fd, value)
}
}
return dm, nil
}
func (p *ProtoRand) randInt32() int32 {
return p.rand.Int31()
}
func (p *ProtoRand) randInt64() int64 {
return p.rand.Int63()
}
func (p *ProtoRand) randUint32() uint32 {
return p.rand.Uint32()
}
func (p *ProtoRand) randUint64() uint64 {
return p.rand.Uint64()
}
func (p *ProtoRand) randFloat32() float32 {
return p.rand.Float32()
}
func (p *ProtoRand) randFloat64() float64 {
return p.rand.Float64()
}
func (p *ProtoRand) randBytes() []byte {
return []byte(p.randString())
}
func (p *ProtoRand) randString() string {
b := make([]rune, 10) // TODO: make the length randomly or use a predefined length?
for i := range b {
b[i] = Chars[p.rand.Intn(len(Chars))]
}
return string(b)
}
func (p *ProtoRand) randBool() bool {
return p.rand.Int31()%2 == 0
}
func (p *ProtoRand) chooseEnumValueRandomly(values protoreflect.EnumValueDescriptors) protoreflect.EnumNumber {
ln := values.Len()
if ln <= 1 {
return 0
}
value := values.Get(p.rand.Intn(ln - 1))
return value.Number()
}
func (p *ProtoRand) chooseOneOfFieldRandomly(oneOf protoreflect.OneofDescriptor) protoreflect.FieldDescriptor {
ln := oneOf.Fields().Len()
if ln == 1 {
return oneOf.Fields().Get(0)
}
index := p.rand.Intn(ln - 1)
return oneOf.Fields().Get(index)
}