Skip to content

Commit 304c8df

Browse files
authored
Merge pull request #9251 from morehouse/fuzz_lnwire
lnwire: add new fuzz targets
2 parents 4d184df + 4f7267e commit 304c8df

File tree

1 file changed

+131
-0
lines changed

1 file changed

+131
-0
lines changed

lnwire/fuzz_test.go

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,17 @@ func FuzzAnnounceSignatures(f *testing.F) {
114114
})
115115
}
116116

117+
func FuzzAnnounceSignatures2(f *testing.F) {
118+
f.Fuzz(func(t *testing.T, data []byte) {
119+
// Prefix with MsgAnnounceSignatures2.
120+
data = prefixWithMsgType(data, MsgAnnounceSignatures2)
121+
122+
// Pass the message into our general fuzz harness for wire
123+
// messages!
124+
harness(t, data)
125+
})
126+
}
127+
117128
func FuzzChannelAnnouncement(f *testing.F) {
118129
f.Fuzz(func(t *testing.T, data []byte) {
119130
// Prefix with MsgChannelAnnouncement.
@@ -125,6 +136,51 @@ func FuzzChannelAnnouncement(f *testing.F) {
125136
})
126137
}
127138

139+
func FuzzChannelAnnouncement2(f *testing.F) {
140+
f.Fuzz(func(t *testing.T, data []byte) {
141+
// Prefix with MsgChannelAnnouncement2.
142+
data = prefixWithMsgType(data, MsgChannelAnnouncement2)
143+
144+
// Because require.Equal considers nil maps and empty maps
145+
// to be non-equal, we must manually compare Features field
146+
// rather than using the harness.
147+
148+
if len(data) > MaxSliceLength {
149+
return
150+
}
151+
152+
r := bytes.NewReader(data)
153+
msg, err := ReadMessage(r, 0)
154+
if err != nil {
155+
return
156+
}
157+
158+
// We will serialize the message into a new bytes buffer.
159+
var b bytes.Buffer
160+
_, err = WriteMessage(&b, msg, 0)
161+
require.NoError(t, err)
162+
163+
// Deserialize the message from the serialized bytes buffer, and
164+
// then assert that the original message is equal to the newly
165+
// deserialized message.
166+
newMsg, err := ReadMessage(&b, 0)
167+
require.NoError(t, err)
168+
169+
require.IsType(t, &ChannelAnnouncement2{}, msg)
170+
first, _ := msg.(*ChannelAnnouncement2)
171+
require.IsType(t, &ChannelAnnouncement2{}, newMsg)
172+
second, _ := newMsg.(*ChannelAnnouncement2)
173+
174+
// We can't use require.Equal for Features, since we consider
175+
// the empty map and nil to be equivalent.
176+
require.True(t, first.Features.Val.Equals(&second.Features.Val))
177+
first.Features.Val = *NewRawFeatureVector()
178+
second.Features.Val = *NewRawFeatureVector()
179+
180+
require.Equal(t, first, second)
181+
})
182+
}
183+
128184
func FuzzChannelReestablish(f *testing.F) {
129185
f.Fuzz(func(t *testing.T, data []byte) {
130186
// Prefix with MsgChannelReestablish.
@@ -147,6 +203,17 @@ func FuzzChannelUpdate(f *testing.F) {
147203
})
148204
}
149205

206+
func FuzzChannelUpdate2(f *testing.F) {
207+
f.Fuzz(func(t *testing.T, data []byte) {
208+
// Prefix with MsgChannelUpdate2.
209+
data = prefixWithMsgType(data, MsgChannelUpdate2)
210+
211+
// Pass the message into our general fuzz harness for wire
212+
// messages!
213+
harness(t, data)
214+
})
215+
}
216+
150217
func FuzzClosingSigned(f *testing.F) {
151218
f.Fuzz(func(t *testing.T, data []byte) {
152219
// Prefix with MsgClosingSigned.
@@ -730,6 +797,37 @@ func FuzzConvertFixedSignature(f *testing.F) {
730797
})
731798
}
732799

800+
// FuzzConvertFixedSchnorrSignature tests that conversion of fixed 64-byte
801+
// Schnorr signatures to and from the btcec format does not panic or mutate the
802+
// signatures.
803+
func FuzzConvertFixedSchnorrSignature(f *testing.F) {
804+
f.Fuzz(func(t *testing.T, data []byte) {
805+
var sig Sig
806+
if len(data) > len(sig.bytes[:]) {
807+
return
808+
}
809+
copy(sig.bytes[:], data)
810+
sig.ForceSchnorr()
811+
812+
btcecSig, err := sig.ToSignature()
813+
if err != nil {
814+
return
815+
}
816+
817+
sig2, err := NewSigFromSignature(btcecSig)
818+
require.NoError(t, err, "failed to parse signature")
819+
820+
btcecSig2, err := sig2.ToSignature()
821+
require.NoError(
822+
t, err, "failed to reconvert signature to btcec format",
823+
)
824+
825+
btcecBytes := btcecSig.Serialize()
826+
btcecBytes2 := btcecSig2.Serialize()
827+
require.Equal(t, btcecBytes, btcecBytes2, "signature mismatch")
828+
})
829+
}
830+
733831
// prefixWithFailCode adds a failure code prefix to data.
734832
func prefixWithFailCode(data []byte, code FailCode) []byte {
735833
var codeBytes [2]byte
@@ -928,6 +1026,12 @@ func FuzzInvalidOnionPayload(f *testing.F) {
9281026
})
9291027
}
9301028

1029+
func FuzzFailInvalidBlinding(f *testing.F) {
1030+
f.Fuzz(func(t *testing.T, data []byte) {
1031+
onionFailureHarness(t, data, CodeInvalidBlinding)
1032+
})
1033+
}
1034+
9311035
func FuzzClosingSig(f *testing.F) {
9321036
f.Fuzz(func(t *testing.T, data []byte) {
9331037
// Prefix with ClosingSig.
@@ -949,3 +1053,30 @@ func FuzzClosingComplete(f *testing.F) {
9491053
harness(t, data)
9501054
})
9511055
}
1056+
1057+
// FuzzFee tests that decoding and re-encoding a Fee TLV does not mutate it.
1058+
func FuzzFee(f *testing.F) {
1059+
f.Fuzz(func(t *testing.T, data []byte) {
1060+
if len(data) > 8 {
1061+
return
1062+
}
1063+
1064+
var fee Fee
1065+
var buf [8]byte
1066+
r := bytes.NewReader(data)
1067+
1068+
if err := feeDecoder(r, &fee, &buf, 8); err != nil {
1069+
return
1070+
}
1071+
1072+
var b bytes.Buffer
1073+
require.NoError(t, feeEncoder(&b, &fee, &buf))
1074+
1075+
// Use bytes.Equal instead of require.Equal so that nil and
1076+
// empty slices are considered equal.
1077+
require.True(
1078+
t, bytes.Equal(data, b.Bytes()), "%v != %v", data,
1079+
b.Bytes(),
1080+
)
1081+
})
1082+
}

0 commit comments

Comments
 (0)