diff --git a/src/main/java/org/olat/core/util/IPUtils.java b/src/main/java/org/olat/core/util/IPUtils.java index 2dab48d5936463d5ca800d9b581356d0e7ec8d85..c2b0fc76669361261b1a2a3e8ea0ecae50858e3d 100644 --- a/src/main/java/org/olat/core/util/IPUtils.java +++ b/src/main/java/org/olat/core/util/IPUtils.java @@ -26,7 +26,7 @@ import org.olat.core.logging.Tracing; * * Thanks: https://gist.github.com/madan712/6651967 * - * It's based of the InetAddresses clas from guava too and + * It's based of the InetAddresses class from guava too and * prevent a DNS lookup of java.net.InetAddress * * Initial date: 18.12.2014<br> @@ -48,6 +48,7 @@ public class IPUtils { public static boolean isValidRange(String ipWithMask, String address) { boolean allOk = false; + ipWithMask = ipWithMask.trim(); int maskIndex = ipWithMask.indexOf('/'); if(maskIndex > 0) { long bits = Long.parseLong(ipWithMask.substring(maskIndex + 1)); @@ -71,6 +72,9 @@ public class IPUtils { */ public static boolean isValidRange(String ipStart, String ipEnd, String ipToCheck) { try { + ipStart = ipStart.trim(); + ipEnd = ipEnd.trim(); + ipToCheck = ipToCheck.trim(); long ipLo = ipToLong(textToNumericFormatV4(ipStart)); long ipHi = ipToLong(textToNumericFormatV4(ipEnd)); long ipToTest = ipToLong(textToNumericFormatV4(ipToCheck)); diff --git a/src/test/java/org/olat/core/util/IPUtilsTest.java b/src/test/java/org/olat/core/util/IPUtilsTest.java index 009111b17e997424a69ca949c7aca72a8c32c981..a74529da95987afaa1309d173e823fcc44eb36dd 100644 --- a/src/test/java/org/olat/core/util/IPUtilsTest.java +++ b/src/test/java/org/olat/core/util/IPUtilsTest.java @@ -41,19 +41,4 @@ public class IPUtilsTest { boolean check2 = IPUtils.isValidRange(start, end, "192.168.5.45"); Assert.assertFalse(check2); } - - @Test - public void checkRange_mask_31() { - String ipWithMask = "192.168.100.1/24"; - - boolean allowed1 = IPUtils.isValidRange(ipWithMask, "192.168.100.1"); - Assert.assertTrue(allowed1); - - boolean notAllowed1 = IPUtils.isValidRange(ipWithMask, "192.168.99.255"); - Assert.assertFalse(notAllowed1); - boolean notAllowed2 = IPUtils.isValidRange(ipWithMask, "192.168.101.1"); - Assert.assertFalse(notAllowed2); - boolean notAllowed3 = IPUtils.isValidRange(ipWithMask, "212.34.100.0"); - Assert.assertFalse(notAllowed3); - } } diff --git a/src/test/java/org/olat/core/util/IPUtilsValidRangeTest.java b/src/test/java/org/olat/core/util/IPUtilsValidRangeTest.java new file mode 100644 index 0000000000000000000000000000000000000000..feb95e8a78b77568c5e8fb7e350047548962c016 --- /dev/null +++ b/src/test/java/org/olat/core/util/IPUtilsValidRangeTest.java @@ -0,0 +1,69 @@ +/** + * <a href="http://www.openolat.org"> + * OpenOLAT - Online Learning and Training</a><br> + * <p> + * Licensed under the Apache License, Version 2.0 (the "License"); <br> + * you may not use this file except in compliance with the License.<br> + * You may obtain a copy of the License at the + * <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache homepage</a> + * <p> + * Unless required by applicable law or agreed to in writing,<br> + * software distributed under the License is distributed on an "AS IS" BASIS, <br> + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. <br> + * See the License for the specific language governing permissions and <br> + * limitations under the License. + * <p> + * Initial code contributed and copyrighted by<br> + * frentix GmbH, http://www.frentix.com + * <p> + */ +package org.olat.core.util; + +import java.util.Arrays; +import java.util.Collection; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameters; + +/** + * + * Initial date: 21 févr. 2018<br> + * @author srosse, stephane.rosse@frentix.com, http://www.frentix.com + * + */ +@RunWith(Parameterized.class) +public class IPUtilsValidRangeTest { + + @Parameters + public static Collection<Object[]> data() { + return Arrays.asList(new Object[][] { + { "192.168.100.1/24", "192.168.100.1", Boolean.TRUE }, + { "192.168.100.1/24", "192.168.99.255", Boolean.FALSE }, + { "192.168.100.1/24", "192.168.101.1", Boolean.FALSE }, + { "192.168.100.1/24", "212.34.100.0", Boolean.FALSE }, + { "192.168.100.1/24 ", "192.168.100.1", Boolean.TRUE }, + { "172.20.36.0/24 ", "192.168.100.1", Boolean.FALSE }, + { "192.168.1.0/24", "192.168.100.1", Boolean.FALSE }, + { "192.168.1.0/24", "192.168.1.1", Boolean.TRUE } + }); + } + + private String ip; + private String ipToCheck; + private Boolean result; + + public IPUtilsValidRangeTest(String ip, String ipToCheck, Boolean result) { + this.ip = ip; + this.ipToCheck = ipToCheck; + this.result = result; + } + + @Test + public void checkRange() { + boolean allowed = IPUtils.isValidRange(ip, ipToCheck); + Assert.assertEquals(result, Boolean.valueOf(allowed)); + } +} diff --git a/src/test/java/org/olat/test/AllTestsJunit4.java b/src/test/java/org/olat/test/AllTestsJunit4.java index bf0a00cfc17d8624946e43395e4bc9b4575d4e40..4ca655f9c23590122d82b0f3dee95f13cb73bf33 100644 --- a/src/test/java/org/olat/test/AllTestsJunit4.java +++ b/src/test/java/org/olat/test/AllTestsJunit4.java @@ -69,6 +69,7 @@ import org.junit.runners.Suite; org.olat.core.util.EncoderTest.class, org.olat.core.util.SimpleHtmlParserTest.class, org.olat.core.util.IPUtilsTest.class, + org.olat.core.util.IPUtilsValidRangeTest.class, org.olat.core.util.mail.EmailAddressValidatorTest.class, org.olat.core.util.mail.manager.MailManagerTest.class, org.olat.core.util.openxml.OpenXmlWorkbookTest.class,