Skip to content
Snippets Groups Projects
Commit fae5af86 authored by srosse's avatar srosse
Browse files

Merge OpenOLAt 12.3 to OpenOLAT default branch with 418ed7f1fc83c7f88e6204daef415b2fc5170395

parents c30164b8 401975da
No related branches found
No related tags found
No related merge requests found
...@@ -26,7 +26,7 @@ import org.olat.core.logging.Tracing; ...@@ -26,7 +26,7 @@ import org.olat.core.logging.Tracing;
* *
* Thanks: https://gist.github.com/madan712/6651967 * 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 * prevent a DNS lookup of java.net.InetAddress
* *
* Initial date: 18.12.2014<br> * Initial date: 18.12.2014<br>
...@@ -48,6 +48,7 @@ public class IPUtils { ...@@ -48,6 +48,7 @@ public class IPUtils {
public static boolean isValidRange(String ipWithMask, String address) { public static boolean isValidRange(String ipWithMask, String address) {
boolean allOk = false; boolean allOk = false;
ipWithMask = ipWithMask.trim();
int maskIndex = ipWithMask.indexOf('/'); int maskIndex = ipWithMask.indexOf('/');
if(maskIndex > 0) { if(maskIndex > 0) {
long bits = Long.parseLong(ipWithMask.substring(maskIndex + 1)); long bits = Long.parseLong(ipWithMask.substring(maskIndex + 1));
...@@ -71,6 +72,9 @@ public class IPUtils { ...@@ -71,6 +72,9 @@ public class IPUtils {
*/ */
public static boolean isValidRange(String ipStart, String ipEnd, String ipToCheck) { public static boolean isValidRange(String ipStart, String ipEnd, String ipToCheck) {
try { try {
ipStart = ipStart.trim();
ipEnd = ipEnd.trim();
ipToCheck = ipToCheck.trim();
long ipLo = ipToLong(textToNumericFormatV4(ipStart)); long ipLo = ipToLong(textToNumericFormatV4(ipStart));
long ipHi = ipToLong(textToNumericFormatV4(ipEnd)); long ipHi = ipToLong(textToNumericFormatV4(ipEnd));
long ipToTest = ipToLong(textToNumericFormatV4(ipToCheck)); long ipToTest = ipToLong(textToNumericFormatV4(ipToCheck));
......
...@@ -41,19 +41,4 @@ public class IPUtilsTest { ...@@ -41,19 +41,4 @@ public class IPUtilsTest {
boolean check2 = IPUtils.isValidRange(start, end, "192.168.5.45"); boolean check2 = IPUtils.isValidRange(start, end, "192.168.5.45");
Assert.assertFalse(check2); 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);
}
} }
/**
* <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));
}
}
...@@ -69,6 +69,7 @@ import org.junit.runners.Suite; ...@@ -69,6 +69,7 @@ import org.junit.runners.Suite;
org.olat.core.util.EncoderTest.class, org.olat.core.util.EncoderTest.class,
org.olat.core.util.SimpleHtmlParserTest.class, org.olat.core.util.SimpleHtmlParserTest.class,
org.olat.core.util.IPUtilsTest.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.EmailAddressValidatorTest.class,
org.olat.core.util.mail.manager.MailManagerTest.class, org.olat.core.util.mail.manager.MailManagerTest.class,
org.olat.core.util.openxml.OpenXmlWorkbookTest.class, org.olat.core.util.openxml.OpenXmlWorkbookTest.class,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment