github.com/lbryio/lbcd@v0.22.119/addrmgr/network_test.go (about) 1 // Copyright (c) 2013-2014 The btcsuite developers 2 // Use of this source code is governed by an ISC 3 // license that can be found in the LICENSE file. 4 5 package addrmgr_test 6 7 import ( 8 "net" 9 "testing" 10 11 "github.com/lbryio/lbcd/addrmgr" 12 "github.com/lbryio/lbcd/wire" 13 ) 14 15 // TestIPTypes ensures the various functions which determine the type of an IP 16 // address based on RFCs work as intended. 17 func TestIPTypes(t *testing.T) { 18 type ipTest struct { 19 in wire.NetAddress 20 rfc1918 bool 21 rfc2544 bool 22 rfc3849 bool 23 rfc3927 bool 24 rfc3964 bool 25 rfc4193 bool 26 rfc4380 bool 27 rfc4843 bool 28 rfc4862 bool 29 rfc5737 bool 30 rfc6052 bool 31 rfc6145 bool 32 rfc6598 bool 33 local bool 34 valid bool 35 routable bool 36 } 37 38 newIPTest := func(ip string, rfc1918, rfc2544, rfc3849, rfc3927, rfc3964, 39 rfc4193, rfc4380, rfc4843, rfc4862, rfc5737, rfc6052, rfc6145, rfc6598, 40 local, valid, routable bool) ipTest { 41 nip := net.ParseIP(ip) 42 na := *wire.NewNetAddressIPPort(nip, 9246, wire.SFNodeNetwork) 43 test := ipTest{na, rfc1918, rfc2544, rfc3849, rfc3927, rfc3964, rfc4193, rfc4380, 44 rfc4843, rfc4862, rfc5737, rfc6052, rfc6145, rfc6598, local, valid, routable} 45 return test 46 } 47 48 tests := []ipTest{ 49 newIPTest("10.255.255.255", true, false, false, false, false, false, 50 false, false, false, false, false, false, false, false, true, false), 51 newIPTest("192.168.0.1", true, false, false, false, false, false, 52 false, false, false, false, false, false, false, false, true, false), 53 newIPTest("172.31.255.1", true, false, false, false, false, false, 54 false, false, false, false, false, false, false, false, true, false), 55 newIPTest("172.32.1.1", false, false, false, false, false, false, false, false, 56 false, false, false, false, false, false, true, true), 57 newIPTest("169.254.250.120", false, false, false, true, false, false, 58 false, false, false, false, false, false, false, false, true, false), 59 newIPTest("0.0.0.0", false, false, false, false, false, false, false, 60 false, false, false, false, false, false, true, false, false), 61 newIPTest("255.255.255.255", false, false, false, false, false, false, 62 false, false, false, false, false, false, false, false, false, false), 63 newIPTest("127.0.0.1", false, false, false, false, false, false, 64 false, false, false, false, false, false, false, true, true, false), 65 newIPTest("fd00:dead::1", false, false, false, false, false, true, 66 false, false, false, false, false, false, false, false, true, false), 67 newIPTest("2001::1", false, false, false, false, false, false, 68 true, false, false, false, false, false, false, false, true, true), 69 newIPTest("2001:10:abcd::1:1", false, false, false, false, false, false, 70 false, true, false, false, false, false, false, false, true, false), 71 newIPTest("fe80::1", false, false, false, false, false, false, 72 false, false, true, false, false, false, false, false, true, false), 73 newIPTest("fe80:1::1", false, false, false, false, false, false, 74 false, false, false, false, false, false, false, false, true, true), 75 newIPTest("64:ff9b::1", false, false, false, false, false, false, 76 false, false, false, false, true, false, false, false, true, true), 77 newIPTest("::ffff:abcd:ef12:1", false, false, false, false, false, false, 78 false, false, false, false, false, false, false, false, true, true), 79 newIPTest("::1", false, false, false, false, false, false, false, false, 80 false, false, false, false, false, true, true, false), 81 newIPTest("198.18.0.1", false, true, false, false, false, false, false, 82 false, false, false, false, false, false, false, true, false), 83 newIPTest("100.127.255.1", false, false, false, false, false, false, false, 84 false, false, false, false, false, true, false, true, false), 85 newIPTest("203.0.113.1", false, false, false, false, false, false, false, 86 false, false, false, false, false, false, false, true, false), 87 } 88 89 t.Logf("Running %d tests", len(tests)) 90 for _, test := range tests { 91 if rv := addrmgr.IsRFC1918(&test.in); rv != test.rfc1918 { 92 t.Errorf("IsRFC1918 %s\n got: %v want: %v", test.in.IP, rv, test.rfc1918) 93 } 94 95 if rv := addrmgr.IsRFC3849(&test.in); rv != test.rfc3849 { 96 t.Errorf("IsRFC3849 %s\n got: %v want: %v", test.in.IP, rv, test.rfc3849) 97 } 98 99 if rv := addrmgr.IsRFC3927(&test.in); rv != test.rfc3927 { 100 t.Errorf("IsRFC3927 %s\n got: %v want: %v", test.in.IP, rv, test.rfc3927) 101 } 102 103 if rv := addrmgr.IsRFC3964(&test.in); rv != test.rfc3964 { 104 t.Errorf("IsRFC3964 %s\n got: %v want: %v", test.in.IP, rv, test.rfc3964) 105 } 106 107 if rv := addrmgr.IsRFC4193(&test.in); rv != test.rfc4193 { 108 t.Errorf("IsRFC4193 %s\n got: %v want: %v", test.in.IP, rv, test.rfc4193) 109 } 110 111 if rv := addrmgr.IsRFC4380(&test.in); rv != test.rfc4380 { 112 t.Errorf("IsRFC4380 %s\n got: %v want: %v", test.in.IP, rv, test.rfc4380) 113 } 114 115 if rv := addrmgr.IsRFC4843(&test.in); rv != test.rfc4843 { 116 t.Errorf("IsRFC4843 %s\n got: %v want: %v", test.in.IP, rv, test.rfc4843) 117 } 118 119 if rv := addrmgr.IsRFC4862(&test.in); rv != test.rfc4862 { 120 t.Errorf("IsRFC4862 %s\n got: %v want: %v", test.in.IP, rv, test.rfc4862) 121 } 122 123 if rv := addrmgr.IsRFC6052(&test.in); rv != test.rfc6052 { 124 t.Errorf("isRFC6052 %s\n got: %v want: %v", test.in.IP, rv, test.rfc6052) 125 } 126 127 if rv := addrmgr.IsRFC6145(&test.in); rv != test.rfc6145 { 128 t.Errorf("IsRFC1918 %s\n got: %v want: %v", test.in.IP, rv, test.rfc6145) 129 } 130 131 if rv := addrmgr.IsLocal(&test.in); rv != test.local { 132 t.Errorf("IsLocal %s\n got: %v want: %v", test.in.IP, rv, test.local) 133 } 134 135 if rv := addrmgr.IsValid(&test.in); rv != test.valid { 136 t.Errorf("IsValid %s\n got: %v want: %v", test.in.IP, rv, test.valid) 137 } 138 139 if rv := addrmgr.IsRoutable(&test.in); rv != test.routable { 140 t.Errorf("IsRoutable %s\n got: %v want: %v", test.in.IP, rv, test.routable) 141 } 142 } 143 } 144 145 // TestGroupKey tests the GroupKey function to ensure it properly groups various 146 // IP addresses. 147 func TestGroupKey(t *testing.T) { 148 tests := []struct { 149 name string 150 ip string 151 expected string 152 }{ 153 // Local addresses. 154 {name: "ipv4 localhost", ip: "127.0.0.1", expected: "local"}, 155 {name: "ipv6 localhost", ip: "::1", expected: "local"}, 156 {name: "ipv4 zero", ip: "0.0.0.0", expected: "local"}, 157 {name: "ipv4 first octet zero", ip: "0.1.2.3", expected: "local"}, 158 159 // Unroutable addresses. 160 {name: "ipv4 invalid bcast", ip: "255.255.255.255", expected: "unroutable"}, 161 {name: "ipv4 rfc1918 10/8", ip: "10.1.2.3", expected: "unroutable"}, 162 {name: "ipv4 rfc1918 172.16/12", ip: "172.16.1.2", expected: "unroutable"}, 163 {name: "ipv4 rfc1918 192.168/16", ip: "192.168.1.2", expected: "unroutable"}, 164 {name: "ipv6 rfc3849 2001:db8::/32", ip: "2001:db8::1234", expected: "unroutable"}, 165 {name: "ipv4 rfc3927 169.254/16", ip: "169.254.1.2", expected: "unroutable"}, 166 {name: "ipv6 rfc4193 fc00::/7", ip: "fc00::1234", expected: "unroutable"}, 167 {name: "ipv6 rfc4843 2001:10::/28", ip: "2001:10::1234", expected: "unroutable"}, 168 {name: "ipv6 rfc4862 fe80::/64", ip: "fe80::1234", expected: "unroutable"}, 169 170 // IPv4 normal. 171 {name: "ipv4 normal class a", ip: "12.1.2.3", expected: "12.1.0.0"}, 172 {name: "ipv4 normal class b", ip: "173.1.2.3", expected: "173.1.0.0"}, 173 {name: "ipv4 normal class c", ip: "196.1.2.3", expected: "196.1.0.0"}, 174 175 // IPv6/IPv4 translations. 176 {name: "ipv6 rfc3964 with ipv4 encap", ip: "2002:0c01:0203::", expected: "12.1.0.0"}, 177 {name: "ipv6 rfc4380 toredo ipv4", ip: "2001:0:1234::f3fe:fdfc", expected: "12.1.0.0"}, 178 {name: "ipv6 rfc6052 well-known prefix with ipv4", ip: "64:ff9b::0c01:0203", expected: "12.1.0.0"}, 179 {name: "ipv6 rfc6145 translated ipv4", ip: "::ffff:0:0c01:0203", expected: "12.1.0.0"}, 180 181 // Tor. 182 {name: "ipv6 tor onioncat", ip: "fd87:d87e:eb43:1234::5678", expected: "tor:2"}, 183 {name: "ipv6 tor onioncat 2", ip: "fd87:d87e:eb43:1245::6789", expected: "tor:2"}, 184 {name: "ipv6 tor onioncat 3", ip: "fd87:d87e:eb43:1345::6789", expected: "tor:3"}, 185 186 // IPv6 normal. 187 {name: "ipv6 normal", ip: "2602:100::1", expected: "2602:100::"}, 188 {name: "ipv6 normal 2", ip: "2602:0100::1234", expected: "2602:100::"}, 189 {name: "ipv6 hurricane electric", ip: "2001:470:1f10:a1::2", expected: "2001:470:1000::"}, 190 {name: "ipv6 hurricane electric 2", ip: "2001:0470:1f10:a1::2", expected: "2001:470:1000::"}, 191 } 192 193 for i, test := range tests { 194 nip := net.ParseIP(test.ip) 195 na := *wire.NewNetAddressIPPort(nip, 9246, wire.SFNodeNetwork) 196 if key := addrmgr.GroupKey(&na); key != test.expected { 197 t.Errorf("TestGroupKey #%d (%s): unexpected group key "+ 198 "- got '%s', want '%s'", i, test.name, 199 key, test.expected) 200 } 201 } 202 }