gitlab.com/SiaPrime/SiaPrime@v1.4.1/modules/netaddress_test.go (about) 1 package modules 2 3 import ( 4 "net" 5 "strings" 6 "testing" 7 ) 8 9 var ( 10 // Networks such as 10.0.0.x have been omitted from testing - behavior 11 // for these networks is currently undefined. 12 13 invalidAddrs = []string{ 14 // Garbage addresses 15 "", 16 "foo:bar:baz", 17 "garbage:6146:616", 18 // Missing host / port 19 ":", 20 "111.111.111.111", 21 "12.34.45.64", 22 "[::2]", 23 "::2", 24 "foo", 25 "hn.com", 26 "世界", 27 "foo:", 28 "世界:", 29 ":foo", 30 ":世界", 31 "localhost:", 32 "[::1]:", 33 // Invalid host / port chars 34 "localhost:-", 35 "[::1]:-", 36 "foo:{}", 37 "{}:123", 38 " foo:123", 39 "foo :123", 40 "f oo:123", 41 "foo: 123", 42 "foo:123 ", 43 "foo:1 23", 44 "\x00:123", 45 "foo:\x00", 46 "世界:123", 47 "bar:世界", 48 "世:界", 49 `":"`, 50 // Unspecified address 51 "[::]:bar", 52 "0.0.0.0:bar", 53 // Invalid hostnames 54 "unqualifiedhost:123", 55 "Yo-Amazon.we-are-really-happy-for-you.and-we-will-let-you-finish.but-Sia-is-the-best-cloud-storage-of-all-time.of-all-time-of-all-time-of-all-time-of-all-time-of-all-time.of-all-time-of-all-time-of-all-time-of-all-time-of-all-time.of-all-time-of-all-time:123", 56 strings.Repeat("a", 64) + ".com:123", // 64 char long label too long. 57 strings.Repeat(strings.Repeat("a", 62)+".", 4) + "co:123", // 254 char long hostname too long. 58 strings.Repeat(strings.Repeat("a", 62)+".", 4) + "co.:123", // 254 char long hostname with trailing dot too long. 59 "-foo.bar:123", 60 "foo-.bar:123", 61 "foo.-bar:123", 62 "foo.bar-:123", 63 "foo-bar.-baz:123", 64 "foo-bar.baz-:123", 65 "foo.-bar.baz:123", 66 "foo.bar-.baz:123", 67 ".:123", 68 ".foo.com:123", 69 "foo.com..:123", 70 // invalid port numbers 71 "foo:0", 72 "foo:65536", 73 "foo:-100", 74 "foo:1000000", 75 "localhost:0", 76 "[::1]:0", 77 } 78 validAddrs = []string{ 79 // Loopback address (valid in testing only, can't really test this well) 80 "localhost:123", 81 "127.0.0.1:123", 82 "[::1]:123", 83 // Valid addresses. 84 "foo.com:1", 85 "foo.com.:1", 86 "a.b.c:1", 87 "a.b.c.:1", 88 "foo-bar.com:123", 89 "FOO.com:1", 90 "1foo.com:1", 91 "tld.foo.com:1", 92 "hn.com:8811", 93 94 "[::2]:65535", 95 "111.111.111.111:111", 96 "12.34.45.64:7777", 97 98 strings.Repeat("foo.", 63) + "f:123", // 253 chars long 99 strings.Repeat("foo.", 63) + "f.:123", // 254 chars long, 253 chars long without trailing dot 100 101 strings.Repeat(strings.Repeat("a", 63)+".", 3) + "a:123", // 3x63 char length labels + 1x1 char length label without trailing dot 102 strings.Repeat(strings.Repeat("a", 63)+".", 3) + ":123", // 3x63 char length labels with trailing dot 103 } 104 ) 105 106 // TestHostPort tests the Host and Port methods of the NetAddress type. 107 func TestHostPort(t *testing.T) { 108 t.Parallel() 109 110 // Test valid addrs. 111 for _, addr := range validAddrs { 112 na := NetAddress(addr) 113 host := na.Host() 114 port := na.Port() 115 expectedHost, expectedPort, err := net.SplitHostPort(addr) 116 if err != nil { 117 t.Fatal(err) 118 } 119 if host != expectedHost { 120 t.Errorf("Host() returned unexpected host for NetAddress '%v': expected '%v', got '%v'", na, expectedHost, host) 121 } 122 if port != expectedPort { 123 t.Errorf("Port() returned unexpected port for NetAddress '%v': expected '%v', got '%v'", na, expectedPort, port) 124 } 125 } 126 127 // Test that Host / Port return "" when net.SplitHostPort errors 128 na := NetAddress("::") 129 host := na.Host() 130 port := na.Port() 131 if host != "" { 132 t.Error("expected Host() to return blank for an un-splittable NetAddress, but it returned:", host) 133 } 134 if port != "" { 135 t.Error("expected Port() to return blank for an un-splittable NetAddress, but it returned:", port) 136 } 137 } 138 139 // TestIsLoopback tests the IsLoopback method of the NetAddress type. 140 func TestIsLoopback(t *testing.T) { 141 t.Parallel() 142 143 testSet := []struct { 144 query NetAddress 145 desiredResponse bool 146 }{ 147 // Localhost tests. 148 {"localhost", false}, 149 {"localhost:1234", true}, 150 {"127.0.0.1", false}, 151 {"127.0.0.1:6723", true}, 152 {"::1", false}, 153 {"[::1]:7124", true}, 154 155 // Local network tests. 156 {"10.0.0.0", false}, 157 {"10.0.0.0:1234", false}, 158 {"10.2.2.5", false}, 159 {"10.2.2.5:16432", false}, 160 {"10.255.255.255", false}, 161 {"10.255.255.255:16432", false}, 162 {"172.16.0.0", false}, 163 {"172.16.0.0:1234", false}, 164 {"172.26.2.5", false}, 165 {"172.26.2.5:16432", false}, 166 {"172.31.255.255", false}, 167 {"172.31.255.255:16432", false}, 168 {"192.168.0.0", false}, 169 {"192.168.0.0:1234", false}, 170 {"192.168.2.5", false}, 171 {"192.168.2.5:16432", false}, 172 {"192.168.255.255", false}, 173 {"192.168.255.255:16432", false}, 174 {"1234:0000:0000:0000:0000:0000:0000:0000", false}, 175 {"[1234:0000:0000:0000:0000:0000:0000:0000]:1234", false}, 176 {"fc00:0000:0000:0000:0000:0000:0000:0000", false}, 177 {"[fc00:0000:0000:0000:0000:0000:0000:0000]:1234", false}, 178 {"fd00:0000:0000:0000:0000:0000:0000:0000", false}, 179 {"[fd00:0000:0000:0000:0000:0000:0000:0000]:1234", false}, 180 {"fd30:0000:0000:0000:0000:0000:0000:0000", false}, 181 {"[fd30:0000:0000:0000:0000:0000:0000:0000]:1234", false}, 182 {"fd00:0000:0030:0000:0000:0000:0000:0000", false}, 183 {"[fd00:0000:0030:0000:0000:0000:0000:0000]:1234", false}, 184 {"fdff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", false}, 185 {"[fdff:ffff:ffff:ffff:ffff:ffff:ffff:ffff]:1234", false}, 186 {"fe00:0000:0000:0000:0000:0000:0000:0000", false}, 187 {"[fe00:0000:0000:0000:0000:0000:0000:0000]:1234", false}, 188 189 // Unspecified address tests. 190 {"0.0.0.0:1234", false}, 191 {"[::]:1234", false}, 192 193 // Public name tests. 194 {"hn.com", false}, 195 {"hn.com:8811", false}, 196 {"2.34.45.64", false}, 197 {"2.34.45.64:7777", false}, 198 {"12.34.45.64", false}, 199 {"12.34.45.64:7777", false}, 200 {"122.34.45.64", false}, 201 {"122.34.45.64:7777", false}, 202 {"197.34.45.64", false}, 203 {"197.34.45.64:7777", false}, 204 {"222.34.45.64", false}, 205 {"222.34.45.64:7777", false}, 206 207 // Garbage name tests. 208 {"", false}, 209 {"garbage", false}, 210 {"garbage:6432", false}, 211 {"garbage:6146:616", false}, 212 {"::1:4646", false}, 213 {"[::1]", false}, 214 } 215 for _, test := range testSet { 216 if test.query.IsLoopback() != test.desiredResponse { 217 t.Error("test failed:", test, test.query.IsLoopback()) 218 } 219 } 220 } 221 222 // TestIsValid tests that IsValid only returns nil for valid addresses. 223 func TestIsValid(t *testing.T) { 224 t.Parallel() 225 226 for _, addr := range validAddrs { 227 na := NetAddress(addr) 228 if err := na.IsValid(); err != nil { 229 t.Errorf("IsValid returned non-nil for valid NetAddress %q: %v", addr, err) 230 } 231 } 232 for _, addr := range invalidAddrs { 233 na := NetAddress(addr) 234 if err := na.IsValid(); err == nil { 235 t.Errorf("IsValid returned nil for an invalid NetAddress %q: %v", addr, err) 236 } 237 } 238 } 239 240 // TestIsLocal checks that the correct values are returned for all local IP 241 // addresses. 242 func TestIsLocal(t *testing.T) { 243 t.Parallel() 244 245 testSet := []struct { 246 query NetAddress 247 desiredResponse bool 248 }{ 249 // Localhost tests. 250 {"localhost", false}, 251 {"localhost:1234", true}, 252 {"127.0.0.1", false}, 253 {"127.0.0.1:6723", true}, 254 {"::1", false}, 255 {"[::1]:7124", true}, 256 257 // Local network tests. 258 {"10.0.0.0", false}, 259 {"10.0.0.0:1234", true}, 260 {"10.2.2.5", false}, 261 {"10.2.2.5:16432", true}, 262 {"10.255.255.255", false}, 263 {"10.255.255.255:16432", true}, 264 {"172.16.0.0", false}, 265 {"172.16.0.0:1234", true}, 266 {"172.26.2.5", false}, 267 {"172.26.2.5:16432", true}, 268 {"172.31.255.255", false}, 269 {"172.31.255.255:16432", true}, 270 {"192.168.0.0", false}, 271 {"192.168.0.0:1234", true}, 272 {"192.168.2.5", false}, 273 {"192.168.2.5:16432", true}, 274 {"192.168.255.255", false}, 275 {"192.168.255.255:16432", true}, 276 {"1234:0000:0000:0000:0000:0000:0000:0000", false}, 277 {"[1234:0000:0000:0000:0000:0000:0000:0000]:1234", false}, 278 {"fc00:0000:0000:0000:0000:0000:0000:0000", false}, 279 {"[fc00:0000:0000:0000:0000:0000:0000:0000]:1234", false}, 280 {"fd00:0000:0000:0000:0000:0000:0000:0000", false}, 281 {"[fd00:0000:0000:0000:0000:0000:0000:0000]:1234", true}, 282 {"fd30:0000:0000:0000:0000:0000:0000:0000", false}, 283 {"[fd30:0000:0000:0000:0000:0000:0000:0000]:1234", true}, 284 {"fd00:0000:0030:0000:0000:0000:0000:0000", false}, 285 {"[fd00:0000:0030:0000:0000:0000:0000:0000]:1234", true}, 286 {"fdff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", false}, 287 {"[fdff:ffff:ffff:ffff:ffff:ffff:ffff:ffff]:1234", true}, 288 {"fe00:0000:0000:0000:0000:0000:0000:0000", false}, 289 {"[fe00:0000:0000:0000:0000:0000:0000:0000]:1234", false}, 290 291 // Unspecified address tests. 292 {"0.0.0.0:1234", false}, 293 {"[::]:1234", false}, 294 295 // Public name tests. 296 {"hn.com", false}, 297 {"hn.com:8811", false}, 298 {"2.34.45.64", false}, 299 {"2.34.45.64:7777", false}, 300 {"12.34.45.64", false}, 301 {"12.34.45.64:7777", false}, 302 {"122.34.45.64", false}, 303 {"122.34.45.64:7777", false}, 304 {"197.34.45.64", false}, 305 {"197.34.45.64:7777", false}, 306 {"222.34.45.64", false}, 307 {"222.34.45.64:7777", false}, 308 309 // Garbage name tests. 310 {"", false}, 311 {"garbage", false}, 312 {"garbage:6432", false}, 313 {"garbage:6146:616", false}, 314 {"::1:4646", false}, 315 {"[::1]", false}, 316 } 317 for _, test := range testSet { 318 if test.query.IsLocal() != test.desiredResponse { 319 t.Error("test failed:", test, test.query.IsLocal()) 320 } 321 } 322 }