Prevent NPE in CryptoIntegration.setProvider

Closes #38596

Signed-off-by: Leonid Rozenblyum <lrozenblyum@gmail.com>
This commit is contained in:
lrozenblyum 2025-04-03 11:06:00 +03:00 committed by GitHub
parent 50fef70f14
commit a0852eaa2e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 37 additions and 1 deletions

View File

@ -94,7 +94,7 @@ public class CryptoIntegration {
}
public static void setProvider(CryptoProvider provider) {
logger.debugf("Using the crypto provider: %s", provider.getClass().getName());
logger.debugf("Using the crypto provider: %s", provider != null ? provider.getClass().getName() : "null");
cryptoProvider = provider;
}
}

View File

@ -0,0 +1,36 @@
package org.keycloak.common.crypto;
import static org.junit.Assert.assertNull;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
public class CryptoIntegrationTest {
private static CryptoProvider originalProvider;
@BeforeClass
public static void keepOriginalProvider() {
CryptoIntegrationTest.originalProvider = getSelectedProvider();
}
// doing our best to avoid any side effects on other tests by restoring the initial state of CryptoIntegration
@AfterClass
public static void restoreOriginalProvider() {
CryptoIntegration.setProvider(originalProvider);
}
@Test
public void canSetNullProvider() {
CryptoIntegration.setProvider(null);
assertNull(getSelectedProvider());
}
private static CryptoProvider getSelectedProvider() {
try {
return CryptoIntegration.getProvider();
} catch (IllegalStateException e) {
return null;
}
}
}