Skip to content

Commit 75bdf2d

Browse files
committed
lnwire: use assertEqualFunc in onion failure harness
Simplifies the code slightly and improves the error message printed if the original and deserialized messages do not match.
1 parent d0e6a7a commit 75bdf2d

File tree

1 file changed

+15
-25
lines changed

1 file changed

+15
-25
lines changed

lnwire/fuzz_test.go

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bytes"
55
"compress/zlib"
66
"encoding/binary"
7-
"reflect"
87
"testing"
98

109
"github.com/stretchr/testify/require"
@@ -554,16 +553,12 @@ func prefixWithFailCode(data []byte, code FailCode) []byte {
554553
return data
555554
}
556555

557-
// equalFunc is a function used to determine whether two deserialized messages
558-
// are equivalent.
559-
type equalFunc func(x, y any) bool
560-
561556
// onionFailureHarnessCustom performs the actual fuzz testing of the appropriate
562557
// onion failure message. This function will check that the passed-in message
563558
// passes wire length checks, is a valid message once deserialized, and passes a
564559
// sequence of serialization and deserialization checks.
565560
func onionFailureHarnessCustom(t *testing.T, data []byte, code FailCode,
566-
eq equalFunc) {
561+
assertEqual assertEqualFunc) {
567562

568563
data = prefixWithFailCode(data, code)
569564

@@ -589,12 +584,7 @@ func onionFailureHarnessCustom(t *testing.T, data []byte, code FailCode,
589584
newMsg, err := DecodeFailureMessage(&b, 0)
590585
require.NoError(t, err, "failed to decode serialized failure message")
591586

592-
require.True(
593-
t, eq(msg, newMsg),
594-
"original message and deserialized message are not equal: "+
595-
"%v != %v",
596-
msg, newMsg,
597-
)
587+
assertEqual(t, msg, newMsg)
598588

599589
// Now verify that encoding/decoding full packets works as expected.
600590

@@ -628,25 +618,23 @@ func onionFailureHarnessCustom(t *testing.T, data []byte, code FailCode,
628618
pktMsg, err := DecodeFailure(&pktBuf, 0)
629619
require.NoError(t, err, "failed to decode failure packet")
630620

631-
require.True(
632-
t, eq(msg, pktMsg),
633-
"original message and decoded packet message are not equal: "+
634-
"%v != %v",
635-
msg, pktMsg,
636-
)
621+
assertEqual(t, msg, pktMsg)
637622
}
638623

639624
func onionFailureHarness(t *testing.T, data []byte, code FailCode) {
640625
t.Helper()
641-
onionFailureHarnessCustom(t, data, code, reflect.DeepEqual)
626+
assertEq := func(t *testing.T, x, y any) {
627+
require.Equal(t, x, y)
628+
}
629+
onionFailureHarnessCustom(t, data, code, assertEq)
642630
}
643631

644632
func FuzzFailIncorrectDetails(f *testing.F) {
645633
f.Fuzz(func(t *testing.T, data []byte) {
646634
// Since FailIncorrectDetails.Decode can leave extraOpaqueData
647635
// as nil while FailIncorrectDetails.Encode writes an empty
648636
// slice, we need to use a custom equality function.
649-
eq := func(x, y any) bool {
637+
assertEq := func(t *testing.T, x, y any) {
650638
msg1, ok := x.(*FailIncorrectDetails)
651639
require.True(
652640
t, ok, "msg1 was not FailIncorrectDetails",
@@ -657,16 +645,18 @@ func FuzzFailIncorrectDetails(f *testing.F) {
657645
t, ok, "msg2 was not FailIncorrectDetails",
658646
)
659647

660-
return msg1.amount == msg2.amount &&
661-
msg1.height == msg2.height &&
662-
bytes.Equal(
648+
require.Equal(t, msg1.amount, msg2.amount)
649+
require.Equal(t, msg1.height, msg2.height)
650+
require.True(
651+
t, bytes.Equal(
663652
msg1.extraOpaqueData,
664653
msg2.extraOpaqueData,
665-
)
654+
),
655+
)
666656
}
667657

668658
onionFailureHarnessCustom(
669-
t, data, CodeIncorrectOrUnknownPaymentDetails, eq,
659+
t, data, CodeIncorrectOrUnknownPaymentDetails, assertEq,
670660
)
671661
})
672662
}

0 commit comments

Comments
 (0)