Search in sources :

Example 21 with InvalidClassException

use of java.io.InvalidClassException in project jdk8u_jdk by JetBrains.

the class CheckInputOrderTest method testRejectedInGlobal.

/**
     * Test:
     *   "global filter reject" + "specific ObjectInputStream filter is empty" => should reject
     *   "global filter reject" + "specific ObjectInputStream filter allow"    => should allow
     */
@Test(dataProvider = "Patterns")
public void testRejectedInGlobal(Object toDeserialized, String pattern, boolean allowed) throws Exception {
    byte[] bytes = SerialFilterTest.writeObjects(toDeserialized);
    ObjectInputFilter filter = ObjectInputFilter.Config.createFilter(pattern);
    try (ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
        ObjectInputStream ois = new ObjectInputStream(bais)) {
        ObjectInputFilter.Config.setObjectInputFilter(ois, filter);
        Object o = ois.readObject();
        assertTrue(allowed, "filter should have thrown an exception");
    } catch (InvalidClassException ice) {
        assertFalse(allowed, "filter should have thrown an exception");
    }
}
Also used : ObjectInputFilter(sun.misc.ObjectInputFilter) ByteArrayInputStream(java.io.ByteArrayInputStream) InvalidClassException(java.io.InvalidClassException) ObjectInputStream(java.io.ObjectInputStream) Test(org.testng.annotations.Test)

Example 22 with InvalidClassException

use of java.io.InvalidClassException in project jdk8u_jdk by JetBrains.

the class MixedFiltersTest method testAllowedInGlobal.

/**
     * Test:
     *   "global filter allow" + "specific ObjectInputStream filter is empty" => should allow
     *   "global filter allow" + "specific ObjectInputStream filter reject"   => should reject
     */
@Test(dataProvider = "AllowedInGlobal")
public void testAllowedInGlobal(Object toDeserialized, String pattern) throws Exception {
    byte[] bytes = SerialFilterTest.writeObjects(toDeserialized);
    try (ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
        ObjectInputStream ois = new ObjectInputStream(bais)) {
        Object o = ois.readObject();
    }
    ObjectInputFilter filter = ObjectInputFilter.Config.createFilter(pattern);
    try (ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
        ObjectInputStream ois = new ObjectInputStream(bais)) {
        ObjectInputFilter.Config.setObjectInputFilter(ois, filter);
        Object o = ois.readObject();
        assertTrue(false, "filter should have thrown an exception");
    } catch (InvalidClassException expected) {
    }
}
Also used : ObjectInputFilter(sun.misc.ObjectInputFilter) ByteArrayInputStream(java.io.ByteArrayInputStream) InvalidClassException(java.io.InvalidClassException) ObjectInputStream(java.io.ObjectInputStream) Test(org.testng.annotations.Test)

Example 23 with InvalidClassException

use of java.io.InvalidClassException in project jdk8u_jdk by JetBrains.

the class XPathExceptionInitCause method main.

public static void main(String[] args) throws Exception {
    Throwable cause = new Throwable("message 1");
    XPathException xpathexcep = new XPathException("message 2");
    //Test XPE initCause() method
    xpathexcep.initCause(cause);
    System.out.println("getCause() result: '" + xpathexcep.getCause() + "' Cause itself: '" + cause + "'");
    if (!xpathexcep.getCause().toString().equals(cause.toString())) {
        throw new Exception("Incorrect cause is set by initCause()");
    }
    //Test serialization/deserialization of initialized XPE
    byte[] xpeserial;
    XPathException xpedeser;
    xpeserial = pickleXPE(xpathexcep);
    xpedeser = unpickleXPE(xpeserial);
    System.out.println("Serialized XPE: message='" + xpathexcep.getMessage() + "' cause='" + xpathexcep.getCause().toString() + "'");
    System.out.println("Deserialized XPE: message='" + xpedeser.getMessage() + "' cause='" + xpedeser.getCause().toString() + "'");
    if (xpedeser.getCause() == null || !xpedeser.getCause().toString().equals(cause.toString()) || !xpedeser.getMessage().toString().equals("message 2"))
        throw new Exception("XPathException incorrectly serialized/deserialized");
    //Test serialization/deserialization of uninitialized cause in XPE
    XPathException xpeuninit = new XPathException("uninitialized cause");
    xpeserial = pickleXPE(xpeuninit);
    xpedeser = unpickleXPE(xpeserial);
    System.out.println("Serialized XPE: message='" + xpeuninit.getMessage() + "' cause='" + xpeuninit.getCause() + "'");
    System.out.println("Deserialized XPE: message='" + xpedeser.getMessage() + "' cause='" + xpedeser.getCause() + "'");
    if (xpedeser.getCause() != null || !xpedeser.getMessage().toString().equals("uninitialized cause"))
        throw new Exception("XPathException incorrectly serialized/deserialized");
    //Test deserialization of normal XPathException serialized by JDK7
    XPathException xpejdk7 = unpickleXPE(NORMALJDK7SER);
    if (xpejdk7 == null || xpejdk7.getCause() == null || !xpejdk7.getMessage().equals("message 2") || !xpejdk7.getCause().getMessage().equals("message 1"))
        throw new Exception("XpathException serialized by JDK7 was " + "incorrectly deserialized.");
    // new XPathException(new Exception()).initCause(null)
    try {
        xpejdk7 = unpickleXPE(TWOCAUSES);
        throw new Exception("Expected InvalidClassException but it wasn't" + " observed");
    } catch (InvalidClassException e) {
        System.out.println("InvalidClassException caught as expected.");
    }
}
Also used : XPathException(javax.xml.xpath.XPathException) InvalidClassException(java.io.InvalidClassException) InvalidClassException(java.io.InvalidClassException) IOException(java.io.IOException) XPathException(javax.xml.xpath.XPathException)

Example 24 with InvalidClassException

use of java.io.InvalidClassException in project midpoint by Evolveum.

the class PropertyComplexValueFilterType method copyOf.

/**
     * Creates and returns a deep copy of a given {@code Serializable}.
     * 
     * @param serializable
     *     The instance to copy or {@code null}.
     * @return
     *     A deep copy of {@code serializable} or {@code null} if {@code serializable} is {@code null}.
     */
private static Serializable copyOf(final Serializable serializable) {
    // CC-XJC Version 2.0 Build 2011-09-16T18:27:24+0000
    if (serializable != null) {
        try {
            final ByteArrayOutputStream byteArrayOutput = new ByteArrayOutputStream();
            final ObjectOutputStream out = new ObjectOutputStream(byteArrayOutput);
            out.writeObject(serializable);
            out.close();
            final ByteArrayInputStream byteArrayInput = new ByteArrayInputStream(byteArrayOutput.toByteArray());
            final ObjectInputStream in = new ObjectInputStream(byteArrayInput);
            final Serializable copy = ((Serializable) in.readObject());
            in.close();
            return copy;
        } catch (SecurityException e) {
            throw ((AssertionError) new AssertionError((("Unexpected instance during copying object '" + serializable) + "'.")).initCause(e));
        } catch (ClassNotFoundException e) {
            throw ((AssertionError) new AssertionError((("Unexpected instance during copying object '" + serializable) + "'.")).initCause(e));
        } catch (InvalidClassException e) {
            throw ((AssertionError) new AssertionError((("Unexpected instance during copying object '" + serializable) + "'.")).initCause(e));
        } catch (NotSerializableException e) {
            throw ((AssertionError) new AssertionError((("Unexpected instance during copying object '" + serializable) + "'.")).initCause(e));
        } catch (StreamCorruptedException e) {
            throw ((AssertionError) new AssertionError((("Unexpected instance during copying object '" + serializable) + "'.")).initCause(e));
        } catch (OptionalDataException e) {
            throw ((AssertionError) new AssertionError((("Unexpected instance during copying object '" + serializable) + "'.")).initCause(e));
        } catch (IOException e) {
            throw ((AssertionError) new AssertionError((("Unexpected instance during copying object '" + serializable) + "'.")).initCause(e));
        }
    }
    return null;
}
Also used : Serializable(java.io.Serializable) InvalidClassException(java.io.InvalidClassException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) OptionalDataException(java.io.OptionalDataException) NotSerializableException(java.io.NotSerializableException) ByteArrayInputStream(java.io.ByteArrayInputStream) StreamCorruptedException(java.io.StreamCorruptedException) ObjectInputStream(java.io.ObjectInputStream)

Example 25 with InvalidClassException

use of java.io.InvalidClassException in project midpoint by Evolveum.

the class QueryType method copyOf.

/**
     * Creates and returns a deep copy of a given {@code Serializable}.
     * 
     * @param serializable
     *     The instance to copy or {@code null}.
     * @return
     *     A deep copy of {@code serializable} or {@code null} if {@code serializable} is {@code null}.
     */
private static Serializable copyOf(final Serializable serializable) {
    // CC-XJC Version 2.0 Build 2011-09-16T18:27:24+0000
    if (serializable != null) {
        try {
            final ByteArrayOutputStream byteArrayOutput = new ByteArrayOutputStream();
            final ObjectOutputStream out = new ObjectOutputStream(byteArrayOutput);
            out.writeObject(serializable);
            out.close();
            final ByteArrayInputStream byteArrayInput = new ByteArrayInputStream(byteArrayOutput.toByteArray());
            final ObjectInputStream in = new ObjectInputStream(byteArrayInput);
            final Serializable copy = ((Serializable) in.readObject());
            in.close();
            return copy;
        } catch (SecurityException e) {
            throw ((AssertionError) new AssertionError((("Unexpected instance during copying object '" + serializable) + "'.")).initCause(e));
        } catch (ClassNotFoundException e) {
            throw ((AssertionError) new AssertionError((("Unexpected instance during copying object '" + serializable) + "'.")).initCause(e));
        } catch (InvalidClassException e) {
            throw ((AssertionError) new AssertionError((("Unexpected instance during copying object '" + serializable) + "'.")).initCause(e));
        } catch (NotSerializableException e) {
            throw ((AssertionError) new AssertionError((("Unexpected instance during copying object '" + serializable) + "'.")).initCause(e));
        } catch (StreamCorruptedException e) {
            throw ((AssertionError) new AssertionError((("Unexpected instance during copying object '" + serializable) + "'.")).initCause(e));
        } catch (OptionalDataException e) {
            throw ((AssertionError) new AssertionError((("Unexpected instance during copying object '" + serializable) + "'.")).initCause(e));
        } catch (IOException e) {
            throw ((AssertionError) new AssertionError((("Unexpected instance during copying object '" + serializable) + "'.")).initCause(e));
        }
    }
    return null;
}
Also used : Serializable(java.io.Serializable) InvalidClassException(java.io.InvalidClassException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) OptionalDataException(java.io.OptionalDataException) NotSerializableException(java.io.NotSerializableException) ByteArrayInputStream(java.io.ByteArrayInputStream) StreamCorruptedException(java.io.StreamCorruptedException) ObjectInputStream(java.io.ObjectInputStream)

Aggregations

InvalidClassException (java.io.InvalidClassException)26 ObjectInputStream (java.io.ObjectInputStream)10 ByteArrayInputStream (java.io.ByteArrayInputStream)9 IOException (java.io.IOException)9 BindingConfigParseException (org.openhab.model.item.binding.BindingConfigParseException)6 StreamCorruptedException (java.io.StreamCorruptedException)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 NotSerializableException (java.io.NotSerializableException)4 ObjectOutputStream (java.io.ObjectOutputStream)4 OptionalDataException (java.io.OptionalDataException)4 Serializable (java.io.Serializable)4 UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)4 Server (org.compiere.interfaces.Server)4 ObjectInputFilter (sun.misc.ObjectInputFilter)4 Test (org.testng.annotations.Test)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 UUID (java.util.UUID)2 EOFException (java.io.EOFException)1 FileInputStream (java.io.FileInputStream)1