all repos — captcha @ ca43a14c3943823f0dc67fc372ff5ad20d6e1889

Go package captcha implements generation and verification of image and audio CAPTCHAs.

captcha_test.go (view raw)

 1package captcha
 2
 3import (
 4	"bytes"
 5	"testing"
 6)
 7
 8func TestNew(t *testing.T) {
 9	c := New(StdLength)
10	if c == "" {
11		t.Errorf("expected id, got empty string")
12	}
13}
14
15func TestVerify(t *testing.T) {
16	id := New(StdLength)
17	if Verify(id, []byte{0, 0}) {
18		t.Errorf("verified wrong captcha")
19	}
20	id = New(StdLength)
21	d := globalStore.Get(id, false) // cheating
22	if !Verify(id, d) {
23		t.Errorf("proper captcha not verified")
24	}
25}
26
27func TestReload(t *testing.T) {
28	id := New(StdLength)
29	d1 := globalStore.Get(id, false) // cheating
30	Reload(id)
31	d2 := globalStore.Get(id, false) // cheating again
32	if bytes.Equal(d1, d2) {
33		t.Errorf("reload didn't work: %v = %v", d1, d2)
34	}
35}
36
37func TestRandomDigits(t *testing.T) {
38	d1 := RandomDigits(10)
39	for _, v := range d1 {
40		if v > 9 {
41			t.Errorf("digits not in range 0-9: %v", d1)
42		}
43	}
44	d2 := RandomDigits(10)
45	if bytes.Equal(d1, d2) {
46		t.Errorf("digits seem to be not random")
47	}
48}