Skip to content

Commit

Permalink
fixes for compatibility with newer ubuntu
Browse files Browse the repository at this point in the history
  • Loading branch information
splitice committed Jan 1, 2024
1 parent 727dd07 commit d88956f
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 41 deletions.
13 changes: 11 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
name: build-${{matrix.os}}
strategy:
matrix:
os: [ubuntu-latest]
os: [ubuntu-2004, ubuntu-latest]

# The type of runner that the job will run on
runs-on: ${{ matrix.os }}
Expand Down Expand Up @@ -56,7 +56,16 @@ jobs:
run: dotnet restore

- name: Modprobe IP6 tables
run: sudo modprobe ip6_tables
run: |
sudo modprobe ip6_tables
sudo modprobe ip6table_filter
sudo modprobe ip6table_mangle
if [[ "${{ matrix.os }}" == "ubuntu-latest" ]]; then
update-alternatives --set iptables /usr/sbin/iptables-legacy
update-alternatives --set ip6tables /usr/sbin/ip6tables-legacy
update-
fi
- name: NUnit Tests
run: SKIP_SYSTEM_TESTS=1 dotnet test IPTables.Net.Tests
Expand Down
2 changes: 1 addition & 1 deletion IPTables.Net.Tests/IPTables.Net.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
<PackageReference Include="NUnit" Version="3.12.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.17.0" />
<PackageReference Include="SystemInteract.Local" Version="1.1.4-cibuild0005" />
<PackageReference Include="SystemInteract.Local" Version="1.1.4-cibuild0006" />
</ItemGroup>

<ItemGroup>
Expand Down
56 changes: 37 additions & 19 deletions IPTables.Net.Tests/IptablesLibraryTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using IPTables.Net.Iptables;
Expand All @@ -11,8 +12,9 @@

namespace IPTables.Net.Tests
{
[NonParallelizable]
[TestFixture(4)]
[TestFixture(6)]
//[TestFixture(6)]
class IptablesLibraryTest
{
private int _ipVersion;
Expand All @@ -31,7 +33,8 @@ public IptablesLibraryTest(int ipVersion)
_ipVersion = ipVersion;
}

private String GetBinary()

private String GetBinaryName()
{
if (_ipVersion == 4)
{
Expand All @@ -40,6 +43,14 @@ private String GetBinary()
return "ip6tables";
}

private String GetBinary()
{
var name = GetBinaryName();
if (Path.Exists("/sbin/" + name)) return "/sbin/" + name;
if (Path.Exists("/usr/sbin/" + name)) return "/usr/sbin/" + name;
return name;
}

[OneTimeSetUp]
public void TestStartup()
{
Expand All @@ -49,12 +60,13 @@ public void TestStartup()
{
Assert.Ignore();
}
Process.Start("/sbin/" + GetBinary(), "-N test2").WaitForExit();
Process.Start("/sbin/" + GetBinary(), "-N test").WaitForExit();
Process.Start("/sbin/" + GetBinary(), "-A test -j ACCEPT").WaitForExit();

Process.Start("/sbin/" + GetBinary(), "-N test3").WaitForExit();
Process.Start("/sbin/" + GetBinary(), "-A test3 -p tcp -m tcp --dport 80 -j ACCEPT").WaitForExit();
var binary = GetBinary();
Process.Start(binary, "-N test2").WaitForExit();
Process.Start(binary, "-N test").WaitForExit();
Process.Start(binary, "-A test -j ACCEPT").WaitForExit();
Process.Start(binary, "-N test3").WaitForExit();
Process.Start(binary, "-A test3 -p tcp -m tcp --dport 80 -j ACCEPT").WaitForExit();
}
}

Expand All @@ -67,14 +79,15 @@ public void TestDestroy()
{
Assert.Ignore();
}
Process.Start("/sbin/"+GetBinary(), "-F test").WaitForExit();
Process.Start("/sbin/"+GetBinary(), "-X test").WaitForExit();
Process.Start("/sbin/"+GetBinary(), "-F test2").WaitForExit();
Process.Start("/sbin/"+GetBinary(), "-X test2").WaitForExit();
Process.Start("/sbin/"+GetBinary(), "-F test3").WaitForExit();
Process.Start("/sbin/"+GetBinary(), "-X test3").WaitForExit();
}
}

var binary = GetBinary();
Process.Start(binary, "-F test").WaitForExit();
Process.Start(binary, "-X test").WaitForExit();
Process.Start(binary, "-F test2").WaitForExit();
//Process.Start(binary, "-X test2").WaitForExit();
Process.Start(binary, "-F test3").WaitForExit();
Process.Start(binary, "-X test3").WaitForExit();
} }

[Test]
public void TestRuleOutput()
Expand Down Expand Up @@ -122,11 +135,16 @@ public void TestRuleAdd()
}
var rule = IpTablesRule.Parse("-A test2 -p 80 -j ACCEPT", system, chain);
client.StartTransaction();
client.AddRule(rule);
client.EndTransactionCommit();

try
{
client.AddRule(rule);
}
finally
{
client.EndTransactionCommit();
}

var proc = Process.Start(new ProcessStartInfo("/sbin/" + GetBinary(), "-L test2"){RedirectStandardOutput = true, UseShellExecute = false});
var proc = Process.Start(new ProcessStartInfo(GetBinary(), "-L test2"){RedirectStandardOutput = true, UseShellExecute = false});
proc.WaitForExit();
String listOutput = proc.StandardOutput.ReadToEnd();
Assert.IsTrue(listOutput.Contains("anywhere"), "must have created rule");
Expand Down
56 changes: 40 additions & 16 deletions IPTables.Net.Tests/IptcInterfaceTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
Expand All @@ -9,8 +10,9 @@

namespace IPTables.Net.Tests
{
[NonParallelizable]
[TestFixture(4)]
[TestFixture(6)]
//[TestFixture(6)]
class IptcInterfaceTest
{
private int _ipVersion;
Expand All @@ -29,7 +31,7 @@ public IptcInterfaceTest(int ipVersion)
_ipVersion = ipVersion;
}

private String GetBinary()
private String GetBinaryName()
{
if (_ipVersion == 4)
{
Expand All @@ -38,6 +40,14 @@ private String GetBinary()
return "ip6tables";
}

private String GetBinary()
{
var name = GetBinaryName();
if(Path.Exists("/sbin/"+name)) return "/sbin/"+name;
if(Path.Exists("/usr/sbin/"+name)) return "/usr/sbin/"+name;
return name;
}

[OneTimeSetUp]
public void TestStartup()
{
Expand All @@ -47,17 +57,29 @@ public void TestStartup()
{
Assert.Ignore();
}
Process.Start("/sbin/" + GetBinary(), "-F test").WaitForExit();

Process.Start("/sbin/" + GetBinary(), "-N test2").WaitForExit();
Process.Start("/sbin/" + GetBinary(), "-N test").WaitForExit();
Process.Start("/sbin/" + GetBinary(), "-A test -j ACCEPT").WaitForExit();
Console.WriteLine("Test Startup");

Process.Start("/sbin/" + GetBinary(), "-N test3").WaitForExit();
Process.Start("/sbin/" + GetBinary(), "-A test3 -p tcp -m tcp --dport 80 -j ACCEPT").WaitForExit();
var binary = GetBinary();
Execute(binary, "-F test");

Execute(binary, "-N test2");
Process.Start(binary, "-N test").WaitForExit();
Process.Start(binary, "-A test -j ACCEPT").WaitForExit();

Process.Start(binary, "-N test3").WaitForExit();
Process.Start(binary, "-A test3 -p tcp -m tcp --dport 80 -j ACCEPT").WaitForExit();
}
}

private void Execute(string binary, string args)
{
var process = Process.Start(new ProcessStartInfo(binary, args){RedirectStandardError = true, RedirectStandardOutput = true});
Console.WriteLine(process.StandardOutput.ReadToEnd());
Console.Error.WriteLine(process.StandardError.ReadToEnd());
process.WaitForExit();
}

[OneTimeTearDown]
public void TestDestroy()
{
Expand All @@ -67,13 +89,15 @@ public void TestDestroy()
{
Assert.Ignore();
}
Process.Start("/sbin/" + GetBinary(), "-D test -j ACCEPT").WaitForExit();
Process.Start("/sbin/" + GetBinary(), "-F test").WaitForExit();
Process.Start("/sbin/"+GetBinary(), "-X test").WaitForExit();
Process.Start("/sbin/"+GetBinary(), "-F test2").WaitForExit();
Process.Start("/sbin/"+GetBinary(), "-X test2").WaitForExit();
Process.Start("/sbin/"+GetBinary(), "-F test3").WaitForExit();
Process.Start("/sbin/"+GetBinary(), "-X test3").WaitForExit();
Console.WriteLine("Test Done");
var binary = GetBinary();
Process.Start(binary, "-D test -j ACCEPT").WaitForExit();
Process.Start(binary, "-F test").WaitForExit();
Process.Start(binary, "-X test").WaitForExit();
Process.Start(binary, "-F test2").WaitForExit();
//Process.Start(binary, "-X test2").WaitForExit();
Process.Start(binary, "-F test3").WaitForExit();
Process.Start(binary, " - X test3").WaitForExit();
}
}

Expand Down Expand Up @@ -120,7 +144,7 @@ public void TestRuleInput()
using (IptcInterface iptc = new IptcInterface("filter", _ipVersion))
{

var status = iptc.ExecuteCommand(_ipVersion == 4 ? "iptables -A test2 -d 1.1.1.1 -p tcp -m tcp --dport 80 -j ACCEPT" : "iptables -A test2 -d ::1 -p tcp -m tcp --dport 80 -j ACCEPT");
var status = iptc.ExecuteCommand(_ipVersion == 4 ? "iptables -A test2 -d 1.1.1.1 -p tcp -m tcp --dport 80 -j ACCEPT" : "ip6tables -A test2 -d ::1 -p tcp -m tcp --dport 80 -j ACCEPT");
Assert.AreEqual(1, status, "Expected OK return value");

var rules = iptc.GetRules("test2");
Expand Down
2 changes: 1 addition & 1 deletion IPTables.Net/IPTables.Net.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<PackageReference Include="IPNetwork2" Version="2.6.618">
<Aliases>IPNetwork2</Aliases>
</PackageReference>
<PackageReference Include="SystemInteract" Version="1.1.4-cibuild0005" />
<PackageReference Include="SystemInteract" Version="1.1.4-cibuild0006" />
</ItemGroup>

</Project>
4 changes: 2 additions & 2 deletions IPTables.Net/IpUtils/Utils/IpController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ protected string[] Command(string command, params string[] args)
{
string output, error;
ProcessHelper.ReadToEnd(process, out output, out error);
Console.WriteLine(cmd);
/*Console.WriteLine(cmd);
Console.WriteLine("output" + output);
Console.WriteLine("error" + error);
Console.WriteLine("error" + error);*/
return new string[] {output.Trim(), error.Trim()};
}
}
Expand Down

0 comments on commit d88956f

Please sign in to comment.