/* * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ import java.io.Serializable; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.math.BigDecimal; import java.math.BigInteger; import java.net.InetAddress; import java.net.UnknownHostException; import java.rmi.Remote; import java.rmi.RemoteException; import java.time.LocalTime; import java.util.ArrayList; import java.util.Arrays; import java.util.EnumSet; import java.util.HashSet; import java.util.HashMap; import java.util.Objects; import java.util.PropertyPermission; import java.util.Set; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.LongAdder; import javax.naming.CommunicationException; import javax.naming.InitialContext; import javax.naming.Context; import javax.naming.NamingException; import javax.rmi.CORBA.Util; import javax.rmi.PortableRemoteObject; import org.omg.CORBA_2_3.ORB; import org.omg.CORBA_2_3.portable.OutputStream; import org.omg.CORBA_2_3.portable.InputStream; import jdk.test.lib.JDKToolFinder; import jdk.test.lib.JDKToolLauncher; import org.testng.Assert; import org.testng.annotations.AfterSuite; import org.testng.annotations.Test; import org.testng.annotations.DataProvider; import org.testng.TestNG; /* * @test * @library /test/lib * @build jdk.test.lib.* * @compile ObjectStreamTest.java ObjectStreamTest$_Echo_Stub.java ObjectStreamTest$_Server_Tie.java * @modules java.corba/com.sun.corba.se.impl.io java.base/java.io java.corba/com.sun.corba.se.impl.activation * @summary Tests of ReflectionFactory use in IIOP Serialization * @run testng/othervm * -Djava.naming.factory.initial=com.sun.jndi.cosnaming.CNCtxFactory * -Djava.naming.provider.url=iiop://localhost:1050 ObjectStreamTest * @run testng/othervm/policy=security.policy * -Djava.naming.factory.initial=com.sun.jndi.cosnaming.CNCtxFactory * -Djava.naming.provider.url=iiop://localhost:1050 ObjectStreamTest */ @Test public class ObjectStreamTest { enum Colors {RED, GREEN, YELLOW} static Set colorSet = new HashSet<>(); static { colorSet.add(Colors.RED); colorSet.add(Colors.GREEN); } /** * The process spawned to run orbd. */ static Process orbdProcess; static Thread orbThread; @DataProvider(name = "Objects") static Object[][] patterns() { BigInteger bigInteger = new BigInteger("8943892002309239"); InetAddress inetAddr; try { inetAddr = java.net.InetAddress.getByAddress(new byte[]{127, 0, 0, 1}); } catch (UnknownHostException ignored) { inetAddr = null; // ignored } HashMap hashMap = new HashMap<>(); hashMap.put("BigInteger", bigInteger); hashMap.put("InetAddress", inetAddr); hashMap.put("String", "bString"); Object[][] patterns = new Object[][]{ {"aString"}, {Integer.valueOf(5)}, {new SimpleObject(4, 4.0f)}, {Arrays.asList("a", "b", "c")}, {new String[]{"x", "y", "z"}}, {new ArrayList(1)}, // uses readObject/writeObject {new StringBuffer("abc")}, // Has serialPersistentFields {new StringBuilder("abc")}, {Colors.RED}, {inetAddr}, {LocalTime.MIDNIGHT}, // uses writeReplace/readResolve {new LongAdder()}, // uses writeReplace/readResolve {EnumSet.allOf(Colors.class)}, // used writeReplace/readResolve {bigInteger}, {new BigDecimal(bigInteger)}, {hashMap}, {new PropertyPermission("abc", "read")}, // has serialPersistentFields }; return patterns; } /** * Check ObjectStreamClass facts match between core serialization and CORBA. * * @param value */ @Test(dataProvider = "Objects") static void factCheck(Serializable value) { Class clazz = value.getClass(); java.io.ObjectStreamClass sOSC = java.io.ObjectStreamClass.lookup(clazz); java.io.ObjectStreamField[] sFields = sOSC.getFields(); com.sun.corba.se.impl.io.ObjectStreamClass cOSC = corbaLookup(clazz); com.sun.corba.se.impl.io.ObjectStreamField[] cFields = cOSC.getFields(); Assert.assertEquals(sFields.length, cFields.length, "Different number of fields"); for (int i = 0; i < sFields.length; i++) { Assert.assertEquals(sFields[i].getName(), cFields[i].getName(), "different field names " + cFields[i].getName()); Assert.assertEquals(sFields[i].getType(), cFields[i].getType(), "different field types " + cFields[i].getName()); Assert.assertEquals(sFields[i].getTypeString(), cFields[i].getTypeString(), "different field typestrings " + cFields[i].getName()); } Assert.assertEquals(baseMethod("hasReadObjectMethod", sOSC, (Class[]) null), corbaMethod("hasReadObject", cOSC, (Class[]) null), "hasReadObject: " + value.getClass()); Assert.assertEquals(baseMethod("hasWriteObjectMethod", sOSC, (Class[]) null), corbaMethod("hasWriteObject", cOSC, (Class[]) null), "hasWriteObject: " + value.getClass()); Assert.assertEquals(baseMethod("hasWriteReplaceMethod", sOSC, (Class[]) null), corbaMethod("hasWriteReplaceMethod", cOSC, (Class[]) null), "hasWriteReplace: " + value.getClass()); Assert.assertEquals(baseMethod("hasReadResolveMethod", sOSC, (Class[]) null), corbaMethod("hasReadResolveMethod", cOSC, (Class[]) null), "hasReadResolve: " + value.getClass()); Assert.assertEquals(baseMethod("getSerialVersionUID", sOSC, (Class[]) null), corbaMethod("getSerialVersionUID", cOSC, (Class[]) null), "getSerialVersionUID: " + value.getClass()); } /** * Test that objects written using Util.writeAny can be serialized * and deserialized using Util.readAny to equivalent objects. */ @Test(dataProvider = "Objects", enabled = true, dependsOnMethods = {"factCheck"}) static void WriteValueObjectStreamTest01(Serializable value) throws Exception { ORB orb = (ORB) ORB.init(new String[0], null); OutputStream out = (OutputStream) orb.create_output_stream(); Util.writeAny(out, value); InputStream in = (InputStream) out.create_input_stream(); Object actual = Util.readAny(in); checkEquals(actual, value); } /** * Test that objects can be echoed to a server and come back equivalent. */ @Test(dataProvider = "Objects", enabled = false, dependsOnMethods = {"factCheck"}) static void echoObjects(Serializable value) throws Exception { Context initialNamingContext = Server.init(); Echo echo = (Echo) PortableRemoteObject.narrow( initialNamingContext.lookup(Server.serverID), Echo.class); Object actual = echo.echo(value); checkEquals(actual, value); } /** * Check if the value and result are equals, with some tests depending on the type. * @param expected the expected value * @param actual the actual value */ static void checkEquals(Object actual, Object expected) { Class cl = expected.getClass(); Assert.assertEquals(actual.getClass(), cl, "type of value not equal to class of result"); try { if (cl.isArray() || !(cl.getDeclaredMethod("equals", cl) == null)) { Assert.assertEquals(actual, expected, "echo'd object not equal"); } else { Assert.assertEquals(toString(actual), toString(expected), "toString values not equal"); } } catch (NoSuchMethodException ex) { Assert.assertEquals(toString(actual), toString(expected), "toString values not equal"); } } /** * Convert an object to a String, and correctly for arrays. * @param obj an object * @return the tostring for the object. */ static String toString(Object obj) { return obj.getClass().isArray() ? Arrays.toString((Object[]) obj) : Objects.toString(obj); } /** * SimpleObject to test round trip. */ static class SimpleObject implements Serializable { private static final long serialVersionUID = 5217577841494640354L; private int i = 0; private float f = 0.0f; SimpleObject(int i, float f) { this.i = i; this.f = f; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; SimpleObject that = (SimpleObject) o; if (i != that.i) return false; return Float.compare(that.f, f) == 0; } @Override public int hashCode() { int result = i; result = 31 * result + (f != +0.0f ? Float.floatToIntBits(f) : 0); return result; } @Override public String toString() { return "SimpleObject{" + "i=" + i + ", f=" + f + '}'; } } /** * Lookup the CORBA ObjectStreamClass instance for a class. * @param clazz the class * @return the CORBA ObjectStreamClass instance for the class */ static com.sun.corba.se.impl.io.ObjectStreamClass corbaLookup(Class clazz) { Class oscClass = com.sun.corba.se.impl.io.ObjectStreamClass.class; try { Method meth = oscClass.getDeclaredMethod("lookup", Class.class); meth.setAccessible(true); return (com.sun.corba.se.impl.io.ObjectStreamClass) meth.invoke(null, clazz); } catch (NoSuchMethodException noMeth) { throw new RuntimeException("missing method", noMeth); } catch (IllegalAccessException | InvocationTargetException rex) { throw new RuntimeException("invocation failed", rex); } } /** * Lookup aand invoke method on a serializable object via the CORBA ObjectStreamClass. * @param methodName method name * @param osc CORBA ObjectStreamClass * @param argClasses method arguments * @return the value returned from invoking the method */ static Object corbaMethod(String methodName, com.sun.corba.se.impl.io.ObjectStreamClass osc, Class... argClasses) { Class oscClass = com.sun.corba.se.impl.io.ObjectStreamClass.class; try { Method meth = oscClass.getDeclaredMethod(methodName, argClasses); meth.setAccessible(true); return meth.invoke(osc); } catch (NoSuchMethodException noMeth) { throw new RuntimeException("missing method" + osc.getName() + "::" + methodName, noMeth); } catch (IllegalAccessException | InvocationTargetException rex) { throw new RuntimeException("invocation failed", rex); } } /** * Lookup aand invoke method on a serializable object via java.io.ObjectStreamClass. * @param methodName method name * @param osc java.io.ObjectStreamClass * @param argClasses method arguments * @return the value returned from invoking the method */ static Object baseMethod(String methodName, java.io.ObjectStreamClass osc, Class... argClasses) { Class oscClass = java.io.ObjectStreamClass.class; try { Method meth = oscClass.getDeclaredMethod(methodName, argClasses); meth.setAccessible(true); return meth.invoke(osc); } catch (NoSuchMethodException noMeth) { throw new RuntimeException("missing method: " + osc.getName() + "::" + methodName, noMeth); } catch (IllegalAccessException | InvocationTargetException rex) { throw new RuntimeException("invocation failed", rex); } } /** * Simple echo interface to check serialization/deserialization. */ interface Echo extends Remote { Object echo(Object obj) throws RemoteException; } static class Server extends PortableRemoteObject implements Echo { public static final String serverID = "ObjectStreamTestServer"; private static Context initialNamingContext; private static Server server; public Server() throws RemoteException { super(); } public Object echo(Object obj) { return obj; } public static Context init() { if (initialNamingContext == null) { try { startOrbd(); Thread.sleep(5000L); // Give it 5 seconds } catch (Exception eex) { throw new RuntimeException("Orbd", eex); } for (int i = 0; i < 1; i++) { try { Thread.sleep(1L); initialNamingContext = new InitialContext(); server = new Server(); initialNamingContext.rebind(serverID, server); } catch (CommunicationException | InterruptedException cex) { System.out.printf("retry #%d sec: ex: %s%n", i, cex); } catch (NamingException ex) { throw new RuntimeException("can't initialize naming context", ex); } catch (RemoteException rex) { throw new RuntimeException("can't initialize server", rex); } } } if (initialNamingContext == null) { Assert.fail("Can't initialize the Orb, no naming context"); } return initialNamingContext; } } static void startOrbd() throws Exception { System.out.println("\nStarting orbd with NS port 1050 "); JDKToolLauncher orbdLauncher = JDKToolLauncher.create("orbd") .addToolArg("-ORBInitialHost").addToolArg("localhost") .addToolArg("-ORBInitialPort").addToolArg("1050"); System.out.println("ObjectStreamTest: Executing: " + Arrays.asList(orbdLauncher.getCommand())); ProcessBuilder pb = new ProcessBuilder(orbdLauncher.getCommand()); pb.redirectError(ProcessBuilder.Redirect.INHERIT); orbdProcess = pb.start(); } @AfterSuite static void killOrbd() throws Exception { if (orbdProcess != null) { orbdProcess.destroyForcibly(); orbdProcess.waitFor(); System.out.printf("destroyed orbd, pid: %d, exitValue: %d%n", orbdProcess.getPid(), orbdProcess.exitValue()); } } // Main can be used to run the tests from the command line with only testng.jar. @SuppressWarnings("raw_types") @Test(enabled = false) public static void main(String[] args) { Class[] testclass = {ObjectStreamTest.class}; TestNG testng = new TestNG(); testng.setTestClasses(testclass); testng.run(); } }