ObjectInputStreamTest.java revision 2ad60cfc28e14ee8f0bb038720836a4696c478ad
1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17
18package tests.api.java.io;
19
20import java.io.BufferedInputStream;
21import java.io.ByteArrayInputStream;
22import java.io.ByteArrayOutputStream;
23import java.io.File;
24import java.io.FileInputStream;
25import java.io.FileOutputStream;
26import java.io.IOException;
27import java.io.InputStream;
28import java.io.NotActiveException;
29import java.io.ObjectInputStream;
30import java.io.ObjectOutputStream;
31import java.io.ObjectStreamClass;
32import java.io.OutputStream;
33import java.io.Serializable;
34import java.io.SerializablePermission;
35import java.io.StreamCorruptedException;
36import java.security.Permission;
37import java.util.Arrays;
38import java.util.Hashtable;
39import java.util.Vector;
40
41import org.apache.harmony.testframework.serialization.SerializationTest;
42import org.apache.harmony.testframework.serialization.SerializationTest.SerializableAssert;
43
44public class ObjectInputStreamTest extends junit.framework.TestCase implements
45        Serializable {
46
47    ObjectInputStream ois;
48
49    ObjectOutputStream oos;
50
51    ByteArrayOutputStream bao;
52
53    public class SerializableTestHelper implements Serializable {
54
55        public String aField1;
56
57        public String aField2;
58
59        SerializableTestHelper() {
60            aField1 = null;
61            aField2 = null;
62        }
63
64        SerializableTestHelper(String s, String t) {
65            aField1 = s;
66            aField2 = t;
67        }
68
69        private void readObject(ObjectInputStream ois) throws Exception {
70            // note aField2 is not read
71            ObjectInputStream.GetField fields = ois.readFields();
72            aField1 = (String) fields.get("aField1", "Zap");
73        }
74
75        private void writeObject(ObjectOutputStream oos) throws IOException {
76            // note aField2 is not written
77            ObjectOutputStream.PutField fields = oos.putFields();
78            fields.put("aField1", aField1);
79            oos.writeFields();
80        }
81
82        public String getText1() {
83            return aField1;
84        }
85
86        public void setText1(String s) {
87            aField1 = s;
88        }
89
90        public String getText2() {
91            return aField2;
92        }
93
94        public void setText2(String s) {
95            aField2 = s;
96        }
97    }
98
99    public static class A1 implements Serializable {
100
101        static final long serialVersionUID = 5942584913446079661L;
102
103        B1 b1 = new B1();
104
105        B1 b2 = b1;
106
107        Vector v = new Vector();
108    }
109
110    public static class B1 implements Serializable {
111
112        int i = 5;
113
114        Hashtable h = new Hashtable();
115    }
116
117    /**
118     * @tests java.io.ObjectInputStream#readObject()
119     */
120    public void test_readObjectMissingClasses() throws Exception {
121        SerializationTest.verifySelf(new A1(), new SerializableAssert() {
122            public void assertDeserialized(Serializable initial,
123                    Serializable deserialized) {
124                assertEquals(5, ((A1) deserialized).b1.i);
125            }
126        });
127    }
128
129    /**
130     * @tests java.io.ObjectInputStream#ObjectInputStream(java.io.InputStream)
131     */
132    public void test_ConstructorLjava_io_InputStream() throws IOException {
133        // Test for method java.io.ObjectInputStream(java.io.InputStream)
134        oos.writeDouble(Double.MAX_VALUE);
135        oos.close();
136        ois = new ObjectInputStream(new ByteArrayInputStream(bao.toByteArray()));
137        ois.close();
138        oos.close();
139
140        try {
141            ois = new ObjectInputStream(new ByteArrayInputStream(new byte[90]));
142            fail("StreamCorruptedException expected");
143        } catch (StreamCorruptedException e) {}
144    }
145
146    /**
147     * @tests java.io.ObjectInputStream#ObjectInputStream(java.io.InputStream)
148     */
149    public void test_ConstructorLjava_io_InputStream_subtest0() throws IOException {
150        SecurityManager sm = System.getSecurityManager();
151        System.setSecurityManager(new SecurityManager() {
152            Permission golden = new SerializablePermission("enableSubclassImplementation");
153
154            public void checkPermission(Permission p) {
155                if (golden.equals(p)) {
156                    throw new SecurityException();
157                }
158            }
159        });
160
161        try {
162            ByteArrayOutputStream out = new ByteArrayOutputStream();
163            ObjectOutputStream obout = new ObjectOutputStream(out);
164            obout.write(0);
165            obout.close();
166
167            InputStream in = new ByteArrayInputStream(out.toByteArray());
168
169            // should not cause SecurityException
170            new ObjectInputStream(in);
171            in.reset();
172
173            // should not cause SecurityException
174            new ObjectInputStream(in) {};
175            in.reset();
176
177            try {
178                new ObjectInputStream(in) {
179                    public Object readUnshared() throws IOException, ClassNotFoundException {
180                        return null;
181                    }
182                };
183                fail("should throw SecurityException 1");
184            } catch (SecurityException e) {}
185
186            in.reset();
187            try {
188                new ObjectInputStream(in) {
189                    public GetField readFields() throws IOException,
190                            ClassNotFoundException, NotActiveException {
191                        return null;
192                    }
193                };
194                fail("should throw SecurityException 2");
195            } catch (SecurityException e) {}
196        } finally {
197            System.setSecurityManager(sm);
198        }
199    }
200
201    /**
202     * @tests java.io.ObjectInputStream#available()
203     */
204    public void test_available() throws IOException {
205        // Test for method int java.io.ObjectInputStream.available()
206        oos.writeBytes("HelloWorld");
207        oos.close();
208        ois = new ObjectInputStream(new ByteArrayInputStream(bao.toByteArray()));
209        assertEquals("Read incorrect bytes", 10, ois.available());
210        ois.close();
211    }
212
213    /**
214     * @tests java.io.ObjectInputStream#close()
215     */
216    public void test_close() throws IOException {
217        // Test for method void java.io.ObjectInputStream.close()
218        oos.writeBytes("HelloWorld");
219        oos.close();
220        ois = new ObjectInputStream(new ByteArrayInputStream(bao.toByteArray()));
221        ois.close();
222    }
223
224    /**
225     * @tests java.io.ObjectInputStream#defaultReadObject()
226     */
227    public void test_defaultReadObject() throws Exception {
228        // Test for method void java.io.ObjectInputStream.defaultReadObject()
229        // SM. This method may as well be private, as if called directly it
230        // throws an exception.
231        String s = "HelloWorld";
232        oos.writeObject(s);
233        oos.close();
234        ois = new ObjectInputStream(new ByteArrayInputStream(bao.toByteArray()));
235        try {
236            ois.defaultReadObject();
237            fail("NotActiveException expected");
238        } catch (NotActiveException e) {
239            // Desired behavior
240        } finally {
241            ois.close();
242        }
243    }
244
245    /**
246     * @tests java.io.ObjectInputStream#read()
247     */
248    public void test_read() throws IOException {
249        // Test for method int java.io.ObjectInputStream.read()
250        oos.write('T');
251        oos.close();
252        ois = new ObjectInputStream(new ByteArrayInputStream(bao.toByteArray()));
253        assertEquals("Read incorrect byte value", 'T', ois.read());
254        ois.close();
255    }
256
257    /**
258     * @tests java.io.ObjectInputStream#read(byte[], int, int)
259     */
260    public void test_read$BII() throws IOException {
261        // Test for method int java.io.ObjectInputStream.read(byte [], int, int)
262        byte[] buf = new byte[10];
263        oos.writeBytes("HelloWorld");
264        oos.close();
265        ois = new ObjectInputStream(new ByteArrayInputStream(bao.toByteArray()));
266        ois.read(buf, 0, 10);
267        ois.close();
268        assertEquals("Read incorrect bytes", "HelloWorld", new String(buf, 0,
269                10));
270    }
271
272    /**
273     * @tests java.io.ObjectInputStream#readBoolean()
274     */
275    public void test_readBoolean() throws IOException {
276        // Test for method boolean java.io.ObjectInputStream.readBoolean()
277        oos.writeBoolean(true);
278        oos.close();
279        ois = new ObjectInputStream(new ByteArrayInputStream(bao.toByteArray()));
280        assertTrue("Read incorrect boolean value", ois.readBoolean());
281        ois.close();
282    }
283
284    /**
285     * @tests java.io.ObjectInputStream#readByte()
286     */
287    public void test_readByte() throws IOException {
288        // Test for method byte java.io.ObjectInputStream.readByte()
289        oos.writeByte(127);
290        oos.close();
291        ois = new ObjectInputStream(new ByteArrayInputStream(bao.toByteArray()));
292        assertEquals("Read incorrect byte value", 127, ois.readByte());
293        ois.close();
294    }
295
296    /**
297     * @tests java.io.ObjectInputStream#readChar()
298     */
299    public void test_readChar() throws IOException {
300        // Test for method char java.io.ObjectInputStream.readChar()
301        oos.writeChar('T');
302        oos.close();
303        ois = new ObjectInputStream(new ByteArrayInputStream(bao.toByteArray()));
304        assertEquals("Read incorrect char value", 'T', ois.readChar());
305        ois.close();
306    }
307
308    /**
309     * @tests java.io.ObjectInputStream#readDouble()
310     */
311    public void test_readDouble() throws IOException {
312        // Test for method double java.io.ObjectInputStream.readDouble()
313        oos.writeDouble(Double.MAX_VALUE);
314        oos.close();
315        ois = new ObjectInputStream(new ByteArrayInputStream(bao.toByteArray()));
316        assertTrue("Read incorrect double value",
317                ois.readDouble() == Double.MAX_VALUE);
318        ois.close();
319    }
320
321    /**
322     * @tests java.io.ObjectInputStream#readFields()
323     */
324    public void test_readFields() throws Exception {
325        // Test for method java.io.ObjectInputStream$GetField
326        // java.io.ObjectInputStream.readFields()
327
328        SerializableTestHelper sth;
329
330        /*
331         * "SerializableTestHelper" is an object created for these tests with
332         * two fields (Strings) and simple implementations of readObject and
333         * writeObject which simply read and write the first field but not the
334         * second
335         */
336
337        oos.writeObject(new SerializableTestHelper("Gabba", "Jabba"));
338        oos.flush();
339        ois = new ObjectInputStream(new ByteArrayInputStream(bao.toByteArray()));
340        sth = (SerializableTestHelper) (ois.readObject());
341        assertEquals("readFields / writeFields failed--first field not set",
342                "Gabba", sth.getText1());
343        assertNull(
344                "readFields / writeFields failed--second field should not have been set",
345                sth.getText2());
346    }
347
348    /**
349     * @tests java.io.ObjectInputStream#readFloat()
350     */
351    public void test_readFloat() throws IOException {
352        // Test for method float java.io.ObjectInputStream.readFloat()
353        oos.writeFloat(Float.MAX_VALUE);
354        oos.close();
355        ois = new ObjectInputStream(new ByteArrayInputStream(bao.toByteArray()));
356        assertTrue("Read incorrect float value",
357                ois.readFloat() == Float.MAX_VALUE);
358        ois.close();
359    }
360
361    /**
362     * @tests java.io.ObjectInputStream#readFully(byte[])
363     */
364    public void test_readFully$B() throws IOException {
365        // Test for method void java.io.ObjectInputStream.readFully(byte [])
366        byte[] buf = new byte[10];
367        oos.writeBytes("HelloWorld");
368        oos.close();
369        ois = new ObjectInputStream(new ByteArrayInputStream(bao.toByteArray()));
370        ois.readFully(buf);
371        ois.close();
372        assertEquals("Read incorrect bytes", "HelloWorld", new String(buf, 0,
373                10));
374    }
375
376    /**
377     * @tests java.io.ObjectInputStream#readFully(byte[], int, int)
378     */
379    public void test_readFully$BII() throws IOException {
380        // Test for method void java.io.ObjectInputStream.readFully(byte [],
381        // int, int)
382        byte[] buf = new byte[10];
383        oos.writeBytes("HelloWorld");
384        oos.close();
385        ois = new ObjectInputStream(new ByteArrayInputStream(bao.toByteArray()));
386        ois.readFully(buf, 0, 10);
387        ois.close();
388        assertEquals("Read incorrect bytes", "HelloWorld", new String(buf, 0,
389                10));
390    }
391
392    /**
393     * @tests java.io.ObjectInputStream#readInt()
394     */
395    public void test_readInt() throws IOException {
396        // Test for method int java.io.ObjectInputStream.readInt()
397        oos.writeInt(Integer.MAX_VALUE);
398        oos.close();
399        ois = new ObjectInputStream(new ByteArrayInputStream(bao.toByteArray()));
400        assertTrue("Read incorrect int value",
401                ois.readInt() == Integer.MAX_VALUE);
402        ois.close();
403    }
404
405    /**
406     * @tests java.io.ObjectInputStream#readLine()
407     */
408    public void test_readLine() throws IOException {
409        // Test for method java.lang.String java.io.ObjectInputStream.readLine()
410        oos.writeBytes("HelloWorld\nSecondLine");
411        oos.close();
412        ois = new ObjectInputStream(new ByteArrayInputStream(bao.toByteArray()));
413        ois.readLine();
414        assertEquals("Read incorrect string value", "SecondLine", ois
415                .readLine());
416        ois.close();
417    }
418
419    /**
420     * @tests java.io.ObjectInputStream#readLong()
421     */
422    public void test_readLong() throws IOException {
423        // Test for method long java.io.ObjectInputStream.readLong()
424        oos.writeLong(Long.MAX_VALUE);
425        oos.close();
426        ois = new ObjectInputStream(new ByteArrayInputStream(bao.toByteArray()));
427        assertTrue("Read incorrect long value",
428                ois.readLong() == Long.MAX_VALUE);
429        ois.close();
430    }
431
432    /**
433     * @tests java.io.ObjectInputStream#readObject()
434     */
435    public void test_readObject() throws Exception {
436        // Test for method java.lang.Object
437        // java.io.ObjectInputStream.readObject()
438        String s = "HelloWorld";
439        oos.writeObject(s);
440        oos.close();
441        ois = new ObjectInputStream(new ByteArrayInputStream(bao.toByteArray()));
442        assertEquals("Read incorrect Object value", s, ois.readObject());
443        ois.close();
444
445        // Regression for HARMONY-91
446        // dynamically create serialization byte array for the next hierarchy:
447        // - class A implements Serializable
448        // - class C extends A
449
450        byte[] cName = C.class.getName().getBytes();
451        byte[] aName = A.class.getName().getBytes();
452
453        ByteArrayOutputStream out = new ByteArrayOutputStream();
454
455        byte[] begStream = new byte[] { (byte) 0xac, (byte) 0xed, // STREAM_MAGIC
456                (byte) 0x00, (byte) 0x05, // STREAM_VERSION
457                (byte) 0x73, // TC_OBJECT
458                (byte) 0x72, // TC_CLASSDESC
459                (byte) 0x00, // only first byte for C class name length
460        };
461
462        out.write(begStream, 0, begStream.length);
463        out.write(cName.length); // second byte for C class name length
464        out.write(cName, 0, cName.length); // C class name
465
466        byte[] midStream = new byte[] { (byte) 0x00, (byte) 0x00, (byte) 0x00,
467                (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
468                (byte) 0x21, // serialVersionUID = 33L
469                (byte) 0x02, // flags
470                (byte) 0x00, (byte) 0x00, // fields : none
471                (byte) 0x78, // TC_ENDBLOCKDATA
472                (byte) 0x72, // Super class for C: TC_CLASSDESC for A class
473                (byte) 0x00, // only first byte for A class name length
474        };
475
476        out.write(midStream, 0, midStream.length);
477        out.write(aName.length); // second byte for A class name length
478        out.write(aName, 0, aName.length); // A class name
479
480        byte[] endStream = new byte[] { (byte) 0x00, (byte) 0x00, (byte) 0x00,
481                (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
482                (byte) 0x0b, // serialVersionUID = 11L
483                (byte) 0x02, // flags
484                (byte) 0x00, (byte) 0x01, // fields
485
486                (byte) 0x4c, // field description: type L (object)
487                (byte) 0x00, (byte) 0x04, // length
488                // field = 'name'
489                (byte) 0x6e, (byte) 0x61, (byte) 0x6d, (byte) 0x65,
490
491                (byte) 0x74, // className1: TC_STRING
492                (byte) 0x00, (byte) 0x12, // length
493                //
494                (byte) 0x4c, (byte) 0x6a, (byte) 0x61, (byte) 0x76,
495                (byte) 0x61, (byte) 0x2f, (byte) 0x6c, (byte) 0x61,
496                (byte) 0x6e, (byte) 0x67, (byte) 0x2f, (byte) 0x53,
497                (byte) 0x74, (byte) 0x72, (byte) 0x69, (byte) 0x6e,
498                (byte) 0x67, (byte) 0x3b,
499
500                (byte) 0x78, // TC_ENDBLOCKDATA
501                (byte) 0x70, // NULL super class for A class
502
503                // classdata
504                (byte) 0x74, // TC_STRING
505                (byte) 0x00, (byte) 0x04, // length
506                (byte) 0x6e, (byte) 0x61, (byte) 0x6d, (byte) 0x65, // value
507        };
508
509        out.write(endStream, 0, endStream.length);
510        out.flush();
511
512        // read created serial. form
513        ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(
514                out.toByteArray()));
515        Object o = ois.readObject();
516        assertEquals(C.class, o.getClass());
517
518		// Regression for HARMONY-846
519        assertNull(new ObjectInputStream() {}.readObject());
520    }
521
522    /**
523     * @tests java.io.ObjectInputStream#readObjectOverride()
524     */
525    public void test_readObjectOverride() throws Exception {
526        // Regression for HARMONY-846
527        assertNull(new ObjectInputStream() {
528
529            public Object readObjectOverride() throws IOException,
530                    ClassNotFoundException {
531                return super.readObjectOverride();
532            }
533
534        }.readObjectOverride());
535    }
536
537    public static class A implements Serializable {
538
539        private static final long serialVersionUID = 11L;
540
541        public String name = "name";
542    }
543
544    public static class B extends A {}
545
546    public static class C extends B {
547
548        private static final long serialVersionUID = 33L;
549    }
550
551    /**
552     * @tests java.io.ObjectInputStream#readObject()
553     */
554    public void test_readObjectCorrupt() {
555        byte[] bytes = { 00, 00, 00, 0x64, 0x43, 0x48, (byte) 0xFD, 0x71, 00,
556                00, 0x0B, (byte) 0xB8, 0x4D, 0x65 };
557        ByteArrayInputStream bin = new ByteArrayInputStream(bytes);
558        boolean exception = false;
559        try {
560            ObjectInputStream in = new ObjectInputStream(bin);
561            in.readObject();
562            fail("Unexpected read of corrupted stream");
563        } catch (StreamCorruptedException e) {
564            exception = true;
565        } catch (IOException e) {
566            fail("Unexpected: " + e);
567        } catch (ClassNotFoundException e) {
568            fail("Unexpected: " + e);
569        }
570        assertTrue("Expected StreamCorruptedException", exception);
571    }
572
573    /**
574     * @tests java.io.ObjectInputStream#readShort()
575     */
576    public void test_readShort() throws IOException {
577        // Test for method short java.io.ObjectInputStream.readShort()
578        oos.writeShort(Short.MAX_VALUE);
579        oos.close();
580        ois = new ObjectInputStream(new ByteArrayInputStream(bao.toByteArray()));
581        assertTrue("Read incorrect short value",
582                ois.readShort() == Short.MAX_VALUE);
583        ois.close();
584    }
585
586    /**
587     * @tests java.io.ObjectInputStream#readUnsignedByte()
588     */
589    public void test_readUnsignedByte() throws IOException {
590        // Test for method int java.io.ObjectInputStream.readUnsignedByte()
591        oos.writeByte(-1);
592        oos.close();
593        ois = new ObjectInputStream(new ByteArrayInputStream(bao.toByteArray()));
594        assertEquals("Read incorrect unsignedByte value", 255, ois
595                .readUnsignedByte());
596        ois.close();
597    }
598
599    /**
600     * @tests java.io.ObjectInputStream#readUnsignedShort()
601     */
602    public void test_readUnsignedShort() throws IOException {
603        // Test for method int java.io.ObjectInputStream.readUnsignedShort()
604        oos.writeShort(-1);
605        oos.close();
606        ois = new ObjectInputStream(new ByteArrayInputStream(bao.toByteArray()));
607        assertEquals("Read incorrect unsignedShort value", 65535, ois
608                .readUnsignedShort());
609        ois.close();
610    }
611
612    /**
613     * @tests java.io.ObjectInputStream#readUTF()
614     */
615    public void test_readUTF() throws IOException {
616        // Test for method java.lang.String java.io.ObjectInputStream.readUTF()
617        oos.writeUTF("HelloWorld");
618        oos.close();
619        ois = new ObjectInputStream(new ByteArrayInputStream(bao.toByteArray()));
620        assertEquals("Read incorrect utf value", "HelloWorld", ois.readUTF());
621        ois.close();
622    }
623
624    /**
625     * @tests java.io.ObjectInputStream#skipBytes(int)
626     */
627    public void test_skipBytesI() throws IOException {
628        // Test for method int java.io.ObjectInputStream.skipBytes(int)
629        byte[] buf = new byte[10];
630        oos.writeBytes("HelloWorld");
631        oos.close();
632        ois = new ObjectInputStream(new ByteArrayInputStream(bao.toByteArray()));
633        ois.skipBytes(5);
634        ois.read(buf, 0, 5);
635        ois.close();
636        assertEquals("Skipped incorrect bytes", "World", new String(buf, 0, 5));
637
638        // Regression for HARMONY-844
639        try {
640            new ObjectInputStream() {}.skipBytes(0);
641            fail("NullPointerException expected");
642        } catch (NullPointerException e) {}
643    }
644
645    // Regression Test for JIRA 2192
646	public void test_readObject_withPrimitiveClass() throws Exception {
647		File file = new File("test.ser");
648		file.deleteOnExit();
649		Test test = new Test();
650		ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(
651				file));
652		out.writeObject(test);
653		out.close();
654
655		ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));
656		Test another = (Test) in.readObject();
657		in.close();
658		assertEquals(test, another);
659	}
660
661    //Regression Test for JIRA-2249
662    public static class ObjectOutputStreamWithWriteDesc extends
663            ObjectOutputStream {
664        public ObjectOutputStreamWithWriteDesc(OutputStream os)
665                throws IOException {
666            super(os);
667        }
668
669        public void writeClassDescriptor(ObjectStreamClass desc)
670                throws IOException {
671        }
672    }
673
674    public static class ObjectIutputStreamWithReadDesc extends
675            ObjectInputStream {
676        private Class returnClass;
677
678        public ObjectIutputStreamWithReadDesc(InputStream is, Class returnClass)
679                throws IOException {
680            super(is);
681            this.returnClass = returnClass;
682        }
683
684        public ObjectStreamClass readClassDescriptor() throws IOException,
685                ClassNotFoundException {
686            return ObjectStreamClass.lookup(returnClass);
687
688        }
689    }
690
691    static class TestClassForSerialization implements Serializable {
692		private static final long serialVersionUID = 1L;
693	}
694
695    public void test_ClassDescriptor() throws IOException,
696			ClassNotFoundException {
697
698		ByteArrayOutputStream baos = new ByteArrayOutputStream();
699		ObjectOutputStreamWithWriteDesc oos = new ObjectOutputStreamWithWriteDesc(
700				baos);
701		oos.writeObject(String.class);
702		oos.close();
703		Class cls = TestClassForSerialization.class;
704		ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
705		ObjectIutputStreamWithReadDesc ois = new ObjectIutputStreamWithReadDesc(
706				bais, cls);
707		Object obj = ois.readObject();
708		ois.close();
709		assertEquals(cls, obj);
710	}
711
712
713	// Regression Test for JIRA-2340
714    public static class ObjectOutputStreamWithWriteDesc1 extends
715			ObjectOutputStream {
716		public ObjectOutputStreamWithWriteDesc1(OutputStream os)
717				throws IOException {
718			super(os);
719		}
720
721		public void writeClassDescriptor(ObjectStreamClass desc)
722				throws IOException {
723			super.writeClassDescriptor(desc);
724		}
725	}
726
727	public static class ObjectIutputStreamWithReadDesc1 extends
728			ObjectInputStream {
729
730		public ObjectIutputStreamWithReadDesc1(InputStream is)
731				throws IOException {
732			super(is);
733		}
734
735		public ObjectStreamClass readClassDescriptor() throws IOException,
736				ClassNotFoundException {
737			return super.readClassDescriptor();
738		}
739	}
740
741    // Regression test for Harmony-1921
742    public static class ObjectInputStreamWithResolve extends ObjectInputStream {
743        public ObjectInputStreamWithResolve(InputStream in) throws IOException {
744            super(in);
745        }
746
747        protected Class resolveClass(ObjectStreamClass desc)
748                throws IOException, ClassNotFoundException {
749            if (desc.getName().equals(
750                    "org.apache.harmony.luni.tests.pkg1.TestClass")) {
751                return org.apache.harmony.luni.tests.pkg2.TestClass.class;
752            }
753            return super.resolveClass(desc);
754        }
755    }
756
757    public void test_resolveClass() throws Exception {
758        org.apache.harmony.luni.tests.pkg1.TestClass to1 = new org.apache.harmony.luni.tests.pkg1.TestClass();
759        to1.i = 555;
760        ByteArrayOutputStream baos = new ByteArrayOutputStream();
761        ObjectOutputStream oos = new ObjectOutputStream(baos);
762        oos.writeObject(to1);
763        oos.flush();
764        byte[] bytes = baos.toByteArray();
765        ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
766        ObjectInputStream ois = new ObjectInputStreamWithResolve(bais);
767        org.apache.harmony.luni.tests.pkg2.TestClass to2 = (org.apache.harmony.luni.tests.pkg2.TestClass) ois
768                .readObject();
769
770        if (to2.i != to1.i) {
771            fail("Wrong object read. Expected val: " + to1.i + ", got: "
772                    + to2.i);
773        }
774    }
775
776    static class ObjectInputStreamWithResolveObject extends ObjectInputStream {
777
778        public static Integer intObj = Integer.valueOf(1000);
779
780        public ObjectInputStreamWithResolveObject(InputStream in) throws IOException {
781            super(in);
782            enableResolveObject(true);
783        }
784
785        protected Object resolveObject(Object obj) throws IOException {
786            if(obj instanceof Integer){
787                obj = intObj;
788            }
789            return super.resolveObject(obj);
790        }
791    }
792
793    /**
794     * @tests java.io.ObjectInputStream#resolveObject(Object)
795     */
796    public void test_resolveObjectLjava_lang_Object() throws Exception {
797        // Write an Integer object into memory
798        Integer original = new Integer(10);
799        ByteArrayOutputStream baos = new ByteArrayOutputStream();
800        ObjectOutputStream oos = new ObjectOutputStream(baos);
801        oos.writeObject(original);
802        oos.flush();
803        oos.close();
804
805        // Read the object from memory
806        byte[] bytes = baos.toByteArray();
807        ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
808        ObjectInputStreamWithResolveObject ois =
809            new ObjectInputStreamWithResolveObject(bais);
810        Integer actual = (Integer) ois.readObject();
811        ois.close();
812
813        // object should be resolved from 10 to 1000
814        assertEquals(ObjectInputStreamWithResolveObject.intObj, actual);
815    }
816
817	public void test_readClassDescriptor() throws IOException,
818			ClassNotFoundException {
819
820		ByteArrayOutputStream baos = new ByteArrayOutputStream();
821		ObjectOutputStreamWithWriteDesc1 oos = new ObjectOutputStreamWithWriteDesc1(
822				baos);
823		ObjectStreamClass desc = ObjectStreamClass
824		.lookup(TestClassForSerialization.class);
825		oos.writeClassDescriptor(desc);
826		oos.close();
827
828        byte[] bytes = baos.toByteArray();
829		ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
830		ObjectIutputStreamWithReadDesc1 ois = new ObjectIutputStreamWithReadDesc1(
831				bais);
832		Object obj = ois.readClassDescriptor();
833		ois.close();
834		assertEquals(desc.getClass(), obj.getClass());
835
836        //eof
837        bais = new ByteArrayInputStream(bytes);
838        ExceptionalBufferedInputStream bis = new ExceptionalBufferedInputStream(
839                bais);
840        ois = new ObjectIutputStreamWithReadDesc1(bis);
841
842        bis.setEOF(true);
843
844        try {
845            obj = ois.readClassDescriptor();
846        } catch (IOException e) {
847            //e.printStackTrace();
848        } finally {
849            ois.close();
850        }
851
852        //throw exception
853        bais = new ByteArrayInputStream(bytes);
854        bis = new ExceptionalBufferedInputStream(bais);
855        ois = new ObjectIutputStreamWithReadDesc1(bis);
856
857        bis.setException(new IOException());
858
859        try {
860            obj = ois.readClassDescriptor();
861        } catch (IOException e) {
862            //e.printStackTrace();
863        } finally {
864            ois.close();
865        }
866
867        //corrupt
868        bais = new ByteArrayInputStream(bytes);
869        bis = new ExceptionalBufferedInputStream(bais);
870        ois = new ObjectIutputStreamWithReadDesc1(bis);
871
872        bis.setCorrupt(true);
873
874        try {
875            obj = ois.readClassDescriptor();
876        } catch (IOException e) {
877            //e.printStackTrace();
878        } finally {
879            ois.close();
880        }
881
882	}
883
884    static class ExceptionalBufferedInputStream extends BufferedInputStream {
885        private boolean eof = false;
886        private IOException exception = null;
887        private boolean corrupt = false;
888
889        public ExceptionalBufferedInputStream(InputStream in) {
890            super(in);
891        }
892
893        public int read() throws IOException {
894            if (exception != null) {
895                throw exception;
896            }
897
898            if (eof) {
899                return -1;
900            }
901
902            if (corrupt) {
903                return 0;
904            }
905            return super.read();
906        }
907
908        public void setEOF(boolean eof) {
909            this.eof = eof;
910        }
911
912        public void setException(IOException exception) {
913            this.exception = exception;
914        }
915
916        public void setCorrupt(boolean corrupt) {
917            this.corrupt = corrupt;
918        }
919    }
920
921    // Regression Test for Harmony-2402
922    public void test_registerValidation() throws Exception {
923        ByteArrayOutputStream baos = new ByteArrayOutputStream();
924        new ObjectOutputStream(baos);
925        ObjectInputStream ois = new ObjectInputStream(
926                new ByteArrayInputStream(baos.toByteArray()));
927
928        try {
929            ois.registerValidation(null, 256);
930            fail("NotActiveException should be thrown");
931        } catch (NotActiveException nae) {
932            // expected
933        }
934    }
935
936
937    /**
938     * Sets up the fixture, for example, open a network connection. This method
939     * is called before a test is executed.
940     */
941    protected void setUp() throws Exception {
942        super.setUp();
943        oos = new ObjectOutputStream(bao = new ByteArrayOutputStream());
944    }
945}
946
947
948class Test implements Serializable {
949	private static final long serialVersionUID = 1L;
950
951	Class classes[] = new Class[] { byte.class, short.class, int.class,
952			long.class, boolean.class, char.class, float.class, double.class };
953
954	public boolean equals(Object o) {
955		if (!(o instanceof Test)) {
956			return false;
957		}
958		return Arrays.equals(classes, ((Test) o).classes);
959	}
960}
961