What changed, and why it matters
This commit only changes test code in LND's lnwire package. It adds a helper function that creates properly formatted TLV (Type-Length-Value) data for use in unit tests, replacing an older helper that generated random bytes. Some message types now require valid TLV formatting during encoding/decoding, so the tests were updated to supply valid data. There is no change to production code and no security vulnerability is present.
No action required. This is a test-only change that improves test data validity. It can be treated as routine maintenance.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The diff modifies lnwire/message_test.go. It introduces createValidTLVExtraData, which generates 1-4 canonically ordered, valid TLV records using tlv.MakePrimitiveRecord and lnwire.EncodeRecords. Several test message constructors (newMsgInit, newMsgShutdown, newMsgUpdateAddHTLC, newMsgUpdateFulfillHTLC, newMsgCommitSig, newMsgChannelAnnouncement, newMsgNodeAnnouncement) are updated to use this helper instead of createExtraData, which produced raw random bytes. The commit message states this was needed because some messages’ Encode/Decode methods now validate/re-parse ExtraData as TLV. No runtime code is affected.
Changed components
lnwire/message_test.goInspect captured patch +44 / −9
diff --git a/lnwire/message_test.go b/lnwire/message_test.go
index 7b03373..340c798 100644
--- a/lnwire/message_test.go
+++ b/lnwire/message_test.go
@@ -16,6 +16,7 @@ import (
"github.com/btcsuite/btcd/btcec/v2/ecdsa"
"github.com/btcsuite/btcd/btcutil"
"github.com/lightningnetwork/lnd/lnwire"
+ "github.com/lightningnetwork/lnd/tlv"
"github.com/lightningnetwork/lnd/tor"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
@@ -308,13 +309,13 @@ func newMsgWarning(tb testing.TB, r io.Reader) *lnwire.Warning {
return msg
}
-func newMsgInit(t testing.TB, r io.Reader) *lnwire.Init {
+func newMsgInit(t testing.TB, r *rand.Rand) *lnwire.Init {
t.Helper()
return &lnwire.Init{
GlobalFeatures: rawFeatureVector(),
Features: rawFeatureVector(),
- ExtraData: createExtraData(t, r),
+ ExtraData: createValidTLVExtraData(t, r),
}
}
@@ -476,7 +477,7 @@ func newMsgShutdown(t testing.TB, r *rand.Rand) *lnwire.Shutdown {
msg := &lnwire.Shutdown{
Address: randDeliveryAddress(t, r),
- ExtraData: createExtraData(t, r),
+ ExtraData: createValidTLVExtraData(t, r),
}
_, err := r.Read(msg.ChannelID[:])
@@ -507,7 +508,7 @@ func newMsgUpdateAddHTLC(t testing.TB, r *rand.Rand) *lnwire.UpdateAddHTLC {
ID: r.Uint64(),
Amount: lnwire.MilliSatoshi(r.Int63()),
Expiry: r.Uint32(),
- ExtraData: createExtraData(t, r),
+ ExtraData: createValidTLVExtraData(t, r),
}
_, err := r.Read(msg.ChanID[:])
@@ -529,7 +530,7 @@ func newMsgUpdateFulfillHTLC(t testing.TB,
msg := &lnwire.UpdateFulfillHTLC{
ID: r.Uint64(),
- ExtraData: createExtraData(t, r),
+ ExtraData: createValidTLVExtraData(t, r),
}
_, err := r.Read(msg.ChanID[:])
@@ -555,7 +556,7 @@ func newMsgUpdateFailHTLC(t testing.TB, r *rand.Rand) *lnwire.UpdateFailHTLC {
return msg
}
-func newMsgCommitSig(t testing.TB, r io.Reader) *lnwire.CommitSig {
+func newMsgCommitSig(t testing.TB, r *rand.Rand) *lnwire.CommitSig {
t.Helper()
msg := lnwire.NewCommitSig()
@@ -564,7 +565,7 @@ func newMsgCommitSig(t testing.TB, r io.Reader) *lnwire.CommitSig {
require.NoError(t, err, "unable to generate chan id")
msg.CommitSig = testNodeSig
- msg.ExtraData = createExtraData(t, r)
+ msg.ExtraData = createValidTLVExtraData(t, r)
msg.HtlcSigs = make([]lnwire.Sig, testNumSigs)
for i := 0; i < testNumSigs; i++ {
@@ -657,7 +658,7 @@ func newMsgChannelAnnouncement(t testing.TB,
NodeID2: randRawKey(t),
BitcoinKey1: randRawKey(t),
BitcoinKey2: randRawKey(t),
- ExtraOpaqueData: createExtraData(t, r),
+ ExtraOpaqueData: createValidTLVExtraData(t, r),
NodeSig1: testNodeSig,
NodeSig2: testNodeSig,
BitcoinSig1: testNodeSig,
@@ -686,7 +687,7 @@ func newMsgNodeAnnouncement(t testing.TB,
},
NodeID: randRawKey(t),
Addresses: randAddrs(t, r),
- ExtraOpaqueData: createExtraData(t, r),
+ ExtraOpaqueData: createValidTLVExtraData(t, r),
Signature: testNodeSig,
}
@@ -1064,3 +1065,37 @@ func createExtraData(t testing.TB, r io.Reader) []byte {
return extraData
}
+
+// createValidTLVExtraData creates a valid, canonically-ordered TLV stream
+// suitable for use as ExtraData in messages whose Encode methods re-parse
+// ExtraData as TLV. Unlike createExtraData which generates raw random bytes,
+// this produces properly encoded TLV records with monotonically increasing
+// types.
+func createValidTLVExtraData(t testing.TB, r *rand.Rand) []byte {
+ t.Helper()
+
+ // Generate between 1 and 4 TLV records with strictly increasing
+ // types and random values.
+ numRecords := r.Intn(4) + 1
+ records := make([]tlv.Record, 0, numRecords)
+ tlvType := tlv.Type(r.Intn(100) + 1)
+
+ for range numRecords {
+ val := make([]byte, r.Intn(20)+1)
+ _, err := r.Read(val)
+ require.NoError(t, err, "unable to generate tlv value")
+
+ records = append(
+ records, tlv.MakePrimitiveRecord(tlvType, &val),
+ )
+
+ // Ensure strictly increasing types.
+ tlvType += tlv.Type(r.Intn(100) + 1)
+ }
+
+ // Encode the records.
+ encoded, err := lnwire.EncodeRecords(records)
+ require.NoError(t, err, "unable to encode tlv records")
+
+ return encoded
+}
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.