-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpassword_test.go
193 lines (176 loc) · 4.16 KB
/
password_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
package passgen
import (
"fmt"
"strings"
"testing"
)
func TestGetPassword(t *testing.T) {
p, err := GetNumericPassword(14, 20)
if err != nil {
t.Fatal("Error generating password", err)
}
if len(p) > 20 || len(p) < 14 {
t.Error("Incorrect sized password returned")
}
}
func TestGetPasswordSameSize(t *testing.T) {
p, err := GetNumericPassword(4, 4)
if err != nil {
t.Fatal("Error generating password", err)
}
if len(p) != 4 {
t.Error("Incorrect sized password returned")
}
}
func TestGetPasswordOddSize(t *testing.T) {
p, err := GetNumericPassword(15, 15)
if err != nil {
t.Fatal("Error generating password", err)
}
if len(p) != 15 {
t.Error("Incorrect sized password returned")
}
}
var digits = "1234567890"
func TestGetNumericPassword(t *testing.T) {
p, err := GetNumericPassword(14, 20)
if err != nil {
t.Fatal("Error generating password", err)
}
for _, c := range p {
if !strings.ContainsRune(digits, c) {
t.Errorf("Invalid character found: %c (%x)", c, c)
break
}
}
}
var alpha_lower = "abcdefghijklmnopqrstuvwxyz"
func TestGetAlphaLowerPassword(t *testing.T) {
p, err := GetAlphaLowerPassword(14, 20)
if err != nil {
t.Fatal("Error generating password", err)
}
for _, c := range p {
if !strings.ContainsRune(alpha_lower, c) {
t.Errorf("Invalid character found: %c (%x)", c, c)
break
}
}
}
var alpha_upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
func TestGetAlphaUpperPassword(t *testing.T) {
p, err := GetAlphaUpperPassword(14, 20)
if err != nil {
t.Fatal("Error generating password", err)
}
for _, c := range p {
if !strings.ContainsRune(alpha_upper, c) {
t.Errorf("Invalid character found: %c (%x)", c, c)
break
}
}
}
func TestGetAlphaPassword(t *testing.T) {
p, err := GetAlphaPassword(14, 20)
if err != nil {
t.Fatal("Error generating password", err)
}
for _, c := range p {
if !strings.ContainsRune(alpha_upper+alpha_lower, c) {
t.Errorf("Invalid character found: %c (%x)", c, c)
break
}
}
}
var symbols = " !@#$%^&*()`~-_=+[{]}\\|,<.>/?;:'\""
func TestGetSecurePassword(t *testing.T) {
p, err := GetSecurePassword(14, 20)
if err != nil {
t.Fatal("Error generating password", err)
}
for _, c := range p {
if !strings.ContainsRune(alpha_upper+alpha_lower+symbols+digits, c) {
t.Errorf("Invalid character found: %c (%x)", c, c)
break
}
}
}
func TestGetLargePassword(t *testing.T) {
p, err := GetSecurePassword(40, 200)
if err != nil {
t.Fatal("Error generating password", err)
}
if len(p) > 200 || len(p) < 40 {
t.Error("Incorrect sized password returned")
}
for _, c := range p {
if !strings.ContainsRune(alpha_upper+alpha_lower+symbols+digits, c) {
t.Errorf("Invalid character found: %c (%x)", c, c)
break
}
}
}
func TestGetLargeNumericPassword(t *testing.T) {
p, err := GetNumericPassword(40, 200)
if err != nil {
t.Fatal("Error generating password", err)
}
if len(p) > 200 || len(p) < 40 {
t.Error("Incorrect sized password returned")
}
for _, c := range p {
if !strings.ContainsRune(digits, c) {
t.Errorf("Invalid character found: %c (%x)", c, c)
break
}
}
}
func TestPasswordGenerator(t *testing.T) {
gen := GetSecurePasswordGenerator()
for i := 0; i < 5; i++ {
p, err := gen.GeneratePassword(14, 20)
if err != nil {
t.Fatal("Error generating password", err)
}
if len(p) > 20 || len(p) < 14 {
t.Error("Incorrect sized password returned")
}
for _, c := range p {
if !strings.ContainsRune(alpha_upper+alpha_lower+symbols+digits, c) {
t.Errorf("Invalid character found: %c", c)
break
}
}
}
}
func TestGetPasswordBias(t *testing.T) {
N := 10000000
var freq [10]map[rune]int
for i := 0; i < 10; i++ {
freq[i] = make(map[rune]int)
}
for i := 0; i < N; i++ {
p, err := GetNumericPassword(10, 10)
if err != nil {
t.Fatal("Error generating password", err)
}
if len(p) != 10 {
t.Error("Incorrect sized password returned")
}
for j, c := range p {
if _, ok := freq[j][c]; ok {
freq[j][c]++
} else {
freq[j][c] = 1
}
}
}
for i := 0; i < 10; i++ {
for _, c := range "0123456789" {
cnt := freq[i][c]
fmt.Printf("'%c' %5.2f ", c, 100*(float64(cnt)/float64(N))-10.0)
}
fmt.Println()
}
t.Fatal("")
}