github.com/decred/dcrlnd@v0.7.6/lnwire/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: %v",
    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  				FirstBlockHeight: 1,
    71  				NumBlocks:        2,
    72  				Complete:         1,
    73  				EncodingType:     test.encType,
    74  				ShortChanIDs:     nil,
    75  				ExtraData:        make([]byte, 0),
    76  			}
    77  
    78  			// First decode the hex string in the test case into a
    79  			// new ReplyChannelRange message. It should be
    80  			// identical to the one created above.
    81  			var req2 ReplyChannelRange
    82  			b, _ := hex.DecodeString(test.encodedHex)
    83  			err := req2.Decode(bytes.NewReader(b), 0)
    84  			if err != nil {
    85  				t.Fatalf("unable to decode req: %v", err)
    86  			}
    87  			if !reflect.DeepEqual(req, req2) {
    88  				t.Fatalf("requests don't match: expected %v got %v",
    89  					spew.Sdump(req), spew.Sdump(req2))
    90  			}
    91  
    92  			// Next, we go in the reverse direction: encode the
    93  			// request created above, and assert that it matches
    94  			// the raw byte encoding.
    95  			var b2 bytes.Buffer
    96  			err = req.Encode(&b2, 0)
    97  			if err != nil {
    98  				t.Fatalf("unable to encode req: %v", err)
    99  			}
   100  			if !bytes.Equal(b, b2.Bytes()) {
   101  				t.Fatalf("encoded requests don't match: expected %x got %x",
   102  					b, b2.Bytes())
   103  			}
   104  		})
   105  	}
   106  }