Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions windows-node-host.Tests/CommandConsentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,19 @@ public CommandConsentTests()
private NodeInvokeRequest Request(params string[] argv) =>
CommandConsent.Request(argv, _root, 10_000, "main", "agent:main:test");

[Fact]
public void ConsentRequestOmitsTimeoutForUpstreamValidationButPreservesFiniteTimeout()
{
var command = new[] { Path.Combine(Environment.SystemDirectory, "hostname.exe") };
var noTimeout = CommandConsent.Request(command, _root, null, "main", "agent:main:test");
var finiteTimeout = CommandConsent.Request(command, _root, 10_000, "main", "agent:main:test");

Assert.False(noTimeout.Args.TryGetProperty("timeoutMs", out _));
Assert.True(ExecApprovalV2InputValidator.Validate(noTimeout).IsValid);
Assert.Equal(10_000, finiteTimeout.Args.GetProperty("timeoutMs").GetInt32());
Assert.True(ExecApprovalV2InputValidator.Validate(finiteTimeout).IsValid);
}

[Fact]
public async Task PreparationDoesNotRememberAndPolicyCannotApproveAMiss()
{
Expand Down
68 changes: 64 additions & 4 deletions windows-node-host.Tests/CwdPolicyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,17 @@ await WriteActivationLease(leasePath, leaseSecret, ActivationLeaseMode.Active,
policyFingerprint: fingerprint,
commandConsent: consent);

async Task<JsonElement> Run(string payload, string? cwd, string decision, bool requiresApproval = true)
async Task<JsonElement> Run(
string payload,
string? cwd,
string decision,
bool requiresApproval = true,
int? timeoutMs = 30_000)
{
var input = JsonSerializer.SerializeToElement(new
{
command = new[] { Path.Combine(Environment.SystemDirectory, "cmd.exe"), "/d", "/s", "/c", payload },
cwd, rawCommand = payload, agentId = "main", sessionKey = "agent:main:test",
cwd, rawCommand = payload, agentId = "main", sessionKey = "agent:main:test", timeoutMs,
});
var prepared = await capability.ExecuteAsync(new NodeInvokeRequest
{
Expand All @@ -102,7 +107,7 @@ async Task<JsonElement> Run(string payload, string? cwd, string decision, bool r
var dispatch = JsonSerializer.SerializeToElement(new
{
command = argv, cwd, rawCommand = commandText,
agentId = "main", sessionKey = "agent:main:test", systemRunPlan = plan,
agentId = "main", sessionKey = "agent:main:test", timeoutMs, systemRunPlan = plan,
});
var now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
var proof = new ApprovalProofRecord(
Expand All @@ -113,7 +118,7 @@ async Task<JsonElement> Run(string payload, string? cwd, string decision, bool r
var request = JsonSerializer.SerializeToElement(new
{
command = argv, cwd, rawCommand = commandText,
agentId = "main", sessionKey = "agent:main:test",
agentId = "main", sessionKey = "agent:main:test", timeoutMs,
systemRunPlan = plan, microclawApprovalProof = proof,
});
var response = await capability.ExecuteAsync(new NodeInvokeRequest
Expand Down Expand Up @@ -143,6 +148,10 @@ async Task<JsonElement> Run(string payload, string? cwd, string decision, bool r
Assert.True(fileRead.GetProperty("success").GetBoolean(), fileRead.GetRawText());
Assert.Equal("consent-file-content", fileRead.GetProperty("stdout").GetString()!.Trim());

var noTimeout = await Run("echo no-timeout", _root, "allow-once", timeoutMs: null);
Assert.True(noTimeout.GetProperty("success").GetBoolean(), noTimeout.GetRawText());
Assert.Equal("no-timeout", noTimeout.GetProperty("stdout").GetString());

var denied = await Run("echo should-not-run", _root, "policy");
Assert.False(denied.GetProperty("success").GetBoolean());
Assert.Equal("command-consent-denied", denied.GetProperty("errorCode").GetString());
Expand Down Expand Up @@ -383,6 +392,57 @@ public void PowerShellRunStripsDeclarationBeforeMxcCommandLine(string command)
Assert.Contains("\\\"C:\\Users\\sunt\\Desktop\\MXC-Approval-Smoke-20261020-1150\\\"", commandLine);
}

[Fact]
public void ParseRunUsesDefaultTimeoutWhenFieldIsMissing()
{
var capability = Capability(Policy([]));

var run = capability.ParseRun(Parse("""{"command":["cmd.exe"]}"""));

Assert.Equal(30_000, run.TimeoutMs);
}

[Theory]
[InlineData("null")]
[InlineData("0")]
public void ParseRunTreatsNullAndZeroAsNoTimeout(string value)
{
var capability = Capability(Policy([]));

var run = capability.ParseRun(Parse(
"""{"command":["cmd.exe"],"timeoutMs":""" + value + "}"));

Assert.Equal(0, run.TimeoutMs);
}

[Theory]
[InlineData("1000", 1000)]
[InlineData("2147483648", 600_000)]
public void ParseRunPreservesFiniteTimeoutsAndClampsTheMaximum(string value, int expected)
{
var capability = Capability(Policy([]));

var run = capability.ParseRun(Parse(
"""{"command":["cmd.exe"],"timeoutMs":""" + value + "}"));

Assert.Equal(expected, run.TimeoutMs);
}

[Theory]
[InlineData("-1")]
[InlineData("1.5")]
[InlineData("\"30000\"")]
[InlineData("true")]
public void ParseRunRejectsInvalidTimeoutValues(string value)
{
var capability = Capability(Policy([]));

var error = Assert.Throws<HostPolicyException>(() => capability.ParseRun(Parse(
"""{"command":["cmd.exe"],"timeoutMs":""" + value + "}")));

Assert.Equal("invalid-timeout", error.Code);
}

[Fact]
public async Task CanonicalCmdHostExecutionPreservesQuotedPathsAndMetacharacters()
{
Expand Down
14 changes: 13 additions & 1 deletion windows-node-host.Tests/HostCommandRunnerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,24 @@ public async Task CanonicalCmdTimesOutAndStopsItsProcessTree()
Assert.InRange(result.DurationMs, 1, 10_000);
}

[Fact]
public async Task CanonicalCmdDoesNotApplyADeadlineWhenTimeoutIsZero()
{
var result = await HostCommandRunner.RunAsync(
Cmd("ping -n 3 127.0.0.1 >nul & echo completed", timeoutMs: 0),
TestContext.Current.CancellationToken);

Assert.False(result.TimedOut);
Assert.Equal(0, result.ExitCode);
Assert.Equal("completed", result.Stdout);
}

[Fact]
public async Task CancellationStopsCanonicalCmd()
{
using var cancellation = new CancellationTokenSource(TimeSpan.FromMilliseconds(300));
await Assert.ThrowsAnyAsync<OperationCanceledException>(() =>
HostCommandRunner.RunAsync(Cmd("ping -n 30 127.0.0.1 >nul"), cancellation.Token));
HostCommandRunner.RunAsync(Cmd("ping -n 30 127.0.0.1 >nul", timeoutMs: 0), cancellation.Token));
}

[Fact]
Expand Down
Loading
Loading