github.com/johnathanhowell/sia@v0.5.1-beta.0.20160524050156-83dcc3d37c94/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 // Networks such as 10.0.0.x have been omitted from testing - behavior 145 // for these networks is currently undefined. 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 // Unspecified address tests. 156 {"0.0.0.0:1234", false}, 157 {"[::]:1234", false}, 158 159 // Public name tests. 160 {"hn.com", false}, 161 {"hn.com:8811", false}, 162 {"12.34.45.64", false}, 163 {"12.34.45.64:7777", false}, 164 165 // Garbage name tests. 166 {"", false}, 167 {"garbage", false}, 168 {"garbage:6432", false}, 169 {"garbage:6146:616", false}, 170 {"::1:4646", false}, 171 {"[::1]", false}, 172 } 173 for _, test := range testSet { 174 if test.query.isLoopback() != test.desiredResponse { 175 t.Error("test failed:", test, test.query.isLoopback()) 176 } 177 } 178 } 179 180 // TestIsValid tests that IsValid only returns nil for valid addresses. 181 func TestIsValid(t *testing.T) { 182 t.Parallel() 183 184 for _, addr := range validAddrs { 185 na := NetAddress(addr) 186 if err := na.IsValid(); err != nil { 187 t.Errorf("IsValid returned non-nil for valid NetAddress %q: %v", addr, err) 188 } 189 } 190 for _, addr := range invalidAddrs { 191 na := NetAddress(addr) 192 if err := na.IsValid(); err == nil { 193 t.Errorf("IsValid returned nil for an invalid NetAddress %q: %v", addr, err) 194 } 195 } 196 }