99import java .util .Base64 ;
1010
1111import org .junit .jupiter .api .Test ;
12+ import org .junit .jupiter .api .extension .ExtendWith ;
1213import org .junit .jupiter .api .io .TempDir ;
14+ import org .springframework .boot .test .system .CapturedOutput ;
15+ import org .springframework .boot .test .system .OutputCaptureExtension ;
1316
17+ @ ExtendWith (OutputCaptureExtension .class )
1418class KeyGeneratorTest {
1519
1620 @ TempDir
@@ -24,17 +28,19 @@ void testGenerateKeysProducesValidKeys() throws NoSuchAlgorithmException, IOExce
2428 KeyGenerator .generateKeys (privateKeyPath .toString (), publicKeyPath .toString ());
2529
2630 String pubKeyContent = Files .readString (publicKeyPath );
27- assertTrue (pubKeyContent .contains ("-----BEGIN PUBLIC KEY-----" ), "Public key should start with PEM header" );
28- assertTrue (pubKeyContent .contains ("-----END PUBLIC KEY-----" ), "Public key should end with PEM footer" );
31+ assertTrue (pubKeyContent .contains ("-----BEGIN PUBLIC KEY-----" ),
32+ "Public key should start with PEM header" );
33+ assertTrue (pubKeyContent .contains ("-----END PUBLIC KEY-----" ),
34+ "Public key should end with PEM footer" );
2935
3036 String privKeyContent = Files .readString (privateKeyPath );
31- assertTrue (privKeyContent .contains ("-----BEGIN PRIVATE KEY-----" ), "Private key should start with PEM header" );
32- assertTrue (privKeyContent .contains ("-----END PRIVATE KEY-----" ), "Private key should end with PEM footer" );
37+ assertTrue (privKeyContent .contains ("-----BEGIN PRIVATE KEY-----" ),
38+ "Private key should start with PEM header" );
39+ assertTrue (privKeyContent .contains ("-----END PRIVATE KEY-----" ),
40+ "Private key should end with PEM footer" );
3341
34- String pubKeyBase64 = pubKeyContent
35- .replace ("-----BEGIN PUBLIC KEY-----" , "" )
36- .replace ("-----END PUBLIC KEY-----" , "" )
37- .replaceAll ("\\ s" , "" );
42+ String pubKeyBase64 = pubKeyContent .replace ("-----BEGIN PUBLIC KEY-----" , "" )
43+ .replace ("-----END PUBLIC KEY-----" , "" ).replaceAll ("\\ s" , "" );
3844 byte [] pubKeyBytes = Base64 .getDecoder ().decode (pubKeyBase64 );
3945 assertTrue (pubKeyBytes .length > 0 , "Decoded public key should not be empty" );
4046 }
@@ -48,33 +54,42 @@ void testGenerateKeysDoesNotOverwriteExistingKeys() throws NoSuchAlgorithmExcept
4854
4955 KeyGenerator .generateKeys (privateKeyPath .toString (), publicKeyPath .toString ());
5056
51- assertEquals ("dummy private key" , Files .readString (privateKeyPath ), "Private key file should not be overwritten" );
52- assertEquals ("dummy public key" , Files .readString (publicKeyPath ), "Public key file should not be overwritten" );
57+ assertEquals ("dummy private key" , Files .readString (privateKeyPath ),
58+ "Private key file should not be overwritten" );
59+ assertEquals ("dummy public key" , Files .readString (publicKeyPath ),
60+ "Public key file should not be overwritten" );
5361 }
5462
5563 @ Test
56- void testGenerateKeysProducesDifferentKeysOnSeparateRuns () throws NoSuchAlgorithmException , IOException {
64+ void testGenerateKeysProducesDifferentKeysOnSeparateRuns ()
65+ throws NoSuchAlgorithmException , IOException {
5766 Path privateKeyPath1 = tempDir .resolve ("test1.key" );
5867 Path publicKeyPath1 = tempDir .resolve ("test1.pub" );
5968 Path privateKeyPath2 = tempDir .resolve ("test2.key" );
6069 Path publicKeyPath2 = tempDir .resolve ("test2.pub" );
61-
70+
6271 KeyGenerator .generateKeys (privateKeyPath1 .toString (), publicKeyPath1 .toString ());
6372 KeyGenerator .generateKeys (privateKeyPath2 .toString (), publicKeyPath2 .toString ());
6473
6574 String pubKeyContent1 = Files .readString (publicKeyPath1 );
6675 String pubKeyContent2 = Files .readString (publicKeyPath2 );
6776
68- String pubKeyBase64_1 = pubKeyContent1
69- .replace ("-----BEGIN PUBLIC KEY-----" , "" )
70- .replace ("-----END PUBLIC KEY-----" , "" )
71- .replaceAll ("\\ s" , "" );
72- String pubKeyBase64_2 = pubKeyContent2
73- .replace ("-----BEGIN PUBLIC KEY-----" , "" )
74- .replace ("-----END PUBLIC KEY-----" , "" )
75- .replaceAll ("\\ s" , "" );
77+ String pubKeyBase64_1 = pubKeyContent1 .replace ("-----BEGIN PUBLIC KEY-----" , "" )
78+ .replace ("-----END PUBLIC KEY-----" , "" ).replaceAll ("\\ s" , "" );
79+ String pubKeyBase64_2 = pubKeyContent2 .replace ("-----BEGIN PUBLIC KEY-----" , "" )
80+ .replace ("-----END PUBLIC KEY-----" , "" ).replaceAll ("\\ s" , "" );
7681
7782 assertNotEquals (pubKeyBase64_1 , pubKeyBase64_2 , "Each generated public key should be unique" );
7883
7984 }
85+
86+ @ Test
87+ void main (CapturedOutput output ) throws IOException {
88+ Files .delete (Path .of ("src/main/resources/app.key" ));
89+ Files .delete (Path .of ("src/main/resources/app.pub" ));
90+ KeyGenerator .main (null );
91+ assertTrue (output .toString ().contains ("Keys Generated" ));
92+ KeyGenerator .main (null );
93+ assertTrue (output .toString ().contains ("Keys already exist" ));
94+ }
8095}
0 commit comments