Merge pull request #43510 from thaJeztah/daemon_fix_hosts_validation_step1b

opts: remove hacks for old go versions, and improve coverage
This commit is contained in:
Tianon Gravi 2022-04-21 12:33:57 -07:00 committed by GitHub
commit e78f6f9c68
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 112 additions and 72 deletions

View File

@ -132,13 +132,6 @@ func ParseTCPAddr(tryAddr string, defaultAddr string) (string, error) {
if err != nil { if err != nil {
return "", err return "", err
} }
// url.Parse fails for trailing colon on IPv6 brackets on Go 1.5, but
// not 1.4. See https://github.com/golang/go/issues/12200 and
// https://github.com/golang/go/issues/6530.
if strings.HasSuffix(addr, "]:") {
addr += defaultPort
}
u, err := url.Parse("tcp://" + addr) u, err := url.Parse("tcp://" + addr)
if err != nil { if err != nil {
return "", err return "", err

View File

@ -7,20 +7,20 @@ import (
) )
func TestParseHost(t *testing.T) { func TestParseHost(t *testing.T) {
invalid := []string{ invalid := map[string]string{
"something with spaces", "something with spaces": `parse "tcp://something with spaces": invalid character " " in host name`,
"://", "://": `Invalid bind address format: ://`,
"unknown://", "unknown://": `Invalid bind address format: unknown://`,
"tcp://:port", "tcp://:port": `parse "tcp://:port": invalid port ":port" after host`,
"tcp://invalid:port", "tcp://invalid:port": `parse "tcp://invalid:port": invalid port ":port" after host`,
"tcp://:5555/", "tcp://:5555/": `invalid bind address (:5555/): should not contain a path element`,
"tcp://:5555/p", "tcp://:5555/p": `invalid bind address (:5555/p): should not contain a path element`,
"tcp://0.0.0.0:5555/", "tcp://0.0.0.0:5555/": `invalid bind address (0.0.0.0:5555/): should not contain a path element`,
"tcp://0.0.0.0:5555/p", "tcp://0.0.0.0:5555/p": `invalid bind address (0.0.0.0:5555/p): should not contain a path element`,
"tcp://[::1]:/", "tcp://[::1]:/": `invalid bind address ([::1]:/): should not contain a path element`,
"tcp://[::1]:5555/", "tcp://[::1]:5555/": `invalid bind address ([::1]:5555/): should not contain a path element`,
"tcp://[::1]:5555/p", "tcp://[::1]:5555/p": `invalid bind address ([::1]:5555/p): should not contain a path element`,
" tcp://:5555/path ", " tcp://:5555/path ": `invalid bind address (:5555/path): should not contain a path element`,
} }
valid := map[string]string{ valid := map[string]string{
@ -31,41 +31,51 @@ func TestParseHost(t *testing.T) {
"fd://something": "fd://something", "fd://something": "fd://something",
"tcp://host:": fmt.Sprintf("tcp://host:%d", DefaultHTTPPort), "tcp://host:": fmt.Sprintf("tcp://host:%d", DefaultHTTPPort),
"tcp://": DefaultTCPHost, "tcp://": DefaultTCPHost,
"tcp://:2375": fmt.Sprintf("tcp://%s:2375", DefaultHTTPHost), "tcp://:": DefaultTCPHost,
"tcp://:2376": fmt.Sprintf("tcp://%s:2376", DefaultHTTPHost), "tcp://:5555": fmt.Sprintf("tcp://%s:5555", DefaultHTTPHost),
"tcp://0.0.0.0:8080": "tcp://0.0.0.0:8080", "tcp://[::1]:": fmt.Sprintf(`tcp://[::1]:%d`, DefaultHTTPPort),
"tcp://192.168.0.0:12000": "tcp://192.168.0.0:12000", "tcp://[::1]:5555": `tcp://[::1]:5555`,
"tcp://192.168:8080": "tcp://192.168:8080", "tcp://0.0.0.0:5555": "tcp://0.0.0.0:5555",
"tcp://192.168:5555": "tcp://192.168:5555",
"tcp://192.168.0.1:5555": "tcp://192.168.0.1:5555",
"tcp://0.0.0.0:1234567890": "tcp://0.0.0.0:1234567890", // yeah it's valid :P "tcp://0.0.0.0:1234567890": "tcp://0.0.0.0:1234567890", // yeah it's valid :P
"tcp://docker.com:2375": "tcp://docker.com:2375", "tcp://docker.com:5555": "tcp://docker.com:5555",
"unix://": "unix://" + DefaultUnixSocket, "unix://": "unix://" + DefaultUnixSocket,
"unix://path/to/socket": "unix://path/to/socket", "unix://path/to/socket": "unix://path/to/socket",
"npipe://": "npipe://" + DefaultNamedPipe, "npipe://": "npipe://" + DefaultNamedPipe,
"npipe:////./pipe/foo": "npipe:////./pipe/foo", "npipe:////./pipe/foo": "npipe:////./pipe/foo",
} }
for _, value := range invalid { for value, expectedError := range invalid {
if _, err := ParseHost(false, false, value); err == nil { t.Run(value, func(t *testing.T) {
t.Errorf("Expected an error for %v, got [nil]", value) _, err := ParseHost(false, false, value)
} if err == nil || err.Error() != expectedError {
t.Errorf(`expected error "%s", got "%v"`, expectedError, err)
}
})
} }
for value, expected := range valid { for value, expected := range valid {
if actual, err := ParseHost(false, false, value); err != nil || actual != expected { t.Run(value, func(t *testing.T) {
t.Errorf("Expected for %v [%v], got [%v, %v]", value, expected, actual, err) actual, err := ParseHost(false, false, value)
} if err != nil {
t.Errorf(`unexpected error: "%v"`, err)
}
if actual != expected {
t.Errorf(`expected "%s", got "%s""`, expected, actual)
}
})
} }
} }
func TestParseDockerDaemonHost(t *testing.T) { func TestParseDockerDaemonHost(t *testing.T) {
invalids := map[string]string{ invalids := map[string]string{
"tcp:a.b.c.d": `parse "tcp://tcp:a.b.c.d": invalid port ":a.b.c.d" after host`,
"tcp:a.b.c.d": "", "tcp:a.b.c.d/path": `parse "tcp://tcp:a.b.c.d/path": invalid port ":a.b.c.d" after host`,
"tcp:a.b.c.d/path": "",
"udp://127.0.0.1": "Invalid bind address format: udp://127.0.0.1", "udp://127.0.0.1": "Invalid bind address format: udp://127.0.0.1",
"udp://127.0.0.1:2375": "Invalid bind address format: udp://127.0.0.1:2375", "udp://127.0.0.1:5555": "Invalid bind address format: udp://127.0.0.1:5555",
"tcp://unix:///run/docker.sock": "Invalid proto, expected tcp: unix:///run/docker.sock", "tcp://unix:///run/docker.sock": "Invalid proto, expected tcp: unix:///run/docker.sock",
" tcp://:7777/path ": "Invalid bind address format: tcp://:7777/path ", " tcp://:5555/path ": "Invalid bind address format: tcp://:5555/path ",
"": "Invalid bind address format: ", "": "Invalid bind address format: ",
":5555/path": "invalid bind address (:5555/path): should not contain a path element", ":5555/path": "invalid bind address (:5555/path): should not contain a path element",
"0.0.0.1:5555/path": "invalid bind address (0.0.0.1:5555/path): should not contain a path element", "0.0.0.1:5555/path": "invalid bind address (0.0.0.1:5555/path): should not contain a path element",
@ -75,41 +85,61 @@ func TestParseDockerDaemonHost(t *testing.T) {
"localhost:5555/path": "invalid bind address (localhost:5555/path): should not contain a path element", "localhost:5555/path": "invalid bind address (localhost:5555/path): should not contain a path element",
} }
valids := map[string]string{ valids := map[string]string{
"0.0.0.1:": "tcp://0.0.0.1:2375", ":": DefaultTCPHost,
":5555": fmt.Sprintf("tcp://%s:5555", DefaultHTTPHost),
"0.0.0.1:": fmt.Sprintf("tcp://0.0.0.1:%d", DefaultHTTPPort),
"0.0.0.1:5555": "tcp://0.0.0.1:5555", "0.0.0.1:5555": "tcp://0.0.0.1:5555",
"[::1]:": "tcp://[::1]:2375", "[::1]:": fmt.Sprintf("tcp://[::1]:%d", DefaultHTTPPort),
"[0:0:0:0:0:0:0:1]:": "tcp://[0:0:0:0:0:0:0:1]:2375", "[::1]:5555": "tcp://[::1]:5555",
":6666": fmt.Sprintf("tcp://%s:6666", DefaultHTTPHost), "[0:0:0:0:0:0:0:1]:": fmt.Sprintf("tcp://[0:0:0:0:0:0:0:1]:%d", DefaultHTTPPort),
"tcp://": DefaultTCPHost, "[0:0:0:0:0:0:0:1]:5555": "tcp://[0:0:0:0:0:0:0:1]:5555",
"tcp://:7777": fmt.Sprintf("tcp://%s:7777", DefaultHTTPHost), "localhost": fmt.Sprintf("tcp://localhost:%d", DefaultHTTPPort),
"unix:///run/docker.sock": "unix:///run/docker.sock", "localhost:": fmt.Sprintf("tcp://localhost:%d", DefaultHTTPPort),
"unix://": "unix://" + DefaultUnixSocket, "localhost:5555": "tcp://localhost:5555",
"fd://": "fd://", "fd://": "fd://",
"fd://something": "fd://something", "fd://something": "fd://something",
"localhost:": "tcp://localhost:2375", "npipe://": "npipe://" + DefaultNamedPipe,
"localhost:5555": "tcp://localhost:5555", "npipe:////./pipe/foo": "npipe:////./pipe/foo",
"tcp://": DefaultTCPHost,
"tcp://:5555": fmt.Sprintf("tcp://%s:5555", DefaultHTTPHost),
"tcp://[::1]:": fmt.Sprintf("tcp://[::1]:%d", DefaultHTTPPort),
"tcp://[::1]:5555": "tcp://[::1]:5555",
"unix://": "unix://" + DefaultUnixSocket,
"unix:///run/docker.sock": "unix:///run/docker.sock",
} }
for invalidAddr, expectedError := range invalids { for invalidAddr, expectedError := range invalids {
if addr, err := parseDaemonHost(invalidAddr); err == nil || expectedError != "" && err.Error() != expectedError { t.Run(invalidAddr, func(t *testing.T) {
t.Errorf("tcp %v address expected error %q return, got %q and addr %v", invalidAddr, expectedError, err, addr) addr, err := parseDaemonHost(invalidAddr)
} if err == nil || err.Error() != expectedError {
t.Errorf(`expected error "%s", got "%v"`, expectedError, err)
}
if addr != "" {
t.Errorf(`expected addr to be empty, got "%s""`, addr)
}
})
} }
for validAddr, expectedAddr := range valids { for validAddr, expectedAddr := range valids {
if addr, err := parseDaemonHost(validAddr); err != nil || addr != expectedAddr { t.Run(validAddr, func(t *testing.T) {
t.Errorf("%v -> expected %v, got (%v) addr (%v)", validAddr, expectedAddr, err, addr) addr, err := parseDaemonHost(validAddr)
} if err != nil {
t.Errorf(`unexpected error: "%v"`, err)
}
if addr != expectedAddr {
t.Errorf(`expected "%s", got "%s""`, expectedAddr, addr)
}
})
} }
} }
func TestParseTCP(t *testing.T) { func TestParseTCP(t *testing.T) {
var ( var (
defaultHTTPHost = "tcp://127.0.0.1:2376" defaultHTTPHost = "tcp://127.0.0.1:8888"
) )
invalids := map[string]string{ invalids := map[string]string{
"tcp:a.b.c.d": "", "tcp:a.b.c.d": `parse "tcp://tcp:a.b.c.d": invalid port ":a.b.c.d" after host`,
"tcp:a.b.c.d/path": "", "tcp:a.b.c.d/path": `parse "tcp://tcp:a.b.c.d/path": invalid port ":a.b.c.d" after host`,
"udp://127.0.0.1": "Invalid proto, expected tcp: udp://127.0.0.1", "udp://127.0.0.1": "Invalid proto, expected tcp: udp://127.0.0.1",
"udp://127.0.0.1:2375": "Invalid proto, expected tcp: udp://127.0.0.1:2375", "udp://127.0.0.1:5555": "Invalid proto, expected tcp: udp://127.0.0.1:5555",
":5555/path": "invalid bind address (:5555/path): should not contain a path element", ":5555/path": "invalid bind address (:5555/path): should not contain a path element",
"0.0.0.1:5555/path": "invalid bind address (0.0.0.1:5555/path): should not contain a path element", "0.0.0.1:5555/path": "invalid bind address (0.0.0.1:5555/path): should not contain a path element",
"[::1]:5555/path": "invalid bind address ([::1]:5555/path): should not contain a path element", "[::1]:5555/path": "invalid bind address ([::1]:5555/path): should not contain a path element",
@ -119,27 +149,44 @@ func TestParseTCP(t *testing.T) {
} }
valids := map[string]string{ valids := map[string]string{
"": defaultHTTPHost, "": defaultHTTPHost,
"tcp://": defaultHTTPHost, "0.0.0.1": "tcp://0.0.0.1:8888",
"0.0.0.1:": "tcp://0.0.0.1:2376", "0.0.0.1:": "tcp://0.0.0.1:8888",
"0.0.0.1:5555": "tcp://0.0.0.1:5555", "0.0.0.1:5555": "tcp://0.0.0.1:5555",
":6666": "tcp://127.0.0.1:6666", ":": "tcp://127.0.0.1:8888",
"tcp://:7777": "tcp://127.0.0.1:7777", ":5555": "tcp://127.0.0.1:5555",
"[::1]:": "tcp://[::1]:2376", "::1": "tcp://[::1]:8888",
"[::1]:": "tcp://[::1]:8888",
"[::1]:5555": "tcp://[::1]:5555", "[::1]:5555": "tcp://[::1]:5555",
"[0:0:0:0:0:0:0:1]:": "tcp://[0:0:0:0:0:0:0:1]:2376", "[0:0:0:0:0:0:0:1]:": "tcp://[0:0:0:0:0:0:0:1]:8888",
"[0:0:0:0:0:0:0:1]:5555": "tcp://[0:0:0:0:0:0:0:1]:5555", "[0:0:0:0:0:0:0:1]:5555": "tcp://[0:0:0:0:0:0:0:1]:5555",
"localhost:": "tcp://localhost:2376", "localhost": "tcp://localhost:8888",
"localhost:": "tcp://localhost:8888",
"localhost:5555": "tcp://localhost:5555", "localhost:5555": "tcp://localhost:5555",
"tcp://": defaultHTTPHost,
"tcp://:": defaultHTTPHost,
"tcp://:5555": "tcp://127.0.0.1:5555",
} }
for invalidAddr, expectedError := range invalids { for invalidAddr, expectedError := range invalids {
if addr, err := ParseTCPAddr(invalidAddr, defaultHTTPHost); err == nil || expectedError != "" && err.Error() != expectedError { t.Run(invalidAddr, func(t *testing.T) {
t.Errorf("tcp %v address expected error %v return, got %s and addr %v", invalidAddr, expectedError, err, addr) addr, err := ParseTCPAddr(invalidAddr, defaultHTTPHost)
} if err == nil || err.Error() != expectedError {
t.Errorf(`expected error "%s", got "%v"`, expectedError, err)
}
if addr != "" {
t.Errorf(`expected addr to be empty, got "%s""`, addr)
}
})
} }
for validAddr, expectedAddr := range valids { for validAddr, expectedAddr := range valids {
if addr, err := ParseTCPAddr(validAddr, defaultHTTPHost); err != nil || addr != expectedAddr { t.Run(validAddr, func(t *testing.T) {
t.Errorf("%v -> expected %v, got %v and addr %v", validAddr, expectedAddr, err, addr) addr, err := ParseTCPAddr(validAddr, defaultHTTPHost)
} if err != nil {
t.Errorf(`unexpected error: "%v"`, err)
}
if addr != expectedAddr {
t.Errorf(`expected "%s", got "%s""`, expectedAddr, addr)
}
})
} }
} }