0N/A/*
2362N/A * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/**
0N/A * @test
0N/A * @bug 6480504
0N/A * @summary Test that client-provided data in the extra field is written and
0N/A * read correctly, taking into account the JAR_MAGIC written into the extra
0N/A * field of the first entry of JAR files.
0N/A * @author Dave Bristor
0N/A */
0N/A
0N/Aimport java.io.*;
0N/Aimport java.nio.charset.Charset;
0N/Aimport java.util.Arrays;
0N/Aimport java.util.jar.*;
0N/Aimport java.util.zip.*;
0N/A
0N/A/**
0N/A * Tests that the get/set operations on extra data in zip and jar files work
0N/A * as advertised. The base class tests ZIP files, the member class
0N/A * TestJarExtra checks JAR files.
0N/A */
0N/Apublic class TestExtra {
0N/A final static int JAR_MAGIC = 0xcafe; // private IN JarOutputStream.java
0N/A final static int TEST_HEADER = 0xbabe;
0N/A
0N/A final static Charset ascii = Charset.forName("ASCII");
0N/A
0N/A // ZipEntry extra data
0N/A final static byte[][] extra = new byte[][] {
0N/A ascii.encode("hello, world").array(),
0N/A ascii.encode("foo bar").array()
0N/A };
0N/A
0N/A // For naming entries in JAR/ZIP streams
0N/A int count = 1;
0N/A
0N/A // Use byte arrays instead of files
0N/A ByteArrayOutputStream baos;
0N/A
0N/A // JAR/ZIP content written here.
0N/A ZipOutputStream zos;
0N/A
0N/A public static void realMain(String[] args) throws Throwable{
0N/A new TestExtra().testHeaderPlusData();
0N/A
0N/A new TestJarExtra().testHeaderPlusData();
0N/A new TestJarExtra().testHeaderOnly();
0N/A new TestJarExtra().testClientJarMagic();
0N/A }
0N/A
0N/A TestExtra() {
0N/A try {
0N/A baos = new ByteArrayOutputStream();
0N/A zos = getOutputStream(baos);
0N/A } catch (Throwable t) {
0N/A unexpected(t);
0N/A }
0N/A }
0N/A
0N/A /** Test that a header + data set by client works. */
0N/A void testHeaderPlusData() throws IOException {
0N/A for (byte[] b : extra) {
0N/A ZipEntry ze = getEntry();
0N/A byte[] data = new byte[b.length + 4];
0N/A set16(data, 0, TEST_HEADER);
0N/A set16(data, 2, b.length);
0N/A for (int i = 0; i < b.length; i++) {
0N/A data[i + 4] = b[i];
0N/A }
0N/A ze.setExtra(data);
0N/A zos.putNextEntry(ze);
0N/A }
0N/A zos.close();
0N/A
0N/A ZipInputStream zis = getInputStream();
0N/A
0N/A ZipEntry ze = zis.getNextEntry();
0N/A checkEntry(ze, 0, extra[0].length);
0N/A
0N/A ze = zis.getNextEntry();
0N/A checkEntry(ze, 1, extra[1].length);
0N/A }
0N/A
0N/A /** Test that a header only (i.e., no extra "data") set by client works. */
0N/A void testHeaderOnly() throws IOException {
0N/A ZipEntry ze = getEntry();
0N/A byte[] data = new byte[4];
0N/A set16(data, 0, TEST_HEADER);
0N/A set16(data, 2, 0); // Length of data is 0.
0N/A ze.setExtra(data);
0N/A zos.putNextEntry(ze);
0N/A
0N/A zos.close();
0N/A
0N/A ZipInputStream zis = getInputStream();
0N/A
0N/A ze = zis.getNextEntry();
0N/A byte[] e = ze.getExtra();
0N/A check(e.length == 8, "expected extra length is 8, got " + e.length);
0N/A checkEntry(ze, 0, 0);
0N/A }
0N/A
0N/A /** Tests the client providing extra data which uses JAR_MAGIC header. */
0N/A void testClientJarMagic() throws IOException {
0N/A ZipEntry ze = getEntry();
0N/A byte[] data = new byte[8];
0N/A
0N/A set16(data, 0, TEST_HEADER);
0N/A set16(data, 2, 0); // Length of data is 0.
0N/A set16(data, 4, JAR_MAGIC);
0N/A set16(data, 6, 0); // Length of data is 0.
0N/A
0N/A ze.setExtra(data);
0N/A zos.putNextEntry(ze);
0N/A
0N/A zos.close();
0N/A
0N/A ZipInputStream zis = getInputStream();
0N/A ze = zis.getNextEntry();
0N/A byte[] e = ze.getExtra();
0N/A check(e.length == 8, "expected extra length is 8, got " + e.length);
0N/A checkEntry(ze, 0, 0);
0N/A }
0N/A
0N/A
0N/A /** Check that the entry's extra data is correct. */
0N/A void checkEntry(ZipEntry ze, int count, int dataLength) {
0N/A byte[] extraData = ze.getExtra();
0N/A byte[] data = getField(TEST_HEADER, extraData);
0N/A if (!check(data != null, "unexpected null data for TEST_HEADER")) {
0N/A return;
0N/A }
0N/A
0N/A if (dataLength == 0) {
0N/A check(data.length == 0, "unexpected non-zero data length for TEST_HEADER");
0N/A } else {
0N/A check(Arrays.equals(extra[count], data),
0N/A "failed to get entry " + ze.getName()
0N/A + ", expected " + new String(extra[count]) + ", got '" + new String(data) + "'");
0N/A }
0N/A }
0N/A
0N/A /** Look up descriptor in data, returning corresponding byte[]. */
0N/A static byte[] getField(int descriptor, byte[] data) {
0N/A byte[] rc = null;
0N/A try {
0N/A int i = 0;
0N/A while (i < data.length) {
0N/A if (get16(data, i) == descriptor) {
0N/A int length = get16(data, i + 2);
0N/A rc = new byte[length];
0N/A for (int j = 0; j < length; j++) {
0N/A rc[j] = data[i + 4 + j];
0N/A }
0N/A return rc;
0N/A }
0N/A i += get16(data, i + 2) + 4;
0N/A }
0N/A } catch (ArrayIndexOutOfBoundsException e) {
0N/A // descriptor not found
0N/A }
0N/A return rc;
0N/A }
0N/A
0N/A ZipInputStream getInputStream() {
0N/A return new ZipInputStream(
0N/A new ByteArrayInputStream(baos.toByteArray()));
0N/A }
0N/A
0N/A ZipOutputStream getOutputStream(ByteArrayOutputStream baos) throws IOException {
0N/A return new ZipOutputStream(baos);
0N/A }
0N/A
0N/A ZipInputStream getInputStream(ByteArrayInputStream bais) throws IOException {
0N/A return new ZipInputStream(bais);
0N/A }
0N/A
0N/A ZipEntry getEntry() { return new ZipEntry("zip" + count++ + ".txt"); }
0N/A
0N/A
0N/A private static int get16(byte[] b, int off) {
0N/A return (b[off] & 0xff) | ((b[off+1] & 0xff) << 8);
0N/A }
0N/A
0N/A private static void set16(byte[] b, int off, int value) {
0N/A b[off+0] = (byte)value;
0N/A b[off+1] = (byte)(value >> 8);
0N/A }
0N/A
0N/A /** Test extra field of a JAR file. */
0N/A static class TestJarExtra extends TestExtra {
0N/A ZipOutputStream getOutputStream(ByteArrayOutputStream baos) throws IOException {
0N/A return new JarOutputStream(baos);
0N/A }
0N/A
0N/A ZipInputStream getInputStream(ByteArrayInputStream bais) throws IOException {
0N/A return new JarInputStream(bais);
0N/A }
0N/A
0N/A ZipEntry getEntry() { return new ZipEntry("jar" + count++ + ".txt"); }
0N/A
0N/A void checkEntry(ZipEntry ze, int count, int dataLength) {
0N/A // zeroth entry should have JAR_MAGIC
0N/A if (count == 0) {
0N/A byte[] extraData = ze.getExtra();
0N/A byte[] data = getField(JAR_MAGIC, extraData);
0N/A if (!check(data != null, "unexpected null data for JAR_MAGIC")) {
0N/A check(data.length != 0, "unexpected non-zero data length for JAR_MAGIC");
0N/A }
0N/A }
0N/A // In a jar file, the first ZipEntry should have both JAR_MAGIC
0N/A // and the TEST_HEADER, so check that also.
0N/A super.checkEntry(ze, count, dataLength);
0N/A }
0N/A }
0N/A
0N/A //--------------------- Infrastructure ---------------------------
0N/A static volatile int passed = 0, failed = 0;
0N/A static void pass() {passed++;}
0N/A static void fail() {failed++; Thread.dumpStack();}
0N/A static void fail(String msg) {System.out.println(msg); fail();}
0N/A static void unexpected(Throwable t) {failed++; t.printStackTrace();}
0N/A static void check(boolean cond) {if (cond) pass(); else fail();}
0N/A static boolean check(boolean cond, String msg) {if (cond) pass(); else fail(msg); return cond; }
0N/A static void equal(Object x, Object y) {
0N/A if (x == null ? y == null : x.equals(y)) pass();
0N/A else fail(x + " not equal to " + y);}
0N/A public static void main(String[] args) throws Throwable {
0N/A try {realMain(args);} catch (Throwable t) {unexpected(t);}
0N/A System.out.println("\nPassed = " + passed + " failed = " + failed);
0N/A if (failed > 0) throw new Error("Some tests failed");}
0N/A}