channeldb/addr_test: add invalid TCP cases

These are invalid length IP addresses that earlier would not fail to
serialize.
This commit is contained in:
Johan T. Halseth 2019-01-17 14:11:56 +01:00
parent 02b7bb356a
commit e2e00f9dd2
No known key found for this signature in database
GPG Key ID: 15BAADA29DA20D26

View File

@ -14,19 +14,23 @@ type unknownAddrType struct{}
func (t unknownAddrType) Network() string { return "unknown" }
func (t unknownAddrType) String() string { return "unknown" }
var testIP4 = net.ParseIP("192.168.1.1")
var testIP6 = net.ParseIP("2001:0db8:0000:0000:0000:ff00:0042:8329")
var addrTests = []struct {
expAddr net.Addr
serErr string
}{
// Valid addresses.
{
expAddr: &net.TCPAddr{
IP: net.ParseIP("192.168.1.1"),
IP: testIP4,
Port: 12345,
},
},
{
expAddr: &net.TCPAddr{
IP: net.ParseIP("2001:0db8:0000:0000:0000:ff00:0042:8329"),
IP: testIP6,
Port: 65535,
},
},
@ -42,10 +46,44 @@ var addrTests = []struct {
Port: 80,
},
},
// Invalid addresses.
{
expAddr: unknownAddrType{},
serErr: ErrUnknownAddressType.Error(),
},
{
expAddr: &net.TCPAddr{
// Remove last byte of IPv4 address.
IP: testIP4[:len(testIP4)-1],
Port: 12345,
},
serErr: "unable to encode",
},
{
expAddr: &net.TCPAddr{
// Add an extra byte of IPv4 address.
IP: append(testIP4, 0xff),
Port: 12345,
},
serErr: "unable to encode",
},
{
expAddr: &net.TCPAddr{
// Remove last byte of IPv6 address.
IP: testIP6[:len(testIP6)-1],
Port: 65535,
},
serErr: "unable to encode",
},
{
expAddr: &net.TCPAddr{
// Add an extra byte to the IPv6 address.
IP: append(testIP6, 0xff),
Port: 65535,
},
serErr: "unable to encode",
},
}
// TestAddrSerialization tests that the serialization method used by channeldb