-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathconfig_test.go
225 lines (209 loc) · 5.66 KB
/
config_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
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
// SPDX-FileCopyrightText: 2021-2024 Winni Neessen <wn@neessen.dev>
//
// SPDX-License-Identifier: MIT
package apg
import (
"testing"
)
func TestNewConfig(t *testing.T) {
c := NewConfig()
if c == nil {
t.Errorf("NewConfig() failed, expected config pointer but got nil")
return
}
c = NewConfig(nil)
if c == nil {
t.Errorf("NewConfig() failed, expected config pointer but got nil")
return
}
if c.MinLength != DefaultMinLength {
t.Errorf("NewConfig() failed, expected min length: %d, got: %d", DefaultMinLength,
c.MinLength)
}
if c.MaxLength != DefaultMaxLength {
t.Errorf("NewConfig() failed, expected max length: %d, got: %d", DefaultMaxLength,
c.MaxLength)
}
if c.NumberPass != DefaultNumberPass {
t.Errorf("NewConfig() failed, expected number of passwords: %d, got: %d",
DefaultNumberPass, c.NumberPass)
}
if c.Mode != DefaultMode {
t.Errorf("NewConfig() failed, expected mode mask: %d, got: %d",
DefaultMode, c.Mode)
}
}
func TestWithAlgorithm(t *testing.T) {
tests := []struct {
name string
algo Algorithm
want int
}{
{"Pronounceable passwords", AlgoPronounceable, 0},
{"Random passwords", AlgoRandom, 1},
{"Coinflip", AlgoCoinFlip, 2},
{"Binary", AlgoBinary, 3},
{"Unsupported", AlgoUnsupported, 4},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := NewConfig(WithAlgorithm(tt.algo))
if c == nil {
t.Errorf("NewConfig(WithAlgorithm()) failed, expected config pointer but got nil")
return
}
if c.Algorithm != tt.algo {
t.Errorf("NewConfig(WithAlgorithm()) failed, expected algo: %d, got: %d",
tt.algo, c.Algorithm)
}
if IntToAlgo(tt.want) != c.Algorithm {
t.Errorf("IntToAlgo() failed, expected algo: %d, got: %d",
tt.want, c.Algorithm)
}
})
}
}
func TestWithBinaryHexMode(t *testing.T) {
c := NewConfig(WithBinaryHexMode())
if c == nil {
t.Errorf("NewConfig(WithBinaryHexMode()) failed, expected config pointer but got nil")
return
}
if !c.BinaryHexMode {
t.Errorf("NewConfig(WithBinaryHexMode()) failed, expected chars: %t, got: %t",
true, c.BinaryHexMode)
}
}
func TestWithExcludeChars(t *testing.T) {
e := "abcdefg"
c := NewConfig(WithExcludeChars(e))
if c == nil {
t.Errorf("NewConfig(WithExcludeChars()) failed, expected config pointer but got nil")
return
}
if c.ExcludeChars != e {
t.Errorf("NewConfig(WithExcludeChars()) failed, expected chars: %s, got: %s",
e, c.ExcludeChars)
}
}
func TestWithFixedLength(t *testing.T) {
var e int64 = 10
c := NewConfig(WithFixedLength(e))
if c == nil {
t.Errorf("NewConfig(WithFixedLength()) failed, expected config pointer but got nil")
return
}
if c.FixedLength != e {
t.Errorf("NewConfig(WithFixedLength()) failed, expected fixed length: %d, got: %d",
e, c.FixedLength)
}
}
func TestWithMaxLength(t *testing.T) {
var e int64 = 123
c := NewConfig(WithMaxLength(e))
if c == nil {
t.Errorf("NewConfig(WithMaxLength()) failed, expected config pointer but got nil")
return
}
if c.MaxLength != e {
t.Errorf("NewConfig(WithMaxLength()) failed, expected max length: %d, got: %d",
e, c.MaxLength)
}
}
func TestWithMinLength(t *testing.T) {
var e int64 = 1
c := NewConfig(WithMinLength(e))
if c == nil {
t.Errorf("NewConfig(WithMinLength()) failed, expected config pointer but got nil")
return
}
if c.MinLength != e {
t.Errorf("NewConfig(WithMinLength()) failed, expected min length: %d, got: %d",
e, c.MinLength)
}
}
func TestWithMinLowercase(t *testing.T) {
var e int64 = 2
c := NewConfig(WithMinLowercase(e))
if c == nil {
t.Errorf("NewConfig(WithMinLowercase()) failed, expected config pointer but got nil")
return
}
if c.MinLowerCase != e {
t.Errorf("NewConfig(WithMinLowercase()) failed, expected min amount: %d, got: %d",
e, c.MinLowerCase)
}
}
func TestWithMinNumeric(t *testing.T) {
var e int64 = 3
c := NewConfig(WithMinNumeric(e))
if c == nil {
t.Errorf("NewConfig(WithMinNumeric()) failed, expected config pointer but got nil")
return
}
if c.MinNumeric != e {
t.Errorf("NewConfig(WithMinNumeric()) failed, expected min amount: %d, got: %d",
e, c.MinNumeric)
}
}
func TestWithMinSpecial(t *testing.T) {
var e int64 = 4
c := NewConfig(WithMinSpecial(e))
if c == nil {
t.Errorf("NewConfig(WithMinSpecial()) failed, expected config pointer but got nil")
return
}
if c.MinSpecial != e {
t.Errorf("NewConfig(WithMinSpecial()) failed, expected min amount: %d, got: %d",
e, c.MinSpecial)
}
}
func TestWithMinUppercase(t *testing.T) {
var e int64 = 5
c := NewConfig(WithMinUppercase(e))
if c == nil {
t.Errorf("NewConfig(WithMinUppercase()) failed, expected config pointer but got nil")
return
}
if c.MinUpperCase != e {
t.Errorf("NewConfig(WithMinUppercase()) failed, expected min amount: %d, got: %d",
e, c.MinUpperCase)
}
}
func TestWithMobileGrouping(t *testing.T) {
c := NewConfig(WithMobileGrouping())
if c == nil {
t.Errorf("NewConfig(WithMobileGrouping()) failed, expected config pointer but got nil")
return
}
if c.MobileGrouping != true {
t.Errorf("NewConfig(WithMobileGrouping()) failed, expected: %t, got: %t",
true, c.MobileGrouping)
}
}
func TestWithModeMask(t *testing.T) {
e := DefaultMode
c := NewConfig(WithModeMask(e))
if c == nil {
t.Errorf("NewConfig(WithModeMask()) failed, expected config pointer but got nil")
return
}
if c.Mode != e {
t.Errorf("NewConfig(WithModeMask()) failed, expected mask: %d, got: %d",
e, c.Mode)
}
}
func FuzzWithAlgorithm(f *testing.F) {
f.Add(0)
f.Add(1)
f.Add(2)
f.Add(3)
f.Add(-1)
f.Add(100)
f.Fuzz(func(t *testing.T, algo int) {
config := NewConfig(WithAlgorithm(Algorithm(algo)))
if config.MaxLength < config.MinLength {
t.Errorf("Invalid algorithm: %d", algo)
}
})
}