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  package org.apache.commons.compress.archivers.zip;
19  
20  import static org.apache.commons.compress.AbstractTest.getFile;
21  import static org.junit.jupiter.api.Assertions.assertEquals;
22  import static org.junit.jupiter.api.Assertions.assertFalse;
23  import static org.junit.jupiter.api.Assertions.assertSame;
24  import static org.junit.jupiter.api.Assertions.assertThrows;
25  import static org.junit.jupiter.api.Assertions.assertTrue;
26  
27  import java.io.File;
28  import java.io.IOException;
29  import java.nio.file.Files;
30  
31  import org.junit.jupiter.api.Test;
32  
33  public class EncryptedArchiveTest {
34  
35      @Test
36      public void testReadPasswordEncryptedEntryViaStream() throws IOException {
37          final File file = getFile("password-encrypted.zip");
38          try (ZipArchiveInputStream zin = new ZipArchiveInputStream(Files.newInputStream(file.toPath()))) {
39              final ZipArchiveEntry zae = zin.getNextZipEntry();
40              assertEquals("LICENSE.txt", zae.getName());
41              assertTrue(zae.getGeneralPurposeBit().usesEncryption());
42              assertFalse(zae.getGeneralPurposeBit().usesStrongEncryption());
43              assertFalse(zin.canReadEntryData(zae));
44              final UnsupportedZipFeatureException ex = assertThrows(UnsupportedZipFeatureException.class, () -> {
45                  final byte[] buf = new byte[1024];
46                  zin.read(buf, 0, buf.length);
47              }, "expected an exception");
48              assertSame(UnsupportedZipFeatureException.Feature.ENCRYPTION, ex.getFeature());
49          }
50      }
51  
52      @Test
53      public void testReadPasswordEncryptedEntryViaZipFile() throws IOException {
54          try (ZipFile zf = ZipFile.builder().setFile(getFile("password-encrypted.zip")).get()) {
55              final ZipArchiveEntry zae = zf.getEntry("LICENSE.txt");
56              assertTrue(zae.getGeneralPurposeBit().usesEncryption());
57              assertFalse(zae.getGeneralPurposeBit().usesStrongEncryption());
58              assertFalse(zf.canReadEntryData(zae));
59              final UnsupportedZipFeatureException ex = assertThrows(UnsupportedZipFeatureException.class, () -> zf.getInputStream(zae), "expected an exception");
60              assertSame(UnsupportedZipFeatureException.Feature.ENCRYPTION, ex.getFeature());
61          }
62      }
63  }