Skip to content

Commit a27ae3e

Browse files
spliticeMathew
andauthored
convert to xunit (#39)
Co-authored-by: Mathew <me@mheard.com>
1 parent c3a9867 commit a27ae3e

61 files changed

Lines changed: 946 additions & 1092 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

AGENTS.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## Overview
44

5-
This repository contains a .NET 10 solution for working with Linux iptables from C#. The managed code lives under `IPTables.Net/`, the NUnit tests live under `IPTables.Net.Tests/`, and the native helper library used by the libiptc-based adapter lives under `ipthelper/`.
5+
This repository contains a .NET 10 solution for working with Linux iptables from C#. The managed code lives under `IPTables.Net/`, the xUnit tests live under `IPTables.Net.Tests/`, and the native helper library used by the libiptc-based adapter lives under `ipthelper/`.
66

77
## Build And Test
88

@@ -31,7 +31,7 @@ Both scripts will bootstrap a usable .NET SDK if `dotnet` is missing. By default
3131

3232
## Development Guidance
3333

34-
- Keep changes narrow and add or update NUnit coverage in `IPTables.Net.Tests/` when behavior changes.
34+
- Keep changes narrow and add or update xUnit coverage in `IPTables.Net.Tests/` when behavior changes.
3535
- Prefer following the existing project layout instead of introducing new abstractions unless the current design is clearly blocking the work.
3636
- When changing native interop behavior, verify both the managed call sites in `IPTables.Net/Iptables/NativeLibrary/` and the corresponding code in `ipthelper/`.
3737
- The tests project is also the best source of usage examples for the public API.

IPTables.Net.TestFramework/IPTables.Net.TestFramework.csproj

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,16 @@
33
<PropertyGroup>
44
<TargetFramework>net10.0</TargetFramework>
55
</PropertyGroup>
6-
<ItemGroup>
7-
<ProjectReference Include="..\IPTables.Net\IPTables.Net.csproj" />
8-
</ItemGroup>
96

10-
<ItemGroup>
11-
<PackageReference Include="NUnit" Version="3.12.0" />
12-
</ItemGroup>
7+
<ItemGroup>
8+
<Using Include="Xunit" />
9+
</ItemGroup>
10+
11+
<ItemGroup>
12+
<ProjectReference Include="..\IPTables.Net\IPTables.Net.csproj" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<PackageReference Include="xunit.v3.assert" Version="3.2.2" />
17+
</ItemGroup>
1318
</Project>

IPTables.Net.TestFramework/MockIpsetSystemFactory.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System.Linq;
55
using SystemInteract;
66
using IPTables.Net.IpSet;
7-
using NUnit.Framework;
87

98
namespace IPTables.Net.TestFramework
109
{
@@ -31,7 +30,7 @@ public void TestSync(IpSetSets rulesNew, List<string> expectedCommands)
3130
{
3231
TestSync(rulesNew);
3332

34-
CollectionAssert.AreEqual(expectedCommands, Commands.Select(a => a.Value).ToList());
33+
Assert.Equal(expectedCommands, Commands.Select(a => a.Value).ToList());
3534
}
3635
}
3736
}

IPTables.Net.TestFramework/MockIptablesSystemFactory.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using IPTables.Net.Iptables;
77
using IPTables.Net.Iptables.Adapter.Client;
88
using IPTables.Net.Iptables.TableSync;
9-
using NUnit.Framework;
109

1110
namespace IPTables.Net.TestFramework
1211
{
@@ -57,7 +56,7 @@ public void TestSync<TSync>(IIPTablesAdapterClient client, IpTablesRuleSet rules
5756

5857
if (expectedCommands != null)
5958
{
60-
CollectionAssert.AreEqual(expectedCommands, ExecutionLog.Select(a => a.Value).ToList());
59+
Assert.Equal(expectedCommands, ExecutionLog.Select(a => a.Value).ToList());
6160
}
6261
}
6362
}

IPTables.Net.Tests/CheckDebugComparison.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
using IPTables.Net.Iptables.Modules.HashLimit;
2-
using NUnit.Framework;
32
using System;
43
using System.Linq;
54
using IPTables.Net.Iptables;
65
using static IPTables.Net.Iptables.IpTablesRule;
76

87
namespace IPTables.Net.Tests
98
{
10-
[TestFixture]
119
public class CheckDebugComparison
1210
{
13-
[TestCase]
11+
[Fact]
1412
public void TestHashLimitMemberProperties()
1513
{
1614
var hl = new HashLimitModule(4);

IPTables.Net.Tests/CheckInternalTables.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@
33
using System.Linq;
44
using System.Text;
55
using IPTables.Net.Iptables;
6-
using NUnit.Framework;
76

87
namespace IPTables.Net.Tests
98
{
10-
[TestFixture]
11-
class CheckInternalTables
9+
public class CheckInternalTables
1210
{
13-
[Test]
11+
[Fact]
1412
public void TestChains()
1513
{
1614
TestChain("filter", "INPUT");
@@ -33,7 +31,7 @@ public void TestChains()
3331

3432
private void TestChain(string table, string chain)
3533
{
36-
Assert.IsTrue(IPTablesTables.IsInternalChain(table, chain), String.Format("{0}:{1} should be internal", table, chain));
34+
Assert.True(IPTablesTables.IsInternalChain(table, chain), String.Format("{0}:{1} should be internal", table, chain));
3735
}
3836
}
3937
}

IPTables.Net.Tests/ConntrackLibraryTests.cs

Lines changed: 25 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -8,64 +8,44 @@
88
using IPTables.Net.Conntrack;
99
using IPTables.Net.Iptables.NativeLibrary;
1010
using IPTables.Net.Supporting;
11-
using NUnit.Framework;
1211

1312
namespace IPTables.Net.Tests
1413
{
15-
[TestFixture]
16-
[Category("NotWorkingOnTravis")]
17-
class ConntrackLibraryTests
14+
[Trait("Category", "NotWorkingOnTravis")]
15+
public class ConntrackLibraryTests
1816
{
19-
public static bool IsLinux
20-
{
21-
get
22-
{
23-
int p = (int)Environment.OSVersion.Platform;
24-
return (p == 4) || (p == 6) || (p == 128);
25-
}
26-
}
27-
28-
[Test]
17+
[Fact]
2918
public void TestStructureSize()
3019
{
31-
Assert.AreEqual(8 + IntPtr.Size, Marshal.SizeOf(typeof(ConntrackQueryFilter)));
20+
Assert.Equal(8 + IntPtr.Size, Marshal.SizeOf(typeof(ConntrackQueryFilter)));
3221
}
3322

34-
[Test]
23+
[Fact]
3524
public void TestDump()
3625
{
37-
if (IsLinux)
38-
{
39-
if (Environment.GetEnvironmentVariable("SKIP_SYSTEM_TESTS") == "1")
40-
{
41-
Assert.Ignore();
42-
}
43-
ConntrackSystem cts = new ConntrackSystem();
44-
List<byte[]> list = new List<byte[]>();
45-
cts.Dump(false,list.Add);
46-
}
26+
TestEnvironment.RequireLinuxSystemTests();
27+
28+
ConntrackSystem cts = new ConntrackSystem();
29+
List<byte[]> list = new List<byte[]>();
30+
cts.Dump(false, list.Add);
4731
}
4832

49-
[Test]
33+
[Fact]
5034
public void TestDumpFiltered()
5135
{
52-
if (IsLinux)
53-
{
54-
if (Environment.GetEnvironmentVariable("SKIP_SYSTEM_TESTS") == "1")
55-
{
56-
Assert.Ignore();
57-
}
58-
ConntrackSystem cts = new ConntrackSystem();
59-
IPAddress addr = IPAddress.Parse("1.1.1.1");
60-
UInt32 addr32;
61-
unchecked
62-
{
63-
addr32 = (UInt32)addr.ToInt();
64-
}
65-
36+
TestEnvironment.RequireLinuxSystemTests();
6637

38+
ConntrackSystem cts = new ConntrackSystem();
39+
IPAddress addr = IPAddress.Parse("1.1.1.1");
40+
UInt32 addr32;
41+
unchecked
42+
{
43+
addr32 = (UInt32)addr.ToInt();
44+
}
6745

68-
var pinned = GCHandle.Alloc(addr32, GCHandleType.Pinned);
46+
var pinned = GCHandle.Alloc(addr32, GCHandleType.Pinned);
47+
try
48+
{
6949
ConntrackQueryFilter[] qf = new ConntrackQueryFilter[]
7050
{
7151
new ConntrackQueryFilter{Key = cts.GetConstant("CTA_TUPLE_ORIG"), Max = cts.GetConstant("CTA_TUPLE_MAX"), CompareLength = 0},
@@ -77,7 +57,9 @@ public void TestDumpFiltered()
7757

7858
List<byte[]> list = new List<byte[]>();
7959
cts.Dump(false, list.Add, qf);
80-
60+
}
61+
finally
62+
{
8163
pinned.Free();
8264
}
8365
}

IPTables.Net.Tests/EscapeHelperTests.cs

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,33 @@
44
using System.Text;
55
using IPTables.Net.Iptables.Helpers;
66
using IPTables.Net.Supporting;
7-
using NUnit.Framework;
87

98
namespace IPTables.Net.Tests
109
{
11-
[TestFixture]
12-
class EscapeHelperTests
10+
public class EscapeHelperTests
1311
{
14-
[Test]
12+
[Fact]
1513
public void TestSpaces()
1614
{
17-
Assert.AreEqual("'a word'", ShellHelper.EscapeArguments("a word"));
18-
Assert.AreEqual("singleword", ShellHelper.EscapeArguments("singleword"));
15+
Assert.Equal("'a word'", ShellHelper.EscapeArguments("a word"));
16+
Assert.Equal("singleword", ShellHelper.EscapeArguments("singleword"));
1917
}
2018

21-
[Test]
19+
[Fact]
2220
public void TestPipe()
2321
{
24-
Assert.AreEqual("'|'", ShellHelper.EscapeArguments("|"));
25-
Assert.AreEqual("'a|word'", ShellHelper.EscapeArguments("a|word"));
26-
Assert.AreEqual("singleword", ShellHelper.EscapeArguments("singleword"));
22+
Assert.Equal("'|'", ShellHelper.EscapeArguments("|"));
23+
Assert.Equal("'a|word'", ShellHelper.EscapeArguments("a|word"));
24+
Assert.Equal("singleword", ShellHelper.EscapeArguments("singleword"));
2725
}
28-
[Test]
26+
[Fact]
2927
public void TestSpace()
3028
{
31-
Assert.AreEqual("a word", ShellHelper.BuildArgumentString(new []{"a", "word"}));
32-
Assert.AreEqual("\"two words\"", ShellHelper.BuildArgumentString(new[] { "two words" }));
33-
Assert.AreEqual("\"two words and \\\"punctuation\\\"\"", ShellHelper.BuildArgumentString(new[] { "two words and \"punctuation\"" }));
34-
Assert.AreEqual("bash -c \"bash -c \\\"echo a\\\"\"", ShellHelper.BuildArgumentString(new[] { "bash", "-c", ShellHelper.BuildArgumentString(new []{"bash", "-c", "echo a"}) }));
35-
Assert.AreEqual("bash -c \"bash -c \\\"echo \\\\\\\"a\\\\\\\"\\\"\"", ShellHelper.BuildArgumentString(new[] { "bash", "-c", ShellHelper.BuildArgumentString(new[] { "bash", "-c", "echo \"a\"" }) }));
29+
Assert.Equal("a word", ShellHelper.BuildArgumentString(new []{"a", "word"}));
30+
Assert.Equal("\"two words\"", ShellHelper.BuildArgumentString(new[] { "two words" }));
31+
Assert.Equal("\"two words and \\\"punctuation\\\"\"", ShellHelper.BuildArgumentString(new[] { "two words and \"punctuation\"" }));
32+
Assert.Equal("bash -c \"bash -c \\\"echo a\\\"\"", ShellHelper.BuildArgumentString(new[] { "bash", "-c", ShellHelper.BuildArgumentString(new []{"bash", "-c", "echo a"}) }));
33+
Assert.Equal("bash -c \"bash -c \\\"echo \\\\\\\"a\\\\\\\"\\\"\"", ShellHelper.BuildArgumentString(new[] { "bash", "-c", ShellHelper.BuildArgumentString(new[] { "bash", "-c", "echo \"a\"" }) }));
3634
}
3735
}
3836
}

IPTables.Net.Tests/GraphLayoutTests.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,12 @@
1010
using IPTables.Net.Iptables.Modules;
1111
using IPTables.Net.Iptables.Modules.Comment;
1212
using IPTables.Net.Tests.MockSystem;
13-
using NUnit.Framework;
1413

1514
namespace IPTables.Net.Tests
1615
{
17-
[TestFixture]
18-
class GraphLayoutTests
16+
public class GraphLayoutTests
1917
{
20-
[Test]
18+
[Fact]
2119
public void TestAddSingles()
2220
{
2321
List<IpCidr> addresses = new List<IpCidr>();
@@ -29,9 +27,9 @@ public void TestAddSingles()
2927
CidrGraph graph = CidrGraph.BuildGraph(addresses);
3028

3129
//These numbers are based on the best I could do on paper. This less than perfect algorithm can beat that.
32-
Assert.IsTrue(graph.CalculateMaxPathLength() < 20, "Criteria for a good algorithm (max): " + graph.CalculateMaxPathLength());
33-
Assert.IsTrue(graph.CalculateMinPathLength() < 10, "Criteria for a good algorithm (min): " + graph.CalculateMinPathLength());
34-
Assert.IsTrue(graph.CalculateAveragePathLength() < 11, "Criteria for a good algorithm (avg): " + graph.CalculateAveragePathLength());
30+
Assert.True(graph.CalculateMaxPathLength() < 20, "Criteria for a good algorithm (max): " + graph.CalculateMaxPathLength());
31+
Assert.True(graph.CalculateMinPathLength() < 10, "Criteria for a good algorithm (min): " + graph.CalculateMinPathLength());
32+
Assert.True(graph.CalculateAveragePathLength() < 11, "Criteria for a good algorithm (avg): " + graph.CalculateAveragePathLength());
3533
}
3634

3735

IPTables.Net.Tests/IPTables.Net.Tests.csproj

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
45
<TargetFramework>net10.0</TargetFramework>
56
</PropertyGroup>
67

78
<ItemGroup>
89
<Compile Remove="GraphLayoutTests.cs" />
910
</ItemGroup>
1011

12+
<ItemGroup>
13+
<Using Include="Xunit" />
14+
</ItemGroup>
15+
1116
<ItemGroup>
1217
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
13-
<PackageReference Include="NUnit" Version="3.12.0" />
14-
<PackageReference Include="NUnit3TestAdapter" Version="3.17.0" />
1518
<PackageReference Include="SystemInteract.Local" Version="1.1.5" />
19+
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.5">
20+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
21+
<PrivateAssets>all</PrivateAssets>
22+
</PackageReference>
23+
<PackageReference Include="xunit.v3" Version="3.2.2" />
1624
</ItemGroup>
1725

1826
<ItemGroup>

0 commit comments

Comments
 (0)