Improve testability, packaging, and docs
This commit is contained in:
@@ -1,5 +1,3 @@
|
||||
using System.Globalization;
|
||||
|
||||
namespace LibNftables.Tests;
|
||||
|
||||
public sealed class NftContextTests
|
||||
@@ -7,7 +5,7 @@ public sealed class NftContextTests
|
||||
[Fact]
|
||||
public void ContextFlagsRoundTrip_Works()
|
||||
{
|
||||
if (!TryCreateContext(out var ctx))
|
||||
if (!NativeTestSupport.TryCreateContext(out var ctx))
|
||||
{
|
||||
return;
|
||||
}
|
||||
@@ -38,7 +36,7 @@ public sealed class NftContextTests
|
||||
[Fact]
|
||||
public void InvalidCommand_ThrowsNftExceptionWithErrorBuffer()
|
||||
{
|
||||
if (!TryCreateContext(out var ctx))
|
||||
if (!NativeTestSupport.TryCreateContext(out var ctx))
|
||||
{
|
||||
return;
|
||||
}
|
||||
@@ -57,12 +55,12 @@ public sealed class NftContextTests
|
||||
[Fact]
|
||||
public void ValidDryRunCommand_CanExecuteAndBufferOutput()
|
||||
{
|
||||
if (!TryCreateContext(out var ctx))
|
||||
if (!NativeTestSupport.TryCreateContext(out var ctx))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!HasCapNetAdmin())
|
||||
if (!NativeTestSupport.HasCapNetAdmin())
|
||||
{
|
||||
return;
|
||||
}
|
||||
@@ -82,61 +80,4 @@ public sealed class NftContextTests
|
||||
}
|
||||
}
|
||||
|
||||
private static bool HasCapNetAdmin()
|
||||
{
|
||||
const int capNetAdminBit = 12;
|
||||
const ulong mask = 1UL << capNetAdminBit;
|
||||
|
||||
try
|
||||
{
|
||||
foreach (var line in File.ReadLines("/proc/self/status"))
|
||||
{
|
||||
if (!line.StartsWith("CapEff:", StringComparison.Ordinal))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var hex = line["CapEff:".Length..].Trim();
|
||||
if (ulong.TryParse(hex, NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture, out var value))
|
||||
{
|
||||
return (value & mask) != 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
// If capability probing fails, keep test conservative.
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private static bool TryCreateContext(out NftContext context)
|
||||
{
|
||||
try
|
||||
{
|
||||
context = new NftContext();
|
||||
return true;
|
||||
}
|
||||
catch (DllNotFoundException)
|
||||
{
|
||||
context = null!;
|
||||
return false;
|
||||
}
|
||||
catch (TypeInitializationException ex) when (ex.InnerException is DllNotFoundException)
|
||||
{
|
||||
context = null!;
|
||||
return false;
|
||||
}
|
||||
catch (BadImageFormatException)
|
||||
{
|
||||
context = null!;
|
||||
return false;
|
||||
}
|
||||
catch (EntryPointNotFoundException)
|
||||
{
|
||||
context = null!;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user