github.com/decred/dcrlnd@v0.7.6/channeldb/migration/lnwire21/reply_channel_range_test.go (about) 1 package lnwire 2 3 import ( 4 "bytes" 5 "encoding/hex" 6 "reflect" 7 "testing" 8 9 "github.com/davecgh/go-spew/spew" 10 ) 11 12 // TestReplyChannelRangeUnsorted tests that decoding a ReplyChannelRange request 13 // that contains duplicate or unsorted ids returns an ErrUnsortedSIDs failure. 14 func TestReplyChannelRangeUnsorted(t *testing.T) { 15 for _, test := range unsortedSidTests { 16 test := test 17 t.Run(test.name, func(t *testing.T) { 18 req := &ReplyChannelRange{ 19 EncodingType: test.encType, 20 ShortChanIDs: test.sids, 21 noSort: true, 22 } 23 24 var b bytes.Buffer 25 err := req.Encode(&b, 0) 26 if err != nil { 27 t.Fatalf("unable to encode req: %v", err) 28 } 29 30 var req2 ReplyChannelRange 31 err = req2.Decode(bytes.NewReader(b.Bytes()), 0) 32 if _, ok := err.(ErrUnsortedSIDs); !ok { 33 t.Fatalf("expected ErrUnsortedSIDs, got: %T", 34 err) 35 } 36 }) 37 } 38 } 39 40 // TestReplyChannelRangeEmpty tests encoding and decoding a ReplyChannelRange 41 // that doesn't contain any channel results. 42 func TestReplyChannelRangeEmpty(t *testing.T) { 43 t.Parallel() 44 45 emptyChannelsTests := []struct { 46 name string 47 encType ShortChanIDEncoding 48 encodedHex string 49 }{ 50 { 51 name: "empty plain encoding", 52 encType: EncodingSortedPlain, 53 encodedHex: "000000000000000000000000000000000000000" + 54 "00000000000000000000000000000000100000002" + 55 "01000100", 56 }, 57 { 58 name: "empty zlib encoding", 59 encType: EncodingSortedZlib, 60 encodedHex: "00000000000000000000000000000000000000" + 61 "0000000000000000000000000000000001000000" + 62 "0201000101", 63 }, 64 } 65 66 for _, test := range emptyChannelsTests { 67 test := test 68 t.Run(test.name, func(t *testing.T) { 69 req := ReplyChannelRange{ 70 QueryChannelRange: QueryChannelRange{ 71 FirstBlockHeight: 1, 72 NumBlocks: 2, 73 }, 74 Complete: 1, 75 EncodingType: test.encType, 76 ShortChanIDs: nil, 77 } 78 79 // First decode the hex string in the test case into a 80 // new ReplyChannelRange message. It should be 81 // identical to the one created above. 82 var req2 ReplyChannelRange 83 b, _ := hex.DecodeString(test.encodedHex) 84 err := req2.Decode(bytes.NewReader(b), 0) 85 if err != nil { 86 t.Fatalf("unable to decode req: %v", err) 87 } 88 if !reflect.DeepEqual(req, req2) { 89 t.Fatalf("requests don't match: expected %v got %v", 90 spew.Sdump(req), spew.Sdump(req2)) 91 } 92 93 // Next, we go in the reverse direction: encode the 94 // request created above, and assert that it matches 95 // the raw byte encoding. 96 var b2 bytes.Buffer 97 err = req.Encode(&b2, 0) 98 if err != nil { 99 t.Fatalf("unable to encode req: %v", err) 100 } 101 if !bytes.Equal(b, b2.Bytes()) { 102 t.Fatalf("encoded requests don't match: expected %x got %x", 103 b, b2.Bytes()) 104 } 105 }) 106 } 107 }