/* * Copyright (c) 2006, 2013, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
/** * @test * @bug 6480504 6303183 * @summary Test that client-provided data in the extra field is written and * read correctly, taking into account the JAR_MAGIC written into the extra * field of the first entry of JAR files. * @author Dave Bristor
*/
/** * Tests that the get/set operations on extra data in zip and jar files work * as advertised. The base class tests ZIP files, the member class * TestJarExtra checks JAR files.
*/ publicclass TestExtra { staticfinalint JAR_MAGIC = 0xcafe; // private IN JarOutputStream.java staticfinalint TEST_HEADER = 0xbabe;
/** Test that a header + data set by client works. */ void testHeaderPlusData() throws IOException { for (byte[] b : extra) {
ZipEntry ze = getEntry(); byte[] data = newbyte[b.length + 4];
set16(data, 0, TEST_HEADER);
set16(data, 2, b.length); for (int i = 0; i < b.length; i++) {
data[i + 4] = b[i];
}
ze.setExtra(data);
zos.putNextEntry(ze);
}
zos.close();
ZipInputStream zis = getInputStream();
ZipEntry ze = zis.getNextEntry();
checkEntry(ze, 0, extra[0].length);
ze = zis.getNextEntry();
checkEntry(ze, 1, extra[1].length);
}
/** Test that a header only (i.e., no extra "data") set by client works. */ void testHeaderOnly() throws IOException {
ZipEntry ze = getEntry(); byte[] data = newbyte[4];
set16(data, 0, TEST_HEADER);
set16(data, 2, 0); // Length of data is 0.
ze.setExtra(data);
zos.putNextEntry(ze);
zos.close();
ZipInputStream zis = getInputStream();
ze = zis.getNextEntry();
checkExtra(data, ze.getExtra());
checkEntry(ze, 0, 0);
}
/** Tests the client providing extra data which uses JAR_MAGIC header. */ void testClientJarMagic() throws IOException {
ZipEntry ze = getEntry(); byte[] data = newbyte[8];
set16(data, 0, TEST_HEADER);
set16(data, 2, 0); // Length of data is 0.
set16(data, 4, JAR_MAGIC);
set16(data, 6, 0); // Length of data is 0.
ze.setExtra(data);
zos.putNextEntry(ze);
zos.close();
ZipInputStream zis = getInputStream();
ze = zis.getNextEntry(); byte[] e = ze.getExtra();
checkExtra(data, ze.getExtra());
checkEntry(ze, 0, 0);
}
// check if all "expected" extra fields equal to their // corresponding fields in "extra". The "extra" might have // timestamp fields added by ZOS. staticvoid checkExtra(byte[] expected, byte[] extra) { if (expected == null) return; int off = 0; int len = expected.length; while (off + 4 < len) { int tag = get16(expected, off); int sz = get16(expected, off + 2); int off0 = 0; int len0 = extra.length; boolean matched = false; while (off0 + 4 < len0) { int tag0 = get16(extra, off0); int sz0 = get16(extra, off0 + 2); if (tag == tag0 && sz == sz0) {
matched = true; for (int i = 0; i < sz; i++) { if (expected[off + i] != extra[off0 +i])
matched = false;
} break;
}
off0 += (4 + sz0);
} if (!matched) {
fail("Expected extra data [tag=" + tag + "sz=" + sz + "] check failed");
}
off += (4 + sz);
}
}
/** Check that the entry's extra data is correct. */ void checkEntry(ZipEntry ze, int count, int dataLength) { byte[] extraData = ze.getExtra(); byte[] data = getField(TEST_HEADER, extraData); if (!check(data != null, "unexpected null data for TEST_HEADER")) { return;
}
if (dataLength == 0) {
check(data.length == 0, "unexpected non-zero data length for TEST_HEADER");
} else {
check(Arrays.equals(extra[count], data), "failed to get entry " + ze.getName()
+ ", expected " + new String(extra[count]) + ", got '" + new String(data) + "'");
}
}
/** Look up descriptor in data, returning corresponding byte[]. */ staticbyte[] getField(int descriptor, byte[] data) { byte[] rc = null; try { int i = 0; while (i < data.length) { if (get16(data, i) == descriptor) { int length = get16(data, i + 2);
rc = newbyte[length]; for (int j = 0; j < length; j++) {
rc[j] = data[i + 4 + j];
} return rc;
}
i += get16(data, i + 2) + 4;
}
} catch (ArrayIndexOutOfBoundsException e) { // descriptor not found
} return rc;
}
ZipInputStream getInputStream() { returnnew ZipInputStream( new ByteArrayInputStream(baos.toByteArray()));
}
privatestaticvoid set16(byte[] b, int off, int value) {
b[off+0] = (byte)value;
b[off+1] = (byte)(value >> 8);
}
/** Test extra field of a JAR file. */ staticclass TestJarExtra extends TestExtra {
ZipOutputStream getOutputStream(ByteArrayOutputStream baos) throws IOException { returnnew JarOutputStream(baos);
}
void checkEntry(ZipEntry ze, int count, int dataLength) { // zeroth entry should have JAR_MAGIC if (count == 0) { byte[] extraData = ze.getExtra(); byte[] data = getField(JAR_MAGIC, extraData); if (!check(data != null, "unexpected null data for JAR_MAGIC")) {
check(data.length != 0, "unexpected non-zero data length for JAR_MAGIC");
}
} // In a jar file, the first ZipEntry should have both JAR_MAGIC // and the TEST_HEADER, so check that also. super.checkEntry(ze, count, dataLength);
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.