Skip to content

Commit

Permalink
Compare method and EqualityComparers for rule
Browse files Browse the repository at this point in the history
  • Loading branch information
splitice committed May 10, 2020
1 parent b75bc64 commit 18dc969
Show file tree
Hide file tree
Showing 20 changed files with 135 additions and 98 deletions.
6 changes: 3 additions & 3 deletions IPTables.Net.Tests/IpTablesComparisonTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void TestComparisonMultiport()
IpTablesRule r1 = IpTablesRule.Parse(rule, null, chains, 4);
IpTablesRule r2 = IpTablesRule.Parse(rule, null, chains, 4);

Assert.IsTrue(r1.Equals(r2));
Assert.IsTrue(r1.Compare(r2));
}

[Test]
Expand All @@ -42,7 +42,7 @@ public void TestLimitComparison()

IpTablesRule irule2 = IpTablesRule.Parse(rule, null, chains, 4);

Assert.AreEqual(irule2, irule);
Assert.IsTrue(irule2.Compare(irule));
}

[Test]
Expand All @@ -57,7 +57,7 @@ public void TestDifficultCharacters()

IpTablesRule irule2 = IpTablesRule.Parse(rule, null, chains, 4);

Assert.AreEqual(irule2, irule);
Assert.IsTrue(irule2.Compare(irule));
}
}
}
41 changes: 40 additions & 1 deletion IPTables.Net.Tests/IpTablesRestoreSyncTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ internal void TestApply(IpTablesRuleSet rulesOrig, IpTablesRuleSet rulesSynced,
catch (Exception ex)
{
Console.WriteLine("Sync:");
DumpRuleset(rulesOrig);
DumpRuleset(rulesSynced);
Console.WriteLine("New:");
DumpRuleset(rulesNew);
throw;
Expand Down Expand Up @@ -540,6 +540,45 @@ public void TestUpdateMiddle()
}
}

[Test]
public void TestUpdateUnlabelled()
{
var mock = new MockIptablesSystemFactory();
var system = new IpTablesSystem(mock, new MockIpTablesRestoreAdapter());

IpTablesRuleSet rulesOriginal = new IpTablesRuleSet(4, new List<String>()
{
"-A INPUT -p tcp -j DROP -m connlimit --connlimit-above 10",
"-A INPUT -p udp -j DROP -m connlimit --connlimit-above 2",
"-A INPUT -p udp -j DROP -m connlimit --connlimit-above 2",
"-A INPUT -p udp -j DROP -m connlimit --connlimit-above 2"
}, system);
IpTablesRuleSet rulesNew = new IpTablesRuleSet(4, new List<String>()
{
"-A INPUT -p tcp -j DROP -m connlimit --connlimit-above 10",
"-A INPUT -p udp -j DROP -m connlimit --connlimit-above 28",
"-A INPUT -p udp -j DROP -m connlimit --connlimit-above 11",
"-A INPUT -p udp -j DROP -m connlimit --connlimit-above 2"
}, system);

List<String> expectedCommands = new List<String>
{
"*filter", rulesNew.Chains.First().Rules[1].GetActionCommand("-R"), rulesNew.Chains.First().Rules[2].GetActionCommand("-R"), "COMMIT"
};


using (var client = system.GetTableAdapter(4))
{
var sync = new DefaultNetfilterSync<IpTablesRule>();
var rulesSynced = rulesOriginal.DeepClone();
mock.TestSync(client, rulesSynced, rulesNew, sync);
var output = (client as IMockIpTablesRestoreGetOutput).GetOutput();
CollectionAssert.AreEqual(expectedCommands, output);

TestApply(rulesOriginal, rulesSynced, rulesNew, expectedCommands);
}
}


[Test]
public void TestUpdateMiddleTwo()
Expand Down
2 changes: 1 addition & 1 deletion IPTables.Net.Tests/SingleCommentRuleParseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void TestDropFragmentedTcpDnsWithCommentEquality()
IpTablesRule irule1 = IpTablesRule.Parse(rule, null, chains, 4);
IpTablesRule irule2 = IpTablesRule.Parse(rule, null, chains, 4);

Assert.AreEqual(irule1, irule2);
Assert.IsTrue(irule2.Compare(irule1));
}

[Test]
Expand Down
2 changes: 1 addition & 1 deletion IPTables.Net.Tests/SingleConnlimitRuleParseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void TestDropConnectionLimitEquality()
IpTablesRule irule1 = IpTablesRule.Parse(rule, null, chains, 4);
IpTablesRule irule2 = IpTablesRule.Parse(rule, null, chains, 4);

Assert.AreEqual(irule1, irule2);
Assert.IsTrue(irule2.Compare(irule1));
}
}
}
2 changes: 1 addition & 1 deletion IPTables.Net.Tests/SingleConntrackRuleParseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public void TestParse()
IpTablesRule irule2 = IpTablesRule.Parse(rule2, null, chains, 4);

irule2.Equals(irule1);
Assert.AreEqual(irule1, irule2);
Assert.IsTrue(irule2.Compare(irule1));
}
}
}
10 changes: 5 additions & 5 deletions IPTables.Net.Tests/SingleCoreRuleParseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void TestCoreDropingDestinationEquality()
IpTablesRule irule1 = IpTablesRule.Parse(rule, null, chains, 4);
IpTablesRule irule2 = IpTablesRule.Parse(rule, null, chains, 4);

Assert.AreEqual(irule1, irule2);
Assert.IsTrue(irule2.Compare(irule1));
}

[Test]
Expand All @@ -83,7 +83,7 @@ public void TestCoreDropingInterfaceEquality()
IpTablesRule irule1 = IpTablesRule.Parse(rule, null, chains, 4);
IpTablesRule irule2 = IpTablesRule.Parse(rule, null, chains, 4);

Assert.AreEqual(irule1, irule2);
Assert.IsTrue(irule2.Compare(irule1));
}

[Test]
Expand All @@ -95,7 +95,7 @@ public void TestCoreDropingSourceEquality()
IpTablesRule irule1 = IpTablesRule.Parse(rule, null, chains, 4);
IpTablesRule irule2 = IpTablesRule.Parse(rule, null, chains, 4);

Assert.AreEqual(irule1, irule2);
Assert.IsTrue(irule2.Compare(irule1));
}

[Test]
Expand All @@ -107,7 +107,7 @@ public void TestCoreDropingUdpEquality()
IpTablesRule irule1 = IpTablesRule.Parse(rule, null, chains, 4);
IpTablesRule irule2 = IpTablesRule.Parse(rule, null, chains, 4);

Assert.AreEqual(irule1, irule2);
Assert.IsTrue(irule2.Compare(irule1));
}

[Test]
Expand All @@ -119,7 +119,7 @@ public void TestCoreFragmentingEquality()
IpTablesRule irule1 = IpTablesRule.Parse(rule, null, chains, 4);
IpTablesRule irule2 = IpTablesRule.Parse(rule, null, chains, 4);

Assert.AreEqual(irule1, irule2);
Assert.IsTrue(irule2.Compare(irule1));
}
}
}
2 changes: 1 addition & 1 deletion IPTables.Net.Tests/SingleDnatParseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public void DnatTest1()
IpTablesRule irule = IpTablesRule.Parse(rule, null, chains, 4);

Assert.AreEqual(rule, irule.GetActionCommand());
Assert.AreEqual(irule, IpTablesRule.Parse(rule, null, chains, 4));
Assert.IsTrue(irule.Compare(IpTablesRule.Parse(rule, null, chains, 4)));
}

}
Expand Down
2 changes: 1 addition & 1 deletion IPTables.Net.Tests/SingleDnatRuleParseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void TestDnatRangeSourceAndEquality()
IpTablesRule irule1 = IpTablesRule.Parse(rule, null, chains, 4);
IpTablesRule irule2 = IpTablesRule.Parse(rule, null, chains, 4);

Assert.AreEqual(irule1, irule2);
Assert.IsTrue(irule2.Compare(irule1));
Assert.AreEqual(rule, irule1.GetActionCommand());
Assert.AreEqual(rule, irule2.GetActionCommand());
}
Expand Down
4 changes: 2 additions & 2 deletions IPTables.Net.Tests/SingleHashlimitRuleParseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public void TestCompare1()
String rule = "-A ABC -m hashlimit --hashlimit-name aaaaaaaaaaaaaaaaaaaaaa --hashlimit-above 125/second --hashlimit-burst 500 --hashlimit-mode dstip,dstport --hashlimit-srcmask 32 --hashlimit-dstmask 32 --hashlimit-htable-size 65000 --hashlimit-htable-max 30000 --hashlimit-htable-expire 6 --hashlimit-htable-gcinterval 600 -j AVS";
IpTablesChainSet chains = new IpTablesChainSet(4);

Assert.AreEqual(IpTablesRule.Parse(rule, null, chains, 4), IpTablesRule.Parse(rule, null, chains, 4));
Assert.IsTrue(IpTablesRule.Parse(rule, null, chains, 4).Compare(IpTablesRule.Parse(rule, null, chains, 4)));
}

[Test]
Expand All @@ -23,7 +23,7 @@ public void TestCompare2()
String rule2 = "-A AAAA -t raw -m hashlimit --hashlimit-above 111/sec --hashlimit-burst 500 --hashlimit-mode dstip,dstport --hashlimit-name synflood_spoofe --hashlimit-htable-size 65000 --hashlimit-htable-max 30000 --hashlimit-htable-gcinterval 600 --hashlimit-htable-expire 6 -g AA";
IpTablesChainSet chains = new IpTablesChainSet(4);

Assert.AreEqual(IpTablesRule.Parse(rule, null, chains, 4), IpTablesRule.Parse(rule2, null, chains));
Assert.IsTrue(IpTablesRule.Parse(rule, null, chains, 4).Compare(IpTablesRule.Parse(rule2, null, chains)));
}
}
}
4 changes: 2 additions & 2 deletions IPTables.Net.Tests/SingleMarkRuleParseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public void MatchMarkDec()
IpTablesRule irule = IpTablesRule.Parse(rule, null, chains, 4);

Assert.AreEqual(ruleExpect, irule.GetActionCommand());
Assert.AreEqual(IpTablesRule.Parse(ruleExpect, null, chains, 4), irule);
Assert.IsTrue(IpTablesRule.Parse(ruleExpect, null, chains, 4).Compare(irule));
}

[Test]
Expand All @@ -30,7 +30,7 @@ public void MatchMarkHex()
IpTablesRule irule = IpTablesRule.Parse(rule, null, chains, 4);

Assert.AreEqual(ruleExpect, irule.GetActionCommand());
Assert.AreEqual(IpTablesRule.Parse(ruleExpect, null, chains, 4), irule);
Assert.IsTrue(IpTablesRule.Parse(ruleExpect, null, chains, 4).Compare(irule));
}


Expand Down
4 changes: 2 additions & 2 deletions IPTables.Net.Tests/SingleNfacctRuleParseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void TestQuote()
IpTablesRule irule = IpTablesRule.Parse(rule, null, chains, 4);

Assert.AreEqual(rule2, irule.GetActionCommand());
Assert.AreEqual(IpTablesRule.Parse(rule2, null, chains, 4), irule);
Assert.IsTrue(IpTablesRule.Parse(rule2, null, chains, 4).Compare(irule));
}

[Test]
Expand All @@ -41,7 +41,7 @@ public void TestDoubleSpace()
IpTablesRule irule = IpTablesRule.Parse(rule, null, chains, 4);

Assert.AreEqual(rule2, irule.GetActionCommand());
Assert.AreEqual(IpTablesRule.Parse(rule2, null, chains, 4), irule);
Assert.IsTrue(IpTablesRule.Parse(rule2, null, chains, 4).Compare(irule));
}
}
}
8 changes: 4 additions & 4 deletions IPTables.Net.Tests/SinglePolyfillTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void TestPolyfillArgumentsComparison1()
String rule = "-A INPUT -m unknown --unknown --unknown-2 1111 -p tcp -d 1.1.1.1 -m tcp --dport 80";
IpTablesChainSet chains = new IpTablesChainSet(4);

Assert.AreEqual(IpTablesRule.Parse(rule, null, chains, 4), IpTablesRule.Parse(rule, null, chains, 4));
Assert.IsTrue(IpTablesRule.Parse(rule, null, chains, 4).Compare(IpTablesRule.Parse(rule, null, chains, 4)));
}

[Test]
Expand All @@ -57,7 +57,7 @@ public void TestPolyfillArgumentsComparison2()
"-A INPUT -m unknown --unknown --unknown-2 1111 -m unknown2 --unknown2 -p tcp -d 1.1.1.1 -m tcp --dport 80";
IpTablesChainSet chains = new IpTablesChainSet(4);

Assert.AreEqual(IpTablesRule.Parse(rule, null, chains, 4), IpTablesRule.Parse(rule, null, chains, 4));
Assert.IsTrue(IpTablesRule.Parse(rule, null, chains, 4).Compare(IpTablesRule.Parse(rule, null, chains, 4)));
}

[Test]
Expand All @@ -69,7 +69,7 @@ public void TestPolyfillArgumentsComparison3()
"-A INPUT -m unknown2 --unknown2 -m unknown --unknown --unknown-2 1111 -p tcp -d 1.1.1.1 -m tcp --dport 80";
IpTablesChainSet chains = new IpTablesChainSet(4);

Assert.AreEqual(IpTablesRule.Parse(rule, null, chains, 4), IpTablesRule.Parse(rule2, null, chains));
Assert.IsTrue(IpTablesRule.Parse(rule, null, chains, 4).Compare(IpTablesRule.Parse(rule2, null, chains)));
}

[Test]
Expand All @@ -81,7 +81,7 @@ public void TestPolyfillArgumentsComparison4()
"-A INPUT -m unknown2 --unknown2 -m unknown --unknown --unknown-2 \'this has spaces & a symbol\' -p tcp -d 1.1.1.1 -m tcp --dport 80";
IpTablesChainSet chains = new IpTablesChainSet(4);

Assert.AreEqual(IpTablesRule.Parse(rule, null, chains, 4), IpTablesRule.Parse(rule2, null, chains));
Assert.IsTrue(IpTablesRule.Parse(rule, null, chains, 4).Compare(IpTablesRule.Parse(rule2, null, chains)));
}

public void TestPolyfillArgumentsComparison5()
Expand Down
2 changes: 1 addition & 1 deletion IPTables.Net.Tests/SingleRecentRuleParseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void TestCompare1()
String rule = "-A ATTK_CHECK -m recent --rcheck --name BANNED --seconds 180 --reap --rttl -j ATTACKED";
IpTablesChainSet chains = new IpTablesChainSet(4);

Assert.AreEqual(IpTablesRule.Parse(rule, null, chains, 4), IpTablesRule.Parse(rule, null, chains, 4));
Assert.IsTrue(IpTablesRule.Parse(rule, null, chains, 4).Compare(IpTablesRule.Parse(rule, null, chains, 4)));
}
}
}
2 changes: 1 addition & 1 deletion IPTables.Net.Tests/SingleSnatRuleParseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void TestSnatRangeSourceAndEquality()
IpTablesRule irule1 = IpTablesRule.Parse(rule, null, chains, 4);
IpTablesRule irule2 = IpTablesRule.Parse(rule, null, chains, 4);

Assert.AreEqual(irule1, irule2);
Assert.IsTrue(irule2.Compare(irule1));
Assert.AreEqual(rule, irule1.GetActionCommand());
Assert.AreEqual(rule, irule2.GetActionCommand());
}
Expand Down
4 changes: 2 additions & 2 deletions IPTables.Net.Tests/SingleTcpRuleParseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void TestDropFragmentedTcpDnsEquality()
IpTablesRule irule1 = IpTablesRule.Parse(rule, null, chains, 4);
IpTablesRule irule2 = IpTablesRule.Parse(rule, null, chains, 4);

Assert.AreEqual(irule1, irule2);
Assert.IsTrue(irule2.Compare(irule1));
}

[Test]
Expand All @@ -39,7 +39,7 @@ public void TestCoreSportEquality()
IpTablesRule irule1 = IpTablesRule.Parse(rule, null, chains, 4);
IpTablesRule irule2 = IpTablesRule.Parse(rule, null, chains, 4);

Assert.AreEqual(irule1, irule2);
Assert.IsTrue(irule2.Compare(irule1));
}

[Test]
Expand Down
5 changes: 3 additions & 2 deletions IPTables.Net/Iptables/IpTablesChain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,10 @@ public override int GetHashCode()
}
}

public bool CompareRules(IpTablesChain ipTablesChain)
public bool CompareRules(IpTablesChain ipTablesChain, IEqualityComparer<IpTablesRule> eqc = null)
{
return Enumerable.SequenceEqual(_rules, ipTablesChain._rules);
eqc = eqc ?? new IpTablesRule.ValueComparison();
return Enumerable.SequenceEqual(_rules, ipTablesChain._rules, eqc);
}
}
}
Loading

0 comments on commit 18dc969

Please sign in to comment.