/************************************************************************* * * * 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.cryptotoken; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import java.util.Properties; import org.cesecore.authentication.tokens.AuthenticationToken; import org.cesecore.authorization.AuthorizationDeniedException; import org.cesecore.keys.token.CryptoTokenAuthenticationFailedException; import org.cesecore.keys.token.CryptoTokenInfo; import org.cesecore.keys.token.CryptoTokenManagementProxySessionRemote; import org.cesecore.keys.token.CryptoTokenManagementSessionRemote; import org.cesecore.keys.token.CryptoTokenOfflineException; import org.cesecore.keys.token.SoftCryptoToken; import org.cesecore.mock.authentication.tokens.TestAlwaysAllowLocalAuthenticationToken; import org.cesecore.util.CryptoProviderTools; import org.cesecore.util.EjbRemoteHelper; import org.junit.After; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; /** * @version $Id: CryptoTokenUpdatePinCommandTest.java 22031 2015-10-15 09:07:34Z mikekushner $ * */ public class CryptoTokenUpdatePinCommandTest { private static final String TOKEN_NAME = CryptoTokenUpdatePinCommandTest.class.getSimpleName(); private static final String TOKEN_PIN = "foo123"; private final CryptoTokenUpdatePinCommand command = new CryptoTokenUpdatePinCommand(); private final AuthenticationToken authenticationToken = new TestAlwaysAllowLocalAuthenticationToken( CryptoTokenDeleteCommandTest.class.getSimpleName()); private CryptoTokenManagementSessionRemote cryptoTokenManagementSession = EjbRemoteHelper.INSTANCE .getRemoteSession(CryptoTokenManagementSessionRemote.class); private CryptoTokenManagementProxySessionRemote cryptoTokenProxySession = EjbRemoteHelper.INSTANCE .getRemoteSession(CryptoTokenManagementProxySessionRemote.class, EjbRemoteHelper.MODULE_TEST); private Integer cryptoTokenId; @BeforeClass public static void beforeClass() throws Exception { CryptoProviderTools.installBCProviderIfNotAvailable(); } @Before public void setup() throws Exception { final Properties cryptoTokenProperties = new Properties(); cryptoTokenId = cryptoTokenManagementSession.createCryptoToken(authenticationToken, TOKEN_NAME, SoftCryptoToken.class.getName(), cryptoTokenProperties, null, TOKEN_PIN.toCharArray()); cryptoTokenManagementSession.deactivate(authenticationToken, cryptoTokenId); } @After public void teardown() throws AuthorizationDeniedException { Integer cryptoTokenId = cryptoTokenManagementSession.getIdFromName(TOKEN_NAME); if (cryptoTokenId != null) { cryptoTokenManagementSession.deleteCryptoToken(authenticationToken, cryptoTokenId); } } @Test public void testCommand() throws AuthorizationDeniedException, CryptoTokenOfflineException, CryptoTokenAuthenticationFailedException { final String newPin = "bar123"; String[] args = new String[] { TOKEN_NAME, TOKEN_PIN, newPin }; command.execute(args); cryptoTokenManagementSession.activate(authenticationToken, cryptoTokenId, newPin.toCharArray()); CryptoTokenInfo cryptoTokenInfo = cryptoTokenManagementSession.getCryptoTokenInfo(authenticationToken, cryptoTokenId); assertTrue("Pin was apparently not replaced.", cryptoTokenInfo.isActive()); } @Test public void testRemovePin() throws AuthorizationDeniedException, CryptoTokenOfflineException, CryptoTokenAuthenticationFailedException { String[] args = new String[] { "--token", TOKEN_NAME, "--oldpin", TOKEN_PIN, "--remove" }; command.execute(args); cryptoTokenProxySession.flushCache(); CryptoTokenInfo cryptoTokenInfo = cryptoTokenManagementSession.getCryptoTokenInfo(authenticationToken, cryptoTokenId); assertFalse("Autoactivation was not removed.", cryptoTokenInfo.isAutoActivation()); } }