View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one or more
3    *  contributor license agreements.  See the NOTICE file distributed with
4    *  this work for additional information regarding copyright ownership.
5    *  The ASF licenses this file to You under the Apache License, Version 2.0
6    *  (the "License"); you may not use this file except in compliance with
7    *  the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   *
17   */
18  
19  package org.apache.commons.compress.archivers.zip;
20  
21  import static org.apache.commons.compress.AbstractTestCase.getFile;
22  import static org.junit.Assert.*;
23  
24  import java.io.ByteArrayOutputStream;
25  import java.util.zip.ZipEntry;
26  
27  import org.junit.Test;
28  
29  /**
30   * JUnit testcases for org.apache.commons.compress.archivers.zip.ZipEntry.
31   *
32   */
33  public class ZipArchiveEntryTest {
34  
35      /**
36       * test handling of extra fields
37       */
38      @Test
39      public void testExtraFields() {
40          final AsiExtraField a = new AsiExtraField();
41          a.setDirectory(true);
42          a.setMode(0755);
43          final UnrecognizedExtraField u = new UnrecognizedExtraField();
44          u.setHeaderId(ExtraFieldUtilsTest.UNRECOGNIZED_HEADER);
45          u.setLocalFileDataData(new byte[0]);
46  
47          final ZipArchiveEntry ze = new ZipArchiveEntry("test/");
48          ze.setExtraFields(new ZipExtraField[] {a, u});
49          final byte[] data1 = ze.getExtra();
50          ZipExtraField[] result = ze.getExtraFields();
51          assertEquals("first pass", 2, result.length);
52          assertSame(a, result[0]);
53          assertSame(u, result[1]);
54  
55          final UnrecognizedExtraField u2 = new UnrecognizedExtraField();
56          u2.setHeaderId(ExtraFieldUtilsTest.UNRECOGNIZED_HEADER);
57          u2.setLocalFileDataData(new byte[] {1});
58  
59          ze.addExtraField(u2);
60          final byte[] data2 = ze.getExtra();
61          result = ze.getExtraFields();
62          assertEquals("second pass", 2, result.length);
63          assertSame(a, result[0]);
64          assertSame(u2, result[1]);
65          assertEquals("length second pass", data1.length+1, data2.length);
66  
67          final UnrecognizedExtraField u3 = new UnrecognizedExtraField();
68          u3.setHeaderId(new ZipShort(2));
69          u3.setLocalFileDataData(new byte[] {1});
70          ze.addExtraField(u3);
71          result = ze.getExtraFields();
72          assertEquals("third pass", 3, result.length);
73  
74          ze.removeExtraField(ExtraFieldUtilsTest.UNRECOGNIZED_HEADER);
75          final byte[] data3 = ze.getExtra();
76          result = ze.getExtraFields();
77          assertEquals("fourth pass", 2, result.length);
78          assertSame(a, result[0]);
79          assertSame(u3, result[1]);
80          assertEquals("length fourth pass", data2.length, data3.length);
81  
82          try {
83              ze.removeExtraField(ExtraFieldUtilsTest.UNRECOGNIZED_HEADER);
84              fail("should be no such element");
85          } catch (final java.util.NoSuchElementException nse) {
86          }
87      }
88  
89      /**
90       * test handling of extra fields via central directory
91       */
92      @Test
93      public void testExtraFieldMerging() {
94          final AsiExtraField a = new AsiExtraField();
95          a.setDirectory(true);
96          a.setMode(0755);
97          final UnrecognizedExtraField u = new UnrecognizedExtraField();
98          u.setHeaderId(ExtraFieldUtilsTest.UNRECOGNIZED_HEADER);
99          u.setLocalFileDataData(new byte[0]);
100 
101         final ZipArchiveEntry ze = new ZipArchiveEntry("test/");
102         ze.setExtraFields(new ZipExtraField[] {a, u});
103 
104         // merge
105         // Header-ID 1 + length 1 + one byte of data
106         final byte[] b = ExtraFieldUtilsTest.UNRECOGNIZED_HEADER.getBytes();
107         ze.setCentralDirectoryExtra(new byte[] {b[0], b[1], 1, 0, 127});
108 
109         ZipExtraField[] result = ze.getExtraFields();
110         assertEquals("first pass", 2, result.length);
111         assertSame(a, result[0]);
112         assertEquals(ExtraFieldUtilsTest.UNRECOGNIZED_HEADER,
113                      result[1].getHeaderId());
114         assertEquals(new ZipShort(0), result[1].getLocalFileDataLength());
115         assertEquals(new ZipShort(1), result[1].getCentralDirectoryLength());
116 
117         // add new
118         // Header-ID 2 + length 0
119         ze.setCentralDirectoryExtra(new byte[] {2, 0, 0, 0});
120 
121         result = ze.getExtraFields();
122         assertEquals("second pass", 3, result.length);
123 
124         // merge
125         // Header-ID 2 + length 1 + one byte of data
126         ze.setExtra(new byte[] {2, 0, 1, 0, 127});
127 
128         result = ze.getExtraFields();
129         assertEquals("third pass", 3, result.length);
130         assertSame(a, result[0]);
131         assertEquals(new ZipShort(2), result[2].getHeaderId());
132         assertEquals(new ZipShort(1), result[2].getLocalFileDataLength());
133         assertEquals(new ZipShort(0), result[2].getCentralDirectoryLength());
134     }
135 
136     /**
137      * test handling of extra fields
138      */
139     @Test
140     public void testAddAsFirstExtraField() {
141         final AsiExtraField a = new AsiExtraField();
142         a.setDirectory(true);
143         a.setMode(0755);
144         final UnrecognizedExtraField u = new UnrecognizedExtraField();
145         u.setHeaderId(ExtraFieldUtilsTest.UNRECOGNIZED_HEADER);
146         u.setLocalFileDataData(new byte[0]);
147 
148         final ZipArchiveEntry ze = new ZipArchiveEntry("test/");
149         ze.setExtraFields(new ZipExtraField[] {a, u});
150         final byte[] data1 = ze.getExtra();
151 
152         final UnrecognizedExtraField u2 = new UnrecognizedExtraField();
153         u2.setHeaderId(ExtraFieldUtilsTest.UNRECOGNIZED_HEADER);
154         u2.setLocalFileDataData(new byte[] {1});
155 
156         ze.addAsFirstExtraField(u2);
157         final byte[] data2 = ze.getExtra();
158         ZipExtraField[] result = ze.getExtraFields();
159         assertEquals("second pass", 2, result.length);
160         assertSame(u2, result[0]);
161         assertSame(a, result[1]);
162         assertEquals("length second pass", data1.length + 1, data2.length);
163 
164         final UnrecognizedExtraField u3 = new UnrecognizedExtraField();
165         u3.setHeaderId(new ZipShort(2));
166         u3.setLocalFileDataData(new byte[] {1});
167         ze.addAsFirstExtraField(u3);
168         result = ze.getExtraFields();
169         assertEquals("third pass", 3, result.length);
170         assertSame(u3, result[0]);
171         assertSame(u2, result[1]);
172         assertSame(a, result[2]);
173     }
174 
175     @Test
176     public void testUnixMode() {
177         ZipArchiveEntry ze = new ZipArchiveEntry("foo");
178         assertEquals(0, ze.getPlatform());
179         ze.setUnixMode(0755);
180         assertEquals(3, ze.getPlatform());
181         assertEquals(0755,
182                      (ze.getExternalAttributes() >> 16) & 0xFFFF);
183         assertEquals(0, ze.getExternalAttributes()  & 0xFFFF);
184 
185         ze.setUnixMode(0444);
186         assertEquals(3, ze.getPlatform());
187         assertEquals(0444,
188                      (ze.getExternalAttributes() >> 16) & 0xFFFF);
189         assertEquals(1, ze.getExternalAttributes()  & 0xFFFF);
190 
191         ze = new ZipArchiveEntry("foo/");
192         assertEquals(0, ze.getPlatform());
193         ze.setUnixMode(0777);
194         assertEquals(3, ze.getPlatform());
195         assertEquals(0777,
196                      (ze.getExternalAttributes() >> 16) & 0xFFFF);
197         assertEquals(0x10, ze.getExternalAttributes()  & 0xFFFF);
198 
199         ze.setUnixMode(0577);
200         assertEquals(3, ze.getPlatform());
201         assertEquals(0577,
202                      (ze.getExternalAttributes() >> 16) & 0xFFFF);
203         assertEquals(0x11, ze.getExternalAttributes()  & 0xFFFF);
204     }
205 
206     /**
207      * Test case for
208      * <a href="https://issues.apache.org/jira/browse/COMPRESS-93"
209      * >COMPRESS-93</a>.
210      */
211     @Test
212     public void testCompressionMethod() throws Exception {
213         final ZipArchiveOutputStream zos =
214             new ZipArchiveOutputStream(new ByteArrayOutputStream());
215         final ZipArchiveEntry entry = new ZipArchiveEntry("foo");
216         assertEquals(-1, entry.getMethod());
217         assertFalse(zos.canWriteEntryData(entry));
218 
219         entry.setMethod(ZipEntry.STORED);
220         assertEquals(ZipEntry.STORED, entry.getMethod());
221         assertTrue(zos.canWriteEntryData(entry));
222 
223         entry.setMethod(ZipEntry.DEFLATED);
224         assertEquals(ZipEntry.DEFLATED, entry.getMethod());
225         assertTrue(zos.canWriteEntryData(entry));
226 
227         // Test the unsupported "imploded" compression method (6)
228         entry.setMethod(6);
229         assertEquals(6, entry.getMethod());
230         assertFalse(zos.canWriteEntryData(entry));
231         zos.close();
232     }
233 
234     /**
235      * Test case for
236      * <a href="https://issues.apache.org/jira/browse/COMPRESS-94"
237      * >COMPRESS-94</a>.
238      */
239     @Test
240     public void testNotEquals() {
241         final ZipArchiveEntry entry1 = new ZipArchiveEntry("foo");
242         final ZipArchiveEntry entry2 = new ZipArchiveEntry("bar");
243         assertFalse(entry1.equals(entry2));
244     }
245 
246     /**
247      * Tests comment's influence on equals comparisons.
248      * @see "https://issues.apache.org/jira/browse/COMPRESS-187"
249      */
250     @Test
251     public void testNullCommentEqualsEmptyComment() {
252         final ZipArchiveEntry entry1 = new ZipArchiveEntry("foo");
253         final ZipArchiveEntry entry2 = new ZipArchiveEntry("foo");
254         final ZipArchiveEntry entry3 = new ZipArchiveEntry("foo");
255         entry1.setComment(null);
256         entry2.setComment("");
257         entry3.setComment("bar");
258         assertEquals(entry1, entry2);
259         assertFalse(entry1.equals(entry3));
260         assertFalse(entry2.equals(entry3));
261     }
262 
263     @Test
264     public void testCopyConstructor() throws Exception {
265         final ZipArchiveEntry archiveEntry = new ZipArchiveEntry("fred");
266         archiveEntry.setUnixMode(0664);
267         archiveEntry.setMethod(ZipEntry.DEFLATED);
268         archiveEntry.getGeneralPurposeBit().useStrongEncryption(true);
269         final ZipArchiveEntry copy = new ZipArchiveEntry(archiveEntry);
270         assertEquals(archiveEntry, copy);
271     }
272 
273     /**
274      * @see "https://issues.apache.org/jira/browse/COMPRESS-379"
275      */
276     @Test
277     public void isUnixSymlinkIsFalseIfMoreThanOneFlagIsSet() throws Exception {
278         try (ZipFile zf = new ZipFile(getFile("COMPRESS-379.jar"))) {
279             ZipArchiveEntry ze = zf.getEntry("META-INF/maven/");
280             assertFalse(ze.isUnixSymlink());
281         }
282     }
283 
284     @Test
285     public void testIsUnixSymlink() {
286         ZipArchiveEntry ze = new ZipArchiveEntry("foo");
287         ze.setUnixMode(UnixStat.LINK_FLAG);
288         assertTrue(ze.isUnixSymlink());
289         ze.setUnixMode(UnixStat.LINK_FLAG | UnixStat.DIR_FLAG);
290         assertFalse(ze.isUnixSymlink());
291     }
292 
293 }