View Javadoc

    1   package org.apache.turbine.services.crypto;
    2
    3   /*
    4    * Licensed to the Apache Software Foundation (ASF) under one
    5    * or more contributor license agreements.  See the NOTICE file
    6    * distributed with this work for additional information
    7    * regarding copyright ownership.  The ASF licenses this file
    8    * to you under the Apache License, Version 2.0 (the
    9    * "License"); you may not use this file except in compliance
    10   * with the License.  You may obtain a copy of the License at
    11   *
    12   *   http://www.apache.org/licenses/LICENSE-2.0
    13   *
    14   * Unless required by applicable law or agreed to in writing,
    15   * software distributed under the License is distributed on an
    16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    17   * KIND, either express or implied.  See the License for the
    18   * specific language governing permissions and limitations
    19   * under the License.
    20   */
    21
    22  import org.apache.fulcrum.crypto.CryptoAlgorithm;
    23  import org.apache.fulcrum.crypto.CryptoService;
    24  import org.apache.turbine.services.ServiceManager;
    25  import org.apache.turbine.services.TurbineServices;
    26  import org.apache.turbine.test.BaseTestCase;
    27  import org.apache.turbine.util.TurbineConfig;
    28  import org.junit.AfterClass;
    29  import org.junit.Before;
    30  import org.junit.BeforeClass;
    31  import org.junit.Test;
    32
    33  import static org.junit.Assert.*;
    34
    35  /**
    36   * Verifies the Fulcrum Crypto Service works properly in Turbine.
    37   *
    38   * @author <a href="mailto:epugh@upstate.com">Eric Pugh</a>
    39   * @author <a href="mailto:Rafal.Krzewski@e-point.pl">Rafal Krzewski</a>
    40   * @author <a href="mailto:sgoeschl@apache.org">Siegfried Goeschl</a>
    41   * @version $Id: CryptoRunningInECMTest.java 222043 2004-12-06 17:47:33Z painter $
    42   */
    43  public class FulcrumCryptoServiceTest extends BaseTestCase
    44  {
    45      private static final String preDefinedInput = "Oeltanks";
    46      private static TurbineConfig tc = null;
    47      private CryptoService cryptoService;
    48
    49
    50
    51      @Test public void testMd5()
    52      {
    53          String preDefinedResult = "XSop0mncK19Ii2r2CUe29w==";
    54
    55          try
    56          {
    57              CryptoAlgorithm ca =cryptoService.getCryptoAlgorithm("default");
    58              ca.setCipher("MD5");
    59              String output = ca.encrypt(preDefinedInput);
    60              assertEquals("MD5 Encryption failed ", preDefinedResult, output);
    61          }
    62          catch (Exception e)
    63          {
    64              e.printStackTrace();
    65              fail();
    66          }
    67      }
    68
    69      @Test public void testSha1()
    70      {
    71          String preDefinedResult = "uVDiJHaavRYX8oWt5ctkaa7j1cw=";
    72
    73          try
    74          {
    75              CryptoAlgorithm ca = cryptoService.getCryptoAlgorithm("default");
    76              ca.setCipher("SHA1");
    77              String output = ca.encrypt(preDefinedInput);
    78              assertEquals("SHA1 Encryption failed ", preDefinedResult, output);
    79          }
    80          catch (Exception e)
    81          {
    82              e.printStackTrace();
    83              fail();
    84          }
    85      }
    86
    87      @BeforeClass
    88      public static void init() throws Exception
    89      {
    90          tc = new TurbineConfig(".", "/conf/test/TestFulcrumComponents.properties");
    91          tc.initialize();
    92
    93      }
    94
    95      @Before
    96      public void setUpBefore() throws Exception
    97      {
    98          ServiceManager serviceManager = TurbineServices.getInstance();
    99          cryptoService = (CryptoService) serviceManager.getService(CryptoService.ROLE);
    100     }
    101
    102     @AfterClass
    103     public static void tearDown() throws Exception
    104     {
    105         if (tc != null)
    106         {
    107             tc.dispose();
    108         }
    109     }
    110 }