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;
21  
22  import static org.junit.jupiter.api.Assertions.assertEquals;
23  import static org.junit.jupiter.api.Assertions.assertNotNull;
24  import static org.junit.jupiter.api.Assertions.assertTrue;
25  
26  import java.io.ByteArrayOutputStream;
27  import java.io.InputStream;
28  import java.io.OutputStream;
29  import java.nio.file.Files;
30  import java.nio.file.Path;
31  
32  import org.apache.commons.compress.archivers.ArchiveEntry;
33  import org.apache.commons.compress.archivers.ArchiveInputStream;
34  import org.apache.commons.compress.archivers.ArchiveOutputStream;
35  import org.apache.commons.compress.archivers.ar.ArArchiveEntry;
36  import org.apache.commons.compress.archivers.cpio.CpioArchiveEntry;
37  import org.apache.commons.compress.archivers.jar.JarArchiveEntry;
38  import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
39  import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
40  import org.junit.jupiter.api.Test;
41  
42  /**
43   * Check that the different write methods create the same output. TODO perform the same checks for reads.
44   */
45  class IOMethodsTest extends AbstractTest {
46  
47      private static final int bytesToTest = 50;
48      private static final byte[] byteTest = new byte[bytesToTest];
49      static {
50          for (int i = 0; i < byteTest.length;) {
51              byteTest[i] = (byte) i;
52              byteTest[i + 1] = (byte) -i;
53              i += 2;
54          }
55      }
56  
57      private void compareReads(final String archiverName) throws Exception {
58          final OutputStream out1 = new ByteArrayOutputStream();
59          final OutputStream out2 = new ByteArrayOutputStream();
60          final OutputStream out3 = new ByteArrayOutputStream();
61          final Path file = createSingleEntryArchive(archiverName);
62  
63          final InputStream is1 = Files.newInputStream(file);
64          final ArchiveInputStream<?> ais1 = factory.createArchiveInputStream(archiverName, is1);
65          final ArchiveEntry nextEntry = ais1.getNextEntry();
66          assertNotNull(nextEntry);
67  
68          final byte[] buff = new byte[10]; // small so multiple reads are needed;
69          final long size = nextEntry.getSize();
70          if (size != ArchiveEntry.SIZE_UNKNOWN) {
71              assertTrue(size > 0, "Size should be > 0, found: " + size);
72          }
73  
74          final InputStream is2 = Files.newInputStream(file);
75          final ArchiveInputStream<?> ais2 = factory.createArchiveInputStream(archiverName, is2);
76          final ArchiveEntry nextEntry2 = ais2.getNextEntry();
77          assertNotNull(nextEntry2);
78          assertEquals(size, nextEntry2.getSize(), "Expected same entry size");
79  
80          final InputStream is3 = Files.newInputStream(file);
81          final ArchiveInputStream<?> ais3 = factory.createArchiveInputStream(archiverName, is3);
82          final ArchiveEntry nextEntry3 = ais3.getNextEntry();
83          assertNotNull(nextEntry3);
84          assertEquals(size, nextEntry3.getSize(), "Expected same entry size");
85  
86          int b;
87          while ((b = ais1.read()) != -1) {
88              out1.write(b);
89          }
90          ais1.close();
91  
92          int bytes;
93          while ((bytes = ais2.read(buff)) > 0) {
94              out2.write(buff, 0, bytes);
95          }
96          ais2.close();
97  
98          while ((bytes = ais3.read(buff, 0, buff.length)) > 0) {
99              out3.write(buff, 0, bytes);
100         }
101         ais3.close();
102 
103         assertEquals(out1.toString().length(), out2.toString().length(), "out1Len!=out2Len");
104         assertEquals(out1.toString().length(), out3.toString().length(), "out1Len!=out3Len");
105         assertEquals(out1.toString(), out2.toString(), "out1!=out2");
106         assertEquals(out1.toString(), out3.toString(), "out1!=out3");
107     }
108 
109     private void compareWrites(final String archiverName, final ArchiveEntry entry) throws Exception {
110         final OutputStream out1 = new ByteArrayOutputStream();
111         final OutputStream out2 = new ByteArrayOutputStream();
112         final OutputStream out3 = new ByteArrayOutputStream();
113         final ArchiveOutputStream<ArchiveEntry> aos1 = factory.createArchiveOutputStream(archiverName, out1);
114         aos1.putArchiveEntry(entry);
115         final ArchiveOutputStream<ArchiveEntry> aos2 = factory.createArchiveOutputStream(archiverName, out2);
116         aos2.putArchiveEntry(entry);
117         final ArchiveOutputStream<ArchiveEntry> aos3 = factory.createArchiveOutputStream(archiverName, out3);
118         aos3.putArchiveEntry(entry);
119         for (final byte element : byteTest) {
120             aos1.write(element);
121         }
122         aos1.closeArchiveEntry();
123         aos1.close();
124 
125         aos2.write(byteTest);
126         aos2.closeArchiveEntry();
127         aos2.close();
128 
129         aos3.write(byteTest, 0, byteTest.length);
130         aos3.closeArchiveEntry();
131         aos3.close();
132         assertEquals(aos1.getBytesWritten(), aos2.getBytesWritten(), "aos1Bytes!=aos2Bytes");
133         assertEquals(aos1.getBytesWritten(), aos3.getBytesWritten(), "aos1Bytes!=aos3Bytes");
134         assertEquals(out1.toString().length(), out2.toString().length(), "out1Len!=out2Len");
135         assertEquals(out1.toString().length(), out3.toString().length(), "out1Len!=out2Len");
136         assertEquals(out1.toString(), out2.toString(), "out1!=out2");
137         assertEquals(out1.toString(), out3.toString(), "out1!=out3");
138     }
139 
140     @Test
141     void testReadAr() throws Exception {
142         compareReads("ar");
143     }
144 
145     @Test
146     void testReadCpio() throws Exception {
147         compareReads("cpio");
148     }
149 
150     @Test
151     void testReadJar() throws Exception {
152         compareReads("jar");
153     }
154 
155     @Test
156     void testReadTar() throws Exception {
157         compareReads("tar");
158     }
159 
160     @Test
161     void testReadZip() throws Exception {
162         compareReads("zip");
163     }
164 
165     @Test
166     void testWriteAr() throws Exception {
167         compareWrites("ar", new ArArchiveEntry("dummy", bytesToTest));
168     }
169 
170     @Test
171     void testWriteCpio() throws Exception {
172         final ArchiveEntry entry = new CpioArchiveEntry("dummy", bytesToTest);
173         compareWrites("cpio", entry);
174     }
175 
176     @Test
177     void testWriteJar() throws Exception {
178         final ArchiveEntry entry = new JarArchiveEntry("dummy");
179         compareWrites("jar", entry);
180     }
181 
182     @Test
183     void testWriteTar() throws Exception {
184         final TarArchiveEntry entry = new TarArchiveEntry("dummy");
185         entry.setSize(bytesToTest);
186         compareWrites("tar", entry);
187     }
188 
189     @Test
190     void testWriteZip() throws Exception {
191         final ArchiveEntry entry = new ZipArchiveEntry("dummy");
192         compareWrites("zip", entry);
193     }
194 }