/************************************************************************* * * * 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.ra; import static org.junit.Assert.assertEquals; import java.util.Date; import org.cesecore.authentication.tokens.AuthenticationToken; import org.cesecore.authentication.tokens.UsernamePrincipal; import org.cesecore.authorization.AuthorizationDeniedException; import org.cesecore.certificates.ca.CaSessionRemote; import org.cesecore.certificates.ca.X509CA; import org.cesecore.certificates.certificateprofile.CertificateProfileConstants; import org.cesecore.certificates.endentity.EndEntityConstants; import org.cesecore.certificates.endentity.EndEntityInformation; import org.cesecore.certificates.endentity.EndEntityType; import org.cesecore.certificates.endentity.EndEntityTypes; 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.core.ejb.ra.EndEntityAccessSessionRemote; import org.ejbca.core.ejb.ra.EndEntityManagementSessionRemote; import org.ejbca.core.model.SecConst; 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: SetCleartextPasswordCommandTest.java 27422 2017-12-05 14:05:42Z bastianf $ * */ public class SetCleartextPasswordCommandTest { private static final String TESTCLASS_NAME = KeyRecoveryNewestCommandTest.class.getSimpleName(); private static final String END_ENTITY_SUBJECT_DN = "C=SE, O=PrimeKey, CN=" + TESTCLASS_NAME; private SetCleartextPasswordCommand command = new SetCleartextPasswordCommand(); private static final CaSessionRemote caSession = EjbRemoteHelper.INSTANCE.getRemoteSession(CaSessionRemote.class); private static final CryptoTokenManagementSessionRemote cryptoTokenManagementSession = EjbRemoteHelper.INSTANCE .getRemoteSession(CryptoTokenManagementSessionRemote.class); private final EndEntityAccessSessionRemote endEntityAccessSession = EjbRemoteHelper.INSTANCE.getRemoteSession(EndEntityAccessSessionRemote.class); private final EndEntityManagementSessionRemote endEntityManagementSession = EjbRemoteHelper.INSTANCE .getRemoteSession(EndEntityManagementSessionRemote.class); private static final AuthenticationToken authenticationToken = new TestAlwaysAllowLocalAuthenticationToken(new UsernamePrincipal(TESTCLASS_NAME)); private static X509CA x509ca = null; @BeforeClass public static void beforeClass() throws Exception { CryptoProviderTools.installBCProvider(); x509ca = CryptoTokenTestUtils.createTestCAWithSoftCryptoToken(authenticationToken, "C=SE,CN=" + TESTCLASS_NAME); } @AfterClass public static void afterClass() throws Exception { 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 { final EndEntityInformation userdata = new EndEntityInformation(TESTCLASS_NAME, END_ENTITY_SUBJECT_DN, x509ca.getCAId(), null, null, EndEntityConstants.STATUS_NEW, new EndEntityType(EndEntityTypes.ENDUSER), EndEntityConstants.EMPTY_END_ENTITY_PROFILE, CertificateProfileConstants.CERTPROFILE_FIXED_ENDUSER, new Date(), new Date(), SecConst.TOKEN_SOFT_P12, 0, null); userdata.setPassword("foo123"); endEntityManagementSession.addUser(authenticationToken, userdata, true); if (null == endEntityAccessSession.findUser(authenticationToken, TESTCLASS_NAME)) { throw new RuntimeException("Could not create end entity."); } } @After public void tearDown() throws Exception { if (null != endEntityAccessSession.findUser(authenticationToken, TESTCLASS_NAME)) { endEntityManagementSession.deleteUser(authenticationToken, TESTCLASS_NAME); } } @Test public void testSetCleartextPassword() throws AuthorizationDeniedException { final String newPassword = "bar123"; final String args[] = new String[] { TESTCLASS_NAME, "--password", newPassword }; assertEquals(CommandResult.SUCCESS, command.execute(args)); assertEquals("Password was not set.", newPassword, endEntityAccessSession.findUser(authenticationToken, TESTCLASS_NAME).getPassword()); } }