Skip to content

Commit 65d53be

Browse files
committed
Polish Serialization Tests
- Error when public, non-ignored, serializable file is missing a sample - Provide mechanism for creating an InstancioApi from scratch Issue gh-17038
1 parent 34afa64 commit 65d53be

File tree

1 file changed

+36
-2
lines changed

1 file changed

+36
-2
lines changed

config/src/test/java/org/springframework/security/SpringSecurityCoreVersionSerializableTests.java

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.security;
1818

19+
import java.io.ByteArrayInputStream;
20+
import java.io.ByteArrayOutputStream;
1921
import java.io.File;
2022
import java.io.FileInputStream;
2123
import java.io.FileOutputStream;
@@ -36,13 +38,17 @@
3638
import java.util.Collection;
3739
import java.util.Date;
3840
import java.util.HashMap;
41+
import java.util.HashSet;
3942
import java.util.List;
4043
import java.util.Locale;
4144
import java.util.Map;
4245
import java.util.Set;
46+
import java.util.UUID;
47+
import java.util.function.Supplier;
4348
import java.util.stream.Stream;
4449

4550
import jakarta.servlet.http.Cookie;
51+
import org.apache.commons.lang3.ObjectUtils;
4652
import org.apereo.cas.client.validation.AssertionImpl;
4753
import org.instancio.Instancio;
4854
import org.instancio.InstancioApi;
@@ -63,6 +69,7 @@
6369
import org.springframework.security.access.AccessDeniedException;
6470
import org.springframework.security.access.AuthorizationServiceException;
6571
import org.springframework.security.access.SecurityConfig;
72+
import org.springframework.security.access.hierarchicalroles.CycleInRoleHierarchyException;
6673
import org.springframework.security.access.intercept.RunAsUserToken;
6774
import org.springframework.security.authentication.AbstractAuthenticationToken;
6875
import org.springframework.security.authentication.AccountExpiredException;
@@ -94,15 +101,19 @@
94101
import org.springframework.security.authentication.jaas.JaasAuthenticationToken;
95102
import org.springframework.security.authentication.jaas.event.JaasAuthenticationFailedEvent;
96103
import org.springframework.security.authentication.jaas.event.JaasAuthenticationSuccessEvent;
104+
import org.springframework.security.authentication.ott.DefaultOneTimeToken;
97105
import org.springframework.security.authentication.ott.InvalidOneTimeTokenException;
98106
import org.springframework.security.authentication.ott.OneTimeTokenAuthenticationToken;
99107
import org.springframework.security.authentication.password.CompromisedPasswordException;
100108
import org.springframework.security.authorization.AuthorityAuthorizationDecision;
101109
import org.springframework.security.authorization.AuthorizationDecision;
102110
import org.springframework.security.authorization.AuthorizationDeniedException;
111+
import org.springframework.security.authorization.event.AuthorizationEvent;
112+
import org.springframework.security.authorization.event.AuthorizationGrantedEvent;
103113
import org.springframework.security.cas.authentication.CasAssertionAuthenticationToken;
104114
import org.springframework.security.cas.authentication.CasAuthenticationToken;
105115
import org.springframework.security.cas.authentication.CasServiceTicketAuthenticationToken;
116+
import org.springframework.security.config.annotation.AlreadyBuiltException;
106117
import org.springframework.security.core.Authentication;
107118
import org.springframework.security.core.GrantedAuthority;
108119
import org.springframework.security.core.SpringSecurityCoreVersion;
@@ -128,11 +139,14 @@
128139
import org.springframework.security.oauth2.client.authentication.OAuth2LoginAuthenticationToken;
129140
import org.springframework.security.oauth2.client.authentication.TestOAuth2AuthenticationTokens;
130141
import org.springframework.security.oauth2.client.authentication.TestOAuth2AuthorizationCodeAuthenticationTokens;
142+
import org.springframework.security.oauth2.client.event.OAuth2AuthorizedClientRefreshedEvent;
143+
import org.springframework.security.oauth2.client.oidc.authentication.event.OidcUserRefreshedEvent;
131144
import org.springframework.security.oauth2.client.oidc.authentication.logout.OidcLogoutToken;
132145
import org.springframework.security.oauth2.client.oidc.authentication.logout.TestOidcLogoutTokens;
133146
import org.springframework.security.oauth2.client.oidc.session.OidcSessionInformation;
134147
import org.springframework.security.oauth2.client.oidc.session.TestOidcSessionInformations;
135148
import org.springframework.security.oauth2.client.registration.ClientRegistration;
149+
import org.springframework.security.oauth2.client.registration.ClientRegistration.ClientSettings;
136150
import org.springframework.security.oauth2.client.registration.TestClientRegistrations;
137151
import org.springframework.security.oauth2.core.DefaultOAuth2AuthenticatedPrincipal;
138152
import org.springframework.security.oauth2.core.OAuth2AccessToken;
@@ -148,6 +162,7 @@
148162
import org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationExchange;
149163
import org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest;
150164
import org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationResponse;
165+
import org.springframework.security.oauth2.core.endpoint.TestOAuth2AccessTokenResponses;
151166
import org.springframework.security.oauth2.core.endpoint.TestOAuth2AuthorizationExchanges;
152167
import org.springframework.security.oauth2.core.endpoint.TestOAuth2AuthorizationRequests;
153168
import org.springframework.security.oauth2.core.endpoint.TestOAuth2AuthorizationResponses;
@@ -172,6 +187,7 @@
172187
import org.springframework.security.oauth2.server.resource.InvalidBearerTokenException;
173188
import org.springframework.security.oauth2.server.resource.authentication.BearerTokenAuthentication;
174189
import org.springframework.security.oauth2.server.resource.authentication.BearerTokenAuthenticationToken;
190+
import org.springframework.security.oauth2.server.resource.authentication.DPoPAuthenticationToken;
175191
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken;
176192
import org.springframework.security.oauth2.server.resource.introspection.BadOpaqueTokenException;
177193
import org.springframework.security.oauth2.server.resource.introspection.OAuth2IntrospectionAuthenticatedPrincipal;
@@ -188,8 +204,10 @@
188204
import org.springframework.security.saml2.provider.service.authentication.Saml2RedirectAuthenticationRequest;
189205
import org.springframework.security.saml2.provider.service.authentication.TestSaml2AuthenticationTokens;
190206
import org.springframework.security.saml2.provider.service.authentication.TestSaml2Authentications;
207+
import org.springframework.security.saml2.provider.service.authentication.TestSaml2LogoutRequests;
191208
import org.springframework.security.saml2.provider.service.authentication.TestSaml2PostAuthenticationRequests;
192209
import org.springframework.security.saml2.provider.service.authentication.TestSaml2RedirectAuthenticationRequests;
210+
import org.springframework.security.saml2.provider.service.authentication.logout.Saml2LogoutRequest;
193211
import org.springframework.security.saml2.provider.service.registration.RelyingPartyRegistration;
194212
import org.springframework.security.saml2.provider.service.registration.RelyingPartyRegistration.AssertingPartyDetails;
195213
import org.springframework.security.saml2.provider.service.registration.TestRelyingPartyRegistrations;
@@ -213,6 +231,7 @@
213231
import org.springframework.security.web.savedrequest.SimpleSavedRequest;
214232
import org.springframework.security.web.server.firewall.ServerExchangeRejectedException;
215233
import org.springframework.security.web.session.HttpSessionCreatedEvent;
234+
import org.springframework.security.web.session.HttpSessionIdChangedEvent;
216235
import org.springframework.security.web.webauthn.api.AuthenticationExtensionsClientInputs;
217236
import org.springframework.security.web.webauthn.api.AuthenticationExtensionsClientOutputs;
218237
import org.springframework.security.web.webauthn.api.AuthenticatorAssertionResponse;
@@ -234,7 +253,9 @@
234253
import org.springframework.security.web.webauthn.api.TestBytes;
235254
import org.springframework.security.web.webauthn.api.TestPublicKeyCredential;
236255
import org.springframework.security.web.webauthn.api.TestPublicKeyCredentialRequestOptions;
256+
import org.springframework.security.web.webauthn.api.TestPublicKeyCredentialUserEntities;
237257
import org.springframework.security.web.webauthn.api.TestPublicKeyCredentialUserEntity;
258+
import org.springframework.security.web.webauthn.api.TestPublicKeyCredentials;
238259
import org.springframework.security.web.webauthn.api.UserVerificationRequirement;
239260
import org.springframework.security.web.webauthn.authentication.WebAuthnAuthentication;
240261
import org.springframework.security.web.webauthn.authentication.WebAuthnAuthenticationRequestToken;
@@ -262,6 +283,8 @@ class SpringSecurityCoreVersionSerializableTests {
262283

263284
private static final Map<Class<?>, Generator<?>> generatorByClassName = new HashMap<>();
264285

286+
private static final Map<Class<?>, Supplier<InstancioApi<?>>> instancioByClassName = new HashMap<>();
287+
265288
static final long securitySerialVersionUid = SpringSecurityCoreVersion.SERIAL_VERSION_UID;
266289

267290
static Path currentVersionFolder = Paths.get("src/test/resources/serialized/" + getCurrentVersion());
@@ -766,10 +789,18 @@ void allSerializableClassesShouldHaveSerialVersionOrSuppressWarnings() throws Ex
766789
|| Arrays.asList(suppressWarnings.value()).contains("Serial");
767790
if (!hasSerialVersion && !hasSerialIgnore) {
768791
classes.add(clazz);
792+
continue;
793+
}
794+
boolean isReachable = Modifier.isPublic(clazz.getModifiers());
795+
boolean hasSampleSerialization = currentVersionFolder.resolve(clazz.getName() + ".serialized")
796+
.toFile()
797+
.exists();
798+
if (hasSerialVersion && isReachable && !hasSampleSerialization) {
799+
classes.add(clazz);
769800
}
770801
}
771-
assertThat(classes)
772-
.describedAs("Found Serializable classes that are either missing a serialVersionUID or a @SuppressWarnings")
802+
assertThat(classes).describedAs(
803+
"Found Serializable classes that are either missing a serialVersionUID or a @SuppressWarnings or a sample serialized file")
773804
.isEmpty();
774805
}
775806

@@ -796,6 +827,9 @@ static Stream<Class<?>> getClassesToSerialize() throws Exception {
796827
}
797828

798829
private static InstancioApi<?> instancioWithDefaults(Class<?> clazz) {
830+
if (instancioByClassName.containsKey(clazz)) {
831+
return instancioByClassName.get(clazz).get();
832+
}
799833
InstancioOfClassApi<?> instancio = Instancio.of(clazz);
800834
ResolvableType[] generics = ResolvableType.forClass(clazz).getGenerics();
801835
for (ResolvableType type : generics) {

0 commit comments

Comments
 (0)