1 /* 2 * Copyright (c) 2014, 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 import java.io.*; 25 import java.lang.reflect.Field; 26 import java.util.Hashtable; 27 28 /** 29 * @test 30 * @bug 8068427 31 * @summary Hashtable deserialization reconstitutes table with wrong capacity 32 */ 33 public class DeserializedLength { 34 35 static boolean testDeserializedLength(int elements, float loadFactor) throws Exception { 36 37 // construct Hashtable with minimal initial capacity and given loadFactor 38 Hashtable<Integer, Integer> ht1 = new Hashtable<>(1, loadFactor); 39 40 // add given number of unique elements 41 for (int i = 0; i < elements; i++) { 42 ht1.put(i, i); 43 } 44 45 // serialize and deserialize into a deep clone 46 Hashtable<Integer, Integer> ht2 = serialClone(ht1); 47 48 // compare lengths of internal tables 49 Object[] table1 = (Object[]) hashtableTableField.get(ht1); 50 Object[] table2 = (Object[]) hashtableTableField.get(ht2); 51 assert table1 != null; 52 assert table2 != null; 53 54 int minLength = (int) (ht1.size() / loadFactor) + 1; 55 int maxLength = minLength * 2; 56 57 boolean ok = (table2.length >= minLength && table2.length <= maxLength); 58 59 System.out.printf( 60 "%7d %5.2f %7d %7d %7d...%7d %s\n", 61 ht1.size(), loadFactor, 62 table1.length, table2.length, 63 minLength, maxLength, 64 (ok ? "OK" : "NOT-OK") 65 ); 66 67 return ok; 68 } 69 70 static <T> T serialClone(T o) throws IOException, ClassNotFoundException { 71 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 72 try (ObjectOutputStream oos = new ObjectOutputStream(bos)) { 73 oos.writeObject(o); 74 } 75 @SuppressWarnings("unchecked") 76 T clone = (T) new ObjectInputStream( 77 new ByteArrayInputStream(bos.toByteArray())).readObject(); 78 return clone; 79 } 80 81 private static final Field hashtableTableField; 82 83 static { 84 try { 85 hashtableTableField = Hashtable.class.getDeclaredField("table"); 86 hashtableTableField.setAccessible(true); 87 } catch (NoSuchFieldException e) { 88 throw new Error(e); 89 } 90 } 91 92 public static void main(String[] args) throws Exception { 93 boolean ok = true; 94 95 System.out.printf("Results:\n" + 96 " ser. deser.\n" + 97 " size load lentgh length valid range ok?\n" + 98 "------- ----- ------- ------- ----------------- ------\n" 99 ); 100 101 for (int elements : new int[]{10, 50, 500, 5000}) { 102 for (float loadFactor : new float[]{0.15f, 0.5f, 0.75f, 1.0f, 2.5f}) { 103 ok &= testDeserializedLength(elements, loadFactor); 104 } 105 } 106 if (!ok) { 107 throw new AssertionError("Test failed."); 108 } 109 } 110 }