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, float loadFactor) throws Exception {
  39         // construct Hashtable with minimal initial capacity and given loadFactor
  40         Hashtable<Integer, Integer> ht1 = new Hashtable<>(1, loadFactor);
  41         // add given number of unique elements
  42         for (int i = 0; i < elements; i++) {
  43             ht1.put(i, i);
  44         }
  45 
  46         // serialize it to byte[] array
  47         byte[] bytes;
  48         try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
  49              ObjectOutputStream oos = new ObjectOutputStream(baos)) {
  50             oos.writeObject(ht1);
  51             oos.flush();
  52             bytes = baos.toByteArray();
  53         }
  54 
  55         // de-serialize it from byte[] array
  56         Hashtable<?, ?> ht2;
  57         try (ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
  58              ObjectInputStream ois = new ObjectInputStream(bais)) {
  59             ht2 = (Hashtable<?, ?>) ois.readObject();
  60         }
  61 
  62         // compare lengths of internal tables
  63         Object[] table1 = (Object[]) hashtableTableField.get(ht1);
  64         Object[] table2 = (Object[]) hashtableTableField.get(ht2);
  65         assert table1 != null;
  66         assert table2 != null;
  67 
  68         int minLength = (int) (ht1.size() / loadFactor) + 1;
  69         int maxLength = minLength * 2;
  70 
  71         boolean ok = (table2.length >= minLength && table2.length <= maxLength);
  72 
  73         System.out.printf(
  74             "%7d %5.2f %7d %7d %7d...%7d %s\n",
  75             ht1.size(), loadFactor,
  76             table1.length, table2.length,
  77             minLength, maxLength,
  78             (ok ? "OK" : "NOT-OK")
  79         );
  80 
  81         return ok;
  82     }
  83 
  84     private static final Field hashtableTableField;
  85 
  86     static {
  87         try {
  88             hashtableTableField = Hashtable.class.getDeclaredField("table");
  89             hashtableTableField.setAccessible(true);
  90         } catch (NoSuchFieldException e) {
  91             throw new Error(e);
  92         }
  93     }
  94 
  95     public static void main(String[] args) throws Exception {
  96         boolean ok = true;
  97 
  98         System.out.printf("Results:\n" +
  99                 "                 ser.  deser.\n" +
 100                 "   size  load  lentgh  length       valid range ok?\n" +
 101                 "------- ----- ------- ------- ----------------- ------\n"
 102         );
 103 
 104         for (int elements : new int[]{10, 50, 500, 5000}) {
 105             for (float loadFactor : new float[]{0.15f, 0.5f, 0.75f, 1.0f, 2.5f}) {
 106                 ok &= testDeserializedLength(elements, loadFactor);
 107             }
 108         }
 109         if (!ok) {
 110             throw new AssertionError("Test failed.");
 111         }
 112     }
 113 }