跳到主要内容

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

浏览文件

proxy_darwin_test.go

89 行 · 3.2 KB历史下载
1//go:build darwin && cgo
2
3package hostproxy
4
5import (
6 "bytes"
7 "net/http"
8 "net/http/httptest"
9 "os"
10 "os/exec"
11 "path/filepath"
12 "strings"
13 "testing"
14)
15
16func TestMacPACResolutionUsesNativeCodeWithoutChangingSystemSettings(t *testing.T) {
17 // Compile the production CFNetwork bridge in an isolated harness. This lets
18 // real PAC evaluation run without replacing the developer's system proxy.
19 source, err := os.ReadFile("proxy_darwin.go")
20 if err != nil {
21 t.Fatal(err)
22 }
23 _, sourceC, ok := strings.Cut(string(source), "/*")
24 if !ok {
25 t.Fatal("missing C bridge")
26 }
27 sourceC, _, ok = strings.Cut(sourceC, "*/")
28 if !ok {
29 t.Fatal("missing C bridge end")
30 }
31 var lines []string
32 for _, line := range strings.Split(sourceC, "\n") {
33 if !strings.HasPrefix(line, "#cgo ") {
34 lines = append(lines, line)
35 }
36 }
37 sourceC = strings.Join(lines, "\n")
38 sourceC += `
39#include <stdio.h>
40int main(int argc, char **argv) {
41 CFStringRef pacText = CFStringCreateWithCString(NULL, argv[1], kCFStringEncodingUTF8);
42 CFURLRef pacURL = CFURLCreateWithString(NULL, pacText, NULL);
43 const void *keys[] = { kCFProxyTypeKey, kCFProxyAutoConfigurationURLKey };
44 const void *values[] = { kCFProxyTypeAutoConfigurationURL, pacURL };
45 CFDictionaryRef proxy = CFDictionaryCreate(NULL, keys, values, 2, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
46 const void *items[] = {proxy};
47 CFArrayRef proxies = CFArrayCreate(NULL, items, 1, &kCFTypeArrayCallBacks);
48 CFURLRef target = CFURLCreateWithString(NULL, CFSTR("https://cloud.debian.org/image"), NULL);
49 CageProxy result = cageResolveArray(proxies, target, 1);
50 if (result.failed) return 2;
51 puts(result.url ? result.url : "DIRECT");
52 free(result.url);
53 CFRelease(target); CFRelease(proxies); CFRelease(proxy); CFRelease(pacURL); CFRelease(pacText);
54 return 0;
55}`
56 program := filepath.Join(t.TempDir(), "pac-check")
57 compile := exec.Command("clang", "-x", "c", "-", "-framework", "CoreFoundation", "-framework", "CFNetwork", "-o", program)
58 compile.Stdin = strings.NewReader(sourceC)
59 if output, err := compile.CombinedOutput(); err != nil {
60 t.Fatalf("compile native PAC harness: %v\n%s", err, output)
61 }
62 for _, test := range []struct {
63 name, script, want string
64 failure bool
65 }{
66 {"http", `function FindProxyForURL(url,host){return "PROXY 127.0.0.1:7896";}`, "http://127.0.0.1:7896", false},
67 {"direct", `function FindProxyForURL(url,host){return "DIRECT";}`, "DIRECT", false},
68 {"socks", `function FindProxyForURL(url,host){return "SOCKS 127.0.0.1:7896";}`, "socks5://127.0.0.1:7896", false},
69 {"broken-script", `this is not a PAC script`, "", true},
70 } {
71 t.Run(test.name, func(t *testing.T) {
72 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
73 w.Header().Set("Content-Type", "application/x-ns-proxy-autoconfig")
74 w.Write([]byte(test.script))
75 }))
76 defer server.Close()
77 output, err := exec.CommandContext(t.Context(), program, server.URL+"/proxy.pac").CombinedOutput()
78 if test.failure {
79 if err == nil {
80 t.Fatalf("broken PAC became direct: %s", output)
81 }
82 return
83 }
84 if err != nil || !bytes.Equal(bytes.TrimSpace(output), []byte(test.want)) {
85 t.Fatalf("result=%q err=%v want=%q", output, err, test.want)
86 }
87 })
88 }
89}