| 1 | package cdp |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "net/http" |
| 7 | "net/http/httptest" |
| 8 | "os" |
| 9 | "path/filepath" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | ) |
| 13 | |
| 14 | func TestResolveWebSocketURLRefusesNonLoopbackByDefault(t *testing.T) { |
| 15 | ctx := context.Background() |
| 16 | for _, endpoint := range []string{"http://10.0.0.5:9222", "ws://example.test:9222/devtools/browser/x", "http://[2001:db8::1]:9222"} { |
| 17 | if _, err := resolveWebSocketURL(ctx, endpoint, nil, false); err == nil { |
| 18 | t.Fatalf("resolveWebSocketURL(%q) accepted a remote DevTools endpoint", endpoint) |
| 19 | } else if !strings.Contains(err.Error(), "loopback") { |
| 20 | t.Fatalf("resolveWebSocketURL(%q) failed for the wrong reason: %v", endpoint, err) |
| 21 | } |
| 22 | } |
| 23 | if _, err := resolveWebSocketURL(ctx, "ws://example.test:9222/devtools/browser/x", nil, true); err != nil { |
| 24 | t.Fatalf("an explicitly allowed remote endpoint was still refused: %v", err) |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | func TestResolveWebSocketURLAcceptsSocketAndProbedEndpoints(t *testing.T) { |
| 29 | ctx := context.Background() |
| 30 | direct, err := resolveWebSocketURL(ctx, "ws://127.0.0.1:9222/devtools/browser/abc", nil, false) |
| 31 | if err != nil || direct != "ws://127.0.0.1:9222/devtools/browser/abc" { |
| 32 | t.Fatalf("socket URL = %q, %v", direct, err) |
| 33 | } |
| 34 | var socket string |
| 35 | srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 36 | if r.URL.Path != "/json/version" { |
| 37 | t.Errorf("probed %s, want /json/version", r.URL.Path) |
| 38 | } |
| 39 | _ = json.NewEncoder(w).Encode(map[string]string{"webSocketDebuggerUrl": socket}) |
| 40 | })) |
| 41 | defer srv.Close() |
| 42 | socket = "ws" + strings.TrimPrefix(srv.URL, "http") + "/devtools/browser/probed" |
| 43 | |
| 44 | got, err := resolveWebSocketURL(ctx, srv.URL, nil, false) |
| 45 | if err != nil || got != socket { |
| 46 | t.Fatalf("probed URL = %q, %v; want %q", got, err, socket) |
| 47 | } |
| 48 | // A bare host:port is the form a user is most likely to paste. |
| 49 | if got, err = resolveWebSocketURL(ctx, strings.TrimPrefix(srv.URL, "http://"), nil, false); err != nil || got != socket { |
| 50 | t.Fatalf("bare host URL = %q, %v; want %q", got, err, socket) |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | func TestResolveWebSocketURLReportsAMissingEndpoint(t *testing.T) { |
| 55 | srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 56 | _ = json.NewEncoder(w).Encode(map[string]string{}) |
| 57 | })) |
| 58 | defer srv.Close() |
| 59 | _, err := resolveWebSocketURL(context.Background(), srv.URL, nil, false) |
| 60 | if err == nil || !strings.Contains(err.Error(), "remote-debugging-port") { |
| 61 | t.Fatalf("got %v, want advice about --remote-debugging-port", err) |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | func TestReadActivePortBuildsTheLoopbackSocketURL(t *testing.T) { |
| 66 | dir := t.TempDir() |
| 67 | if err := os.WriteFile(filepath.Join(dir, activePortFile), []byte("54321\n/devtools/browser/launched\n"), 0o600); err != nil { |
| 68 | t.Fatalf("write port file: %v", err) |
| 69 | } |
| 70 | got, err := readActivePort(context.Background(), dir, 0) |
| 71 | if err != nil { |
| 72 | t.Fatalf("readActivePort: %v", err) |
| 73 | } |
| 74 | if got != "ws://127.0.0.1:54321/devtools/browser/launched" { |
| 75 | t.Fatalf("socket URL = %q", got) |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | func TestChromePathPrefersTheConfiguredBinary(t *testing.T) { |
| 80 | dir := t.TempDir() |
| 81 | bin := filepath.Join(dir, "chrome") |
| 82 | if err := os.WriteFile(bin, []byte("#!/bin/sh\n"), 0o700); err != nil { |
| 83 | t.Fatalf("write fake binary: %v", err) |
| 84 | } |
| 85 | got, err := chromePath(bin) |
| 86 | if err != nil || got != bin { |
| 87 | t.Fatalf("chromePath(configured) = %q, %v", got, err) |
| 88 | } |
| 89 | if _, err := chromePath(filepath.Join(dir, "missing")); err == nil { |
| 90 | t.Fatal("a configured path that does not exist was accepted") |
| 91 | } |
| 92 | } |
| 93 |