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);
}
}
Aggregations