if (isAny(testAddress) || isAny(filterAddress)) {
return false;
}
-
- // Derive the masks length. A null mask means a full mask
- int testMaskLen = (testMask != null) ? NetUtils
- .getSubnetMaskLength(testMask.getAddress())
- : (testAddress instanceof Inet6Address) ? 128 : 32;
- int filterMaskLen = (filterMask != null) ? NetUtils
- .getSubnetMaskLength(filterMask.getAddress())
- : (filterAddress instanceof Inet6Address) ? 128 : 32;
-
- // Mask length check. Test mask has to be more generic than filter one
- if (testMaskLen < filterMaskLen) {
+
+ int testMaskLen = (testMask != null) ? NetUtils.getSubnetMaskLength(testMask.getAddress()) : 0;
+ int filterMaskLen = (filterMask != null) ? NetUtils.getSubnetMaskLength(filterMask.getAddress()) : 0;
+
+ int testPrefixLen = (testAddress instanceof Inet6Address) ? (128 - testMaskLen) : (32 - testMaskLen);
+ int filterPrefixLen = (filterAddress instanceof Inet6Address) ? (128 - filterMaskLen) : (32 - filterMaskLen);
+
+ // Mask length check. Test mask has to be more specific than filter one
+ if (testPrefixLen < filterPrefixLen) {
return true;
}
// Subnet Prefix on filter mask length must be the same
- InetAddress prefix1 = getSubnetPrefix(testAddress, filterMaskLen);
- InetAddress prefix2 = getSubnetPrefix(filterAddress, filterMaskLen);
+ InetAddress prefix1 = getSubnetPrefix(testAddress, filterPrefixLen);
+ InetAddress prefix2 = getSubnetPrefix(filterAddress, filterPrefixLen);
return (!prefix1.equals(prefix2));
}
import org.junit.Assert;
import org.junit.Test;
-import org.opendaylight.controller.sal.utils.NetUtils;
public class NetUtilsTest {
.isIPv6AddressValid("fe80:::0:0:0:204:61ff:fe9d/-1")); //not valid both
}
+
+ @Test
+ public void testInetAddressConflict() throws UnknownHostException {
+
+ // test a ipv4 testAddress in the same subnet as the filter
+ // the method should return false as there is no conflict
+ Assert.assertFalse(NetUtils.inetAddressConflict(
+ InetAddress.getByName("9.9.1.1"),
+ InetAddress.getByName("9.9.1.0"), null,
+ InetAddress.getByName("255.255.255.0")));
+
+ // test a ipv4 testAddress not in the same subnet as the filter
+ // the method should return true as there is a conflict
+ Assert.assertTrue(NetUtils.inetAddressConflict(
+ InetAddress.getByName("9.9.2.1"),
+ InetAddress.getByName("9.9.1.0"), null,
+ InetAddress.getByName("255.255.255.0")));
+
+ // test a ipv4 testAddress more generic than the filter
+ // the method should return true as there is a conflict
+ Assert.assertTrue(NetUtils.inetAddressConflict(
+ InetAddress.getByName("9.9.1.1"),
+ InetAddress.getByName("9.9.1.0"),
+ InetAddress.getByName("255.255.0.0"),
+ InetAddress.getByName("255.255.255.0")));
+
+ // test a ipv4 testAddress less generic than the filter and in the same
+ // subnet as the filter
+ // the method should return false as there is no conflict
+ Assert.assertFalse(NetUtils.inetAddressConflict(
+ InetAddress.getByName("9.9.1.0"),
+ InetAddress.getByName("9.9.0.0"),
+ InetAddress.getByName("255.255.255.0"),
+ InetAddress.getByName("255.255.0.0")));
+
+ // test a ipv4 testAddress less generic than the filter and not in the
+ // same subnet as the filter
+ // the method should return true as there is a conflict
+ Assert.assertTrue(NetUtils.inetAddressConflict(
+ InetAddress.getByName("9.8.1.0"),
+ InetAddress.getByName("9.9.0.0"),
+ InetAddress.getByName("255.255.255.0"),
+ InetAddress.getByName("255.255.0.0")));
+
+ }
}