github.com/zhuohuang-hust/src-cbuild@v0.0.0-20230105071821-c7aab3e7c840/opts/hosts_test.go (about)

     1  package opts
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  )
     7  
     8  func TestParseHost(t *testing.T) {
     9  	invalid := []string{
    10  		"something with spaces",
    11  		"://",
    12  		"unknown://",
    13  		"tcp://:port",
    14  		"tcp://invalid:port",
    15  	}
    16  
    17  	valid := map[string]string{
    18  		"":                         DefaultHost,
    19  		" ":                        DefaultHost,
    20  		"  ":                       DefaultHost,
    21  		"fd://":                    "fd://",
    22  		"fd://something":           "fd://something",
    23  		"tcp://host:":              fmt.Sprintf("tcp://host:%d", DefaultHTTPPort),
    24  		"tcp://":                   DefaultTCPHost,
    25  		"tcp://:2375":              fmt.Sprintf("tcp://%s:2375", DefaultHTTPHost),
    26  		"tcp://:2376":              fmt.Sprintf("tcp://%s:2376", DefaultHTTPHost),
    27  		"tcp://0.0.0.0:8080":       "tcp://0.0.0.0:8080",
    28  		"tcp://192.168.0.0:12000":  "tcp://192.168.0.0:12000",
    29  		"tcp://192.168:8080":       "tcp://192.168:8080",
    30  		"tcp://0.0.0.0:1234567890": "tcp://0.0.0.0:1234567890", // yeah it's valid :P
    31  		" tcp://:7777/path ":       fmt.Sprintf("tcp://%s:7777/path", DefaultHTTPHost),
    32  		"tcp://docker.com:2375":    "tcp://docker.com:2375",
    33  		"unix://":                  "unix://" + DefaultUnixSocket,
    34  		"unix://path/to/socket":    "unix://path/to/socket",
    35  		"npipe://":                 "npipe://" + DefaultNamedPipe,
    36  		"npipe:////./pipe/foo":     "npipe:////./pipe/foo",
    37  	}
    38  
    39  	for _, value := range invalid {
    40  		if _, err := ParseHost(false, value); err == nil {
    41  			t.Errorf("Expected an error for %v, got [nil]", value)
    42  		}
    43  	}
    44  
    45  	for value, expected := range valid {
    46  		if actual, err := ParseHost(false, value); err != nil || actual != expected {
    47  			t.Errorf("Expected for %v [%v], got [%v, %v]", value, expected, actual, err)
    48  		}
    49  	}
    50  }
    51  
    52  func TestParseDockerDaemonHost(t *testing.T) {
    53  	invalids := map[string]string{
    54  
    55  		"tcp:a.b.c.d":                   "Invalid bind address format: tcp:a.b.c.d",
    56  		"tcp:a.b.c.d/path":              "Invalid bind address format: tcp:a.b.c.d/path",
    57  		"udp://127.0.0.1":               "Invalid bind address format: udp://127.0.0.1",
    58  		"udp://127.0.0.1:2375":          "Invalid bind address format: udp://127.0.0.1:2375",
    59  		"tcp://unix:///run/docker.sock": "Invalid proto, expected tcp: unix:///run/docker.sock",
    60  		" tcp://:7777/path ":            "Invalid bind address format:  tcp://:7777/path ",
    61  		"":                              "Invalid bind address format: ",
    62  	}
    63  	valids := map[string]string{
    64  		"0.0.0.1:":                    "tcp://0.0.0.1:2375",
    65  		"0.0.0.1:5555":                "tcp://0.0.0.1:5555",
    66  		"0.0.0.1:5555/path":           "tcp://0.0.0.1:5555/path",
    67  		"[::1]:":                      "tcp://[::1]:2375",
    68  		"[::1]:5555/path":             "tcp://[::1]:5555/path",
    69  		"[0:0:0:0:0:0:0:1]:":          "tcp://[0:0:0:0:0:0:0:1]:2375",
    70  		"[0:0:0:0:0:0:0:1]:5555/path": "tcp://[0:0:0:0:0:0:0:1]:5555/path",
    71  		":6666":                   fmt.Sprintf("tcp://%s:6666", DefaultHTTPHost),
    72  		":6666/path":              fmt.Sprintf("tcp://%s:6666/path", DefaultHTTPHost),
    73  		"tcp://":                  DefaultTCPHost,
    74  		"tcp://:7777":             fmt.Sprintf("tcp://%s:7777", DefaultHTTPHost),
    75  		"tcp://:7777/path":        fmt.Sprintf("tcp://%s:7777/path", DefaultHTTPHost),
    76  		"unix:///run/docker.sock": "unix:///run/docker.sock",
    77  		"unix://":                 "unix://" + DefaultUnixSocket,
    78  		"fd://":                   "fd://",
    79  		"fd://something":          "fd://something",
    80  		"localhost:":              "tcp://localhost:2375",
    81  		"localhost:5555":          "tcp://localhost:5555",
    82  		"localhost:5555/path":     "tcp://localhost:5555/path",
    83  	}
    84  	for invalidAddr, expectedError := range invalids {
    85  		if addr, err := parseDockerDaemonHost(invalidAddr); err == nil || err.Error() != expectedError {
    86  			t.Errorf("tcp %v address expected error %q return, got %q and addr %v", invalidAddr, expectedError, err, addr)
    87  		}
    88  	}
    89  	for validAddr, expectedAddr := range valids {
    90  		if addr, err := parseDockerDaemonHost(validAddr); err != nil || addr != expectedAddr {
    91  			t.Errorf("%v -> expected %v, got (%v) addr (%v)", validAddr, expectedAddr, err, addr)
    92  		}
    93  	}
    94  }
    95  
    96  func TestParseTCP(t *testing.T) {
    97  	var (
    98  		defaultHTTPHost = "tcp://127.0.0.1:2376"
    99  	)
   100  	invalids := map[string]string{
   101  		"tcp:a.b.c.d":          "Invalid bind address format: tcp:a.b.c.d",
   102  		"tcp:a.b.c.d/path":     "Invalid bind address format: tcp:a.b.c.d/path",
   103  		"udp://127.0.0.1":      "Invalid proto, expected tcp: udp://127.0.0.1",
   104  		"udp://127.0.0.1:2375": "Invalid proto, expected tcp: udp://127.0.0.1:2375",
   105  	}
   106  	valids := map[string]string{
   107  		"":                            defaultHTTPHost,
   108  		"tcp://":                      defaultHTTPHost,
   109  		"0.0.0.1:":                    "tcp://0.0.0.1:2376",
   110  		"0.0.0.1:5555":                "tcp://0.0.0.1:5555",
   111  		"0.0.0.1:5555/path":           "tcp://0.0.0.1:5555/path",
   112  		":6666":                       "tcp://127.0.0.1:6666",
   113  		":6666/path":                  "tcp://127.0.0.1:6666/path",
   114  		"tcp://:7777":                 "tcp://127.0.0.1:7777",
   115  		"tcp://:7777/path":            "tcp://127.0.0.1:7777/path",
   116  		"[::1]:":                      "tcp://[::1]:2376",
   117  		"[::1]:5555":                  "tcp://[::1]:5555",
   118  		"[::1]:5555/path":             "tcp://[::1]:5555/path",
   119  		"[0:0:0:0:0:0:0:1]:":          "tcp://[0:0:0:0:0:0:0:1]:2376",
   120  		"[0:0:0:0:0:0:0:1]:5555":      "tcp://[0:0:0:0:0:0:0:1]:5555",
   121  		"[0:0:0:0:0:0:0:1]:5555/path": "tcp://[0:0:0:0:0:0:0:1]:5555/path",
   122  		"localhost:":                  "tcp://localhost:2376",
   123  		"localhost:5555":              "tcp://localhost:5555",
   124  		"localhost:5555/path":         "tcp://localhost:5555/path",
   125  	}
   126  	for invalidAddr, expectedError := range invalids {
   127  		if addr, err := ParseTCPAddr(invalidAddr, defaultHTTPHost); err == nil || err.Error() != expectedError {
   128  			t.Errorf("tcp %v address expected error %v return, got %s and addr %v", invalidAddr, expectedError, err, addr)
   129  		}
   130  	}
   131  	for validAddr, expectedAddr := range valids {
   132  		if addr, err := ParseTCPAddr(validAddr, defaultHTTPHost); err != nil || addr != expectedAddr {
   133  			t.Errorf("%v -> expected %v, got %v and addr %v", validAddr, expectedAddr, err, addr)
   134  		}
   135  	}
   136  }
   137  
   138  func TestParseInvalidUnixAddrInvalid(t *testing.T) {
   139  	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" {
   140  		t.Fatalf("Expected an error, got %v", err)
   141  	}
   142  	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" {
   143  		t.Fatalf("Expected an error, got %v", err)
   144  	}
   145  	if v, err := parseSimpleProtoAddr("unix", "", "/var/run/docker.sock"); err != nil || v != "unix:///var/run/docker.sock" {
   146  		t.Fatalf("Expected an %v, got %v", v, "unix:///var/run/docker.sock")
   147  	}
   148  }