z85_test.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package z85
  2. import (
  3. "bytes"
  4. "testing"
  5. )
  6. func TestEncodingAndDecoding(t *testing.T) {
  7. tests := []struct {
  8. src []byte
  9. z85 string
  10. err error
  11. }{
  12. {nil, "", nil},
  13. {[]byte{0x86, 0x4F, 0xD2, 0x6F, 0xB5, 0x59, 0xF7, 0x5B}, "HelloWorld", nil},
  14. {[]byte{0x8E, 0x0B, 0xDD, 0x69, 0x76, 0x28, 0xB9, 0x1D,
  15. 0x8F, 0x24, 0x55, 0x87, 0xEE, 0x95, 0xC5, 0xB0,
  16. 0x4D, 0x48, 0x96, 0x3F, 0x79, 0x25, 0x98, 0x77,
  17. 0xB4, 0x9C, 0xD9, 0x06, 0x3A, 0xEA, 0xD3, 0xB7,
  18. }, "JTKVSB%%)wK0E.X)V>+}o?pNmC{O&4W4b!Ni{Lh6", nil},
  19. // Alignment error
  20. {[]byte{1, 2, 3}, "abcdefghi", ErrLength},
  21. // Invalid data
  22. {nil, ` `, InvalidByteError(' ')}, // ' ' < minDigit
  23. {nil, `~~~~~`, InvalidByteError('~')}, // '~' > maxDigit
  24. {nil, `"""""`, InvalidByteError('"')}, // '"' is in range, but not part of the z85 alphabet
  25. }
  26. for _, test := range tests {
  27. // Encode
  28. if _, ok := test.err.(InvalidByteError); !ok {
  29. dst := make([]byte, EncodedLen(len(test.src)))
  30. n, err := Encode(dst, test.src)
  31. if err != test.err {
  32. t.Fatalf("got %q, want %q", err, test.err)
  33. }
  34. if err == nil {
  35. if !bytes.Equal([]byte(test.z85), dst) {
  36. t.Fatalf("got %q, want %q", dst, test.z85)
  37. }
  38. if n != len(test.z85) {
  39. t.Fatalf("got %d, want %d", n, len(test.z85))
  40. }
  41. }
  42. }
  43. // Decode
  44. dst := make([]byte, DecodedLen(len(test.z85)))
  45. n, err := Decode(dst, []byte(test.z85))
  46. if err != test.err {
  47. t.Fatalf("got %q, want %q", err, test.err)
  48. }
  49. if err == nil {
  50. if !bytes.Equal(test.src, dst) {
  51. t.Fatalf("got %d, want %d", dst, test.src)
  52. }
  53. if n != len(dst) {
  54. t.Fatalf("got %d, want %d", n, len(dst))
  55. }
  56. }
  57. }
  58. }