• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2009 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.os.cts;
18 
19 import static org.junit.Assert.assertArrayEquals;
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertFalse;
22 import static org.junit.Assert.assertNotNull;
23 import static org.junit.Assert.assertNull;
24 import static org.junit.Assert.assertSame;
25 import static org.junit.Assert.assertThrows;
26 import static org.junit.Assert.assertTrue;
27 import static org.junit.Assert.fail;
28 
29 import android.os.BadParcelableException;
30 import android.os.Binder;
31 import android.os.Bundle;
32 import android.os.Flags;
33 import android.os.IBinder;
34 import android.os.IInterface;
35 import android.os.Parcel;
36 import android.os.ParcelFileDescriptor;
37 import android.os.Parcelable;
38 import android.platform.test.annotations.AppModeSdkSandbox;
39 import android.platform.test.annotations.AsbSecurityTest;
40 import android.platform.test.annotations.DisabledOnRavenwood;
41 import android.platform.test.annotations.RequiresFlagsEnabled;
42 import android.platform.test.flag.junit.CheckFlagsRule;
43 import android.platform.test.flag.junit.DeviceFlagsValueProvider;
44 import android.platform.test.ravenwood.RavenwoodRule;
45 import android.util.Log;
46 import android.util.SparseArray;
47 import android.util.SparseBooleanArray;
48 
49 import org.junit.Rule;
50 import org.junit.Test;
51 
52 import java.io.FileDescriptor;
53 import java.io.Serializable;
54 import java.nio.BufferOverflowException;
55 import java.nio.ByteBuffer;
56 import java.nio.charset.StandardCharsets;
57 import java.util.ArrayList;
58 import java.util.Arrays;
59 import java.util.Collection;
60 import java.util.HashMap;
61 import java.util.HashSet;
62 import java.util.Map;
63 import java.util.Set;
64 
65 @AppModeSdkSandbox(reason = "Allow test in the SDK sandbox (does not prevent other modes).")
66 public class ParcelTest {
67     @Rule
68     public RavenwoodRule mRavenwood = new RavenwoodRule.Builder().build();
69 
70     @Rule
71     public final CheckFlagsRule mCheckFlagsRule = DeviceFlagsValueProvider.createCheckFlagsRule();
72 
73     private static final float DELTA_FLOAT = 0.0f;
74     private static final double DELTA_DOUBLE = 0.0d;
75 
76     /**
77      * Since we're focused on testing Parcel here, give ourselves a local equivalent of {@code
78      * android.content.pm.Signature} so that we're not running an integration test.
79      */
80     public static class Signature implements Parcelable {
81         private final byte[] mSignature;
82 
Signature(String signature)83         public Signature(String signature) {
84             mSignature = signature.getBytes(StandardCharsets.UTF_8);
85         }
86 
Signature(Parcel source)87         private Signature(Parcel source) {
88             mSignature = source.createByteArray();
89         }
90 
91         @Override
equals(Object other)92         public boolean equals(Object other) {
93             if (other instanceof Signature) {
94                 return Arrays.equals(mSignature, ((Signature) other).mSignature);
95             } else {
96                 return false;
97             }
98         }
99 
100         @Override
hashCode()101         public int hashCode() {
102             return Arrays.hashCode(mSignature);
103         }
104 
describeContents()105         public int describeContents() {
106             return 0;
107         }
108 
writeToParcel(Parcel dest, int parcelableFlags)109         public void writeToParcel(Parcel dest, int parcelableFlags) {
110             dest.writeByteArray(mSignature);
111         }
112 
113         public static final Parcelable.Creator<Signature> CREATOR = new Parcelable.Creator<>() {
114             public Signature createFromParcel(Parcel source) {
115                 return new Signature(source);
116             }
117 
118             public Signature[] newArray(int size) {
119                 return new Signature[size];
120             }
121         };
122     }
123 
124     /**
125      * Since we're focused on testing Parcel here, give ourselves a local equivalent of {@code
126      * android.content.Intent} so that we're not running an integration test.
127      */
128     public static class Intent implements Parcelable {
Intent()129         public Intent() {
130         }
131 
Intent(Intent other)132         private Intent(Intent other) {
133         }
134 
describeContents()135         public int describeContents() {
136             return 0;
137         }
138 
writeToParcel(Parcel dest, int parcelableFlags)139         public void writeToParcel(Parcel dest, int parcelableFlags) {
140             dest.writeString(getClass().toString());
141         }
142 
readFromParcel(Parcel source)143         public void readFromParcel(Parcel source) {
144             assertEquals(getClass().toString(), source.readString());
145         }
146 
147         public static final Parcelable.Creator<Intent> CREATOR = new Parcelable.Creator<>() {
148             public Intent createFromParcel(Parcel source) {
149                 Intent intent = new Intent();
150                 intent.readFromParcel(source);
151                 return intent;
152             }
153 
154             public Intent[] newArray(int size) {
155                 return new Intent[size];
156             }
157         };
158     }
159 
160     @Test
testObtain()161     public void testObtain() {
162         Parcel p1 = Parcel.obtain();
163         assertNotNull(p1);
164         Parcel p2 = Parcel.obtain();
165         assertNotNull(p2);
166         Parcel p3 = Parcel.obtain();
167         assertNotNull(p3);
168         Parcel p4 = Parcel.obtain();
169         assertNotNull(p4);
170         Parcel p5 = Parcel.obtain();
171         assertNotNull(p5);
172         Parcel p6 = Parcel.obtain();
173         assertNotNull(p6);
174         Parcel p7 = Parcel.obtain();
175         assertNotNull(p7);
176 
177         p1.recycle();
178         p2.recycle();
179         p3.recycle();
180         p4.recycle();
181         p5.recycle();
182         p6.recycle();
183         p7.recycle();
184     }
185 
186     @Test
testAppendFrom()187     public void testAppendFrom() {
188         Parcel p;
189         Parcel p2;
190         int d1;
191         int d2;
192 
193         p = Parcel.obtain();
194         d1 = p.dataPosition();
195         p.writeInt(7);
196         p.writeInt(5);
197         d2 = p.dataPosition();
198         p2 = Parcel.obtain();
199         p2.appendFrom(p, d1, d2 - d1);
200         p2.setDataPosition(0);
201         assertEquals(7, p2.readInt());
202         assertEquals(5, p2.readInt());
203         p2.recycle();
204         p.recycle();
205     }
206 
207     @Test
testDataAvail()208     public void testDataAvail() {
209         Parcel p;
210 
211         p = Parcel.obtain();
212         p.writeInt(7); // size 4
213         p.writeInt(5); // size 4
214         p.writeLong(7L); // size 8
215         p.writeString("7L"); // size 12
216         p.setDataPosition(0);
217         assertEquals(p.dataSize(), p.dataAvail());
218         p.readInt();
219         assertEquals(p.dataSize() - p.dataPosition(), p.dataAvail());
220         p.readInt();
221         assertEquals(p.dataSize() - p.dataPosition(), p.dataAvail());
222         p.readLong();
223         assertEquals(p.dataSize() - p.dataPosition(), p.dataAvail());
224         p.readString();
225         assertEquals(p.dataSize() - p.dataPosition(), p.dataAvail());
226         p.recycle();
227     }
228 
229     @Test
testDataCapacity()230     public void testDataCapacity() {
231         Parcel p;
232 
233         p = Parcel.obtain();
234         assertEquals(0, p.dataCapacity());
235         p.writeInt(7); // size 4
236         int dC1 = p.dataCapacity();
237         p.writeDouble(2.19);
238         int dC2 = p.dataCapacity();
239         assertTrue(dC2 >= dC1);
240         p.recycle();
241     }
242 
243     @Test
testSetDataCapacity()244     public void testSetDataCapacity() {
245         Parcel p;
246 
247         p = Parcel.obtain();
248         assertEquals(0, p.dataCapacity());
249         p.setDataCapacity(2);
250         assertEquals(2, p.dataCapacity());
251         p.setDataCapacity(1);
252         assertEquals(2, p.dataCapacity());
253         p.setDataCapacity(3);
254         assertEquals(3, p.dataCapacity());
255         p.recycle();
256     }
257 
258     @Test
testSetDataCapacityNegative()259     public void testSetDataCapacityNegative() {
260         Parcel p;
261 
262         p = Parcel.obtain();
263         try {
264             p.setDataCapacity(-1);
265             fail();
266         } catch (IllegalArgumentException e) {
267             // ignore
268         } finally {
269             p.recycle();
270         }
271     }
272 
273     @Test
testDataPosition()274     public void testDataPosition() {
275         Parcel p;
276 
277         p = Parcel.obtain();
278         assertEquals(0, p.dataPosition());
279         p.writeInt(7); // size 4
280         int dP1 = p.dataPosition();
281         p.writeLong(7L); // size 8
282         int dP2 = p.dataPosition();
283         assertTrue(dP2 > dP1);
284         p.recycle();
285     }
286 
287     @Test
testSetDataPosition()288     public void testSetDataPosition() {
289         Parcel p;
290 
291         p = Parcel.obtain();
292         assertEquals(0, p.dataSize());
293         assertEquals(0, p.dataPosition());
294         p.setDataPosition(4);
295         assertEquals(4, p.dataPosition());
296         p.setDataPosition(7);
297         assertEquals(7, p.dataPosition());
298         p.setDataPosition(0);
299         p.writeInt(7);
300         assertEquals(4, p.dataSize());
301         p.setDataPosition(4);
302         assertEquals(4, p.dataPosition());
303         p.setDataPosition(7);
304         assertEquals(7, p.dataPosition());
305         p.recycle();
306     }
307 
308     @Test
testDataSize()309     public void testDataSize() {
310         Parcel p;
311 
312         p = Parcel.obtain();
313         assertEquals(0, p.dataSize());
314         p.writeInt(7); // size 4
315         assertEquals(4, p.dataSize());
316         p.writeInt(5); // size 4
317         assertEquals(8, p.dataSize());
318         p.writeLong(7L); // size 8
319         assertEquals(16, p.dataSize());
320         p.recycle();
321     }
322 
323     @Test
testSetDataSize()324     public void testSetDataSize() {
325         Parcel p;
326 
327         p = Parcel.obtain();
328         assertEquals(0, p.dataSize());
329         p.setDataSize(5);
330         assertEquals(5, p.dataSize());
331         p.setDataSize(3);
332         assertEquals(3, p.dataSize());
333 
334         p.writeInt(3);
335         assertEquals(4, p.dataSize());
336         p.setDataSize(5);
337         assertEquals(5, p.dataSize());
338         p.setDataSize(3);
339         assertEquals(3, p.dataSize());
340         p.recycle();
341     }
342 
343     @Test
testObtainWithBinder()344     public void testObtainWithBinder() {
345         Parcel p = Parcel.obtain(new Binder("anything"));
346         // testing does not throw an exception, Parcel still works
347 
348         final int kTest = 17;
349         p.writeInt(kTest);
350         p.setDataPosition(0);
351         assertEquals(kTest, p.readInt());
352 
353         p.recycle();
354     }
355 
356     @Test
testEnforceInterface()357     public void testEnforceInterface() {
358         Parcel p;
359         String s = "IBinder interface token";
360 
361         p = Parcel.obtain();
362         p.writeInterfaceToken(s);
363         p.setDataPosition(0);
364         try {
365             p.enforceInterface("");
366             fail("Should throw an SecurityException");
367         } catch (SecurityException e) {
368             //expected
369         }
370         p.recycle();
371 
372         p = Parcel.obtain();
373         p.writeInterfaceToken(s);
374         p.setDataPosition(0);
375         p.enforceInterface(s);
376         p.recycle();
377     }
378 
379     @Test
testEnforceNoDataAvail()380     public void testEnforceNoDataAvail(){
381         final Parcel p = Parcel.obtain();
382         p.writeInt(1);
383         p.writeString("test");
384 
385         p.setDataPosition(0);
386         p.readInt();
387         Throwable error = assertThrows(BadParcelableException.class, () -> p.enforceNoDataAvail());
388         assertTrue(error.getMessage().contains("Parcel data not fully consumed"));
389 
390         p.readString();
391         p.enforceNoDataAvail();
392         p.recycle();
393     }
394 
395     @Test
testMarshall()396     public void testMarshall() {
397         final byte[] c = {Byte.MAX_VALUE, (byte) 111, (byte) 11, (byte) 1, (byte) 0,
398                     (byte) -1, (byte) -11, (byte) -111, Byte.MIN_VALUE};
399 
400         Parcel p1 = Parcel.obtain();
401         p1.writeByteArray(c);
402         p1.setDataPosition(0);
403         byte[] d1 = p1.marshall();
404 
405         Parcel p2 = Parcel.obtain();
406         p2.unmarshall(d1, 0, d1.length);
407         p2.setDataPosition(0);
408         byte[] d2 = new byte[c.length];
409         p2.readByteArray(d2);
410 
411         for (int i = 0; i < c.length; i++) {
412             assertEquals(c[i], d2[i]);
413         }
414 
415         p1.recycle();
416         p2.recycle();
417     }
418 
419     private static final byte[] TEST_DATA = new byte[] {4, 8, 15, 16, 23, 42};
420 
421     // Allow for some Parcel overhead
422     private static final int TEST_DATA_LENGTH = TEST_DATA.length + 100;
423 
424     @Test
425     @RequiresFlagsEnabled(Flags.FLAG_PARCEL_MARSHALL_BYTEBUFFER)
testMarshall_ByteBuffer_wrapped()426     public void testMarshall_ByteBuffer_wrapped() {
427         ByteBuffer bb = ByteBuffer.allocate(TEST_DATA_LENGTH);
428         testMarshall_ByteBuffer(bb);
429     }
430 
431     @Test
432     @RequiresFlagsEnabled(Flags.FLAG_PARCEL_MARSHALL_BYTEBUFFER)
testMarshall_DirectByteBuffer()433     public void testMarshall_DirectByteBuffer() {
434         ByteBuffer bb = ByteBuffer.allocateDirect(TEST_DATA_LENGTH);
435         testMarshall_ByteBuffer(bb);
436     }
437 
testMarshall_ByteBuffer(ByteBuffer bb)438     private void testMarshall_ByteBuffer(ByteBuffer bb) {
439         // Ensure that Parcel respects the starting offset by not starting at 0
440         bb.position(1);
441         bb.mark();
442 
443         // Parcel test data, then marshall into the ByteBuffer
444         Parcel p1 = Parcel.obtain();
445         p1.writeByteArray(TEST_DATA);
446         p1.marshall(bb);
447         p1.recycle();
448 
449         assertTrue(bb.position() > 1);
450         bb.reset();
451 
452         // Unmarshall test data into a new Parcel
453         Parcel p2 = Parcel.obtain();
454         bb.reset();
455         p2.unmarshall(bb);
456         assertTrue(bb.position() > 1);
457         p2.setDataPosition(0);
458         byte[] marshalled = p2.marshall();
459 
460         bb.reset();
461         for (int i = 0; i < TEST_DATA.length; i++) {
462             assertEquals(bb.get(), marshalled[i]);
463         }
464 
465         byte[] testDataCopy = new byte[TEST_DATA.length];
466         p2.setDataPosition(0);
467         p2.readByteArray(testDataCopy);
468         for (int i = 0; i < TEST_DATA.length; i++) {
469             assertEquals(TEST_DATA[i], testDataCopy[i]);
470         }
471 
472         // Test that overflowing the buffer throws an exception
473         bb.reset();
474         // Leave certainly not enough room for the test data
475         bb.limit(bb.position() + TEST_DATA.length - 1);
476         assertThrows(BufferOverflowException.class, () -> p2.marshall(bb));
477         p2.recycle();
478     }
479 
480     @Test
481     @SuppressWarnings("unchecked")
testReadValue()482     public void testReadValue() {
483         Parcel p;
484         MockClassLoader mcl = new MockClassLoader();
485 
486         // test null
487         p = Parcel.obtain();
488         p.writeValue(null);
489         p.setDataPosition(0);
490         assertNull(p.readValue(mcl));
491         p.recycle();
492 
493         // test String
494         p = Parcel.obtain();
495         p.writeValue("String");
496         p.setDataPosition(0);
497         assertEquals("String", p.readValue(mcl));
498         p.recycle();
499 
500         // test Integer
501         p = Parcel.obtain();
502         p.writeValue(Integer.MAX_VALUE);
503         p.setDataPosition(0);
504         assertEquals(Integer.MAX_VALUE, p.readValue(mcl));
505         p.recycle();
506 
507         // test Map
508         HashMap map = new HashMap();
509         HashMap map2;
510         map.put("string", "String");
511         map.put("int", Integer.MAX_VALUE);
512         map.put("boolean", true);
513         p = Parcel.obtain();
514         p.writeValue(map);
515         p.setDataPosition(0);
516         map2 = (HashMap) p.readValue(mcl);
517         assertNotNull(map2);
518         assertEquals(map.size(), map2.size());
519         assertEquals("String", map.get("string"));
520         assertEquals(Integer.MAX_VALUE, map.get("int"));
521         assertEquals(true, map.get("boolean"));
522         p.recycle();
523 
524         // test Bundle
525         Bundle bundle = new Bundle();
526         bundle.putBoolean("boolean", true);
527         bundle.putInt("int", Integer.MAX_VALUE);
528         bundle.putString("string", "String");
529         Bundle bundle2;
530         p = Parcel.obtain();
531         p.writeValue(bundle);
532         p.setDataPosition(0);
533         bundle2 = (Bundle) p.readValue(mcl);
534         assertNotNull(bundle2);
535         assertEquals(true, bundle2.getBoolean("boolean"));
536         assertEquals(Integer.MAX_VALUE, bundle2.getInt("int"));
537         assertEquals("String", bundle2.getString("string"));
538         p.recycle();
539 
540         // test Parcelable
541         final String signatureString  = "1234567890abcdef";
542         Signature s = new Signature(signatureString);
543         p = Parcel.obtain();
544         p.writeValue(s);
545         p.setDataPosition(0);
546         assertEquals(s, p.readValue(mcl));
547         p.recycle();
548 
549         // test Short
550         p = Parcel.obtain();
551         p.writeValue(Short.MAX_VALUE);
552         p.setDataPosition(0);
553         assertEquals(Short.MAX_VALUE, p.readValue(mcl));
554         p.recycle();
555 
556         // test Long
557         p = Parcel.obtain();
558         p.writeValue(Long.MAX_VALUE);
559         p.setDataPosition(0);
560         assertEquals(Long.MAX_VALUE, p.readValue(mcl));
561         p.recycle();
562 
563         // test Float
564         p = Parcel.obtain();
565         p.writeValue(Float.MAX_VALUE);
566         p.setDataPosition(0);
567         assertEquals(Float.MAX_VALUE, p.readValue(mcl));
568         p.recycle();
569 
570         // test Double
571         p = Parcel.obtain();
572         p.writeValue(Double.MAX_VALUE);
573         p.setDataPosition(0);
574         assertEquals(Double.MAX_VALUE, p.readValue(mcl));
575         p.recycle();
576 
577         // test Boolean
578         p = Parcel.obtain();
579         p.writeValue(true);
580         p.writeValue(false);
581         p.setDataPosition(0);
582         assertTrue((Boolean) p.readValue(mcl));
583         assertFalse((Boolean) p.readValue(mcl));
584         p.recycle();
585 
586         // test CharSequence
587         p = Parcel.obtain();
588         p.writeValue((CharSequence) "CharSequence");
589         p.setDataPosition(0);
590         assertEquals("CharSequence", p.readValue(mcl));
591         p.recycle();
592 
593         // test List
594         ArrayList arrayList2 = new ArrayList();
595         arrayList2.add(Integer.MAX_VALUE);
596         arrayList2.add(true);
597         arrayList2.add(Long.MAX_VALUE);
598         ArrayList arrayList = new ArrayList();
599         p = Parcel.obtain();
600         p.writeValue(arrayList2);
601         p.setDataPosition(0);
602         assertEquals(0, arrayList.size());
603         arrayList = (ArrayList) p.readValue(mcl);
604         assertEquals(3, arrayList.size());
605         for (int i = 0; i < arrayList.size(); i++) {
606             assertEquals(arrayList.get(i), arrayList2.get(i));
607         }
608         p.recycle();
609 
610         // test SparseArray
611         SparseArray<Object> sparseArray = new SparseArray<Object>();
612         sparseArray.put(3, "String");
613         sparseArray.put(2, Long.MAX_VALUE);
614         sparseArray.put(4, Float.MAX_VALUE);
615         sparseArray.put(0, Integer.MAX_VALUE);
616         sparseArray.put(1, true);
617         sparseArray.put(10, true);
618         SparseArray<Object> sparseArray2;
619         p = Parcel.obtain();
620         p.writeValue(sparseArray);
621         p.setDataPosition(0);
622         sparseArray2 = (SparseArray<Object>) p.readValue(mcl);
623         assertNotNull(sparseArray2);
624         assertEquals(sparseArray.size(), sparseArray2.size());
625         assertEquals(sparseArray.get(0), sparseArray2.get(0));
626         assertEquals(sparseArray.get(1), sparseArray2.get(1));
627         assertEquals(sparseArray.get(2), sparseArray2.get(2));
628         assertEquals(sparseArray.get(3), sparseArray2.get(3));
629         assertEquals(sparseArray.get(4), sparseArray2.get(4));
630         assertEquals(sparseArray.get(10), sparseArray2.get(10));
631         p.recycle();
632 
633         // test boolean[]
634         boolean[] booleanArray  = {true, false, true, false};
635         boolean[] booleanArray2 = new boolean[booleanArray.length];
636         p = Parcel.obtain();
637         p.writeValue(booleanArray);
638         p.setDataPosition(0);
639         booleanArray2 = (boolean[]) p.readValue(mcl);
640         for (int i = 0; i < booleanArray.length; i++) {
641             assertEquals(booleanArray[i], booleanArray2[i]);
642         }
643         p.recycle();
644 
645         // test byte[]
646         byte[] byteArray = {Byte.MAX_VALUE, (byte) 111, (byte) 11, (byte) 1, (byte) 0,
647                 (byte) -1, (byte) -11, (byte) -111, Byte.MIN_VALUE};
648         byte[] byteArray2 = new byte[byteArray.length];
649         p = Parcel.obtain();
650         p.writeValue(byteArray);
651         p.setDataPosition(0);
652         byteArray2 = (byte[]) p.readValue(mcl);
653         for (int i = 0; i < byteArray.length; i++) {
654             assertEquals(byteArray[i], byteArray2[i]);
655         }
656         p.recycle();
657 
658         // test string[]
659         String[] stringArray = {"",
660                 "a",
661                 "Hello, Android!",
662                 "A long string that is used to test the api readStringArray(),"};
663         String[] stringArray2 = new String[stringArray.length];
664         p = Parcel.obtain();
665         p.writeValue(stringArray);
666         p.setDataPosition(0);
667         stringArray2 = (String[]) p.readValue(mcl);
668         for (int i = 0; i < stringArray.length; i++) {
669             assertEquals(stringArray[i], stringArray2[i]);
670         }
671         p.recycle();
672 
673         // test IBinder
674         Binder binder;
675         Binder binder2 = new Binder();
676         p = Parcel.obtain();
677         p.writeValue(binder2);
678         p.setDataPosition(0);
679         binder = (Binder) p.readValue(mcl);
680         assertEquals(binder2, binder);
681         p.recycle();
682 
683         // test Parcelable[]
684         Signature[] signatures = {new Signature("1234"),
685                 new Signature("ABCD"),
686                 new Signature("abcd")};
687         Parcelable[] signatures2;
688         p = Parcel.obtain();
689         p.writeValue(signatures);
690         p.setDataPosition(0);
691         signatures2 = (Parcelable[]) p.readValue(mcl);
692         for (int i = 0; i < signatures.length; i++) {
693             assertEquals(signatures[i], signatures2[i]);
694         }
695         p.recycle();
696 
697         // test Object
698         Object[] objects = new Object[5];
699         objects[0] = Integer.MAX_VALUE;
700         objects[1] = true;
701         objects[2] = Long.MAX_VALUE;
702         objects[3] = "String";
703         objects[4] = Float.MAX_VALUE;
704         Object[] objects2;
705         p = Parcel.obtain();
706         p.writeValue(objects);
707         p.setDataPosition(0);
708         objects2 = (Object[]) p.readValue(mcl);
709         assertNotNull(objects2);
710         for (int i = 0; i < objects2.length; i++) {
711             assertEquals(objects[i], objects2[i]);
712         }
713         p.recycle();
714 
715         // test int[]
716         int[] intArray = {111, 11, 1, 0, -1, -11, -111};
717         int[] intArray2 = new int[intArray.length];
718         p = Parcel.obtain();
719         p.writeValue(intArray);
720         p.setDataPosition(0);
721         intArray2= (int[]) p.readValue(mcl);
722         assertNotNull(intArray2);
723         for (int i = 0; i < intArray2.length; i++) {
724             assertEquals(intArray[i], intArray2[i]);
725         }
726         p.recycle();
727 
728         // test long[]
729         long[] longArray = {111L, 11L, 1L, 0L, -1L, -11L, -111L};
730         long[] longArray2 = new long[longArray.length];
731         p = Parcel.obtain();
732         p.writeValue(longArray);
733         p.setDataPosition(0);
734         longArray2= (long[]) p.readValue(mcl);
735         assertNotNull(longArray2);
736         for (int i = 0; i < longArray2.length; i++) {
737             assertEquals(longArray[i], longArray2[i]);
738         }
739         p.recycle();
740 
741         // test byte
742         p = Parcel.obtain();
743         p.writeValue(Byte.MAX_VALUE);
744         p.setDataPosition(0);
745         assertEquals(Byte.MAX_VALUE, p.readValue(mcl));
746         p.recycle();
747 
748         // test Serializable
749         p = Parcel.obtain();
750         p.writeValue((Serializable) "Serializable");
751         p.setDataPosition(0);
752         assertEquals("Serializable", p.readValue(mcl));
753         p.recycle();
754     }
755 
756     @Test
testReadByte()757     public void testReadByte() {
758         Parcel p;
759 
760         p = Parcel.obtain();
761         p.writeByte((byte) 0);
762         p.setDataPosition(0);
763         assertEquals((byte) 0, p.readByte());
764         p.recycle();
765 
766         p = Parcel.obtain();
767         p.writeByte((byte) 1);
768         p.setDataPosition(0);
769         assertEquals((byte) 1, p.readByte());
770         p.recycle();
771 
772         p = Parcel.obtain();
773         p.writeByte((byte) -1);
774         p.setDataPosition(0);
775         assertEquals((byte) -1, p.readByte());
776         p.recycle();
777 
778         p = Parcel.obtain();
779         p.writeByte(Byte.MAX_VALUE);
780         p.setDataPosition(0);
781         assertEquals(Byte.MAX_VALUE, p.readByte());
782         p.recycle();
783 
784         p = Parcel.obtain();
785         p.writeByte(Byte.MIN_VALUE);
786         p.setDataPosition(0);
787         assertEquals(Byte.MIN_VALUE, p.readByte());
788         p.recycle();
789 
790         p = Parcel.obtain();
791         p.writeByte(Byte.MAX_VALUE);
792         p.writeByte((byte) 11);
793         p.writeByte((byte) 1);
794         p.writeByte((byte) 0);
795         p.writeByte((byte) -1);
796         p.writeByte((byte) -11);
797         p.writeByte(Byte.MIN_VALUE);
798         p.setDataPosition(0);
799         assertEquals(Byte.MAX_VALUE, p.readByte());
800         assertEquals((byte) 11, p.readByte());
801         assertEquals((byte) 1, p.readByte());
802         assertEquals((byte) 0, p.readByte());
803         assertEquals((byte) -1, p.readByte());
804         assertEquals((byte) -11, p.readByte());
805         assertEquals(Byte.MIN_VALUE, p.readByte());
806         p.recycle();
807     }
808 
809     @Test
testReadByteArray()810     public void testReadByteArray() {
811         Parcel p;
812 
813         byte[] a = {(byte) 21};
814         byte[] b = new byte[a.length];
815 
816         byte[] c = {Byte.MAX_VALUE, (byte) 111, (byte) 11, (byte) 1, (byte) 0,
817                     (byte) -1, (byte) -11, (byte) -111, Byte.MIN_VALUE};
818         byte[] d = new byte[c.length];
819 
820         // test write null
821         p = Parcel.obtain();
822         p.writeByteArray(null);
823         p.setDataPosition(0);
824         try {
825             p.readByteArray(null);
826             fail("Should throw a RuntimeException");
827         } catch (RuntimeException e) {
828             //expected
829         }
830 
831         p.setDataPosition(0);
832         try {
833             p.readByteArray(b);
834             fail("Should throw a RuntimeException");
835         } catch (RuntimeException e) {
836             //expected
837         }
838         p.recycle();
839 
840         // test write byte array with length: 1
841         p = Parcel.obtain();
842         p.writeByteArray(a);
843         p.setDataPosition(0);
844         try {
845             p.readByteArray(d);
846             fail("Should throw a RuntimeException");
847         } catch (RuntimeException e) {
848             //expected
849         }
850 
851         p.setDataPosition(0);
852         p.readByteArray(b);
853         for (int i = 0; i < a.length; i++) {
854             assertEquals(a[i], b[i]);
855         }
856         p.recycle();
857 
858         // test write byte array with length: 9
859         p = Parcel.obtain();
860         p.writeByteArray(c);
861         p.setDataPosition(0);
862         try {
863             p.readByteArray(b);
864             fail("Should throw a RuntimeException");
865         } catch (RuntimeException e) {
866             //expected
867         }
868 
869         p.setDataPosition(0);
870         p.readByteArray(d);
871         for (int i = 0; i < c.length; i++) {
872             assertEquals(c[i], d[i]);
873         }
874         p.recycle();
875 
876         // Test array bounds checks (null already checked above).
877         p = Parcel.obtain();
878         try {
879             p.writeByteArray(c, -1, 1); // Negative offset.
880             fail();
881         } catch (RuntimeException expected) {
882         }
883         try {
884             p.writeByteArray(c, 0, -1); // Negative count.
885             fail();
886         } catch (RuntimeException expected) {
887         }
888         try {
889             p.writeByteArray(c, c.length + 1, 1); // High offset.
890             fail();
891         } catch (RuntimeException expected) {
892         }
893         try {
894             p.writeByteArray(c, 0, c.length + 1); // High count.
895             fail();
896         } catch (RuntimeException expected) {
897         }
898         p.recycle();
899     }
900 
901     @Test
testWriteBlob()902     public void testWriteBlob() {
903         Parcel p;
904 
905         byte[] shortBytes = {(byte) 21};
906         // Create a byte array with 70 KiB to make sure it is large enough to be saved into Android
907         // Shared Memory. The native blob inplace limit is 16 KiB. Also make it larger than the
908         // IBinder.MAX_IPC_SIZE which is 64 KiB.
909         byte[] largeBytes = new byte[70 * 1024];
910         for (int i = 0; i < largeBytes.length; i++) {
911             largeBytes[i] = (byte) (i / Byte.MAX_VALUE);
912         }
913         // test write null
914         p = Parcel.obtain();
915         p.writeBlob(null, 0, 2);
916         p.setDataPosition(0);
917         byte[] outputBytes = p.readBlob();
918         assertNull(outputBytes);
919         p.recycle();
920 
921         // test write short bytes
922         p = Parcel.obtain();
923         p.writeBlob(shortBytes, 0, 1);
924         p.setDataPosition(0);
925         assertEquals(shortBytes[0], p.readBlob()[0]);
926         p.recycle();
927 
928         // test write large bytes
929         p = Parcel.obtain();
930         p.writeBlob(largeBytes, 0, largeBytes.length);
931         p.setDataPosition(0);
932         outputBytes = p.readBlob();
933         for (int i = 0; i < largeBytes.length; i++) {
934             assertEquals(largeBytes[i], outputBytes[i]);
935         }
936         p.recycle();
937     }
938 
939     @Test
testWriteByteArray()940     public void testWriteByteArray() {
941         Parcel p;
942 
943         byte[] a = {(byte) 21};
944         byte[] b = new byte[a.length];
945 
946         byte[] c = {Byte.MAX_VALUE, (byte) 111, (byte) 11, (byte) 1, (byte) 0,
947                     (byte) -1, (byte) -11, (byte) -111, Byte.MIN_VALUE};
948         byte[] d = new byte[c.length - 2];
949 
950         // test write null
951         p = Parcel.obtain();
952         p.writeByteArray(null, 0, 2);
953         p.setDataPosition(0);
954         try {
955             p.readByteArray(null);
956             fail("Should throw a RuntimeException");
957         } catch (RuntimeException e) {
958             //expected
959         }
960 
961         p.setDataPosition(0);
962         try {
963             p.readByteArray(b);
964             fail("Should throw a RuntimeException");
965         } catch (RuntimeException e) {
966             //expected
967         }
968         p.recycle();
969 
970         // test with wrong offset and length
971         p = Parcel.obtain();
972         try {
973             p.writeByteArray(a, 0, 2);
974             fail("Should throw a ArrayIndexOutOfBoundsException");
975         } catch (ArrayIndexOutOfBoundsException e) {
976             //expected
977         }
978         p.recycle();
979 
980         p = Parcel.obtain();
981         try {
982             p.writeByteArray(a, -1, 1);
983             fail("Should throw a ArrayIndexOutOfBoundsException");
984         } catch (ArrayIndexOutOfBoundsException e) {
985             //expected
986         }
987         p.recycle();
988 
989         p = Parcel.obtain();
990         try {
991             p.writeByteArray(a, 0, -1);
992             fail("Should throw a ArrayIndexOutOfBoundsException");
993         } catch (ArrayIndexOutOfBoundsException e) {
994             //expected
995         }
996         p.recycle();
997 
998         // test write byte array with length: 1
999         p = Parcel.obtain();
1000         p.writeByteArray(a, 0 , 1);
1001         p.setDataPosition(0);
1002         try {
1003             p.readByteArray(d);
1004             fail("Should throw a RuntimeException");
1005         } catch (RuntimeException e) {
1006             //expected
1007         }
1008 
1009         p.setDataPosition(0);
1010         p.readByteArray(b);
1011         for (int i = 0; i < a.length; i++) {
1012             assertEquals(a[i], b[i]);
1013         }
1014         p.recycle();
1015 
1016         // test write byte array with offset: 1, length: 7
1017         p = Parcel.obtain();
1018         p.writeByteArray(c, 1, 7);
1019         p.setDataPosition(0);
1020         try {
1021             p.readByteArray(b);
1022             fail("Should throw a RuntimeException");
1023         } catch (RuntimeException e) {
1024             //expected
1025         }
1026 
1027         d = new byte[c.length - 2];
1028         p.setDataPosition(0);
1029         p.readByteArray(d);
1030         for (int i = 0; i < d.length; i++) {
1031             Log.d("Trace", "i=" + i + " d[i]=" + d[i]);
1032         }
1033         for (int i = 0; i < 7; i++) {
1034             assertEquals(c[i + 1], d[i]);
1035         }
1036         p.recycle();
1037     }
1038 
1039     @Test
testCreateByteArray()1040     public void testCreateByteArray() {
1041         Parcel p;
1042 
1043         byte[] a = {(byte) 21};
1044         byte[] b;
1045 
1046         byte[] c = {Byte.MAX_VALUE, (byte) 111, (byte) 11, (byte) 1, (byte) 0,
1047                     (byte) -1, (byte) -11, (byte) -111, Byte.MIN_VALUE};
1048         byte[] d;
1049 
1050         byte[] e = {};
1051         byte[] f;
1052 
1053         // test write null
1054         p = Parcel.obtain();
1055         p.writeByteArray(null);
1056         p.setDataPosition(0);
1057         b = p.createByteArray();
1058         assertNull(b);
1059         p.recycle();
1060 
1061         // test write byte array with length: 0
1062         p = Parcel.obtain();
1063         p.writeByteArray(e);
1064         p.setDataPosition(0);
1065         f = p.createByteArray();
1066         assertNotNull(f);
1067         assertEquals(0, f.length);
1068         p.recycle();
1069 
1070         // test write byte array with length: 1
1071         p = Parcel.obtain();
1072         p.writeByteArray(a);
1073         p.setDataPosition(0);
1074         b = p.createByteArray();
1075         assertNotNull(b);
1076         for (int i = 0; i < a.length; i++) {
1077             assertEquals(a[i], b[i]);
1078         }
1079         p.recycle();
1080 
1081         // test write byte array with length: 9
1082         p = Parcel.obtain();
1083         p.writeByteArray(c);
1084         p.setDataPosition(0);
1085         d = p.createByteArray();
1086         assertNotNull(d);
1087         for (int i = 0; i < c.length; i++) {
1088             assertEquals(c[i], d[i]);
1089         }
1090         p.recycle();
1091     }
1092 
1093     @Test
testReadCharArray()1094     public void testReadCharArray() {
1095         Parcel p;
1096 
1097         char[] a = {'a'};
1098         char[] b = new char[a.length];
1099 
1100         char[] c = {'a', Character.MAX_VALUE, Character.MIN_VALUE, Character.MAX_SURROGATE, Character.MIN_SURROGATE,
1101                     Character.MAX_HIGH_SURROGATE, Character.MAX_LOW_SURROGATE,
1102                     Character.MIN_HIGH_SURROGATE, Character.MIN_LOW_SURROGATE};
1103         char[] d = new char[c.length];
1104 
1105         // test write null
1106         p = Parcel.obtain();
1107         p.writeCharArray(null);
1108         p.setDataPosition(0);
1109         try {
1110             p.readCharArray(null);
1111             fail("Should throw a RuntimeException");
1112         } catch (RuntimeException e) {
1113             //expected
1114         }
1115 
1116         p.setDataPosition(0);
1117         try {
1118             p.readCharArray(b);
1119             fail("Should throw a RuntimeException");
1120         } catch (RuntimeException e) {
1121             //expected
1122         }
1123         p.recycle();
1124 
1125         // test write char array with length: 1
1126         p = Parcel.obtain();
1127         p.writeCharArray(a);
1128         p.setDataPosition(0);
1129         try {
1130             p.readCharArray(d);
1131             fail("Should throw a RuntimeException");
1132         } catch (RuntimeException e) {
1133             //expected
1134         }
1135 
1136         p.setDataPosition(0);
1137         p.readCharArray(b);
1138         for (int i = 0; i < a.length; i++) {
1139             assertEquals(a[i], b[i]);
1140         }
1141         p.recycle();
1142 
1143         // test write char array with length: 9
1144         p = Parcel.obtain();
1145         p.writeCharArray(c);
1146         p.setDataPosition(0);
1147         try {
1148             p.readCharArray(b);
1149             fail("Should throw a RuntimeException");
1150         } catch (RuntimeException e) {
1151             //expected
1152         }
1153 
1154         p.setDataPosition(0);
1155         p.readCharArray(d);
1156         for (int i = 0; i < c.length; i++) {
1157             assertEquals(c[i], d[i]);
1158         }
1159         p.recycle();
1160     }
1161 
1162     @Test
testCreateCharArray()1163     public void testCreateCharArray() {
1164         Parcel p;
1165 
1166         char[] a = {'a'};
1167         char[] b;
1168 
1169         char[] c = {'a', Character.MAX_VALUE, Character.MIN_VALUE, Character.MAX_SURROGATE, Character.MIN_SURROGATE,
1170                     Character.MAX_HIGH_SURROGATE, Character.MAX_LOW_SURROGATE,
1171                     Character.MIN_HIGH_SURROGATE, Character.MIN_LOW_SURROGATE};
1172         char[] d;
1173 
1174         char[] e = {};
1175         char[] f;
1176 
1177         // test write null
1178         p = Parcel.obtain();
1179         p.writeCharArray(null);
1180         p.setDataPosition(0);
1181         b = p.createCharArray();
1182         assertNull(b);
1183         p.recycle();
1184 
1185         // test write char array with length: 1
1186         p = Parcel.obtain();
1187         p.writeCharArray(e);
1188         p.setDataPosition(0);
1189         f = p.createCharArray();
1190         assertNotNull(e);
1191         assertEquals(0, f.length);
1192         p.recycle();
1193 
1194         // test write char array with length: 1
1195         p = Parcel.obtain();
1196         p.writeCharArray(a);
1197         p.setDataPosition(0);
1198         b = p.createCharArray();
1199         assertNotNull(b);
1200         for (int i = 0; i < a.length; i++) {
1201             assertEquals(a[i], b[i]);
1202         }
1203         p.recycle();
1204 
1205         // test write char array with length: 9
1206         p = Parcel.obtain();
1207         p.writeCharArray(c);
1208         p.setDataPosition(0);
1209         d = p.createCharArray();
1210         assertNotNull(d);
1211         for (int i = 0; i < c.length; i++) {
1212             assertEquals(c[i], d[i]);
1213         }
1214         p.recycle();
1215     }
1216 
1217     @Test
testReadInt()1218     public void testReadInt() {
1219         Parcel p;
1220 
1221         p = Parcel.obtain();
1222         p.writeInt(0);
1223         p.setDataPosition(0);
1224         assertEquals(0, p.readInt());
1225         p.recycle();
1226 
1227         p = Parcel.obtain();
1228         p.writeInt(1);
1229         p.setDataPosition(0);
1230         assertEquals(1, p.readInt());
1231         p.recycle();
1232 
1233         p = Parcel.obtain();
1234         p.writeInt(-1);
1235         p.setDataPosition(0);
1236         assertEquals(-1, p.readInt());
1237         p.recycle();
1238 
1239         p = Parcel.obtain();
1240         p.writeInt(Integer.MAX_VALUE);
1241         p.setDataPosition(0);
1242         assertEquals(Integer.MAX_VALUE, p.readInt());
1243         p.recycle();
1244 
1245         p = Parcel.obtain();
1246         p.writeInt(Integer.MIN_VALUE);
1247         p.setDataPosition(0);
1248         assertEquals(Integer.MIN_VALUE, p.readInt());
1249         p.recycle();
1250 
1251         p = Parcel.obtain();
1252         p.writeInt(Integer.MAX_VALUE);
1253         p.writeInt(11);
1254         p.writeInt(1);
1255         p.writeInt(0);
1256         p.writeInt(-1);
1257         p.writeInt(-11);
1258         p.writeInt(Integer.MIN_VALUE);
1259         p.setDataPosition(0);
1260         assertEquals(Integer.MAX_VALUE, p.readInt());
1261         assertEquals(11, p.readInt());
1262         assertEquals(1, p.readInt());
1263         assertEquals(0, p.readInt());
1264         assertEquals(-1, p.readInt());
1265         assertEquals(-11, p.readInt());
1266         assertEquals(Integer.MIN_VALUE, p.readInt());
1267         p.recycle();
1268     }
1269 
1270     @Test
testReadIntArray()1271     public void testReadIntArray() {
1272         Parcel p;
1273 
1274         int[] a = {21};
1275         int[] b = new int[a.length];
1276 
1277         int[] c = {Integer.MAX_VALUE, 111, 11, 1, 0, -1, -11, -111, Integer.MIN_VALUE};
1278         int[] d = new int[c.length];
1279 
1280         // test write null
1281         p = Parcel.obtain();
1282         p.writeIntArray(null);
1283         p.setDataPosition(0);
1284         try {
1285             p.readIntArray(null);
1286             fail("Should throw a RuntimeException");
1287         } catch (RuntimeException e) {
1288             //expected
1289         }
1290 
1291         p.setDataPosition(0);
1292         try {
1293             p.readIntArray(b);
1294             fail("Should throw a RuntimeException");
1295         } catch (RuntimeException e) {
1296             //expected
1297         }
1298         p.recycle();
1299 
1300         // test write int array with length: 1
1301         p = Parcel.obtain();
1302         p.writeIntArray(a);
1303         p.setDataPosition(0);
1304         try {
1305             p.readIntArray(d);
1306             fail("Should throw a RuntimeException");
1307         } catch (RuntimeException e) {
1308             //expected
1309         }
1310 
1311         p.setDataPosition(0);
1312         p.readIntArray(b);
1313         for (int i = 0; i < a.length; i++) {
1314             assertEquals(a[i], b[i]);
1315         }
1316         p.recycle();
1317 
1318         // test write int array with length: 9
1319         p = Parcel.obtain();
1320         p.writeIntArray(c);
1321         p.setDataPosition(0);
1322         try {
1323             p.readIntArray(b);
1324             fail("Should throw a RuntimeException");
1325         } catch (RuntimeException e) {
1326             //expected
1327         }
1328 
1329         p.setDataPosition(0);
1330         p.readIntArray(d);
1331         for (int i = 0; i < c.length; i++) {
1332             assertEquals(c[i], d[i]);
1333         }
1334         p.recycle();
1335     }
1336 
1337     @Test
testCreateIntArray()1338     public void testCreateIntArray() {
1339         Parcel p;
1340 
1341         int[] a = {21};
1342         int[] b;
1343 
1344         int[] c = {Integer.MAX_VALUE, 111, 11, 1, 0, -1, -11, -111, Integer.MIN_VALUE};
1345         int[] d;
1346 
1347         int[] e = {};
1348         int[] f;
1349 
1350         // test write null
1351         p = Parcel.obtain();
1352         p.writeIntArray(null);
1353         p.setDataPosition(0);
1354         b = p.createIntArray();
1355         assertNull(b);
1356         p.recycle();
1357 
1358         // test write int array with length: 0
1359         p = Parcel.obtain();
1360         p.writeIntArray(e);
1361         p.setDataPosition(0);
1362         f = p.createIntArray();
1363         assertNotNull(e);
1364         assertEquals(0, f.length);
1365         p.recycle();
1366 
1367         // test write int array with length: 1
1368         p = Parcel.obtain();
1369         p.writeIntArray(a);
1370         p.setDataPosition(0);
1371         b = p.createIntArray();
1372         assertNotNull(b);
1373         for (int i = 0; i < a.length; i++) {
1374             assertEquals(a[i], b[i]);
1375         }
1376         p.recycle();
1377 
1378         // test write int array with length: 9
1379         p = Parcel.obtain();
1380         p.writeIntArray(c);
1381         p.setDataPosition(0);
1382         d = p.createIntArray();
1383         assertNotNull(d);
1384         for (int i = 0; i < c.length; i++) {
1385             assertEquals(c[i], d[i]);
1386         }
1387         p.recycle();
1388     }
1389 
1390     @Test
testReadLong()1391     public void testReadLong() {
1392         Parcel p;
1393 
1394         p = Parcel.obtain();
1395         p.writeLong(0L);
1396         p.setDataPosition(0);
1397         assertEquals(0, p.readLong());
1398         p.recycle();
1399 
1400         p = Parcel.obtain();
1401         p.writeLong(1L);
1402         p.setDataPosition(0);
1403         assertEquals(1, p.readLong());
1404         p.recycle();
1405 
1406         p = Parcel.obtain();
1407         p.writeLong(-1L);
1408         p.setDataPosition(0);
1409         assertEquals(-1L, p.readLong());
1410         p.recycle();
1411 
1412         p = Parcel.obtain();
1413         p.writeLong(Long.MAX_VALUE);
1414         p.writeLong(11L);
1415         p.writeLong(1L);
1416         p.writeLong(0L);
1417         p.writeLong(-1L);
1418         p.writeLong(-11L);
1419         p.writeLong(Long.MIN_VALUE);
1420         p.setDataPosition(0);
1421         assertEquals(Long.MAX_VALUE, p.readLong());
1422         assertEquals(11L, p.readLong());
1423         assertEquals(1L, p.readLong());
1424         assertEquals(0L, p.readLong());
1425         assertEquals(-1L, p.readLong());
1426         assertEquals(-11L, p.readLong());
1427         assertEquals(Long.MIN_VALUE, p.readLong());
1428         p.recycle();
1429     }
1430 
1431     @Test
testReadLongArray()1432     public void testReadLongArray() {
1433         Parcel p;
1434 
1435         long[] a = {21L};
1436         long[] b = new long[a.length];
1437 
1438         long[] c = {Long.MAX_VALUE, 111L, 11L, 1L, 0L, -1L, -11L, -111L, Long.MIN_VALUE};
1439         long[] d = new long[c.length];
1440 
1441         // test write null
1442         p = Parcel.obtain();
1443         p.writeLongArray(null);
1444         p.setDataPosition(0);
1445         try {
1446             p.readLongArray(null);
1447             fail("Should throw a RuntimeException");
1448         } catch (RuntimeException e) {
1449             //expected
1450         }
1451 
1452         p.setDataPosition(0);
1453         try {
1454             p.readLongArray(b);
1455             fail("Should throw a RuntimeException");
1456         } catch (RuntimeException e) {
1457             //expected
1458         }
1459         p.recycle();
1460 
1461         // test write long array with length: 1
1462         p = Parcel.obtain();
1463         p.writeLongArray(a);
1464         p.setDataPosition(0);
1465         try {
1466             p.readLongArray(d);
1467             fail("Should throw a RuntimeException");
1468         } catch (RuntimeException e) {
1469             //expected
1470         }
1471 
1472         p.setDataPosition(0);
1473         p.readLongArray(b);
1474         for (int i = 0; i < a.length; i++) {
1475             assertEquals(a[i], b[i]);
1476         }
1477         p.recycle();
1478 
1479         // test write long array with length: 9
1480         p = Parcel.obtain();
1481         p.writeLongArray(c);
1482         p.setDataPosition(0);
1483         try {
1484             p.readLongArray(b);
1485             fail("Should throw a RuntimeException");
1486         } catch (RuntimeException e) {
1487             //expected
1488         }
1489 
1490         p.setDataPosition(0);
1491         p.readLongArray(d);
1492         for (int i = 0; i < c.length; i++) {
1493             assertEquals(c[i], d[i]);
1494         }
1495         p.recycle();
1496     }
1497 
1498     @Test
testCreateLongArray()1499     public void testCreateLongArray() {
1500         Parcel p;
1501 
1502         long[] a = {21L};
1503         long[] b;
1504 
1505         long[] c = {Long.MAX_VALUE, 111L, 11L, 1L, 0L, -1L, -11L, -111L, Long.MIN_VALUE};
1506         long[] d;
1507 
1508         long[] e = {};
1509         long[] f;
1510 
1511         // test write null
1512         p = Parcel.obtain();
1513         p.writeLongArray(null);
1514         p.setDataPosition(0);
1515         b = p.createLongArray();
1516         assertNull(b);
1517         p.recycle();
1518 
1519         // test write long array with length: 0
1520         p = Parcel.obtain();
1521         p.writeLongArray(e);
1522         p.setDataPosition(0);
1523         f = p.createLongArray();
1524         assertNotNull(e);
1525         assertEquals(0, f.length);
1526         p.recycle();
1527 
1528         // test write long array with length: 1
1529         p = Parcel.obtain();
1530         p.writeLongArray(a);
1531         p.setDataPosition(0);
1532         b = p.createLongArray();
1533         assertNotNull(b);
1534         for (int i = 0; i < a.length; i++) {
1535             assertEquals(a[i], b[i]);
1536         }
1537         p.recycle();
1538 
1539         // test write long array with length: 9
1540         p = Parcel.obtain();
1541         p.writeLongArray(c);
1542         p.setDataPosition(0);
1543         d = p.createLongArray();
1544         assertNotNull(d);
1545         for (int i = 0; i < c.length; i++) {
1546             assertEquals(c[i], d[i]);
1547         }
1548         p.recycle();
1549     }
1550 
1551     @Test
testReadFloat()1552     public void testReadFloat() {
1553         Parcel p;
1554 
1555         p = Parcel.obtain();
1556         p.writeFloat(.0f);
1557         p.setDataPosition(0);
1558         assertEquals(.0f, p.readFloat(), DELTA_DOUBLE);
1559         p.recycle();
1560 
1561         p = Parcel.obtain();
1562         p.writeFloat(0.1f);
1563         p.setDataPosition(0);
1564         assertEquals(0.1f, p.readFloat(), DELTA_DOUBLE);
1565         p.recycle();
1566 
1567         p = Parcel.obtain();
1568         p.writeFloat(-1.1f);
1569         p.setDataPosition(0);
1570         assertEquals(-1.1f, p.readFloat(), DELTA_DOUBLE);
1571         p.recycle();
1572 
1573         p = Parcel.obtain();
1574         p.writeFloat(Float.MAX_VALUE);
1575         p.setDataPosition(0);
1576         assertEquals(Float.MAX_VALUE, p.readFloat(), DELTA_DOUBLE);
1577         p.recycle();
1578 
1579         p = Parcel.obtain();
1580         p.writeFloat(Float.MIN_VALUE);
1581         p.setDataPosition(0);
1582         assertEquals(Float.MIN_VALUE, p.readFloat(), DELTA_DOUBLE);
1583         p.recycle();
1584 
1585         p = Parcel.obtain();
1586         p.writeFloat(Float.MAX_VALUE);
1587         p.writeFloat(1.1f);
1588         p.writeFloat(0.1f);
1589         p.writeFloat(.0f);
1590         p.writeFloat(-0.1f);
1591         p.writeFloat(-1.1f);
1592         p.writeFloat(Float.MIN_VALUE);
1593         p.setDataPosition(0);
1594         assertEquals(Float.MAX_VALUE, p.readFloat(), DELTA_DOUBLE);
1595         assertEquals(1.1f, p.readFloat(), DELTA_DOUBLE);
1596         assertEquals(0.1f, p.readFloat(), DELTA_DOUBLE);
1597         assertEquals(.0f, p.readFloat(), DELTA_DOUBLE);
1598         assertEquals(-0.1f, p.readFloat(), DELTA_DOUBLE);
1599         assertEquals(-1.1f, p.readFloat(), DELTA_DOUBLE);
1600         assertEquals(Float.MIN_VALUE, p.readFloat(), DELTA_DOUBLE);
1601         p.recycle();
1602     }
1603 
1604     @Test
testReadFloatArray()1605     public void testReadFloatArray() {
1606         Parcel p;
1607 
1608         float[] a = {2.1f};
1609         float[] b = new float[a.length];
1610 
1611         float[] c = {Float.MAX_VALUE, 11.1f, 1.1f, 0.1f, .0f, -0.1f, -1.1f, -11.1f, Float.MIN_VALUE};
1612         float[] d = new float[c.length];
1613 
1614         // test write null
1615         p = Parcel.obtain();
1616         p.writeFloatArray(null);
1617         p.setDataPosition(0);
1618         try {
1619             p.readFloatArray(null);
1620             fail("Should throw a RuntimeException");
1621         } catch (RuntimeException e) {
1622             //expected
1623         }
1624 
1625         p.setDataPosition(0);
1626         try {
1627             p.readFloatArray(b);
1628             fail("Should throw a RuntimeException");
1629         } catch (RuntimeException e) {
1630             //expected
1631         }
1632         p.recycle();
1633 
1634         // test write float array with length: 1
1635         p = Parcel.obtain();
1636         p.writeFloatArray(a);
1637         p.setDataPosition(0);
1638         try {
1639             p.readFloatArray(d);
1640             fail("Should throw a RuntimeException");
1641         } catch (RuntimeException e) {
1642             //expected
1643         }
1644 
1645         p.setDataPosition(0);
1646         p.readFloatArray(b);
1647         for (int i = 0; i < a.length; i++) {
1648             assertEquals(a[i], b[i], DELTA_DOUBLE);
1649         }
1650         p.recycle();
1651 
1652         // test write float array with length: 9
1653         p = Parcel.obtain();
1654         p.writeFloatArray(c);
1655         p.setDataPosition(0);
1656         try {
1657             p.readFloatArray(b);
1658             fail("Should throw a RuntimeException");
1659         } catch (RuntimeException e) {
1660             //expected
1661         }
1662 
1663         p.setDataPosition(0);
1664         p.readFloatArray(d);
1665         for (int i = 0; i < c.length; i++) {
1666             assertEquals(c[i], d[i], DELTA_DOUBLE);
1667         }
1668         p.recycle();
1669     }
1670 
1671     @Test
testCreateFloatArray()1672     public void testCreateFloatArray() {
1673         Parcel p;
1674 
1675         float[] a = {2.1f};
1676         float[] b;
1677 
1678         float[] c = {Float.MAX_VALUE, 11.1f, 1.1f, 0.1f, .0f, -0.1f, -1.1f, -11.1f, Float.MIN_VALUE};
1679         float[] d;
1680 
1681         float[] e = {};
1682         float[] f;
1683 
1684         // test write null
1685         p = Parcel.obtain();
1686         p.writeFloatArray(null);
1687         p.setDataPosition(0);
1688         b = p.createFloatArray();
1689         assertNull(b);
1690         p.recycle();
1691 
1692         // test write float array with length: 0
1693         p = Parcel.obtain();
1694         p.writeFloatArray(e);
1695         p.setDataPosition(0);
1696         f = p.createFloatArray();
1697         assertNotNull(f);
1698         assertEquals(0, f.length);
1699         p.recycle();
1700 
1701         // test write float array with length: 1
1702         p = Parcel.obtain();
1703         p.writeFloatArray(a);
1704         p.setDataPosition(0);
1705         b = p.createFloatArray();
1706         assertNotNull(b);
1707         for (int i = 0; i < a.length; i++) {
1708             assertEquals(a[i], b[i], DELTA_FLOAT);
1709         }
1710         p.recycle();
1711 
1712         // test write float array with length: 9
1713         p = Parcel.obtain();
1714         p.writeFloatArray(c);
1715         p.setDataPosition(0);
1716         d = p.createFloatArray();
1717         assertNotNull(d);
1718         for (int i = 0; i < c.length; i++) {
1719             assertEquals(c[i], d[i], DELTA_FLOAT);
1720         }
1721         p.recycle();
1722     }
1723 
1724     @Test
testReadDouble()1725     public void testReadDouble() {
1726         Parcel p;
1727 
1728         p = Parcel.obtain();
1729         p.writeDouble(.0d);
1730         p.setDataPosition(0);
1731         assertEquals(.0d, p.readDouble(), DELTA_DOUBLE);
1732         p.recycle();
1733 
1734         p = Parcel.obtain();
1735         p.writeDouble(0.1d);
1736         p.setDataPosition(0);
1737         assertEquals(0.1d, p.readDouble(), DELTA_DOUBLE);
1738         p.recycle();
1739 
1740         p = Parcel.obtain();
1741         p.writeDouble(-1.1d);
1742         p.setDataPosition(0);
1743         assertEquals(-1.1d, p.readDouble(), DELTA_DOUBLE);
1744         p.recycle();
1745 
1746         p = Parcel.obtain();
1747         p.writeDouble(Double.MAX_VALUE);
1748         p.setDataPosition(0);
1749         assertEquals(Double.MAX_VALUE, p.readDouble(), DELTA_DOUBLE);
1750         p.recycle();
1751 
1752         p = Parcel.obtain();
1753         p.writeDouble(Double.MIN_VALUE);
1754         p.setDataPosition(0);
1755         assertEquals(Double.MIN_VALUE, p.readDouble(), DELTA_DOUBLE);
1756         p.recycle();
1757 
1758         p = Parcel.obtain();
1759         p.writeDouble(Double.MAX_VALUE);
1760         p.writeDouble(1.1d);
1761         p.writeDouble(0.1d);
1762         p.writeDouble(.0d);
1763         p.writeDouble(-0.1d);
1764         p.writeDouble(-1.1d);
1765         p.writeDouble(Double.MIN_VALUE);
1766         p.setDataPosition(0);
1767         assertEquals(Double.MAX_VALUE, p.readDouble(), DELTA_DOUBLE);
1768         assertEquals(1.1d, p.readDouble(), DELTA_DOUBLE);
1769         assertEquals(0.1d, p.readDouble(), DELTA_DOUBLE);
1770         assertEquals(.0d, p.readDouble(), DELTA_DOUBLE);
1771         assertEquals(-0.1d, p.readDouble(), DELTA_DOUBLE);
1772         assertEquals(-1.1d, p.readDouble(), DELTA_DOUBLE);
1773         assertEquals(Double.MIN_VALUE, p.readDouble(), DELTA_DOUBLE);
1774         p.recycle();
1775     }
1776 
1777     @Test
testReadDoubleArray()1778     public void testReadDoubleArray() {
1779         Parcel p;
1780 
1781         double[] a = {2.1d};
1782         double[] b = new double[a.length];
1783 
1784         double[] c = {Double.MAX_VALUE, 11.1d, 1.1d, 0.1d, .0d, -0.1d, -1.1d, -11.1d, Double.MIN_VALUE};
1785         double[] d = new double[c.length];
1786 
1787         // test write null
1788         p = Parcel.obtain();
1789         p.writeDoubleArray(null);
1790         p.setDataPosition(0);
1791         try {
1792             p.readDoubleArray(null);
1793             fail("Should throw a RuntimeException");
1794         } catch (RuntimeException e) {
1795             //expected
1796         }
1797 
1798         p.setDataPosition(0);
1799         try {
1800             p.readDoubleArray(b);
1801             fail("Should throw a RuntimeException");
1802         } catch (RuntimeException e) {
1803             //expected
1804         }
1805         p.recycle();
1806 
1807         // test write double array with length: 1
1808         p = Parcel.obtain();
1809         p.writeDoubleArray(a);
1810         p.setDataPosition(0);
1811         try {
1812             p.readDoubleArray(d);
1813             fail("Should throw a RuntimeException");
1814         } catch (RuntimeException e) {
1815             //expected
1816         }
1817 
1818         p.setDataPosition(0);
1819         p.readDoubleArray(b);
1820         for (int i = 0; i < a.length; i++) {
1821             assertEquals(a[i], b[i], DELTA_DOUBLE);
1822         }
1823         p.recycle();
1824 
1825         // test write double array with length: 9
1826         p = Parcel.obtain();
1827         p.writeDoubleArray(c);
1828         p.setDataPosition(0);
1829         try {
1830             p.readDoubleArray(b);
1831             fail("Should throw a RuntimeException");
1832         } catch (RuntimeException e) {
1833             //expected
1834         }
1835 
1836         p.setDataPosition(0);
1837         p.readDoubleArray(d);
1838         for (int i = 0; i < c.length; i++) {
1839             assertEquals(c[i], d[i], DELTA_DOUBLE);
1840         }
1841         p.recycle();
1842     }
1843 
1844     @Test
testCreateDoubleArray()1845     public void testCreateDoubleArray() {
1846         Parcel p;
1847 
1848         double[] a = {2.1d};
1849         double[] b;
1850 
1851         double[] c = {
1852                 Double.MAX_VALUE, 11.1d, 1.1d, 0.1d, .0d, -0.1d, -1.1d, -11.1d, Double.MIN_VALUE
1853         };
1854         double[] d;
1855 
1856         double[] e = {};
1857         double[] f;
1858 
1859         // test write null
1860         p = Parcel.obtain();
1861         p.writeDoubleArray(null);
1862         p.setDataPosition(0);
1863         b = p.createDoubleArray();
1864         assertNull(b);
1865         p.recycle();
1866 
1867         // test write double array with length: 0
1868         p = Parcel.obtain();
1869         p.writeDoubleArray(e);
1870         p.setDataPosition(0);
1871         f = p.createDoubleArray();
1872         assertNotNull(f);
1873         assertEquals(0, f.length);
1874         p.recycle();
1875 
1876         // test write double array with length: 1
1877         p = Parcel.obtain();
1878         p.writeDoubleArray(a);
1879         p.setDataPosition(0);
1880         b = p.createDoubleArray();
1881         assertNotNull(b);
1882         for (int i = 0; i < a.length; i++) {
1883             assertEquals(a[i], b[i], DELTA_DOUBLE);
1884         }
1885         p.recycle();
1886 
1887         // test write double array with length: 9
1888         p = Parcel.obtain();
1889         p.writeDoubleArray(c);
1890         p.setDataPosition(0);
1891         d = p.createDoubleArray();
1892         assertNotNull(d);
1893         for (int i = 0; i < c.length; i++) {
1894             assertEquals(c[i], d[i], DELTA_DOUBLE);
1895         }
1896         p.recycle();
1897     }
1898 
1899     @Test
testReadBooleanArray()1900     public void testReadBooleanArray() {
1901         Parcel p;
1902 
1903         boolean[] a = {true};
1904         boolean[] b = new boolean[a.length];
1905 
1906         boolean[] c = {true, false, true, false};
1907         boolean[] d = new boolean[c.length];
1908 
1909         // test write null
1910         p = Parcel.obtain();
1911         p.writeBooleanArray(null);
1912         p.setDataPosition(0);
1913         try {
1914             p.readIntArray(null);
1915             fail("Should throw a RuntimeException");
1916         } catch (RuntimeException e) {
1917             //expected
1918         }
1919 
1920         p.setDataPosition(0);
1921         try {
1922             p.readBooleanArray(b);
1923             fail("Should throw a RuntimeException");
1924         } catch (RuntimeException e) {
1925             //expected
1926         }
1927         p.recycle();
1928 
1929         // test write boolean array with length: 1
1930         p = Parcel.obtain();
1931         p.writeBooleanArray(a);
1932         p.setDataPosition(0);
1933         try {
1934             p.readBooleanArray(d);
1935             fail("Should throw a RuntimeException");
1936         } catch (RuntimeException e) {
1937             //expected
1938         }
1939 
1940         p.setDataPosition(0);
1941         p.readBooleanArray(b);
1942         for (int i = 0; i < a.length; i++) {
1943             assertEquals(a[i], b[i]);
1944         }
1945         p.recycle();
1946 
1947         // test write boolean array with length: 4
1948         p = Parcel.obtain();
1949         p.writeBooleanArray(c);
1950         p.setDataPosition(0);
1951         try {
1952             p.readBooleanArray(b);
1953             fail("Should throw a RuntimeException");
1954         } catch (RuntimeException e) {
1955             //expected
1956         }
1957 
1958         p.setDataPosition(0);
1959         p.readBooleanArray(d);
1960         for (int i = 0; i < c.length; i++) {
1961             assertEquals(c[i], d[i]);
1962         }
1963         p.recycle();
1964     }
1965 
1966     @Test
testCreateBooleanArray()1967     public void testCreateBooleanArray() {
1968         Parcel p;
1969 
1970         boolean[] a = {true};
1971         boolean[] b;
1972 
1973         boolean[] c = {true, false, true, false};
1974         boolean[] d;
1975 
1976         boolean[] e = {};
1977         boolean[] f;
1978 
1979         // test write null
1980         p = Parcel.obtain();
1981         p.writeBooleanArray(null);
1982         p.setDataPosition(0);
1983         b = p.createBooleanArray();
1984         assertNull(b);
1985         p.recycle();
1986 
1987         // test write boolean array with length: 0
1988         p = Parcel.obtain();
1989         p.writeBooleanArray(e);
1990         p.setDataPosition(0);
1991         f = p.createBooleanArray();
1992         assertNotNull(f);
1993         assertEquals(0, f.length);
1994         p.recycle();
1995 
1996         // test write boolean array with length: 1
1997         p = Parcel.obtain();
1998         p.writeBooleanArray(a);
1999 
2000         p.setDataPosition(0);
2001         b = p.createBooleanArray();
2002         assertNotNull(b);
2003         for (int i = 0; i < a.length; i++) {
2004             assertEquals(a[i], b[i]);
2005         }
2006         p.recycle();
2007 
2008         // test write boolean array with length: 4
2009         p = Parcel.obtain();
2010         p.writeBooleanArray(c);
2011         p.setDataPosition(0);
2012         d = p.createBooleanArray();
2013         assertNotNull(d);
2014         for (int i = 0; i < c.length; i++) {
2015             assertEquals(c[i], d[i]);
2016         }
2017         p.recycle();
2018     }
2019 
2020     @Test
testReadString()2021     public void testReadString() {
2022         Parcel p;
2023         final String string = "Hello, Android!";
2024 
2025         // test write null
2026         p = Parcel.obtain();
2027         p.writeString(null);
2028         p.setDataPosition(0);
2029         assertNull(p.readString());
2030         p.recycle();
2031 
2032         p = Parcel.obtain();
2033         p.writeString("");
2034         p.setDataPosition(0);
2035         assertEquals("", p.readString());
2036         p.recycle();
2037 
2038         p = Parcel.obtain();
2039         p.writeString("a");
2040         p.setDataPosition(0);
2041         assertEquals("a", p.readString());
2042         p.recycle();
2043 
2044         p = Parcel.obtain();
2045         p.writeString(string);
2046         p.setDataPosition(0);
2047         assertEquals(string, p.readString());
2048         p.recycle();
2049 
2050         p = Parcel.obtain();
2051         p.writeString(string);
2052         p.writeString("a");
2053         p.writeString("");
2054         p.setDataPosition(0);
2055         assertEquals(string, p.readString());
2056         assertEquals("a", p.readString());
2057         assertEquals("", p.readString());
2058         p.recycle();
2059     }
2060 
2061     @Test
testReadStringArray()2062     public void testReadStringArray() {
2063         Parcel p;
2064 
2065         String[] a = {"21"};
2066         String[] b = new String[a.length];
2067 
2068         String[] c = {"",
2069                 "a",
2070                 "Hello, Android!",
2071                 "A long string that is used to test the api readStringArray(),"};
2072         String[] d = new String[c.length];
2073 
2074         // test write null
2075         p = Parcel.obtain();
2076         p.writeStringArray(null);
2077         p.setDataPosition(0);
2078         try {
2079             p.readStringArray(null);
2080             fail("Should throw a RuntimeException");
2081         } catch (RuntimeException e) {
2082             //expected
2083         }
2084 
2085         p.setDataPosition(0);
2086         try {
2087             p.readStringArray(b);
2088             fail("Should throw a RuntimeException");
2089         } catch (RuntimeException e) {
2090             //expected
2091         }
2092         p.recycle();
2093 
2094         // test write String array with length: 1
2095         p = Parcel.obtain();
2096         p.writeStringArray(a);
2097         p.setDataPosition(0);
2098         try {
2099             p.readStringArray(d);
2100             fail("Should throw a RuntimeException");
2101         } catch (RuntimeException e) {
2102             //expected
2103         }
2104 
2105         p.setDataPosition(0);
2106         p.readStringArray(b);
2107         for (int i = 0; i < a.length; i++) {
2108             assertEquals(a[i], b[i]);
2109         }
2110         p.recycle();
2111 
2112         // test write String array with length: 9
2113         p = Parcel.obtain();
2114         p.writeStringArray(c);
2115         p.setDataPosition(0);
2116         try {
2117             p.readStringArray(b);
2118             fail("Should throw a RuntimeException");
2119         } catch (RuntimeException e) {
2120             //expected
2121         }
2122 
2123         p.setDataPosition(0);
2124         p.readStringArray(d);
2125         for (int i = 0; i < c.length; i++) {
2126             assertEquals(c[i], d[i]);
2127         }
2128         p.recycle();
2129     }
2130 
2131     @Test
testCreateStringArray()2132     public void testCreateStringArray() {
2133         Parcel p;
2134 
2135         String[] a = {"21"};
2136         String[] b;
2137 
2138         String[] c = {"",
2139                 "a",
2140                 "Hello, Android!",
2141                 "A long string that is used to test the api readStringArray(),"};
2142         String[] d;
2143 
2144         String[] e = {};
2145         String[] f;
2146 
2147         // test write null
2148         p = Parcel.obtain();
2149         p.writeStringArray(null);
2150         p.setDataPosition(0);
2151         b = p.createStringArray();
2152         assertNull(b);
2153         p.recycle();
2154 
2155         // test write String array with length: 0
2156         p = Parcel.obtain();
2157         p.writeStringArray(e);
2158         p.setDataPosition(0);
2159         f = p.createStringArray();
2160         assertNotNull(e);
2161         assertEquals(0, f.length);
2162         p.recycle();
2163 
2164         // test write String array with length: 1
2165         p = Parcel.obtain();
2166         p.writeStringArray(a);
2167         p.setDataPosition(0);
2168         b = p.createStringArray();
2169         assertNotNull(b);
2170         for (int i = 0; i < a.length; i++) {
2171             assertEquals(a[i], b[i]);
2172         }
2173         p.recycle();
2174 
2175         // test write String array with length: 9
2176         p = Parcel.obtain();
2177         p.writeStringArray(c);
2178         p.setDataPosition(0);
2179         d = p.createStringArray();
2180         assertNotNull(d);
2181         for (int i = 0; i < c.length; i++) {
2182             assertEquals(c[i], d[i]);
2183         }
2184         p.recycle();
2185     }
2186 
2187     @Test
testReadStringList()2188     public void testReadStringList() {
2189         Parcel p;
2190 
2191         ArrayList<String> a = new ArrayList<String>();
2192         a.add("21");
2193         ArrayList<String> b = new ArrayList<String>();
2194 
2195         ArrayList<String> c = new ArrayList<String>();
2196         c.add("");
2197         c.add("a");
2198         c.add("Hello, Android!");
2199         c.add("A long string that is used to test the api readStringList(),");
2200         ArrayList<String> d = new ArrayList<String>();
2201 
2202         // test write null
2203         p = Parcel.obtain();
2204         p.writeStringList(null);
2205         p.setDataPosition(0);
2206         try {
2207             p.readStringList(null);
2208             fail("Should throw a RuntimeException");
2209         } catch (RuntimeException e) {
2210             //expected
2211         }
2212 
2213         p.setDataPosition(0);
2214         p.readStringList(b);
2215         assertTrue(0 == b.size());
2216         p.recycle();
2217 
2218         // test write String array with length: 1
2219         p = Parcel.obtain();
2220         p.writeStringList(a);
2221         p.setDataPosition(0);
2222         assertTrue(c.size() > a.size());
2223         p.readStringList(c);
2224         assertTrue(c.size() == a.size());
2225         assertEquals(a, c);
2226 
2227         p.setDataPosition(0);
2228         assertTrue(0 == b.size() && 0 != a.size());
2229         p.readStringList(b);
2230         assertEquals(a, b);
2231         p.recycle();
2232 
2233         c = new ArrayList<String>();
2234         c.add("");
2235         c.add("a");
2236         c.add("Hello, Android!");
2237         c.add("A long string that is used to test the api readStringList(),");
2238         // test write String array with length: 4
2239         p = Parcel.obtain();
2240         p.writeStringList(c);
2241         p.setDataPosition(0);
2242 
2243         assertTrue(b.size() < c.size());
2244         p.readStringList(b);
2245         assertTrue(b.size() == c.size());
2246         assertEquals(c, b);
2247 
2248         p.setDataPosition(0);
2249         assertTrue(d.size() < c.size());
2250         p.readStringList(d);
2251         assertEquals(c, d);
2252         p.recycle();
2253     }
2254 
2255     @Test
2256     public void testCreateStringArrayList() {
2257         Parcel p;
2258 
2259         ArrayList<String> a = new ArrayList<String>();
2260         a.add("21");
2261         ArrayList<String> b;
2262 
2263         ArrayList<String> c = new ArrayList<String>();
2264         c.add("");
2265         c.add("a");
2266         c.add("Hello, Android!");
2267         c.add("A long string that is used to test the api readStringList(),");
2268         ArrayList<String> d;
2269 
2270         ArrayList<String> e = new ArrayList<String>();
2271         ArrayList<String> f = null;
2272 
2273         // test write null
2274         p = Parcel.obtain();
2275         p.writeStringList(null);
2276         p.setDataPosition(0);
2277         b = p.createStringArrayList();
2278         assertNull(b);
2279         p.recycle();
2280 
2281         // test write String array with length: 0
2282         p = Parcel.obtain();
2283         p.writeStringList(e);
2284         p.setDataPosition(0);
2285         assertNull(f);
2286         f = p.createStringArrayList();
2287         assertNotNull(f);
2288         p.recycle();
2289 
2290         // test write String array with length: 1
2291         p = Parcel.obtain();
2292         p.writeStringList(a);
2293         p.setDataPosition(0);
2294         b = p.createStringArrayList();
2295         assertEquals(a, b);
2296         p.recycle();
2297 
2298         // test write String array with length: 4
2299         p = Parcel.obtain();
2300         p.writeStringList(c);
2301         p.setDataPosition(0);
2302         d = p.createStringArrayList();
2303         assertEquals(c, d);
2304         p.recycle();
2305     }
2306 
2307     @Test
2308     public void testReadSerializable() {
2309         Parcel p;
2310 
2311         // test write null
2312         p = Parcel.obtain();
2313         p.writeSerializable(null);
2314         p.setDataPosition(0);
2315         assertNull(p.readSerializable());
2316         p.recycle();
2317 
2318         p = Parcel.obtain();
2319         p.writeSerializable("Hello, Android!");
2320         p.setDataPosition(0);
2321         assertEquals("Hello, Android!", p.readSerializable());
2322         p.recycle();
2323     }
2324 
2325     @Test
2326     public void testReadSerializableWithClass_whenNull(){
2327         Parcel p = Parcel.obtain();
2328         MockClassLoader mcl = new MockClassLoader();
2329         p.writeSerializable(null);
2330         p.setDataPosition(0);
2331         assertNull(p.readSerializable(mcl, Exception.class));
2332 
2333         p.setDataPosition(0);
2334         assertNull(p.readSerializable(null, Exception.class));
2335         p.recycle();
2336     }
2337 
2338     @Test
2339     @DisabledOnRavenwood(reason = "Test classes are loaded with the bootclassloader")
2340     public void testReadSerializableWithClass_whenNullClassLoader(){
2341         Parcel p = Parcel.obtain();
2342         TestSubException testSubException = new TestSubException("test");
2343         p.writeSerializable(testSubException);
2344         p.setDataPosition(0);
2345         Throwable error = assertThrows(BadParcelableException.class, () ->
2346                 p.readSerializable(null, TestSubException.class));
2347         assertTrue(error.getMessage().contains("ClassNotFoundException reading a Serializable"));
2348         p.recycle();
2349     }
2350 
2351     @Test
2352     public void testReadSerializableWithClass_whenSameClass(){
2353         Parcel p = Parcel.obtain();
2354         MockClassLoader mcl = new MockClassLoader();
2355         Throwable throwable = new Throwable("test");
2356         p.writeSerializable(throwable);
2357         p.setDataPosition(0);
2358         Object object = p.readSerializable(mcl, Throwable.class);
2359         assertTrue(object instanceof Throwable);
2360         Throwable t1 = (Throwable) object;
2361         assertEquals("test", t1.getMessage());
2362 
2363         p.setDataPosition(0);
2364         Object object1 = p.readSerializable(null, Throwable.class);
2365         assertTrue(object1 instanceof Throwable);
2366         Throwable t2 = (Throwable) object1;
2367         assertEquals("test", t2.getMessage());
2368         p.recycle();
2369     }
2370 
2371     @Test
2372     public void testReadSerializableWithClass_whenSubClass(){
2373         Parcel p = Parcel.obtain();
2374         MockClassLoader mcl = new MockClassLoader();
2375         Exception exception = new Exception("test");
2376         p.writeSerializable(exception);
2377         p.setDataPosition(0);
2378         Object object = p.readSerializable(mcl, Throwable.class);
2379         assertTrue(object instanceof Exception);
2380         Exception e1 = (Exception) object;
2381         assertEquals("test", e1.getMessage());
2382 
2383         p.setDataPosition(0);
2384         Object object1 = p.readSerializable(null, Throwable.class);
2385         assertTrue(object1 instanceof Exception);
2386         Exception e2 = (Exception) object1;
2387         assertEquals("test", e2.getMessage());
2388 
2389         p.setDataPosition(0);
2390         Object object2 = p.readSerializable(null, Object.class);
2391         assertTrue(object1 instanceof Exception);
2392         Exception e3 = (Exception) object2;
2393         assertEquals("test", e3.getMessage());
2394 
2395         p.setDataPosition(0);
2396         assertThrows(BadParcelableException.class, () -> p.readSerializable(mcl, String.class));
2397         p.recycle();
2398     }
2399 
2400     @Test
testReadParcelable()2401     public void testReadParcelable() {
2402         Parcel p;
2403         MockClassLoader mcl = new MockClassLoader();
2404         final String signatureString  = "1234567890abcdef";
2405         Signature s = new Signature(signatureString);
2406 
2407         // test write null
2408         p = Parcel.obtain();
2409         p.writeParcelable(null, 0);
2410         p.setDataPosition(0);
2411         assertNull(p.readParcelable(mcl));
2412         p.recycle();
2413 
2414         p = Parcel.obtain();
2415         p.writeParcelable(s, 0);
2416         p.setDataPosition(0);
2417         assertEquals(s, p.readParcelable(mcl));
2418 
2419         p.recycle();
2420     }
2421 
2422     @Test
testReadParcelableWithClass()2423     public void testReadParcelableWithClass() {
2424         Parcel p;
2425         MockClassLoader mcl = new MockClassLoader();
2426         final String signatureString  = "1234567890abcdef";
2427         Signature s = new Signature(signatureString);
2428 
2429         p = Parcel.obtain();
2430         p.writeParcelable(s, 0);
2431         p.setDataPosition(0);
2432         assertEquals(s, p.readParcelable(mcl, Signature.class));
2433 
2434         p.setDataPosition(0);
2435         assertThrows(BadParcelableException.class, () -> p.readParcelable(mcl, Intent.class));
2436         p.recycle();
2437     }
2438 
2439     @Test
testReadParcelableWithSubClass()2440     public void testReadParcelableWithSubClass() {
2441         Parcel p;
2442 
2443         final TestSubIntent testSubIntent = new TestSubIntent(new Intent(), "Test");
2444         p = Parcel.obtain();
2445         p.writeParcelable(testSubIntent, 0);
2446         p.setDataPosition(0);
2447         assertEquals(testSubIntent, (p.readParcelable(getClass().getClassLoader(), Intent.class)));
2448 
2449         p.setDataPosition(0);
2450         assertEquals(testSubIntent, (p.readParcelable(getClass().getClassLoader(), Object.class)));
2451         p.recycle();
2452     }
2453 
2454     @Test
testReadParcelableCreator()2455     public void testReadParcelableCreator() {
2456         MockClassLoader mcl = new MockClassLoader();
2457         final String signatureString  = "1234567890abcdef";
2458         Signature s = new Signature(signatureString);
2459 
2460         Parcel p = Parcel.obtain();
2461         p.writeParcelableCreator(s);
2462         p.setDataPosition(0);
2463         assertSame(Signature.CREATOR, p.readParcelableCreator(mcl));
2464 
2465         p.recycle();
2466     }
2467 
2468     @Test
testReadParcelableCreatorWithClass()2469     public void testReadParcelableCreatorWithClass() {
2470         MockClassLoader mcl = new MockClassLoader();
2471         final String signatureString  = "1234567890abcdef";
2472         Signature s = new Signature(signatureString);
2473 
2474         Parcel p = Parcel.obtain();
2475         p.writeParcelableCreator(s);
2476 
2477         p.setDataPosition(0);
2478         assertThrows(BadParcelableException.class, () -> p.readParcelableCreator(mcl, Intent.class));
2479         p.recycle();
2480     }
2481 
2482     @Test
testReadParcelableCreatorWithSubClass()2483     public void testReadParcelableCreatorWithSubClass() {
2484         final TestSubIntent testSubIntent = new TestSubIntent(new Intent(), "1234567890abcdef");
2485 
2486         Parcel p = Parcel.obtain();
2487         p.writeParcelableCreator(testSubIntent);
2488 
2489         p.setDataPosition(0);
2490         assertSame(TestSubIntent.CREATOR,
2491                 p.readParcelableCreator(getClass().getClassLoader(), Intent.class));
2492         p.recycle();
2493     }
2494 
2495     @Test
testReadParcelableArray()2496     public void testReadParcelableArray() {
2497         Parcel p;
2498         MockClassLoader mcl = new MockClassLoader();
2499         Signature[] s = {new Signature("1234"),
2500                 new Signature("ABCD"),
2501                 new Signature("abcd")};
2502 
2503         Signature[] s2 = {new Signature("1234"),
2504                 null,
2505                 new Signature("abcd")};
2506         Parcelable[] s3;
2507 
2508         // test write null
2509         p = Parcel.obtain();
2510         p.writeParcelableArray(null, 0);
2511         p.setDataPosition(0);
2512         assertNull(p.readParcelableArray(mcl));
2513         p.recycle();
2514 
2515         p = Parcel.obtain();
2516         p.writeParcelableArray(s, 0);
2517         p.setDataPosition(0);
2518         s3 = p.readParcelableArray(mcl);
2519         for (int i = 0; i < s.length; i++) {
2520             assertEquals(s[i], s3[i]);
2521         }
2522         p.recycle();
2523 
2524         p = Parcel.obtain();
2525         p.writeParcelableArray(s2, 0);
2526         p.setDataPosition(0);
2527         s3 = p.readParcelableArray(mcl);
2528         for (int i = 0; i < s2.length; i++) {
2529             assertEquals(s2[i], s3[i]);
2530         }
2531         p.recycle();
2532     }
2533 
2534     @Test
testReadParcelableArrayWithClass_whenNull()2535     public void testReadParcelableArrayWithClass_whenNull() {
2536         Parcel p = Parcel.obtain();
2537         p.writeParcelableArray(null, 0);
2538         p.setDataPosition(0);
2539         assertNull(p.readParcelableArray(getClass().getClassLoader(), Intent.class));
2540         p.recycle();
2541     }
2542 
2543     @Test
testReadParcelableArrayWithClass_whenSameClass()2544     public void testReadParcelableArrayWithClass_whenSameClass(){
2545         Parcel p = Parcel.obtain();
2546         MockClassLoader mcl = new MockClassLoader();
2547         Signature[] s = {new Signature("1234"),
2548                 null,
2549                 new Signature("abcd")
2550         };
2551         p.writeParcelableArray(s, 0);
2552         p.setDataPosition(0);
2553         Parcelable[] s1 = p.readParcelableArray(mcl, Signature.class);
2554         assertTrue(Arrays.equals(s, s1));
2555         p.recycle();
2556     }
2557 
2558     @Test
testReadParcelableArrayWithClass_whenSubclasses()2559     public void testReadParcelableArrayWithClass_whenSubclasses() {
2560         Parcel p = Parcel.obtain();
2561         final Intent baseIntent = new Intent();
2562         Intent[] intentArray = {
2563                 new TestSubIntent(baseIntent, "1234567890abcdef"),
2564                 null,
2565                 new TestSubIntent(baseIntent, "abcdef1234567890")
2566         };
2567 
2568         p.writeParcelableArray(intentArray, 0);
2569         p.setDataPosition(0);
2570         Parcelable[] s = p.readParcelableArray(getClass().getClassLoader(), Intent.class);
2571         assertTrue(Arrays.equals(intentArray, s));
2572 
2573         p.setDataPosition(0);
2574         assertThrows(BadParcelableException.class, () -> p.readParcelableArray(
2575                 getClass().getClassLoader(), Signature.class));
2576         p.recycle();
2577     }
2578 
2579     @Test
testReadTypedArray()2580     public void testReadTypedArray() {
2581         Parcel p;
2582         Signature[] s = {new Signature("1234"),
2583                 new Signature("ABCD"),
2584                 new Signature("abcd")};
2585 
2586         Signature[] s2 = {new Signature("1234"),
2587                 null,
2588                 new Signature("abcd")};
2589         Signature[] s3 = new Signature[3];
2590         Signature[] s4 = new Signature[4];
2591 
2592         // test write null
2593         p = Parcel.obtain();
2594         p.writeTypedArray(null, 0);
2595         p.setDataPosition(0);
2596         try {
2597             p.readTypedArray(s3, Signature.CREATOR);
2598             fail("should throw a RuntimeException");
2599         } catch (RuntimeException e) {
2600             //expected
2601         }
2602 
2603         p.setDataPosition(0);
2604         try {
2605             p.readTypedArray(null, Signature.CREATOR);
2606             fail("should throw a RuntimeException");
2607         } catch (RuntimeException e) {
2608             //expected
2609         }
2610         p.recycle();
2611 
2612         // test write not null
2613         p = Parcel.obtain();
2614         p.writeTypedArray(s, 0);
2615         p.setDataPosition(0);
2616         p.readTypedArray(s3, Signature.CREATOR);
2617         for (int i = 0; i < s.length; i++) {
2618             assertEquals(s[i], s3[i]);
2619         }
2620 
2621         p.setDataPosition(0);
2622         try {
2623             p.readTypedArray(null, Signature.CREATOR);
2624             fail("should throw a RuntimeException");
2625         } catch (RuntimeException e) {
2626             //expected
2627         }
2628 
2629         p.setDataPosition(0);
2630         try {
2631             p.readTypedArray(s4, Signature.CREATOR);
2632             fail("should throw a RuntimeException");
2633         } catch (RuntimeException e) {
2634             //expected
2635         }
2636         p.recycle();
2637 
2638         s3 = new Signature[s2.length];
2639         p = Parcel.obtain();
2640         p.writeTypedArray(s2, 0);
2641         p.setDataPosition(0);
2642         p.readTypedArray(s3, Signature.CREATOR);
2643         for (int i = 0; i < s.length; i++) {
2644             assertEquals(s2[i], s3[i]);
2645         }
2646         p.recycle();
2647     }
2648 
2649     @Test
testReadTypedArray2()2650     public void testReadTypedArray2() {
2651         Parcel p;
2652         Signature[] s = {
2653                 new Signature("1234"), new Signature("ABCD"), new Signature("abcd")
2654         };
2655 
2656         Signature[] s2 = {
2657                 new Signature("1234"), null, new Signature("abcd")
2658         };
2659         Signature[] s3 = {
2660                 null, null, null
2661         };
2662 
2663         // test write null
2664         p = Parcel.obtain();
2665         p.writeTypedArray(null, 0);
2666         p.setDataPosition(0);
2667         p.recycle();
2668 
2669         // test write not null
2670         p = Parcel.obtain();
2671         p.writeTypedArray(s, 0);
2672         p.setDataPosition(0);
2673         p.readTypedArray(s3, Signature.CREATOR);
2674         for (int i = 0; i < s.length; i++) {
2675             assertEquals(s[i], s3[i]);
2676         }
2677         p.recycle();
2678 
2679         p = Parcel.obtain();
2680         p.writeTypedArray(s2, 0);
2681         p.setDataPosition(0);
2682         p.readTypedArray(s3, Signature.CREATOR);
2683         for (int i = 0; i < s.length; i++) {
2684             assertEquals(s2[i], s3[i]);
2685         }
2686         p.recycle();
2687     }
2688 
2689     @Test
testCreateTypedArray()2690     public void testCreateTypedArray() {
2691         Parcel p;
2692         Signature[] s = {new Signature("1234"),
2693                 new Signature("ABCD"),
2694                 new Signature("abcd")};
2695 
2696         Signature[] s2 = {new Signature("1234"),
2697                 null,
2698                 new Signature("abcd")};
2699         Signature[] s3;
2700 
2701         // test write null
2702         p = Parcel.obtain();
2703         p.writeTypedArray(null, 0);
2704         p.setDataPosition(0);
2705         assertNull(p.createTypedArray(Signature.CREATOR));
2706         p.recycle();
2707 
2708         // test write not null
2709         p = Parcel.obtain();
2710         p.writeTypedArray(s, 0);
2711         p.setDataPosition(0);
2712         s3 = p.createTypedArray(Signature.CREATOR);
2713         for (int i = 0; i < s.length; i++) {
2714             assertEquals(s[i], s3[i]);
2715         }
2716         p.recycle();
2717 
2718         p = Parcel.obtain();
2719         p.writeTypedArray(s2, 0);
2720         p.setDataPosition(0);
2721         s3 = p.createTypedArray(Signature.CREATOR);
2722         for (int i = 0; i < s.length; i++) {
2723             assertEquals(s2[i], s3[i]);
2724         }
2725         p.recycle();
2726     }
2727 
2728     @Test
testReadTypedList()2729     public void testReadTypedList() {
2730         Parcel p;
2731         ArrayList<Signature> s = new ArrayList<Signature>();
2732         s.add(new Signature("1234"));
2733         s.add(new Signature("ABCD"));
2734         s.add(new Signature("abcd"));
2735 
2736         ArrayList<Signature> s2 = new ArrayList<Signature>();
2737         s2.add(new Signature("1234"));
2738         s2.add(null);
2739 
2740         ArrayList<Signature> s3 = new ArrayList<Signature>();
2741 
2742         // test write null
2743         p = Parcel.obtain();
2744         p.writeTypedList(null);
2745         p.setDataPosition(0);
2746         p.readTypedList(s3, Signature.CREATOR);
2747         assertEquals(0, s3.size());
2748 
2749         p.setDataPosition(0);
2750         try {
2751             p.readTypedList(null, Signature.CREATOR);
2752             fail("should throw a RuntimeException");
2753         } catch (RuntimeException e) {
2754             //expected
2755         }
2756         p.recycle();
2757 
2758         // test write not null
2759         p = Parcel.obtain();
2760         p.writeTypedList(s);
2761         p.setDataPosition(0);
2762         p.readTypedList(s3, Signature.CREATOR);
2763         for (int i = 0; i < s.size(); i++) {
2764             assertEquals(s.get(i), s3.get(i));
2765         }
2766 
2767         p.setDataPosition(0);
2768         try {
2769             p.readTypedList(null, Signature.CREATOR);
2770             fail("should throw a RuntimeException");
2771         } catch (RuntimeException e) {
2772             //expected
2773         }
2774 
2775         p.setDataPosition(0);
2776         p.readTypedList(s2, Signature.CREATOR);
2777         assertEquals(s.size(), s2.size());
2778         for (int i = 0; i < s.size(); i++) {
2779             assertEquals(s.get(i), s2.get(i));
2780         }
2781         p.recycle();
2782 
2783         s2 = new ArrayList<Signature>();
2784         s2.add(new Signature("1234"));
2785         s2.add(null);
2786         p = Parcel.obtain();
2787         p.writeTypedList(s2);
2788         p.setDataPosition(0);
2789         p.readTypedList(s3, Signature.CREATOR);
2790         assertEquals(s3.size(), s2.size());
2791         for (int i = 0; i < s2.size(); i++) {
2792             assertEquals(s2.get(i), s3.get(i));
2793         }
2794         p.recycle();
2795     }
2796 
2797     @Test
testCreateTypedArrayList()2798     public void testCreateTypedArrayList() {
2799         Parcel p;
2800         ArrayList<Signature> s = new ArrayList<Signature>();
2801         s.add(new Signature("1234"));
2802         s.add(new Signature("ABCD"));
2803         s.add(new Signature("abcd"));
2804 
2805         ArrayList<Signature> s2 = new ArrayList<Signature>();
2806         s2.add(new Signature("1234"));
2807         s2.add(null);
2808 
2809         ArrayList<Signature> s3;
2810 
2811         // test write null
2812         p = Parcel.obtain();
2813         p.writeTypedList(null);
2814         p.setDataPosition(0);
2815         assertNull(p.createTypedArrayList(Signature.CREATOR));
2816         p.recycle();
2817 
2818         // test write not null
2819         p = Parcel.obtain();
2820         p.writeTypedList(s);
2821         p.setDataPosition(0);
2822         s3 = p.createTypedArrayList(Signature.CREATOR);
2823         for (int i = 0; i < s.size(); i++) {
2824             assertEquals(s.get(i), s3.get(i));
2825         }
2826 
2827         p = Parcel.obtain();
2828         p.writeTypedList(s2);
2829         p.setDataPosition(0);
2830         s3 = p.createTypedArrayList(Signature.CREATOR);
2831         assertEquals(s3.size(), s2.size());
2832         for (int i = 0; i < s2.size(); i++) {
2833             assertEquals(s2.get(i), s3.get(i));
2834         }
2835         p.recycle();
2836     }
2837 
2838     @Test
testReadException()2839     public void testReadException() {
2840     }
2841 
2842     @Test
testReadException2()2843     public void testReadException2() {
2844         Parcel p = Parcel.obtain();
2845         String msg = "testReadException2";
2846 
2847         p.writeException(new SecurityException(msg));
2848         p.setDataPosition(0);
2849         try {
2850             p.readException();
2851             fail("Should throw a SecurityException");
2852         } catch (SecurityException e) {
2853             assertEquals(msg, e.getMessage());
2854         }
2855 
2856         p.setDataPosition(0);
2857         p.writeException(new BadParcelableException(msg));
2858         p.setDataPosition(0);
2859         try {
2860             p.readException();
2861             fail("Should throw a BadParcelableException");
2862         } catch (BadParcelableException e) {
2863             assertEquals(msg, e.getMessage());
2864         }
2865 
2866         p.setDataPosition(0);
2867         p.writeException(new IllegalArgumentException(msg));
2868         p.setDataPosition(0);
2869         try {
2870             p.readException();
2871             fail("Should throw an IllegalArgumentException");
2872         } catch (IllegalArgumentException e) {
2873             assertEquals(msg, e.getMessage());
2874         }
2875 
2876         p.setDataPosition(0);
2877         p.writeException(new NullPointerException(msg));
2878         p.setDataPosition(0);
2879         try {
2880             p.readException();
2881             fail("Should throw a NullPointerException");
2882         } catch (NullPointerException e) {
2883             assertEquals(msg, e.getMessage());
2884         }
2885 
2886         p.setDataPosition(0);
2887         p.writeException(new IllegalStateException(msg));
2888         p.setDataPosition(0);
2889         try {
2890             p.readException();
2891             fail("Should throw an IllegalStateException");
2892         } catch (IllegalStateException e) {
2893             assertEquals(msg, e.getMessage());
2894         }
2895 
2896         p.setDataPosition(0);
2897         try {
2898             p.writeException(new RuntimeException());
2899             fail("Should throw a RuntimeException");
2900         } catch (RuntimeException e) {
2901             //expected
2902         }
2903         p.recycle();
2904     }
2905 
2906     @Test
testWriteNoException()2907     public void testWriteNoException() {
2908         Parcel p = Parcel.obtain();
2909         p.writeNoException();
2910         p.setDataPosition(0);
2911         p.readException();
2912         p.recycle();
2913     }
2914 
2915     @Test
testWriteFileDescriptor()2916     public void testWriteFileDescriptor() {
2917         Parcel p;
2918         FileDescriptor fIn = FileDescriptor.in;
2919         ParcelFileDescriptor pfd;
2920 
2921         p = Parcel.obtain();
2922         pfd = p.readFileDescriptor();
2923         assertNull(pfd);
2924         p.recycle();
2925 
2926         p = Parcel.obtain();
2927         p.writeFileDescriptor(fIn);
2928         p.setDataPosition(0);
2929         pfd = p.readFileDescriptor();
2930         assertNotNull(pfd);
2931         assertNotNull(pfd.getFileDescriptor());
2932         p.recycle();
2933     }
2934 
2935     @Test
testHasFileDescriptor()2936     public void testHasFileDescriptor() {
2937         Parcel p;
2938         FileDescriptor fIn = FileDescriptor.in;
2939 
2940         p = Parcel.obtain();
2941         p.writeFileDescriptor(fIn);
2942         p.setDataPosition(0);
2943         assertTrue(p.hasFileDescriptors());
2944         p.recycle();
2945 
2946         p = Parcel.obtain();
2947         p.writeInt(111);
2948         p.setDataPosition(0);
2949         assertFalse(p.hasFileDescriptors());
2950         p.recycle();
2951     }
2952 
2953     @Test
testHasFileDescriptorInRange_outsideRange()2954     public void testHasFileDescriptorInRange_outsideRange() {
2955         Parcel p = Parcel.obtain();
2956         int i0 = p.dataPosition();
2957         p.writeInt(13);
2958         int i1 = p.dataPosition();
2959         p.writeFileDescriptor(FileDescriptor.in);
2960         int i2 = p.dataPosition();
2961         p.writeString("Tiramisu");
2962         int i3 = p.dataPosition();
2963 
2964         assertFalse(p.hasFileDescriptors(i0, i1 - i0));
2965         assertFalse(p.hasFileDescriptors(i2, i3 - i2));
2966         p.recycle();
2967     }
2968 
2969     @Test
testHasFileDescriptorInRange_partiallyInsideRange()2970     public void testHasFileDescriptorInRange_partiallyInsideRange() {
2971         Parcel p = Parcel.obtain();
2972         int i0 = p.dataPosition();
2973         p.writeInt(13);
2974         int i1 = p.dataPosition();
2975         p.writeFileDescriptor(FileDescriptor.in);
2976         int i2 = p.dataPosition();
2977         p.writeString("Tiramisu");
2978         int i3 = p.dataPosition();
2979 
2980         // It has to contain the whole object
2981         assertFalse(p.hasFileDescriptors(i1, i2 - i1 - 1));
2982         assertFalse(p.hasFileDescriptors(i1 + 1, i2 - i1));
2983         p.recycle();
2984     }
2985 
2986     @Test
testHasFileDescriptorInRange_insideRange()2987     public void testHasFileDescriptorInRange_insideRange() {
2988         Parcel p = Parcel.obtain();
2989         int i0 = p.dataPosition();
2990         p.writeInt(13);
2991         int i1 = p.dataPosition();
2992         p.writeFileDescriptor(FileDescriptor.in);
2993         int i2 = p.dataPosition();
2994         p.writeString("Tiramisu");
2995         int i3 = p.dataPosition();
2996 
2997         assertTrue(p.hasFileDescriptors(i0, i2 - i0));
2998         assertTrue(p.hasFileDescriptors(i1, i2 - i1));
2999         assertTrue(p.hasFileDescriptors(i1, i3 - i1));
3000         assertTrue(p.hasFileDescriptors(i0, i3 - i0));
3001         assertTrue(p.hasFileDescriptors(i0, p.dataSize()));
3002         p.recycle();
3003     }
3004 
3005     @Test
testHasFileDescriptorInRange_zeroLength()3006     public void testHasFileDescriptorInRange_zeroLength() {
3007         Parcel p = Parcel.obtain();
3008         int i0 = p.dataPosition();
3009         p.writeInt(13);
3010         int i1 = p.dataPosition();
3011         p.writeFileDescriptor(FileDescriptor.in);
3012         int i2 = p.dataPosition();
3013         p.writeString("Tiramisu");
3014         int i3 = p.dataPosition();
3015 
3016         assertFalse(p.hasFileDescriptors(i1, 0));
3017         p.recycle();
3018     }
3019 
3020     /**
3021      * When we rewind the cursor using {@link Parcel#setDataPosition(int)} and write a FD, the
3022      * internal representation of FDs in {@link Parcel} may lose the sorted property, so we test
3023      * this case.
3024      */
3025     @Test
testHasFileDescriptorInRange_withUnsortedFdObjects()3026     public void testHasFileDescriptorInRange_withUnsortedFdObjects() {
3027         Parcel p = Parcel.obtain();
3028         int i0 = p.dataPosition();
3029         p.writeLongArray(new long[] {0, 0, 0, 0, 0, 0});
3030         int i1 = p.dataPosition();
3031         p.writeFileDescriptor(FileDescriptor.in);
3032         p.setDataPosition(0);
3033         p.writeFileDescriptor(FileDescriptor.in);
3034         p.setDataPosition(0);
3035 
3036         assertTrue(p.hasFileDescriptors(i0, i1 - i0));
3037         p.recycle();
3038     }
3039 
3040     @Test
testHasFileDescriptorInRange_limitOutOfBounds()3041     public void testHasFileDescriptorInRange_limitOutOfBounds() {
3042         Parcel p = Parcel.obtain();
3043         int i0 = p.dataPosition();
3044         p.writeFileDescriptor(FileDescriptor.in);
3045         int i1 = p.dataPosition();
3046 
3047         assertThrows(IllegalArgumentException.class, () -> p.hasFileDescriptors(i0, i1 - i0 + 1));
3048         assertThrows(IllegalArgumentException.class,
3049                 () -> p.hasFileDescriptors(0, p.dataSize() + 1));
3050         p.recycle();
3051     }
3052 
3053     @Test
testHasFileDescriptorInRange_offsetOutOfBounds()3054     public void testHasFileDescriptorInRange_offsetOutOfBounds() {
3055         Parcel p = Parcel.obtain();
3056         int i0 = p.dataPosition();
3057         p.writeFileDescriptor(FileDescriptor.in);
3058         int i1 = p.dataPosition();
3059 
3060         assertThrows(IllegalArgumentException.class, () -> p.hasFileDescriptors(i1, 1));
3061         assertThrows(IllegalArgumentException.class, () -> p.hasFileDescriptors(i1 + 1, 1));
3062         p.recycle();
3063     }
3064 
3065     @Test
testHasFileDescriptorInRange_offsetOutOfBoundsAndZeroLength()3066     public void testHasFileDescriptorInRange_offsetOutOfBoundsAndZeroLength() {
3067         Parcel p = Parcel.obtain();
3068         int i0 = p.dataPosition();
3069         p.writeFileDescriptor(FileDescriptor.in);
3070         int i1 = p.dataPosition();
3071 
3072         assertFalse(p.hasFileDescriptors(i1, 0));
3073         assertThrows(IllegalArgumentException.class, () -> p.hasFileDescriptors(i1 + 1, 0));
3074         p.recycle();
3075     }
3076 
3077     @Test
testHasFileDescriptorInRange_zeroLengthParcel()3078     public void testHasFileDescriptorInRange_zeroLengthParcel() {
3079         Parcel p = Parcel.obtain();
3080 
3081         assertFalse(p.hasFileDescriptors(0, 0));
3082         p.recycle();
3083     }
3084 
3085     @Test
testHasFileDescriptorInRange_negativeLength()3086     public void testHasFileDescriptorInRange_negativeLength() {
3087         Parcel p = Parcel.obtain();
3088         int i0 = p.dataPosition();
3089         p.writeFileDescriptor(FileDescriptor.in);
3090         int i1 = p.dataPosition();
3091 
3092         assertThrows(IllegalArgumentException.class, () -> p.hasFileDescriptors(i0, -1));
3093         assertThrows(IllegalArgumentException.class, () -> p.hasFileDescriptors(i0, -(i1 - i0)));
3094         p.recycle();
3095     }
3096 
3097     @Test
testHasFileDescriptorInRange_negativeOffset()3098     public void testHasFileDescriptorInRange_negativeOffset() {
3099         Parcel p = Parcel.obtain();
3100         p.writeFileDescriptor(FileDescriptor.in);
3101 
3102         assertThrows(IllegalArgumentException.class, () -> p.hasFileDescriptors(-1, 1));
3103         p.recycle();
3104     }
3105 
3106     @Test
testReadBundle()3107     public void testReadBundle() {
3108         Bundle bundle = new Bundle();
3109         bundle.putBoolean("boolean", true);
3110         bundle.putInt("int", Integer.MAX_VALUE);
3111         bundle.putString("string", "String");
3112 
3113         Bundle bundle2;
3114         Parcel p;
3115 
3116         // test null
3117         p = Parcel.obtain();
3118         p.writeBundle(null);
3119         p.setDataPosition(0);
3120         bundle2 = p.readBundle();
3121         assertNull(bundle2);
3122         p.recycle();
3123 
3124         // test not null
3125         bundle2 = null;
3126         p = Parcel.obtain();
3127         p.writeBundle(bundle);
3128         p.setDataPosition(0);
3129         bundle2 = p.readBundle();
3130         assertNotNull(bundle2);
3131         assertEquals(true, bundle2.getBoolean("boolean"));
3132         assertEquals(Integer.MAX_VALUE, bundle2.getInt("int"));
3133         assertEquals("String", bundle2.getString("string"));
3134         p.recycle();
3135 
3136         bundle2 = null;
3137         Parcel a = Parcel.obtain();
3138         bundle2 = new Bundle();
3139         bundle2.putString("foo", "test");
3140         a.writeBundle(bundle2);
3141         a.setDataPosition(0);
3142         bundle.readFromParcel(a);
3143         p = Parcel.obtain();
3144         p.setDataPosition(0);
3145         p.writeBundle(bundle);
3146         p.setDataPosition(0);
3147         bundle2 = p.readBundle();
3148         assertNotNull(bundle2);
3149         assertFalse(true == bundle2.getBoolean("boolean"));
3150         assertFalse(Integer.MAX_VALUE == bundle2.getInt("int"));
3151         assertFalse("String".equals( bundle2.getString("string")));
3152         a.recycle();
3153         p.recycle();
3154     }
3155 
3156     @Test
testReadBundle2()3157     public void testReadBundle2() {
3158         Bundle b = new Bundle();
3159         b.putBoolean("boolean", true);
3160         b.putInt("int", Integer.MAX_VALUE);
3161         b.putString("string", "String");
3162 
3163         Bundle u;
3164         Parcel p;
3165         MockClassLoader m = new MockClassLoader();
3166 
3167         p = Parcel.obtain();
3168         p.writeBundle(null);
3169         p.setDataPosition(0);
3170         u = p.readBundle(m);
3171         assertNull(u);
3172         p.recycle();
3173 
3174         u = null;
3175         p = Parcel.obtain();
3176         p.writeBundle(b);
3177         p.setDataPosition(0);
3178         u = p.readBundle(m);
3179         assertNotNull(u);
3180         assertEquals(true, b.getBoolean("boolean"));
3181         assertEquals(Integer.MAX_VALUE, b.getInt("int"));
3182         assertEquals("String", b.getString("string"));
3183         p.recycle();
3184     }
3185 
3186     @Test
testWriteArray()3187     public void testWriteArray() {
3188         Parcel p;
3189         MockClassLoader mcl = new MockClassLoader();
3190 
3191         p = Parcel.obtain();
3192         p.writeArray(null);
3193         p.setDataPosition(0);
3194         assertNull(p.readArray(mcl));
3195         p.recycle();
3196 
3197         Object[] objects = new Object[5];
3198         objects[0] = Integer.MAX_VALUE;
3199         objects[1] = true;
3200         objects[2] = Long.MAX_VALUE;
3201         objects[3] = "String";
3202         objects[4] = Float.MAX_VALUE;
3203         Object[] objects2;
3204 
3205         p = Parcel.obtain();
3206         p.writeArray(objects);
3207         p.setDataPosition(0);
3208         objects2 = p.readArray(mcl);
3209         assertNotNull(objects2);
3210         for (int i = 0; i < objects2.length; i++) {
3211             assertEquals(objects[i], objects2[i]);
3212         }
3213         p.recycle();
3214     }
3215 
3216     @Test
testReadArrayWithClass_whenNull()3217     public void testReadArrayWithClass_whenNull(){
3218         Parcel p = Parcel.obtain();
3219         MockClassLoader mcl = new MockClassLoader();
3220 
3221         p.writeArray(null);
3222         p.setDataPosition(0);
3223         assertNull(p.readArray(mcl, Intent.class));
3224         p.recycle();
3225     }
3226 
3227     @Test
testReadArrayWithClass_whenNonParcelableClass()3228     public void testReadArrayWithClass_whenNonParcelableClass(){
3229         Parcel p = Parcel.obtain();
3230         MockClassLoader mcl = new MockClassLoader();
3231 
3232         String[] sArray = {"1234", null, "4321"};
3233         p.writeArray(sArray);
3234 
3235         p.setDataPosition(0);
3236         Object[] objects = p.readArray(mcl, String.class);
3237         assertTrue(Arrays.equals(sArray, objects));
3238         p.setDataPosition(0);
3239         assertThrows(BadParcelableException.class, () -> p.readArray(
3240                 getClass().getClassLoader(), Intent.class));
3241         p.recycle();
3242     }
3243 
3244     @Test
testReadArrayWithClass_whenSameClass()3245     public void testReadArrayWithClass_whenSameClass(){
3246         Parcel p = Parcel.obtain();
3247         MockClassLoader mcl = new MockClassLoader();
3248 
3249         Signature[] s = {new Signature("1234"),
3250                 null,
3251                 new Signature("abcd")};
3252         p.writeArray(s);
3253 
3254         p.setDataPosition(0);
3255         Object[] s1 = p.readArray(mcl, Signature.class);
3256         assertTrue(Arrays.equals(s, s1));
3257 
3258         p.setDataPosition(0);
3259         assertThrows(BadParcelableException.class, () -> p.readArray(
3260                 getClass().getClassLoader(), Intent.class));
3261         p.recycle();
3262     }
3263 
3264     @Test
testReadArrayWithClass_whenSubclasses()3265     public void testReadArrayWithClass_whenSubclasses(){
3266         final Parcel p = Parcel.obtain();
3267         final Intent baseIntent = new Intent();
3268         Intent[] intentArray = {
3269             new TestSubIntent(baseIntent, "1234567890abcdef"),
3270             null,
3271             new TestSubIntent(baseIntent, "abcdef1234567890")
3272         };
3273         p.writeArray(intentArray);
3274 
3275         p.setDataPosition(0);
3276         Object[] objects = p.readArray(getClass().getClassLoader(), Intent.class);
3277         assertTrue(Arrays.equals(intentArray, objects));
3278 
3279         p.setDataPosition(0);
3280         assertThrows(BadParcelableException.class, () -> p.readArray(
3281                 getClass().getClassLoader(), Signature.class));
3282         p.recycle();
3283     }
3284 
3285     @Test
testReadArrayList()3286     public void testReadArrayList() {
3287         Parcel p;
3288         MockClassLoader mcl = new MockClassLoader();
3289 
3290         p = Parcel.obtain();
3291         p.writeArray(null);
3292         p.setDataPosition(0);
3293         assertNull(p.readArrayList(mcl));
3294         p.recycle();
3295 
3296         Object[] objects = new Object[5];
3297         objects[0] = Integer.MAX_VALUE;
3298         objects[1] = true;
3299         objects[2] = Long.MAX_VALUE;
3300         objects[3] = "String";
3301         objects[4] = Float.MAX_VALUE;
3302         ArrayList<?> objects2;
3303 
3304         p = Parcel.obtain();
3305         p.writeArray(objects);
3306         p.setDataPosition(0);
3307         objects2 = p.readArrayList(mcl);
3308         assertNotNull(objects2);
3309         for (int i = 0; i < objects2.size(); i++) {
3310             assertEquals(objects[i], objects2.get(i));
3311         }
3312         p.recycle();
3313     }
3314 
3315     @Test
testReadArrayListWithClass_whenNull()3316     public void testReadArrayListWithClass_whenNull(){
3317         Parcel p = Parcel.obtain();
3318         MockClassLoader mcl = new MockClassLoader();
3319 
3320         p.writeArray(null);
3321         p.setDataPosition(0);
3322         assertNull(p.readArrayList(mcl, Intent.class));
3323         p.recycle();
3324     }
3325 
3326     @Test
testReadArrayListWithClass_whenNonParcelableClass()3327     public void testReadArrayListWithClass_whenNonParcelableClass(){
3328         Parcel p = Parcel.obtain();
3329         MockClassLoader mcl = new MockClassLoader();
3330 
3331         ArrayList<String> sArrayList = new ArrayList<>();
3332         sArrayList.add("1234");
3333         sArrayList.add(null);
3334         sArrayList.add("4321");
3335 
3336         p.writeList(sArrayList);
3337         p.setDataPosition(0);
3338         ArrayList<String> s1 = p.readArrayList(mcl, String.class);
3339         assertEquals(sArrayList, s1);
3340         p.setDataPosition(0);
3341         assertThrows(BadParcelableException.class, () -> p.readArray(mcl, Intent.class));
3342         p.recycle();
3343     }
3344 
3345     @Test
testReadArrayListWithClass_whenSameClass()3346     public void testReadArrayListWithClass_whenSameClass(){
3347         final Parcel p = Parcel.obtain();
3348         MockClassLoader mcl = new MockClassLoader();
3349 
3350         ArrayList<Signature> s = new ArrayList<>();
3351         s.add(new Signature("1234567890abcdef"));
3352         s.add(null);
3353         s.add(new Signature("abcdef1234567890"));
3354 
3355         p.writeList(s);
3356         p.setDataPosition(0);
3357         ArrayList<Signature> s1 = p.readArrayList(mcl, Signature.class);
3358         assertEquals(s, s1);
3359 
3360         p.setDataPosition(0);
3361         assertThrows(BadParcelableException.class, () -> p.readArray(mcl, Intent.class));
3362         p.recycle();
3363     }
3364 
3365     @Test
testReadArrayListWithClass_whenSubclasses()3366     public void testReadArrayListWithClass_whenSubclasses(){
3367         final Parcel p = Parcel.obtain();
3368         final Intent baseIntent = new Intent();
3369 
3370         ArrayList<Intent> intentArrayList = new ArrayList<>();
3371         intentArrayList.add(new TestSubIntent(baseIntent, "1234567890abcdef"));
3372         intentArrayList.add(null);
3373         intentArrayList.add(new TestSubIntent(baseIntent, "abcdef1234567890"));
3374 
3375         p.writeList(intentArrayList);
3376         p.setDataPosition(0);
3377         ArrayList<Intent> objects = p.readArrayList(getClass().getClassLoader(), Intent.class);
3378         assertEquals(intentArrayList, objects);
3379 
3380         p.setDataPosition(0);
3381         ArrayList<Intent> objects1 = p.readArrayList(
3382                 getClass().getClassLoader(), TestSubIntent.class);
3383         assertEquals(intentArrayList, objects1);
3384 
3385         p.setDataPosition(0);
3386         assertThrows(BadParcelableException.class, () -> p.readArray(
3387                 getClass().getClassLoader(), Signature.class));
3388         p.recycle();
3389     }
3390 
3391     @Test
3392     @SuppressWarnings("unchecked")
testWriteSparseArray()3393     public void testWriteSparseArray() {
3394         Parcel p;
3395         MockClassLoader mcl = new MockClassLoader();
3396 
3397         p = Parcel.obtain();
3398         p.writeSparseArray(null);
3399         p.setDataPosition(0);
3400         assertNull(p.readSparseArray(mcl));
3401         p.recycle();
3402 
3403         SparseArray<Object> sparseArray = new SparseArray<Object>();
3404         sparseArray.put(3, "String");
3405         sparseArray.put(2, Long.MAX_VALUE);
3406         sparseArray.put(4, Float.MAX_VALUE);
3407         sparseArray.put(0, Integer.MAX_VALUE);
3408         sparseArray.put(1, true);
3409         sparseArray.put(10, true);
3410         SparseArray<Object> sparseArray2;
3411 
3412         p = Parcel.obtain();
3413         p.writeSparseArray(sparseArray);
3414         p.setDataPosition(0);
3415         sparseArray2 = p.readSparseArray(mcl);
3416         assertNotNull(sparseArray2);
3417         assertEquals(sparseArray.size(), sparseArray2.size());
3418         assertEquals(sparseArray.get(0), sparseArray2.get(0));
3419         assertEquals(sparseArray.get(1), sparseArray2.get(1));
3420         assertEquals(sparseArray.get(2), sparseArray2.get(2));
3421         assertEquals(sparseArray.get(3), sparseArray2.get(3));
3422         assertEquals(sparseArray.get(4), sparseArray2.get(4));
3423         assertEquals(sparseArray.get(10), sparseArray2.get(10));
3424         p.recycle();
3425     }
3426 
3427     @Test
testReadSparseArrayWithClass_whenNull()3428     public void testReadSparseArrayWithClass_whenNull(){
3429         Parcel p = Parcel.obtain();
3430         MockClassLoader mcl = new MockClassLoader();
3431 
3432         p.writeSparseArray(null);
3433         p.setDataPosition(0);
3434         assertNull(p.readSparseArray(mcl, Intent.class));
3435         p.recycle();
3436     }
3437 
3438     @Test
testReadSparseArrayWithClass_whenNonParcelableClass()3439     public void testReadSparseArrayWithClass_whenNonParcelableClass(){
3440         Parcel p = Parcel.obtain();
3441         MockClassLoader mcl = new MockClassLoader();
3442 
3443         SparseArray<String> s = new SparseArray<>();
3444         s.put(0, "1234567890abcdef");
3445         s.put(2, null);
3446         s.put(3, "abcdef1234567890");
3447         p.writeSparseArray(s);
3448 
3449         p.setDataPosition(0);
3450         SparseArray<String> s1 = p.readSparseArray(mcl, String.class);
3451         assertNotNull(s1);
3452         assertTrue(s.contentEquals(s1));
3453 
3454         p.setDataPosition(0);
3455         assertThrows(BadParcelableException.class, () -> p.readSparseArray(
3456                 getClass().getClassLoader(), Intent.class));
3457         p.recycle();
3458     }
3459 
3460     @Test
testReadSparseArrayWithClass_whenSameClass()3461     public void testReadSparseArrayWithClass_whenSameClass(){
3462         Parcel p = Parcel.obtain();
3463         MockClassLoader mcl = new MockClassLoader();
3464 
3465         SparseArray<Signature> s = new SparseArray<>();
3466         s.put(0, new Signature("1234567890abcdef"));
3467         s.put(2, null);
3468         s.put(3, new Signature("abcdef1234567890"));
3469         p.writeSparseArray(s);
3470 
3471         p.setDataPosition(0);
3472         SparseArray<Signature> s1 = p.readSparseArray(mcl, Signature.class);
3473         assertNotNull(s1);
3474         assertTrue(s.contentEquals(s1));
3475 
3476         p.setDataPosition(0);
3477         assertThrows(BadParcelableException.class, () -> p.readSparseArray(
3478                 getClass().getClassLoader(), Intent.class));
3479         p.recycle();
3480     }
3481 
3482     @Test
testReadSparseArrayWithClass_whenSubclasses()3483     public void testReadSparseArrayWithClass_whenSubclasses(){
3484         final Parcel p = Parcel.obtain();
3485         final Intent baseIntent = new Intent();
3486         SparseArray<Intent> intentArray = new SparseArray<>();
3487         intentArray.put(0, new TestSubIntent(baseIntent, "1234567890abcdef"));
3488         intentArray.put(3, new TestSubIntent(baseIntent, "1234567890abcdef"));
3489         p.writeSparseArray(intentArray);
3490 
3491         p.setDataPosition(0);
3492         SparseArray<Intent> sparseArray = p.readSparseArray(
3493                 getClass().getClassLoader(), Intent.class);
3494         assertNotNull(sparseArray);
3495         assertTrue(intentArray.contentEquals(sparseArray));
3496 
3497         p.setDataPosition(0);
3498         SparseArray<Intent> sparseArray1 = p.readSparseArray(
3499                 getClass().getClassLoader(), TestSubIntent.class);
3500         assertNotNull(sparseArray1);
3501         assertTrue(intentArray.contentEquals(sparseArray1));
3502 
3503         p.setDataPosition(0);
3504         assertThrows(BadParcelableException.class, () -> p.readSparseArray(
3505                 getClass().getClassLoader(), Signature.class));
3506         p.recycle();
3507     }
3508 
3509     @Test
testWriteSparseBooleanArray()3510     public void testWriteSparseBooleanArray() {
3511         Parcel p;
3512 
3513         p = Parcel.obtain();
3514         p.writeSparseArray(null);
3515         p.setDataPosition(0);
3516         assertNull(p.readSparseBooleanArray());
3517         p.recycle();
3518 
3519         SparseBooleanArray sparseBooleanArray = new SparseBooleanArray();
3520         sparseBooleanArray.put(3, true);
3521         sparseBooleanArray.put(2, false);
3522         sparseBooleanArray.put(4, false);
3523         sparseBooleanArray.put(0, true);
3524         sparseBooleanArray.put(1, true);
3525         sparseBooleanArray.put(10, true);
3526         SparseBooleanArray sparseBoolanArray2;
3527 
3528         p = Parcel.obtain();
3529         p.writeSparseBooleanArray(sparseBooleanArray);
3530         p.setDataPosition(0);
3531         sparseBoolanArray2 = p.readSparseBooleanArray();
3532         assertNotNull(sparseBoolanArray2);
3533         assertEquals(sparseBooleanArray.size(), sparseBoolanArray2.size());
3534         assertEquals(sparseBooleanArray.get(0), sparseBoolanArray2.get(0));
3535         assertEquals(sparseBooleanArray.get(1), sparseBoolanArray2.get(1));
3536         assertEquals(sparseBooleanArray.get(2), sparseBoolanArray2.get(2));
3537         assertEquals(sparseBooleanArray.get(3), sparseBoolanArray2.get(3));
3538         assertEquals(sparseBooleanArray.get(4), sparseBoolanArray2.get(4));
3539         assertEquals(sparseBooleanArray.get(10), sparseBoolanArray2.get(10));
3540         p.recycle();
3541     }
3542 
3543     @Test
testWriteStrongBinder()3544     public void testWriteStrongBinder() {
3545         Parcel p;
3546         Binder binder;
3547         Binder binder2 = new Binder();
3548 
3549         p = Parcel.obtain();
3550         p.writeStrongBinder(null);
3551         p.setDataPosition(0);
3552         assertNull(p.readStrongBinder());
3553         p.recycle();
3554 
3555         p = Parcel.obtain();
3556         p.writeStrongBinder(binder2);
3557         p.setDataPosition(0);
3558         binder = (Binder) p.readStrongBinder();
3559         assertEquals(binder2, binder);
3560         p.recycle();
3561     }
3562 
3563     @Test
testWriteStrongInterface()3564     public void testWriteStrongInterface() {
3565         Parcel p;
3566         MockIInterface mockInterface = new MockIInterface();
3567         MockIInterface mockIInterface2 = new MockIInterface();
3568 
3569         p = Parcel.obtain();
3570         p.writeStrongInterface(null);
3571         p.setDataPosition(0);
3572         assertNull(p.readStrongBinder());
3573         p.recycle();
3574 
3575         p = Parcel.obtain();
3576         p.writeStrongInterface(mockInterface);
3577         p.setDataPosition(0);
3578         mockIInterface2.binder = (Binder) p.readStrongBinder();
3579         assertEquals(mockInterface.binder, mockIInterface2.binder);
3580         p.recycle();
3581     }
3582 
3583     @Test
testWriteBinderArray()3584     public void testWriteBinderArray() {
3585         Parcel p;
3586         IBinder[] ibinder2 = {new Binder(), new Binder()};
3587         IBinder[] ibinder3 = new IBinder[2];
3588         IBinder[] ibinder4 = new IBinder[3];
3589 
3590         p = Parcel.obtain();
3591         p.writeBinderArray(null);
3592         p.setDataPosition(0);
3593         try {
3594             p.readBinderArray(null);
3595             fail("Should throw a RuntimeException");
3596         } catch (RuntimeException e) {
3597             //expected
3598         }
3599 
3600         p.setDataPosition(0);
3601         try {
3602             p.readBinderArray(ibinder3);
3603             fail("Should throw a RuntimeException");
3604         } catch (RuntimeException e) {
3605             //expected
3606         }
3607 
3608         p.setDataPosition(0);
3609         try {
3610             p.readBinderArray(ibinder2);
3611             fail("Should throw a RuntimeException");
3612         } catch (RuntimeException e) {
3613             //expected
3614         }
3615         p.recycle();
3616 
3617         p = Parcel.obtain();
3618         p.writeBinderArray(ibinder2);
3619         p.setDataPosition(0);
3620         try {
3621             p.readBinderArray(null);
3622             fail("Should throw a RuntimeException");
3623         } catch (RuntimeException e) {
3624             //expected
3625         }
3626 
3627         p.setDataPosition(0);
3628         try {
3629             p.readBinderArray(ibinder4);
3630             fail("Should throw a RuntimeException");
3631         } catch (RuntimeException e) {
3632             //expected
3633         }
3634 
3635         p.setDataPosition(0);
3636         p.readBinderArray(ibinder3);
3637         assertNotNull(ibinder3);
3638         for (int i = 0; i < ibinder3.length; i++) {
3639             assertNotNull(ibinder3[i]);
3640             assertEquals(ibinder2[i], ibinder3[i]);
3641         }
3642         p.recycle();
3643     }
3644 
3645     @Test
testCreateBinderArray()3646     public void testCreateBinderArray() {
3647         Parcel p;
3648         IBinder[] ibinder  = {};
3649         IBinder[] ibinder2 = {new Binder(), new Binder()};
3650         IBinder[] ibinder3;
3651         IBinder[] ibinder4;
3652 
3653         p = Parcel.obtain();
3654         p.writeBinderArray(null);
3655         p.setDataPosition(0);
3656         ibinder3 = p.createBinderArray();
3657         assertNull(ibinder3);
3658         p.recycle();
3659 
3660         p = Parcel.obtain();
3661         p.writeBinderArray(ibinder);
3662         p.setDataPosition(0);
3663         ibinder4 = p.createBinderArray();
3664         assertNotNull(ibinder4);
3665         assertEquals(0, ibinder4.length);
3666         p.recycle();
3667 
3668         p = Parcel.obtain();
3669         p.writeBinderArray(ibinder2);
3670         p.setDataPosition(0);
3671         ibinder3 = p.createBinderArray();
3672         assertNotNull(ibinder3);
3673         for (int i = 0; i < ibinder3.length; i++) {
3674             assertNotNull(ibinder3[i]);
3675             assertEquals(ibinder2[i], ibinder3[i]);
3676         }
3677         p.recycle();
3678     }
3679 
3680     @Test
testWriteBinderList()3681     public void testWriteBinderList() {
3682         Parcel p;
3683         ArrayList<IBinder> arrayList = new ArrayList<IBinder>();
3684         ArrayList<IBinder> arrayList2 = new ArrayList<IBinder>();
3685         arrayList2.add(new Binder());
3686         arrayList2.add(new Binder());
3687         ArrayList<IBinder> arrayList3 = new ArrayList<IBinder>();
3688         arrayList3.add(new Binder());
3689         arrayList3.add(new Binder());
3690         arrayList3.add(new Binder());
3691 
3692         p = Parcel.obtain();
3693         p.writeBinderList(null);
3694         p.setDataPosition(0);
3695         try {
3696             p.readBinderList(null);
3697             fail("Should throw a RuntimeException");
3698         } catch (RuntimeException e) {
3699             //expected
3700         }
3701         p.setDataPosition(0);
3702         assertEquals(0, arrayList.size());
3703         p.readBinderList(arrayList);
3704         assertEquals(0, arrayList.size());
3705         p.recycle();
3706 
3707         p = Parcel.obtain();
3708         p.writeBinderList(arrayList2);
3709         p.setDataPosition(0);
3710         assertEquals(0, arrayList.size());
3711         p.readBinderList(arrayList);
3712         assertEquals(2, arrayList.size());
3713         assertEquals(arrayList2, arrayList);
3714         p.recycle();
3715 
3716         p = Parcel.obtain();
3717         p.writeBinderList(arrayList2);
3718         p.setDataPosition(0);
3719         assertEquals(3, arrayList3.size());
3720         p.readBinderList(arrayList3);
3721         assertEquals(2, arrayList3.size());
3722         assertEquals(arrayList2, arrayList3);
3723         p.recycle();
3724     }
3725 
3726     @Test
testCreateBinderArrayList()3727     public void testCreateBinderArrayList() {
3728         Parcel p;
3729         ArrayList<IBinder> arrayList = new ArrayList<IBinder>();
3730         ArrayList<IBinder> arrayList2 = new ArrayList<IBinder>();
3731         arrayList2.add(new Binder());
3732         arrayList2.add(new Binder());
3733         ArrayList<IBinder> arrayList3;
3734         ArrayList<IBinder> arrayList4;
3735 
3736         p = Parcel.obtain();
3737         p.writeBinderList(null);
3738         p.setDataPosition(0);
3739         arrayList3 = p.createBinderArrayList();
3740         assertNull(arrayList3);
3741         p.recycle();
3742 
3743         p = Parcel.obtain();
3744         p.writeBinderList(arrayList);
3745         p.setDataPosition(0);
3746         arrayList3 = p.createBinderArrayList();
3747         assertNotNull(arrayList3);
3748         assertEquals(0, arrayList3.size());
3749         p.recycle();
3750 
3751         p = Parcel.obtain();
3752         p.writeBinderList(arrayList2);
3753         p.setDataPosition(0);
3754         arrayList4 = p.createBinderArrayList();
3755         assertNotNull(arrayList4);
3756         assertEquals(arrayList2, arrayList4);
3757         p.recycle();
3758     }
3759 
3760     @Test
testInterfaceArray()3761     public void testInterfaceArray() {
3762         Parcel p;
3763         MockIInterface[] iface2 = {new MockIInterface(), new MockIInterface(), null};
3764         MockIInterface[] iface3 = new MockIInterface[iface2.length];
3765         MockIInterface[] iface4 = new MockIInterface[iface2.length + 1];
3766 
3767         p = Parcel.obtain();
3768         p.writeInterfaceArray(null);
3769         p.setDataPosition(0);
3770         try {
3771             // input array shouldn't be null
3772             p.readInterfaceArray(null, null);
3773             fail("Should throw a RuntimeException");
3774         } catch (RuntimeException e) {
3775             // expected
3776         }
3777 
3778         p.setDataPosition(0);
3779         try {
3780             // can't read null array
3781             p.readInterfaceArray(iface3, MockIInterface::asInterface);
3782             fail("Should throw a RuntimeException");
3783         } catch (RuntimeException e) {
3784             // expected
3785         }
3786 
3787         p.setDataPosition(0);
3788         // null if parcel has null array
3789         assertNull(p.createInterfaceArray(MockIInterface[]::new, MockIInterface::asInterface));
3790         p.recycle();
3791 
3792         p = Parcel.obtain();
3793         p.writeInterfaceArray(iface2);
3794         p.setDataPosition(0);
3795         try {
3796             // input array shouldn't be null
3797             p.readInterfaceArray(null, null);
3798             fail("Should throw a RuntimeException");
3799         } catch (RuntimeException e) {
3800             // expected
3801         }
3802 
3803         p.setDataPosition(0);
3804         try {
3805             // input array should be the same size
3806             p.readInterfaceArray(iface4, MockIInterface::asInterface);
3807             fail("Should throw a RuntimeException");
3808         } catch (RuntimeException e) {
3809             // expected
3810         }
3811 
3812         p.setDataPosition(0);
3813         try {
3814             // asInterface shouldn't be null
3815             p.readInterfaceArray(iface3, null);
3816             fail("Should throw a RuntimeException");
3817         } catch (RuntimeException e) {
3818             // expected
3819         }
3820 
3821         p.setDataPosition(0);
3822         // read into input array with the exact size
3823         p.readInterfaceArray(iface3, MockIInterface::asInterface);
3824         for (int i = 0; i < iface3.length; i++) {
3825             assertEquals(iface2[i], iface3[i]);
3826         }
3827 
3828         p.setDataPosition(0);
3829         try {
3830             // newArray/asInterface shouldn't be null
3831             p.createInterfaceArray(null, null);
3832             fail("Should throw a RuntimeException");
3833         } catch (RuntimeException e) {
3834             // expected
3835         }
3836 
3837         p.setDataPosition(0);
3838         // create a new array from parcel
3839         MockIInterface[] iface5 =
3840             p.createInterfaceArray(MockIInterface[]::new, MockIInterface::asInterface);
3841         assertNotNull(iface5);
3842         assertEquals(iface2.length, iface5.length);
3843         for (int i = 0; i < iface5.length; i++) {
3844             assertEquals(iface2[i], iface5[i]);
3845         }
3846         p.recycle();
3847     }
3848 
3849     @Test
testInterfaceList()3850     public void testInterfaceList() {
3851         Parcel p;
3852         ArrayList<MockIInterface> arrayList = new ArrayList<>();
3853         ArrayList<MockIInterface> arrayList2 = new ArrayList<>();
3854         ArrayList<MockIInterface> arrayList3;
3855         arrayList.add(new MockIInterface());
3856         arrayList.add(new MockIInterface());
3857         arrayList.add(null);
3858 
3859         p = Parcel.obtain();
3860         p.writeInterfaceList(null);
3861         p.setDataPosition(0);
3862         try {
3863             // input list shouldn't be null
3864             p.readInterfaceList(null, null);
3865             fail("Should throw a RuntimeException");
3866         } catch (RuntimeException e) {
3867             // expected
3868         }
3869 
3870         p.setDataPosition(0);
3871         arrayList2.clear();
3872         arrayList2.add(null);
3873         try {
3874             // can't read null list into non-empty list
3875             p.readInterfaceList(arrayList2, MockIInterface::asInterface);
3876             fail("Should throw a RuntimeException");
3877         } catch (RuntimeException e) {
3878             // expected
3879         }
3880 
3881         p.setDataPosition(0);
3882         arrayList2.clear();
3883         // read null list into empty list
3884         p.readInterfaceList(arrayList2, MockIInterface::asInterface);
3885         assertEquals(0, arrayList2.size());
3886 
3887         p.setDataPosition(0);
3888         // null if parcel has null list
3889         arrayList3 = p.createInterfaceArrayList(MockIInterface::asInterface);
3890         assertNull(arrayList3);
3891         p.recycle();
3892 
3893         p = Parcel.obtain();
3894         p.writeInterfaceList(arrayList);
3895         p.setDataPosition(0);
3896         try {
3897             // input list shouldn't be null
3898             p.readInterfaceList(null, null);
3899             fail("Should throw a RuntimeException");
3900         } catch (RuntimeException e) {
3901             // expected
3902         }
3903 
3904         p.setDataPosition(0);
3905         arrayList2.clear();
3906         try {
3907             // asInterface shouldn't be null
3908             p.readInterfaceList(arrayList2, null);
3909             fail("Should throw a RuntimeException");
3910         } catch (RuntimeException e) {
3911             //expected
3912         }
3913 
3914         p.setDataPosition(0);
3915         arrayList2.clear();
3916         // fill a list with parcel
3917         p.readInterfaceList(arrayList2, MockIInterface::asInterface);
3918         assertEquals(arrayList, arrayList2);
3919 
3920         p.setDataPosition(0);
3921         arrayList2.clear();
3922         // add one more item
3923         for (int i=0; i<arrayList.size() + 1; i++) {
3924             arrayList2.add(null);
3925         }
3926         // extra item should be discarded after read
3927         p.readInterfaceList(arrayList2, MockIInterface::asInterface);
3928         assertEquals(arrayList, arrayList2);
3929 
3930         p.setDataPosition(0);
3931         // create a new ArrayList from parcel
3932         arrayList3 = p.createInterfaceArrayList(MockIInterface::asInterface);
3933         assertEquals(arrayList, arrayList3);
3934         p.recycle();
3935     }
3936 
3937     @Test
testFixedArray()3938     public void testFixedArray() {
3939         Parcel p = Parcel.obtain();
3940 
3941         //  test int[2][3]
3942         int[][] ints = new int[][] {{1,2,3}, {4,5,6}};
3943         p.writeFixedArray(ints, 0, new int[]{2, 3});
3944         p.setDataPosition(0);
3945         assertArrayEquals(ints, p.createFixedArray(int[][].class, new int[]{2, 3}));
3946         int[][] readInts = new int[2][3];
3947         p.setDataPosition(0);
3948         p.readFixedArray(readInts);
3949         assertArrayEquals(ints, readInts);
3950 
3951         // test Parcelable[2][3]
3952         p.setDataPosition(0);
3953         Signature[][] signatures = {
3954             {new Signature("1234"), new Signature("ABCD"), new Signature("abcd")},
3955             {new Signature("5678"), new Signature("EFAB"), new Signature("efab")}};
3956         p.writeFixedArray(signatures, 0, new int[]{2, 3});
3957         p.setDataPosition(0);
3958         assertArrayEquals(signatures, p.createFixedArray(Signature[][].class, Signature.CREATOR, new int[]{2, 3}));
3959         Signature[][] readSignatures = new Signature[2][3];
3960         p.setDataPosition(0);
3961         p.readFixedArray(readSignatures, Signature.CREATOR);
3962         assertArrayEquals(signatures, readSignatures);
3963 
3964         // test null
3965         p.setDataPosition(0);
3966         int[][] nullInts = null;
3967         p.writeFixedArray(nullInts, 0, new int[]{2, 3});
3968         p.setDataPosition(0);
3969         assertNull(p.createFixedArray(int[][].class, new int[]{2, 3}));
3970 
3971         // reject wrong dimensions when writing
3972         p.setDataPosition(0);
3973         assertThrows(BadParcelableException.class,
3974             () -> p.writeFixedArray(new int[3][2], 0, new int[]{2, 2}));
3975         assertThrows(BadParcelableException.class,
3976             () -> p.writeFixedArray(new int[3], 0, new int[]{3, 2}));
3977         assertThrows(BadParcelableException.class,
3978             () -> p.writeFixedArray(new int[3][2], 0, new int[]{3}));
3979 
3980         // reject wrong dimensions when reading
3981         p.setDataPosition(0);
3982         p.writeFixedArray(new int[2][3], 0, new int[]{2, 3});
3983         p.setDataPosition(0);
3984         assertThrows(BadParcelableException.class, () -> p.createFixedArray(int[][].class, 1, 3));
3985         assertThrows(BadParcelableException.class, () -> p.createFixedArray(int[][].class, 2, 2));
3986 
3987         // test IInterface[2][3]
3988         p.setDataPosition(0);
3989         MockIInterface[][] interfaces = {
3990             {new MockIInterface(), new MockIInterface(), new MockIInterface()},
3991             {new MockIInterface(), new MockIInterface(), new MockIInterface()}};
3992         p.writeFixedArray(interfaces, 0, new int[]{2, 3});
3993         p.setDataPosition(0);
3994         MockIInterface[][] interfacesRead = p.createFixedArray(MockIInterface[][].class,
3995                 MockIInterface::asInterface, new int[]{2, 3});
3996         assertEquals(2, interfacesRead.length);
3997         assertEquals(3, interfacesRead[0].length);
3998         MockIInterface[][] mockInterfaces = new MockIInterface[2][3];
3999         p.setDataPosition(0);
4000         p.readFixedArray(mockInterfaces, MockIInterface::asInterface);
4001         assertArrayEquals(interfaces, mockInterfaces);
4002 
4003 
4004         p.recycle();
4005     }
4006 
4007     @Test
4008     @SuppressWarnings("unchecked")
testWriteMap()4009     public void testWriteMap() {
4010         Parcel p;
4011         MockClassLoader mcl = new MockClassLoader();
4012         HashMap map = new HashMap();
4013         HashMap map2 = new HashMap();
4014 
4015         p = Parcel.obtain();
4016         p.writeMap(null);
4017         p.setDataPosition(0);
4018         assertEquals(0, map2.size());
4019         p.readMap(map2, mcl);
4020         assertEquals(0, map2.size());
4021         p.recycle();
4022 
4023         map.put("string", "String");
4024         map.put("int", Integer.MAX_VALUE);
4025         map.put("boolean", true);
4026         p = Parcel.obtain();
4027         p.writeMap(map);
4028         p.setDataPosition(0);
4029         assertEquals(0, map2.size());
4030         p.readMap(map2, mcl);
4031         assertEquals(3, map2.size());
4032         assertEquals("String", map.get("string"));
4033         assertEquals(Integer.MAX_VALUE, map.get("int"));
4034         assertEquals(true, map.get("boolean"));
4035         p.recycle();
4036     }
4037 
4038     @Test
testReadMapWithClass_whenNull()4039     public void testReadMapWithClass_whenNull() {
4040         Parcel p = Parcel.obtain();
4041         MockClassLoader mcl = new MockClassLoader();
4042         p.writeMap(null);
4043         HashMap<String, Intent> map = new HashMap<>();
4044 
4045         p.setDataPosition(0);
4046         p.readMap(map, mcl, String.class, Intent.class);
4047         assertEquals(0, map.size());
4048 
4049         p.recycle();
4050     }
4051 
4052     @Test
testReadMapWithClass_whenMismatchingClass()4053     public void testReadMapWithClass_whenMismatchingClass() {
4054         Parcel p = Parcel.obtain();
4055         ClassLoader loader = getClass().getClassLoader();
4056         HashMap<Signature, TestSubIntent> map = new HashMap<>();
4057 
4058         Intent baseIntent = new Intent();
4059         map.put(new Signature("1234"), new TestSubIntent(
4060                 baseIntent, "test_intent1"));
4061         map.put(new Signature("4321"), new TestSubIntent(
4062                 baseIntent, "test_intent2"));
4063         p.writeMap(map);
4064 
4065         p.setDataPosition(0);
4066         assertThrows(BadParcelableException.class, () ->
4067                 p.readMap(new HashMap<Intent, TestSubIntent>(), loader,
4068                         Intent.class, TestSubIntent.class));
4069 
4070         p.setDataPosition(0);
4071         assertThrows(BadParcelableException.class, () ->
4072                 p.readMap(new HashMap<Signature, Signature>(), loader,
4073                         Signature.class, Signature.class));
4074         p.recycle();
4075     }
4076 
4077     @Test
testReadMapWithClass_whenSameClass()4078     public void testReadMapWithClass_whenSameClass() {
4079         Parcel p = Parcel.obtain();
4080         ClassLoader loader = getClass().getClassLoader();
4081         HashMap<String, TestSubIntent> map = new HashMap<>();
4082         HashMap<String, TestSubIntent> map2 = new HashMap<>();
4083 
4084         Intent baseIntent = new Intent();
4085         map.put("key1", new TestSubIntent(
4086                 baseIntent, "test_intent1"));
4087         map.put("key2", new TestSubIntent(
4088                 baseIntent, "test_intent2"));
4089         p.writeMap(map);
4090         p.setDataPosition(0);
4091         p.readMap(map2, loader, String.class, TestSubIntent.class);
4092         assertEquals(map, map2);
4093 
4094         p.recycle();
4095     }
4096 
4097     @Test
testReadMapWithClass_whenSubClass()4098     public void testReadMapWithClass_whenSubClass() {
4099         Parcel p = Parcel.obtain();
4100         ClassLoader loader = getClass().getClassLoader();
4101         HashMap<TestSubIntent, TestSubIntent> map = new HashMap<>();
4102 
4103         Intent baseIntent = new Intent();
4104         map.put(new TestSubIntent(baseIntent, "test_intent_key1"), new TestSubIntent(
4105                 baseIntent, "test_intent_val1"));
4106         p.writeMap(map);
4107         p.setDataPosition(0);
4108         HashMap<Intent, Intent> map2 = new HashMap<>();
4109         p.readMap(map2, loader, Intent.class, TestSubIntent.class);
4110         assertEquals(map, map2);
4111 
4112         p.setDataPosition(0);
4113         HashMap<Intent, Intent> map3 = new HashMap<>();
4114         p.readMap(map3, loader, TestSubIntent.class, Intent.class);
4115         assertEquals(map, map3);
4116 
4117         p.recycle();
4118     }
4119 
4120     @Test
4121     @SuppressWarnings("unchecked")
testReadHashMap()4122     public void testReadHashMap() {
4123         Parcel p;
4124         MockClassLoader mcl = new MockClassLoader();
4125         HashMap map = new HashMap();
4126         HashMap map2;
4127 
4128         p = Parcel.obtain();
4129         p.writeMap(null);
4130         p.setDataPosition(0);
4131         map2 = p.readHashMap(mcl);
4132         assertNull(map2);
4133         p.recycle();
4134 
4135         map.put("string", "String");
4136         map.put("int", Integer.MAX_VALUE);
4137         map.put("boolean", true);
4138         map2 = null;
4139         p = Parcel.obtain();
4140         p.writeMap(map);
4141         p.setDataPosition(0);
4142         map2 = p.readHashMap(mcl);
4143         assertNotNull(map2);
4144         assertEquals(3, map2.size());
4145         assertEquals("String", map.get("string"));
4146         assertEquals(Integer.MAX_VALUE, map.get("int"));
4147         assertEquals(true, map.get("boolean"));
4148         p.recycle();
4149     }
4150 
4151     @Test
testReadHashMapWithClass_whenNull()4152     public void testReadHashMapWithClass_whenNull() {
4153         Parcel p = Parcel.obtain();
4154         MockClassLoader mcl = new MockClassLoader();
4155         p.writeMap(null);
4156         p.setDataPosition(0);
4157         assertNull(p.readHashMap(mcl, String.class, Intent.class));
4158 
4159         p.setDataPosition(0);
4160         assertNull(p.readHashMap(null, String.class, Intent.class));
4161         p.recycle();
4162     }
4163 
4164     @Test
testReadHashMapWithClass_whenMismatchingClass()4165     public void testReadHashMapWithClass_whenMismatchingClass() {
4166         Parcel p = Parcel.obtain();
4167         ClassLoader loader = getClass().getClassLoader();
4168         HashMap<Signature, TestSubIntent> map = new HashMap<>();
4169 
4170         Intent baseIntent = new Intent();
4171         map.put(new Signature("1234"), new TestSubIntent(
4172                 baseIntent, "test_intent1"));
4173         map.put(new Signature("4321"), new TestSubIntent(
4174                 baseIntent, "test_intent2"));
4175         p.writeMap(map);
4176 
4177         p.setDataPosition(0);
4178         assertThrows(BadParcelableException.class, () ->
4179                 p.readHashMap(loader, Intent.class, TestSubIntent.class));
4180 
4181         p.setDataPosition(0);
4182         assertThrows(BadParcelableException.class, () ->
4183                 p.readHashMap(loader, Signature.class, Signature.class));
4184         p.recycle();
4185     }
4186 
4187     @Test
testReadHashMapWithClass_whenSameClass()4188     public void testReadHashMapWithClass_whenSameClass() {
4189         Parcel p = Parcel.obtain();
4190         ClassLoader loader = getClass().getClassLoader();
4191         HashMap<String, TestSubIntent> map = new HashMap<>();
4192 
4193         Intent baseIntent = new Intent();
4194         map.put("key1", new TestSubIntent(
4195                 baseIntent, "test_intent1"));
4196         map.put("key2", new TestSubIntent(
4197                 baseIntent, "test_intent2"));
4198 
4199         p.writeMap(map);
4200         p.setDataPosition(0);
4201         HashMap<String, TestSubIntent> map2 = p.readHashMap(loader, String.class,
4202                 TestSubIntent.class);
4203         assertEquals(map, map2);
4204 
4205         p.setDataPosition(0);
4206         HashMap<Object, Intent> map3 = p.readHashMap(loader, String.class,
4207                 TestSubIntent.class);
4208         assertEquals(map, map3);
4209 
4210         p.recycle();
4211     }
4212 
4213     @Test
testReadHashMapWithClass_whenSubClass()4214     public void testReadHashMapWithClass_whenSubClass() {
4215         Parcel p = Parcel.obtain();
4216         ClassLoader loader = getClass().getClassLoader();
4217         HashMap<TestSubIntent, TestSubIntent> map = new HashMap<>();
4218 
4219         Intent baseIntent = new Intent();
4220         TestSubIntent test_intent_key1 = new TestSubIntent(baseIntent, "test_intent_key1");
4221         map.put(test_intent_key1, new TestSubIntent(
4222                 baseIntent, "test_intent_val1"));
4223         p.writeMap(map);
4224         p.setDataPosition(0);
4225         HashMap<Intent, Intent> map2 = p.readHashMap(loader, Intent.class, TestSubIntent.class);
4226         assertEquals(map, map2);
4227 
4228         p.setDataPosition(0);
4229         HashMap<Intent, Intent> map3 = p.readHashMap(loader, TestSubIntent.class, Intent.class);
4230         assertEquals(map, map3);
4231         p.recycle();
4232     }
4233 
4234     @Test
4235     @SuppressWarnings("unchecked")
testReadList()4236     public void testReadList() {
4237         Parcel p;
4238         MockClassLoader mcl = new MockClassLoader();
4239         ArrayList arrayList = new ArrayList();
4240 
4241         p = Parcel.obtain();
4242         p.writeList(null);
4243         p.setDataPosition(0);
4244         assertEquals(0, arrayList.size());
4245         p.readList(arrayList, mcl);
4246         assertEquals(0, arrayList.size());
4247         p.recycle();
4248 
4249         ArrayList arrayList2 = new ArrayList();
4250         arrayList2.add(Integer.MAX_VALUE);
4251         arrayList2.add(true);
4252         arrayList2.add(Long.MAX_VALUE);
4253         arrayList2.add("String");
4254         arrayList2.add(Float.MAX_VALUE);
4255 
4256         p = Parcel.obtain();
4257         p.writeList(arrayList2);
4258         p.setDataPosition(0);
4259         assertEquals(0, arrayList.size());
4260         p.readList(arrayList, mcl);
4261         assertEquals(5, arrayList.size());
4262         for (int i = 0; i < arrayList.size(); i++) {
4263             assertEquals(arrayList.get(i), arrayList2.get(i));
4264         }
4265 
4266         p.recycle();
4267     }
4268 
4269     @Test
4270     @SuppressWarnings("unchecked")
testReadListWithClass()4271     public void testReadListWithClass() {
4272         Parcel p;
4273         MockClassLoader mcl = new MockClassLoader();
4274         ArrayList<Signature> arrayList = new ArrayList();
4275         ArrayList<Signature> parcelableArrayList = new ArrayList();
4276         final String s1  = "1234567890abcdef";
4277         final String s2  = "abcdef1234567890";
4278         parcelableArrayList.add(new Signature(s1));
4279         parcelableArrayList.add(new Signature(s2));
4280 
4281         p = Parcel.obtain();
4282         p.writeList(parcelableArrayList);
4283         p.setDataPosition(0);
4284         assertEquals(0, arrayList.size());
4285         p.readList(arrayList, mcl, Signature.class);
4286         assertEquals(2, arrayList.size());
4287         for (int i = 0; i < arrayList.size(); i++) {
4288             assertEquals(arrayList.get(i), parcelableArrayList.get(i));
4289         }
4290 
4291         p.setDataPosition(0);
4292         assertThrows(BadParcelableException.class, () -> p.readList(new ArrayList(), mcl, Intent.class));
4293 
4294         p.setDataPosition(0);
4295         assertThrows(BadParcelableException.class, () -> p.readList(new ArrayList(), mcl, Integer.class));
4296         p.recycle();
4297 
4298         ArrayList<String> stringArrayList = new ArrayList();
4299         stringArrayList.add(s1);
4300         stringArrayList.add(s2);
4301         Parcel p1 = Parcel.obtain();
4302         p1.writeList(stringArrayList);
4303 
4304         p1.setDataPosition(0);
4305         assertThrows(BadParcelableException.class, () -> p1.readList(new ArrayList(), mcl, Integer.class));
4306         p1.recycle();
4307     }
4308 
4309     @Test
4310     @SuppressWarnings("unchecked")
testReadListWithSubClass()4311     public void testReadListWithSubClass() {
4312         Parcel p;
4313         ArrayList<Intent> arrayList = new ArrayList();
4314         ArrayList<Intent> arrayList2 = new ArrayList();
4315         ArrayList<Intent> parcelableArrayList = new ArrayList();
4316         final Intent baseIntent = new Intent();
4317         final TestSubIntent testSubIntent = new TestSubIntent(baseIntent, "1234567890abcdef");
4318         final TestSubIntent testSubIntent1 = new TestSubIntent(baseIntent, "abcdef1234567890");
4319         parcelableArrayList.add(testSubIntent);
4320         parcelableArrayList.add(testSubIntent1);
4321 
4322         p = Parcel.obtain();
4323         p.writeList(parcelableArrayList);
4324         p.setDataPosition(0);
4325         assertEquals(0, arrayList.size());
4326         p.readList(arrayList, getClass().getClassLoader(), Intent.class);
4327         assertEquals(2, arrayList.size());
4328         for (int i = 0; i < arrayList.size(); i++) {
4329             assertEquals(arrayList.get(i), parcelableArrayList.get(i));
4330         }
4331 
4332         p.setDataPosition(0);
4333         assertEquals(0, arrayList2.size());
4334         p.readList(arrayList2, getClass().getClassLoader(), TestSubIntent.class);
4335         assertEquals(2, arrayList2.size());
4336         for (int i = 0; i < arrayList2.size(); i++) {
4337             assertEquals(arrayList2.get(i), parcelableArrayList.get(i));
4338         }
4339 
4340         p.recycle();
4341     }
4342 
4343     @Test
testBinderDataProtection()4344     public void testBinderDataProtection() {
4345         Parcel p;
4346         IBinder b = new Binder();
4347 
4348         p = Parcel.obtain();
4349         final int firstIntPos = p.dataPosition();
4350         p.writeInt(1);
4351         p.writeStrongBinder(b);
4352         final int secondIntPos = p.dataPosition();
4353         p.writeInt(2);
4354         p.writeStrongBinder(b);
4355         final int thirdIntPos = p.dataPosition();
4356         p.writeInt(3);
4357 
4358         for (int pos = 0; pos <= thirdIntPos; pos++) {
4359             p.setDataPosition(pos);
4360             int value = p.readInt();
4361 
4362             // WARNING: this is using unstable APIs: these positions aren't guaranteed
4363             if (firstIntPos - 4 <= pos && pos <= firstIntPos) continue;
4364             if (secondIntPos - 4 <= pos && pos <= secondIntPos) continue;
4365             if (thirdIntPos - 4 <= pos && pos <= thirdIntPos) continue;
4366 
4367             // All other read attempts cross into protected data and will return 0
4368             assertEquals(0, value);
4369         }
4370 
4371         p.recycle();
4372     }
4373 
4374     @Test
testBinderDataProtectionIncrements()4375     public void testBinderDataProtectionIncrements() {
4376         Parcel p;
4377         IBinder b = new Binder();
4378 
4379         p = Parcel.obtain();
4380         final int firstIntPos = p.dataPosition();
4381         p.writeInt(1);
4382         p.writeStrongBinder(b);
4383         final int secondIntPos = p.dataPosition();
4384         p.writeInt(2);
4385         p.writeStrongBinder(b);
4386         final int thirdIntPos = p.dataPosition();
4387         p.writeInt(3);
4388         final int end = p.dataPosition();
4389 
4390         p.setDataPosition(0);
4391         int pos;
4392         do {
4393             pos = p.dataPosition();
4394             int value = p.readInt();
4395 
4396             // WARNING: this is using unstable APIs: these positions aren't guaranteed
4397             if (firstIntPos - 4 <= pos && pos <= firstIntPos) continue;
4398             if (secondIntPos - 4 <= pos && pos <= secondIntPos) continue;
4399             if (thirdIntPos - 4 <= pos && pos <= thirdIntPos) continue;
4400 
4401             assertEquals(0, value);
4402         } while(pos < end);
4403 
4404         p.recycle();
4405     }
4406 
4407     private class MockClassLoader extends ClassLoader {
MockClassLoader()4408         public MockClassLoader() {
4409             super(ParcelTest.class.getClassLoader());
4410         }
4411     }
4412 
4413     private static class MockIInterface implements IInterface {
4414         public Binder binder;
4415         private static final String DESCRIPTOR = "MockIInterface";
MockIInterface()4416         public MockIInterface() {
4417             binder = new Binder();
4418             binder.attachInterface(this, DESCRIPTOR);
4419         }
4420 
asBinder()4421         public IBinder asBinder() {
4422             return binder;
4423         }
4424 
asInterface(IBinder binder)4425         public static MockIInterface asInterface(IBinder binder) {
4426             if (binder != null) {
4427                 IInterface iface = binder.queryLocalInterface(DESCRIPTOR);
4428                 if (iface != null && iface instanceof MockIInterface) {
4429                     return (MockIInterface) iface;
4430                 }
4431             }
4432             return null;
4433         }
4434     }
4435 
4436     private static boolean parcelableWithBadCreatorInitializerHasRun;
4437     private static boolean invalidCreatorIntializerHasRun;
4438 
4439     /**
4440      * A class that would be Parcelable except that it doesn't have a CREATOR field declared to be
4441      * of the correct type.
4442      */
4443     @SuppressWarnings({"unused", "ParcelableCreator"}) // Referenced via reflection only
4444     private static class ParcelableWithBadCreator implements Parcelable {
4445 
4446         static {
4447             ParcelTest.parcelableWithBadCreatorInitializerHasRun = true;
4448         }
4449 
4450         private static class InvalidCreator
4451                 implements Parcelable.Creator<ParcelableWithBadCreator> {
4452 
4453             static {
4454                 invalidCreatorIntializerHasRun = true;
4455             }
4456 
4457             @Override
createFromParcel(Parcel source)4458             public ParcelableWithBadCreator createFromParcel(Parcel source) {
4459                 return null;
4460             }
4461 
4462             @Override
newArray(int size)4463             public ParcelableWithBadCreator[] newArray(int size) {
4464                 return new ParcelableWithBadCreator[0];
4465             }
4466 
4467         }
4468 
4469         // Invalid declaration: Must be declared as Parcelable.Creator or a subclass.
4470         public static Object CREATOR = new InvalidCreator();
4471 
4472         @Override
describeContents()4473         public int describeContents() {
4474             return 0;
4475         }
4476 
4477         @Override
writeToParcel(Parcel dest, int flags)4478         public void writeToParcel(Parcel dest, int flags) {
4479 
4480         }
4481     }
4482 
4483     // http://b/1171613
4484     @Test
testBadStream_invalidCreator()4485     public void testBadStream_invalidCreator() {
4486         Parcel parcel = Parcel.obtain();
4487         // Create an invalid stream by manipulating the Parcel.
4488         parcel.writeString(getClass().getName() + "$ParcelableWithBadCreator");
4489         byte[] badData = parcel.marshall();
4490         parcel.recycle();
4491 
4492         // Now try to read the bad data.
4493         parcel = Parcel.obtain();
4494         parcel.unmarshall(badData, 0, badData.length);
4495         parcel.setDataPosition(0);
4496         try {
4497             parcel.readParcelable(getClass().getClassLoader());
4498             fail();
4499         } catch (BadParcelableException expected) {
4500         } finally {
4501             parcel.recycle();
4502         }
4503 
4504         assertFalse(invalidCreatorIntializerHasRun);
4505         assertFalse(parcelableWithBadCreatorInitializerHasRun);
4506     }
4507 
4508     private static boolean doesNotImplementParcelableInitializerHasRun;
4509 
4510     /** A class that would be Parcelable except that it does not implement Parcelable. */
4511     @SuppressWarnings("unused") // Referenced via reflection only
4512     private static class DoesNotImplementParcelable {
4513 
4514         static {
4515             doesNotImplementParcelableInitializerHasRun = true;
4516         }
4517 
4518         public static Parcelable.Creator<Object> CREATOR = new Parcelable.Creator<Object>() {
4519             @Override
4520             public Object createFromParcel(Parcel source) {
4521                 return new DoesNotImplementParcelable();
4522             }
4523 
4524             @Override
4525             public Object[] newArray(int size) {
4526                 return new Object[size];
4527             }
4528         };
4529     }
4530 
4531     // http://b/1171613
4532     @Test
testBadStream_objectDoesNotImplementParcelable()4533     public void testBadStream_objectDoesNotImplementParcelable() {
4534         Parcel parcel = Parcel.obtain();
4535         // Create an invalid stream by manipulating the Parcel.
4536         parcel.writeString(getClass().getName() + "$DoesNotImplementParcelable");
4537         byte[] badData = parcel.marshall();
4538         parcel.recycle();
4539 
4540         // Now try to read the bad data.
4541         parcel = Parcel.obtain();
4542         parcel.unmarshall(badData, 0, badData.length);
4543         parcel.setDataPosition(0);
4544         try {
4545             parcel.readParcelable(getClass().getClassLoader());
4546             fail();
4547         } catch (BadParcelableException expected) {
4548         } finally {
4549             parcel.recycle();
4550         }
4551 
4552         assertFalse(doesNotImplementParcelableInitializerHasRun);
4553     }
4554 
4555     public static class SimpleParcelable implements Parcelable {
4556         private final int value;
4557 
SimpleParcelable(int value)4558         public SimpleParcelable(int value) {
4559             this.value = value;
4560         }
4561 
SimpleParcelable(Parcel in)4562         private SimpleParcelable(Parcel in) {
4563             this.value = in.readInt();
4564         }
4565 
getValue()4566         public int getValue() {
4567             return value;
4568         }
4569 
4570         @Override
describeContents()4571         public int describeContents() {
4572             return 0;
4573         }
4574 
4575         @Override
writeToParcel(Parcel out, int flags)4576         public void writeToParcel(Parcel out, int flags) {
4577             out.writeInt(value);
4578         }
4579 
4580         public static Parcelable.Creator<SimpleParcelable> CREATOR =
4581                 new Parcelable.Creator<SimpleParcelable>() {
4582 
4583             @Override
4584             public SimpleParcelable createFromParcel(Parcel source) {
4585                 return new SimpleParcelable(source);
4586             }
4587 
4588             @Override
4589             public SimpleParcelable[] newArray(int size) {
4590                 return new SimpleParcelable[size];
4591             }
4592         };
4593     }
4594 
4595     @Test
testReadWriteParcellableList()4596     public void testReadWriteParcellableList() {
4597         Parcel parcel = Parcel.obtain();
4598 
4599         ArrayList<SimpleParcelable> list = new ArrayList<>();
4600         list.add(new SimpleParcelable(57));
4601 
4602         // Writing a |null| list to a parcel should work, and reading it back
4603         // from a parcel should clear the target list.
4604         parcel.writeParcelableList(null, 0);
4605         parcel.setDataPosition(0);
4606         parcel.readParcelableList(list, SimpleParcelable.class.getClassLoader());
4607         assertEquals(0, list.size());
4608 
4609         list.clear();
4610         list.add(new SimpleParcelable(42));
4611         list.add(new SimpleParcelable(56));
4612 
4613         parcel.setDataPosition(0);
4614         parcel.writeParcelableList(list, 0);
4615 
4616         // Populate the list with a value, we will later assert that the
4617         // value has been removed.
4618         list.clear();
4619         list.add(new SimpleParcelable(100));
4620 
4621         parcel.setDataPosition(0);
4622         parcel.readParcelableList(list, SimpleParcelable.class.getClassLoader());
4623 
4624         assertEquals(2, list.size());
4625         assertEquals(42, list.get(0).getValue());
4626         assertEquals(56, list.get(1).getValue());
4627     }
4628 
4629     @Test
testReadParcelableListWithClass_whenNull()4630     public void testReadParcelableListWithClass_whenNull(){
4631         final Parcel p = Parcel.obtain();
4632         ArrayList<Intent> list = new ArrayList<>();
4633         list.add(new Intent());
4634 
4635         p.writeParcelableList(null, 0);
4636         p.setDataPosition(0);
4637         p.readParcelableList(list, getClass().getClassLoader(), Intent.class);
4638         assertEquals(0, list.size());
4639         p.recycle();
4640     }
4641 
4642     @Test
testReadParcelableListWithClass_whenMismatchingClass()4643     public void testReadParcelableListWithClass_whenMismatchingClass(){
4644         final Parcel p = Parcel.obtain();
4645         ArrayList<Signature> list = new ArrayList<>();
4646         ArrayList<Intent> list1 = new ArrayList<>();
4647         list.add(new Signature("1234"));
4648         p.writeParcelableList(list, 0);
4649         p.setDataPosition(0);
4650         assertThrows(BadParcelableException.class, () ->
4651                 p.readParcelableList(list1, getClass().getClassLoader(), Intent.class));
4652 
4653         p.recycle();
4654     }
4655 
4656     @Test
testReadParcelableListWithClass_whenSameClass()4657     public void testReadParcelableListWithClass_whenSameClass(){
4658         final Parcel p = Parcel.obtain();
4659         ArrayList<Signature> list = new ArrayList<>();
4660         ArrayList<Signature> list1 = new ArrayList<>();
4661         list.add(new Signature("1234"));
4662         list.add(new Signature("4321"));
4663         p.writeParcelableList(list, 0);
4664         p.setDataPosition(0);
4665         p.readParcelableList(list1, getClass().getClassLoader(), Signature.class);
4666 
4667         assertEquals(list, list1);
4668         p.recycle();
4669     }
4670 
4671     @Test
testReadParcelableListWithClass_whenSubClass()4672     public void testReadParcelableListWithClass_whenSubClass(){
4673         final Parcel p = Parcel.obtain();
4674         final Intent baseIntent = new Intent();
4675 
4676         ArrayList<Intent> intentArrayList = new ArrayList<>();
4677         ArrayList<Intent> intentArrayList1 = new ArrayList<>();
4678         ArrayList<Intent> intentArrayList2 = new ArrayList<>();
4679 
4680         intentArrayList.add(new TestSubIntent(baseIntent, "1234567890abcdef"));
4681         intentArrayList.add(null);
4682         intentArrayList.add(new TestSubIntent(baseIntent, "abcdef1234567890"));
4683 
4684         p.writeParcelableList(intentArrayList, 0);
4685         p.setDataPosition(0);
4686         p.readParcelableList(intentArrayList1, getClass().getClassLoader(), Intent.class);
4687         assertEquals(intentArrayList, intentArrayList1);
4688 
4689         p.setDataPosition(0);
4690         p.readParcelableList(intentArrayList2, getClass().getClassLoader(), TestSubIntent.class);
4691         assertEquals(intentArrayList, intentArrayList2);
4692         p.recycle();
4693     }
4694 
4695     // http://b/35384981
4696     @Test
testCreateArrayWithTruncatedParcel()4697     public void testCreateArrayWithTruncatedParcel() {
4698         Parcel parcel = Parcel.obtain();
4699         parcel.writeByteArray(new byte[] { 'a', 'b' });
4700         byte[] marshalled = parcel.marshall();
4701 
4702         // Test that createByteArray returns null with a truncated parcel.
4703         parcel = Parcel.obtain();
4704         parcel.unmarshall(marshalled, 0, marshalled.length);
4705         parcel.setDataPosition(0);
4706         // Shorten the data size by 2 to remove padding at the end of the array.
4707         parcel.setDataSize(marshalled.length - 2);
4708         assertNull(parcel.createByteArray());
4709 
4710         // Test that readByteArray returns null with a truncated parcel.
4711         parcel = Parcel.obtain();
4712         parcel.unmarshall(marshalled, 0, marshalled.length);
4713         parcel.setDataSize(marshalled.length - 2);
4714         try {
4715             parcel.readByteArray(new byte[2]);
4716             fail();
4717         } catch (RuntimeException expected) {
4718         }
4719     }
4720 
4721     @Test
testMaliciousMapWrite()4722     public void testMaliciousMapWrite() {
4723         class MaliciousMap<K, V> extends HashMap<K, V> {
4724             public int fakeSize = 0;
4725             public boolean armed = false;
4726 
4727             class FakeEntrySet extends HashSet<Entry<K, V>> {
4728                 public FakeEntrySet(Collection<? extends Entry<K, V>> c) {
4729                     super(c);
4730                 }
4731 
4732                 @Override
4733                 public int size() {
4734                     if (armed) {
4735                         // Only return fake size on next call, to mitigate unexpected behavior.
4736                         armed = false;
4737                         return fakeSize;
4738                     } else {
4739                         return super.size();
4740                     }
4741                 }
4742             }
4743 
4744             @Override
4745             public Set<Map.Entry<K, V>> entrySet() {
4746                 return new FakeEntrySet(super.entrySet());
4747             }
4748         }
4749 
4750         Parcel parcel = Parcel.obtain();
4751 
4752         // Fake having more Map entries than there really are
4753         MaliciousMap map = new MaliciousMap<String, String>();
4754         map.fakeSize = 1;
4755         map.armed = true;
4756         try {
4757             parcel.writeMap(map);
4758             fail("Should have thrown a BadParcelableException");
4759         } catch (BadParcelableException bpe) {
4760             // good
4761         }
4762 
4763         // Fake having fewer Map entries than there really are
4764         map = new MaliciousMap<String, String>();
4765         map.put("key", "value");
4766         map.fakeSize = 0;
4767         map.armed = true;
4768         try {
4769             parcel.writeMap(map);
4770             fail("Should have thrown a BadParcelableException");
4771         } catch (BadParcelableException bpe) {
4772             // good
4773         }
4774     }
4775 
4776     public static class TestSubIntent extends Intent {
4777         private final String mString;
4778 
TestSubIntent(Intent baseIntent, String s)4779         public TestSubIntent(Intent baseIntent, String s) {
4780             super(baseIntent);
4781             mString = s;
4782         }
4783 
writeToParcel(Parcel dest, int parcelableFlags)4784         public void writeToParcel(Parcel dest, int parcelableFlags) {
4785             super.writeToParcel(dest, parcelableFlags);
4786             dest.writeString(mString);
4787         }
4788 
TestSubIntent(Parcel in)4789         TestSubIntent(Parcel in) {
4790             readFromParcel(in);
4791             mString = in.readString();
4792         }
4793 
4794         public static final Creator<TestSubIntent> CREATOR = new Creator<TestSubIntent>() {
4795             public TestSubIntent createFromParcel(Parcel source) {
4796                 return new TestSubIntent(source);
4797             }
4798 
4799             @Override
4800             public TestSubIntent[] newArray(int size) {
4801                 return new TestSubIntent[size];
4802             }
4803         };
4804 
4805         @Override
equals(Object obj)4806         public boolean equals(Object obj) {
4807             final TestSubIntent other = (TestSubIntent) obj;
4808             return mString.equals(other.mString);
4809         }
4810 
4811         @Override
hashCode()4812         public int hashCode() {
4813             return mString.hashCode();
4814         }
4815     }
4816 
4817     private static class TestSubException extends Exception{
TestSubException(String msg)4818         public TestSubException(String msg) {
4819             super(msg);
4820         }
4821     }
4822 
4823     @Test
4824     @AsbSecurityTest(cveBugId = 140419401)
testObjectResize()4825     public void testObjectResize() throws Exception {
4826         Parcel p;
4827         IBinder b1 = new Binder();
4828         IBinder b2 = new Binder();
4829 
4830         p = Parcel.obtain();
4831         p.writeStrongBinder(b1);
4832         p.setDataSize(0);
4833         p.writeStrongBinder(b2);
4834 
4835         p.setDataPosition(0);
4836         assertEquals("Object in parcel should match the binder written after the resize", b2,
4837                 p.readStrongBinder());
4838         p.recycle();
4839 
4840         p = Parcel.obtain();
4841         p.writeStrongBinder(b1);
4842         final int secondBinderPos = p.dataPosition();
4843         p.writeStrongBinder(b1);
4844         p.setDataSize(secondBinderPos);
4845         p.writeStrongBinder(b2);
4846 
4847         p.setDataPosition(0);
4848         assertEquals("Object at the start of the parcel parcel should match the first binder", b1,
4849                 p.readStrongBinder());
4850         assertEquals("Object in parcel should match the binder written after the resize", b2,
4851                 p.readStrongBinder());
4852         p.recycle();
4853     }
4854 
4855     @Test
testFlags()4856     public void testFlags() {
4857         Parcel p;
4858 
4859         p = Parcel.obtain();
4860         assertEquals(0, p.getFlags());
4861         p.setPropagateAllowBlocking();
4862         assertEquals(Parcel.FLAG_PROPAGATE_ALLOW_BLOCKING, p.getFlags());
4863 
4864         // recycle / obtain should clear the flag.
4865         p.recycle();
4866         p = Parcel.obtain();
4867         assertEquals(0, p.getFlags());
4868     }
4869 
4870     public static class SimpleParcelableWithoutNestedCreator implements Parcelable {
4871         private final int value;
4872 
SimpleParcelableWithoutNestedCreator(int value)4873         public SimpleParcelableWithoutNestedCreator(int value) {
4874             this.value = value;
4875         }
4876 
SimpleParcelableWithoutNestedCreator(Parcel in)4877         private SimpleParcelableWithoutNestedCreator(Parcel in) {
4878             this.value = in.readInt();
4879         }
4880 
getValue()4881         public int getValue() {
4882             return value;
4883         }
4884 
4885         @Override
describeContents()4886         public int describeContents() {
4887             return 0;
4888         }
4889 
4890         @Override
writeToParcel(Parcel out, int flags)4891         public void writeToParcel(Parcel out, int flags) {
4892             out.writeInt(value);
4893         }
4894 
4895         public static Parcelable.Creator<SimpleParcelableWithoutNestedCreator> CREATOR =
4896                 new SimpleParcelableWithoutNestedCreatorCreator();
4897     }
4898 
4899     public static class SimpleParcelableWithoutNestedCreatorCreator implements
4900             Parcelable.Creator<SimpleParcelableWithoutNestedCreator> {
4901         @Override
createFromParcel(Parcel source)4902         public SimpleParcelableWithoutNestedCreator createFromParcel(Parcel source) {
4903             return new SimpleParcelableWithoutNestedCreator(source);
4904         }
4905 
4906         @Override
newArray(int size)4907         public SimpleParcelableWithoutNestedCreator[] newArray(int size) {
4908             return new SimpleParcelableWithoutNestedCreator[size];
4909         }
4910     }
4911 
4912     // http://b/232589966
4913     @Test
testReadParcelableWithoutNestedCreator()4914     public void testReadParcelableWithoutNestedCreator() {
4915         Parcel p = Parcel.obtain();
4916         p.writeParcelable(new SimpleParcelableWithoutNestedCreator(1), 0);
4917         p.setDataPosition(0);
4918         // First time checks the type of the creator using reflection
4919         SimpleParcelableWithoutNestedCreator parcelable =
4920                 p.readParcelable(getClass().getClassLoader(),
4921                         SimpleParcelableWithoutNestedCreator.class);
4922         assertEquals(1, parcelable.value);
4923         p.recycle();
4924 
4925         p = Parcel.obtain();
4926         p.writeParcelable(new SimpleParcelableWithoutNestedCreator(2), 0);
4927         p.setDataPosition(0);
4928         // Second time tries to read it from a cache
4929         parcelable =
4930                 p.readParcelable(getClass().getClassLoader(),
4931                         SimpleParcelableWithoutNestedCreator.class);
4932         assertEquals(2, parcelable.value);
4933         p.recycle();
4934     }
4935 
4936     @Test
4937     // @ApiTest(apis = {"android.os.Parcel#writeTypedSparseArray(android.utilSparseArray, int)",
4938     //        "android.os.Parcel#createTypedSparseArray(android.os.Parcelable.Creator)"})
testCreateTypedSparseArray()4939     public void testCreateTypedSparseArray() {
4940         Parcel p;
4941         SparseArray<Signature> s = new SparseArray<>();
4942         s.append(1, new Signature("1234"));
4943         s.append(2, new Signature("ABCD"));
4944         s.append(3, new Signature("abcd"));
4945 
4946         SparseArray<Signature> s2 = new SparseArray<>();
4947         s2.append(1, new Signature("1234"));
4948         s2.append(2, null);
4949         s2.append(3, new Signature("abcd"));
4950         SparseArray<Signature> s3;
4951 
4952         // test write null
4953         p = Parcel.obtain();
4954         p.writeTypedSparseArray(null, 0);
4955         p.setDataPosition(0);
4956         assertNull(p.createTypedSparseArray(Signature.CREATOR));
4957         p.recycle();
4958 
4959         // test write not null
4960         p = Parcel.obtain();
4961         p.writeTypedSparseArray(s, 0);
4962         p.setDataPosition(0);
4963         s3 = p.createTypedSparseArray(Signature.CREATOR);
4964         assertNotNull(s3);
4965         assertEquals(s3.size(), s.size());
4966         for (int i = 0; i < s.size(); i++) {
4967             assertEquals(s3.keyAt(i), s.keyAt(i));
4968             assertEquals(s3.valueAt(i), s.valueAt(i));
4969         }
4970         p.recycle();
4971 
4972         p = Parcel.obtain();
4973         p.writeTypedSparseArray(s2, 0);
4974         p.setDataPosition(0);
4975         s3 = p.createTypedSparseArray(Signature.CREATOR);
4976         assertNotNull(s3);
4977         assertEquals(s3.size(), s2.size());
4978         for (int i = 0; i < s.size(); i++) {
4979             assertEquals(s3.keyAt(i), s2.keyAt(i));
4980             assertEquals(s3.valueAt(i), s2.valueAt(i));
4981         }
4982         p.recycle();
4983     }
4984 
4985     @Test
testReadEmpty()4986     public void testReadEmpty() {
4987         Parcel p = Parcel.obtain();
4988         assertEquals(false, p.readBoolean());
4989         assertEquals(0, p.readByte());
4990         assertEquals(0, p.readInt());
4991         assertEquals(0, p.readLong());
4992         assertEquals(0f, p.readFloat(), DELTA_FLOAT);
4993         assertEquals(0d, p.readDouble(), DELTA_DOUBLE);
4994         assertNull(p.readString());
4995     }
4996 }
4997