This repository has been archived on 2024-08-25. You can view files and clone it, but cannot push or open issues or pull requests.
captcha/captcha_test.go

201 lines
3.9 KiB
Go
Raw Permalink Normal View History

2017-09-19 12:17:25 +01:00
package captcha
import (
"bytes"
2017-11-08 06:21:40 +00:00
"errors"
2017-09-20 02:51:27 +01:00
"image/color"
2018-10-04 02:48:52 +01:00
"image/color/palette"
2017-11-08 06:21:40 +00:00
"image/gif"
"image/jpeg"
2017-09-27 06:07:08 +01:00
"math/rand"
"os"
2017-09-28 01:18:29 +01:00
"testing"
2017-12-11 08:12:44 +00:00
"golang.org/x/image/font/gofont/goregular"
2017-09-19 12:17:25 +01:00
)
func TestNewCaptcha(t *testing.T) {
data, err := New(150, 50)
if err != nil {
t.Fatal(err)
}
buf := new(bytes.Buffer)
2017-11-08 06:21:40 +00:00
err = data.WriteImage(buf)
if err != nil {
t.Fatal(err)
}
2017-09-19 12:17:25 +01:00
}
2017-09-20 02:51:27 +01:00
func TestSmallCaptcha(t *testing.T) {
_, err := New(36, 12)
if err != nil {
t.Fatal(err)
}
}
func TestEncodeJPG(t *testing.T) {
data, err := New(150, 50)
if err != nil {
t.Fatal(err)
}
buf := new(bytes.Buffer)
2017-11-08 06:21:40 +00:00
err = data.WriteJPG(buf, &jpeg.Options{Quality: 70})
if err != nil {
t.Fatal(err)
}
}
func TestEncodeGIF(t *testing.T) {
data, err := New(150, 50)
if err != nil {
t.Fatal(err)
}
buf := new(bytes.Buffer)
err = data.WriteGIF(buf, &gif.Options{})
if err != nil {
t.Fatal(err)
}
}
2017-09-20 02:51:27 +01:00
func TestNewCaptchaOptions(t *testing.T) {
New(100, 34, func(options *Options) {
options.BackgroundColor = color.Opaque
options.CharPreset = "1234567890"
options.CurveNumber = 0
options.TextLength = 6
2018-10-04 02:48:52 +01:00
options.Palette = palette.WebSafe
2017-09-20 02:51:27 +01:00
})
2017-09-28 01:18:29 +01:00
NewMathExpr(100, 34, func(options *Options) {
options.BackgroundColor = color.Black
})
2022-06-27 06:00:35 +01:00
NewCustomGenerator(100, 34, func() (anwser string, question string) {
return "4", "2x2?"
}, func(o *Options) {
o.BackgroundColor = color.Black
})
2017-09-28 01:18:29 +01:00
}
func TestNewMathExpr(t *testing.T) {
_, err := NewMathExpr(150, 50)
if err != nil {
t.Fatal(err)
}
2017-09-20 02:51:27 +01:00
}
2022-06-10 05:16:34 +01:00
func TestNewCustomGenerator(t *testing.T) {
_, err := NewCustomGenerator(150, 50, func() (anwser string, question string) {
return "1", "2"
})
if err != nil {
t.Fatal(err)
}
}
2020-02-18 07:13:05 +00:00
func TestNilFontError(t *testing.T) {
2017-09-20 02:51:27 +01:00
temp := ttfFont
ttfFont = nil
_, err := New(150, 50)
if err == nil {
t.Fatal("Expect to get nil font error")
}
2017-09-28 01:18:29 +01:00
_, err = NewMathExpr(150, 50)
if err == nil {
t.Fatal("Expect to get nil font error")
}
2022-06-27 06:00:35 +01:00
_, err = NewCustomGenerator(150, 50, func() (anwser string, question string) {
return "1", "2"
})
if err == nil {
t.Fatal("Expect to get nil font error")
}
2017-09-20 02:51:27 +01:00
ttfFont = temp
}
2017-09-22 03:01:40 +01:00
2017-11-08 06:21:40 +00:00
type errReader struct{}
2017-10-15 13:15:14 +01:00
func (errReader) Read(_ []byte) (int, error) {
return 0, errors.New("")
}
2020-02-18 07:13:05 +00:00
func TestReaderErr(t *testing.T) {
2017-10-15 13:15:14 +01:00
err := LoadFontFromReader(errReader{})
if err == nil {
t.Fatal("Expect to get io.Reader error")
}
}
2017-09-22 03:01:40 +01:00
func TestLoadFont(t *testing.T) {
err := LoadFont(goregular.TTF)
if err != nil {
t.Fatal("Fail to load go font")
}
err = LoadFont([]byte("invalid"))
if err == nil {
t.Fatal("LoadFont incorrectly parse an invalid font")
2017-09-22 03:01:40 +01:00
}
}
2017-09-27 06:07:08 +01:00
func TestLoadFontFromReader(t *testing.T) {
file, err := os.Open("./fonts/Comismsh.ttf")
if err != nil {
t.Fatal("Fail to load test file")
}
if err = LoadFontFromReader(file); err != nil {
t.Fatal("Fail to load font from io.Reader")
}
}
2017-09-27 06:07:08 +01:00
func TestMaxColor(t *testing.T) {
var result uint32
result = maxColor()
if result != 0 {
t.Fatalf("Expect max color to be 0, got %v", result)
}
result = maxColor(1)
if result != 1 {
t.Fatalf("Expect max color to be 1, got %v", result)
}
result = maxColor(52428, 65535)
if result != 255 {
t.Fatalf("Expect max color to be 255, got %v", result)
}
var rng = rand.New(rand.NewSource(0))
for i := 0; i < 10; i++ {
result = maxColor(rng.Uint32(), rng.Uint32(), rng.Uint32())
if result > 255 {
t.Fatalf("Number out of range: %v", result)
}
}
}
func TestMinColor(t *testing.T) {
var result uint32
result = minColor()
if result != 255 {
t.Fatalf("Expect min color to be 255, got %v", result)
}
result = minColor(1)
if result != 1 {
t.Fatalf("Expect min color to be 1, got %v", result)
}
result = minColor(52428, 65535)
if result != 204 {
t.Fatalf("Expect min color to be 1, got %v", result)
}
var rng = rand.New(rand.NewSource(0))
for i := 0; i < 10; i++ {
result = minColor(rng.Uint32(), rng.Uint32(), rng.Uint32())
if result > 255 {
t.Fatalf("Number out of range: %v", result)
}
}
}