yintai2/app/admin/service/captcha_test.go

43 lines
827 B
Go

package service
import (
"bytes"
"image/png"
"strings"
"testing"
)
func TestRandomCaptcha(t *testing.T) {
code := randomCaptcha(4)
if len(code) != 4 {
t.Fatalf("len = %d", len(code))
}
for _, r := range code {
if !strings.ContainsRune(captchaAlphabet, r) {
t.Fatalf("unexpected char %q", r)
}
if _, ok := captchaFont[r]; !ok {
t.Fatalf("missing glyph %q", r)
}
}
}
func TestCaptchaPNG(t *testing.T) {
raw := captchaPNG("W8K2")
img, err := png.Decode(bytes.NewReader(raw))
if err != nil {
t.Fatal(err)
}
if img.Bounds().Dx() < 40 || img.Bounds().Dy() < 20 {
t.Fatalf("image too small: %v", img.Bounds())
}
}
func TestCaptchaFontCoversAlphabet(t *testing.T) {
for _, r := range captchaAlphabet {
if _, ok := captchaFont[r]; !ok {
t.Fatalf("alphabet char %q has no glyph", r)
}
}
}