mirror of
https://github.com/golang/go
synced 2024-11-21 22:44:40 -07:00
encoding/hex: canonicalize error type names
Also simplify the tests. Fixes #2849. R=golang-dev, bradfitz, r CC=golang-dev https://golang.org/cl/5643045
This commit is contained in:
parent
5e381d3a9a
commit
92f55949f9
@ -7,8 +7,9 @@ package hex
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"strconv"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const hextable = "0123456789abcdef"
|
const hextable = "0123456789abcdef"
|
||||||
@ -29,16 +30,14 @@ func Encode(dst, src []byte) int {
|
|||||||
return len(src) * 2
|
return len(src) * 2
|
||||||
}
|
}
|
||||||
|
|
||||||
// OddLengthInputError results from decoding an odd length slice.
|
// ErrLength results from decoding an odd length slice.
|
||||||
type OddLengthInputError struct{}
|
var ErrLength = errors.New("encoding/hex: odd length hex string")
|
||||||
|
|
||||||
func (OddLengthInputError) Error() string { return "odd length hex string" }
|
// InvalidByteError values describe errors resulting from an invalid byte in a hex string.
|
||||||
|
type InvalidByteError byte
|
||||||
|
|
||||||
// InvalidHexCharError results from finding an invalid character in a hex string.
|
func (e InvalidByteError) Error() string {
|
||||||
type InvalidHexCharError byte
|
return fmt.Sprintf("encoding/hex: invalid byte: %#U", rune(e))
|
||||||
|
|
||||||
func (e InvalidHexCharError) Error() string {
|
|
||||||
return "invalid hex char: " + strconv.Itoa(int(e))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func DecodedLen(x int) int { return x / 2 }
|
func DecodedLen(x int) int { return x / 2 }
|
||||||
@ -46,21 +45,20 @@ func DecodedLen(x int) int { return x / 2 }
|
|||||||
// Decode decodes src into DecodedLen(len(src)) bytes, returning the actual
|
// Decode decodes src into DecodedLen(len(src)) bytes, returning the actual
|
||||||
// number of bytes written to dst.
|
// number of bytes written to dst.
|
||||||
//
|
//
|
||||||
// If Decode encounters invalid input, it returns an OddLengthInputError or an
|
// If Decode encounters invalid input, it returns an error describing the failure.
|
||||||
// InvalidHexCharError.
|
|
||||||
func Decode(dst, src []byte) (int, error) {
|
func Decode(dst, src []byte) (int, error) {
|
||||||
if len(src)%2 == 1 {
|
if len(src)%2 == 1 {
|
||||||
return 0, OddLengthInputError{}
|
return 0, ErrLength
|
||||||
}
|
}
|
||||||
|
|
||||||
for i := 0; i < len(src)/2; i++ {
|
for i := 0; i < len(src)/2; i++ {
|
||||||
a, ok := fromHexChar(src[i*2])
|
a, ok := fromHexChar(src[i*2])
|
||||||
if !ok {
|
if !ok {
|
||||||
return 0, InvalidHexCharError(src[i*2])
|
return 0, InvalidByteError(src[i*2])
|
||||||
}
|
}
|
||||||
b, ok := fromHexChar(src[i*2+1])
|
b, ok := fromHexChar(src[i*2+1])
|
||||||
if !ok {
|
if !ok {
|
||||||
return 0, InvalidHexCharError(src[i*2+1])
|
return 0, InvalidByteError(src[i*2+1])
|
||||||
}
|
}
|
||||||
dst[i] = (a << 4) | b
|
dst[i] = (a << 4) | b
|
||||||
}
|
}
|
||||||
|
@ -9,141 +9,98 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
type encodeTest struct {
|
type encDecTest struct {
|
||||||
in, out []byte
|
enc string
|
||||||
|
dec []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
var encodeTests = []encodeTest{
|
var encDecTests = []encDecTest{
|
||||||
{[]byte{}, []byte{}},
|
{"", []byte{}},
|
||||||
{[]byte{0x01}, []byte{'0', '1'}},
|
{"0001020304050607", []byte{0, 1, 2, 3, 4, 5, 6, 7}},
|
||||||
{[]byte{0xff}, []byte{'f', 'f'}},
|
{"08090a0b0c0d0e0f", []byte{8, 9, 10, 11, 12, 13, 14, 15}},
|
||||||
{[]byte{0xff, 00}, []byte{'f', 'f', '0', '0'}},
|
{"f0f1f2f3f4f5f6f7", []byte{0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7}},
|
||||||
{[]byte{0}, []byte{'0', '0'}},
|
{"f8f9fafbfcfdfeff", []byte{0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff}},
|
||||||
{[]byte{1}, []byte{'0', '1'}},
|
{"67", []byte{'g'}},
|
||||||
{[]byte{2}, []byte{'0', '2'}},
|
{"e3a1", []byte{0xe3, 0xa1}},
|
||||||
{[]byte{3}, []byte{'0', '3'}},
|
|
||||||
{[]byte{4}, []byte{'0', '4'}},
|
|
||||||
{[]byte{5}, []byte{'0', '5'}},
|
|
||||||
{[]byte{6}, []byte{'0', '6'}},
|
|
||||||
{[]byte{7}, []byte{'0', '7'}},
|
|
||||||
{[]byte{8}, []byte{'0', '8'}},
|
|
||||||
{[]byte{9}, []byte{'0', '9'}},
|
|
||||||
{[]byte{10}, []byte{'0', 'a'}},
|
|
||||||
{[]byte{11}, []byte{'0', 'b'}},
|
|
||||||
{[]byte{12}, []byte{'0', 'c'}},
|
|
||||||
{[]byte{13}, []byte{'0', 'd'}},
|
|
||||||
{[]byte{14}, []byte{'0', 'e'}},
|
|
||||||
{[]byte{15}, []byte{'0', 'f'}},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEncode(t *testing.T) {
|
func TestEncode(t *testing.T) {
|
||||||
for i, test := range encodeTests {
|
for i, test := range encDecTests {
|
||||||
dst := make([]byte, EncodedLen(len(test.in)))
|
dst := make([]byte, EncodedLen(len(test.dec)))
|
||||||
n := Encode(dst, test.in)
|
n := Encode(dst, test.dec)
|
||||||
if n != len(dst) {
|
if n != len(dst) {
|
||||||
t.Errorf("#%d: bad return value: got: %d want: %d", i, n, len(dst))
|
t.Errorf("#%d: bad return value: got: %d want: %d", i, n, len(dst))
|
||||||
}
|
}
|
||||||
if bytes.Compare(dst, test.out) != 0 {
|
if string(dst) != test.enc {
|
||||||
t.Errorf("#%d: got: %#v want: %#v", i, dst, test.out)
|
t.Errorf("#%d: got: %#v want: %#v", i, dst, test.enc)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type decodeTest struct {
|
|
||||||
in, out []byte
|
|
||||||
ok bool
|
|
||||||
}
|
|
||||||
|
|
||||||
var decodeTests = []decodeTest{
|
|
||||||
{[]byte{}, []byte{}, true},
|
|
||||||
{[]byte{'0'}, []byte{}, false},
|
|
||||||
{[]byte{'0', 'g'}, []byte{}, false},
|
|
||||||
{[]byte{'0', '\x01'}, []byte{}, false},
|
|
||||||
{[]byte{'0', '0'}, []byte{0}, true},
|
|
||||||
{[]byte{'0', '1'}, []byte{1}, true},
|
|
||||||
{[]byte{'0', '2'}, []byte{2}, true},
|
|
||||||
{[]byte{'0', '3'}, []byte{3}, true},
|
|
||||||
{[]byte{'0', '4'}, []byte{4}, true},
|
|
||||||
{[]byte{'0', '5'}, []byte{5}, true},
|
|
||||||
{[]byte{'0', '6'}, []byte{6}, true},
|
|
||||||
{[]byte{'0', '7'}, []byte{7}, true},
|
|
||||||
{[]byte{'0', '8'}, []byte{8}, true},
|
|
||||||
{[]byte{'0', '9'}, []byte{9}, true},
|
|
||||||
{[]byte{'0', 'a'}, []byte{10}, true},
|
|
||||||
{[]byte{'0', 'b'}, []byte{11}, true},
|
|
||||||
{[]byte{'0', 'c'}, []byte{12}, true},
|
|
||||||
{[]byte{'0', 'd'}, []byte{13}, true},
|
|
||||||
{[]byte{'0', 'e'}, []byte{14}, true},
|
|
||||||
{[]byte{'0', 'f'}, []byte{15}, true},
|
|
||||||
{[]byte{'0', 'A'}, []byte{10}, true},
|
|
||||||
{[]byte{'0', 'B'}, []byte{11}, true},
|
|
||||||
{[]byte{'0', 'C'}, []byte{12}, true},
|
|
||||||
{[]byte{'0', 'D'}, []byte{13}, true},
|
|
||||||
{[]byte{'0', 'E'}, []byte{14}, true},
|
|
||||||
{[]byte{'0', 'F'}, []byte{15}, true},
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestDecode(t *testing.T) {
|
func TestDecode(t *testing.T) {
|
||||||
for i, test := range decodeTests {
|
for i, test := range encDecTests {
|
||||||
dst := make([]byte, DecodedLen(len(test.in)))
|
dst := make([]byte, DecodedLen(len(test.enc)))
|
||||||
n, err := Decode(dst, test.in)
|
n, err := Decode(dst, []byte(test.enc))
|
||||||
if err == nil && n != len(dst) {
|
if err != nil {
|
||||||
t.Errorf("#%d: bad return value: got:%d want:%d", i, n, len(dst))
|
t.Errorf("#%d: bad return value: got:%d want:%d", i, n, len(dst))
|
||||||
}
|
} else if !bytes.Equal(dst, test.dec) {
|
||||||
if test.ok != (err == nil) {
|
t.Errorf("#%d: got: %#v want: %#v", i, dst, test.dec)
|
||||||
t.Errorf("#%d: unexpected err value: %s", i, err)
|
|
||||||
}
|
|
||||||
if err == nil && bytes.Compare(dst, test.out) != 0 {
|
|
||||||
t.Errorf("#%d: got: %#v want: %#v", i, dst, test.out)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type encodeStringTest struct {
|
|
||||||
in []byte
|
|
||||||
out string
|
|
||||||
}
|
|
||||||
|
|
||||||
var encodeStringTests = []encodeStringTest{
|
|
||||||
{[]byte{}, ""},
|
|
||||||
{[]byte{0}, "00"},
|
|
||||||
{[]byte{0, 1}, "0001"},
|
|
||||||
{[]byte{0, 1, 255}, "0001ff"},
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestEncodeToString(t *testing.T) {
|
func TestEncodeToString(t *testing.T) {
|
||||||
for i, test := range encodeStringTests {
|
for i, test := range encDecTests {
|
||||||
s := EncodeToString(test.in)
|
s := EncodeToString(test.dec)
|
||||||
if s != test.out {
|
if s != test.enc {
|
||||||
t.Errorf("#%d got:%s want:%s", i, s, test.out)
|
t.Errorf("#%d got:%s want:%s", i, s, test.enc)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type decodeStringTest struct {
|
|
||||||
in string
|
|
||||||
out []byte
|
|
||||||
ok bool
|
|
||||||
}
|
|
||||||
|
|
||||||
var decodeStringTests = []decodeStringTest{
|
|
||||||
{"", []byte{}, true},
|
|
||||||
{"0", []byte{}, false},
|
|
||||||
{"00", []byte{0}, true},
|
|
||||||
{"0\x01", []byte{}, false},
|
|
||||||
{"0g", []byte{}, false},
|
|
||||||
{"00ff00", []byte{0, 255, 0}, true},
|
|
||||||
{"0000ff", []byte{0, 0, 255}, true},
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestDecodeString(t *testing.T) {
|
func TestDecodeString(t *testing.T) {
|
||||||
for i, test := range decodeStringTests {
|
for i, test := range encDecTests {
|
||||||
dst, err := DecodeString(test.in)
|
dst, err := DecodeString(test.enc)
|
||||||
if test.ok != (err == nil) {
|
if err != nil {
|
||||||
t.Errorf("#%d: unexpected err value: %s", i, err)
|
t.Errorf("#%d: unexpected err value: %s", i, err)
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
if err == nil && bytes.Compare(dst, test.out) != 0 {
|
if bytes.Compare(dst, test.dec) != 0 {
|
||||||
t.Errorf("#%d: got: %#v want: #%v", i, dst, test.out)
|
t.Errorf("#%d: got: %#v want: #%v", i, dst, test.dec)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type errTest struct {
|
||||||
|
in string
|
||||||
|
err string
|
||||||
|
}
|
||||||
|
|
||||||
|
var errTests = []errTest{
|
||||||
|
{"0", "encoding/hex: odd length hex string"},
|
||||||
|
{"0g", "encoding/hex: invalid byte: U+0067 'g'"},
|
||||||
|
{"0\x01", "encoding/hex: invalid byte: U+0001"},
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestInvalidErr(t *testing.T) {
|
||||||
|
for i, test := range errTests {
|
||||||
|
dst := make([]byte, DecodedLen(len(test.in)))
|
||||||
|
_, err := Decode(dst, []byte(test.in))
|
||||||
|
if err == nil {
|
||||||
|
t.Errorf("#%d: expected error; got none")
|
||||||
|
} else if err.Error() != test.err {
|
||||||
|
t.Errorf("#%d: got: %v want: %v", i, err, test.err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestInvalidStringErr(t *testing.T) {
|
||||||
|
for i, test := range errTests {
|
||||||
|
_, err := DecodeString(test.in)
|
||||||
|
if err == nil {
|
||||||
|
t.Errorf("#%d: expected error; got none")
|
||||||
|
} else if err.Error() != test.err {
|
||||||
|
t.Errorf("#%d: got: %v want: %v", i, err, test.err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user