1 | /*
|
---|
2 | * Copyright 2001,2004 The Apache Software Foundation
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
5 | * you may not use this file except in compliance with the License.
|
---|
6 | * You may obtain a copy of the License at
|
---|
7 | *
|
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
9 | *
|
---|
10 | * Unless required by applicable law or agreed to in writing, software
|
---|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
13 | * See the License for the specific language governing permissions and
|
---|
14 | * limitations under the License.
|
---|
15 | *
|
---|
16 | */
|
---|
17 |
|
---|
18 | package org.apache.tools.zip;
|
---|
19 |
|
---|
20 | import junit.framework.TestCase;
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * JUnit 3 testcases for org.apache.tools.zip.ExtraFieldUtils.
|
---|
24 | *
|
---|
25 | */
|
---|
26 | public class ExtraFieldUtilsTest extends TestCase implements UnixStat {
|
---|
27 | public ExtraFieldUtilsTest(String name) {
|
---|
28 | super(name);
|
---|
29 | }
|
---|
30 |
|
---|
31 | private AsiExtraField a;
|
---|
32 | private UnrecognizedExtraField dummy;
|
---|
33 | private byte[] data;
|
---|
34 | private byte[] aLocal;
|
---|
35 |
|
---|
36 | public void setUp() {
|
---|
37 | a = new AsiExtraField();
|
---|
38 | a.setMode(0755);
|
---|
39 | a.setDirectory(true);
|
---|
40 | dummy = new UnrecognizedExtraField();
|
---|
41 | dummy.setHeaderId(new ZipShort(1));
|
---|
42 | dummy.setLocalFileDataData(new byte[0]);
|
---|
43 | dummy.setCentralDirectoryData(new byte[] {0});
|
---|
44 |
|
---|
45 | aLocal = a.getLocalFileDataData();
|
---|
46 | byte[] dummyLocal = dummy.getLocalFileDataData();
|
---|
47 | data = new byte[4 + aLocal.length + 4 + dummyLocal.length];
|
---|
48 | System.arraycopy(a.getHeaderId().getBytes(), 0, data, 0, 2);
|
---|
49 | System.arraycopy(a.getLocalFileDataLength().getBytes(), 0, data, 2, 2);
|
---|
50 | System.arraycopy(aLocal, 0, data, 4, aLocal.length);
|
---|
51 | System.arraycopy(dummy.getHeaderId().getBytes(), 0, data,
|
---|
52 | 4+aLocal.length, 2);
|
---|
53 | System.arraycopy(dummy.getLocalFileDataLength().getBytes(), 0, data,
|
---|
54 | 4+aLocal.length+2, 2);
|
---|
55 | System.arraycopy(dummyLocal, 0, data,
|
---|
56 | 4+aLocal.length+4, dummyLocal.length);
|
---|
57 |
|
---|
58 | }
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * test parser.
|
---|
62 | */
|
---|
63 | public void testParse() throws Exception {
|
---|
64 | ZipExtraField[] ze = ExtraFieldUtils.parse(data);
|
---|
65 | assertEquals("number of fields", 2, ze.length);
|
---|
66 | assertTrue("type field 1", ze[0] instanceof AsiExtraField);
|
---|
67 | assertEquals("mode field 1", 040755,
|
---|
68 | ((AsiExtraField) ze[0]).getMode());
|
---|
69 | assertTrue("type field 2", ze[1] instanceof UnrecognizedExtraField);
|
---|
70 | assertEquals("data length field 2", 0,
|
---|
71 | ze[1].getLocalFileDataLength().getValue());
|
---|
72 |
|
---|
73 | byte[] data2 = new byte[data.length-1];
|
---|
74 | System.arraycopy(data, 0, data2, 0, data2.length);
|
---|
75 | try {
|
---|
76 | ExtraFieldUtils.parse(data2);
|
---|
77 | fail("data should be invalid");
|
---|
78 | } catch (Exception e) {
|
---|
79 | assertEquals("message",
|
---|
80 | "data starting at "+(4+aLocal.length)+" is in unknown format",
|
---|
81 | e.getMessage());
|
---|
82 | }
|
---|
83 | }
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * Test merge methods
|
---|
87 | */
|
---|
88 | public void testMerge() {
|
---|
89 | byte[] local =
|
---|
90 | ExtraFieldUtils.mergeLocalFileDataData(new ZipExtraField[] {a, dummy});
|
---|
91 | assertEquals("local length", data.length, local.length);
|
---|
92 | for (int i=0; i<local.length; i++) {
|
---|
93 | assertEquals("local byte "+i, data[i], local[i]);
|
---|
94 | }
|
---|
95 |
|
---|
96 | byte[] dummyCentral = dummy.getCentralDirectoryData();
|
---|
97 | byte[] data2 = new byte[4 + aLocal.length + 4 + dummyCentral.length];
|
---|
98 | System.arraycopy(data, 0, data2, 0, 4 + aLocal.length + 2);
|
---|
99 | System.arraycopy(dummy.getCentralDirectoryLength().getBytes(), 0,
|
---|
100 | data2, 4+aLocal.length+2, 2);
|
---|
101 | System.arraycopy(dummyCentral, 0, data2,
|
---|
102 | 4+aLocal.length+4, dummyCentral.length);
|
---|
103 |
|
---|
104 |
|
---|
105 | byte[] central =
|
---|
106 | ExtraFieldUtils.mergeCentralDirectoryData(new ZipExtraField[] {a, dummy});
|
---|
107 | assertEquals("central length", data2.length, central.length);
|
---|
108 | for (int i=0; i<central.length; i++) {
|
---|
109 | assertEquals("central byte "+i, data2[i], central[i]);
|
---|
110 | }
|
---|
111 |
|
---|
112 | }
|
---|
113 | }
|
---|