package command import ( "os" "path/filepath" "testing" ) func TestLaunchedAsSshSubsystem(t *testing.T) { r, w, err := os.Pipe() if err != nil { t.Fatal(err) } defer r.Close() defer w.Close() regular, err := os.Create(filepath.Join(t.TempDir(), "out")) if err != nil { t.Fatal(err) } defer regular.Close() stdin, stdout := os.Stdin, os.Stdout defer func() { os.Stdin, os.Stdout = stdin, stdout }() cases := []struct { name string sshConnection string in, out *os.File want bool }{ {"sshd subsystem", "10.0.0.1 50000 10.0.0.2 22", r, w, true}, {"no ssh connection", "", r, w, false}, {"stdout to a file", "10.0.0.1 50000 10.0.0.2 22", r, regular, false}, {"stdin from a file", "10.0.0.1 50000 10.0.0.2 22", regular, w, false}, } for _, c := range cases { t.Run(c.name, func(t *testing.T) { t.Setenv("SSH_CONNECTION", c.sshConnection) os.Stdin, os.Stdout = c.in, c.out got := launchedAsSshSubsystem() os.Stdin, os.Stdout = stdin, stdout if got != c.want { t.Fatalf("launchedAsSshSubsystem() = %v, want %v", got, c.want) } }) } }