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