67 lines
1.9 KiB
C#
67 lines
1.9 KiB
C#
namespace LibNftables.Tests;
|
|
|
|
public sealed class NftablesClientTests
|
|
{
|
|
[Fact]
|
|
public void Apply_WithBothTextAndFile_ThrowsValidationException()
|
|
{
|
|
var client = new NftablesClient();
|
|
var request = new NftApplyRequest
|
|
{
|
|
RulesetText = "flush ruleset",
|
|
RulesetFilePath = "/tmp/does-not-matter.nft",
|
|
};
|
|
|
|
Assert.Throws<NftValidationException>(() => client.Apply(request));
|
|
}
|
|
|
|
[Fact]
|
|
public void Validate_InvalidRuleset_ReturnsInvalidResult()
|
|
{
|
|
var client = new NftablesClient();
|
|
var request = NftApplyRequest.FromText("this is not valid nft syntax");
|
|
|
|
NftValidationResult result = client.Validate(request);
|
|
|
|
Assert.False(result.IsValid);
|
|
Assert.False(string.IsNullOrWhiteSpace(result.Diagnostics));
|
|
}
|
|
|
|
[Fact]
|
|
public async System.Threading.Tasks.Task ValidateAsync_InvalidRuleset_ReturnsInvalidResult()
|
|
{
|
|
var client = new NftablesClient();
|
|
var request = NftApplyRequest.FromText("this is not valid nft syntax");
|
|
|
|
NftValidationResult result = await client.ValidateAsync(request);
|
|
|
|
Assert.False(result.IsValid);
|
|
Assert.False(string.IsNullOrWhiteSpace(result.Diagnostics));
|
|
}
|
|
|
|
[Fact]
|
|
public void Apply_InvalidRuleset_ThrowsValidationException()
|
|
{
|
|
var client = new NftablesClient();
|
|
var request = NftApplyRequest.FromText("this is not valid nft syntax");
|
|
|
|
Assert.Throws<NftValidationException>(() => client.Apply(request));
|
|
}
|
|
|
|
[Fact]
|
|
public void Snapshot_WithInsufficientPrivileges_ThrowsPermissionOrReturnsRuleset()
|
|
{
|
|
var client = new NftablesClient();
|
|
|
|
try
|
|
{
|
|
NftSnapshot snapshot = client.Snapshot();
|
|
Assert.False(string.IsNullOrWhiteSpace(snapshot.RulesetText));
|
|
}
|
|
catch (NftPermissionException)
|
|
{
|
|
// Expected in unprivileged environments.
|
|
}
|
|
}
|
|
}
|