Skip to content

Commit

Permalink
...
Browse files Browse the repository at this point in the history
  • Loading branch information
splitice committed Jan 1, 2024
1 parent 38601e6 commit db3d889
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
8 changes: 7 additions & 1 deletion IPTables.Net.TestFramework/MockIptablesSystemFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ 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)
{
Expand All @@ -31,7 +37,7 @@ public ISystemProcess StartProcess(string command, string arguments)
error = MockOutputs[exe][1];
}
}
else
else if(_strict)
{
throw new Exception("Mock output \"" + command + "\" " + arguments + " not found");
}
Expand Down
16 changes: 8 additions & 8 deletions IPTables.Net.Tests/IpUtilsRuleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,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 @@ -32,15 +32,15 @@ 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 @@ -56,7 +56,7 @@ 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 @@ -72,7 +72,7 @@ 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(""))) });
Expand All @@ -95,7 +95,7 @@ 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 @@ -111,7 +111,7 @@ 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();
Expand All @@ -130,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

0 comments on commit db3d889

Please sign in to comment.