CLOUDSTACK-9086: ACS allows to create isolated networks with invalide gateway IP address - Fixed and Test cases added

This commit is contained in:
Kshitij Kansal 2015-12-08 13:01:39 +05:30
parent e08294a95f
commit c7c389d61c
3 changed files with 33 additions and 0 deletions

View File

@ -1197,6 +1197,13 @@ public class NetworkServiceImpl extends ManagerBase implements NetworkService {
}
if (gateway != null && netmask != null) {
if(NetUtils.isNetworkorBroadcastIP(gateway,netmask)) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("The gateway IP provided is " + gateway + " and netmask is " + netmask + ". The IP is either broadcast or network IP.");
}
throw new InvalidParameterValueException("Invalid gateway IP provided. Either the IP is broadcast or network IP.");
}
if (!NetUtils.isValidIp(gateway)) {
throw new InvalidParameterValueException("Invalid gateway");
}

View File

@ -1570,5 +1570,13 @@ public class NetUtils {
}
return false;
}
public static boolean isNetworkorBroadcastIP(String ip, String netmask){
String cidr = getCidrFromGatewayAndNetmask(ip,netmask);
final SubnetUtils subnetUtils = new SubnetUtils(cidr);
subnetUtils.setInclusiveHostCount(false);
final boolean isInRange = subnetUtils.getInfo().isInRange(ip);
return !isInRange;
}
}

View File

@ -508,4 +508,22 @@ public class NetUtilsTest {
public void testIsNetworksOverlapWithEmptyValues() {
assertEquals(false, NetUtils.isNetworksOverlap("", null));
}
@Test
public void testisNetworkorBroadCastIP(){
//Checking the True conditions
assertTrue(NetUtils.isNetworkorBroadcastIP("192.168.0.0","255.255.255.0"));
assertTrue(NetUtils.isNetworkorBroadcastIP("192.168.0.255","255.255.255.0"));
assertTrue(NetUtils.isNetworkorBroadcastIP("192.168.0.127","255.255.255.128"));
assertTrue(NetUtils.isNetworkorBroadcastIP("192.168.0.63","255.255.255.192"));
//Checking the False conditions
assertFalse(NetUtils.isNetworkorBroadcastIP("192.168.0.1","255.255.255.0"));
assertFalse(NetUtils.isNetworkorBroadcastIP("192.168.0.127","255.255.255.0"));
assertFalse(NetUtils.isNetworkorBroadcastIP("192.168.0.126","255.255.255.128"));
assertFalse(NetUtils.isNetworkorBroadcastIP("192.168.0.62","255.255.255.192"));
assertTrue(NetUtils.isNetworkorBroadcastIP("192.168.0.63","255.255.255.192"));
assertFalse(NetUtils.isNetworkorBroadcastIP("192.168.0.63","255.255.255.128"));
}
}