-
Notifications
You must be signed in to change notification settings - Fork 276
/
Copy pathcaptcha_test.go
104 lines (87 loc) · 2.35 KB
/
captcha_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
package base64Captcha
import (
"io/ioutil"
"strings"
"testing"
)
const ExampleFontDirPath = "/Users/ericzhou/go/src/github.com/mojocn/base64Captcha/examples/fonts"
const ExampleFontExtension = "ttf"
var configD = ConfigDigit{
Height: 80,
Width: 240,
MaxSkew: 0.7,
DotCount: 80,
CaptchaLen: 5,
}
var configA = ConfigAudio{
CaptchaLen: 6,
Language: "zh",
}
var configC = ConfigCharacter{
Height: 60,
Width: 240,
Mode: 0,
ComplexOfNoiseText: 0,
ComplexOfNoiseDot: 0,
IsUseSimpleFont: false,
IsShowHollowLine: false,
IsShowNoiseDot: false,
IsShowNoiseText: false,
IsShowSlimeLine: false,
IsShowSineLine: false,
CaptchaLen: 6,
}
func TestGenerateCaptcha(t *testing.T) {
GenerateCaptcha("", CaptchaItem{})
for idx, vv := range []interface{}{configA, configD} {
idkey, cap := GenerateCaptcha("", vv)
ext := "png"
if idx == 0 {
ext = "wav"
}
GoTestOutputDir, _ := ioutil.TempDir("", "")
CaptchaWriteToFile(cap, GoTestOutputDir, idkey, ext)
CaptchaWriteToFile(cap, GoTestOutputDir, idkey, ext)
tempDir, _ := ioutil.TempDir("", "")
CaptchaWriteToFile(cap, tempDir, idkey, ext)
//t.Log(idkey, globalStore.Get(idkey, false))
}
for i := 0; i < 16; i++ {
configC.Mode = i % 4
idkey, cap := GenerateCaptcha("", configC)
ext := "png"
err := CaptchaWriteToFile(cap, GoTestOutputDir+"/all", "char_"+idkey, ext)
if err == nil {
t.Log(idkey, globalStore.Get(idkey, false))
} else {
t.Error(idkey)
}
}
}
func TestCaptchaWriteToBase64Encoding(t *testing.T) {
idkey, cap := GenerateCaptcha("", configD)
base64string := CaptchaWriteToBase64Encoding(cap)
if strings.Contains(base64string, MimeTypeCaptchaImage) {
t.Log(base64string, idkey)
} else {
t.Error("encodeing base64 string failed.")
}
idkeyA, capA := GenerateCaptcha("", configA)
base64stringA := CaptchaWriteToBase64Encoding(capA)
if strings.Contains(base64stringA, MimeTypeCaptchaAudio) {
t.Log(base64string, idkeyA)
} else {
t.Error("encodeing base64 string failed.")
}
}
func TestVerifyCaptcha(t *testing.T) {
idkey, _ := GenerateCaptcha("", configD)
verifyValue := globalStore.Get(idkey, false)
if VerifyCaptcha(idkey, verifyValue) {
t.Log(idkey, verifyValue)
} else {
t.Error("verify captcha content is failed.")
}
VerifyCaptcha("", "")
VerifyCaptcha("dsafasf", "ddd")
}