1 6 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 ; 31 import java.io.IOException ; 32 import java.io.OutputStream ; 33 import java.io.UnsupportedEncodingException ; 34 import java.util.logging.Logger ; 35 36 41 public abstract class RaesZipTestBase extends ZipTestBase { 42 43 private static final Logger logger 44 = Logger.getLogger(RaesZipTestBase.class.getName()); 45 46 47 private static int AUTHENTICATION_TRIGGER = 512 * 1024; 48 49 private static final String 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 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 85 public RaesZipTestBase(String testName) { 86 super(testName); 87 } 88 89 protected ZipOutputStream createZipOutputStream(final OutputStream out) 90 throws IOException { 91 final RaesOutputStream ros = RaesOutputStream.getInstance( 92 out, raesParameters); 93 try { 94 return new ZipOutputStream(ros); 95 } catch (NullPointerException exc) { 96 ros.close(); 97 throw exc; 98 } 99 } 100 101 protected ZipOutputStream createZipOutputStream( 102 final OutputStream out, final String encoding) 103 throws IOException , UnsupportedEncodingException { 104 final RaesOutputStream ros = RaesOutputStream.getInstance( 105 out, raesParameters); 106 try { 107 return new ZipOutputStream(ros, encoding); 108 } catch (NullPointerException exc) { 109 ros.close(); 110 throw exc; 111 } catch (UnsupportedEncodingException exc) { 112 ros.close(); 113 throw exc; 114 } 115 } 116 117 protected ZipFile createZipFile(final String name) 118 throws IOException { 119 return new ZipFile(name) { 120 protected ReadOnlyFile createReadOnlyFile(final File file) 121 throws IOException { 122 final RaesReadOnlyFile rof = RaesReadOnlyFile.getInstance( 123 file, raesParameters); 124 if (rof.length() < AUTHENTICATION_TRIGGER) rof.authenticate(); 126 return rof; 127 } 128 }; 129 } 130 131 protected ZipFile createZipFile( 132 final String name, final String encoding) 133 throws IOException , UnsupportedEncodingException { 134 return new ZipFile(name, encoding) { 135 protected ReadOnlyFile createReadOnlyFile(final File file) 136 throws IOException { 137 final RaesReadOnlyFile rof = RaesReadOnlyFile.getInstance( 138 file, raesParameters); 139 if (rof.length() < AUTHENTICATION_TRIGGER) rof.authenticate(); 141 return rof; 142 } 143 }; 144 } 145 146 protected ZipFile createZipFile(final File file) 147 throws IOException { 148 return new ZipFile(file) { 149 protected ReadOnlyFile createReadOnlyFile(final File file) 150 throws IOException { 151 final RaesReadOnlyFile rof = RaesReadOnlyFile.getInstance( 152 file, raesParameters); 153 if (rof.length() < AUTHENTICATION_TRIGGER) rof.authenticate(); 155 return rof; 156 } 157 }; 158 } 159 160 protected ZipFile createZipFile( 161 final File file, final String encoding) 162 throws IOException , UnsupportedEncodingException { 163 return new ZipFile(file, encoding) { 164 protected ReadOnlyFile createReadOnlyFile(final File file) 165 throws IOException { 166 final RaesReadOnlyFile rof = RaesReadOnlyFile.getInstance( 167 file, raesParameters); 168 if (rof.length() < AUTHENTICATION_TRIGGER) rof.authenticate(); 170 return rof; 171 } 172 }; 173 } 174 175 protected ZipFile createZipFile(final ReadOnlyFile file) 176 throws IOException { 177 final RaesReadOnlyFile rof; 178 rof = RaesReadOnlyFile.getInstance(file, raesParameters); 179 if (rof.length() < AUTHENTICATION_TRIGGER) rof.authenticate(); 181 try { 182 return new ZipFile(rof); 183 } catch (NullPointerException exc) { 184 rof.close(); 185 throw exc; 186 } catch (IOException exc) { 187 rof.close(); 188 throw exc; 189 } 190 } 191 192 protected ZipFile createZipFile( 193 final ReadOnlyFile file, final String encoding) 194 throws NullPointerException , IOException , UnsupportedEncodingException { 195 if (encoding == null) 197 throw new NullPointerException ("encoding"); 198 new String (new byte[0], encoding); 200 final RaesReadOnlyFile rof; 201 rof = RaesReadOnlyFile.getInstance(file, raesParameters); 202 if (rof.length() < AUTHENTICATION_TRIGGER) rof.authenticate(); 204 try { 205 return new ZipFile(rof, encoding); 206 } catch (NullPointerException exc) { 207 rof.close(); 208 throw exc; 209 } catch (IOException exc) { 210 rof.close(); 211 throw exc; 212 } 213 } 214 } 215 | Popular Tags |