-
Notifications
You must be signed in to change notification settings - Fork 55
/
captcha_test.go
114 lines (91 loc) · 2.8 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
105
106
107
108
109
110
111
112
113
114
// Copyright 2013 hanguofeng. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package gocaptcha
import (
"os"
"testing"
"time"
)
func TestCaptcha(t *testing.T) {
captcha, err := getCaptcha()
if nil != err {
t.Fatalf("getCaptcha Error:%s", err.Error())
}
key, err := captcha.GetKey(4)
if nil != err {
t.Fatalf("GetKey Error:%s", err.Error())
}
captcha.GetImage(key)
captcha.Verify(key, "test")
}
func BenchmarkCaptcha(t *testing.B) {
captcha, err := getCaptcha()
if nil != err {
t.Fatalf("getCaptcha Error:%s", err.Error())
}
for i := 0; i < t.N; i++ {
s, _ := captcha.GetKey(4)
captcha.GetImage(s)
captcha.Verify(s, "ssss")
}
}
func BenchmarkCaptchaInternalAPI(t *testing.B) {
captcha, err := getCaptcha()
if nil != err {
t.Fatalf("getCaptcha Error:%s", err.Error())
}
for i := 0; i < t.N; i++ {
s, _ := captcha.GetKey(4)
captcha.Verify(s, "ssss")
}
}
func BenchmarkCaptchaDrawImage(t *testing.B) {
captcha, err := getCaptcha()
if nil != err {
t.Fatalf("getCaptcha Error:%s", err.Error())
}
for i := 0; i < t.N; i++ {
s, _ := captcha.GetKey(4)
captcha.GetImage(s)
}
}
func getCaptcha() (*Captcha, error) {
wordDict, captchaConfig, imageConfig, filterConfig, storeConfig := loadConfig()
wordmgr, err := CreateWordManagerFromDataFile(wordDict)
captcha, err := CreateCaptcha(wordmgr, captchaConfig, imageConfig, filterConfig, storeConfig)
return captcha, err
}
func loadConfig() (string, *CaptchaConfig, *ImageConfig, *FilterConfig, *StoreConfig) {
pwd, _ := os.Getwd()
data_path := pwd + "/data/"
wordDict := data_path + "en_phrases"
captchaConfig := new(CaptchaConfig)
captchaConfig.LifeTime = 10 * time.Second
imageConfig := new(ImageConfig)
imageConfig.FontFiles = []string{data_path + "zpix.ttf"}
imageConfig.FontSize = 26
imageConfig.Height = 40
imageConfig.Width = 120
filterConfig := new(FilterConfig)
filterConfig.Init()
filterConfig.Filters = []string{"ImageFilterNoiseLine", "ImageFilterNoisePoint", "ImageFilterStrike"}
var filterConfigGroup *FilterConfigGroup
filterConfigGroup = new(FilterConfigGroup)
filterConfigGroup.Init()
filterConfigGroup.SetItem("Num", "5")
filterConfig.SetGroup("ImageFilterNoiseLine", filterConfigGroup)
filterConfigGroup = new(FilterConfigGroup)
filterConfigGroup.Init()
filterConfigGroup.SetItem("Num", "10")
filterConfig.SetGroup("ImageFilterNoisePoint", filterConfigGroup)
filterConfigGroup = new(FilterConfigGroup)
filterConfigGroup.Init()
filterConfigGroup.SetItem("Num", "3")
filterConfig.SetGroup("ImageFilterStrike", filterConfigGroup)
storeConfig := new(StoreConfig)
storeConfig.Engine = STORE_ENGINE_BUILDIN
storeConfig.GcDivisor = 100
storeConfig.GcProbability = 1
return wordDict, captchaConfig, imageConfig, filterConfig, storeConfig
}