87 lines
2.1 KiB
C#
87 lines
2.1 KiB
C#
namespace LibNftables.Tests;
|
|
|
|
public sealed class NftablesClientIntegrationTests
|
|
{
|
|
[Fact]
|
|
public void Validate_InvalidRuleset_ReturnsInvalidResult()
|
|
{
|
|
if (!CanCreateClient())
|
|
{
|
|
return;
|
|
}
|
|
|
|
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 Task ValidateAsync_InvalidRuleset_ReturnsInvalidResult()
|
|
{
|
|
if (!CanCreateClient())
|
|
{
|
|
return;
|
|
}
|
|
|
|
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()
|
|
{
|
|
if (!CanCreateClient())
|
|
{
|
|
return;
|
|
}
|
|
|
|
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()
|
|
{
|
|
if (!CanCreateClient())
|
|
{
|
|
return;
|
|
}
|
|
|
|
var client = new NftablesClient();
|
|
|
|
try
|
|
{
|
|
NftSnapshot snapshot = client.Snapshot();
|
|
Assert.False(string.IsNullOrWhiteSpace(snapshot.RulesetText));
|
|
}
|
|
catch (NftPermissionException)
|
|
{
|
|
// Expected in unprivileged environments.
|
|
}
|
|
}
|
|
|
|
private static bool CanCreateClient()
|
|
{
|
|
try
|
|
{
|
|
_ = new NftablesClient();
|
|
return true;
|
|
}
|
|
catch (NftException)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
}
|