htlcswitch: fuzz hop.Payload and route.Hop

hop.Payload and route.Hop are analogs, with onion payloads encoded from
route.Hops and decoded to hop.Payloads. For checking equality of
encoding/decoding, we implement a helper function to convert
hop.Payloads into route.Hops.
This commit is contained in:
Matt Morehouse
2023-09-07 15:26:22 -05:00
parent 9c51bea790
commit 5863b9f2fc

View File

@@ -2,9 +2,11 @@ package hop
import (
"bytes"
"errors"
"testing"
sphinx "github.com/lightningnetwork/lightning-onion"
"github.com/lightningnetwork/lnd/routing/route"
"github.com/stretchr/testify/require"
)
@@ -78,3 +80,47 @@ func FuzzOnionPacket(f *testing.F) {
require.Equal(t, pkt1, pkt2)
})
}
func hopFromPayload(p *Payload) (*route.Hop, uint64) {
return &route.Hop{
AmtToForward: p.FwdInfo.AmountToForward,
OutgoingTimeLock: p.FwdInfo.OutgoingCTLV,
MPP: p.MPP,
AMP: p.AMP,
Metadata: p.metadata,
CustomRecords: p.customRecords,
}, p.FwdInfo.NextHop.ToUint64()
}
func FuzzPayload(f *testing.F) {
f.Fuzz(func(t *testing.T, data []byte) {
if len(data) > sphinx.MaxPayloadSize {
return
}
r := bytes.NewReader(data)
payload1, err := NewPayloadFromReader(r)
if err != nil {
return
}
var b bytes.Buffer
hop, nextChanID := hopFromPayload(payload1)
err = hop.PackHopPayload(&b, nextChanID)
if errors.Is(err, route.ErrAMPMissingMPP) {
// PackHopPayload refuses to encode an AMP record
// without an MPP record. However, NewPayloadFromReader
// does allow decoding an AMP record without an MPP
// record, since validation is done at a later stage. Do
// not report a bug for this case.
return
}
require.NoError(t, err)
payload2, err := NewPayloadFromReader(&b)
require.NoError(t, err)
require.Equal(t, payload1, payload2)
})
}