Browse Source

more checks and validations on passphrase settings

bmallred 10 years ago
parent
commit
47eaa1f2b6
2 changed files with 115 additions and 2 deletions
  1. 42 2
      main.go
  2. 73 0
      main_test.go

+ 42 - 2
main.go

@ -27,8 +27,6 @@ type Site struct {
27 27
}
28 28
29 29
func main() {
30
	log.Printf("%x", getBookname("bmallred"))
31
32 30
	http.HandleFunc("/api", func(w http.ResponseWriter, r *http.Request) {
33 31
	})
34 32
@ -174,3 +172,45 @@ func containsDigits(source []byte, minOccurrences int) bool {
174 172
175 173
	return len(matches) >= minOccurrences
176 174
}
175
176
func containsUppercase(source []byte, minOccurrences int) bool {
177
	r := regexp.MustCompile(`[A-Z]+`)
178
179
	var matches [][]byte
180
	if matches = r.FindAll(source, -1); matches == nil {
181
		return false
182
	}
183
184
	return len(matches) >= minOccurrences
185
}
186
187
func containsSpecialCharacters(source []byte, specialCharacters string, minOccurrences int) bool {
188
	s := specialCharacters
189
	s = strings.Replace(s, "\\", "\\\\", -1)
190
	s = strings.Replace(s, ".", "\\.", -1)
191
	s = strings.Replace(s, " ", "\\s", -1)
192
	s = strings.Replace(s, "-", "\\-", -1)
193
	s = strings.Replace(s, "[", "\\[", -1)
194
	s = strings.Replace(s, "]", "\\]", -1)
195
196
	r := regexp.MustCompile(`[` + s + `]+`)
197
198
	var matches [][]byte
199
	if matches = r.FindAll(source, -1); matches == nil {
200
		return false
201
	}
202
203
	return len(matches) >= minOccurrences
204
}
205
206
func validateLength(source []byte, minimum, maximum int) bool {
207
	if minimum > -1 && len(source) < minimum {
208
		return false
209
	}
210
211
	if maximum > -1 && len(source) > maximum {
212
		return false
213
	}
214
215
	return true
216
}

+ 73 - 0
main_test.go

@ -21,3 +21,76 @@ func TestContainsDigits(t *testing.T) {
21 21
		t.FailNow()
22 22
	}
23 23
}
24
25
func TestContainsUppercase(t *testing.T) {
26
	expected := true
27
	actual := containsUppercase([]byte("Blah0Blah2"), 2)
28
	if actual != expected {
29
		t.FailNow()
30
	}
31
32
	expected = false
33
	actual = containsUppercase([]byte("Blah0blah"), 2)
34
	if actual != expected {
35
		t.FailNow()
36
	}
37
38
	expected = true
39
	actual = containsUppercase([]byte("Blah0BLaH12"), 2)
40
	if actual != expected {
41
		t.FailNow()
42
	}
43
}
44
45
func TestContainsSpecialCharacters(t *testing.T) {
46
	special := "~!@#$%^&*()_+-= []{};':\",./<>?\\|"
47
	expected := true
48
	actual := containsSpecialCharacters([]byte("Blah!Blah&"), special, 2)
49
	if actual != expected {
50
		t.FailNow()
51
	}
52
53
	expected = false
54
	actual = containsSpecialCharacters([]byte("Blah!blah"), special, 2)
55
	if actual != expected {
56
		t.FailNow()
57
	}
58
59
	expected = true
60
	actual = containsSpecialCharacters([]byte("Blah!BLa l."), special, 2)
61
	if actual != expected {
62
		t.FailNow()
63
	}
64
}
65
66
func TestLength(t *testing.T) {
67
	expected := true
68
	actual := validateLength([]byte("eightormore"), 8, -1)
69
	if actual != expected {
70
		t.FailNow()
71
	}
72
73
	expected = true
74
	actual = validateLength([]byte("eighttoten"), 8, 10)
75
	if actual != expected {
76
		t.FailNow()
77
	}
78
79
	expected = false
80
	actual = validateLength([]byte("eighttoten----"), 8, 10)
81
	if actual != expected {
82
		t.FailNow()
83
	}
84
85
	expected = true
86
	actual = validateLength([]byte("maxof8--"), -1, 8)
87
	if actual != expected {
88
		t.FailNow()
89
	}
90
91
	expected = false
92
	actual = validateLength([]byte("ei"), 8, -1)
93
	if actual != expected {
94
		t.FailNow()
95
	}
96
}