From e2e00f9dd2d6f1b86e56efc85b9c1eabf3aa6bcf Mon Sep 17 00:00:00 2001 From: "Johan T. Halseth" Date: Thu, 17 Jan 2019 14:11:56 +0100 Subject: [PATCH] channeldb/addr_test: add invalid TCP cases These are invalid length IP addresses that earlier would not fail to serialize. --- channeldb/addr_test.go | 42 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/channeldb/addr_test.go b/channeldb/addr_test.go index c3d4e07f..ad0da59e 100644 --- a/channeldb/addr_test.go +++ b/channeldb/addr_test.go @@ -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