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.ByteArrayInputStream;
24  import java.io.ByteArrayOutputStream;
25  import java.io.File;
26  import java.io.FileInputStream;
27  
28  import junit.framework.TestCase;
29  
30  /**
31   * Test suite for SmartDecryptingInputStream
32   *
33   * @author <a href="mailto:siegfried.goeschl@it20one.at">Siegfried Goeschl</a>
34   */
35  
36  public class SmartDecryptingInputStreamTest extends TestCase
37  {
38      /** the password to be used */
39      private String password;
40  
41      /** the test data directory */
42      private File testDataDirectory;
43  
44      /** the temp data director */
45      private File tempDataDirectory;
46  
47      /**
48       * Constructor
49       * @param name the name of the test case
50       */
51      public SmartDecryptingInputStreamTest( String name )
52      {
53          super(name);
54  
55          this.password = "mysecret";
56          this.testDataDirectory = new File( "./src/test/data" );
57          this.tempDataDirectory = new File( "./temp" );
58      }
59  
60      /**
61       * @see junit.framework.TestCase#setUp()
62       */
63      protected void setUp() throws Exception
64      {
65          CryptoStreamFactoryImpl factory = new CryptoStreamFactoryImpl(
66              CryptoParameters.SALT,
67              CryptoParameters.COUNT,
68              "PBEWithMD5AndDES",
69              CryptoParameters.PROVIDERNAME
70              );
71  
72          CryptoStreamFactoryImpl.setInstance( factory );
73      }
74  
75      /**
76       * @return Returns the password.
77       */
78      protected char[] getPassword()
79      {
80          return password.toCharArray();
81      }
82  
83      /**
84       * @return Returns the tempDataDirectory.
85       */
86      protected File getTempDataDirectory()
87      {
88          return tempDataDirectory;
89      }
90  
91      /**
92       * @return Returns the testDataDirectory.
93       */
94      protected File getTestDataDirectory()
95      {
96          return testDataDirectory;
97      }
98  
99      public void testSmartEmtpyDecryption() throws Exception
100     {
101         this.testSmartDecryption("empty.txt","ISO-8859-1");
102     }
103 
104     public void testSmartTextDecryption() throws Exception
105     {
106         this.testSmartDecryption("plain.txt","ISO-8859-1");
107     }
108 
109     public void testSmartGroovyDecryption() throws Exception
110     {
111         this.testSmartDecryption("plain.groovy","ISO-8859-1");
112     }
113 
114     public void testSmartXmlIso8859Utf8Decryption() throws Exception
115     {
116         this.testSmartDecryption("plain-iso-8859-1.xml","ISO-8859-1");
117     }
118 
119     public void testSmartXmlUtf8Decryption() throws Exception
120     {
121         this.testSmartDecryption("plain-utf8.xml","UTF-8");
122     }
123 
124     public void testSmartXmlUtf16Decryption() throws Exception
125     {
126         this.testSmartDecryption("plain-utf16.xml","UTF-16");
127     }
128 
129     public void testPDFDecryption() throws Exception
130     {
131         this.testSmartDecryption("plain.pdf","ISO-8859-1");
132     }
133 
134     public void testZIPDecryption() throws Exception
135     {
136         this.testSmartDecryption("plain.zip","ISO-8859-1");
137     }
138 
139     /** Test smart decryption for a given file */
140     private void testSmartDecryption( String fileName, String enc ) throws Exception
141     {
142         File sourceFile = new File( this.getTestDataDirectory(), fileName );
143         String plainText = this.loadTextFile(sourceFile,enc);
144         String smartText = this.smartDecrypt(sourceFile,enc);
145         byte[] cipherText = this.encryptTextFile(sourceFile);
146         String decryptedText = this.smartDecrypt(cipherText,enc);
147 
148         assertTrue( plainText.length() == smartText.length() );
149         assertTrue( plainText.length() == decryptedText.length() );
150         assertEquals( plainText, smartText );
151         assertEquals( plainText, decryptedText );
152     }
153 
154     /**
155      * Loads a plain text file.
156      * @param file the file to load
157      */
158     private String loadTextFile( File file, String enc ) throws Exception
159     {
160         String result = null;
161         FileInputStream fis = new FileInputStream( file );
162         ByteArrayOutputStream baos = new ByteArrayOutputStream();
163         CryptoUtil.copy(fis,baos);
164         fis.close();
165         result = new String( baos.toByteArray(), enc );
166         return result;
167     }
168 
169     /**
170      * Encrypt a plain text file.
171      * @param file the file to encrypt
172      */
173     private byte[] encryptTextFile( File file ) throws Exception
174     {
175         ByteArrayOutputStream baos = new ByteArrayOutputStream();
176         FileInputStream fis = new FileInputStream( file );
177 
178         CryptoUtil.encrypt(
179             CryptoStreamFactoryImpl.getInstance(),
180             fis,
181             baos,
182             this.getPassword()
183             );
184 
185         fis.close();
186 
187         return baos.toByteArray();
188     }
189 
190     /**
191      * Use smart decryption on a cipherText.
192      *
193      * @param cipherText the encrypted text
194      * @return the decrypeted content
195      */
196     private String smartDecrypt( byte[] cipherText, String enc ) throws Exception
197     {
198         ByteArrayInputStream bais = new ByteArrayInputStream(cipherText);
199         ByteArrayOutputStream baos = new ByteArrayOutputStream();
200 
201         SmartDecryptingInputStream sdis = new SmartDecryptingInputStream(
202             CryptoStreamFactoryImpl.getInstance(),
203             bais,
204             this.getPassword()
205             );
206 
207         CryptoUtil.copy(sdis,baos);
208 
209         return new String( baos.toByteArray(), enc );
210     }
211 
212     /**
213      * Use smart decryption on a plain text file.
214      *
215      * @param file the file to load
216      * @return the content
217      */
218     private String smartDecrypt( File file, String enc ) throws Exception
219     {
220         ByteArrayOutputStream baos = new ByteArrayOutputStream();
221         FileInputStream fis = new FileInputStream( file );
222 
223         SmartDecryptingInputStream sdis = new SmartDecryptingInputStream(
224             CryptoStreamFactoryImpl.getInstance(),
225             fis,
226             this.getPassword()
227             );
228 
229         CryptoUtil.copy(sdis,baos);
230         return new String( baos.toByteArray(), enc );
231     }
232 
233 }