ParcelTest: Add test for b/35384981

Test that createByteArray returns null if readInPlace fails.

Test: atest ParcelTest
Bug: 35384981

Change-Id: Iaa0e9f66609fea21af095d22ad7d4cbaf7fbac67
diff --git a/tests/tests/os/src/android/os/cts/ParcelTest.java b/tests/tests/os/src/android/os/cts/ParcelTest.java
index c65c61b..7645477 100644
--- a/tests/tests/os/src/android/os/cts/ParcelTest.java
+++ b/tests/tests/os/src/android/os/cts/ParcelTest.java
@@ -3225,4 +3225,29 @@
         assertEquals(42, list.get(0).getValue());
         assertEquals(56, list.get(1).getValue());
     }
+
+    // http://b/35384981
+    public void testCreateArrayWithTruncatedParcel() {
+        Parcel parcel = Parcel.obtain();
+        parcel.writeByteArray(new byte[] { 'a', 'b' });
+        byte[] marshalled = parcel.marshall();
+
+        // Test that createByteArray returns null with a truncated parcel.
+        parcel = Parcel.obtain();
+        parcel.unmarshall(marshalled, 0, marshalled.length);
+        parcel.setDataPosition(0);
+        // Shorten the data size by 2 to remove padding at the end of the array.
+        parcel.setDataSize(marshalled.length - 2);
+        assertNull(parcel.createByteArray());
+
+        // Test that readByteArray returns null with a truncated parcel.
+        parcel = Parcel.obtain();
+        parcel.unmarshall(marshalled, 0, marshalled.length);
+        parcel.setDataSize(marshalled.length - 2);
+        try {
+            parcel.readByteArray(new byte[2]);
+            fail();
+        } catch (RuntimeException expected) {
+        }
+    }
 }