github.com/ZuluSpl0it/Sia@v1.3.7/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 strings.Repeat("foo.", 63) + "f:123", // 253 chars long 94 strings.Repeat("foo.", 63) + "f.:123", // 254 chars long, 253 chars long without trailing dot 95 strings.Repeat(strings.Repeat("a", 63)+".", 3) + "a:123", // 3x63 char length labels + 1x1 char length label without trailing dot 96 strings.Repeat(strings.Repeat("a", 63)+".", 3) + ":123", // 3x63 char length labels with trailing dot 97 "[::2]:65535", 98 "111.111.111.111:111", 99 "12.34.45.64:7777", 100 } 101 ) 102 103 // TestHostPort tests the Host and Port methods of the NetAddress type. 104 func TestHostPort(t *testing.T) { 105 t.Parallel() 106 107 // Test valid addrs. 108 for _, addr := range validAddrs { 109 na := NetAddress(addr) 110 host := na.Host() 111 port := na.Port() 112 expectedHost, expectedPort, err := net.SplitHostPort(addr) 113 if err != nil { 114 t.Fatal(err) 115 } 116 if host != expectedHost { 117 t.Errorf("Host() returned unexpected host for NetAddress '%v': expected '%v', got '%v'", na, expectedHost, host) 118 } 119 if port != expectedPort { 120 t.Errorf("Port() returned unexpected port for NetAddress '%v': expected '%v', got '%v'", na, expectedPort, port) 121 } 122 } 123 124 // Test that Host / Port return "" when net.SplitHostPort errors 125 na := NetAddress("::") 126 host := na.Host() 127 port := na.Port() 128 if host != "" { 129 t.Error("expected Host() to return blank for an un-splittable NetAddress, but it returned:", host) 130 } 131 if port != "" { 132 t.Error("expected Port() to return blank for an un-splittable NetAddress, but it returned:", port) 133 } 134 } 135 136 // TestIsLoopback tests the IsLoopback method of the NetAddress type. 137 func TestIsLoopback(t *testing.T) { 138 t.Parallel() 139 140 testSet := []struct { 141 query NetAddress 142 desiredResponse bool 143 }{ 144 // Localhost tests. 145 {"localhost", false}, 146 {"localhost:1234", true}, 147 {"127.0.0.1", false}, 148 {"127.0.0.1:6723", true}, 149 {"::1", false}, 150 {"[::1]:7124", true}, 151 152 // Local network tests. 153 {"10.0.0.0", false}, 154 {"10.0.0.0:1234", false}, 155 {"10.2.2.5", false}, 156 {"10.2.2.5:16432", false}, 157 {"10.255.255.255", false}, 158 {"10.255.255.255:16432", false}, 159 {"172.16.0.0", false}, 160 {"172.16.0.0:1234", false}, 161 {"172.26.2.5", false}, 162 {"172.26.2.5:16432", false}, 163 {"172.31.255.255", false}, 164 {"172.31.255.255:16432", false}, 165 {"192.168.0.0", false}, 166 {"192.168.0.0:1234", false}, 167 {"192.168.2.5", false}, 168 {"192.168.2.5:16432", false}, 169 {"192.168.255.255", false}, 170 {"192.168.255.255:16432", false}, 171 {"1234:0000:0000:0000:0000:0000:0000:0000", false}, 172 {"[1234:0000:0000:0000:0000:0000:0000:0000]:1234", false}, 173 {"fc00:0000:0000:0000:0000:0000:0000:0000", false}, 174 {"[fc00:0000:0000:0000:0000:0000:0000:0000]:1234", false}, 175 {"fd00:0000:0000:0000:0000:0000:0000:0000", false}, 176 {"[fd00:0000:0000:0000:0000:0000:0000:0000]:1234", false}, 177 {"fd30:0000:0000:0000:0000:0000:0000:0000", false}, 178 {"[fd30:0000:0000:0000:0000:0000:0000:0000]:1234", false}, 179 {"fd00:0000:0030:0000:0000:0000:0000:0000", false}, 180 {"[fd00:0000:0030:0000:0000:0000:0000:0000]:1234", false}, 181 {"fdff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", false}, 182 {"[fdff:ffff:ffff:ffff:ffff:ffff:ffff:ffff]:1234", false}, 183 {"fe00:0000:0000:0000:0000:0000:0000:0000", false}, 184 {"[fe00:0000:0000:0000:0000:0000:0000:0000]:1234", false}, 185 186 // Unspecified address tests. 187 {"0.0.0.0:1234", false}, 188 {"[::]:1234", false}, 189 190 // Public name tests. 191 {"hn.com", false}, 192 {"hn.com:8811", false}, 193 {"2.34.45.64", false}, 194 {"2.34.45.64:7777", false}, 195 {"12.34.45.64", false}, 196 {"12.34.45.64:7777", false}, 197 {"122.34.45.64", false}, 198 {"122.34.45.64:7777", false}, 199 {"197.34.45.64", false}, 200 {"197.34.45.64:7777", false}, 201 {"222.34.45.64", false}, 202 {"222.34.45.64:7777", false}, 203 204 // Garbage name tests. 205 {"", false}, 206 {"garbage", false}, 207 {"garbage:6432", false}, 208 {"garbage:6146:616", false}, 209 {"::1:4646", false}, 210 {"[::1]", false}, 211 } 212 for _, test := range testSet { 213 if test.query.IsLoopback() != test.desiredResponse { 214 t.Error("test failed:", test, test.query.IsLoopback()) 215 } 216 } 217 } 218 219 // TestIsValid tests that IsValid only returns nil for valid addresses. 220 func TestIsValid(t *testing.T) { 221 t.Parallel() 222 223 for _, addr := range validAddrs { 224 na := NetAddress(addr) 225 if err := na.IsValid(); err != nil { 226 t.Errorf("IsValid returned non-nil for valid NetAddress %q: %v", addr, err) 227 } 228 } 229 for _, addr := range invalidAddrs { 230 na := NetAddress(addr) 231 if err := na.IsValid(); err == nil { 232 t.Errorf("IsValid returned nil for an invalid NetAddress %q: %v", addr, err) 233 } 234 } 235 } 236 237 // TestIsLocal checks that the correct values are returned for all local IP 238 // addresses. 239 func TestIsLocal(t *testing.T) { 240 t.Parallel() 241 242 testSet := []struct { 243 query NetAddress 244 desiredResponse bool 245 }{ 246 // Localhost tests. 247 {"localhost", false}, 248 {"localhost:1234", true}, 249 {"127.0.0.1", false}, 250 {"127.0.0.1:6723", true}, 251 {"::1", false}, 252 {"[::1]:7124", true}, 253 254 // Local network tests. 255 {"10.0.0.0", false}, 256 {"10.0.0.0:1234", true}, 257 {"10.2.2.5", false}, 258 {"10.2.2.5:16432", true}, 259 {"10.255.255.255", false}, 260 {"10.255.255.255:16432", true}, 261 {"172.16.0.0", false}, 262 {"172.16.0.0:1234", true}, 263 {"172.26.2.5", false}, 264 {"172.26.2.5:16432", true}, 265 {"172.31.255.255", false}, 266 {"172.31.255.255:16432", true}, 267 {"192.168.0.0", false}, 268 {"192.168.0.0:1234", true}, 269 {"192.168.2.5", false}, 270 {"192.168.2.5:16432", true}, 271 {"192.168.255.255", false}, 272 {"192.168.255.255:16432", true}, 273 {"1234:0000:0000:0000:0000:0000:0000:0000", false}, 274 {"[1234:0000:0000:0000:0000:0000:0000:0000]:1234", false}, 275 {"fc00:0000:0000:0000:0000:0000:0000:0000", false}, 276 {"[fc00:0000:0000:0000:0000:0000:0000:0000]:1234", false}, 277 {"fd00:0000:0000:0000:0000:0000:0000:0000", false}, 278 {"[fd00:0000:0000:0000:0000:0000:0000:0000]:1234", true}, 279 {"fd30:0000:0000:0000:0000:0000:0000:0000", false}, 280 {"[fd30:0000:0000:0000:0000:0000:0000:0000]:1234", true}, 281 {"fd00:0000:0030:0000:0000:0000:0000:0000", false}, 282 {"[fd00:0000:0030:0000:0000:0000:0000:0000]:1234", true}, 283 {"fdff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", false}, 284 {"[fdff:ffff:ffff:ffff:ffff:ffff:ffff:ffff]:1234", true}, 285 {"fe00:0000:0000:0000:0000:0000:0000:0000", false}, 286 {"[fe00:0000:0000:0000:0000:0000:0000:0000]:1234", false}, 287 288 // Unspecified address tests. 289 {"0.0.0.0:1234", false}, 290 {"[::]:1234", false}, 291 292 // Public name tests. 293 {"hn.com", false}, 294 {"hn.com:8811", false}, 295 {"2.34.45.64", false}, 296 {"2.34.45.64:7777", false}, 297 {"12.34.45.64", false}, 298 {"12.34.45.64:7777", false}, 299 {"122.34.45.64", false}, 300 {"122.34.45.64:7777", false}, 301 {"197.34.45.64", false}, 302 {"197.34.45.64:7777", false}, 303 {"222.34.45.64", false}, 304 {"222.34.45.64:7777", false}, 305 306 // Garbage name tests. 307 {"", false}, 308 {"garbage", false}, 309 {"garbage:6432", false}, 310 {"garbage:6146:616", false}, 311 {"::1:4646", false}, 312 {"[::1]", false}, 313 } 314 for _, test := range testSet { 315 if test.query.IsLocal() != test.desiredResponse { 316 t.Error("test failed:", test, test.query.IsLocal()) 317 } 318 } 319 }