fuzz_test.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  1. package lnwire
  2. import (
  3. "bytes"
  4. "compress/zlib"
  5. "encoding/binary"
  6. "reflect"
  7. "testing"
  8. "github.com/stretchr/testify/require"
  9. )
  10. // prefixWithMsgType takes []byte and adds a wire protocol prefix
  11. // to make the []byte into an actual message to be used in fuzzing.
  12. func prefixWithMsgType(data []byte, prefix MessageType) []byte {
  13. var prefixBytes [2]byte
  14. binary.BigEndian.PutUint16(prefixBytes[:], uint16(prefix))
  15. data = append(prefixBytes[:], data...)
  16. return data
  17. }
  18. // harness performs the actual fuzz testing of the appropriate wire message.
  19. // This function will check that the passed-in message passes wire length
  20. // checks, is a valid message once deserialized, and passes a sequence of
  21. // serialization and deserialization checks.
  22. func harness(t *testing.T, data []byte) {
  23. t.Helper()
  24. // Create a reader with the byte array.
  25. r := bytes.NewReader(data)
  26. // Check that the created message is not greater than the maximum
  27. // message size.
  28. if len(data) > MaxSliceLength {
  29. return
  30. }
  31. msg, err := ReadMessage(r, 0)
  32. if err != nil {
  33. return
  34. }
  35. // We will serialize the message into a new bytes buffer.
  36. var b bytes.Buffer
  37. _, err = WriteMessage(&b, msg, 0)
  38. require.NoError(t, err)
  39. // Deserialize the message from the serialized bytes buffer, and then
  40. // assert that the original message is equal to the newly deserialized
  41. // message.
  42. newMsg, err := ReadMessage(&b, 0)
  43. require.NoError(t, err)
  44. require.Equal(t, msg, newMsg)
  45. }
  46. func FuzzAcceptChannel(f *testing.F) {
  47. f.Fuzz(func(t *testing.T, data []byte) {
  48. data = prefixWithMsgType(data, MsgAcceptChannel)
  49. // Create a reader with the byte array.
  50. r := bytes.NewReader(data)
  51. // Make sure byte array length (excluding 2 bytes for message
  52. // type) is less than max payload size for the wire message.
  53. payloadLen := uint32(len(data)) - 2
  54. if payloadLen > MaxMsgBody {
  55. return
  56. }
  57. msg, err := ReadMessage(r, 0)
  58. if err != nil {
  59. return
  60. }
  61. // We will serialize the message into a new bytes buffer.
  62. var b bytes.Buffer
  63. _, err = WriteMessage(&b, msg, 0)
  64. require.NoError(t, err)
  65. // Deserialize the message from the serialized bytes buffer, and
  66. // then assert that the original message is equal to the newly
  67. // deserialized message.
  68. newMsg, err := ReadMessage(&b, 0)
  69. require.NoError(t, err)
  70. require.IsType(t, &AcceptChannel{}, msg)
  71. first, _ := msg.(*AcceptChannel)
  72. require.IsType(t, &AcceptChannel{}, newMsg)
  73. second, _ := newMsg.(*AcceptChannel)
  74. // We can't use require.Equal for UpfrontShutdownScript, since
  75. // we consider the empty slice and nil to be equivalent.
  76. require.True(
  77. t, bytes.Equal(
  78. first.UpfrontShutdownScript,
  79. second.UpfrontShutdownScript,
  80. ),
  81. )
  82. first.UpfrontShutdownScript = nil
  83. second.UpfrontShutdownScript = nil
  84. require.Equal(t, first, second)
  85. })
  86. }
  87. func FuzzAnnounceSignatures(f *testing.F) {
  88. f.Fuzz(func(t *testing.T, data []byte) {
  89. // Prefix with MsgAnnounceSignatures.
  90. data = prefixWithMsgType(data, MsgAnnounceSignatures)
  91. // Pass the message into our general fuzz harness for wire
  92. // messages!
  93. harness(t, data)
  94. })
  95. }
  96. func FuzzChannelAnnouncement(f *testing.F) {
  97. f.Fuzz(func(t *testing.T, data []byte) {
  98. // Prefix with MsgChannelAnnouncement.
  99. data = prefixWithMsgType(data, MsgChannelAnnouncement)
  100. // Pass the message into our general fuzz harness for wire
  101. // messages!
  102. harness(t, data)
  103. })
  104. }
  105. func FuzzChannelReestablish(f *testing.F) {
  106. f.Fuzz(func(t *testing.T, data []byte) {
  107. // Prefix with MsgChannelReestablish.
  108. data = prefixWithMsgType(data, MsgChannelReestablish)
  109. // Pass the message into our general fuzz harness for wire
  110. // messages!
  111. harness(t, data)
  112. })
  113. }
  114. func FuzzChannelUpdate(f *testing.F) {
  115. f.Fuzz(func(t *testing.T, data []byte) {
  116. // Prefix with MsgChannelUpdate.
  117. data = prefixWithMsgType(data, MsgChannelUpdate)
  118. // Pass the message into our general fuzz harness for wire
  119. // messages!
  120. harness(t, data)
  121. })
  122. }
  123. func FuzzClosingSigned(f *testing.F) {
  124. f.Fuzz(func(t *testing.T, data []byte) {
  125. // Prefix with MsgClosingSigned.
  126. data = prefixWithMsgType(data, MsgClosingSigned)
  127. // Pass the message into our general fuzz harness for wire
  128. // messages!
  129. harness(t, data)
  130. })
  131. }
  132. func FuzzCommitSig(f *testing.F) {
  133. f.Fuzz(func(t *testing.T, data []byte) {
  134. // Prefix with MsgCommitSig.
  135. data = prefixWithMsgType(data, MsgCommitSig)
  136. // Pass the message into our general fuzz harness for wire
  137. // messages!
  138. harness(t, data)
  139. })
  140. }
  141. func FuzzError(f *testing.F) {
  142. f.Fuzz(func(t *testing.T, data []byte) {
  143. // Prefix with MsgError.
  144. data = prefixWithMsgType(data, MsgError)
  145. // Pass the message into our general fuzz harness for wire
  146. // messages!
  147. harness(t, data)
  148. })
  149. }
  150. func FuzzWarning(f *testing.F) {
  151. f.Fuzz(func(t *testing.T, data []byte) {
  152. // Prefix with MsgWarning.
  153. data = prefixWithMsgType(data, MsgWarning)
  154. // Pass the message into our general fuzz harness for wire
  155. // messages!
  156. harness(t, data)
  157. })
  158. }
  159. func FuzzStfu(f *testing.F) {
  160. f.Fuzz(func(t *testing.T, data []byte) {
  161. // Prefix with MsgStfu.
  162. data = prefixWithMsgType(data, MsgStfu)
  163. // Pass the message into our general fuzz harness for wire
  164. // messages.
  165. harness(t, data)
  166. })
  167. }
  168. func FuzzFundingCreated(f *testing.F) {
  169. f.Fuzz(func(t *testing.T, data []byte) {
  170. // Prefix with MsgFundingCreated.
  171. data = prefixWithMsgType(data, MsgFundingCreated)
  172. // Pass the message into our general fuzz harness for wire
  173. // messages!
  174. harness(t, data)
  175. })
  176. }
  177. func FuzzChannelReady(f *testing.F) {
  178. f.Fuzz(func(t *testing.T, data []byte) {
  179. // Prefix with MsgChannelReady.
  180. data = prefixWithMsgType(data, MsgChannelReady)
  181. // Pass the message into our general fuzz harness for wire
  182. // messages!
  183. harness(t, data)
  184. })
  185. }
  186. func FuzzFundingSigned(f *testing.F) {
  187. f.Fuzz(func(t *testing.T, data []byte) {
  188. // Prefix with MsgFundingSigned.
  189. data = prefixWithMsgType(data, MsgFundingSigned)
  190. // Pass the message into our general fuzz harness for wire
  191. // messages!
  192. harness(t, data)
  193. })
  194. }
  195. func FuzzGossipTimestampRange(f *testing.F) {
  196. f.Fuzz(func(t *testing.T, data []byte) {
  197. // Prefix with MsgGossipTimestampRange.
  198. data = prefixWithMsgType(data, MsgGossipTimestampRange)
  199. // Pass the message into our general fuzz harness for wire
  200. // messages!
  201. harness(t, data)
  202. })
  203. }
  204. func FuzzInit(f *testing.F) {
  205. f.Fuzz(func(t *testing.T, data []byte) {
  206. // Prefix with MsgInit.
  207. data = prefixWithMsgType(data, MsgInit)
  208. // Pass the message into our general fuzz harness for wire
  209. // messages!
  210. harness(t, data)
  211. })
  212. }
  213. func FuzzNodeAnnouncement(f *testing.F) {
  214. f.Fuzz(func(t *testing.T, data []byte) {
  215. // Prefix with MsgNodeAnnouncement.
  216. data = prefixWithMsgType(data, MsgNodeAnnouncement)
  217. // We have to do this here instead of in harness so that
  218. // reflect.DeepEqual isn't called. Address (de)serialization
  219. // messes up the fuzzing assertions.
  220. // Create a reader with the byte array.
  221. r := bytes.NewReader(data)
  222. // Make sure byte array length (excluding 2 bytes for message
  223. // type) is less than max payload size for the wire message.
  224. payloadLen := uint32(len(data)) - 2
  225. if payloadLen > MaxMsgBody {
  226. return
  227. }
  228. msg, err := ReadMessage(r, 0)
  229. if err != nil {
  230. return
  231. }
  232. // We will serialize the message into a new bytes buffer.
  233. var b bytes.Buffer
  234. _, err = WriteMessage(&b, msg, 0)
  235. require.NoError(t, err)
  236. // Deserialize the message from the serialized bytes buffer, and
  237. // then assert that the original message is equal to the newly
  238. // deserialized message.
  239. newMsg, err := ReadMessage(&b, 0)
  240. require.NoError(t, err)
  241. require.IsType(t, &NodeAnnouncement{}, msg)
  242. first, _ := msg.(*NodeAnnouncement)
  243. require.IsType(t, &NodeAnnouncement{}, newMsg)
  244. second, _ := newMsg.(*NodeAnnouncement)
  245. // We can't use require.Equal for Addresses, since the same IP
  246. // can be represented by different underlying bytes. Instead, we
  247. // compare the normalized string representation of each address.
  248. require.Equal(t, len(first.Addresses), len(second.Addresses))
  249. for i := range first.Addresses {
  250. require.Equal(
  251. t, first.Addresses[i].String(),
  252. second.Addresses[i].String(),
  253. )
  254. }
  255. first.Addresses = nil
  256. second.Addresses = nil
  257. require.Equal(t, first, second)
  258. })
  259. }
  260. func FuzzOpenChannel(f *testing.F) {
  261. f.Fuzz(func(t *testing.T, data []byte) {
  262. // Prefix with MsgOpenChannel.
  263. data = prefixWithMsgType(data, MsgOpenChannel)
  264. // We have to do this here instead of in harness so that
  265. // reflect.DeepEqual isn't called. Because of the
  266. // UpfrontShutdownScript encoding, the first message and second
  267. // message aren't deeply equal since the first has a nil slice
  268. // and the other has an empty slice.
  269. // Create a reader with the byte array.
  270. r := bytes.NewReader(data)
  271. // Make sure byte array length (excluding 2 bytes for message
  272. // type) is less than max payload size for the wire message.
  273. payloadLen := uint32(len(data)) - 2
  274. if payloadLen > MaxMsgBody {
  275. return
  276. }
  277. msg, err := ReadMessage(r, 0)
  278. if err != nil {
  279. return
  280. }
  281. // We will serialize the message into a new bytes buffer.
  282. var b bytes.Buffer
  283. _, err = WriteMessage(&b, msg, 0)
  284. require.NoError(t, err)
  285. // Deserialize the message from the serialized bytes buffer, and
  286. // then assert that the original message is equal to the newly
  287. // deserialized message.
  288. newMsg, err := ReadMessage(&b, 0)
  289. require.NoError(t, err)
  290. require.IsType(t, &OpenChannel{}, msg)
  291. first, _ := msg.(*OpenChannel)
  292. require.IsType(t, &OpenChannel{}, newMsg)
  293. second, _ := newMsg.(*OpenChannel)
  294. // We can't use require.Equal for UpfrontShutdownScript, since
  295. // we consider the empty slice and nil to be equivalent.
  296. require.True(
  297. t, bytes.Equal(
  298. first.UpfrontShutdownScript,
  299. second.UpfrontShutdownScript,
  300. ),
  301. )
  302. first.UpfrontShutdownScript = nil
  303. second.UpfrontShutdownScript = nil
  304. require.Equal(t, first, second)
  305. })
  306. }
  307. func FuzzPing(f *testing.F) {
  308. f.Fuzz(func(t *testing.T, data []byte) {
  309. // Prefix with MsgPing.
  310. data = prefixWithMsgType(data, MsgPing)
  311. // Pass the message into our general fuzz harness for wire
  312. // messages!
  313. harness(t, data)
  314. })
  315. }
  316. func FuzzPong(f *testing.F) {
  317. f.Fuzz(func(t *testing.T, data []byte) {
  318. // Prefix with MsgPong.
  319. data = prefixWithMsgType(data, MsgPong)
  320. // Pass the message into our general fuzz harness for wire
  321. // messages!
  322. harness(t, data)
  323. })
  324. }
  325. func FuzzQueryChannelRange(f *testing.F) {
  326. f.Fuzz(func(t *testing.T, data []byte) {
  327. // Prefix with MsgQueryChannelRange.
  328. data = prefixWithMsgType(data, MsgQueryChannelRange)
  329. // Pass the message into our general fuzz harness for wire
  330. // messages!
  331. harness(t, data)
  332. })
  333. }
  334. func FuzzZlibQueryShortChanIDs(f *testing.F) {
  335. f.Fuzz(func(t *testing.T, data []byte) {
  336. var buf bytes.Buffer
  337. zlibWriter := zlib.NewWriter(&buf)
  338. _, err := zlibWriter.Write(data)
  339. require.NoError(t, err) // Zlib bug?
  340. err = zlibWriter.Close()
  341. require.NoError(t, err) // Zlib bug?
  342. compressedPayload := buf.Bytes()
  343. chainhash := []byte("00000000000000000000000000000000")
  344. numBytesInBody := len(compressedPayload) + 1
  345. zlibByte := []byte("\x01")
  346. bodyBytes := make([]byte, 2)
  347. binary.BigEndian.PutUint16(bodyBytes, uint16(numBytesInBody))
  348. payload := chainhash
  349. payload = append(payload, bodyBytes...)
  350. payload = append(payload, zlibByte...)
  351. payload = append(payload, compressedPayload...)
  352. // Prefix with MsgQueryShortChanIDs.
  353. payload = prefixWithMsgType(payload, MsgQueryShortChanIDs)
  354. // Pass the message into our general fuzz harness for wire
  355. // messages!
  356. harness(t, payload)
  357. })
  358. }
  359. func FuzzQueryShortChanIDs(f *testing.F) {
  360. f.Fuzz(func(t *testing.T, data []byte) {
  361. // Prefix with MsgQueryShortChanIDs.
  362. data = prefixWithMsgType(data, MsgQueryShortChanIDs)
  363. // Pass the message into our general fuzz harness for wire
  364. // messages!
  365. harness(t, data)
  366. })
  367. }
  368. func FuzzZlibReplyChannelRange(f *testing.F) {
  369. f.Fuzz(func(t *testing.T, data []byte) {
  370. var buf bytes.Buffer
  371. zlibWriter := zlib.NewWriter(&buf)
  372. _, err := zlibWriter.Write(data)
  373. require.NoError(t, err) // Zlib bug?
  374. err = zlibWriter.Close()
  375. require.NoError(t, err) // Zlib bug?
  376. compressedPayload := buf.Bytes()
  377. // Initialize some []byte vars which will prefix our payload
  378. chainhash := []byte("00000000000000000000000000000000")
  379. firstBlockHeight := []byte("\x00\x00\x00\x00")
  380. numBlocks := []byte("\x00\x00\x00\x00")
  381. completeByte := []byte("\x00")
  382. numBytesInBody := len(compressedPayload) + 1
  383. zlibByte := []byte("\x01")
  384. bodyBytes := make([]byte, 2)
  385. binary.BigEndian.PutUint16(bodyBytes, uint16(numBytesInBody))
  386. payload := chainhash
  387. payload = append(payload, firstBlockHeight...)
  388. payload = append(payload, numBlocks...)
  389. payload = append(payload, completeByte...)
  390. payload = append(payload, bodyBytes...)
  391. payload = append(payload, zlibByte...)
  392. payload = append(payload, compressedPayload...)
  393. // Prefix with MsgReplyChannelRange.
  394. payload = prefixWithMsgType(payload, MsgReplyChannelRange)
  395. // Pass the message into our general fuzz harness for wire
  396. // messages!
  397. harness(t, payload)
  398. })
  399. }
  400. func FuzzReplyChannelRange(f *testing.F) {
  401. f.Fuzz(func(t *testing.T, data []byte) {
  402. // Prefix with MsgReplyChannelRange.
  403. data = prefixWithMsgType(data, MsgReplyChannelRange)
  404. // Because require.Equal considers nil slices and empty slices
  405. // to be non-equal, we must manually compare the Timestamps
  406. // field rather than using the harness.
  407. if len(data) > MaxSliceLength {
  408. return
  409. }
  410. r := bytes.NewReader(data)
  411. msg, err := ReadMessage(r, 0)
  412. if err != nil {
  413. return
  414. }
  415. // We will serialize the message into a new bytes buffer.
  416. var b bytes.Buffer
  417. _, err = WriteMessage(&b, msg, 0)
  418. require.NoError(t, err)
  419. // Deserialize the message from the serialized bytes buffer, and
  420. // then assert that the original message is equal to the newly
  421. // deserialized message.
  422. newMsg, err := ReadMessage(&b, 0)
  423. require.NoError(t, err)
  424. require.IsType(t, &ReplyChannelRange{}, msg)
  425. first, _ := msg.(*ReplyChannelRange)
  426. require.IsType(t, &ReplyChannelRange{}, newMsg)
  427. second, _ := newMsg.(*ReplyChannelRange)
  428. // We can't use require.Equal for Timestamps, since we consider
  429. // the empty slice and nil to be equivalent.
  430. require.Equal(t, len(first.Timestamps), len(second.Timestamps))
  431. for i, ts1 := range first.Timestamps {
  432. ts2 := second.Timestamps[i]
  433. require.Equal(t, ts1, ts2)
  434. }
  435. first.Timestamps = nil
  436. second.Timestamps = nil
  437. require.Equal(t, first, second)
  438. })
  439. }
  440. func FuzzReplyShortChanIDsEnd(f *testing.F) {
  441. f.Fuzz(func(t *testing.T, data []byte) {
  442. // Prefix with MsgReplyShortChanIDsEnd.
  443. data = prefixWithMsgType(data, MsgReplyShortChanIDsEnd)
  444. // Pass the message into our general fuzz harness for wire
  445. // messages!
  446. harness(t, data)
  447. })
  448. }
  449. func FuzzRevokeAndAck(f *testing.F) {
  450. f.Fuzz(func(t *testing.T, data []byte) {
  451. // Prefix with MsgRevokeAndAck.
  452. data = prefixWithMsgType(data, MsgRevokeAndAck)
  453. // Pass the message into our general fuzz harness for wire
  454. // messages!
  455. harness(t, data)
  456. })
  457. }
  458. func FuzzShutdown(f *testing.F) {
  459. f.Fuzz(func(t *testing.T, data []byte) {
  460. // Prefix with MsgShutdown.
  461. data = prefixWithMsgType(data, MsgShutdown)
  462. // Pass the message into our general fuzz harness for wire
  463. // messages!
  464. harness(t, data)
  465. })
  466. }
  467. func FuzzUpdateAddHTLC(f *testing.F) {
  468. f.Fuzz(func(t *testing.T, data []byte) {
  469. // Prefix with MsgUpdateAddHTLC.
  470. data = prefixWithMsgType(data, MsgUpdateAddHTLC)
  471. // Pass the message into our general fuzz harness for wire
  472. // messages!
  473. harness(t, data)
  474. })
  475. }
  476. func FuzzUpdateFailHTLC(f *testing.F) {
  477. f.Fuzz(func(t *testing.T, data []byte) {
  478. // Prefix with MsgUpdateFailHTLC.
  479. data = prefixWithMsgType(data, MsgUpdateFailHTLC)
  480. // Pass the message into our general fuzz harness for wire
  481. // messages!
  482. harness(t, data)
  483. })
  484. }
  485. func FuzzUpdateFailMalformedHTLC(f *testing.F) {
  486. f.Fuzz(func(t *testing.T, data []byte) {
  487. // Prefix with MsgUpdateFailMalformedHTLC.
  488. data = prefixWithMsgType(data, MsgUpdateFailMalformedHTLC)
  489. // Pass the message into our general fuzz harness for wire
  490. // messages!
  491. harness(t, data)
  492. })
  493. }
  494. func FuzzUpdateFee(f *testing.F) {
  495. f.Fuzz(func(t *testing.T, data []byte) {
  496. // Prefix with MsgUpdateFee.
  497. data = prefixWithMsgType(data, MsgUpdateFee)
  498. // Pass the message into our general fuzz harness for wire
  499. // messages!
  500. harness(t, data)
  501. })
  502. }
  503. func FuzzUpdateFulfillHTLC(f *testing.F) {
  504. f.Fuzz(func(t *testing.T, data []byte) {
  505. // Prefix with MsgUpdateFulFillHTLC.
  506. data = prefixWithMsgType(data, MsgUpdateFulfillHTLC)
  507. // Pass the message into our general fuzz harness for wire
  508. // messages!
  509. harness(t, data)
  510. })
  511. }
  512. func FuzzDynPropose(f *testing.F) {
  513. f.Fuzz(func(t *testing.T, data []byte) {
  514. // Prefix with DynPropose.
  515. data = prefixWithMsgType(data, MsgDynPropose)
  516. // Pass the message into our general fuzz harness for wire
  517. // messages!
  518. harness(t, data)
  519. })
  520. }
  521. func FuzzDynReject(f *testing.F) {
  522. f.Fuzz(func(t *testing.T, data []byte) {
  523. // Prefix with DynReject.
  524. data = prefixWithMsgType(data, MsgDynReject)
  525. // Pass the message into our general fuzz harness for wire
  526. // messages!
  527. harness(t, data)
  528. })
  529. }
  530. func FuzzDynAck(f *testing.F) {
  531. f.Fuzz(func(t *testing.T, data []byte) {
  532. // Prefix with DynReject.
  533. data = prefixWithMsgType(data, MsgDynAck)
  534. // Pass the message into our general fuzz harness for wire
  535. // messages!
  536. harness(t, data)
  537. })
  538. }
  539. func FuzzKickoffSig(f *testing.F) {
  540. f.Fuzz(func(t *testing.T, data []byte) {
  541. // Prefix with KickoffSig
  542. data = prefixWithMsgType(data, MsgKickoffSig)
  543. // Pass the message into our general fuzz harness for wire
  544. // messages!
  545. harness(t, data)
  546. })
  547. }
  548. func FuzzCustomMessage(f *testing.F) {
  549. f.Fuzz(func(t *testing.T, data []byte, customMessageType uint16) {
  550. if customMessageType < uint16(CustomTypeStart) {
  551. customMessageType += uint16(CustomTypeStart)
  552. }
  553. // Prefix with CustomMessage.
  554. data = prefixWithMsgType(data, MessageType(customMessageType))
  555. // Pass the message into our general fuzz harness for wire
  556. // messages!
  557. harness(t, data)
  558. })
  559. }
  560. // FuzzParseRawSignature tests that our DER-encoded signature parsing does not
  561. // panic for arbitrary inputs and that serializing and reparsing the signatures
  562. // does not mutate them.
  563. func FuzzParseRawSignature(f *testing.F) {
  564. f.Fuzz(func(t *testing.T, data []byte) {
  565. sig, err := NewSigFromECDSARawSignature(data)
  566. if err != nil {
  567. return
  568. }
  569. sig2, err := NewSigFromECDSARawSignature(sig.ToSignatureBytes())
  570. require.NoError(t, err, "failed to reparse signature")
  571. require.Equal(t, sig, sig2, "signature mismatch")
  572. })
  573. }
  574. // FuzzConvertFixedSignature tests that conversion of fixed 64-byte signatures
  575. // to DER-encoded signatures does not panic and that parsing and reconverting
  576. // the signatures does not mutate them.
  577. func FuzzConvertFixedSignature(f *testing.F) {
  578. f.Fuzz(func(t *testing.T, data []byte) {
  579. var sig Sig
  580. if len(data) > len(sig.bytes[:]) {
  581. return
  582. }
  583. copy(sig.bytes[:], data)
  584. derSig, err := sig.ToSignature()
  585. if err != nil {
  586. return
  587. }
  588. sig2, err := NewSigFromSignature(derSig)
  589. require.NoError(t, err, "failed to parse signature")
  590. derSig2, err := sig2.ToSignature()
  591. require.NoError(t, err, "failed to reconvert signature to DER")
  592. derBytes := derSig.Serialize()
  593. derBytes2 := derSig2.Serialize()
  594. require.Equal(t, derBytes, derBytes2, "signature mismatch")
  595. })
  596. }
  597. // prefixWithFailCode adds a failure code prefix to data.
  598. func prefixWithFailCode(data []byte, code FailCode) []byte {
  599. var codeBytes [2]byte
  600. binary.BigEndian.PutUint16(codeBytes[:], uint16(code))
  601. data = append(codeBytes[:], data...)
  602. return data
  603. }
  604. // equalFunc is a function used to determine whether two deserialized messages
  605. // are equivalent.
  606. type equalFunc func(x, y any) bool
  607. // onionFailureHarnessCustom performs the actual fuzz testing of the appropriate
  608. // onion failure message. This function will check that the passed-in message
  609. // passes wire length checks, is a valid message once deserialized, and passes a
  610. // sequence of serialization and deserialization checks.
  611. func onionFailureHarnessCustom(t *testing.T, data []byte, code FailCode,
  612. eq equalFunc) {
  613. data = prefixWithFailCode(data, code)
  614. // Don't waste time fuzzing messages larger than we'll ever accept.
  615. if len(data) > MaxSliceLength {
  616. return
  617. }
  618. // First check whether the failure message can be decoded.
  619. r := bytes.NewReader(data)
  620. msg, err := DecodeFailureMessage(r, 0)
  621. if err != nil {
  622. return
  623. }
  624. // We now have a valid decoded message. Verify that encoding and
  625. // decoding the message does not mutate it.
  626. var b bytes.Buffer
  627. err = EncodeFailureMessage(&b, msg, 0)
  628. require.NoError(t, err, "failed to encode failure message")
  629. newMsg, err := DecodeFailureMessage(&b, 0)
  630. require.NoError(t, err, "failed to decode serialized failure message")
  631. require.True(
  632. t, eq(msg, newMsg),
  633. "original message and deserialized message are not equal: "+
  634. "%v != %v",
  635. msg, newMsg,
  636. )
  637. // Now verify that encoding/decoding full packets works as expected.
  638. var pktBuf bytes.Buffer
  639. if err := EncodeFailure(&pktBuf, msg, 0); err != nil {
  640. // EncodeFailure returns an error if the encoded message would
  641. // exceed FailureMessageLength bytes, as LND always encodes
  642. // fixed-size packets for privacy. But it is valid to decode
  643. // messages longer than this, so we should not report an error
  644. // if the original message was longer.
  645. //
  646. // We add 2 to the length of the original message since it may
  647. // have omitted a channel_update type prefix of 2 bytes. When
  648. // we re-encode such a message, we will add the 2-byte prefix
  649. // as prescribed by the spec.
  650. if len(data)+2 > FailureMessageLength {
  651. return
  652. }
  653. t.Fatalf("failed to encode failure packet: %v", err)
  654. }
  655. // We should use FailureMessageLength sized packets plus 2 bytes to
  656. // encode the message length and 2 bytes to encode the padding length,
  657. // as recommended by the spec.
  658. require.Equal(
  659. t, pktBuf.Len(), FailureMessageLength+4,
  660. "wrong failure message length",
  661. )
  662. pktMsg, err := DecodeFailure(&pktBuf, 0)
  663. require.NoError(t, err, "failed to decode failure packet")
  664. require.True(
  665. t, eq(msg, pktMsg),
  666. "original message and decoded packet message are not equal: "+
  667. "%v != %v",
  668. msg, pktMsg,
  669. )
  670. }
  671. func onionFailureHarness(t *testing.T, data []byte, code FailCode) {
  672. t.Helper()
  673. onionFailureHarnessCustom(t, data, code, reflect.DeepEqual)
  674. }
  675. func FuzzFailIncorrectDetails(f *testing.F) {
  676. f.Fuzz(func(t *testing.T, data []byte) {
  677. // Since FailIncorrectDetails.Decode can leave extraOpaqueData
  678. // as nil while FailIncorrectDetails.Encode writes an empty
  679. // slice, we need to use a custom equality function.
  680. eq := func(x, y any) bool {
  681. msg1, ok := x.(*FailIncorrectDetails)
  682. require.True(
  683. t, ok, "msg1 was not FailIncorrectDetails",
  684. )
  685. msg2, ok := y.(*FailIncorrectDetails)
  686. require.True(
  687. t, ok, "msg2 was not FailIncorrectDetails",
  688. )
  689. return msg1.amount == msg2.amount &&
  690. msg1.height == msg2.height &&
  691. bytes.Equal(
  692. msg1.extraOpaqueData,
  693. msg2.extraOpaqueData,
  694. )
  695. }
  696. onionFailureHarnessCustom(
  697. t, data, CodeIncorrectOrUnknownPaymentDetails, eq,
  698. )
  699. })
  700. }
  701. func FuzzFailInvalidOnionVersion(f *testing.F) {
  702. f.Fuzz(func(t *testing.T, data []byte) {
  703. onionFailureHarness(t, data, CodeInvalidOnionVersion)
  704. })
  705. }
  706. func FuzzFailInvalidOnionHmac(f *testing.F) {
  707. f.Fuzz(func(t *testing.T, data []byte) {
  708. onionFailureHarness(t, data, CodeInvalidOnionHmac)
  709. })
  710. }
  711. func FuzzFailInvalidOnionKey(f *testing.F) {
  712. f.Fuzz(func(t *testing.T, data []byte) {
  713. onionFailureHarness(t, data, CodeInvalidOnionKey)
  714. })
  715. }
  716. func FuzzFailTemporaryChannelFailure(f *testing.F) {
  717. f.Fuzz(func(t *testing.T, data []byte) {
  718. onionFailureHarness(t, data, CodeTemporaryChannelFailure)
  719. })
  720. }
  721. func FuzzFailAmountBelowMinimum(f *testing.F) {
  722. f.Fuzz(func(t *testing.T, data []byte) {
  723. onionFailureHarness(t, data, CodeAmountBelowMinimum)
  724. })
  725. }
  726. func FuzzFailFeeInsufficient(f *testing.F) {
  727. f.Fuzz(func(t *testing.T, data []byte) {
  728. onionFailureHarness(t, data, CodeFeeInsufficient)
  729. })
  730. }
  731. func FuzzFailIncorrectCltvExpiry(f *testing.F) {
  732. f.Fuzz(func(t *testing.T, data []byte) {
  733. onionFailureHarness(t, data, CodeIncorrectCltvExpiry)
  734. })
  735. }
  736. func FuzzFailExpiryTooSoon(f *testing.F) {
  737. f.Fuzz(func(t *testing.T, data []byte) {
  738. onionFailureHarness(t, data, CodeExpiryTooSoon)
  739. })
  740. }
  741. func FuzzFailChannelDisabled(f *testing.F) {
  742. f.Fuzz(func(t *testing.T, data []byte) {
  743. onionFailureHarness(t, data, CodeChannelDisabled)
  744. })
  745. }
  746. func FuzzFailFinalIncorrectCltvExpiry(f *testing.F) {
  747. f.Fuzz(func(t *testing.T, data []byte) {
  748. onionFailureHarness(t, data, CodeFinalIncorrectCltvExpiry)
  749. })
  750. }
  751. func FuzzFailFinalIncorrectHtlcAmount(f *testing.F) {
  752. f.Fuzz(func(t *testing.T, data []byte) {
  753. onionFailureHarness(t, data, CodeFinalIncorrectHtlcAmount)
  754. })
  755. }
  756. func FuzzInvalidOnionPayload(f *testing.F) {
  757. f.Fuzz(func(t *testing.T, data []byte) {
  758. onionFailureHarness(t, data, CodeInvalidOnionPayload)
  759. })
  760. }
  761. func FuzzClosingSig(f *testing.F) {
  762. f.Fuzz(func(t *testing.T, data []byte) {
  763. // Prefix with ClosingSig.
  764. data = prefixWithMsgType(data, MsgClosingSig)
  765. // Pass the message into our general fuzz harness for wire
  766. // messages!
  767. harness(t, data)
  768. })
  769. }
  770. func FuzzClosingComplete(f *testing.F) {
  771. f.Fuzz(func(t *testing.T, data []byte) {
  772. // Prefix with ClosingComplete.
  773. data = prefixWithMsgType(data, MsgClosingComplete)
  774. // Pass the message into our general fuzz harness for wire
  775. // messages!
  776. harness(t, data)
  777. })
  778. }