mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-09-20 04:21:37 +02:00
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:
@@ -2,9 +2,11 @@ package hop
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"errors"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
sphinx "github.com/lightningnetwork/lightning-onion"
|
sphinx "github.com/lightningnetwork/lightning-onion"
|
||||||
|
"github.com/lightningnetwork/lnd/routing/route"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -78,3 +80,47 @@ func FuzzOnionPacket(f *testing.F) {
|
|||||||
require.Equal(t, pkt1, pkt2)
|
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)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user