Skip to content

Commit 7ff3474

Browse files
authored
keygeneratortestfileadded (#331)
Thanks for the test! Looks great 👍
1 parent 1f16ec4 commit 7ff3474

1 file changed

Lines changed: 80 additions & 0 deletions

File tree

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package dev.findfirst.security.util;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
import java.io.IOException;
6+
import java.nio.file.Files;
7+
import java.nio.file.Path;
8+
import java.security.NoSuchAlgorithmException;
9+
import java.util.Base64;
10+
11+
import org.junit.jupiter.api.Test;
12+
import org.junit.jupiter.api.io.TempDir;
13+
14+
class KeyGeneratorTest {
15+
16+
@TempDir
17+
Path tempDir;
18+
19+
@Test
20+
void testGenerateKeysProducesValidKeys() throws NoSuchAlgorithmException, IOException {
21+
Path privateKeyPath = tempDir.resolve("test.key");
22+
Path publicKeyPath = tempDir.resolve("test.pub");
23+
24+
KeyGenerator.generateKeys(privateKeyPath.toString(), publicKeyPath.toString());
25+
26+
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");
29+
30+
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");
33+
34+
String pubKeyBase64 = pubKeyContent
35+
.replace("-----BEGIN PUBLIC KEY-----", "")
36+
.replace("-----END PUBLIC KEY-----", "")
37+
.replaceAll("\\s", "");
38+
byte[] pubKeyBytes = Base64.getDecoder().decode(pubKeyBase64);
39+
assertTrue(pubKeyBytes.length > 0, "Decoded public key should not be empty");
40+
}
41+
42+
@Test
43+
void testGenerateKeysDoesNotOverwriteExistingKeys() throws NoSuchAlgorithmException, IOException {
44+
Path privateKeyPath = tempDir.resolve("existing.key");
45+
Path publicKeyPath = tempDir.resolve("existing.pub");
46+
Files.writeString(privateKeyPath, "dummy private key");
47+
Files.writeString(publicKeyPath, "dummy public key");
48+
49+
KeyGenerator.generateKeys(privateKeyPath.toString(), publicKeyPath.toString());
50+
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");
53+
}
54+
55+
@Test
56+
void testGenerateKeysProducesDifferentKeysOnSeparateRuns() throws NoSuchAlgorithmException, IOException {
57+
Path privateKeyPath1 = tempDir.resolve("test1.key");
58+
Path publicKeyPath1 = tempDir.resolve("test1.pub");
59+
Path privateKeyPath2 = tempDir.resolve("test2.key");
60+
Path publicKeyPath2 = tempDir.resolve("test2.pub");
61+
62+
KeyGenerator.generateKeys(privateKeyPath1.toString(), publicKeyPath1.toString());
63+
KeyGenerator.generateKeys(privateKeyPath2.toString(), publicKeyPath2.toString());
64+
65+
String pubKeyContent1 = Files.readString(publicKeyPath1);
66+
String pubKeyContent2 = Files.readString(publicKeyPath2);
67+
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", "");
76+
77+
assertNotEquals(pubKeyBase64_1, pubKeyBase64_2, "Each generated public key should be unique");
78+
79+
}
80+
}

0 commit comments

Comments
 (0)