captcha_test.go (view raw)
1// Copyright 2011 Dmitry Chestnykh. All rights reserved.
2// Use of this source code is governed by a MIT-style
3// license that can be found in the LICENSE file.
4
5package captcha
6
7import (
8 "bytes"
9 "testing"
10)
11
12func TestNew(t *testing.T) {
13 c := New()
14 if c == "" {
15 t.Errorf("expected id, got empty string")
16 }
17}
18
19func TestVerify(t *testing.T) {
20 id := New()
21 if Verify(id, []byte{0, 0}) {
22 t.Errorf("verified wrong captcha")
23 }
24 id = New()
25 d := globalStore.Get(id, false) // cheating
26 if !Verify(id, d) {
27 t.Errorf("proper captcha not verified")
28 }
29}
30
31func TestReload(t *testing.T) {
32 id := New()
33 d1 := globalStore.Get(id, false) // cheating
34 Reload(id)
35 d2 := globalStore.Get(id, false) // cheating again
36 if bytes.Equal(d1, d2) {
37 t.Errorf("reload didn't work: %v = %v", d1, d2)
38 }
39}
40
41func TestRandomDigits(t *testing.T) {
42 d1 := RandomDigits(10)
43 for _, v := range d1 {
44 if v > 9 {
45 t.Errorf("digits not in range 0-9: %v", d1)
46 }
47 }
48 d2 := RandomDigits(10)
49 if bytes.Equal(d1, d2) {
50 t.Errorf("digits seem to be not random")
51 }
52}