github.com/pwn-term/docker@v0.0.0-20210616085119-6e977cce2565/cli/opts/hosts_test.go (about) 1 package opts 2 3 import ( 4 "fmt" 5 "strings" 6 "testing" 7 ) 8 9 func TestParseHost(t *testing.T) { 10 invalid := []string{ 11 "something with spaces", 12 "://", 13 "unknown://", 14 "tcp://:port", 15 "tcp://invalid:port", 16 } 17 18 valid := map[string]string{ 19 "": defaultHost, 20 " ": defaultHost, 21 " ": defaultHost, 22 "fd://": "fd://", 23 "fd://something": "fd://something", 24 "tcp://host:": fmt.Sprintf("tcp://host:%s", defaultHTTPPort), 25 "tcp://": defaultTCPHost, 26 "tcp://:2375": fmt.Sprintf("tcp://%s:%s", defaultHTTPHost, defaultHTTPPort), 27 "tcp://:2376": fmt.Sprintf("tcp://%s:%s", defaultHTTPHost, defaultTLSHTTPPort), 28 "tcp://0.0.0.0:8080": "tcp://0.0.0.0:8080", 29 "tcp://192.168.0.0:12000": "tcp://192.168.0.0:12000", 30 "tcp://192.168:8080": "tcp://192.168:8080", 31 "tcp://0.0.0.0:1234567890": "tcp://0.0.0.0:1234567890", // yeah it's valid :P 32 " tcp://:7777/path ": fmt.Sprintf("tcp://%s:7777/path", defaultHTTPHost), 33 "tcp://docker.com:2375": "tcp://docker.com:2375", 34 "unix://": "unix://" + defaultUnixSocket, 35 "unix://path/to/socket": "unix://path/to/socket", 36 "npipe://": "npipe://" + defaultNamedPipe, 37 "npipe:////./pipe/foo": "npipe:////./pipe/foo", 38 } 39 40 for _, value := range invalid { 41 if _, err := ParseHost(false, value); err == nil { 42 t.Errorf("Expected an error for %v, got [nil]", value) 43 } 44 } 45 46 for value, expected := range valid { 47 if actual, err := ParseHost(false, value); err != nil || actual != expected { 48 t.Errorf("Expected for %v [%v], got [%v, %v]", value, expected, actual, err) 49 } 50 } 51 } 52 53 func TestParseDockerDaemonHost(t *testing.T) { 54 invalids := map[string]string{ 55 56 "tcp:a.b.c.d": "", 57 "tcp:a.b.c.d/path": "", 58 "udp://127.0.0.1": "Invalid bind address format: udp://127.0.0.1", 59 "udp://127.0.0.1:2375": "Invalid bind address format: udp://127.0.0.1:2375", 60 "tcp://unix:///run/docker.sock": "Invalid proto, expected tcp: unix:///run/docker.sock", 61 " tcp://:7777/path ": "Invalid bind address format: tcp://:7777/path ", 62 "": "Invalid bind address format: ", 63 } 64 valids := map[string]string{ 65 "0.0.0.1:": "tcp://0.0.0.1:2375", 66 "0.0.0.1:5555": "tcp://0.0.0.1:5555", 67 "0.0.0.1:5555/path": "tcp://0.0.0.1:5555/path", 68 "[::1]:": "tcp://[::1]:2375", 69 "[::1]:5555/path": "tcp://[::1]:5555/path", 70 "[0:0:0:0:0:0:0:1]:": "tcp://[0:0:0:0:0:0:0:1]:2375", 71 "[0:0:0:0:0:0:0:1]:5555/path": "tcp://[0:0:0:0:0:0:0:1]:5555/path", 72 ":6666": fmt.Sprintf("tcp://%s:6666", defaultHTTPHost), 73 ":6666/path": fmt.Sprintf("tcp://%s:6666/path", defaultHTTPHost), 74 "tcp://": defaultTCPHost, 75 "tcp://:7777": fmt.Sprintf("tcp://%s:7777", defaultHTTPHost), 76 "tcp://:7777/path": fmt.Sprintf("tcp://%s:7777/path", defaultHTTPHost), 77 "unix:///run/docker.sock": "unix:///run/docker.sock", 78 "unix://": "unix://" + defaultUnixSocket, 79 "fd://": "fd://", 80 "fd://something": "fd://something", 81 "localhost:": "tcp://localhost:2375", 82 "localhost:5555": "tcp://localhost:5555", 83 "localhost:5555/path": "tcp://localhost:5555/path", 84 } 85 for invalidAddr, expectedError := range invalids { 86 if addr, err := parseDockerDaemonHost(invalidAddr); err == nil || expectedError != "" && err.Error() != expectedError { 87 t.Errorf("tcp %v address expected error %q return, got %q and addr %v", invalidAddr, expectedError, err, addr) 88 } 89 } 90 for validAddr, expectedAddr := range valids { 91 if addr, err := parseDockerDaemonHost(validAddr); err != nil || addr != expectedAddr { 92 t.Errorf("%v -> expected %v, got (%v) addr (%v)", validAddr, expectedAddr, err, addr) 93 } 94 } 95 } 96 97 func TestParseTCP(t *testing.T) { 98 var ( 99 defaultHTTPHost = "tcp://127.0.0.1:2376" 100 ) 101 invalids := map[string]string{ 102 "tcp:a.b.c.d": "", 103 "tcp:a.b.c.d/path": "", 104 "udp://127.0.0.1": "Invalid proto, expected tcp: udp://127.0.0.1", 105 "udp://127.0.0.1:2375": "Invalid proto, expected tcp: udp://127.0.0.1:2375", 106 } 107 valids := map[string]string{ 108 "": defaultHTTPHost, 109 "tcp://": defaultHTTPHost, 110 "0.0.0.1:": "tcp://0.0.0.1:2376", 111 "0.0.0.1:5555": "tcp://0.0.0.1:5555", 112 "0.0.0.1:5555/path": "tcp://0.0.0.1:5555/path", 113 ":6666": "tcp://127.0.0.1:6666", 114 ":6666/path": "tcp://127.0.0.1:6666/path", 115 "tcp://:7777": "tcp://127.0.0.1:7777", 116 "tcp://:7777/path": "tcp://127.0.0.1:7777/path", 117 "[::1]:": "tcp://[::1]:2376", 118 "[::1]:5555": "tcp://[::1]:5555", 119 "[::1]:5555/path": "tcp://[::1]:5555/path", 120 "[0:0:0:0:0:0:0:1]:": "tcp://[0:0:0:0:0:0:0:1]:2376", 121 "[0:0:0:0:0:0:0:1]:5555": "tcp://[0:0:0:0:0:0:0:1]:5555", 122 "[0:0:0:0:0:0:0:1]:5555/path": "tcp://[0:0:0:0:0:0:0:1]:5555/path", 123 "localhost:": "tcp://localhost:2376", 124 "localhost:5555": "tcp://localhost:5555", 125 "localhost:5555/path": "tcp://localhost:5555/path", 126 } 127 for invalidAddr, expectedError := range invalids { 128 if addr, err := ParseTCPAddr(invalidAddr, defaultHTTPHost); err == nil || expectedError != "" && err.Error() != expectedError { 129 t.Errorf("tcp %v address expected error %v return, got %s and addr %v", invalidAddr, expectedError, err, addr) 130 } 131 } 132 for validAddr, expectedAddr := range valids { 133 if addr, err := ParseTCPAddr(validAddr, defaultHTTPHost); err != nil || addr != expectedAddr { 134 t.Errorf("%v -> expected %v, got %v and addr %v", validAddr, expectedAddr, err, addr) 135 } 136 } 137 } 138 139 func TestParseInvalidUnixAddrInvalid(t *testing.T) { 140 if _, err := parseSimpleProtoAddr("unix", "tcp://127.0.0.1", "unix:///data/docker/run/docker.sock"); err == nil || err.Error() != "Invalid proto, expected unix: tcp://127.0.0.1" { 141 t.Fatalf("Expected an error, got %v", err) 142 } 143 if _, err := parseSimpleProtoAddr("unix", "unix://tcp://127.0.0.1", "/data/docker/run/docker.sock"); err == nil || err.Error() != "Invalid proto, expected unix: tcp://127.0.0.1" { 144 t.Fatalf("Expected an error, got %v", err) 145 } 146 if v, err := parseSimpleProtoAddr("unix", "", "/data/docker/run/docker.sock"); err != nil || v != "unix:///data/docker/run/docker.sock" { 147 t.Fatalf("Expected an %v, got %v", v, "unix:///data/docker/run/docker.sock") 148 } 149 } 150 151 func TestValidateExtraHosts(t *testing.T) { 152 valid := []string{ 153 `myhost:192.168.0.1`, 154 `thathost:10.0.2.1`, 155 `anipv6host:2003:ab34:e::1`, 156 `ipv6local:::1`, 157 `host.docker.internal:host-gateway`, 158 } 159 160 invalid := map[string]string{ 161 `myhost:192.notanipaddress.1`: `invalid IP`, 162 `thathost-nosemicolon10.0.0.1`: `bad format`, 163 `anipv6host:::::1`: `invalid IP`, 164 `ipv6local:::0::`: `invalid IP`, 165 } 166 167 for _, extrahost := range valid { 168 if _, err := ValidateExtraHost(extrahost); err != nil { 169 t.Fatalf("ValidateExtraHost(`"+extrahost+"`) should succeed: error %v", err) 170 } 171 } 172 173 for extraHost, expectedError := range invalid { 174 if _, err := ValidateExtraHost(extraHost); err == nil { 175 t.Fatalf("ValidateExtraHost(`%q`) should have failed validation", extraHost) 176 } else { 177 if !strings.Contains(err.Error(), expectedError) { 178 t.Fatalf("ValidateExtraHost(`%q`) error should contain %q", extraHost, expectedError) 179 } 180 } 181 } 182 }