|
| 1 | +/* |
| 2 | + * Copyright 2014-2022 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.session.aot.hint; |
| 18 | + |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.TreeSet; |
| 21 | +import java.util.stream.Stream; |
| 22 | + |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | +import org.junit.jupiter.params.ParameterizedTest; |
| 25 | +import org.junit.jupiter.params.provider.MethodSource; |
| 26 | + |
| 27 | +import org.springframework.aot.hint.RuntimeHints; |
| 28 | +import org.springframework.aot.hint.RuntimeHintsRegistrar; |
| 29 | +import org.springframework.aot.hint.TypeReference; |
| 30 | +import org.springframework.core.io.support.SpringFactoriesLoader; |
| 31 | +import org.springframework.security.authentication.AbstractAuthenticationToken; |
| 32 | +import org.springframework.security.authentication.AccountExpiredException; |
| 33 | +import org.springframework.security.authentication.AuthenticationServiceException; |
| 34 | +import org.springframework.security.authentication.BadCredentialsException; |
| 35 | +import org.springframework.security.authentication.CredentialsExpiredException; |
| 36 | +import org.springframework.security.authentication.DisabledException; |
| 37 | +import org.springframework.security.authentication.InsufficientAuthenticationException; |
| 38 | +import org.springframework.security.authentication.LockedException; |
| 39 | +import org.springframework.security.authentication.ProviderNotFoundException; |
| 40 | +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; |
| 41 | +import org.springframework.security.core.AuthenticationException; |
| 42 | +import org.springframework.security.core.authority.SimpleGrantedAuthority; |
| 43 | +import org.springframework.security.core.context.SecurityContextImpl; |
| 44 | +import org.springframework.security.core.userdetails.User; |
| 45 | +import org.springframework.security.core.userdetails.UsernameNotFoundException; |
| 46 | + |
| 47 | +import static org.assertj.core.api.Assertions.assertThat; |
| 48 | + |
| 49 | +/** |
| 50 | + * Tests for {@link CommonSessionSecurityHints} |
| 51 | + * |
| 52 | + * @author Marcus Da Coregio |
| 53 | + */ |
| 54 | +class CommonSessionSecurityHintsTests { |
| 55 | + |
| 56 | + private final RuntimeHints hints = new RuntimeHints(); |
| 57 | + |
| 58 | + private final CommonSessionSecurityHints commonSessionSecurityHints = new CommonSessionSecurityHints(); |
| 59 | + |
| 60 | + @ParameterizedTest |
| 61 | + @MethodSource("getSerializationHintTypes") |
| 62 | + void coreTypesHasHints(TypeReference typeReference) { |
| 63 | + this.commonSessionSecurityHints.registerHints(this.hints, getClass().getClassLoader()); |
| 64 | + assertThat(new SerializationHintsPredicates().onType(typeReference)).accepts(this.hints); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + void aotFactoriesContainsRegistrar() { |
| 69 | + boolean match = SpringFactoriesLoader.forResourceLocation("META-INF/spring/aot.factories") |
| 70 | + .load(RuntimeHintsRegistrar.class).stream() |
| 71 | + .anyMatch((registrar) -> registrar instanceof CommonSessionSecurityHints); |
| 72 | + assertThat(match).isTrue(); |
| 73 | + } |
| 74 | + |
| 75 | + private static Stream<TypeReference> getSerializationHintTypes() { |
| 76 | + return Stream.of(TypeReference.of(String.class), TypeReference.of(ArrayList.class), |
| 77 | + TypeReference.of(TreeSet.class), TypeReference.of(SecurityContextImpl.class), |
| 78 | + TypeReference.of(SimpleGrantedAuthority.class), TypeReference.of(User.class), |
| 79 | + TypeReference.of(Number.class), TypeReference.of(Long.class), TypeReference.of(Integer.class), |
| 80 | + TypeReference.of(AbstractAuthenticationToken.class), |
| 81 | + TypeReference.of(UsernamePasswordAuthenticationToken.class), TypeReference.of(StackTraceElement.class), |
| 82 | + TypeReference.of(Throwable.class), TypeReference.of(Exception.class), |
| 83 | + TypeReference.of(RuntimeException.class), TypeReference.of(AuthenticationException.class), |
| 84 | + TypeReference.of(BadCredentialsException.class), TypeReference.of(UsernameNotFoundException.class), |
| 85 | + TypeReference.of(AccountExpiredException.class), TypeReference.of(ProviderNotFoundException.class), |
| 86 | + TypeReference.of(DisabledException.class), TypeReference.of(LockedException.class), |
| 87 | + TypeReference.of(AuthenticationServiceException.class), |
| 88 | + TypeReference.of(CredentialsExpiredException.class), |
| 89 | + TypeReference.of(InsufficientAuthenticationException.class), |
| 90 | + TypeReference |
| 91 | + .of("org.springframework.security.web.authentication.session.SessionAuthenticationException"), |
| 92 | + TypeReference.of( |
| 93 | + "org.springframework.security.web.authentication.rememberme.RememberMeAuthenticationException"), |
| 94 | + TypeReference.of("java.util.Collections$UnmodifiableCollection"), |
| 95 | + TypeReference.of("java.util.Collections$UnmodifiableList"), |
| 96 | + TypeReference.of("java.util.Collections$EmptyList"), |
| 97 | + TypeReference.of("java.util.Collections$UnmodifiableRandomAccessList"), |
| 98 | + TypeReference.of("java.util.Collections$UnmodifiableSet"), |
| 99 | + TypeReference.of("org.springframework.security.core.userdetails.User$AuthorityComparator"), |
| 100 | + TypeReference.of("org.springframework.security.oauth2.server.resource.BearerTokenAuthenticationToken"), |
| 101 | + TypeReference.of( |
| 102 | + "org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken"), |
| 103 | + TypeReference.of("org.springframework.security.oauth2.core.OAuth2AuthenticationException"), |
| 104 | + TypeReference.of("org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken"), |
| 105 | + TypeReference |
| 106 | + .of("org.springframework.security.oauth2.client.authentication.OAuth2LoginAuthenticationToken"), |
| 107 | + TypeReference.of( |
| 108 | + "org.springframework.security.oauth2.client.authentication.OAuth2AuthorizationCodeAuthenticationToken"), |
| 109 | + TypeReference.of("org.springframework.security.oauth2.core.OAuth2AuthenticationException")); |
| 110 | + } |
| 111 | + |
| 112 | +} |
0 commit comments