174 lines
3.7 KiB
Go
174 lines
3.7 KiB
Go
package text
|
|
|
|
import (
|
|
"bytes"
|
|
"testing"
|
|
)
|
|
|
|
func TestAppendHexFromBinary(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
dst []byte
|
|
src []byte
|
|
quote bool
|
|
expected []byte
|
|
}{
|
|
{
|
|
name: "basic hex encoding with quote",
|
|
dst: []byte("prefix"),
|
|
src: []byte{0x01, 0x23, 0x45, 0x67},
|
|
quote: true,
|
|
expected: []byte(`prefix"01234567"`),
|
|
},
|
|
{
|
|
name: "basic hex encoding without quote",
|
|
dst: []byte("prefix"),
|
|
src: []byte{0x01, 0x23, 0x45, 0x67},
|
|
quote: false,
|
|
expected: []byte("prefix01234567"),
|
|
},
|
|
{
|
|
name: "empty src with quote",
|
|
dst: []byte("start"),
|
|
src: []byte{},
|
|
quote: true,
|
|
expected: []byte(`start""`),
|
|
},
|
|
{
|
|
name: "empty src without quote",
|
|
dst: []byte("start"),
|
|
src: []byte{},
|
|
quote: false,
|
|
expected: []byte("start"),
|
|
},
|
|
{
|
|
name: "single byte with quote",
|
|
dst: []byte{},
|
|
src: []byte{0xff},
|
|
quote: true,
|
|
expected: []byte(`"ff"`),
|
|
},
|
|
{
|
|
name: "single byte without quote",
|
|
dst: []byte{},
|
|
src: []byte{0xff},
|
|
quote: false,
|
|
expected: []byte("ff"),
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
result := AppendHexFromBinary(tt.dst, tt.src, tt.quote)
|
|
if !bytes.Equal(result, tt.expected) {
|
|
t.Errorf("AppendHexFromBinary() = %q, want %q", result, tt.expected)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestAppendBinaryFromHex(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
dst []byte
|
|
src []byte
|
|
unquote bool
|
|
expected []byte
|
|
wantErr bool
|
|
}{
|
|
{
|
|
name: "basic hex decoding with unquote",
|
|
dst: []byte("prefix"),
|
|
src: []byte(`"01234567"`),
|
|
unquote: true,
|
|
expected: []byte("prefix\x01\x23\x45\x67"),
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "basic hex decoding without unquote",
|
|
dst: []byte("prefix"),
|
|
src: []byte("01234567"),
|
|
unquote: false,
|
|
expected: []byte("prefix\x01\x23\x45\x67"),
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "empty hex with unquote",
|
|
dst: []byte("start"),
|
|
src: []byte(`""`),
|
|
unquote: true,
|
|
expected: []byte("start"),
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "empty hex without unquote",
|
|
dst: []byte("start"),
|
|
src: []byte(""),
|
|
unquote: false,
|
|
expected: []byte("start"),
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "single byte hex with unquote",
|
|
dst: []byte{},
|
|
src: []byte(`"ff"`),
|
|
unquote: true,
|
|
expected: []byte{0xff},
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "single byte hex without unquote",
|
|
dst: []byte{},
|
|
src: []byte("ff"),
|
|
unquote: false,
|
|
expected: []byte{0xff},
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "invalid hex with unquote",
|
|
dst: []byte{},
|
|
src: []byte(`"gg"`),
|
|
unquote: true,
|
|
expected: []byte{},
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "invalid hex without unquote",
|
|
dst: []byte{},
|
|
src: []byte("gg"),
|
|
unquote: false,
|
|
expected: []byte{},
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "odd length hex with unquote",
|
|
dst: []byte{},
|
|
src: []byte(`"123"`),
|
|
unquote: true,
|
|
expected: []byte{},
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "odd length hex without unquote",
|
|
dst: []byte{},
|
|
src: []byte("123"),
|
|
unquote: false,
|
|
expected: []byte{},
|
|
wantErr: true,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
result, err := AppendBinaryFromHex(tt.dst, tt.src, tt.unquote)
|
|
if (err != nil) != tt.wantErr {
|
|
t.Errorf("AppendBinaryFromHex() error = %v, wantErr %v", err, tt.wantErr)
|
|
return
|
|
}
|
|
if !tt.wantErr && !bytes.Equal(result, tt.expected) {
|
|
t.Errorf("AppendBinaryFromHex() = %q, want %q", result, tt.expected)
|
|
}
|
|
})
|
|
}
|
|
}
|