package connect
import (
"bytes"
"io"
"net"
"os/exec"
"reflect"
"strings"
"testing"
"git.neuromancer.ovh/bastien-mrq/sssh/internal/host"
)
func TestParseOptions(t *testing.T) {
for _, tt := range []struct {
name string
args []string
want Options
}{
{"shell", nil, Options{}},
{"tunnel", []string{"-f", "80"}, Options{Forwards: []int{80}}},
{"multiple", []string{"-f", "80", "--forward=443", "--forward", "5432"}, Options{Forwards: []int{80, 443, 5432}}},
{"command", []string{"-f", "80", "tail", "-f", "/tmp/log"}, Options{Forwards: []int{80}, Command: []string{"tail", "-f", "/tmp/log"}}},
{"untouched", []string{"tail", "-f", "/tmp/log"}, Options{Command: []string{"tail", "-f", "/tmp/log"}}},
{"separator", []string{"--", "-f", "80"}, Options{Command: []string{"-f", "80"}}},
} {
t.Run(tt.name, func(t *testing.T) {
got, err := ParseOptions(tt.args)
if err != nil || !reflect.DeepEqual(got, tt.want) {
t.Fatalf("got %#v, %v; want %#v", got, err, tt.want)
}
})
}
for _, args := range [][]string{{"-f"}, {"--forward"}, {"-f", "0"}, {"-f", "65536"}, {"-f", "-1"}, {"-f", "abc"}, {"--forward="}, {"-f", "80:80"}} {
if _, err := ParseOptions(args); err == nil {
t.Errorf("expected invalid port error for %q", args)
}
}
}
func TestForwardReservations(t *testing.T) {
h := host.Host{Name: "filab", Host: "example.invalid", User: "alice", Port: 2222, Jump: "bastion"}
var out bytes.Buffer
args, release, err := (Options{Forwards: []int{80, 443}}).sshArgs(h, &out)
if err != nil {
t.Fatal(err)
}
defer release()
var addresses []string
for i, arg := range args {
if arg != "-L" {
continue
}
parts := strings.Split(args[i+1], ":")
if len(parts) != 4 || parts[0] != "127.0.0.1" || parts[2] != "127.0.0.1" {
t.Fatalf("unsafe forwarding specification: %q", args[i+1])
}
address := strings.Join(parts[:2], ":")
l, err := net.Listen("tcp4", address)
if err == nil {
l.Close()
t.Fatalf("port %s was not reserved", address)
}
if !strings.Contains(out.String(), address) {
t.Errorf("local address missing from output: %s", address)
}
addresses = append(addresses, address)
}
if len(addresses) != 2 || addresses[0] == addresses[1] {
t.Fatalf("expected two distinct listeners: %v", addresses)
}
release()
for _, address := range addresses {
l, err := net.Listen("tcp4", address)
if err != nil {
t.Fatal(err)
}
l.Close()
}
}
// Vérifie l'interprétation réelle par OpenSSH sans connexion réseau (-G).
func TestOpenSSHForwardConfig(t *testing.T) {
ssh, err := exec.LookPath("ssh")
if err != nil {
t.Skip("OpenSSH unavailable")
}
for _, remoteCommand := range [][]string{nil, {"uptime"}} {
opts := Options{Forwards: []int{80}, Command: remoteCommand}
args, release, err := opts.sshArgs(host.Host{Host: "example.invalid", Port: 2222, Jump: "bastion"}, io.Discard)
if err != nil {
t.Fatal(err)
}
release()
out, err := exec.Command(ssh, append([]string{"-G", "-F", "/dev/null"}, args...)...).Output()
if err != nil {
t.Fatal(err)
}
for _, want := range []string{"exitonforwardfailure yes", "forkafterauthentication no", "clearallforwardings no", "localforward [127.0.0.1]:", " [127.0.0.1]:80", "port 2222", "proxyjump bastion"} {
if !strings.Contains(string(out), want) {
t.Errorf("missing %q in SSH configuration:\n%s", want, out)
}
}
if (len(remoteCommand) == 0) != strings.Contains(string(out), "sessiontype none") {
t.Errorf("unexpected session type for command %v:\n%s", remoteCommand, out)
}
}
}