git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@271065 13f79535-47bb-0310-9956-ffa450edef68master
@@ -0,0 +1,165 @@ | |||||
/* | |||||
* Copyright (C) The Apache Software Foundation. All rights reserved. | |||||
* | |||||
* This software is published under the terms of the Apache Software License | |||||
* version 1.1, a copy of which has been included with this distribution in | |||||
* the LICENSE.txt file. | |||||
*/ | |||||
package org.apache.aut.zip; | |||||
import java.util.zip.ZipException; | |||||
import junit.framework.TestCase; | |||||
/** | |||||
* JUnit testcases AsiExtraField. | |||||
* | |||||
* @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a> | |||||
*/ | |||||
public class AsiExtraFieldTest | |||||
extends TestCase | |||||
implements UnixStat | |||||
{ | |||||
public AsiExtraFieldTest( final String name ) | |||||
{ | |||||
super( name ); | |||||
} | |||||
/** | |||||
* Test file mode magic. | |||||
*/ | |||||
public void testModes() | |||||
{ | |||||
final AsiExtraField field = new AsiExtraField(); | |||||
field.setMode( 0123 ); | |||||
assertEquals( "plain file", 0100123, field.getMode() ); | |||||
field.setDirectory( true ); | |||||
assertEquals( "directory", 040123, field.getMode() ); | |||||
field.setLinkedFile( "test" ); | |||||
assertEquals( "symbolic link", 0120123, field.getMode() ); | |||||
} | |||||
private AsiExtraField createField() | |||||
{ | |||||
final AsiExtraField field = new AsiExtraField(); | |||||
field.setMode( 0123 ); | |||||
field.setUserId( 5 ); | |||||
field.setGroupId( 6 ); | |||||
return field; | |||||
} | |||||
public void testContent1() | |||||
{ | |||||
final AsiExtraField field = createField(); | |||||
final byte[] data = field.getLocalFileDataData(); | |||||
// CRC manually calculated, sorry | |||||
final byte[] expect = {(byte)0xC6, 0x02, 0x78, (byte)0xB6, // CRC | |||||
0123, (byte)0x80, // mode | |||||
0, 0, 0, 0, // link length | |||||
5, 0, 6, 0}; // uid, gid | |||||
assertEquals( "no link", expect.length, data.length ); | |||||
for( int i = 0; i < expect.length; i++ ) | |||||
{ | |||||
assertEquals( "no link, byte " + i, expect[ i ], data[ i ] ); | |||||
} | |||||
field.setLinkedFile( "test" ); | |||||
} | |||||
public void testContent2() | |||||
{ | |||||
final AsiExtraField field = createField(); | |||||
field.setLinkedFile( "test" ); | |||||
final byte[] data = field.getLocalFileDataData(); | |||||
final byte[] expect = new byte[]{0x75, (byte)0x8E, 0x41, (byte)0xFD, // CRC | |||||
0123, (byte)0xA0, // mode | |||||
4, 0, 0, 0, // link length | |||||
5, 0, 6, 0, // uid, gid | |||||
(byte)'t', (byte)'e', (byte)'s', (byte)'t'}; | |||||
assertEquals( "no link", expect.length, data.length ); | |||||
for( int i = 0; i < expect.length; i++ ) | |||||
{ | |||||
assertEquals( "no link, byte " + i, expect[ i ], data[ i ] ); | |||||
} | |||||
} | |||||
public void testReparse1() | |||||
throws ZipException | |||||
{ | |||||
// CRC manually calculated, sorry | |||||
final byte[] data = {(byte)0xC6, 0x02, 0x78, (byte)0xB6, // CRC | |||||
0123, (byte)0x80, // mode | |||||
0, 0, 0, 0, // link length | |||||
5, 0, 6, 0}; // uid, gid | |||||
final AsiExtraField field = new AsiExtraField(); | |||||
field.parseFromLocalFileData( data, 0, data.length ); | |||||
assertEquals( "length plain file", data.length, | |||||
field.getLocalFileDataLength().getValue() ); | |||||
assertTrue( "plain file, no link", !field.isLink() ); | |||||
assertTrue( "plain file, no dir", !field.isDirectory() ); | |||||
assertEquals( "mode plain file", FILE_FLAG | 0123, field.getMode() ); | |||||
assertEquals( "uid plain file", 5, field.getUserId() ); | |||||
assertEquals( "gid plain file", 6, field.getGroupId() ); | |||||
} | |||||
public void testReparse2() | |||||
throws ZipException | |||||
{ | |||||
final byte[] data = new byte[]{0x75, (byte)0x8E, 0x41, (byte)0xFD, // CRC | |||||
0123, (byte)0xA0, // mode | |||||
4, 0, 0, 0, // link length | |||||
5, 0, 6, 0, // uid, gid | |||||
(byte)'t', (byte)'e', (byte)'s', (byte)'t'}; | |||||
final AsiExtraField field = new AsiExtraField(); | |||||
field.parseFromLocalFileData( data, 0, data.length ); | |||||
assertEquals( "length link", data.length, | |||||
field.getLocalFileDataLength().getValue() ); | |||||
assertTrue( "link, is link", field.isLink() ); | |||||
assertTrue( "link, no dir", !field.isDirectory() ); | |||||
assertEquals( "mode link", LINK_FLAG | 0123, field.getMode() ); | |||||
assertEquals( "uid link", 5, field.getUserId() ); | |||||
assertEquals( "gid link", 6, field.getGroupId() ); | |||||
assertEquals( "test", field.getLinkedFile() ); | |||||
} | |||||
public void testReparse3() | |||||
throws ZipException | |||||
{ | |||||
final byte[] data = new byte[]{(byte)0x8E, 0x01, (byte)0xBF, (byte)0x0E, // CRC | |||||
0123, (byte)0x40, // mode | |||||
0, 0, 0, 0, // link | |||||
5, 0, 6, 0}; // uid, gid | |||||
final AsiExtraField field = new AsiExtraField(); | |||||
field.parseFromLocalFileData( data, 0, data.length ); | |||||
assertEquals( "length dir", data.length, | |||||
field.getLocalFileDataLength().getValue() ); | |||||
assertTrue( "dir, no link", !field.isLink() ); | |||||
assertTrue( "dir, is dir", field.isDirectory() ); | |||||
assertEquals( "mode dir", DIR_FLAG | 0123, field.getMode() ); | |||||
assertEquals( "uid dir", 5, field.getUserId() ); | |||||
assertEquals( "gid dir", 6, field.getGroupId() ); | |||||
} | |||||
public void testReparse4() | |||||
throws Exception | |||||
{ | |||||
final byte[] data = new byte[]{0, 0, 0, 0, // bad CRC | |||||
0123, (byte)0x40, // mode | |||||
0, 0, 0, 0, // link | |||||
5, 0, 6, 0}; // uid, gid | |||||
final AsiExtraField field = new AsiExtraField(); | |||||
try | |||||
{ | |||||
field.parseFromLocalFileData( data, 0, data.length ); | |||||
fail( "should raise bad CRC exception" ); | |||||
} | |||||
catch( Exception e ) | |||||
{ | |||||
assertEquals( "bad CRC checksum 0 instead of ebf018e", | |||||
e.getMessage() ); | |||||
} | |||||
} | |||||
} |
@@ -0,0 +1,114 @@ | |||||
/* | |||||
* Copyright (C) The Apache Software Foundation. All rights reserved. | |||||
* | |||||
* This software is published under the terms of the Apache Software License | |||||
* version 1.1, a copy of which has been included with this distribution in | |||||
* the LICENSE.txt file. | |||||
*/ | |||||
package org.apache.aut.zip; | |||||
import junit.framework.TestCase; | |||||
/** | |||||
* JUnit testcases ExtraFieldUtils. | |||||
* | |||||
* @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a> | |||||
*/ | |||||
public class ExtraFieldUtilsTest | |||||
extends TestCase | |||||
implements UnixStat | |||||
{ | |||||
private AsiExtraField m_field; | |||||
private UnrecognizedExtraField m_dummy; | |||||
private byte[] m_data; | |||||
private byte[] m_local; | |||||
public ExtraFieldUtilsTest( final String name ) | |||||
{ | |||||
super( name ); | |||||
} | |||||
public void setUp() | |||||
{ | |||||
m_field = new AsiExtraField(); | |||||
m_field.setMode( 0755 ); | |||||
m_field.setDirectory( true ); | |||||
m_dummy = new UnrecognizedExtraField(); | |||||
m_dummy.setHeaderId( new ZipShort( 1 ) ); | |||||
m_dummy.setLocalFileDataData( new byte[ 0 ] ); | |||||
m_dummy.setCentralDirectoryData( new byte[]{0} ); | |||||
m_local = m_field.getLocalFileDataData(); | |||||
final byte[] dummyLocal = m_dummy.getLocalFileDataData(); | |||||
m_data = new byte[ 4 + m_local.length + 4 + dummyLocal.length ]; | |||||
System.arraycopy( m_field.getHeaderId().getBytes(), 0, m_data, 0, 2 ); | |||||
System.arraycopy( m_field.getLocalFileDataLength().getBytes(), 0, m_data, 2, 2 ); | |||||
System.arraycopy( m_local, 0, m_data, 4, m_local.length ); | |||||
System.arraycopy( m_dummy.getHeaderId().getBytes(), 0, m_data, | |||||
4 + m_local.length, 2 ); | |||||
System.arraycopy( m_dummy.getLocalFileDataLength().getBytes(), 0, m_data, | |||||
4 + m_local.length + 2, 2 ); | |||||
System.arraycopy( dummyLocal, 0, m_data, | |||||
4 + m_local.length + 4, dummyLocal.length ); | |||||
} | |||||
/** | |||||
* test parser. | |||||
*/ | |||||
public void testParse() throws Exception | |||||
{ | |||||
final ZipExtraField[] extraField = ExtraFieldUtils.parse( m_data ); | |||||
assertEquals( "number of fields", 2, extraField.length ); | |||||
assertTrue( "type field 1", extraField[ 0 ] instanceof AsiExtraField ); | |||||
assertEquals( "mode field 1", 040755, | |||||
( (AsiExtraField)extraField[ 0 ] ).getMode() ); | |||||
assertTrue( "type field 2", extraField[ 1 ] instanceof UnrecognizedExtraField ); | |||||
assertEquals( "data length field 2", 0, | |||||
extraField[ 1 ].getLocalFileDataLength().getValue() ); | |||||
final byte[] data2 = new byte[ m_data.length - 1 ]; | |||||
System.arraycopy( m_data, 0, data2, 0, data2.length ); | |||||
try | |||||
{ | |||||
ExtraFieldUtils.parse( data2 ); | |||||
fail( "data should be invalid" ); | |||||
} | |||||
catch( Exception e ) | |||||
{ | |||||
assertEquals( "message", | |||||
"data starting at " + ( 4 + m_local.length ) + " is in unknown format", | |||||
e.getMessage() ); | |||||
} | |||||
} | |||||
/** | |||||
* Test merge methods | |||||
*/ | |||||
public void testMerge() | |||||
{ | |||||
final byte[] local = | |||||
ExtraFieldUtils.mergeLocalFileDataData( new ZipExtraField[]{m_field, m_dummy} ); | |||||
assertEquals( "local length", m_data.length, local.length ); | |||||
for( int i = 0; i < local.length; i++ ) | |||||
{ | |||||
assertEquals( "local byte " + i, m_data[ i ], local[ i ] ); | |||||
} | |||||
final byte[] dummyCentral = m_dummy.getCentralDirectoryData(); | |||||
final byte[] data2 = new byte[ 4 + m_local.length + 4 + dummyCentral.length ]; | |||||
System.arraycopy( m_data, 0, data2, 0, 4 + m_local.length + 2 ); | |||||
System.arraycopy( m_dummy.getCentralDirectoryLength().getBytes(), 0, | |||||
data2, 4 + m_local.length + 2, 2 ); | |||||
System.arraycopy( dummyCentral, 0, data2, | |||||
4 + m_local.length + 4, dummyCentral.length ); | |||||
final byte[] central = | |||||
ExtraFieldUtils.mergeCentralDirectoryData( new ZipExtraField[]{m_field, m_dummy} ); | |||||
assertEquals( "central length", data2.length, central.length ); | |||||
for( int i = 0; i < central.length; i++ ) | |||||
{ | |||||
assertEquals( "central byte " + i, data2[ i ], central[ i ] ); | |||||
} | |||||
} | |||||
} |
@@ -0,0 +1,94 @@ | |||||
/* | |||||
* Copyright (C) The Apache Software Foundation. All rights reserved. | |||||
* | |||||
* This software is published under the terms of the Apache Software License | |||||
* version 1.1, a copy of which has been included with this distribution in | |||||
* the LICENSE.txt file. | |||||
*/ | |||||
package org.apache.aut.zip; | |||||
import junit.framework.TestCase; | |||||
import java.util.NoSuchElementException; | |||||
/** | |||||
* JUnit testcases ZipEntry. | |||||
* | |||||
* @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a> | |||||
*/ | |||||
public class ZipEntryTest | |||||
extends TestCase | |||||
{ | |||||
public ZipEntryTest( final String name ) | |||||
{ | |||||
super( name ); | |||||
} | |||||
/** | |||||
* test handling of extra fields | |||||
*/ | |||||
public void testExtraFields() | |||||
{ | |||||
final AsiExtraField field = createField(); | |||||
final UnrecognizedExtraField extraField = createExtraField(); | |||||
final ZipEntry entry = new ZipEntry( "test/" ); | |||||
entry.setExtraFields( new ZipExtraField[]{field, extraField} ); | |||||
final byte[] data1 = entry.getExtra(); | |||||
ZipExtraField[] result = entry.getExtraFields(); | |||||
assertEquals( "first pass", 2, result.length ); | |||||
assertSame( field, result[ 0 ] ); | |||||
assertSame( extraField, result[ 1 ] ); | |||||
UnrecognizedExtraField u2 = new UnrecognizedExtraField(); | |||||
u2.setHeaderId( new ZipShort( 1 ) ); | |||||
u2.setLocalFileDataData( new byte[]{1} ); | |||||
entry.addExtraField( u2 ); | |||||
byte[] data2 = entry.getExtra(); | |||||
result = entry.getExtraFields(); | |||||
assertEquals( "second pass", 2, result.length ); | |||||
assertSame( field, result[ 0 ] ); | |||||
assertSame( u2, result[ 1 ] ); | |||||
assertEquals( "length second pass", data1.length + 1, data2.length ); | |||||
UnrecognizedExtraField u3 = new UnrecognizedExtraField(); | |||||
u3.setHeaderId( new ZipShort( 2 ) ); | |||||
u3.setLocalFileDataData( new byte[]{1} ); | |||||
entry.addExtraField( u3 ); | |||||
result = entry.getExtraFields(); | |||||
assertEquals( "third pass", 3, result.length ); | |||||
entry.removeExtraField( new ZipShort( 1 ) ); | |||||
byte[] data3 = entry.getExtra(); | |||||
result = entry.getExtraFields(); | |||||
assertEquals( "fourth pass", 2, result.length ); | |||||
assertSame( field, result[ 0 ] ); | |||||
assertSame( u3, result[ 1 ] ); | |||||
assertEquals( "length fourth pass", data2.length, data3.length ); | |||||
try | |||||
{ | |||||
entry.removeExtraField( new ZipShort( 1 ) ); | |||||
fail( "should be no such element" ); | |||||
} | |||||
catch( final NoSuchElementException nse ) | |||||
{ | |||||
} | |||||
} | |||||
private UnrecognizedExtraField createExtraField() | |||||
{ | |||||
UnrecognizedExtraField extraField = new UnrecognizedExtraField(); | |||||
extraField.setHeaderId( new ZipShort( 1 ) ); | |||||
extraField.setLocalFileDataData( new byte[ 0 ] ); | |||||
return extraField; | |||||
} | |||||
private AsiExtraField createField() | |||||
{ | |||||
final AsiExtraField field = new AsiExtraField(); | |||||
field.setDirectory( true ); | |||||
field.setMode( 0755 ); | |||||
return field; | |||||
} | |||||
} |
@@ -0,0 +1,79 @@ | |||||
/* | |||||
* Copyright (C) The Apache Software Foundation. All rights reserved. | |||||
* | |||||
* This software is published under the terms of the Apache Software License | |||||
* version 1.1, a copy of which has been included with this distribution in | |||||
* the LICENSE.txt file. | |||||
*/ | |||||
package org.apache.aut.zip; | |||||
import junit.framework.TestCase; | |||||
/** | |||||
* JUnit 3 testcases for org.apache.tools.zip.ZipLong. | |||||
* | |||||
* @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a> | |||||
*/ | |||||
public class ZipLongTest | |||||
extends TestCase | |||||
{ | |||||
public ZipLongTest( final String name ) | |||||
{ | |||||
super( name ); | |||||
} | |||||
/** | |||||
* Test conversion to bytes. | |||||
*/ | |||||
public void testToBytes() | |||||
{ | |||||
final ZipLong zipLong = new ZipLong( 0x12345678 ); | |||||
final byte[] result = zipLong.getBytes(); | |||||
assertEquals( "length getBytes", 4, result.length ); | |||||
assertEquals( "first byte getBytes", 0x78, result[ 0 ] ); | |||||
assertEquals( "second byte getBytes", 0x56, result[ 1 ] ); | |||||
assertEquals( "third byte getBytes", 0x34, result[ 2 ] ); | |||||
assertEquals( "fourth byte getBytes", 0x12, result[ 3 ] ); | |||||
} | |||||
/** | |||||
* Test conversion from bytes. | |||||
*/ | |||||
public void testFromBytes() | |||||
{ | |||||
final byte[] value = new byte[]{0x78, 0x56, 0x34, 0x12}; | |||||
final ZipLong zipLong = new ZipLong( value ); | |||||
assertEquals( "value from bytes", 0x12345678, zipLong.getValue() ); | |||||
} | |||||
/** | |||||
* Test the contract of the equals method. | |||||
*/ | |||||
public void testEquals() | |||||
{ | |||||
final ZipLong zipLong1 = new ZipLong( 0x12345678 ); | |||||
final ZipLong zipLong2 = new ZipLong( 0x12345678 ); | |||||
final ZipLong zipLong3 = new ZipLong( 0x87654321 ); | |||||
assertTrue( "reflexive", zipLong1.equals( zipLong1 ) ); | |||||
assertTrue( "works", zipLong1.equals( zipLong2 ) ); | |||||
assertTrue( "works, part two", !zipLong1.equals( zipLong3 ) ); | |||||
assertTrue( "symmetric", zipLong2.equals( zipLong1 ) ); | |||||
assertTrue( "null handling", !zipLong1.equals( null ) ); | |||||
assertTrue( "non ZipLong handling", !zipLong1.equals( new Integer( 0x1234 ) ) ); | |||||
} | |||||
/** | |||||
* Test sign handling. | |||||
*/ | |||||
public void testSign() | |||||
{ | |||||
final ZipLong zipLong = | |||||
new ZipLong( new byte[]{(byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF} ); | |||||
assertEquals( 0x00000000FFFFFFFFl, zipLong.getValue() ); | |||||
} | |||||
} |
@@ -0,0 +1,75 @@ | |||||
/* | |||||
* Copyright (C) The Apache Software Foundation. All rights reserved. | |||||
* | |||||
* This software is published under the terms of the Apache Software License | |||||
* version 1.1, a copy of which has been included with this distribution in | |||||
* the LICENSE.txt file. | |||||
*/ | |||||
package org.apache.aut.zip; | |||||
import junit.framework.TestCase; | |||||
/** | |||||
* JUnit 3 testcases for org.apache.tools.zip.ZipShort. | |||||
* | |||||
* @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a> | |||||
*/ | |||||
public class ZipShortTest | |||||
extends TestCase | |||||
{ | |||||
public ZipShortTest( String name ) | |||||
{ | |||||
super( name ); | |||||
} | |||||
/** | |||||
* Test conversion to bytes. | |||||
*/ | |||||
public void testToBytes() | |||||
{ | |||||
final ZipShort zipShort = new ZipShort( 0x1234 ); | |||||
byte[] result = zipShort.getBytes(); | |||||
assertEquals( "length getBytes", 2, result.length ); | |||||
assertEquals( "first byte getBytes", 0x34, result[ 0 ] ); | |||||
assertEquals( "second byte getBytes", 0x12, result[ 1 ] ); | |||||
} | |||||
/** | |||||
* Test conversion from bytes. | |||||
*/ | |||||
public void testFromBytes() | |||||
{ | |||||
byte[] val = new byte[]{0x34, 0x12}; | |||||
final ZipShort zipShort = new ZipShort( val ); | |||||
assertEquals( "value from bytes", 0x1234, zipShort.getValue() ); | |||||
} | |||||
/** | |||||
* Test the contract of the equals method. | |||||
*/ | |||||
public void testEquals() | |||||
{ | |||||
final ZipShort zipShort = new ZipShort( 0x1234 ); | |||||
final ZipShort zipShort2 = new ZipShort( 0x1234 ); | |||||
final ZipShort zipShort3 = new ZipShort( 0x5678 ); | |||||
assertTrue( "reflexive", zipShort.equals( zipShort ) ); | |||||
assertTrue( "works", zipShort.equals( zipShort2 ) ); | |||||
assertTrue( "works, part two", !zipShort.equals( zipShort3 ) ); | |||||
assertTrue( "symmetric", zipShort2.equals( zipShort ) ); | |||||
assertTrue( "null handling", !zipShort.equals( null ) ); | |||||
assertTrue( "non ZipShort handling", !zipShort.equals( new Integer( 0x1234 ) ) ); | |||||
} | |||||
/** | |||||
* Test sign handling. | |||||
*/ | |||||
public void testSign() | |||||
{ | |||||
final ZipShort zipShort = new ZipShort( new byte[]{(byte)0xFF, (byte)0xFF} ); | |||||
assertEquals( 0x0000FFFF, zipShort.getValue() ); | |||||
} | |||||
} |
@@ -0,0 +1,165 @@ | |||||
/* | |||||
* Copyright (C) The Apache Software Foundation. All rights reserved. | |||||
* | |||||
* This software is published under the terms of the Apache Software License | |||||
* version 1.1, a copy of which has been included with this distribution in | |||||
* the LICENSE.txt file. | |||||
*/ | |||||
package org.apache.aut.zip; | |||||
import java.util.zip.ZipException; | |||||
import junit.framework.TestCase; | |||||
/** | |||||
* JUnit testcases AsiExtraField. | |||||
* | |||||
* @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a> | |||||
*/ | |||||
public class AsiExtraFieldTest | |||||
extends TestCase | |||||
implements UnixStat | |||||
{ | |||||
public AsiExtraFieldTest( final String name ) | |||||
{ | |||||
super( name ); | |||||
} | |||||
/** | |||||
* Test file mode magic. | |||||
*/ | |||||
public void testModes() | |||||
{ | |||||
final AsiExtraField field = new AsiExtraField(); | |||||
field.setMode( 0123 ); | |||||
assertEquals( "plain file", 0100123, field.getMode() ); | |||||
field.setDirectory( true ); | |||||
assertEquals( "directory", 040123, field.getMode() ); | |||||
field.setLinkedFile( "test" ); | |||||
assertEquals( "symbolic link", 0120123, field.getMode() ); | |||||
} | |||||
private AsiExtraField createField() | |||||
{ | |||||
final AsiExtraField field = new AsiExtraField(); | |||||
field.setMode( 0123 ); | |||||
field.setUserId( 5 ); | |||||
field.setGroupId( 6 ); | |||||
return field; | |||||
} | |||||
public void testContent1() | |||||
{ | |||||
final AsiExtraField field = createField(); | |||||
final byte[] data = field.getLocalFileDataData(); | |||||
// CRC manually calculated, sorry | |||||
final byte[] expect = {(byte)0xC6, 0x02, 0x78, (byte)0xB6, // CRC | |||||
0123, (byte)0x80, // mode | |||||
0, 0, 0, 0, // link length | |||||
5, 0, 6, 0}; // uid, gid | |||||
assertEquals( "no link", expect.length, data.length ); | |||||
for( int i = 0; i < expect.length; i++ ) | |||||
{ | |||||
assertEquals( "no link, byte " + i, expect[ i ], data[ i ] ); | |||||
} | |||||
field.setLinkedFile( "test" ); | |||||
} | |||||
public void testContent2() | |||||
{ | |||||
final AsiExtraField field = createField(); | |||||
field.setLinkedFile( "test" ); | |||||
final byte[] data = field.getLocalFileDataData(); | |||||
final byte[] expect = new byte[]{0x75, (byte)0x8E, 0x41, (byte)0xFD, // CRC | |||||
0123, (byte)0xA0, // mode | |||||
4, 0, 0, 0, // link length | |||||
5, 0, 6, 0, // uid, gid | |||||
(byte)'t', (byte)'e', (byte)'s', (byte)'t'}; | |||||
assertEquals( "no link", expect.length, data.length ); | |||||
for( int i = 0; i < expect.length; i++ ) | |||||
{ | |||||
assertEquals( "no link, byte " + i, expect[ i ], data[ i ] ); | |||||
} | |||||
} | |||||
public void testReparse1() | |||||
throws ZipException | |||||
{ | |||||
// CRC manually calculated, sorry | |||||
final byte[] data = {(byte)0xC6, 0x02, 0x78, (byte)0xB6, // CRC | |||||
0123, (byte)0x80, // mode | |||||
0, 0, 0, 0, // link length | |||||
5, 0, 6, 0}; // uid, gid | |||||
final AsiExtraField field = new AsiExtraField(); | |||||
field.parseFromLocalFileData( data, 0, data.length ); | |||||
assertEquals( "length plain file", data.length, | |||||
field.getLocalFileDataLength().getValue() ); | |||||
assertTrue( "plain file, no link", !field.isLink() ); | |||||
assertTrue( "plain file, no dir", !field.isDirectory() ); | |||||
assertEquals( "mode plain file", FILE_FLAG | 0123, field.getMode() ); | |||||
assertEquals( "uid plain file", 5, field.getUserId() ); | |||||
assertEquals( "gid plain file", 6, field.getGroupId() ); | |||||
} | |||||
public void testReparse2() | |||||
throws ZipException | |||||
{ | |||||
final byte[] data = new byte[]{0x75, (byte)0x8E, 0x41, (byte)0xFD, // CRC | |||||
0123, (byte)0xA0, // mode | |||||
4, 0, 0, 0, // link length | |||||
5, 0, 6, 0, // uid, gid | |||||
(byte)'t', (byte)'e', (byte)'s', (byte)'t'}; | |||||
final AsiExtraField field = new AsiExtraField(); | |||||
field.parseFromLocalFileData( data, 0, data.length ); | |||||
assertEquals( "length link", data.length, | |||||
field.getLocalFileDataLength().getValue() ); | |||||
assertTrue( "link, is link", field.isLink() ); | |||||
assertTrue( "link, no dir", !field.isDirectory() ); | |||||
assertEquals( "mode link", LINK_FLAG | 0123, field.getMode() ); | |||||
assertEquals( "uid link", 5, field.getUserId() ); | |||||
assertEquals( "gid link", 6, field.getGroupId() ); | |||||
assertEquals( "test", field.getLinkedFile() ); | |||||
} | |||||
public void testReparse3() | |||||
throws ZipException | |||||
{ | |||||
final byte[] data = new byte[]{(byte)0x8E, 0x01, (byte)0xBF, (byte)0x0E, // CRC | |||||
0123, (byte)0x40, // mode | |||||
0, 0, 0, 0, // link | |||||
5, 0, 6, 0}; // uid, gid | |||||
final AsiExtraField field = new AsiExtraField(); | |||||
field.parseFromLocalFileData( data, 0, data.length ); | |||||
assertEquals( "length dir", data.length, | |||||
field.getLocalFileDataLength().getValue() ); | |||||
assertTrue( "dir, no link", !field.isLink() ); | |||||
assertTrue( "dir, is dir", field.isDirectory() ); | |||||
assertEquals( "mode dir", DIR_FLAG | 0123, field.getMode() ); | |||||
assertEquals( "uid dir", 5, field.getUserId() ); | |||||
assertEquals( "gid dir", 6, field.getGroupId() ); | |||||
} | |||||
public void testReparse4() | |||||
throws Exception | |||||
{ | |||||
final byte[] data = new byte[]{0, 0, 0, 0, // bad CRC | |||||
0123, (byte)0x40, // mode | |||||
0, 0, 0, 0, // link | |||||
5, 0, 6, 0}; // uid, gid | |||||
final AsiExtraField field = new AsiExtraField(); | |||||
try | |||||
{ | |||||
field.parseFromLocalFileData( data, 0, data.length ); | |||||
fail( "should raise bad CRC exception" ); | |||||
} | |||||
catch( Exception e ) | |||||
{ | |||||
assertEquals( "bad CRC checksum 0 instead of ebf018e", | |||||
e.getMessage() ); | |||||
} | |||||
} | |||||
} |
@@ -0,0 +1,114 @@ | |||||
/* | |||||
* Copyright (C) The Apache Software Foundation. All rights reserved. | |||||
* | |||||
* This software is published under the terms of the Apache Software License | |||||
* version 1.1, a copy of which has been included with this distribution in | |||||
* the LICENSE.txt file. | |||||
*/ | |||||
package org.apache.aut.zip; | |||||
import junit.framework.TestCase; | |||||
/** | |||||
* JUnit testcases ExtraFieldUtils. | |||||
* | |||||
* @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a> | |||||
*/ | |||||
public class ExtraFieldUtilsTest | |||||
extends TestCase | |||||
implements UnixStat | |||||
{ | |||||
private AsiExtraField m_field; | |||||
private UnrecognizedExtraField m_dummy; | |||||
private byte[] m_data; | |||||
private byte[] m_local; | |||||
public ExtraFieldUtilsTest( final String name ) | |||||
{ | |||||
super( name ); | |||||
} | |||||
public void setUp() | |||||
{ | |||||
m_field = new AsiExtraField(); | |||||
m_field.setMode( 0755 ); | |||||
m_field.setDirectory( true ); | |||||
m_dummy = new UnrecognizedExtraField(); | |||||
m_dummy.setHeaderId( new ZipShort( 1 ) ); | |||||
m_dummy.setLocalFileDataData( new byte[ 0 ] ); | |||||
m_dummy.setCentralDirectoryData( new byte[]{0} ); | |||||
m_local = m_field.getLocalFileDataData(); | |||||
final byte[] dummyLocal = m_dummy.getLocalFileDataData(); | |||||
m_data = new byte[ 4 + m_local.length + 4 + dummyLocal.length ]; | |||||
System.arraycopy( m_field.getHeaderId().getBytes(), 0, m_data, 0, 2 ); | |||||
System.arraycopy( m_field.getLocalFileDataLength().getBytes(), 0, m_data, 2, 2 ); | |||||
System.arraycopy( m_local, 0, m_data, 4, m_local.length ); | |||||
System.arraycopy( m_dummy.getHeaderId().getBytes(), 0, m_data, | |||||
4 + m_local.length, 2 ); | |||||
System.arraycopy( m_dummy.getLocalFileDataLength().getBytes(), 0, m_data, | |||||
4 + m_local.length + 2, 2 ); | |||||
System.arraycopy( dummyLocal, 0, m_data, | |||||
4 + m_local.length + 4, dummyLocal.length ); | |||||
} | |||||
/** | |||||
* test parser. | |||||
*/ | |||||
public void testParse() throws Exception | |||||
{ | |||||
final ZipExtraField[] extraField = ExtraFieldUtils.parse( m_data ); | |||||
assertEquals( "number of fields", 2, extraField.length ); | |||||
assertTrue( "type field 1", extraField[ 0 ] instanceof AsiExtraField ); | |||||
assertEquals( "mode field 1", 040755, | |||||
( (AsiExtraField)extraField[ 0 ] ).getMode() ); | |||||
assertTrue( "type field 2", extraField[ 1 ] instanceof UnrecognizedExtraField ); | |||||
assertEquals( "data length field 2", 0, | |||||
extraField[ 1 ].getLocalFileDataLength().getValue() ); | |||||
final byte[] data2 = new byte[ m_data.length - 1 ]; | |||||
System.arraycopy( m_data, 0, data2, 0, data2.length ); | |||||
try | |||||
{ | |||||
ExtraFieldUtils.parse( data2 ); | |||||
fail( "data should be invalid" ); | |||||
} | |||||
catch( Exception e ) | |||||
{ | |||||
assertEquals( "message", | |||||
"data starting at " + ( 4 + m_local.length ) + " is in unknown format", | |||||
e.getMessage() ); | |||||
} | |||||
} | |||||
/** | |||||
* Test merge methods | |||||
*/ | |||||
public void testMerge() | |||||
{ | |||||
final byte[] local = | |||||
ExtraFieldUtils.mergeLocalFileDataData( new ZipExtraField[]{m_field, m_dummy} ); | |||||
assertEquals( "local length", m_data.length, local.length ); | |||||
for( int i = 0; i < local.length; i++ ) | |||||
{ | |||||
assertEquals( "local byte " + i, m_data[ i ], local[ i ] ); | |||||
} | |||||
final byte[] dummyCentral = m_dummy.getCentralDirectoryData(); | |||||
final byte[] data2 = new byte[ 4 + m_local.length + 4 + dummyCentral.length ]; | |||||
System.arraycopy( m_data, 0, data2, 0, 4 + m_local.length + 2 ); | |||||
System.arraycopy( m_dummy.getCentralDirectoryLength().getBytes(), 0, | |||||
data2, 4 + m_local.length + 2, 2 ); | |||||
System.arraycopy( dummyCentral, 0, data2, | |||||
4 + m_local.length + 4, dummyCentral.length ); | |||||
final byte[] central = | |||||
ExtraFieldUtils.mergeCentralDirectoryData( new ZipExtraField[]{m_field, m_dummy} ); | |||||
assertEquals( "central length", data2.length, central.length ); | |||||
for( int i = 0; i < central.length; i++ ) | |||||
{ | |||||
assertEquals( "central byte " + i, data2[ i ], central[ i ] ); | |||||
} | |||||
} | |||||
} |
@@ -0,0 +1,94 @@ | |||||
/* | |||||
* Copyright (C) The Apache Software Foundation. All rights reserved. | |||||
* | |||||
* This software is published under the terms of the Apache Software License | |||||
* version 1.1, a copy of which has been included with this distribution in | |||||
* the LICENSE.txt file. | |||||
*/ | |||||
package org.apache.aut.zip; | |||||
import junit.framework.TestCase; | |||||
import java.util.NoSuchElementException; | |||||
/** | |||||
* JUnit testcases ZipEntry. | |||||
* | |||||
* @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a> | |||||
*/ | |||||
public class ZipEntryTest | |||||
extends TestCase | |||||
{ | |||||
public ZipEntryTest( final String name ) | |||||
{ | |||||
super( name ); | |||||
} | |||||
/** | |||||
* test handling of extra fields | |||||
*/ | |||||
public void testExtraFields() | |||||
{ | |||||
final AsiExtraField field = createField(); | |||||
final UnrecognizedExtraField extraField = createExtraField(); | |||||
final ZipEntry entry = new ZipEntry( "test/" ); | |||||
entry.setExtraFields( new ZipExtraField[]{field, extraField} ); | |||||
final byte[] data1 = entry.getExtra(); | |||||
ZipExtraField[] result = entry.getExtraFields(); | |||||
assertEquals( "first pass", 2, result.length ); | |||||
assertSame( field, result[ 0 ] ); | |||||
assertSame( extraField, result[ 1 ] ); | |||||
UnrecognizedExtraField u2 = new UnrecognizedExtraField(); | |||||
u2.setHeaderId( new ZipShort( 1 ) ); | |||||
u2.setLocalFileDataData( new byte[]{1} ); | |||||
entry.addExtraField( u2 ); | |||||
byte[] data2 = entry.getExtra(); | |||||
result = entry.getExtraFields(); | |||||
assertEquals( "second pass", 2, result.length ); | |||||
assertSame( field, result[ 0 ] ); | |||||
assertSame( u2, result[ 1 ] ); | |||||
assertEquals( "length second pass", data1.length + 1, data2.length ); | |||||
UnrecognizedExtraField u3 = new UnrecognizedExtraField(); | |||||
u3.setHeaderId( new ZipShort( 2 ) ); | |||||
u3.setLocalFileDataData( new byte[]{1} ); | |||||
entry.addExtraField( u3 ); | |||||
result = entry.getExtraFields(); | |||||
assertEquals( "third pass", 3, result.length ); | |||||
entry.removeExtraField( new ZipShort( 1 ) ); | |||||
byte[] data3 = entry.getExtra(); | |||||
result = entry.getExtraFields(); | |||||
assertEquals( "fourth pass", 2, result.length ); | |||||
assertSame( field, result[ 0 ] ); | |||||
assertSame( u3, result[ 1 ] ); | |||||
assertEquals( "length fourth pass", data2.length, data3.length ); | |||||
try | |||||
{ | |||||
entry.removeExtraField( new ZipShort( 1 ) ); | |||||
fail( "should be no such element" ); | |||||
} | |||||
catch( final NoSuchElementException nse ) | |||||
{ | |||||
} | |||||
} | |||||
private UnrecognizedExtraField createExtraField() | |||||
{ | |||||
UnrecognizedExtraField extraField = new UnrecognizedExtraField(); | |||||
extraField.setHeaderId( new ZipShort( 1 ) ); | |||||
extraField.setLocalFileDataData( new byte[ 0 ] ); | |||||
return extraField; | |||||
} | |||||
private AsiExtraField createField() | |||||
{ | |||||
final AsiExtraField field = new AsiExtraField(); | |||||
field.setDirectory( true ); | |||||
field.setMode( 0755 ); | |||||
return field; | |||||
} | |||||
} |
@@ -0,0 +1,79 @@ | |||||
/* | |||||
* Copyright (C) The Apache Software Foundation. All rights reserved. | |||||
* | |||||
* This software is published under the terms of the Apache Software License | |||||
* version 1.1, a copy of which has been included with this distribution in | |||||
* the LICENSE.txt file. | |||||
*/ | |||||
package org.apache.aut.zip; | |||||
import junit.framework.TestCase; | |||||
/** | |||||
* JUnit 3 testcases for org.apache.tools.zip.ZipLong. | |||||
* | |||||
* @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a> | |||||
*/ | |||||
public class ZipLongTest | |||||
extends TestCase | |||||
{ | |||||
public ZipLongTest( final String name ) | |||||
{ | |||||
super( name ); | |||||
} | |||||
/** | |||||
* Test conversion to bytes. | |||||
*/ | |||||
public void testToBytes() | |||||
{ | |||||
final ZipLong zipLong = new ZipLong( 0x12345678 ); | |||||
final byte[] result = zipLong.getBytes(); | |||||
assertEquals( "length getBytes", 4, result.length ); | |||||
assertEquals( "first byte getBytes", 0x78, result[ 0 ] ); | |||||
assertEquals( "second byte getBytes", 0x56, result[ 1 ] ); | |||||
assertEquals( "third byte getBytes", 0x34, result[ 2 ] ); | |||||
assertEquals( "fourth byte getBytes", 0x12, result[ 3 ] ); | |||||
} | |||||
/** | |||||
* Test conversion from bytes. | |||||
*/ | |||||
public void testFromBytes() | |||||
{ | |||||
final byte[] value = new byte[]{0x78, 0x56, 0x34, 0x12}; | |||||
final ZipLong zipLong = new ZipLong( value ); | |||||
assertEquals( "value from bytes", 0x12345678, zipLong.getValue() ); | |||||
} | |||||
/** | |||||
* Test the contract of the equals method. | |||||
*/ | |||||
public void testEquals() | |||||
{ | |||||
final ZipLong zipLong1 = new ZipLong( 0x12345678 ); | |||||
final ZipLong zipLong2 = new ZipLong( 0x12345678 ); | |||||
final ZipLong zipLong3 = new ZipLong( 0x87654321 ); | |||||
assertTrue( "reflexive", zipLong1.equals( zipLong1 ) ); | |||||
assertTrue( "works", zipLong1.equals( zipLong2 ) ); | |||||
assertTrue( "works, part two", !zipLong1.equals( zipLong3 ) ); | |||||
assertTrue( "symmetric", zipLong2.equals( zipLong1 ) ); | |||||
assertTrue( "null handling", !zipLong1.equals( null ) ); | |||||
assertTrue( "non ZipLong handling", !zipLong1.equals( new Integer( 0x1234 ) ) ); | |||||
} | |||||
/** | |||||
* Test sign handling. | |||||
*/ | |||||
public void testSign() | |||||
{ | |||||
final ZipLong zipLong = | |||||
new ZipLong( new byte[]{(byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF} ); | |||||
assertEquals( 0x00000000FFFFFFFFl, zipLong.getValue() ); | |||||
} | |||||
} |
@@ -0,0 +1,75 @@ | |||||
/* | |||||
* Copyright (C) The Apache Software Foundation. All rights reserved. | |||||
* | |||||
* This software is published under the terms of the Apache Software License | |||||
* version 1.1, a copy of which has been included with this distribution in | |||||
* the LICENSE.txt file. | |||||
*/ | |||||
package org.apache.aut.zip; | |||||
import junit.framework.TestCase; | |||||
/** | |||||
* JUnit 3 testcases for org.apache.tools.zip.ZipShort. | |||||
* | |||||
* @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a> | |||||
*/ | |||||
public class ZipShortTest | |||||
extends TestCase | |||||
{ | |||||
public ZipShortTest( String name ) | |||||
{ | |||||
super( name ); | |||||
} | |||||
/** | |||||
* Test conversion to bytes. | |||||
*/ | |||||
public void testToBytes() | |||||
{ | |||||
final ZipShort zipShort = new ZipShort( 0x1234 ); | |||||
byte[] result = zipShort.getBytes(); | |||||
assertEquals( "length getBytes", 2, result.length ); | |||||
assertEquals( "first byte getBytes", 0x34, result[ 0 ] ); | |||||
assertEquals( "second byte getBytes", 0x12, result[ 1 ] ); | |||||
} | |||||
/** | |||||
* Test conversion from bytes. | |||||
*/ | |||||
public void testFromBytes() | |||||
{ | |||||
byte[] val = new byte[]{0x34, 0x12}; | |||||
final ZipShort zipShort = new ZipShort( val ); | |||||
assertEquals( "value from bytes", 0x1234, zipShort.getValue() ); | |||||
} | |||||
/** | |||||
* Test the contract of the equals method. | |||||
*/ | |||||
public void testEquals() | |||||
{ | |||||
final ZipShort zipShort = new ZipShort( 0x1234 ); | |||||
final ZipShort zipShort2 = new ZipShort( 0x1234 ); | |||||
final ZipShort zipShort3 = new ZipShort( 0x5678 ); | |||||
assertTrue( "reflexive", zipShort.equals( zipShort ) ); | |||||
assertTrue( "works", zipShort.equals( zipShort2 ) ); | |||||
assertTrue( "works, part two", !zipShort.equals( zipShort3 ) ); | |||||
assertTrue( "symmetric", zipShort2.equals( zipShort ) ); | |||||
assertTrue( "null handling", !zipShort.equals( null ) ); | |||||
assertTrue( "non ZipShort handling", !zipShort.equals( new Integer( 0x1234 ) ) ); | |||||
} | |||||
/** | |||||
* Test sign handling. | |||||
*/ | |||||
public void testSign() | |||||
{ | |||||
final ZipShort zipShort = new ZipShort( new byte[]{(byte)0xFF, (byte)0xFF} ); | |||||
assertEquals( 0x0000FFFF, zipShort.getValue() ); | |||||
} | |||||
} |