49 lines
1.4 KiB
Go
49 lines
1.4 KiB
Go
package grpcaddr
|
|
|
|
import (
|
|
"path/filepath"
|
|
"runtime"
|
|
"testing"
|
|
)
|
|
|
|
func TestDefaultUsesUnixSocketOnUnixLikeSystems(t *testing.T) {
|
|
if runtime.GOOS == "windows" {
|
|
t.Skip("unix default is not expected on windows")
|
|
}
|
|
t.Setenv("XDG_RUNTIME_DIR", "/tmp/keepassgo-runtime-test")
|
|
|
|
got := Default("linux")
|
|
want := "unix:///tmp/keepassgo-runtime-test/keepassgo/keepassgo-grpc.sock"
|
|
if got != want {
|
|
t.Fatalf("Default() = %q, want %q", got, want)
|
|
}
|
|
}
|
|
|
|
func TestParse(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tests := []struct {
|
|
name string
|
|
input string
|
|
wantNetwork string
|
|
wantEnd string
|
|
}{
|
|
{name: "unix scheme", input: "unix:///tmp/keepassgo.sock", wantNetwork: "unix", wantEnd: "/tmp/keepassgo.sock"},
|
|
{name: "tcp scheme", input: "tcp://127.0.0.1:47777", wantNetwork: "tcp", wantEnd: "127.0.0.1:47777"},
|
|
{name: "bare path", input: filepath.Clean("/tmp/keepassgo.sock"), wantNetwork: "unix", wantEnd: filepath.Clean("/tmp/keepassgo.sock")},
|
|
{name: "bare tcp", input: "127.0.0.1:47777", wantNetwork: "tcp", wantEnd: "127.0.0.1:47777"},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
gotNetwork, gotEnd, err := Parse(tt.input)
|
|
if err != nil {
|
|
t.Fatalf("Parse() error = %v", err)
|
|
}
|
|
if gotNetwork != tt.wantNetwork || gotEnd != tt.wantEnd {
|
|
t.Fatalf("Parse() = (%q, %q), want (%q, %q)", gotNetwork, gotEnd, tt.wantNetwork, tt.wantEnd)
|
|
}
|
|
})
|
|
}
|
|
}
|