KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > schlichtherle > util > zip > RaesZipTestBase


1 /*
2  * RandomMessageZipTest.java
3  *
4  * Created on 14. Oktober 2005, 16:49
5  */

6 /*
7  * Copyright 2005 Schlichtherle IT Services
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */

21
22 package de.schlichtherle.util.zip;
23
24 import de.schlichtherle.crypto.io.raes.RaesOutputStream;
25 import de.schlichtherle.crypto.io.raes.RaesParameters;
26 import de.schlichtherle.crypto.io.raes.Type0RaesParameters;
27 import de.schlichtherle.crypto.io.raes.RaesReadOnlyFile;
28 import de.schlichtherle.io.rof.ReadOnlyFile;
29
30 import java.io.File JavaDoc;
31 import java.io.IOException JavaDoc;
32 import java.io.OutputStream JavaDoc;
33 import java.io.UnsupportedEncodingException JavaDoc;
34 import java.util.logging.Logger JavaDoc;
35
36 /**
37  * Tests compression and encryption of data.
38  *
39  * @author Christian Schlichtherle
40  */

41 public abstract class RaesZipTestBase extends ZipTestBase {
42
43     private static final Logger JavaDoc logger
44             = Logger.getLogger(RaesZipTestBase.class.getName());
45
46     /** Cipher text shorter than this gets authenticated. */
47     private static int AUTHENTICATION_TRIGGER = 512 * 1024;
48     
49     private static final String JavaDoc PASSWD = "passwd";
50
51     private static final int[] keyStrengths = {
52         Type0RaesParameters.KEY_STRENGTH_128,
53         Type0RaesParameters.KEY_STRENGTH_192,
54         Type0RaesParameters.KEY_STRENGTH_256,
55     };
56
57     private static int createKeyStrength() {
58         final int keyStrength = keyStrengths[rnd.nextInt(keyStrengths.length)];
59         //final int keyStrength = KEY_STRENGTH_ULTRA;
60
logger.fine("Using " + (128 + keyStrength * 64) + " bits cipher key.");
61         return keyStrength;
62     }
63
64     private static final RaesParameters raesParameters = new Type0RaesParameters() {
65         public char[] getOpenPasswd() {
66             return PASSWD.toCharArray();
67         }
68
69         public void invalidOpenPasswd() {
70         }
71
72         public char[] getCreatePasswd() {
73             return PASSWD.toCharArray();
74         }
75
76         public int getKeyStrength() {
77             return createKeyStrength();
78         }
79         
80         public void setKeyStrength(int keyStrength) {
81         }
82     };
83     
84     /** Creates a new instance of RandomMessageZipTest */
85     public RaesZipTestBase(String JavaDoc testName) {
86         super(testName);
87     }
88
89     protected ZipOutputStream createZipOutputStream(final OutputStream out)
90     throws IOException JavaDoc {
91         final RaesOutputStream ros = RaesOutputStream.getInstance(
92                 out, raesParameters);
93         try {
94             return new ZipOutputStream(ros);
95         } catch (NullPointerException JavaDoc exc) {
96             ros.close();
97             throw exc;
98         }
99     }
100
101     protected ZipOutputStream createZipOutputStream(
102             final OutputStream out, final String JavaDoc encoding)
103     throws IOException JavaDoc, UnsupportedEncodingException JavaDoc {
104         final RaesOutputStream ros = RaesOutputStream.getInstance(
105                 out, raesParameters);
106         try {
107             return new ZipOutputStream(ros, encoding);
108         } catch (NullPointerException JavaDoc exc) {
109             ros.close();
110             throw exc;
111         } catch (UnsupportedEncodingException JavaDoc exc) {
112             ros.close();
113             throw exc;
114         }
115     }
116
117     protected ZipFile createZipFile(final String JavaDoc name)
118     throws IOException JavaDoc {
119         return new ZipFile(name) {
120             protected ReadOnlyFile createReadOnlyFile(final File file)
121             throws IOException JavaDoc {
122                 final RaesReadOnlyFile rof = RaesReadOnlyFile.getInstance(
123                         file, raesParameters);
124                 if (rof.length() < AUTHENTICATION_TRIGGER) // heuristic
125
rof.authenticate();
126                 return rof;
127             }
128         };
129     }
130
131     protected ZipFile createZipFile(
132             final String JavaDoc name, final String JavaDoc encoding)
133     throws IOException JavaDoc, UnsupportedEncodingException JavaDoc {
134         return new ZipFile(name, encoding) {
135             protected ReadOnlyFile createReadOnlyFile(final File file)
136             throws IOException JavaDoc {
137                 final RaesReadOnlyFile rof = RaesReadOnlyFile.getInstance(
138                         file, raesParameters);
139                 if (rof.length() < AUTHENTICATION_TRIGGER) // heuristic
140
rof.authenticate();
141                 return rof;
142             }
143         };
144     }
145
146     protected ZipFile createZipFile(final File file)
147     throws IOException JavaDoc {
148         return new ZipFile(file) {
149             protected ReadOnlyFile createReadOnlyFile(final File file)
150             throws IOException JavaDoc {
151                 final RaesReadOnlyFile rof = RaesReadOnlyFile.getInstance(
152                         file, raesParameters);
153                 if (rof.length() < AUTHENTICATION_TRIGGER) // heuristic
154
rof.authenticate();
155                 return rof;
156             }
157         };
158     }
159
160     protected ZipFile createZipFile(
161             final File file, final String JavaDoc encoding)
162     throws IOException JavaDoc, UnsupportedEncodingException JavaDoc {
163         return new ZipFile(file, encoding) {
164             protected ReadOnlyFile createReadOnlyFile(final File file)
165             throws IOException JavaDoc {
166                 final RaesReadOnlyFile rof = RaesReadOnlyFile.getInstance(
167                         file, raesParameters);
168                 if (rof.length() < AUTHENTICATION_TRIGGER) // heuristic
169
rof.authenticate();
170                 return rof;
171             }
172         };
173     }
174
175     protected ZipFile createZipFile(final ReadOnlyFile file)
176     throws IOException JavaDoc {
177         final RaesReadOnlyFile rof;
178         rof = RaesReadOnlyFile.getInstance(file, raesParameters);
179         if (rof.length() < AUTHENTICATION_TRIGGER) // heuristic
180
rof.authenticate();
181         try {
182             return new ZipFile(rof);
183         } catch (NullPointerException JavaDoc exc) {
184             rof.close();
185             throw exc;
186         } catch (IOException JavaDoc exc) {
187             rof.close();
188             throw exc;
189         }
190     }
191
192     protected ZipFile createZipFile(
193             final ReadOnlyFile file, final String JavaDoc encoding)
194     throws NullPointerException JavaDoc, IOException JavaDoc, UnsupportedEncodingException JavaDoc {
195         // Check parameters (fail fast).
196
if (encoding == null)
197             throw new NullPointerException JavaDoc("encoding");
198         new String JavaDoc(new byte[0], encoding); // may throw UnsupportedEncodingException!
199

200         final RaesReadOnlyFile rof;
201         rof = RaesReadOnlyFile.getInstance(file, raesParameters);
202         if (rof.length() < AUTHENTICATION_TRIGGER) // heuristic
203
rof.authenticate();
204         try {
205             return new ZipFile(rof, encoding);
206         } catch (NullPointerException JavaDoc exc) {
207             rof.close();
208             throw exc;
209         } catch (IOException JavaDoc exc) {
210             rof.close();
211             throw exc;
212         }
213     }
214 }
215
Popular Tags