-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
token_mock_test.go
156 lines (125 loc) · 2.79 KB
/
token_mock_test.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
package dns
import (
"context"
"fmt"
"math/rand"
"strings"
"github.com/Pallinder/go-randomdata"
)
// Combination of space, tab, newline, etc...
const WHITESPACES = `
`
type TestToken struct {
*Token
records []string
hasToken bool
}
func TTokens(prefix string, count int) []TestToken {
var tokens []TestToken
for _, url := range RandomUrls(count) {
tok, err := RandomToken(prefix, url)
if err != nil {
panic(err)
}
txtRecords, hasToken := RandTXT(count, tok.String())
tokens = append(tokens, TestToken{
Token: tok,
records: txtRecords,
hasToken: hasToken,
})
}
return tokens
}
// var monthAgo = time.Now().AddDate(0, -1, 0)
// var monthAhead = time.Now().AddDate(0, 1, 0)
// var setFormat = "2006-01-02"
// var parseFormat = "Monday 2 Jan 2006"
func RandomToken(prefix, url string) (*Token, error) {
// time.Parse(parseFormat, randomdata.FullDateInRange(monthAgo.Format(setFormat), monthAhead.Format(setFormat)))
return NewToken(url, prefix, nil)
}
func RandomUrls(n int) []string {
var urls []string
var protoMod = rand.Int()%10 + 1
var subMod = rand.Int()%10 + 1
var portMod = rand.Int()%10 + 1
var preSMod = rand.Int()%10 + 1
var postSMod = rand.Int()%10 + 1
for i := 0; i < n; i++ {
var proto string
if i%protoMod == 0 {
proto = fmt.Sprintf("%s://", randomdata.SillyName()[:5])
}
var subdom string
if i%subMod == 0 {
subdom = fmt.Sprintf("%s.", randomdata.SillyName())
}
var port string
if i%portMod == 0 {
port = fmt.Sprintf(":%d", rand.Int()%65535)
}
var randomSpacePre string
if i%preSMod == 0 {
randomSpacePre = WHITESPACES
}
var randomSpacePost string
if i%postSMod == 0 {
randomSpacePost = WHITESPACES
}
urls = append(urls,
strings.ToLower(fmt.Sprintf(
"%s%s%s%s%s.%s%s",
randomSpacePre,
proto,
subdom,
randomdata.SillyName(),
randomdata.SillyName()[:3],
port,
randomSpacePost,
)))
}
return urls
}
func RandTXT(n int, insert string) ([]string, bool) {
var txt []string
randi := randomdata.Number(0, 2*n)
hasRecord := false
var preSMod = rand.Int()%10 + 1
var postSMod = rand.Int()%10 + 1
for i := 0; i < n; i++ {
if i == randi {
txt = append(txt, insert)
hasRecord = true
continue
}
var randomSpacePre string
if i%preSMod == 0 {
randomSpacePre = WHITESPACES
}
var randomSpacePost string
if i%postSMod == 0 {
randomSpacePost = WHITESPACES
}
txt = append(txt, fmt.Sprintf(
"%s%s%s",
randomSpacePre,
randomdata.SillyName(),
randomSpacePost,
))
}
return txt, hasRecord
}
type TestResolver struct {
Values map[string][]string
Tokens map[string]*Token
}
func (tr *TestResolver) LookupTXT(
_ context.Context,
name string,
) ([]string, error) {
v, ok := tr.Values[name]
if !ok {
return nil, nil
}
return v, nil
}