|
18 | 18 |
|
19 | 19 | import java.io.IOException;
|
20 | 20 | import java.io.InvalidClassException;
|
| 21 | +import java.io.NotSerializableException; |
21 | 22 | import java.io.ObjectStreamClass;
|
22 | 23 | import java.io.ObjectStreamField;
|
23 | 24 | import java.io.Serializable;
|
@@ -49,6 +50,97 @@ static class FieldMadeTransient implements Serializable {
|
49 | 50 | private int nonTransientInt;
|
50 | 51 | }
|
51 | 52 |
|
| 53 | + public void testSerializeFieldMadeStatic() throws Exception { |
| 54 | + // Does ObjectStreamClass have the right idea? |
| 55 | + ObjectStreamClass osc = ObjectStreamClass.lookup(FieldMadeStatic.class); |
| 56 | + ObjectStreamField[] fields = osc.getFields(); |
| 57 | + assertEquals(0, fields.length); |
| 58 | + |
| 59 | + // This was created by serializing a FieldMadeStatic with a non-static staticInt |
| 60 | + String s = "aced0005737200316c6962636f72652e6a6176612e696f2e53657269616c697a6174696f6e54657" |
| 61 | + + "374244669656c644d6164655374617469630000000000000000020001490009737461746963496e7" |
| 62 | + + "47870000022b8"; |
| 63 | + FieldMadeStatic deserialized = (FieldMadeStatic) SerializationTester.deserializeHex(s); |
| 64 | + // The field data is simply ignored if it is static. |
| 65 | + assertEquals(9999, deserialized.staticInt); |
| 66 | + } |
| 67 | + |
| 68 | + static class FieldMadeStatic implements Serializable { |
| 69 | + private static final long serialVersionUID = 0L; |
| 70 | + // private int staticInt = 8888; |
| 71 | + private static int staticInt = 9999; |
| 72 | + } |
| 73 | + |
| 74 | + // We can serialize an object that has an unserializable field providing it is null. |
| 75 | + public void testDeserializeNullUnserializableField() throws Exception { |
| 76 | + // This was created by creating a new SerializableContainer and not setting the |
| 77 | + // unserializable field. A canned serialized form is used so we can tell if the static |
| 78 | + // initializers were executed during deserialization. |
| 79 | + // SerializationTester.serializeHex(new SerializableContainer()); |
| 80 | + String s = "aced0005737200376c6962636f72652e6a6176612e696f2e53657269616c697a6174696f6e54657" |
| 81 | + + "3742453657269616c697a61626c65436f6e7461696e657200000000000000000200014c000e756e7" |
| 82 | + + "3657269616c697a61626c657400334c6c6962636f72652f6a6176612f696f2f53657269616c697a6" |
| 83 | + + "174696f6e546573742457617353657269616c697a61626c653b787070"; |
| 84 | + |
| 85 | + serializableContainerInitializedFlag = false; |
| 86 | + wasSerializableInitializedFlag = false; |
| 87 | + |
| 88 | + SerializableContainer sc = (SerializableContainer) SerializationTester.deserializeHex(s); |
| 89 | + assertNull(sc.unserializable); |
| 90 | + |
| 91 | + // Confirm the container was initialized, but the class for the null field was not. |
| 92 | + assertTrue(serializableContainerInitializedFlag); |
| 93 | + assertFalse(wasSerializableInitializedFlag); |
| 94 | + } |
| 95 | + |
| 96 | + public static boolean serializableContainerInitializedFlag = false; |
| 97 | + |
| 98 | + static class SerializableContainer implements Serializable { |
| 99 | + private static final long serialVersionUID = 0L; |
| 100 | + private Object unserializable = null; |
| 101 | + |
| 102 | + static { |
| 103 | + serializableContainerInitializedFlag = true; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + // We must not serialize an object that has a non-null unserializable field. |
| 108 | + public void testSerializeUnserializableField() throws Exception { |
| 109 | + SerializableContainer sc = new SerializableContainer(); |
| 110 | + sc.unserializable = new WasSerializable(); |
| 111 | + try { |
| 112 | + SerializationTester.serializeHex(sc); |
| 113 | + fail(); |
| 114 | + } catch (NotSerializableException expected) { |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + // It must not be possible to deserialize an object if a field is no longer serializable. |
| 119 | + public void testDeserializeUnserializableField() throws Exception { |
| 120 | + // This was generated by creating a SerializableContainer and setting the unserializable |
| 121 | + // field to a WasSerializable when it was still Serializable. A canned serialized form is |
| 122 | + // used so we can tell if the static initializers were executed during deserialization. |
| 123 | + // SerializableContainer sc = new SerializableContainer(); |
| 124 | + // sc.unserializable = new WasSerializable(); |
| 125 | + // SerializationTester.serializeHex(sc); |
| 126 | + String s = "aced0005737200376c6962636f72652e6a6176612e696f2e53657269616c697a6174696f6e54657" |
| 127 | + + "3742453657269616c697a61626c65436f6e7461696e657200000000000000000200014c000e756e7" |
| 128 | + + "3657269616c697a61626c657400124c6a6176612f6c616e672f4f626a6563743b7870737200316c6" |
| 129 | + + "962636f72652e6a6176612e696f2e53657269616c697a6174696f6e5465737424576173536572696" |
| 130 | + + "16c697a61626c65000000000000000002000149000169787000000000"; |
| 131 | + |
| 132 | + serializableContainerInitializedFlag = false; |
| 133 | + wasSerializableInitializedFlag = false; |
| 134 | + try { |
| 135 | + SerializationTester.deserializeHex(s); |
| 136 | + fail(); |
| 137 | + } catch (InvalidClassException expected) { |
| 138 | + } |
| 139 | + // Confirm neither the container nor the contained class was initialized. |
| 140 | + assertFalse(serializableContainerInitializedFlag); |
| 141 | + assertFalse(wasSerializableInitializedFlag); |
| 142 | + } |
| 143 | + |
52 | 144 | public void testSerialVersionUidChange() throws Exception {
|
53 | 145 | // this was created by serializing a SerialVersionUidChanged with serialVersionUID = 0L
|
54 | 146 | String s = "aced0005737200396c6962636f72652e6a6176612e696f2e53657269616c697a6174696f6e54657"
|
|
0 commit comments