package harness import ( "strings" "testing" ) // stubComposePort temporarily replaces the bounded compose port probe and // restores it on cleanup, recording probe invocations in calls when non-nil. func stubComposePort(t *testing.T, output string, calls *int) { t.Helper() previous := composePort composePort = func(directory, service, containerPort string) (string, error) { if calls != nil { *calls++ } if directory != "/checkout" || service != "pipelines" || containerPort != "8080" { t.Fatalf("unexpected probe %s %s %s", directory, service, containerPort) } return output, nil } t.Cleanup(func() { composePort = previous }) } // forbidProbe installs a probe that fails the test if it runs. func forbidProbe(t *testing.T) { t.Helper() previous := composePort composePort = func(_, _, _ string) (string, error) { t.Fatal("probe must not run") return "", nil } t.Cleanup(func() { composePort = previous }) } func TestCLIPrefixDockerLinkedUsesContainerBinaryAndScopedAliases(t *testing.T) { stubComposePort(t, "0.0.0.0:8081\n", nil) prefix, err := CLIPrefix(Profile{DockerDirectory: "/checkout"}, "ws://127.0.0.1:8081/connect", "/Users/alan/go/bin/pipelines") if err != nil { t.Fatal(err) } for _, want := range []string{ "/usr/local/bin/pipelines", "--url 'ws://127.0.0.1:8081/connect'", "PIPELINES_TRANSPORT_SOURCE='ws://127.0.0.1:8081/connect'", "PIPELINES_TRANSPORT_TARGET='ws://127.0.0.1:8080/connect'", } { if !strings.Contains(prefix, want) { t.Fatalf("prefix missing %q: %s", want, prefix) } } if strings.Contains(prefix, "/Users/alan/go/bin/pipelines") { t.Fatalf("container prefix leaked the host binary: %s", prefix) } } // TestCLIPrefixRemoteDNSHostnameStaysRemote pins the negative case that a remote // DNS name beginning with "127." must not be treated as a loopback literal and // must not trigger a Docker probe. func TestCLIPrefixRemoteDNSHostnameStaysRemote(t *testing.T) { forbidProbe(t) prefix, err := CLIPrefix(Profile{DockerDirectory: "/checkout"}, "ws://127.remote.example:8081/connect", "/host/pipelines") if err != nil { t.Fatal(err) } if strings.Contains(prefix, "PIPELINES_TRANSPORT_") { t.Fatalf("remote DNS hostname was mapped: %s", prefix) } if !strings.Contains(prefix, "'/usr/local/bin/pipelines' --url 'ws://127.remote.example:8081/connect'") { t.Fatalf("remote DNS prefix changed: %s", prefix) } } func TestCLIPrefixRemoteEndpointStaysRemote(t *testing.T) { forbidProbe(t) prefix, err := CLIPrefix(Profile{DockerDirectory: "/checkout"}, "wss://example.com:8443/connect", "/host/pipelines") if err != nil { t.Fatal(err) } if strings.Contains(prefix, "PIPELINES_TRANSPORT_") || !strings.Contains(prefix, "/usr/local/bin/pipelines") { t.Fatalf("remote prefix = %s", prefix) } } // TestCLIPrefixLoopbackLiteralsAndLocalhost covers the positive loopback forms: // IPv4/IPv6 literals and the exact localhost name, each with a matching binding. func TestCLIPrefixLoopbackLiteralsAndLocalhost(t *testing.T) { cases := []struct { url string bindings string }{ {"ws://127.0.0.1:8081/connect", "127.0.0.1:8081\n"}, {"ws://127.0.0.1:8081/connect", "0.0.0.0:8081\n"}, {"ws://[::1]:8081/connect", "[::1]:8081\n"}, {"ws://[::1]:8081/connect", "[::]:8081\n"}, {"ws://localhost:8081/connect", "0.0.0.0:8081\n"}, {"ws://localhost:8081/connect", "[::1]:8081\n"}, } for _, tc := range cases { stubComposePort(t, tc.bindings, nil) prefix, err := CLIPrefix(Profile{DockerDirectory: "/checkout"}, tc.url, "/host/pipelines") if err != nil { t.Fatalf("%s with %q: %v", tc.url, tc.bindings, err) } if !strings.Contains(prefix, "PIPELINES_TRANSPORT_SOURCE='"+tc.url+"'") || !strings.Contains(prefix, "PIPELINES_TRANSPORT_TARGET='ws://127.0.0.1:8080/connect'") { t.Fatalf("%s with %q not aliased: %s", tc.url, tc.bindings, prefix) } } } // TestCLIPrefixBindingMustCoverLoopbackAddressAndPort proves a published port // number alone is not proof: a different local service on the same port must not // satisfy a loopback request, while a loopback or wildcard binding of the right // family does. func TestCLIPrefixBindingMustCoverLoopbackAddressAndPort(t *testing.T) { positive := []string{"127.0.0.1:8081\n", "0.0.0.0:8081\n", "127.0.0.1:8081\n0.0.0.0:8081\n"} for _, bindings := range positive { stubComposePort(t, bindings, nil) if _, err := CLIPrefix(Profile{DockerDirectory: "/checkout"}, "ws://127.0.0.1:8081/connect", "/host/pipelines"); err != nil { t.Fatalf("%q should cover 127.0.0.1:8081: %v", bindings, err) } } unmatched := []string{ "10.0.0.75:8081\n", // different local service, same port "192.168.1.10:8081\n", // LAN address, not loopback "[::1]:8081\n", // family mismatch for an IPv4 request "0.0.0.0:9999\n", // port mismatch } for _, bindings := range unmatched { stubComposePort(t, bindings, nil) _, err := CLIPrefix(Profile{DockerDirectory: "/checkout"}, "ws://127.0.0.1:8081/connect", "/host/pipelines") if err == nil || !strings.Contains(err.Error(), "refusing to redirect") { t.Fatalf("%q must not cover 127.0.0.1:8081: %v", bindings, err) } } // A valid binding anywhere in the output still matches. stubComposePort(t, "127.0.0.1:8081\n10.0.0.75:8081\n", nil) if _, err := CLIPrefix(Profile{DockerDirectory: "/checkout"}, "ws://127.0.0.1:8081/connect", "/host/pipelines"); err != nil { t.Fatalf("valid binding among non-matching lines should match: %v", err) } // IPv6 family mismatch the other way: an IPv4 wildcard does not cover ::1. stubComposePort(t, "0.0.0.0:8081\n", nil) if _, err := CLIPrefix(Profile{DockerDirectory: "/checkout"}, "ws://[::1]:8081/connect", "/host/pipelines"); err == nil { t.Fatal("IPv4 wildcard covered an IPv6 loopback request") } } func TestCLIPrefixMalformedOrEmptyBindingsFailClearly(t *testing.T) { for _, output := range []string{"8081\n", "127.0.0.1\n", "not-a-binding\n", "\n", " \n"} { stubComposePort(t, output, nil) _, err := CLIPrefix(Profile{DockerDirectory: "/checkout"}, "ws://127.0.0.1:8081/connect", "/host/pipelines") if err == nil || !strings.Contains(err.Error(), "/checkout") { t.Fatalf("malformed output %q accepted: %v", output, err) } } } // TestCLIPrefixMultipleBindingsUseSingleProbe proves all returned bindings are // matched from ONE bounded probe. func TestCLIPrefixMultipleBindingsUseSingleProbe(t *testing.T) { calls := 0 stubComposePort(t, "10.0.0.75:8081\n127.0.0.1:8081\n[::1]:8081\n", &calls) if _, err := CLIPrefix(Profile{DockerDirectory: "/checkout"}, "ws://127.0.0.1:8081/connect", "/host/pipelines"); err != nil { t.Fatal(err) } if calls != 1 { t.Fatalf("probe calls = %d, want 1", calls) } } // TestCLIPrefixLoopbackWSSRejected pins that a loopback wss endpoint fails clearly // instead of being downgraded to plaintext ws while a remote wss endpoint stays // unchanged without a probe. func TestCLIPrefixLoopbackWSSRejected(t *testing.T) { stubComposePort(t, "0.0.0.0:8081\n", nil) _, err := CLIPrefix(Profile{DockerDirectory: "/checkout"}, "wss://127.0.0.1:8081/connect", "/host/pipelines") if err == nil || !strings.Contains(err.Error(), "no supported local TLS mapping") { t.Fatalf("loopback wss not rejected clearly: %v", err) } forbidProbe(t) prefix, err := CLIPrefix(Profile{DockerDirectory: "/checkout"}, "wss://remote.example:8443/connect", "/host/pipelines") if err != nil { t.Fatal(err) } if strings.Contains(prefix, "PIPELINES_TRANSPORT_") { t.Fatalf("remote wss was rewritten: %s", prefix) } } func TestCLIPrefixUnmatchedLoopbackFailsClosed(t *testing.T) { stubComposePort(t, "0.0.0.0:9999\n", nil) _, err := CLIPrefix(Profile{DockerDirectory: "/checkout"}, "ws://127.0.0.1:8081/connect", "/host/pipelines") if err == nil || !strings.Contains(err.Error(), "refusing to redirect") { t.Fatalf("unmatched loopback accepted: %v", err) } } func TestCLIPrefixNativeUsesHostExecutableWithoutProbe(t *testing.T) { forbidProbe(t) prefix, err := CLIPrefix(Profile{}, "ws://127.0.0.1:8081/connect", "/Users/alan/go/bin/pipelines") if err != nil { t.Fatal(err) } if prefix != "'/Users/alan/go/bin/pipelines' --url 'ws://127.0.0.1:8081/connect'" { t.Fatalf("native prefix = %s", prefix) } } func TestCLIPrefixForUnconfiguredUsesNativePrefix(t *testing.T) { t.Setenv("PIPELINES_HARNESS_CONFIG", t.TempDir()+"/harnesses.json") prefix, err := CLIPrefixFor("", "ws://127.0.0.1:8081/connect", "/host/pipelines") if err != nil { t.Fatal(err) } if prefix != "'/host/pipelines' --url 'ws://127.0.0.1:8081/connect'" { t.Fatalf("unconfigured prefix = %s", prefix) } } func TestShellQuoteHandlesSingleQuotes(t *testing.T) { if got := ShellQuote("a'b"); got != `'a'"'"'b'` { t.Fatalf("quote = %s", got) } }