Skip to content

Commit 81d6eb3

Browse files
committed
Add main key gen test too
1 parent 7ff3474 commit 81d6eb3

2 files changed

Lines changed: 36 additions & 21 deletions

File tree

server/src/main/java/dev/findfirst/security/util/KeyGenerator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public static void main(String[] args) {
7474
e.printStackTrace();
7575
}
7676
} else {
77-
System.out.println("Keys already exist.");
77+
logger.info("Keys already exist.");
7878
}
7979
}
8080
}

server/src/test/java/dev/findfirst/security/util/KeyGeneratorTest.java

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@
99
import java.util.Base64;
1010

1111
import org.junit.jupiter.api.Test;
12+
import org.junit.jupiter.api.extension.ExtendWith;
1213
import 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)
1418
class 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

Comments
 (0)