跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

sshutil_windows_test.go

249 行 · 8.4 KB历史下载
1// SPDX-FileCopyrightText: Copyright The Lima Authors
2// SPDX-License-Identifier: Apache-2.0
3
4package sshutil
5
6import (
7 "os"
8 "path/filepath"
9 "runtime"
10 "slices"
11 "strings"
12 "testing"
13
14 "gotest.tools/v3/assert"
15)
16
17const (
18 fakeCygpathOutEnv = "LIMA_TEST_FAKE_CYGPATH_OUT"
19 fakeCygpathFailure = "exit-non-zero"
20)
21
22// TestMain doubles as a fake cygpath.exe. With fakeCygpathOutEnv set it prints
23// that value and exits 0, or exits 1 when the value is fakeCygpathFailure, so a
24// test can copy this binary beside an ssh.exe and drive the Cygwin branch. An
25// empty value would not work as the failure signal, because Windows cannot
26// distinguish an empty environment variable from an unset one.
27//
28// It answers only the sftp-server probe and rejects any other command line. A
29// change to the arguments in SftpServerForSSH then breaks this test instead of
30// going unnoticed. A test driving a different cygpath call needs its own arm.
31func 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// TestPickCompleteSSHOnWindows: an ssh.exe missing scp.exe or
48// ssh-keygen.exe (MinGit's shape) is skipped for the next complete
49// install on PATH.
50func 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// TestCygpathForSSH: an ssh.exe next to cygpath.exe is Cygwin-based and
89// resolves the sibling cygpath; one without is native.
90func 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// TestSftpServerForSSH: a native ssh.exe resolves the sibling
125// sftp-server.exe, and returns "" when none exists so the caller falls
126// back to sshocker's own auto-detection.
127func 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// TestSftpServerForSSHCygwin: the Cygwin branch reports what the sibling
159// cygpath resolves, but only once that names an executable. cygpath omits the
160// .exe suffix, so the candidate reaches LookPath without one.
161func 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// TestCompanionForSSH: a companion tool is resolved beside the selected
194// ssh.exe, and falls back to the bare name when no sibling exists.
195func 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// cygwinToolchain writes an ssh.exe into a fresh directory, plus a cygpath.exe
224// that is a copy of this test binary, and returns the ssh.exe path.
225func 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// resolvedTempDir is t.TempDir() run through EvalSymlinks, matching what
240// the production helpers compute. On GitHub Windows runners t.TempDir()
241// is an 8.3 short path (C:\Users\RUNNER~1\...) that the helpers expand,
242// so a raw compare would fail.
243func 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}