Path: blob/master/test/jdk/sun/security/util/Oid/S11N.java
41152 views
/*1* Copyright (c) 2013, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 4811968 6908628 8006564 8130696 824215126* @modules java.base/sun.security.util27* @run main S11N check28* @summary Serialization compatibility with old versions (and fixes)29*/3031import java.io.ByteArrayInputStream;32import java.io.ByteArrayOutputStream;33import java.io.ObjectInputStream;34import java.io.ObjectOutputStream;35import java.util.HashMap;36import java.util.Map;37import sun.security.util.ObjectIdentifier;3839public class S11N {40static String[] SMALL= {41"0.0",42"1.1",43"2.2",44"1.2.3456",45"1.2.2147483647.4",46"1.2.268435456.4",47};4849static String[] HUGE = {50"2.16.764.1.3101555394.1.0.100.2.1",51"1.2.2147483648.4",52"2.3.4444444444444444444444",53"1.2.8888888888888888.33333333333333333.44444444444444",54};5556public static void main(String[] args) throws Exception {57if (args[0].equals("check")) {58String jv = System.getProperty("java.version");59// java.version format: $VNUM\-$PRE60String [] va = (jv.split("-")[0]).split("\\.");61String v = (va.length == 1 || !va[0].equals("1")) ? va[0] : va[1];62int version = Integer.valueOf(v);63System.out.println("version is " + version);64if (version >= 7) {65for (String oid: SMALL) {66// 7 -> 767check(out(oid), oid);68// 6 -> 769check(out6(oid), oid);70}71for (String oid: HUGE) {72// 7 -> 773check(out(oid), oid);74}75} else {76for (String oid: SMALL) {77// 6 -> 678check(out(oid), oid);79// 7 -> 680check(out7(oid), oid);81}82for (String oid: HUGE) {83// 7 -> 6 fails for HUGE oids84boolean ok = false;85try {86check(out7(oid), oid);87ok = true;88} catch (Exception e) {89System.out.println(e);90}91if (ok) {92throw new Exception();93}94}95}96} else {97// Generates the JDK6 serialized string inside this test, call98// $JDK7/bin/java S11N dump799// $JDK6/bin/java S11N dump6100// and paste the output at the end of this test.101dump(args[0], SMALL);102// For jdk6, the following line will throw an exception, ignore it103dump(args[0], HUGE);104}105}106107// Gets the serialized form for jdk6108private static byte[] out6(String oid) throws Exception {109return decode(dump6.get(oid));110}111112// Gets the serialized form for jdk7113private static byte[] out7(String oid) throws Exception {114return decode(dump7.get(oid));115}116117// Gets the serialized form for this java118private static byte[] out(String oid) throws Exception {119ByteArrayOutputStream bout = new ByteArrayOutputStream();120new ObjectOutputStream(bout).writeObject(ObjectIdentifier.of(oid));121return bout.toByteArray();122}123124// Makes sure serialized form can be deserialized125private static void check(byte[] in, String oid) throws Exception {126ObjectIdentifier o = (ObjectIdentifier) (127new ObjectInputStream(new ByteArrayInputStream(in)).readObject());128if (!o.toString().equals(oid)) {129throw new Exception("Read Fail " + o + ", not " + oid);130}131}132133// dump serialized form to java code style text134private static void dump(String title, String[] oids) throws Exception {135for (String oid: oids) {136String hex = encode(out(oid));137System.out.println(" " + title + ".put(\"" + oid + "\",");138for (int i = 0; i<hex.length(); i+= 64) {139int end = i + 64;140if (end > hex.length()) {141end = hex.length();142}143System.out.print(" \"" + hex.substring(i, end) + "\"");144if (end == hex.length()) {145System.out.println(");");146} else {147System.out.println(" +");148}149}150}151}152153private static String encode(byte[] bytes) {154StringBuilder sb = new StringBuilder(bytes.length * 2);155for (byte b: bytes) {156sb.append(String.format("%02x", b & 0xff));157}158return sb.toString();159}160161private static byte[] decode(String var) {162byte[] data = new byte[var.length()/2];163for (int i=0; i<data.length; i++) {164data[i] = Integer.valueOf(var.substring(2 * i, 2 * i + 2), 16).byteValue();165}166return data;167}168169// Do not use diamond operator, this test is also meant to run in jdk6170private static Map<String,String> dump7 = new HashMap<String,String>();171private static Map<String,String> dump6 = new HashMap<String,String>();172173static {174////////////// PASTE BEGIN //////////////175dump7.put("0.0",176"aced00057372002273756e2e73656375726974792e7574696c2e4f626a656374" +177"4964656e74696669657278b20eec64177f2e03000349000c636f6d706f6e656e" +178"744c656e4c000a636f6d706f6e656e74737400124c6a6176612f6c616e672f4f" +179"626a6563743b5b0008656e636f64696e677400025b4278700000000275720002" +180"5b494dba602676eab2a50200007870000000020000000000000000757200025b" +181"42acf317f8060854e00200007870000000010078");182dump7.put("1.1",183"aced00057372002273756e2e73656375726974792e7574696c2e4f626a656374" +184"4964656e74696669657278b20eec64177f2e03000349000c636f6d706f6e656e" +185"744c656e4c000a636f6d706f6e656e74737400124c6a6176612f6c616e672f4f" +186"626a6563743b5b0008656e636f64696e677400025b4278700000000275720002" +187"5b494dba602676eab2a50200007870000000020000000100000001757200025b" +188"42acf317f8060854e00200007870000000012978");189dump7.put("2.2",190"aced00057372002273756e2e73656375726974792e7574696c2e4f626a656374" +191"4964656e74696669657278b20eec64177f2e03000349000c636f6d706f6e656e" +192"744c656e4c000a636f6d706f6e656e74737400124c6a6176612f6c616e672f4f" +193"626a6563743b5b0008656e636f64696e677400025b4278700000000275720002" +194"5b494dba602676eab2a50200007870000000020000000200000002757200025b" +195"42acf317f8060854e00200007870000000015278");196dump7.put("1.2.3456",197"aced00057372002273756e2e73656375726974792e7574696c2e4f626a656374" +198"4964656e74696669657278b20eec64177f2e03000349000c636f6d706f6e656e" +199"744c656e4c000a636f6d706f6e656e74737400124c6a6176612f6c616e672f4f" +200"626a6563743b5b0008656e636f64696e677400025b4278700000000375720002" +201"5b494dba602676eab2a5020000787000000003000000010000000200000d8075" +202"7200025b42acf317f8060854e00200007870000000032a9b0078");203dump7.put("1.2.2147483647.4",204"aced00057372002273756e2e73656375726974792e7574696c2e4f626a656374" +205"4964656e74696669657278b20eec64177f2e03000349000c636f6d706f6e656e" +206"744c656e4c000a636f6d706f6e656e74737400124c6a6176612f6c616e672f4f" +207"626a6563743b5b0008656e636f64696e677400025b4278700000000475720002" +208"5b494dba602676eab2a502000078700000000400000001000000027fffffff00" +209"000004757200025b42acf317f8060854e00200007870000000072a87ffffff7f" +210"0478");211dump7.put("1.2.268435456.4",212"aced00057372002273756e2e73656375726974792e7574696c2e4f626a656374" +213"4964656e74696669657278b20eec64177f2e03000349000c636f6d706f6e656e" +214"744c656e4c000a636f6d706f6e656e74737400124c6a6176612f6c616e672f4f" +215"626a6563743b5b0008656e636f64696e677400025b4278700000000475720002" +216"5b494dba602676eab2a502000078700000000400000001000000021000000000" +217"000004757200025b42acf317f8060854e00200007870000000072a8180808000" +218"0478");219dump7.put("2.16.764.1.3101555394.1.0.100.2.1",220"aced00057372002273756e2e73656375726974792e7574696c2e4f626a656374" +221"4964656e74696669657278b20eec64177f2e03000349000c636f6d706f6e656e" +222"744c656e4c000a636f6d706f6e656e74737400124c6a6176612f6c616e672f4f" +223"626a6563743b5b0008656e636f64696e677400025b427870ffffffff7372003e" +224"73756e2e73656375726974792e7574696c2e4f626a6563744964656e74696669" +225"657224487567654f69644e6f74537570706f7274656442794f6c644a444b0000" +226"0000000000010200007870757200025b42acf317f8060854e002000078700000" +227"000e60857c018bc6f7f542010064020178");228dump7.put("1.2.2147483648.4",229"aced00057372002273756e2e73656375726974792e7574696c2e4f626a656374" +230"4964656e74696669657278b20eec64177f2e03000349000c636f6d706f6e656e" +231"744c656e4c000a636f6d706f6e656e74737400124c6a6176612f6c616e672f4f" +232"626a6563743b5b0008656e636f64696e677400025b427870ffffffff7372003e" +233"73756e2e73656375726974792e7574696c2e4f626a6563744964656e74696669" +234"657224487567654f69644e6f74537570706f7274656442794f6c644a444b0000" +235"0000000000010200007870757200025b42acf317f8060854e002000078700000" +236"00072a88808080000478");237dump7.put("2.3.4444444444444444444444",238"aced00057372002273756e2e73656375726974792e7574696c2e4f626a656374" +239"4964656e74696669657278b20eec64177f2e03000349000c636f6d706f6e656e" +240"744c656e4c000a636f6d706f6e656e74737400124c6a6176612f6c616e672f4f" +241"626a6563743b5b0008656e636f64696e677400025b427870ffffffff7372003e" +242"73756e2e73656375726974792e7574696c2e4f626a6563744964656e74696669" +243"657224487567654f69644e6f74537570706f7274656442794f6c644a444b0000" +244"0000000000010200007870757200025b42acf317f8060854e002000078700000" +245"000c5383e1ef87a4d1bdebc78e1c78");246dump7.put("1.2.8888888888888888.33333333333333333.44444444444444",247"aced00057372002273756e2e73656375726974792e7574696c2e4f626a656374" +248"4964656e74696669657278b20eec64177f2e03000349000c636f6d706f6e656e" +249"744c656e4c000a636f6d706f6e656e74737400124c6a6176612f6c616e672f4f" +250"626a6563743b5b0008656e636f64696e677400025b427870ffffffff7372003e" +251"73756e2e73656375726974792e7574696c2e4f626a6563744964656e74696669" +252"657224487567654f69644e6f74537570706f7274656442794f6c644a444b0000" +253"0000000000010200007870757200025b42acf317f8060854e002000078700000" +254"00182a8fe58cdbc5ae9c38bb9b8fd7a48daa558a8dc0bacb8e1c78");255256dump6.put("0.0",257"aced00057372002273756e2e73656375726974792e7574696c2e4f626a656374" +258"4964656e74696669657278b20eec64177f2e02000249000c636f6d706f6e656e" +259"744c656e5b000a636f6d706f6e656e74737400025b4978700000000275720002" +260"5b494dba602676eab2a50200007870000000020000000000000000");261dump6.put("1.1",262"aced00057372002273756e2e73656375726974792e7574696c2e4f626a656374" +263"4964656e74696669657278b20eec64177f2e02000249000c636f6d706f6e656e" +264"744c656e5b000a636f6d706f6e656e74737400025b4978700000000275720002" +265"5b494dba602676eab2a50200007870000000020000000100000001");266dump6.put("2.2",267"aced00057372002273756e2e73656375726974792e7574696c2e4f626a656374" +268"4964656e74696669657278b20eec64177f2e02000249000c636f6d706f6e656e" +269"744c656e5b000a636f6d706f6e656e74737400025b4978700000000275720002" +270"5b494dba602676eab2a50200007870000000020000000200000002");271dump6.put("1.2.3456",272"aced00057372002273756e2e73656375726974792e7574696c2e4f626a656374" +273"4964656e74696669657278b20eec64177f2e02000249000c636f6d706f6e656e" +274"744c656e5b000a636f6d706f6e656e74737400025b4978700000000375720002" +275"5b494dba602676eab2a5020000787000000003000000010000000200000d80");276dump6.put("1.2.2147483647.4",277"aced00057372002273756e2e73656375726974792e7574696c2e4f626a656374" +278"4964656e74696669657278b20eec64177f2e02000249000c636f6d706f6e656e" +279"744c656e5b000a636f6d706f6e656e74737400025b4978700000000475720002" +280"5b494dba602676eab2a502000078700000000400000001000000027fffffff00" +281"000004");282dump6.put("1.2.268435456.4",283"aced00057372002273756e2e73656375726974792e7574696c2e4f626a656374" +284"4964656e74696669657278b20eec64177f2e02000249000c636f6d706f6e656e" +285"744c656e5b000a636f6d706f6e656e74737400025b4978700000000475720002" +286"5b494dba602676eab2a502000078700000000400000001000000021000000000" +287"000004");288289////////////// PASTE END //////////////290}291}292293294