+
+ @Test
+ public void testVlanNone() throws Exception {
+ // The value 0 is used to indicate that no VLAN ID is set
+ short vlan = (short) 0;
+ MatchField field = new MatchField(MatchType.DL_VLAN, vlan);
+
+ Assert.assertTrue(field != null);
+ Assert.assertTrue(field.getValue().equals(new Short(vlan)));
+ Assert.assertTrue(field.isValid());
+ }
+
+ @Test
+ public void testIntersection() throws UnknownHostException {
+ Short ethType = Short.valueOf((short)0x800);
+ InetAddress ip1 = InetAddress.getByName("1.1.1.1");
+ InetAddress ip2 = InetAddress.getByName("1.1.1.0");
+ 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);
+ m1.setField(MatchType.NW_SRC, ip1);
+
+ Match m2 = new Match();
+ m2.setField(MatchType.DL_TYPE, ethType);
+ m2.setField(MatchType.NW_SRC, ip2, ipm2);
+
+ Match m3 = new Match();
+ m3.setField(MatchType.DL_TYPE, ethType);
+ m3.setField(MatchType.NW_SRC, ip3, ipm3);
+ m3.setField(MatchType.NW_PROTO, IPProtocols.TCP.byteValue());
+
+ Match m3r = m3.reverse();
+ Assert.assertTrue(m3.intersetcs(m3r));
+
+ Assert.assertTrue(m1.intersetcs(m2));
+ Assert.assertTrue(m2.intersetcs(m1));
+ Assert.assertFalse(m1.intersetcs(m3));
+ Assert.assertTrue(m1.intersetcs(m3r));
+ Assert.assertFalse(m3.intersetcs(m1));
+ Assert.assertTrue(m3.intersetcs(m1.reverse()));
+ Assert.assertFalse(m2.intersetcs(m3));
+ Assert.assertFalse(m3.intersetcs(m2));
+ Assert.assertTrue(m2.intersetcs(m3r));
+
+
+ Match i = m1.getIntersection(m2);
+ Assert.assertTrue(((Short)i.getField(MatchType.DL_TYPE).getValue()).equals(ethType));
+ // 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);
+ Assert.assertNull(i);
+
+ 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);
+ m5.setField(MatchType.NW_PROTO, IPProtocols.UDP.byteValue());
+ Assert.assertFalse(m5.intersetcs(m3));
+ Assert.assertFalse(m5.intersetcs(m4));
+ Assert.assertTrue(m5.intersetcs(m5));
+ Assert.assertFalse(m3.intersetcs(m5));
+ Assert.assertFalse(m4.intersetcs(m5));
+
+
+ Match i2 = m4.getIntersection(m3);
+ Assert.assertFalse(i2.getMatches() == 0);
+ Assert.assertFalse(i2.getMatchesList().isEmpty());
+ Assert.assertTrue(((InetAddress)i2.getField(MatchType.NW_SRC).getValue()).equals(ip3));
+ Assert.assertTrue(((InetAddress)i2.getField(MatchType.NW_SRC).getMask()).equals(ipm3));
+ Assert.assertTrue(((Byte)i2.getField(MatchType.NW_PROTO).getValue()).equals(IPProtocols.TCP.byteValue()));
+
+ byte src[] = {(byte)0, (byte)0xab,(byte)0xbc,(byte)0xcd,(byte)0xde,(byte)0xef};
+ byte dst[] = {(byte)0x10, (byte)0x11,(byte)0x12,(byte)0x13,(byte)0x14,(byte)0x15};
+ Short srcPort = (short)1024;
+ Short dstPort = (short)80;
+
+ // Check identity
+ Match m6 = new Match();
+ m6.setField(MatchType.DL_SRC, src);
+ m6.setField(MatchType.DL_DST, dst);
+ m6.setField(MatchType.NW_SRC, ip2, ipm2);
+ m6.setField(MatchType.NW_DST, ip3, ipm3);
+ m6.setField(MatchType.NW_PROTO, IPProtocols.UDP.byteValue());
+ m6.setField(MatchType.TP_SRC, srcPort);
+ m6.setField(MatchType.TP_DST, dstPort);
+ Assert.assertTrue(m6.intersetcs(m6));
+ Assert.assertTrue(m6.getIntersection(m6).equals(m6));
+
+ // Empty match, represents the universal set (all packets)
+ Match u = new Match();
+ Assert.assertTrue(m6.getIntersection(u).equals(m6));
+ Assert.assertTrue(u.getIntersection(m6).equals(m6));
+
+ // No intersection with null match, empty set
+ Assert.assertNull(m6.getIntersection(null));
+ }
+
+ @Test
+ public void testMetadata() {
+ Property tier1 = new Tier(1);
+ Property tier2 = new Tier(2);
+ Property table1 = new Tables((byte)0x7f);
+ Match m1 = new Match();
+ List<Property> resprops = null;
+ resprops = m1.getMetadatas();
+ // This should be null
+ Assert.assertTrue(resprops.isEmpty());
+ m1.setMetadata("tier1", tier1);
+ m1.setMetadata("tier2", tier2);
+ m1.setMetadata("table1", table1);
+ resprops = m1.getMetadatas();
+ // Check for the number of elements in it
+ Assert.assertTrue(resprops.size() == 3);
+ // Check if the elements are in it
+ Assert.assertTrue(resprops.contains(tier1));
+ Assert.assertTrue(resprops.contains(tier2));
+ Assert.assertTrue(resprops.contains(table1));
+ // Check for single elements retrieve
+ Assert.assertTrue(m1.getMetadata("tier1").equals(tier1));
+ Assert.assertTrue(m1.getMetadata("tier2").equals(tier2));
+ Assert.assertTrue(m1.getMetadata("table1").equals(table1));
+ // Now remove an element and make sure the remaining are
+ // correct
+ m1.removeMetadata("tier1");
+
+ resprops = m1.getMetadatas();
+ // Check for the number of elements in it
+ Assert.assertTrue(resprops.size() == 2);
+ // Check if the elements are in it
+ Assert.assertFalse(resprops.contains(tier1));
+ Assert.assertTrue(resprops.contains(tier2));
+ Assert.assertTrue(resprops.contains(table1));
+ // Check for single elements retrieve
+ Assert.assertTrue(m1.getMetadata("table1").equals(table1));
+ Assert.assertTrue(m1.getMetadata("tier2").equals(tier2));
+ Assert.assertNull(m1.getMetadata("tier1"));
+
+ // Check for an element never existed
+ Assert.assertNull(m1.getMetadata("table100"));
+
+ // Remove them all
+ m1.removeMetadata("tier2");
+ m1.removeMetadata("table1");
+
+ // Remove also a non-existent one
+ m1.removeMetadata("table100");
+
+ resprops = m1.getMetadatas();
+ // Check there are no elements left
+ Assert.assertTrue(resprops.size() == 0);
+
+ // Now check for exception on setting null values
+ try {
+ m1.setMetadata("foo", null);
+ // The line below should never be reached
+ Assert.assertTrue(false);
+ } catch (NullPointerException nue) {
+ // NPE should be raised for null value
+ Assert.assertTrue(true);
+ }
+
+ // Now check on using null key
+ try {
+ m1.setMetadata(null, table1);
+ // The line below should never be reached
+ Assert.assertTrue(false);
+ } catch (NullPointerException nue) {
+ // NPE should be raised for null value
+ Assert.assertTrue(true);
+ }
+
+ // Now check on using null key and null value
+ try {
+ m1.setMetadata(null, null);
+ // The line below should never be reached
+ Assert.assertTrue(false);
+ } catch (NullPointerException nue) {
+ // NPE should be raised for null value
+ Assert.assertTrue(true);
+ }
+ }