Improve testability, packaging, and docs
This commit is contained in:
86
tests/LibNftables.Tests/NftablesClientIntegrationTests.cs
Normal file
86
tests/LibNftables.Tests/NftablesClientIntegrationTests.cs
Normal file
@@ -0,0 +1,86 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user