/* * Copyright (c) 2013, 2020, 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 4811968 6908628 8006564 8130696 8242151 * @modules java.base/sun.security.util * @run main S11N check * @summary Serialization compatibility with old versions (and fixes)
*/
publicstaticvoid main(String[] args) throws Exception { if (args[0].equals("check")) {
String jv = System.getProperty("java.version"); // java.version format: $VNUM\-$PRE
String [] va = (jv.split("-")[0]).split("\\.");
String v = (va.length == 1 || !va[0].equals("1")) ? va[0] : va[1]; int version = Integer.valueOf(v);
System.out.println("version is " + version); if (version >= 7) { for (String oid: SMALL) { // 7 -> 7
check(out(oid), oid); // 6 -> 7
check(out6(oid), oid);
} for (String oid: HUGE) { // 7 -> 7
check(out(oid), oid);
}
} else { for (String oid: SMALL) { // 6 -> 6
check(out(oid), oid); // 7 -> 6
check(out7(oid), oid);
} for (String oid: HUGE) { // 7 -> 6 fails for HUGE oids boolean ok = false; try {
check(out7(oid), oid);
ok = true;
} catch (Exception e) {
System.out.println(e);
} if (ok) { thrownew Exception();
}
}
}
} else { // Generates the JDK6 serialized string inside this test, call // $JDK7/bin/java S11N dump7 // $JDK6/bin/java S11N dump6 // and paste the output at the end of this test.
dump(args[0], SMALL); // For jdk6, the following line will throw an exception, ignore it
dump(args[0], HUGE);
}
}
// Gets the serialized form for jdk6 privatestaticbyte[] out6(String oid) throws Exception { return decode(dump6.get(oid));
}
// Gets the serialized form for jdk7 privatestaticbyte[] out7(String oid) throws Exception { return decode(dump7.get(oid));
}
// Gets the serialized form for this java privatestaticbyte[] out(String oid) throws Exception {
ByteArrayOutputStream bout = new ByteArrayOutputStream(); new ObjectOutputStream(bout).writeObject(ObjectIdentifier.of(oid)); return bout.toByteArray();
}
// Makes sure serialized form can be deserialized privatestaticvoid check(byte[] in, String oid) throws Exception {
ObjectIdentifier o = (ObjectIdentifier) ( new ObjectInputStream(new ByteArrayInputStream(in)).readObject()); if (!o.toString().equals(oid)) { thrownew Exception("Read Fail " + o + ", not " + oid);
}
}
// dump serialized form to java code style text privatestaticvoid dump(String title, String[] oids) throws Exception { for (String oid: oids) {
String hex = encode(out(oid));
System.out.println(" " + title + ".put(\"" + oid + "\","); for (int i = 0; i<hex.length(); i+= 64) { int end = i + 64; if (end > hex.length()) {
end = hex.length();
}
System.out.print(" \"" + hex.substring(i, end) + "\""); if (end == hex.length()) {
System.out.println(");");
} else {
System.out.println(" +");
}
}
}
}
privatestatic String encode(byte[] bytes) {
StringBuilder sb = new StringBuilder(bytes.length * 2); for (byte b: bytes) {
sb.append(String.format("%02x", b & 0xff));
} return sb.toString();
}
privatestaticbyte[] decode(String var) { byte[] data = newbyte[var.length()/2]; for (int i=0; i<data.length; i++) {
data[i] = Integer.valueOf(var.substring(2 * i, 2 * i + 2), 16).byteValue();
} return data;
}
// Do not use diamond operator, this test is also meant to run in jdk6 privatestatic Map<String,String> dump7 = new HashMap<String,String>(); privatestatic Map<String,String> dump6 = new HashMap<String,String>();
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.