View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one or more
3    *  contributor license agreements.  See the NOTICE file distributed with
4    *  this work for additional information regarding copyright ownership.
5    *  The ASF licenses this file to You under the Apache License, Version 2.0
6    *  (the "License"); you may not use this file except in compliance with
7    *  the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   *
17   */
18  
19  package org.apache.commons.compress.archivers.zip;
20  
21  import static org.apache.commons.compress.AbstractTestCase.getFile;
22  
23  import java.io.File;
24  import java.io.FileInputStream;
25  import java.io.IOException;
26  
27  import junit.framework.TestCase;
28  
29  public class EncryptedArchiveTest extends TestCase {
30  
31      public void testReadPasswordEncryptedEntryViaZipFile()
32          throws IOException {
33          File file = getFile("password-encrypted.zip");
34          ZipFile zf = null;
35          try {
36              zf = new ZipFile(file);
37              ZipArchiveEntry zae = zf.getEntry("LICENSE.txt");
38              assertTrue(zae.getGeneralPurposeBit().usesEncryption());
39              assertFalse(zae.getGeneralPurposeBit().usesStrongEncryption());
40              assertFalse(zf.canReadEntryData(zae));
41              try {
42                  zf.getInputStream(zae);
43                  fail("expected an exception");
44              } catch (UnsupportedZipFeatureException ex) {
45                  assertSame(UnsupportedZipFeatureException.Feature.ENCRYPTION,
46                             ex.getFeature());
47              }
48          } finally {
49              ZipFile.closeQuietly(zf);
50          }
51      }
52  
53      public void testReadPasswordEncryptedEntryViaStream()
54          throws IOException {
55          File file = getFile("password-encrypted.zip");
56          ZipArchiveInputStream zin = null;
57          try {
58              zin = new ZipArchiveInputStream(new FileInputStream(file));
59              ZipArchiveEntry zae = zin.getNextZipEntry();
60              assertEquals("LICENSE.txt", zae.getName());
61              assertTrue(zae.getGeneralPurposeBit().usesEncryption());
62              assertFalse(zae.getGeneralPurposeBit().usesStrongEncryption());
63              assertFalse(zin.canReadEntryData(zae));
64              try {
65                  byte[] buf = new byte[1024];
66                  zin.read(buf, 0, buf.length);
67                  fail("expected an exception");
68              } catch (UnsupportedZipFeatureException ex) {
69                  assertSame(UnsupportedZipFeatureException.Feature.ENCRYPTION,
70                             ex.getFeature());
71              }
72          } finally {
73              if (zin != null) {
74                  zin.close();
75              }
76          }
77      }
78  }