github.com/decred/dcrlnd@v0.7.6/lnwire/query_short_chan_ids_test.go (about)

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