Skip to content

Commit 33e687b

Browse files
committed
Add tests for new dirty keystore methods
Signed-off-by: Jon Bartels <jonathan.bartels@gmail.com>
1 parent d4eb254 commit 33e687b

1 file changed

Lines changed: 118 additions & 0 deletions

File tree

‎server/src/test/java/com/mirth/connect/server/controllers/DefaultConfigurationControllerTest.java‎

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
package com.mirth.connect.server.controllers;
1111

1212
import static org.junit.Assert.assertEquals;
13+
import static org.junit.Assert.assertFalse;
1314
import static org.junit.Assert.assertTrue;
1415
import static org.junit.Assert.fail;
1516
import static org.mockito.ArgumentMatchers.any;
@@ -24,18 +25,26 @@
2425
import java.io.StringReader;
2526
import java.io.StringWriter;
2627
import java.io.Writer;
28+
import java.lang.reflect.Field;
29+
import java.lang.reflect.InvocationTargetException;
30+
import java.lang.reflect.Method;
31+
import java.security.KeyStore;
32+
import java.security.Provider;
2733
import java.util.ArrayList;
2834
import java.util.Arrays;
2935
import java.util.List;
3036
import java.util.Properties;
3137

38+
import javax.crypto.KeyGenerator;
39+
import javax.crypto.SecretKey;
3240
import javax.xml.XMLConstants;
3341
import javax.xml.transform.Source;
3442
import javax.xml.transform.TransformerFactory;
3543
import javax.xml.transform.stream.StreamResult;
3644
import javax.xml.transform.stream.StreamSource;
3745

3846
import org.apache.commons.io.FileUtils;
47+
import org.bouncycastle.jce.provider.BouncyCastleProvider;
3948
import org.junit.BeforeClass;
4049
import org.junit.Test;
4150
import org.mockito.invocation.Invocation;
@@ -46,6 +55,7 @@
4655
import com.google.inject.Injector;
4756
import com.mirth.connect.client.core.ControllerException;
4857
import com.mirth.connect.model.DriverInfo;
58+
import com.mirth.connect.model.EncryptionSettings;
4959

5060
public class DefaultConfigurationControllerTest {
5161

@@ -275,6 +285,114 @@ public void testParseDbdriversXmlWithExternalDtd() {
275285
assertTrue(exceptionCaught);
276286
}
277287

288+
/*
289+
* The following tests cover the keystore "dirty" tracking added so that initializeSecuritySettings()
290+
* only rewrites the keystore file when something actually changed. configureEncryption() and
291+
* generateDefaultCertificate() are private, so they are exercised via reflection; both live in the
292+
* same package as this test. Each returns true only when it mutated the keystore.
293+
*/
294+
295+
@Test
296+
public void configureEncryption_returnsTrue_andStoresKey_whenKeyMissing() throws Exception {
297+
setEncryptionConfig(new EncryptionSettings(new Properties()));
298+
299+
Provider provider = new BouncyCastleProvider();
300+
KeyStore keyStore = newEmptyKeyStore();
301+
char[] keyPassword = KEYSTORE_PASSWORD;
302+
303+
assertFalse(keyStore.containsAlias(DefaultConfigurationController.SECRET_KEY_ALIAS));
304+
305+
boolean dirtied = invokeConfigureEncryption(provider, keyStore, keyPassword);
306+
307+
assertTrue("A newly generated encryption key should mark the keystore dirty", dirtied);
308+
assertTrue("The encryption key should now be present in the keystore", keyStore.containsAlias(DefaultConfigurationController.SECRET_KEY_ALIAS));
309+
}
310+
311+
@Test
312+
public void configureEncryption_returnsFalse_whenKeyAlreadyPresent() throws Exception {
313+
setEncryptionConfig(new EncryptionSettings(new Properties()));
314+
315+
Provider provider = new BouncyCastleProvider();
316+
KeyStore keyStore = newEmptyKeyStore();
317+
char[] keyPassword = KEYSTORE_PASSWORD;
318+
319+
// Seed the keystore with an existing secret key so the "found" branch is taken.
320+
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
321+
keyGenerator.init(128);
322+
SecretKey existingKey = keyGenerator.generateKey();
323+
keyStore.setEntry(DefaultConfigurationController.SECRET_KEY_ALIAS, new KeyStore.SecretKeyEntry(existingKey), new KeyStore.PasswordProtection(keyPassword));
324+
325+
boolean dirtied = invokeConfigureEncryption(provider, keyStore, keyPassword);
326+
327+
assertFalse("Reusing an existing encryption key should not mark the keystore dirty", dirtied);
328+
}
329+
330+
@Test
331+
public void generateDefaultCertificate_returnsTrue_andStoresCert_whenCertMissing() throws Exception {
332+
Provider provider = new BouncyCastleProvider();
333+
KeyStore keyStore = newEmptyKeyStore();
334+
char[] keyPassword = KEYSTORE_PASSWORD;
335+
336+
assertFalse(keyStore.containsAlias(CERTIFICATE_ALIAS));
337+
338+
boolean dirtied = invokeGenerateDefaultCertificate(provider, keyStore, keyPassword);
339+
340+
assertTrue("A newly generated certificate should mark the keystore dirty", dirtied);
341+
assertTrue("The certificate should now be present in the keystore", keyStore.containsAlias(CERTIFICATE_ALIAS));
342+
}
343+
344+
@Test
345+
public void generateDefaultCertificate_returnsFalse_whenCertAlreadyPresent() throws Exception {
346+
Provider provider = new BouncyCastleProvider();
347+
KeyStore keyStore = newEmptyKeyStore();
348+
char[] keyPassword = KEYSTORE_PASSWORD;
349+
350+
// First call generates and stores the certificate.
351+
assertTrue(invokeGenerateDefaultCertificate(provider, keyStore, keyPassword));
352+
353+
// Second call should find the existing certificate and leave the keystore untouched.
354+
boolean dirtied = invokeGenerateDefaultCertificate(provider, keyStore, keyPassword);
355+
356+
assertFalse("Reusing an existing certificate should not mark the keystore dirty", dirtied);
357+
}
358+
359+
private static final char[] KEYSTORE_PASSWORD = "testpass".toCharArray();
360+
private static final String CERTIFICATE_ALIAS = "mirthconnect";
361+
362+
private KeyStore newEmptyKeyStore() throws Exception {
363+
KeyStore keyStore = KeyStore.getInstance("JCEKS");
364+
keyStore.load(null, KEYSTORE_PASSWORD);
365+
return keyStore;
366+
}
367+
368+
private void setEncryptionConfig(EncryptionSettings encryptionConfig) throws Exception {
369+
Field field = DefaultConfigurationController.class.getDeclaredField("encryptionConfig");
370+
field.setAccessible(true);
371+
field.set(null, encryptionConfig);
372+
}
373+
374+
private boolean invokeConfigureEncryption(Provider provider, KeyStore keyStore, char[] keyPassword) throws Exception {
375+
return invokePrivateDirtyMethod("configureEncryption", provider, keyStore, keyPassword);
376+
}
377+
378+
private boolean invokeGenerateDefaultCertificate(Provider provider, KeyStore keyStore, char[] keyPassword) throws Exception {
379+
return invokePrivateDirtyMethod("generateDefaultCertificate", provider, keyStore, keyPassword);
380+
}
381+
382+
private boolean invokePrivateDirtyMethod(String methodName, Provider provider, KeyStore keyStore, char[] keyPassword) throws Exception {
383+
Method method = DefaultConfigurationController.class.getDeclaredMethod(methodName, Provider.class, KeyStore.class, char[].class);
384+
method.setAccessible(true);
385+
try {
386+
return (boolean) method.invoke(new DefaultConfigurationController(), provider, keyStore, keyPassword);
387+
} catch (InvocationTargetException e) {
388+
Throwable cause = e.getCause();
389+
if (cause instanceof Exception) {
390+
throw (Exception) cause;
391+
}
392+
throw e;
393+
}
394+
}
395+
278396
private void assertDefaultDrivers(List<DriverInfo> drivers, boolean includeODBC) {
279397
assertEquals(includeODBC ? 7 : 6, drivers.size());
280398
int i = 0;

0 commit comments

Comments
 (0)