using System.Diagnostics;
using System.Text;
using System.Text.RegularExpressions;
using Backend.Enrollment;
using Shared.Contracts.Models;
using Xunit;
namespace Backend.Tests;
///
/// The served install scripts are safe to run any number of times: they parse (Windows
/// PowerShell 5.1, PowerShell 7, bash), decide from what is on the device between leaving
/// it alone, installing, upgrading, repairing and re-enrolling, and never print the token.
/// The decisions run the scripts' own functions against described device states; nothing
/// is installed.
///
public partial class InstallScriptRepairTests
{
private const string Server = "https://control.example.com";
private const string Token = "s3cret-token.0f1e2d3c4b5a69788796a5b4c3d2e1f00f1e2d3c4b5a69788796a5b4c3d2e1f0";
private const string Fingerprint = "0f1e2d3c4b5a69788796a5b4c3d2e1f00f1e2d3c4b5a69788796a5b4c3d2e1f0";
public static TheoryData WindowsVariants => ["msi", "msi-pinned", "zip", "quote"];
public static TheoryData UnixVariants => ["linux", "linux-pinned", "osx", "osx-pinned", "quote"];
private static string WindowsScript(string variant) => variant switch
{
"msi" => InstallEndpoints.BuildWindowsScript(Server, Token, $"{Server}/msi?token=t", $"{Server}/zip?token=t", "1.5.0", "1.5.0", false, null),
"msi-pinned" => InstallEndpoints.BuildWindowsScript(Server, Token, $"{Server}/msi?token=t", $"{Server}/zip?token=t", "1.5.0", null, false, Fingerprint),
"zip" => InstallEndpoints.BuildWindowsScript(Server, Token, null, $"{Server}/zip?token=t", null, "1.5.0.3", true, null),
_ => InstallEndpoints.BuildWindowsScript(Server, "it's", $"{Server}/msi?token=t", $"{Server}/zip?token=t", null, null, false, null),
};
private static string UnixScript(string variant) => variant switch
{
"linux" => InstallEndpoints.BuildUnixScript(Server, Token, "linux", "token=t", false, null, "1.5.0", "1.5.0"),
"linux-pinned" => InstallEndpoints.BuildUnixScript(Server, Token, "linux", "token=t", false, Fingerprint, "1.5.0", null),
"osx" => InstallEndpoints.BuildUnixScript(Server, Token, "osx", "token=t", true, null, null, "1.5.0"),
"osx-pinned" => InstallEndpoints.BuildUnixScript(Server, Token, "osx", "token=t", false, Fingerprint, "1.5.0", "1.5.0"),
_ => InstallEndpoints.BuildUnixScript(Server, "it's", "linux", "token=t", false, null, null, null),
};
[Theory]
[MemberData(nameof(WindowsVariants))]
public async Task WindowsScriptsParseInWindowsPowerShellAndPowerShell7(string variant)
{
var shells = new[] { WindowsPowerShell(), Pwsh() }.OfType().ToList();
Assert.SkipWhen(shells.Count == 0, "Needs Windows PowerShell or pwsh.");
var check = """
$tokens = $null; $errors = $null
[void][System.Management.Automation.Language.Parser]::ParseInput([IO.File]::ReadAllText($args[0]), [ref]$tokens, [ref]$errors)
$errors | ForEach-Object { $_.Message + ' (line ' + $_.Extent.StartLineNumber + ')' }
exit $errors.Count
""";
using var folder = new ScratchFolder();
var script = folder.Write("install.ps1", WindowsScript(variant));
var checker = folder.Write("check.ps1", check);
foreach (var shell in shells)
{
var (code, output, _) = await RunAsync(shell, $"-NoProfile -NonInteractive -ExecutionPolicy Bypass -File \"{checker}\" \"{script}\"");
Assert.True(code == 0, $"{Path.GetFileName(shell)}: {output}");
}
}
[Theory]
[MemberData(nameof(UnixVariants))]
public async Task UnixScriptsPassBashSyntaxCheck(string variant)
{
var bash = Bash();
Assert.SkipWhen(bash is null, "Needs bash (Git Bash on Windows).");
using var folder = new ScratchFolder();
var script = folder.Write("install.sh", UnixScript(variant));
var (code, output, _) = await RunAsync(bash!, $"-n \"{ForBash(script)}\"");
Assert.True(code == 0, output);
Assert.DoesNotContain("\r", UnixScript(variant));
}
[Fact]
public void TheTokenAndServerAreQuotedForEachShell()
{
Assert.Contains("$token = 'it''s'", WindowsScript("quote"));
Assert.Contains("token='it'\\''s'", UnixScript("quote"));
Assert.Equal("'a''b'", InstallEndpoints.PsQuote("a'b"));
Assert.Equal("'a'\\''b'", InstallEndpoints.ShQuote("a'b"));
}
[Fact]
public void TheServedVersionIsEmbeddedForTheComparison()
{
var msi = WindowsScript("msi");
Assert.Contains("$msiVersion = '1.5.0'", msi);
Assert.Contains("$portableOnly = $false", msi);
var zip = WindowsScript("zip");
Assert.Contains("$msiUrl = ''", zip);
Assert.Contains("$zipVersion = '1.5.0.3'", zip);
Assert.Contains("$portableOnly = $true", zip);
var osx = UnixScript("osx");
Assert.Contains("x64) target='' ;;", osx);
Assert.Contains("*) target='1.5.0' ;;", osx);
}
[Theory]
[InlineData("1.4.0", "1.4.0")]
[InlineData(" 1.4.0.7+1a2b3c4 ", "1.4.0.7+1a2b3c4")]
[InlineData("2026.10.23.1400", "2026.10.23.1400")]
[InlineData("0.0.0.0", null)]
[InlineData("1.4", null)]
[InlineData("1.4.0'; rm -rf /", null)]
[InlineData(null, null)]
public void OnlyAVersionTheComparisonUnderstandsGoesIntoAScript(string? version, string? expected)
=> Assert.Equal(expected, InstallEndpoints.ScriptSafeVersion(version));
[Theory]
[MemberData(nameof(WindowsVariants))]
public void TheWindowsTokenIsOnlyEverHandedToWindowsInstallerOrTheRegistry(string variant)
{
// Allowed: the assignment, masking it out of an error, msiexec's (hidden) property,
// the registry value the agent reads, and comparing it with a pending one.
string[] allowed =
[
@"^\$token = '",
@"^if \(\$token -and \$Text\) \{ return \$Text\.Replace\(\$token, ''\) \}$",
@"^if \(\$Enroll\) \{ \$arguments \+= ""BOOTSTRAPTOKEN=\$token"" \}$",
@"Set-ItemProperty -Path \$executerKey -Name 'BootstrapToken' -Value \$token( \})?$",
@"^\$state\.TokenIsOurs = \[bool\]\(\$pending -and \$pending -ceq \$token\)$",
];
var script = WindowsScript(variant);
foreach (var line in Lines(script).Where(l => TokenVariable().IsMatch(l)))
{
Assert.True(allowed.Any(pattern => Regex.IsMatch(line, pattern)), $"unexpected use of $token: {line}");
}
if (variant != "quote")
{
Assert.Single(Lines(script), l => l.Contains(Token, StringComparison.Ordinal));
}
}
[Theory]
[MemberData(nameof(UnixVariants))]
public void TheUnixTokenIsOnlyEverHandedToTheAgentsConfiguration(string variant)
{
string[] allowed =
[
@"^token='",
@"^""\$exe"" configure --url ""\$server"" --token ""\$token"" >/dev/null$",
@"^if \[ ""\$pending"" = ""\$token"" \]; then pending_ours=1; fi$",
];
var script = UnixScript(variant);
foreach (var line in Lines(script).Where(l => TokenVariable().IsMatch(l)))
{
Assert.True(allowed.Any(pattern => Regex.IsMatch(line, pattern)), $"unexpected use of $token: {line}");
}
if (variant != "quote")
{
Assert.Single(Lines(script), l => l.Contains(Token, StringComparison.Ordinal));
}
}
[Theory]
[MemberData(nameof(WindowsVariants))]
public void WindowsScriptsEndWithOneOutcomeAndExitNonZeroOnFailure(string variant)
{
var script = WindowsScript(variant);
foreach (var outcome in new[] { "'Already Installed: ExecuteR Agent '", "{ 'Installed' }", "{ 'Upgraded' }", "{ 'Repaired' }", "('Failed: ' + $reason)" })
{
Assert.Contains(outcome, script);
}
Assert.Contains("if ($launchedForThis) { exit 1 }", script);
Assert.EndsWith("}", script.TrimEnd());
// Interrupted runs: one setup at a time, leftovers cleared, a kept enrollment put back.
Assert.Contains(@"'Global\ExecuteRAgentSetup'", script);
Assert.Contains("Clear-ExecuteRLeftovers", script);
Assert.Contains(@"'Global\_MSIExecute'", script);
Assert.Contains("Restore-ExecuteRKept", script);
}
[Theory]
[MemberData(nameof(UnixVariants))]
public void UnixScriptsRunEverythingFromTheLastLine(string variant)
{
var script = UnixScript(variant);
Assert.Equal("main", Lines(script).Last());
foreach (var outcome in new[] { "echo \"Already Installed: ExecuteR Agent", "outcome=Installed", "outcome=Upgraded", "outcome=Repaired", "echo \"Failed: $*\"" })
{
Assert.Contains(outcome, script);
}
Assert.Contains("mkdir \"$lock\"", script);
Assert.Contains("rm -rf \"$dest.staging\"", script);
}
/// A device state for the Windows plan: healthy, current (1.5.0), MSI, enrolled with this server.
private const string PsHealthy = """
@{ Present = $true; ExeExists = $true; ExeWorks = $true; Version = '1.5.0'; ServerUrl = 'https://control.example.com';
PolicyServerUrl = $null; Credential = 'valid'; TokenPending = $false; TokenIsOurs = $false; MsiMarker = $true;
Msi = @{ ProductCode = '{A}'; Version = '1.5.0.0'; CacheOk = $true };
Agent = @{ Exists = $true; Status = 'Running'; StartType = 'Automatic' };
Watchdog = @{ Exists = $true; Status = 'Running'; StartType = 'Automatic' }; WatchdogExpected = $true }
""";
private static readonly (string Name, string Changes, string Target, string Kind, string Action, bool Enroll, bool Pending)[] WindowsPlans =
{
// name, state changes, target, msi or zip script, expected action, enroll, enrollment pending
("healthy and current", "", "1.5.0", "msi", "None", false, false),
("healthy and newer than the server", "$s.Version = '1.6.0'", "1.5.0", "msi", "None", false, false),
("no package version known", "", "", "msi", "None", false, false),
("server with a trailing slash and other case", "$s.ServerUrl = 'HTTPS://Control.Example.com/'", "1.5.0", "msi", "None", false, false),
("nothing installed", "$s = @{ Present = $false }", "1.5.0", "msi", "Install", true, false),
("older", "$s.Version = '1.4.9'", "1.5.0", "msi", "Upgrade", false, false),
("a calendar-era build", "$s.Version = '2026.10.23.1400'", "1.5.0", "msi", "Upgrade", false, false),
("older and enrolled elsewhere", "$s.Version = '1.4.9'; $s.ServerUrl = 'https://old.example.com'", "1.5.0", "msi", "Upgrade", true, false),
("service stopped", "$s.Agent.Status = 'Stopped'", "1.5.0", "msi", "Repair", false, false),
("service disabled", "$s.Agent.StartType = 'Disabled'; $s.Agent.Status = 'Stopped'", "1.5.0", "msi", "Repair", false, false),
("service missing", "$s.Agent = @{ Exists = $false; Status = 'Missing'; StartType = 'Missing' }", "1.5.0", "msi", "Repair", false, false),
("watchdog stopped", "$s.Watchdog.Status = 'Stopped'", "1.5.0", "msi", "Repair", false, false),
("binary missing", "$s.ExeExists = $false; $s.ExeWorks = $false; $s.Version = $null", "1.5.0", "msi", "Repair", false, false),
("binary broken", "$s.ExeWorks = $false; $s.Version = $null", "1.5.0", "msi", "Repair", false, false),
("Windows Installer cache missing", "$s.Msi.CacheOk = $false", "1.5.0", "msi", "Repair", false, false),
("Windows Installer registration gone", "$s.Msi = $null", "1.5.0", "msi", "Repair", false, false),
("portable: no MSI is fine", "$s.Msi = $null; $s.MsiMarker = $false", "1.5.0", "zip", "None", false, false),
("enrolled elsewhere", "$s.ServerUrl = 'https://old.example.com'", "1.5.0", "msi", "Enroll", true, false),
("no server configured", "$s.ServerUrl = $null", "1.5.0", "msi", "Enroll", true, false),
("credential missing", "$s.Credential = 'missing'", "1.5.0", "msi", "Enroll", true, false),
("credential unreadable", "$s.Credential = 'unreadable'", "1.5.0", "msi", "Enroll", true, false),
("credential of other hardware", "$s.Credential = 'other-device'", "1.5.0", "msi", "Enroll", true, false),
("enrolling with this token already", "$s.Credential = 'missing'; $s.TokenPending = $true; $s.TokenIsOurs = $true", "1.5.0", "msi", "None", false, true),
("a pending token of another run", "$s.Credential = 'missing'; $s.TokenPending = $true", "1.5.0", "msi", "Enroll", true, false),
("stopped and without credential", "$s.Agent.Status = 'Stopped'; $s.Credential = 'missing'", "1.5.0", "msi", "Repair", true, false),
};
[Fact]
public async Task WindowsPlansFollowTheDeviceState()
{
var shell = WindowsPowerShell() ?? Pwsh();
Assert.SkipWhen(shell is null, "Needs Windows PowerShell or pwsh.");
var body = new StringBuilder("""
$server = 'https://control.example.com'
$token = 'tok'
$msiUrl = 'x'; $zipUrl = 'x'; $msiVersion = ''; $zipVersion = ''; $portableOnly = $false
""");
body.AppendLine(InstallScripts.WindowsFunctions);
var cases = WindowsPlans;
for (var i = 0; i < cases.Length; i++)
{
body.AppendLine($"$s = {PsHealthy}");
body.AppendLine(cases[i].Changes);
body.AppendLine($"$p = Get-ExecuteRPlan $s '{cases[i].Target}' ${(cases[i].Kind == "zip" ? "true" : "false")}");
body.AppendLine($"Write-Output ('{i}=' + $p.Action + '|' + $p.Enroll + '|' + $p.EnrollPending)");
}
using var folder = new ScratchFolder();
var script = folder.Write("plans.ps1", body.ToString());
var (code, output, _) = await RunAsync(shell!, $"-NoProfile -NonInteractive -ExecutionPolicy Bypass -File \"{script}\"");
Assert.True(code == 0, output);
var results = Results(output);
for (var i = 0; i < cases.Length; i++)
{
var expected = $"{cases[i].Action}|{(cases[i].Enroll ? "True" : "False")}|{(cases[i].Pending ? "True" : "False")}";
Assert.True(results.TryGetValue(i.ToString(), out var actual) && actual == expected, $"{cases[i].Name}: expected {expected}, got {actual}\n{output}");
}
}
/// A device state for the unix plan: healthy, current (1.5.0), enrolled with this server.
private const string ShHealthy =
"present=1 works=1 version=1.5.0 configured=https://control.example.com credential=valid token_pending=0 pending_ours=0 "
+ "registered=1 agent_running=1 agent_disabled=0 watchdog_expected=1 watchdog_running=1";
private static readonly (string Name, string Changes, string Target, string Action, string Enroll, string Pending)[] UnixPlans =
{
// name, state changes, target, expected action, enroll, enrollment pending
("healthy and current", "", "1.5.0", "none", "0", "0"),
("healthy and newer than the server", "version=1.6.0", "1.5.0", "none", "0", "0"),
("no package version known", "", "", "none", "0", "0"),
("server with a trailing slash and other case", "configured=HTTPS://Control.Example.com/", "1.5.0", "none", "0", "0"),
("nothing installed", "present=0 works=0 registered=0 agent_running=0 watchdog_expected=0 watchdog_running=0", "1.5.0", "install", "1", "0"),
("older", "version=1.4.9", "1.5.0", "upgrade", "0", "0"),
("a calendar-era build", "version=2026.10.23.1400", "1.5.0", "upgrade", "0", "0"),
("older and enrolled elsewhere", "version=1.4.9 configured=https://old.example.com", "1.5.0", "upgrade", "1", "0"),
("service stopped", "agent_running=0", "1.5.0", "repair", "0", "0"),
("service disabled", "agent_running=0 agent_disabled=1", "1.5.0", "repair", "0", "0"),
("service not registered", "registered=0", "1.5.0", "repair", "0", "0"),
("watchdog stopped", "watchdog_running=0", "1.5.0", "repair", "0", "0"),
("binary broken", "works=0 version=", "1.5.0", "repair", "0", "0"),
("enrolled elsewhere", "configured=https://old.example.com", "1.5.0", "enroll", "1", "0"),
("no server configured", "configured=", "1.5.0", "enroll", "1", "0"),
("credential missing", "credential=missing", "1.5.0", "enroll", "1", "0"),
("credential of other hardware", "credential=other-device", "1.5.0", "enroll", "1", "0"),
("enrolling with this token already", "credential=missing token_pending=1 pending_ours=1", "1.5.0", "none", "0", "1"),
("a pending token of another run", "credential=missing token_pending=1", "1.5.0", "enroll", "1", "0"),
("stopped and without credential", "agent_running=0 credential=missing", "1.5.0", "repair", "1", "0"),
};
[Fact]
public async Task UnixPlansFollowTheDeviceState()
{
var bash = Bash();
Assert.SkipWhen(bash is null, "Needs bash (Git Bash on Windows).");
var body = new StringBuilder("""
set -euo pipefail
server='https://control.example.com'
token='tok'
os=linux arch=x64 pkg=x dest=/nonexistent/executer tmp=/nonexistent reported=0 locked=0 lock=/nonexistent/lock
""");
body.Append(InstallScripts.UnixFunctions.ReplaceLineEndings("\n")).Append('\n');
var cases = UnixPlans;
for (var i = 0; i < cases.Length; i++)
{
body.Append($"{ShHealthy}\n{cases[i].Changes}\ntarget='{cases[i].Target}'\nplan\necho \"{i}=$action|$enroll|$enroll_pending\"\n");
}
using var folder = new ScratchFolder();
var script = folder.Write("plans.sh", body.ToString());
var (code, output, _) = await RunAsync(bash!, $"\"{ForBash(script)}\"");
Assert.True(code == 0, output);
var results = Results(output);
for (var i = 0; i < cases.Length; i++)
{
var expected = $"{cases[i].Action}|{cases[i].Enroll}|{cases[i].Pending}";
Assert.True(results.TryGetValue(i.ToString(), out var actual) && actual == expected, $"{cases[i].Name}: expected {expected}, got {actual}\n{output}");
}
}
private static readonly (string Left, string Right)[] VersionPairs =
[
("1.5.0", "1.4.9"), ("1.4.9", "1.5.0"), ("1.5.0", "1.5.0.0"), ("1.5.0.1", "1.5.0"), ("1.5.0+abc", "1.5.0"),
("1.10.0", "1.9.0"), ("1.5.0", "2026.10.23.1400"), ("2026.10.23.1400", "2026.10.22.2359"), ("1.5.0", ""),
("", "1.5.0"), ("0.0.0.0", "1.0.0"), ("1.5", "1.5.0"), ("01.05.00", "1.5.0"), ("", ""), ("junk", "junk"),
];
[Fact]
public async Task WindowsVersionsCompareAsTheServerDoes()
{
var shell = WindowsPowerShell() ?? Pwsh();
Assert.SkipWhen(shell is null, "Needs Windows PowerShell or pwsh.");
var body = new StringBuilder("$server = 'x'; $token = 'x'\n").AppendLine(InstallScripts.WindowsFunctions);
for (var i = 0; i < VersionPairs.Length; i++)
{
body.AppendLine($"Write-Output ('{i}=' + (Compare-ExecuteRVersion '{VersionPairs[i].Left}' '{VersionPairs[i].Right}'))");
}
using var folder = new ScratchFolder();
var (code, output, _) = await RunAsync(shell!, $"-NoProfile -NonInteractive -ExecutionPolicy Bypass -File \"{folder.Write("versions.ps1", body.ToString())}\"");
Assert.True(code == 0, output);
AssertVersionResults(Results(output), output);
}
[Fact]
public async Task UnixVersionsCompareAsTheServerDoes()
{
var bash = Bash();
Assert.SkipWhen(bash is null, "Needs bash (Git Bash on Windows).");
var body = new StringBuilder("set -euo pipefail\ndest=/nonexistent\n").Append(InstallScripts.UnixFunctions.ReplaceLineEndings("\n")).Append('\n');
for (var i = 0; i < VersionPairs.Length; i++)
{
body.Append($"echo \"{i}=$(version_compare '{VersionPairs[i].Left}' '{VersionPairs[i].Right}')\"\n");
}
using var folder = new ScratchFolder();
var (code, output, _) = await RunAsync(bash!, $"\"{ForBash(folder.Write("versions.sh", body.ToString()))}\"");
Assert.True(code == 0, output);
AssertVersionResults(Results(output), output);
}
///
/// A stand-in for executer-agent: version, status (the JSON line), configure and
/// initialize, keeping its state in files under FAKE_STATE and noting each call
/// (configure with a token as configure-token, never the token itself).
///
private const string FakeUnixAgent = """
#!/usr/bin/env bash
here=$(cd "$(dirname "$0")" && pwd)
S="$FAKE_STATE"
enroll() { if [ -f "$S/token" ]; then echo valid > "$S/credential"; rm -f "$S/token"; fi; }
case "$1" in
version) echo version >> "$S/calls"; cat "$here/AGENT_VERSION" ;;
status)
echo status >> "$S/calls"
srv=null; if [ -f "$S/server" ]; then srv="\"$(cat "$S/server")\""; fi
cred=missing; if [ -f "$S/credential" ]; then cred=$(cat "$S/credential"); fi
pending=false; if [ -f "$S/token" ]; then pending=true; fi
echo "{\"service\":\"executer-agent\",\"installed\":true,\"state\":\"active\",\"version\":\"$(cat "$here/AGENT_VERSION")\",\"serverUrl\":$srv,\"credential\":\"$cred\",\"enrollmentId\":null,\"credentialExpiresUtc\":null,\"bootstrapTokenPending\":$pending}"
echo "Service 'ExecuteR Agent': active" ;;
configure)
shift
call=configure
while [ $# -gt 0 ]; do
case "$1" in
--url) printf '%s' "$2" > "$S/server"; shift 2 ;;
--token) printf '%s' "$2" > "$S/token"; call=configure-token; shift 2 ;;
*) shift ;;
esac
done
echo "$call" >> "$S/calls" ;;
initialize)
echo initialize >> "$S/calls"
if [ -f "$S/fail-initialize" ]; then echo 'cannot register the service' >&2; exit 3; fi
touch "$S/registered.agent" "$S/running.executer-agent" "$S/running.executer-watchdog"
enroll
echo 'systemd unit executer-agent installed.' ;;
esac
""";
/// The service manager and the package download, stood in for after the script's own functions.
private const string UnixOverrides = """
mac=0
sleep() { case "$1" in 0.*) command sleep "$1" ;; esac; }
download() { : > "$2"; }
unpack() { cp -R "$FAKE_PACKAGE/." "$2/"; }
svc_registered() { [ -f "$FAKE_STATE/registered.$1" ]; }
systemctl() {
local verb=$1 u
shift
if [ "${1:-}" = --quiet ]; then shift; fi
case "$verb" in
is-active) [ -f "$FAKE_STATE/running.$1" ] ;;
is-enabled) echo enabled ;;
stop) for u in "$@"; do rm -f "$FAKE_STATE/running.$u"; done ;;
restart)
touch "$FAKE_STATE/running.$1"
if [ -f "$FAKE_STATE/token" ]; then echo valid > "$FAKE_STATE/credential"; rm -f "$FAKE_STATE/token"; fi ;;
*) return 0 ;;
esac
}
""";
[Fact]
public async Task TheUnixScriptInstallsThenLeavesAloneRepairsUpgradesAndReEnrolls()
{
var bash = Bash();
Assert.SkipWhen(bash is null, "Needs bash (Git Bash on Windows).");
using var folder = new ScratchFolder();
var root = ForBash(folder.Path);
var state = Directory.CreateDirectory(Path.Combine(folder.Path, "state")).FullName;
var package = Directory.CreateDirectory(Path.Combine(folder.Path, "package")).FullName;
Directory.CreateDirectory(Path.Combine(folder.Path, "tmp"));
File.WriteAllText(Path.Combine(package, "executer-agent"), FakeUnixAgent.ReplaceLineEndings("\n"));
File.WriteAllText(Path.Combine(package, "executer-watchdog"), "#!/usr/bin/env bash\n");
File.WriteAllText(Path.Combine(package, "AGENT_VERSION"), "1.5.0\n");
var dest = Path.Combine(folder.Path, "opt", "agent");
var calls = Path.Combine(state, "calls");
async Task<(int Code, string Last, string[] Calls, string Output)> Run(string target)
{
var before = File.Exists(calls) ? File.ReadAllLines(calls).Length : 0;
var script = InstallEndpoints.BuildUnixScript(Server, Token, "linux", "token=t", false, null, target, target)
.Replace("if [ \"$(id -u)\" -ne 0 ]; then echo 'Failed: run with sudo/root.' >&2; exit 1; fi\n", "")
.Replace("dest=/opt/executer/agent\n", $"dest='{root}/opt/agent'\n")
.Replace("lock=/var/run/executer-install.lock\n", $"lock='{root}/lock'\n");
Assert.EndsWith("\nmain", script.TrimEnd());
script = script.TrimEnd()[..^"main".Length] + UnixOverrides.ReplaceLineEndings("\n") + "\nmain\n";
var (code, output, stdout) = await RunAsync(bash!, $"\"{ForBash(folder.Write("install.sh", script))}\"", new()
{
["FAKE_STATE"] = ForBash(state),
["FAKE_PACKAGE"] = ForBash(package),
["TMPDIR"] = $"{root}/tmp",
});
Assert.DoesNotContain(Token, output);
var all = File.Exists(calls) ? File.ReadAllLines(calls) : [];
return (code, Lines(stdout).Last(), all[before..], output);
}
// Nothing there: installed and enrolled with the token.
var run = await Run("1.5.0");
Assert.True(run.Code == 0, run.Output);
Assert.StartsWith("Installed: ExecuteR Agent 1.5.0 is running for https://control.example.com, enrolled", run.Last);
Assert.Contains("configure-token", run.Calls);
Assert.Contains("initialize", run.Calls);
// Again: nothing changes.
run = await Run("1.5.0");
Assert.Equal(0, run.Code);
Assert.StartsWith("Already Installed: ExecuteR Agent 1.5.0 is running", run.Last);
Assert.All(run.Calls, call => Assert.Contains(call, new[] { "version", "status" }));
// Interrupted earlier runs leave a staging folder and a temp folder: both go, nothing else changes.
Directory.CreateDirectory(dest + ".staging");
Directory.CreateDirectory(Path.Combine(folder.Path, "tmp", "executer-install.stale"));
run = await Run("1.5.0");
Assert.StartsWith("Already Installed:", run.Last);
Assert.False(Directory.Exists(dest + ".staging"));
Assert.False(Directory.Exists(Path.Combine(folder.Path, "tmp", "executer-install.stale")));
// The service stopped: started again, the enrollment kept (no token).
File.Delete(Path.Combine(state, "running.executer-agent"));
run = await Run("1.5.0");
Assert.StartsWith("Repaired: ExecuteR Agent 1.5.0 is running", run.Last);
Assert.Contains("initialize", run.Calls);
Assert.DoesNotContain("configure-token", run.Calls);
// A newer package: upgraded in place, the enrollment kept.
File.WriteAllText(Path.Combine(package, "AGENT_VERSION"), "1.6.0\n");
run = await Run("1.6.0");
Assert.StartsWith("Upgraded: ExecuteR Agent 1.6.0 is running", run.Last);
Assert.Equal("1.6.0", File.ReadAllText(Path.Combine(dest, "AGENT_VERSION")).Trim());
Assert.DoesNotContain("configure-token", run.Calls);
// Enrolled with another server: pointed here and enrolled with the token.
File.WriteAllText(Path.Combine(state, "server"), "https://old.example.com");
run = await Run("1.6.0");
Assert.StartsWith("Repaired:", run.Last);
Assert.Contains("configure-token", run.Calls);
Assert.Equal(Server, File.ReadAllText(Path.Combine(state, "server")));
// A binary that no longer runs: the files laid down again.
File.WriteAllText(Path.Combine(dest, "executer-agent"), "\u007fELF garbage");
run = await Run("1.6.0");
Assert.StartsWith("Repaired: ExecuteR Agent 1.6.0 is running", run.Last);
Assert.Equal(FakeUnixAgent.ReplaceLineEndings("\n"), File.ReadAllText(Path.Combine(dest, "executer-agent")).ReplaceLineEndings("\n"));
// A device where the service cannot be registered: one Failed line and a non-zero exit.
foreach (var file in Directory.GetFiles(state))
{
File.Delete(file);
}
Directory.Delete(dest, recursive: true);
File.WriteAllText(Path.Combine(state, "fail-initialize"), "");
run = await Run("1.6.0");
Assert.Equal(1, run.Code);
Assert.StartsWith("Failed: ", run.Last);
}
///
/// Everything of the Windows script that touches the machine (services, registry, Windows
/// Installer, files) replaced by a device kept in $F; the agent binary is Invoke-FakeAgent.
///
private const string WindowsFakes = """
$global:Clock = 0
function Start-Sleep([int]$Seconds) { $global:Clock += $Seconds }
function Get-Date { [datetime]::Now.AddSeconds($global:Clock) }
function Test-ExecuteRElevated { $true }
function Clear-ExecuteRLeftovers { }
function Invoke-FakeAgent {
$F.Calls.Add('initialize')
if (-not $F.FailInitialize) { $F.Running = -not $F.FailStart }
if ($F.Running -and $F.Token) { $F.Credential = 'valid'; $F.Token = $null }
$global:LASTEXITCODE = 0
}
function Get-ExecuteRService([string]$Name) {
if (-not $F.Present) { return @{ Exists = $false; Status = 'Missing'; StartType = 'Missing' } }
return @{ Exists = $true; Status = $(if ($F.Running) { 'Running' } else { 'Stopped' }); StartType = 'Automatic' }
}
function Get-ExecuteRState([bool]$Portable) {
return @{
Present = $F.Present; Dir = 'C:\Fake'; Exe = 'Invoke-FakeAgent'; ExeExists = $F.Present; ExeWorks = ($F.Present -and $F.Works)
Version = $(if ($F.Present -and $F.Works) { $F.Version } else { $null }); ServerUrl = $F.Server; PolicyServerUrl = $null
Credential = $F.Credential; TokenPending = [bool]$F.Token; TokenIsOurs = [bool]($F.Token -and $F.Token -ceq $token); MsiMarker = $F.Present
Msi = $(if ($F.Present) { @{ ProductCode = '{A}'; Version = $F.Version; CacheOk = $true } } else { $null })
Agent = (Get-ExecuteRService $agentServiceName); Watchdog = (Get-ExecuteRService $watchdogServiceName); WatchdogExpected = $F.Present
}
}
function Get-ExecuteRStatus([string]$Exe) { [pscustomobject]@{ credential = $F.Credential; bootstrapTokenPending = [bool]$F.Token } }
function Install-ExecuteRPackage([bool]$Portable, [bool]$Enroll, [bool]$Downgrade) {
$F.Calls.Add('package:' + $Enroll)
$F.Present = $true; $F.Works = $true; $F.Version = $F.Package; $F.Server = $server
if ($Enroll) { $F.Token = $token }
$F.Running = -not $F.FailStart
if ($F.Running -and $F.Token) { $F.Credential = 'valid'; $F.Token = $null }
}
function Set-ExecuteREnrollment { $F.Calls.Add('enroll'); $F.Server = $server; $F.Token = $token }
function Stop-ExecuteRService([string]$Name) { $F.Calls.Add('stop:' + $Name); if ($Name -eq $agentServiceName) { $F.Running = $false } }
function Restart-ExecuteRAgent {
$F.Calls.Add('restart')
$F.Running = -not $F.FailStart
if ($F.Running -and $F.Token) { $F.Credential = 'valid'; $F.Token = $null }
}
""";
public static TheoryData WindowsRuns => new()
{
// name, device ($F changes), exit code, outcome line start, calls (in order), calls that must not happen
{ "nothing installed", "$F.Present = $false", 0, "Installed: ExecuteR Agent 1.5.0 is running for https://control.example.com, enrolled", "package:True", "initialize" },
{ "healthy and current", "", 0, "Already Installed: ExecuteR Agent 1.5.0 is running for https://control.example.com", "", "package:True,package:False,initialize,enroll,restart" },
{ "older", "$F.Version = '1.4.0'", 0, "Upgraded: ExecuteR Agent 1.5.0 is running", "package:False", "enroll,initialize" },
{ "stopped", "$F.Running = $false", 0, "Repaired: ExecuteR Agent 1.5.0 is running", "initialize", "package:False,enroll" },
{ "restart does not help", "$F.Running = $false; $F.FailInitialize = $true", 0, "Repaired: ExecuteR Agent 1.5.0 is running", "initialize,package:False", "enroll" },
{ "binary broken", "$F.Works = $false", 0, "Repaired: ExecuteR Agent 1.5.0 is running", "package:False", "initialize,enroll" },
{ "enrolled elsewhere", "$F.Server = 'https://old.example.com'", 0, "Repaired: ExecuteR Agent 1.5.0 is running for https://control.example.com, enrolled", "enroll,restart", "package:False,initialize" },
{ "credential missing", "$F.Credential = 'missing'", 0, "Repaired: ExecuteR Agent 1.5.0 is running", "enroll,restart", "package:False" },
{ "keeps stopping", "$F.Running = $false; $F.FailStart = $true", 1, "Failed: the agent is not healthy: the agent service is stopped", "initialize,package:False", "" },
};
[Theory]
[MemberData(nameof(WindowsRuns))]
public async Task TheWindowsScriptDoesOnlyWhatTheDeviceNeeds(string name, string device, int exitCode, string outcome, string calls, string never)
{
var shell = WindowsPowerShell() ?? Pwsh();
Assert.SkipWhen(shell is null, "Needs Windows PowerShell or pwsh.");
var script = new StringBuilder("""
$ErrorActionPreference = 'Stop'
$server = 'https://control.example.com'
$token = 's3cret-token'
$msiUrl = 'x'; $zipUrl = 'x'; $msiVersion = '1.5.0'; $zipVersion = '1.5.0'; $portableOnly = $false
""")
.AppendLine(InstallScripts.WindowsFunctions)
.AppendLine(WindowsFakes)
.AppendLine("""
$F = @{ Present = $true; Works = $true; Version = '1.5.0'; Package = '1.5.0'; Running = $true; Server = 'https://control.example.com'
Credential = 'valid'; Token = $null; FailStart = $false; FailInitialize = $false; Calls = New-Object System.Collections.Generic.List[string] }
""")
.AppendLine(device)
.AppendLine("try {")
.AppendLine(InstallScripts.WindowsMain)
.AppendLine("} finally { Write-Host ('calls=' + ($F.Calls -join ',')) }");
using var folder = new ScratchFolder();
var (code, output, stdout) = await RunAsync(shell!, $"-NoProfile -NonInteractive -ExecutionPolicy Bypass -File \"{folder.Write("run.ps1", script.ToString())}\"");
Assert.DoesNotContain("s3cret-token", output);
var lines = Lines(stdout).ToList();
var made = lines.Single(l => l.StartsWith("calls=", StringComparison.Ordinal))["calls=".Length..].Split(',', StringSplitOptions.RemoveEmptyEntries);
Assert.True(code == exitCode, $"{name}: exit {code}\n{output}");
Assert.True(lines.Any(l => l.StartsWith(outcome, StringComparison.Ordinal)), $"{name}: no '{outcome}'\n{output}");
Assert.True(lines.IndexOf(lines.First(l => l.StartsWith(outcome, StringComparison.Ordinal))) == lines.Count - 2, $"{name}: the outcome is not the last line\n{output}");
var expected = calls.Split(',', StringSplitOptions.RemoveEmptyEntries);
Assert.True(expected.SequenceEqual(made.Where(expected.Contains).Distinct()), $"{name}: calls {string.Join(',', made)}\n{output}");
foreach (var call in never.Split(',', StringSplitOptions.RemoveEmptyEntries))
{
Assert.DoesNotContain(call, made);
}
}
[Fact]
public async Task AFailureMasksTheTokenInItsOutcomeLine()
{
var shell = WindowsPowerShell() ?? Pwsh();
Assert.SkipWhen(shell is null, "Needs Windows PowerShell or pwsh.");
var body = "$server = 'x'; $token = 'abc123'\n" + InstallScripts.WindowsFunctions + "\nWrite-Output (Hide-ExecuteRToken 'GET https://x/?token=abc123 failed')\n";
using var folder = new ScratchFolder();
var (code, output, _) = await RunAsync(shell!, $"-NoProfile -NonInteractive -ExecutionPolicy Bypass -File \"{folder.Write("mask.ps1", body)}\"");
Assert.True(code == 0, output);
Assert.Contains("token= failed", output);
Assert.DoesNotContain("abc123", output);
}
private static void AssertVersionResults(Dictionary results, string output)
{
for (var i = 0; i < VersionPairs.Length; i++)
{
var expected = Math.Sign(AgentVersions.Compare(VersionPairs[i].Left, VersionPairs[i].Right)).ToString();
Assert.True(results.TryGetValue(i.ToString(), out var actual) && actual == expected,
$"{VersionPairs[i].Left} vs {VersionPairs[i].Right}: expected {expected}, got {actual}\n{output}");
}
}
private static Dictionary Results(string output)
=> Lines(output)
.Select(line => ResultLine().Match(line))
.Where(match => match.Success)
.ToDictionary(match => match.Groups[1].Value, match => match.Groups[2].Value);
[GeneratedRegex(@"^(\d+)=(.*)$")]
private static partial Regex ResultLine();
/// The token variable itself ($token, ${token}), not $token_pending.
[GeneratedRegex(@"\$\{?token\b")]
private static partial Regex TokenVariable();
private static IEnumerable Lines(string text)
=> text.Split('\n').Select(line => line.TrimEnd('\r').Trim()).Where(line => line.Length > 0);
private static string? WindowsPowerShell()
{
if (!OperatingSystem.IsWindows())
{
return null;
}
var path = Path.Combine(Environment.SystemDirectory, @"WindowsPowerShell\v1.0\powershell.exe");
return File.Exists(path) ? path : null;
}
private static string? Pwsh() => FindOnPath(OperatingSystem.IsWindows() ? "pwsh.exe" : "pwsh");
/// Git Bash first on Windows: System32's bash.exe is WSL's, which cannot take a Windows path.
private static string? Bash()
{
if (OperatingSystem.IsWindows())
{
var git = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), @"Git\bin\bash.exe");
return File.Exists(git) ? git : null;
}
return FindOnPath("bash");
}
private static string? FindOnPath(string name)
=> (Environment.GetEnvironmentVariable("PATH") ?? "")
.Split(Path.PathSeparator, StringSplitOptions.RemoveEmptyEntries)
.Select(directory => Path.Combine(directory, name))
.FirstOrDefault(File.Exists);
private static string ForBash(string path) => path.Replace('\\', '/');
private static async Task<(int Code, string Output, string Stdout)> RunAsync(string fileName, string arguments, Dictionary? environment = null)
{
var info = new ProcessStartInfo(fileName, arguments)
{
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
};
foreach (var (name, value) in environment ?? [])
{
info.Environment[name] = value;
}
using var process = Process.Start(info)!;
var output = process.StandardOutput.ReadToEndAsync();
var errors = process.StandardError.ReadToEndAsync();
using var timeout = new CancellationTokenSource(TimeSpan.FromMinutes(2));
await process.WaitForExitAsync(timeout.Token);
return (process.ExitCode, await output + await errors, await output);
}
private sealed class ScratchFolder : IDisposable
{
public string Path { get; } = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "executer-install-" + Guid.NewGuid().ToString("N"));
public ScratchFolder() => Directory.CreateDirectory(Path);
public string Write(string name, string content)
{
var file = System.IO.Path.Combine(Path, name);
File.WriteAllText(file, content);
return file;
}
public void Dispose() => Directory.Delete(Path, recursive: true);
}
}