From b8acca2c892d1961e65a6b58dcb925c41cc5c3d0 Mon Sep 17 00:00:00 2001 From: MPins Date: Tue, 24 Jun 2025 16:29:21 -0300 Subject: [PATCH] zpay32: test case for parsing non UTF-8 description --- zpay32/invoice_internal_test.go | 18 +++++++++++++++--- zpay32/invoice_test.go | 1 + 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/zpay32/invoice_internal_test.go b/zpay32/invoice_internal_test.go index 35091d6a6..a9fed9bc8 100644 --- a/zpay32/invoice_internal_test.go +++ b/zpay32/invoice_internal_test.go @@ -369,14 +369,25 @@ func TestParse32Bytes(t *testing.T) { func TestParseDescription(t *testing.T) { t.Parallel() + testNonUTF8StrData, _ := bech32.ConvertBits( + []byte(testNonUTF8Str), 8, 5, true, + ) + testCupOfCoffeeData, _ := bech32.ConvertBits([]byte(testCupOfCoffee), 8, 5, true) testPleaseConsiderData, _ := bech32.ConvertBits([]byte(testPleaseConsider), 8, 5, true) tests := []struct { - data []byte - valid bool - result *string + data []byte + valid bool + result *string + expectedErr error }{ + { + data: testNonUTF8StrData, + valid: false, + expectedErr: ErrInvalidUTF8Description, + result: nil, + }, { data: []byte{}, valid: true, @@ -400,6 +411,7 @@ func TestParseDescription(t *testing.T) { t.Errorf("description decoding test %d failed: %v", i, err) return } + require.ErrorIs(t, err, test.expectedErr) if test.valid && !reflect.DeepEqual(description, test.result) { t.Fatalf("test %d failed decoding description: "+ "expected \"%s\", got \"%s\"", diff --git a/zpay32/invoice_test.go b/zpay32/invoice_test.go index 55718007d..4cb1a6170 100644 --- a/zpay32/invoice_test.go +++ b/zpay32/invoice_test.go @@ -50,6 +50,7 @@ var ( 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, } + testNonUTF8Str = "1 cup coffee\xff\xfe\xfd" testEmptyString = "" testCupOfCoffee = "1 cup coffee" testCoffeeBeans = "coffee beans"