Skip to content

Commit

Permalink
improve testing
Browse files Browse the repository at this point in the history
  • Loading branch information
splitice committed Jan 1, 2024
1 parent 4af92c0 commit 727dd07
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 17 deletions.
18 changes: 14 additions & 4 deletions IPTables.Net.TestFramework/MockIptablesSystemFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,33 @@ public class MockIptablesSystemFactory : ISystemFactory
{
public List<KeyValuePair<String, String>> ExecutionLog = new List<KeyValuePair<string, string>>();
public Dictionary<KeyValuePair<String, String>,StreamReader[]> MockOutputs = new Dictionary<KeyValuePair<string, string>, StreamReader[]>();
private readonly bool _strict;

public MockIptablesSystemFactory(bool strict = false)
{
_strict = strict;
}

public ISystemProcess StartProcess(string command, string arguments)
{
var exe = new KeyValuePair<string, string>(command, arguments);
ExecutionLog.Add(exe);
StreamReader output = null, error = null;
if (MockOutputs.ContainsKey(exe))
if (MockOutputs.TryGetValue(exe, out var mo))
{
if(MockOutputs[exe].Length >= 1)
if(mo.Length >= 1)
{
output = MockOutputs[exe][0];
output = mo[0];
}
if (MockOutputs[exe].Length >= 2)
{
error = MockOutputs[exe][1];
error = mo[1];
}
}
else if(_strict)
{
throw new Exception("Mock output \"" + command + "\" " + arguments + " not found");
}
return new MockIptablesSystemProcess(output,error);
}

Expand Down
2 changes: 1 addition & 1 deletion IPTables.Net.TestFramework/MockIptablesSystemProcess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace IPTables.Net.TestFramework
{
public class MockIptablesSystemProcess : ISystemProcess
{
private ProcessStartInfo _startInfo = new ProcessStartInfo();
private ProcessStartInfo _startInfo = new ProcessStartInfo { UseShellExecute = false };
private Thread _outputReader;
private Thread _errorReader;

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.3" />
<PackageReference Include="SystemInteract.Local" Version="1.1.4-cibuild0005" />
</ItemGroup>

<ItemGroup>
Expand Down
25 changes: 17 additions & 8 deletions IPTables.Net.Tests/IpUtilsRuleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using IPTables.Net.IpUtils;
using IPTables.Net.IpUtils.Utils;
using IPTables.Net.TestFramework;
using Microsoft.VisualStudio.TestPlatform.Utilities;
using NUnit.Framework;

namespace IPTables.Net.Tests
Expand All @@ -18,7 +19,7 @@ class IpUtilsRuleTests
[Test]
public void TestParseRule()
{
var systemFactory = new MockIptablesSystemFactory();
var systemFactory = new MockIptablesSystemFactory(true);
var ipUtils = new IpRuleController(systemFactory);
var one = ipUtils.ParseObjectInternal("default via 10.17.199.1 dev s4 table 200", "to");
var two = ipUtils.ParseObjectInternal("default via 10.17.199.1 dev s4 table 200", "to");
Expand All @@ -31,15 +32,16 @@ public void TestParseRule()
[Test]
public void TestParsePref()
{
var systemFactory = new MockIptablesSystemFactory();
var systemFactory = new MockIptablesSystemFactory(true);
var ipUtils = new IpRuleController(systemFactory);
var one = ipUtils.ParseObject("0: from all fwmark 0x1000200/0x1ffff00 lookup 15002");
}

[Test]
public void TestAddRule()
{
var systemFactory = new MockIptablesSystemFactory();
var systemFactory = new MockIptablesSystemFactory(true);
systemFactory.MockOutputs.Add(new KeyValuePair<string, string>("ip", "rule add from 1.1.1.1 lookup 100"), new StreamReader[] { new StreamReader(new MemoryStream(Encoding.ASCII.GetBytes(""))) });
var ipUtils = new IpRuleController(systemFactory);
ipUtils.Add("from","1.1.1.1","lookup","100");

Expand All @@ -54,7 +56,8 @@ public void TestAddRule()
[Test]
public void TestAddRule2()
{
var systemFactory = new MockIptablesSystemFactory();
var systemFactory = new MockIptablesSystemFactory(true);
systemFactory.MockOutputs.Add(new KeyValuePair<string, string>("ip", "rule add not from 1.1.1.1 lookup 100"), new StreamReader[] { new StreamReader(new MemoryStream(Encoding.ASCII.GetBytes(""))) });
var ipUtils = new IpRuleController(systemFactory);
ipUtils.Add("not", "from", "1.1.1.1", "lookup", "100");

Expand All @@ -69,7 +72,11 @@ public void TestAddRule2()
[Test]
public void TestAddObjRule()
{
var systemFactory = new MockIptablesSystemFactory();
var systemFactory = new MockIptablesSystemFactory(true);


systemFactory.MockOutputs.Add(new KeyValuePair<string, string>("ip", "rule add not from 1.1.1.1 lookup 100"), new StreamReader[] { new StreamReader(new MemoryStream(Encoding.ASCII.GetBytes(""))) });

var ipUtils = new IpRuleController(systemFactory);
var obj = new IpObject();
obj.Pairs.Add("from", "1.1.1.1");
Expand All @@ -88,7 +95,8 @@ public void TestAddObjRule()
[Test]
public void TestDeleteRule()
{
var systemFactory = new MockIptablesSystemFactory();
var systemFactory = new MockIptablesSystemFactory(true);
systemFactory.MockOutputs.Add(new KeyValuePair<string, string>("ip", "rule delete from 1.1.1.1 lookup 100"), new StreamReader[] { new StreamReader(new MemoryStream(Encoding.ASCII.GetBytes(""))) });
var ipUtils = new IpRuleController(systemFactory);
ipUtils.Delete("from", "1.1.1.1", "lookup", "100");

Expand All @@ -103,7 +111,8 @@ public void TestDeleteRule()
[Test]
public void TestDeleteRuleId()
{
var systemFactory = new MockIptablesSystemFactory();
var systemFactory = new MockIptablesSystemFactory(true);
systemFactory.MockOutputs.Add(new KeyValuePair<string, string>("ip", "rule delete pref 100 from 1.1.1.1"), new StreamReader[] { new StreamReader(new MemoryStream(Encoding.ASCII.GetBytes(""))) });
var ipUtils = new IpRuleController(systemFactory);
IpObject ipObject = new IpObject();
ipObject.Pairs.Add("pref","100");
Expand All @@ -121,7 +130,7 @@ public void TestDeleteRuleId()
[Test]
public void TestGetRules()
{
var systemFactory = new MockIptablesSystemFactory();
var systemFactory = new MockIptablesSystemFactory(true);
var output = "32766: from all lookup main\n32767: from all lookup default";
systemFactory.MockOutputs.Add(new KeyValuePair<string, string>("ip","rule show"), new StreamReader[]{new StreamReader(new MemoryStream(Encoding.ASCII.GetBytes(output)))});
var ipUtils = new IpRuleController(systemFactory);
Expand Down
4 changes: 2 additions & 2 deletions IPTables.Net/IPTables.Net.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

<ItemGroup>
<PackageReference Include="Dynamitey" Version="2.0.10.189" />
<PackageReference Include="IPNetwork2" Version="2.6.609">
<PackageReference Include="IPNetwork2" Version="2.6.618">
<Aliases>IPNetwork2</Aliases>
</PackageReference>
<PackageReference Include="SystemInteract" Version="1.1.4-cibuild0001" />
<PackageReference Include="SystemInteract" Version="1.1.4-cibuild0005" />
</ItemGroup>

</Project>
5 changes: 4 additions & 1 deletion IPTables.Net/IpUtils/Utils/IpController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,14 @@ protected virtual IpObject ParseObject(string str, string firstKey, char[] first

protected string[] Command(string command, params string[] args)
{
var cmd = string.Format("{0} {1} {2}", _module, command, string.Join(" ", args));
var cmd = $"{_module} {command} {string.Join(" ", args)}";
using (var process = _system.StartProcess("ip", cmd.TrimEnd()))
{
string output, error;
ProcessHelper.ReadToEnd(process, out output, out error);
Console.WriteLine(cmd);
Console.WriteLine("output" + output);
Console.WriteLine("error" + error);
return new string[] {output.Trim(), error.Trim()};
}
}
Expand Down

0 comments on commit 727dd07

Please sign in to comment.