Skip to content

Commit 0701aba

Browse files
authored
Merge pull request #353 from vulncheck-oss/random/emails-and-domains
Add random domain and random email functions
2 parents 83e5b4a + 2601670 commit 0701aba

File tree

2 files changed

+57
-3
lines changed

2 files changed

+57
-3
lines changed

random/random.go

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ package random
33
import (
44
"crypto/rand"
55
"math/big"
6+
"strings"
7+
8+
"github.com/vulncheck-oss/go-exploit/output"
69
)
710

811
var (
@@ -16,7 +19,10 @@ func RandIntRange(rangeMin int, rangeMax int) int {
1619
rangeMaxBig := big.NewInt(int64(rangeMax) - int64(rangeMin))
1720
n, err := rand.Int(rand.Reader, rangeMaxBig)
1821
if err != nil {
19-
panic(err)
22+
// if random fails the process will die anyway: https://github.com/golang/go/issues/66821
23+
output.PrintfFrameworkError("Random generation error: %s", err.Error())
24+
25+
return 0
2026
}
2127

2228
return int(n.Int64() + int64(rangeMin))
@@ -26,7 +32,10 @@ func RandIntRange(rangeMin int, rangeMax int) int {
2632
func RandPositiveInt(rangeMax int) int {
2733
n, err := rand.Int(rand.Reader, big.NewInt(int64(rangeMax)))
2834
if err != nil {
29-
panic(err)
35+
// if random fails the process will die anyway: https://github.com/golang/go/issues/66821
36+
output.PrintfFrameworkError("Random generation error: %s", err.Error())
37+
38+
return 0
3039
}
3140

3241
return int(n.Int64())
@@ -42,7 +51,7 @@ func RandLetters(n int) string {
4251
return string(runeSlice)
4352
}
4453

45-
// RandLetters generates a random alpha string with no bad chars of length n.
54+
// RandLettersNoBadChars generates a random alpha string with no bad chars of length n.
4655
// This will return an empty string if the caller badchars all "letters".
4756
func RandLettersNoBadChars(n int, badchars []rune) string {
4857
// rebuild the letters slice without the bad chars. O(n^2) implementation
@@ -111,3 +120,23 @@ func RandDigits(n int) string {
111120
func RandDigitsRange(rangeMin int, rangeMax int) string {
112121
return RandDigits(RandIntRange(rangeMin, rangeMax))
113122
}
123+
124+
// RandDomain generates a random domain name with a common TLDs. The domain will be between 8 and 14 characters.
125+
func RandDomain() string {
126+
return strings.ToLower(RandLettersRange(4, 10) + "." + CommonTLDs[RandPositiveInt(len(CommonTLDs))])
127+
}
128+
129+
// RandEmail generates a random email address using common domain TLDs. The largest size of the
130+
// generated domain will be 23 characters and smallest will be 13 characters. The goal is not to
131+
// generate a set of RFC valid domains, but simple lower case emails that are valid for most
132+
// automated account registration or usage, so these might be "too simple" or for some uses.
133+
func RandEmail() string {
134+
return strings.ToLower(RandLettersRange(4, 8) + "@" + RandDomain())
135+
}
136+
137+
// CommonTLDs contains the 3 most common DNS TLDs.
138+
var CommonTLDs = []string{
139+
"com",
140+
"org",
141+
"net",
142+
}

random/random_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package random
22

33
import (
4+
"strings"
45
"testing"
56
)
67

@@ -140,3 +141,27 @@ func Test_RandDigitsRange(t *testing.T) {
140141
}
141142
}
142143
}
144+
145+
func Test_RandDomain(t *testing.T) {
146+
for range 100 {
147+
r := RandDomain()
148+
if len(r) > 14 || len(r) < 8 {
149+
t.Error("Domain generated with an unexpected length: " + r)
150+
}
151+
if !strings.Contains(r, ".") {
152+
t.Error("Domain generated without expected characters: " + r)
153+
}
154+
}
155+
}
156+
157+
func Test_RandEmail(t *testing.T) {
158+
for range 100 {
159+
r := RandEmail()
160+
if len(r) > 23 || len(r) < 13 {
161+
t.Error("Domain generated with an unexpected length: " + r)
162+
}
163+
if !strings.Contains(r, ".") {
164+
t.Error("Domain generated without expected characters: " + r)
165+
}
166+
}
167+
}

0 commit comments

Comments
 (0)