reply_channel_range_test.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. package lnwire
  2. import (
  3. "bytes"
  4. "encoding/hex"
  5. "testing"
  6. "github.com/stretchr/testify/require"
  7. )
  8. // TestReplyChannelRangeUnsorted tests that decoding a ReplyChannelRange request
  9. // that contains duplicate or unsorted ids returns an ErrUnsortedSIDs failure.
  10. func TestReplyChannelRangeUnsorted(t *testing.T) {
  11. for _, test := range unsortedSidTests {
  12. test := test
  13. t.Run(test.name, func(t *testing.T) {
  14. req := &ReplyChannelRange{
  15. EncodingType: test.encType,
  16. ShortChanIDs: test.sids,
  17. noSort: true,
  18. }
  19. var b bytes.Buffer
  20. err := req.Encode(&b, 0)
  21. if err != nil {
  22. t.Fatalf("unable to encode req: %v", err)
  23. }
  24. var req2 ReplyChannelRange
  25. err = req2.Decode(bytes.NewReader(b.Bytes()), 0)
  26. if _, ok := err.(ErrUnsortedSIDs); !ok {
  27. t.Fatalf("expected ErrUnsortedSIDs, got: %v",
  28. err)
  29. }
  30. })
  31. }
  32. }
  33. // TestReplyChannelRangeEmpty tests encoding and decoding a ReplyChannelRange
  34. // that doesn't contain any channel results.
  35. func TestReplyChannelRangeEmpty(t *testing.T) {
  36. t.Parallel()
  37. emptyChannelsTests := []struct {
  38. name string
  39. encType QueryEncoding
  40. encodedHex string
  41. }{
  42. {
  43. name: "empty plain encoding",
  44. encType: EncodingSortedPlain,
  45. encodedHex: "000000000000000000000000000000000000000" +
  46. "00000000000000000000000000000000100000002" +
  47. "01000100",
  48. },
  49. {
  50. name: "empty zlib encoding",
  51. encType: EncodingSortedZlib,
  52. encodedHex: "00000000000000000000000000000000000000" +
  53. "0000000000000000000000000000000001000000" +
  54. "0201000101",
  55. },
  56. }
  57. for _, test := range emptyChannelsTests {
  58. test := test
  59. t.Run(test.name, func(t *testing.T) {
  60. req := ReplyChannelRange{
  61. FirstBlockHeight: 1,
  62. NumBlocks: 2,
  63. Complete: 1,
  64. EncodingType: test.encType,
  65. ShortChanIDs: nil,
  66. ExtraData: make([]byte, 0),
  67. }
  68. // First decode the hex string in the test case into a
  69. // new ReplyChannelRange message. It should be
  70. // identical to the one created above.
  71. req2 := NewReplyChannelRange()
  72. b, _ := hex.DecodeString(test.encodedHex)
  73. err := req2.Decode(bytes.NewReader(b), 0)
  74. require.NoError(t, err)
  75. require.Equal(t, req, *req2)
  76. // Next, we go in the reverse direction: encode the
  77. // request created above, and assert that it matches
  78. // the raw byte encoding.
  79. var b2 bytes.Buffer
  80. err = req.Encode(&b2, 0)
  81. require.NoError(t, err)
  82. require.Equal(t, b, b2.Bytes())
  83. })
  84. }
  85. }
  86. // TestReplyChannelRangeEncode tests that encoding a ReplyChannelRange message
  87. // results in the correct sorting of the SCIDs and Timestamps.
  88. func TestReplyChannelRangeEncode(t *testing.T) {
  89. t.Parallel()
  90. tests := []struct {
  91. name string
  92. scids []ShortChannelID
  93. timestamps Timestamps
  94. expError string
  95. expScids []ShortChannelID
  96. expTimestamps Timestamps
  97. }{
  98. {
  99. name: "scids only, sorted",
  100. scids: []ShortChannelID{
  101. {BlockHeight: 100},
  102. {BlockHeight: 200},
  103. {BlockHeight: 300},
  104. },
  105. expScids: []ShortChannelID{
  106. {BlockHeight: 100},
  107. {BlockHeight: 200},
  108. {BlockHeight: 300},
  109. },
  110. },
  111. {
  112. name: "scids only, unsorted",
  113. scids: []ShortChannelID{
  114. {BlockHeight: 300},
  115. {BlockHeight: 100},
  116. {BlockHeight: 200},
  117. },
  118. expScids: []ShortChannelID{
  119. {BlockHeight: 100},
  120. {BlockHeight: 200},
  121. {BlockHeight: 300},
  122. },
  123. },
  124. {
  125. name: "scids and timestamps, sorted",
  126. scids: []ShortChannelID{
  127. {BlockHeight: 100},
  128. {BlockHeight: 200},
  129. {BlockHeight: 300},
  130. },
  131. timestamps: Timestamps{
  132. {Timestamp1: 1, Timestamp2: 2},
  133. {Timestamp1: 3, Timestamp2: 4},
  134. {Timestamp1: 5, Timestamp2: 6},
  135. },
  136. expScids: []ShortChannelID{
  137. {BlockHeight: 100},
  138. {BlockHeight: 200},
  139. {BlockHeight: 300},
  140. },
  141. expTimestamps: Timestamps{
  142. {Timestamp1: 1, Timestamp2: 2},
  143. {Timestamp1: 3, Timestamp2: 4},
  144. {Timestamp1: 5, Timestamp2: 6},
  145. },
  146. },
  147. {
  148. name: "scids and timestamps, unsorted",
  149. scids: []ShortChannelID{
  150. {BlockHeight: 300},
  151. {BlockHeight: 100},
  152. {BlockHeight: 200},
  153. },
  154. timestamps: Timestamps{
  155. {Timestamp1: 5, Timestamp2: 6},
  156. {Timestamp1: 1, Timestamp2: 2},
  157. {Timestamp1: 3, Timestamp2: 4},
  158. },
  159. expScids: []ShortChannelID{
  160. {BlockHeight: 100},
  161. {BlockHeight: 200},
  162. {BlockHeight: 300},
  163. },
  164. expTimestamps: Timestamps{
  165. {Timestamp1: 1, Timestamp2: 2},
  166. {Timestamp1: 3, Timestamp2: 4},
  167. {Timestamp1: 5, Timestamp2: 6},
  168. },
  169. },
  170. {
  171. name: "scid and timestamp count does not match",
  172. scids: []ShortChannelID{
  173. {BlockHeight: 100},
  174. {BlockHeight: 200},
  175. {BlockHeight: 300},
  176. },
  177. timestamps: Timestamps{
  178. {Timestamp1: 1, Timestamp2: 2},
  179. {Timestamp1: 3, Timestamp2: 4},
  180. },
  181. expError: "got must provide a timestamp pair for " +
  182. "each of the given SCIDs",
  183. },
  184. {
  185. name: "duplicate scids",
  186. scids: []ShortChannelID{
  187. {BlockHeight: 100},
  188. {BlockHeight: 200},
  189. {BlockHeight: 200},
  190. },
  191. timestamps: Timestamps{
  192. {Timestamp1: 1, Timestamp2: 2},
  193. {Timestamp1: 3, Timestamp2: 4},
  194. {Timestamp1: 5, Timestamp2: 6},
  195. },
  196. expError: "scid list should not contain duplicates",
  197. },
  198. }
  199. for _, test := range tests {
  200. test := test
  201. t.Run(test.name, func(t *testing.T) {
  202. t.Parallel()
  203. replyMsg := &ReplyChannelRange{
  204. FirstBlockHeight: 1,
  205. NumBlocks: 2,
  206. Complete: 1,
  207. EncodingType: EncodingSortedPlain,
  208. ShortChanIDs: test.scids,
  209. Timestamps: test.timestamps,
  210. ExtraData: make([]byte, 0),
  211. }
  212. var buf bytes.Buffer
  213. _, err := WriteMessage(&buf, replyMsg, 0)
  214. if len(test.expError) != 0 {
  215. require.ErrorContains(t, err, test.expError)
  216. return
  217. }
  218. require.NoError(t, err)
  219. r := bytes.NewBuffer(buf.Bytes())
  220. msg, err := ReadMessage(r, 0)
  221. require.NoError(t, err)
  222. msg2, ok := msg.(*ReplyChannelRange)
  223. require.True(t, ok)
  224. require.Equal(t, test.expScids, msg2.ShortChanIDs)
  225. require.Equal(t, test.expTimestamps, msg2.Timestamps)
  226. })
  227. }
  228. }
  229. // TestReplyChannelRangeDecode tests the decoding of some ReplyChannelRange
  230. // test vectors.
  231. func TestReplyChannelRangeDecode(t *testing.T) {
  232. t.Parallel()
  233. tests := []struct {
  234. name string
  235. hex string
  236. expEncoding QueryEncoding
  237. expSCIDs []string
  238. expTimestamps Timestamps
  239. expError string
  240. }{
  241. {
  242. name: "plain encoding",
  243. hex: "01080f9188f13cb7b2c71f2a335e3a4fc328bf5beb4360" +
  244. "12afca590b1a11466e2206000b8a06000005dc01001" +
  245. "900000000000000008e0000000000003c6900000000" +
  246. "0045a6c4",
  247. expEncoding: EncodingSortedPlain,
  248. expSCIDs: []string{
  249. "0:0:142",
  250. "0:0:15465",
  251. "0:69:42692",
  252. },
  253. },
  254. {
  255. name: "zlib encoding",
  256. hex: "01080f9188f13cb7b2c71f2a335e3a4fc328bf5beb4360" +
  257. "12afca590b1a11466e2206000006400000006e010016" +
  258. "01789c636000833e08659309a65878be010010a9023a",
  259. expEncoding: EncodingSortedZlib,
  260. expSCIDs: []string{
  261. "0:0:142",
  262. "0:0:15465",
  263. "0:4:3318",
  264. },
  265. },
  266. {
  267. name: "plain encoding including timestamps",
  268. hex: "01080f9188f13cb7b2c71f2a335e3a4fc328bf5beb43601" +
  269. "2afca590b1a11466e22060001ddde000005dc0100190" +
  270. "0000000000000304300000000000778d600000000004" +
  271. "6e1c1011900000282c1000e77c5000778ad00490ab00" +
  272. "000b57800955bff031800000457000008ae00000d050" +
  273. "000115c000015b300001a0a",
  274. expEncoding: EncodingSortedPlain,
  275. expSCIDs: []string{
  276. "0:0:12355",
  277. "0:7:30934",
  278. "0:70:57793",
  279. },
  280. expTimestamps: Timestamps{
  281. {
  282. Timestamp1: 164545,
  283. Timestamp2: 948165,
  284. },
  285. {
  286. Timestamp1: 489645,
  287. Timestamp2: 4786864,
  288. },
  289. {
  290. Timestamp1: 46456,
  291. Timestamp2: 9788415,
  292. },
  293. },
  294. },
  295. {
  296. name: "unsupported encoding",
  297. hex: "01080f9188f13cb7b2c71f2a335e3a4fc328bf5beb" +
  298. "436012afca590b1a11466e22060001ddde000005dc01" +
  299. "001801789c63600001036730c55e710d4cbb3d3c0800" +
  300. "17c303b1012201789c63606a3ac8c0577e9481bd622d" +
  301. "8327d7060686ad150c53a3ff0300554707db03180000" +
  302. "0457000008ae00000d050000115c000015b300001a0a",
  303. expError: "unsupported encoding",
  304. },
  305. }
  306. for _, test := range tests {
  307. test := test
  308. t.Run(test.name, func(t *testing.T) {
  309. t.Parallel()
  310. b, err := hex.DecodeString(test.hex)
  311. require.NoError(t, err)
  312. r := bytes.NewBuffer(b)
  313. msg, err := ReadMessage(r, 0)
  314. if len(test.expError) != 0 {
  315. require.ErrorContains(t, err, test.expError)
  316. return
  317. }
  318. require.NoError(t, err)
  319. replyMsg, ok := msg.(*ReplyChannelRange)
  320. require.True(t, ok)
  321. require.Equal(
  322. t, test.expEncoding, replyMsg.EncodingType,
  323. )
  324. scids := make([]string, len(replyMsg.ShortChanIDs))
  325. for i, id := range replyMsg.ShortChanIDs {
  326. scids[i] = id.String()
  327. }
  328. require.Equal(t, scids, test.expSCIDs)
  329. require.Equal(
  330. t, test.expTimestamps, replyMsg.Timestamps,
  331. )
  332. })
  333. }
  334. }