PageRenderTime 42ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/external/apache-harmony/luni/src/test/api/common/org/apache/harmony/luni/tests/java/io/ObjectInputStreamTest.java

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