~ubuntu-branches/ubuntu/saucy/libcommons-compress-java/saucy-proposed

« back to all changes in this revision

Viewing changes to src/test/java/org/apache/commons/compress/archivers/zip/Zip64ExtendedInformationExtraFieldTest.java

  • Committer: Bazaar Package Importer
  • Author(s): Torsten Werner
  • Date: 2011-08-07 01:56:15 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20110807015615-cvbmhuv10g12fpz1
Tags: 1.2-1
* New upstream release
* Clean up Depends and Suggests fields.
* Switch to source format 3.0.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
import junit.framework.TestCase;
 
24
 
 
25
public class Zip64ExtendedInformationExtraFieldTest extends TestCase {
 
26
    public Zip64ExtendedInformationExtraFieldTest(String name) {
 
27
        super(name);
 
28
    }
 
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
    public void testWriteCDOnlySizes() {
 
41
        Zip64ExtendedInformationExtraField f =
 
42
            new Zip64ExtendedInformationExtraField(SIZE, CSIZE);
 
43
        assertEquals(new ZipShort(16), f.getCentralDirectoryLength());
 
44
        byte[] b = f.getCentralDirectoryData();
 
45
        assertEquals(16, b.length);
 
46
        checkSizes(b);
 
47
    }
 
48
 
 
49
    public void testWriteCDSizeAndOffset() {
 
50
        Zip64ExtendedInformationExtraField f =
 
51
            new Zip64ExtendedInformationExtraField(SIZE, CSIZE, OFF, null);
 
52
        assertEquals(new ZipShort(24), f.getCentralDirectoryLength());
 
53
        byte[] b = f.getCentralDirectoryData();
 
54
        assertEquals(24, b.length);
 
55
        checkSizes(b);
 
56
        checkOffset(b, 16);
 
57
    }
 
58
 
 
59
    public void testWriteCDSizeOffsetAndDisk() {
 
60
        Zip64ExtendedInformationExtraField f =
 
61
            new Zip64ExtendedInformationExtraField(SIZE, CSIZE, OFF, DISK);
 
62
        assertEquals(new ZipShort(28), f.getCentralDirectoryLength());
 
63
        byte[] b = f.getCentralDirectoryData();
 
64
        assertEquals(28, b.length);
 
65
        checkSizes(b);
 
66
        checkOffset(b, 16);
 
67
        checkDisk(b, 24);
 
68
    }
 
69
 
 
70
    public void testWriteCDSizeAndDisk() {
 
71
        Zip64ExtendedInformationExtraField f =
 
72
            new Zip64ExtendedInformationExtraField(SIZE, CSIZE, null, DISK);
 
73
        assertEquals(new ZipShort(20), f.getCentralDirectoryLength());
 
74
        byte[] b = f.getCentralDirectoryData();
 
75
        assertEquals(20, b.length);
 
76
        checkSizes(b);
 
77
        checkDisk(b, 16);
 
78
    }
 
79
 
 
80
    public void testReadLFHSizesOnly() throws ZipException {
 
81
        Zip64ExtendedInformationExtraField f =
 
82
            new Zip64ExtendedInformationExtraField();
 
83
        byte[] b = new byte[16];
 
84
        System.arraycopy(SIZE.getBytes(), 0, b, 0, 8);
 
85
        System.arraycopy(CSIZE.getBytes(), 0, b, 8, 8);
 
86
        f.parseFromLocalFileData(b, 0, b.length);
 
87
        assertEquals(SIZE, f.getSize());
 
88
        assertEquals(CSIZE, f.getCompressedSize());
 
89
        assertNull(f.getRelativeHeaderOffset());
 
90
        assertNull(f.getDiskStartNumber());
 
91
    }
 
92
 
 
93
    public void testReadLFHSizesAndOffset() throws ZipException {
 
94
        Zip64ExtendedInformationExtraField f =
 
95
            new Zip64ExtendedInformationExtraField();
 
96
        byte[] b = new byte[24];
 
97
        System.arraycopy(SIZE.getBytes(), 0, b, 0, 8);
 
98
        System.arraycopy(CSIZE.getBytes(), 0, b, 8, 8);
 
99
        System.arraycopy(OFF.getBytes(), 0, b, 16, 8);
 
100
        f.parseFromLocalFileData(b, 0, b.length);
 
101
        assertEquals(SIZE, f.getSize());
 
102
        assertEquals(CSIZE, f.getCompressedSize());
 
103
        assertEquals(OFF, f.getRelativeHeaderOffset());
 
104
        assertNull(f.getDiskStartNumber());
 
105
    }
 
106
 
 
107
    public void testReadLFHSizesOffsetAndDisk() throws ZipException {
 
108
        Zip64ExtendedInformationExtraField f =
 
109
            new Zip64ExtendedInformationExtraField();
 
110
        byte[] b = new byte[28];
 
111
        System.arraycopy(SIZE.getBytes(), 0, b, 0, 8);
 
112
        System.arraycopy(CSIZE.getBytes(), 0, b, 8, 8);
 
113
        System.arraycopy(OFF.getBytes(), 0, b, 16, 8);
 
114
        System.arraycopy(DISK.getBytes(), 0, b, 24, 4);
 
115
        f.parseFromLocalFileData(b, 0, b.length);
 
116
        assertEquals(SIZE, f.getSize());
 
117
        assertEquals(CSIZE, f.getCompressedSize());
 
118
        assertEquals(OFF, f.getRelativeHeaderOffset());
 
119
        assertEquals(DISK, f.getDiskStartNumber());
 
120
    }
 
121
 
 
122
    public void testReadLFHSizesAndDisk() throws ZipException {
 
123
        Zip64ExtendedInformationExtraField f =
 
124
            new Zip64ExtendedInformationExtraField();
 
125
        byte[] b = new byte[20];
 
126
        System.arraycopy(SIZE.getBytes(), 0, b, 0, 8);
 
127
        System.arraycopy(CSIZE.getBytes(), 0, b, 8, 8);
 
128
        System.arraycopy(DISK.getBytes(), 0, b, 16, 4);
 
129
        f.parseFromLocalFileData(b, 0, b.length);
 
130
        assertEquals(SIZE, f.getSize());
 
131
        assertEquals(CSIZE, f.getCompressedSize());
 
132
        assertNull(f.getRelativeHeaderOffset());
 
133
        assertEquals(DISK, f.getDiskStartNumber());
 
134
    }
 
135
 
 
136
    public void testReadCDSizesOffsetAndDisk() throws ZipException {
 
137
        Zip64ExtendedInformationExtraField f =
 
138
            new Zip64ExtendedInformationExtraField();
 
139
        byte[] b = new byte[28];
 
140
        System.arraycopy(SIZE.getBytes(), 0, b, 0, 8);
 
141
        System.arraycopy(CSIZE.getBytes(), 0, b, 8, 8);
 
142
        System.arraycopy(OFF.getBytes(), 0, b, 16, 8);
 
143
        System.arraycopy(DISK.getBytes(), 0, b, 24, 4);
 
144
        f.parseFromCentralDirectoryData(b, 0, b.length);
 
145
        assertEquals(SIZE, f.getSize());
 
146
        assertEquals(CSIZE, f.getCompressedSize());
 
147
        assertEquals(OFF, f.getRelativeHeaderOffset());
 
148
        assertEquals(DISK, f.getDiskStartNumber());
 
149
    }
 
150
 
 
151
    public void testReadCDSomethingAndDisk() throws ZipException {
 
152
        Zip64ExtendedInformationExtraField f =
 
153
            new Zip64ExtendedInformationExtraField();
 
154
        byte[] b = new byte[12];
 
155
        System.arraycopy(SIZE.getBytes(), 0, b, 0, 8);
 
156
        System.arraycopy(DISK.getBytes(), 0, b, 8, 4);
 
157
        f.parseFromCentralDirectoryData(b, 0, b.length);
 
158
        assertNull(f.getSize());
 
159
        assertNull(f.getCompressedSize());
 
160
        assertNull(f.getRelativeHeaderOffset());
 
161
        assertEquals(DISK, f.getDiskStartNumber());
 
162
    }
 
163
 
 
164
    private static void checkSizes(byte[] b) {
 
165
        assertEquals(0x78, b[0]);
 
166
        assertEquals(0x56, b[1]);
 
167
        assertEquals(0x34, b[2]);
 
168
        assertEquals(0x12, b[3]);
 
169
        assertEquals(0x00, b[4]);
 
170
        assertEquals(0x00, b[5]);
 
171
        assertEquals(0x00, b[6]);
 
172
        assertEquals(0x00, b[7]);
 
173
        assertEquals((byte) 0xEF, b[8]);
 
174
        assertEquals((byte) 0xCD, b[9]);
 
175
        assertEquals((byte) 0xAB, b[10]);
 
176
        assertEquals(0x09, b[11]);
 
177
        assertEquals(0x00, b[12]);
 
178
        assertEquals(0x00, b[13]);
 
179
        assertEquals(0x00, b[14]);
 
180
        assertEquals(0x00, b[15]);
 
181
    }
 
182
 
 
183
    private static void checkOffset(byte[] b, int off) {
 
184
        assertEquals(0x78, b[0 + off]);
 
185
        assertEquals(0x56, b[1 + off]);
 
186
        assertEquals(0x34, b[2 + off]);
 
187
        assertEquals(0x12, b[3 + off]);
 
188
        assertEquals((byte) 0x09, b[4 + off]);
 
189
        assertEquals((byte) 0xEF, b[5 + off]);
 
190
        assertEquals((byte) 0xCD, b[6 + off]);
 
191
        assertEquals((byte) 0xAB, b[7 + off]);
 
192
    }
 
193
 
 
194
    private static void checkDisk(byte[] b, int off) {
 
195
        assertEquals(0x12, b[0 + off]);
 
196
        assertEquals(0x00, b[1 + off]);
 
197
        assertEquals(0x00, b[2 + off]);
 
198
        assertEquals(0x00, b[3 + off]);
 
199
    }
 
200
}
 
 
b'\\ No newline at end of file'