Search in sources :

Example 11 with ObjectInputFilter

use of sun.misc.ObjectInputFilter in project jdk8u_jdk by JetBrains.

the class SerialFilterTest method nonResettableFilter.

/**
     * Test that the filter on a OIS can be set only on a fresh OIS,
     * before deserializing any objects.
     * This test is agnostic the global filter being set or not.
     */
@Test
static void nonResettableFilter() {
    Validator validator1 = new Validator();
    Validator validator2 = new Validator();
    try {
        // an object
        byte[] bytes = writeObjects("text1");
        try (ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
            ObjectInputStream ois = new ObjectInputStream(bais)) {
            // Check the initial filter is the global filter; may be null
            ObjectInputFilter global = ObjectInputFilter.Config.getSerialFilter();
            ObjectInputFilter initial = ObjectInputFilter.Config.getObjectInputFilter(ois);
            Assert.assertEquals(global, initial, "initial filter should be the global filter");
            // Check if it can be set to null
            ObjectInputFilter.Config.setObjectInputFilter(ois, null);
            ObjectInputFilter filter = ObjectInputFilter.Config.getObjectInputFilter(ois);
            Assert.assertNull(filter, "set to null should be null");
            ObjectInputFilter.Config.setObjectInputFilter(ois, validator1);
            Object o = ois.readObject();
            try {
                ObjectInputFilter.Config.setObjectInputFilter(ois, validator2);
                Assert.fail("Should not be able to set filter twice");
            } catch (IllegalStateException ise) {
            // success, the exception was expected
            }
        } catch (EOFException eof) {
            Assert.fail("Should not reach end-of-file", eof);
        } catch (ClassNotFoundException cnf) {
            Assert.fail("Deserializing", cnf);
        }
    } catch (IOException ex) {
        Assert.fail("Unexpected IOException", ex);
    }
}
Also used : ObjectInputFilter(sun.misc.ObjectInputFilter) ByteArrayInputStream(java.io.ByteArrayInputStream) EOFException(java.io.EOFException) IOException(java.io.IOException) ObjectInputStream(java.io.ObjectInputStream) Test(org.testng.annotations.Test)

Aggregations

ObjectInputFilter (sun.misc.ObjectInputFilter)11 Test (org.testng.annotations.Test)8 ByteArrayInputStream (java.io.ByteArrayInputStream)4 InvalidClassException (java.io.InvalidClassException)4 ObjectInputStream (java.io.ObjectInputStream)4 IOException (java.io.IOException)2 EOFException (java.io.EOFException)1 SerializablePermission (java.io.SerializablePermission)1 Log (sun.rmi.runtime.Log)1