You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

126 lines
2.7 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. package modhex
  2. import (
  3. "bytes"
  4. "testing"
  5. )
  6. type encDecTest struct {
  7. enc string
  8. dec []byte
  9. }
  10. var encDecTests = []encDecTest{
  11. {"", []byte{}},
  12. {"dteffuje", []byte{0x2d, 0x34, 0x4e, 0x83}},
  13. {
  14. "hknhfjbrjnlnldnhcujvddbikngjrtgh",
  15. []byte{0x69, 0xb6, 0x48, 0x1c, 0x8b, 0xab,
  16. 0xa2, 0xb6, 0x0e, 0x8f, 0x22, 0x17,
  17. 0x9b, 0x58, 0xcd, 0x56},
  18. },
  19. {
  20. "urtubjtnuihvntcreeeecvbregfjibtn",
  21. []byte{0xec, 0xde, 0x18, 0xdb, 0xe7, 0x6f,
  22. 0xbd, 0x0c, 0x33, 0x33, 0x0f, 0x1c,
  23. 0x35, 0x48, 0x71, 0xdb},
  24. },
  25. }
  26. func TestEncode(t *testing.T) {
  27. for i, test := range encDecTests {
  28. dst := make([]byte, EncodedLen(len(test.dec)))
  29. n := Encode(dst, test.dec)
  30. if n != len(dst) {
  31. t.Errorf("#%d: bad return value: got %d want: %d", i, n, len(dst))
  32. }
  33. if string(dst) != test.enc {
  34. t.Errorf("#%d: got: %#v want %#v", i, dst, []byte(test.enc))
  35. }
  36. }
  37. }
  38. func TestDecode(t *testing.T) {
  39. for i, test := range encDecTests {
  40. dst := make([]byte, DecodedLen(len(test.enc)))
  41. n, err := Decode(dst, []byte(test.enc))
  42. if err != nil {
  43. t.Errorf("#%d: unexpected err value: %s", i, err)
  44. continue
  45. }
  46. if n != len(dst) {
  47. t.Errorf("#%d: bad return value got: %d want:%d", i, n, len(dst))
  48. }
  49. if !bytes.Equal(dst, test.dec) {
  50. t.Errorf("#%d: got %#v want: %#v", i, dst, test.dec)
  51. }
  52. }
  53. }
  54. func TestEncodeToString(t *testing.T) {
  55. for i, test := range encDecTests {
  56. s := EncodeToString(test.dec)
  57. if s != test.enc {
  58. t.Errorf("#%d: got: %s want: %s", i, s, test.enc)
  59. }
  60. }
  61. }
  62. func TestDecodeString(t *testing.T) {
  63. for i, test := range encDecTests {
  64. dst, err := DecodeString(test.enc)
  65. if err != nil {
  66. t.Errorf("#%d: unexpected err value: %s", i, err)
  67. continue
  68. }
  69. if !bytes.Equal(dst, test.dec) {
  70. t.Errorf("#%d: got %#v want: %#v", i, dst, test.dec)
  71. }
  72. }
  73. }
  74. type errTest struct {
  75. in string
  76. out string
  77. err error
  78. }
  79. var errTests = []errTest{
  80. {"", "", nil},
  81. {"c", "", ErrLength},
  82. {"zteff", "", InvalidByteError('z')},
  83. {"dtefz", "\x2d\x34", InvalidByteError('z')},
  84. {"ecebe", "01", ErrLength},
  85. {"cq", "", InvalidByteError('q')},
  86. {"ccqq", "\x00", InvalidByteError('q')},
  87. {"c\x01", "", InvalidByteError('\x01')},
  88. {"vvuut", "\xff\xee", ErrLength},
  89. }
  90. func TestDecodeErr(t *testing.T) {
  91. for i, test := range errTests {
  92. out := make([]byte, len(test.in)+10)
  93. n, err := Decode(out, []byte(test.in))
  94. if string(out[:n]) != test.out || err != test.err {
  95. t.Errorf("#%d: Decode(%q) = %q, %v, want %q, %v", i, test.in, string(out[:n]), err, test.out, test.err)
  96. }
  97. }
  98. }
  99. func TestDecodeStringErr(t *testing.T) {
  100. for i, test := range errTests {
  101. out, err := DecodeString(test.in)
  102. if string(out) != test.out || err != test.err {
  103. t.Errorf("#%d: DecodeString(%q) = %q, %v, want %q, %v", i, test.in, out, err, test.out, test.err)
  104. }
  105. }
  106. }