Files
ExecuteR/tests/Agent.Tests/AgentConfigurationTests.cs
Alphaeus Mote 36cd4d2272 Merge registry editor [skip ci]
# Conflicts:
#	README.md
#	src/Installer/Product.wxs
2026-11-06 15:03:37 -05:00

235 lines
10 KiB
C#

using Agent.Configuration;
using Agent.Storage;
using Shared.Infrastructure.Configuration;
using Shared.Infrastructure.Constants;
using Xunit;
namespace Agent.Tests;
public class AgentConfigurationTests
{
private readonly InMemoryRegistryStore registry = new();
[Fact]
public void GroupPolicyOverridesNormalRegistry()
{
registry.SetString(Branding.AgentRegistryKey, AgentConfigurationKeys.ServerUrl, "https://normal.example.com");
registry.SetString(Branding.AgentPolicyRegistryKey, AgentConfigurationKeys.ServerUrl, "https://policy.example.com");
var config = new AgentConfigurationLoader(registry, allowEnvironment: false).Load();
Assert.Equal("https://policy.example.com", config.ServerUrlText.Value);
Assert.Equal(ConfigurationSource.GroupPolicy, config.ServerUrlText.Source);
Assert.True(config.ServerUrlText.IsPolicyControlled);
}
[Fact]
public void NormalRegistryUsedWhenNoPolicy()
{
registry.SetString(Branding.AgentRegistryKey, AgentConfigurationKeys.ServerUrl, "https://normal.example.com");
var config = new AgentConfigurationLoader(registry, allowEnvironment: false).Load();
Assert.Equal("https://normal.example.com", config.ServerUrlText.Value);
Assert.Equal(ConfigurationSource.Registry, config.ServerUrlText.Source);
}
[Fact]
public void EnvironmentNeverOverridesGroupPolicyOrRegistry()
{
Environment.SetEnvironmentVariable(AgentConfigurationKeys.EnvironmentNames.ServerUrl, "https://env.example.com");
try
{
registry.SetString(Branding.AgentRegistryKey, AgentConfigurationKeys.ServerUrl, "https://normal.example.com");
var config = new AgentConfigurationLoader(registry, allowEnvironment: true).Load();
Assert.Equal("https://normal.example.com", config.ServerUrlText.Value);
}
finally
{
Environment.SetEnvironmentVariable(AgentConfigurationKeys.EnvironmentNames.ServerUrl, null);
}
}
[Fact]
public void EnvironmentAppliesOnlyInDevelopmentMode()
{
Environment.SetEnvironmentVariable(AgentConfigurationKeys.EnvironmentNames.ServerUrl, "https://env.example.com");
try
{
var dev = new AgentConfigurationLoader(registry, allowEnvironment: true).Load();
Assert.Equal("https://env.example.com", dev.ServerUrlText.Value);
Assert.Equal(ConfigurationSource.EnvironmentDevelopmentOnly, dev.ServerUrlText.Source);
var service = new AgentConfigurationLoader(registry, allowEnvironment: false).Load();
Assert.Null(service.ServerUrlText.Value);
Assert.Equal(ConfigurationSource.Default, service.ServerUrlText.Source);
}
finally
{
Environment.SetEnvironmentVariable(AgentConfigurationKeys.EnvironmentNames.ServerUrl, null);
}
}
[Fact]
public void DefaultsApplyWhenNothingConfigured()
{
var config = new AgentConfigurationLoader(registry, allowEnvironment: false).Load();
Assert.Equal(ProtocolDefaults.DefaultInventoryInterval, config.InventoryInterval.Value);
Assert.Equal(ProtocolDefaults.ReconnectMinDelay, config.ReconnectMinDelay.Value);
Assert.Equal(ProtocolDefaults.ReconnectMaxDelay, config.ReconnectMaxDelay.Value);
Assert.True(config.AllowRemoteShell.Value); // on by default (opt-out)
Assert.True(config.AllowScriptExecution.Value);
Assert.True(config.AllowFileAccess.Value);
Assert.False(config.TlsSkipVerify.Value);
Assert.Null(config.ProxyUrl.Value);
Assert.Equal("Information", config.LogLevel.Value);
}
[Fact]
public void ProxyUrlFollowsStringPrecedence()
{
registry.SetString(Branding.AgentRegistryKey, AgentConfigurationKeys.ProxyUrl, "http://local-proxy:8080");
var local = new AgentConfigurationLoader(registry, allowEnvironment: false).Load();
Assert.Equal("http://local-proxy:8080", local.ProxyUrl.Value);
registry.SetString(Branding.AgentPolicyRegistryKey, AgentConfigurationKeys.ProxyUrl, "http://gpo-proxy:8080");
var policy = new AgentConfigurationLoader(registry, allowEnvironment: false).Load();
Assert.Equal("http://gpo-proxy:8080", policy.ProxyUrl.Value);
Assert.True(policy.ProxyUrl.IsPolicyControlled);
}
[Fact]
public void TlsSkipVerifyFollowsDwordPrecedence()
{
registry.SetDword(Branding.AgentRegistryKey, AgentConfigurationKeys.TlsSkipVerify, 1);
var local = new AgentConfigurationLoader(registry, allowEnvironment: false).Load();
Assert.True(local.TlsSkipVerify.Value);
// GPO wins over the locally configured value.
registry.SetDword(Branding.AgentPolicyRegistryKey, AgentConfigurationKeys.TlsSkipVerify, 0);
var policy = new AgentConfigurationLoader(registry, allowEnvironment: false).Load();
Assert.False(policy.TlsSkipVerify.Value);
Assert.True(policy.TlsSkipVerify.IsPolicyControlled);
}
[Fact]
public void RegistryAccessIsOnByDefault_RegistryTurnsItOff_AndGroupPolicyWins()
{
Assert.True(new AgentConfigurationLoader(registry, allowEnvironment: false).Load().AllowRegistryAccess.Value);
registry.SetDword(Branding.AgentRegistryKey, AgentConfigurationKeys.AllowRegistryAccess, 0);
var local = new AgentConfigurationLoader(registry, allowEnvironment: false).Load();
Assert.False(local.AllowRegistryAccess.Value);
Assert.Equal(ConfigurationSource.Registry, local.AllowRegistryAccess.Source);
registry.SetDword(Branding.AgentPolicyRegistryKey, AgentConfigurationKeys.AllowRegistryAccess, 1);
var policy = new AgentConfigurationLoader(registry, allowEnvironment: false).Load();
Assert.True(policy.AllowRegistryAccess.Value);
Assert.True(policy.AllowRegistryAccess.IsPolicyControlled);
}
[Fact]
public void RemoteViewPolicyDefaults_AndGroupPolicyWins()
{
var defaults = new AgentConfigurationLoader(registry, allowEnvironment: false).Load();
Assert.True(defaults.AllowRemoteView.Value);
Assert.Equal(Shared.Contracts.Models.ScreenSessionPolicy.Default, Agent.Screen.ScreenHost.PolicyOf(defaults));
registry.SetDword(Branding.AgentRegistryKey, AgentConfigurationKeys.RemoteViewAskFirst, 1);
registry.SetString(Branding.AgentRegistryKey, AgentConfigurationKeys.RemoteViewAskTimeout, "45");
registry.SetDword(Branding.AgentPolicyRegistryKey, AgentConfigurationKeys.RemoteViewAllowUnattended, 0);
registry.SetDword(Branding.AgentPolicyRegistryKey, AgentConfigurationKeys.RemoteViewNotify, 0);
registry.SetDword(Branding.AgentPolicyRegistryKey, AgentConfigurationKeys.AllowRemoteView, 0);
var configured = new AgentConfigurationLoader(registry, allowEnvironment: false).Load();
Assert.False(configured.AllowRemoteView.Value);
Assert.True(configured.AllowRemoteView.IsPolicyControlled);
Assert.Equal(new Shared.Contracts.Models.ScreenSessionPolicy(false, true, 45, false), Agent.Screen.ScreenHost.PolicyOf(configured));
}
[Fact]
public void TheBeingViewedBannerIsOffByDefault_RegistryTurnsItOn_AndGroupPolicyWins()
{
var defaults = new AgentConfigurationLoader(registry, allowEnvironment: false).Load();
Assert.False(defaults.RemoteViewBanner.Value);
Assert.False(Agent.Screen.ScreenHost.PolicyOf(defaults).Banner);
registry.SetDword(Branding.AgentRegistryKey, AgentConfigurationKeys.RemoteViewBanner, 1);
var local = new AgentConfigurationLoader(registry, allowEnvironment: false).Load();
Assert.True(local.RemoteViewBanner.Value);
Assert.True(Agent.Screen.ScreenHost.PolicyOf(local).Banner);
registry.SetDword(Branding.AgentPolicyRegistryKey, AgentConfigurationKeys.RemoteViewBanner, 0);
var policy = new AgentConfigurationLoader(registry, allowEnvironment: false).Load();
Assert.False(policy.RemoteViewBanner.Value);
Assert.True(policy.RemoteViewBanner.IsPolicyControlled);
}
[Fact]
public void IntervalAcceptsSecondsAndTimeSpanFormats()
{
registry.SetString(Branding.AgentRegistryKey, AgentConfigurationKeys.InventoryInterval, "300");
registry.SetString(Branding.AgentRegistryKey, AgentConfigurationKeys.ReconnectMinDelay, "00:00:05");
var config = new AgentConfigurationLoader(registry, allowEnvironment: false).Load();
Assert.Equal(TimeSpan.FromMinutes(5), config.InventoryInterval.Value);
Assert.Equal(TimeSpan.FromSeconds(5), config.ReconnectMinDelay.Value);
}
[Fact]
public void PolicyDwordControlsRemoteShell()
{
registry.SetDword(Branding.AgentRegistryKey, AgentConfigurationKeys.AllowRemoteShell, 1);
registry.SetDword(Branding.AgentPolicyRegistryKey, AgentConfigurationKeys.AllowRemoteShell, 0);
var config = new AgentConfigurationLoader(registry, allowEnvironment: false).Load();
Assert.False(config.AllowRemoteShell.Value);
Assert.True(config.AllowRemoteShell.IsPolicyControlled);
}
[Fact]
public void InvalidServerUrlYieldsNullParsedUrl()
{
registry.SetString(Branding.AgentRegistryKey, AgentConfigurationKeys.ServerUrl, "ftp://bad.example.com");
var config = new AgentConfigurationLoader(registry, allowEnvironment: false).Load();
Assert.NotNull(config.ServerUrlText.Value);
Assert.Null(config.ServerUrl);
}
[Fact]
public void ClearBootstrapTokenRemovesOnlyThatValue()
{
registry.SetString(Branding.AgentRegistryKey, AgentConfigurationKeys.BootstrapToken, "secret-token");
registry.SetString(Branding.AgentRegistryKey, AgentConfigurationKeys.ServerUrl, "https://control.example.com");
var store = new RuntimeStateStore(registry);
store.ClearBootstrapToken();
Assert.False(registry.Contains(Branding.AgentRegistryKey, AgentConfigurationKeys.BootstrapToken));
Assert.True(registry.Contains(Branding.AgentRegistryKey, AgentConfigurationKeys.ServerUrl));
}
[Fact]
public void RuntimeStateRoundTrips()
{
var store = new RuntimeStateStore(registry);
var now = DateTimeOffset.UtcNow;
store.ClientId = "abc123";
store.LastEnrollmentUtc = now;
Assert.Equal("abc123", store.ClientId);
Assert.Equal(now, store.LastEnrollmentUtc);
store.Purge();
Assert.Null(store.ClientId);
Assert.Null(store.LastEnrollmentUtc);
}
}