1 /*
   2  * Copyright (c) 1998, 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 /*
  25  * @test
  26  * @bug 4100915
  27  * @summary Verify that [write/read]ObjectOverride methods get called.
  28  *          Test verifies that ALL methods to write an object can
  29  *          be overridden. Howver, the testing for reading an object
  30  *          is incomplete. Only test that readObjectOverride is called.
  31  *          An entire protocol would need to be implemented and written
  32  *          out before being able to test the input side of the API.
  33  *
  34  *          Also, would be appropriate that this program verify
  35  *          that if SerializablePermission "enableSubclassImplamentation"
  36  *          is not in the security policy and security is enabled, that
  37  *          a security excepiton is thrown when constructing the
  38  *          ObjectOutputStream subclass.
  39  *
  40  *
  41  * @compile AbstractObjectInputStream.java AbstractObjectOutputStream.java XObjectInputStream.java XObjectOutputStream.java Test.java
  42  * @run main  Test
  43  */
  44 
  45 import java.io.*;
  46 
  47 /**
  48  * Test if customized readObject and writeObject are called.
  49  */
  50 class B implements Serializable {
  51     public int publicIntField;
  52     public static int numWriteObjectCalled = 0;
  53     B(int v) {
  54         publicIntField = v;
  55     }
  56     private void writeObject(ObjectOutputStream os) throws IOException {
  57         numWriteObjectCalled++;
  58         os.defaultWriteObject();
  59     }
  60 
  61     private void readObject(ObjectInputStream is)
  62         throws IOException, ClassNotFoundException
  63     {
  64         is.defaultReadObject();
  65     }
  66 
  67 };
  68 
  69 /**
  70  * Test PutFields interface.
  71  */
  72 
  73 class C implements Serializable {
  74     public int xx1;
  75     public int xx2;
  76     static final ObjectStreamField[] serialPersistentFields = {
  77         new ObjectStreamField("x1", Integer.TYPE),
  78         new ObjectStreamField("x2", Integer.TYPE),
  79         new ObjectStreamField("x3", Integer.TYPE),
  80         new ObjectStreamField("x4", Integer.TYPE)
  81     };
  82     C() {
  83         xx1 = 300;
  84         xx2 = 400;
  85     }
  86 
  87     private void writeObject(ObjectOutputStream os) throws IOException {
  88         ObjectOutputStream.PutField putFields = os.putFields();
  89         putFields.put("x1", xx1);
  90         putFields.put("x2", xx2);
  91         putFields.put("x3", xx1 * 2);
  92         putFields.put("x4", xx2 * 2);
  93         os.writeFields();
  94     }
  95 
  96 };
  97 
  98 
  99 class A implements Serializable {
 100     public int  publicIntField;
 101     public long publicLongField;
 102     public B    publicBField;
 103     public B[]  publicBArray = { new B(4), new B(6)};
 104     public C    publicCField;
 105 
 106     public A() {
 107         publicIntField = 3;
 108         publicLongField = 10L;
 109         publicBField = new B(5);
 110         publicCField = new C();
 111     }
 112 };
 113 
 114 public class Test {
 115     public static void main(String argv[])
 116         throws IOException, ClassNotFoundException
 117     {
 118         boolean expectSecurityException = false;
 119 
 120         if (argv.length > 0 &&
 121             argv[0].compareTo("-expectSecurityException") == 0)
 122             expectSecurityException = true;
 123 
 124         ByteArrayOutputStream baos = new ByteArrayOutputStream(20);
 125         XObjectOutputStream os = null;
 126         try {
 127             os = new XObjectOutputStream(baos);
 128             if (expectSecurityException)
 129                 throw new Error("Assertion failure. " +
 130                                 "Expected a security exception on previous line.");
 131         } catch (SecurityException e) {
 132             if (expectSecurityException)
 133                 return;
 134             else
 135                 throw e;
 136         }
 137         os.writeObject(new A());
 138         os.close();
 139         if (B.numWriteObjectCalled != 3)
 140             throw new Error("Expected B.writeObject() to be called 3 times;" +
 141                             " observed only " + B.numWriteObjectCalled + " times");
 142 
 143         XObjectInputStream is =
 144             new XObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
 145         try {
 146             A a = (A)is.readObject();
 147             throw new Error("Expected readObjectOverride() to be called and throw IOException(not implemented)");
 148         } catch (IOException e) {
 149         }
 150         is.close();
 151     }
 152 };