1 /*
   2  * Copyright (c) 2000, 2010, 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 /* @test
  25  * @summary Verifies basic correct functioning of proxy serialization.
  26  * @key randomness
  27  */
  28 
  29 import java.io.*;
  30 import java.lang.reflect.*;
  31 import java.util.Random;
  32 
  33 // proxy interfaces
  34 interface Foo { int foo(); }
  35 interface Bar { float bar(); }
  36 
  37 // dummy invocation handler
  38 class Handler implements InvocationHandler, Serializable {
  39 
  40     static Method fooMethod, barMethod;
  41     static {
  42         try {
  43             fooMethod = Foo.class.getDeclaredMethod("foo", new Class[0]);
  44             barMethod = Bar.class.getDeclaredMethod("bar", new Class[0]);
  45         } catch (NoSuchMethodException ex) {
  46             throw new Error();
  47         }
  48     }
  49 
  50     int foo;
  51     float bar;
  52 
  53     Handler(int foo, float bar) {
  54         this.foo = foo;
  55         this.bar = bar;
  56     }
  57 
  58     public Object invoke(Object proxy, Method method, Object[] args)
  59         throws Throwable
  60     {
  61         if (method.equals(fooMethod)) {
  62             return new Integer(foo);
  63         } else if (method.equals(barMethod)) {
  64             return new Float(bar);
  65         } else {
  66             throw new UnsupportedOperationException();
  67         }
  68     }
  69 }
  70 
  71 // ObjectInputStream incapable of resolving proxy classes
  72 class ProxyBlindInputStream extends ObjectInputStream {
  73 
  74     ProxyBlindInputStream(InputStream in) throws IOException { super(in); }
  75 
  76     protected Class resolveProxyClass(String[] interfaces)
  77         throws IOException, ClassNotFoundException
  78     {
  79         throw new ClassNotFoundException();
  80     }
  81 }
  82 
  83 public class Basic {
  84     public static void main(String[] args) throws Exception {
  85         ClassLoader loader = Basic.class.getClassLoader();
  86         Class[] interfaces = new Class[] { Foo.class, Bar.class };
  87         Random rand = new Random();
  88         int foo = rand.nextInt();
  89         float bar = rand.nextFloat();
  90         ObjectOutputStream oout;
  91         ObjectInputStream oin;
  92         ByteArrayOutputStream bout;
  93         Object proxy;
  94 
  95 
  96         // test simple proxy write + read
  97         bout = new ByteArrayOutputStream();
  98         oout = new ObjectOutputStream(bout);
  99         oout.writeObject(Proxy.newProxyInstance(
 100             loader, interfaces, new Handler(foo, bar)));
 101         oout.close();
 102 
 103         oin = new ObjectInputStream(
 104             new ByteArrayInputStream(bout.toByteArray()));
 105         proxy = oin.readObject();
 106         if ((((Foo) proxy).foo() != foo) || (((Bar) proxy).bar() != bar)) {
 107             throw new Error();
 108         }
 109 
 110 
 111         // test missing proxy class ClassNotFoundException
 112         oin = new ProxyBlindInputStream(
 113             new ByteArrayInputStream(bout.toByteArray()));
 114         try {
 115             oin.readObject();
 116             throw new Error();
 117         } catch (ClassNotFoundException ex) {
 118         }
 119     }
 120 }