add tests for RFC*, Local, Valid, and Routable.

This commit is contained in:
David Hill 2013-10-02 17:34:25 -04:00
parent 6f9aada482
commit 0cf8601169

View file

@ -18,9 +18,76 @@ type naTest struct {
want string want string
} }
type ipTest struct {
in btcwire.NetAddress
rfc1918 bool
rfc3849 bool
rfc3927 bool
rfc3964 bool
rfc4193 bool
rfc4380 bool
rfc4843 bool
rfc4862 bool
rfc6052 bool
rfc6145 bool
local bool
valid bool
routable bool
}
// naTests houses all of the tests to be performed against the NetAddressKey // naTests houses all of the tests to be performed against the NetAddressKey
// method. // method.
var naTests = make([]naTest, 0) var naTests = make([]naTest, 0)
var ipTests = make([]ipTest, 0)
func addIpTest(ip string, rfc1918, rfc3849, rfc3927, rfc3964, rfc4193, rfc4380,
rfc4843, rfc4862, rfc6052, rfc6145, local, valid, routable bool) {
nip := net.ParseIP(ip)
na := btcwire.NetAddress{
Timestamp: time.Now(),
Services: btcwire.SFNodeNetwork,
IP: nip,
Port: 8333,
}
test := ipTest{na, rfc1918, rfc3849, rfc3927, rfc3964, rfc4193, rfc4380,
rfc4843, rfc4862, rfc6052, rfc6145, local, valid, routable}
ipTests = append(ipTests, test)
}
func addIpTests() {
addIpTest("10.255.255.255", true, false, false, false, false, false,
false, false, false, false, false, true, false)
addIpTest("192.168.0.1", true, false, false, false, false, false,
false, false, false, false, false, true, false)
addIpTest("172.31.255.1", true, false, false, false, false, false,
false, false, false, false, false, true, false)
addIpTest("172.32.1.1", false, false, false, false, false, false,
false, false, false, false, false, true, true)
addIpTest("169.254.250.120", false, false, true, false, false, false,
false, false, false, false, false, true, false)
addIpTest("0.0.0.0", false, false, false, false, false, false,
false, false, false, false, true, false, false)
addIpTest("255.255.255.255", false, false, false, false, false, false,
false, false, false, false, false, false, false)
addIpTest("127.0.0.1", false, false, false, false, false, false,
false, false, false, false, true, true, false)
addIpTest("fd00:dead::1", false, false, false, false, true, false,
false, false, false, false, false, true, false)
addIpTest("2001::1", false, false, false, false, false, true,
false, false, false, false, false, true, true)
addIpTest("2001:10:abcd::1:1", false, false, false, false, false, false,
true, false, false, false, false, true, false)
addIpTest("fe80::1", false, false, false, false, false, false,
false, true, false, false, false, true, false)
addIpTest("fe80:1::1", false, false, false, false, false, false,
false, false, false, false, false, true, true)
addIpTest("64:ff9b::1", false, false, false, false, false, false,
false, false, true, false, false, true, true)
addIpTest("::ffff:abcd:ef12:1", false, false, false, false, false, false,
false, false, false, false, false, true, true)
addIpTest("::1", false, false, false, false, false, false,
false, false, false, false, true, true, false)
}
func addNaTest(ip string, port uint16, want string) { func addNaTest(ip string, port uint16, want string) {
nip := net.ParseIP(ip) nip := net.ParseIP(ip)
@ -95,6 +162,110 @@ func addNaTests() {
addNaTest("fef3::4:4", 8336, "[fef3::4:4]:8336") addNaTest("fef3::4:4", 8336, "[fef3::4:4]:8336")
} }
func TestGetAddress(t *testing.T) {
n := NewAddrManager()
if rv := n.GetAddress("any", 10); rv != nil {
t.Errorf("GetAddress failed: got: %v want: %v\n", rv, nil)
}
}
func TestIpTypes(t *testing.T) {
addIpTests()
t.Logf("Running %d tests", len(ipTests))
for _, test := range ipTests {
rv := RFC1918(&test.in)
if rv != test.rfc1918 {
t.Errorf("RFC1918 %s\n got: %v want: %v", test.in.IP, rv, test.rfc1918)
continue
}
}
for _, test := range ipTests {
rv := RFC3849(&test.in)
if rv != test.rfc3849 {
t.Errorf("RFC3849 %s\n got: %v want: %v", test.in.IP, rv, test.rfc3849)
continue
}
}
for _, test := range ipTests {
rv := RFC3927(&test.in)
if rv != test.rfc3927 {
t.Errorf("RFC3927 %s\n got: %v want: %v", test.in.IP, rv, test.rfc3927)
continue
}
}
for _, test := range ipTests {
rv := RFC3964(&test.in)
if rv != test.rfc3964 {
t.Errorf("RFC3964 %s\n got: %v want: %v", test.in.IP, rv, test.rfc3964)
continue
}
}
for _, test := range ipTests {
rv := RFC4193(&test.in)
if rv != test.rfc4193 {
t.Errorf("RFC4193 %s\n got: %v want: %v", test.in.IP, rv, test.rfc4193)
continue
}
}
for _, test := range ipTests {
rv := RFC4380(&test.in)
if rv != test.rfc4380 {
t.Errorf("RFC4380 %s\n got: %v want: %v", test.in.IP, rv, test.rfc4380)
continue
}
}
for _, test := range ipTests {
rv := RFC4843(&test.in)
if rv != test.rfc4843 {
t.Errorf("RFC4843 %s\n got: %v want: %v", test.in.IP, rv, test.rfc4843)
continue
}
}
for _, test := range ipTests {
rv := RFC4862(&test.in)
if rv != test.rfc4862 {
t.Errorf("RFC4862 %s\n got: %v want: %v", test.in.IP, rv, test.rfc4862)
continue
}
}
for _, test := range ipTests {
rv := RFC6052(&test.in)
if rv != test.rfc6052 {
t.Errorf("RFC6052 %s\n got: %v want: %v", test.in.IP, rv, test.rfc6052)
continue
}
}
for _, test := range ipTests {
rv := RFC6145(&test.in)
if rv != test.rfc6145 {
t.Errorf("RFC1918 %s\n got: %v want: %v", test.in.IP, rv, test.rfc6145)
continue
}
}
for _, test := range ipTests {
rv := Local(&test.in)
if rv != test.local {
t.Errorf("Local %s\n got: %v want: %v", test.in.IP, rv, test.local)
continue
}
}
for _, test := range ipTests {
rv := Valid(&test.in)
if rv != test.valid {
t.Errorf("Valid %s\n got: %v want: %v", test.in.IP, rv, test.valid)
continue
}
}
for _, test := range ipTests {
rv := Routable(&test.in)
if rv != test.routable {
t.Errorf("Routable %s\n got: %v want: %v", test.in.IP, rv, test.routable)
continue
}
}
}
func TestNetAddressKey(t *testing.T) { func TestNetAddressKey(t *testing.T) {
addNaTests() addNaTests()
@ -106,4 +277,5 @@ func TestNetAddressKey(t *testing.T) {
continue continue
} }
} }
} }