提交 47d4546f 编写于 作者: A alanb

6962045: TEST_BUG: Tests in test/java/io/Serializable leave files open

Reviewed-by: mchung
上级 7ff91a0f
......@@ -196,80 +196,6 @@ java/beans/XMLEncoder/6329581/Test6329581.java generic-all
java/io/BufferedReader/BigMark.java generic-all
java/io/BufferedReader/ReadLineSync.java generic-all
# One of these is leaving "a.ser" file open, windows samevm
java/io/Serializable/duplicateSerialFields/Setup.java generic-all
java/io/Serializable/duplicateSerialFields/Test.java generic-all
# One of these leaving foo.ser open, windows samevm problem
java/io/Serializable/enum/constantSubclasses/Read.java generic-all
java/io/Serializable/enum/constantSubclasses/Write.java generic-all
java/io/Serializable/enum/missingConstant/Read.java generic-all
java/io/Serializable/enum/missingConstant/Write.java generic-all
# This is leaving subtest1.tmp open, windows samevm problem
java/io/Serializable/oldTests/AnnotateClass.java generic-all
# One or more of these leave a piotest* file open, windows samevm
java/io/Serializable/oldTests/ArrayFields.java generic-all
java/io/Serializable/oldTests/ArraysOfArrays.java generic-all
java/io/Serializable/oldTests/BinaryTree.java generic-all
java/io/Serializable/oldTests/CircularList.java generic-all
java/io/Serializable/oldTests/SerializeWithException.java generic-all
java/io/Serializable/oldTests/SimpleArrays.java generic-all
java/io/Serializable/oldTests/WritePrimitive.java generic-all
# Missing close on file 0.ser, windows samevm
java/io/Serializable/enum/badResolve/Read.java generic-all
java/io/Serializable/enum/badResolve/Write.java generic-all
# One of these tests is leaving parents.ser open, windows samevm
java/io/Serializable/parents/EvolvedClass.java generic-all
java/io/Serializable/parents/OriginalClass.java generic-all
# One of these tests is leaving file foo.ser and/or bar.ser open, windows samevm
java/io/Serializable/fieldTypeString/Read.java generic-all
java/io/Serializable/fieldTypeString/Write.java generic-all
# One of these tests is leaving tmp.ser file open, windows samevm
java/io/Serializable/ClassCastExceptionDetail/Read.java generic-all
java/io/Serializable/ClassCastExceptionDetail/Write.java generic-all
java/io/Serializable/GetField/Read.java generic-all
java/io/Serializable/GetField/Read2.java generic-all
java/io/Serializable/GetField/Write.java generic-all
java/io/Serializable/PutField/Read.java generic-all
java/io/Serializable/PutField/Read2.java generic-all
java/io/Serializable/PutField/Write.java generic-all
java/io/Serializable/PutField/Write2.java generic-all
java/io/Serializable/arraySuidConflict/Read.java generic-all
java/io/Serializable/arraySuidConflict/Write.java generic-all
java/io/Serializable/backRefCNFException/Read.java generic-all
java/io/Serializable/backRefCNFException/Write.java generic-all
java/io/Serializable/class/Test.java generic-all
java/io/Serializable/evolution/AddedExternField/ReadAddedField.java generic-all
java/io/Serializable/evolution/AddedExternField/WriteAddedField.java generic-all
java/io/Serializable/evolution/AddedExternField/run.sh generic-all
java/io/Serializable/evolution/AddedField/ReadAddedField.java generic-all
java/io/Serializable/evolution/AddedField/WriteAddedField.java generic-all
java/io/Serializable/evolution/AddedSuperClass/ReadAddedSuperClass.java generic-all
java/io/Serializable/evolution/AddedSuperClass/ReadAddedSuperClass2.java generic-all
java/io/Serializable/evolution/AddedSuperClass/WriteAddedSuperClass.java generic-all
java/io/Serializable/proxy/skipMissing/Read.java generic-all
java/io/Serializable/proxy/skipMissing/Write.java generic-all
java/io/Serializable/readObjectNoData/Read.java generic-all
java/io/Serializable/readObjectNoData/Write.java generic-all
java/io/Serializable/skipWriteObject/Read.java generic-all
java/io/Serializable/skipWriteObject/Write.java generic-all
java/io/Serializable/skippedObjCNFException/Read.java generic-all
java/io/Serializable/skippedObjCNFException/Write.java generic-all
java/io/Serializable/stopCustomDeserialization/Read.java generic-all
java/io/Serializable/stopCustomDeserialization/Write.java generic-all
java/io/Serializable/unresolvedClassDesc/Read.java generic-all
java/io/Serializable/unresolvedClassDesc/Write.java generic-all
java/io/Serializable/unshared/Read.java generic-all
java/io/Serializable/unshared/Write.java generic-all
java/io/Serializable/wrongReturnTypes/Read.java generic-all
java/io/Serializable/wrongReturnTypes/Write.java generic-all
# Windows samevm issues? triggers other tests to fail, missing close() on f.txt?
java/io/DataInputStream/OpsAfterClose.java generic-all
......@@ -323,13 +249,6 @@ java/io/RandomAccessFile/skipBytes/SkipBytes.java generic-all
java/io/readBytes/MemoryLeak.java generic-all
java/io/readBytes/ReadBytesBounds.java generic-all
# Missing close on fields.ser, windows samevm
java/io/Serializable/checkModifiers/CheckModifiers.java generic-all
# Should be othervm, or corrected for samevm, fails with samevm:
java/io/Serializable/auditStreamSubclass/AuditStreamSubclass.java generic-all
java/io/Serializable/proxy/Basic.java generic-all
# Possibly not doing a close() on input.txt, windows samevm issues.
java/io/StreamTokenizer/Comment.java generic-all
......
......@@ -40,9 +40,9 @@ class Gub extends Foo {}
public class Read {
public static void main(String[] args) throws Exception {
ObjectInputStream oin =
new ObjectInputStream(new FileInputStream("tmp.ser"));
FileInputStream in = new FileInputStream("tmp.ser");
try {
ObjectInputStream oin = new ObjectInputStream(in);
oin.readObject();
throw new Error("readObject should not succeed");
} catch (ClassCastException e) {
......@@ -57,6 +57,8 @@ public class Read {
{
throw new Error("ClassNotFoundException message incomplete");
}
} finally {
in.close();
}
}
}
......@@ -26,6 +26,8 @@
* @summary Verify that unauthorized ObjectOutputStream and ObjectInputStream
* cannot be constructed if they override security-sensitive non-final
* methods.
* @build AuditStreamSubclass
* @run main/othervm AuditStreamSubclass
*/
import java.io.*;
......
......@@ -38,13 +38,17 @@ class A implements Serializable {
public class Read {
public static void main(String[] args) throws Exception {
ObjectInputStream oin =
new ObjectInputStream(new FileInputStream("tmp.ser"));
oin.readObject();
FileInputStream in = new FileInputStream("tmp.ser");
try {
ObjectInputStream oin = new ObjectInputStream(in);
oin.readObject();
throw new Error("back reference read succeeded");
} catch (ClassNotFoundException ex) {
try {
oin.readObject();
throw new Error("back reference read succeeded");
} catch (ClassNotFoundException ex) {
}
} finally {
in.close();
}
}
}
......@@ -213,33 +213,39 @@ public class CheckModifiers {
FileOutputStream fos = new FileOutputStream("fields.ser");
ObjectOutputStream oos = new ObjectOutputStream(fos);
System.out.println("Writing obj 1");
oos.writeObject(tc1);
System.out.println("Writing obj 2");
oos.writeObject(tc2);
System.out.println("Writing obj 3");
oos.writeObject(tc3);
System.out.println("Writing obj 4");
oos.writeObject(tc4);
try {
ObjectOutputStream oos = new ObjectOutputStream(fos);
System.out.println("Writing obj 1");
oos.writeObject(tc1);
System.out.println("Writing obj 2");
oos.writeObject(tc2);
System.out.println("Writing obj 3");
oos.writeObject(tc3);
System.out.println("Writing obj 4");
oos.writeObject(tc4);
oos.flush();
} finally {
fos.close();
}
FileInputStream fis = new FileInputStream("fields.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
System.out.println("Test modifiers for serialPeristentFields ");
System.out.println("---------------------------------------- ");
System.out.println("Declaration missing final modifier");
ois.readObject();
System.out.println();
System.out.println("Declaration with public instead of private access");
ois.readObject();
System.out.println();
System.out.println("Declaration with different type");
ois.readObject();
System.out.println();
System.out.println("Declaration as in specification");
ois.readObject();
try {
ObjectInputStream ois = new ObjectInputStream(fis);
System.out.println("Test modifiers for serialPeristentFields ");
System.out.println("---------------------------------------- ");
System.out.println("Declaration missing final modifier");
ois.readObject();
System.out.println();
System.out.println("Declaration with public instead of private access");
ois.readObject();
System.out.println();
System.out.println("Declaration with different type");
ois.readObject();
System.out.println();
System.out.println("Declaration as in specification");
ois.readObject();
} finally {
fis.close();
}
}
};
......@@ -42,10 +42,15 @@ public class Read {
* descriptor.
*/
File f = new File(System.getProperty("test.src", "."), "Foo.ser");
new ObjectInputStream(new FileInputStream(f)).readObject();
throw new Error(
"read succeeded for object whose class descriptor has " +
"both SC_SERIALIZABLE and SC_EXTERNALIZABLE flags set");
FileInputStream in = new FileInputStream(f);
try {
new ObjectInputStream(in).readObject();
throw new Error(
"read succeeded for object whose class descriptor has " +
"both SC_SERIALIZABLE and SC_EXTERNALIZABLE flags set");
} finally {
in.close();
}
} catch (InvalidClassException e) {
}
}
......
......@@ -93,8 +93,12 @@ public class ClassDescHooks implements ObjectStreamConstants {
bout = new ByteArrayOutputStream();
foof = new File(System.getProperty("test.src", "."), "Foo.ser");
fin = new FileInputStream(foof);
while (fin.available() > 0)
bout.write(fin.read());
try {
while (fin.available() > 0)
bout.write(fin.read());
} finally {
fin.close();
}
byte[] buf1 = bout.toByteArray();
bout = new ByteArrayOutputStream();
......@@ -107,11 +111,16 @@ public class ClassDescHooks implements ObjectStreamConstants {
if (! Arrays.equals(buf1, buf2))
throw new Error("Incompatible stream format (write)");
Foo foocopy;
fin = new FileInputStream(foof);
oin = new ObjectInputStream(fin);
Foo foocopy = (Foo) oin.readObject();
if (! foo.equals(foocopy))
throw new Error("Incompatible stream format (read)");
try {
oin = new ObjectInputStream(fin);
foocopy = (Foo) oin.readObject();
if (! foo.equals(foocopy))
throw new Error("Incompatible stream format (read)");
} finally {
fin.close();
}
// make sure write hook not called when old protocol in use
bout = new ByteArrayOutputStream();
......
......@@ -82,18 +82,26 @@ public class Test {
} catch (InvalidClassException e) {
}
FileInputStream in = new FileInputStream("a.ser");
try {
new ObjectInputStream(new FileInputStream("a.ser")).readObject();
ObjectInputStream oin = new ObjectInputStream(in);
oin.readObject();
throw new Error(
"read of A should fail with InvalidClassException");
} catch (InvalidClassException e) {
} finally {
in.close();
}
in = new FileInputStream("b.ser");
try {
new ObjectInputStream(new FileInputStream("b.ser")).readObject();
ObjectInputStream oin = new ObjectInputStream(in);
oin.readObject();
throw new Error(
"read of B should fail with InvalidClassException");
} catch (InvalidClassException e) {
} finally {
in.close();
}
}
}
......@@ -44,13 +44,15 @@ public class Read {
}
static void read(String filename) throws Exception {
ObjectInputStream oin =
new ObjectInputStream(new FileInputStream(filename));
FileInputStream in = new FileInputStream(filename);
try {
ObjectInputStream oin = new ObjectInputStream(in);
Object obj = oin.readObject();
throw new Error("read of " + obj + " should not have succeeded");
} catch (InvalidClassException e) {
System.out.println("caught expected exception " + e);
} finally {
in.close();
}
}
}
......@@ -38,13 +38,17 @@ enum Foo {
public class Read {
public static void main(String[] args) throws Exception {
ObjectInputStream oin =
new ObjectInputStream(new FileInputStream("foo.ser"));
for (Foo f : Foo.values()) {
Object obj = oin.readObject();
if (obj != f) {
throw new Error("expected " + f + ", got " + obj);
FileInputStream in = new FileInputStream("foo.ser");
try {
ObjectInputStream oin = new ObjectInputStream(in);
for (Foo f : Foo.values()) {
Object obj = oin.readObject();
if (obj != f) {
throw new Error("expected " + f + ", got " + obj);
}
}
} finally {
in.close();
}
}
}
......@@ -33,19 +33,23 @@ enum Foo { foo, bar }
public class Read {
public static void main(String[] args) throws Exception {
ObjectInputStream oin =
new ObjectInputStream(new FileInputStream("foo.ser"));
for (Foo f : Foo.values()) {
Object obj = oin.readObject();
if (obj != f) {
throw new Error("expected " + f + ", got " + obj);
}
}
FileInputStream in = new FileInputStream("foo.ser");
try {
Object obj = oin.readObject();
throw new Error("read of " + obj + " should not succeed");
} catch (InvalidObjectException e) {
System.out.println("caught expected exception: " + e);
ObjectInputStream oin = new ObjectInputStream(in);
for (Foo f : Foo.values()) {
Object obj = oin.readObject();
if (obj != f) {
throw new Error("expected " + f + ", got " + obj);
}
}
try {
Object obj = oin.readObject();
throw new Error("read of " + obj + " should not succeed");
} catch (InvalidObjectException e) {
System.out.println("caught expected exception: " + e);
}
} finally {
in.close();
}
}
}
......@@ -44,23 +44,30 @@ class Bar implements Serializable {
public class Read {
public static void main(String[] args) throws Exception {
ObjectInputStream oin =
new ObjectInputStream(new FileInputStream("foo.ser"));
Foo foo = (Foo) oin.readObject();
if (! foo.obj.equals("foo")) {
throw new Error();
}
FileInputStream in = new FileInputStream("foo.ser");
try {
oin.readObject();
throw new Error();
} catch (ClassCastException ex) {
ObjectInputStream oin = new ObjectInputStream(in);
Foo foo = (Foo) oin.readObject();
if (! foo.obj.equals("foo")) {
throw new Error();
}
try {
oin.readObject();
throw new Error();
} catch (ClassCastException ex) {
}
} finally {
in.close();
}
oin = new ObjectInputStream(new FileInputStream("bar.ser"));
in = new FileInputStream("bar.ser");
try {
ObjectInputStream oin = new ObjectInputStream(in);
oin.readObject();
throw new Error();
} catch (InvalidClassException ex) {
} finally {
in.close();
}
}
}
......@@ -38,21 +38,31 @@ public class Test {
* serialized String object followed by an illegal handle
*/
File f = new File(base, "negativeHandle.ser");
ObjectInputStream oin = new ObjectInputStream(new FileInputStream(f));
oin.readObject();
FileInputStream in = new FileInputStream(f);
try {
ObjectInputStream oin = new ObjectInputStream(in);
oin.readObject();
throw new Error("negative handle read should not succeed");
} catch (StreamCorruptedException ex) {
try {
oin.readObject();
throw new Error("negative handle read should not succeed");
} catch (StreamCorruptedException ex) {
}
} finally {
in.close();
}
f = new File(base, "tooHighHandle.ser");
oin = new ObjectInputStream(new FileInputStream(f));
oin.readObject();
in = new FileInputStream(f);
try {
ObjectInputStream oin = new ObjectInputStream(in);
oin.readObject();
throw new Error("too-high handle read should not succeed");
} catch (StreamCorruptedException ex) {
try {
oin.readObject();
throw new Error("too-high handle read should not succeed");
} catch (StreamCorruptedException ex) {
}
} finally {
in.close();
}
}
}
......@@ -68,17 +68,25 @@ public class LongString {
mesgf = new File(System.getProperty("test.src", "."), "mesg.ser");
fin = new FileInputStream(mesgf);
bout = new ByteArrayOutputStream();
while (fin.available() > 0)
bout.write(fin.read());
try {
while (fin.available() > 0)
bout.write(fin.read());
} finally {
fin.close();
}
byte[] buf2 = bout.toByteArray();
if (! Arrays.equals(buf1, buf2))
throw new Error("incompatible string format (write)");
fin = new FileInputStream(mesgf);
oin = new ObjectInputStream(fin);
String mesgcopy = (String) oin.readObject();
if (! mesg.equals(mesgcopy))
throw new Error("incompatible string format (read)");
try {
oin = new ObjectInputStream(fin);
String mesgcopy = (String) oin.readObject();
if (! mesg.equals(mesgcopy))
throw new Error("incompatible string format (read)");
} finally {
fin.close();
}
}
}
......@@ -37,36 +37,43 @@ public class AnnotateClass {
"methods \n");
try {
FileOutputStream ostream = new FileOutputStream("subtest1.tmp");
TestOutputStream p = new TestOutputStream(ostream);
p.writeObject(System.out);
p.writeObject(System.err);
p.writeObject(new PrintStream(ostream));
p.flush();
ostream.close();
try {
TestOutputStream p = new TestOutputStream(ostream);
p.writeObject(System.out);
p.writeObject(System.err);
p.writeObject(new PrintStream(ostream));
p.flush();
} finally {
ostream.close();
}
FileInputStream istream = new FileInputStream("subtest1.tmp");
TestInputStream q = new TestInputStream(istream);
PrintStream out = (PrintStream)q.readObject();
PrintStream err = (PrintStream)q.readObject();
Object other = q.readObject();
if (out != System.out) {
System.err.println(
"\nTEST FAILED: System.out not read correctly");
throw new Error();
}
if (err != System.err) {
System.err.println(
"\nTEST FAILED: System.err not read correctly");
throw new Error();
}
if (other != null) {
System.err.println(
"\nTEST FAILED: Non-system PrintStream should have " +
"been written/read as null");
throw new Error();
try {
TestInputStream q = new TestInputStream(istream);
PrintStream out = (PrintStream)q.readObject();
PrintStream err = (PrintStream)q.readObject();
Object other = q.readObject();
if (out != System.out) {
System.err.println(
"\nTEST FAILED: System.out not read correctly");
throw new Error();
}
if (err != System.err) {
System.err.println(
"\nTEST FAILED: System.err not read correctly");
throw new Error();
}
if (other != null) {
System.err.println(
"\nTEST FAILED: Non-system PrintStream should have " +
"been written/read as null");
throw new Error();
}
} finally {
istream.close();
}
System.err.println("\nTEST PASSED");
} catch (Exception e) {
System.err.print("TEST FAILED: ");
......
......@@ -35,20 +35,22 @@ import java.io.*;
public class ArrayFields {
public static void main (String argv[]) {
public static void main (String argv[]) throws IOException {
System.err.println("\nRegression test for testing of " +
"serialization/deserialization of objects with " +
"fields of array type\n");
FileOutputStream ostream = null;
FileInputStream istream = null;
try {
FileOutputStream ostream = new FileOutputStream("piotest4.tmp");
ostream = new FileOutputStream("piotest4.tmp");
ObjectOutputStream p = new ObjectOutputStream(ostream);
ArrayTest array = new ArrayTest();
p.writeObject(array);
p.flush();
FileInputStream istream = new FileInputStream("piotest4.tmp");
istream = new FileInputStream("piotest4.tmp");
ObjectInputStream q = new ObjectInputStream(istream);
Object obj = null;
......@@ -73,6 +75,9 @@ public class ArrayFields {
System.err.print("TEST FAILED: ");
e.printStackTrace();
throw new Error();
} finally {
if (istream != null) istream.close();
if (ostream != null) ostream.close();
}
}
}
......@@ -31,14 +31,15 @@
import java.io.*;
public class ArraysOfArrays {
public static void main (String argv[]) {
public static void main (String argv[]) throws IOException {
System.err.println("\nRegression test for testing of " +
"serialization/deserialization of objects as " +
"arrays of arrays \n");
FileInputStream istream = null;
FileOutputStream ostream = null;
try {
FileOutputStream ostream = new FileOutputStream("piotest5.tmp");
ostream = new FileOutputStream("piotest5.tmp");
ObjectOutputStream p = new ObjectOutputStream(ostream);
byte b[][] = {{ 0, 1}, {2,3}};
......@@ -207,6 +208,9 @@ public class ArraysOfArrays {
throw new Error();
}
throw new Error();
} finally {
if (istream != null) istream.close();
if (ostream != null) ostream.close();
}
}
}
......@@ -37,28 +37,34 @@ public class BinaryTree {
"with BinaryTree types \n");
try {
FileOutputStream ostream = new FileOutputStream("piotest3.tmp");
ObjectOutputStream p = new ObjectOutputStream(ostream);
BinaryTreeTest base = new BinaryTreeTest(2);
p.writeObject(null);
p.writeObject(base);
p.flush();
ostream.close();
FileOutputStream ostream = new FileOutputStream("piotest3.tmp");
try {
ObjectOutputStream p = new ObjectOutputStream(ostream);
p.writeObject(null);
p.writeObject(base);
p.flush();
} finally {
ostream.close();
}
FileInputStream istream = new FileInputStream("piotest3.tmp");
ObjectInputStream q = new ObjectInputStream(istream);
Object n = q.readObject();
if (n != null) {
System.err.println("\nnull read as " + n);
}
BinaryTreeTest nbase = (BinaryTreeTest)q.readObject();
if (!base.equals(nbase)) {
System.err.println("\nTEST FAILED: BinaryTree read " +
"incorrectly.");
throw new Error();
try {
ObjectInputStream q = new ObjectInputStream(istream);
Object n = q.readObject();
if (n != null) {
System.err.println("\nnull read as " + n);
}
BinaryTreeTest nbase = (BinaryTreeTest)q.readObject();
if (!base.equals(nbase)) {
System.err.println("\nTEST FAILED: BinaryTree read " +
"incorrectly.");
throw new Error();
}
} finally {
istream.close();
}
System.err.println("\nTEST PASSED");
} catch (Exception e) {
System.err.print("TEST FAILED: ");
......
......@@ -31,20 +31,22 @@
import java.io.*;
public class CircularList {
public static void main (String argv[]) {
public static void main (String argv[]) throws IOException {
System.err.println("\nRegression test for testing of " +
"serialization/deserialization of " +
"objects with CirculalListType types \n");
FileInputStream istream = null;
FileOutputStream ostream = null;
try {
FileOutputStream ostream = new FileOutputStream("piotest7.tmp");
ostream = new FileOutputStream("piotest7.tmp");
ObjectOutputStream p = new ObjectOutputStream(ostream);
CircularListTest.setup();
p.writeObject(CircularListTest.list);
p.flush();
FileInputStream istream = new FileInputStream("piotest7.tmp");
istream = new FileInputStream("piotest7.tmp");
ObjectInputStream q = new ObjectInputStream(istream);
CircularListTest cv = (CircularListTest)q.readObject();
......@@ -58,6 +60,9 @@ public class CircularList {
System.err.print("TEST FAILED: ");
e.printStackTrace();
throw new Error();
} finally {
if (istream != null) istream.close();
if (ostream != null) ostream.close();
}
}
}
......
......@@ -35,13 +35,14 @@ import java.io.*;
public class SimpleArrays {
public static void main (String argv[]) {
public static void main (String argv[]) throws IOException {
System.err.println("\nRegression test for testing of " +
"serialization/deserialization of objects with Arrays types\n");
FileInputStream istream = null;
FileOutputStream ostream = null;
try {
FileOutputStream ostream = new FileOutputStream("piotest2.tmp");
ostream = new FileOutputStream("piotest2.tmp");
ObjectOutputStream p = new ObjectOutputStream(ostream);
byte b[] = { 0, 1};
......@@ -177,6 +178,9 @@ public class SimpleArrays {
throw new Error();
}
throw new Error();
} finally {
if (istream != null) istream.close();
if (ostream != null) ostream.close();
}
}
}
......@@ -34,11 +34,12 @@
import java.io.*;
public class WritePrimitive {
public static void main (String argv[]) {
public static void main (String argv[]) throws IOException {
System.err.println("\nRegression test for testing of " +
"serialization/deserialization of primitives \n");
FileInputStream istream = null;
FileOutputStream ostream = null;
try {
int i = 123456;
byte b = 12;
......@@ -51,7 +52,7 @@ public class WritePrimitive {
String string = "The String";
PrimitivesTest prim = new PrimitivesTest();
FileOutputStream ostream = new FileOutputStream("piotest1.tmp");
ostream = new FileOutputStream("piotest1.tmp");
ObjectOutputStream p = new ObjectOutputStream(ostream);
p.writeInt(i);
......@@ -154,6 +155,9 @@ public class WritePrimitive {
throw new Error();
}
throw new Error();
} finally {
if (istream != null) istream.close();
if (ostream != null) ostream.close();
}
}
}
......@@ -38,7 +38,8 @@ public class Test {
public static void main(String[] args) throws Exception {
ClassLoader ldr =
new URLClassLoader(new URL[]{ new URL("file:foo.jar") });
new URLClassLoader(new URL[]{ new URL("file:foo.jar") },
Test.class.getClassLoader());
bcl = Class.forName("B", true, ldr);
dcl = Class.forName("D", true, ldr);
......
......@@ -34,8 +34,12 @@ public class EvolvedClass {
// Deserialize in to new class object
FileInputStream fi = new FileInputStream("parents.ser");
ObjectInputStream si = new ObjectInputStream(fi);
cnew = (ASubClass) si.readObject();
try {
ObjectInputStream si = new ObjectInputStream(fi);
cnew = (ASubClass) si.readObject();
} finally {
fi.close();
}
System.out.println("Printing the deserialized class: ");
System.out.println();
......
......@@ -43,9 +43,13 @@ public class OriginalClass {
// Serialize the subclass
FileOutputStream fo = new FileOutputStream("parents.ser");
ObjectOutputStream so = new ObjectOutputStream(fo);
so.writeObject(corg);
so.flush();
try {
ObjectOutputStream so = new ObjectOutputStream(fo);
so.writeObject(corg);
so.flush();
} finally {
fo.close();
}
System.out.println("Printing the serialized class: ");
System.out.println();
......
......@@ -81,7 +81,7 @@ class ProxyBlindInputStream extends ObjectInputStream {
public class Basic {
public static void main(String[] args) throws Exception {
ClassLoader loader = ClassLoader.getSystemClassLoader();
ClassLoader loader = Basic.class.getClassLoader();
Class[] interfaces = new Class[] { Foo.class, Bar.class };
Random rand = new Random();
int foo = rand.nextInt();
......
......@@ -49,20 +49,24 @@ class B implements Serializable {
public class Read {
public static void main(String[] args) throws Exception {
ObjectInputStream oin = new ObjectInputStream(
new FileInputStream("tmp.ser"));
A a = (A) oin.readObject();
if (! (a.a.equals("a") && a.z.equals("z"))) {
throw new Error("A fields corrupted");
}
B b = (B) oin.readObject();
if (! b.s.equals("s")) {
throw new Error("B fields corrupted");
}
FileInputStream in = new FileInputStream("tmp.ser");
try {
oin.readObject();
throw new Error("proxy read should not succeed");
} catch (ClassNotFoundException ex) {
ObjectInputStream oin = new ObjectInputStream(in);
A a = (A) oin.readObject();
if (! (a.a.equals("a") && a.z.equals("z"))) {
throw new Error("A fields corrupted");
}
B b = (B) oin.readObject();
if (! b.s.equals("s")) {
throw new Error("B fields corrupted");
}
try {
oin.readObject();
throw new Error("proxy read should not succeed");
} catch (ClassNotFoundException ex) {
}
} finally {
in.close();
}
}
}
......@@ -64,7 +64,7 @@ class B implements Serializable {
public class Write {
public static void main(String[] args) throws Exception {
Object proxy = Proxy.newProxyInstance(
ClassLoader.getSystemClassLoader(),
Write.class.getClassLoader(),
new Class[] { I.class }, new Handler());
ObjectOutputStream oout = new ObjectOutputStream(
new FileOutputStream("tmp.ser"));
......
......@@ -95,14 +95,18 @@ class F extends E {
public class Read {
public static void main(String[] args) throws Exception {
ObjectInputStream oin =
new ObjectInputStream(new FileInputStream("tmp.ser"));
F f = (F) oin.readObject();
if (f.aCalled || f.bCalled || f.dCalled || f.eCalled) {
throw new Error("readObjectNoData invoked erroneously");
}
if (! f.cCalled) {
throw new Error("readObjectNoData not invoked");
FileInputStream in = new FileInputStream("tmp.ser");
try {
ObjectInputStream oin = new ObjectInputStream(in);
F f = (F) oin.readObject();
if (f.aCalled || f.bCalled || f.dCalled || f.eCalled) {
throw new Error("readObjectNoData invoked erroneously");
}
if (! f.cCalled) {
throw new Error("readObjectNoData not invoked");
}
} finally {
in.close();
}
}
}
......@@ -38,9 +38,13 @@ public class Read {
public static void main(String[] args) throws Exception {
ObjectInputStream oin =
new ObjectInputStream(new FileInputStream("tmp.ser"));
Object[] array = (Object[]) oin.readObject();
if (! (array[0].equals("before") && array[2].equals("after"))) {
throw new Error();
try {
Object[] array = (Object[]) oin.readObject();
if (! (array[0].equals("before") && array[2].equals("after"))) {
throw new Error();
}
} finally {
oin.close();
}
}
}
......@@ -44,8 +44,12 @@ class B implements Serializable {
public class Read {
public static void main(String[] args) throws Exception {
ObjectInputStream oin =
new ObjectInputStream(new FileInputStream("tmp.ser"));
oin.readObject();
FileInputStream in = new FileInputStream("tmp.ser");
try {
ObjectInputStream oin = new ObjectInputStream(in);
oin.readObject();
} finally {
in.close();
}
}
}
......@@ -53,16 +53,20 @@ class C extends B {
public class Read {
public static void main(String[] args) throws Exception {
ObjectInputStream oin =
new ObjectInputStream(new FileInputStream("tmp.ser"));
FileInputStream in = new FileInputStream("tmp.ser");
try {
oin.readObject();
throw new Error("readObject should not succeed");
} catch (ClassNotFoundException e) {
// expected
}
if (!oin.readObject().equals("after")) {
throw new Error("subsequent object corrupted");
ObjectInputStream oin = new ObjectInputStream(in);
try {
oin.readObject();
throw new Error("readObject should not succeed");
} catch (ClassNotFoundException e) {
// expected
}
if (!oin.readObject().equals("after")) {
throw new Error("subsequent object corrupted");
}
} finally {
in.close();
}
}
}
......@@ -32,19 +32,23 @@ import java.io.*;
public class Read {
public static void main(String[] args) throws Exception {
ObjectInputStream oin =
new ObjectInputStream(new FileInputStream("tmp.ser"));
oin.readObject();
oin.readObject();
FileInputStream in = new FileInputStream("tmp.ser");
try {
ObjectInputStream oin = new ObjectInputStream(in);
oin.readObject();
throw new Error("read of Foo instance succeeded");
} catch (ClassNotFoundException ex) {
}
try {
oin.readObject();
throw new Error("indirect read of Foo instance succeeded");
} catch (ClassNotFoundException ex) {
try {
oin.readObject();
throw new Error("read of Foo instance succeeded");
} catch (ClassNotFoundException ex) {
}
try {
oin.readObject();
throw new Error("indirect read of Foo instance succeeded");
} catch (ClassNotFoundException ex) {
}
} finally {
in.close();
}
}
}
......@@ -80,20 +80,30 @@ public class Read {
}
// read in objects written by Write.main()
oin = new ObjectInputStream(new FileInputStream("tmp.ser"));
oin.readObject();
FileInputStream in = new FileInputStream("tmp.ser");
try {
oin = new ObjectInputStream(in);
oin.readObject();
throw new Error();
} catch (ObjectStreamException ex) {
try {
oin.readObject();
throw new Error();
} catch (ObjectStreamException ex) {
}
} finally {
in.close();
}
oin = new ObjectInputStream(new FileInputStream("tmp.ser"));
oin.readObject();
in = new FileInputStream("tmp.ser");
try {
oin.readUnshared();
throw new Error();
} catch (ObjectStreamException ex) {
oin = new ObjectInputStream(in);
oin.readObject();
try {
oin.readUnshared();
throw new Error();
} catch (ObjectStreamException ex) {
}
} finally {
in.close();
}
}
}
......@@ -62,15 +62,19 @@ class B extends A {
public class Read {
public static void main(String[] args) throws Exception {
ObjectInputStream oin =
new ObjectInputStream(new FileInputStream("tmp.ser"));
B b = (B) oin.readObject();
if (A.readObjectNoDataCalled) {
throw new Error("readObjectNoData with wrong return type called");
} else if (B.readObjectCalled) {
throw new Error("readObject with wrong return type called");
} else if (B.readResolveCalled) {
throw new Error("readResolve with wrong return type called");
FileInputStream in = new FileInputStream("tmp.ser");
try {
ObjectInputStream oin = new ObjectInputStream(in);
B b = (B) oin.readObject();
if (A.readObjectNoDataCalled) {
throw new Error("readObjectNoData with wrong return type called");
} else if (B.readObjectCalled) {
throw new Error("readObject with wrong return type called");
} else if (B.readResolveCalled) {
throw new Error("readResolve with wrong return type called");
}
} finally {
in.close();
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册