513fa1aa16
Comment/XML-doc changes only: - add summaries to ~10k test methods, fixtures, fakes and helpers (+ <param> on theories, <returns> on async Task methods, <inheritdoc/> on interface implementations the checker can resolve) - strip ~2.6k project-tracking references (WP*, P*-*, row-NN, Task N, #NN, Component-0NN) from comments, keeping the explanatory text; reword false positives in comments only - real docs on fakes implementing BCL/library interfaces (IDisposable, ILogger, IOptionsMonitor, IAsyncLifetime) - reword the last 5 src/ false positives (CWE-1236, failures-1, year-0001, M365) Only non-comment edits: four one-line nested classes split across lines so members can carry docs. Build 0 errors / 0 warnings.
178 lines
7.0 KiB
C#
178 lines
7.0 KiB
C#
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Options;
|
|
using ZB.MOM.WW.ScadaBridge.AuditLog.Configuration;
|
|
|
|
namespace ZB.MOM.WW.ScadaBridge.AuditLog.Tests.Configuration;
|
|
|
|
/// <summary>
|
|
/// Tests for <see cref="AuditLogOptions"/> binding and validator
|
|
/// behavior. The validator enforces invariants used by the audit writers
|
|
/// (per <c>docs/plans/2026-05-20-auditlog-m1-foundation.md</c>):
|
|
/// <c>DefaultCapBytes > 0</c>, <c>ErrorCapBytes >= DefaultCapBytes</c>,
|
|
/// <c>RetentionDays in [30, 3650]</c>. Header-redact defaults match the
|
|
/// design doc (alog.md §6): Authorization, X-Api-Key, Cookie, Set-Cookie.
|
|
/// </summary>
|
|
public class AuditLogOptionsTests
|
|
{
|
|
private static IOptions<AuditLogOptions> BuildOptions(Dictionary<string, string?> config)
|
|
{
|
|
var configuration = new ConfigurationBuilder()
|
|
.AddInMemoryCollection(config)
|
|
.Build();
|
|
var services = new ServiceCollection();
|
|
services.AddAuditLog(configuration);
|
|
return services.BuildServiceProvider().GetRequiredService<IOptions<AuditLogOptions>>();
|
|
}
|
|
|
|
/// <summary>Verifies that a fully populated configuration section binds every scalar field onto the options.</summary>
|
|
[Fact]
|
|
public void ValidBinding_PopulatesAllScalarFields()
|
|
{
|
|
var opts = BuildOptions(new Dictionary<string, string?>
|
|
{
|
|
["AuditLog:DefaultCapBytes"] = "4096",
|
|
["AuditLog:ErrorCapBytes"] = "32768",
|
|
["AuditLog:RetentionDays"] = "180",
|
|
}).Value;
|
|
|
|
Assert.Equal(4096, opts.DefaultCapBytes);
|
|
Assert.Equal(32768, opts.ErrorCapBytes);
|
|
Assert.Equal(180, opts.RetentionDays);
|
|
}
|
|
|
|
/// <summary>Verifies that an empty configuration section yields reasonable default option values.</summary>
|
|
[Fact]
|
|
public void DefaultsAreReasonable_WhenSectionEmpty()
|
|
{
|
|
var opts = BuildOptions(new Dictionary<string, string?>()).Value;
|
|
|
|
Assert.Equal(8192, opts.DefaultCapBytes);
|
|
Assert.Equal(65536, opts.ErrorCapBytes);
|
|
Assert.Equal(365, opts.RetentionDays);
|
|
Assert.Contains("Authorization", opts.HeaderRedactList);
|
|
Assert.Contains("X-Api-Key", opts.HeaderRedactList);
|
|
Assert.Contains("Cookie", opts.HeaderRedactList);
|
|
Assert.Contains("Set-Cookie", opts.HeaderRedactList);
|
|
Assert.Empty(opts.GlobalBodyRedactors);
|
|
Assert.Empty(opts.PerTargetOverrides);
|
|
}
|
|
|
|
/// <summary>Verifies that header redaction entries bound from configuration are appended to the default list.</summary>
|
|
[Fact]
|
|
public void HeaderRedactList_BindsFromConfig_AppendsToDefaults()
|
|
{
|
|
// Microsoft.Extensions.Configuration's collection binder appends to a
|
|
// defaulted list (it does not replace it), so config-supplied entries
|
|
// augment the built-in redact list rather than overriding it. The
|
|
// built-in entries are the safety-net defaults documented on
|
|
// AuditLogOptions; supplying additional headers is the supported
|
|
// extension point.
|
|
var opts = BuildOptions(new Dictionary<string, string?>
|
|
{
|
|
["AuditLog:HeaderRedactList:0"] = "X-Custom-Auth",
|
|
["AuditLog:HeaderRedactList:1"] = "X-Tenant-Id",
|
|
}).Value;
|
|
|
|
Assert.Contains("X-Custom-Auth", opts.HeaderRedactList);
|
|
Assert.Contains("X-Tenant-Id", opts.HeaderRedactList);
|
|
Assert.Contains("Authorization", opts.HeaderRedactList);
|
|
Assert.Contains("X-Api-Key", opts.HeaderRedactList);
|
|
}
|
|
|
|
/// <summary>Verifies that per-target override settings bind from configuration.</summary>
|
|
[Fact]
|
|
public void PerTargetOverrides_BindsFromConfig()
|
|
{
|
|
var opts = BuildOptions(new Dictionary<string, string?>
|
|
{
|
|
["AuditLog:PerTargetOverrides:CRM:CapBytes"] = "16384",
|
|
["AuditLog:PerTargetOverrides:CRM:AdditionalBodyRedactors:0"] = @"\d{16}",
|
|
}).Value;
|
|
|
|
Assert.True(opts.PerTargetOverrides.ContainsKey("CRM"));
|
|
var crm = opts.PerTargetOverrides["CRM"];
|
|
Assert.Equal(16384, crm.CapBytes);
|
|
Assert.NotNull(crm.AdditionalBodyRedactors);
|
|
Assert.Contains(@"\d{16}", crm.AdditionalBodyRedactors!);
|
|
}
|
|
|
|
/// <summary>Verifies that an invalid default payload cap fails validation.</summary>
|
|
[Fact]
|
|
public void InvalidDefaultCapBytes_FailsValidation()
|
|
{
|
|
var result = new AuditLogOptionsValidator().Validate(
|
|
Options.DefaultName,
|
|
new AuditLogOptions { DefaultCapBytes = 0 });
|
|
|
|
Assert.True(result.Failed);
|
|
Assert.Contains("DefaultCapBytes", result.FailureMessage);
|
|
}
|
|
|
|
/// <summary>Verifies that an invalid error payload cap fails validation.</summary>
|
|
[Fact]
|
|
public void InvalidErrorCapBytes_FailsValidation()
|
|
{
|
|
var result = new AuditLogOptionsValidator().Validate(
|
|
Options.DefaultName,
|
|
new AuditLogOptions { DefaultCapBytes = 1000, ErrorCapBytes = 100 });
|
|
|
|
Assert.True(result.Failed);
|
|
Assert.Contains("ErrorCapBytes", result.FailureMessage);
|
|
}
|
|
|
|
/// <summary>Verifies that a retention period below the minimum fails validation.</summary>
|
|
[Fact]
|
|
public void RetentionDaysBelowMinimum_FailsValidation()
|
|
{
|
|
var result = new AuditLogOptionsValidator().Validate(
|
|
Options.DefaultName,
|
|
new AuditLogOptions { RetentionDays = 0 });
|
|
|
|
Assert.True(result.Failed);
|
|
Assert.Contains("RetentionDays", result.FailureMessage);
|
|
}
|
|
|
|
/// <summary>Verifies that a retention period above the maximum fails validation.</summary>
|
|
[Fact]
|
|
public void RetentionDaysAboveMaximum_FailsValidation()
|
|
{
|
|
var result = new AuditLogOptionsValidator().Validate(
|
|
Options.DefaultName,
|
|
new AuditLogOptions { RetentionDays = 3651 });
|
|
|
|
Assert.True(result.Failed);
|
|
Assert.Contains("RetentionDays", result.FailureMessage);
|
|
}
|
|
|
|
/// <summary>Verifies that the default options pass validation.</summary>
|
|
[Fact]
|
|
public void DefaultOptions_PassValidation()
|
|
{
|
|
var result = new AuditLogOptionsValidator().Validate(
|
|
Options.DefaultName,
|
|
new AuditLogOptions());
|
|
|
|
Assert.True(result.Succeeded, result.FailureMessage);
|
|
}
|
|
|
|
/// <summary>Verifies that an invalid retention value bound from configuration is rejected when the options value is accessed.</summary>
|
|
[Fact]
|
|
public void InvalidRetention_BoundViaConfig_RejectedOnValueAccess()
|
|
{
|
|
var configuration = new ConfigurationBuilder()
|
|
.AddInMemoryCollection(new Dictionary<string, string?>
|
|
{
|
|
["AuditLog:RetentionDays"] = "0",
|
|
})
|
|
.Build();
|
|
var services = new ServiceCollection();
|
|
services.AddAuditLog(configuration);
|
|
var provider = services.BuildServiceProvider();
|
|
var opts = provider.GetRequiredService<IOptions<AuditLogOptions>>();
|
|
|
|
var ex = Assert.Throws<OptionsValidationException>(() => _ = opts.Value);
|
|
Assert.Contains("RetentionDays", ex.Message);
|
|
}
|
|
}
|