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.AsiExtraField.
025: *
026: */
027: public class AsiExtraFieldTest extends TestCase implements UnixStat {
028: public AsiExtraFieldTest(String name) {
029: super (name);
030: }
031:
032: /**
033: * Test file mode magic.
034: */
035: public void testModes() {
036: AsiExtraField a = new AsiExtraField();
037: a.setMode(0123);
038: assertEquals("plain file", 0100123, a.getMode());
039: a.setDirectory(true);
040: assertEquals("directory", 040123, a.getMode());
041: a.setLinkedFile("test");
042: assertEquals("symbolic link", 0120123, a.getMode());
043: }
044:
045: /**
046: * Test content.
047: */
048: public void testContent() {
049: AsiExtraField a = new AsiExtraField();
050: a.setMode(0123);
051: a.setUserId(5);
052: a.setGroupId(6);
053: byte[] b = a.getLocalFileDataData();
054:
055: // CRC manually calculated, sorry
056: byte[] expect = { (byte) 0xC6, 0x02, 0x78, (byte) 0xB6, // CRC
057: 0123, (byte) 0x80, // mode
058: 0, 0, 0, 0, // link length
059: 5, 0, 6, 0 }; // uid, gid
060: assertEquals("no link", expect.length, b.length);
061: for (int i = 0; i < expect.length; i++) {
062: assertEquals("no link, byte " + i, expect[i], b[i]);
063: }
064:
065: a.setLinkedFile("test");
066: expect = new byte[] { 0x75, (byte) 0x8E, 0x41, (byte) 0xFD, // CRC
067: 0123, (byte) 0xA0, // mode
068: 4, 0, 0, 0, // link length
069: 5, 0, 6, 0, // uid, gid
070: (byte) 't', (byte) 'e', (byte) 's', (byte) 't' };
071: b = a.getLocalFileDataData();
072: assertEquals("no link", expect.length, b.length);
073: for (int i = 0; i < expect.length; i++) {
074: assertEquals("no link, byte " + i, expect[i], b[i]);
075: }
076:
077: }
078:
079: /**
080: * Test reparse
081: */
082: public void testReparse() throws Exception {
083: // CRC manually calculated, sorry
084: byte[] data = { (byte) 0xC6, 0x02, 0x78, (byte) 0xB6, // CRC
085: 0123, (byte) 0x80, // mode
086: 0, 0, 0, 0, // link length
087: 5, 0, 6, 0 }; // uid, gid
088: AsiExtraField a = new AsiExtraField();
089: a.parseFromLocalFileData(data, 0, data.length);
090: assertEquals("length plain file", data.length, a
091: .getLocalFileDataLength().getValue());
092: assertTrue("plain file, no link", !a.isLink());
093: assertTrue("plain file, no dir", !a.isDirectory());
094: assertEquals("mode plain file", FILE_FLAG | 0123, a.getMode());
095: assertEquals("uid plain file", 5, a.getUserId());
096: assertEquals("gid plain file", 6, a.getGroupId());
097:
098: data = new byte[] { 0x75, (byte) 0x8E, 0x41, (byte) 0xFD, // CRC
099: 0123, (byte) 0xA0, // mode
100: 4, 0, 0, 0, // link length
101: 5, 0, 6, 0, // uid, gid
102: (byte) 't', (byte) 'e', (byte) 's', (byte) 't' };
103: a = new AsiExtraField();
104: a.parseFromLocalFileData(data, 0, data.length);
105: assertEquals("length link", data.length, a
106: .getLocalFileDataLength().getValue());
107: assertTrue("link, is link", a.isLink());
108: assertTrue("link, no dir", !a.isDirectory());
109: assertEquals("mode link", LINK_FLAG | 0123, a.getMode());
110: assertEquals("uid link", 5, a.getUserId());
111: assertEquals("gid link", 6, a.getGroupId());
112: assertEquals("test", a.getLinkedFile());
113:
114: data = new byte[] { (byte) 0x8E, 0x01, (byte) 0xBF,
115: (byte) 0x0E, // CRC
116: 0123, (byte) 0x40, // mode
117: 0, 0, 0, 0, // link
118: 5, 0, 6, 0 }; // uid, gid
119: a = new AsiExtraField();
120: a.parseFromLocalFileData(data, 0, data.length);
121: assertEquals("length dir", data.length, a
122: .getLocalFileDataLength().getValue());
123: assertTrue("dir, no link", !a.isLink());
124: assertTrue("dir, is dir", a.isDirectory());
125: assertEquals("mode dir", DIR_FLAG | 0123, a.getMode());
126: assertEquals("uid dir", 5, a.getUserId());
127: assertEquals("gid dir", 6, a.getGroupId());
128:
129: data = new byte[] { 0, 0, 0, 0, // bad CRC
130: 0123, (byte) 0x40, // mode
131: 0, 0, 0, 0, // link
132: 5, 0, 6, 0 }; // uid, gid
133: a = new AsiExtraField();
134: try {
135: a.parseFromLocalFileData(data, 0, data.length);
136: fail("should raise bad CRC exception");
137: } catch (Exception e) {
138: assertEquals("bad CRC checksum 0 instead of ebf018e", e
139: .getMessage());
140: }
141: }
142: }
|