captcha_test.go 1.0 KB

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