-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils_test.go
156 lines (128 loc) · 2.58 KB
/
utils_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 dicewarelib
import (
"log"
"slices"
"strings"
"testing"
)
func TestGetRandomInt(t *testing.T) {
// a very naive testing method that just generates a bunch
// of GetRandomInt values and asserts that none of them are outside
// the specified range
maxInt := 10
for i := 0; i < 2500; i++ {
actual := GetRandomInt(maxInt)
if actual > maxInt {
t.Fail()
}
if actual < 0 {
t.Fail()
}
}
}
func TestGetRandomSymbol(t *testing.T) {
for i := 0; i < 2500; i++ {
actual := getRandomSymbol()
if !slices.Contains(symbols, actual) {
t.Fail()
}
}
}
func setupRandomWordTest() ([]string, *Words) {
// generate a min-length string
sb := new(strings.Builder)
for i := 0; i < minWordLength; i++ {
sb.WriteString("a")
}
shortWord := sb.String()
sb.Reset()
// generate a max-length string
for i := 0; i < maxWordLength+1; i++ {
sb.WriteString("a")
}
longWord := sb.String()
// there's probably a more efficient way to do this but it's not critical
// right now
validWords := []string{
shortWord,
"onlinux",
longWord,
"fedora",
"debian",
"archlinux",
"qubes",
}
wordList := map[int]string{
0: shortWord,
1: "onlinux",
2: longWord,
3: "fedora",
4: "debian",
5: "archlinux",
6: "qubes",
}
words := Words{
Simple: &wordList,
SimpleCount: 7,
Complex: &wordList,
ComplexCount: 7,
}
return validWords, &words
}
func TestGetRandomWord(t *testing.T) {
validWords, words := setupRandomWordTest()
for i := 0; i < 5000; i++ {
actual := getRandomWord(*words.Complex)
if len(actual) > maxWordLength {
t.Fail()
}
if len(actual) < minWordLength {
t.Fail()
}
if !slices.Contains(validWords, actual) {
t.Fail()
}
if actual == "" {
t.Fail()
}
}
}
func TestGeneratePassword(t *testing.T) {
_, words := setupRandomWordTest()
tests := []struct {
n int
s string
maxLen int
minLen int
shouldSucceed bool
name string
}{
{4, " ", 64, 4, true, "1"},
{4, " ", 32, 4, true, "2"},
{5, " ", 32, 4, true, "3"},
{3, " ", 64, 4, true, "4"},
{4, "2", 64, 4, true, "5"},
{90, "3", 64, 4, false, "6"},
{90, "-", 64, 4, false, "7"},
{90, " ", 10, 4, false, "8"},
}
for _, test := range tests {
log.Printf("test: %v", test.name)
actual := GeneratePassword(words, test.n, test.s, test.maxLen, test.minLen, false)
if test.shouldSucceed {
if len(actual) > test.maxLen {
t.Fail()
}
if len(actual) < test.minLen {
t.Fail()
}
if actual == "" {
t.Fail()
}
continue
}
if actual != "" {
t.Fail()
}
}
}