|
1 /* |
|
2 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. |
|
8 * |
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
|
13 * accompanied this code). |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License version |
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 * |
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 * or visit www.oracle.com if you need additional information or have any |
|
21 * questions. |
|
22 */ |
|
23 |
|
24 /* |
|
25 * @test |
|
26 * @bug 4811968 6908628 8006564 |
|
27 * @run main S11N check |
|
28 * @summary Serialization compatibility with old versions (and fixes) |
|
29 */ |
|
30 |
|
31 import java.io.ByteArrayInputStream; |
|
32 import java.io.ByteArrayOutputStream; |
|
33 import java.io.ObjectInputStream; |
|
34 import java.io.ObjectOutputStream; |
|
35 import java.util.HashMap; |
|
36 import java.util.Map; |
|
37 import sun.misc.BASE64Encoder; |
|
38 import sun.security.util.ObjectIdentifier; |
|
39 |
|
40 public class S11N { |
|
41 static String[] SMALL= { |
|
42 "0.0", |
|
43 "1.1", |
|
44 "2.2", |
|
45 "1.2.3456", |
|
46 "1.2.2147483647.4", |
|
47 "1.2.268435456.4", |
|
48 }; |
|
49 |
|
50 static String[] HUGE = { |
|
51 "2.16.764.1.3101555394.1.0.100.2.1", |
|
52 "1.2.2147483648.4", |
|
53 "2.3.4444444444444444444444", |
|
54 "1.2.8888888888888888.33333333333333333.44444444444444", |
|
55 }; |
|
56 |
|
57 // Do not use j.u.Base64, the test needs to run in jdk6 |
|
58 static BASE64Encoder encoder = new BASE64Encoder() { |
|
59 @Override |
|
60 protected int bytesPerLine() { |
|
61 return 48; |
|
62 } |
|
63 }; |
|
64 |
|
65 public static void main(String[] args) throws Exception { |
|
66 if (args[0].equals("check")) { |
|
67 int version = Integer.valueOf(System.getProperty("java.version") |
|
68 .split("\\.")[1]); |
|
69 System.out.println("version is " + version); |
|
70 if (version >= 7) { |
|
71 for (String oid: SMALL) { |
|
72 // 7 -> 7 |
|
73 check(out(oid), oid); |
|
74 // 6 -> 7 |
|
75 check(out6(oid), oid); |
|
76 } |
|
77 for (String oid: HUGE) { |
|
78 // 7 -> 7 |
|
79 check(out(oid), oid); |
|
80 } |
|
81 } else { |
|
82 for (String oid: SMALL) { |
|
83 // 6 -> 6 |
|
84 check(out(oid), oid); |
|
85 // 7 -> 6 |
|
86 check(out7(oid), oid); |
|
87 } |
|
88 for (String oid: HUGE) { |
|
89 // 7 -> 6 fails for HUGE oids |
|
90 boolean ok = false; |
|
91 try { |
|
92 check(out7(oid), oid); |
|
93 ok = true; |
|
94 } catch (Exception e) { |
|
95 System.out.println(e); |
|
96 } |
|
97 if (ok) { |
|
98 throw new Exception(); |
|
99 } |
|
100 } |
|
101 } |
|
102 } else { |
|
103 // Generates the JDK6 serialized string inside this test, call |
|
104 // $JDK7/bin/java S11N dump7 |
|
105 // $JDK6/bin/java S11N dump6 |
|
106 // and paste the output at the end of this test. |
|
107 dump(args[0], SMALL); |
|
108 // For jdk6, the following line will throw an exception, ignore it |
|
109 dump(args[0], HUGE); |
|
110 } |
|
111 } |
|
112 |
|
113 // Gets the serialized form for jdk6 |
|
114 private static byte[] out6(String oid) throws Exception { |
|
115 return new sun.misc.BASE64Decoder().decodeBuffer(dump6.get(oid)); |
|
116 } |
|
117 |
|
118 // Gets the serialized form for jdk7 |
|
119 private static byte[] out7(String oid) throws Exception { |
|
120 return new sun.misc.BASE64Decoder().decodeBuffer(dump7.get(oid)); |
|
121 } |
|
122 |
|
123 // Gets the serialized form for this java |
|
124 private static byte[] out(String oid) throws Exception { |
|
125 ByteArrayOutputStream bout = new ByteArrayOutputStream(); |
|
126 new ObjectOutputStream(bout).writeObject(new ObjectIdentifier(oid)); |
|
127 return bout.toByteArray(); |
|
128 } |
|
129 |
|
130 // Makes sure serialized form can be deserialized |
|
131 private static void check(byte[] in, String oid) throws Exception { |
|
132 ObjectIdentifier o = (ObjectIdentifier) ( |
|
133 new ObjectInputStream(new ByteArrayInputStream(in)).readObject()); |
|
134 if (!o.toString().equals(oid)) { |
|
135 throw new Exception("Read Fail " + o + ", not " + oid); |
|
136 } |
|
137 } |
|
138 |
|
139 // dump serialized form to java code style text |
|
140 private static void dump(String title, String[] oids) throws Exception { |
|
141 for (String oid: oids) { |
|
142 String[] base64 = encoder.encodeBuffer(out(oid)).split("\n"); |
|
143 System.out.println(" " + title + ".put(\"" + oid + "\","); |
|
144 for (int i = 0; i<base64.length; i++) { |
|
145 System.out.print(" \"" + base64[i] + "\""); |
|
146 if (i == base64.length - 1) { |
|
147 System.out.println(");"); |
|
148 } else { |
|
149 System.out.println(" +"); |
|
150 } |
|
151 } |
|
152 } |
|
153 } |
|
154 |
|
155 // Do not use diamond operator, this test is also meant to run in jdk6 |
|
156 private static Map<String,String> dump7 = new HashMap<String,String>(); |
|
157 private static Map<String,String> dump6 = new HashMap<String,String>(); |
|
158 |
|
159 static { |
|
160 ////////////// PASTE BEGIN ////////////// |
|
161 dump7.put("0.0", |
|
162 "rO0ABXNyACJzdW4uc2VjdXJpdHkudXRpbC5PYmplY3RJZGVudGlmaWVyeLIO7GQX" + |
|
163 "fy4DAANJAAxjb21wb25lbnRMZW5MAApjb21wb25lbnRzdAASTGphdmEvbGFuZy9P" + |
|
164 "YmplY3Q7WwAIZW5jb2Rpbmd0AAJbQnhwAAAAAnVyAAJbSU26YCZ26rKlAgAAeHAA" + |
|
165 "AAACAAAAAAAAAAB1cgACW0Ks8xf4BghU4AIAAHhwAAAAAQB4"); |
|
166 dump7.put("1.1", |
|
167 "rO0ABXNyACJzdW4uc2VjdXJpdHkudXRpbC5PYmplY3RJZGVudGlmaWVyeLIO7GQX" + |
|
168 "fy4DAANJAAxjb21wb25lbnRMZW5MAApjb21wb25lbnRzdAASTGphdmEvbGFuZy9P" + |
|
169 "YmplY3Q7WwAIZW5jb2Rpbmd0AAJbQnhwAAAAAnVyAAJbSU26YCZ26rKlAgAAeHAA" + |
|
170 "AAACAAAAAQAAAAF1cgACW0Ks8xf4BghU4AIAAHhwAAAAASl4"); |
|
171 dump7.put("2.2", |
|
172 "rO0ABXNyACJzdW4uc2VjdXJpdHkudXRpbC5PYmplY3RJZGVudGlmaWVyeLIO7GQX" + |
|
173 "fy4DAANJAAxjb21wb25lbnRMZW5MAApjb21wb25lbnRzdAASTGphdmEvbGFuZy9P" + |
|
174 "YmplY3Q7WwAIZW5jb2Rpbmd0AAJbQnhwAAAAAnVyAAJbSU26YCZ26rKlAgAAeHAA" + |
|
175 "AAACAAAAAgAAAAJ1cgACW0Ks8xf4BghU4AIAAHhwAAAAAVJ4"); |
|
176 dump7.put("1.2.3456", |
|
177 "rO0ABXNyACJzdW4uc2VjdXJpdHkudXRpbC5PYmplY3RJZGVudGlmaWVyeLIO7GQX" + |
|
178 "fy4DAANJAAxjb21wb25lbnRMZW5MAApjb21wb25lbnRzdAASTGphdmEvbGFuZy9P" + |
|
179 "YmplY3Q7WwAIZW5jb2Rpbmd0AAJbQnhwAAAAA3VyAAJbSU26YCZ26rKlAgAAeHAA" + |
|
180 "AAADAAAAAQAAAAIAAA2AdXIAAltCrPMX+AYIVOACAAB4cAAAAAMqmwB4"); |
|
181 dump7.put("1.2.2147483647.4", |
|
182 "rO0ABXNyACJzdW4uc2VjdXJpdHkudXRpbC5PYmplY3RJZGVudGlmaWVyeLIO7GQX" + |
|
183 "fy4DAANJAAxjb21wb25lbnRMZW5MAApjb21wb25lbnRzdAASTGphdmEvbGFuZy9P" + |
|
184 "YmplY3Q7WwAIZW5jb2Rpbmd0AAJbQnhwAAAABHVyAAJbSU26YCZ26rKlAgAAeHAA" + |
|
185 "AAAEAAAAAQAAAAJ/////AAAABHVyAAJbQqzzF/gGCFTgAgAAeHAAAAAHKof///9/" + |
|
186 "BHg="); |
|
187 dump7.put("1.2.268435456.4", |
|
188 "rO0ABXNyACJzdW4uc2VjdXJpdHkudXRpbC5PYmplY3RJZGVudGlmaWVyeLIO7GQX" + |
|
189 "fy4DAANJAAxjb21wb25lbnRMZW5MAApjb21wb25lbnRzdAASTGphdmEvbGFuZy9P" + |
|
190 "YmplY3Q7WwAIZW5jb2Rpbmd0AAJbQnhwAAAABHVyAAJbSU26YCZ26rKlAgAAeHAA" + |
|
191 "AAAEAAAAAQAAAAIQAAAAAAAABHVyAAJbQqzzF/gGCFTgAgAAeHAAAAAHKoGAgIAA" + |
|
192 "BHg="); |
|
193 dump7.put("2.16.764.1.3101555394.1.0.100.2.1", |
|
194 "rO0ABXNyACJzdW4uc2VjdXJpdHkudXRpbC5PYmplY3RJZGVudGlmaWVyeLIO7GQX" + |
|
195 "fy4DAANJAAxjb21wb25lbnRMZW5MAApjb21wb25lbnRzdAASTGphdmEvbGFuZy9P" + |
|
196 "YmplY3Q7WwAIZW5jb2Rpbmd0AAJbQnhw/////3NyAD5zdW4uc2VjdXJpdHkudXRp" + |
|
197 "bC5PYmplY3RJZGVudGlmaWVyJEh1Z2VPaWROb3RTdXBwb3J0ZWRCeU9sZEpESwAA" + |
|
198 "AAAAAAABAgAAeHB1cgACW0Ks8xf4BghU4AIAAHhwAAAADmCFfAGLxvf1QgEAZAIB" + |
|
199 "eA=="); |
|
200 dump7.put("1.2.2147483648.4", |
|
201 "rO0ABXNyACJzdW4uc2VjdXJpdHkudXRpbC5PYmplY3RJZGVudGlmaWVyeLIO7GQX" + |
|
202 "fy4DAANJAAxjb21wb25lbnRMZW5MAApjb21wb25lbnRzdAASTGphdmEvbGFuZy9P" + |
|
203 "YmplY3Q7WwAIZW5jb2Rpbmd0AAJbQnhw/////3NyAD5zdW4uc2VjdXJpdHkudXRp" + |
|
204 "bC5PYmplY3RJZGVudGlmaWVyJEh1Z2VPaWROb3RTdXBwb3J0ZWRCeU9sZEpESwAA" + |
|
205 "AAAAAAABAgAAeHB1cgACW0Ks8xf4BghU4AIAAHhwAAAAByqIgICAAAR4"); |
|
206 dump7.put("2.3.4444444444444444444444", |
|
207 "rO0ABXNyACJzdW4uc2VjdXJpdHkudXRpbC5PYmplY3RJZGVudGlmaWVyeLIO7GQX" + |
|
208 "fy4DAANJAAxjb21wb25lbnRMZW5MAApjb21wb25lbnRzdAASTGphdmEvbGFuZy9P" + |
|
209 "YmplY3Q7WwAIZW5jb2Rpbmd0AAJbQnhw/////3NyAD5zdW4uc2VjdXJpdHkudXRp" + |
|
210 "bC5PYmplY3RJZGVudGlmaWVyJEh1Z2VPaWROb3RTdXBwb3J0ZWRCeU9sZEpESwAA" + |
|
211 "AAAAAAABAgAAeHB1cgACW0Ks8xf4BghU4AIAAHhwAAAADFOD4e+HpNG968eOHHg="); |
|
212 dump7.put("1.2.8888888888888888.33333333333333333.44444444444444", |
|
213 "rO0ABXNyACJzdW4uc2VjdXJpdHkudXRpbC5PYmplY3RJZGVudGlmaWVyeLIO7GQX" + |
|
214 "fy4DAANJAAxjb21wb25lbnRMZW5MAApjb21wb25lbnRzdAASTGphdmEvbGFuZy9P" + |
|
215 "YmplY3Q7WwAIZW5jb2Rpbmd0AAJbQnhw/////3NyAD5zdW4uc2VjdXJpdHkudXRp" + |
|
216 "bC5PYmplY3RJZGVudGlmaWVyJEh1Z2VPaWROb3RTdXBwb3J0ZWRCeU9sZEpESwAA" + |
|
217 "AAAAAAABAgAAeHB1cgACW0Ks8xf4BghU4AIAAHhwAAAAGCqP5Yzbxa6cOLubj9ek" + |
|
218 "japVio3AusuOHHg="); |
|
219 |
|
220 dump6.put("0.0", |
|
221 "rO0ABXNyACJzdW4uc2VjdXJpdHkudXRpbC5PYmplY3RJZGVudGlmaWVyeLIO7GQX" + |
|
222 "fy4CAAJJAAxjb21wb25lbnRMZW5bAApjb21wb25lbnRzdAACW0l4cAAAAAJ1cgAC" + |
|
223 "W0lNumAmduqypQIAAHhwAAAAAgAAAAAAAAAA"); |
|
224 dump6.put("1.1", |
|
225 "rO0ABXNyACJzdW4uc2VjdXJpdHkudXRpbC5PYmplY3RJZGVudGlmaWVyeLIO7GQX" + |
|
226 "fy4CAAJJAAxjb21wb25lbnRMZW5bAApjb21wb25lbnRzdAACW0l4cAAAAAJ1cgAC" + |
|
227 "W0lNumAmduqypQIAAHhwAAAAAgAAAAEAAAAB"); |
|
228 dump6.put("2.2", |
|
229 "rO0ABXNyACJzdW4uc2VjdXJpdHkudXRpbC5PYmplY3RJZGVudGlmaWVyeLIO7GQX" + |
|
230 "fy4CAAJJAAxjb21wb25lbnRMZW5bAApjb21wb25lbnRzdAACW0l4cAAAAAJ1cgAC" + |
|
231 "W0lNumAmduqypQIAAHhwAAAAAgAAAAIAAAAC"); |
|
232 dump6.put("1.2.3456", |
|
233 "rO0ABXNyACJzdW4uc2VjdXJpdHkudXRpbC5PYmplY3RJZGVudGlmaWVyeLIO7GQX" + |
|
234 "fy4CAAJJAAxjb21wb25lbnRMZW5bAApjb21wb25lbnRzdAACW0l4cAAAAAN1cgAC" + |
|
235 "W0lNumAmduqypQIAAHhwAAAAAwAAAAEAAAACAAANgA=="); |
|
236 dump6.put("1.2.2147483647.4", |
|
237 "rO0ABXNyACJzdW4uc2VjdXJpdHkudXRpbC5PYmplY3RJZGVudGlmaWVyeLIO7GQX" + |
|
238 "fy4CAAJJAAxjb21wb25lbnRMZW5bAApjb21wb25lbnRzdAACW0l4cAAAAAR1cgAC" + |
|
239 "W0lNumAmduqypQIAAHhwAAAABAAAAAEAAAACf////wAAAAQ="); |
|
240 dump6.put("1.2.268435456.4", |
|
241 "rO0ABXNyACJzdW4uc2VjdXJpdHkudXRpbC5PYmplY3RJZGVudGlmaWVyeLIO7GQX" + |
|
242 "fy4CAAJJAAxjb21wb25lbnRMZW5bAApjb21wb25lbnRzdAACW0l4cAAAAAR1cgAC" + |
|
243 "W0lNumAmduqypQIAAHhwAAAABAAAAAEAAAACEAAAAAAAAAQ="); |
|
244 ////////////// PASTE END ////////////// |
|
245 } |
|
246 } |