Add netssh sftp and netssh scp. Both resolve the host like a connection does (cache, shortcuts and rewrites, instance/host, default user and port, SSH options and rules, --jump/--no-jump, --dry-run) and get shell completion. The per-client flag tables handle the differences from ssh: the port is -P, -l is a bandwidth limit (the default user goes in as -o User=), and IPv6 addresses are bracketed in host:path. All remote scp operands must name the same host. Add named port forwards under ssh.forwards (global, per instance, per rule; a more specific layer replaces a forward of the same name), netssh tunnel HOST NAME|SPEC... (ssh -N with ExitOnForwardFailure) and netssh --forward NAME. Specs such as 8080:localhost:80 work without configuration. Forwards are editable in netssh configure and completed in the shell; the TUI opens SFTP with ctrl+o and a tunnel with ctrl+t. Host resolution moves out of runSSHWrapper into resolveTarget, shared by ssh, sftp, scp, tunnel, and the TUI. Options added by netssh are now inserted before the first operand or "--": after "--", ssh read them as the destination, and scp stops reading options at the first operand. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
93 lines
2.8 KiB
Go
93 lines
2.8 KiB
Go
package setup
|
|
|
|
import (
|
|
"reflect"
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.zb-server.de/Sebi/ssh-netbox-wrapper/internal/config"
|
|
)
|
|
|
|
func TestParseLines(t *testing.T) {
|
|
got := parseLines(" -A \n\n\t-C\n \n")
|
|
if want := []string{"-A", "-C"}; !reflect.DeepEqual(got, want) {
|
|
t.Fatalf("got %q, want %q", got, want)
|
|
}
|
|
if got := parseLines(" \n "); got != nil {
|
|
t.Fatalf("blank input: got %q, want nil", got)
|
|
}
|
|
}
|
|
|
|
func TestParseOptionsRoundtrip(t *testing.T) {
|
|
text := "ServerAliveInterval = 60\n\nForwardAgent=yes\nLocalCommand=echo a=b\n"
|
|
got, err := parseOptions(text)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
want := map[string]string{"ServerAliveInterval": "60", "ForwardAgent": "yes", "LocalCommand": "echo a=b"}
|
|
if !reflect.DeepEqual(got, want) {
|
|
t.Fatalf("got %v, want %v", got, want)
|
|
}
|
|
again, err := parseOptions(formatOptions(got))
|
|
if err != nil || !reflect.DeepEqual(again, want) {
|
|
t.Fatalf("roundtrip: got %v (%v)", again, err)
|
|
}
|
|
if formatOptions(got) != "ForwardAgent=yes\nLocalCommand=echo a=b\nServerAliveInterval=60" {
|
|
t.Fatalf("formatOptions not sorted: %q", formatOptions(got))
|
|
}
|
|
}
|
|
|
|
func TestParseOptionsEmpty(t *testing.T) {
|
|
got, err := parseOptions("\n \n")
|
|
if err != nil || got != nil {
|
|
t.Fatalf("got %v, %v; want nil, nil", got, err)
|
|
}
|
|
}
|
|
|
|
func TestParseOptionsInvalid(t *testing.T) {
|
|
for _, text := range []string{"ServerAliveInterval", "=60", "Bad-Key=1", "1Key=1"} {
|
|
if _, err := parseOptions(text); err == nil {
|
|
t.Errorf("%q: expected error", text)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestDescribeMatch(t *testing.T) {
|
|
got := describeMatch(config.SSHMatch{Tag: "dmz", Name: "*.dmz"})
|
|
if got != "[tag=dmz name=*.dmz]" {
|
|
t.Fatalf("got %q", got)
|
|
}
|
|
}
|
|
|
|
func TestRuleValidation(t *testing.T) {
|
|
if err := (config.SSHMatch{}).Validate("rule"); err == nil || !strings.Contains(err.Error(), "at least one") {
|
|
t.Fatalf("empty match: got %v", err)
|
|
}
|
|
if err := (config.SSHMatch{Name: "[web"}).Validate("rule"); err == nil {
|
|
t.Fatal("invalid glob accepted")
|
|
}
|
|
if err := (config.SSHMatch{Kind: "vm"}).Validate("rule"); err != nil {
|
|
t.Fatalf("valid match rejected: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestParseForwardsRoundtrip(t *testing.T) {
|
|
text := "Web L 8080:localhost:80\n\nsocks d 1080\nback R 9000:localhost:9000\n"
|
|
got, err := parseForwards(text)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
want := map[string]config.Forward{"web": {Local: "8080:localhost:80"}, "socks": {Dynamic: "1080"}, "back": {Remote: "9000:localhost:9000"}}
|
|
if !reflect.DeepEqual(got, want) {
|
|
t.Fatalf("got %+v", got)
|
|
}
|
|
if formatForwards(got) != "back R 9000:localhost:9000\nsocks D 1080\nweb L 8080:localhost:80" {
|
|
t.Fatalf("format: %q", formatForwards(got))
|
|
}
|
|
for _, bad := range []string{"web 8080:localhost:80", "web X 8080", "web L -oFoo", "bad/name L 1:a:2"} {
|
|
if _, err := parseForwards(bad); err == nil {
|
|
t.Errorf("%q accepted", bad)
|
|
}
|
|
}
|
|
}
|