Skip to content

Commit 1d6ee2d

Browse files
Changed the behavior of methods that use NetUtils.cidrToLong(String)
Given that the method com.cloud.utils.net.NetUtils.cidrToLong(String) now throws an exception when receiving null or empty cidrs, there is the need to change methods that use it. Those methods were changed and test cases created.
1 parent 02058b9 commit 1d6ee2d

File tree

2 files changed

+51
-13
lines changed

2 files changed

+51
-13
lines changed

utils/src/main/java/com/cloud/utils/net/NetUtils.java

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import java.util.regex.Matcher;
4141
import java.util.regex.Pattern;
4242

43+
import org.apache.commons.lang.StringUtils;
4344
import org.apache.commons.lang.SystemUtils;
4445
import org.apache.commons.net.util.SubnetUtils;
4546
import org.apache.commons.validator.routines.InetAddressValidator;
@@ -838,13 +839,13 @@ public static enum SupersetOrSubset {
838839
}
839840

840841
public static SupersetOrSubset isNetowrkASubsetOrSupersetOfNetworkB(final String cidrA, final String cidrB) {
842+
if (!areCidrsNotEmpty(cidrA, cidrB)) {
843+
return SupersetOrSubset.errorInCidrFormat;
844+
}
841845
final Long[] cidrALong = cidrToLong(cidrA);
842846
final Long[] cidrBLong = cidrToLong(cidrB);
843847
long shift = 0;
844-
if (cidrALong == null || cidrBLong == null) {
845-
//implies error in the cidr format
846-
return SupersetOrSubset.errorInCidrFormat;
847-
}
848+
848849
if (cidrALong[1] >= cidrBLong[1]) {
849850
shift = MAX_CIDR - cidrBLong[1];
850851
} else {
@@ -867,15 +868,20 @@ public static SupersetOrSubset isNetowrkASubsetOrSupersetOfNetworkB(final String
867868
}
868869

869870
public static boolean isNetworkAWithinNetworkB(final String cidrA, final String cidrB) {
870-
final Long[] cidrALong = cidrToLong(cidrA);
871-
final Long[] cidrBLong = cidrToLong(cidrB);
872-
if (cidrALong == null || cidrBLong == null) {
871+
if (!areCidrsNotEmpty(cidrA, cidrB)) {
873872
return false;
874873
}
875-
final long shift = MAX_CIDR - cidrBLong[1];
874+
Long[] cidrALong = cidrToLong(cidrA);
875+
Long[] cidrBLong = cidrToLong(cidrB);
876+
877+
long shift = MAX_CIDR - cidrBLong[1];
876878
return cidrALong[0] >> shift == cidrBLong[0] >> shift;
877879
}
878880

881+
static boolean areCidrsNotEmpty(String cidrA, String cidrB) {
882+
return StringUtils.isNotEmpty(cidrA) && StringUtils.isNotEmpty(cidrB);
883+
}
884+
879885
public static Long[] cidrToLong(final String cidr) {
880886
if (cidr == null || cidr.isEmpty()) {
881887
throw new CloudRuntimeException("empty cidr can not be converted to longs");
@@ -887,12 +893,12 @@ public static Long[] cidrToLong(final String cidr) {
887893
final String cidrAddress = cidrPair[0];
888894
final String cidrSize = cidrPair[1];
889895
if (!isValidIp(cidrAddress)) {
890-
throw new CloudRuntimeException("cidr is not bvalid in ip space" + cidr);
896+
throw new CloudRuntimeException("cidr is not valid in ip space" + cidr);
891897
}
892898
long cidrSizeNum = getCidrSizeFromString(cidrSize);
893899
final long numericNetmask = netMaskFromCidr(cidrSizeNum);
894900
final long ipAddr = ip2Long(cidrAddress);
895-
final Long[] cidrlong = {ipAddr & numericNetmask, (long)cidrSizeNum};
901+
final Long[] cidrlong = {ipAddr & numericNetmask, cidrSizeNum};
896902
return cidrlong;
897903

898904
}
@@ -1164,11 +1170,12 @@ public static boolean verifyInstanceName(final String instanceName) {
11641170
}
11651171

11661172
public static boolean isNetworksOverlap(final String cidrA, final String cidrB) {
1167-
final Long[] cidrALong = cidrToLong(cidrA);
1168-
final Long[] cidrBLong = cidrToLong(cidrB);
1169-
if (cidrALong == null || cidrBLong == null) {
1173+
if (!areCidrsNotEmpty(cidrA, cidrB)) {
11701174
return false;
11711175
}
1176+
Long[] cidrALong = cidrToLong(cidrA);
1177+
Long[] cidrBLong = cidrToLong(cidrB);
1178+
11721179
final long shift = MAX_CIDR - (cidrALong[1] > cidrBLong[1] ? cidrBLong[1] : cidrALong[1]);
11731180
return cidrALong[0] >> shift == cidrBLong[0] >> shift;
11741181
}

utils/src/test/java/com/cloud/utils/net/NetUtilsTest.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.junit.Test;
4040

4141
import com.cloud.utils.exception.CloudRuntimeException;
42+
import com.cloud.utils.net.NetUtils.SupersetOrSubset;
4243
import com.googlecode.ipv6.IPv6Address;
4344

4445
public class NetUtilsTest {
@@ -477,4 +478,34 @@ public void testNetmaskFromCidr() {
477478
mask = NetUtils.netMaskFromCidr(32l);
478479
assertEquals("mask not right: " + mask, 0xffffffff, mask);
479480
}
481+
482+
@Test
483+
public void testIsCidrsNotEmptyWithNullCidrs() {
484+
assertEquals(false, NetUtils.areCidrsNotEmpty(null, null));
485+
}
486+
487+
@Test
488+
public void testIsCidrsNotEmptyWithEmptyCidrs() {
489+
assertEquals(false, NetUtils.areCidrsNotEmpty("", " "));
490+
}
491+
492+
@Test
493+
public void testIsCidrsNotEmpty() {
494+
assertEquals(true, NetUtils.areCidrsNotEmpty("10.10.0.0/16", "10.1.2.3/16"));
495+
}
496+
497+
@Test
498+
public void testIsNetowrkASubsetOrSupersetOfNetworkBWithEmptyValues() {
499+
assertEquals(SupersetOrSubset.errorInCidrFormat, NetUtils.isNetowrkASubsetOrSupersetOfNetworkB("", null));
500+
}
501+
502+
@Test
503+
public void testIsNetworkAWithinNetworkBWithEmptyValues() {
504+
assertEquals(false, NetUtils.isNetworkAWithinNetworkB("", null));
505+
}
506+
507+
@Test
508+
public void testIsNetworksOverlapWithEmptyValues() {
509+
assertEquals(false, NetUtils.isNetworksOverlap("", null));
510+
}
480511
}

0 commit comments

Comments
 (0)