From bfab03b837df218dabb151fffc37ea83c8eb348c Mon Sep 17 00:00:00 2001 From: Alexander Schwartz Date: Fri, 29 Apr 2022 17:12:26 +0200 Subject: [PATCH] Throw an IllegalArgumentException once a ClassCastException occurs. Closes #11775 --- .../MapRootAuthenticationSessionProvider.java | 2 +- .../map/storage/chm/CriteriaOperator.java | 9 +-- .../map/storage/chm/CriteriaOperatorTest.java | 56 +++++++++++++++++++ 3 files changed, 60 insertions(+), 7 deletions(-) create mode 100644 model/map/src/test/java/org/keycloak/models/map/storage/chm/CriteriaOperatorTest.java diff --git a/model/map/src/main/java/org/keycloak/models/map/authSession/MapRootAuthenticationSessionProvider.java b/model/map/src/main/java/org/keycloak/models/map/authSession/MapRootAuthenticationSessionProvider.java index 1a2ee52629..103807ba32 100644 --- a/model/map/src/main/java/org/keycloak/models/map/authSession/MapRootAuthenticationSessionProvider.java +++ b/model/map/src/main/java/org/keycloak/models/map/authSession/MapRootAuthenticationSessionProvider.java @@ -145,7 +145,7 @@ public class MapRootAuthenticationSessionProvider implements AuthenticationSessi DefaultModelCriteria mcb = criteria(); mcb = mcb.compare(SearchableFields.REALM_ID, Operator.EQ, realm.getId()) - .compare(SearchableFields.EXPIRATION, Operator.LT, Time.currentTime()); + .compare(SearchableFields.EXPIRATION, Operator.LT, (long) Time.currentTime()); long deletedCount = tx.delete(withCriteria(mcb)); diff --git a/model/map/src/main/java/org/keycloak/models/map/storage/chm/CriteriaOperator.java b/model/map/src/main/java/org/keycloak/models/map/storage/chm/CriteriaOperator.java index 69764d9fe8..7025107d2b 100644 --- a/model/map/src/main/java/org/keycloak/models/map/storage/chm/CriteriaOperator.java +++ b/model/map/src/main/java/org/keycloak/models/map/storage/chm/CriteriaOperator.java @@ -16,6 +16,7 @@ */ package org.keycloak.models.map.storage.chm; +import org.jboss.logging.Logger; import org.keycloak.models.map.storage.ModelCriteriaBuilder.Operator; import java.util.Arrays; import java.util.Collection; @@ -24,11 +25,8 @@ import java.util.EnumMap; import java.util.EnumSet; import java.util.HashSet; import java.util.Objects; -import java.util.Set; import java.util.function.Function; import java.util.function.Predicate; -import java.util.logging.Level; -import java.util.logging.Logger; import java.util.regex.Pattern; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -70,7 +68,7 @@ class CriteriaOperator { /** * Returns a predicate {@code P(x)} for comparing {@code value} and {@code x} as {@code x OP value}. * Implementation note: Note that this may mean reverse logic to e.g. {@link Comparable#compareTo}. - * @param operator + * @param op * @param value * @return */ @@ -261,8 +259,7 @@ class CriteriaOperator { try { return o != null && op.isComparisonTrue(cValue.compareTo(o)); } catch (ClassCastException ex) { - LOG.log(Level.WARNING, "Incomparable argument type for comparison operation: {0}", cValue.getClass().getSimpleName()); - return false; + throw new IllegalArgumentException("Incomparable argument type for comparison operation: " + cValue.getClass().getSimpleName() + " vs. " + o.getClass().getSimpleName(), ex); } } diff --git a/model/map/src/test/java/org/keycloak/models/map/storage/chm/CriteriaOperatorTest.java b/model/map/src/test/java/org/keycloak/models/map/storage/chm/CriteriaOperatorTest.java new file mode 100644 index 0000000000..5b7fdaa738 --- /dev/null +++ b/model/map/src/test/java/org/keycloak/models/map/storage/chm/CriteriaOperatorTest.java @@ -0,0 +1,56 @@ +/* + * Copyright 2022. Red Hat, Inc. and/or its affiliates + * and other contributors as indicated by the @author tags. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.keycloak.models.map.storage.chm; + +import org.hamcrest.CoreMatchers; +import org.hamcrest.MatcherAssert; +import org.junit.Test; + +import java.util.function.Predicate; + +import static org.junit.Assert.assertThrows; +import static org.junit.Assert.assertTrue; + +/** + * @author Alexander Schwartz + */ +public class CriteriaOperatorTest { + + @Test + public void shouldCompareLongs() { + Predicate predicate = CriteriaOperator.ge(new Long[]{0L}); + + assertTrue(predicate.test(1L)); + } + + @Test + public void shouldCompareInts() { + Predicate predicate = CriteriaOperator.ge(new Integer[]{0}); + + assertTrue(predicate.test(1)); + } + + @Test + public void shouldThrowAnExceptionOnIncompatibleTypes() { + Predicate predicate = CriteriaOperator.ge(new Long[]{0L}); + + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> predicate.test(1)); + MatcherAssert.assertThat(exception.getMessage(), CoreMatchers.startsWith("Incomparable argument type for comparison operation")); + } + +} \ No newline at end of file