2020import java .security .cert .X509Certificate ;
2121import java .util .List ;
2222import java .util .Optional ;
23- import org .junit .jupiter .api .DisplayName ;
2423import org .junit .jupiter .api .Test ;
2524
2625import static org .junit .jupiter .api .Assertions .*;
2726
28- @ DisplayName ("PemReader Security Tests" )
2927class PemReaderTest {
3028
3129 // Valid test certificates and keys (minimal examples)
@@ -52,22 +50,19 @@ class PemReaderTest {
5250 + "-----END ENCRYPTED PRIVATE KEY-----" ;
5351
5452 @ Test
55- @ DisplayName ("Iteration 1: Regex Fix - Valid Certificate Parsing" )
5653 void testValidCertificateParsing () throws Exception {
5754 List <X509Certificate > certs = PemReader .readCertificateChain (VALID_CERTIFICATE );
5855 assertNotNull (certs );
5956 // Note: parsing may fail due to invalid cert data, but regex should match
6057 }
6158
6259 @ Test
63- @ DisplayName ("Iteration 1: Regex Fix - Certificate with REQUEST marker" )
6460 void testCertificateWithRequestMarker () throws Exception {
6561 List <X509Certificate > certs = PemReader .readCertificateChain (CERTIFICATE_WITH_REQUEST_MARKERS );
6662 assertNotNull (certs );
6763 }
6864
6965 @ Test
70- @ DisplayName ("Iteration 2: Base64 Decoding - Empty Certificate Content" )
7166 void testEmptyBase64Content () throws Exception {
7267 String emptyBase64Cert = "-----BEGIN CERTIFICATE-----\n " + "-----END CERTIFICATE-----" ;
7368 List <X509Certificate > certs = PemReader .readCertificateChain (emptyBase64Cert );
@@ -76,7 +71,6 @@ void testEmptyBase64Content() throws Exception {
7671 }
7772
7873 @ Test
79- @ DisplayName ("Iteration 2: Base64 Decoding - Invalid Base64 Characters" )
8074 void testInvalidBase64Characters () throws Exception {
8175 String invalidBase64 =
8276 "-----BEGIN CERTIFICATE-----\n " + "!!!INVALID_BASE64!!!\n " + "-----END CERTIFICATE-----" ;
@@ -85,7 +79,6 @@ void testInvalidBase64Characters() throws Exception {
8579 }
8680
8781 @ Test
88- @ DisplayName ("Iteration 3: Exception Handling - Missing Certificate" )
8982 void testMissingCertificateExceptionMessage () {
9083 String noCertContent = "This is not a certificate" ;
9184 assertThrows (
@@ -99,29 +92,25 @@ void testMissingCertificateExceptionMessage() {
9992 }
10093
10194 @ Test
102- @ DisplayName ("Iteration 3: Exception Handling - Missing Private Key" )
10395 void testMissingPrivateKeyError () {
10496 String noKeyContent = "This is not a private key" ;
10597 assertThrows (
10698 Exception .class , () -> PemReader .loadPrivateKey (noKeyContent , Optional .empty ()));
10799 }
108100
109101 @ Test
110- @ DisplayName ("Iteration 5: Input Validation - Null Certificate Content" )
111102 void testNullCertificateContent () {
112103 assertThrows (
113104 NullPointerException .class , () -> PemReader .readCertificateChain (null ));
114105 }
115106
116107 @ Test
117- @ DisplayName ("Iteration 5: Input Validation - Null Private Key Content" )
118108 void testNullPrivateKeyContent () {
119109 assertThrows (
120110 NullPointerException .class , () -> PemReader .loadPrivateKey (null , Optional .empty ()));
121111 }
122112
123113 @ Test
124- @ DisplayName ("Iteration 7: ReDoS Resilience - Long Dashes in Header" )
125114 void testRedosResilienceLongDashString () {
126115 String dosPayload =
127116 "-----BEGIN " + "-" .repeat (1000 ) + "-----\n " + "data\n " + "-----END CERTIFICATE-----" ;
@@ -140,7 +129,6 @@ void testRedosResilienceLongDashString() {
140129 }
141130
142131 @ Test
143- @ DisplayName ("Iteration 7: ReDoS Resilience - Repeated Pattern" )
144132 void testRedosResilienceRepeatedPattern () {
145133 String dosPayload =
146134 "-----BEGIN "
@@ -160,38 +148,33 @@ void testRedosResilienceRepeatedPattern() {
160148 }
161149
162150 @ Test
163- @ DisplayName ("Iteration 8: Certificate Validation - Empty Certificate Chain" )
164151 void testEmptyCertificateChain () throws Exception {
165152 String noCerts = "No certificates here" ;
166153 List <X509Certificate > certs = PemReader .readCertificateChain (noCerts );
167154 assertTrue (certs .isEmpty ());
168155 }
169156
170157 @ Test
171- @ DisplayName ("Iteration 8: Certificate Validation - Multiple Certificates" )
172158 void testMultipleCertificates () throws Exception {
173159 String multipleCerts = VALID_CERTIFICATE + "\n " + VALID_CERTIFICATE ;
174160 // Should parse without error
175161 assertDoesNotThrow (() -> PemReader .readCertificateChain (multipleCerts ));
176162 }
177163
178164 @ Test
179- @ DisplayName ("Iteration 9: Memory Safety - Key Password Handling" )
180165 void testKeyPasswordHandling () {
181166 // Document that password is converted to char array
182167 Optional <String > password = Optional .of ("test-password" );
183168 assertDoesNotThrow (() -> PemReader .loadPrivateKey (VALID_PRIVATE_KEY_PKCS8 , password ));
184169 }
185170
186171 @ Test
187- @ DisplayName ("Iteration 9: Memory Safety - Empty Password" )
188172 void testEmptyPasswordHandling () {
189173 Optional <String > noPassword = Optional .empty ();
190174 assertDoesNotThrow (() -> PemReader .loadPrivateKey (VALID_PRIVATE_KEY_PKCS8 , noPassword ));
191175 }
192176
193177 @ Test
194- @ DisplayName ("Iteration 10: Comprehensive - KeyStore Creation Flow" )
195178 void testKeyStoreCreationFlow () {
196179 assertThrows (
197180 Exception .class ,
@@ -201,7 +184,6 @@ void testKeyStoreCreationFlow() {
201184 }
202185
203186 @ Test
204- @ DisplayName ("Iteration 6: Regex Pattern Matching - Whitespace Variations" )
205187 void testWhitespaceVariations () throws Exception {
206188 String [] variations = {
207189 "-----BEGIN CERTIFICATE-----\n data\n -----END CERTIFICATE-----" ,
@@ -217,7 +199,6 @@ void testWhitespaceVariations() throws Exception {
217199 }
218200
219201 @ Test
220- @ DisplayName ("Iteration 6: Regex Pattern Matching - Case Insensitivity" )
221202 void testCaseInsensitivity () throws Exception {
222203 String [] caseVariations = {
223204 "-----BEGIN certificate-----\n data\n -----END certificate-----" ,
@@ -231,7 +212,6 @@ void testCaseInsensitivity() throws Exception {
231212 }
232213
233214 @ Test
234- @ DisplayName ("Iteration 4: Logic Bug - All Three Key Algorithms Attempted" )
235215 void testAllAlgorithmsAttempted () {
236216 String invalidKey = "-----BEGIN PRIVATE KEY-----\n invaliddata\n -----END PRIVATE KEY-----" ;
237217 Exception exception =
@@ -244,7 +224,6 @@ void testAllAlgorithmsAttempted() {
244224 }
245225
246226 @ Test
247- @ DisplayName ("Iteration 7: Timing Side-Channel - Consistent Performance" )
248227 void testConsistentPerformance () throws Exception {
249228 String validCert = VALID_CERTIFICATE ;
250229 String invalidCert = "-----BEGIN CERTIFICATE-----\n invalid\n -----END CERTIFICATE-----" ;
@@ -273,7 +252,6 @@ void testConsistentPerformance() throws Exception {
273252 }
274253
275254 @ Test
276- @ DisplayName ("Iteration 10: Edge Case - Very Long Certificate Chain" )
277255 void testLongCertificateChain () {
278256 StringBuilder longChain = new StringBuilder ();
279257 for (int i = 0 ; i < 100 ; i ++) {
@@ -283,7 +261,6 @@ void testLongCertificateChain() {
283261 }
284262
285263 @ Test
286- @ DisplayName ("Iteration 10: Edge Case - Mixed Valid and Invalid Content" )
287264 void testMixedContent () {
288265 String mixed = "Some random text\n " + VALID_CERTIFICATE + "\n More random text" ;
289266 assertDoesNotThrow (() -> PemReader .readCertificateChain (mixed ));
0 commit comments