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 java.math.BigInteger;
22  import java.util.zip.ZipException;
23  
24  import junit.framework.TestCase;
25  
26  public class Zip64ExtendedInformationExtraFieldTest extends TestCase {
27      public Zip64ExtendedInformationExtraFieldTest(String name) {
28          super(name);
29      }
30  
31      private static final ZipEightByteInteger SIZE =
32          new ZipEightByteInteger(0x12345678);
33      private static final ZipEightByteInteger CSIZE =
34          new ZipEightByteInteger(0x9ABCDEF);
35      private static final ZipEightByteInteger OFF =
36          new ZipEightByteInteger(BigInteger.valueOf(0xABCDEF091234567l)
37                                  .shiftLeft(4)
38                                  .setBit(3));
39      private static final ZipLong DISK = new ZipLong(0x12);
40  
41      public void testWriteCDOnlySizes() {
42          Zip64ExtendedInformationExtraField f =
43              new Zip64ExtendedInformationExtraField(SIZE, CSIZE);
44          assertEquals(new ZipShort(16), f.getCentralDirectoryLength());
45          byte[] b = f.getCentralDirectoryData();
46          assertEquals(16, b.length);
47          checkSizes(b);
48      }
49  
50      public void testWriteCDSizeAndOffset() {
51          Zip64ExtendedInformationExtraField f =
52              new Zip64ExtendedInformationExtraField(SIZE, CSIZE, OFF, null);
53          assertEquals(new ZipShort(24), f.getCentralDirectoryLength());
54          byte[] b = f.getCentralDirectoryData();
55          assertEquals(24, b.length);
56          checkSizes(b);
57          checkOffset(b, 16);
58      }
59  
60      public void testWriteCDSizeOffsetAndDisk() {
61          Zip64ExtendedInformationExtraField f =
62              new Zip64ExtendedInformationExtraField(SIZE, CSIZE, OFF, DISK);
63          assertEquals(new ZipShort(28), f.getCentralDirectoryLength());
64          byte[] b = f.getCentralDirectoryData();
65          assertEquals(28, b.length);
66          checkSizes(b);
67          checkOffset(b, 16);
68          checkDisk(b, 24);
69      }
70  
71      public void testWriteCDSizeAndDisk() {
72          Zip64ExtendedInformationExtraField f =
73              new Zip64ExtendedInformationExtraField(SIZE, CSIZE, null, DISK);
74          assertEquals(new ZipShort(20), f.getCentralDirectoryLength());
75          byte[] b = f.getCentralDirectoryData();
76          assertEquals(20, b.length);
77          checkSizes(b);
78          checkDisk(b, 16);
79      }
80  
81      public void testReadLFHSizesOnly() throws ZipException {
82          Zip64ExtendedInformationExtraField f =
83              new Zip64ExtendedInformationExtraField();
84          byte[] b = new byte[16];
85          System.arraycopy(SIZE.getBytes(), 0, b, 0, 8);
86          System.arraycopy(CSIZE.getBytes(), 0, b, 8, 8);
87          f.parseFromLocalFileData(b, 0, b.length);
88          assertEquals(SIZE, f.getSize());
89          assertEquals(CSIZE, f.getCompressedSize());
90          assertNull(f.getRelativeHeaderOffset());
91          assertNull(f.getDiskStartNumber());
92      }
93  
94      public void testReadLFHSizesAndOffset() throws ZipException {
95          Zip64ExtendedInformationExtraField f =
96              new Zip64ExtendedInformationExtraField();
97          byte[] b = new byte[24];
98          System.arraycopy(SIZE.getBytes(), 0, b, 0, 8);
99          System.arraycopy(CSIZE.getBytes(), 0, b, 8, 8);
100         System.arraycopy(OFF.getBytes(), 0, b, 16, 8);
101         f.parseFromLocalFileData(b, 0, b.length);
102         assertEquals(SIZE, f.getSize());
103         assertEquals(CSIZE, f.getCompressedSize());
104         assertEquals(OFF, f.getRelativeHeaderOffset());
105         assertNull(f.getDiskStartNumber());
106     }
107 
108     public void testReadLFHSizesOffsetAndDisk() throws ZipException {
109         Zip64ExtendedInformationExtraField f =
110             new Zip64ExtendedInformationExtraField();
111         byte[] b = new byte[28];
112         System.arraycopy(SIZE.getBytes(), 0, b, 0, 8);
113         System.arraycopy(CSIZE.getBytes(), 0, b, 8, 8);
114         System.arraycopy(OFF.getBytes(), 0, b, 16, 8);
115         System.arraycopy(DISK.getBytes(), 0, b, 24, 4);
116         f.parseFromLocalFileData(b, 0, b.length);
117         assertEquals(SIZE, f.getSize());
118         assertEquals(CSIZE, f.getCompressedSize());
119         assertEquals(OFF, f.getRelativeHeaderOffset());
120         assertEquals(DISK, f.getDiskStartNumber());
121     }
122 
123     public void testReadLFHSizesAndDisk() throws ZipException {
124         Zip64ExtendedInformationExtraField f =
125             new Zip64ExtendedInformationExtraField();
126         byte[] b = new byte[20];
127         System.arraycopy(SIZE.getBytes(), 0, b, 0, 8);
128         System.arraycopy(CSIZE.getBytes(), 0, b, 8, 8);
129         System.arraycopy(DISK.getBytes(), 0, b, 16, 4);
130         f.parseFromLocalFileData(b, 0, b.length);
131         assertEquals(SIZE, f.getSize());
132         assertEquals(CSIZE, f.getCompressedSize());
133         assertNull(f.getRelativeHeaderOffset());
134         assertEquals(DISK, f.getDiskStartNumber());
135     }
136 
137     public void testReadCDSizesOffsetAndDisk() throws ZipException {
138         Zip64ExtendedInformationExtraField f =
139             new Zip64ExtendedInformationExtraField();
140         byte[] b = new byte[28];
141         System.arraycopy(SIZE.getBytes(), 0, b, 0, 8);
142         System.arraycopy(CSIZE.getBytes(), 0, b, 8, 8);
143         System.arraycopy(OFF.getBytes(), 0, b, 16, 8);
144         System.arraycopy(DISK.getBytes(), 0, b, 24, 4);
145         f.parseFromCentralDirectoryData(b, 0, b.length);
146         assertEquals(SIZE, f.getSize());
147         assertEquals(CSIZE, f.getCompressedSize());
148         assertEquals(OFF, f.getRelativeHeaderOffset());
149         assertEquals(DISK, f.getDiskStartNumber());
150     }
151 
152     public void testReadCDSizesAndOffset() throws ZipException {
153         Zip64ExtendedInformationExtraField f =
154             new Zip64ExtendedInformationExtraField();
155         byte[] b = new byte[24];
156         System.arraycopy(SIZE.getBytes(), 0, b, 0, 8);
157         System.arraycopy(CSIZE.getBytes(), 0, b, 8, 8);
158         System.arraycopy(OFF.getBytes(), 0, b, 16, 8);
159         f.parseFromCentralDirectoryData(b, 0, b.length);
160         assertEquals(SIZE, f.getSize());
161         assertEquals(CSIZE, f.getCompressedSize());
162         assertEquals(OFF, f.getRelativeHeaderOffset());
163         assertNull(f.getDiskStartNumber());
164     }
165 
166     public void testReadCDSomethingAndDisk() throws ZipException {
167         Zip64ExtendedInformationExtraField f =
168             new Zip64ExtendedInformationExtraField();
169         byte[] b = new byte[12];
170         System.arraycopy(SIZE.getBytes(), 0, b, 0, 8);
171         System.arraycopy(DISK.getBytes(), 0, b, 8, 4);
172         f.parseFromCentralDirectoryData(b, 0, b.length);
173         assertNull(f.getSize());
174         assertNull(f.getCompressedSize());
175         assertNull(f.getRelativeHeaderOffset());
176         assertEquals(DISK, f.getDiskStartNumber());
177     }
178 
179     public void testReparseCDSingleEightByteData() throws ZipException {
180         Zip64ExtendedInformationExtraField f =
181             new Zip64ExtendedInformationExtraField();
182         byte[] b = new byte[8];
183         System.arraycopy(SIZE.getBytes(), 0, b, 0, 8);
184         f.parseFromCentralDirectoryData(b, 0, b.length);
185         f.reparseCentralDirectoryData(true, false, false, false);
186         assertEquals(SIZE, f.getSize());
187         assertNull(f.getCompressedSize());
188         assertNull(f.getRelativeHeaderOffset());
189         assertNull(f.getDiskStartNumber());
190         f.setSize(null);
191         f.reparseCentralDirectoryData(false, true, false, false);
192         assertNull(f.getSize());
193         assertEquals(SIZE, f.getCompressedSize());
194         assertNull(f.getRelativeHeaderOffset());
195         assertNull(f.getDiskStartNumber());
196         f.setCompressedSize(null);
197         f.reparseCentralDirectoryData(false, false, true, false);
198         assertNull(f.getSize());
199         assertNull(f.getCompressedSize());
200         assertEquals(SIZE, f.getRelativeHeaderOffset());
201         assertNull(f.getDiskStartNumber());
202     }
203 
204     private static void checkSizes(byte[] b) {
205         assertEquals(0x78, b[0]);
206         assertEquals(0x56, b[1]);
207         assertEquals(0x34, b[2]);
208         assertEquals(0x12, b[3]);
209         assertEquals(0x00, b[4]);
210         assertEquals(0x00, b[5]);
211         assertEquals(0x00, b[6]);
212         assertEquals(0x00, b[7]);
213         assertEquals((byte) 0xEF, b[8]);
214         assertEquals((byte) 0xCD, b[9]);
215         assertEquals((byte) 0xAB, b[10]);
216         assertEquals(0x09, b[11]);
217         assertEquals(0x00, b[12]);
218         assertEquals(0x00, b[13]);
219         assertEquals(0x00, b[14]);
220         assertEquals(0x00, b[15]);
221     }
222 
223     private static void checkOffset(byte[] b, int off) {
224         assertEquals(0x78, b[0 + off]);
225         assertEquals(0x56, b[1 + off]);
226         assertEquals(0x34, b[2 + off]);
227         assertEquals(0x12, b[3 + off]);
228         assertEquals((byte) 0x09, b[4 + off]);
229         assertEquals((byte) 0xEF, b[5 + off]);
230         assertEquals((byte) 0xCD, b[6 + off]);
231         assertEquals((byte) 0xAB, b[7 + off]);
232     }
233 
234     private static void checkDisk(byte[] b, int off) {
235         assertEquals(0x12, b[0 + off]);
236         assertEquals(0x00, b[1 + off]);
237         assertEquals(0x00, b[2 + off]);
238         assertEquals(0x00, b[3 + off]);
239     }
240 }