Avoiding hash collisions of a match with its reverse
[controller.git] / opendaylight / sal / api / src / test / java / org / opendaylight / controller / sal / match / MatchTest.java
index f3c7a95b0ebcdfb44a5de81e8a5509572827d2f6..b88ae034d9aa713c96f4ce3ef633f977d5a223b3 100644 (file)
@@ -16,9 +16,6 @@ import org.junit.Assert;
 import org.junit.Test;
 import org.opendaylight.controller.sal.core.Node;
 import org.opendaylight.controller.sal.core.NodeConnector;
-import org.opendaylight.controller.sal.match.Match;
-import org.opendaylight.controller.sal.match.MatchField;
-import org.opendaylight.controller.sal.match.MatchType;
 import org.opendaylight.controller.sal.utils.EtherTypes;
 import org.opendaylight.controller.sal.utils.IPProtocols;
 import org.opendaylight.controller.sal.utils.NodeConnectorCreator;
@@ -27,7 +24,7 @@ import org.opendaylight.controller.sal.utils.NodeCreator;
 public class MatchTest {
     @Test
     public void testMatchCreation() {
-        Node node = NodeCreator.createOFNode(7l);
+        Node node = NodeCreator.createOFNode(7L);
         NodeConnector port = NodeConnectorCreator.createOFNodeConnector((short) 6, node);
         MatchField field = new MatchField(MatchType.IN_PORT, port);
 
@@ -63,7 +60,7 @@ public class MatchTest {
     public void testMatchSetGet() {
         Match x = new Match();
         short val = 2346;
-        NodeConnector inPort = NodeConnectorCreator.createOFNodeConnector(val, NodeCreator.createOFNode(1l));
+        NodeConnector inPort = NodeConnectorCreator.createOFNodeConnector(val, NodeCreator.createOFNode(1L));
         x.setField(MatchType.IN_PORT, inPort);
         Assert.assertTrue(((NodeConnector) x.getField(MatchType.IN_PORT).getValue()).equals(inPort));
         Assert.assertTrue((Short) ((NodeConnector) x.getField(MatchType.IN_PORT).getValue()).getID() == val);
@@ -178,7 +175,7 @@ public class MatchTest {
     @Test
     public void testMatchMask() {
         Match x = new Match();
-        NodeConnector inPort = NodeConnectorCreator.createOFNodeConnector((short) 6, NodeCreator.createOFNode(3l));
+        NodeConnector inPort = NodeConnectorCreator.createOFNodeConnector((short) 6, NodeCreator.createOFNode(3L));
         x.setField(MatchType.IN_PORT, inPort);
         x.setField(MatchType.DL_VLAN, (short) 28, (short) 0xfff);
         Assert.assertFalse(x.getMatches() == 0);
@@ -189,7 +186,7 @@ public class MatchTest {
     public void testMatchBitMask() {
         byte mac[] = { (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 22, (byte) 12 };
         byte mask[] = { (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0 };
-        NodeConnector inPort = NodeConnectorCreator.createOFNodeConnector((short) 4095, NodeCreator.createOFNode(7l));
+        NodeConnector inPort = NodeConnectorCreator.createOFNodeConnector((short) 4095, NodeCreator.createOFNode(7L));
 
         MatchField x = new MatchField(MatchType.IN_PORT, inPort);
         Assert.assertTrue((x.getMask()) == null);
@@ -205,7 +202,7 @@ public class MatchTest {
     @Test
     public void testNullMask() {
         byte mac[] = { (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 22, (byte) 12 };
-        NodeConnector inPort = NodeConnectorCreator.createOFNodeConnector((short) 2000, NodeCreator.createOFNode(7l));
+        NodeConnector inPort = NodeConnectorCreator.createOFNodeConnector((short) 2000, NodeCreator.createOFNode(7L));
 
         MatchField x = new MatchField(MatchType.IN_PORT, inPort);
         Assert.assertTrue(x.getBitMask() == 0);
@@ -222,7 +219,7 @@ public class MatchTest {
 
     @Test
     public void testEquality() throws Exception {
-        Node node = NodeCreator.createOFNode(7l);
+        Node node = NodeCreator.createOFNode(7L);
         NodeConnector port = NodeConnectorCreator.createOFNodeConnector((short) 24, node);
         NodeConnector port2 = NodeConnectorCreator.createOFNodeConnector((short) 24, node);
         byte srcMac[] = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9a, (byte) 0xbc };
@@ -340,6 +337,21 @@ public class MatchTest {
         Assert.assertTrue(match1.equals(match2));
     }
 
+    @Test
+    public void testHashCodeWithReverseMatch() throws Exception {
+        InetAddress srcIP1 = InetAddress.getByName("1.1.1.1");
+        InetAddress ipMask1 = InetAddress.getByName("255.255.255.255");
+        InetAddress srcIP2 = InetAddress.getByName("2.2.2.2");
+        InetAddress ipMask2 = InetAddress.getByName("255.255.255.255");
+        MatchField field1 = new MatchField(MatchType.NW_SRC, srcIP1, ipMask1);
+        MatchField field2 = new MatchField(MatchType.NW_DST, srcIP2, ipMask2);
+        Match match1 = new Match();
+        match1.setField(field1);
+        match1.setField(field2);
+        Match match2 = match1.reverse();
+        Assert.assertFalse(match1.hashCode() == match2.hashCode());
+    }
+
     @Test
     public void testHashCode() throws Exception {
         byte srcMac1[] = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9a, (byte) 0xbc };
@@ -392,7 +404,7 @@ public class MatchTest {
 
     @Test
     public void testCloning() throws Exception {
-        Node node = NodeCreator.createOFNode(7l);
+        Node node = NodeCreator.createOFNode(7L);
         NodeConnector port = NodeConnectorCreator.createOFNodeConnector((short) 24, node);
         byte srcMac[] = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9a, (byte) 0xbc };
         byte dstMac[] = { (byte) 0x1a, (byte) 0x2b, (byte) 0x3c, (byte) 0x4d, (byte) 0x5e, (byte) 0x6f };
@@ -458,7 +470,7 @@ public class MatchTest {
 
     @Test
     public void testFlip() throws Exception {
-        Node node = NodeCreator.createOFNode(7l);
+        Node node = NodeCreator.createOFNode(7L);
         NodeConnector port = NodeConnectorCreator.createOFNodeConnector((short) 24, node);
         byte srcMac[] = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9a, (byte) 0xbc };
         byte dstMac[] = { (byte) 0x1a, (byte) 0x2b, (byte) 0x3c, (byte) 0x4d, (byte) 0x5e, (byte) 0x6f };
@@ -532,6 +544,8 @@ public class MatchTest {
         InetAddress ipm2 = InetAddress.getByName("255.255.255.0");
         InetAddress ip3 = InetAddress.getByName("1.3.0.0");
         InetAddress ipm3 = InetAddress.getByName("255.255.0.0");
+        InetAddress ip4 = InetAddress.getByName("1.3.4.4");
+        InetAddress ipm4 = InetAddress.getByName("255.255.255.0");
 
         Match m1 = new Match();
         m1.setField(MatchType.DL_TYPE, ethType);
@@ -562,8 +576,9 @@ public class MatchTest {
 
         Match i = m1.getIntersection(m2);
         Assert.assertTrue(((Short)i.getField(MatchType.DL_TYPE).getValue()).equals(ethType));
-        Assert.assertTrue(((InetAddress)i.getField(MatchType.NW_SRC).getValue()).equals(ip2));
-        Assert.assertTrue(((InetAddress)i.getField(MatchType.NW_SRC).getMask()).equals(ipm2));
+        // Verify intersection of IP addresses is correct
+        Assert.assertTrue(((InetAddress)i.getField(MatchType.NW_SRC).getValue()).equals(ip1));
+        Assert.assertNull(i.getField(MatchType.NW_SRC).getMask());
 
         // Empty set
         i = m2.getIntersection(m3);
@@ -572,8 +587,14 @@ public class MatchTest {
         Match m4 = new Match();
         m4.setField(MatchType.DL_TYPE, ethType);
         m4.setField(MatchType.NW_PROTO, IPProtocols.TCP.byteValue());
+        m3.setField(MatchType.NW_SRC, ip4, ipm4);
         Assert.assertTrue(m4.intersetcs(m3));
 
+        // Verify intersection of IP and IP mask addresses is correct
+        Match ii = m3.getIntersection(m4);
+        Assert.assertTrue(((InetAddress)ii.getField(MatchType.NW_SRC).getValue()).equals(ip4));
+        Assert.assertTrue(((InetAddress)ii.getField(MatchType.NW_SRC).getMask()).equals(ipm4));
+
         Match m5 = new Match();
         m5.setField(MatchType.DL_TYPE, ethType);
         m3.setField(MatchType.NW_SRC, ip3, ipm3);