1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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 }