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.ByteArrayInputStream;
  25 import java.io.ByteArrayOutputStream;
  26 import java.io.ObjectInputStream;
  27 import java.io.ObjectOutputStream;
  28 import java.lang.reflect.Field;
  29 import java.util.Hashtable;
  30 
  31 /**
  32  * @test
  33  * @bug 8068427
  34  * @summary Hashtable deserialization reconstitutes table with wrong capacity
  35  */
  36 public class DeserializedLength {
  37 
  38     static boolean testDeserializedLength(int elements) throws Exception {
  39         // construct Hashtable
  40         float loadFactor = 0.75f;
  41         Hashtable<Integer, Integer> ht1 = new Hashtable<>(
  42             (int) (elements / loadFactor) + 1,
  43             loadFactor
  44         );
  45         for (int i = 0; i < elements; i++) {
  46             ht1.put(i, i);
  47         }
  48 
  49         // serialize it to byte[] array
  50         byte[] bytes;
  51         try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
  52              ObjectOutputStream oos = new ObjectOutputStream(baos)) {
  53             oos.writeObject(ht1);
  54             oos.flush();
  55             bytes = baos.toByteArray();
  56         }
  57 
  58         // de-serialize it from byte[] array
  59         Hashtable<?, ?> ht2;
  60         try (ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
  61              ObjectInputStream ois = new ObjectInputStream(bais)) {
  62             ht2 = (Hashtable<?, ?>) ois.readObject();
  63         }
  64 
  65         // compare lengths of internal tables
  66         Object[] table1 = (Object[]) hashtableTableField.get(ht1);
  67         Object[] table2 = (Object[]) hashtableTableField.get(ht2);
  68 
  69         System.out.println("Serialized Hashtable of size=" + ht1.size() +
  70             " has internal table.length=" + table1.length +
  71             " de-serialized table.length=" + table2.length);
  72 
  73         return table2.length >= table1.length;
  74     }
  75 
  76     private static final Field hashtableTableField;
  77 
  78     static {
  79         try {
  80             hashtableTableField = Hashtable.class.getDeclaredField("table");
  81             hashtableTableField.setAccessible(true);
  82         } catch (NoSuchFieldException e) {
  83             throw new Error(e);
  84         }
  85     }
  86 
  87     public static void main(String[] args) throws Exception {
  88         boolean ok = true;
  89         ok &= testDeserializedLength(10);
  90         ok &= testDeserializedLength(50);
  91         ok &= testDeserializedLength(500);
  92         ok &= testDeserializedLength(5000);
  93         if (!ok) {
  94             throw new AssertionError("Test failed.");
  95         }
  96     }
  97 }