This is the mail archive of the mauve-patches@sources.redhat.com mailing list for the Mauve project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Patch: New serialization tests


Here's a couple of new serialization tests. One checks that ObjectOutputStream generates a valid-looking stream, and the other that ObjectInputStream's readObject/resolveClass use the right class loader depending on the context in which they're called.

Bryce


2005-02-16  Bryce McKinlay  <mckinlay@redhat.com>

	* gnu/testlet/java/io/ObjectOutputStream/StreamDataTest.java: New test.
	* gnu/testlet/java/io/ObjectInputStream/ClassLoaderTest.java: New test.

Index: gnu/testlet/java/io/ObjectInputStream/ClassLoaderTest.java
===================================================================
RCS file: gnu/testlet/java/io/ObjectInputStream/ClassLoaderTest.java
diff -N gnu/testlet/java/io/ObjectInputStream/ClassLoaderTest.java
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ gnu/testlet/java/io/ObjectInputStream/ClassLoaderTest.java	17 Feb 2005 06:18:21 -0000
@@ -0,0 +1,131 @@
+// Tags: JDK1.2
+
+// Copyright (C) 2005 Free Software Foundation
+
+// This file is part of Mauve.
+
+// Mauve is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 2, or (at your option)
+// any later version.
+
+// Mauve is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License
+// along with Mauve; see the file COPYING.  If not, write to
+// the Free Software Foundation, 59 Temple Place - Suite 330,
+// Boston, MA 02111-1307, USA.
+
+package gnu.testlet.java.io.ObjectInputStream;
+
+import gnu.testlet.TestHarness;
+import gnu.testlet.Testlet;
+
+import java.io.*;
+import java.lang.reflect.*;
+
+/**
+ * This test checks that ObjectInputStream.readObject() resolves objects
+ * in the stream using the correct ClassLoader, based on the context it is
+ * called from.
+ */
+public class ClassLoaderTest implements Testlet 
+{
+  static class MyClassLoader extends ClassLoader
+  {    
+    public Class defineClass(File cls, String name)
+      throws ClassNotFoundException, IOException
+    {
+      if (!cls.exists())
+        throw new ClassNotFoundException();
+      
+      FileInputStream is = new FileInputStream(cls);
+      ByteArrayOutputStream os = new ByteArrayOutputStream();
+      byte[] buf = new byte[512];
+      int len, written;
+      while (true)
+        {
+	  len = is.read(buf, 0, buf.length);
+	  if (len == -1)
+	    break;
+	  written = 0;
+	  while (written < len)
+	    {
+	      os.write(buf, written, len - written);
+	      written += len;
+	    }
+	}
+	
+      byte[] classData = os.toByteArray();
+
+      return super.defineClass(name, classData, 0, classData.length);      
+    }
+  }
+
+  public static class MyClass implements Serializable
+  {
+    int i = 555;
+    
+    public static Object deserialize(byte[] serData) 
+      throws IOException, ClassNotFoundException
+    {
+      ByteArrayInputStream bis = new ByteArrayInputStream(serData);
+      ObjectInputStream ois = new ObjectInputStream(bis);
+      Object obj = ois.readObject();
+      ois.close();
+      return obj;
+    }
+  }
+
+  /**
+   * Runs the test using the specified harness.
+   * 
+   * @param harness  the test harness (<code>null</code> not permitted).
+   */
+  public void test(TestHarness harness) throws Exception
+  {
+    MyClassLoader loader = new MyClassLoader();
+    ClassLoader sysLoader = this.getClass().getClassLoader();
+    
+    // FIXME: This assumes that mauve is run from its base directory, and
+    // the test classes are not in a .jar or something. Might be better to use
+    // a URL or something to find this?
+    File classFile = new File(
+      "gnu/testlet/java/io/ObjectInputStream/ClassLoaderTest$MyClass.class");
+    harness.check(classFile.exists(), "class file exists");
+    String className =
+      "gnu.testlet.java.io.ObjectInputStream.ClassLoaderTest$MyClass";
+
+    Class cl = loader.defineClass(classFile, className);
+
+    harness.check(cl.getClassLoader() == loader, 
+      "Class has correct classloader");
+
+    // Now the fun part. Pipe an instance of MyClass through an Object 
+    // stream. Depending on which class-context we deserialize it in, the 
+    // resulting instance should have a different ClassLoader (and class).
+
+    harness.checkPoint ("Deserialized objects have correct ClassLoader");
+
+    ByteArrayOutputStream bos = new ByteArrayOutputStream();
+    ObjectOutputStream oos = new ObjectOutputStream(bos);
+    oos.writeObject(cl.newInstance());
+    oos.close();    
+    byte[] serData = bos.toByteArray();
+
+    Object obj = MyClass.deserialize(serData);
+
+    harness.check (obj.getClass().getClassLoader() == sysLoader);
+    System.out.println (obj.getClass().getClassLoader() == loader);
+
+    Method m = cl.getMethod("deserialize", new Class[] {byte[].class});
+    Object obj2 = m.invoke(null, new Object[] {serData});
+    harness.check(obj2.getClass().getClassLoader() == loader);
+    System.out.println (obj2.getClass().getClassLoader() == loader);
+
+    harness.check (obj.getClass() != obj2.getClass(), "Class equality");
+  }  
+}
Index: gnu/testlet/java/io/ObjectOutputStream/StreamDataTest.java
===================================================================
RCS file: gnu/testlet/java/io/ObjectOutputStream/StreamDataTest.java
diff -N gnu/testlet/java/io/ObjectOutputStream/StreamDataTest.java
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ gnu/testlet/java/io/ObjectOutputStream/StreamDataTest.java	17 Feb 2005 06:18:21 -0000
@@ -0,0 +1,137 @@
+// Tags: JDK1.1
+
+/* Copyright (c) 2005 by Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published
+   by the Free Software Foundation, version 2. (see COPYING)
+
+   This program is distributed in the hope that it will be useful, but
+   WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software Foundation
+   Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307 USA */
+
+package gnu.testlet.java.io.ObjectOutputStream;
+
+import gnu.testlet.Testlet;
+import gnu.testlet.TestHarness;
+
+import java.io.ObjectOutputStream;
+import java.io.ObjectInputStream;
+import java.io.ObjectStreamConstants;
+import java.io.IOException;
+import java.io.ByteArrayOutputStream;
+import java.io.ByteArrayInputStream;
+
+/* Basic tests for ObjectOutputStream compliance with the serialization data
+   stream specification. */
+public class StreamDataTest implements Testlet
+{
+  static int offset = 0;
+  static byte[] streamData;
+
+  static boolean compare(int[] expectedData)
+  {
+    try
+    {
+      for (int i=0; i < expectedData.length; i++)
+	if (streamData[offset + i] != (byte) (expectedData[i] & 0xff))
+	  return false;
+    }
+    finally
+    {
+      offset += expectedData.length;
+    }
+    
+    return true;
+  }
+
+  public void test(TestHarness harness)
+  {
+    try
+    {
+      checkStream(harness);
+    }
+    catch (IOException x)
+    {
+      harness.fail(x.toString());
+    }
+  }
+
+  public void checkStream(TestHarness harness) throws IOException
+  {
+    ByteArrayOutputStream os = new ByteArrayOutputStream();
+    ObjectOutputStream oos = new ObjectOutputStream(os);
+    
+    oos.useProtocolVersion(ObjectStreamConstants.PROTOCOL_VERSION_2);
+    
+    oos.writeInt(1);
+    oos.writeShort((short) 7);
+    oos.writeFloat(9.96601f);
+    oos.writeLong(-900000000000001l);
+    oos.writeShort((short) -1);
+    oos.writeDouble(Math.PI);
+    oos.writeByte((byte) 'z');
+    oos.writeDouble(Double.NaN);
+    
+    byte[] bytes = new byte[] {-1,2,-3,4,-5};
+    oos.writeObject(bytes);
+    oos.writeByte(100);
+    oos.writeChar('X');
+    oos.close();
+
+    streamData = os.toByteArray();
+    
+    harness.check(streamData.length, 76, "Stream length");
+
+    int[] data;
+    data = new int[] {0xac, 0xed};
+    harness.check(compare(data), "magic");
+    data = new int[] {0x0, 0x5};
+    harness.check(compare(data), "version");
+    data = new int[] {0x77, 0x25};
+    harness.check(compare(data), "TC_BLOCKDATA");
+    data = new int[] {0x0, 0x0, 0x0, 0x1};
+    harness.check(compare(data), "(int) 1");
+    data = new int[] {0x0, 0x7};
+    harness.check(compare(data), "(short) 7");
+    data = new int[] {0x41, 0x1f, 0x74, 0xc7};
+    harness.check(compare(data), "(float)");
+    data = new int[] {0xff, 0xfc, 0xcd, 0x74, 0x6b, 0xb3, 0xbf, 0xff};
+    harness.check(compare(data), "(long)");
+    data = new int[] {0xff, 0xff};
+    harness.check(compare(data), "(short) -1");
+    data = new int[] {0x40, 0x9, 0x21, 0xfb, 0x54, 0x44, 0x2d, 0x18};
+    harness.check(compare(data), "(double) Math.PI");
+    data = new int[] {0x7a};
+    harness.check(compare(data), "(byte) 'z'");
+    data = new int[] {0x7f, 0xf8, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
+    harness.check(compare(data), "(double) Double.NaN");
+    data = new int[] {0x75};
+    harness.check(compare(data), "TC_NEWARRAY");
+    data = new int[] {0x72};
+    harness.check(compare(data), "TC_CLASSDESC");
+    data = new int[] {0x0, 0x2, 0x5b, 0x42};
+    harness.check(compare(data), "[B");
+    data = new int[] {0xac, 0xf3, 0x17, 0xf8, 0x6, 0x8, 0x54, 0xe0};
+    harness.check(compare(data), "SerialVersionUID");
+    data = new int[] {0x2, 0x0, 0x0, 0x78};
+    harness.check(compare(data), "Handle");
+    data = new int[] {0x70};
+    harness.check(compare(data), "ClassDescInfo");
+    data = new int[] {0x0, 0x0, 0x0, 0x5};
+    harness.check(compare(data), "array size (int) 5");
+    data = new int[] {0xff, 0x2, 0xfd, 0x4, 0xfb};
+    harness.check(compare(data), "int[] array data");
+    data = new int[] {0x77, 0x3};
+    harness.check(compare(data), "TC_BLOCKDATA");
+    data = new int[] {0x64};
+    harness.check(compare(data), "(byte) 100");
+    data = new int[] {0x0, 0x58};
+    harness.check(compare(data), "(char) 'X'");
+  }
+}

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]