/************************************************************************* * * * EJBCA Community: The OpenSource Certificate Authority * * * * This software is free software; you can redistribute it and/or * * modify it under the terms of the GNU Lesser General Public * * License as published by the Free Software Foundation; either * * version 2.1 of the License, or any later version. * * * * See terms of license at gnu.org. * * * *************************************************************************/ package org.ejbca.core.protocol.cmp; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import java.net.HttpURLConnection; import java.net.URL; import org.apache.log4j.Logger; import org.cesecore.SystemTestsConfiguration; import org.cesecore.configuration.GlobalConfigurationSession; import org.cesecore.configuration.GlobalConfigurationSessionRemote; import org.cesecore.util.EjbRemoteHelper; import org.ejbca.config.CmpConfiguration; import org.ejbca.config.WebConfiguration; import org.junit.After; import org.junit.Before; import org.junit.Test; /** * * @version $Id: CmpAliasTest.java 22450 2015-12-15 14:06:34Z mikekushner $ * */ public class CmpAliasTest extends CmpTestCase { private static final Logger log = Logger.getLogger(CmpAliasTest.class); private final GlobalConfigurationSession globalConfigurationSession = EjbRemoteHelper.INSTANCE.getRemoteSession(GlobalConfigurationSessionRemote.class); private final String baseResource = "publicweb/cmp"; private final String httpReqPath; public CmpAliasTest() { final String httpServerPubHttp = SystemTestsConfiguration.getRemotePortHttp(this.configurationSession.getProperty(WebConfiguration.CONFIG_HTTPSERVERPUBHTTP)); final String httpServerHost = SystemTestsConfiguration.getRemoteHost(this.configurationSession.getProperty(WebConfiguration.CONFIG_HTTPSSERVERHOSTNAME)); this.httpReqPath = "http://" + httpServerHost + ":" + httpServerPubHttp + "/ejbca"; } @Override @Before public void setUp() throws Exception { super.setUp(); } @Override @After public void tearDown() throws Exception { super.tearDown(); } /** * Sends a CMP request with the alias requestAlias in the URL and expects a CMP error message * if that extractedAlias does not exist. * * @param requestAlias the alias that is specified in the URL * @param extractedAlias the alias that EJBCA will use to handle the CMP request * @throws Exception */ private void sendCmpRequest(CmpConfiguration cmpconfig, String requestAlias, String extractedAlias) throws Exception { if(cmpconfig.aliasExists(extractedAlias)) { cmpconfig.renameAlias(extractedAlias, "backUpAlias" + extractedAlias + "ForAliasTesting001122334455"); this.globalConfigurationSession.saveConfiguration(ADMIN, cmpconfig); } try { String urlString = this.httpReqPath + '/' + this.baseResource; if(requestAlias != null) { urlString += "/" + requestAlias; } log.info("http URL: " + urlString); URL url = new URL(urlString); final HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setDoOutput(true); con.setRequestMethod("POST"); con.setRequestProperty("Content-type", "application/pkixcmp"); con.connect(); assertEquals("Unexpected HTTP response code.", 404, con.getResponseCode()); // A cmp alias that does not will result in a HTTP not found error } finally { if(cmpconfig.aliasExists("backUpAlias" + extractedAlias + "ForAliasTesting001122334455")) { cmpconfig.renameAlias("backUpAlias" + extractedAlias + "ForAliasTesting001122334455", extractedAlias); this.globalConfigurationSession.saveConfiguration(ADMIN, cmpconfig); } } } /** * Tests that the right configuration alias is extracted from the CMP URL. * * A CMP request for a non-existing alias is sent. Expected an error message caused by the absence of the expected CMP alias * * @throws Exception */ @Test public void test01Access() throws Exception { log.trace(">test01Access()"); CmpConfiguration cmpConfig = (CmpConfiguration) this.globalConfigurationSession.getCachedConfiguration(CmpConfiguration.CMP_CONFIGURATION_ID); sendCmpRequest(cmpConfig, "alias123", "alias123"); // "alias123" in the request causes Ejbca to use "alias123" as CMP alias sendCmpRequest(cmpConfig, "123", "123"); // "123" in the request causes Ejbca to use "123" as CMP alias sendCmpRequest(cmpConfig, "", "cmp"); // No alias in the request causes Ejbca to use "cmp" (the default alias) as CMP alias sendCmpRequest(cmpConfig, null, "cmp"); // No alias in the request causes Ejbca to use "cmp" (the default alias) as CMP alias sendCmpRequest(cmpConfig, "alias??&!!foo", "alias"); // Specifying alias with non-alphanumeric characters cause Ejbca to use, // as CMP alias, a substring of the first alphanumeric characters, in this // case: alias sendCmpRequest(cmpConfig, "??##!!&", "cmp"); // Specifying alias with non-alphanumeric characters cause EJBCA to use, // as CMP alias, a substring of the first alphanumeric characters, in this // case: empty string, which means that the default alias "cmp" will be used log.trace("