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