View Javadoc
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.BaseUnit5Test;
24  import org.junit.jupiter.api.BeforeEach;
25  import org.junit.jupiter.api.DisplayName;
26  import org.junit.jupiter.api.Test;
27  import org.junit.jupiter.api.TestInfo;
28  import org.junit.platform.runner.JUnitPlatform;
29  import org.junit.runner.RunWith;
30  
31  import static org.junit.jupiter.api.Assertions.assertEquals;
32  
33  
34  /**
35   * Basic testing of the Container
36   *
37   * @author <a href="mailto:epugh@upstate.com">Eric Pugh</a>
38   * @author <a href="mailto:mcconnell@apache.org">Stephen McConnell</a>
39   * @version $Id: CryptoServiceTest.java 1852158 2019-01-25 18:19:46Z painter $
40   */
41  @DisplayName("Crypto Service Test")
42  @RunWith(JUnitPlatform.class)
43  public class CryptoServiceTest extends BaseUnit5Test
44  {
45      private CryptoService sc = null;
46      private static final String preDefinedInput = "Oeltanks";
47  
48      /**
49       * Constructor for test.
50       *
51       * @param testInfo Junit 5 info object of the test being executed
52       */
53      public CryptoServiceTest(TestInfo testInfo)
54      {
55  
56      }
57  
58      @BeforeEach
59      public void setUp() throws Exception
60      {
61          sc = (CryptoService) this.lookup( CryptoService.ROLE );
62      }
63  
64  
65      @Test
66      public void testUnixCrypt() throws Exception
67      {
68          String preDefinedSeed = "z5";
69          String preDefinedResult = "z5EQaXpuu059c";
70  
71          CryptoAlgorithm ca = sc.getCryptoAlgorithm("unix");
72          
73          /*
74           * Test predefined Seed
75           */
76          ca.setSeed(preDefinedSeed);
77          String output = ca.encrypt(preDefinedInput);
78          assertEquals( preDefinedResult, output, "Encryption failed ");
79          
80          /*
81           * Test random Seed
82           *
83           */
84          ca.setSeed(null);
85          String result = ca.encrypt(preDefinedInput);
86          ca.setSeed(result);
87          output = ca.encrypt(preDefinedInput);
88          assertEquals( output, result, "Encryption failed ");
89  
90      }
91      
92      /**
93       * Test no encryption 
94       * @throws Exception exception
95       */
96      @Test
97      public void testClearCrypt() throws Exception
98      {
99          String preDefinedResult = "Oeltanks";
100 
101         CryptoAlgorithm ca = sc.getCryptoAlgorithm("clear");
102         String output = ca.encrypt(preDefinedInput);
103         assertEquals( preDefinedResult, output, "Encryption failed ");
104 
105     }
106     
107     /**
108      * @throws Exception exception
109      */
110     @Test
111     @DisplayName("OldJavaCrypt: Truncated base64 from MD5 (Turbine 2.1) Test")
112     public void testOldJavaCryptMd5() throws Exception
113     {
114         String preDefinedResult = "XSop0mncK19Ii2r2CUe2";
115 
116         CryptoAlgorithm ca = sc.getCryptoAlgorithm("oldjava");
117         ca.setCipher("MD5");
118         String output = ca.encrypt(preDefinedInput);
119         assertEquals( preDefinedResult, output, "MD5 Encryption failed ");
120 
121     }
122     
123     /**
124      * @throws Exception exception
125      */
126     @Test
127     public void testOldJavaCryptSha1() throws Exception
128     {
129         String preDefinedResult = "uVDiJHaavRYX8oWt5ctkaa7j";
130 
131         CryptoAlgorithm ca = sc.getCryptoAlgorithm("oldjava");
132         ca.setCipher("SHA1");
133         String output = ca.encrypt(preDefinedInput);
134         assertEquals( preDefinedResult, output, "SHA1 Encryption failed ");
135 
136     }
137     
138     /**
139      * @throws Exception exception
140      */
141     @Test
142     public void testJavaCryptMd5() throws Exception
143     {
144         String preDefinedResult = "XSop0mncK19Ii2r2CUe29w==";
145         CryptoAlgorithm ca = sc.getCryptoAlgorithm("java");
146         ca.setCipher("MD5");
147         String output = ca.encrypt(preDefinedInput);
148         assertEquals( preDefinedResult, output, "MD5 Encryption failed ");
149     }
150     
151     /**
152      * @throws Exception exception
153      */
154     @Test
155     public void testJavaCryptSha1() throws Exception
156     {
157         String preDefinedResult = "uVDiJHaavRYX8oWt5ctkaa7j1cw=";
158         CryptoAlgorithm ca = sc.getCryptoAlgorithm("java");
159         ca.setCipher("SHA1");
160         String output = ca.encrypt(preDefinedInput);
161         assertEquals( preDefinedResult, output, "SHA1 Encryption failed ");
162 
163     }
164     
165     /**
166      * @throws Exception exception
167      */
168     @Test
169     public void testJavaCryptSha256() throws Exception
170     {
171         String preDefinedResult = "XBSqev4ilv7P7852G2rL5WgX3FLy8VzfOY+tVq+xjek=";
172         CryptoAlgorithm ca = sc.getCryptoAlgorithm("java");
173         ca.setCipher("SHA-256");
174         String output = ca.encrypt(preDefinedInput);
175         assertEquals( preDefinedResult, output, "SHA256 Encryption failed ");
176     }
177     
178     /**
179      * @throws Exception exception
180      */
181     @Test
182     public void testJavaCryptSha512() throws Exception
183     {
184         String preDefinedResult = "QlxxOMtVn0FFAXF+DRQl8o/b+WKEG6Nc7QRdqf/LTTz/+bOaoE/JihM8uJqTW7JQm/l/TmnmVKuLaD7jdVAtJw==";
185         CryptoAlgorithm ca = sc.getCryptoAlgorithm("java");
186         ca.setCipher("SHA-512");
187         String output = ca.encrypt(preDefinedInput);
188         assertEquals( preDefinedResult, output, "SHA512 Encryption failed ");
189     }
190 }