Browse Source

implemented tests for encrypted mail config

Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
sqlite
Stephan Richter 2 months ago
parent
commit
48542c67fb
  1. 2
      de.srsoftware.oidc.app/src/main/java/de/srsoftware/oidc/app/Application.java
  2. 1
      de.srsoftware.oidc.datastore.encrypted/build.gradle
  3. 3
      de.srsoftware.oidc.datastore.encrypted/src/main/java/de/srsoftware/oidc/datastore/encrypted/EncryptedConfig.java
  4. 4
      de.srsoftware.oidc.datastore.encrypted/src/main/java/de/srsoftware/oidc/datastore/encrypted/EncryptedMailConfig.java
  5. 20
      de.srsoftware.oidc.datastore.encrypted/src/test/java/EncryptedConfigTest.java
  6. 129
      de.srsoftware.oidc.datastore.encrypted/src/test/java/EncryptedMailConfigTest.java

2
de.srsoftware.oidc.app/src/main/java/de/srsoftware/oidc/app/Application.java

@ -117,10 +117,10 @@ public class Application { @@ -117,10 +117,10 @@ public class Application {
};
Optional<String> encryptionKey = config.get(ENCRYPTION_KEY);
var salt = config.getOrDefault(SALT,uuid());
if (encryptionKey.isPresent()){
var salt = config.getOrDefault(SALT,uuid());
mailConfig = new EncryptedMailConfig(mailConfig,encryptionKey.get(),salt);
}
return mailConfig;

1
de.srsoftware.oidc.datastore.encrypted/build.gradle

@ -14,6 +14,7 @@ dependencies { @@ -14,6 +14,7 @@ dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter'
implementation project(':de.srsoftware.oidc.api')
implementation 'com.sun.mail:jakarta.mail:2.0.1'
implementation project(':de.srsoftware.utils')
}

3
de.srsoftware.oidc.datastore.encrypted/src/main/java/de/srsoftware/oidc/datastore/encrypted/EncryptedConfig.java

@ -33,6 +33,7 @@ public class EncryptedConfig { @@ -33,6 +33,7 @@ public class EncryptedConfig {
}
public String encrypt(String plain) {
if (plain == null) return null;
SecureRandom secureRandom = new SecureRandom();
byte[] iv = new byte[16];
secureRandom.nextBytes(iv);
@ -52,6 +53,8 @@ public class EncryptedConfig { @@ -52,6 +53,8 @@ public class EncryptedConfig {
}
public String decrypt(String secret) {
if (secret == null) return null;
if (secret.isBlank()) return "";
byte[] encryptedData = Base64.getDecoder().decode(secret);
byte[] iv = new byte[16];
System.arraycopy(encryptedData, 0, iv, 0, iv.length);

4
de.srsoftware.oidc.datastore.encrypted/src/main/java/de/srsoftware/oidc/datastore/encrypted/EncryptedMailConfig.java

@ -14,8 +14,8 @@ public class EncryptedMailConfig extends EncryptedConfig implements MailConfig { @@ -14,8 +14,8 @@ public class EncryptedMailConfig extends EncryptedConfig implements MailConfig {
private final MailConfig storage;
private Authenticator auth;
public EncryptedMailConfig(MailConfig storage, String encryotionKey, String salt) {
super(encryotionKey, salt);
public EncryptedMailConfig(MailConfig storage, String encryptionKey, String salt) {
super(encryptionKey, salt);
this.storage = storage;
}

20
de.srsoftware.oidc.datastore.encrypted/src/test/java/EncryptedConfigTest.java

@ -0,0 +1,20 @@ @@ -0,0 +1,20 @@
/* © SRSoftware 2024 */
import static de.srsoftware.utils.Strings.uuid;
import static org.junit.jupiter.api.Assertions.assertEquals;
import de.srsoftware.oidc.datastore.encrypted.EncryptedConfig;
import org.junit.jupiter.api.Test;
public class EncryptedConfigTest {
@Test
public void testEncryptionDecryption() {
var key = uuid();
var salt = uuid();
var secret = uuid();
var encryptor = new EncryptedConfig(key, salt);
var decryptor = new EncryptedConfig(key, salt);
var encrypted = encryptor.encrypt(secret);
var decrypted = decryptor.decrypt(encrypted);
assertEquals(secret, decrypted);
}
}

129
de.srsoftware.oidc.datastore.encrypted/src/test/java/EncryptedMailConfigTest.java

@ -0,0 +1,129 @@ @@ -0,0 +1,129 @@
/* © SRSoftware 2024 */
import static de.srsoftware.utils.Strings.uuid;
import static org.junit.jupiter.api.Assertions.*;
import de.srsoftware.oidc.api.MailConfig;
import de.srsoftware.oidc.datastore.encrypted.EncryptedMailConfig;
import jakarta.mail.Authenticator;
import java.util.Map;
import java.util.Properties;
import org.junit.jupiter.api.Test;
public class EncryptedMailConfigTest {
private class InMemoryMailConfig implements MailConfig {
private String smtpHost;
private int port;
private String addr;
private String pass;
private boolean tls;
private boolean auth;
@Override
public String smtpHost() {
return smtpHost;
}
@Override
public MailConfig smtpHost(String newValue) {
smtpHost = newValue;
return this;
}
@Override
public int smtpPort() {
return port;
}
@Override
public MailConfig smtpPort(int newValue) {
port = newValue;
return this;
}
@Override
public String senderAddress() {
return addr;
}
@Override
public MailConfig senderAddress(String newValue) {
addr = newValue;
return this;
}
@Override
public String senderPassword() {
return pass;
}
@Override
public MailConfig senderPassword(String newValue) {
pass = newValue;
return this;
}
@Override
public boolean startTls() {
return tls;
}
@Override
public MailConfig startTls(boolean newValue) {
tls = newValue;
return this;
}
@Override
public boolean smtpAuth() {
return auth;
}
@Override
public MailConfig smtpAuth(boolean newValue) {
auth = newValue;
return this;
}
@Override
public Properties props() {
return null;
}
@Override
public Map<String, Object> map() {
return null;
}
@Override
public Authenticator authenticator() {
return null;
}
@Override
public MailConfig save() {
return this;
}
}
@Test
public void TestEncryptedMailConfig() {
var key = uuid();
var salt = uuid();
var addr = uuid();
var pass = uuid();
var host = uuid();
var plainMailConfig = new InMemoryMailConfig();
var writer = new EncryptedMailConfig(plainMailConfig, key, salt);
writer.senderAddress(addr).senderPassword(pass).smtpHost(host).smtpAuth(true).startTls(false);
var reader = new EncryptedMailConfig(plainMailConfig, key, salt);
assertEquals(addr, reader.senderAddress());
assertEquals(host, reader.smtpHost());
assertEquals(pass, reader.senderPassword());
assertTrue(reader.smtpAuth());
assertFalse(reader.startTls());
}
}
Loading…
Cancel
Save