KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > schlichtherle > io > RandomDataZip32RaesTest


1 /*
2  * RandomDataZip32RaesTest.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.io;
23
24 import de.schlichtherle.crypto.io.raes.*;
25 import de.schlichtherle.io.archive.*;
26 import de.schlichtherle.io.archive.zip.raes.*;
27
28 import java.io.IOException JavaDoc;
29
30 import junit.framework.*;
31
32 /**
33  * Tests the TrueZIP API in de.schlichtherle.io with the ZIP.RAES (TZP) driver.
34  *
35  * @author Christian Schlichtherle
36  */

37 public class RandomDataZip32RaesTest extends FileTestBase {
38
39     private static final DefaultArchiveDetector defaultArchiveDetector
40             = new StandardArchiveDetector();
41
42     public static Test suite() throws Exception JavaDoc {
43         TestSuite suite = new TestSuite(RandomDataZip32RaesTest.class);
44         /*TestSuite suite = new TestSuite();
45         suite.addTest(new RandomDataZip32RaesTest("testMultithreading1"));*/

46         
47         return suite;
48     }
49     
50     /**
51      * Creates a new instance of RandomDataFileTest
52      */

53     public RandomDataZip32RaesTest(String JavaDoc testName) {
54         super(testName);
55     }
56     
57     protected void setUp() throws Exception JavaDoc {
58         suffix = ".zip.rae";
59         File.setDefaultArchiveDetector(defaultArchiveDetector);
60
61         super.setUp();
62     }
63
64     public void testCancelling()
65     throws IOException JavaDoc {
66         final ArchiveDetector passwdCancellingDetector
67                 = new PasswdCancellingDetector();
68         File.setDefaultArchiveDetector(passwdCancellingDetector);
69
70         archive = new File(archive.getPath()); // recreate with passwdCancellingDetector
71
final File entry1 = new File(archive, "entry1");
72
73         assertFalse(new java.io.File JavaDoc(archive.getPath()).exists());
74
75         assertFalse(entry1.mkdirs());
76         assertFalse(new java.io.File JavaDoc(entry1.getPath()).exists());
77         assertFalse(new java.io.File JavaDoc(archive.getPath()).exists());
78
79         try {
80             assertFalse(entry1.createNewFile());
81             fail("An IOException should have been thrown because password prompting has been disabled!");
82         } catch (IOException JavaDoc expected) {
83         }
84         assertFalse(new java.io.File JavaDoc(entry1.getPath()).exists());
85         assertFalse(new java.io.File JavaDoc(archive.getPath()).exists());
86
87         final File entry2 = new File(entry1, "entry2");
88         assertFalse(entry2.mkdirs());
89         assertFalse(new java.io.File JavaDoc(entry2.getPath()).exists());
90         assertFalse(new java.io.File JavaDoc(entry1.getPath()).exists());
91         assertFalse(new java.io.File JavaDoc(archive.getPath()).exists());
92
93         try {
94             assertFalse(entry2.createNewFile());
95             fail("An IOException should have been thrown because password prompting has been disabled!");
96         } catch (IOException JavaDoc expected) {
97         }
98         assertFalse(new java.io.File JavaDoc(entry2.getPath()).exists());
99         assertFalse(new java.io.File JavaDoc(entry1.getPath()).exists());
100         assertFalse(new java.io.File JavaDoc(archive.getPath()).exists());
101     }
102
103     public static class StandardArchiveDetector extends DefaultArchiveDetector {
104         private StandardArchiveDetector() {
105             super(ArchiveDetector.NULL,
106                 "zip.rae",
107                 new SafeZip32RaesDriver() {
108                     public RaesParameters getRaesParameters(Archive archive) {
109                         return new Type0RaesParameters() {
110                             public char[] getOpenPasswd() {
111                                 return "test".toCharArray();
112                             }
113
114                             public void invalidOpenPasswd() {
115                             }
116
117                             public char[] getCreatePasswd() {
118                                 return "test".toCharArray();
119                             }
120
121                             public int getKeyStrength() {
122                                 return KEY_STRENGTH_256;
123                             }
124
125                             public void setKeyStrength(int keyStrength) {
126                             }
127                         };
128                     }
129                 }
130             );
131         }
132     }
133
134     public static class PasswdCancellingDetector extends DefaultArchiveDetector {
135         private PasswdCancellingDetector() {
136             super(ArchiveDetector.NULL,
137                 "zip.rae",
138                 new SafeZip32RaesDriver() {
139                     public RaesParameters getRaesParameters(Archive archive) {
140                         return new Type0RaesParameters() {
141                             public char[] getOpenPasswd() throws RaesKeyException {
142                                 throw new RaesKeyException();
143                             }
144
145                             public void invalidOpenPasswd() {
146                                 throw new AssertionError JavaDoc("Password prompting has been cancelled. Hence, this callback is illegal!");
147                             }
148
149                             public char[] getCreatePasswd() throws RaesKeyException {
150                                 throw new RaesKeyException();
151                             }
152
153                             public int getKeyStrength() {
154                                 return KEY_STRENGTH_256;
155                             }
156
157                             public void setKeyStrength(int keyStrength) {
158                             }
159                         };
160                     }
161                 }
162             );
163         }
164     }
165 }
166
Popular Tags