mirror of
https://github.com/rcourtman/Pulse.git
synced 2026-11-28 16:58:31 +01:00
Keep restored collectors running after helper rollback with their error status. Require typed post-replacement restart failure before handing over to the service manager. Sync executable bytes and mode before an atomic POSIX replacement and apply the same barriers to persistent copies. Make helper startup evidence authoritative, including an explicit absence, so recovered and later helper starts cannot consume stale local markers. Carry updater evidence through the canonical agent facet and host snapshot with owned status and timestamp copies for fleet diagnostics. Keep fleet diagnostic rows keyed by connection identity so refreshed update evidence does not discard an open disclosure or its keyboard focus.
1737 lines
52 KiB
Go
1737 lines
52 KiB
Go
package agentupdate
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"errors"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/rcourtman/pulse-go-rewrite/internal/securityutil"
|
|
"github.com/rs/zerolog"
|
|
)
|
|
|
|
type roundTripperFunc func(*http.Request) (*http.Response, error)
|
|
|
|
func (f roundTripperFunc) RoundTrip(r *http.Request) (*http.Response, error) {
|
|
return f(r)
|
|
}
|
|
|
|
func TestPrivilegedUpdateRollbackFailurePreservesPendingHandoff(t *testing.T) {
|
|
originalGOOS := runtimeGOOS
|
|
originalRestart := restartProcessFn
|
|
t.Cleanup(func() {
|
|
runtimeGOOS = originalGOOS
|
|
restartProcessFn = originalRestart
|
|
})
|
|
runtimeGOOS = goOSLinux
|
|
restartProcessFn = func(string) error { return errors.New("exec refused") }
|
|
|
|
binary := append([]byte{0x7f, 'E', 'L', 'F'}, bytes.Repeat([]byte("x"), 128)...)
|
|
sum := sha256.Sum256(binary)
|
|
digest := hex.EncodeToString(sum[:])
|
|
helper := &fakePrivilegedUpdate{root: t.TempDir(), rollbackErr: errors.New("helper unavailable")}
|
|
if err := securityutil.HardenPrivatePath(helper.root, 0o700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
u := New(Config{PrivilegedUpdate: helper, Disabled: true, StateDir: helper.root, CurrentVersion: "1.0.0"})
|
|
u.selfTestFn = func(context.Context, string) error { return nil }
|
|
|
|
err := u.performPrivilegedUpdate(context.Background(), "/usr/local/bin/pulse-agent", bytes.NewReader(binary), int64(len(binary)), digest, "signed-update", "1.1.0")
|
|
if err == nil || !strings.Contains(err.Error(), "typed helper rollback failed") {
|
|
t.Fatalf("performPrivilegedUpdate error = %v", err)
|
|
}
|
|
loaded, loadErr := LoadPendingPrivilegedUpdate(helper.root)
|
|
if loadErr != nil || loaded == nil || loaded.Activation.ActivationID != helper.activation.ActivationID ||
|
|
loaded.Activation.ActiveSHA256 != helper.activation.ActiveSHA256 ||
|
|
loaded.Activation.RollbackSHA256 != helper.activation.RollbackSHA256 {
|
|
t.Fatalf("failed-rollback handoff = %#v, %v", loaded, loadErr)
|
|
}
|
|
}
|
|
|
|
func TestUpdateRetryBackoffUsesOperationalDelays(t *testing.T) {
|
|
if updateRetryBaseDelay < time.Second {
|
|
t.Fatalf("updateRetryBaseDelay = %s, want at least 1s", updateRetryBaseDelay)
|
|
}
|
|
if updateRetryMaxDelay < 30*time.Second {
|
|
t.Fatalf("updateRetryMaxDelay = %s, want at least 30s", updateRetryMaxDelay)
|
|
}
|
|
}
|
|
|
|
func testBinary() []byte {
|
|
switch runtime.GOOS {
|
|
case "darwin":
|
|
return []byte{0xcf, 0xfa, 0xed, 0xfe, 0x01, 0x02, 0x03, 0x04}
|
|
case "windows":
|
|
return []byte{'M', 'Z', 0x90, 0x00, 0x01, 0x02, 0x03, 0x04}
|
|
default:
|
|
return []byte{0x7f, 'E', 'L', 'F', 0x01, 0x02, 0x03, 0x04}
|
|
}
|
|
}
|
|
|
|
func checksum(data []byte) string {
|
|
sum := sha256.Sum256(data)
|
|
return hex.EncodeToString(sum[:])
|
|
}
|
|
|
|
func writeTempExec(t *testing.T) (string, string) {
|
|
t.Helper()
|
|
|
|
dir := t.TempDir()
|
|
execPath := filepath.Join(dir, "agent")
|
|
if err := os.WriteFile(execPath, []byte("old-binary"), 0755); err != nil {
|
|
t.Fatalf("write exec: %v", err)
|
|
}
|
|
return dir, execPath
|
|
}
|
|
|
|
func newUpdaterForTest(serverURL string) *Updater {
|
|
cfg := Config{
|
|
PulseURL: serverURL,
|
|
AgentName: "pulse-agent",
|
|
CurrentVersion: "1.0.0",
|
|
CheckInterval: 10 * time.Millisecond,
|
|
}
|
|
u := New(cfg)
|
|
u.selfTestFn = func(context.Context, string) error { return nil }
|
|
return u
|
|
}
|
|
|
|
func selfTestHelperCommand(exitCode string) *exec.Cmd {
|
|
cmd := exec.Command(os.Args[0], "-test.run=TestAgentUpdateSelfTestHelperProcess", "--")
|
|
cmd.Env = append(os.Environ(),
|
|
"PULSE_AGENTUPDATE_SELFTEST_HELPER=1",
|
|
"PULSE_AGENTUPDATE_SELFTEST_EXIT="+exitCode,
|
|
)
|
|
return cmd
|
|
}
|
|
|
|
func TestAgentUpdateSelfTestHelperProcess(t *testing.T) {
|
|
if os.Getenv("PULSE_AGENTUPDATE_SELFTEST_HELPER") != "1" {
|
|
return
|
|
}
|
|
if os.Getenv("PULSE_AGENTUPDATE_SELFTEST_EXIT") != "0" {
|
|
os.Exit(3)
|
|
}
|
|
os.Exit(0)
|
|
}
|
|
|
|
func TestRunDownloadedBinarySelfTestUsesTokenFile(t *testing.T) {
|
|
u := New(Config{APIToken: " token-with-whitespace \n"})
|
|
|
|
origExec := execCommandContextFn
|
|
t.Cleanup(func() { execCommandContextFn = origExec })
|
|
|
|
var (
|
|
sawBinaryPath string
|
|
sawArgs []string
|
|
tokenFilePath string
|
|
tokenContent string
|
|
tokenMode os.FileMode
|
|
)
|
|
execCommandContextFn = func(ctx context.Context, name string, args ...string) *exec.Cmd {
|
|
sawBinaryPath = name
|
|
sawArgs = append([]string(nil), args...)
|
|
if len(args) == 3 {
|
|
tokenFilePath = args[2]
|
|
data, err := os.ReadFile(tokenFilePath)
|
|
if err != nil {
|
|
t.Fatalf("read self-test token file: %v", err)
|
|
}
|
|
tokenContent = string(data)
|
|
info, err := os.Stat(tokenFilePath)
|
|
if err != nil {
|
|
t.Fatalf("stat self-test token file: %v", err)
|
|
}
|
|
tokenMode = info.Mode().Perm()
|
|
}
|
|
return selfTestHelperCommand("0")
|
|
}
|
|
|
|
if err := u.runDownloadedBinarySelfTest(context.Background(), "/tmp/pulse-agent-new"); err != nil {
|
|
t.Fatalf("runDownloadedBinarySelfTest: %v", err)
|
|
}
|
|
|
|
if sawBinaryPath != "/tmp/pulse-agent-new" {
|
|
t.Fatalf("self-test binary path = %q", sawBinaryPath)
|
|
}
|
|
if len(sawArgs) != 3 || sawArgs[0] != "--self-test" || sawArgs[1] != "--token-file" || sawArgs[2] == "" {
|
|
t.Fatalf("unexpected self-test args: %#v", sawArgs)
|
|
}
|
|
if tokenContent != "token-with-whitespace" {
|
|
t.Fatalf("self-test token content = %q", tokenContent)
|
|
}
|
|
if runtime.GOOS != "windows" && tokenMode != 0o600 {
|
|
t.Fatalf("self-test token mode = %o, want 0600", tokenMode)
|
|
}
|
|
if _, err := os.Stat(tokenFilePath); !errors.Is(err, os.ErrNotExist) {
|
|
t.Fatalf("expected self-test token file cleanup, got err=%v", err)
|
|
}
|
|
}
|
|
|
|
func TestRunDownloadedBinarySelfTestPropagatesFailureWithoutTokenArg(t *testing.T) {
|
|
u := New(Config{})
|
|
|
|
origExec := execCommandContextFn
|
|
t.Cleanup(func() { execCommandContextFn = origExec })
|
|
|
|
var sawArgs []string
|
|
execCommandContextFn = func(ctx context.Context, name string, args ...string) *exec.Cmd {
|
|
sawArgs = append([]string(nil), args...)
|
|
return selfTestHelperCommand("3")
|
|
}
|
|
|
|
err := u.runDownloadedBinarySelfTest(context.Background(), "/tmp/pulse-agent-new")
|
|
if err == nil || !strings.Contains(err.Error(), "new pulse-agent binary failed self-test") {
|
|
t.Fatalf("expected self-test failure, got %v", err)
|
|
}
|
|
if len(sawArgs) != 1 || sawArgs[0] != "--self-test" {
|
|
t.Fatalf("unexpected self-test args without token: %#v", sawArgs)
|
|
}
|
|
}
|
|
|
|
func TestDetermineArchOverrides(t *testing.T) {
|
|
origOS, origArch, origUname := runtimeGOOS, runtimeGOARCH, unameCommand
|
|
t.Cleanup(func() {
|
|
runtimeGOOS = origOS
|
|
runtimeGOARCH = origArch
|
|
unameCommand = origUname
|
|
})
|
|
|
|
runtimeGOOS = "linux"
|
|
runtimeGOARCH = "arm"
|
|
if got := determineArch(); got != "linux-armv7" {
|
|
t.Fatalf("expected linux-armv7, got %q", got)
|
|
}
|
|
|
|
runtimeGOARCH = "386"
|
|
if got := determineArch(); got != "linux-386" {
|
|
t.Fatalf("expected linux-386, got %q", got)
|
|
}
|
|
|
|
runtimeGOOS = "freebsd"
|
|
runtimeGOARCH = "amd64"
|
|
if got := determineArch(); got != "freebsd-amd64" {
|
|
t.Fatalf("expected freebsd-amd64, got %q", got)
|
|
}
|
|
|
|
runtimeGOARCH = "arm64"
|
|
if got := determineArch(); got != "freebsd-arm64" {
|
|
t.Fatalf("expected freebsd-arm64, got %q", got)
|
|
}
|
|
|
|
runtimeGOOS = "solaris"
|
|
runtimeGOARCH = "amd64"
|
|
unameCommand = func() ([]byte, error) { return []byte("aarch64"), nil }
|
|
if got := determineArch(); got != "linux-arm64" {
|
|
t.Fatalf("expected linux-arm64, got %q", got)
|
|
}
|
|
|
|
unameCommand = func() ([]byte, error) { return []byte("x86_64"), nil }
|
|
if got := determineArch(); got != "linux-amd64" {
|
|
t.Fatalf("expected linux-amd64, got %q", got)
|
|
}
|
|
|
|
unameCommand = func() ([]byte, error) { return []byte("armv7l"), nil }
|
|
if got := determineArch(); got != "linux-armv7" {
|
|
t.Fatalf("expected linux-armv7, got %q", got)
|
|
}
|
|
|
|
unameCommand = func() ([]byte, error) { return []byte("mips"), nil }
|
|
if got := determineArch(); got != "" {
|
|
t.Fatalf("expected empty arch for unknown uname, got %q", got)
|
|
}
|
|
|
|
unameCommand = func() ([]byte, error) { return nil, errors.New("fail") }
|
|
if got := determineArch(); got != "" {
|
|
t.Fatalf("expected empty arch on uname error, got %q", got)
|
|
}
|
|
}
|
|
|
|
func TestUnameCommandDefault(t *testing.T) {
|
|
if _, err := exec.LookPath("uname"); err != nil {
|
|
t.Skip("uname not available")
|
|
}
|
|
|
|
orig := unameCommand
|
|
t.Cleanup(func() { unameCommand = orig })
|
|
unameCommand = orig
|
|
|
|
if _, err := unameCommand(); err != nil {
|
|
t.Fatalf("expected uname to run, got %v", err)
|
|
}
|
|
}
|
|
|
|
func TestNormalizeAgentName(t *testing.T) {
|
|
t.Run("valid", func(t *testing.T) {
|
|
got, err := normalizeAgentName("pulse-agent_1.2")
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if got != "pulse-agent_1.2" {
|
|
t.Fatalf("unexpected normalized name: %q", got)
|
|
}
|
|
})
|
|
|
|
t.Run("invalid", func(t *testing.T) {
|
|
cases := []string{"", " ", "../pulse-agent", "pulse/agent", "pulse\\agent", "pulse agent", "pulse@agent"}
|
|
for _, tc := range cases {
|
|
if _, err := normalizeAgentName(tc); err == nil {
|
|
t.Fatalf("expected invalid agent name for %q", tc)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestIsLoopbackHost(t *testing.T) {
|
|
t.Run("true", func(t *testing.T) {
|
|
cases := []string{"localhost", "LOCALHOST", "agent.localhost", "127.0.0.1", "::1"}
|
|
for _, tc := range cases {
|
|
if !securityutil.IsLoopbackHost(tc) {
|
|
t.Fatalf("expected loopback host for %q", tc)
|
|
}
|
|
}
|
|
})
|
|
|
|
t.Run("false", func(t *testing.T) {
|
|
cases := []string{"", "example.com", "192.168.1.10", "10.0.0.5"}
|
|
for _, tc := range cases {
|
|
if securityutil.IsLoopbackHost(tc) {
|
|
t.Fatalf("expected non-loopback host for %q", tc)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestValidatePulseURL(t *testing.T) {
|
|
t.Run("AllowHTTPS", func(t *testing.T) {
|
|
u := newUpdaterForTest("https://pulse.example.com")
|
|
if err := u.validatePulseURL(); err != nil {
|
|
t.Fatalf("expected https URL to be valid, got %v", err)
|
|
}
|
|
})
|
|
|
|
t.Run("AllowLoopbackHTTP", func(t *testing.T) {
|
|
u := newUpdaterForTest("http://127.0.0.1:7655")
|
|
if err := u.validatePulseURL(); err != nil {
|
|
t.Fatalf("expected loopback http URL to be valid, got %v", err)
|
|
}
|
|
})
|
|
|
|
t.Run("RejectRemoteHTTP", func(t *testing.T) {
|
|
u := newUpdaterForTest("http://pulse.example.com")
|
|
if err := u.validatePulseURL(); err == nil {
|
|
t.Fatalf("expected remote http URL to be rejected")
|
|
}
|
|
})
|
|
|
|
t.Run("RejectPublicHTTPInInsecureMode", func(t *testing.T) {
|
|
u := newUpdaterForTest("http://pulse.example.com")
|
|
u.cfg.InsecureSkipVerify = true
|
|
if err := u.validatePulseURL(); err == nil {
|
|
t.Fatalf("expected public http URL in insecure mode to be rejected")
|
|
}
|
|
})
|
|
|
|
t.Run("AllowPrivateNetworkHTTP", func(t *testing.T) {
|
|
u := newUpdaterForTest("http://10.0.0.5:7655")
|
|
if err := u.validatePulseURL(); err != nil {
|
|
t.Fatalf("expected private-network http URL to be allowed, got %v", err)
|
|
}
|
|
})
|
|
|
|
t.Run("AllowHomeDomainHTTP", func(t *testing.T) {
|
|
u := newUpdaterForTest("http://ct-pulse.home:7655")
|
|
if err := u.validatePulseURL(); err != nil {
|
|
t.Fatalf("expected .home http URL to be allowed, got %v", err)
|
|
}
|
|
})
|
|
|
|
t.Run("RejectUnsupportedScheme", func(t *testing.T) {
|
|
u := newUpdaterForTest("ftp://pulse.example.com")
|
|
if err := u.validatePulseURL(); err == nil {
|
|
t.Fatalf("expected unsupported scheme to be rejected")
|
|
}
|
|
})
|
|
|
|
t.Run("RejectUserCredentials", func(t *testing.T) {
|
|
u := newUpdaterForTest("https://user:pass@pulse.example.com")
|
|
if err := u.validatePulseURL(); err == nil {
|
|
t.Fatalf("expected URL with credentials to be rejected")
|
|
}
|
|
})
|
|
|
|
t.Run("RejectQuery", func(t *testing.T) {
|
|
u := newUpdaterForTest("https://pulse.example.com?redirect=1")
|
|
if err := u.validatePulseURL(); err == nil {
|
|
t.Fatalf("expected URL with query to be rejected")
|
|
}
|
|
})
|
|
|
|
t.Run("RejectFragment", func(t *testing.T) {
|
|
u := newUpdaterForTest("https://pulse.example.com#frag")
|
|
if err := u.validatePulseURL(); err == nil {
|
|
t.Fatalf("expected URL with fragment to be rejected")
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestPerformUpdateWithExecPathInvalidAgentName(t *testing.T) {
|
|
u := newUpdaterForTest("http://localhost")
|
|
u.cfg.AgentName = "../pulse-agent"
|
|
|
|
_, execPath := writeTempExec(t)
|
|
if err := u.performUpdateWithExecPath(context.Background(), execPath); err == nil {
|
|
t.Fatalf("expected invalid agent name error")
|
|
}
|
|
}
|
|
|
|
func TestVerifyBinaryMagicOverrides(t *testing.T) {
|
|
origOS := runtimeGOOS
|
|
t.Cleanup(func() { runtimeGOOS = origOS })
|
|
|
|
tmpDir := t.TempDir()
|
|
|
|
runtimeGOOS = "linux"
|
|
elfPath := filepath.Join(tmpDir, "elf")
|
|
elfData := []byte{0x7f, 'E', 'L', 'F', 0x00}
|
|
if err := os.WriteFile(elfPath, elfData, 0644); err != nil {
|
|
t.Fatalf("write elf: %v", err)
|
|
}
|
|
if err := verifyBinaryMagic(elfPath); err != nil {
|
|
t.Fatalf("expected ELF to validate, got %v", err)
|
|
}
|
|
|
|
badELFPath := filepath.Join(tmpDir, "bad-elf")
|
|
if err := os.WriteFile(badELFPath, []byte{0x00, 0x00, 0x00, 0x00}, 0644); err != nil {
|
|
t.Fatalf("write bad elf: %v", err)
|
|
}
|
|
if err := verifyBinaryMagic(badELFPath); err == nil {
|
|
t.Fatalf("expected ELF error")
|
|
}
|
|
|
|
runtimeGOOS = "freebsd"
|
|
if err := verifyBinaryMagic(elfPath); err != nil {
|
|
t.Fatalf("expected FreeBSD ELF to validate, got %v", err)
|
|
}
|
|
if err := verifyBinaryMagic(badELFPath); err == nil {
|
|
t.Fatalf("expected FreeBSD ELF error")
|
|
}
|
|
|
|
runtimeGOOS = "darwin"
|
|
machoPath := filepath.Join(tmpDir, "macho")
|
|
machoData := []byte{0xcf, 0xfa, 0xed, 0xfe, 0x00}
|
|
if err := os.WriteFile(machoPath, machoData, 0644); err != nil {
|
|
t.Fatalf("write macho: %v", err)
|
|
}
|
|
if err := verifyBinaryMagic(machoPath); err != nil {
|
|
t.Fatalf("expected macho to validate, got %v", err)
|
|
}
|
|
|
|
badPath := filepath.Join(tmpDir, "macho-bad")
|
|
if err := os.WriteFile(badPath, []byte{0x00, 0x00, 0x00, 0x00}, 0644); err != nil {
|
|
t.Fatalf("write bad macho: %v", err)
|
|
}
|
|
if err := verifyBinaryMagic(badPath); err == nil {
|
|
t.Fatalf("expected macho error")
|
|
}
|
|
|
|
runtimeGOOS = "windows"
|
|
pePath := filepath.Join(tmpDir, "pe.exe")
|
|
if err := os.WriteFile(pePath, []byte{'M', 'Z', 0x00, 0x00}, 0644); err != nil {
|
|
t.Fatalf("write pe: %v", err)
|
|
}
|
|
if err := verifyBinaryMagic(pePath); err != nil {
|
|
t.Fatalf("expected PE to validate, got %v", err)
|
|
}
|
|
|
|
badPEPath := filepath.Join(tmpDir, "bad-pe.exe")
|
|
if err := os.WriteFile(badPEPath, []byte{0x00, 0x00, 0x00, 0x00}, 0644); err != nil {
|
|
t.Fatalf("write bad pe: %v", err)
|
|
}
|
|
if err := verifyBinaryMagic(badPEPath); err == nil {
|
|
t.Fatalf("expected PE error")
|
|
}
|
|
|
|
runtimeGOOS = "plan9"
|
|
planPath := filepath.Join(tmpDir, "plan9")
|
|
if err := os.WriteFile(planPath, []byte{0x00, 0x01, 0x02, 0x03}, 0644); err != nil {
|
|
t.Fatalf("write plan9: %v", err)
|
|
}
|
|
if err := verifyBinaryMagic(planPath); err == nil {
|
|
t.Fatalf("expected unknown OS to fail closed")
|
|
}
|
|
}
|
|
|
|
func TestIsUnraidOverride(t *testing.T) {
|
|
orig := unraidVersionPath
|
|
t.Cleanup(func() { unraidVersionPath = orig })
|
|
|
|
tmpDir := t.TempDir()
|
|
unraidVersionPath = filepath.Join(tmpDir, "unraid-version")
|
|
if isUnraid() {
|
|
t.Fatalf("expected false when file missing")
|
|
}
|
|
if err := os.WriteFile(unraidVersionPath, []byte("6.12"), 0644); err != nil {
|
|
t.Fatalf("write unraid: %v", err)
|
|
}
|
|
if !isUnraid() {
|
|
t.Fatalf("expected true when file exists")
|
|
}
|
|
}
|
|
|
|
func TestGetServerVersion(t *testing.T) {
|
|
t.Run("Success", func(t *testing.T) {
|
|
var sawToken bool
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Header.Get("X-API-Token") == "token" && strings.HasPrefix(r.Header.Get("Authorization"), "Bearer ") {
|
|
sawToken = true
|
|
}
|
|
_, _ = w.Write([]byte(`{"version":"1.2.3"}`))
|
|
}))
|
|
t.Cleanup(server.Close)
|
|
|
|
u := newUpdaterForTest(server.URL)
|
|
u.cfg.APIToken = "token"
|
|
u.client = server.Client()
|
|
|
|
version, err := u.getServerVersion(context.Background())
|
|
if err != nil {
|
|
t.Fatalf("getServerVersion error: %v", err)
|
|
}
|
|
if version != "1.2.3" {
|
|
t.Fatalf("expected version 1.2.3, got %q", version)
|
|
}
|
|
if !sawToken {
|
|
t.Fatalf("expected token headers to be set")
|
|
}
|
|
})
|
|
|
|
t.Run("InvalidURL", func(t *testing.T) {
|
|
u := newUpdaterForTest("http://[::1")
|
|
if _, err := u.getServerVersion(context.Background()); err == nil {
|
|
t.Fatalf("expected error for invalid URL")
|
|
}
|
|
})
|
|
|
|
t.Run("RequestError", func(t *testing.T) {
|
|
u := newUpdaterForTest("https://example")
|
|
u.client = &http.Client{
|
|
Transport: roundTripperFunc(func(*http.Request) (*http.Response, error) {
|
|
return nil, errors.New("boom")
|
|
}),
|
|
}
|
|
if _, err := u.getServerVersion(context.Background()); err == nil {
|
|
t.Fatalf("expected request error")
|
|
}
|
|
})
|
|
|
|
t.Run("StatusError", func(t *testing.T) {
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
}))
|
|
t.Cleanup(server.Close)
|
|
|
|
u := newUpdaterForTest(server.URL)
|
|
u.client = server.Client()
|
|
if _, err := u.getServerVersion(context.Background()); err == nil {
|
|
t.Fatalf("expected status error")
|
|
}
|
|
})
|
|
|
|
t.Run("DecodeError", func(t *testing.T) {
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
_, _ = w.Write([]byte("{"))
|
|
}))
|
|
t.Cleanup(server.Close)
|
|
|
|
u := newUpdaterForTest(server.URL)
|
|
u.client = server.Client()
|
|
if _, err := u.getServerVersion(context.Background()); err == nil {
|
|
t.Fatalf("expected decode error")
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestCheckAndUpdateBranches(t *testing.T) {
|
|
t.Run("Disabled", func(t *testing.T) {
|
|
u := newUpdaterForTest("https://example")
|
|
u.cfg.Disabled = true
|
|
u.performUpdateFn = func(context.Context, string) error {
|
|
t.Fatalf("should not update when disabled")
|
|
return nil
|
|
}
|
|
u.CheckAndUpdate(context.Background())
|
|
})
|
|
|
|
t.Run("DevCurrent", func(t *testing.T) {
|
|
u := newUpdaterForTest("https://example")
|
|
u.cfg.CurrentVersion = "dev"
|
|
u.performUpdateFn = func(context.Context, string) error {
|
|
t.Fatalf("should not update in dev mode")
|
|
return nil
|
|
}
|
|
u.CheckAndUpdate(context.Background())
|
|
})
|
|
|
|
t.Run("NoPulseURL", func(t *testing.T) {
|
|
u := newUpdaterForTest("")
|
|
u.performUpdateFn = func(context.Context, string) error {
|
|
t.Fatalf("should not update without URL")
|
|
return nil
|
|
}
|
|
u.CheckAndUpdate(context.Background())
|
|
})
|
|
|
|
t.Run("InsecureHTTPURL", func(t *testing.T) {
|
|
u := newUpdaterForTest("http://pulse.example.com")
|
|
u.performUpdateFn = func(context.Context, string) error {
|
|
t.Fatalf("should not update with insecure Pulse URL")
|
|
return nil
|
|
}
|
|
u.CheckAndUpdate(context.Background())
|
|
})
|
|
|
|
t.Run("ServerError", func(t *testing.T) {
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
}))
|
|
t.Cleanup(server.Close)
|
|
|
|
u := newUpdaterForTest(server.URL)
|
|
u.client = server.Client()
|
|
u.performUpdateFn = func(context.Context, string) error {
|
|
t.Fatalf("should not update on server error")
|
|
return nil
|
|
}
|
|
u.CheckAndUpdate(context.Background())
|
|
})
|
|
|
|
t.Run("ServerDev", func(t *testing.T) {
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
_, _ = w.Write([]byte(`{"version":"dev"}`))
|
|
}))
|
|
t.Cleanup(server.Close)
|
|
|
|
u := newUpdaterForTest(server.URL)
|
|
u.client = server.Client()
|
|
u.performUpdateFn = func(context.Context, string) error {
|
|
t.Fatalf("should not update when server dev")
|
|
return nil
|
|
}
|
|
u.CheckAndUpdate(context.Background())
|
|
})
|
|
|
|
t.Run("UpToDate", func(t *testing.T) {
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
_, _ = w.Write([]byte(`{"version":"1.0.0"}`))
|
|
}))
|
|
t.Cleanup(server.Close)
|
|
|
|
u := newUpdaterForTest(server.URL)
|
|
u.client = server.Client()
|
|
u.performUpdateFn = func(context.Context, string) error {
|
|
t.Fatalf("should not update when up to date")
|
|
return nil
|
|
}
|
|
u.CheckAndUpdate(context.Background())
|
|
})
|
|
|
|
t.Run("ServerOlder", func(t *testing.T) {
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
_, _ = w.Write([]byte(`{"version":"0.9.0"}`))
|
|
}))
|
|
t.Cleanup(server.Close)
|
|
|
|
u := newUpdaterForTest(server.URL)
|
|
u.client = server.Client()
|
|
u.performUpdateFn = func(context.Context, string) error {
|
|
t.Fatalf("should not downgrade")
|
|
return nil
|
|
}
|
|
u.CheckAndUpdate(context.Background())
|
|
})
|
|
|
|
t.Run("ServerNewer", func(t *testing.T) {
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
_, _ = w.Write([]byte(`{"version":"1.1.0"}`))
|
|
}))
|
|
t.Cleanup(server.Close)
|
|
|
|
u := newUpdaterForTest(server.URL)
|
|
u.client = server.Client()
|
|
var called int32
|
|
u.performUpdateFn = func(context.Context, string) error {
|
|
atomic.AddInt32(&called, 1)
|
|
return nil
|
|
}
|
|
u.CheckAndUpdate(context.Background())
|
|
if called != 1 {
|
|
t.Fatalf("expected update to be called")
|
|
}
|
|
})
|
|
|
|
t.Run("UpdateError", func(t *testing.T) {
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
_, _ = w.Write([]byte(`{"version":"1.1.0"}`))
|
|
}))
|
|
t.Cleanup(server.Close)
|
|
|
|
u := newUpdaterForTest(server.URL)
|
|
u.client = server.Client()
|
|
var called int32
|
|
u.performUpdateFn = func(context.Context, string) error {
|
|
atomic.AddInt32(&called, 1)
|
|
return errors.New("fail")
|
|
}
|
|
u.CheckAndUpdate(context.Background())
|
|
if called != 1 {
|
|
t.Fatalf("expected update to be called")
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestRunLoop(t *testing.T) {
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
_, _ = w.Write([]byte(`{"version":"1.1.0"}`))
|
|
}))
|
|
t.Cleanup(server.Close)
|
|
|
|
u := newUpdaterForTest(server.URL)
|
|
u.client = server.Client()
|
|
u.initialDelay = 0
|
|
u.newTicker = func(d time.Duration) *time.Ticker {
|
|
return time.NewTicker(5 * time.Millisecond)
|
|
}
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
|
|
var called int32
|
|
u.performUpdateFn = func(context.Context, string) error {
|
|
if atomic.AddInt32(&called, 1) >= 2 {
|
|
cancel()
|
|
}
|
|
return nil
|
|
}
|
|
|
|
done := make(chan struct{})
|
|
go func() {
|
|
u.RunLoop(ctx)
|
|
close(done)
|
|
}()
|
|
|
|
select {
|
|
case <-done:
|
|
case <-time.After(200 * time.Millisecond):
|
|
t.Fatalf("RunLoop did not exit")
|
|
}
|
|
|
|
if atomic.LoadInt32(&called) < 1 {
|
|
t.Fatalf("expected RunLoop to invoke update")
|
|
}
|
|
}
|
|
|
|
func TestRunLoopEarlyExit(t *testing.T) {
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
cancel()
|
|
|
|
u := newUpdaterForTest("https://example")
|
|
u.cfg.Disabled = true
|
|
|
|
done := make(chan struct{})
|
|
go func() {
|
|
u.RunLoop(ctx)
|
|
close(done)
|
|
}()
|
|
|
|
select {
|
|
case <-done:
|
|
case <-time.After(50 * time.Millisecond):
|
|
t.Fatalf("expected RunLoop to exit quickly")
|
|
}
|
|
|
|
u2 := newUpdaterForTest("https://example")
|
|
u2.cfg.CurrentVersion = "dev"
|
|
|
|
done2 := make(chan struct{})
|
|
go func() {
|
|
u2.RunLoop(context.Background())
|
|
close(done2)
|
|
}()
|
|
|
|
select {
|
|
case <-done2:
|
|
case <-time.After(50 * time.Millisecond):
|
|
t.Fatalf("expected RunLoop to exit for dev")
|
|
}
|
|
}
|
|
|
|
func TestRunLoopInitialCancel(t *testing.T) {
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
cancel()
|
|
|
|
u := newUpdaterForTest("https://example")
|
|
u.initialDelay = 5 * time.Second
|
|
|
|
done := make(chan struct{})
|
|
go func() {
|
|
u.RunLoop(ctx)
|
|
close(done)
|
|
}()
|
|
|
|
select {
|
|
case <-done:
|
|
case <-time.After(50 * time.Millisecond):
|
|
t.Fatalf("expected RunLoop to exit on cancel")
|
|
}
|
|
}
|
|
|
|
func TestGetUpdatedFromVersion(t *testing.T) {
|
|
origExec := osExecutableFn
|
|
origEval := evalSymlinksFn
|
|
t.Cleanup(func() {
|
|
osExecutableFn = origExec
|
|
evalSymlinksFn = origEval
|
|
})
|
|
|
|
tmpDir := t.TempDir()
|
|
execPath := filepath.Join(tmpDir, "agent")
|
|
infoPath := filepath.Join(tmpDir, ".pulse-update-info")
|
|
if err := os.WriteFile(infoPath, []byte("1.0.0"), 0644); err != nil {
|
|
t.Fatalf("write update info: %v", err)
|
|
}
|
|
|
|
osExecutableFn = func() (string, error) { return execPath, nil }
|
|
evalSymlinksFn = func(string) (string, error) { return execPath, nil }
|
|
|
|
version := GetUpdatedFromVersion()
|
|
if version != "1.0.0" {
|
|
t.Fatalf("expected version 1.0.0, got %q", version)
|
|
}
|
|
if _, err := os.Stat(infoPath); err == nil {
|
|
t.Fatalf("expected update info file to be removed")
|
|
}
|
|
|
|
missingPath := filepath.Join(tmpDir, "agent-missing")
|
|
osExecutableFn = func() (string, error) { return missingPath, nil }
|
|
evalSymlinksFn = func(string) (string, error) { return "", errors.New("fail") }
|
|
if GetUpdatedFromVersion() != "" {
|
|
t.Fatalf("expected empty version on missing file")
|
|
}
|
|
|
|
osExecutableFn = func() (string, error) { return "", errors.New("fail") }
|
|
if GetUpdatedFromVersion() != "" {
|
|
t.Fatalf("expected empty version on error")
|
|
}
|
|
}
|
|
|
|
func TestPerformUpdatePersistsPreviousVersionForNextStart(t *testing.T) {
|
|
data := testBinary()
|
|
check := checksum(data)
|
|
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("X-Checksum-Sha256", check)
|
|
_, _ = w.Write(data)
|
|
}))
|
|
t.Cleanup(server.Close)
|
|
|
|
_, execPath := writeTempExec(t)
|
|
u := newUpdaterForTest(server.URL)
|
|
u.cfg.CurrentVersion = "5.1.14"
|
|
u.client = server.Client()
|
|
|
|
origRestart := restartProcessFn
|
|
origExec := osExecutableFn
|
|
origEval := evalSymlinksFn
|
|
t.Cleanup(func() {
|
|
restartProcessFn = origRestart
|
|
osExecutableFn = origExec
|
|
evalSymlinksFn = origEval
|
|
})
|
|
|
|
restartProcessFn = func(string) error { return nil }
|
|
osExecutableFn = func() (string, error) { return execPath, nil }
|
|
evalSymlinksFn = func(string) (string, error) { return execPath, nil }
|
|
|
|
if err := u.performUpdateWithExecPath(context.Background(), execPath); err != nil {
|
|
t.Fatalf("performUpdateWithExecPath: %v", err)
|
|
}
|
|
|
|
if got := GetUpdatedFromVersion(); got != "5.1.14" {
|
|
t.Fatalf("GetUpdatedFromVersion() = %q, want %q", got, "5.1.14")
|
|
}
|
|
if got := GetUpdatedFromVersion(); got != "" {
|
|
t.Fatalf("GetUpdatedFromVersion() second read = %q, want empty string", got)
|
|
}
|
|
}
|
|
|
|
func TestPerformUpdateWrapper(t *testing.T) {
|
|
t.Run("ExecPathError", func(t *testing.T) {
|
|
origExec := osExecutableFn
|
|
t.Cleanup(func() { osExecutableFn = origExec })
|
|
osExecutableFn = func() (string, error) { return "", errors.New("fail") }
|
|
|
|
u := newUpdaterForTest("https://example")
|
|
if err := u.performUpdate(context.Background()); err == nil {
|
|
t.Fatalf("expected exec path error")
|
|
}
|
|
})
|
|
|
|
t.Run("Success", func(t *testing.T) {
|
|
data := testBinary()
|
|
check := checksum(data)
|
|
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("X-Checksum-Sha256", check)
|
|
_, _ = w.Write(data)
|
|
}))
|
|
t.Cleanup(server.Close)
|
|
|
|
_, execPath := writeTempExec(t)
|
|
u := newUpdaterForTest(server.URL)
|
|
u.client = server.Client()
|
|
|
|
origExec := osExecutableFn
|
|
origRestart := restartProcessFn
|
|
t.Cleanup(func() {
|
|
osExecutableFn = origExec
|
|
restartProcessFn = origRestart
|
|
})
|
|
osExecutableFn = func() (string, error) { return execPath, nil }
|
|
restartProcessFn = func(string) error { return nil }
|
|
|
|
if err := u.performUpdate(context.Background()); err != nil {
|
|
t.Fatalf("expected update success, got %v", err)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestPerformUpdateInvalidRequest(t *testing.T) {
|
|
u := newUpdaterForTest("http://[::1")
|
|
if err := u.performUpdateWithExecPath(context.Background(), ""); err == nil {
|
|
t.Fatalf("expected error for invalid URL")
|
|
}
|
|
}
|
|
|
|
func TestPerformUpdateDownloadErrorAndHeaders(t *testing.T) {
|
|
_, execPath := writeTempExec(t)
|
|
u := newUpdaterForTest("https://example")
|
|
u.cfg.APIToken = "token"
|
|
|
|
var sawToken bool
|
|
u.client = &http.Client{
|
|
Transport: roundTripperFunc(func(r *http.Request) (*http.Response, error) {
|
|
if r.Header.Get("X-API-Token") == "token" && strings.HasPrefix(r.Header.Get("Authorization"), "Bearer ") {
|
|
sawToken = true
|
|
}
|
|
return nil, errors.New("download fail")
|
|
}),
|
|
}
|
|
|
|
if err := u.performUpdateWithExecPath(context.Background(), execPath); err == nil {
|
|
t.Fatalf("expected download error")
|
|
}
|
|
if !sawToken {
|
|
t.Fatalf("expected auth headers to be set")
|
|
}
|
|
}
|
|
|
|
func TestPerformUpdateDownloadRetriesTransientError(t *testing.T) {
|
|
_, execPath := writeTempExec(t)
|
|
u := newUpdaterForTest("https://example")
|
|
u.cfg.APIToken = "token"
|
|
|
|
data := testBinary()
|
|
check := checksum(data)
|
|
|
|
origRestart := restartProcessFn
|
|
origRetrySleep := retrySleepFn
|
|
t.Cleanup(func() {
|
|
restartProcessFn = origRestart
|
|
retrySleepFn = origRetrySleep
|
|
})
|
|
restartProcessFn = func(string) error { return nil }
|
|
retrySleepFn = func(context.Context, time.Duration) error { return nil }
|
|
|
|
var calls int32
|
|
var sawToken bool
|
|
u.client = &http.Client{
|
|
Transport: roundTripperFunc(func(r *http.Request) (*http.Response, error) {
|
|
if r.Header.Get("X-API-Token") == "token" && strings.HasPrefix(r.Header.Get("Authorization"), "Bearer ") {
|
|
sawToken = true
|
|
}
|
|
|
|
if atomic.AddInt32(&calls, 1) < 3 {
|
|
return nil, errors.New("transient download error")
|
|
}
|
|
|
|
return &http.Response{
|
|
StatusCode: http.StatusOK,
|
|
Status: "200 OK",
|
|
Body: io.NopCloser(bytes.NewReader(data)),
|
|
Header: http.Header{"X-Checksum-Sha256": []string{check}},
|
|
}, nil
|
|
}),
|
|
}
|
|
|
|
if err := u.performUpdateWithExecPath(context.Background(), execPath); err != nil {
|
|
t.Fatalf("expected retry recovery success, got %v", err)
|
|
}
|
|
if got := atomic.LoadInt32(&calls); got != 3 {
|
|
t.Fatalf("expected 3 attempts, got %d", got)
|
|
}
|
|
if !sawToken {
|
|
t.Fatalf("expected auth headers to be set")
|
|
}
|
|
}
|
|
|
|
func TestPerformUpdateStatusFallbackAndSuccess(t *testing.T) {
|
|
data := testBinary()
|
|
check := checksum(data)
|
|
|
|
var calls int32
|
|
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
atomic.AddInt32(&calls, 1)
|
|
if strings.Contains(r.URL.RawQuery, "arch=") {
|
|
w.WriteHeader(http.StatusNotFound)
|
|
return
|
|
}
|
|
w.Header().Set("X-Checksum-Sha256", check)
|
|
_, _ = w.Write(data)
|
|
}))
|
|
t.Cleanup(server.Close)
|
|
|
|
_, execPath := writeTempExec(t)
|
|
|
|
u := newUpdaterForTest(server.URL)
|
|
u.client = server.Client()
|
|
|
|
origRestart := restartProcessFn
|
|
t.Cleanup(func() { restartProcessFn = origRestart })
|
|
restartProcessFn = func(string) error { return nil }
|
|
|
|
if err := u.performUpdateWithExecPath(context.Background(), execPath); err != nil {
|
|
t.Fatalf("expected update success, got %v", err)
|
|
}
|
|
|
|
updated, err := os.ReadFile(execPath)
|
|
if err != nil {
|
|
t.Fatalf("read updated exec: %v", err)
|
|
}
|
|
if !bytes.HasPrefix(updated, testBinary()[:4]) {
|
|
t.Fatalf("expected updated binary content")
|
|
}
|
|
if got := atomic.LoadInt32(&calls); got != 2 {
|
|
t.Fatalf("expected arch fallback with no 404 retries (2 calls), got %d", got)
|
|
}
|
|
}
|
|
|
|
func TestPerformUpdateSymlinkFallback(t *testing.T) {
|
|
data := testBinary()
|
|
check := checksum(data)
|
|
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("X-Checksum-Sha256", check)
|
|
_, _ = w.Write(data)
|
|
}))
|
|
t.Cleanup(server.Close)
|
|
|
|
_, execPath := writeTempExec(t)
|
|
u := newUpdaterForTest(server.URL)
|
|
u.client = server.Client()
|
|
|
|
origEval := evalSymlinksFn
|
|
origRestart := restartProcessFn
|
|
t.Cleanup(func() {
|
|
evalSymlinksFn = origEval
|
|
restartProcessFn = origRestart
|
|
})
|
|
evalSymlinksFn = func(string) (string, error) { return "", errors.New("fail") }
|
|
restartProcessFn = func(string) error { return nil }
|
|
|
|
if err := u.performUpdateWithExecPath(context.Background(), execPath); err != nil {
|
|
t.Fatalf("expected update success with symlink fallback, got %v", err)
|
|
}
|
|
}
|
|
|
|
func TestPerformUpdateSelfTestFailurePreservesCurrentBinary(t *testing.T) {
|
|
u := newUpdaterForTest("https://example")
|
|
_, execPath := writeTempExec(t)
|
|
data := testBinary()
|
|
|
|
origRestart := restartProcessFn
|
|
t.Cleanup(func() { restartProcessFn = origRestart })
|
|
restartProcessFn = func(string) error {
|
|
t.Fatalf("restart should not be reached after self-test failure")
|
|
return nil
|
|
}
|
|
u.selfTestFn = func(context.Context, string) error { return errors.New("self-test fail") }
|
|
|
|
u.client = &http.Client{
|
|
Transport: roundTripperFunc(func(r *http.Request) (*http.Response, error) {
|
|
return &http.Response{
|
|
StatusCode: http.StatusOK,
|
|
Status: "200 OK",
|
|
Body: io.NopCloser(bytes.NewReader(data)),
|
|
Header: http.Header{"X-Checksum-Sha256": []string{checksum(data)}},
|
|
}, nil
|
|
}),
|
|
}
|
|
|
|
if err := u.performUpdateWithExecPath(context.Background(), execPath); err == nil || !strings.Contains(err.Error(), "self-test fail") {
|
|
t.Fatalf("expected self-test error, got %v", err)
|
|
}
|
|
current, err := os.ReadFile(execPath)
|
|
if err != nil {
|
|
t.Fatalf("read current binary: %v", err)
|
|
}
|
|
if string(current) != "old-binary" {
|
|
t.Fatalf("current binary changed after failed self-test: %q", string(current))
|
|
}
|
|
if _, err := os.Stat(execPath + ".backup"); !errors.Is(err, os.ErrNotExist) {
|
|
t.Fatalf("backup should not be created before self-test passes, got err=%v", err)
|
|
}
|
|
}
|
|
|
|
func TestPerformUpdateErrors(t *testing.T) {
|
|
t.Run("CreateTempError", func(t *testing.T) {
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("X-Checksum-Sha256", checksum(testBinary()))
|
|
_, _ = w.Write(testBinary())
|
|
}))
|
|
t.Cleanup(server.Close)
|
|
|
|
_, execPath := writeTempExec(t)
|
|
u := newUpdaterForTest(server.URL)
|
|
u.client = server.Client()
|
|
|
|
origCreate := createTempFn
|
|
t.Cleanup(func() { createTempFn = origCreate })
|
|
createTempFn = func(string, string) (*os.File, error) {
|
|
return nil, errors.New("temp fail")
|
|
}
|
|
|
|
if err := u.performUpdateWithExecPath(context.Background(), execPath); err == nil {
|
|
t.Fatalf("expected create temp error")
|
|
}
|
|
})
|
|
|
|
t.Run("CopyError", func(t *testing.T) {
|
|
_, execPath := writeTempExec(t)
|
|
u := newUpdaterForTest("https://example")
|
|
|
|
u.client = &http.Client{
|
|
Transport: roundTripperFunc(func(r *http.Request) (*http.Response, error) {
|
|
body := io.NopCloser(&errorReader{})
|
|
return &http.Response{
|
|
StatusCode: http.StatusOK,
|
|
Status: "200 OK",
|
|
Body: body,
|
|
Header: http.Header{"X-Checksum-Sha256": []string{checksum(testBinary())}},
|
|
}, nil
|
|
}),
|
|
}
|
|
|
|
if err := u.performUpdateWithExecPath(context.Background(), execPath); err == nil {
|
|
t.Fatalf("expected copy error")
|
|
}
|
|
})
|
|
|
|
t.Run("TooLarge", func(t *testing.T) {
|
|
_, execPath := writeTempExec(t)
|
|
u := newUpdaterForTest("https://example")
|
|
|
|
origMax := maxBinarySizeBytes
|
|
t.Cleanup(func() { maxBinarySizeBytes = origMax })
|
|
maxBinarySizeBytes = 4
|
|
|
|
data := []byte{0x7f, 'E', 'L', 'F', 0x00, 0x01}
|
|
u.client = &http.Client{
|
|
Transport: roundTripperFunc(func(r *http.Request) (*http.Response, error) {
|
|
return &http.Response{
|
|
StatusCode: http.StatusOK,
|
|
Status: "200 OK",
|
|
Body: io.NopCloser(bytes.NewReader(data)),
|
|
Header: http.Header{"X-Checksum-Sha256": []string{checksum(data)}},
|
|
}, nil
|
|
}),
|
|
}
|
|
|
|
if err := u.performUpdateWithExecPath(context.Background(), execPath); err == nil {
|
|
t.Fatalf("expected size error")
|
|
}
|
|
})
|
|
|
|
t.Run("TooLargeContentLength", func(t *testing.T) {
|
|
_, execPath := writeTempExec(t)
|
|
u := newUpdaterForTest("https://example")
|
|
data := testBinary()
|
|
|
|
origMax := maxBinarySizeBytes
|
|
t.Cleanup(func() { maxBinarySizeBytes = origMax })
|
|
maxBinarySizeBytes = 8
|
|
|
|
u.client = &http.Client{
|
|
Transport: roundTripperFunc(func(r *http.Request) (*http.Response, error) {
|
|
return &http.Response{
|
|
StatusCode: http.StatusOK,
|
|
Status: "200 OK",
|
|
Body: io.NopCloser(bytes.NewReader(data)),
|
|
ContentLength: maxBinarySizeBytes + 1,
|
|
Header: http.Header{"X-Checksum-Sha256": []string{checksum(data)}},
|
|
}, nil
|
|
}),
|
|
}
|
|
|
|
if err := u.performUpdateWithExecPath(context.Background(), execPath); err == nil {
|
|
t.Fatalf("expected size error from content length")
|
|
}
|
|
})
|
|
|
|
t.Run("CloseError", func(t *testing.T) {
|
|
data := testBinary()
|
|
u := newUpdaterForTest("https://example")
|
|
_, execPath := writeTempExec(t)
|
|
|
|
origClose := closeFileFn
|
|
origRestart := restartProcessFn
|
|
t.Cleanup(func() {
|
|
closeFileFn = origClose
|
|
restartProcessFn = origRestart
|
|
})
|
|
closeFileFn = func(file *os.File) error {
|
|
_ = file.Close()
|
|
return errors.New("close fail")
|
|
}
|
|
restartProcessFn = func(string) error { return nil }
|
|
|
|
u.client = &http.Client{
|
|
Transport: roundTripperFunc(func(r *http.Request) (*http.Response, error) {
|
|
return &http.Response{
|
|
StatusCode: http.StatusOK,
|
|
Status: "200 OK",
|
|
Body: io.NopCloser(bytes.NewReader(data)),
|
|
Header: http.Header{"X-Checksum-Sha256": []string{checksum(data)}},
|
|
}, nil
|
|
}),
|
|
}
|
|
|
|
if err := u.performUpdateWithExecPath(context.Background(), execPath); err == nil {
|
|
t.Fatalf("expected close error")
|
|
}
|
|
})
|
|
|
|
t.Run("InvalidBinary", func(t *testing.T) {
|
|
u := newUpdaterForTest("https://example")
|
|
_, execPath := writeTempExec(t)
|
|
|
|
data := []byte{0x00, 0x00, 0x00, 0x00}
|
|
u.client = &http.Client{
|
|
Transport: roundTripperFunc(func(r *http.Request) (*http.Response, error) {
|
|
return &http.Response{
|
|
StatusCode: http.StatusOK,
|
|
Status: "200 OK",
|
|
Body: io.NopCloser(bytes.NewReader(data)),
|
|
Header: http.Header{"X-Checksum-Sha256": []string{checksum(data)}},
|
|
}, nil
|
|
}),
|
|
}
|
|
|
|
if err := u.performUpdateWithExecPath(context.Background(), execPath); err == nil {
|
|
t.Fatalf("expected invalid binary error")
|
|
}
|
|
})
|
|
|
|
t.Run("MissingChecksum", func(t *testing.T) {
|
|
u := newUpdaterForTest("https://example")
|
|
_, execPath := writeTempExec(t)
|
|
|
|
data := testBinary()
|
|
u.client = &http.Client{
|
|
Transport: roundTripperFunc(func(r *http.Request) (*http.Response, error) {
|
|
return &http.Response{
|
|
StatusCode: http.StatusOK,
|
|
Status: "200 OK",
|
|
Body: io.NopCloser(bytes.NewReader(data)),
|
|
Header: http.Header{},
|
|
}, nil
|
|
}),
|
|
}
|
|
|
|
if err := u.performUpdateWithExecPath(context.Background(), execPath); err == nil {
|
|
t.Fatalf("expected checksum missing error")
|
|
}
|
|
})
|
|
|
|
t.Run("ChecksumMismatch", func(t *testing.T) {
|
|
u := newUpdaterForTest("https://example")
|
|
_, execPath := writeTempExec(t)
|
|
|
|
data := testBinary()
|
|
u.client = &http.Client{
|
|
Transport: roundTripperFunc(func(r *http.Request) (*http.Response, error) {
|
|
return &http.Response{
|
|
StatusCode: http.StatusOK,
|
|
Status: "200 OK",
|
|
Body: io.NopCloser(bytes.NewReader(data)),
|
|
Header: http.Header{"X-Checksum-Sha256": []string{"deadbeef"}},
|
|
}, nil
|
|
}),
|
|
}
|
|
|
|
if err := u.performUpdateWithExecPath(context.Background(), execPath); err == nil {
|
|
t.Fatalf("expected checksum mismatch error")
|
|
}
|
|
})
|
|
|
|
t.Run("ChmodError", func(t *testing.T) {
|
|
u := newUpdaterForTest("https://example")
|
|
_, execPath := writeTempExec(t)
|
|
data := testBinary()
|
|
|
|
origChmod := chmodFn
|
|
origRestart := restartProcessFn
|
|
t.Cleanup(func() {
|
|
chmodFn = origChmod
|
|
restartProcessFn = origRestart
|
|
})
|
|
chmodFn = func(string, os.FileMode) error { return errors.New("chmod fail") }
|
|
restartProcessFn = func(string) error { return nil }
|
|
|
|
u.client = &http.Client{
|
|
Transport: roundTripperFunc(func(r *http.Request) (*http.Response, error) {
|
|
return &http.Response{
|
|
StatusCode: http.StatusOK,
|
|
Status: "200 OK",
|
|
Body: io.NopCloser(bytes.NewReader(data)),
|
|
Header: http.Header{"X-Checksum-Sha256": []string{checksum(data)}},
|
|
}, nil
|
|
}),
|
|
}
|
|
|
|
if err := u.performUpdateWithExecPath(context.Background(), execPath); err == nil {
|
|
t.Fatalf("expected chmod error")
|
|
}
|
|
})
|
|
|
|
t.Run("BackupRenameError", func(t *testing.T) {
|
|
// Only the Windows path moves the running binary aside; elsewhere the
|
|
// staged binary is renamed over it (update_recovery_test.go).
|
|
origGOOS := runtimeGOOS
|
|
t.Cleanup(func() { runtimeGOOS = origGOOS })
|
|
runtimeGOOS = goOSWindows
|
|
u := newUpdaterForTest("https://example")
|
|
_, execPath := writeTempExec(t)
|
|
data := []byte{'M', 'Z', 0x90, 0x00, 0x01, 0x02, 0x03, 0x04}
|
|
|
|
origRename := renameFn
|
|
origRestart := restartProcessFn
|
|
t.Cleanup(func() {
|
|
renameFn = origRename
|
|
restartProcessFn = origRestart
|
|
})
|
|
renameFn = func(oldPath, newPath string) error {
|
|
if strings.HasSuffix(newPath, ".backup") {
|
|
return errors.New("backup fail")
|
|
}
|
|
return origRename(oldPath, newPath)
|
|
}
|
|
restartProcessFn = func(string) error { return nil }
|
|
|
|
u.client = &http.Client{
|
|
Transport: roundTripperFunc(func(r *http.Request) (*http.Response, error) {
|
|
return &http.Response{
|
|
StatusCode: http.StatusOK,
|
|
Status: "200 OK",
|
|
Body: io.NopCloser(bytes.NewReader(data)),
|
|
Header: http.Header{"X-Checksum-Sha256": []string{checksum(data)}},
|
|
}, nil
|
|
}),
|
|
}
|
|
|
|
if err := u.performUpdateWithExecPath(context.Background(), execPath); err == nil {
|
|
t.Fatalf("expected backup rename error")
|
|
}
|
|
})
|
|
|
|
t.Run("ReplaceRenameError", func(t *testing.T) {
|
|
// The second rename exists only on the Windows path (see above).
|
|
origGOOS := runtimeGOOS
|
|
t.Cleanup(func() { runtimeGOOS = origGOOS })
|
|
runtimeGOOS = goOSWindows
|
|
u := newUpdaterForTest("https://example")
|
|
_, execPath := writeTempExec(t)
|
|
data := []byte{'M', 'Z', 0x90, 0x00, 0x01, 0x02, 0x03, 0x04}
|
|
|
|
origRename := renameFn
|
|
origRestart := restartProcessFn
|
|
t.Cleanup(func() {
|
|
renameFn = origRename
|
|
restartProcessFn = origRestart
|
|
})
|
|
var calls int32
|
|
renameFn = func(oldPath, newPath string) error {
|
|
if atomic.AddInt32(&calls, 1) == 2 {
|
|
return errors.New("replace fail")
|
|
}
|
|
return origRename(oldPath, newPath)
|
|
}
|
|
restartProcessFn = func(string) error { return nil }
|
|
|
|
u.client = &http.Client{
|
|
Transport: roundTripperFunc(func(r *http.Request) (*http.Response, error) {
|
|
return &http.Response{
|
|
StatusCode: http.StatusOK,
|
|
Status: "200 OK",
|
|
Body: io.NopCloser(bytes.NewReader(data)),
|
|
Header: http.Header{"X-Checksum-Sha256": []string{checksum(data)}},
|
|
}, nil
|
|
}),
|
|
}
|
|
|
|
if err := u.performUpdateWithExecPath(context.Background(), execPath); err == nil {
|
|
t.Fatalf("expected replace rename error")
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestPerformUpdateUnraidPaths(t *testing.T) {
|
|
data := testBinary()
|
|
check := checksum(data)
|
|
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("X-Checksum-Sha256", check)
|
|
_, _ = w.Write(data)
|
|
}))
|
|
t.Cleanup(server.Close)
|
|
|
|
_, execPath := writeTempExec(t)
|
|
u := newUpdaterForTest(server.URL)
|
|
u.client = server.Client()
|
|
|
|
origUnraid := unraidVersionPath
|
|
origPersist := unraidPersistentPathFn
|
|
origRead := readFileFn
|
|
origWrite := writeFileFn
|
|
origRename := renameFn
|
|
origRestart := restartProcessFn
|
|
origCreateTemp := createTempFn
|
|
t.Cleanup(func() {
|
|
unraidVersionPath = origUnraid
|
|
unraidPersistentPathFn = origPersist
|
|
readFileFn = origRead
|
|
writeFileFn = origWrite
|
|
renameFn = origRename
|
|
restartProcessFn = origRestart
|
|
createTempFn = origCreateTemp
|
|
})
|
|
|
|
tmpDir := t.TempDir()
|
|
unraidVersionPath = filepath.Join(tmpDir, "unraid-version")
|
|
if err := os.WriteFile(unraidVersionPath, []byte("6.12"), 0644); err != nil {
|
|
t.Fatalf("write unraid version: %v", err)
|
|
}
|
|
|
|
persistDir := filepath.Join(tmpDir, "persist")
|
|
if err := os.MkdirAll(persistDir, 0755); err != nil {
|
|
t.Fatalf("mkdir persist: %v", err)
|
|
}
|
|
persistPath := filepath.Join(persistDir, "pulse-agent")
|
|
if err := os.WriteFile(persistPath, []byte("old"), 0644); err != nil {
|
|
t.Fatalf("write persist: %v", err)
|
|
}
|
|
unraidPersistentPathFn = func(string) string { return persistPath }
|
|
restartProcessFn = func(string) error { return nil }
|
|
|
|
t.Run("ReadError", func(t *testing.T) {
|
|
readFileFn = func(string) ([]byte, error) { return nil, errors.New("read fail") }
|
|
if err := u.performUpdateWithExecPath(context.Background(), execPath); err != nil {
|
|
t.Fatalf("expected update success, got %v", err)
|
|
}
|
|
})
|
|
|
|
t.Run("WriteError", func(t *testing.T) {
|
|
readFileFn = func(string) ([]byte, error) { return []byte("new"), nil }
|
|
createTempFn = func(dir, pattern string) (*os.File, error) {
|
|
file, err := os.CreateTemp(dir, pattern)
|
|
if err == nil && strings.Contains(pattern, "-persist-") {
|
|
err = file.Close() // Make the real persistent write fail.
|
|
}
|
|
return file, err
|
|
}
|
|
if err := u.performUpdateWithExecPath(context.Background(), execPath); err != nil {
|
|
t.Fatalf("expected update success, got %v", err)
|
|
}
|
|
createTempFn = origCreateTemp
|
|
})
|
|
|
|
t.Run("RenameError", func(t *testing.T) {
|
|
readFileFn = func(string) ([]byte, error) { return []byte("new"), nil }
|
|
writeFileFn = os.WriteFile
|
|
renameFn = func(oldPath, newPath string) error {
|
|
if newPath == persistPath {
|
|
return errors.New("rename fail")
|
|
}
|
|
return os.Rename(oldPath, newPath)
|
|
}
|
|
if err := u.performUpdateWithExecPath(context.Background(), execPath); err != nil {
|
|
t.Fatalf("expected update success, got %v", err)
|
|
}
|
|
})
|
|
|
|
t.Run("Success", func(t *testing.T) {
|
|
readFileFn = func(string) ([]byte, error) { return []byte("new"), nil }
|
|
writeFileFn = os.WriteFile
|
|
renameFn = os.Rename
|
|
if err := u.performUpdateWithExecPath(context.Background(), execPath); err != nil {
|
|
t.Fatalf("expected update success, got %v", err)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestPerformUpdateQNAPPaths(t *testing.T) {
|
|
data := testBinary()
|
|
check := checksum(data)
|
|
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("X-Checksum-Sha256", check)
|
|
_, _ = w.Write(data)
|
|
}))
|
|
t.Cleanup(server.Close)
|
|
|
|
_, execPath := writeTempExec(t)
|
|
u := newUpdaterForTest(server.URL)
|
|
u.client = server.Client()
|
|
|
|
origQnapGetcfg := qnapGetcfgPath
|
|
origQnapQpkgConf := qnapQpkgConfPath
|
|
origQnapPersist := qnapPersistentPathFn
|
|
origRead := readFileFn
|
|
origWrite := writeFileFn
|
|
origRename := renameFn
|
|
origRestart := restartProcessFn
|
|
origCreateTemp := createTempFn
|
|
t.Cleanup(func() {
|
|
qnapGetcfgPath = origQnapGetcfg
|
|
qnapQpkgConfPath = origQnapQpkgConf
|
|
qnapPersistentPathFn = origQnapPersist
|
|
readFileFn = origRead
|
|
writeFileFn = origWrite
|
|
renameFn = origRename
|
|
restartProcessFn = origRestart
|
|
createTempFn = origCreateTemp
|
|
})
|
|
|
|
tmpDir := t.TempDir()
|
|
qnapGetcfgPath = filepath.Join(tmpDir, "getcfg")
|
|
if err := os.WriteFile(qnapGetcfgPath, []byte("#!/bin/sh\n"), 0755); err != nil {
|
|
t.Fatalf("write qnap marker: %v", err)
|
|
}
|
|
qnapQpkgConfPath = filepath.Join(tmpDir, "qpkg.conf")
|
|
|
|
persistDir := filepath.Join(tmpDir, "state")
|
|
if err := os.MkdirAll(persistDir, 0755); err != nil {
|
|
t.Fatalf("mkdir qnap state: %v", err)
|
|
}
|
|
persistPath := filepath.Join(persistDir, "pulse-agent")
|
|
u.cfg.StateDir = persistDir
|
|
qnapPersistentPathFn = func(string, string) string { return persistPath }
|
|
restartProcessFn = func(string) error { return nil }
|
|
|
|
t.Run("WriteError", func(t *testing.T) {
|
|
readFileFn = func(string) ([]byte, error) { return []byte("new"), nil }
|
|
createTempFn = func(dir, pattern string) (*os.File, error) {
|
|
file, err := os.CreateTemp(dir, pattern)
|
|
if err == nil && strings.Contains(pattern, "-persist-") {
|
|
err = file.Close()
|
|
}
|
|
return file, err
|
|
}
|
|
renameFn = os.Rename
|
|
if err := u.performUpdateWithExecPath(context.Background(), execPath); err != nil {
|
|
t.Fatalf("expected update success, got %v", err)
|
|
}
|
|
createTempFn = origCreateTemp
|
|
})
|
|
|
|
t.Run("RenameError", func(t *testing.T) {
|
|
readFileFn = func(string) ([]byte, error) { return []byte("new"), nil }
|
|
writeFileFn = os.WriteFile
|
|
renameFn = func(oldPath, newPath string) error {
|
|
if newPath == persistPath {
|
|
return errors.New("rename fail")
|
|
}
|
|
return os.Rename(oldPath, newPath)
|
|
}
|
|
if err := u.performUpdateWithExecPath(context.Background(), execPath); err != nil {
|
|
t.Fatalf("expected update success, got %v", err)
|
|
}
|
|
})
|
|
|
|
t.Run("Success", func(t *testing.T) {
|
|
readFileFn = func(string) ([]byte, error) { return []byte("new"), nil }
|
|
writeFileFn = os.WriteFile
|
|
renameFn = os.Rename
|
|
if err := u.performUpdateWithExecPath(context.Background(), execPath); err != nil {
|
|
t.Fatalf("expected update success, got %v", err)
|
|
}
|
|
got, err := os.ReadFile(persistPath)
|
|
if err != nil {
|
|
t.Fatalf("read qnap persistent binary: %v", err)
|
|
}
|
|
if string(got) != "new" {
|
|
t.Fatalf("qnap persistent binary = %q, want %q", string(got), "new")
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestPersistentBinaryCopyFailuresRetainExecutable(t *testing.T) {
|
|
if runtime.GOOS != "linux" {
|
|
t.Skip("NAS executable persistence controls require Linux")
|
|
}
|
|
oldBinary := buildNASUpdateExecutable(t, "6.4.5")
|
|
newBinary := buildNASUpdateExecutable(t, "6.5.0")
|
|
for _, phase := range []string{"read", "create", "write", "close", "chmod", "rename", "success", "legacy-temp-file", "legacy-temp-symlink", "same-file", "hardlink"} {
|
|
t.Run(phase, func(t *testing.T) {
|
|
dir := t.TempDir()
|
|
execPath := filepath.Join(dir, "runtime")
|
|
persistPath := filepath.Join(dir, "pulse-agent")
|
|
writeNASUpdateExecutable(t, execPath, newBinary)
|
|
if phase == "same-file" {
|
|
persistPath = execPath
|
|
} else {
|
|
writeNASUpdateExecutable(t, persistPath, oldBinary)
|
|
}
|
|
if phase == "hardlink" {
|
|
if err := os.Link(persistPath, persistPath+".old-link"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
const foreign = "unrelated recovery bytes"
|
|
if phase == "legacy-temp-file" {
|
|
if err := os.WriteFile(persistPath+".tmp", []byte(foreign), 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if phase == "legacy-temp-symlink" {
|
|
if err := os.WriteFile(filepath.Join(dir, "foreign"), []byte(foreign), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.Symlink(filepath.Join(dir, "foreign"), persistPath+".tmp"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
origRead, origCreate, origClose, origChmod, origRename := readFileFn, createTempFn, closeFileFn, chmodFn, renameFn
|
|
t.Cleanup(func() {
|
|
readFileFn, createTempFn, closeFileFn, chmodFn, renameFn = origRead, origCreate, origClose, origChmod, origRename
|
|
})
|
|
var temp *os.File
|
|
createTempFn = func(dir, pattern string) (*os.File, error) {
|
|
if phase == "create" {
|
|
return nil, errors.New("fixture create refused")
|
|
}
|
|
file, err := os.CreateTemp(dir, pattern)
|
|
temp = file
|
|
if err == nil && phase == "write" {
|
|
err = file.Close()
|
|
}
|
|
return file, err
|
|
}
|
|
if phase == "read" {
|
|
readFileFn = func(string) ([]byte, error) { return nil, errors.New("fixture read refused") }
|
|
}
|
|
if phase == "close" {
|
|
closeFileFn = func(file *os.File) error {
|
|
if err := file.Close(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return errors.New("fixture close refused")
|
|
}
|
|
}
|
|
if phase == "chmod" {
|
|
chmodFn = func(string, os.FileMode) error { return errors.New("fixture chmod refused") }
|
|
}
|
|
if phase == "rename" {
|
|
renameFn = func(string, string) error { return errors.New("fixture rename refused") }
|
|
}
|
|
var logs bytes.Buffer
|
|
logger := zerolog.New(&logs)
|
|
u := New(Config{Logger: &logger})
|
|
u.syncPersistentBinaryCopy(execPath, persistPath, "qnap")
|
|
failure := phase == "read" || phase == "create" || phase == "write" || phase == "close" || phase == "chmod" || phase == "rename"
|
|
wantBytes, wantVersion := newBinary, "6.5.0"
|
|
if failure {
|
|
wantBytes, wantVersion = oldBinary, "6.4.5"
|
|
if !strings.Contains(logs.String(), "failed to ") || !strings.Contains(logs.String(), "persistent") {
|
|
t.Fatalf("persistence failure was not logged: %s", logs.String())
|
|
}
|
|
}
|
|
got, err := os.ReadFile(persistPath)
|
|
if err != nil || checksum(got) != checksum(wantBytes) {
|
|
t.Fatalf("persistent recovery bytes changed: %v", err)
|
|
}
|
|
info, err := os.Stat(persistPath)
|
|
if err != nil || info.Mode().Perm() != 0755 {
|
|
t.Fatalf("persistent binary lost execute mode: %v", err)
|
|
}
|
|
runNASUpdateExecutable(t, persistPath, wantVersion)
|
|
runNASUpdateExecutable(t, execPath, "6.5.0")
|
|
if temp != nil {
|
|
if _, err := temp.Stat(); err == nil {
|
|
t.Fatal("persistent temporary descriptor was not closed")
|
|
}
|
|
if _, err := os.Stat(temp.Name()); !errors.Is(err, os.ErrNotExist) {
|
|
t.Fatalf("owned persistent temporary was not cleaned: %v", err)
|
|
}
|
|
} else if phase == "same-file" && strings.Contains(logs.String(), "updated persistent binary") {
|
|
t.Fatal("already replaced executable was copied again")
|
|
}
|
|
if phase == "legacy-temp-file" || phase == "legacy-temp-symlink" {
|
|
got, err := os.ReadFile(persistPath + ".tmp")
|
|
if err != nil || string(got) != foreign {
|
|
t.Fatalf("unowned legacy temporary was changed: %v", err)
|
|
}
|
|
}
|
|
if phase == "hardlink" {
|
|
got, err := os.ReadFile(persistPath + ".old-link")
|
|
if err != nil || checksum(got) != checksum(oldBinary) {
|
|
t.Fatalf("atomic replace mutated another hardlink: %v", err)
|
|
}
|
|
runNASUpdateExecutable(t, persistPath+".old-link", "6.4.5")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
type errorReader struct {
|
|
sent bool
|
|
}
|
|
|
|
func TestCoveragePrivilegeHelperUpdateRejectsMissingSocket(t *testing.T) {
|
|
if _, err := NewPrivilegeHelperUpdate(""); err == nil {
|
|
t.Fatal("expected an empty helper socket path to fail closed")
|
|
}
|
|
}
|
|
|
|
func (e *errorReader) Read(p []byte) (int, error) {
|
|
if e.sent {
|
|
return 0, errors.New("read fail")
|
|
}
|
|
e.sent = true
|
|
copy(p, testBinary())
|
|
return len(testBinary()), errors.New("read fail")
|
|
}
|