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