1   package org.apache.fulcrum.jce.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 java.io.ByteArrayOutputStream;
24  import java.io.File;
25  
26  import junit.framework.TestCase;
27  
28  /**
29   * Test suite for crypto functionality
30   *
31   * @author <a href="mailto:siegfried.goeschl@it20one.at">Siegfried Goeschl</a>
32   */
33  
34  public class CryptoUtilTest extends TestCase
35  {
36      /** the password to be used */
37      private String password;
38  
39      /** the test data directory */
40      private File testDataDirectory;
41  
42      /** the temp data director */
43      private File tempDataDirectory;
44  
45      /**
46       * Constructor
47       * @param name the name of the test case
48       */
49      public CryptoUtilTest( String name )
50      {
51          super(name);
52  
53          this.password = "mysecret";
54          this.testDataDirectory = new File( "./src/test/data" );
55          this.tempDataDirectory = new File( "./temp" );
56      }
57  
58      /**
59       * @see junit.framework.TestCase#setUp()
60       *         byte[] salt,
61          int count,
62          String algorithm,
63          String providerName )
64  
65       */
66      protected void setUp() throws Exception
67      {
68          CryptoStreamFactoryImpl factory = new CryptoStreamFactoryImpl(
69              CryptoParameters.SALT,
70              CryptoParameters.COUNT,
71              "PBEWithMD5AndDES",
72              CryptoParameters.PROVIDERNAME
73              );
74  
75          CryptoStreamFactoryImpl.setInstance( factory );
76      }
77  
78      /**
79       * @return Returns the password.
80       */
81      protected char[] getPassword()
82      {
83          return password.toCharArray();
84      }
85  
86      /**
87       * @return Returns the tempDataDirectory.
88       */
89      protected File getTempDataDirectory()
90      {
91          return tempDataDirectory;
92      }
93  
94      /**
95       * @return Returns the testDataDirectory.
96       */
97      protected File getTestDataDirectory()
98      {
99          return testDataDirectory;
100     }
101 
102     /** Encrypt a text file */
103     public void testTextEncryption() throws Exception
104     {
105         File sourceFile = new File( this.getTestDataDirectory(), "plain.txt" );
106         File targetFile = new File( this.getTempDataDirectory(), "plain.enc.txt" );
107         CryptoUtil.encrypt( sourceFile, targetFile, this.getPassword() );
108     }
109 
110     /** Decrypt a text file */
111     public void testTextDecryption() throws Exception
112     {
113         testTextEncryption();
114         File sourceFile = new File( this.getTempDataDirectory(), "plain.enc.txt" );
115         File targetFile = new File( this.getTempDataDirectory(), "plain.dec.txt" );
116         CryptoUtil.decrypt( sourceFile, targetFile, this.getPassword() );
117     }
118 
119     /** Encrypt an empty text file */
120     public void testEmptyTextEncryption() throws Exception
121     {
122         File sourceFile = new File( this.getTestDataDirectory(), "empty.txt" );
123         File targetFile = new File( this.getTempDataDirectory(), "empty.enc.txt" );
124         CryptoUtil.encrypt( sourceFile, targetFile, this.getPassword() );
125     }
126 
127     /** Decrypt a text file */
128     public void testEmptyTextDecryption() throws Exception
129     {
130         testEmptyTextEncryption();
131         File sourceFile = new File( this.getTempDataDirectory(), "empty.enc.txt" );
132         File targetFile = new File( this.getTempDataDirectory(), "empty.dec.txt" );
133         CryptoUtil.decrypt( sourceFile, targetFile, this.getPassword() );
134     }
135 
136     /** Encrypt a PDF file */
137     public void testPdfEncryption() throws Exception
138     {
139         File sourceFile = new File( this.getTestDataDirectory(), "plain.pdf" );
140         File targetFile = new File( this.getTempDataDirectory(), "plain.enc.pdf" );
141         CryptoUtil.encrypt( sourceFile, targetFile, this.getPassword() );
142     }
143 
144     /** Decrypt a PDF file */
145     public void testPdfDecryption() throws Exception
146     {
147         testPdfEncryption();
148         File sourceFile = new File( this.getTempDataDirectory(), "plain.enc.pdf" );
149         File targetFile = new File( this.getTempDataDirectory(), "plain.dec.pdf" );
150         CryptoUtil.decrypt( sourceFile, targetFile, this.getPassword() );
151     }
152 
153     /** Encrypt a ZIP file */
154     public void testZipEncryption() throws Exception
155     {
156         File sourceFile = new File( this.getTestDataDirectory(), "plain.zip" );
157         File targetFile = new File( this.getTempDataDirectory(), "plain.enc.zip" );
158         CryptoUtil.encrypt( sourceFile, targetFile, this.getPassword() );
159     }
160 
161     /** Decrypt a ZIP file */
162     public void testZipDecryption() throws Exception
163     {
164         testZipEncryption();
165         File sourceFile = new File( this.getTempDataDirectory(), "plain.enc.zip" );
166         File targetFile = new File( this.getTempDataDirectory(), "plain.dec.zip" );
167         CryptoUtil.decrypt( sourceFile, targetFile, this.getPassword() );
168     }
169 
170     /** Encrypt a UTF-16 XML file */
171     public void testXmlUTF16Encryption() throws Exception
172     {
173         File sourceFile = new File( this.getTestDataDirectory(), "plain-utf16.xml" );
174         File targetFile = new File( this.getTempDataDirectory(), "plain-utf16.enc.xml" );
175         CryptoUtil.encrypt( sourceFile, targetFile, this.getPassword() );
176     }
177 
178     /** Decrypt a UTF-16 XML file */
179     public void testXMLUTF16Decryption() throws Exception
180     {
181         testXmlUTF16Encryption();
182         File sourceFile = new File( this.getTempDataDirectory(), "plain-utf16.enc.xml" );
183         File targetFile = new File( this.getTempDataDirectory(), "plain-utf16.dec.xml" );
184         CryptoUtil.decrypt( sourceFile, targetFile, this.getPassword() );
185     }
186 
187     /** Encrypt a UTF-8 XML file */
188     public void testXmlUTF8Encryption() throws Exception
189     {
190         File sourceFile = new File( this.getTestDataDirectory(), "plain-utf8.xml" );
191         File targetFile = new File( this.getTempDataDirectory(), "plain-utf8.enc.xml" );
192         CryptoUtil.encrypt( sourceFile, targetFile, this.getPassword() );
193     }
194 
195     /** Decrypt a UTF-8 XML file */
196     public void testXMLUTF8Decryption() throws Exception
197     {
198         testXmlUTF8Encryption();
199         File sourceFile = new File( this.getTempDataDirectory(), "plain-utf8.enc.xml" );
200         File targetFile = new File( this.getTempDataDirectory(), "plain-utf8.dec.xml" );
201         CryptoUtil.decrypt( sourceFile, targetFile, this.getPassword() );
202     }
203 
204     /** Encrypt a ISO-8859-1 XML file */
205     public void testXmlISO88591Encryption() throws Exception
206     {
207         File sourceFile = new File( this.getTestDataDirectory(), "plain-iso-8859-1.xml" );
208         File targetFile = new File( this.getTempDataDirectory(), "plain-iso-8859-1.enc.xml" );
209         CryptoUtil.encrypt( sourceFile, targetFile, this.getPassword() );
210     }
211 
212     /** Decrypt a UTF-8 XML file */
213     public void testXmlISO88591Decryption() throws Exception
214     {
215         testXmlISO88591Encryption();
216         File sourceFile = new File( this.getTempDataDirectory(), "plain-iso-8859-1.enc.xml" );
217         File targetFile = new File( this.getTempDataDirectory(), "plain-iso-8859-1.dec.xml" );
218         CryptoUtil.decrypt( sourceFile, targetFile, this.getPassword() );
219     }
220     /** Test encryption and decryption of Strings */
221     public void testStringEncryption() throws Exception
222     {
223         char[] testVector = new char[513];
224 
225         for( int i=0; i<testVector.length; i++ )
226         {
227             testVector[i] = (char) i;
228         }
229 
230         String source = new String( testVector );
231         String cipherText = CryptoUtil.encryptString( source, this.getPassword() );
232         String plainText = CryptoUtil.decryptString( cipherText, this.getPassword() );
233         assertEquals( source, plainText );
234     }
235 
236     /** Test encryption and decryption of Strings */
237     public void testStringHandling() throws Exception
238     {
239         String source = "Nobody knows the toubles I have seen ...";
240         String cipherText = CryptoUtil.encryptString( source, this.getPassword() );
241         String plainText = CryptoUtil.decryptString( cipherText, this.getPassword() );
242         assertEquals( source, plainText );
243     }
244 
245     /** Test encryption and decryption of binary data */
246     public void testBinaryHandling() throws Exception
247     {
248         byte[] source = new byte[256];
249         byte[] result = null;
250 
251         for( int i=0; i<source.length; i++ )
252         {
253             source[i] = (byte) i;
254         }
255 
256         ByteArrayOutputStream cipherText = new ByteArrayOutputStream();
257         ByteArrayOutputStream plainText = new ByteArrayOutputStream();
258 
259         CryptoUtil.encrypt( source, cipherText, this.getPassword() );
260         CryptoUtil.decrypt( cipherText, plainText, this.getPassword() );
261 
262         result = plainText.toByteArray();
263 
264         for( int i=0; i<source.length; i++ )
265         {
266             if( source[i] != result[i] )
267             {
268                 fail( "Binary data are different at position " + i );
269             }
270         }
271     }
272 
273     /** Test creating a password */
274     public void testPasswordFactory() throws Exception
275     {
276         char[] result = null;
277         result = PasswordFactory.create();
278         result = PasswordFactory.create( this.getPassword() );
279         assertNotNull(result);
280         return;
281     }
282 
283     public void testHexConverter() throws Exception
284     {
285         String source = "DceuATAABWSaVTSIK";
286         String hexString = HexConverter.toString( source.getBytes() );
287         String result = new String( HexConverter.toBytes( hexString ) );
288         assertEquals( source, result );
289     }
290 
291     /** Test encryption and decryption of Strings */
292     public void testPasswordEncryption() throws Exception
293     {
294         char[] password = "57cb-4a23-d838-45222".toCharArray();
295         String source = "e02c-3b76-ff1e-5d9a1";
296         String cipherText = CryptoUtil.encryptString( source, password );
297         String plainText = CryptoUtil.decryptString( cipherText, password );
298         assertEquals( source, plainText );
299     }
300 
301 }