stringcrypt_test.go 994 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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 cryptutil
  10. import (
  11. "testing"
  12. )
  13. func TestStringEncryption(t *testing.T) {
  14. secret := "This is a test"
  15. encString, err := EncryptString("foo", secret)
  16. if err != nil {
  17. t.Error(err)
  18. return
  19. }
  20. decString, err := DecryptString("foo", encString)
  21. if err != nil {
  22. t.Error(err)
  23. return
  24. }
  25. if decString != secret {
  26. t.Error("Unexpected result:", decString, secret)
  27. return
  28. }
  29. decString, err = DecryptString("foo1", encString)
  30. if err.Error() != "Could not decrypt data" {
  31. t.Error(err)
  32. return
  33. }
  34. if decString != "" {
  35. t.Error("Unexpected result:", decString)
  36. return
  37. }
  38. _, err = DecryptString("foo1", "bar")
  39. if err.Error() != "Ciphertext is too short - must be at least: 16" {
  40. t.Error(err)
  41. return
  42. }
  43. }