1 /*
   2  * Copyright (c) 2014, 2016, 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.awt.Button;
  25 import java.awt.Component;
  26 import java.awt.Container;
  27 import java.io.ByteArrayInputStream;
  28 import java.io.ByteArrayOutputStream;
  29 import java.io.ObjectInputStream;
  30 import java.io.ObjectOutputStream;
  31 
  32 /**
  33  * @test
  34  * @key headful
  35  * @bug 8059590
  36  * @summary ArrayIndexOutOfBoundsException occurs when Container with overridden getComponents() is deserialized.
  37  * @author Alexey Ivanov
  38  * @run main ContainerAIOOBE
  39  */
  40 public class ContainerAIOOBE {
  41 
  42     public static void main(final String[] args) throws Exception {
  43         ZContainer z = new ZContainer();
  44         z.add(new Button());
  45 
  46         ByteArrayOutputStream baos = new ByteArrayOutputStream();
  47         ObjectOutputStream oos = new ObjectOutputStream(baos);
  48         oos.writeObject(z);
  49         oos.flush();
  50         oos.close();
  51 
  52         byte[] array = baos.toByteArray();
  53         ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(array));
  54 
  55         // Reading the object must not throw ArrayIndexOutOfBoundsException
  56         ZContainer zz = (ZContainer) ois.readObject();
  57 
  58         if (zz.getComponentCount() != 1) {
  59             throw new Exception("deserialized object must have 1 component");
  60         }
  61         if (!(zz.getComponent(0) instanceof Button)) {
  62             throw new Exception("deserialized object must contain Button component");
  63         }
  64         if (zz.getComponents().length != 0) {
  65             throw new Exception("deserialized object returns non-empty array");
  66         }
  67         System.out.println("Test passed");
  68     }
  69 
  70     static class ZContainer extends Container {
  71         public Component[] getComponents() {
  72             return new Component[0];
  73         }
  74     }
  75 }