Files
CommandForge/tests/CommandForge.Tests/RunCommandUseCaseTests.cs
T
poli0981andClaude Opus 4.8 6e9e5dc3c5 Phase 3a: elevation broker + Safe admin commands
- Elevator pipe server: accepts only a commandId, validates it against its OWN
  embedded catalog (id exists + requiresAdmin), runs via ProcessCommandExecutor,
  streams output, supports mid-run cancel. Never builds a command from pipe input.
- Pipe: per-session GUID name + current-user DACL; UI is the pipe server
  (medium IL), Elevator connects as client (high IL) to avoid the integrity barrier.
- BrokerElevationService: spawns the Elevator once per session via runas (one UAC),
  relays Run/Cancel and streamed output; graceful UAC-declined handling.
- RunCommandUseCase routes admin commands to the broker, others in-process.
- 6 Safe admin commands (DISM CheckHealth/ScanHealth/Analyze, chkdsk read,
  BitLocker status, Windows features) + EN/VI strings; system-maintenance populated.
- Wpf copies CommandForge.Elevator.exe next to the app (build + publish).
- Security hardening (from review): pin process WorkingDirectory to System32 so a
  bare executable name can't resolve from an attacker-controlled CWD when elevated.
- Tests: pipe protocol round-trip, ElevationServer over an in-process named pipe
  (fakes), admin/non-admin routing (35 total).

Build -c Release: 0 warnings. dotnet test: 35/35. UAC flow verified manually.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-17 20:54:10 +07:00

75 lines
2.3 KiB
C#

using System.Threading.Channels;
using CommandForge.Application.Ports;
using CommandForge.Application.UseCases;
using CommandForge.Domain;
namespace CommandForge.Tests;
/// <summary>Tests that commands route to the right executor based on <c>RequiresAdmin</c>.</summary>
public sealed class RunCommandUseCaseTests
{
[Fact]
public async Task AdminCommand_RoutesToElevation()
{
var executor = new Recording();
var elevation = new RecordingElevation();
var useCase = new RunCommandUseCase(executor, elevation);
await useCase.RunAsync(Command(requiresAdmin: true), Channel.CreateUnbounded<OutputLine>().Writer);
Assert.True(elevation.Called);
Assert.False(executor.Called);
}
[Fact]
public async Task NonAdminCommand_RoutesToExecutor()
{
var executor = new Recording();
var elevation = new RecordingElevation();
var useCase = new RunCommandUseCase(executor, elevation);
await useCase.RunAsync(Command(requiresAdmin: false), Channel.CreateUnbounded<OutputLine>().Writer);
Assert.True(executor.Called);
Assert.False(elevation.Called);
}
private static CommandDefinition Command(bool requiresAdmin) => new()
{
Id = "x",
CategoryId = "c",
TitleKey = "t",
DescriptionKey = "d",
Executable = "x",
RequiresAdmin = requiresAdmin,
};
private static ExecutionResult Ok() => new() { ExitCode = 0, Success = true, Duration = TimeSpan.Zero };
private sealed class Recording : ICommandExecutor
{
public bool Called { get; private set; }
public Task<ExecutionResult> ExecuteAsync(CommandDefinition command, ChannelWriter<OutputLine> output, CancellationToken cancellationToken)
{
Called = true;
output.TryComplete();
return Task.FromResult(Ok());
}
}
private sealed class RecordingElevation : IElevationService
{
public bool Called { get; private set; }
public bool IsElevationAvailable => true;
public Task<ExecutionResult> RunElevatedAsync(CommandDefinition command, ChannelWriter<OutputLine> output, CancellationToken cancellationToken)
{
Called = true;
output.TryComplete();
return Task.FromResult(Ok());
}
}
}