Avoid exception if PBKDF2WithHmacSHA256 is not available

Issue gh-12873
This commit is contained in:
Marcus Da Coregio
2023-04-03 14:37:42 -03:00
parent a513fc0f38
commit d5603a944d
3 changed files with 46 additions and 3 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,11 +16,17 @@
package org.springframework.security.crypto.factory;
import java.security.NoSuchAlgorithmException;
import org.junit.jupiter.api.Test;
import org.mockito.MockedStatic;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.crypto.password.Pbkdf2PasswordEncoder;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatNoException;
import static org.mockito.Mockito.mockStatic;
/**
* @author Rob Winch
@@ -123,4 +129,14 @@ public class PasswordEncoderFactoriesTests {
assertThat(this.encoder.matches(this.rawPassword, encodedPassword)).isTrue();
}
@Test
void constructWhenAlgorithmNotAvailableThenSkip() {
try (MockedStatic<Pbkdf2PasswordEncoder> pbkdf2PasswordEncoderMock = mockStatic(Pbkdf2PasswordEncoder.class)) {
pbkdf2PasswordEncoderMock.when(Pbkdf2PasswordEncoder::defaultsForSpringSecurity_v5_8)
.thenThrow(new IllegalArgumentException(new NoSuchAlgorithmException()));
assertThatNoException().isThrownBy(PasswordEncoderFactories::createDelegatingPasswordEncoder);
}
}
}