0001: /*
0002: * Licensed to the Apache Software Foundation (ASF) under one or more
0003: * contributor license agreements. See the NOTICE file distributed with
0004: * this work for additional information regarding copyright ownership.
0005: * The ASF licenses this file to You under the Apache License, Version 2.0
0006: * (the "License"); you may not use this file except in compliance with
0007: * the License. You may obtain a copy of the License at
0008: *
0009: * http://www.apache.org/licenses/LICENSE-2.0
0010: *
0011: * Unless required by applicable law or agreed to in writing, software
0012: * distributed under the License is distributed on an "AS IS" BASIS,
0013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014: * See the License for the specific language governing permissions and
0015: * limitations under the License.
0016: */
0017:
0018: package org.apache.harmony.luni.tests.java.io;
0019:
0020: import java.io.BufferedInputStream;
0021: import java.io.ByteArrayInputStream;
0022: import java.io.ByteArrayOutputStream;
0023: import java.io.Externalizable;
0024: import java.io.File;
0025: import java.io.FileInputStream;
0026: import java.io.FileOutputStream;
0027: import java.io.IOException;
0028: import java.io.InputStream;
0029: import java.io.InvalidObjectException;
0030: import java.io.NotActiveException;
0031: import java.io.ObjectInput;
0032: import java.io.ObjectInputStream;
0033: import java.io.ObjectInputValidation;
0034: import java.io.ObjectOutput;
0035: import java.io.ObjectOutputStream;
0036: import java.io.ObjectStreamClass;
0037: import java.io.OutputStream;
0038: import java.io.PipedInputStream;
0039: import java.io.PipedOutputStream;
0040: import java.io.Serializable;
0041: import java.io.SerializablePermission;
0042: import java.io.StreamCorruptedException;
0043: import java.security.Permission;
0044: import java.util.Arrays;
0045: import java.util.HashMap;
0046: import java.util.Hashtable;
0047: import java.util.Vector;
0048:
0049: import junit.framework.TestCase;
0050:
0051: import org.apache.harmony.testframework.serialization.SerializationTest;
0052: import org.apache.harmony.testframework.serialization.SerializationTest.SerializableAssert;
0053:
0054: @SuppressWarnings("serial")
0055: public class ObjectInputStreamTest extends TestCase implements
0056: Serializable {
0057:
0058: ObjectInputStream ois;
0059:
0060: ObjectOutputStream oos;
0061:
0062: ByteArrayOutputStream bao;
0063:
0064: public class SerializableTestHelper implements Serializable {
0065:
0066: public String aField1;
0067:
0068: public String aField2;
0069:
0070: SerializableTestHelper() {
0071: aField1 = null;
0072: aField2 = null;
0073: }
0074:
0075: SerializableTestHelper(String s, String t) {
0076: aField1 = s;
0077: aField2 = t;
0078: }
0079:
0080: private void readObject(ObjectInputStream ois) throws Exception {
0081: // note aField2 is not read
0082: ObjectInputStream.GetField fields = ois.readFields();
0083: aField1 = (String) fields.get("aField1", "Zap");
0084: }
0085:
0086: private void writeObject(ObjectOutputStream oos)
0087: throws IOException {
0088: // note aField2 is not written
0089: ObjectOutputStream.PutField fields = oos.putFields();
0090: fields.put("aField1", aField1);
0091: oos.writeFields();
0092: }
0093:
0094: public String getText1() {
0095: return aField1;
0096: }
0097:
0098: public void setText1(String s) {
0099: aField1 = s;
0100: }
0101:
0102: public String getText2() {
0103: return aField2;
0104: }
0105:
0106: public void setText2(String s) {
0107: aField2 = s;
0108: }
0109: }
0110:
0111: public static class A1 implements Serializable {
0112:
0113: static final long serialVersionUID = 5942584913446079661L;
0114:
0115: B1 b1 = new B1();
0116:
0117: B1 b2 = b1;
0118:
0119: Vector v = new Vector();
0120: }
0121:
0122: public static class B1 implements Serializable {
0123:
0124: int i = 5;
0125:
0126: Hashtable h = new Hashtable();
0127: }
0128:
0129: /**
0130: * @tests java.io.ObjectInputStream#readObject()
0131: */
0132: public void test_readObjectMissingClasses() throws Exception {
0133: SerializationTest.verifySelf(new A1(),
0134: new SerializableAssert() {
0135: public void assertDeserialized(
0136: Serializable initial,
0137: Serializable deserialized) {
0138: assertEquals(5, ((A1) deserialized).b1.i);
0139: }
0140: });
0141: }
0142:
0143: /**
0144: * @tests java.io.ObjectInputStream#ObjectInputStream(java.io.InputStream)
0145: */
0146: public void test_ConstructorLjava_io_InputStream()
0147: throws IOException {
0148: oos.writeDouble(Double.MAX_VALUE);
0149: oos.close();
0150: ois = new ObjectInputStream(new ByteArrayInputStream(bao
0151: .toByteArray()));
0152: ois.close();
0153: oos.close();
0154:
0155: try {
0156: ois = new ObjectInputStream(new ByteArrayInputStream(
0157: new byte[90]));
0158: fail("StreamCorruptedException expected");
0159: } catch (StreamCorruptedException e) {
0160: // Expected
0161: }
0162: }
0163:
0164: /**
0165: * @tests java.io.ObjectInputStream#ObjectInputStream(java.io.InputStream)
0166: */
0167: public void test_ConstructorLjava_io_InputStream_subtest0()
0168: throws IOException {
0169: SecurityManager sm = System.getSecurityManager();
0170: System.setSecurityManager(new SecurityManager() {
0171: Permission golden = new SerializablePermission(
0172: "enableSubclassImplementation");
0173:
0174: @Override
0175: public void checkPermission(Permission p) {
0176: if (golden.equals(p)) {
0177: throw new SecurityException();
0178: }
0179: }
0180: });
0181:
0182: try {
0183: ByteArrayOutputStream out = new ByteArrayOutputStream();
0184: ObjectOutputStream obout = new ObjectOutputStream(out);
0185: obout.write(0);
0186: obout.close();
0187:
0188: InputStream in = new ByteArrayInputStream(out.toByteArray());
0189:
0190: // should not cause SecurityException
0191: new ObjectInputStream(in);
0192: in.reset();
0193:
0194: // should not cause SecurityException
0195: new ObjectInputStream(in) {
0196: };
0197: in.reset();
0198:
0199: try {
0200: new ObjectInputStream(in) {
0201: @Override
0202: public Object readUnshared() throws IOException,
0203: ClassNotFoundException {
0204: return null;
0205: }
0206: };
0207: fail("should throw SecurityException 1");
0208: } catch (SecurityException e) {
0209: // Expected
0210: }
0211:
0212: in.reset();
0213: try {
0214: new ObjectInputStream(in) {
0215: @Override
0216: public GetField readFields() throws IOException,
0217: ClassNotFoundException, NotActiveException {
0218: return null;
0219: }
0220: };
0221: fail("should throw SecurityException 2");
0222: } catch (SecurityException e) {
0223: // Expected
0224: }
0225: } finally {
0226: System.setSecurityManager(sm);
0227: }
0228: }
0229:
0230: /**
0231: * @tests java.io.ObjectInputStream#available()
0232: */
0233: public void test_available() throws IOException {
0234: oos.writeBytes("HelloWorld");
0235: oos.close();
0236: ois = new ObjectInputStream(new ByteArrayInputStream(bao
0237: .toByteArray()));
0238: assertEquals("Read incorrect bytes", 10, ois.available());
0239: ois.close();
0240: }
0241:
0242: /**
0243: * @tests java.io.ObjectInputStream#close()
0244: */
0245: public void test_close() throws IOException {
0246: oos.writeBytes("HelloWorld");
0247: oos.close();
0248: ois = new ObjectInputStream(new ByteArrayInputStream(bao
0249: .toByteArray()));
0250: ois.close();
0251: }
0252:
0253: /**
0254: * @tests java.io.ObjectInputStream#defaultReadObject()
0255: */
0256: public void test_defaultReadObject() throws Exception {
0257: // SM. This method may as well be private, as if called directly it
0258: // throws an exception.
0259: String s = "HelloWorld";
0260: oos.writeObject(s);
0261: oos.close();
0262: ois = new ObjectInputStream(new ByteArrayInputStream(bao
0263: .toByteArray()));
0264: try {
0265: ois.defaultReadObject();
0266: fail("NotActiveException expected");
0267: } catch (NotActiveException e) {
0268: // Desired behavior
0269: } finally {
0270: ois.close();
0271: }
0272: }
0273:
0274: /**
0275: * @tests java.io.ObjectInputStream#read()
0276: */
0277: public void test_read() throws IOException {
0278: oos.write('T');
0279: oos.close();
0280: ois = new ObjectInputStream(new ByteArrayInputStream(bao
0281: .toByteArray()));
0282: assertEquals("Read incorrect byte value", 'T', ois.read());
0283: ois.close();
0284: }
0285:
0286: /**
0287: * @tests java.io.ObjectInputStream#read(byte[], int, int)
0288: */
0289: public void test_read$BII() throws IOException {
0290: byte[] buf = new byte[10];
0291: oos.writeBytes("HelloWorld");
0292: oos.close();
0293: ois = new ObjectInputStream(new ByteArrayInputStream(bao
0294: .toByteArray()));
0295: ois.read(buf, 0, 10);
0296: ois.close();
0297: assertEquals("Read incorrect bytes", "HelloWorld", new String(
0298: buf, 0, 10));
0299: }
0300:
0301: /**
0302: * @tests java.io.ObjectInputStream#readBoolean()
0303: */
0304: public void test_readBoolean() throws IOException {
0305: oos.writeBoolean(true);
0306: oos.close();
0307: ois = new ObjectInputStream(new ByteArrayInputStream(bao
0308: .toByteArray()));
0309: assertTrue("Read incorrect boolean value", ois.readBoolean());
0310: ois.close();
0311: }
0312:
0313: /**
0314: * @tests java.io.ObjectInputStream#readByte()
0315: */
0316: public void test_readByte() throws IOException {
0317: oos.writeByte(127);
0318: oos.close();
0319: ois = new ObjectInputStream(new ByteArrayInputStream(bao
0320: .toByteArray()));
0321: assertEquals("Read incorrect byte value", 127, ois.readByte());
0322: ois.close();
0323: }
0324:
0325: /**
0326: * @tests java.io.ObjectInputStream#readChar()
0327: */
0328: public void test_readChar() throws IOException {
0329: oos.writeChar('T');
0330: oos.close();
0331: ois = new ObjectInputStream(new ByteArrayInputStream(bao
0332: .toByteArray()));
0333: assertEquals("Read incorrect char value", 'T', ois.readChar());
0334: ois.close();
0335: }
0336:
0337: /**
0338: * @tests java.io.ObjectInputStream#readDouble()
0339: */
0340: public void test_readDouble() throws IOException {
0341: oos.writeDouble(Double.MAX_VALUE);
0342: oos.close();
0343: ois = new ObjectInputStream(new ByteArrayInputStream(bao
0344: .toByteArray()));
0345: assertTrue("Read incorrect double value",
0346: ois.readDouble() == Double.MAX_VALUE);
0347: ois.close();
0348: }
0349:
0350: /**
0351: * @tests java.io.ObjectInputStream#readFields()
0352: */
0353: public void test_readFields() throws Exception {
0354:
0355: SerializableTestHelper sth;
0356:
0357: /*
0358: * "SerializableTestHelper" is an object created for these tests with
0359: * two fields (Strings) and simple implementations of readObject and
0360: * writeObject which simply read and write the first field but not the
0361: * second
0362: */
0363:
0364: oos.writeObject(new SerializableTestHelper("Gabba", "Jabba"));
0365: oos.flush();
0366: ois = new ObjectInputStream(new ByteArrayInputStream(bao
0367: .toByteArray()));
0368: sth = (SerializableTestHelper) (ois.readObject());
0369: assertEquals(
0370: "readFields / writeFields failed--first field not set",
0371: "Gabba", sth.getText1());
0372: assertNull(
0373: "readFields / writeFields failed--second field should not have been set",
0374: sth.getText2());
0375: }
0376:
0377: /**
0378: * @tests java.io.ObjectInputStream#readFloat()
0379: */
0380: public void test_readFloat() throws IOException {
0381: oos.writeFloat(Float.MAX_VALUE);
0382: oos.close();
0383: ois = new ObjectInputStream(new ByteArrayInputStream(bao
0384: .toByteArray()));
0385: assertTrue("Read incorrect float value",
0386: ois.readFloat() == Float.MAX_VALUE);
0387: ois.close();
0388: }
0389:
0390: /**
0391: * @tests java.io.ObjectInputStream#readFully(byte[])
0392: */
0393: public void test_readFully$B() throws IOException {
0394: byte[] buf = new byte[10];
0395: oos.writeBytes("HelloWorld");
0396: oos.close();
0397: ois = new ObjectInputStream(new ByteArrayInputStream(bao
0398: .toByteArray()));
0399: ois.readFully(buf);
0400: ois.close();
0401: assertEquals("Read incorrect bytes", "HelloWorld", new String(
0402: buf, 0, 10));
0403: }
0404:
0405: /**
0406: * @tests java.io.ObjectInputStream#readFully(byte[], int, int)
0407: */
0408: public void test_readFully$BII() throws IOException {
0409: byte[] buf = new byte[10];
0410: oos.writeBytes("HelloWorld");
0411: oos.close();
0412: ois = new ObjectInputStream(new ByteArrayInputStream(bao
0413: .toByteArray()));
0414: ois.readFully(buf, 0, 10);
0415: ois.close();
0416: assertEquals("Read incorrect bytes", "HelloWorld", new String(
0417: buf, 0, 10));
0418: }
0419:
0420: /**
0421: * @tests java.io.ObjectInputStream#readInt()
0422: */
0423: public void test_readInt() throws IOException {
0424: oos.writeInt(Integer.MAX_VALUE);
0425: oos.close();
0426: ois = new ObjectInputStream(new ByteArrayInputStream(bao
0427: .toByteArray()));
0428: assertTrue("Read incorrect int value",
0429: ois.readInt() == Integer.MAX_VALUE);
0430: ois.close();
0431: }
0432:
0433: /**
0434: * @tests java.io.ObjectInputStream#readLine()
0435: */
0436: @SuppressWarnings("deprecation")
0437: public void test_readLine() throws IOException {
0438: oos.writeBytes("HelloWorld\nSecondLine");
0439: oos.close();
0440: ois = new ObjectInputStream(new ByteArrayInputStream(bao
0441: .toByteArray()));
0442: ois.readLine();
0443: assertEquals("Read incorrect string value", "SecondLine", ois
0444: .readLine());
0445: ois.close();
0446: }
0447:
0448: /**
0449: * @tests java.io.ObjectInputStream#readLong()
0450: */
0451: public void test_readLong() throws IOException {
0452: oos.writeLong(Long.MAX_VALUE);
0453: oos.close();
0454: ois = new ObjectInputStream(new ByteArrayInputStream(bao
0455: .toByteArray()));
0456: assertTrue("Read incorrect long value",
0457: ois.readLong() == Long.MAX_VALUE);
0458: ois.close();
0459: }
0460:
0461: /**
0462: * @tests java.io.ObjectInputStream#readObject()
0463: */
0464: public void test_readObject() throws Exception {
0465: String s = "HelloWorld";
0466: oos.writeObject(s);
0467: oos.close();
0468: ois = new ObjectInputStream(new ByteArrayInputStream(bao
0469: .toByteArray()));
0470: assertEquals("Read incorrect Object value", s, ois.readObject());
0471: ois.close();
0472:
0473: // Regression for HARMONY-91
0474: // dynamically create serialization byte array for the next hierarchy:
0475: // - class A implements Serializable
0476: // - class C extends A
0477:
0478: byte[] cName = C.class.getName().getBytes();
0479: byte[] aName = A.class.getName().getBytes();
0480:
0481: ByteArrayOutputStream out = new ByteArrayOutputStream();
0482:
0483: byte[] begStream = new byte[] { (byte) 0xac, (byte) 0xed, // STREAM_MAGIC
0484: (byte) 0x00, (byte) 0x05, // STREAM_VERSION
0485: (byte) 0x73, // TC_OBJECT
0486: (byte) 0x72, // TC_CLASSDESC
0487: (byte) 0x00, // only first byte for C class name length
0488: };
0489:
0490: out.write(begStream, 0, begStream.length);
0491: out.write(cName.length); // second byte for C class name length
0492: out.write(cName, 0, cName.length); // C class name
0493:
0494: byte[] midStream = new byte[] { (byte) 0x00, (byte) 0x00,
0495: (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
0496: (byte) 0x00, (byte) 0x21, // serialVersionUID = 33L
0497: (byte) 0x02, // flags
0498: (byte) 0x00, (byte) 0x00, // fields : none
0499: (byte) 0x78, // TC_ENDBLOCKDATA
0500: (byte) 0x72, // Super class for C: TC_CLASSDESC for A class
0501: (byte) 0x00, // only first byte for A class name length
0502: };
0503:
0504: out.write(midStream, 0, midStream.length);
0505: out.write(aName.length); // second byte for A class name length
0506: out.write(aName, 0, aName.length); // A class name
0507:
0508: byte[] endStream = new byte[] {
0509: (byte) 0x00,
0510: (byte) 0x00,
0511: (byte) 0x00,
0512: (byte) 0x00,
0513: (byte) 0x00,
0514: (byte) 0x00,
0515: (byte) 0x00,
0516: (byte) 0x0b, // serialVersionUID = 11L
0517: (byte) 0x02, // flags
0518: (byte) 0x00,
0519: (byte) 0x01, // fields
0520:
0521: (byte) 0x4c, // field description: type L (object)
0522: (byte) 0x00,
0523: (byte) 0x04, // length
0524: // field = 'name'
0525: (byte) 0x6e,
0526: (byte) 0x61,
0527: (byte) 0x6d,
0528: (byte) 0x65,
0529:
0530: (byte) 0x74, // className1: TC_STRING
0531: (byte) 0x00,
0532: (byte) 0x12, // length
0533: //
0534: (byte) 0x4c, (byte) 0x6a, (byte) 0x61, (byte) 0x76,
0535: (byte) 0x61, (byte) 0x2f, (byte) 0x6c, (byte) 0x61,
0536: (byte) 0x6e, (byte) 0x67, (byte) 0x2f, (byte) 0x53,
0537: (byte) 0x74, (byte) 0x72, (byte) 0x69, (byte) 0x6e,
0538: (byte) 0x67, (byte) 0x3b,
0539:
0540: (byte) 0x78, // TC_ENDBLOCKDATA
0541: (byte) 0x70, // NULL super class for A class
0542:
0543: // classdata
0544: (byte) 0x74, // TC_STRING
0545: (byte) 0x00, (byte) 0x04, // length
0546: (byte) 0x6e, (byte) 0x61, (byte) 0x6d, (byte) 0x65, // value
0547: };
0548:
0549: out.write(endStream, 0, endStream.length);
0550: out.flush();
0551:
0552: // read created serial. form
0553: ObjectInputStream ois = new ObjectInputStream(
0554: new ByteArrayInputStream(out.toByteArray()));
0555: Object o = ois.readObject();
0556: assertEquals(C.class, o.getClass());
0557:
0558: // Regression for HARMONY-846
0559: assertNull(new ObjectInputStream() {
0560: }.readObject());
0561: }
0562:
0563: /**
0564: * @tests java.io.ObjectInputStream#readObjectOverride()
0565: */
0566: public void test_readObjectOverride() throws Exception {
0567: // Regression for HARMONY-846
0568: assertNull(new ObjectInputStream() {
0569:
0570: @Override
0571: public Object readObjectOverride() throws IOException,
0572: ClassNotFoundException {
0573: return super .readObjectOverride();
0574: }
0575:
0576: }.readObjectOverride());
0577: }
0578:
0579: public static class A implements Serializable {
0580:
0581: private static final long serialVersionUID = 11L;
0582:
0583: public String name = "name";
0584: }
0585:
0586: public static class B extends A {
0587: }
0588:
0589: public static class C extends B {
0590:
0591: private static final long serialVersionUID = 33L;
0592: }
0593:
0594: /**
0595: * @tests java.io.ObjectInputStream#readObject()
0596: */
0597: public void test_readObjectCorrupt() throws IOException,
0598: ClassNotFoundException {
0599: byte[] bytes = { 00, 00, 00, 0x64, 0x43, 0x48, (byte) 0xFD,
0600: 0x71, 00, 00, 0x0B, (byte) 0xB8, 0x4D, 0x65 };
0601: ByteArrayInputStream bin = new ByteArrayInputStream(bytes);
0602: try {
0603: ObjectInputStream in = new ObjectInputStream(bin);
0604: in.readObject();
0605: fail("Unexpected read of corrupted stream");
0606: } catch (StreamCorruptedException e) {
0607: // Expected
0608: }
0609: }
0610:
0611: /**
0612: * @tests java.io.ObjectInputStream#readShort()
0613: */
0614: public void test_readShort() throws IOException {
0615: oos.writeShort(Short.MAX_VALUE);
0616: oos.close();
0617: ois = new ObjectInputStream(new ByteArrayInputStream(bao
0618: .toByteArray()));
0619: assertTrue("Read incorrect short value",
0620: ois.readShort() == Short.MAX_VALUE);
0621: ois.close();
0622: }
0623:
0624: /**
0625: * @tests java.io.ObjectInputStream#readUnsignedByte()
0626: */
0627: public void test_readUnsignedByte() throws IOException {
0628: oos.writeByte(-1);
0629: oos.close();
0630: ois = new ObjectInputStream(new ByteArrayInputStream(bao
0631: .toByteArray()));
0632: assertEquals("Read incorrect unsignedByte value", 255, ois
0633: .readUnsignedByte());
0634: ois.close();
0635: }
0636:
0637: /**
0638: * @tests java.io.ObjectInputStream#readUnsignedShort()
0639: */
0640: public void test_readUnsignedShort() throws IOException {
0641: oos.writeShort(-1);
0642: oos.close();
0643: ois = new ObjectInputStream(new ByteArrayInputStream(bao
0644: .toByteArray()));
0645: assertEquals("Read incorrect unsignedShort value", 65535, ois
0646: .readUnsignedShort());
0647: ois.close();
0648: }
0649:
0650: /**
0651: * @tests java.io.ObjectInputStream#readUTF()
0652: */
0653: public void test_readUTF() throws IOException {
0654: oos.writeUTF("HelloWorld");
0655: oos.close();
0656: ois = new ObjectInputStream(new ByteArrayInputStream(bao
0657: .toByteArray()));
0658: assertEquals("Read incorrect utf value", "HelloWorld", ois
0659: .readUTF());
0660: ois.close();
0661: }
0662:
0663: /**
0664: * @tests java.io.ObjectInputStream#skipBytes(int)
0665: */
0666: public void test_skipBytesI() throws IOException {
0667: byte[] buf = new byte[10];
0668: oos.writeBytes("HelloWorld");
0669: oos.close();
0670: ois = new ObjectInputStream(new ByteArrayInputStream(bao
0671: .toByteArray()));
0672: ois.skipBytes(5);
0673: ois.read(buf, 0, 5);
0674: ois.close();
0675: assertEquals("Skipped incorrect bytes", "World", new String(
0676: buf, 0, 5));
0677:
0678: // Regression for HARMONY-844
0679: try {
0680: new ObjectInputStream() {
0681: }.skipBytes(0);
0682: fail("NullPointerException expected");
0683: } catch (NullPointerException e) {
0684: }
0685: }
0686:
0687: // Regression Test for JIRA 2192
0688: public void test_readObject_withPrimitiveClass() throws Exception {
0689: File file = new File("test.ser");
0690: file.deleteOnExit();
0691: Test test = new Test();
0692: ObjectOutputStream out = new ObjectOutputStream(
0693: new FileOutputStream(file));
0694: out.writeObject(test);
0695: out.close();
0696:
0697: ObjectInputStream in = new ObjectInputStream(
0698: new FileInputStream(file));
0699: Test another = (Test) in.readObject();
0700: in.close();
0701: assertEquals(test, another);
0702: }
0703:
0704: //Regression Test for JIRA-2249
0705: public static class ObjectOutputStreamWithWriteDesc extends
0706: ObjectOutputStream {
0707: public ObjectOutputStreamWithWriteDesc(OutputStream os)
0708: throws IOException {
0709: super (os);
0710: }
0711:
0712: @Override
0713: public void writeClassDescriptor(ObjectStreamClass desc)
0714: throws IOException {
0715: }
0716: }
0717:
0718: public static class ObjectIutputStreamWithReadDesc extends
0719: ObjectInputStream {
0720: private Class returnClass;
0721:
0722: public ObjectIutputStreamWithReadDesc(InputStream is,
0723: Class returnClass) throws IOException {
0724: super (is);
0725: this .returnClass = returnClass;
0726: }
0727:
0728: @Override
0729: public ObjectStreamClass readClassDescriptor()
0730: throws IOException, ClassNotFoundException {
0731: return ObjectStreamClass.lookup(returnClass);
0732:
0733: }
0734: }
0735:
0736: static class TestClassForSerialization implements Serializable {
0737: private static final long serialVersionUID = 1L;
0738: }
0739:
0740: public void test_ClassDescriptor() throws IOException,
0741: ClassNotFoundException {
0742:
0743: ByteArrayOutputStream baos = new ByteArrayOutputStream();
0744: ObjectOutputStreamWithWriteDesc oos = new ObjectOutputStreamWithWriteDesc(
0745: baos);
0746: oos.writeObject(String.class);
0747: oos.close();
0748: Class cls = TestClassForSerialization.class;
0749: ByteArrayInputStream bais = new ByteArrayInputStream(baos
0750: .toByteArray());
0751: ObjectIutputStreamWithReadDesc ois = new ObjectIutputStreamWithReadDesc(
0752: bais, cls);
0753: Object obj = ois.readObject();
0754: ois.close();
0755: assertEquals(cls, obj);
0756: }
0757:
0758: // Regression Test for JIRA-2340
0759: public static class ObjectOutputStreamWithWriteDesc1 extends
0760: ObjectOutputStream {
0761: public ObjectOutputStreamWithWriteDesc1(OutputStream os)
0762: throws IOException {
0763: super (os);
0764: }
0765:
0766: @Override
0767: public void writeClassDescriptor(ObjectStreamClass desc)
0768: throws IOException {
0769: super .writeClassDescriptor(desc);
0770: }
0771: }
0772:
0773: public static class ObjectIutputStreamWithReadDesc1 extends
0774: ObjectInputStream {
0775:
0776: public ObjectIutputStreamWithReadDesc1(InputStream is)
0777: throws IOException {
0778: super (is);
0779: }
0780:
0781: @Override
0782: public ObjectStreamClass readClassDescriptor()
0783: throws IOException, ClassNotFoundException {
0784: return super .readClassDescriptor();
0785: }
0786: }
0787:
0788: // Regression test for Harmony-1921
0789: public static class ObjectInputStreamWithResolve extends
0790: ObjectInputStream {
0791: public ObjectInputStreamWithResolve(InputStream in)
0792: throws IOException {
0793: super (in);
0794: }
0795:
0796: @Override
0797: @SuppressWarnings("unchecked")
0798: protected Class resolveClass(ObjectStreamClass desc)
0799: throws IOException, ClassNotFoundException {
0800: if (desc.getName().equals(
0801: "org.apache.harmony.luni.tests.pkg1.TestClass")) {
0802: return org.apache.harmony.luni.tests.pkg2.TestClass.class;
0803: }
0804: return super .resolveClass(desc);
0805: }
0806: }
0807:
0808: public void test_resolveClass() throws Exception {
0809: org.apache.harmony.luni.tests.pkg1.TestClass to1 = new org.apache.harmony.luni.tests.pkg1.TestClass();
0810: to1.i = 555;
0811: ByteArrayOutputStream baos = new ByteArrayOutputStream();
0812: ObjectOutputStream oos = new ObjectOutputStream(baos);
0813: oos.writeObject(to1);
0814: oos.flush();
0815: byte[] bytes = baos.toByteArray();
0816: ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
0817: ObjectInputStream ois = new ObjectInputStreamWithResolve(bais);
0818: org.apache.harmony.luni.tests.pkg2.TestClass to2 = (org.apache.harmony.luni.tests.pkg2.TestClass) ois
0819: .readObject();
0820:
0821: if (to2.i != to1.i) {
0822: fail("Wrong object read. Expected val: " + to1.i
0823: + ", got: " + to2.i);
0824: }
0825: }
0826:
0827: static class ObjectInputStreamWithResolveObject extends
0828: ObjectInputStream {
0829:
0830: public static Integer intObj = Integer.valueOf(1000);
0831:
0832: public ObjectInputStreamWithResolveObject(InputStream in)
0833: throws IOException {
0834: super (in);
0835: enableResolveObject(true);
0836: }
0837:
0838: @Override
0839: protected Object resolveObject(Object obj) throws IOException {
0840: if (obj instanceof Integer) {
0841: obj = intObj;
0842: }
0843: return super .resolveObject(obj);
0844: }
0845: }
0846:
0847: /**
0848: * @tests java.io.ObjectInputStream#resolveObject(Object)
0849: */
0850: public void test_resolveObjectLjava_lang_Object() throws Exception {
0851: // Write an Integer object into memory
0852: Integer original = new Integer(10);
0853: ByteArrayOutputStream baos = new ByteArrayOutputStream();
0854: ObjectOutputStream oos = new ObjectOutputStream(baos);
0855: oos.writeObject(original);
0856: oos.flush();
0857: oos.close();
0858:
0859: // Read the object from memory
0860: byte[] bytes = baos.toByteArray();
0861: ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
0862: ObjectInputStreamWithResolveObject ois = new ObjectInputStreamWithResolveObject(
0863: bais);
0864: Integer actual = (Integer) ois.readObject();
0865: ois.close();
0866:
0867: // object should be resolved from 10 to 1000
0868: assertEquals(ObjectInputStreamWithResolveObject.intObj, actual);
0869: }
0870:
0871: public void test_readClassDescriptor() throws IOException,
0872: ClassNotFoundException {
0873:
0874: ByteArrayOutputStream baos = new ByteArrayOutputStream();
0875: ObjectOutputStreamWithWriteDesc1 oos = new ObjectOutputStreamWithWriteDesc1(
0876: baos);
0877: ObjectStreamClass desc = ObjectStreamClass
0878: .lookup(TestClassForSerialization.class);
0879: oos.writeClassDescriptor(desc);
0880: oos.close();
0881:
0882: byte[] bytes = baos.toByteArray();
0883: ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
0884: ObjectIutputStreamWithReadDesc1 ois = new ObjectIutputStreamWithReadDesc1(
0885: bais);
0886: Object obj = ois.readClassDescriptor();
0887: ois.close();
0888: assertEquals(desc.getClass(), obj.getClass());
0889:
0890: //eof
0891: bais = new ByteArrayInputStream(bytes);
0892: ExceptionalBufferedInputStream bis = new ExceptionalBufferedInputStream(
0893: bais);
0894: ois = new ObjectIutputStreamWithReadDesc1(bis);
0895:
0896: bis.setEOF(true);
0897:
0898: try {
0899: obj = ois.readClassDescriptor();
0900: } catch (IOException e) {
0901: //e.printStackTrace();
0902: } finally {
0903: ois.close();
0904: }
0905:
0906: //throw exception
0907: bais = new ByteArrayInputStream(bytes);
0908: bis = new ExceptionalBufferedInputStream(bais);
0909: ois = new ObjectIutputStreamWithReadDesc1(bis);
0910:
0911: bis.setException(new IOException());
0912:
0913: try {
0914: obj = ois.readClassDescriptor();
0915: } catch (IOException e) {
0916: //e.printStackTrace();
0917: } finally {
0918: ois.close();
0919: }
0920:
0921: //corrupt
0922: bais = new ByteArrayInputStream(bytes);
0923: bis = new ExceptionalBufferedInputStream(bais);
0924: ois = new ObjectIutputStreamWithReadDesc1(bis);
0925:
0926: bis.setCorrupt(true);
0927:
0928: try {
0929: obj = ois.readClassDescriptor();
0930: } catch (IOException e) {
0931: //e.printStackTrace();
0932: } finally {
0933: ois.close();
0934: }
0935: }
0936:
0937: static class ExceptionalBufferedInputStream extends
0938: BufferedInputStream {
0939: private boolean eof = false;
0940: private IOException exception = null;
0941: private boolean corrupt = false;
0942:
0943: public ExceptionalBufferedInputStream(InputStream in) {
0944: super (in);
0945: }
0946:
0947: @Override
0948: public int read() throws IOException {
0949: if (exception != null) {
0950: throw exception;
0951: }
0952:
0953: if (eof) {
0954: return -1;
0955: }
0956:
0957: if (corrupt) {
0958: return 0;
0959: }
0960: return super .read();
0961: }
0962:
0963: public void setEOF(boolean eof) {
0964: this .eof = eof;
0965: }
0966:
0967: public void setException(IOException exception) {
0968: this .exception = exception;
0969: }
0970:
0971: public void setCorrupt(boolean corrupt) {
0972: this .corrupt = corrupt;
0973: }
0974: }
0975:
0976: public static class ObjectIutputStreamWithReadDesc2 extends
0977: ObjectInputStream {
0978: private Class returnClass;
0979:
0980: public ObjectIutputStreamWithReadDesc2(InputStream is,
0981: Class returnClass) throws IOException {
0982: super (is);
0983: this .returnClass = returnClass;
0984: }
0985:
0986: @Override
0987: public ObjectStreamClass readClassDescriptor()
0988: throws IOException, ClassNotFoundException {
0989: ObjectStreamClass osc = super .readClassDescriptor();
0990:
0991: if (osc.getName().equals(returnClass.getName())) {
0992: return ObjectStreamClass.lookup(returnClass);
0993: }
0994: return osc;
0995: }
0996: }
0997:
0998: /*
0999: * Testing classDescriptor replacement with the value generated by
1000: * ObjectStreamClass.lookup() method.
1001: * Regression test for HARMONY-4638
1002: */
1003: public void test_readClassDescriptor_1() throws IOException,
1004: ClassNotFoundException {
1005: A a = new A();
1006: a.name = "It's a test";
1007: PipedOutputStream pout = new PipedOutputStream();
1008: PipedInputStream pin = new PipedInputStream(pout);
1009: ObjectOutputStream out = new ObjectOutputStream(pout);
1010: ObjectInputStream in = new ObjectIutputStreamWithReadDesc2(pin,
1011: A.class);
1012:
1013: // test single object
1014: out.writeObject(a);
1015: A a1 = (A) in.readObject();
1016: assertEquals("Single case: incorrectly read the field of A",
1017: a.name, a1.name);
1018:
1019: // test cyclic reference
1020: HashMap m = new HashMap();
1021: a = new A();
1022: a.name = "It's a test 0";
1023: a1 = new A();
1024: a1.name = "It's a test 1";
1025: m.put("0", a);
1026: m.put("1", a1);
1027: out.writeObject(m);
1028: HashMap m1 = (HashMap) in.readObject();
1029: assertEquals("Incorrectly read the field of A", a.name, ((A) m1
1030: .get("0")).name);
1031: assertEquals("Incorrectly read the field of A1", a1.name,
1032: ((A) m1.get("1")).name);
1033: }
1034:
1035: public void test_registerValidation() throws Exception {
1036: // Regression Test for Harmony-2402
1037: ByteArrayOutputStream baos = new ByteArrayOutputStream();
1038: new ObjectOutputStream(baos);
1039: ObjectInputStream ois = new ObjectInputStream(
1040: new ByteArrayInputStream(baos.toByteArray()));
1041:
1042: try {
1043: ois.registerValidation(null, 256);
1044: fail("NotActiveException should be thrown");
1045: } catch (NotActiveException nae) {
1046: // expected
1047: }
1048:
1049: // Regression Test for Harmony-3916
1050: baos = new ByteArrayOutputStream();
1051: ObjectOutputStream oos = new ObjectOutputStream(baos);
1052: oos.writeObject(new RegisterValidationClass());
1053: oos.close();
1054: ByteArrayInputStream bais = new ByteArrayInputStream(baos
1055: .toByteArray());
1056: ObjectInputStream fis = new ObjectInputStream(bais);
1057: // should not throw NotActiveException
1058: fis.readObject();
1059: }
1060:
1061: private static class RegisterValidationClass implements
1062: Serializable {
1063: @SuppressWarnings("unused")
1064: private A a = new A();
1065:
1066: private void readObject(ObjectInputStream stream)
1067: throws IOException, ClassNotFoundException {
1068: stream.defaultReadObject();
1069: stream.registerValidation(new MockObjectInputValidation(),
1070: 0);
1071: }
1072: }
1073:
1074: private static class MockObjectInputValidation implements
1075: ObjectInputValidation {
1076: public void validateObject() throws InvalidObjectException {
1077:
1078: }
1079: }
1080:
1081: //Regression Test for HARMONY-3726
1082: public void test_readObject_array() throws Exception {
1083:
1084: final String resourcePrefix = ObjectInputStreamTest.class
1085: .getPackage().getName().replace('.', '/');
1086:
1087: // ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("/temp/test_array_strings.ser"));
1088: // TestArray ta = new TestArray(new String[] { "AAA", "BBB" });
1089: // oos.writeObject(ta);
1090: // oos.close();
1091: // oos = new ObjectOutputStream(new FileOutputStream("/temp/test_array_integers.ser"));
1092: // ta = new TestArray(new Integer[] { 10, 20 });
1093: // oos.writeObject(ta);
1094: // oos.close();
1095:
1096: ObjectInputStream oin = new ObjectInputStream(this .getClass()
1097: .getClassLoader().getResourceAsStream(
1098: "serialization/" + resourcePrefix
1099: + "/test_array_strings.ser"));
1100: TestArray testArray = (TestArray) oin.readObject();
1101: String[] strings = new String[] { "AAA", "BBB" };
1102: assertTrue(java.util.Arrays.equals(strings, testArray.array));
1103:
1104: oin = new ObjectInputStream(this .getClass().getClassLoader()
1105: .getResourceAsStream(
1106: "serialization/" + resourcePrefix
1107: + "/test_array_integers.ser"));
1108: testArray = (TestArray) oin.readObject();
1109: Integer[] integers = new Integer[] { 10, 20 };
1110: assertTrue(java.util.Arrays.equals(integers, testArray.array));
1111: }
1112:
1113: public static class TestExtObject implements Externalizable {
1114: public void writeExternal(ObjectOutput out) throws IOException {
1115: out.writeInt(10);
1116: }
1117:
1118: public void readExternal(ObjectInput in) throws IOException,
1119: ClassNotFoundException {
1120: in.readInt();
1121: }
1122: }
1123:
1124: static class TestObjectOutputStream extends ObjectOutputStream {
1125: private ObjectStreamClass[] objs;
1126: private int pos = 0;
1127:
1128: public TestObjectOutputStream(OutputStream out,
1129: ObjectStreamClass[] objs) throws IOException {
1130: super (out);
1131: this .objs = objs;
1132: }
1133:
1134: @Override
1135: protected void writeClassDescriptor(ObjectStreamClass osc)
1136: throws IOException {
1137: objs[pos++] = osc;
1138: }
1139: }
1140:
1141: static class TestObjectInputStream extends ObjectInputStream {
1142: private ObjectStreamClass[] objs;
1143: private int pos = 0;
1144:
1145: public TestObjectInputStream(InputStream in,
1146: ObjectStreamClass[] objs) throws IOException {
1147: super (in);
1148: this .objs = objs;
1149: }
1150:
1151: @Override
1152: protected ObjectStreamClass readClassDescriptor()
1153: throws IOException, ClassNotFoundException {
1154: return objs[pos++];
1155: }
1156: }
1157:
1158: // Regression test for HARMONY-4996
1159: public void test_readObject_replacedClassDescriptor()
1160: throws Exception {
1161: ObjectStreamClass[] objs = new ObjectStreamClass[1000];
1162: PipedOutputStream pout = new PipedOutputStream();
1163: PipedInputStream pin = new PipedInputStream(pout);
1164: ObjectOutputStream oout = new TestObjectOutputStream(pout, objs);
1165: oout.writeObject(new TestExtObject());
1166: oout.writeObject("test");
1167: oout.close();
1168: ObjectInputStream oin = new TestObjectInputStream(pin, objs);
1169: oin.readObject();
1170: oin.readObject();
1171: }
1172:
1173: /**
1174: * Sets up the fixture, for example, open a network connection. This method
1175: * is called before a test is executed.
1176: */
1177: @Override
1178: protected void setUp() throws Exception {
1179: super .setUp();
1180: oos = new ObjectOutputStream(bao = new ByteArrayOutputStream());
1181: }
1182: }
1183:
1184: class TestArray implements Serializable {
1185: private static final long serialVersionUID = 1L;
1186:
1187: public Object[] array;
1188:
1189: public TestArray(Object[] array) {
1190: this .array = array;
1191: }
1192:
1193: }
1194:
1195: class Test implements Serializable {
1196: private static final long serialVersionUID = 1L;
1197:
1198: Class classes[] = new Class[] { byte.class, short.class, int.class,
1199: long.class, boolean.class, char.class, float.class,
1200: double.class };
1201:
1202: @Override
1203: public boolean equals(Object o) {
1204: if (!(o instanceof Test)) {
1205: return false;
1206: }
1207: return Arrays.equals(classes, ((Test) o).classes);
1208: }
1209: }
|