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

     1  package lnwire
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/base32"
     6  	"image/color"
     7  	"math"
     8  	"net"
     9  	"testing"
    10  
    11  	"github.com/decred/dcrd/chaincfg/chainhash"
    12  	"github.com/decred/dcrd/dcrutil/v4"
    13  	"github.com/decred/dcrd/wire"
    14  	"github.com/decred/dcrlnd/tor"
    15  	"github.com/stretchr/testify/require"
    16  )
    17  
    18  func TestWriteBytes(t *testing.T) {
    19  	buf := new(bytes.Buffer)
    20  	data := []byte{1, 2, 3}
    21  
    22  	err := WriteBytes(buf, data)
    23  
    24  	require.NoError(t, err)
    25  	require.Equal(t, data, buf.Bytes())
    26  }
    27  
    28  func TestWriteUint8(t *testing.T) {
    29  	buf := new(bytes.Buffer)
    30  	data := uint8(1)
    31  	expectedBytes := []byte{1}
    32  
    33  	err := WriteUint8(buf, data)
    34  
    35  	require.NoError(t, err)
    36  	require.Equal(t, expectedBytes, buf.Bytes())
    37  }
    38  
    39  func TestWriteUint16(t *testing.T) {
    40  	buf := new(bytes.Buffer)
    41  	data := uint16(1)
    42  	expectedBytes := []byte{0, 1}
    43  
    44  	err := WriteUint16(buf, data)
    45  
    46  	require.NoError(t, err)
    47  	require.Equal(t, expectedBytes, buf.Bytes())
    48  }
    49  
    50  func TestWriteUint32(t *testing.T) {
    51  	buf := new(bytes.Buffer)
    52  	data := uint32(1)
    53  	expectedBytes := []byte{0, 0, 0, 1}
    54  
    55  	err := WriteUint32(buf, data)
    56  
    57  	require.NoError(t, err)
    58  	require.Equal(t, expectedBytes, buf.Bytes())
    59  }
    60  
    61  func TestWriteUint64(t *testing.T) {
    62  	buf := new(bytes.Buffer)
    63  	data := uint64(1)
    64  	expectedBytes := []byte{0, 0, 0, 0, 0, 0, 0, 1}
    65  
    66  	err := WriteUint64(buf, data)
    67  
    68  	require.NoError(t, err)
    69  	require.Equal(t, expectedBytes, buf.Bytes())
    70  }
    71  
    72  func TestWriteSatoshi(t *testing.T) {
    73  	buf := new(bytes.Buffer)
    74  	data := dcrutil.Amount(1)
    75  	expectedBytes := []byte{0, 0, 0, 0, 0, 0, 0, 1}
    76  
    77  	err := WriteSatoshi(buf, data)
    78  
    79  	require.NoError(t, err)
    80  	require.Equal(t, expectedBytes, buf.Bytes())
    81  }
    82  
    83  func TestWriteMilliAtom(t *testing.T) {
    84  	buf := new(bytes.Buffer)
    85  	data := MilliAtom(1)
    86  	expectedBytes := []byte{0, 0, 0, 0, 0, 0, 0, 1}
    87  
    88  	err := WriteMilliAtom(buf, data)
    89  
    90  	require.NoError(t, err)
    91  	require.Equal(t, expectedBytes, buf.Bytes())
    92  }
    93  
    94  func TestWritePublicKey(t *testing.T) {
    95  	buf := new(bytes.Buffer)
    96  
    97  	// Check that when nil pubkey is used, an error will return.
    98  	err := WritePublicKey(buf, nil)
    99  	require.Equal(t, ErrNilPublicKey, err)
   100  
   101  	pub, err := randPubKey()
   102  	require.NoError(t, err)
   103  	expectedBytes := pub.SerializeCompressed()
   104  
   105  	err = WritePublicKey(buf, pub)
   106  
   107  	require.NoError(t, err)
   108  	require.Equal(t, expectedBytes, buf.Bytes())
   109  }
   110  
   111  func TestWriteChannelID(t *testing.T) {
   112  	buf := new(bytes.Buffer)
   113  	data := ChannelID{1}
   114  	expectedBytes := [32]byte{1}
   115  
   116  	err := WriteChannelID(buf, data)
   117  
   118  	require.NoError(t, err)
   119  	require.Equal(t, expectedBytes[:], buf.Bytes())
   120  }
   121  
   122  func TestWriteNodeAlias(t *testing.T) {
   123  	buf := new(bytes.Buffer)
   124  	data := NodeAlias{1}
   125  	expectedBytes := [32]byte{1}
   126  
   127  	err := WriteNodeAlias(buf, data)
   128  
   129  	require.NoError(t, err)
   130  	require.Equal(t, expectedBytes[:], buf.Bytes())
   131  }
   132  
   133  func TestWriteShortChannelID(t *testing.T) {
   134  	buf := new(bytes.Buffer)
   135  	data := ShortChannelID{BlockHeight: 1, TxIndex: 2, TxPosition: 3}
   136  	expectedBytes := []byte{
   137  		0, 0, 1, // First three bytes encodes BlockHeight.
   138  		0, 0, 2, // Second three bytes encodes TxIndex.
   139  		0, 3, // Final two bytes encodes TxPosition.
   140  	}
   141  
   142  	err := WriteShortChannelID(buf, data)
   143  
   144  	require.NoError(t, err)
   145  	require.Equal(t, expectedBytes, buf.Bytes())
   146  }
   147  
   148  func TestWriteSig(t *testing.T) {
   149  	buf := new(bytes.Buffer)
   150  	data := Sig{1, 2, 3}
   151  	expectedBytes := [64]byte{1, 2, 3}
   152  
   153  	err := WriteSig(buf, data)
   154  
   155  	require.NoError(t, err)
   156  	require.Equal(t, expectedBytes[:], buf.Bytes())
   157  }
   158  
   159  func TestWriteSigs(t *testing.T) {
   160  	buf := new(bytes.Buffer)
   161  	sig1, sig2, sig3 := Sig{1}, Sig{2}, Sig{3}
   162  	data := []Sig{sig1, sig2, sig3}
   163  
   164  	// First two bytes encode the length of the slice.
   165  	expectedBytes := []byte{0, 3}
   166  	expectedBytes = append(expectedBytes, sig1[:]...)
   167  	expectedBytes = append(expectedBytes, sig2[:]...)
   168  	expectedBytes = append(expectedBytes, sig3[:]...)
   169  
   170  	err := WriteSigs(buf, data)
   171  
   172  	require.NoError(t, err)
   173  	require.Equal(t, expectedBytes, buf.Bytes())
   174  }
   175  
   176  func TestWriteFailCode(t *testing.T) {
   177  	buf := new(bytes.Buffer)
   178  	data := FailCode(1)
   179  	expectedBytes := []byte{0, 1}
   180  
   181  	err := WriteFailCode(buf, data)
   182  
   183  	require.NoError(t, err)
   184  	require.Equal(t, expectedBytes, buf.Bytes())
   185  }
   186  
   187  // TODO(yy): expand the test to cover more encoding scenarios.
   188  func TestWriteRawFeatureVector(t *testing.T) {
   189  	buf := new(bytes.Buffer)
   190  
   191  	// Check that when nil feature is used, an error will return.
   192  	err := WriteRawFeatureVector(buf, nil)
   193  	require.Equal(t, ErrNilFeatureVector, err)
   194  
   195  	// Create a raw feature vector.
   196  	feature := &RawFeatureVector{
   197  		features: map[FeatureBit]struct{}{
   198  			InitialRoutingSync: {}, // FeatureBit 3.
   199  		},
   200  	}
   201  	expectedBytes := []byte{
   202  		0, 1, // First two bytes encode the length.
   203  		8, // Last byte encodes the feature bit (1 << 3).
   204  	}
   205  
   206  	err = WriteRawFeatureVector(buf, feature)
   207  	require.NoError(t, err)
   208  	require.Equal(t, expectedBytes, buf.Bytes())
   209  }
   210  
   211  func TestWriteColorRGBA(t *testing.T) {
   212  	buf := new(bytes.Buffer)
   213  	data := color.RGBA{R: 1, G: 2, B: 3}
   214  	expectedBytes := []byte{1, 2, 3}
   215  
   216  	err := WriteColorRGBA(buf, data)
   217  
   218  	require.NoError(t, err)
   219  	require.Equal(t, expectedBytes, buf.Bytes())
   220  }
   221  
   222  func TestWriteShortChanIDEncoding(t *testing.T) {
   223  	buf := new(bytes.Buffer)
   224  	data := ShortChanIDEncoding(1)
   225  	expectedBytes := []byte{1}
   226  
   227  	err := WriteShortChanIDEncoding(buf, data)
   228  
   229  	require.NoError(t, err)
   230  	require.Equal(t, expectedBytes, buf.Bytes())
   231  }
   232  
   233  func TestWriteFundingFlag(t *testing.T) {
   234  	buf := new(bytes.Buffer)
   235  	data := FundingFlag(1)
   236  	expectedBytes := []byte{1}
   237  
   238  	err := WriteFundingFlag(buf, data)
   239  
   240  	require.NoError(t, err)
   241  	require.Equal(t, expectedBytes, buf.Bytes())
   242  }
   243  
   244  func TestWriteChanUpdateMsgFlags(t *testing.T) {
   245  	buf := new(bytes.Buffer)
   246  	data := ChanUpdateMsgFlags(1)
   247  	expectedBytes := []byte{1}
   248  
   249  	err := WriteChanUpdateMsgFlags(buf, data)
   250  
   251  	require.NoError(t, err)
   252  	require.Equal(t, expectedBytes, buf.Bytes())
   253  }
   254  
   255  func TestWriteChanUpdateChanFlags(t *testing.T) {
   256  	buf := new(bytes.Buffer)
   257  	data := ChanUpdateChanFlags(1)
   258  	expectedBytes := []byte{1}
   259  
   260  	err := WriteChanUpdateChanFlags(buf, data)
   261  
   262  	require.NoError(t, err)
   263  	require.Equal(t, expectedBytes, buf.Bytes())
   264  }
   265  
   266  func TestWriteDeliveryAddress(t *testing.T) {
   267  	buf := new(bytes.Buffer)
   268  	data := DeliveryAddress{1, 1, 1}
   269  	expectedBytes := []byte{
   270  		0, 3, // First two bytes encode the length.
   271  		1, 1, 1, // The actual data.
   272  	}
   273  
   274  	err := WriteDeliveryAddress(buf, data)
   275  
   276  	require.NoError(t, err)
   277  	require.Equal(t, expectedBytes, buf.Bytes())
   278  }
   279  
   280  func TestWritePingPayload(t *testing.T) {
   281  	buf := new(bytes.Buffer)
   282  	data := PingPayload{1, 1, 1}
   283  	expectedBytes := []byte{
   284  		0, 3, // First two bytes encode the length.
   285  		1, 1, 1, // The actual data.
   286  	}
   287  
   288  	err := WritePingPayload(buf, data)
   289  
   290  	require.NoError(t, err)
   291  	require.Equal(t, expectedBytes, buf.Bytes())
   292  }
   293  
   294  func TestWritePongPayload(t *testing.T) {
   295  	buf := new(bytes.Buffer)
   296  	data := PongPayload{1, 1, 1}
   297  	expectedBytes := []byte{
   298  		0, 3, // First two bytes encode the length.
   299  		1, 1, 1, // The actual data.
   300  	}
   301  
   302  	err := WritePongPayload(buf, data)
   303  
   304  	require.NoError(t, err)
   305  	require.Equal(t, expectedBytes, buf.Bytes())
   306  }
   307  
   308  func TestWriteErrorData(t *testing.T) {
   309  	buf := new(bytes.Buffer)
   310  	data := ErrorData{1, 1, 1}
   311  	expectedBytes := []byte{
   312  		0, 3, // First two bytes encode the length.
   313  		1, 1, 1, // The actual data.
   314  	}
   315  
   316  	err := WriteErrorData(buf, data)
   317  
   318  	require.NoError(t, err)
   319  	require.Equal(t, expectedBytes, buf.Bytes())
   320  }
   321  
   322  func TestWriteOpaqueReason(t *testing.T) {
   323  	buf := new(bytes.Buffer)
   324  	data := OpaqueReason{1, 1, 1}
   325  	expectedBytes := []byte{
   326  		0, 3, // First two bytes encode the length.
   327  		1, 1, 1, // The actual data.
   328  	}
   329  
   330  	err := WriteOpaqueReason(buf, data)
   331  
   332  	require.NoError(t, err)
   333  	require.Equal(t, expectedBytes, buf.Bytes())
   334  }
   335  
   336  func TestWriteBool(t *testing.T) {
   337  	buf := new(bytes.Buffer)
   338  
   339  	// Test write true.
   340  	data := true
   341  	expectedBytes := []byte{1}
   342  
   343  	err := WriteBool(buf, data)
   344  
   345  	require.NoError(t, err)
   346  	require.Equal(t, expectedBytes, buf.Bytes())
   347  
   348  	// Test write false.
   349  	data = false
   350  	expectedBytes = append(expectedBytes, 0)
   351  
   352  	err = WriteBool(buf, data)
   353  
   354  	require.NoError(t, err)
   355  	require.Equal(t, expectedBytes, buf.Bytes())
   356  }
   357  
   358  func TestWritePkScript(t *testing.T) {
   359  	buf := new(bytes.Buffer)
   360  
   361  	// Write a very long script to check the error is returned as expected.
   362  	script := PkScript{}
   363  	zeros := [35]byte{}
   364  	script = append(script, zeros[:]...)
   365  	err := WritePkScript(buf, script)
   366  	require.Equal(t, ErrPkScriptTooLong, err)
   367  
   368  	data := PkScript{1, 1, 1}
   369  	expectedBytes := []byte{
   370  		3,       // First byte encodes the length.
   371  		1, 1, 1, // The actual data.
   372  	}
   373  
   374  	err = WritePkScript(buf, data)
   375  
   376  	require.NoError(t, err)
   377  	require.Equal(t, expectedBytes, buf.Bytes())
   378  }
   379  
   380  func TestWriteOutPoint(t *testing.T) {
   381  	buf := new(bytes.Buffer)
   382  
   383  	// Create an outpoint with very large index to check the error is
   384  	// returned as expected.
   385  	outpointWrong := wire.OutPoint{Index: math.MaxUint16 + 1}
   386  	err := WriteOutPoint(buf, outpointWrong)
   387  	require.Equal(t, ErrOutpointIndexTooBig(outpointWrong.Index), err)
   388  
   389  	// Now check the normal write succeeds.
   390  	hash := chainhash.Hash{1}
   391  	data := wire.OutPoint{Index: 2, Hash: hash}
   392  	expectedBytes := []byte{}
   393  	expectedBytes = append(expectedBytes, hash[:]...)
   394  	expectedBytes = append(expectedBytes, []byte{0, 2}...)
   395  
   396  	err = WriteOutPoint(buf, data)
   397  
   398  	require.NoError(t, err)
   399  	require.Equal(t, expectedBytes, buf.Bytes())
   400  }
   401  
   402  func TestWriteTCPAddr(t *testing.T) {
   403  	buf := new(bytes.Buffer)
   404  
   405  	testCases := []struct {
   406  		name string
   407  		addr *net.TCPAddr
   408  
   409  		expectedErr   error
   410  		expectedBytes []byte
   411  	}{
   412  		{
   413  			// Check that the error is returned when nil address is
   414  			// used.
   415  			name:          "nil address err",
   416  			addr:          nil,
   417  			expectedErr:   ErrNilTCPAddress,
   418  			expectedBytes: nil,
   419  		},
   420  		{
   421  			// Check write IPv4.
   422  			name: "write ipv4",
   423  			addr: &net.TCPAddr{
   424  				IP:   net.IP{127, 0, 0, 1},
   425  				Port: 8080,
   426  			},
   427  			expectedErr: nil,
   428  			expectedBytes: []byte{
   429  				0x1,                 // The addressType.
   430  				0x7f, 0x0, 0x0, 0x1, // The IP.
   431  				0x1f, 0x90, // The port (31 * 256 + 144).
   432  			},
   433  		},
   434  		{
   435  			// Check write IPv6.
   436  			name: "write ipv6",
   437  			addr: &net.TCPAddr{
   438  				IP: net.IP{
   439  					1, 1, 1, 1, 1, 1, 1, 1,
   440  					1, 1, 1, 1, 1, 1, 1, 1,
   441  				},
   442  				Port: 8080,
   443  			},
   444  			expectedErr: nil,
   445  			expectedBytes: []byte{
   446  				0x2, // The addressType.
   447  				// The IP.
   448  				0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1,
   449  				0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1,
   450  				0x1f, 0x90, // The port (31 * 256 + 144).
   451  			},
   452  		},
   453  	}
   454  
   455  	for _, tc := range testCases {
   456  		tc := tc
   457  		t.Run(tc.name, func(t *testing.T) {
   458  			oldLen := buf.Len()
   459  
   460  			err := WriteTCPAddr(buf, tc.addr)
   461  			require.Equal(t, tc.expectedErr, err)
   462  
   463  			bytesWritten := buf.Bytes()[oldLen:buf.Len()]
   464  			require.Equal(t, tc.expectedBytes, bytesWritten)
   465  		})
   466  	}
   467  }
   468  
   469  func TestWriteOnionAddr(t *testing.T) {
   470  	buf := new(bytes.Buffer)
   471  
   472  	testCases := []struct {
   473  		name string
   474  		addr *tor.OnionAddr
   475  
   476  		expectedErr   error
   477  		expectedBytes []byte
   478  	}{
   479  		{
   480  			// Check that the error is returned when nil address is
   481  			// used.
   482  			name:          "nil address err",
   483  			addr:          nil,
   484  			expectedErr:   ErrNilOnionAddress,
   485  			expectedBytes: nil,
   486  		},
   487  		{
   488  			// Check the error is returned when an invalid onion
   489  			// address is used.
   490  			name:          "wrong onion service length",
   491  			addr:          &tor.OnionAddr{OnionService: "wrong"},
   492  			expectedErr:   ErrUnknownServiceLength,
   493  			expectedBytes: nil,
   494  		},
   495  		{
   496  			// Check when the address has invalid base32 encoding,
   497  			// the error is returned.
   498  			name: "invalid base32 encoding",
   499  			addr: &tor.OnionAddr{
   500  				OnionService: "1234567890123456.onion",
   501  			},
   502  			expectedErr:   base32.CorruptInputError(0),
   503  			expectedBytes: nil,
   504  		},
   505  		{
   506  			// Check write onion v2.
   507  			name: "onion address v2",
   508  			addr: &tor.OnionAddr{
   509  				OnionService: "abcdefghijklmnop.onion",
   510  				Port:         9065,
   511  			},
   512  			expectedErr: nil,
   513  			expectedBytes: []byte{
   514  				0x3,                         // The descriptor.
   515  				0x0, 0x44, 0x32, 0x14, 0xc7, // The host.
   516  				0x42, 0x54, 0xb6, 0x35, 0xcf,
   517  				0x23, 0x69, // The port.
   518  			},
   519  		},
   520  		{
   521  			// Check write onion v3.
   522  			name: "onion address v3",
   523  			addr: &tor.OnionAddr{
   524  				OnionService: "abcdefghij" +
   525  					"abcdefghijabcdefghij" +
   526  					"abcdefghijabcdefghij" +
   527  					"234567.onion",
   528  				Port: 9065,
   529  			},
   530  			expectedErr: nil,
   531  			expectedBytes: []byte{
   532  				0x4, // The descriptor.
   533  				0x0, 0x44, 0x32, 0x14, 0xc7, 0x42, 0x40,
   534  				0x11, 0xc, 0x85, 0x31, 0xd0, 0x90, 0x4,
   535  				0x43, 0x21, 0x4c, 0x74, 0x24, 0x1, 0x10,
   536  				0xc8, 0x53, 0x1d, 0x9, 0x0, 0x44, 0x32,
   537  				0x14, 0xc7, 0x42, 0x75, 0xbe, 0x77, 0xdf,
   538  				0x23, 0x69, // The port.
   539  			},
   540  		},
   541  	}
   542  
   543  	for _, tc := range testCases {
   544  		tc := tc
   545  		t.Run(tc.name, func(t *testing.T) {
   546  			oldLen := buf.Len()
   547  
   548  			err := WriteOnionAddr(buf, tc.addr)
   549  			require.Equal(t, tc.expectedErr, err)
   550  
   551  			bytesWritten := buf.Bytes()[oldLen:buf.Len()]
   552  			require.Equal(t, tc.expectedBytes, bytesWritten)
   553  		})
   554  	}
   555  }
   556  
   557  func TestWriteNetAddrs(t *testing.T) {
   558  	buf := new(bytes.Buffer)
   559  	tcpAddr := &net.TCPAddr{
   560  		IP:   net.IP{127, 0, 0, 1},
   561  		Port: 8080,
   562  	}
   563  	onionAddr := &tor.OnionAddr{
   564  		OnionService: "abcdefghijklmnop.onion",
   565  		Port:         9065,
   566  	}
   567  
   568  	testCases := []struct {
   569  		name string
   570  		addr []net.Addr
   571  
   572  		expectedErr   error
   573  		expectedBytes []byte
   574  	}{
   575  		{
   576  			// Check that the error is returned when nil address is
   577  			// used.
   578  			name:          "nil address err",
   579  			addr:          []net.Addr{nil, tcpAddr, onionAddr},
   580  			expectedErr:   ErrNilNetAddress,
   581  			expectedBytes: nil,
   582  		},
   583  		{
   584  			// Check empty address slice.
   585  			name:        "empty address slice",
   586  			addr:        []net.Addr{},
   587  			expectedErr: nil,
   588  			// Use two bytes to encode the address size.
   589  			expectedBytes: []byte{0, 0},
   590  		},
   591  		{
   592  			// Check a successful writes of a slice of addresses.
   593  			name:        "two addresses",
   594  			addr:        []net.Addr{tcpAddr, onionAddr},
   595  			expectedErr: nil,
   596  			expectedBytes: []byte{
   597  				// 7 bytes for TCP and 13 bytes for onion.
   598  				0x0, 0x14,
   599  				// TCP address.
   600  				0x1, 0x7f, 0x0, 0x0, 0x1, 0x1f, 0x90,
   601  				// Onion address.
   602  				0x3, 0x0, 0x44, 0x32, 0x14, 0xc7, 0x42,
   603  				0x54, 0xb6, 0x35, 0xcf, 0x23, 0x69,
   604  			},
   605  		},
   606  	}
   607  
   608  	for _, tc := range testCases {
   609  		tc := tc
   610  		t.Run(tc.name, func(t *testing.T) {
   611  			oldLen := buf.Len()
   612  
   613  			err := WriteNetAddrs(buf, tc.addr)
   614  			require.Equal(t, tc.expectedErr, err)
   615  
   616  			bytesWritten := buf.Bytes()[oldLen:buf.Len()]
   617  			require.Equal(t, tc.expectedBytes, bytesWritten)
   618  		})
   619  	}
   620  }