lnwire: add TestDecodeUnknownAddressType

Add a test to demonstrate that if a NodeAnnouncement includes an address
with an unknown type, then we incorrectly return an error. This will be
fixed in the following commit.
This commit is contained in:
Elle Mouton 2022-04-20 16:53:44 +02:00
parent 12b82d0156
commit f77388e072
No known key found for this signature in database
GPG Key ID: D7D916376026F177

View File

@ -20,6 +20,7 @@ import (
"github.com/btcsuite/btcd/wire"
"github.com/lightningnetwork/lnd/tor"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var (
@ -226,6 +227,52 @@ func TestChanUpdateChanFlags(t *testing.T) {
}
}
// TestDecodeUnknownAddressType shows that an unknown address type is currently
// incorrectly dealt with.
func TestDecodeUnknownAddressType(t *testing.T) {
// First, we'll encode all the addresses into an intermediate
// buffer. We need to do this in order to compute the total
// length of the addresses.
addrBuf := bytes.NewBuffer(make([]byte, 0, MaxMsgBody))
// Add a normal, clearnet address.
tcpAddr := &net.TCPAddr{
IP: net.IP{127, 0, 0, 1},
Port: 8080,
}
err := WriteTCPAddr(addrBuf, tcpAddr)
require.NoError(t, err)
// Add an onion address.
onionAddr := &tor.OnionAddr{
OnionService: "abcdefghijklmnop.onion",
Port: 9065,
}
err = WriteOnionAddr(addrBuf, onionAddr)
require.NoError(t, err)
// Now add an address with an unknown type.
var newAddrType addressType = math.MaxUint8
data := make([]byte, 0, 16)
data = append(data, uint8(newAddrType))
_, err = addrBuf.Write(data)
require.NoError(t, err)
// Now that we have all the addresses, we can append the length.
buffer := bytes.NewBuffer(make([]byte, 0, MaxMsgBody))
err = writeDataWithLength(buffer, addrBuf.Bytes())
require.NoError(t, err)
// Now we attempt to parse the bytes and we assert that we get an
// error.
var addrs []net.Addr
err = ReadElement(buffer, &addrs)
require.Error(t, err)
require.IsType(t, err, &ErrUnknownAddrType{})
}
func TestMaxOutPointIndex(t *testing.T) {
t.Parallel()