Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix delayed junits #84

Merged
merged 2 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@
<dependency>
<groupId>com.siemens.pki</groupId>
<artifactId>CmpRaComponent</artifactId>
<version>4.1.3</version>
<version>4.1.4</version>
</dependency>
<dependency>
<groupId>jakarta.xml.bind</groupId>
Expand Down
23 changes: 12 additions & 11 deletions src/main/java/com/siemens/pki/lightweightcmpra/main/RA.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ void set(final T val) {
}
}

private static ArrayList<RA> raList;
private static ArrayList<RA> raList = new ArrayList<>();

/**
* @param args command line arguments. Call with &lt;name of XML/YAML/JSON
Expand All @@ -90,35 +90,35 @@ public static void main(final String[] args) throws Exception {
System.err.println("call with <name of YAML/JSON config file>");
return;
}
raList = new ArrayList<>(args.length);
// start RAs
for (final String actConfigFile : args) {
raList.add(new RA(actConfigFile));
synchronized (raList) {
// start RAs
for (final String actConfigFile : args) {
raList.add(new RA(actConfigFile));
}
}
}

/**
* stop all RA instances, used for unit tests
*/
public static void stopAllRas() {
for (; ; ) {
if (raList.isEmpty()) {
break;
}
raList.remove(0).stop();
synchronized (raList) {
raList.forEach(RA::stop);
raList.clear();
}
}

private DownstreamInterface downstreamInterface;
private String configFile;
private Map<CertProfileBodyTypeTupel, UpstreamInterface> upstreamInterfaceMap;

private RA(final String actConfigFile) throws Exception {
configFile = actConfigFile;

try {
final ConfigurationImpl configuration = YamlConfigLoader.loadConfig(configFile, ConfigurationImpl.class);
final DeferredSupplier<CmpRaInterface> raHolder = new DeferredSupplier<>();
final Map<CertProfileBodyTypeTupel, UpstreamInterface> upstreamInterfaceMap = new HashMap<>();
upstreamInterfaceMap = new HashMap<>();
final UpstreamExchange upstreamExchange = (request, certProfile, bodyTypeOfFirstRequest) -> {
final CertProfileBodyTypeTupel key = new CertProfileBodyTypeTupel(certProfile, bodyTypeOfFirstRequest);
UpstreamInterface upstreamInterface = upstreamInterfaceMap.get(key);
Expand Down Expand Up @@ -146,6 +146,7 @@ private void stop() {
if (downstreamInterface != null) {
downstreamInterface.stop();
}
upstreamInterfaceMap.values().forEach(UpstreamInterface::stop);
System.out.println("RA configured with " + configFile + " stopped");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,6 @@ interface AsyncResponseHandler {
* the callback
*/
void setDelayedResponseHandler(AsyncResponseHandler asyncResponseHandler);

void stop();
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public class CmpFileOfflineClient implements UpstreamInterface {

private final File outputDirectory;
private AsyncResponseHandler asyncResponseHandler;
private TimerTask timerTask;

/**
*
Expand All @@ -66,14 +67,14 @@ public CmpFileOfflineClient(final OfflineFileClientConfig config) throws IOExcep
}
final long pollInterval = config.getInputDirectoryPollcycle() * 1000L;
final Timer pollTimer = new Timer(true);
final TimerTask task = new TimerTask() {
timerTask = new TimerTask() {

@Override
public void run() {
pollInputDirectory();
}
};
pollTimer.schedule(task, new Date(System.currentTimeMillis() + pollInterval), pollInterval);
pollTimer.schedule(timerTask, new Date(System.currentTimeMillis() + pollInterval), pollInterval);
}

@Override
Expand Down Expand Up @@ -118,4 +119,9 @@ private void pollInputDirectory() {
}
}
}

@Override
public void stop() {
timerTask.cancel();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,9 @@ public byte[] apply(final byte[] message, final String certProfile) {
public void setDelayedResponseHandler(final AsyncResponseHandler asyncResponseHandler) {
// no async response expected
}

@Override
public void stop() {
client.shutdown();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,10 @@ public byte[] apply(final byte[] message, final String certProfile) {
public void setDelayedResponseHandler(final AsyncResponseHandler asyncResponseHandler) {
// no async response expected
}

@Override
public void stop() {
// nothing to do

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import com.siemens.pki.lightweightcmpra.test.framework.TestUtils;
import com.siemens.pki.lightweightcmpra.util.ConfigFileLoader;
import java.io.File;
import java.security.GeneralSecurityException;
import java.security.Security;
import org.junit.AfterClass;
import org.junit.Before;
Expand All @@ -34,8 +33,7 @@ public class CmpTestcaseBase {
public static final File CONFIG_DIRECTORY =
new File("./src/test/java/com/siemens/pki/lightweightcmpra/test/config");

protected static void initTestbed(final String... namesOfRaConfigFile)
throws Exception, GeneralSecurityException, InterruptedException {
protected static void initTestbed(final String... namesOfRaConfigFile) throws Exception {
for (final String nameOfRaConfigFile : namesOfRaConfigFile) {
RA.main(new String[] {nameOfRaConfigFile});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
*/
package com.siemens.pki.lightweightcmpclient.test;

import static org.junit.Assert.assertTrue;

import com.siemens.pki.lightweightcmpra.main.RA;
import com.siemens.pki.lightweightcmpra.test.framework.TestUtils;
import java.security.GeneralSecurityException;
Expand Down Expand Up @@ -48,13 +50,13 @@ public void setUpDelayedEnrollmentDirs() throws Exception {
TestUtils.deleteAllFilesIn("./target/CmpTest/Downstream", "./target/CmpTest/Upstream");
}

@Test
public void testCrWithPolling() throws Exception {
enrollWithConfig("DelayedClientEnrollmentConfigWithHttpAndSignature.yaml");
@Test(timeout = 60000L)
public void testCrWithPolling() {
assertTrue(enrollWithConfig("DelayedClientEnrollmentConfigWithHttpAndSignature.yaml"));
}

@Test
public void testRrWithPolling() throws Exception {
revokeWithConfigAndCert("DelayedClientEnrollmentConfigWithHttpAndSignature.yaml");
@Test(timeout = 60000L)
public void testRrWithPolling() {
assertTrue(revokeWithConfigAndCert("DelayedClientEnrollmentConfigWithHttpAndSignature.yaml"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
import com.siemens.pki.lightweightcmpra.test.framework.CmpCaMock;
import com.siemens.pki.lightweightcmpra.util.CredentialLoader;
import java.io.File;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.security.cert.X509Certificate;
import org.junit.AfterClass;
import org.junit.BeforeClass;
Expand All @@ -42,18 +40,18 @@ public static void shutDownCA() {
CmpCaMock.stopSingleCaMock();
}

public void enrollWithConfig(String configFile) throws IOException, GeneralSecurityException {
public boolean enrollWithConfig(String configFile) {
final String cmdArgs =
"--configfile " + configFile + " " + "--enroll ./target/CmpTest/Results/EnrollmentResult.pem "
+ "--enrollmentChain ./target/CmpTest/Results/EnrollmentChain.pem ";
final int ret = CliCmpClient.runClient(cmdArgs.split("\\s+"));
assertEquals("Client failed", 0, ret);
assertTrue("enrollment result", new File("./target/CmpTest/Results/EnrollmentResult.pem").length() > 0);
assertTrue("enrollment chain", new File("./target/CmpTest/Results/EnrollmentChain.pem").length() > 0);
return true;
}

public void enrollWithConfigAndCertProfile(String configFile, String certProfile)
throws IOException, GeneralSecurityException {
public boolean enrollWithConfigAndCertProfile(String configFile, String certProfile) {
final String cmdArgs = "--configfile " + configFile + " "
+ "--enroll ./target/CmpTest/Results/EnrollmentResult.pem "
+ "--enrollmentChain ./target/CmpTest/Results/EnrollmentChain.pem " + "--certProfile " + certProfile
Expand All @@ -62,9 +60,10 @@ public void enrollWithConfigAndCertProfile(String configFile, String certProfile
assertEquals("Client failed", 0, ret);
assertTrue("enrollment result", new File("./target/CmpTest/Results/EnrollmentResult.pem").length() > 0);
assertTrue("enrollment chain", new File("./target/CmpTest/Results/EnrollmentChain.pem").length() > 0);
return true;
}

public void revokeWithConfigAndCert(String configFile) throws IOException, GeneralSecurityException {
public boolean revokeWithConfigAndCert(String configFile) {
String cmdArgs = "--configfile " + configFile + " " + "--enroll ./target/CmpTest/Results/EnrollmentResult.pem "
+ "--enrollmentChain ./target/CmpTest/Results/EnrollmentChain.pem "
+ "--enrollmentKeystore ./target/CmpTest/Results/EnrollmentKeystore.p12 "
Expand All @@ -77,9 +76,10 @@ public void revokeWithConfigAndCert(String configFile) throws IOException, Gener
cmdArgs = "--configfile " + configFile + " " + "--revokecert ./target/CmpTest/Results/EnrollmentResult.pem ";
ret = CliCmpClient.runClient(cmdArgs.split("\\s+"));
assertEquals("Client failed", 0, ret);
return true;
}

public void revokeWithIssuerAndSerial(String configFile) throws IOException, GeneralSecurityException {
public boolean revokeWithIssuerAndSerial(String configFile) {
String cmdArgs = "--configfile " + configFile + " " + "--enroll ./target/CmpTest/Results/EnrollmentResult.pem "
+ "--enrollmentChain ./target/CmpTest/Results/EnrollmentChain.pem "
+ "--enrollmentKeystore ./target/CmpTest/Results/EnrollmentKeystore.p12 "
Expand All @@ -95,5 +95,6 @@ public void revokeWithIssuerAndSerial(String configFile) throws IOException, Gen
+ certToRevoke.getIssuerX500Principal().getName() + " --serial " + certToRevoke.getSerialNumber();
ret = CliCmpClient.runClient(cmdArgs.split("\\s+"));
assertEquals("Client failed", 0, ret);
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
*/
package com.siemens.pki.lightweightcmpclient.test;

import static org.junit.Assert.assertTrue;

import com.siemens.pki.lightweightcmpra.main.RA;
import org.junit.AfterClass;
import org.junit.BeforeClass;
Expand All @@ -35,7 +37,7 @@ public static void stopAllRas() {
}

@Test
public void testCrWithKeyAgreement() throws Exception {
enrollWithConfig("ClientConfigWithCKGAgreeHttpAndSignature.yaml");
public void testCrWithKeyAgreement() {
assertTrue(enrollWithConfig("ClientConfigWithCKGAgreeHttpAndSignature.yaml"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
*/
package com.siemens.pki.lightweightcmpclient.test;

import static org.junit.Assert.assertTrue;

import com.siemens.pki.lightweightcmpra.main.RA;
import org.junit.AfterClass;
import org.junit.BeforeClass;
Expand All @@ -35,7 +37,7 @@ public static void stopAllRas() {
}

@Test
public void testCrWithKeyTransport() throws Exception {
enrollWithConfig("ClientConfigWithCKGTransHttpAndSignature.yaml");
public void testCrWithKeyTransport() {
assertTrue(enrollWithConfig("ClientConfigWithCKGTransHttpAndSignature.yaml"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
*/
package com.siemens.pki.lightweightcmpclient.test;

import static org.junit.Assert.assertTrue;

import com.siemens.pki.lightweightcmpra.main.RA;
import org.junit.AfterClass;
import org.junit.BeforeClass;
Expand All @@ -35,7 +37,7 @@ public static void stopAllRas() {
}

@Test
public void testCrWithPassword() throws Exception {
enrollWithConfig("ClientConfigWithCKGPassword.yaml");
public void testCrWithPassword() {
assertTrue(enrollWithConfig("ClientConfigWithCKGPassword.yaml"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
*/
package com.siemens.pki.lightweightcmpclient.test;

import static org.junit.Assert.assertTrue;

import com.siemens.pki.lightweightcmpra.main.RA;
import org.junit.AfterClass;
import org.junit.BeforeClass;
Expand All @@ -40,8 +42,8 @@ public static void stopAllRas() {
* @throws Exception
*/
@Test(timeout = 100000L)
public void testEnrollmentWithCertProfile1() throws Exception {
enrollWithConfigAndCertProfile("ClientCertProfileEnrollment.yaml", "secret1");
public void testEnrollmentWithCertProfile1() {
assertTrue(enrollWithConfigAndCertProfile("ClientCertProfileEnrollment.yaml", "secret1"));
}

/**
Expand All @@ -50,7 +52,7 @@ public void testEnrollmentWithCertProfile1() throws Exception {
* @throws Exception
*/
@Test
public void testEnrollmentWithCertProfile2() throws Exception {
enrollWithConfigAndCertProfile("ClientCertProfileEnrollment.yaml", "secret2");
public void testEnrollmentWithCertProfile2() {
assertTrue(enrollWithConfigAndCertProfile("ClientCertProfileEnrollment.yaml", "secret2"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
*/
package com.siemens.pki.lightweightcmpclient.test;

import static org.junit.Assert.assertTrue;

import com.siemens.pki.lightweightcmpra.main.RA;
import org.junit.AfterClass;
import org.junit.BeforeClass;
Expand All @@ -40,7 +42,7 @@ public static void stopAllRas() {
* @throws Exception
*/
@Test
public void testCr() throws Exception {
enrollWithConfig("ClientEnrollmentConfigWithCoapAndSignature.yaml");
public void testCr() {
assertTrue(enrollWithConfig("ClientEnrollmentConfigWithCoapAndSignature.yaml"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
*/
package com.siemens.pki.lightweightcmpclient.test;

import static org.junit.Assert.assertTrue;

import com.siemens.pki.lightweightcmpra.main.RA;
import org.junit.AfterClass;
import org.junit.BeforeClass;
Expand All @@ -40,7 +42,7 @@ public static void stopAllRas() {
* @throws Exception
*/
@Test
public void testCr() throws Exception {
enrollWithConfig("ClientEnrollmentConfigWithHttpAndSignature.yaml");
public void testCr() {
assertTrue(enrollWithConfig("ClientEnrollmentConfigWithHttpAndSignature.yaml"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
*/
package com.siemens.pki.lightweightcmpclient.test;

import static org.junit.Assert.assertTrue;

import com.siemens.pki.lightweightcmpra.main.RA;
import org.junit.AfterClass;
import org.junit.BeforeClass;
Expand All @@ -40,7 +42,7 @@ public static void stopAllRas() {
* @throws Exception
*/
@Test
public void testNestedCr() throws Exception {
enrollWithConfig("ClientNestedEnrollmentConfig.yaml");
public void testNestedCr() {
assertTrue(enrollWithConfig("ClientNestedEnrollmentConfig.yaml"));
}
}
Loading