mirror of
https://github.com/rcourtman/Pulse.git
synced 2026-11-19 23:34:57 +00:00
The loopback stall cost more than two minutes in hosted installer slices. Check each shipped curl argument before scaling only the fixture transfer and retry windows. Require two partial TLS responses, the actual curl timeout exit, preserved installed bytes and no owned staging residue. Contract-Neutral: Test fixture timing only; shipped transport settings, verification scope and installer behavior are unchanged. Change-source: pulse-maintainer
268 lines
11 KiB
Go
268 lines
11 KiB
Go
//go:build !windows
|
|
|
|
package installtests
|
|
|
|
import (
|
|
"context"
|
|
"crypto/sha256"
|
|
"encoding/pem"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
"sync/atomic"
|
|
"syscall"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// The Python controls exercise both workflow phases and real compiled Go tests
|
|
// without recursively running the installer suite. Native CI supplies the
|
|
// platform timing proof; these controls reject lost checks and false passes.
|
|
func TestNativeUnixContractRunnerPartitionsWithoutDroppingChecks(t *testing.T) {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
|
|
defer cancel()
|
|
cmd := exec.CommandContext(ctx, "python3", "-I", "-S", repoFile("scripts", "tests", "test_native_installer_contract_runner.py"))
|
|
output, err := cmd.CombinedOutput()
|
|
if ctx.Err() != nil {
|
|
t.Fatalf("native installer runner control deadline: %v", ctx.Err())
|
|
}
|
|
if err != nil {
|
|
t.Fatalf("native installer runner control: %v\n%s", err, output)
|
|
}
|
|
}
|
|
|
|
// Exercise the whole installer with real curl, TLS, signatures and publication.
|
|
// The curl shim checks the shipped request before directing it to an isolated
|
|
// local peer. Only the stalled fixture shortens two timeout values: real curl
|
|
// still has to expire, retry and exhaust the window on a partial TLS body.
|
|
// The sole source substitution is a disposable signing key.
|
|
// This is a synthetic Unix install, not a released binary or native client proof.
|
|
func TestInstallMCPTransport(t *testing.T) {
|
|
t.Parallel()
|
|
realCurl, err := exec.LookPath("curl")
|
|
if err != nil {
|
|
t.Fatal("curl is required for the MCP installer transport controls")
|
|
}
|
|
for _, command := range []string{"bash", "ssh-keygen"} {
|
|
if _, err := exec.LookPath(command); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
binaryName := "pulse-mcp-" + runtime.GOOS + "-" + runtime.GOARCH
|
|
cases := []struct {
|
|
name, mode, phase string
|
|
trusted, success bool
|
|
}{
|
|
{"complete", "complete", "", true, true},
|
|
{"https_redirects", "redirect", "", true, true},
|
|
{"ambient_proxy_ignored", "proxy", "", true, true},
|
|
{"untrusted_certificate", "complete", "", false, false},
|
|
{"ambient_insecure_ignored", "insecure", "", false, false},
|
|
{"binary_downgrade", "downgrade", binaryName, true, false},
|
|
{"checksum_downgrade", "downgrade", "checksums.txt", true, false},
|
|
{"signature_downgrade", "downgrade", "checksums.txt.sshsig", true, false},
|
|
{"stalled_binary", "stalled", binaryName, true, false},
|
|
}
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
root := t.TempDir()
|
|
tools, downloads, destination := filepath.Join(root, "tools"), filepath.Join(root, "downloads"), filepath.Join(root, "install with spaces")
|
|
for _, path := range []string{tools, downloads, destination} {
|
|
if err := os.Mkdir(path, 0700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
key := filepath.Join(root, "signer")
|
|
runTestCommand(t, exec.Command("ssh-keygen", "-q", "-t", "ed25519", "-N", "", "-f", key))
|
|
publicKey, err := os.ReadFile(key + ".pub")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
script, err := os.ReadFile(repoFile("scripts", "install-mcp.sh"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if strings.Count(string(script), productionMCPReleaseKey) != 1 {
|
|
t.Fatal("unexpected signing authority")
|
|
}
|
|
scriptPath := filepath.Join(root, "install-mcp.sh")
|
|
writeTestFile(t, scriptPath, []byte(strings.Replace(string(script), productionMCPReleaseKey, strings.TrimSpace(string(publicKey)), 1)), 0700)
|
|
replacement := []byte("signed MCP transport fixture\n")
|
|
manifest := []byte(fmt.Sprintf("%x %s\n", sha256.Sum256(replacement), binaryName))
|
|
manifestPath := filepath.Join(root, "checksums.txt")
|
|
writeTestFile(t, manifestPath, manifest, 0600)
|
|
runTestCommand(t, exec.Command("ssh-keygen", "-q", "-Y", "sign", "-f", key, "-n", "pulse-install", manifestPath))
|
|
signature, err := os.ReadFile(manifestPath + ".sig")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
assets := map[string][]byte{binaryName: replacement, "checksums.txt": manifest, "checksums.txt.sshsig": signature}
|
|
var httpRequests, tlsRequests, stalledResponses atomic.Int32
|
|
insecure := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
httpRequests.Add(1)
|
|
_, _ = w.Write(assets[filepath.Base(r.URL.Path)])
|
|
}))
|
|
defer insecure.Close()
|
|
peer := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
tlsRequests.Add(1)
|
|
name := filepath.Base(r.URL.Path)
|
|
data, found := assets[name]
|
|
if !found {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
if !strings.HasPrefix(r.URL.Path, "/bytes/") {
|
|
if tc.mode == "redirect" {
|
|
http.Redirect(w, r, "/bytes/"+name, http.StatusFound)
|
|
return
|
|
}
|
|
if name == tc.phase {
|
|
switch tc.mode {
|
|
case "downgrade":
|
|
http.Redirect(w, r, insecure.URL+"/"+name, http.StatusFound)
|
|
return
|
|
case "stalled":
|
|
w.Header().Set("Content-Length", "100")
|
|
if _, err := io.WriteString(w, "partial bytes"); err == nil {
|
|
w.(http.Flusher).Flush()
|
|
stalledResponses.Add(1)
|
|
}
|
|
<-r.Context().Done()
|
|
return
|
|
}
|
|
}
|
|
}
|
|
_, _ = w.Write(data)
|
|
}))
|
|
defer peer.Close()
|
|
ca := filepath.Join(root, "ca.pem")
|
|
writeTestFile(t, ca, pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: peer.Certificate().Raw}), 0600)
|
|
// Deliberately conflicting curlrc files must not override the
|
|
// installer's verified HTTPS policy or break a healthy download.
|
|
curlrc := ""
|
|
if tc.mode == "proxy" {
|
|
curlrc = "proxy = http://127.0.0.1:1\nnoproxy = \"\"\n"
|
|
}
|
|
if tc.mode == "insecure" {
|
|
curlrc = "insecure\n"
|
|
}
|
|
writeTestFile(t, filepath.Join(root, ".curlrc"), []byte(curlrc), 0600)
|
|
writeTestFile(t, filepath.Join(tools, "curl"), []byte(`#!/bin/bash
|
|
set -euo pipefail
|
|
unexpected_transport() {
|
|
echo 'unexpected shipped MCP curl arguments' >&2
|
|
exit 97
|
|
}
|
|
[[ $# -eq 17 && "${15:-}" == https://github.com/rcourtman/Pulse/releases/download/v-transport/* && "${16:-}" == -o ]] || unexpected_transport
|
|
args=("$@")
|
|
expected=(-q -fsSL --connect-timeout 5 --max-time 60 --retry 3 --retry-max-time 120 --proto '=https' --proto-redir '=https')
|
|
for ((i=0; i<${#expected[@]}; i++)); do
|
|
[[ "${args[i]}" == "${expected[i]}" ]] || unexpected_transport
|
|
done
|
|
args[14]="${MCP_TEST_PEER}/${args[14]##*/}"
|
|
if [[ "$MCP_TEST_MODE" == stalled ]]; then
|
|
# Shorten only the already verified transfer/retry clocks in this local
|
|
# control. With the first 1s backoff, 5/10 permits one retry but not a
|
|
# third attempt (after the second transfer expires).
|
|
args[5]=5
|
|
args[9]=10
|
|
status=0
|
|
"$MCP_TEST_CURL" "${args[@]}" || status=$?
|
|
printf '%s\n' "$status" > "$MCP_TEST_CURL_RESULT"
|
|
exit "$status"
|
|
fi
|
|
exec "$MCP_TEST_CURL" "${args[@]}"
|
|
`), 0700)
|
|
installed := filepath.Join(destination, "pulse-mcp")
|
|
curlResult := filepath.Join(root, "curl-exit")
|
|
old := []byte("previous working adapter\n")
|
|
writeTestFile(t, installed, old, 0700)
|
|
limit := 210 * time.Second
|
|
if tc.mode == "stalled" {
|
|
limit = 30 * time.Second
|
|
}
|
|
ctx, cancel := context.WithTimeout(context.Background(), limit)
|
|
defer cancel()
|
|
cmd := exec.CommandContext(ctx, "bash", scriptPath)
|
|
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
|
|
cmd.Cancel = func() error { return syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL) }
|
|
cmd.WaitDelay = 5 * time.Second
|
|
cmd.Env = append(os.Environ(), "PATH="+tools+string(os.PathListSeparator)+os.Getenv("PATH"),
|
|
"HOME="+root, "CURL_HOME="+root, "XDG_CONFIG_HOME="+root,
|
|
"TMPDIR="+downloads, "PULSE_MCP_BIN_DIR="+destination,
|
|
"PULSE_MCP_VERSION=v-transport", "PULSE_MCP_REPO=rcourtman/Pulse",
|
|
"MCP_TEST_CURL="+realCurl, "MCP_TEST_PEER="+peer.URL, "MCP_TEST_MODE="+tc.mode,
|
|
"MCP_TEST_CURL_RESULT="+curlResult,
|
|
"HTTPS_PROXY=", "https_proxy=", "HTTP_PROXY=", "http_proxy=", "ALL_PROXY=", "all_proxy=",
|
|
"NO_PROXY=", "no_proxy=", "SSL_CERT_FILE=", "CURL_CA_BUNDLE=")
|
|
if tc.trusted {
|
|
cmd.Env = append(cmd.Env, "CURL_CA_BUNDLE="+ca)
|
|
}
|
|
started := time.Now()
|
|
output, runErr := cmd.CombinedOutput()
|
|
elapsed := time.Since(started)
|
|
if strings.Contains(string(output), "unexpected shipped MCP curl arguments") {
|
|
t.Fatalf("shipped argument check refused the request:\n%s", output)
|
|
}
|
|
if ctx.Err() != nil {
|
|
t.Fatalf("installer exceeded its transport bound: %v\n%s", ctx.Err(), output)
|
|
}
|
|
if (runErr == nil) != tc.success {
|
|
t.Fatalf("installer success=%v want=%v: %v\n%s", runErr == nil, tc.success, runErr, output)
|
|
}
|
|
got, err := os.ReadFile(installed)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
info, err := os.Stat(installed)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if tc.success {
|
|
if string(got) != string(replacement) || info.Mode().Perm() != 0755 || !strings.Contains(string(output), "installed:") {
|
|
t.Fatalf("verified publication missing: mode=%v bytes=%q\n%s", info.Mode(), got, output)
|
|
}
|
|
} else {
|
|
if string(got) != string(old) || info.Mode().Perm() != 0700 || strings.Contains(string(output), "installed:") {
|
|
t.Fatalf("failed transport replaced or reported installation: mode=%v bytes=%q\n%s", info.Mode(), got, output)
|
|
}
|
|
diagnostic := "download failed:"
|
|
if tc.phase == "checksums.txt" {
|
|
diagnostic = "could not fetch checksums.txt; refusing unverified install"
|
|
}
|
|
if tc.phase == "checksums.txt.sshsig" {
|
|
diagnostic = "could not fetch checksums.txt.sshsig; refusing unverified install"
|
|
}
|
|
if !strings.Contains(string(output), diagnostic) {
|
|
t.Fatalf("missing phase diagnostic %q\n%s", diagnostic, output)
|
|
}
|
|
}
|
|
if count := httpRequests.Load(); count != 0 {
|
|
t.Fatalf("HTTPS downgrade contacted HTTP peer %d times", count)
|
|
}
|
|
leftovers, err := os.ReadDir(downloads)
|
|
if err != nil || len(leftovers) != 0 {
|
|
t.Fatalf("download cleanup incomplete: %v %v", leftovers, err)
|
|
}
|
|
stages, err := filepath.Glob(filepath.Join(destination, ".pulse-mcp.*"))
|
|
if err != nil || len(stages) != 0 {
|
|
t.Fatalf("staged cleanup incomplete: %v %v", stages, err)
|
|
}
|
|
if tc.mode == "stalled" {
|
|
curlExit, readErr := os.ReadFile(curlResult)
|
|
if readErr != nil || string(curlExit) != "28\n" || tlsRequests.Load() != 2 || stalledResponses.Load() != 2 {
|
|
t.Fatalf("deadline/retry result elapsed=%v requests=%d partial_responses=%d curl_exit=%q read=%v\n%s",
|
|
elapsed, tlsRequests.Load(), stalledResponses.Load(), curlExit, readErr, output)
|
|
}
|
|
t.Logf("two partial responses, curl exit 28; shipped transfer/retry values 60s/120s checked, loopback fixture 5s/10s")
|
|
}
|
|
t.Logf("success=%v tls_requests=%d http_requests=%d elapsed=%s previous_preserved=%v owned_cleanup=complete", tc.success, tlsRequests.Load(), httpRequests.Load(), elapsed.Round(time.Millisecond), !tc.success)
|
|
})
|
|
}
|
|
}
|