跳到主要内容

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

浏览文件

suite_test.go

130 行 · 2.6 KB历史下载
1//go:build windows
2
3package e2e
4
5import (
6 "flag"
7 "fmt"
8 "os"
9 "os/exec"
10 "path/filepath"
11 "syscall"
12 "testing"
13 "time"
14
15 . "github.com/onsi/ginkgo"
16 . "github.com/onsi/gomega"
17 "github.com/sirupsen/logrus"
18)
19
20const (
21 WM_QUIT = 0x12
22)
23
24var (
25 tmpDir string
26 binDir string
27 keyFile string
28 winSshProxy string
29 tidFile string
30)
31
32func TestSuite(t *testing.T) {
33 RegisterFailHandler(Fail)
34 RunSpecs(t, "win-sshproxy suite")
35}
36
37func init() {
38 flag.StringVar(&tmpDir, "tmpDir", "../tmp", "temporary working directory")
39 flag.StringVar(&binDir, "bin", "../bin", "directory with compiled binaries")
40 _ = os.MkdirAll(tmpDir, 0755)
41 keyFile = filepath.Join(tmpDir, "id.key")
42 _ = os.WriteFile(keyFile, []byte(fakeHostKey), 0600)
43 winSshProxy = filepath.Join(binDir, "win-sshproxy.exe")
44 tidFile = filepath.Join(tmpDir, "win-sshproxy.tid")
45}
46
47var _ = BeforeSuite(func() {
48 startMockServer()
49})
50
51var _ = AfterSuite(func() {
52 stopMockServer()
53})
54
55func startProxy() error {
56 os.Remove(tidFile)
57 cmd := exec.Command(winSshProxy, "-debug", "test", tmpDir, "npipe:////./pipe/fake_docker_engine", "ssh://localhost:2134/run/podman/podman.sock", keyFile)
58 return cmd.Start()
59}
60
61func readTid() (uint32, uint32, error) {
62 contents, err := os.ReadFile(tidFile)
63 if err != nil {
64 return 0, 0, err
65 }
66
67 var pid, tid uint32
68 _, err = fmt.Sscanf(string(contents), "%d:%d", &pid, &tid)
69 if err != nil {
70 return 0, 0, err
71 }
72 return pid, tid, nil
73}
74
75func sendQuit(tid uint32) {
76 user32 := syscall.NewLazyDLL("user32.dll")
77 postMessage := user32.NewProc("PostThreadMessageW")
78 _, _, err := postMessage.Call(uintptr(tid), WM_QUIT, 0, 0)
79 if err != nil {
80 logrus.Errorf("Error posting quit message: %v", err)
81 }
82}
83
84func stopProxy(noKill bool) error {
85 pid, tid, err := readTid()
86 if err != nil {
87 return err
88 }
89
90 proc, err := os.FindProcess(int(pid))
91 if err != nil {
92 return err
93 }
94 sendQuit(tid)
95 state := waitTimeout(proc, 20*time.Second)
96 if state == nil || !state.Exited() {
97 if noKill {
98 return fmt.Errorf("proxy did not exit on request")
99 }
100 _ = proc.Kill()
101 state = waitTimeout(proc, 20*time.Second)
102 }
103
104 if state == nil || !state.Exited() {
105 return fmt.Errorf("Stop proxy failed: %d", pid)
106 }
107
108 _ = os.Remove(tidFile)
109 return nil
110}
111
112func waitTimeout(proc *os.Process, timeout time.Duration) *os.ProcessState {
113 return doTimeout(func(complete chan *os.ProcessState) {
114 state, _ := proc.Wait()
115 complete <- state
116 }, timeout)
117}
118
119func doTimeout(action func(complete chan *os.ProcessState), timeout time.Duration) *os.ProcessState {
120 complete := make(chan *os.ProcessState)
121
122 go action(complete)
123 select {
124 case <-time.After(timeout):
125 return nil
126
127 case state := <-complete:
128 return state
129 }
130}