captcha_test.go 884 B

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