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 junit.framework.TestCase;
22  
23  import java.io.ByteArrayOutputStream;
24  import java.util.zip.ZipEntry;
25  
26  /**
27   * JUnit 3 testcases for org.apache.commons.compress.archivers.zip.ZipEntry.
28   *
29   */
30  public class ZipArchiveEntryTest extends TestCase {
31  
32      public ZipArchiveEntryTest(String name) {
33          super(name);
34      }
35  
36      /**
37       * test handling of extra fields
38       */
39      public void testExtraFields() {
40          AsiExtraField a = new AsiExtraField();
41          a.setDirectory(true);
42          a.setMode(0755);
43          UnrecognizedExtraField u = new UnrecognizedExtraField();
44          u.setHeaderId(ExtraFieldUtilsTest.UNRECOGNIZED_HEADER);
45          u.setLocalFileDataData(new byte[0]);
46  
47          ZipArchiveEntry ze = new ZipArchiveEntry("test/");
48          ze.setExtraFields(new ZipExtraField[] {a, u});
49          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          UnrecognizedExtraField u2 = new UnrecognizedExtraField();
56          u2.setHeaderId(ExtraFieldUtilsTest.UNRECOGNIZED_HEADER);
57          u2.setLocalFileDataData(new byte[] {1});
58  
59          ze.addExtraField(u2);
60          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          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          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 (java.util.NoSuchElementException nse) {
86          }
87      }
88  
89      /**
90       * test handling of extra fields via central directory
91       */
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     public void testAddAsFirstExtraField() {
139         AsiExtraField a = new AsiExtraField();
140         a.setDirectory(true);
141         a.setMode(0755);
142         UnrecognizedExtraField u = new UnrecognizedExtraField();
143         u.setHeaderId(ExtraFieldUtilsTest.UNRECOGNIZED_HEADER);
144         u.setLocalFileDataData(new byte[0]);
145 
146         ZipArchiveEntry ze = new ZipArchiveEntry("test/");
147         ze.setExtraFields(new ZipExtraField[] {a, u});
148         byte[] data1 = ze.getExtra();
149 
150         UnrecognizedExtraField u2 = new UnrecognizedExtraField();
151         u2.setHeaderId(ExtraFieldUtilsTest.UNRECOGNIZED_HEADER);
152         u2.setLocalFileDataData(new byte[] {1});
153 
154         ze.addAsFirstExtraField(u2);
155         byte[] data2 = ze.getExtra();
156         ZipExtraField[] result = ze.getExtraFields();
157         assertEquals("second pass", 2, result.length);
158         assertSame(u2, result[0]);
159         assertSame(a, result[1]);
160         assertEquals("length second pass", data1.length + 1, data2.length);
161 
162         UnrecognizedExtraField u3 = new UnrecognizedExtraField();
163         u3.setHeaderId(new ZipShort(2));
164         u3.setLocalFileDataData(new byte[] {1});
165         ze.addAsFirstExtraField(u3);
166         result = ze.getExtraFields();
167         assertEquals("third pass", 3, result.length);
168         assertSame(u3, result[0]);
169         assertSame(u2, result[1]);
170         assertSame(a, result[2]);
171     }
172 
173     public void testUnixMode() {
174         ZipArchiveEntry ze = new ZipArchiveEntry("foo");
175         assertEquals(0, ze.getPlatform());
176         ze.setUnixMode(0755);
177         assertEquals(3, ze.getPlatform());
178         assertEquals(0755,
179                      (ze.getExternalAttributes() >> 16) & 0xFFFF);
180         assertEquals(0, ze.getExternalAttributes()  & 0xFFFF);
181 
182         ze.setUnixMode(0444);
183         assertEquals(3, ze.getPlatform());
184         assertEquals(0444,
185                      (ze.getExternalAttributes() >> 16) & 0xFFFF);
186         assertEquals(1, ze.getExternalAttributes()  & 0xFFFF);
187 
188         ze = new ZipArchiveEntry("foo/");
189         assertEquals(0, ze.getPlatform());
190         ze.setUnixMode(0777);
191         assertEquals(3, ze.getPlatform());
192         assertEquals(0777,
193                      (ze.getExternalAttributes() >> 16) & 0xFFFF);
194         assertEquals(0x10, ze.getExternalAttributes()  & 0xFFFF);
195 
196         ze.setUnixMode(0577);
197         assertEquals(3, ze.getPlatform());
198         assertEquals(0577,
199                      (ze.getExternalAttributes() >> 16) & 0xFFFF);
200         assertEquals(0x11, ze.getExternalAttributes()  & 0xFFFF);
201     }
202 
203     /**
204      * Test case for
205      * <a href="https://issues.apache.org/jira/browse/COMPRESS-93"
206      * >COMPRESS-93</a>.
207      */
208     public void testCompressionMethod() throws Exception {
209         ZipArchiveOutputStream zos =
210             new ZipArchiveOutputStream(new ByteArrayOutputStream());
211         ZipArchiveEntry entry = new ZipArchiveEntry("foo");
212         assertEquals(-1, entry.getMethod());
213         assertFalse(zos.canWriteEntryData(entry));
214 
215         entry.setMethod(ZipEntry.STORED);
216         assertEquals(ZipEntry.STORED, entry.getMethod());
217         assertTrue(zos.canWriteEntryData(entry));
218 
219         entry.setMethod(ZipEntry.DEFLATED);
220         assertEquals(ZipEntry.DEFLATED, entry.getMethod());
221         assertTrue(zos.canWriteEntryData(entry));
222 
223         // Test the unsupported "imploded" compression method (6)
224         entry.setMethod(6);
225         assertEquals(6, entry.getMethod());
226         assertFalse(zos.canWriteEntryData(entry));
227         zos.close();
228     }
229 
230     /**
231      * Test case for
232      * <a href="https://issues.apache.org/jira/browse/COMPRESS-94"
233      * >COMPRESS-94</a>.
234      */
235     public void testNotEquals() {
236         ZipArchiveEntry entry1 = new ZipArchiveEntry("foo");
237         ZipArchiveEntry entry2 = new ZipArchiveEntry("bar");
238         assertFalse(entry1.equals(entry2));
239     }
240 
241     /**
242      * Tests comment's influence on equals comparisons.
243      * @see "https://issues.apache.org/jira/browse/COMPRESS-187"
244      */
245     public void testNullCommentEqualsEmptyComment() {
246         ZipArchiveEntry entry1 = new ZipArchiveEntry("foo");
247         ZipArchiveEntry entry2 = new ZipArchiveEntry("foo");
248         ZipArchiveEntry entry3 = new ZipArchiveEntry("foo");
249         entry1.setComment(null);
250         entry2.setComment("");
251         entry3.setComment("bar");
252         assertEquals(entry1, entry2);
253         assertFalse(entry1.equals(entry3));
254         assertFalse(entry2.equals(entry3));
255     }
256 }