query_short_chan_ids_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package lnwire
  2. import (
  3. "bytes"
  4. "testing"
  5. )
  6. type unsortedSidTest struct {
  7. name string
  8. encType QueryEncoding
  9. sids []ShortChannelID
  10. }
  11. var (
  12. unsortedSids = []ShortChannelID{
  13. NewShortChanIDFromInt(4),
  14. NewShortChanIDFromInt(3),
  15. }
  16. duplicateSids = []ShortChannelID{
  17. NewShortChanIDFromInt(3),
  18. NewShortChanIDFromInt(3),
  19. }
  20. unsortedSidTests = []unsortedSidTest{
  21. {
  22. name: "plain unsorted",
  23. encType: EncodingSortedPlain,
  24. sids: unsortedSids,
  25. },
  26. {
  27. name: "plain duplicate",
  28. encType: EncodingSortedPlain,
  29. sids: duplicateSids,
  30. },
  31. {
  32. name: "zlib unsorted",
  33. encType: EncodingSortedZlib,
  34. sids: unsortedSids,
  35. },
  36. {
  37. name: "zlib duplicate",
  38. encType: EncodingSortedZlib,
  39. sids: duplicateSids,
  40. },
  41. }
  42. )
  43. // TestQueryShortChanIDsUnsorted tests that decoding a QueryShortChanID request
  44. // that contains duplicate or unsorted ids returns an ErrUnsortedSIDs failure.
  45. func TestQueryShortChanIDsUnsorted(t *testing.T) {
  46. for _, test := range unsortedSidTests {
  47. test := test
  48. t.Run(test.name, func(t *testing.T) {
  49. req := &QueryShortChanIDs{
  50. EncodingType: test.encType,
  51. ShortChanIDs: test.sids,
  52. noSort: true,
  53. }
  54. var b bytes.Buffer
  55. err := req.Encode(&b, 0)
  56. if err != nil {
  57. t.Fatalf("unable to encode req: %v", err)
  58. }
  59. var req2 QueryShortChanIDs
  60. err = req2.Decode(bytes.NewReader(b.Bytes()), 0)
  61. if _, ok := err.(ErrUnsortedSIDs); !ok {
  62. t.Fatalf("expected ErrUnsortedSIDs, got: %T",
  63. err)
  64. }
  65. })
  66. }
  67. }
  68. // TestQueryShortChanIDsZero ensures that decoding of a list of short chan ids
  69. // still works as expected when the first element of the list is zero.
  70. func TestQueryShortChanIDsZero(t *testing.T) {
  71. testCases := []struct {
  72. name string
  73. encoding QueryEncoding
  74. }{
  75. {
  76. name: "plain",
  77. encoding: EncodingSortedPlain,
  78. }, {
  79. name: "zlib",
  80. encoding: EncodingSortedZlib,
  81. },
  82. }
  83. testSids := []ShortChannelID{
  84. NewShortChanIDFromInt(0),
  85. NewShortChanIDFromInt(10),
  86. }
  87. for _, test := range testCases {
  88. test := test
  89. t.Run(test.name, func(t *testing.T) {
  90. req := &QueryShortChanIDs{
  91. EncodingType: test.encoding,
  92. ShortChanIDs: testSids,
  93. noSort: true,
  94. }
  95. var b bytes.Buffer
  96. err := req.Encode(&b, 0)
  97. if err != nil {
  98. t.Fatalf("unable to encode req: %v", err)
  99. }
  100. var req2 QueryShortChanIDs
  101. err = req2.Decode(bytes.NewReader(b.Bytes()), 0)
  102. if err != nil {
  103. t.Fatalf("unexpected decoding error: %v", err)
  104. }
  105. })
  106. }
  107. }