zpay32: test case for parsing non UTF-8 description

This commit is contained in:
MPins
2025-06-24 16:29:21 -03:00
committed by MPins
parent 2b1a89fbaf
commit b8acca2c89
2 changed files with 16 additions and 3 deletions

View File

@@ -369,14 +369,25 @@ func TestParse32Bytes(t *testing.T) {
func TestParseDescription(t *testing.T) { func TestParseDescription(t *testing.T) {
t.Parallel() t.Parallel()
testNonUTF8StrData, _ := bech32.ConvertBits(
[]byte(testNonUTF8Str), 8, 5, true,
)
testCupOfCoffeeData, _ := bech32.ConvertBits([]byte(testCupOfCoffee), 8, 5, true) testCupOfCoffeeData, _ := bech32.ConvertBits([]byte(testCupOfCoffee), 8, 5, true)
testPleaseConsiderData, _ := bech32.ConvertBits([]byte(testPleaseConsider), 8, 5, true) testPleaseConsiderData, _ := bech32.ConvertBits([]byte(testPleaseConsider), 8, 5, true)
tests := []struct { tests := []struct {
data []byte data []byte
valid bool valid bool
result *string result *string
expectedErr error
}{ }{
{
data: testNonUTF8StrData,
valid: false,
expectedErr: ErrInvalidUTF8Description,
result: nil,
},
{ {
data: []byte{}, data: []byte{},
valid: true, valid: true,
@@ -400,6 +411,7 @@ func TestParseDescription(t *testing.T) {
t.Errorf("description decoding test %d failed: %v", i, err) t.Errorf("description decoding test %d failed: %v", i, err)
return return
} }
require.ErrorIs(t, err, test.expectedErr)
if test.valid && !reflect.DeepEqual(description, test.result) { if test.valid && !reflect.DeepEqual(description, test.result) {
t.Fatalf("test %d failed decoding description: "+ t.Fatalf("test %d failed decoding description: "+
"expected \"%s\", got \"%s\"", "expected \"%s\", got \"%s\"",

View File

@@ -50,6 +50,7 @@ var (
0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
} }
testNonUTF8Str = "1 cup coffee\xff\xfe\xfd"
testEmptyString = "" testEmptyString = ""
testCupOfCoffee = "1 cup coffee" testCupOfCoffee = "1 cup coffee"
testCoffeeBeans = "coffee beans" testCoffeeBeans = "coffee beans"