|
| 1 | +/* |
| 2 | + * Copyright 2025, AutoMQ HK Limited. |
| 3 | + * |
| 4 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 5 | + * contributor license agreements. See the NOTICE file distributed with |
| 6 | + * this work for additional information regarding copyright ownership. |
| 7 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 8 | + * (the "License"); you may not use this file except in compliance with |
| 9 | + * the License. You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.kafka.server.metrics.s3stream; |
| 21 | + |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | + |
| 24 | +import java.lang.reflect.Method; |
| 25 | +import java.security.cert.X509Certificate; |
| 26 | + |
| 27 | +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; |
| 28 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 29 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 30 | + |
| 31 | +public class S3StreamKafkaMetricsManagerTest { |
| 32 | + |
| 33 | + @Test |
| 34 | + public void testParseCertificatesWithEmptyString() throws Exception { |
| 35 | + X509Certificate[] certificates = callParseCertificates(""); |
| 36 | + |
| 37 | + assertNotNull(certificates); |
| 38 | + assertEquals(0, certificates.length); |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + public void testParseCertificatesWithWhitespaceInBase64() throws Exception { |
| 43 | + // Test certificate with whitespace in Base64 content that would cause "Illegal base64 character 20" error |
| 44 | + String certWithSpaces = "-----BEGIN CERTIFICATE-----\n" + |
| 45 | + "TUlJQmtUQ0IrUFNKQnFaUUhpUWxDd0ZBTUJReEVqQVFCZ05W" + // base64 line with spaces |
| 46 | + " QkFNTUNXeHZZMkZzYUc5emREQWVGdzB5TlRFd01qbHhNREF3TUZG\n" + // Leading space |
| 47 | + "QUFNVUNXeHZZMkZzYUc5emREQWVGdzB5TlRFd01qbHhNREF3\t" + // Trailing tab |
| 48 | + "TUZGUUFNVUNXeHZZMG\r\n" + // Carriage return + newline |
| 49 | + "-----END CERTIFICATE-----"; |
| 50 | + |
| 51 | + // This should not throw IllegalArgumentException due to the fix |
| 52 | + assertDoesNotThrow(() -> { |
| 53 | + X509Certificate[] certificates = callParseCertificates(certWithSpaces); |
| 54 | + assertNotNull(certificates); |
| 55 | + // The certificate might not be valid (just test data), but at least it shouldn't crash with Base64 error |
| 56 | + }); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void testParseCertificatesWithInvalidBase64() throws Exception { |
| 61 | + String invalidCert = "-----BEGIN CERTIFICATE-----\n" + |
| 62 | + "InvalidBase64Content!!!\n" + |
| 63 | + "-----END CERTIFICATE-----"; |
| 64 | + |
| 65 | + // Should not throw exception but return empty array due to graceful error handling |
| 66 | + assertDoesNotThrow(() -> { |
| 67 | + X509Certificate[] certificates = callParseCertificates(invalidCert); |
| 68 | + assertNotNull(certificates); |
| 69 | + assertEquals(0, certificates.length); // Invalid cert should be skipped |
| 70 | + }); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Helper method to call the private parseCertificates method using reflection |
| 75 | + */ |
| 76 | + private X509Certificate[] callParseCertificates(String pemContent) throws Exception { |
| 77 | + Method method = S3StreamKafkaMetricsManager.class.getDeclaredMethod("parseCertificates", String.class); |
| 78 | + method.setAccessible(true); |
| 79 | + return (X509Certificate[]) method.invoke(null, pemContent); |
| 80 | + } |
| 81 | +} |
0 commit comments