1 /*
   2  * Copyright 2003 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  20  * CA 95054 USA or visit www.sun.com if you need additional information or
  21  * have any questions.
  22  */
  23 
  24 /* @test
  25  * @bug 4838379
  26  * @summary Verify that TC_OBJECT followed by a class descriptor for an enum
  27  *          class results in an InvalidClassException, as does TC_ENUM followed
  28  *          by a class descriptor for a non-enum class.
  29  */
  30 
  31 import java.io.*;
  32 
  33 enum Foo { bar }
  34 
  35 class TestObjectOutputStream extends ObjectOutputStream {
  36 
  37     static ObjectStreamClass fooDesc = ObjectStreamClass.lookup(Foo.class);
  38     static ObjectStreamClass integerDesc =
  39         ObjectStreamClass.lookup(Integer.class);
  40 
  41     TestObjectOutputStream(OutputStream out) throws IOException {
  42         super(out);
  43     }
  44 
  45     protected void writeClassDescriptor(ObjectStreamClass desc)
  46         throws IOException
  47     {
  48         if (desc == fooDesc) {
  49             super.writeClassDescriptor(integerDesc);
  50         } else if (desc == integerDesc) {
  51             super.writeClassDescriptor(fooDesc);
  52         } else {
  53             super.writeClassDescriptor(desc);
  54         }
  55     }
  56 }
  57 
  58 public class Test {
  59     public static void main(String[] args) throws Exception {
  60         ByteArrayOutputStream bout = new ByteArrayOutputStream();
  61         ObjectOutputStream oout = new TestObjectOutputStream(bout);
  62         oout.writeObject(Foo.bar);
  63         oout.close();
  64         ObjectInputStream oin = new ObjectInputStream(
  65             new ByteArrayInputStream(bout.toByteArray()));
  66         try {
  67             Object obj = oin.readObject();
  68             throw new Error("read of " + obj + " should not have succeeded");
  69         } catch (InvalidClassException e) {
  70             System.out.println("caught expected exception " + e);
  71         }
  72 
  73         oout = new TestObjectOutputStream(bout = new ByteArrayOutputStream());
  74         oout.writeObject(new Integer(5));
  75         oout.close();
  76         oin = new ObjectInputStream(
  77             new ByteArrayInputStream(bout.toByteArray()));
  78         try {
  79             Object obj = oin.readObject();
  80             throw new Error("read of " + obj + " should not have succeeded");
  81         } catch (InvalidClassException e) {
  82             System.out.println("caught expected exception " + e);
  83         }
  84     }
  85 }