/************************************************************************* * * * 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.ui.cli.keybind; import static org.junit.Assert.assertEquals; import org.cesecore.authentication.tokens.AuthenticationToken; import org.cesecore.authorization.AuthorizationDeniedException; import org.cesecore.certificates.ca.CaSessionRemote; import org.cesecore.certificates.ca.X509CA; import org.cesecore.certificates.util.AlgorithmConstants; import org.cesecore.keybind.InternalKeyBindingInfo; import org.cesecore.keybind.InternalKeyBindingMgmtSessionRemote; import org.cesecore.keybind.InternalKeyBindingStatus; import org.cesecore.keybind.impl.OcspKeyBinding; import org.cesecore.keys.token.CryptoTokenManagementSessionRemote; import org.cesecore.keys.token.CryptoTokenTestUtils; import org.cesecore.mock.authentication.tokens.TestAlwaysAllowLocalAuthenticationToken; import org.cesecore.util.CryptoProviderTools; import org.cesecore.util.EjbRemoteHelper; import org.ejbca.ui.cli.infrastructure.command.CommandResult; import org.junit.After; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; /** * @version $Id: InternalKeyBindingModifyCommandTest.java 19902 2014-09-30 14:32:24Z anatom $ * */ public class InternalKeyBindingModifyCommandTest { private static final String TESTCLASS_NAME = InternalKeyBindingModifyCommandTest.class.getSimpleName(); private static final String NEXT_KEYPAIR_NAME = "nextKeyPair"; private static final AuthenticationToken authenticationToken = new TestAlwaysAllowLocalAuthenticationToken( InternalKeyBindingModifyCommandTest.class.getSimpleName()); private InternalKeyBindingModifyCommand command = new InternalKeyBindingModifyCommand(); private static final CaSessionRemote caSession = EjbRemoteHelper.INSTANCE.getRemoteSession(CaSessionRemote.class); private static final CryptoTokenManagementSessionRemote cryptoTokenManagementSession = EjbRemoteHelper.INSTANCE .getRemoteSession(CryptoTokenManagementSessionRemote.class); private final InternalKeyBindingMgmtSessionRemote internalKeyBindingMgmtSession = EjbRemoteHelper.INSTANCE .getRemoteSession(InternalKeyBindingMgmtSessionRemote.class); private static X509CA x509ca = null; private static int cryptoTokenId; private static int internalKeyBindingId; @BeforeClass public static void beforeClass() throws Exception { CryptoProviderTools.installBCProvider(); x509ca = CryptoTokenTestUtils.createTestCAWithSoftCryptoToken(authenticationToken, "CN=" + TESTCLASS_NAME); cryptoTokenId = CryptoTokenTestUtils.createSoftCryptoToken(authenticationToken, TESTCLASS_NAME); cryptoTokenManagementSession.createKeyPair(authenticationToken, cryptoTokenId, TESTCLASS_NAME, "RSA2048"); cryptoTokenManagementSession.createKeyPair(authenticationToken, cryptoTokenId, NEXT_KEYPAIR_NAME, "RSA2048"); } @AfterClass public static void afterClass() throws Exception { cryptoTokenManagementSession.deleteCryptoToken(authenticationToken, cryptoTokenId); if (x509ca != null) { final int caCryptoTokenId = caSession.getCAInfo(authenticationToken, x509ca.getCAId()).getCAToken().getCryptoTokenId(); cryptoTokenManagementSession.deleteCryptoToken(authenticationToken, caCryptoTokenId); caSession.removeCA(authenticationToken, x509ca.getCAId()); } } @Before public void setup() throws Exception { internalKeyBindingId = internalKeyBindingMgmtSession.createInternalKeyBinding(authenticationToken, OcspKeyBinding.IMPLEMENTATION_ALIAS, TESTCLASS_NAME, InternalKeyBindingStatus.DISABLED, null, cryptoTokenId, TESTCLASS_NAME, AlgorithmConstants.SIGALG_SHA1_WITH_RSA, null, null); } @After public void tearDown() throws AuthorizationDeniedException { Integer keyBindingId = internalKeyBindingMgmtSession.getIdFromName(TESTCLASS_NAME); if (keyBindingId != null) { internalKeyBindingMgmtSession.deleteInternalKeyBinding(authenticationToken, keyBindingId); } Integer cryptoTokenId = cryptoTokenManagementSession.getIdFromName(TESTCLASS_NAME); if (cryptoTokenId != null) { cryptoTokenManagementSession.deleteCryptoToken(authenticationToken, cryptoTokenId); } } @Test public void testModifyBinding() throws AuthorizationDeniedException { String[] args = new String[] { TESTCLASS_NAME, "--nextkeypair", NEXT_KEYPAIR_NAME, "-"+OcspKeyBinding.PROPERTY_MAX_AGE + "=30" }; assertEquals(CommandResult.SUCCESS, command.execute(args)); InternalKeyBindingInfo internalKeyBindingInfo = internalKeyBindingMgmtSession.getInternalKeyBindingInfo(authenticationToken, internalKeyBindingId); assertEquals("Next keypair alias was not set.", NEXT_KEYPAIR_NAME, internalKeyBindingInfo.getNextKeyPairAlias()); assertEquals("Property " + OcspKeyBinding.PROPERTY_MAX_AGE + " was not modified", 30L, internalKeyBindingInfo.getProperty(OcspKeyBinding.PROPERTY_MAX_AGE).getValue()); } }