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

package modhex
import (
"bytes"
"testing"
)
type encDecTest struct {
enc string
dec []byte
}
var encDecTests = []encDecTest{
{"", []byte{}},
{"dteffuje", []byte{0x2d, 0x34, 0x4e, 0x83}},
{
"hknhfjbrjnlnldnhcujvddbikngjrtgh",
[]byte{0x69, 0xb6, 0x48, 0x1c, 0x8b, 0xab,
0xa2, 0xb6, 0x0e, 0x8f, 0x22, 0x17,
0x9b, 0x58, 0xcd, 0x56},
},
{
"urtubjtnuihvntcreeeecvbregfjibtn",
[]byte{0xec, 0xde, 0x18, 0xdb, 0xe7, 0x6f,
0xbd, 0x0c, 0x33, 0x33, 0x0f, 0x1c,
0x35, 0x48, 0x71, 0xdb},
},
}
func TestEncode(t *testing.T) {
for i, test := range encDecTests {
dst := make([]byte, EncodedLen(len(test.dec)))
n := Encode(dst, test.dec)
if n != len(dst) {
t.Errorf("#%d: bad return value: got %d want: %d", i, n, len(dst))
}
if string(dst) != test.enc {
t.Errorf("#%d: got: %#v want %#v", i, dst, []byte(test.enc))
}
}
}
func TestDecode(t *testing.T) {
for i, test := range encDecTests {
dst := make([]byte, DecodedLen(len(test.enc)))
n, err := Decode(dst, []byte(test.enc))
if err != nil {
t.Errorf("#%d: unexpected err value: %s", i, err)
continue
}
if n != len(dst) {
t.Errorf("#%d: bad return value got: %d want:%d", i, n, len(dst))
}
if !bytes.Equal(dst, test.dec) {
t.Errorf("#%d: got %#v want: %#v", i, dst, test.dec)
}
}
}
func TestEncodeToString(t *testing.T) {
for i, test := range encDecTests {
s := EncodeToString(test.dec)
if s != test.enc {
t.Errorf("#%d: got: %s want: %s", i, s, test.enc)
}
}
}
func TestDecodeString(t *testing.T) {
for i, test := range encDecTests {
dst, err := DecodeString(test.enc)
if err != nil {
t.Errorf("#%d: unexpected err value: %s", i, err)
continue
}
if !bytes.Equal(dst, test.dec) {
t.Errorf("#%d: got %#v want: %#v", i, dst, test.dec)
}
}
}
type errTest struct {
in string
out string
err error
}
var errTests = []errTest{
{"", "", nil},
{"c", "", ErrLength},
{"zteff", "", InvalidByteError('z')},
{"dtefz", "\x2d\x34", InvalidByteError('z')},
{"ecebe", "01", ErrLength},
{"cq", "", InvalidByteError('q')},
{"ccqq", "\x00", InvalidByteError('q')},
{"c\x01", "", InvalidByteError('\x01')},
{"vvuut", "\xff\xee", ErrLength},
}
func TestDecodeErr(t *testing.T) {
for i, test := range errTests {
out := make([]byte, len(test.in)+10)
n, err := Decode(out, []byte(test.in))
if string(out[:n]) != test.out || err != test.err {
t.Errorf("#%d: Decode(%q) = %q, %v, want %q, %v", i, test.in, string(out[:n]), err, test.out, test.err)
}
}
}
func TestDecodeStringErr(t *testing.T) {
for i, test := range errTests {
out, err := DecodeString(test.in)
if string(out) != test.out || err != test.err {
t.Errorf("#%d: DecodeString(%q) = %q, %v, want %q, %v", i, test.in, out, err, test.out, test.err)
}
}
}