nonce_test.go 947 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. * Public Domain Software
  3. *
  4. * I (Matthias Ladkau) am the author of the source code in this file.
  5. * I have placed the source code in this file in the public domain.
  6. *
  7. * For further information see: http://creativecommons.org/publicdomain/zero/1.0/
  8. */
  9. package datautil
  10. import (
  11. "testing"
  12. )
  13. func TestNonces(t *testing.T) {
  14. n1 := NewNonce()
  15. n2 := NewNonce()
  16. // Test normal check
  17. if err := CheckNonce(n1); err != nil {
  18. t.Error(err)
  19. return
  20. }
  21. // Test consumption
  22. if err := ConsumeNonce(n1); err != nil {
  23. t.Error(err)
  24. return
  25. }
  26. if err := CheckNonce(n1); err != ErrInvlaidNonce {
  27. t.Error("Nonce should no longer be valid")
  28. return
  29. }
  30. // Simulate timeout
  31. nonces = nil
  32. if err := CheckNonce(n2); err != ErrInvlaidNonce {
  33. t.Error("Nonce should no longer be valid")
  34. return
  35. }
  36. // Test error case
  37. if err := CheckNonce("test"); err != ErrInvlaidNonce {
  38. t.Error("Nonce should no longer be valid")
  39. return
  40. }
  41. }