github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/opts/hosts_test.go (about) 1 package opts // import "github.com/demonoid81/moby/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:%d", DefaultHTTPPort), 25 "tcp://": DefaultTCPHost, 26 "tcp://:2375": fmt.Sprintf("tcp://%s:2375", DefaultHTTPHost), 27 "tcp://:2376": fmt.Sprintf("tcp://%s:2376", DefaultHTTPHost), 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, 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, 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 := parseDaemonHost(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 := parseDaemonHost(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:///var/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", "/var/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", "", "/var/run/docker.sock"); err != nil || v != "unix:///var/run/docker.sock" { 147 t.Fatalf("Expected an %v, got %v", v, "unix:///var/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 } 158 159 invalid := map[string]string{ 160 `myhost:192.notanipaddress.1`: `invalid IP`, 161 `thathost-nosemicolon10.0.0.1`: `bad format`, 162 `anipv6host:::::1`: `invalid IP`, 163 `ipv6local:::0::`: `invalid IP`, 164 } 165 166 for _, extrahost := range valid { 167 if _, err := ValidateExtraHost(extrahost); err != nil { 168 t.Fatalf("ValidateExtraHost(`"+extrahost+"`) should succeed: error %v", err) 169 } 170 } 171 172 for extraHost, expectedError := range invalid { 173 if _, err := ValidateExtraHost(extraHost); err == nil { 174 t.Fatalf("ValidateExtraHost(`%q`) should have failed validation", extraHost) 175 } else { 176 if !strings.Contains(err.Error(), expectedError) { 177 t.Fatalf("ValidateExtraHost(`%q`) error should contain %q", extraHost, expectedError) 178 } 179 } 180 } 181 }