| 1 | |
| 2 | |
| 3 | |
| 4 | package sshutil |
| 5 | |
| 6 | import ( |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "runtime" |
| 10 | "slices" |
| 11 | "strings" |
| 12 | "testing" |
| 13 | |
| 14 | "gotest.tools/v3/assert" |
| 15 | ) |
| 16 | |
| 17 | const ( |
| 18 | fakeCygpathOutEnv = "LIMA_TEST_FAKE_CYGPATH_OUT" |
| 19 | fakeCygpathFailure = "exit-non-zero" |
| 20 | ) |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | func TestMain(m *testing.M) { |
| 32 | if out, ok := os.LookupEnv(fakeCygpathOutEnv); ok { |
| 33 | if want := []string{"-w", "/usr/lib/ssh/sftp-server"}; !slices.Equal(os.Args[1:], want) { |
| 34 | os.Stderr.WriteString("fake cygpath: got " + strings.Join(os.Args[1:], " ") + |
| 35 | ", want " + strings.Join(want, " ") + "\n") |
| 36 | os.Exit(2) |
| 37 | } |
| 38 | if out == fakeCygpathFailure { |
| 39 | os.Exit(1) |
| 40 | } |
| 41 | os.Stdout.WriteString(out + "\n") |
| 42 | os.Exit(0) |
| 43 | } |
| 44 | os.Exit(m.Run()) |
| 45 | } |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | func TestPickCompleteSSHOnWindows(t *testing.T) { |
| 51 | if runtime.GOOS != "windows" { |
| 52 | t.Skip("Windows-only PATH walk") |
| 53 | } |
| 54 | |
| 55 | mkDir := func(t *testing.T, exes ...string) string { |
| 56 | t.Helper() |
| 57 | dir := resolvedTempDir(t) |
| 58 | for _, exe := range exes { |
| 59 | assert.NilError(t, os.WriteFile(filepath.Join(dir, exe), nil, 0o644)) |
| 60 | } |
| 61 | return dir |
| 62 | } |
| 63 | |
| 64 | t.Run("complete install on PATH is picked", func(t *testing.T) { |
| 65 | full := mkDir(t, "ssh.exe", "scp.exe", "ssh-keygen.exe") |
| 66 | t.Setenv("PATH", full) |
| 67 | assert.Equal(t, pickCompleteSSHOnWindows(), filepath.Join(full, "ssh.exe")) |
| 68 | }) |
| 69 | |
| 70 | t.Run("incomplete install before complete install is skipped", func(t *testing.T) { |
| 71 | mingit := mkDir(t, "ssh.exe") |
| 72 | full := mkDir(t, "ssh.exe", "scp.exe", "ssh-keygen.exe") |
| 73 | t.Setenv("PATH", mingit+string(os.PathListSeparator)+full) |
| 74 | assert.Equal(t, pickCompleteSSHOnWindows(), filepath.Join(full, "ssh.exe")) |
| 75 | }) |
| 76 | |
| 77 | t.Run("falls back to native install when nothing on PATH is complete", func(t *testing.T) { |
| 78 | nativeSSH := filepath.Join(systemRoot(), "System32", "OpenSSH", "ssh.exe") |
| 79 | if _, err := os.Stat(nativeSSH); err != nil { |
| 80 | t.Skipf("native OpenSSH not present at %q on this host", nativeSSH) |
| 81 | } |
| 82 | mingit := mkDir(t, "ssh.exe") |
| 83 | t.Setenv("PATH", mingit) |
| 84 | assert.Equal(t, pickCompleteSSHOnWindows(), nativeSSH) |
| 85 | }) |
| 86 | } |
| 87 | |
| 88 | |
| 89 | |
| 90 | func TestCygpathForSSH(t *testing.T) { |
| 91 | if runtime.GOOS != "windows" { |
| 92 | t.Skip("cygpath detection only runs on Windows") |
| 93 | } |
| 94 | |
| 95 | t.Run("cygwin", func(t *testing.T) { |
| 96 | dir := resolvedTempDir(t) |
| 97 | sshExe := filepath.Join(dir, "ssh.exe") |
| 98 | cygpathExe := filepath.Join(dir, "cygpath.exe") |
| 99 | assert.NilError(t, os.WriteFile(sshExe, nil, 0o644)) |
| 100 | assert.NilError(t, os.WriteFile(cygpathExe, nil, 0o644)) |
| 101 | |
| 102 | got, ok := cygpathForSSH(SSHExe{Exe: sshExe}) |
| 103 | assert.Equal(t, ok, true, "ssh.exe next to cygpath.exe should be Cygwin") |
| 104 | assert.Equal(t, got, cygpathExe, "should return the sibling cygpath, not bare 'cygpath'") |
| 105 | }) |
| 106 | |
| 107 | t.Run("native", func(t *testing.T) { |
| 108 | dir := resolvedTempDir(t) |
| 109 | sshExe := filepath.Join(dir, "ssh.exe") |
| 110 | assert.NilError(t, os.WriteFile(sshExe, nil, 0o644)) |
| 111 | |
| 112 | got, ok := cygpathForSSH(SSHExe{Exe: sshExe}) |
| 113 | assert.Equal(t, ok, false, "ssh.exe with no sibling cygpath.exe should be native") |
| 114 | assert.Equal(t, got, "") |
| 115 | }) |
| 116 | |
| 117 | t.Run("empty", func(t *testing.T) { |
| 118 | got, ok := cygpathForSSH(SSHExe{}) |
| 119 | assert.Equal(t, ok, false) |
| 120 | assert.Equal(t, got, "") |
| 121 | }) |
| 122 | } |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | func TestSftpServerForSSH(t *testing.T) { |
| 128 | if runtime.GOOS != "windows" { |
| 129 | t.Skip("Windows-only path handling") |
| 130 | } |
| 131 | ctx := t.Context() |
| 132 | |
| 133 | t.Run("native: sibling sftp-server.exe next to ssh.exe", func(t *testing.T) { |
| 134 | dir := resolvedTempDir(t) |
| 135 | sshExe := filepath.Join(dir, "ssh.exe") |
| 136 | sftpExe := filepath.Join(dir, "sftp-server.exe") |
| 137 | assert.NilError(t, os.WriteFile(sshExe, nil, 0o644)) |
| 138 | assert.NilError(t, os.WriteFile(sftpExe, nil, 0o644)) |
| 139 | |
| 140 | got := SftpServerForSSH(ctx, SSHExe{Exe: sshExe}) |
| 141 | assert.Equal(t, got, sftpExe) |
| 142 | }) |
| 143 | |
| 144 | t.Run("native: no sibling sftp-server.exe returns empty", func(t *testing.T) { |
| 145 | dir := resolvedTempDir(t) |
| 146 | sshExe := filepath.Join(dir, "ssh.exe") |
| 147 | assert.NilError(t, os.WriteFile(sshExe, nil, 0o644)) |
| 148 | |
| 149 | got := SftpServerForSSH(ctx, SSHExe{Exe: sshExe}) |
| 150 | assert.Equal(t, got, "", "no sftp-server.exe sibling -> caller falls back to sshocker auto-detect") |
| 151 | }) |
| 152 | |
| 153 | t.Run("empty input returns empty", func(t *testing.T) { |
| 154 | assert.Equal(t, SftpServerForSSH(ctx, SSHExe{}), "") |
| 155 | }) |
| 156 | } |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | func TestSftpServerForSSHCygwin(t *testing.T) { |
| 162 | if runtime.GOOS != "windows" { |
| 163 | t.Skip("Windows-only path handling") |
| 164 | } |
| 165 | ctx := t.Context() |
| 166 | |
| 167 | t.Run("candidate without .exe resolves to the binary beside it", func(t *testing.T) { |
| 168 | sshExe := cygwinToolchain(t) |
| 169 | dir := filepath.Dir(sshExe) |
| 170 | sftpExe := filepath.Join(dir, "sftp-server.exe") |
| 171 | assert.NilError(t, os.WriteFile(sftpExe, nil, 0o644)) |
| 172 | t.Setenv(fakeCygpathOutEnv, filepath.Join(dir, "sftp-server")) |
| 173 | |
| 174 | assert.Equal(t, SftpServerForSSH(ctx, SSHExe{Exe: sshExe}), sftpExe) |
| 175 | }) |
| 176 | |
| 177 | t.Run("candidate that resolves to nothing returns empty", func(t *testing.T) { |
| 178 | sshExe := cygwinToolchain(t) |
| 179 | t.Setenv(fakeCygpathOutEnv, filepath.Join(filepath.Dir(sshExe), "sftp-server")) |
| 180 | |
| 181 | assert.Equal(t, SftpServerForSSH(ctx, SSHExe{Exe: sshExe}), "", |
| 182 | "unresolvable candidate -> caller falls back to sshocker auto-detect") |
| 183 | }) |
| 184 | |
| 185 | t.Run("cygpath failure returns empty", func(t *testing.T) { |
| 186 | sshExe := cygwinToolchain(t) |
| 187 | t.Setenv(fakeCygpathOutEnv, fakeCygpathFailure) |
| 188 | |
| 189 | assert.Equal(t, SftpServerForSSH(ctx, SSHExe{Exe: sshExe}), "") |
| 190 | }) |
| 191 | } |
| 192 | |
| 193 | |
| 194 | |
| 195 | func TestCompanionForSSH(t *testing.T) { |
| 196 | if runtime.GOOS != "windows" { |
| 197 | t.Skip("companion resolution only runs on Windows") |
| 198 | } |
| 199 | |
| 200 | t.Run("sibling is picked over PATH", func(t *testing.T) { |
| 201 | dir := resolvedTempDir(t) |
| 202 | sshExe := filepath.Join(dir, "ssh.exe") |
| 203 | keygenExe := filepath.Join(dir, "ssh-keygen.exe") |
| 204 | assert.NilError(t, os.WriteFile(sshExe, nil, 0o644)) |
| 205 | assert.NilError(t, os.WriteFile(keygenExe, nil, 0o644)) |
| 206 | |
| 207 | assert.Equal(t, companionForSSH(SSHExe{Exe: sshExe}, "ssh-keygen"), keygenExe) |
| 208 | }) |
| 209 | |
| 210 | t.Run("no sibling falls back to the bare name", func(t *testing.T) { |
| 211 | dir := resolvedTempDir(t) |
| 212 | sshExe := filepath.Join(dir, "ssh.exe") |
| 213 | assert.NilError(t, os.WriteFile(sshExe, nil, 0o644)) |
| 214 | |
| 215 | assert.Equal(t, companionForSSH(SSHExe{Exe: sshExe}, "ssh-keygen"), "ssh-keygen") |
| 216 | }) |
| 217 | |
| 218 | t.Run("empty input falls back to the bare name", func(t *testing.T) { |
| 219 | assert.Equal(t, companionForSSH(SSHExe{}, "ssh-keygen"), "ssh-keygen") |
| 220 | }) |
| 221 | } |
| 222 | |
| 223 | |
| 224 | |
| 225 | func cygwinToolchain(t *testing.T) string { |
| 226 | t.Helper() |
| 227 | dir := resolvedTempDir(t) |
| 228 | sshExe := filepath.Join(dir, "ssh.exe") |
| 229 | assert.NilError(t, os.WriteFile(sshExe, nil, 0o644)) |
| 230 | |
| 231 | self, err := os.Executable() |
| 232 | assert.NilError(t, err) |
| 233 | binary, err := os.ReadFile(self) |
| 234 | assert.NilError(t, err) |
| 235 | assert.NilError(t, os.WriteFile(filepath.Join(dir, "cygpath.exe"), binary, 0o755)) |
| 236 | return sshExe |
| 237 | } |
| 238 | |
| 239 | |
| 240 | |
| 241 | |
| 242 | |
| 243 | func resolvedTempDir(t *testing.T) string { |
| 244 | t.Helper() |
| 245 | dir := t.TempDir() |
| 246 | resolved, err := filepath.EvalSymlinks(dir) |
| 247 | assert.NilError(t, err) |
| 248 | return resolved |
| 249 | } |