http://git-wip-us.apache.org/repos/asf/ant/blob/1ae68097/src/tests/junit/org/apache/tools/tar/TarEntryTest.java ---------------------------------------------------------------------- diff --git a/src/tests/junit/org/apache/tools/tar/TarEntryTest.java b/src/tests/junit/org/apache/tools/tar/TarEntryTest.java index e942cdb..4c14943 100644 --- a/src/tests/junit/org/apache/tools/tar/TarEntryTest.java +++ b/src/tests/junit/org/apache/tools/tar/TarEntryTest.java @@ -1,35 +1,35 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -package org.apache.tools.tar; - -import org.junit.Test; - -/** - * @since Ant 1.6 - */ -public class TarEntryTest { - - /** - * demonstrates bug 18105 on OSes with os.name shorter than 7. - */ - @Test - public void testFileConstructor() { - new TarEntry(new java.io.File("/foo")); - } -} +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.tools.tar; + +import org.junit.Test; + +/** + * @since Ant 1.6 + */ +public class TarEntryTest { + + /** + * demonstrates bug 18105 on OSes with os.name shorter than 7. + */ + @Test + public void testFileConstructor() { + new TarEntry(new java.io.File("/foo")); + } +}
http://git-wip-us.apache.org/repos/asf/ant/blob/1ae68097/src/tests/junit/org/apache/tools/tar/TarOutputStreamTest.java ---------------------------------------------------------------------- diff --git a/src/tests/junit/org/apache/tools/tar/TarOutputStreamTest.java b/src/tests/junit/org/apache/tools/tar/TarOutputStreamTest.java index cb65e2d..fd43c35 100644 --- a/src/tests/junit/org/apache/tools/tar/TarOutputStreamTest.java +++ b/src/tests/junit/org/apache/tools/tar/TarOutputStreamTest.java @@ -1,35 +1,35 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -package org.apache.tools.tar; - -import org.junit.Test; - -import java.io.ByteArrayOutputStream; -import java.io.IOException; - -public class TarOutputStreamTest { - - @Test - public void testClose() throws IOException { - ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); - TarOutputStream stream = new TarOutputStream(byteStream); - stream.close(); - stream.close(); - } -} +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.tools.tar; + +import org.junit.Test; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; + +public class TarOutputStreamTest { + + @Test + public void testClose() throws IOException { + ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); + TarOutputStream stream = new TarOutputStream(byteStream); + stream.close(); + stream.close(); + } +} http://git-wip-us.apache.org/repos/asf/ant/blob/1ae68097/src/tests/junit/org/apache/tools/tar/TarRoundTripTest.java ---------------------------------------------------------------------- diff --git a/src/tests/junit/org/apache/tools/tar/TarRoundTripTest.java b/src/tests/junit/org/apache/tools/tar/TarRoundTripTest.java index 98e6378..64b79c8 100644 --- a/src/tests/junit/org/apache/tools/tar/TarRoundTripTest.java +++ b/src/tests/junit/org/apache/tools/tar/TarRoundTripTest.java @@ -1,74 +1,74 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ -package org.apache.tools.tar; - -import org.junit.Test; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; - -public class TarRoundTripTest { - - private static final String LONG_NAME - = "this/path/name/contains/more/than/one/hundred/characters/in/order/" - + "to/test/the/GNU/long/file/name/capability/round/tripped"; - - /** - * test round-tripping long (GNU) entries - */ - @Test - public void testLongRoundTrippingGNU() throws IOException { - testLongRoundTripping(TarOutputStream.LONGFILE_GNU); - } - - /** - * test round-tripping long (POSIX) entries - */ - @Test - public void testLongRoundTrippingPOSIX() throws IOException { - testLongRoundTripping(TarOutputStream.LONGFILE_POSIX); - } - - private void testLongRoundTripping(int mode) throws IOException { - TarEntry original = new TarEntry(LONG_NAME); - assertTrue("over 100 chars", LONG_NAME.length() > 100); - assertEquals("original name", LONG_NAME, original.getName()); - - - ByteArrayOutputStream buff = new ByteArrayOutputStream(); - TarOutputStream tos = new TarOutputStream(buff); - tos.setLongFileMode(mode); - tos.putNextEntry(original); - tos.closeEntry(); - tos.close(); - - TarInputStream tis - = new TarInputStream(new ByteArrayInputStream(buff.toByteArray())); - TarEntry tripped = tis.getNextEntry(); - assertEquals("round-tripped name", LONG_NAME, tripped.getName()); - assertNull("no more entries", tis.getNextEntry()); - tis.close(); - } -} - - +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package org.apache.tools.tar; + +import org.junit.Test; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +public class TarRoundTripTest { + + private static final String LONG_NAME + = "this/path/name/contains/more/than/one/hundred/characters/in/order/" + + "to/test/the/GNU/long/file/name/capability/round/tripped"; + + /** + * test round-tripping long (GNU) entries + */ + @Test + public void testLongRoundTrippingGNU() throws IOException { + testLongRoundTripping(TarOutputStream.LONGFILE_GNU); + } + + /** + * test round-tripping long (POSIX) entries + */ + @Test + public void testLongRoundTrippingPOSIX() throws IOException { + testLongRoundTripping(TarOutputStream.LONGFILE_POSIX); + } + + private void testLongRoundTripping(int mode) throws IOException { + TarEntry original = new TarEntry(LONG_NAME); + assertTrue("over 100 chars", LONG_NAME.length() > 100); + assertEquals("original name", LONG_NAME, original.getName()); + + + ByteArrayOutputStream buff = new ByteArrayOutputStream(); + TarOutputStream tos = new TarOutputStream(buff); + tos.setLongFileMode(mode); + tos.putNextEntry(original); + tos.closeEntry(); + tos.close(); + + TarInputStream tis + = new TarInputStream(new ByteArrayInputStream(buff.toByteArray())); + TarEntry tripped = tis.getNextEntry(); + assertEquals("round-tripped name", LONG_NAME, tripped.getName()); + assertNull("no more entries", tis.getNextEntry()); + tis.close(); + } +} + + http://git-wip-us.apache.org/repos/asf/ant/blob/1ae68097/src/tests/junit/org/apache/tools/zip/AsiExtraFieldTest.java ---------------------------------------------------------------------- diff --git a/src/tests/junit/org/apache/tools/zip/AsiExtraFieldTest.java b/src/tests/junit/org/apache/tools/zip/AsiExtraFieldTest.java index 873b3f5..9b06a6a 100644 --- a/src/tests/junit/org/apache/tools/zip/AsiExtraFieldTest.java +++ b/src/tests/junit/org/apache/tools/zip/AsiExtraFieldTest.java @@ -1,163 +1,163 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -package org.apache.tools.zip; - -import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotSame; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - -/** - * JUnit testcases for org.apache.tools.zip.AsiExtraField. - * - */ -public class AsiExtraFieldTest implements UnixStat { - - /** - * Test file mode magic. - */ - @Test - public void testModes() { - AsiExtraField a = new AsiExtraField(); - a.setMode(0123); - assertEquals("plain file", 0100123, a.getMode()); - a.setDirectory(true); - assertEquals("directory", 040123, a.getMode()); - a.setLinkedFile("test"); - assertEquals("symbolic link", 0120123, a.getMode()); - } - - /** - * Test content. - */ - @Test - public void testContent() { - AsiExtraField a = new AsiExtraField(); - a.setMode(0123); - a.setUserId(5); - a.setGroupId(6); - byte[] b = a.getLocalFileDataData(); - - // CRC manually calculated, sorry - 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, b.length); - for (int i=0; i<expect.length; i++) { - assertEquals("no link, byte "+i, expect[i], b[i]); - } - - a.setLinkedFile("test"); - 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'}; - b = a.getLocalFileDataData(); - assertEquals("no link", expect.length, b.length); - for (int i=0; i<expect.length; i++) { - assertEquals("no link, byte "+i, expect[i], b[i]); - } - - } - - /** - * Test reparse - */ - @Test - public void testReparse() throws Exception { - // CRC manually calculated, sorry - 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 - AsiExtraField a = new AsiExtraField(); - a.parseFromLocalFileData(data, 0, data.length); - assertEquals("length plain file", data.length, - a.getLocalFileDataLength().getValue()); - assertTrue("plain file, no link", !a.isLink()); - assertTrue("plain file, no dir", !a.isDirectory()); - assertEquals("mode plain file", FILE_FLAG | 0123, a.getMode()); - assertEquals("uid plain file", 5, a.getUserId()); - assertEquals("gid plain file", 6, a.getGroupId()); - - 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'}; - a = new AsiExtraField(); - a.parseFromLocalFileData(data, 0, data.length); - assertEquals("length link", data.length, - a.getLocalFileDataLength().getValue()); - assertTrue("link, is link", a.isLink()); - assertTrue("link, no dir", !a.isDirectory()); - assertEquals("mode link", LINK_FLAG | 0123, a.getMode()); - assertEquals("uid link", 5, a.getUserId()); - assertEquals("gid link", 6, a.getGroupId()); - assertEquals("test", a.getLinkedFile()); - - 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 - a = new AsiExtraField(); - a.parseFromLocalFileData(data, 0, data.length); - assertEquals("length dir", data.length, - a.getLocalFileDataLength().getValue()); - assertTrue("dir, no link", !a.isLink()); - assertTrue("dir, is dir", a.isDirectory()); - assertEquals("mode dir", DIR_FLAG | 0123, a.getMode()); - assertEquals("uid dir", 5, a.getUserId()); - assertEquals("gid dir", 6, a.getGroupId()); - - data = new byte[] {0, 0, 0, 0, // bad CRC - 0123, (byte)0x40, // mode - 0, 0, 0, 0, // link - 5, 0, 6, 0}; // uid, gid - a = new AsiExtraField(); - try { - a.parseFromLocalFileData(data, 0, data.length); - fail("should raise bad CRC exception"); - } catch (Exception e) { - assertEquals("bad CRC checksum 0 instead of ebf018e", - e.getMessage()); - } - } - - @Test - public void testClone() { - AsiExtraField s1 = new AsiExtraField(); - s1.setUserId(42); - s1.setGroupId(12); - s1.setLinkedFile("foo"); - s1.setMode(0644); - s1.setDirectory(true); - AsiExtraField s2 = (AsiExtraField) s1.clone(); - assertNotSame(s1, s2); - assertEquals(s1.getUserId(), s2.getUserId()); - assertEquals(s1.getGroupId(), s2.getGroupId()); - assertEquals(s1.getLinkedFile(), s2.getLinkedFile()); - assertEquals(s1.getMode(), s2.getMode()); - assertEquals(s1.isDirectory(), s2.isDirectory()); - } -} +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.tools.zip; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotSame; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +/** + * JUnit testcases for org.apache.tools.zip.AsiExtraField. + * + */ +public class AsiExtraFieldTest implements UnixStat { + + /** + * Test file mode magic. + */ + @Test + public void testModes() { + AsiExtraField a = new AsiExtraField(); + a.setMode(0123); + assertEquals("plain file", 0100123, a.getMode()); + a.setDirectory(true); + assertEquals("directory", 040123, a.getMode()); + a.setLinkedFile("test"); + assertEquals("symbolic link", 0120123, a.getMode()); + } + + /** + * Test content. + */ + @Test + public void testContent() { + AsiExtraField a = new AsiExtraField(); + a.setMode(0123); + a.setUserId(5); + a.setGroupId(6); + byte[] b = a.getLocalFileDataData(); + + // CRC manually calculated, sorry + 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, b.length); + for (int i=0; i<expect.length; i++) { + assertEquals("no link, byte "+i, expect[i], b[i]); + } + + a.setLinkedFile("test"); + 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'}; + b = a.getLocalFileDataData(); + assertEquals("no link", expect.length, b.length); + for (int i=0; i<expect.length; i++) { + assertEquals("no link, byte "+i, expect[i], b[i]); + } + + } + + /** + * Test reparse + */ + @Test + public void testReparse() throws Exception { + // CRC manually calculated, sorry + 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 + AsiExtraField a = new AsiExtraField(); + a.parseFromLocalFileData(data, 0, data.length); + assertEquals("length plain file", data.length, + a.getLocalFileDataLength().getValue()); + assertTrue("plain file, no link", !a.isLink()); + assertTrue("plain file, no dir", !a.isDirectory()); + assertEquals("mode plain file", FILE_FLAG | 0123, a.getMode()); + assertEquals("uid plain file", 5, a.getUserId()); + assertEquals("gid plain file", 6, a.getGroupId()); + + 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'}; + a = new AsiExtraField(); + a.parseFromLocalFileData(data, 0, data.length); + assertEquals("length link", data.length, + a.getLocalFileDataLength().getValue()); + assertTrue("link, is link", a.isLink()); + assertTrue("link, no dir", !a.isDirectory()); + assertEquals("mode link", LINK_FLAG | 0123, a.getMode()); + assertEquals("uid link", 5, a.getUserId()); + assertEquals("gid link", 6, a.getGroupId()); + assertEquals("test", a.getLinkedFile()); + + 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 + a = new AsiExtraField(); + a.parseFromLocalFileData(data, 0, data.length); + assertEquals("length dir", data.length, + a.getLocalFileDataLength().getValue()); + assertTrue("dir, no link", !a.isLink()); + assertTrue("dir, is dir", a.isDirectory()); + assertEquals("mode dir", DIR_FLAG | 0123, a.getMode()); + assertEquals("uid dir", 5, a.getUserId()); + assertEquals("gid dir", 6, a.getGroupId()); + + data = new byte[] {0, 0, 0, 0, // bad CRC + 0123, (byte)0x40, // mode + 0, 0, 0, 0, // link + 5, 0, 6, 0}; // uid, gid + a = new AsiExtraField(); + try { + a.parseFromLocalFileData(data, 0, data.length); + fail("should raise bad CRC exception"); + } catch (Exception e) { + assertEquals("bad CRC checksum 0 instead of ebf018e", + e.getMessage()); + } + } + + @Test + public void testClone() { + AsiExtraField s1 = new AsiExtraField(); + s1.setUserId(42); + s1.setGroupId(12); + s1.setLinkedFile("foo"); + s1.setMode(0644); + s1.setDirectory(true); + AsiExtraField s2 = (AsiExtraField) s1.clone(); + assertNotSame(s1, s2); + assertEquals(s1.getUserId(), s2.getUserId()); + assertEquals(s1.getGroupId(), s2.getGroupId()); + assertEquals(s1.getLinkedFile(), s2.getLinkedFile()); + assertEquals(s1.getMode(), s2.getMode()); + assertEquals(s1.isDirectory(), s2.isDirectory()); + } +} http://git-wip-us.apache.org/repos/asf/ant/blob/1ae68097/src/tests/junit/org/apache/tools/zip/ExtraFieldUtilsTest.java ---------------------------------------------------------------------- diff --git a/src/tests/junit/org/apache/tools/zip/ExtraFieldUtilsTest.java b/src/tests/junit/org/apache/tools/zip/ExtraFieldUtilsTest.java index 95a6779..3d82698 100644 --- a/src/tests/junit/org/apache/tools/zip/ExtraFieldUtilsTest.java +++ b/src/tests/junit/org/apache/tools/zip/ExtraFieldUtilsTest.java @@ -1,210 +1,210 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -package org.apache.tools.zip; - -import org.junit.Before; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - -/** - * JUnit 3 testcases for org.apache.tools.zip.ExtraFieldUtils. - * - */ -public class ExtraFieldUtilsTest implements UnixStat { - - /** - * Header-ID of a ZipExtraField not supported by Ant. - * - * <p>Used to be ZipShort(1) but this is the ID of the Zip64 extra - * field.</p> - */ - static final ZipShort UNRECOGNIZED_HEADER = new ZipShort(0x5555); - - private AsiExtraField a; - private UnrecognizedExtraField dummy; - private byte[] data; - private byte[] aLocal; - - @Before - public void setUp() { - a = new AsiExtraField(); - a.setMode(0755); - a.setDirectory(true); - dummy = new UnrecognizedExtraField(); - dummy.setHeaderId(UNRECOGNIZED_HEADER); - dummy.setLocalFileDataData(new byte[] {0}); - dummy.setCentralDirectoryData(new byte[] {0}); - - aLocal = a.getLocalFileDataData(); - byte[] dummyLocal = dummy.getLocalFileDataData(); - data = new byte[4 + aLocal.length + 4 + dummyLocal.length]; - System.arraycopy(a.getHeaderId().getBytes(), 0, data, 0, 2); - System.arraycopy(a.getLocalFileDataLength().getBytes(), 0, data, 2, 2); - System.arraycopy(aLocal, 0, data, 4, aLocal.length); - System.arraycopy(dummy.getHeaderId().getBytes(), 0, data, - 4+aLocal.length, 2); - System.arraycopy(dummy.getLocalFileDataLength().getBytes(), 0, data, - 4+aLocal.length+2, 2); - System.arraycopy(dummyLocal, 0, data, - 4+aLocal.length+4, dummyLocal.length); - - } - - /** - * test parser. - */ - @Test - public void testParse() throws Exception { - ZipExtraField[] ze = ExtraFieldUtils.parse(data); - assertEquals("number of fields", 2, ze.length); - assertTrue("type field 1", ze[0] instanceof AsiExtraField); - assertEquals("mode field 1", 040755, - ((AsiExtraField) ze[0]).getMode()); - assertTrue("type field 2", ze[1] instanceof UnrecognizedExtraField); - assertEquals("data length field 2", 1, - ze[1].getLocalFileDataLength().getValue()); - - byte[] data2 = new byte[data.length-1]; - System.arraycopy(data, 0, data2, 0, data2.length); - try { - ExtraFieldUtils.parse(data2); - fail("data should be invalid"); - } catch (Exception e) { - assertEquals("message", - "bad extra field starting at "+(4 + aLocal.length) - + ". Block length of 1 bytes exceeds remaining data of 0 bytes.", - e.getMessage()); - } - } - - @Test - public void testParseWithRead() throws Exception { - ZipExtraField[] ze = - ExtraFieldUtils.parse(data, true, - ExtraFieldUtils.UnparseableExtraField.READ); - assertEquals("number of fields", 2, ze.length); - assertTrue("type field 1", ze[0] instanceof AsiExtraField); - assertEquals("mode field 1", 040755, - ((AsiExtraField) ze[0]).getMode()); - assertTrue("type field 2", ze[1] instanceof UnrecognizedExtraField); - assertEquals("data length field 2", 1, - ze[1].getLocalFileDataLength().getValue()); - - byte[] data2 = new byte[data.length-1]; - System.arraycopy(data, 0, data2, 0, data2.length); - ze = ExtraFieldUtils.parse(data2, true, - ExtraFieldUtils.UnparseableExtraField.READ); - assertEquals("number of fields", 2, ze.length); - assertTrue("type field 1", ze[0] instanceof AsiExtraField); - assertEquals("mode field 1", 040755, - ((AsiExtraField) ze[0]).getMode()); - assertTrue("type field 2", ze[1] instanceof UnparseableExtraFieldData); - assertEquals("data length field 2", 4, - ze[1].getLocalFileDataLength().getValue()); - for (int i = 0; i < 4; i++) { - assertEquals("byte number " + i, - data2[data.length - 5 + i], - ze[1].getLocalFileDataData()[i]); - } - } - - @Test - public void testParseWithSkip() throws Exception { - ZipExtraField[] ze = - ExtraFieldUtils.parse(data, true, - ExtraFieldUtils.UnparseableExtraField.SKIP); - assertEquals("number of fields", 2, ze.length); - assertTrue("type field 1", ze[0] instanceof AsiExtraField); - assertEquals("mode field 1", 040755, - ((AsiExtraField) ze[0]).getMode()); - assertTrue("type field 2", ze[1] instanceof UnrecognizedExtraField); - assertEquals("data length field 2", 1, - ze[1].getLocalFileDataLength().getValue()); - - byte[] data2 = new byte[data.length-1]; - System.arraycopy(data, 0, data2, 0, data2.length); - ze = ExtraFieldUtils.parse(data2, true, - ExtraFieldUtils.UnparseableExtraField.SKIP); - assertEquals("number of fields", 1, ze.length); - assertTrue("type field 1", ze[0] instanceof AsiExtraField); - assertEquals("mode field 1", 040755, - ((AsiExtraField) ze[0]).getMode()); - } - - /** - * Test merge methods - */ - @Test - public void testMerge() { - byte[] local = - ExtraFieldUtils.mergeLocalFileDataData(new ZipExtraField[] {a, dummy}); - assertEquals("local length", data.length, local.length); - for (int i=0; i<local.length; i++) { - assertEquals("local byte "+i, data[i], local[i]); - } - - byte[] dummyCentral = dummy.getCentralDirectoryData(); - byte[] data2 = new byte[4 + aLocal.length + 4 + dummyCentral.length]; - System.arraycopy(data, 0, data2, 0, 4 + aLocal.length + 2); - System.arraycopy(dummy.getCentralDirectoryLength().getBytes(), 0, - data2, 4+aLocal.length+2, 2); - System.arraycopy(dummyCentral, 0, data2, - 4+aLocal.length+4, dummyCentral.length); - - - byte[] central = - ExtraFieldUtils.mergeCentralDirectoryData(new ZipExtraField[] {a, dummy}); - assertEquals("central length", data2.length, central.length); - for (int i=0; i<central.length; i++) { - assertEquals("central byte "+i, data2[i], central[i]); - } - - } - - @Test - public void testMergeWithUnparseableData() throws Exception { - ZipExtraField d = new UnparseableExtraFieldData(); - byte[] b = UNRECOGNIZED_HEADER.getBytes(); - d.parseFromLocalFileData(new byte[] {b[0], b[1], 1, 0}, 0, 4); - byte[] local = - ExtraFieldUtils.mergeLocalFileDataData(new ZipExtraField[] {a, d}); - assertEquals("local length", data.length - 1, local.length); - for (int i = 0; i < local.length; i++) { - assertEquals("local byte " + i, data[i], local[i]); - } - - byte[] dCentral = d.getCentralDirectoryData(); - byte[] data2 = new byte[4 + aLocal.length + dCentral.length]; - System.arraycopy(data, 0, data2, 0, 4 + aLocal.length + 2); - System.arraycopy(dCentral, 0, data2, - 4 + aLocal.length, dCentral.length); - - - byte[] central = - ExtraFieldUtils.mergeCentralDirectoryData(new ZipExtraField[] {a, d}); - assertEquals("central length", data2.length, central.length); - for (int i = 0; i < central.length; i++) { - assertEquals("central byte " + i, data2[i], central[i]); - } - - } -} +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.tools.zip; + +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +/** + * JUnit 3 testcases for org.apache.tools.zip.ExtraFieldUtils. + * + */ +public class ExtraFieldUtilsTest implements UnixStat { + + /** + * Header-ID of a ZipExtraField not supported by Ant. + * + * <p>Used to be ZipShort(1) but this is the ID of the Zip64 extra + * field.</p> + */ + static final ZipShort UNRECOGNIZED_HEADER = new ZipShort(0x5555); + + private AsiExtraField a; + private UnrecognizedExtraField dummy; + private byte[] data; + private byte[] aLocal; + + @Before + public void setUp() { + a = new AsiExtraField(); + a.setMode(0755); + a.setDirectory(true); + dummy = new UnrecognizedExtraField(); + dummy.setHeaderId(UNRECOGNIZED_HEADER); + dummy.setLocalFileDataData(new byte[] {0}); + dummy.setCentralDirectoryData(new byte[] {0}); + + aLocal = a.getLocalFileDataData(); + byte[] dummyLocal = dummy.getLocalFileDataData(); + data = new byte[4 + aLocal.length + 4 + dummyLocal.length]; + System.arraycopy(a.getHeaderId().getBytes(), 0, data, 0, 2); + System.arraycopy(a.getLocalFileDataLength().getBytes(), 0, data, 2, 2); + System.arraycopy(aLocal, 0, data, 4, aLocal.length); + System.arraycopy(dummy.getHeaderId().getBytes(), 0, data, + 4+aLocal.length, 2); + System.arraycopy(dummy.getLocalFileDataLength().getBytes(), 0, data, + 4+aLocal.length+2, 2); + System.arraycopy(dummyLocal, 0, data, + 4+aLocal.length+4, dummyLocal.length); + + } + + /** + * test parser. + */ + @Test + public void testParse() throws Exception { + ZipExtraField[] ze = ExtraFieldUtils.parse(data); + assertEquals("number of fields", 2, ze.length); + assertTrue("type field 1", ze[0] instanceof AsiExtraField); + assertEquals("mode field 1", 040755, + ((AsiExtraField) ze[0]).getMode()); + assertTrue("type field 2", ze[1] instanceof UnrecognizedExtraField); + assertEquals("data length field 2", 1, + ze[1].getLocalFileDataLength().getValue()); + + byte[] data2 = new byte[data.length-1]; + System.arraycopy(data, 0, data2, 0, data2.length); + try { + ExtraFieldUtils.parse(data2); + fail("data should be invalid"); + } catch (Exception e) { + assertEquals("message", + "bad extra field starting at "+(4 + aLocal.length) + + ". Block length of 1 bytes exceeds remaining data of 0 bytes.", + e.getMessage()); + } + } + + @Test + public void testParseWithRead() throws Exception { + ZipExtraField[] ze = + ExtraFieldUtils.parse(data, true, + ExtraFieldUtils.UnparseableExtraField.READ); + assertEquals("number of fields", 2, ze.length); + assertTrue("type field 1", ze[0] instanceof AsiExtraField); + assertEquals("mode field 1", 040755, + ((AsiExtraField) ze[0]).getMode()); + assertTrue("type field 2", ze[1] instanceof UnrecognizedExtraField); + assertEquals("data length field 2", 1, + ze[1].getLocalFileDataLength().getValue()); + + byte[] data2 = new byte[data.length-1]; + System.arraycopy(data, 0, data2, 0, data2.length); + ze = ExtraFieldUtils.parse(data2, true, + ExtraFieldUtils.UnparseableExtraField.READ); + assertEquals("number of fields", 2, ze.length); + assertTrue("type field 1", ze[0] instanceof AsiExtraField); + assertEquals("mode field 1", 040755, + ((AsiExtraField) ze[0]).getMode()); + assertTrue("type field 2", ze[1] instanceof UnparseableExtraFieldData); + assertEquals("data length field 2", 4, + ze[1].getLocalFileDataLength().getValue()); + for (int i = 0; i < 4; i++) { + assertEquals("byte number " + i, + data2[data.length - 5 + i], + ze[1].getLocalFileDataData()[i]); + } + } + + @Test + public void testParseWithSkip() throws Exception { + ZipExtraField[] ze = + ExtraFieldUtils.parse(data, true, + ExtraFieldUtils.UnparseableExtraField.SKIP); + assertEquals("number of fields", 2, ze.length); + assertTrue("type field 1", ze[0] instanceof AsiExtraField); + assertEquals("mode field 1", 040755, + ((AsiExtraField) ze[0]).getMode()); + assertTrue("type field 2", ze[1] instanceof UnrecognizedExtraField); + assertEquals("data length field 2", 1, + ze[1].getLocalFileDataLength().getValue()); + + byte[] data2 = new byte[data.length-1]; + System.arraycopy(data, 0, data2, 0, data2.length); + ze = ExtraFieldUtils.parse(data2, true, + ExtraFieldUtils.UnparseableExtraField.SKIP); + assertEquals("number of fields", 1, ze.length); + assertTrue("type field 1", ze[0] instanceof AsiExtraField); + assertEquals("mode field 1", 040755, + ((AsiExtraField) ze[0]).getMode()); + } + + /** + * Test merge methods + */ + @Test + public void testMerge() { + byte[] local = + ExtraFieldUtils.mergeLocalFileDataData(new ZipExtraField[] {a, dummy}); + assertEquals("local length", data.length, local.length); + for (int i=0; i<local.length; i++) { + assertEquals("local byte "+i, data[i], local[i]); + } + + byte[] dummyCentral = dummy.getCentralDirectoryData(); + byte[] data2 = new byte[4 + aLocal.length + 4 + dummyCentral.length]; + System.arraycopy(data, 0, data2, 0, 4 + aLocal.length + 2); + System.arraycopy(dummy.getCentralDirectoryLength().getBytes(), 0, + data2, 4+aLocal.length+2, 2); + System.arraycopy(dummyCentral, 0, data2, + 4+aLocal.length+4, dummyCentral.length); + + + byte[] central = + ExtraFieldUtils.mergeCentralDirectoryData(new ZipExtraField[] {a, dummy}); + assertEquals("central length", data2.length, central.length); + for (int i=0; i<central.length; i++) { + assertEquals("central byte "+i, data2[i], central[i]); + } + + } + + @Test + public void testMergeWithUnparseableData() throws Exception { + ZipExtraField d = new UnparseableExtraFieldData(); + byte[] b = UNRECOGNIZED_HEADER.getBytes(); + d.parseFromLocalFileData(new byte[] {b[0], b[1], 1, 0}, 0, 4); + byte[] local = + ExtraFieldUtils.mergeLocalFileDataData(new ZipExtraField[] {a, d}); + assertEquals("local length", data.length - 1, local.length); + for (int i = 0; i < local.length; i++) { + assertEquals("local byte " + i, data[i], local[i]); + } + + byte[] dCentral = d.getCentralDirectoryData(); + byte[] data2 = new byte[4 + aLocal.length + dCentral.length]; + System.arraycopy(data, 0, data2, 0, 4 + aLocal.length + 2); + System.arraycopy(dCentral, 0, data2, + 4 + aLocal.length, dCentral.length); + + + byte[] central = + ExtraFieldUtils.mergeCentralDirectoryData(new ZipExtraField[] {a, d}); + assertEquals("central length", data2.length, central.length); + for (int i = 0; i < central.length; i++) { + assertEquals("central byte " + i, data2[i], central[i]); + } + + } +} http://git-wip-us.apache.org/repos/asf/ant/blob/1ae68097/src/tests/junit/org/apache/tools/zip/UTF8ZipFilesTest.java ---------------------------------------------------------------------- diff --git a/src/tests/junit/org/apache/tools/zip/UTF8ZipFilesTest.java b/src/tests/junit/org/apache/tools/zip/UTF8ZipFilesTest.java index 38b2df5..564bdaf 100644 --- a/src/tests/junit/org/apache/tools/zip/UTF8ZipFilesTest.java +++ b/src/tests/junit/org/apache/tools/zip/UTF8ZipFilesTest.java @@ -1,260 +1,260 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -package org.apache.tools.zip; - -import java.io.File; -import java.io.InputStream; -import java.io.IOException; -import java.nio.ByteBuffer; -import java.util.Enumeration; -import java.util.zip.CRC32; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; - -public class UTF8ZipFilesTest { - - private static final String UTF_8 = "utf-8"; - private static final String CP437 = "cp437"; - private static final String US_ASCII = "US-ASCII"; - private static final String ASCII_TXT = "ascii.txt"; - private static final String EURO_FOR_DOLLAR_TXT = "\u20AC_for_Dollar.txt"; - private static final String OIL_BARREL_TXT = "\u00D6lf\u00E4sser.txt"; - - @Test - public void testUtf8FileRoundtripExplicitUnicodeExtra() - throws IOException { - testFileRoundtrip(UTF_8, true, true); - } - - @Test - public void testUtf8FileRoundtripNoEFSExplicitUnicodeExtra() - throws IOException { - testFileRoundtrip(UTF_8, false, true); - } - - @Test - public void testCP437FileRoundtripExplicitUnicodeExtra() - throws IOException { - testFileRoundtrip(CP437, false, true); - } - - @Test - public void testASCIIFileRoundtripExplicitUnicodeExtra() - throws IOException { - testFileRoundtrip(US_ASCII, false, true); - } - - @Test - public void testUtf8FileRoundtripImplicitUnicodeExtra() - throws IOException { - testFileRoundtrip(UTF_8, true, false); - } - - @Test - public void testUtf8FileRoundtripNoEFSImplicitUnicodeExtra() - throws IOException { - testFileRoundtrip(UTF_8, false, false); - } - - @Test - public void testCP437FileRoundtripImplicitUnicodeExtra() - throws IOException { - testFileRoundtrip(CP437, false, false); - } - - @Test - public void testASCIIFileRoundtripImplicitUnicodeExtra() - throws IOException { - testFileRoundtrip(US_ASCII, false, false); - } - - @Test - public void testZipFileReadsUnicodeFields() throws IOException { - File file = File.createTempFile("unicode-test", ".zip"); - ZipFile zf = null; - try { - createTestFile(file, US_ASCII, false, true); - zf = new ZipFile(file, US_ASCII, true); - assertCanRead(zf, ASCII_TXT); - assertCanRead(zf, EURO_FOR_DOLLAR_TXT); - assertCanRead(zf, OIL_BARREL_TXT); - } finally { - ZipFile.closeQuietly(zf); - if (file.exists()) { - file.delete(); - } - } - } - - private static void testFileRoundtrip(String encoding, boolean withEFS, - boolean withExplicitUnicodeExtra) - throws IOException { - - File file = File.createTempFile(encoding + "-test", ".zip"); - try { - createTestFile(file, encoding, withEFS, withExplicitUnicodeExtra); - testFile(file, encoding); - } finally { - if (file.exists()) { - file.delete(); - } - } - } - - private static void createTestFile(File file, String encoding, - boolean withEFS, - boolean withExplicitUnicodeExtra) - throws IOException { - - ZipEncoding zipEncoding = ZipEncodingHelper.getZipEncoding(encoding); - - ZipOutputStream zos = null; - try { - zos = new ZipOutputStream(file); - zos.setEncoding(encoding); - zos.setUseLanguageEncodingFlag(withEFS); - zos.setCreateUnicodeExtraFields(withExplicitUnicodeExtra ? - ZipOutputStream - .UnicodeExtraFieldPolicy.NEVER - : ZipOutputStream - .UnicodeExtraFieldPolicy.ALWAYS); - - ZipEntry ze = new ZipEntry(OIL_BARREL_TXT); - if (withExplicitUnicodeExtra - && !zipEncoding.canEncode(ze.getName())) { - - ByteBuffer en = zipEncoding.encode(ze.getName()); - - ze.addExtraField(new UnicodePathExtraField(ze.getName(), - en.array(), - en.arrayOffset(), - en.limit())); - } - - zos.putNextEntry(ze); - zos.write("Hello, world!".getBytes("US-ASCII")); - zos.closeEntry(); - - ze = new ZipEntry(EURO_FOR_DOLLAR_TXT); - if (withExplicitUnicodeExtra - && !zipEncoding.canEncode(ze.getName())) { - - ByteBuffer en = zipEncoding.encode(ze.getName()); - - ze.addExtraField(new UnicodePathExtraField(ze.getName(), - en.array(), - en.arrayOffset(), - en.limit())); - } - - zos.putNextEntry(ze); - zos.write("Give me your money!".getBytes("US-ASCII")); - zos.closeEntry(); - - ze = new ZipEntry(ASCII_TXT); - - if (withExplicitUnicodeExtra - && !zipEncoding.canEncode(ze.getName())) { - - ByteBuffer en = zipEncoding.encode(ze.getName()); - - ze.addExtraField(new UnicodePathExtraField(ze.getName(), - en.array(), - en.arrayOffset(), - en.limit())); - } - - zos.putNextEntry(ze); - zos.write("ascii".getBytes("US-ASCII")); - zos.closeEntry(); - } finally { - if (zos != null) { - try { - zos.close(); - } catch (IOException e) { /* swallow */ } - } - } - } - - private static void testFile(File file, String encoding) - throws IOException { - ZipFile zf = null; - try { - zf = new ZipFile(file, encoding, false); - - Enumeration e = zf.getEntries(); - while (e.hasMoreElements()) { - ZipEntry ze = (ZipEntry) e.nextElement(); - - if (ze.getName().endsWith("sser.txt")) { - assertUnicodeName(ze, OIL_BARREL_TXT, encoding); - - } else if (ze.getName().endsWith("_for_Dollar.txt")) { - assertUnicodeName(ze, EURO_FOR_DOLLAR_TXT, encoding); - } else if (!ze.getName().equals(ASCII_TXT)) { - throw new AssertionError("Urecognized ZIP entry with name [" - + ze.getName() + "] found."); - } - } - } finally { - ZipFile.closeQuietly(zf); - } - } - - private static UnicodePathExtraField findUniCodePath(ZipEntry ze) { - return (UnicodePathExtraField) - ze.getExtraField(UnicodePathExtraField.UPATH_ID); - } - - private static void assertUnicodeName(ZipEntry ze, - String expectedName, - String encoding) - throws IOException { - if (!expectedName.equals(ze.getName())) { - UnicodePathExtraField ucpf = findUniCodePath(ze); - assertNotNull(ucpf); - - ZipEncoding enc = ZipEncodingHelper.getZipEncoding(encoding); - ByteBuffer ne = enc.encode(ze.getName()); - - CRC32 crc = new CRC32(); - crc.update(ne.array(),ne.arrayOffset(),ne.limit()); - - assertEquals(crc.getValue(), ucpf.getNameCRC32()); - assertEquals(expectedName, new String(ucpf.getUnicodeName(), - UTF_8)); - } - } - - private static void assertCanRead(ZipFile zf, String fileName) throws IOException { - ZipEntry entry = zf.getEntry(fileName); - assertNotNull("Entry " + fileName + " doesn't exist", entry); - InputStream is = zf.getInputStream(entry); - assertNotNull("InputStream is null", is); - try { - is.read(); - } finally { - is.close(); - } - } - -} - +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.tools.zip; + +import java.io.File; +import java.io.InputStream; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.Enumeration; +import java.util.zip.CRC32; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +public class UTF8ZipFilesTest { + + private static final String UTF_8 = "utf-8"; + private static final String CP437 = "cp437"; + private static final String US_ASCII = "US-ASCII"; + private static final String ASCII_TXT = "ascii.txt"; + private static final String EURO_FOR_DOLLAR_TXT = "\u20AC_for_Dollar.txt"; + private static final String OIL_BARREL_TXT = "\u00D6lf\u00E4sser.txt"; + + @Test + public void testUtf8FileRoundtripExplicitUnicodeExtra() + throws IOException { + testFileRoundtrip(UTF_8, true, true); + } + + @Test + public void testUtf8FileRoundtripNoEFSExplicitUnicodeExtra() + throws IOException { + testFileRoundtrip(UTF_8, false, true); + } + + @Test + public void testCP437FileRoundtripExplicitUnicodeExtra() + throws IOException { + testFileRoundtrip(CP437, false, true); + } + + @Test + public void testASCIIFileRoundtripExplicitUnicodeExtra() + throws IOException { + testFileRoundtrip(US_ASCII, false, true); + } + + @Test + public void testUtf8FileRoundtripImplicitUnicodeExtra() + throws IOException { + testFileRoundtrip(UTF_8, true, false); + } + + @Test + public void testUtf8FileRoundtripNoEFSImplicitUnicodeExtra() + throws IOException { + testFileRoundtrip(UTF_8, false, false); + } + + @Test + public void testCP437FileRoundtripImplicitUnicodeExtra() + throws IOException { + testFileRoundtrip(CP437, false, false); + } + + @Test + public void testASCIIFileRoundtripImplicitUnicodeExtra() + throws IOException { + testFileRoundtrip(US_ASCII, false, false); + } + + @Test + public void testZipFileReadsUnicodeFields() throws IOException { + File file = File.createTempFile("unicode-test", ".zip"); + ZipFile zf = null; + try { + createTestFile(file, US_ASCII, false, true); + zf = new ZipFile(file, US_ASCII, true); + assertCanRead(zf, ASCII_TXT); + assertCanRead(zf, EURO_FOR_DOLLAR_TXT); + assertCanRead(zf, OIL_BARREL_TXT); + } finally { + ZipFile.closeQuietly(zf); + if (file.exists()) { + file.delete(); + } + } + } + + private static void testFileRoundtrip(String encoding, boolean withEFS, + boolean withExplicitUnicodeExtra) + throws IOException { + + File file = File.createTempFile(encoding + "-test", ".zip"); + try { + createTestFile(file, encoding, withEFS, withExplicitUnicodeExtra); + testFile(file, encoding); + } finally { + if (file.exists()) { + file.delete(); + } + } + } + + private static void createTestFile(File file, String encoding, + boolean withEFS, + boolean withExplicitUnicodeExtra) + throws IOException { + + ZipEncoding zipEncoding = ZipEncodingHelper.getZipEncoding(encoding); + + ZipOutputStream zos = null; + try { + zos = new ZipOutputStream(file); + zos.setEncoding(encoding); + zos.setUseLanguageEncodingFlag(withEFS); + zos.setCreateUnicodeExtraFields(withExplicitUnicodeExtra ? + ZipOutputStream + .UnicodeExtraFieldPolicy.NEVER + : ZipOutputStream + .UnicodeExtraFieldPolicy.ALWAYS); + + ZipEntry ze = new ZipEntry(OIL_BARREL_TXT); + if (withExplicitUnicodeExtra + && !zipEncoding.canEncode(ze.getName())) { + + ByteBuffer en = zipEncoding.encode(ze.getName()); + + ze.addExtraField(new UnicodePathExtraField(ze.getName(), + en.array(), + en.arrayOffset(), + en.limit())); + } + + zos.putNextEntry(ze); + zos.write("Hello, world!".getBytes("US-ASCII")); + zos.closeEntry(); + + ze = new ZipEntry(EURO_FOR_DOLLAR_TXT); + if (withExplicitUnicodeExtra + && !zipEncoding.canEncode(ze.getName())) { + + ByteBuffer en = zipEncoding.encode(ze.getName()); + + ze.addExtraField(new UnicodePathExtraField(ze.getName(), + en.array(), + en.arrayOffset(), + en.limit())); + } + + zos.putNextEntry(ze); + zos.write("Give me your money!".getBytes("US-ASCII")); + zos.closeEntry(); + + ze = new ZipEntry(ASCII_TXT); + + if (withExplicitUnicodeExtra + && !zipEncoding.canEncode(ze.getName())) { + + ByteBuffer en = zipEncoding.encode(ze.getName()); + + ze.addExtraField(new UnicodePathExtraField(ze.getName(), + en.array(), + en.arrayOffset(), + en.limit())); + } + + zos.putNextEntry(ze); + zos.write("ascii".getBytes("US-ASCII")); + zos.closeEntry(); + } finally { + if (zos != null) { + try { + zos.close(); + } catch (IOException e) { /* swallow */ } + } + } + } + + private static void testFile(File file, String encoding) + throws IOException { + ZipFile zf = null; + try { + zf = new ZipFile(file, encoding, false); + + Enumeration e = zf.getEntries(); + while (e.hasMoreElements()) { + ZipEntry ze = (ZipEntry) e.nextElement(); + + if (ze.getName().endsWith("sser.txt")) { + assertUnicodeName(ze, OIL_BARREL_TXT, encoding); + + } else if (ze.getName().endsWith("_for_Dollar.txt")) { + assertUnicodeName(ze, EURO_FOR_DOLLAR_TXT, encoding); + } else if (!ze.getName().equals(ASCII_TXT)) { + throw new AssertionError("Urecognized ZIP entry with name [" + + ze.getName() + "] found."); + } + } + } finally { + ZipFile.closeQuietly(zf); + } + } + + private static UnicodePathExtraField findUniCodePath(ZipEntry ze) { + return (UnicodePathExtraField) + ze.getExtraField(UnicodePathExtraField.UPATH_ID); + } + + private static void assertUnicodeName(ZipEntry ze, + String expectedName, + String encoding) + throws IOException { + if (!expectedName.equals(ze.getName())) { + UnicodePathExtraField ucpf = findUniCodePath(ze); + assertNotNull(ucpf); + + ZipEncoding enc = ZipEncodingHelper.getZipEncoding(encoding); + ByteBuffer ne = enc.encode(ze.getName()); + + CRC32 crc = new CRC32(); + crc.update(ne.array(),ne.arrayOffset(),ne.limit()); + + assertEquals(crc.getValue(), ucpf.getNameCRC32()); + assertEquals(expectedName, new String(ucpf.getUnicodeName(), + UTF_8)); + } + } + + private static void assertCanRead(ZipFile zf, String fileName) throws IOException { + ZipEntry entry = zf.getEntry(fileName); + assertNotNull("Entry " + fileName + " doesn't exist", entry); + InputStream is = zf.getInputStream(entry); + assertNotNull("InputStream is null", is); + try { + is.read(); + } finally { + is.close(); + } + } + +} + http://git-wip-us.apache.org/repos/asf/ant/blob/1ae68097/src/tests/junit/org/apache/tools/zip/ZipEncodingTest.java ---------------------------------------------------------------------- diff --git a/src/tests/junit/org/apache/tools/zip/ZipEncodingTest.java b/src/tests/junit/org/apache/tools/zip/ZipEncodingTest.java index 826e410..b9eefd5 100644 --- a/src/tests/junit/org/apache/tools/zip/ZipEncodingTest.java +++ b/src/tests/junit/org/apache/tools/zip/ZipEncodingTest.java @@ -1,152 +1,152 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.apache.tools.zip; - -import java.io.IOException; -import java.nio.ByteBuffer; - -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -/** - * Test zip encodings. - */ -public class ZipEncodingTest { - private static final String UNENC_STRING = "\u2016"; - - // stress test for internal grow method. - private static final String BAD_STRING = - "\u2016\u2015\u2016\u2015\u2016\u2015\u2016\u2015\u2016\u2015\u2016"; - - private static final String BAD_STRING_ENC = - "%U2016%U2015%U2016%U2015%U2016%U2015%U2016%U2015%U2016%U2015%U2016"; - - @Test - public void testSimpleCp437Encoding() throws IOException { - - doSimpleEncodingTest("Cp437", null); - } - - @Test - public void testSimpleCp850Encoding() throws IOException { - - doSimpleEncodingTest("Cp850", null); - } - - @Test - public void testNioCp1252Encoding() throws IOException { - // CP1252 has some undefined code points, these are - // the defined ones - // retrieved by - // awk '/^0x/ && NF>2 {print $1;}' CP1252.TXT - byte[] b = - new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, - 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, - 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, - 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, - 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, - 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, - 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, - 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F, - 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, - 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, - 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, - 0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F, - 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, - 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, - 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, - 0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7E, 0x7F, - (byte) 0x80, (byte) 0x82, (byte) 0x83, (byte) 0x84, - (byte) 0x85, (byte) 0x86, (byte) 0x87, (byte) 0x88, - (byte) 0x89, (byte) 0x8A, (byte) 0x8B, (byte) 0x8C, - (byte) 0x8E, (byte) 0x91, (byte) 0x92, (byte) 0x93, - (byte) 0x94, (byte) 0x95, (byte) 0x96, (byte) 0x97, - (byte) 0x98, (byte) 0x99, (byte) 0x9A, (byte) 0x9B, - (byte) 0x9C, (byte) 0x9E, (byte) 0x9F, (byte) 0xA0, - (byte) 0xA1, (byte) 0xA2, (byte) 0xA3, (byte) 0xA4, - (byte) 0xA5, (byte) 0xA6, (byte) 0xA7, (byte) 0xA8, - (byte) 0xA9, (byte) 0xAA, (byte) 0xAB, (byte) 0xAC, - (byte) 0xAD, (byte) 0xAE, (byte) 0xAF, (byte) 0xB0, - (byte) 0xB1, (byte) 0xB2, (byte) 0xB3, (byte) 0xB4, - (byte) 0xB5, (byte) 0xB6, (byte) 0xB7, (byte) 0xB8, - (byte) 0xB9, (byte) 0xBA, (byte) 0xBB, (byte) 0xBC, - (byte) 0xBD, (byte) 0xBE, (byte) 0xBF, (byte) 0xC0, - (byte) 0xC1, (byte) 0xC2, (byte) 0xC3, (byte) 0xC4, - (byte) 0xC5, (byte) 0xC6, (byte) 0xC7, (byte) 0xC8, - (byte) 0xC9, (byte) 0xCA, (byte) 0xCB, (byte) 0xCC, - (byte) 0xCD, (byte) 0xCE, (byte) 0xCF, (byte) 0xD0, - (byte) 0xD1, (byte) 0xD2, (byte) 0xD3, (byte) 0xD4, - (byte) 0xD5, (byte) 0xD6, (byte) 0xD7, (byte) 0xD8, - (byte) 0xD9, (byte) 0xDA, (byte) 0xDB, (byte) 0xDC, - (byte) 0xDD, (byte) 0xDE, (byte) 0xDF, (byte) 0xE0, - (byte) 0xE1, (byte) 0xE2, (byte) 0xE3, (byte) 0xE4, - (byte) 0xE5, (byte) 0xE6, (byte) 0xE7, (byte) 0xE8, - (byte) 0xE9, (byte) 0xEA, (byte) 0xEB, (byte) 0xEC, - (byte) 0xED, (byte) 0xEE, (byte) 0xEF, (byte) 0xF0, - (byte) 0xF1, (byte) 0xF2, (byte) 0xF3, (byte) 0xF4, - (byte) 0xF5, (byte) 0xF6, (byte) 0xF7, (byte) 0xF8, - (byte) 0xF9, (byte) 0xFA, (byte) 0xFB, (byte) 0xFC, - (byte) 0xFD, (byte) 0xFE, (byte) 0xFF }; - - doSimpleEncodingTest("Cp1252",b); - } - - private static void assertByteEquals(byte[] expected, ByteBuffer actual) { - - assertEquals(expected.length, actual.limit()); - - for (int i = 0; i < expected.length; ++i) { - - byte a = actual.get(); - assertEquals(expected[i], a); - } - - } - - private void doSimpleEncodingTest(String name, byte[] testBytes) - throws IOException { - - ZipEncoding enc = ZipEncodingHelper.getZipEncoding(name); - - if (testBytes == null) { - - testBytes = new byte[256]; - for (int i = 0; i < 256; ++i) { - testBytes[i] = (byte) i; - } - } - - String decoded = enc.decode(testBytes); - - assertEquals(true, enc.canEncode(decoded)); - - ByteBuffer encoded = enc.encode(decoded); - - assertByteEquals(testBytes, encoded); - - assertEquals(false, enc.canEncode(UNENC_STRING)); - assertByteEquals("%U2016".getBytes("US-ASCII"), enc.encode(UNENC_STRING)); - assertEquals(false, enc.canEncode(BAD_STRING)); - assertByteEquals(BAD_STRING_ENC.getBytes("US-ASCII"), - enc.encode(BAD_STRING)); - } - -} +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.tools.zip; + +import java.io.IOException; +import java.nio.ByteBuffer; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +/** + * Test zip encodings. + */ +public class ZipEncodingTest { + private static final String UNENC_STRING = "\u2016"; + + // stress test for internal grow method. + private static final String BAD_STRING = + "\u2016\u2015\u2016\u2015\u2016\u2015\u2016\u2015\u2016\u2015\u2016"; + + private static final String BAD_STRING_ENC = + "%U2016%U2015%U2016%U2015%U2016%U2015%U2016%U2015%U2016%U2015%U2016"; + + @Test + public void testSimpleCp437Encoding() throws IOException { + + doSimpleEncodingTest("Cp437", null); + } + + @Test + public void testSimpleCp850Encoding() throws IOException { + + doSimpleEncodingTest("Cp850", null); + } + + @Test + public void testNioCp1252Encoding() throws IOException { + // CP1252 has some undefined code points, these are + // the defined ones + // retrieved by + // awk '/^0x/ && NF>2 {print $1;}' CP1252.TXT + byte[] b = + new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, + 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, + 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, + 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, + 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F, + 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, + 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, + 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, + 0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F, + 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, + 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, + 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, + 0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7E, 0x7F, + (byte) 0x80, (byte) 0x82, (byte) 0x83, (byte) 0x84, + (byte) 0x85, (byte) 0x86, (byte) 0x87, (byte) 0x88, + (byte) 0x89, (byte) 0x8A, (byte) 0x8B, (byte) 0x8C, + (byte) 0x8E, (byte) 0x91, (byte) 0x92, (byte) 0x93, + (byte) 0x94, (byte) 0x95, (byte) 0x96, (byte) 0x97, + (byte) 0x98, (byte) 0x99, (byte) 0x9A, (byte) 0x9B, + (byte) 0x9C, (byte) 0x9E, (byte) 0x9F, (byte) 0xA0, + (byte) 0xA1, (byte) 0xA2, (byte) 0xA3, (byte) 0xA4, + (byte) 0xA5, (byte) 0xA6, (byte) 0xA7, (byte) 0xA8, + (byte) 0xA9, (byte) 0xAA, (byte) 0xAB, (byte) 0xAC, + (byte) 0xAD, (byte) 0xAE, (byte) 0xAF, (byte) 0xB0, + (byte) 0xB1, (byte) 0xB2, (byte) 0xB3, (byte) 0xB4, + (byte) 0xB5, (byte) 0xB6, (byte) 0xB7, (byte) 0xB8, + (byte) 0xB9, (byte) 0xBA, (byte) 0xBB, (byte) 0xBC, + (byte) 0xBD, (byte) 0xBE, (byte) 0xBF, (byte) 0xC0, + (byte) 0xC1, (byte) 0xC2, (byte) 0xC3, (byte) 0xC4, + (byte) 0xC5, (byte) 0xC6, (byte) 0xC7, (byte) 0xC8, + (byte) 0xC9, (byte) 0xCA, (byte) 0xCB, (byte) 0xCC, + (byte) 0xCD, (byte) 0xCE, (byte) 0xCF, (byte) 0xD0, + (byte) 0xD1, (byte) 0xD2, (byte) 0xD3, (byte) 0xD4, + (byte) 0xD5, (byte) 0xD6, (byte) 0xD7, (byte) 0xD8, + (byte) 0xD9, (byte) 0xDA, (byte) 0xDB, (byte) 0xDC, + (byte) 0xDD, (byte) 0xDE, (byte) 0xDF, (byte) 0xE0, + (byte) 0xE1, (byte) 0xE2, (byte) 0xE3, (byte) 0xE4, + (byte) 0xE5, (byte) 0xE6, (byte) 0xE7, (byte) 0xE8, + (byte) 0xE9, (byte) 0xEA, (byte) 0xEB, (byte) 0xEC, + (byte) 0xED, (byte) 0xEE, (byte) 0xEF, (byte) 0xF0, + (byte) 0xF1, (byte) 0xF2, (byte) 0xF3, (byte) 0xF4, + (byte) 0xF5, (byte) 0xF6, (byte) 0xF7, (byte) 0xF8, + (byte) 0xF9, (byte) 0xFA, (byte) 0xFB, (byte) 0xFC, + (byte) 0xFD, (byte) 0xFE, (byte) 0xFF }; + + doSimpleEncodingTest("Cp1252",b); + } + + private static void assertByteEquals(byte[] expected, ByteBuffer actual) { + + assertEquals(expected.length, actual.limit()); + + for (int i = 0; i < expected.length; ++i) { + + byte a = actual.get(); + assertEquals(expected[i], a); + } + + } + + private void doSimpleEncodingTest(String name, byte[] testBytes) + throws IOException { + + ZipEncoding enc = ZipEncodingHelper.getZipEncoding(name); + + if (testBytes == null) { + + testBytes = new byte[256]; + for (int i = 0; i < 256; ++i) { + testBytes[i] = (byte) i; + } + } + + String decoded = enc.decode(testBytes); + + assertEquals(true, enc.canEncode(decoded)); + + ByteBuffer encoded = enc.encode(decoded); + + assertByteEquals(testBytes, encoded); + + assertEquals(false, enc.canEncode(UNENC_STRING)); + assertByteEquals("%U2016".getBytes("US-ASCII"), enc.encode(UNENC_STRING)); + assertEquals(false, enc.canEncode(BAD_STRING)); + assertByteEquals(BAD_STRING_ENC.getBytes("US-ASCII"), + enc.encode(BAD_STRING)); + } + +}
