Eliraz Levi has uploaded a new change for review. Change subject: tools: CidrValidation Utils ......................................................................
tools: CidrValidation Utils Cidr validator - check for format correctness and valid network address including UTest Change-Id: Ib1277dbc815953926fe1648350cd55cb75e1084a Bug-Url: https://bugzilla.redhat.com/1080074 Signed-off-by: Eliraz Levi <[email protected]> --- M backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/utils/ValidationUtils.java A backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/validation/CidrValidator.java A backend/manager/modules/common/src/test/java/org/ovirt/engine/core/common/validation/CidrValidatorTest.java 3 files changed, 259 insertions(+), 1 deletion(-) git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/39/32539/1 diff --git a/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/utils/ValidationUtils.java b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/utils/ValidationUtils.java index bd006b4..f3744f3 100644 --- a/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/utils/ValidationUtils.java +++ b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/utils/ValidationUtils.java @@ -24,7 +24,9 @@ public static final String NO_TRIMMING_WHITE_SPACES_PATTERN = "^$|\\S.*\\S"; public static final String IP_PATTERN = "^\\b((25[0-5]|2[0-4]\\d|[01]\\d\\d|\\d?\\d)\\.){3}(25[0-5]|2[0-4]\\d|[01]\\d\\d|\\d?\\d)\\b$|^$"; - + //TODO elevi consider name - could be miss leading + public static final String CIDR_FORMAT_PATTERN = + "\\b((25[0-5]|2[0-4]\\d|[01]\\d\\d|\\d?\\d)\\.){3}(25[0-5]|2[0-4]\\d|[01]\\d\\d|\\d?\\d)(?:/(?:3[0-2]|[12]?[0-9]))$"; public static final String ISO_SUFFIX = ".iso"; public static final String ISO_SUFFIX_PATTERN = "^$|^.+\\.iso$"; diff --git a/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/validation/CidrValidator.java b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/validation/CidrValidator.java new file mode 100644 index 0000000..5229668 --- /dev/null +++ b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/validation/CidrValidator.java @@ -0,0 +1,70 @@ +package org.ovirt.engine.core.common.validation; + +import org.ovirt.engine.core.common.utils.ValidationUtils; + +public class CidrValidator { + private static String getCidrRegex() { + return ValidationUtils.CIDR_FORMAT_PATTERN; + } + + /*** + * check if CIDR is in correct format - x.x.x.x/y where: x belongs to [0,255] y belongs to [0,32] both inclusive + * note! the function is not validating that ip and mask match to a network address, please see @see + * {@link CidrValidator#isCidrFormatValid(String)} + * @param cidr + * @return true if correct format, false otherwise. + */ + public static boolean isCidrFormatValid(String cidr) { + return (cidr == null || !cidr.matches(getCidrRegex())) ? + false : true; + } + + /*** + * check if CIDR represent valid network add + * @param cidr + * - in correct format, please use the following function first: @see + * {@link CidrValidator#isCidrFormatValid(String)} + * @return true if valid CIDR ,false otherwise + */ + public static boolean isCidrValidNetworkAddress(String cidr) { + String ipAsString = getIpFromCidr(cidr); + int ipAsInteger = covnertIpToInt(ipAsString); + int mask = getMaskFromCidr(cidr); + return isNetworkAddress(ipAsInteger, mask); + } + + private static int getMaskFromCidr(String cidr) { + String[] temp = cidr.split("/"); + return Integer.parseInt(temp[1]); + } + + private static String getIpFromCidr(String cidr) { + String[] temp = cidr.split("/"); + return temp[0]; + } + + private static int covnertIpToInt(String ipAdd) { + String[] subAdd = ipAdd.split("\\."); + int output = 0; + int temp; + for (int index = 3; index > -1; index--) { + temp = Integer.parseInt(subAdd[3 - index]); + temp <<= (index * 8); + output |= temp; + } + return output; + } + + private static boolean isNetworkAddress(int ip, int mask) { + int temp = 1; + int check = 0; + for (int i = 0; i < mask; i++) { + check = temp & ip; + if (check != 0) { + return false; + } + temp <<= 1; + } + return true; + } +} diff --git a/backend/manager/modules/common/src/test/java/org/ovirt/engine/core/common/validation/CidrValidatorTest.java b/backend/manager/modules/common/src/test/java/org/ovirt/engine/core/common/validation/CidrValidatorTest.java new file mode 100644 index 0000000..7e89cef --- /dev/null +++ b/backend/manager/modules/common/src/test/java/org/ovirt/engine/core/common/validation/CidrValidatorTest.java @@ -0,0 +1,186 @@ +package org.ovirt.engine.core.common.validation; + +import static org.junit.Assert.assertEquals; + +import java.util.Arrays; +import java.util.Collection; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameters; + +@RunWith(Parameterized.class) +public class CidrValidatorTest { + private String cidr; + private boolean expectedResult; + + public CidrValidatorTest(String cidr, boolean expectedResult) { + this.cidr = cidr; + this.expectedResult = expectedResult; + } + + @Test + public void checkCidrValidation() { + boolean result = CidrValidator.isCidrFormatValid(cidr); + if (result) { + assertEquals(expectedResult, CidrValidator.isCidrValidNetworkAddress(cidr)); + } + else { + assertEquals(expectedResult, CidrValidator.isCidrFormatValid(cidr)); + } + } + + @Parameters + public static Collection<Object[]> data() { + return Arrays.asList(new Object[][] { + // Bad Format + { null, false }, + { "", false }, + { "?\"?>!", false }, + { "a", false }, + { "a.a", false }, + { "a.a.a", false }, + { "a.a.a.a", false }, + { "a.a.a.a/a", false }, + { "1.1.1.1/33", false }, + { "1111.1.1.1/1", false }, + { "1111.1.1.1/32", false }, + { "1.1.1.1/1/1", false }, + { "1.1/1.1/.1/.1", false }, + { "256.1.1.1/1", false }, + { "256.1.1.1/32", false }, + { "256.1.1.1/222222222222222222222222", false }, + { "255.?.?././", false }, + { "255?23?1?0/8", false }, + { "23\22\22\22\22\\", false }, + { ".................", false }, + { "././././", false }, + { "?/?/?/?/", false }, + // Not A Network address + { "255.255.255.250/16", false }, + { "255.255.255.255/16", false }, + { "255.255.255.255/1", false }, + { "255.255.255.253/2", false }, + { "255.255.255.247/3", false }, + { "255.255.255.243/4", false }, + { "255.255.255.228/5", false }, + { "255.255.255.199/6", false }, + { "255.255.255.119/7", false }, + { "255.255.255.16/8", false }, + + { "255.255.254.128/9", false }, + { "255.255.252.64/10", false }, + { "255.255.255.0/11", false }, + { "255.255.253.0/12", false }, + { "255.255.240.0/13", false }, + { "255.255.200.17/14", false }, + { "255.255.140.0/15", false }, + { "255.255.64.0/16", false }, + + { "255.254.128.0/17", false }, + { "255.255.0.0/18", false }, + { "255.248.64.0/19", false }, + { "255.240.54.32/20", false }, + { "255.225.0.0/21", false }, + { "255.193.0.0/22", false }, + { "255.14.1.0/23", false }, + { "255.1.0.0/24", false }, + + { "255.0.0.64/25", false }, + { "253.0.0.0/26", false }, + { "249.43.32.0/27", false }, + { "245.0.0.0/28", false }, + { "226.0.0.0/29", false }, + { "198.0.0.0/30", false }, + { "130.0.0.0/31", false }, + { "1.0.0.0/32", false }, + + // valid CIDR + { "255.255.255.255/0", true }, + { "255.255.255.254/1", true }, + { "255.255.255.252/2", true }, + { "255.255.255.248/3", true }, + { "255.255.255.240/4", true }, + { "255.255.255.224/5", true }, + { "255.255.255.192/6", true }, + { "255.255.255.128/7", true }, + { "255.255.255.0/8", true }, + + { "255.255.254.0/9", true }, + { "255.255.252.0/10", true }, + { "255.255.248.0/11", true }, + { "255.255.240.0/12", true }, + { "255.255.224.0/13", true }, + { "255.255.192.0/14", true }, + { "255.255.128.0/15", true }, + { "255.255.0.0/16", true }, + + { "255.254.0.0/17", true }, + { "255.252.0.0/18", true }, + { "255.248.0.0/19", true }, + { "255.240.0.0/20", true }, + { "255.224.0.0/21", true }, + { "255.192.0.0/22", true }, + { "255.128.0.0/23", true }, + { "255.0.0.0/24", true }, + + { "254.0.0.0/25", true }, + { "252.0.0.0/26", true }, + { "248.0.0.0/27", true }, + { "240.0.0.0/28", true }, + { "224.0.0.0/29", true }, + { "192.0.0.0/30", true }, + { "128.0.0.0/31", true }, + { "0.0.0.0/32", true }, + + { "255.255.255.255/0", true }, + { "255.255.255.254/1", true }, + { "255.255.255.252/2", true }, + { "255.255.255.248/3", true }, + { "255.255.255.240/4", true }, + { "255.255.255.224/5", true }, + { "255.255.255.192/6", true }, + { "255.255.255.128/7", true }, + { "255.255.255.0/8", true }, + + { "255.0.254.0/9", true }, + { "255.0.252.0/10", true }, + { "255.0.248.0/11", true }, + { "255.0.240.0/12", true }, + { "255.0.224.0/13", true }, + { "255.0.192.0/14", true }, + { "255.0.128.0/15", true }, + { "255.0.0.0/16", true }, + // TODO elevi ask if this consider a valid cidr + { "255.0.0.0/17", true }, + { "255.0.0.0/18", true }, + { "255.0.0.0/19", true }, + { "255.0.0.0/20", true }, + { "1.0.0.0/21", true }, + { "1.0.0.0/22", true }, + { "1.0.0.0/23", true }, + { "1.0.0.0/24", true }, + + { "0.255.255.255/0", true }, + { "0.255.255.254/1", true }, + { "0.255.255.252/2", true }, + { "05.255.255.248/3", true }, + { "0.255.255.240/4", true }, + { "0.255.255.224/5", true }, + { "0.255.255.192/6", true }, + { "0.255.255.128/7", true }, + { "0.255.255.0/8", true }, + + { "0.255.254.0/9", true }, + { "0.255.252.0/10", true }, + { "0.255.248.0/11", true }, + { "0.255.240.0/12", true }, + { "0.255.224.0/13", true }, + { "0.255.192.0/14", true }, + { "0.255.128.0/15", true }, + { "0.255.0.0/16", true }, + }); + } + +} -- To view, visit http://gerrit.ovirt.org/32539 To unsubscribe, visit http://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Ib1277dbc815953926fe1648350cd55cb75e1084a Gerrit-PatchSet: 1 Gerrit-Project: ovirt-engine Gerrit-Branch: master Gerrit-Owner: Eliraz Levi <[email protected]> _______________________________________________ Engine-patches mailing list [email protected] http://lists.ovirt.org/mailman/listinfo/engine-patches
