-
Notifications
You must be signed in to change notification settings - Fork 2
/
random.go
203 lines (172 loc) · 4.89 KB
/
random.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
package randomize
import (
"crypto/md5"
"fmt"
"math/rand"
"strconv"
"strings"
"time"
"github.com/gofrs/uuid"
"github.com/volatiletech/strmangle"
)
const alphabetAll = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
const alphabetLowerAlpha = "abcdefghijklmnopqrstuvwxyz"
// Str creates a randomized string from printable characters in the alphabet
func Str(nextInt func() int64, ln int) string {
str := make([]byte, ln)
for i := 0; i < ln; i++ {
str[i] = byte(alphabetAll[nextInt()%int64(len(alphabetAll))])
}
return string(str)
}
// FormattedString checks a field type to see if it's in a range of special
// values and if so returns a randomized string for it.
func FormattedString(nextInt func() int64, fieldType string) (string, bool) {
if strings.HasPrefix(fieldType, "enum") {
enum, err := EnumValue(nextInt, fieldType)
if err != nil {
panic(err)
}
return enum, true
}
switch fieldType {
case "json", "jsonb":
return `"` + Str(nextInt, 1) + `"`, true
case "interval":
return strconv.Itoa((int(nextInt())%26)+2) + " days", true
case "uuid":
randomUUID, err := uuid.NewV4()
if err != nil {
panic(err)
}
return randomUUID.String(), true
case "cidr", "inet":
return randNetAddr(nextInt), true
case "macaddr":
return randMacAddr(nextInt), true
case "pg_lsn":
return randLsn(nextInt), true
case "txid_snapshot":
return randTxID(nextInt), true
case "money":
return randMoney(nextInt), true
case "time":
return randTime(nextInt), true
}
return "", false
}
// MediumInt is a special case in mysql (thanks for that -_-)
// this function checks if the fieldtype matches and if so returns
// a random value in the proper range.
func MediumInt(nextInt func() int64, fieldType string) (int32, bool) {
if fieldType == "mediumint" {
return int32(nextInt()) % 8388607, true
}
return 0, false
}
// MediumUint is the unsigned version of MediumInt
func MediumUint(nextInt func() int64, fieldType string) (uint32, bool) {
fmt.Println(fieldType)
if fieldType == "mediumint" {
return uint32(nextInt()) % 16777215, true
}
return 0, false
}
// Date generates a random time.Time between 1850 and 2050.
// Only the Day/Month/Year columns are set so that Dates and DateTimes do
// not cause mismatches in the test data comparisons.
func Date(nextInt func() int64) time.Time {
t := time.Date(
int(1972+nextInt()%60),
time.Month(1+(nextInt()%12)),
int(1+(nextInt()%25)),
0,
0,
0,
0,
time.UTC,
)
return t
}
// EnumValue takes an enum field type, parses it's definition
// to figure out valid values, and selects a random one from within them.
func EnumValue(nextInt func() int64, enum string) (string, error) {
vals := strmangle.ParseEnumVals(enum)
if vals == nil || len(vals) == 0 {
return "", fmt.Errorf("unable to parse enum string: %s", enum)
}
return vals[int(nextInt())%len(vals)], nil
}
// ByteSlice creates a random set of bytes (non-printables included)
func ByteSlice(nextInt func() int64, ln int) []byte {
str := make([]byte, ln)
for i := 0; i < ln; i++ {
str[i] = byte(nextInt() % 256)
}
return str
}
func randNetAddr(nextInt func() int64) string {
return fmt.Sprintf(
"%d.%d.%d.%d",
nextInt()%254+1,
nextInt()%254+1,
nextInt()%254+1,
nextInt()%254+1,
)
}
func randMacAddr(nextInt func() int64) string {
buf := make([]byte, 6)
for i := range buf {
buf[i] = byte(nextInt())
}
// Set the local bit
buf[0] |= 2
return fmt.Sprintf(
"%02x:%02x:%02x:%02x:%02x:%02x",
buf[0], buf[1], buf[2], buf[3], buf[4], buf[5],
)
}
func randLsn(nextInt func() int64) string {
a := nextInt() % 9000000
b := nextInt() % 9000000
return fmt.Sprintf("%d/%d", a, b)
}
func randTxID(nextInt func() int64) string {
// Order of integers is relevant
a := nextInt()%200 + 100
b := a + 100
c := a
d := a + 50
return fmt.Sprintf("%d:%d:%d,%d", a, b, c, d)
}
func randMoney(nextInt func() int64) string {
return fmt.Sprintf("%d.00", nextInt()%100000)
}
func randTime(nextInt func() int64) string {
return fmt.Sprintf("%d:%d:%d", nextInt()%24, nextInt()%60, nextInt()%60)
}
// StableDBName takes a database name in, and generates
// a random string using the database name as the rand Seed.
// getDBNameHash is used to generate unique test database names.
func StableDBName(input string) string {
return randStrFromSource(stableSource(input), 40)
}
// stableSource takes an input value, and produces a random
// seed from it that will produce very few collisions in
// a 40 character random string made from a different alphabet.
func stableSource(input string) *rand.Rand {
sum := md5.Sum([]byte(input))
var seed int64
for i, byt := range sum {
seed ^= int64(byt) << uint((i*4)%64)
}
return rand.New(rand.NewSource(seed))
}
func randStrFromSource(r *rand.Rand, length int) string {
ln := len(alphabetLowerAlpha)
output := make([]rune, length)
for i := 0; i < length; i++ {
output[i] = rune(alphabetLowerAlpha[r.Intn(ln)])
}
return string(output)
}