From ef11492d42f6e2ba482dea1b5f59cad733406e9c Mon Sep 17 00:00:00 2001 From: Jason Woods Date: Fri, 10 Feb 2023 16:55:35 +0000 Subject: [PATCH] fix: Fix :127.0.0.1:1234 not working as specified in documentation for admin connect string Fixes #395 --- docs/AdministrationUtility.md | 3 +- lc-lib/admin/transport.go | 17 ++++- lc-lib/admin/transport_test.go | 113 +++++++++++++++++++++++++++++++++ 3 files changed, 130 insertions(+), 3 deletions(-) create mode 100644 lc-lib/admin/transport_test.go diff --git a/docs/AdministrationUtility.md b/docs/AdministrationUtility.md index 963678d8d..372cdbe2d 100644 --- a/docs/AdministrationUtility.md +++ b/docs/AdministrationUtility.md @@ -91,7 +91,8 @@ Default: tcp:127.0.0.1:1234 Connect to the REST API using the specified address. Any `-config` option is ignored if `-connect` is used. -The address must be in the format `transport:address`. +The address is in the format `transport:address` or just `address`, in which case +"tcp" is the assumed transport. Allowed transports are "tcp", "tcp4", "tcp6" (Windows and *nix) and "unix" (*nix only). For the tcp transports the address format is `host:port`. For the diff --git a/lc-lib/admin/transport.go b/lc-lib/admin/transport.go index 360af922a..c27fd65db 100644 --- a/lc-lib/admin/transport.go +++ b/lc-lib/admin/transport.go @@ -47,9 +47,22 @@ func registerTransport(name string, dialer dialerFunc, listener listenerFunc) { func splitAdminConnectString(adminConnect string) []string { connect := strings.SplitN(adminConnect, ":", 2) - if len(connect) == 1 { - connect = append(connect, connect[0]) + if connect[0] == "unix" { + if len(connect) < 2 { + connect = append(connect, "") + } + return connect + } + + if connect[0] != "tcp" && connect[0] != "tcp4" && connect[0] != "tcp6" { + if len(connect) < 2 { + connect = append(connect, connect[0]) + } else if connect[0] != "" { + connect[1] = connect[0] + ":" + connect[1] + } connect[0] = "tcp" + } else if len(connect) < 2 { + connect = append(connect, "") } return connect diff --git a/lc-lib/admin/transport_test.go b/lc-lib/admin/transport_test.go new file mode 100644 index 000000000..16dae544a --- /dev/null +++ b/lc-lib/admin/transport_test.go @@ -0,0 +1,113 @@ +package admin + +import "testing" + +func TestAdminConnectAddressOnlyNoPort(t *testing.T) { + connect := splitAdminConnectString("127.0.0.1") + + if connect[0] != "tcp" { + t.Errorf("Expected connection type tcp, received: %s", connect[0]) + } + if connect[1] != "127.0.0.1" { + t.Errorf("Expected connection address 127.0.0.1, received: %s", connect[1]) + } +} + +func TestAdminConnectAddressOnlyWithPort(t *testing.T) { + connect := splitAdminConnectString("127.0.0.1:1234") + + if connect[0] != "tcp" { + t.Errorf("Expected connection type tcp, received: %s", connect[0]) + } + if connect[1] != "127.0.0.1:1234" { + t.Errorf("Expected connection address 127.0.0.1:1234, received: %s", connect[1]) + } +} + +func TestAdminConnectAddressEmptyTransport(t *testing.T) { + connect := splitAdminConnectString(":127.0.0.1:1234") + + if connect[0] != "tcp" { + t.Errorf("Expected connection type tcp, received: %s", connect[0]) + } + if connect[1] != "127.0.0.1:1234" { + t.Errorf("Expected connection address 127.0.0.1:1234, received: %s", connect[1]) + } +} + +func TestAdminConnectTcpNoPort(t *testing.T) { + connect := splitAdminConnectString("tcp:127.0.0.1") + + if connect[0] != "tcp" { + t.Errorf("Expected connection type tcp, received: %s", connect[0]) + } + if connect[1] != "127.0.0.1" { + t.Errorf("Expected connection address 127.0.0.1, received: %s", connect[1]) + } +} + +func TestAdminConnectTcpWithPort(t *testing.T) { + connect := splitAdminConnectString("tcp:127.0.0.1:1234") + + if connect[0] != "tcp" { + t.Errorf("Expected connection type tcp, received: %s", connect[0]) + } + if connect[1] != "127.0.0.1:1234" { + t.Errorf("Expected connection address 127.0.0.1:1234, received: %s", connect[1]) + } +} + +func TestAdminConnectTcp6NoPort(t *testing.T) { + connect := splitAdminConnectString("tcp:[::1]") + + if connect[0] != "tcp" { + t.Errorf("Expected connection type tcp, received: %s", connect[0]) + } + if connect[1] != "[::1]" { + t.Errorf("Expected connection address [::1], received: %s", connect[1]) + } +} + +func TestAdminConnectTcp6WithPort(t *testing.T) { + connect := splitAdminConnectString("tcp:[::1]:1234") + + if connect[0] != "tcp" { + t.Errorf("Expected connection type tcp, received: %s", connect[0]) + } + if connect[1] != "[::1]:1234" { + t.Errorf("Expected connection address [::1]:1234, received: %s", connect[1]) + } +} + +func TestAdminConnectUnix(t *testing.T) { + connect := splitAdminConnectString("unix:/var/path/somewhere") + + if connect[0] != "unix" { + t.Errorf("Expected connection type unix, received: %s", connect[0]) + } + if connect[1] != "/var/path/somewhere" { + t.Errorf("Expected connection address /var/path/somewhere, received: %s", connect[1]) + } +} + +func TestAdminConnectUnixAlone(t *testing.T) { + connect := splitAdminConnectString("unix") + + if connect[0] != "unix" { + t.Errorf("Expected connection type unix, received: %s", connect[0]) + } + if connect[1] != "" { + t.Errorf("Expected empty connection address, received: %s", connect[1]) + } +} + +func TestAdminConnectTcpAlone(t *testing.T) { + connect := splitAdminConnectString("tcp") + + if connect[0] != "tcp" { + t.Errorf("Expected connection type tcp, received: %s", connect[0]) + } + if connect[1] != "" { + t.Errorf("Expected empty connection address, received: %s", connect[1]) + } +}