104 lines
3.6 KiB
Java
104 lines
3.6 KiB
Java
package SuMoC.backend.security;
|
|
|
|
import SuMoC.backend.security.utils.RSAKeyProperties;
|
|
import org.junit.jupiter.api.Test;
|
|
import org.junit.jupiter.api.io.TempDir;
|
|
import org.springframework.boot.test.context.SpringBootTest;
|
|
import org.springframework.test.context.ActiveProfiles;
|
|
|
|
import java.io.IOException;
|
|
import java.nio.file.Files;
|
|
import java.nio.file.Path;
|
|
import java.nio.file.Paths;
|
|
import java.security.interfaces.RSAPrivateKey;
|
|
import java.security.interfaces.RSAPublicKey;
|
|
|
|
import static org.junit.jupiter.api.Assertions.*;
|
|
|
|
@SpringBootTest
|
|
@ActiveProfiles("test")
|
|
public class RSAKeyPropertiesTest {
|
|
|
|
@TempDir
|
|
Path tempDir;
|
|
|
|
private String pubKeyPath() { return tempDir.resolve("public.pem").toString(); }
|
|
private String privKeyPath() { return tempDir.resolve("private.pem").toString(); }
|
|
|
|
@Test
|
|
public void constructor_generatesAndPersistsKeys_whenFilesDoNotExist() {
|
|
RSAKeyProperties props = new RSAKeyProperties(pubKeyPath(), privKeyPath());
|
|
|
|
assertNotNull(props.getPublicKey());
|
|
assertNotNull(props.getPrivateKey());
|
|
assertInstanceOf(RSAPublicKey.class, props.getPublicKey());
|
|
assertInstanceOf(RSAPrivateKey.class, props.getPrivateKey());
|
|
}
|
|
|
|
@Test
|
|
public void constructor_loadsExistingKeys_whenFilesExist() throws IOException {
|
|
RSAKeyProperties first = new RSAKeyProperties(pubKeyPath(), privKeyPath());
|
|
|
|
RSAKeyProperties second = new RSAKeyProperties(pubKeyPath(), privKeyPath());
|
|
|
|
assertEquals(
|
|
first.getPublicKey().getModulus(),
|
|
second.getPublicKey().getModulus()
|
|
);
|
|
assertEquals(
|
|
first.getPrivateKey().getModulus(),
|
|
second.getPrivateKey().getModulus()
|
|
);
|
|
|
|
Path onlyPub = tempDir.resolve("onlypub");
|
|
String pub2 = onlyPub.resolve("public.pem").toString();
|
|
String priv2 = onlyPub.resolve("private.pem").toString();
|
|
|
|
RSAKeyProperties onlyPubProps = new RSAKeyProperties(pub2, priv2);
|
|
Files.delete(Paths.get(priv2));
|
|
|
|
RSAKeyProperties mixed = new RSAKeyProperties(pub2, priv2);
|
|
assertNotNull(mixed.getPublicKey());
|
|
assertNotNull(mixed.getPrivateKey());
|
|
}
|
|
|
|
@Test
|
|
public void constructor_generatesDifferentKeys_forDifferentDirs() {
|
|
Path otherDir = tempDir.resolve("other");
|
|
String otherPub = otherDir.resolve("public.pem").toString();
|
|
String otherPriv = otherDir.resolve("private.pem").toString();
|
|
|
|
RSAKeyProperties first = new RSAKeyProperties(pubKeyPath(), privKeyPath());
|
|
RSAKeyProperties second = new RSAKeyProperties(otherPub, otherPriv);
|
|
|
|
assertNotEquals(
|
|
first.getPublicKey().getModulus(),
|
|
second.getPublicKey().getModulus()
|
|
);
|
|
}
|
|
|
|
@Test
|
|
public void constructor_throwsIllegalStateException_onInvalidPath() {
|
|
assertThrows(IllegalStateException.class,
|
|
() -> new RSAKeyProperties("/invalid\0path/pub.pem", "/invalid\0path/priv.pem")
|
|
);
|
|
}
|
|
|
|
@Test
|
|
public void constructor_throwsIllegalStateException_whenDirectoryNotWritable() throws Exception {
|
|
Path readOnlyDir = tempDir.resolve("readonly");
|
|
Files.createDirectories(readOnlyDir);
|
|
readOnlyDir.toFile().setWritable(false);
|
|
|
|
String pub = readOnlyDir.resolve("public.pem").toString();
|
|
String priv = readOnlyDir.resolve("private.pem").toString();
|
|
|
|
try {
|
|
assertThrows(IllegalStateException.class,
|
|
() -> new RSAKeyProperties(pub, priv)
|
|
);
|
|
} finally {
|
|
readOnlyDir.toFile().setWritable(true);
|
|
}
|
|
}
|
|
} |