J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 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 4460983 |
| 26 | * @summary This test verifies that when a RemoteObject is being |
| 27 | * deserialized, if the "external ref type name" is a non-empty |
| 28 | * string that is not equal to one of the external ref type names |
| 29 | * required to be supported by the specification, then a |
| 30 | * ClassNotFoundException is thrown. Of particular note, with the |
| 31 | * specification change for 4460983, "ActivatableServerRef" is no |
| 32 | * longer a supported "external ref type name", and the names of |
| 33 | * other classes in the internal package sun.rmi.server should |
| 34 | * produce the same result. |
| 35 | * See also test/java/rmi/activation/Activatable/notSerializable. |
| 36 | * @author Peter Jones |
| 37 | * |
| 38 | * @run main/othervm UnrecognizedRefType |
| 39 | */ |
| 40 | |
| 41 | import java.io.ByteArrayInputStream; |
| 42 | import java.io.ByteArrayOutputStream; |
| 43 | import java.io.ObjectInput; |
| 44 | import java.io.ObjectInputStream; |
| 45 | import java.io.ObjectOutput; |
| 46 | import java.io.ObjectOutputStream; |
| 47 | import java.lang.reflect.Method; |
| 48 | import java.rmi.Remote; |
| 49 | import java.rmi.server.Operation; |
| 50 | import java.rmi.server.RemoteCall; |
| 51 | import java.rmi.server.RemoteObject; |
| 52 | import java.rmi.server.RemoteRef; |
| 53 | |
| 54 | public class UnrecognizedRefType { |
| 55 | public static void main(String[] args) throws Exception { |
| 56 | System.err.println("\nRegression test for bug 4460983\n"); |
| 57 | |
| 58 | test(new FakeRemoteObject("ActivatableServerRef")); |
| 59 | test(new FakeRemoteObject("MarshalInputStream")); |
| 60 | test(new FakeRemoteObject("XXX")); |
| 61 | |
| 62 | System.err.println("TEST PASSED"); |
| 63 | } |
| 64 | |
| 65 | private static void test(RemoteObject obj) throws Exception { |
| 66 | ByteArrayOutputStream bout = new ByteArrayOutputStream(); |
| 67 | ObjectOutputStream out = new ObjectOutputStream(bout); |
| 68 | out.writeObject(obj); |
| 69 | ByteArrayInputStream bin = |
| 70 | new ByteArrayInputStream(bout.toByteArray()); |
| 71 | ObjectInputStream in = new ObjectInputStream(bin); |
| 72 | try { |
| 73 | Object obj2 = in.readObject(); |
| 74 | System.err.println( |
| 75 | "Object unexpectedly deserialized successfully: " + obj2); |
| 76 | throw new RuntimeException( |
| 77 | "TEST FAILED: object successfully deserialized"); |
| 78 | } catch (ClassNotFoundException e) { |
| 79 | System.err.println("ClassNotFoundException as expected:"); |
| 80 | e.printStackTrace(); |
| 81 | } // other exceptions cause test failure |
| 82 | } |
| 83 | |
| 84 | private static class FakeRemoteObject extends RemoteObject { |
| 85 | FakeRemoteObject(String refType) { |
| 86 | super(new FakeRemoteRef(refType)); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | private static class FakeRemoteRef implements RemoteRef { |
| 91 | private final String refType; |
| 92 | |
| 93 | FakeRemoteRef(String refType) { |
| 94 | this.refType = refType; |
| 95 | } |
| 96 | |
| 97 | public Object invoke(Remote obj, |
| 98 | Method method, |
| 99 | Object[] params, |
| 100 | long opnum) |
| 101 | { |
| 102 | throw new UnsupportedOperationException(); |
| 103 | } |
| 104 | |
| 105 | public RemoteCall newCall(RemoteObject obj, |
| 106 | Operation[] op, |
| 107 | int opnum, |
| 108 | long hash) |
| 109 | { |
| 110 | throw new UnsupportedOperationException(); |
| 111 | } |
| 112 | |
| 113 | public void invoke(RemoteCall call) { |
| 114 | throw new UnsupportedOperationException(); |
| 115 | } |
| 116 | |
| 117 | public void done(RemoteCall call) { |
| 118 | throw new UnsupportedOperationException(); |
| 119 | } |
| 120 | |
| 121 | public String getRefClass(java.io.ObjectOutput out) { |
| 122 | return refType; |
| 123 | } |
| 124 | |
| 125 | public int remoteHashCode() { return hashCode(); } |
| 126 | public boolean remoteEquals(RemoteRef obj) { return equals(obj); } |
| 127 | public String remoteToString() { return toString(); } |
| 128 | |
| 129 | public void readExternal(ObjectInput in) { |
| 130 | throw new UnsupportedOperationException(); |
| 131 | } |
| 132 | |
| 133 | public void writeExternal(ObjectOutput out) { |
| 134 | // no data to write |
| 135 | } |
| 136 | } |
| 137 | } |