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.junit.Assert.*;
22  
23  import java.io.ByteArrayOutputStream;
24  import java.util.zip.ZipEntry;
25  
26  import org.junit.Test;
27  
28  /**
29   * JUnit testcases for org.apache.commons.compress.archivers.zip.ZipEntry.
30   *
31   */
32  public class ZipArchiveEntryTest {
33  
34      /**
35       * test handling of extra fields
36       */
37      @Test
38      public void testExtraFields() {
39          AsiExtraField a = new AsiExtraField();
40          a.setDirectory(true);
41          a.setMode(0755);
42          UnrecognizedExtraField u = new UnrecognizedExtraField();
43          u.setHeaderId(ExtraFieldUtilsTest.UNRECOGNIZED_HEADER);
44          u.setLocalFileDataData(new byte[0]);
45  
46          ZipArchiveEntry ze = new ZipArchiveEntry("test/");
47          ze.setExtraFields(new ZipExtraField[] {a, u});
48          byte[] data1 = ze.getExtra();
49          ZipExtraField[] result = ze.getExtraFields();
50          assertEquals("first pass", 2, result.length);
51          assertSame(a, result[0]);
52          assertSame(u, result[1]);
53  
54          UnrecognizedExtraField u2 = new UnrecognizedExtraField();
55          u2.setHeaderId(ExtraFieldUtilsTest.UNRECOGNIZED_HEADER);
56          u2.setLocalFileDataData(new byte[] {1});
57  
58          ze.addExtraField(u2);
59          byte[] data2 = ze.getExtra();
60          result = ze.getExtraFields();
61          assertEquals("second pass", 2, result.length);
62          assertSame(a, result[0]);
63          assertSame(u2, result[1]);
64          assertEquals("length second pass", data1.length+1, data2.length);
65  
66          UnrecognizedExtraField u3 = new UnrecognizedExtraField();
67          u3.setHeaderId(new ZipShort(2));
68          u3.setLocalFileDataData(new byte[] {1});
69          ze.addExtraField(u3);
70          result = ze.getExtraFields();
71          assertEquals("third pass", 3, result.length);
72  
73          ze.removeExtraField(ExtraFieldUtilsTest.UNRECOGNIZED_HEADER);
74          byte[] data3 = ze.getExtra();
75          result = ze.getExtraFields();
76          assertEquals("fourth pass", 2, result.length);
77          assertSame(a, result[0]);
78          assertSame(u3, result[1]);
79          assertEquals("length fourth pass", data2.length, data3.length);
80  
81          try {
82              ze.removeExtraField(ExtraFieldUtilsTest.UNRECOGNIZED_HEADER);
83              fail("should be no such element");
84          } catch (java.util.NoSuchElementException nse) {
85          }
86      }
87  
88      /**
89       * test handling of extra fields via central directory
90       */
91      @Test
92      public void testExtraFieldMerging() {
93          AsiExtraField a = new AsiExtraField();
94          a.setDirectory(true);
95          a.setMode(0755);
96          UnrecognizedExtraField u = new UnrecognizedExtraField();
97          u.setHeaderId(ExtraFieldUtilsTest.UNRECOGNIZED_HEADER);
98          u.setLocalFileDataData(new byte[0]);
99  
100         ZipArchiveEntry ze = new ZipArchiveEntry("test/");
101         ze.setExtraFields(new ZipExtraField[] {a, u});
102 
103         // merge
104         // Header-ID 1 + length 1 + one byte of data
105         byte[] b = ExtraFieldUtilsTest.UNRECOGNIZED_HEADER.getBytes();
106         ze.setCentralDirectoryExtra(new byte[] {b[0], b[1], 1, 0, 127});
107 
108         ZipExtraField[] result = ze.getExtraFields();
109         assertEquals("first pass", 2, result.length);
110         assertSame(a, result[0]);
111         assertEquals(ExtraFieldUtilsTest.UNRECOGNIZED_HEADER,
112                      result[1].getHeaderId());
113         assertEquals(new ZipShort(0), result[1].getLocalFileDataLength());
114         assertEquals(new ZipShort(1), result[1].getCentralDirectoryLength());
115 
116         // add new
117         // Header-ID 2 + length 0
118         ze.setCentralDirectoryExtra(new byte[] {2, 0, 0, 0});
119 
120         result = ze.getExtraFields();
121         assertEquals("second pass", 3, result.length);
122 
123         // merge
124         // Header-ID 2 + length 1 + one byte of data
125         ze.setExtra(new byte[] {2, 0, 1, 0, 127});
126 
127         result = ze.getExtraFields();
128         assertEquals("third pass", 3, result.length);
129         assertSame(a, result[0]);
130         assertEquals(new ZipShort(2), result[2].getHeaderId());
131         assertEquals(new ZipShort(1), result[2].getLocalFileDataLength());
132         assertEquals(new ZipShort(0), result[2].getCentralDirectoryLength());
133     }
134 
135     /**
136      * test handling of extra fields
137      */
138     @Test
139     public void testAddAsFirstExtraField() {
140         AsiExtraField a = new AsiExtraField();
141         a.setDirectory(true);
142         a.setMode(0755);
143         UnrecognizedExtraField u = new UnrecognizedExtraField();
144         u.setHeaderId(ExtraFieldUtilsTest.UNRECOGNIZED_HEADER);
145         u.setLocalFileDataData(new byte[0]);
146 
147         ZipArchiveEntry ze = new ZipArchiveEntry("test/");
148         ze.setExtraFields(new ZipExtraField[] {a, u});
149         byte[] data1 = ze.getExtra();
150 
151         UnrecognizedExtraField u2 = new UnrecognizedExtraField();
152         u2.setHeaderId(ExtraFieldUtilsTest.UNRECOGNIZED_HEADER);
153         u2.setLocalFileDataData(new byte[] {1});
154 
155         ze.addAsFirstExtraField(u2);
156         byte[] data2 = ze.getExtra();
157         ZipExtraField[] result = ze.getExtraFields();
158         assertEquals("second pass", 2, result.length);
159         assertSame(u2, result[0]);
160         assertSame(a, result[1]);
161         assertEquals("length second pass", data1.length + 1, data2.length);
162 
163         UnrecognizedExtraField u3 = new UnrecognizedExtraField();
164         u3.setHeaderId(new ZipShort(2));
165         u3.setLocalFileDataData(new byte[] {1});
166         ze.addAsFirstExtraField(u3);
167         result = ze.getExtraFields();
168         assertEquals("third pass", 3, result.length);
169         assertSame(u3, result[0]);
170         assertSame(u2, result[1]);
171         assertSame(a, result[2]);
172     }
173 
174     @Test
175     public void testUnixMode() {
176         ZipArchiveEntry ze = new ZipArchiveEntry("foo");
177         assertEquals(0, ze.getPlatform());
178         ze.setUnixMode(0755);
179         assertEquals(3, ze.getPlatform());
180         assertEquals(0755,
181                      (ze.getExternalAttributes() >> 16) & 0xFFFF);
182         assertEquals(0, ze.getExternalAttributes()  & 0xFFFF);
183 
184         ze.setUnixMode(0444);
185         assertEquals(3, ze.getPlatform());
186         assertEquals(0444,
187                      (ze.getExternalAttributes() >> 16) & 0xFFFF);
188         assertEquals(1, ze.getExternalAttributes()  & 0xFFFF);
189 
190         ze = new ZipArchiveEntry("foo/");
191         assertEquals(0, ze.getPlatform());
192         ze.setUnixMode(0777);
193         assertEquals(3, ze.getPlatform());
194         assertEquals(0777,
195                      (ze.getExternalAttributes() >> 16) & 0xFFFF);
196         assertEquals(0x10, ze.getExternalAttributes()  & 0xFFFF);
197 
198         ze.setUnixMode(0577);
199         assertEquals(3, ze.getPlatform());
200         assertEquals(0577,
201                      (ze.getExternalAttributes() >> 16) & 0xFFFF);
202         assertEquals(0x11, ze.getExternalAttributes()  & 0xFFFF);
203     }
204 
205     /**
206      * Test case for
207      * <a href="https://issues.apache.org/jira/browse/COMPRESS-93"
208      * >COMPRESS-93</a>.
209      */
210     @Test
211     public void testCompressionMethod() throws Exception {
212         ZipArchiveOutputStream zos =
213             new ZipArchiveOutputStream(new ByteArrayOutputStream());
214         ZipArchiveEntry entry = new ZipArchiveEntry("foo");
215         assertEquals(-1, entry.getMethod());
216         assertFalse(zos.canWriteEntryData(entry));
217 
218         entry.setMethod(ZipEntry.STORED);
219         assertEquals(ZipEntry.STORED, entry.getMethod());
220         assertTrue(zos.canWriteEntryData(entry));
221 
222         entry.setMethod(ZipEntry.DEFLATED);
223         assertEquals(ZipEntry.DEFLATED, entry.getMethod());
224         assertTrue(zos.canWriteEntryData(entry));
225 
226         // Test the unsupported "imploded" compression method (6)
227         entry.setMethod(6);
228         assertEquals(6, entry.getMethod());
229         assertFalse(zos.canWriteEntryData(entry));
230         zos.close();
231     }
232 
233     /**
234      * Test case for
235      * <a href="https://issues.apache.org/jira/browse/COMPRESS-94"
236      * >COMPRESS-94</a>.
237      */
238     @Test
239     public void testNotEquals() {
240         ZipArchiveEntry entry1 = new ZipArchiveEntry("foo");
241         ZipArchiveEntry entry2 = new ZipArchiveEntry("bar");
242         assertFalse(entry1.equals(entry2));
243     }
244 
245     /**
246      * Tests comment's influence on equals comparisons.
247      * @see "https://issues.apache.org/jira/browse/COMPRESS-187"
248      */
249     @Test
250     public void testNullCommentEqualsEmptyComment() {
251         ZipArchiveEntry entry1 = new ZipArchiveEntry("foo");
252         ZipArchiveEntry entry2 = new ZipArchiveEntry("foo");
253         ZipArchiveEntry entry3 = new ZipArchiveEntry("foo");
254         entry1.setComment(null);
255         entry2.setComment("");
256         entry3.setComment("bar");
257         assertEquals(entry1, entry2);
258         assertFalse(entry1.equals(entry3));
259         assertFalse(entry2.equals(entry3));
260     }
261 
262     @Test
263     public void testCopyConstructor() throws Exception {
264         ZipArchiveEntry archiveEntry = new ZipArchiveEntry("fred");
265         archiveEntry.setUnixMode(0664);
266         archiveEntry.setMethod(ZipEntry.DEFLATED);
267         archiveEntry.getGeneralPurposeBit().useStrongEncryption(true);
268         ZipArchiveEntry copy = new ZipArchiveEntry(archiveEntry);
269         assertEquals(archiveEntry, copy);
270     }
271 }