Browse Source

Refactor errors and tests

master
Blink The Things 4 years ago
parent
commit
5347da1624
2 changed files with 86 additions and 45 deletions
  1. +16
    -4
      modhex.go
  2. +70
    -41
      modhex_test.go

+ 16
- 4
modhex.go View File

@ -67,6 +67,18 @@ func DecodedLen(n int) int {
return n / 2 return n / 2
} }
// ErrLength reports an attempt to decode an odd-length input using Decode
// or DecodeString.
var ErrLength = errors.New("modhex: odd length modhex string")
// InvalidByteError values describe errors resulting from an invalid byte in a
// hex string.
type InvalidByteError byte
func (e InvalidByteError) Error() string {
return fmt.Sprintf("modhex: invalid byte: %#U", rune(e))
}
// Decode decodes src into DecodedLen(len(src)) bytes, returning // Decode decodes src into DecodedLen(len(src)) bytes, returning
// the actual number of bytes written to dst. // the actual number of bytes written to dst.
// //
@ -78,12 +90,12 @@ func Decode(dst, src []byte) (int, error) {
for ; j < len(src); j += 2 { for ; j < len(src); j += 2 {
a, ok := modhexMap[src[j-1]] a, ok := modhexMap[src[j-1]]
if !ok { if !ok {
return i, fmt.Errorf("modhex: invalid byte: %#U", rune(src[j-1])) return i, InvalidByteError(src[j-1])
} }
b, ok := modhexMap[src[j]] b, ok := modhexMap[src[j]]
if !ok { if !ok {
return i, fmt.Errorf("modhex: invalid byte: %#U", rune(src[j])) return i, InvalidByteError(src[j])
} }
dst[i] = (a << 4) | b dst[i] = (a << 4) | b
@ -92,10 +104,10 @@ func Decode(dst, src []byte) (int, error) {
if len(src)%2 == 1 { if len(src)%2 == 1 {
if _, ok := modhexMap[src[j-1]]; !ok { if _, ok := modhexMap[src[j-1]]; !ok {
return i, fmt.Errorf("modhex: invalid byte: %#U", rune(src[j-1])) return i, InvalidByteError(src[j-1])
} }
return i, errors.New("modhex: odd length modhex string") return i, ErrLength
} }
return i, nil return i, nil


+ 70
- 41
modhex_test.go View File

@ -1,60 +1,89 @@
package modhex package modhex
import ( import (
"encoding/hex" "bytes"
"strings"
"testing" "testing"
) )
type test struct { type encDecTest struct {
hex string enc string
modhex string dec []byte
} }
func TestEncodeHexDecodeHex(t *testing.T) { var encDecTests = []encDecTest{
var tests = []test{ {"", []byte{}},
{ {"dteffuje", []byte{0x2d, 0x34, 0x4e, 0x83}},
modhex: "dteffuje", {
hex: "2d344e83", "hknhfjbrjnlnldnhcujvddbikngjrtgh",
}, []byte{0x69, 0xb6, 0x48, 0x1c, 0x8b, 0xab,
0xa2, 0xb6, 0x0e, 0x8f, 0x22, 0x17,
{ 0x9b, 0x58, 0xcd, 0x56},
modhex: "DTEFFUJE", },
hex: "2d344e83", {
}, "urtubjtnuihvntcreeeecvbregfjibtn",
[]byte{0xec, 0xde, 0x18, 0xdb, 0xe7, 0x6f,
{ 0xbd, 0x0c, 0x33, 0x33, 0x0f, 0x1c,
modhex: "dTeFfUjE", 0x35, 0x48, 0x71, 0xdb},
hex: "2d344e83", },
}, }
func TestEncode(t *testing.T) {
{ for i, test := range encDecTests {
modhex: "hknhfjbrjnlnldnhcujvddbikngjrtgh", dst := make([]byte, EncodedLen(len(test.dec)))
hex: "69b6481c8baba2b60e8f22179b58cd56", n := Encode(dst, test.dec)
}, if n != len(dst) {
t.Errorf("#%d: bad return value: got %d want: %d", i, n, len(dst))
{ }
modhex: "urtubjtnuihvntcreeeecvbregfjibtn", if string(dst) != test.enc {
hex: "ecde18dbe76fbd0c33330f1c354871db", 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)))
for _, tc := range tests { n, err := Decode(dst, []byte(test.enc))
src, _ := hex.DecodeString(tc.hex)
modhex := EncodeToString(src)
if modhex != strings.ToLower(modhex) { if err != nil {
t.Errorf("Encode Incorrect: Actual: %s; Expected: %s\n", modhex, tc.modhex) t.Errorf("#%d: unexpected err value: %s", i, err)
continue
} }
hexBytes, err := DecodeString(tc.modhex) 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 { if err != nil {
t.Error(err) t.Errorf("#%d: unexpected err value: %s", i, err)
continue
} }
hexStr := hex.EncodeToString(hexBytes) if !bytes.Equal(dst, test.dec) {
if hexStr != tc.hex { t.Errorf("#%d: got %#v want: %#v", i, dst, test.dec)
t.Errorf("Decode Incorrect: Actual: %s; Expected: %s\n", hexStr, tc.hex)
} }
} }
} }

|||||||
x
 
000:0
Loading…
Cancel
Save