Skip to content

Commit 546687e

Browse files
committed
Added test case for applyAliasToKM in DefaultSslEngineFactoryTest
1 parent e97d9a4 commit 546687e

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

clients/src/main/java/org/apache/kafka/common/security/ssl/DefaultSslEngineFactory.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
import org.apache.kafka.common.config.internals.BrokerSecurityConfigs;
3838
import org.apache.kafka.common.config.types.Password;
3939
import org.apache.kafka.common.network.Mode;
40-
import org.apache.kafka.common.utils.Base64;
40+
4141
import org.apache.kafka.common.utils.Utils;
4242
import org.slf4j.Logger;
4343
import org.slf4j.LoggerFactory;
@@ -73,6 +73,8 @@
7373
import java.security.cert.CertificateFactory;
7474
import java.security.spec.InvalidKeySpecException;
7575
import java.security.spec.PKCS8EncodedKeySpec;
76+
import java.security.Principal;
77+
import java.security.cert.X509Certificate;
7678
import java.util.ArrayList;
7779
import java.util.Arrays;
7880
import java.util.Base64;

clients/src/test/java/org/apache/kafka/common/security/ssl/DefaultSslEngineFactoryTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import org.junit.jupiter.api.BeforeEach;
2424
import org.junit.jupiter.api.Test;
2525

26+
import javax.net.ssl.KeyManager;
27+
import java.lang.reflect.Method;
2628
import java.security.KeyStore;
2729
import java.util.Arrays;
2830
import java.util.Collections;
@@ -33,6 +35,7 @@
3335
import static org.junit.jupiter.api.Assertions.assertEquals;
3436
import static org.junit.jupiter.api.Assertions.assertNotNull;
3537
import static org.junit.jupiter.api.Assertions.assertNull;
38+
import static org.mockito.Mockito.mock;
3639

3740
@SuppressWarnings("this-escape")
3841
public class DefaultSslEngineFactoryTest {
@@ -324,6 +327,8 @@ private String pemFilePath(String pem) throws Exception {
324327
return TestUtils.tempFile(pem).getAbsolutePath();
325328
}
326329

330+
331+
327332
private Password pemAsConfigValue(String... pemValues) {
328333
StringBuilder builder = new StringBuilder();
329334
for (String pem : pemValues) {
@@ -332,4 +337,27 @@ private Password pemAsConfigValue(String... pemValues) {
332337
}
333338
return new Password(builder.toString().trim());
334339
}
340+
341+
@Test
342+
void testApplyAliasToKM() throws Exception {
343+
DefaultSslEngineFactory instance = new DefaultSslEngineFactory();
344+
// Mock KeyManager array
345+
KeyManager mockKeyManager = mock(KeyManager.class);
346+
KeyManager[] kms = new KeyManager[]{mockKeyManager};
347+
348+
// Define the alias
349+
String alias = "testAlias";
350+
351+
// Use reflection to access the private method
352+
Method method = DefaultSslEngineFactory.class.getDeclaredMethod("applyAliasToKM", KeyManager[].class, String.class);
353+
method.setAccessible(true);
354+
355+
// Invoke the method
356+
KeyManager[] result = (KeyManager[]) method.invoke(instance, (Object) kms, alias);
357+
358+
// Validate results (Modify based on actual method behavior)
359+
assertNotNull(result);
360+
assertEquals(1, result.length);
361+
}
362+
335363
}

0 commit comments

Comments
 (0)