netaddress_test.go 8.82 KB
Newer Older
1 2 3
package modules

import (
4
	"net"
5
	"strings"
6 7 8
	"testing"
)

9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
var (
	// Networks such as 10.0.0.x have been omitted from testing - behavior
	// for these networks is currently undefined.

	invalidAddrs = []string{
		// Garbage addresses
		"",
		"foo:bar:baz",
		"garbage:6146:616",
		// Missing host / port
		":",
		"111.111.111.111",
		"12.34.45.64",
		"[::2]",
		"::2",
		"foo",
		"hn.com",
		"世界",
		"foo:",
		"世界:",
		":foo",
		":世界",
31 32
		"localhost:",
		"[::1]:",
33
		// Invalid host / port chars
34 35
		"localhost:-",
		"[::1]:-",
36 37 38 39 40 41 42 43 44
		"foo:{}",
		"{}:123",
		" foo:123",
		"foo :123",
		"f oo:123",
		"foo: 123",
		"foo:123 ",
		"foo:1 23",
		"\x00:123",
45
		"foo:\x00",
46 47 48 49
		"世界:123",
		"bar:世界",
		"世:界",
		`":"`,
50 51 52
		// Unspecified address
		"[::]:bar",
		"0.0.0.0:bar",
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
		// Invalid hostnames
		"unqualifiedhost:123",
		"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",
		strings.Repeat("a", 64) + ".com:123",                       // 64 char long label too long.
		strings.Repeat(strings.Repeat("a", 62)+".", 4) + "co:123",  // 254 char long hostname too long.
		strings.Repeat(strings.Repeat("a", 62)+".", 4) + "co.:123", // 254 char long hostname with trailing dot too long.
		"-foo.bar:123",
		"foo-.bar:123",
		"foo.-bar:123",
		"foo.bar-:123",
		"foo-bar.-baz:123",
		"foo-bar.baz-:123",
		"foo.-bar.baz:123",
		"foo.bar-.baz:123",
		".:123",
		".foo.com:123",
		"foo.com..:123",
70 71 72 73 74
		// invalid port numbers
		"foo:0",
		"foo:65536",
		"foo:-100",
		"foo:1000000",
75 76
		"localhost:0",
		"[::1]:0",
77 78 79
	}
	validAddrs = []string{
		// Loopback address (valid in testing only, can't really test this well)
80 81 82
		"localhost:123",
		"127.0.0.1:123",
		"[::1]:123",
83
		// Valid addresses.
84 85 86 87 88 89 90 91
		"foo.com:1",
		"foo.com.:1",
		"a.b.c:1",
		"a.b.c.:1",
		"foo-bar.com:123",
		"FOO.com:1",
		"1foo.com:1",
		"tld.foo.com:1",
92
		"hn.com:8811",
93 94 95 96
		strings.Repeat("foo.", 63) + "f:123",                     // 253 chars long
		strings.Repeat("foo.", 63) + "f.:123",                    // 254 chars long, 253 chars long without trailing dot
		strings.Repeat(strings.Repeat("a", 63)+".", 3) + "a:123", // 3x63 char length labels + 1x1 char length label without trailing dot
		strings.Repeat(strings.Repeat("a", 63)+".", 3) + ":123",  // 3x63 char length labels with trailing dot
97
		"[::2]:65535",
98 99 100 101 102 103 104
		"111.111.111.111:111",
		"12.34.45.64:7777",
	}
)

// TestHostPort tests the Host and Port methods of the NetAddress type.
func TestHostPort(t *testing.T) {
David Vorick's avatar
David Vorick committed
105 106
	t.Parallel()

107
	// Test valid addrs.
108 109 110 111 112 113 114 115 116 117 118 119 120 121
	for _, addr := range validAddrs {
		na := NetAddress(addr)
		host := na.Host()
		port := na.Port()
		expectedHost, expectedPort, err := net.SplitHostPort(addr)
		if err != nil {
			t.Fatal(err)
		}
		if host != expectedHost {
			t.Errorf("Host() returned unexpected host for NetAddress '%v': expected '%v', got '%v'", na, expectedHost, host)
		}
		if port != expectedPort {
			t.Errorf("Port() returned unexpected port for NetAddress '%v': expected '%v', got '%v'", na, expectedPort, port)
		}
122
	}
123 124 125 126 127 128 129 130 131 132

	// Test that Host / Port return "" when net.SplitHostPort errors
	na := NetAddress("::")
	host := na.Host()
	port := na.Port()
	if host != "" {
		t.Error("expected Host() to return blank for an un-splittable NetAddress, but it returned:", host)
	}
	if port != "" {
		t.Error("expected Port() to return blank for an un-splittable NetAddress, but it returned:", port)
133 134 135
	}
}

136 137
// TestIsLoopback tests the IsLoopback method of the NetAddress type.
func TestIsLoopback(t *testing.T) {
David Vorick's avatar
David Vorick committed
138 139
	t.Parallel()

140 141 142 143 144
	testSet := []struct {
		query           NetAddress
		desiredResponse bool
	}{
		// 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 151
		{"[::1]:7124", true},

152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
		// Local network tests.
		{"10.0.0.0", false},
		{"10.0.0.0:1234", false},
		{"10.2.2.5", false},
		{"10.2.2.5:16432", false},
		{"10.255.255.255", false},
		{"10.255.255.255:16432", false},
		{"172.16.0.0", false},
		{"172.16.0.0:1234", false},
		{"172.26.2.5", false},
		{"172.26.2.5:16432", false},
		{"172.31.255.255", false},
		{"172.31.255.255:16432", false},
		{"192.168.0.0", false},
		{"192.168.0.0:1234", false},
		{"192.168.2.5", false},
		{"192.168.2.5:16432", false},
		{"192.168.255.255", false},
		{"192.168.255.255:16432", false},
		{"1234:0000:0000:0000:0000:0000:0000:0000", false},
		{"[1234:0000:0000:0000:0000:0000:0000:0000]:1234", false},
		{"fc00:0000:0000:0000:0000:0000:0000:0000", false},
		{"[fc00:0000:0000:0000:0000:0000:0000:0000]:1234", false},
		{"fd00:0000:0000:0000:0000:0000:0000:0000", false},
		{"[fd00:0000:0000:0000:0000:0000:0000:0000]:1234", false},
		{"fd30:0000:0000:0000:0000:0000:0000:0000", false},
		{"[fd30:0000:0000:0000:0000:0000:0000:0000]:1234", false},
		{"fd00:0000:0030:0000:0000:0000:0000:0000", false},
		{"[fd00:0000:0030:0000:0000:0000:0000:0000]:1234", false},
		{"fdff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", false},
		{"[fdff:ffff:ffff:ffff:ffff:ffff:ffff:ffff]:1234", false},
		{"fe00:0000:0000:0000:0000:0000:0000:0000", false},
		{"[fe00:0000:0000:0000:0000:0000:0000:0000]:1234", false},

186 187 188 189
		// Unspecified address tests.
		{"0.0.0.0:1234", false},
		{"[::]:1234", false},

190 191 192
		// Public name tests.
		{"hn.com", false},
		{"hn.com:8811", false},
193 194
		{"2.34.45.64", false},
		{"2.34.45.64:7777", false},
195 196
		{"12.34.45.64", false},
		{"12.34.45.64:7777", false},
197 198 199 200 201 202
		{"122.34.45.64", false},
		{"122.34.45.64:7777", false},
		{"197.34.45.64", false},
		{"197.34.45.64:7777", false},
		{"222.34.45.64", false},
		{"222.34.45.64:7777", false},
203 204

		// Garbage name tests.
205
		{"", false},
206 207 208
		{"garbage", false},
		{"garbage:6432", false},
		{"garbage:6146:616", false},
Luke Champine's avatar
Luke Champine committed
209
		{"::1:4646", false},
210 211 212
		{"[::1]", false},
	}
	for _, test := range testSet {
213 214
		if test.query.IsLoopback() != test.desiredResponse {
			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) {
David Vorick's avatar
David Vorick committed
221 222
	t.Parallel()

223 224 225
	for _, addr := range validAddrs {
		na := NetAddress(addr)
		if err := na.IsValid(); err != nil {
226
			t.Errorf("IsValid returned non-nil for valid NetAddress %q: %v", addr, err)
227
		}
228
	}
229 230 231
	for _, addr := range invalidAddrs {
		na := NetAddress(addr)
		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 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319

// TestIsLocal checks that the correct values are returned for all local IP
// addresses.
func TestIsLocal(t *testing.T) {
	t.Parallel()

	testSet := []struct {
		query           NetAddress
		desiredResponse bool
	}{
		// Localhost tests.
		{"localhost", false},
		{"localhost:1234", true},
		{"127.0.0.1", false},
		{"127.0.0.1:6723", true},
		{"::1", false},
		{"[::1]:7124", true},

		// Local network tests.
		{"10.0.0.0", false},
		{"10.0.0.0:1234", true},
		{"10.2.2.5", false},
		{"10.2.2.5:16432", true},
		{"10.255.255.255", false},
		{"10.255.255.255:16432", true},
		{"172.16.0.0", false},
		{"172.16.0.0:1234", true},
		{"172.26.2.5", false},
		{"172.26.2.5:16432", true},
		{"172.31.255.255", false},
		{"172.31.255.255:16432", true},
		{"192.168.0.0", false},
		{"192.168.0.0:1234", true},
		{"192.168.2.5", false},
		{"192.168.2.5:16432", true},
		{"192.168.255.255", false},
		{"192.168.255.255:16432", true},
		{"1234:0000:0000:0000:0000:0000:0000:0000", false},
		{"[1234:0000:0000:0000:0000:0000:0000:0000]:1234", false},
		{"fc00:0000:0000:0000:0000:0000:0000:0000", false},
		{"[fc00:0000:0000:0000:0000:0000:0000:0000]:1234", false},
		{"fd00:0000:0000:0000:0000:0000:0000:0000", false},
		{"[fd00:0000:0000:0000:0000:0000:0000:0000]:1234", true},
		{"fd30:0000:0000:0000:0000:0000:0000:0000", false},
		{"[fd30:0000:0000:0000:0000:0000:0000:0000]:1234", true},
		{"fd00:0000:0030:0000:0000:0000:0000:0000", false},
		{"[fd00:0000:0030:0000:0000:0000:0000:0000]:1234", true},
		{"fdff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", false},
		{"[fdff:ffff:ffff:ffff:ffff:ffff:ffff:ffff]:1234", true},
		{"fe00:0000:0000:0000:0000:0000:0000:0000", false},
		{"[fe00:0000:0000:0000:0000:0000:0000:0000]:1234", false},

		// Unspecified address tests.
		{"0.0.0.0:1234", false},
		{"[::]:1234", false},

		// Public name tests.
		{"hn.com", false},
		{"hn.com:8811", false},
		{"2.34.45.64", false},
		{"2.34.45.64:7777", false},
		{"12.34.45.64", false},
		{"12.34.45.64:7777", false},
		{"122.34.45.64", false},
		{"122.34.45.64:7777", false},
		{"197.34.45.64", false},
		{"197.34.45.64:7777", false},
		{"222.34.45.64", false},
		{"222.34.45.64:7777", false},

		// Garbage name tests.
		{"", false},
		{"garbage", false},
		{"garbage:6432", false},
		{"garbage:6146:616", false},
		{"::1:4646", false},
		{"[::1]", false},
	}
	for _, test := range testSet {
		if test.query.IsLocal() != test.desiredResponse {
			t.Error("test failed:", test, test.query.IsLocal())
		}
	}
}