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