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   * http://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  package org.apache.commons.compress.compressors;
20  
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.assertTrue;
24  
25  import java.io.File;
26  import java.io.InputStream;
27  import java.io.OutputStream;
28  import java.nio.file.Files;
29  import java.util.ArrayList;
30  import java.util.HashMap;
31  import java.util.List;
32  import java.util.Map;
33  
34  import org.apache.commons.compress.AbstractTest;
35  import org.apache.commons.compress.archivers.ArchiveEntry;
36  import org.apache.commons.compress.archivers.ArchiveInputStream;
37  import org.apache.commons.compress.archivers.ArchiveStreamFactory;
38  import org.apache.commons.compress.archivers.jar.JarArchiveOutputStream;
39  import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
40  import org.apache.commons.compress.compressors.pack200.Pack200CompressorInputStream;
41  import org.apache.commons.compress.compressors.pack200.Pack200CompressorOutputStream;
42  import org.apache.commons.compress.compressors.pack200.Pack200Strategy;
43  import org.apache.commons.io.IOUtils;
44  import org.junit.jupiter.api.Test;
45  
46  public final class Pack200Test extends AbstractTest {
47  
48      private void jarArchiveCreation(final Pack200Strategy mode) throws Exception {
49          final File output = newTempFile("bla.pack");
50  
51          final File file1 = getFile("test1.xml");
52          final File file2 = getFile("test2.xml");
53  
54          try (OutputStream out = new Pack200CompressorOutputStream(Files.newOutputStream(output.toPath()), mode);
55                  JarArchiveOutputStream os = ArchiveStreamFactory.DEFAULT.createArchiveOutputStream("jar", out)) {
56  
57              os.putArchiveEntry(new ZipArchiveEntry("testdata/test1.xml"));
58              Files.copy(file1.toPath(), os);
59              os.closeArchiveEntry();
60  
61              os.putArchiveEntry(new ZipArchiveEntry("testdata/test2.xml"));
62              Files.copy(file2.toPath(), os);
63              os.closeArchiveEntry();
64          }
65  
66          try (InputStream is = new Pack200CompressorInputStream(output);
67                  ArchiveInputStream<?> in = ArchiveStreamFactory.DEFAULT.createArchiveInputStream("jar", is)) {
68              final List<String> files = new ArrayList<>();
69              files.add("testdata/test1.xml");
70              files.add("testdata/test2.xml");
71              checkArchiveContent(in, files);
72          }
73      }
74  
75      private void jarUnarchiveAll(final boolean useFile, final Pack200Strategy mode) throws Exception {
76          final File input = getFile("bla.pack");
77          try (InputStream is = useFile ? new Pack200CompressorInputStream(input, mode)
78                  : new Pack200CompressorInputStream(Files.newInputStream(input.toPath()), mode);
79                  ArchiveInputStream<?> in = ArchiveStreamFactory.DEFAULT.createArchiveInputStream("jar", is)) {
80  
81              ArchiveEntry entry = in.getNextEntry();
82              while (entry != null) {
83                  final File archiveEntry = newTempFile(entry.getName());
84                  archiveEntry.getParentFile().mkdirs();
85                  if (entry.isDirectory()) {
86                      archiveEntry.mkdir();
87                      entry = in.getNextEntry();
88                      continue;
89                  }
90                  Files.copy(in, archiveEntry.toPath());
91                  entry = in.getNextEntry();
92              }
93          }
94      }
95  
96      private void multiByteReadConsistentlyReturnsMinusOneAtEof(final Pack200Strategy s) throws Exception {
97          final File input = getFile("bla.pack");
98          final byte[] buf = new byte[2];
99          try (Pack200CompressorInputStream in = new Pack200CompressorInputStream(input, s)) {
100             IOUtils.toByteArray(in);
101             assertEquals(-1, in.read(buf));
102             assertEquals(-1, in.read(buf));
103         }
104     }
105 
106     private void singleByteReadConsistentlyReturnsMinusOneAtEof(final Pack200Strategy s) throws Exception {
107         final File input = getFile("bla.pack");
108         try (Pack200CompressorInputStream in = new Pack200CompressorInputStream(input, s)) {
109             IOUtils.toByteArray(in);
110             assertEquals(-1, in.read());
111             assertEquals(-1, in.read());
112         }
113     }
114 
115     @Test
116     public void testBadSignature() throws Exception {
117         try (InputStream is = newInputStream("bla.jar")) {
118             final byte[] sig = new byte[4];
119             is.read(sig);
120             assertFalse(Pack200CompressorInputStream.matches(sig, 4));
121         }
122     }
123 
124     @Test
125     public void testGoodSignature() throws Exception {
126         try (InputStream is = newInputStream("bla.pack")) {
127             final byte[] sig = new byte[4];
128             is.read(sig);
129             assertTrue(Pack200CompressorInputStream.matches(sig, 4));
130         }
131     }
132 
133     @Test
134     public void testInputStreamMethods() throws Exception {
135         final Map<String, String> m = new HashMap<>();
136         m.put("foo", "bar");
137         try (InputStream is = new Pack200CompressorInputStream(newInputStream("bla.jar"), m)) {
138             // packed file is a jar, which is a ZIP, so it starts with
139             // a local file header
140             assertTrue(is.markSupported());
141             is.mark(5);
142             assertEquals(0x50, is.read());
143             final byte[] rest = new byte[3];
144             assertEquals(3, is.read(rest));
145             assertEquals(0x4b, rest[0]);
146             assertEquals(3, rest[1]);
147             assertEquals(4, rest[2]);
148             assertEquals(1, is.skip(1));
149             is.reset();
150             assertEquals(0x50, is.read());
151             assertTrue(is.available() > 0);
152         }
153     }
154 
155     @Test
156     public void testJarArchiveCreationInMemory() throws Exception {
157         jarArchiveCreation(Pack200Strategy.IN_MEMORY);
158     }
159 
160     @Test
161     public void testJarArchiveCreationTempFile() throws Exception {
162         jarArchiveCreation(Pack200Strategy.TEMP_FILE);
163     }
164 
165     @Test
166     public void testJarUnarchiveAllFileArgInMemory() throws Exception {
167         jarUnarchiveAll(true, Pack200Strategy.IN_MEMORY);
168     }
169 
170     @Test
171     public void testJarUnarchiveAllFileTempFile() throws Exception {
172         jarUnarchiveAll(true, Pack200Strategy.TEMP_FILE);
173     }
174 
175     @Test
176     public void testJarUnarchiveAllInMemory() throws Exception {
177         jarUnarchiveAll(false, Pack200Strategy.IN_MEMORY);
178     }
179 
180     @Test
181     public void testJarUnarchiveAllTempFile() throws Exception {
182         jarUnarchiveAll(false, Pack200Strategy.TEMP_FILE);
183     }
184 
185     @Test
186     public void testMultiByteReadFromMemoryConsistentlyReturnsMinusOneAtEof() throws Exception {
187         multiByteReadConsistentlyReturnsMinusOneAtEof(Pack200Strategy.IN_MEMORY);
188     }
189 
190     @Test
191     public void testMultiByteReadFromTempFileConsistentlyReturnsMinusOneAtEof() throws Exception {
192         multiByteReadConsistentlyReturnsMinusOneAtEof(Pack200Strategy.TEMP_FILE);
193     }
194 
195     @Test
196     public void testOutputStreamMethods() throws Exception {
197         final File output = newTempFile("bla.pack");
198         final Map<String, String> m = new HashMap<>();
199         m.put("foo", "bar");
200         try (OutputStream out = Files.newOutputStream(output.toPath());
201                 OutputStream os = new Pack200CompressorOutputStream(out, m)) {
202             os.write(1);
203             os.write(new byte[] { 2, 3 });
204         }
205     }
206 
207     @Test
208     public void testShortSignature() throws Exception {
209         try (InputStream is = newInputStream("bla.pack")) {
210             final byte[] sig = new byte[2];
211             is.read(sig);
212             assertFalse(Pack200CompressorInputStream.matches(sig, 2));
213         }
214     }
215 
216     @Test
217     public void testSingleByteReadFromMemoryConsistentlyReturnsMinusOneAtEof() throws Exception {
218         singleByteReadConsistentlyReturnsMinusOneAtEof(Pack200Strategy.IN_MEMORY);
219     }
220 
221     @Test
222     public void testSingleByteReadFromTempFileConsistentlyReturnsMinusOneAtEof() throws Exception {
223         singleByteReadConsistentlyReturnsMinusOneAtEof(Pack200Strategy.TEMP_FILE);
224     }
225 
226 }