Skip to content

Commit

Permalink
fix: Fix :127.0.0.1:1234 not working as specified in documentation fo…
Browse files Browse the repository at this point in the history
…r admin connect string

Fixes #395
  • Loading branch information
driskell committed Feb 10, 2023
1 parent ea7a63c commit ef11492
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 3 deletions.
3 changes: 2 additions & 1 deletion docs/AdministrationUtility.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 15 additions & 2 deletions lc-lib/admin/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
113 changes: 113 additions & 0 deletions lc-lib/admin/transport_test.go
Original file line number Diff line number Diff line change
@@ -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])
}
}

0 comments on commit ef11492

Please sign in to comment.