001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.tools.zip;
020:
021: import junit.framework.TestCase;
022:
023: /**
024: * JUnit 3 testcases for org.apache.tools.zip.ExtraFieldUtils.
025: *
026: */
027: public class ExtraFieldUtilsTest extends TestCase implements UnixStat {
028: public ExtraFieldUtilsTest(String name) {
029: super (name);
030: }
031:
032: private AsiExtraField a;
033: private UnrecognizedExtraField dummy;
034: private byte[] data;
035: private byte[] aLocal;
036:
037: public void setUp() {
038: a = new AsiExtraField();
039: a.setMode(0755);
040: a.setDirectory(true);
041: dummy = new UnrecognizedExtraField();
042: dummy.setHeaderId(new ZipShort(1));
043: dummy.setLocalFileDataData(new byte[0]);
044: dummy.setCentralDirectoryData(new byte[] { 0 });
045:
046: aLocal = a.getLocalFileDataData();
047: byte[] dummyLocal = dummy.getLocalFileDataData();
048: data = new byte[4 + aLocal.length + 4 + dummyLocal.length];
049: System.arraycopy(a.getHeaderId().getBytes(), 0, data, 0, 2);
050: System.arraycopy(a.getLocalFileDataLength().getBytes(), 0,
051: data, 2, 2);
052: System.arraycopy(aLocal, 0, data, 4, aLocal.length);
053: System.arraycopy(dummy.getHeaderId().getBytes(), 0, data,
054: 4 + aLocal.length, 2);
055: System.arraycopy(dummy.getLocalFileDataLength().getBytes(), 0,
056: data, 4 + aLocal.length + 2, 2);
057: System.arraycopy(dummyLocal, 0, data, 4 + aLocal.length + 4,
058: dummyLocal.length);
059:
060: }
061:
062: /**
063: * test parser.
064: */
065: public void testParse() throws Exception {
066: ZipExtraField[] ze = ExtraFieldUtils.parse(data);
067: assertEquals("number of fields", 2, ze.length);
068: assertTrue("type field 1", ze[0] instanceof AsiExtraField);
069: assertEquals("mode field 1", 040755, ((AsiExtraField) ze[0])
070: .getMode());
071: assertTrue("type field 2",
072: ze[1] instanceof UnrecognizedExtraField);
073: assertEquals("data length field 2", 0, ze[1]
074: .getLocalFileDataLength().getValue());
075:
076: byte[] data2 = new byte[data.length - 1];
077: System.arraycopy(data, 0, data2, 0, data2.length);
078: try {
079: ExtraFieldUtils.parse(data2);
080: fail("data should be invalid");
081: } catch (Exception e) {
082: assertEquals("message", "data starting at "
083: + (4 + aLocal.length) + " is in unknown format", e
084: .getMessage());
085: }
086: }
087:
088: /**
089: * Test merge methods
090: */
091: public void testMerge() {
092: byte[] local = ExtraFieldUtils
093: .mergeLocalFileDataData(new ZipExtraField[] { a, dummy });
094: assertEquals("local length", data.length, local.length);
095: for (int i = 0; i < local.length; i++) {
096: assertEquals("local byte " + i, data[i], local[i]);
097: }
098:
099: byte[] dummyCentral = dummy.getCentralDirectoryData();
100: byte[] data2 = new byte[4 + aLocal.length + 4
101: + dummyCentral.length];
102: System.arraycopy(data, 0, data2, 0, 4 + aLocal.length + 2);
103: System.arraycopy(dummy.getCentralDirectoryLength().getBytes(),
104: 0, data2, 4 + aLocal.length + 2, 2);
105: System.arraycopy(dummyCentral, 0, data2, 4 + aLocal.length + 4,
106: dummyCentral.length);
107:
108: byte[] central = ExtraFieldUtils
109: .mergeCentralDirectoryData(new ZipExtraField[] { a,
110: dummy });
111: assertEquals("central length", data2.length, central.length);
112: for (int i = 0; i < central.length; i++) {
113: assertEquals("central byte " + i, data2[i], central[i]);
114: }
115:
116: }
117: }
|