1   package org.apache.fulcrum.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  
23  import org.apache.fulcrum.testcontainer.BaseUnitTest;
24  
25  /**
26   * Basic testing of the Container
27   *
28   * @author <a href="mailto:epugh@upstate.com">Eric Pugh</a>
29   * @author <a href="mailto:mcconnell@apache.org">Stephen McConnell</a>
30   * @version $Id: CryptoServiceTest.java 535465 2007-05-05 06:58:06Z tv $
31   */
32  public class CryptoServiceTest extends BaseUnitTest
33  {
34      private CryptoService sc = null;
35      private static final String preDefinedInput = "Oeltanks";
36  
37      /**
38       * Constructor for test.
39       *
40       * @param testName name of the test being executed
41       */
42      public CryptoServiceTest(String testName)
43      {
44          super(testName);
45      }
46  
47  
48      public void setUp() throws Exception
49      {
50          super.setUp();
51          sc = (CryptoService) this.lookup( CryptoService.ROLE );
52  
53      }
54  
55      public void testUnixCrypt() throws Exception
56      {
57          String preDefinedSeed = "z5";
58          String preDefinedResult = "z5EQaXpuu059c";
59  
60          CryptoAlgorithm ca = sc.getCryptoAlgorithm("unix");
61          /*
62           * Test predefined Seed
63           */
64          ca.setSeed(preDefinedSeed);
65          String output = ca.encrypt(preDefinedInput);
66          assertEquals("Encryption failed ", preDefinedResult, output);
67          /*
68           * Test random Seed
69           *
70           */
71          ca.setSeed(null);
72          String result = ca.encrypt(preDefinedInput);
73          ca.setSeed(result);
74          output = ca.encrypt(preDefinedInput);
75          assertEquals("Encryption failed ", output, result);
76  
77  
78  
79      }
80  
81      public void testClearCrypt() throws Exception
82      {
83          String preDefinedResult = "Oeltanks";
84  
85          CryptoAlgorithm ca = sc.getCryptoAlgorithm("clear");
86          String output = ca.encrypt(preDefinedInput);
87          assertEquals("Encryption failed ", preDefinedResult, output);
88  
89      }
90  
91      public void testOldJavaCryptMd5() throws Exception
92      {
93          String preDefinedResult = "XSop0mncK19Ii2r2CUe2";
94  
95          CryptoAlgorithm ca = sc.getCryptoAlgorithm("oldjava");
96          ca.setCipher("MD5");
97          String output = ca.encrypt(preDefinedInput);
98          assertEquals("MD5 Encryption failed ", preDefinedResult, output);
99  
100     }
101     public void testOldJavaCryptSha1() throws Exception
102     {
103         String preDefinedResult = "uVDiJHaavRYX8oWt5ctkaa7j";
104 
105         CryptoAlgorithm ca = sc.getCryptoAlgorithm("oldjava");
106         ca.setCipher("SHA1");
107         String output = ca.encrypt(preDefinedInput);
108         assertEquals("SHA1 Encryption failed ", preDefinedResult, output);
109 
110     }
111     public void testJavaCryptMd5() throws Exception
112     {
113         String preDefinedResult = "XSop0mncK19Ii2r2CUe29w==";
114         CryptoAlgorithm ca = sc.getCryptoAlgorithm("java");
115         ca.setCipher("MD5");
116         String output = ca.encrypt(preDefinedInput);
117         assertEquals("MD5 Encryption failed ", preDefinedResult, output);
118     }
119 
120     public void testJavaCryptSha1() throws Exception
121     {
122         String preDefinedResult = "uVDiJHaavRYX8oWt5ctkaa7j1cw=";
123         CryptoAlgorithm ca = sc.getCryptoAlgorithm("java");
124         ca.setCipher("SHA1");
125         String output = ca.encrypt(preDefinedInput);
126         assertEquals("SHA1 Encryption failed ", preDefinedResult, output);
127 
128     }
129 }