| 1 | |
| 2 | |
| 3 | package hostproxy |
| 4 | |
| 5 | import ( |
| 6 | "bytes" |
| 7 | "net/http" |
| 8 | "net/http/httptest" |
| 9 | "os" |
| 10 | "os/exec" |
| 11 | "path/filepath" |
| 12 | "strings" |
| 13 | "testing" |
| 14 | ) |
| 15 | |
| 16 | func TestMacPACResolutionUsesNativeCodeWithoutChangingSystemSettings(t *testing.T) { |
| 17 | |
| 18 | |
| 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> |
| 40 | int 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 | } |