View Javadoc
1   package org.apache.fulcrum.crypto;
2   
3   import static org.junit.jupiter.api.Assertions.assertEquals;
4   
5   /*
6    * Licensed to the Apache Software Foundation (ASF) under one
7    * or more contributor license agreements.  See the NOTICE file
8    * distributed with this work for additional information
9    * regarding copyright ownership.  The ASF licenses this file
10   * to you under the Apache License, Version 2.0 (the
11   * "License"); you may not use this file except in compliance
12   * with the License.  You may obtain a copy of the License at
13   *
14   *   http://www.apache.org/licenses/LICENSE-2.0
15   *
16   * Unless required by applicable law or agreed to in writing,
17   * software distributed under the License is distributed on an
18   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
19   * KIND, either express or implied.  See the License for the
20   * specific language governing permissions and limitations
21   * under the License.
22   */
23  
24  
25  import org.apache.fulcrum.testcontainer.BaseUnit5Test;
26  import org.junit.jupiter.api.BeforeEach;
27  import org.junit.jupiter.api.DisplayName;
28  import org.junit.jupiter.api.Test;
29  import org.junit.jupiter.api.TestInfo;
30  
31  
32  /**
33   * Basic testing of the Container
34   *
35   * @author <a href="mailto:epugh@upstate.com">Eric Pugh</a>
36   * @author <a href="mailto:mcconnell@apache.org">Stephen McConnell</a>
37   * @version $Id$
38   */
39  @DisplayName("Crypto Service Test")
40  public class CryptoServiceTest extends BaseUnit5Test
41  {
42      private CryptoService sc = null;
43      private static final String preDefinedInput = "Oeltanks";
44  
45      /**
46       * Constructor for test.
47       *
48       * @param testInfo Junit 5 info object of the test being executed
49       */
50      public CryptoServiceTest(TestInfo testInfo)
51      {
52  
53      }
54  
55      @BeforeEach
56      public void setUp() throws Exception
57      {
58          sc = (CryptoService) this.lookup( CryptoService.ROLE );
59      }
60  
61  
62      @Test
63      public void testUnixCrypt() throws Exception
64      {
65          String preDefinedSeed = "z5";
66          String preDefinedResult = "z5EQaXpuu059c";
67  
68          CryptoAlgorithm ca = sc.getCryptoAlgorithm("unix");
69          
70          /*
71           * Test predefined Seed
72           */
73          ca.setSeed(preDefinedSeed);
74          String output = ca.encrypt(preDefinedInput);
75          assertEquals( preDefinedResult, output, "Encryption failed ");
76          
77          /*
78           * Test random Seed
79           *
80           */
81          ca.setSeed(null);
82          String result = ca.encrypt(preDefinedInput);
83          ca.setSeed(result);
84          output = ca.encrypt(preDefinedInput);
85          assertEquals( output, result, "Encryption failed ");
86  
87      }
88      
89      /**
90       * Test no encryption 
91       * @throws Exception exception
92       */
93      @Test
94      public void testClearCrypt() throws Exception
95      {
96          String preDefinedResult = "Oeltanks";
97  
98          CryptoAlgorithm ca = sc.getCryptoAlgorithm("clear");
99          String output = ca.encrypt(preDefinedInput);
100         assertEquals( preDefinedResult, output, "Encryption failed ");
101 
102     }
103     
104     /**
105      * @throws Exception exception
106      */
107     @Test
108     @DisplayName("OldJavaCrypt: Truncated base64 from MD5 (Turbine 2.1) Test")
109     public void testOldJavaCryptMd5() throws Exception
110     {
111         String preDefinedResult = "XSop0mncK19Ii2r2CUe2";
112 
113         CryptoAlgorithm ca = sc.getCryptoAlgorithm("oldjava");
114         ca.setCipher("MD5");
115         String output = ca.encrypt(preDefinedInput);
116         assertEquals( preDefinedResult, output, "MD5 Encryption failed ");
117 
118     }
119     
120     /**
121      * @throws Exception exception
122      */
123     @Test
124     public void testOldJavaCryptSha1() throws Exception
125     {
126         String preDefinedResult = "uVDiJHaavRYX8oWt5ctkaa7j";
127 
128         CryptoAlgorithm ca = sc.getCryptoAlgorithm("oldjava");
129         ca.setCipher("SHA1");
130         String output = ca.encrypt(preDefinedInput);
131         assertEquals( preDefinedResult, output, "SHA1 Encryption failed ");
132 
133     }
134     
135     /**
136      * @throws Exception exception
137      */
138     @Test
139     public void testJavaCryptMd5() throws Exception
140     {
141         String preDefinedResult = "XSop0mncK19Ii2r2CUe29w==";
142         CryptoAlgorithm ca = sc.getCryptoAlgorithm("java");
143         ca.setCipher("MD5");
144         String output = ca.encrypt(preDefinedInput);
145         assertEquals( preDefinedResult, output, "MD5 Encryption failed ");
146     }
147     
148     /**
149      * @throws Exception exception
150      */
151     @Test
152     public void testJavaCryptSha1() throws Exception
153     {
154         String preDefinedResult = "uVDiJHaavRYX8oWt5ctkaa7j1cw=";
155         CryptoAlgorithm ca = sc.getCryptoAlgorithm("java");
156         ca.setCipher("SHA1");
157         String output = ca.encrypt(preDefinedInput);
158         assertEquals( preDefinedResult, output, "SHA1 Encryption failed ");
159 
160     }
161     
162     /**
163      * @throws Exception exception
164      */
165     @Test
166     public void testJavaCryptSha256() throws Exception
167     {
168         String preDefinedResult = "XBSqev4ilv7P7852G2rL5WgX3FLy8VzfOY+tVq+xjek=";
169         CryptoAlgorithm ca = sc.getCryptoAlgorithm("java");
170         ca.setCipher("SHA-256");
171         String output = ca.encrypt(preDefinedInput);
172         assertEquals( preDefinedResult, output, "SHA256 Encryption failed ");
173     }
174     
175     /**
176      * @throws Exception exception
177      */
178     @Test
179     public void testJavaCryptSha512() throws Exception
180     {
181         String preDefinedResult = "QlxxOMtVn0FFAXF+DRQl8o/b+WKEG6Nc7QRdqf/LTTz/+bOaoE/JihM8uJqTW7JQm/l/TmnmVKuLaD7jdVAtJw==";
182         CryptoAlgorithm ca = sc.getCryptoAlgorithm("java");
183         ca.setCipher("SHA-512");
184         String output = ca.encrypt(preDefinedInput);
185         assertEquals( preDefinedResult, output, "SHA512 Encryption failed ");
186     }
187 }