lnwire: rand CustomRecords for UpdateAddHTLC encode/decode fuzz test

This commit adds a random `CustomRecords` field to the `UpdateAddHTLC`
message encode/decode unit test.
This commit is contained in:
ffranr 2024-05-14 12:51:02 +01:00
parent b93d2885e3
commit 6fa2db0663
No known key found for this signature in database
GPG Key ID: B1F8848557AA29D2

View File

@ -389,6 +389,45 @@ func TestEmptyMessageUnknownType(t *testing.T) {
}
}
// randCustomRecords generates a random set of custom records for testing.
func randCustomRecords(t *testing.T, r *rand.Rand) CustomRecords {
var (
customRecords = CustomRecords{}
// We'll generate a random number of records,
// between 0 and 10.
numRecords = r.Intn(10)
)
// For each record, we'll generate a random key and
// value.
for i := 0; i < numRecords; i++ {
// Keys must be equal to or greater than
// MinCustomRecordsTlvType.
keyOffset := uint64(r.Intn(100))
key := MinCustomRecordsTlvType + keyOffset
// Values are byte slices of any length.
value := make([]byte, r.Intn(100))
_, err := r.Read(value)
require.NoError(t, err)
customRecords[key] = value
}
// Validate the custom records as a sanity check.
err := customRecords.Validate()
require.NoError(t, err)
// If by chance we end up with an empty set of custom records, we'll
// set it to nil to simplify test comparisons.
if len(customRecords) == 0 {
customRecords = nil
}
return customRecords
}
// TestLightningWireProtocol uses the testing/quick package to create a series
// of fuzz tests to attempt to break a primary scenario which is implemented as
// property based testing scenario.
@ -1353,6 +1392,8 @@ func TestLightningWireProtocol(t *testing.T) {
_, err = r.Read(req.OnionBlob[:])
require.NoError(t, err)
req.CustomRecords = randCustomRecords(t, r)
// Generate a blinding point 50% of the time, since not
// all update adds will use route blinding.
if r.Int31()%2 == 0 {