github.com/rish1988/moby@v25.0.2+incompatible/opts/hosts_test.go (about) 1 package opts // import "github.com/docker/docker/opts" 2 3 import ( 4 "fmt" 5 "strings" 6 "testing" 7 ) 8 9 func TestParseHost(t *testing.T) { 10 invalid := map[string]string{ 11 "something with spaces": `invalid bind address (something with spaces): parse "tcp://something with spaces": invalid character " " in host name`, 12 "://": `invalid bind address (://): unsupported proto ''`, 13 "unknown://": `invalid bind address (unknown://): unsupported proto 'unknown'`, 14 "tcp://:port": `invalid bind address (tcp://:port): parse "tcp://:port": invalid port ":port" after host`, 15 "tcp://invalid:port": `invalid bind address (tcp://invalid:port): parse "tcp://invalid:port": invalid port ":port" after host`, 16 "tcp://:5555/": `invalid bind address (tcp://:5555/): should not contain a path element`, 17 "tcp://:5555/p": `invalid bind address (tcp://:5555/p): should not contain a path element`, 18 "tcp://0.0.0.0:5555/": `invalid bind address (tcp://0.0.0.0:5555/): should not contain a path element`, 19 "tcp://0.0.0.0:5555/p": `invalid bind address (tcp://0.0.0.0:5555/p): should not contain a path element`, 20 "tcp://[::1]:/": `invalid bind address (tcp://[::1]:/): should not contain a path element`, 21 "tcp://[::1]:5555/": `invalid bind address (tcp://[::1]:5555/): should not contain a path element`, 22 "tcp://[::1]:5555/p": `invalid bind address (tcp://[::1]:5555/p): should not contain a path element`, 23 " tcp://:5555/path ": `invalid bind address (tcp://:5555/path): should not contain a path element`, 24 } 25 26 valid := map[string]string{ 27 "": DefaultHost, 28 " ": DefaultHost, 29 " ": DefaultHost, 30 "fd://": "fd://", 31 "fd://something": "fd://something", 32 "tcp://host:": fmt.Sprintf("tcp://host:%d", DefaultHTTPPort), 33 "tcp://": DefaultTCPHost, 34 "tcp://:": DefaultTCPHost, 35 "tcp://:5555": fmt.Sprintf("tcp://%s:5555", DefaultHTTPHost), //nolint:nosprintfhostport // sprintf is more readable for this case. 36 "tcp://[::1]": fmt.Sprintf(`tcp://[::1]:%d`, DefaultHTTPPort), 37 "tcp://[::1]:": fmt.Sprintf(`tcp://[::1]:%d`, DefaultHTTPPort), 38 "tcp://[::1]:5555": `tcp://[::1]:5555`, 39 "tcp://0.0.0.0:5555": "tcp://0.0.0.0:5555", 40 "tcp://192.168:5555": "tcp://192.168:5555", 41 "tcp://192.168.0.1:5555": "tcp://192.168.0.1:5555", 42 "tcp://0.0.0.0:1234567890": "tcp://0.0.0.0:1234567890", // yeah it's valid :P 43 "tcp://docker.com:5555": "tcp://docker.com:5555", 44 "unix://": "unix://" + DefaultUnixSocket, 45 "unix://path/to/socket": "unix://path/to/socket", 46 "npipe://": "npipe://" + DefaultNamedPipe, 47 "npipe:////./pipe/foo": "npipe:////./pipe/foo", 48 } 49 50 for value, expectedError := range invalid { 51 t.Run(value, func(t *testing.T) { 52 _, err := ParseHost(false, false, value) 53 if err == nil || err.Error() != expectedError { 54 t.Errorf(`expected error "%s", got "%v"`, expectedError, err) 55 } 56 }) 57 } 58 59 for value, expected := range valid { 60 t.Run(value, func(t *testing.T) { 61 actual, err := ParseHost(false, false, value) 62 if err != nil { 63 t.Errorf(`unexpected error: "%v"`, err) 64 } 65 if actual != expected { 66 t.Errorf(`expected "%s", got "%s""`, expected, actual) 67 } 68 }) 69 } 70 } 71 72 func TestParseDockerDaemonHost(t *testing.T) { 73 invalids := map[string]string{ 74 "tcp:a.b.c.d": `invalid bind address (tcp:a.b.c.d): parse "tcp://tcp:a.b.c.d": invalid port ":a.b.c.d" after host`, 75 "tcp:a.b.c.d/path": `invalid bind address (tcp:a.b.c.d/path): parse "tcp://tcp:a.b.c.d/path": invalid port ":a.b.c.d" after host`, 76 "tcp://127.0.0.1/": "invalid bind address (tcp://127.0.0.1/): should not contain a path element", 77 "udp://127.0.0.1": "invalid bind address (udp://127.0.0.1): unsupported proto 'udp'", 78 "udp://127.0.0.1:5555": "invalid bind address (udp://127.0.0.1:5555): unsupported proto 'udp'", 79 "tcp://unix:///run/docker.sock": "invalid bind address (tcp://unix:///run/docker.sock): should not contain a path element", 80 " tcp://:5555/path ": "invalid bind address ( tcp://:5555/path ): unsupported proto ' tcp'", 81 "": "invalid bind address (): unsupported proto ''", 82 ":5555/path": "invalid bind address (:5555/path): should not contain a path element", 83 "0.0.0.1:5555/path": "invalid bind address (0.0.0.1:5555/path): should not contain a path element", 84 "[::1]:5555/path": "invalid bind address ([::1]:5555/path): should not contain a path element", 85 "[0:0:0:0:0:0:0:1]:5555/path": "invalid bind address ([0:0:0:0:0:0:0:1]:5555/path): should not contain a path element", 86 "tcp://:5555/path": "invalid bind address (tcp://:5555/path): should not contain a path element", 87 "localhost:5555/path": "invalid bind address (localhost:5555/path): should not contain a path element", 88 "unix://tcp://127.0.0.1": "invalid bind address (unix://tcp://127.0.0.1): invalid unix address: tcp://127.0.0.1", 89 "unix://unix://tcp://127.0.0.1": "invalid bind address (unix://unix://tcp://127.0.0.1): invalid unix address: unix://tcp://127.0.0.1", 90 } 91 valids := map[string]string{ 92 ":": DefaultTCPHost, 93 ":5555": fmt.Sprintf("tcp://%s:5555", DefaultHTTPHost), //nolint:nosprintfhostport // sprintf is more readable for this case. 94 "0.0.0.1:": fmt.Sprintf("tcp://0.0.0.1:%d", DefaultHTTPPort), 95 "0.0.0.1:5555": "tcp://0.0.0.1:5555", 96 "[::1]": fmt.Sprintf("tcp://[::1]:%d", DefaultHTTPPort), 97 "[::1]:": fmt.Sprintf("tcp://[::1]:%d", DefaultHTTPPort), 98 "[::1]:5555": "tcp://[::1]:5555", 99 "[0:0:0:0:0:0:0:1]": fmt.Sprintf("tcp://[0:0:0:0:0:0:0:1]:%d", DefaultHTTPPort), 100 "[0:0:0:0:0:0:0:1]:": fmt.Sprintf("tcp://[0:0:0:0:0:0:0:1]:%d", DefaultHTTPPort), 101 "[0:0:0:0:0:0:0:1]:5555": "tcp://[0:0:0:0:0:0:0:1]:5555", 102 "localhost": fmt.Sprintf("tcp://localhost:%d", DefaultHTTPPort), 103 "localhost:": fmt.Sprintf("tcp://localhost:%d", DefaultHTTPPort), 104 "localhost:5555": "tcp://localhost:5555", 105 "fd://": "fd://", 106 "fd://something": "fd://something", 107 "npipe://": "npipe://" + DefaultNamedPipe, 108 "npipe:////./pipe/foo": "npipe:////./pipe/foo", 109 "tcp://": DefaultTCPHost, 110 "tcp://:5555": fmt.Sprintf("tcp://%s:5555", DefaultHTTPHost), 111 "tcp://[::1]": fmt.Sprintf("tcp://[::1]:%d", DefaultHTTPPort), 112 "tcp://[::1]:": fmt.Sprintf("tcp://[::1]:%d", DefaultHTTPPort), 113 "tcp://[::1]:5555": "tcp://[::1]:5555", 114 "unix://": "unix://" + DefaultUnixSocket, 115 "unix:///run/docker.sock": "unix:///run/docker.sock", 116 } 117 for invalidAddr, expectedError := range invalids { 118 t.Run(invalidAddr, func(t *testing.T) { 119 addr, err := parseDaemonHost(invalidAddr) 120 if err == nil || err.Error() != expectedError { 121 t.Errorf(`expected error "%s", got "%v"`, expectedError, err) 122 } 123 if addr != "" { 124 t.Errorf(`expected addr to be empty, got "%s""`, addr) 125 } 126 }) 127 } 128 for validAddr, expectedAddr := range valids { 129 t.Run(validAddr, func(t *testing.T) { 130 addr, err := parseDaemonHost(validAddr) 131 if err != nil { 132 t.Errorf(`unexpected error: "%v"`, err) 133 } 134 if addr != expectedAddr { 135 t.Errorf(`expected "%s", got "%s"`, expectedAddr, addr) 136 } 137 }) 138 } 139 } 140 141 func TestParseTCP(t *testing.T) { 142 defaultHTTPHost := "tcp://127.0.0.1:8888" 143 invalids := map[string]string{ 144 "tcp:a.b.c.d": `invalid bind address (tcp:a.b.c.d): parse "tcp://tcp:a.b.c.d": invalid port ":a.b.c.d" after host`, 145 "tcp:a.b.c.d/path": `invalid bind address (tcp:a.b.c.d/path): parse "tcp://tcp:a.b.c.d/path": invalid port ":a.b.c.d" after host`, 146 "udp://127.0.0.1": "invalid bind address (udp://127.0.0.1): unsupported proto 'udp'", 147 "udp://127.0.0.1:5555": "invalid bind address (udp://127.0.0.1:5555): unsupported proto 'udp'", 148 ":5555/path": "invalid bind address (:5555/path): should not contain a path element", 149 "0.0.0.1:5555/path": "invalid bind address (0.0.0.1:5555/path): should not contain a path element", 150 "[::1]:5555/path": "invalid bind address ([::1]:5555/path): should not contain a path element", 151 "[0:0:0:0:0:0:0:1]:5555/path": "invalid bind address ([0:0:0:0:0:0:0:1]:5555/path): should not contain a path element", 152 "tcp://:5555/path": "invalid bind address (tcp://:5555/path): should not contain a path element", 153 "localhost:5555/path": "invalid bind address (localhost:5555/path): should not contain a path element", 154 } 155 invalidDefaults := map[string]string{ 156 "localhost": `invalid default address (localhost): unsupported proto ''`, 157 "udp://localhost": `invalid default address (udp://localhost): unsupported proto 'udp'`, 158 "tcp://localhost:noport": `invalid default address (tcp://localhost:noport): parse "tcp://localhost:noport": invalid port ":noport" after host`, 159 "tcp://localhost:5555/path": `invalid default address (tcp://localhost:5555/path): should not contain a path element`, 160 } 161 valids := map[string]string{ 162 "": defaultHTTPHost, 163 "0.0.0.1": "tcp://0.0.0.1:8888", 164 "0.0.0.1:": "tcp://0.0.0.1:8888", 165 "0.0.0.1:5555": "tcp://0.0.0.1:5555", 166 ":": "tcp://127.0.0.1:8888", 167 ":5555": "tcp://127.0.0.1:5555", 168 "[::1]": "tcp://[::1]:8888", 169 "[::1]:": "tcp://[::1]:8888", 170 "[::1]:5555": "tcp://[::1]:5555", 171 "[0:0:0:0:0:0:0:1]": "tcp://[0:0:0:0:0:0:0:1]:8888", 172 "[0:0:0:0:0:0:0:1]:": "tcp://[0:0:0:0:0:0:0:1]:8888", 173 "[0:0:0:0:0:0:0:1]:5555": "tcp://[0:0:0:0:0:0:0:1]:5555", 174 "localhost": "tcp://localhost:8888", 175 "localhost:": "tcp://localhost:8888", 176 "localhost:5555": "tcp://localhost:5555", 177 "tcp://": defaultHTTPHost, 178 "tcp://:": defaultHTTPHost, 179 "tcp://:5555": "tcp://127.0.0.1:5555", 180 } 181 for invalidAddr, expectedError := range invalids { 182 t.Run(invalidAddr, func(t *testing.T) { 183 addr, err := ParseTCPAddr(invalidAddr, defaultHTTPHost) 184 if err == nil || err.Error() != expectedError { 185 t.Errorf(`expected error "%s", got "%v"`, expectedError, err) 186 } 187 if addr != "" { 188 t.Errorf(`expected addr to be empty, got "%s""`, addr) 189 } 190 }) 191 } 192 for invalidAddr, expectedError := range invalidDefaults { 193 t.Run("default "+invalidAddr, func(t *testing.T) { 194 _, err := ParseTCPAddr("tcp://0.0.0.0:2375", invalidAddr) 195 if err == nil || err.Error() != expectedError { 196 t.Errorf(`expected error "%s", got "%v"`, expectedError, err) 197 } 198 }) 199 } 200 for validAddr, expectedAddr := range valids { 201 t.Run(validAddr, func(t *testing.T) { 202 addr, err := ParseTCPAddr(validAddr, defaultHTTPHost) 203 if err != nil { 204 t.Errorf(`unexpected error: "%v"`, err) 205 } 206 if addr != expectedAddr { 207 t.Errorf(`expected "%s", got "%s""`, expectedAddr, addr) 208 } 209 }) 210 } 211 } 212 213 func TestValidateExtraHosts(t *testing.T) { 214 valid := []string{ 215 `myhost:192.168.0.1`, 216 `thathost:10.0.2.1`, 217 `anipv6host:2003:ab34:e::1`, 218 `ipv6local:::1`, 219 } 220 221 invalid := map[string]string{ 222 `myhost:192.notanipaddress.1`: `invalid IP`, 223 `thathost-nosemicolon10.0.0.1`: `bad format`, 224 `anipv6host:::::1`: `invalid IP`, 225 `ipv6local:::0::`: `invalid IP`, 226 } 227 228 for _, extrahost := range valid { 229 if _, err := ValidateExtraHost(extrahost); err != nil { 230 t.Fatalf("ValidateExtraHost(`"+extrahost+"`) should succeed: error %v", err) 231 } 232 } 233 234 for extraHost, expectedError := range invalid { 235 if _, err := ValidateExtraHost(extraHost); err == nil { 236 t.Fatalf("ValidateExtraHost(`%q`) should have failed validation", extraHost) 237 } else { 238 if !strings.Contains(err.Error(), expectedError) { 239 t.Fatalf("ValidateExtraHost(`%q`) error should contain %q", extraHost, expectedError) 240 } 241 } 242 } 243 }