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 java.io.FileDescriptor; 20 import java.io.Serializable; 21 import java.util.ArrayList; 22 import java.util.Collection; 23 import java.util.HashMap; 24 import java.util.HashSet; 25 import java.util.Map; 26 import java.util.Set; 27 import java.util.concurrent.ExecutionException; 28 import java.util.concurrent.TimeUnit; 29 import java.util.concurrent.TimeoutException; 30 31 import android.app.Service; 32 import android.content.ComponentName; 33 import android.content.Context; 34 import android.content.Intent; 35 import android.content.ServiceConnection; 36 import android.content.pm.Signature; 37 import android.os.BadParcelableException; 38 import android.os.Binder; 39 import android.os.Bundle; 40 import android.os.IBinder; 41 import android.os.IInterface; 42 import android.os.Parcel; 43 import android.os.ParcelFileDescriptor; 44 import android.os.Parcelable; 45 import android.platform.test.annotations.AsbSecurityTest; 46 import android.test.AndroidTestCase; 47 import android.util.Log; 48 import android.util.SparseArray; 49 import android.util.SparseBooleanArray; 50 51 import com.google.common.util.concurrent.AbstractFuture; 52 53 public class ParcelTest extends AndroidTestCase { 54 testObtain()55 public void testObtain() { 56 Parcel p1 = Parcel.obtain(); 57 assertNotNull(p1); 58 Parcel p2 = Parcel.obtain(); 59 assertNotNull(p2); 60 Parcel p3 = Parcel.obtain(); 61 assertNotNull(p3); 62 Parcel p4 = Parcel.obtain(); 63 assertNotNull(p4); 64 Parcel p5 = Parcel.obtain(); 65 assertNotNull(p5); 66 Parcel p6 = Parcel.obtain(); 67 assertNotNull(p6); 68 Parcel p7 = Parcel.obtain(); 69 assertNotNull(p7); 70 71 p1.recycle(); 72 p2.recycle(); 73 p3.recycle(); 74 p4.recycle(); 75 p5.recycle(); 76 p6.recycle(); 77 p7.recycle(); 78 } 79 testAppendFrom()80 public void testAppendFrom() { 81 Parcel p; 82 Parcel p2; 83 int d1; 84 int d2; 85 86 p = Parcel.obtain(); 87 d1 = p.dataPosition(); 88 p.writeInt(7); 89 p.writeInt(5); 90 d2 = p.dataPosition(); 91 p2 = Parcel.obtain(); 92 p2.appendFrom(p, d1, d2 - d1); 93 p2.setDataPosition(0); 94 assertEquals(7, p2.readInt()); 95 assertEquals(5, p2.readInt()); 96 p2.recycle(); 97 p.recycle(); 98 } 99 testDataAvail()100 public void testDataAvail() { 101 Parcel p; 102 103 p = Parcel.obtain(); 104 p.writeInt(7); // size 4 105 p.writeInt(5); // size 4 106 p.writeLong(7L); // size 8 107 p.writeString("7L"); // size 12 108 p.setDataPosition(0); 109 assertEquals(p.dataSize(), p.dataAvail()); 110 p.readInt(); 111 assertEquals(p.dataSize() - p.dataPosition(), p.dataAvail()); 112 p.readInt(); 113 assertEquals(p.dataSize() - p.dataPosition(), p.dataAvail()); 114 p.readLong(); 115 assertEquals(p.dataSize() - p.dataPosition(), p.dataAvail()); 116 p.readString(); 117 assertEquals(p.dataSize() - p.dataPosition(), p.dataAvail()); 118 p.recycle(); 119 } 120 testDataCapacity()121 public void testDataCapacity() { 122 Parcel p; 123 124 p = Parcel.obtain(); 125 assertEquals(0, p.dataCapacity()); 126 p.writeInt(7); // size 4 127 int dC1 = p.dataCapacity(); 128 p.writeDouble(2.19); 129 int dC2 = p.dataCapacity(); 130 assertTrue(dC2 >= dC1); 131 p.recycle(); 132 } 133 testSetDataCapacity()134 public void testSetDataCapacity() { 135 Parcel p; 136 137 p = Parcel.obtain(); 138 assertEquals(0, p.dataCapacity()); 139 p.setDataCapacity(2); 140 assertEquals(2, p.dataCapacity()); 141 p.setDataCapacity(1); 142 assertEquals(2, p.dataCapacity()); 143 p.setDataCapacity(3); 144 assertEquals(3, p.dataCapacity()); 145 p.recycle(); 146 } 147 testDataPosition()148 public void testDataPosition() { 149 Parcel p; 150 151 p = Parcel.obtain(); 152 assertEquals(0, p.dataPosition()); 153 p.writeInt(7); // size 4 154 int dP1 = p.dataPosition(); 155 p.writeLong(7L); // size 8 156 int dP2 = p.dataPosition(); 157 assertTrue(dP2 > dP1); 158 p.recycle(); 159 } 160 testSetDataPosition()161 public void testSetDataPosition() { 162 Parcel p; 163 164 p = Parcel.obtain(); 165 assertEquals(0, p.dataSize()); 166 assertEquals(0, p.dataPosition()); 167 p.setDataPosition(4); 168 assertEquals(4, p.dataPosition()); 169 p.setDataPosition(7); 170 assertEquals(7, p.dataPosition()); 171 p.setDataPosition(0); 172 p.writeInt(7); 173 assertEquals(4, p.dataSize()); 174 p.setDataPosition(4); 175 assertEquals(4, p.dataPosition()); 176 p.setDataPosition(7); 177 assertEquals(7, p.dataPosition()); 178 p.recycle(); 179 } 180 testDataSize()181 public void testDataSize() { 182 Parcel p; 183 184 p = Parcel.obtain(); 185 assertEquals(0, p.dataSize()); 186 p.writeInt(7); // size 4 187 assertEquals(4, p.dataSize()); 188 p.writeInt(5); // size 4 189 assertEquals(8, p.dataSize()); 190 p.writeLong(7L); // size 8 191 assertEquals(16, p.dataSize()); 192 p.recycle(); 193 } 194 testSetDataSize()195 public void testSetDataSize() { 196 Parcel p; 197 198 p = Parcel.obtain(); 199 assertEquals(0, p.dataSize()); 200 p.setDataSize(5); 201 assertEquals(5, p.dataSize()); 202 p.setDataSize(3); 203 assertEquals(3, p.dataSize()); 204 205 p.writeInt(3); 206 assertEquals(4, p.dataSize()); 207 p.setDataSize(5); 208 assertEquals(5, p.dataSize()); 209 p.setDataSize(3); 210 assertEquals(3, p.dataSize()); 211 p.recycle(); 212 } 213 testEnforceInterface()214 public void testEnforceInterface() { 215 Parcel p; 216 String s = "IBinder interface token"; 217 218 p = Parcel.obtain(); 219 p.writeInterfaceToken(s); 220 p.setDataPosition(0); 221 try { 222 p.enforceInterface(""); 223 fail("Should throw an SecurityException"); 224 } catch (SecurityException e) { 225 //expected 226 } 227 p.recycle(); 228 229 p = Parcel.obtain(); 230 p.writeInterfaceToken(s); 231 p.setDataPosition(0); 232 p.enforceInterface(s); 233 p.recycle(); 234 } 235 testMarshall()236 public void testMarshall() { 237 final byte[] c = {Byte.MAX_VALUE, (byte) 111, (byte) 11, (byte) 1, (byte) 0, 238 (byte) -1, (byte) -11, (byte) -111, Byte.MIN_VALUE}; 239 240 Parcel p1 = Parcel.obtain(); 241 p1.writeByteArray(c); 242 p1.setDataPosition(0); 243 byte[] d1 = p1.marshall(); 244 245 Parcel p2 = Parcel.obtain(); 246 p2.unmarshall(d1, 0, d1.length); 247 p2.setDataPosition(0); 248 byte[] d2 = new byte[c.length]; 249 p2.readByteArray(d2); 250 251 for (int i = 0; i < c.length; i++) { 252 assertEquals(c[i], d2[i]); 253 } 254 255 p1.recycle(); 256 p2.recycle(); 257 } 258 259 @SuppressWarnings("unchecked") testReadValue()260 public void testReadValue() { 261 Parcel p; 262 MockClassLoader mcl = new MockClassLoader(); 263 264 // test null 265 p = Parcel.obtain(); 266 p.writeValue(null); 267 p.setDataPosition(0); 268 assertNull(p.readValue(mcl)); 269 p.recycle(); 270 271 // test String 272 p = Parcel.obtain(); 273 p.writeValue("String"); 274 p.setDataPosition(0); 275 assertEquals("String", p.readValue(mcl)); 276 p.recycle(); 277 278 // test Integer 279 p = Parcel.obtain(); 280 p.writeValue(Integer.MAX_VALUE); 281 p.setDataPosition(0); 282 assertEquals(Integer.MAX_VALUE, p.readValue(mcl)); 283 p.recycle(); 284 285 // test Map 286 HashMap map = new HashMap(); 287 HashMap map2; 288 map.put("string", "String"); 289 map.put("int", Integer.MAX_VALUE); 290 map.put("boolean", true); 291 p = Parcel.obtain(); 292 p.writeValue(map); 293 p.setDataPosition(0); 294 map2 = (HashMap) p.readValue(mcl); 295 assertNotNull(map2); 296 assertEquals(map.size(), map2.size()); 297 assertEquals("String", map.get("string")); 298 assertEquals(Integer.MAX_VALUE, map.get("int")); 299 assertEquals(true, map.get("boolean")); 300 p.recycle(); 301 302 // test Bundle 303 Bundle bundle = new Bundle(); 304 bundle.putBoolean("boolean", true); 305 bundle.putInt("int", Integer.MAX_VALUE); 306 bundle.putString("string", "String"); 307 Bundle bundle2; 308 p = Parcel.obtain(); 309 p.writeValue(bundle); 310 p.setDataPosition(0); 311 bundle2 = (Bundle) p.readValue(mcl); 312 assertNotNull(bundle2); 313 assertEquals(true, bundle2.getBoolean("boolean")); 314 assertEquals(Integer.MAX_VALUE, bundle2.getInt("int")); 315 assertEquals("String", bundle2.getString("string")); 316 p.recycle(); 317 318 // test Parcelable 319 final String signatureString = "1234567890abcdef"; 320 Signature s = new Signature(signatureString); 321 p = Parcel.obtain(); 322 p.writeValue(s); 323 p.setDataPosition(0); 324 assertEquals(s, p.readValue(mcl)); 325 p.recycle(); 326 327 // test Short 328 p = Parcel.obtain(); 329 p.writeValue(Short.MAX_VALUE); 330 p.setDataPosition(0); 331 assertEquals(Short.MAX_VALUE, p.readValue(mcl)); 332 p.recycle(); 333 334 // test Long 335 p = Parcel.obtain(); 336 p.writeValue(Long.MAX_VALUE); 337 p.setDataPosition(0); 338 assertEquals(Long.MAX_VALUE, p.readValue(mcl)); 339 p.recycle(); 340 341 // test Float 342 p = Parcel.obtain(); 343 p.writeValue(Float.MAX_VALUE); 344 p.setDataPosition(0); 345 assertEquals(Float.MAX_VALUE, p.readValue(mcl)); 346 p.recycle(); 347 348 // test Double 349 p = Parcel.obtain(); 350 p.writeValue(Double.MAX_VALUE); 351 p.setDataPosition(0); 352 assertEquals(Double.MAX_VALUE, p.readValue(mcl)); 353 p.recycle(); 354 355 // test Boolean 356 p = Parcel.obtain(); 357 p.writeValue(true); 358 p.writeValue(false); 359 p.setDataPosition(0); 360 assertTrue((Boolean) p.readValue(mcl)); 361 assertFalse((Boolean) p.readValue(mcl)); 362 p.recycle(); 363 364 // test CharSequence 365 p = Parcel.obtain(); 366 p.writeValue((CharSequence) "CharSequence"); 367 p.setDataPosition(0); 368 assertEquals("CharSequence", p.readValue(mcl)); 369 p.recycle(); 370 371 // test List 372 ArrayList arrayList2 = new ArrayList(); 373 arrayList2.add(Integer.MAX_VALUE); 374 arrayList2.add(true); 375 arrayList2.add(Long.MAX_VALUE); 376 ArrayList arrayList = new ArrayList(); 377 p = Parcel.obtain(); 378 p.writeValue(arrayList2); 379 p.setDataPosition(0); 380 assertEquals(0, arrayList.size()); 381 arrayList = (ArrayList) p.readValue(mcl); 382 assertEquals(3, arrayList.size()); 383 for (int i = 0; i < arrayList.size(); i++) { 384 assertEquals(arrayList.get(i), arrayList2.get(i)); 385 } 386 p.recycle(); 387 388 // test SparseArray 389 SparseArray<Object> sparseArray = new SparseArray<Object>(); 390 sparseArray.put(3, "String"); 391 sparseArray.put(2, Long.MAX_VALUE); 392 sparseArray.put(4, Float.MAX_VALUE); 393 sparseArray.put(0, Integer.MAX_VALUE); 394 sparseArray.put(1, true); 395 sparseArray.put(10, true); 396 SparseArray<Object> sparseArray2; 397 p = Parcel.obtain(); 398 p.writeValue(sparseArray); 399 p.setDataPosition(0); 400 sparseArray2 = (SparseArray<Object>) p.readValue(mcl); 401 assertNotNull(sparseArray2); 402 assertEquals(sparseArray.size(), sparseArray2.size()); 403 assertEquals(sparseArray.get(0), sparseArray2.get(0)); 404 assertEquals(sparseArray.get(1), sparseArray2.get(1)); 405 assertEquals(sparseArray.get(2), sparseArray2.get(2)); 406 assertEquals(sparseArray.get(3), sparseArray2.get(3)); 407 assertEquals(sparseArray.get(4), sparseArray2.get(4)); 408 assertEquals(sparseArray.get(10), sparseArray2.get(10)); 409 p.recycle(); 410 411 // test boolean[] 412 boolean[] booleanArray = {true, false, true, false}; 413 boolean[] booleanArray2 = new boolean[booleanArray.length]; 414 p = Parcel.obtain(); 415 p.writeValue(booleanArray); 416 p.setDataPosition(0); 417 booleanArray2 = (boolean[]) p.readValue(mcl); 418 for (int i = 0; i < booleanArray.length; i++) { 419 assertEquals(booleanArray[i], booleanArray2[i]); 420 } 421 p.recycle(); 422 423 // test byte[] 424 byte[] byteArray = {Byte.MAX_VALUE, (byte) 111, (byte) 11, (byte) 1, (byte) 0, 425 (byte) -1, (byte) -11, (byte) -111, Byte.MIN_VALUE}; 426 byte[] byteArray2 = new byte[byteArray.length]; 427 p = Parcel.obtain(); 428 p.writeValue(byteArray); 429 p.setDataPosition(0); 430 byteArray2 = (byte[]) p.readValue(mcl); 431 for (int i = 0; i < byteArray.length; i++) { 432 assertEquals(byteArray[i], byteArray2[i]); 433 } 434 p.recycle(); 435 436 // test string[] 437 String[] stringArray = {"", 438 "a", 439 "Hello, Android!", 440 "A long string that is used to test the api readStringArray(),"}; 441 String[] stringArray2 = new String[stringArray.length]; 442 p = Parcel.obtain(); 443 p.writeValue(stringArray); 444 p.setDataPosition(0); 445 stringArray2 = (String[]) p.readValue(mcl); 446 for (int i = 0; i < stringArray.length; i++) { 447 assertEquals(stringArray[i], stringArray2[i]); 448 } 449 p.recycle(); 450 451 // test IBinder 452 Binder binder; 453 Binder binder2 = new Binder(); 454 p = Parcel.obtain(); 455 p.writeValue(binder2); 456 p.setDataPosition(0); 457 binder = (Binder) p.readValue(mcl); 458 assertEquals(binder2, binder); 459 p.recycle(); 460 461 // test Parcelable[] 462 Signature[] signatures = {new Signature("1234"), 463 new Signature("ABCD"), 464 new Signature("abcd")}; 465 Parcelable[] signatures2; 466 p = Parcel.obtain(); 467 p.writeValue(signatures); 468 p.setDataPosition(0); 469 signatures2 = (Parcelable[]) p.readValue(mcl); 470 for (int i = 0; i < signatures.length; i++) { 471 assertEquals(signatures[i], signatures2[i]); 472 } 473 p.recycle(); 474 475 // test Object 476 Object[] objects = new Object[5]; 477 objects[0] = Integer.MAX_VALUE; 478 objects[1] = true; 479 objects[2] = Long.MAX_VALUE; 480 objects[3] = "String"; 481 objects[4] = Float.MAX_VALUE; 482 Object[] objects2; 483 p = Parcel.obtain(); 484 p.writeValue(objects); 485 p.setDataPosition(0); 486 objects2 = (Object[]) p.readValue(mcl); 487 assertNotNull(objects2); 488 for (int i = 0; i < objects2.length; i++) { 489 assertEquals(objects[i], objects2[i]); 490 } 491 p.recycle(); 492 493 // test int[] 494 int[] intArray = {111, 11, 1, 0, -1, -11, -111}; 495 int[] intArray2 = new int[intArray.length]; 496 p = Parcel.obtain(); 497 p.writeValue(intArray); 498 p.setDataPosition(0); 499 intArray2= (int[]) p.readValue(mcl); 500 assertNotNull(intArray2); 501 for (int i = 0; i < intArray2.length; i++) { 502 assertEquals(intArray[i], intArray2[i]); 503 } 504 p.recycle(); 505 506 // test long[] 507 long[] longArray = {111L, 11L, 1L, 0L, -1L, -11L, -111L}; 508 long[] longArray2 = new long[longArray.length]; 509 p = Parcel.obtain(); 510 p.writeValue(longArray); 511 p.setDataPosition(0); 512 longArray2= (long[]) p.readValue(mcl); 513 assertNotNull(longArray2); 514 for (int i = 0; i < longArray2.length; i++) { 515 assertEquals(longArray[i], longArray2[i]); 516 } 517 p.recycle(); 518 519 // test byte 520 p = Parcel.obtain(); 521 p.writeValue(Byte.MAX_VALUE); 522 p.setDataPosition(0); 523 assertEquals(Byte.MAX_VALUE, p.readValue(mcl)); 524 p.recycle(); 525 526 // test Serializable 527 p = Parcel.obtain(); 528 p.writeValue((Serializable) "Serializable"); 529 p.setDataPosition(0); 530 assertEquals("Serializable", p.readValue(mcl)); 531 p.recycle(); 532 } 533 testReadByte()534 public void testReadByte() { 535 Parcel p; 536 537 p = Parcel.obtain(); 538 p.writeByte((byte) 0); 539 p.setDataPosition(0); 540 assertEquals((byte) 0, p.readByte()); 541 p.recycle(); 542 543 p = Parcel.obtain(); 544 p.writeByte((byte) 1); 545 p.setDataPosition(0); 546 assertEquals((byte) 1, p.readByte()); 547 p.recycle(); 548 549 p = Parcel.obtain(); 550 p.writeByte((byte) -1); 551 p.setDataPosition(0); 552 assertEquals((byte) -1, p.readByte()); 553 p.recycle(); 554 555 p = Parcel.obtain(); 556 p.writeByte(Byte.MAX_VALUE); 557 p.setDataPosition(0); 558 assertEquals(Byte.MAX_VALUE, p.readByte()); 559 p.recycle(); 560 561 p = Parcel.obtain(); 562 p.writeByte(Byte.MIN_VALUE); 563 p.setDataPosition(0); 564 assertEquals(Byte.MIN_VALUE, p.readByte()); 565 p.recycle(); 566 567 p = Parcel.obtain(); 568 p.writeByte(Byte.MAX_VALUE); 569 p.writeByte((byte) 11); 570 p.writeByte((byte) 1); 571 p.writeByte((byte) 0); 572 p.writeByte((byte) -1); 573 p.writeByte((byte) -11); 574 p.writeByte(Byte.MIN_VALUE); 575 p.setDataPosition(0); 576 assertEquals(Byte.MAX_VALUE, p.readByte()); 577 assertEquals((byte) 11, p.readByte()); 578 assertEquals((byte) 1, p.readByte()); 579 assertEquals((byte) 0, p.readByte()); 580 assertEquals((byte) -1, p.readByte()); 581 assertEquals((byte) -11, p.readByte()); 582 assertEquals(Byte.MIN_VALUE, p.readByte()); 583 p.recycle(); 584 } 585 testReadByteArray()586 public void testReadByteArray() { 587 Parcel p; 588 589 byte[] a = {(byte) 21}; 590 byte[] b = new byte[a.length]; 591 592 byte[] c = {Byte.MAX_VALUE, (byte) 111, (byte) 11, (byte) 1, (byte) 0, 593 (byte) -1, (byte) -11, (byte) -111, Byte.MIN_VALUE}; 594 byte[] d = new byte[c.length]; 595 596 // test write null 597 p = Parcel.obtain(); 598 p.writeByteArray(null); 599 p.setDataPosition(0); 600 try { 601 p.readByteArray(null); 602 fail("Should throw a RuntimeException"); 603 } catch (RuntimeException e) { 604 //expected 605 } 606 607 p.setDataPosition(0); 608 try { 609 p.readByteArray(b); 610 fail("Should throw a RuntimeException"); 611 } catch (RuntimeException e) { 612 //expected 613 } 614 p.recycle(); 615 616 // test write byte array with length: 1 617 p = Parcel.obtain(); 618 p.writeByteArray(a); 619 p.setDataPosition(0); 620 try { 621 p.readByteArray(d); 622 fail("Should throw a RuntimeException"); 623 } catch (RuntimeException e) { 624 //expected 625 } 626 627 p.setDataPosition(0); 628 p.readByteArray(b); 629 for (int i = 0; i < a.length; i++) { 630 assertEquals(a[i], b[i]); 631 } 632 p.recycle(); 633 634 // test write byte array with length: 9 635 p = Parcel.obtain(); 636 p.writeByteArray(c); 637 p.setDataPosition(0); 638 try { 639 p.readByteArray(b); 640 fail("Should throw a RuntimeException"); 641 } catch (RuntimeException e) { 642 //expected 643 } 644 645 p.setDataPosition(0); 646 p.readByteArray(d); 647 for (int i = 0; i < c.length; i++) { 648 assertEquals(c[i], d[i]); 649 } 650 p.recycle(); 651 652 // Test array bounds checks (null already checked above). 653 p = Parcel.obtain(); 654 try { 655 p.writeByteArray(c, -1, 1); // Negative offset. 656 fail(); 657 } catch (RuntimeException expected) { 658 } 659 try { 660 p.writeByteArray(c, 0, -1); // Negative count. 661 fail(); 662 } catch (RuntimeException expected) { 663 } 664 try { 665 p.writeByteArray(c, c.length + 1, 1); // High offset. 666 fail(); 667 } catch (RuntimeException expected) { 668 } 669 try { 670 p.writeByteArray(c, 0, c.length + 1); // High count. 671 fail(); 672 } catch (RuntimeException expected) { 673 } 674 p.recycle(); 675 } 676 testWriteByteArray()677 public void testWriteByteArray() { 678 Parcel p; 679 680 byte[] a = {(byte) 21}; 681 byte[] b = new byte[a.length]; 682 683 byte[] c = {Byte.MAX_VALUE, (byte) 111, (byte) 11, (byte) 1, (byte) 0, 684 (byte) -1, (byte) -11, (byte) -111, Byte.MIN_VALUE}; 685 byte[] d = new byte[c.length - 2]; 686 687 // test write null 688 p = Parcel.obtain(); 689 p.writeByteArray(null, 0, 2); 690 p.setDataPosition(0); 691 try { 692 p.readByteArray(null); 693 fail("Should throw a RuntimeException"); 694 } catch (RuntimeException e) { 695 //expected 696 } 697 698 p.setDataPosition(0); 699 try { 700 p.readByteArray(b); 701 fail("Should throw a RuntimeException"); 702 } catch (RuntimeException e) { 703 //expected 704 } 705 p.recycle(); 706 707 // test with wrong offset and length 708 p = Parcel.obtain(); 709 try { 710 p.writeByteArray(a, 0, 2); 711 fail("Should throw a ArrayIndexOutOfBoundsException"); 712 } catch (ArrayIndexOutOfBoundsException e) { 713 //expected 714 } 715 p.recycle(); 716 717 p = Parcel.obtain(); 718 try { 719 p.writeByteArray(a, -1, 1); 720 fail("Should throw a ArrayIndexOutOfBoundsException"); 721 } catch (ArrayIndexOutOfBoundsException e) { 722 //expected 723 } 724 p.recycle(); 725 726 p = Parcel.obtain(); 727 try { 728 p.writeByteArray(a, 0, -1); 729 fail("Should throw a ArrayIndexOutOfBoundsException"); 730 } catch (ArrayIndexOutOfBoundsException e) { 731 //expected 732 } 733 p.recycle(); 734 735 // test write byte array with length: 1 736 p = Parcel.obtain(); 737 p.writeByteArray(a, 0 , 1); 738 p.setDataPosition(0); 739 try { 740 p.readByteArray(d); 741 fail("Should throw a RuntimeException"); 742 } catch (RuntimeException e) { 743 //expected 744 } 745 746 p.setDataPosition(0); 747 p.readByteArray(b); 748 for (int i = 0; i < a.length; i++) { 749 assertEquals(a[i], b[i]); 750 } 751 p.recycle(); 752 753 // test write byte array with offset: 1, length: 7 754 p = Parcel.obtain(); 755 p.writeByteArray(c, 1, 7); 756 p.setDataPosition(0); 757 try { 758 p.readByteArray(b); 759 fail("Should throw a RuntimeException"); 760 } catch (RuntimeException e) { 761 //expected 762 } 763 764 d = new byte[c.length - 2]; 765 p.setDataPosition(0); 766 p.readByteArray(d); 767 for (int i = 0; i < d.length; i++) { 768 Log.d("Trace", "i=" + i + " d[i]=" + d[i]); 769 } 770 for (int i = 0; i < 7; i++) { 771 assertEquals(c[i + 1], d[i]); 772 } 773 p.recycle(); 774 } 775 testCreateByteArray()776 public void testCreateByteArray() { 777 Parcel p; 778 779 byte[] a = {(byte) 21}; 780 byte[] b; 781 782 byte[] c = {Byte.MAX_VALUE, (byte) 111, (byte) 11, (byte) 1, (byte) 0, 783 (byte) -1, (byte) -11, (byte) -111, Byte.MIN_VALUE}; 784 byte[] d; 785 786 byte[] e = {}; 787 byte[] f; 788 789 // test write null 790 p = Parcel.obtain(); 791 p.writeByteArray(null); 792 p.setDataPosition(0); 793 b = p.createByteArray(); 794 assertNull(b); 795 p.recycle(); 796 797 // test write byte array with length: 0 798 p = Parcel.obtain(); 799 p.writeByteArray(e); 800 p.setDataPosition(0); 801 f = p.createByteArray(); 802 assertNotNull(f); 803 assertEquals(0, f.length); 804 p.recycle(); 805 806 // test write byte array with length: 1 807 p = Parcel.obtain(); 808 p.writeByteArray(a); 809 p.setDataPosition(0); 810 b = p.createByteArray(); 811 assertNotNull(b); 812 for (int i = 0; i < a.length; i++) { 813 assertEquals(a[i], b[i]); 814 } 815 p.recycle(); 816 817 // test write byte array with length: 9 818 p = Parcel.obtain(); 819 p.writeByteArray(c); 820 p.setDataPosition(0); 821 d = p.createByteArray(); 822 assertNotNull(d); 823 for (int i = 0; i < c.length; i++) { 824 assertEquals(c[i], d[i]); 825 } 826 p.recycle(); 827 } 828 testReadCharArray()829 public void testReadCharArray() { 830 Parcel p; 831 832 char[] a = {'a'}; 833 char[] b = new char[a.length]; 834 835 char[] c = {'a', Character.MAX_VALUE, Character.MIN_VALUE, Character.MAX_SURROGATE, Character.MIN_SURROGATE, 836 Character.MAX_HIGH_SURROGATE, Character.MAX_LOW_SURROGATE, 837 Character.MIN_HIGH_SURROGATE, Character.MIN_LOW_SURROGATE}; 838 char[] d = new char[c.length]; 839 840 // test write null 841 p = Parcel.obtain(); 842 p.writeCharArray(null); 843 p.setDataPosition(0); 844 try { 845 p.readCharArray(null); 846 fail("Should throw a RuntimeException"); 847 } catch (RuntimeException e) { 848 //expected 849 } 850 851 p.setDataPosition(0); 852 try { 853 p.readCharArray(b); 854 fail("Should throw a RuntimeException"); 855 } catch (RuntimeException e) { 856 //expected 857 } 858 p.recycle(); 859 860 // test write char array with length: 1 861 p = Parcel.obtain(); 862 p.writeCharArray(a); 863 p.setDataPosition(0); 864 try { 865 p.readCharArray(d); 866 fail("Should throw a RuntimeException"); 867 } catch (RuntimeException e) { 868 //expected 869 } 870 871 p.setDataPosition(0); 872 p.readCharArray(b); 873 for (int i = 0; i < a.length; i++) { 874 assertEquals(a[i], b[i]); 875 } 876 p.recycle(); 877 878 // test write char array with length: 9 879 p = Parcel.obtain(); 880 p.writeCharArray(c); 881 p.setDataPosition(0); 882 try { 883 p.readCharArray(b); 884 fail("Should throw a RuntimeException"); 885 } catch (RuntimeException e) { 886 //expected 887 } 888 889 p.setDataPosition(0); 890 p.readCharArray(d); 891 for (int i = 0; i < c.length; i++) { 892 assertEquals(c[i], d[i]); 893 } 894 p.recycle(); 895 } 896 testCreateCharArray()897 public void testCreateCharArray() { 898 Parcel p; 899 900 char[] a = {'a'}; 901 char[] b; 902 903 char[] c = {'a', Character.MAX_VALUE, Character.MIN_VALUE, Character.MAX_SURROGATE, Character.MIN_SURROGATE, 904 Character.MAX_HIGH_SURROGATE, Character.MAX_LOW_SURROGATE, 905 Character.MIN_HIGH_SURROGATE, Character.MIN_LOW_SURROGATE}; 906 char[] d; 907 908 char[] e = {}; 909 char[] f; 910 911 // test write null 912 p = Parcel.obtain(); 913 p.writeCharArray(null); 914 p.setDataPosition(0); 915 b = p.createCharArray(); 916 assertNull(b); 917 p.recycle(); 918 919 // test write char array with length: 1 920 p = Parcel.obtain(); 921 p.writeCharArray(e); 922 p.setDataPosition(0); 923 f = p.createCharArray(); 924 assertNotNull(e); 925 assertEquals(0, f.length); 926 p.recycle(); 927 928 // test write char array with length: 1 929 p = Parcel.obtain(); 930 p.writeCharArray(a); 931 p.setDataPosition(0); 932 b = p.createCharArray(); 933 assertNotNull(b); 934 for (int i = 0; i < a.length; i++) { 935 assertEquals(a[i], b[i]); 936 } 937 p.recycle(); 938 939 // test write char array with length: 9 940 p = Parcel.obtain(); 941 p.writeCharArray(c); 942 p.setDataPosition(0); 943 d = p.createCharArray(); 944 assertNotNull(d); 945 for (int i = 0; i < c.length; i++) { 946 assertEquals(c[i], d[i]); 947 } 948 p.recycle(); 949 } 950 testReadInt()951 public void testReadInt() { 952 Parcel p; 953 954 p = Parcel.obtain(); 955 p.writeInt(0); 956 p.setDataPosition(0); 957 assertEquals(0, p.readInt()); 958 p.recycle(); 959 960 p = Parcel.obtain(); 961 p.writeInt(1); 962 p.setDataPosition(0); 963 assertEquals(1, p.readInt()); 964 p.recycle(); 965 966 p = Parcel.obtain(); 967 p.writeInt(-1); 968 p.setDataPosition(0); 969 assertEquals(-1, p.readInt()); 970 p.recycle(); 971 972 p = Parcel.obtain(); 973 p.writeInt(Integer.MAX_VALUE); 974 p.setDataPosition(0); 975 assertEquals(Integer.MAX_VALUE, p.readInt()); 976 p.recycle(); 977 978 p = Parcel.obtain(); 979 p.writeInt(Integer.MIN_VALUE); 980 p.setDataPosition(0); 981 assertEquals(Integer.MIN_VALUE, p.readInt()); 982 p.recycle(); 983 984 p = Parcel.obtain(); 985 p.writeInt(Integer.MAX_VALUE); 986 p.writeInt(11); 987 p.writeInt(1); 988 p.writeInt(0); 989 p.writeInt(-1); 990 p.writeInt(-11); 991 p.writeInt(Integer.MIN_VALUE); 992 p.setDataPosition(0); 993 assertEquals(Integer.MAX_VALUE, p.readInt()); 994 assertEquals(11, p.readInt()); 995 assertEquals(1, p.readInt()); 996 assertEquals(0, p.readInt()); 997 assertEquals(-1, p.readInt()); 998 assertEquals(-11, p.readInt()); 999 assertEquals(Integer.MIN_VALUE, p.readInt()); 1000 p.recycle(); 1001 } 1002 testReadIntArray()1003 public void testReadIntArray() { 1004 Parcel p; 1005 1006 int[] a = {21}; 1007 int[] b = new int[a.length]; 1008 1009 int[] c = {Integer.MAX_VALUE, 111, 11, 1, 0, -1, -11, -111, Integer.MIN_VALUE}; 1010 int[] d = new int[c.length]; 1011 1012 // test write null 1013 p = Parcel.obtain(); 1014 p.writeIntArray(null); 1015 p.setDataPosition(0); 1016 try { 1017 p.readIntArray(null); 1018 fail("Should throw a RuntimeException"); 1019 } catch (RuntimeException e) { 1020 //expected 1021 } 1022 1023 p.setDataPosition(0); 1024 try { 1025 p.readIntArray(b); 1026 fail("Should throw a RuntimeException"); 1027 } catch (RuntimeException e) { 1028 //expected 1029 } 1030 p.recycle(); 1031 1032 // test write int array with length: 1 1033 p = Parcel.obtain(); 1034 p.writeIntArray(a); 1035 p.setDataPosition(0); 1036 try { 1037 p.readIntArray(d); 1038 fail("Should throw a RuntimeException"); 1039 } catch (RuntimeException e) { 1040 //expected 1041 } 1042 1043 p.setDataPosition(0); 1044 p.readIntArray(b); 1045 for (int i = 0; i < a.length; i++) { 1046 assertEquals(a[i], b[i]); 1047 } 1048 p.recycle(); 1049 1050 // test write int array with length: 9 1051 p = Parcel.obtain(); 1052 p.writeIntArray(c); 1053 p.setDataPosition(0); 1054 try { 1055 p.readIntArray(b); 1056 fail("Should throw a RuntimeException"); 1057 } catch (RuntimeException e) { 1058 //expected 1059 } 1060 1061 p.setDataPosition(0); 1062 p.readIntArray(d); 1063 for (int i = 0; i < c.length; i++) { 1064 assertEquals(c[i], d[i]); 1065 } 1066 p.recycle(); 1067 } 1068 testCreateIntArray()1069 public void testCreateIntArray() { 1070 Parcel p; 1071 1072 int[] a = {21}; 1073 int[] b; 1074 1075 int[] c = {Integer.MAX_VALUE, 111, 11, 1, 0, -1, -11, -111, Integer.MIN_VALUE}; 1076 int[] d; 1077 1078 int[] e = {}; 1079 int[] f; 1080 1081 // test write null 1082 p = Parcel.obtain(); 1083 p.writeIntArray(null); 1084 p.setDataPosition(0); 1085 b = p.createIntArray(); 1086 assertNull(b); 1087 p.recycle(); 1088 1089 // test write int array with length: 0 1090 p = Parcel.obtain(); 1091 p.writeIntArray(e); 1092 p.setDataPosition(0); 1093 f = p.createIntArray(); 1094 assertNotNull(e); 1095 assertEquals(0, f.length); 1096 p.recycle(); 1097 1098 // test write int array with length: 1 1099 p = Parcel.obtain(); 1100 p.writeIntArray(a); 1101 p.setDataPosition(0); 1102 b = p.createIntArray(); 1103 assertNotNull(b); 1104 for (int i = 0; i < a.length; i++) { 1105 assertEquals(a[i], b[i]); 1106 } 1107 p.recycle(); 1108 1109 // test write int array with length: 9 1110 p = Parcel.obtain(); 1111 p.writeIntArray(c); 1112 p.setDataPosition(0); 1113 d = p.createIntArray(); 1114 assertNotNull(d); 1115 for (int i = 0; i < c.length; i++) { 1116 assertEquals(c[i], d[i]); 1117 } 1118 p.recycle(); 1119 } 1120 testReadLong()1121 public void testReadLong() { 1122 Parcel p; 1123 1124 p = Parcel.obtain(); 1125 p.writeLong(0L); 1126 p.setDataPosition(0); 1127 assertEquals(0, p.readLong()); 1128 p.recycle(); 1129 1130 p = Parcel.obtain(); 1131 p.writeLong(1L); 1132 p.setDataPosition(0); 1133 assertEquals(1, p.readLong()); 1134 p.recycle(); 1135 1136 p = Parcel.obtain(); 1137 p.writeLong(-1L); 1138 p.setDataPosition(0); 1139 assertEquals(-1L, p.readLong()); 1140 p.recycle(); 1141 1142 p = Parcel.obtain(); 1143 p.writeLong(Long.MAX_VALUE); 1144 p.writeLong(11L); 1145 p.writeLong(1L); 1146 p.writeLong(0L); 1147 p.writeLong(-1L); 1148 p.writeLong(-11L); 1149 p.writeLong(Long.MIN_VALUE); 1150 p.setDataPosition(0); 1151 assertEquals(Long.MAX_VALUE, p.readLong()); 1152 assertEquals(11L, p.readLong()); 1153 assertEquals(1L, p.readLong()); 1154 assertEquals(0L, p.readLong()); 1155 assertEquals(-1L, p.readLong()); 1156 assertEquals(-11L, p.readLong()); 1157 assertEquals(Long.MIN_VALUE, p.readLong()); 1158 p.recycle(); 1159 } 1160 testReadLongArray()1161 public void testReadLongArray() { 1162 Parcel p; 1163 1164 long[] a = {21L}; 1165 long[] b = new long[a.length]; 1166 1167 long[] c = {Long.MAX_VALUE, 111L, 11L, 1L, 0L, -1L, -11L, -111L, Long.MIN_VALUE}; 1168 long[] d = new long[c.length]; 1169 1170 // test write null 1171 p = Parcel.obtain(); 1172 p.writeLongArray(null); 1173 p.setDataPosition(0); 1174 try { 1175 p.readLongArray(null); 1176 fail("Should throw a RuntimeException"); 1177 } catch (RuntimeException e) { 1178 //expected 1179 } 1180 1181 p.setDataPosition(0); 1182 try { 1183 p.readLongArray(b); 1184 fail("Should throw a RuntimeException"); 1185 } catch (RuntimeException e) { 1186 //expected 1187 } 1188 p.recycle(); 1189 1190 // test write long array with length: 1 1191 p = Parcel.obtain(); 1192 p.writeLongArray(a); 1193 p.setDataPosition(0); 1194 try { 1195 p.readLongArray(d); 1196 fail("Should throw a RuntimeException"); 1197 } catch (RuntimeException e) { 1198 //expected 1199 } 1200 1201 p.setDataPosition(0); 1202 p.readLongArray(b); 1203 for (int i = 0; i < a.length; i++) { 1204 assertEquals(a[i], b[i]); 1205 } 1206 p.recycle(); 1207 1208 // test write long array with length: 9 1209 p = Parcel.obtain(); 1210 p.writeLongArray(c); 1211 p.setDataPosition(0); 1212 try { 1213 p.readLongArray(b); 1214 fail("Should throw a RuntimeException"); 1215 } catch (RuntimeException e) { 1216 //expected 1217 } 1218 1219 p.setDataPosition(0); 1220 p.readLongArray(d); 1221 for (int i = 0; i < c.length; i++) { 1222 assertEquals(c[i], d[i]); 1223 } 1224 p.recycle(); 1225 } 1226 testCreateLongArray()1227 public void testCreateLongArray() { 1228 Parcel p; 1229 1230 long[] a = {21L}; 1231 long[] b; 1232 1233 long[] c = {Long.MAX_VALUE, 111L, 11L, 1L, 0L, -1L, -11L, -111L, Long.MIN_VALUE}; 1234 long[] d; 1235 1236 long[] e = {}; 1237 long[] f; 1238 1239 // test write null 1240 p = Parcel.obtain(); 1241 p.writeLongArray(null); 1242 p.setDataPosition(0); 1243 b = p.createLongArray(); 1244 assertNull(b); 1245 p.recycle(); 1246 1247 // test write long array with length: 0 1248 p = Parcel.obtain(); 1249 p.writeLongArray(e); 1250 p.setDataPosition(0); 1251 f = p.createLongArray(); 1252 assertNotNull(e); 1253 assertEquals(0, f.length); 1254 p.recycle(); 1255 1256 // test write long array with length: 1 1257 p = Parcel.obtain(); 1258 p.writeLongArray(a); 1259 p.setDataPosition(0); 1260 b = p.createLongArray(); 1261 assertNotNull(b); 1262 for (int i = 0; i < a.length; i++) { 1263 assertEquals(a[i], b[i]); 1264 } 1265 p.recycle(); 1266 1267 // test write long array with length: 9 1268 p = Parcel.obtain(); 1269 p.writeLongArray(c); 1270 p.setDataPosition(0); 1271 d = p.createLongArray(); 1272 assertNotNull(d); 1273 for (int i = 0; i < c.length; i++) { 1274 assertEquals(c[i], d[i]); 1275 } 1276 p.recycle(); 1277 } 1278 testReadFloat()1279 public void testReadFloat() { 1280 Parcel p; 1281 1282 p = Parcel.obtain(); 1283 p.writeFloat(.0f); 1284 p.setDataPosition(0); 1285 assertEquals(.0f, p.readFloat()); 1286 p.recycle(); 1287 1288 p = Parcel.obtain(); 1289 p.writeFloat(0.1f); 1290 p.setDataPosition(0); 1291 assertEquals(0.1f, p.readFloat()); 1292 p.recycle(); 1293 1294 p = Parcel.obtain(); 1295 p.writeFloat(-1.1f); 1296 p.setDataPosition(0); 1297 assertEquals(-1.1f, p.readFloat()); 1298 p.recycle(); 1299 1300 p = Parcel.obtain(); 1301 p.writeFloat(Float.MAX_VALUE); 1302 p.setDataPosition(0); 1303 assertEquals(Float.MAX_VALUE, p.readFloat()); 1304 p.recycle(); 1305 1306 p = Parcel.obtain(); 1307 p.writeFloat(Float.MIN_VALUE); 1308 p.setDataPosition(0); 1309 assertEquals(Float.MIN_VALUE, p.readFloat()); 1310 p.recycle(); 1311 1312 p = Parcel.obtain(); 1313 p.writeFloat(Float.MAX_VALUE); 1314 p.writeFloat(1.1f); 1315 p.writeFloat(0.1f); 1316 p.writeFloat(.0f); 1317 p.writeFloat(-0.1f); 1318 p.writeFloat(-1.1f); 1319 p.writeFloat(Float.MIN_VALUE); 1320 p.setDataPosition(0); 1321 assertEquals(Float.MAX_VALUE, p.readFloat()); 1322 assertEquals(1.1f, p.readFloat()); 1323 assertEquals(0.1f, p.readFloat()); 1324 assertEquals(.0f, p.readFloat()); 1325 assertEquals(-0.1f, p.readFloat()); 1326 assertEquals(-1.1f, p.readFloat()); 1327 assertEquals(Float.MIN_VALUE, p.readFloat()); 1328 p.recycle(); 1329 } 1330 testReadFloatArray()1331 public void testReadFloatArray() { 1332 Parcel p; 1333 1334 float[] a = {2.1f}; 1335 float[] b = new float[a.length]; 1336 1337 float[] c = {Float.MAX_VALUE, 11.1f, 1.1f, 0.1f, .0f, -0.1f, -1.1f, -11.1f, Float.MIN_VALUE}; 1338 float[] d = new float[c.length]; 1339 1340 // test write null 1341 p = Parcel.obtain(); 1342 p.writeFloatArray(null); 1343 p.setDataPosition(0); 1344 try { 1345 p.readFloatArray(null); 1346 fail("Should throw a RuntimeException"); 1347 } catch (RuntimeException e) { 1348 //expected 1349 } 1350 1351 p.setDataPosition(0); 1352 try { 1353 p.readFloatArray(b); 1354 fail("Should throw a RuntimeException"); 1355 } catch (RuntimeException e) { 1356 //expected 1357 } 1358 p.recycle(); 1359 1360 // test write float array with length: 1 1361 p = Parcel.obtain(); 1362 p.writeFloatArray(a); 1363 p.setDataPosition(0); 1364 try { 1365 p.readFloatArray(d); 1366 fail("Should throw a RuntimeException"); 1367 } catch (RuntimeException e) { 1368 //expected 1369 } 1370 1371 p.setDataPosition(0); 1372 p.readFloatArray(b); 1373 for (int i = 0; i < a.length; i++) { 1374 assertEquals(a[i], b[i]); 1375 } 1376 p.recycle(); 1377 1378 // test write float array with length: 9 1379 p = Parcel.obtain(); 1380 p.writeFloatArray(c); 1381 p.setDataPosition(0); 1382 try { 1383 p.readFloatArray(b); 1384 fail("Should throw a RuntimeException"); 1385 } catch (RuntimeException e) { 1386 //expected 1387 } 1388 1389 p.setDataPosition(0); 1390 p.readFloatArray(d); 1391 for (int i = 0; i < c.length; i++) { 1392 assertEquals(c[i], d[i]); 1393 } 1394 p.recycle(); 1395 } 1396 testCreateFloatArray()1397 public void testCreateFloatArray() { 1398 Parcel p; 1399 1400 float[] a = {2.1f}; 1401 float[] b; 1402 1403 float[] c = {Float.MAX_VALUE, 11.1f, 1.1f, 0.1f, .0f, -0.1f, -1.1f, -11.1f, Float.MIN_VALUE}; 1404 float[] d; 1405 1406 float[] e = {}; 1407 float[] f; 1408 1409 // test write null 1410 p = Parcel.obtain(); 1411 p.writeFloatArray(null); 1412 p.setDataPosition(0); 1413 b = p.createFloatArray(); 1414 assertNull(b); 1415 p.recycle(); 1416 1417 // test write float array with length: 0 1418 p = Parcel.obtain(); 1419 p.writeFloatArray(e); 1420 p.setDataPosition(0); 1421 f = p.createFloatArray(); 1422 assertNotNull(f); 1423 assertEquals(0, f.length); 1424 p.recycle(); 1425 1426 // test write float array with length: 1 1427 p = Parcel.obtain(); 1428 p.writeFloatArray(a); 1429 p.setDataPosition(0); 1430 b = p.createFloatArray(); 1431 assertNotNull(b); 1432 for (int i = 0; i < a.length; i++) { 1433 assertEquals(a[i], b[i]); 1434 } 1435 p.recycle(); 1436 1437 // test write float array with length: 9 1438 p = Parcel.obtain(); 1439 p.writeFloatArray(c); 1440 p.setDataPosition(0); 1441 d = p.createFloatArray(); 1442 assertNotNull(d); 1443 for (int i = 0; i < c.length; i++) { 1444 assertEquals(c[i], d[i]); 1445 } 1446 p.recycle(); 1447 } 1448 testReadDouble()1449 public void testReadDouble() { 1450 Parcel p; 1451 1452 p = Parcel.obtain(); 1453 p.writeDouble(.0d); 1454 p.setDataPosition(0); 1455 assertEquals(.0d, p.readDouble()); 1456 p.recycle(); 1457 1458 p = Parcel.obtain(); 1459 p.writeDouble(0.1d); 1460 p.setDataPosition(0); 1461 assertEquals(0.1d, p.readDouble()); 1462 p.recycle(); 1463 1464 p = Parcel.obtain(); 1465 p.writeDouble(-1.1d); 1466 p.setDataPosition(0); 1467 assertEquals(-1.1d, p.readDouble()); 1468 p.recycle(); 1469 1470 p = Parcel.obtain(); 1471 p.writeDouble(Double.MAX_VALUE); 1472 p.setDataPosition(0); 1473 assertEquals(Double.MAX_VALUE, p.readDouble()); 1474 p.recycle(); 1475 1476 p = Parcel.obtain(); 1477 p.writeDouble(Double.MIN_VALUE); 1478 p.setDataPosition(0); 1479 assertEquals(Double.MIN_VALUE, p.readDouble()); 1480 p.recycle(); 1481 1482 p = Parcel.obtain(); 1483 p.writeDouble(Double.MAX_VALUE); 1484 p.writeDouble(1.1d); 1485 p.writeDouble(0.1d); 1486 p.writeDouble(.0d); 1487 p.writeDouble(-0.1d); 1488 p.writeDouble(-1.1d); 1489 p.writeDouble(Double.MIN_VALUE); 1490 p.setDataPosition(0); 1491 assertEquals(Double.MAX_VALUE, p.readDouble()); 1492 assertEquals(1.1d, p.readDouble()); 1493 assertEquals(0.1d, p.readDouble()); 1494 assertEquals(.0d, p.readDouble()); 1495 assertEquals(-0.1d, p.readDouble()); 1496 assertEquals(-1.1d, p.readDouble()); 1497 assertEquals(Double.MIN_VALUE, p.readDouble()); 1498 p.recycle(); 1499 } 1500 testReadDoubleArray()1501 public void testReadDoubleArray() { 1502 Parcel p; 1503 1504 double[] a = {2.1d}; 1505 double[] b = new double[a.length]; 1506 1507 double[] c = {Double.MAX_VALUE, 11.1d, 1.1d, 0.1d, .0d, -0.1d, -1.1d, -11.1d, Double.MIN_VALUE}; 1508 double[] d = new double[c.length]; 1509 1510 // test write null 1511 p = Parcel.obtain(); 1512 p.writeDoubleArray(null); 1513 p.setDataPosition(0); 1514 try { 1515 p.readDoubleArray(null); 1516 fail("Should throw a RuntimeException"); 1517 } catch (RuntimeException e) { 1518 //expected 1519 } 1520 1521 p.setDataPosition(0); 1522 try { 1523 p.readDoubleArray(b); 1524 fail("Should throw a RuntimeException"); 1525 } catch (RuntimeException e) { 1526 //expected 1527 } 1528 p.recycle(); 1529 1530 // test write double array with length: 1 1531 p = Parcel.obtain(); 1532 p.writeDoubleArray(a); 1533 p.setDataPosition(0); 1534 try { 1535 p.readDoubleArray(d); 1536 fail("Should throw a RuntimeException"); 1537 } catch (RuntimeException e) { 1538 //expected 1539 } 1540 1541 p.setDataPosition(0); 1542 p.readDoubleArray(b); 1543 for (int i = 0; i < a.length; i++) { 1544 assertEquals(a[i], b[i]); 1545 } 1546 p.recycle(); 1547 1548 // test write double array with length: 9 1549 p = Parcel.obtain(); 1550 p.writeDoubleArray(c); 1551 p.setDataPosition(0); 1552 try { 1553 p.readDoubleArray(b); 1554 fail("Should throw a RuntimeException"); 1555 } catch (RuntimeException e) { 1556 //expected 1557 } 1558 1559 p.setDataPosition(0); 1560 p.readDoubleArray(d); 1561 for (int i = 0; i < c.length; i++) { 1562 assertEquals(c[i], d[i]); 1563 } 1564 p.recycle(); 1565 } 1566 testCreateDoubleArray()1567 public void testCreateDoubleArray() { 1568 Parcel p; 1569 1570 double[] a = {2.1d}; 1571 double[] b; 1572 1573 double[] c = { 1574 Double.MAX_VALUE, 11.1d, 1.1d, 0.1d, .0d, -0.1d, -1.1d, -11.1d, Double.MIN_VALUE 1575 }; 1576 double[] d; 1577 1578 double[] e = {}; 1579 double[] f; 1580 1581 // test write null 1582 p = Parcel.obtain(); 1583 p.writeDoubleArray(null); 1584 p.setDataPosition(0); 1585 b = p.createDoubleArray(); 1586 assertNull(b); 1587 p.recycle(); 1588 1589 // test write double array with length: 0 1590 p = Parcel.obtain(); 1591 p.writeDoubleArray(e); 1592 p.setDataPosition(0); 1593 f = p.createDoubleArray(); 1594 assertNotNull(f); 1595 assertEquals(0, f.length); 1596 p.recycle(); 1597 1598 // test write double array with length: 1 1599 p = Parcel.obtain(); 1600 p.writeDoubleArray(a); 1601 p.setDataPosition(0); 1602 b = p.createDoubleArray(); 1603 assertNotNull(b); 1604 for (int i = 0; i < a.length; i++) { 1605 assertEquals(a[i], b[i]); 1606 } 1607 p.recycle(); 1608 1609 // test write double array with length: 9 1610 p = Parcel.obtain(); 1611 p.writeDoubleArray(c); 1612 p.setDataPosition(0); 1613 d = p.createDoubleArray(); 1614 assertNotNull(d); 1615 for (int i = 0; i < c.length; i++) { 1616 assertEquals(c[i], d[i]); 1617 } 1618 p.recycle(); 1619 } 1620 testReadBooleanArray()1621 public void testReadBooleanArray() { 1622 Parcel p; 1623 1624 boolean[] a = {true}; 1625 boolean[] b = new boolean[a.length]; 1626 1627 boolean[] c = {true, false, true, false}; 1628 boolean[] d = new boolean[c.length]; 1629 1630 // test write null 1631 p = Parcel.obtain(); 1632 p.writeBooleanArray(null); 1633 p.setDataPosition(0); 1634 try { 1635 p.readIntArray(null); 1636 fail("Should throw a RuntimeException"); 1637 } catch (RuntimeException e) { 1638 //expected 1639 } 1640 1641 p.setDataPosition(0); 1642 try { 1643 p.readBooleanArray(b); 1644 fail("Should throw a RuntimeException"); 1645 } catch (RuntimeException e) { 1646 //expected 1647 } 1648 p.recycle(); 1649 1650 // test write boolean array with length: 1 1651 p = Parcel.obtain(); 1652 p.writeBooleanArray(a); 1653 p.setDataPosition(0); 1654 try { 1655 p.readBooleanArray(d); 1656 fail("Should throw a RuntimeException"); 1657 } catch (RuntimeException e) { 1658 //expected 1659 } 1660 1661 p.setDataPosition(0); 1662 p.readBooleanArray(b); 1663 for (int i = 0; i < a.length; i++) { 1664 assertEquals(a[i], b[i]); 1665 } 1666 p.recycle(); 1667 1668 // test write boolean array with length: 4 1669 p = Parcel.obtain(); 1670 p.writeBooleanArray(c); 1671 p.setDataPosition(0); 1672 try { 1673 p.readBooleanArray(b); 1674 fail("Should throw a RuntimeException"); 1675 } catch (RuntimeException e) { 1676 //expected 1677 } 1678 1679 p.setDataPosition(0); 1680 p.readBooleanArray(d); 1681 for (int i = 0; i < c.length; i++) { 1682 assertEquals(c[i], d[i]); 1683 } 1684 p.recycle(); 1685 } 1686 testCreateBooleanArray()1687 public void testCreateBooleanArray() { 1688 Parcel p; 1689 1690 boolean[] a = {true}; 1691 boolean[] b; 1692 1693 boolean[] c = {true, false, true, false}; 1694 boolean[] d; 1695 1696 boolean[] e = {}; 1697 boolean[] f; 1698 1699 // test write null 1700 p = Parcel.obtain(); 1701 p.writeBooleanArray(null); 1702 p.setDataPosition(0); 1703 b = p.createBooleanArray(); 1704 assertNull(b); 1705 p.recycle(); 1706 1707 // test write boolean array with length: 0 1708 p = Parcel.obtain(); 1709 p.writeBooleanArray(e); 1710 p.setDataPosition(0); 1711 f = p.createBooleanArray(); 1712 assertNotNull(f); 1713 assertEquals(0, f.length); 1714 p.recycle(); 1715 1716 // test write boolean array with length: 1 1717 p = Parcel.obtain(); 1718 p.writeBooleanArray(a); 1719 1720 p.setDataPosition(0); 1721 b = p.createBooleanArray(); 1722 assertNotNull(b); 1723 for (int i = 0; i < a.length; i++) { 1724 assertEquals(a[i], b[i]); 1725 } 1726 p.recycle(); 1727 1728 // test write boolean array with length: 4 1729 p = Parcel.obtain(); 1730 p.writeBooleanArray(c); 1731 p.setDataPosition(0); 1732 d = p.createBooleanArray(); 1733 assertNotNull(d); 1734 for (int i = 0; i < c.length; i++) { 1735 assertEquals(c[i], d[i]); 1736 } 1737 p.recycle(); 1738 } 1739 testReadString()1740 public void testReadString() { 1741 Parcel p; 1742 final String string = "Hello, Android!"; 1743 1744 // test write null 1745 p = Parcel.obtain(); 1746 p.writeString(null); 1747 p.setDataPosition(0); 1748 assertNull(p.readString()); 1749 p.recycle(); 1750 1751 p = Parcel.obtain(); 1752 p.writeString(""); 1753 p.setDataPosition(0); 1754 assertEquals("", p.readString()); 1755 p.recycle(); 1756 1757 p = Parcel.obtain(); 1758 p.writeString("a"); 1759 p.setDataPosition(0); 1760 assertEquals("a", p.readString()); 1761 p.recycle(); 1762 1763 p = Parcel.obtain(); 1764 p.writeString(string); 1765 p.setDataPosition(0); 1766 assertEquals(string, p.readString()); 1767 p.recycle(); 1768 1769 p = Parcel.obtain(); 1770 p.writeString(string); 1771 p.writeString("a"); 1772 p.writeString(""); 1773 p.setDataPosition(0); 1774 assertEquals(string, p.readString()); 1775 assertEquals("a", p.readString()); 1776 assertEquals("", p.readString()); 1777 p.recycle(); 1778 } 1779 testReadStringArray()1780 public void testReadStringArray() { 1781 Parcel p; 1782 1783 String[] a = {"21"}; 1784 String[] b = new String[a.length]; 1785 1786 String[] c = {"", 1787 "a", 1788 "Hello, Android!", 1789 "A long string that is used to test the api readStringArray(),"}; 1790 String[] d = new String[c.length]; 1791 1792 // test write null 1793 p = Parcel.obtain(); 1794 p.writeStringArray(null); 1795 p.setDataPosition(0); 1796 try { 1797 p.readStringArray(null); 1798 fail("Should throw a RuntimeException"); 1799 } catch (RuntimeException e) { 1800 //expected 1801 } 1802 1803 p.setDataPosition(0); 1804 try { 1805 p.readStringArray(b); 1806 fail("Should throw a RuntimeException"); 1807 } catch (RuntimeException e) { 1808 //expected 1809 } 1810 p.recycle(); 1811 1812 // test write String array with length: 1 1813 p = Parcel.obtain(); 1814 p.writeStringArray(a); 1815 p.setDataPosition(0); 1816 try { 1817 p.readStringArray(d); 1818 fail("Should throw a RuntimeException"); 1819 } catch (RuntimeException e) { 1820 //expected 1821 } 1822 1823 p.setDataPosition(0); 1824 p.readStringArray(b); 1825 for (int i = 0; i < a.length; i++) { 1826 assertEquals(a[i], b[i]); 1827 } 1828 p.recycle(); 1829 1830 // test write String array with length: 9 1831 p = Parcel.obtain(); 1832 p.writeStringArray(c); 1833 p.setDataPosition(0); 1834 try { 1835 p.readStringArray(b); 1836 fail("Should throw a RuntimeException"); 1837 } catch (RuntimeException e) { 1838 //expected 1839 } 1840 1841 p.setDataPosition(0); 1842 p.readStringArray(d); 1843 for (int i = 0; i < c.length; i++) { 1844 assertEquals(c[i], d[i]); 1845 } 1846 p.recycle(); 1847 } 1848 testCreateStringArray()1849 public void testCreateStringArray() { 1850 Parcel p; 1851 1852 String[] a = {"21"}; 1853 String[] b; 1854 1855 String[] c = {"", 1856 "a", 1857 "Hello, Android!", 1858 "A long string that is used to test the api readStringArray(),"}; 1859 String[] d; 1860 1861 String[] e = {}; 1862 String[] f; 1863 1864 // test write null 1865 p = Parcel.obtain(); 1866 p.writeStringArray(null); 1867 p.setDataPosition(0); 1868 b = p.createStringArray(); 1869 assertNull(b); 1870 p.recycle(); 1871 1872 // test write String array with length: 0 1873 p = Parcel.obtain(); 1874 p.writeStringArray(e); 1875 p.setDataPosition(0); 1876 f = p.createStringArray(); 1877 assertNotNull(e); 1878 assertEquals(0, f.length); 1879 p.recycle(); 1880 1881 // test write String array with length: 1 1882 p = Parcel.obtain(); 1883 p.writeStringArray(a); 1884 p.setDataPosition(0); 1885 b = p.createStringArray(); 1886 assertNotNull(b); 1887 for (int i = 0; i < a.length; i++) { 1888 assertEquals(a[i], b[i]); 1889 } 1890 p.recycle(); 1891 1892 // test write String array with length: 9 1893 p = Parcel.obtain(); 1894 p.writeStringArray(c); 1895 p.setDataPosition(0); 1896 d = p.createStringArray(); 1897 assertNotNull(d); 1898 for (int i = 0; i < c.length; i++) { 1899 assertEquals(c[i], d[i]); 1900 } 1901 p.recycle(); 1902 } 1903 testReadStringList()1904 public void testReadStringList() { 1905 Parcel p; 1906 1907 ArrayList<String> a = new ArrayList<String>(); 1908 a.add("21"); 1909 ArrayList<String> b = new ArrayList<String>(); 1910 1911 ArrayList<String> c = new ArrayList<String>(); 1912 c.add(""); 1913 c.add("a"); 1914 c.add("Hello, Android!"); 1915 c.add("A long string that is used to test the api readStringList(),"); 1916 ArrayList<String> d = new ArrayList<String>(); 1917 1918 // test write null 1919 p = Parcel.obtain(); 1920 p.writeStringList(null); 1921 p.setDataPosition(0); 1922 try { 1923 p.readStringList(null); 1924 fail("Should throw a RuntimeException"); 1925 } catch (RuntimeException e) { 1926 //expected 1927 } 1928 1929 p.setDataPosition(0); 1930 p.readStringList(b); 1931 assertTrue(0 == b.size()); 1932 p.recycle(); 1933 1934 // test write String array with length: 1 1935 p = Parcel.obtain(); 1936 p.writeStringList(a); 1937 p.setDataPosition(0); 1938 assertTrue(c.size() > a.size()); 1939 p.readStringList(c); 1940 assertTrue(c.size() == a.size()); 1941 assertEquals(a, c); 1942 1943 p.setDataPosition(0); 1944 assertTrue(0 == b.size() && 0 != a.size()); 1945 p.readStringList(b); 1946 assertEquals(a, b); 1947 p.recycle(); 1948 1949 c = new ArrayList<String>(); 1950 c.add(""); 1951 c.add("a"); 1952 c.add("Hello, Android!"); 1953 c.add("A long string that is used to test the api readStringList(),"); 1954 // test write String array with length: 4 1955 p = Parcel.obtain(); 1956 p.writeStringList(c); 1957 p.setDataPosition(0); 1958 1959 assertTrue(b.size() < c.size()); 1960 p.readStringList(b); 1961 assertTrue(b.size() == c.size()); 1962 assertEquals(c, b); 1963 1964 p.setDataPosition(0); 1965 assertTrue(d.size() < c.size()); 1966 p.readStringList(d); 1967 assertEquals(c, d); 1968 p.recycle(); 1969 } 1970 1971 public void testCreateStringArrayList() { 1972 Parcel p; 1973 1974 ArrayList<String> a = new ArrayList<String>(); 1975 a.add("21"); 1976 ArrayList<String> b; 1977 1978 ArrayList<String> c = new ArrayList<String>(); 1979 c.add(""); 1980 c.add("a"); 1981 c.add("Hello, Android!"); 1982 c.add("A long string that is used to test the api readStringList(),"); 1983 ArrayList<String> d; 1984 1985 ArrayList<String> e = new ArrayList<String>(); 1986 ArrayList<String> f = null; 1987 1988 // test write null 1989 p = Parcel.obtain(); 1990 p.writeStringList(null); 1991 p.setDataPosition(0); 1992 b = p.createStringArrayList(); 1993 assertNull(b); 1994 p.recycle(); 1995 1996 // test write String array with length: 0 1997 p = Parcel.obtain(); 1998 p.writeStringList(e); 1999 p.setDataPosition(0); 2000 assertNull(f); 2001 f = p.createStringArrayList(); 2002 assertNotNull(f); 2003 p.recycle(); 2004 2005 // test write String array with length: 1 2006 p = Parcel.obtain(); 2007 p.writeStringList(a); 2008 p.setDataPosition(0); 2009 b = p.createStringArrayList(); 2010 assertEquals(a, b); 2011 p.recycle(); 2012 2013 // test write String array with length: 4 2014 p = Parcel.obtain(); 2015 p.writeStringList(c); 2016 p.setDataPosition(0); 2017 d = p.createStringArrayList(); 2018 assertEquals(c, d); 2019 p.recycle(); 2020 } 2021 2022 public void testReadSerializable() { 2023 Parcel p; 2024 2025 // test write null 2026 p = Parcel.obtain(); 2027 p.writeSerializable(null); 2028 p.setDataPosition(0); 2029 assertNull(p.readSerializable()); 2030 p.recycle(); 2031 2032 p = Parcel.obtain(); 2033 p.writeSerializable("Hello, Android!"); 2034 p.setDataPosition(0); 2035 assertEquals("Hello, Android!", p.readSerializable()); 2036 p.recycle(); 2037 } 2038 2039 public void testReadParcelable() { 2040 Parcel p; 2041 MockClassLoader mcl = new MockClassLoader(); 2042 final String signatureString = "1234567890abcdef"; 2043 Signature s = new Signature(signatureString); 2044 2045 // test write null 2046 p = Parcel.obtain(); 2047 p.writeParcelable(null, 0); 2048 p.setDataPosition(0); 2049 assertNull(p.readParcelable(mcl)); 2050 p.recycle(); 2051 2052 p = Parcel.obtain(); 2053 p.writeParcelable(s, 0); 2054 p.setDataPosition(0); 2055 assertEquals(s, p.readParcelable(mcl)); 2056 p.recycle(); 2057 } 2058 2059 public void testReadParcelableCreator() { 2060 MockClassLoader mcl = new MockClassLoader(); 2061 final String signatureString = "1234567890abcdef"; 2062 Signature s = new Signature(signatureString); 2063 2064 Parcel p = Parcel.obtain(); 2065 p.writeParcelableCreator(s); 2066 p.setDataPosition(0); 2067 assertSame(Signature.CREATOR, p.readParcelableCreator(mcl)); 2068 p.recycle(); 2069 } 2070 2071 public void testReadParcelableArray() { 2072 Parcel p; 2073 MockClassLoader mcl = new MockClassLoader(); 2074 Signature[] s = {new Signature("1234"), 2075 new Signature("ABCD"), 2076 new Signature("abcd")}; 2077 2078 Signature[] s2 = {new Signature("1234"), 2079 null, 2080 new Signature("abcd")}; 2081 Parcelable[] s3; 2082 2083 // test write null 2084 p = Parcel.obtain(); 2085 p.writeParcelableArray(null, 0); 2086 p.setDataPosition(0); 2087 assertNull(p.readParcelableArray(mcl)); 2088 p.recycle(); 2089 2090 p = Parcel.obtain(); 2091 p.writeParcelableArray(s, 0); 2092 p.setDataPosition(0); 2093 s3 = p.readParcelableArray(mcl); 2094 for (int i = 0; i < s.length; i++) { 2095 assertEquals(s[i], s3[i]); 2096 } 2097 p.recycle(); 2098 2099 p = Parcel.obtain(); 2100 p.writeParcelableArray(s2, 0); 2101 p.setDataPosition(0); 2102 s3 = p.readParcelableArray(mcl); 2103 for (int i = 0; i < s2.length; i++) { 2104 assertEquals(s2[i], s3[i]); 2105 } 2106 p.recycle(); 2107 } 2108 2109 public void testReadTypedArray() { 2110 Parcel p; 2111 Signature[] s = {new Signature("1234"), 2112 new Signature("ABCD"), 2113 new Signature("abcd")}; 2114 2115 Signature[] s2 = {new Signature("1234"), 2116 null, 2117 new Signature("abcd")}; 2118 Signature[] s3 = new Signature[3]; 2119 Signature[] s4 = new Signature[4]; 2120 2121 // test write null 2122 p = Parcel.obtain(); 2123 p.writeTypedArray(null, 0); 2124 p.setDataPosition(0); 2125 try { 2126 p.readTypedArray(s3, Signature.CREATOR); 2127 fail("should throw a RuntimeException"); 2128 } catch (RuntimeException e) { 2129 //expected 2130 } 2131 2132 p.setDataPosition(0); 2133 try { 2134 p.readTypedArray(null, Signature.CREATOR); 2135 fail("should throw a RuntimeException"); 2136 } catch (RuntimeException e) { 2137 //expected 2138 } 2139 p.recycle(); 2140 2141 // test write not null 2142 p = Parcel.obtain(); 2143 p.writeTypedArray(s, 0); 2144 p.setDataPosition(0); 2145 p.readTypedArray(s3, Signature.CREATOR); 2146 for (int i = 0; i < s.length; i++) { 2147 assertEquals(s[i], s3[i]); 2148 } 2149 2150 p.setDataPosition(0); 2151 try { 2152 p.readTypedArray(null, Signature.CREATOR); 2153 fail("should throw a RuntimeException"); 2154 } catch (RuntimeException e) { 2155 //expected 2156 } 2157 2158 p.setDataPosition(0); 2159 try { 2160 p.readTypedArray(s4, Signature.CREATOR); 2161 fail("should throw a RuntimeException"); 2162 } catch (RuntimeException e) { 2163 //expected 2164 } 2165 p.recycle(); 2166 2167 s3 = new Signature[s2.length]; 2168 p = Parcel.obtain(); 2169 p.writeTypedArray(s2, 0); 2170 p.setDataPosition(0); 2171 p.readTypedArray(s3, Signature.CREATOR); 2172 for (int i = 0; i < s.length; i++) { 2173 assertEquals(s2[i], s3[i]); 2174 } 2175 p.recycle(); 2176 } 2177 2178 public void testReadTypedArray2() { 2179 Parcel p; 2180 Signature[] s = { 2181 new Signature("1234"), new Signature("ABCD"), new Signature("abcd") 2182 }; 2183 2184 Signature[] s2 = { 2185 new Signature("1234"), null, new Signature("abcd") 2186 }; 2187 Signature[] s3 = { 2188 null, null, null 2189 }; 2190 2191 // test write null 2192 p = Parcel.obtain(); 2193 p.writeTypedArray(null, 0); 2194 p.setDataPosition(0); 2195 p.recycle(); 2196 2197 // test write not null 2198 p = Parcel.obtain(); 2199 p.writeTypedArray(s, 0); 2200 p.setDataPosition(0); 2201 p.readTypedArray(s3, Signature.CREATOR); 2202 for (int i = 0; i < s.length; i++) { 2203 assertEquals(s[i], s3[i]); 2204 } 2205 p.recycle(); 2206 2207 p = Parcel.obtain(); 2208 p.writeTypedArray(s2, 0); 2209 p.setDataPosition(0); 2210 p.readTypedArray(s3, Signature.CREATOR); 2211 for (int i = 0; i < s.length; i++) { 2212 assertEquals(s2[i], s3[i]); 2213 } 2214 p.recycle(); 2215 } 2216 2217 public void testCreateTypedArray() { 2218 Parcel p; 2219 Signature[] s = {new Signature("1234"), 2220 new Signature("ABCD"), 2221 new Signature("abcd")}; 2222 2223 Signature[] s2 = {new Signature("1234"), 2224 null, 2225 new Signature("abcd")}; 2226 Signature[] s3; 2227 2228 // test write null 2229 p = Parcel.obtain(); 2230 p.writeTypedArray(null, 0); 2231 p.setDataPosition(0); 2232 assertNull(p.createTypedArray(Signature.CREATOR)); 2233 p.recycle(); 2234 2235 // test write not null 2236 p = Parcel.obtain(); 2237 p.writeTypedArray(s, 0); 2238 p.setDataPosition(0); 2239 s3 = p.createTypedArray(Signature.CREATOR); 2240 for (int i = 0; i < s.length; i++) { 2241 assertEquals(s[i], s3[i]); 2242 } 2243 p.recycle(); 2244 2245 p = Parcel.obtain(); 2246 p.writeTypedArray(s2, 0); 2247 p.setDataPosition(0); 2248 s3 = p.createTypedArray(Signature.CREATOR); 2249 for (int i = 0; i < s.length; i++) { 2250 assertEquals(s2[i], s3[i]); 2251 } 2252 p.recycle(); 2253 } 2254 2255 public void testReadTypedList() { 2256 Parcel p; 2257 ArrayList<Signature> s = new ArrayList<Signature>(); 2258 s.add(new Signature("1234")); 2259 s.add(new Signature("ABCD")); 2260 s.add(new Signature("abcd")); 2261 2262 ArrayList<Signature> s2 = new ArrayList<Signature>(); 2263 s2.add(new Signature("1234")); 2264 s2.add(null); 2265 2266 ArrayList<Signature> s3 = new ArrayList<Signature>(); 2267 2268 // test write null 2269 p = Parcel.obtain(); 2270 p.writeTypedList(null); 2271 p.setDataPosition(0); 2272 p.readTypedList(s3, Signature.CREATOR); 2273 assertEquals(0, s3.size()); 2274 2275 p.setDataPosition(0); 2276 try { 2277 p.readTypedList(null, Signature.CREATOR); 2278 fail("should throw a RuntimeException"); 2279 } catch (RuntimeException e) { 2280 //expected 2281 } 2282 p.recycle(); 2283 2284 // test write not null 2285 p = Parcel.obtain(); 2286 p.writeTypedList(s); 2287 p.setDataPosition(0); 2288 p.readTypedList(s3, Signature.CREATOR); 2289 for (int i = 0; i < s.size(); i++) { 2290 assertEquals(s.get(i), s3.get(i)); 2291 } 2292 2293 p.setDataPosition(0); 2294 try { 2295 p.readTypedList(null, Signature.CREATOR); 2296 fail("should throw a RuntimeException"); 2297 } catch (RuntimeException e) { 2298 //expected 2299 } 2300 2301 p.setDataPosition(0); 2302 p.readTypedList(s2, Signature.CREATOR); 2303 assertEquals(s.size(), s2.size()); 2304 for (int i = 0; i < s.size(); i++) { 2305 assertEquals(s.get(i), s2.get(i)); 2306 } 2307 p.recycle(); 2308 2309 s2 = new ArrayList<Signature>(); 2310 s2.add(new Signature("1234")); 2311 s2.add(null); 2312 p = Parcel.obtain(); 2313 p.writeTypedList(s2); 2314 p.setDataPosition(0); 2315 p.readTypedList(s3, Signature.CREATOR); 2316 assertEquals(s3.size(), s2.size()); 2317 for (int i = 0; i < s2.size(); i++) { 2318 assertEquals(s2.get(i), s3.get(i)); 2319 } 2320 p.recycle(); 2321 } 2322 2323 public void testCreateTypedArrayList() { 2324 Parcel p; 2325 ArrayList<Signature> s = new ArrayList<Signature>(); 2326 s.add(new Signature("1234")); 2327 s.add(new Signature("ABCD")); 2328 s.add(new Signature("abcd")); 2329 2330 ArrayList<Signature> s2 = new ArrayList<Signature>(); 2331 s2.add(new Signature("1234")); 2332 s2.add(null); 2333 2334 ArrayList<Signature> s3; 2335 2336 // test write null 2337 p = Parcel.obtain(); 2338 p.writeTypedList(null); 2339 p.setDataPosition(0); 2340 assertNull(p.createTypedArrayList(Signature.CREATOR)); 2341 p.recycle(); 2342 2343 // test write not null 2344 p = Parcel.obtain(); 2345 p.writeTypedList(s); 2346 p.setDataPosition(0); 2347 s3 = p.createTypedArrayList(Signature.CREATOR); 2348 for (int i = 0; i < s.size(); i++) { 2349 assertEquals(s.get(i), s3.get(i)); 2350 } 2351 2352 p = Parcel.obtain(); 2353 p.writeTypedList(s2); 2354 p.setDataPosition(0); 2355 s3 = p.createTypedArrayList(Signature.CREATOR); 2356 assertEquals(s3.size(), s2.size()); 2357 for (int i = 0; i < s2.size(); i++) { 2358 assertEquals(s2.get(i), s3.get(i)); 2359 } 2360 p.recycle(); 2361 } 2362 2363 public void testReadException() { 2364 } 2365 2366 public void testReadException2() { 2367 Parcel p = Parcel.obtain(); 2368 String msg = "testReadException2"; 2369 2370 p.writeException(new SecurityException(msg)); 2371 p.setDataPosition(0); 2372 try { 2373 p.readException(); 2374 fail("Should throw a SecurityException"); 2375 } catch (SecurityException e) { 2376 assertEquals(msg, e.getMessage()); 2377 } 2378 2379 p.setDataPosition(0); 2380 p.writeException(new BadParcelableException(msg)); 2381 p.setDataPosition(0); 2382 try { 2383 p.readException(); 2384 fail("Should throw a BadParcelableException"); 2385 } catch (BadParcelableException e) { 2386 assertEquals(msg, e.getMessage()); 2387 } 2388 2389 p.setDataPosition(0); 2390 p.writeException(new IllegalArgumentException(msg)); 2391 p.setDataPosition(0); 2392 try { 2393 p.readException(); 2394 fail("Should throw an IllegalArgumentException"); 2395 } catch (IllegalArgumentException e) { 2396 assertEquals(msg, e.getMessage()); 2397 } 2398 2399 p.setDataPosition(0); 2400 p.writeException(new NullPointerException(msg)); 2401 p.setDataPosition(0); 2402 try { 2403 p.readException(); 2404 fail("Should throw a NullPointerException"); 2405 } catch (NullPointerException e) { 2406 assertEquals(msg, e.getMessage()); 2407 } 2408 2409 p.setDataPosition(0); 2410 p.writeException(new IllegalStateException(msg)); 2411 p.setDataPosition(0); 2412 try { 2413 p.readException(); 2414 fail("Should throw an IllegalStateException"); 2415 } catch (IllegalStateException e) { 2416 assertEquals(msg, e.getMessage()); 2417 } 2418 2419 p.setDataPosition(0); 2420 try { 2421 p.writeException(new RuntimeException()); 2422 fail("Should throw an IllegalStateException"); 2423 } catch (RuntimeException e) { 2424 //expected 2425 } 2426 p.recycle(); 2427 } 2428 2429 public void testWriteNoException() { 2430 Parcel p = Parcel.obtain(); 2431 p.writeNoException(); 2432 p.setDataPosition(0); 2433 p.readException(); 2434 p.recycle(); 2435 } 2436 2437 public void testWriteFileDescriptor() { 2438 Parcel p; 2439 FileDescriptor fIn = FileDescriptor.in; 2440 ParcelFileDescriptor pfd; 2441 2442 p = Parcel.obtain(); 2443 pfd = p.readFileDescriptor(); 2444 assertNull(pfd); 2445 p.recycle(); 2446 2447 p = Parcel.obtain(); 2448 p.writeFileDescriptor(fIn); 2449 p.setDataPosition(0); 2450 pfd = p.readFileDescriptor(); 2451 assertNotNull(pfd); 2452 assertNotNull(pfd.getFileDescriptor()); 2453 p.recycle(); 2454 } 2455 2456 public void testHasFileDescriptor() { 2457 Parcel p; 2458 FileDescriptor fIn = FileDescriptor.in; 2459 2460 p = Parcel.obtain(); 2461 p.writeFileDescriptor(fIn); 2462 p.setDataPosition(0); 2463 assertTrue(p.hasFileDescriptors()); 2464 p.recycle(); 2465 2466 p = Parcel.obtain(); 2467 p.writeInt(111); 2468 p.setDataPosition(0); 2469 assertFalse(p.hasFileDescriptors()); 2470 p.recycle(); 2471 } 2472 2473 public void testReadBundle() { 2474 Bundle bundle = new Bundle(); 2475 bundle.putBoolean("boolean", true); 2476 bundle.putInt("int", Integer.MAX_VALUE); 2477 bundle.putString("string", "String"); 2478 2479 Bundle bundle2; 2480 Parcel p; 2481 2482 // test null 2483 p = Parcel.obtain(); 2484 p.writeBundle(null); 2485 p.setDataPosition(0); 2486 bundle2 = p.readBundle(); 2487 assertNull(bundle2); 2488 p.recycle(); 2489 2490 // test not null 2491 bundle2 = null; 2492 p = Parcel.obtain(); 2493 p.writeBundle(bundle); 2494 p.setDataPosition(0); 2495 bundle2 = p.readBundle(); 2496 assertNotNull(bundle2); 2497 assertEquals(true, bundle2.getBoolean("boolean")); 2498 assertEquals(Integer.MAX_VALUE, bundle2.getInt("int")); 2499 assertEquals("String", bundle2.getString("string")); 2500 p.recycle(); 2501 2502 bundle2 = null; 2503 Parcel a = Parcel.obtain(); 2504 bundle2 = new Bundle(); 2505 bundle2.putString("foo", "test"); 2506 a.writeBundle(bundle2); 2507 a.setDataPosition(0); 2508 bundle.readFromParcel(a); 2509 p = Parcel.obtain(); 2510 p.setDataPosition(0); 2511 p.writeBundle(bundle); 2512 p.setDataPosition(0); 2513 bundle2 = p.readBundle(); 2514 assertNotNull(bundle2); 2515 assertFalse(true == bundle2.getBoolean("boolean")); 2516 assertFalse(Integer.MAX_VALUE == bundle2.getInt("int")); 2517 assertFalse("String".equals( bundle2.getString("string"))); 2518 a.recycle(); 2519 p.recycle(); 2520 } 2521 2522 public void testReadBundle2() { 2523 Bundle b = new Bundle(); 2524 b.putBoolean("boolean", true); 2525 b.putInt("int", Integer.MAX_VALUE); 2526 b.putString("string", "String"); 2527 2528 Bundle u; 2529 Parcel p; 2530 MockClassLoader m = new MockClassLoader(); 2531 2532 p = Parcel.obtain(); 2533 p.writeBundle(null); 2534 p.setDataPosition(0); 2535 u = p.readBundle(m); 2536 assertNull(u); 2537 p.recycle(); 2538 2539 u = null; 2540 p = Parcel.obtain(); 2541 p.writeBundle(b); 2542 p.setDataPosition(0); 2543 u = p.readBundle(m); 2544 assertNotNull(u); 2545 assertEquals(true, b.getBoolean("boolean")); 2546 assertEquals(Integer.MAX_VALUE, b.getInt("int")); 2547 assertEquals("String", b.getString("string")); 2548 p.recycle(); 2549 } 2550 2551 public void testWriteArray() { 2552 Parcel p; 2553 MockClassLoader mcl = new MockClassLoader(); 2554 2555 p = Parcel.obtain(); 2556 p.writeArray(null); 2557 p.setDataPosition(0); 2558 assertNull(p.readArray(mcl)); 2559 p.recycle(); 2560 2561 Object[] objects = new Object[5]; 2562 objects[0] = Integer.MAX_VALUE; 2563 objects[1] = true; 2564 objects[2] = Long.MAX_VALUE; 2565 objects[3] = "String"; 2566 objects[4] = Float.MAX_VALUE; 2567 Object[] objects2; 2568 2569 p = Parcel.obtain(); 2570 p.writeArray(objects); 2571 p.setDataPosition(0); 2572 objects2 = p.readArray(mcl); 2573 assertNotNull(objects2); 2574 for (int i = 0; i < objects2.length; i++) { 2575 assertEquals(objects[i], objects2[i]); 2576 } 2577 p.recycle(); 2578 } 2579 2580 public void testReadArrayList() { 2581 Parcel p; 2582 MockClassLoader mcl = new MockClassLoader(); 2583 2584 p = Parcel.obtain(); 2585 p.writeArray(null); 2586 p.setDataPosition(0); 2587 assertNull(p.readArrayList(mcl)); 2588 p.recycle(); 2589 2590 Object[] objects = new Object[5]; 2591 objects[0] = Integer.MAX_VALUE; 2592 objects[1] = true; 2593 objects[2] = Long.MAX_VALUE; 2594 objects[3] = "String"; 2595 objects[4] = Float.MAX_VALUE; 2596 ArrayList<?> objects2; 2597 2598 p = Parcel.obtain(); 2599 p.writeArray(objects); 2600 p.setDataPosition(0); 2601 objects2 = p.readArrayList(mcl); 2602 assertNotNull(objects2); 2603 for (int i = 0; i < objects2.size(); i++) { 2604 assertEquals(objects[i], objects2.get(i)); 2605 } 2606 p.recycle(); 2607 } 2608 2609 @SuppressWarnings("unchecked") 2610 public void testWriteSparseArray() { 2611 Parcel p; 2612 MockClassLoader mcl = new MockClassLoader(); 2613 2614 p = Parcel.obtain(); 2615 p.writeSparseArray(null); 2616 p.setDataPosition(0); 2617 assertNull(p.readSparseArray(mcl)); 2618 p.recycle(); 2619 2620 SparseArray<Object> sparseArray = new SparseArray<Object>(); 2621 sparseArray.put(3, "String"); 2622 sparseArray.put(2, Long.MAX_VALUE); 2623 sparseArray.put(4, Float.MAX_VALUE); 2624 sparseArray.put(0, Integer.MAX_VALUE); 2625 sparseArray.put(1, true); 2626 sparseArray.put(10, true); 2627 SparseArray<Object> sparseArray2; 2628 2629 p = Parcel.obtain(); 2630 p.writeSparseArray(sparseArray); 2631 p.setDataPosition(0); 2632 sparseArray2 = p.readSparseArray(mcl); 2633 assertNotNull(sparseArray2); 2634 assertEquals(sparseArray.size(), sparseArray2.size()); 2635 assertEquals(sparseArray.get(0), sparseArray2.get(0)); 2636 assertEquals(sparseArray.get(1), sparseArray2.get(1)); 2637 assertEquals(sparseArray.get(2), sparseArray2.get(2)); 2638 assertEquals(sparseArray.get(3), sparseArray2.get(3)); 2639 assertEquals(sparseArray.get(4), sparseArray2.get(4)); 2640 assertEquals(sparseArray.get(10), sparseArray2.get(10)); 2641 p.recycle(); 2642 } 2643 2644 public void testWriteSparseBooleanArray() { 2645 Parcel p; 2646 2647 p = Parcel.obtain(); 2648 p.writeSparseArray(null); 2649 p.setDataPosition(0); 2650 assertNull(p.readSparseBooleanArray()); 2651 p.recycle(); 2652 2653 SparseBooleanArray sparseBooleanArray = new SparseBooleanArray(); 2654 sparseBooleanArray.put(3, true); 2655 sparseBooleanArray.put(2, false); 2656 sparseBooleanArray.put(4, false); 2657 sparseBooleanArray.put(0, true); 2658 sparseBooleanArray.put(1, true); 2659 sparseBooleanArray.put(10, true); 2660 SparseBooleanArray sparseBoolanArray2; 2661 2662 p = Parcel.obtain(); 2663 p.writeSparseBooleanArray(sparseBooleanArray); 2664 p.setDataPosition(0); 2665 sparseBoolanArray2 = p.readSparseBooleanArray(); 2666 assertNotNull(sparseBoolanArray2); 2667 assertEquals(sparseBooleanArray.size(), sparseBoolanArray2.size()); 2668 assertEquals(sparseBooleanArray.get(0), sparseBoolanArray2.get(0)); 2669 assertEquals(sparseBooleanArray.get(1), sparseBoolanArray2.get(1)); 2670 assertEquals(sparseBooleanArray.get(2), sparseBoolanArray2.get(2)); 2671 assertEquals(sparseBooleanArray.get(3), sparseBoolanArray2.get(3)); 2672 assertEquals(sparseBooleanArray.get(4), sparseBoolanArray2.get(4)); 2673 assertEquals(sparseBooleanArray.get(10), sparseBoolanArray2.get(10)); 2674 p.recycle(); 2675 } 2676 2677 public void testWriteStrongBinder() { 2678 Parcel p; 2679 Binder binder; 2680 Binder binder2 = new Binder(); 2681 2682 p = Parcel.obtain(); 2683 p.writeStrongBinder(null); 2684 p.setDataPosition(0); 2685 assertNull(p.readStrongBinder()); 2686 p.recycle(); 2687 2688 p = Parcel.obtain(); 2689 p.writeStrongBinder(binder2); 2690 p.setDataPosition(0); 2691 binder = (Binder) p.readStrongBinder(); 2692 assertEquals(binder2, binder); 2693 p.recycle(); 2694 } 2695 2696 public void testWriteStrongInterface() { 2697 Parcel p; 2698 MockIInterface mockInterface = new MockIInterface(); 2699 MockIInterface mockIInterface2 = new MockIInterface(); 2700 2701 p = Parcel.obtain(); 2702 p.writeStrongInterface(null); 2703 p.setDataPosition(0); 2704 assertNull(p.readStrongBinder()); 2705 p.recycle(); 2706 2707 p = Parcel.obtain(); 2708 p.writeStrongInterface(mockInterface); 2709 p.setDataPosition(0); 2710 mockIInterface2.binder = (Binder) p.readStrongBinder(); 2711 assertEquals(mockInterface.binder, mockIInterface2.binder); 2712 p.recycle(); 2713 } 2714 2715 public void testWriteBinderArray() { 2716 Parcel p; 2717 IBinder[] ibinder2 = {new Binder(), new Binder()}; 2718 IBinder[] ibinder3 = new IBinder[2]; 2719 IBinder[] ibinder4 = new IBinder[3]; 2720 2721 p = Parcel.obtain(); 2722 p.writeBinderArray(null); 2723 p.setDataPosition(0); 2724 try { 2725 p.readBinderArray(null); 2726 fail("Should throw a RuntimeException"); 2727 } catch (RuntimeException e) { 2728 //expected 2729 } 2730 2731 p.setDataPosition(0); 2732 try { 2733 p.readBinderArray(ibinder3); 2734 fail("Should throw a RuntimeException"); 2735 } catch (RuntimeException e) { 2736 //expected 2737 } 2738 2739 p.setDataPosition(0); 2740 try { 2741 p.readBinderArray(ibinder2); 2742 fail("Should throw a RuntimeException"); 2743 } catch (RuntimeException e) { 2744 //expected 2745 } 2746 p.recycle(); 2747 2748 p = Parcel.obtain(); 2749 p.writeBinderArray(ibinder2); 2750 p.setDataPosition(0); 2751 try { 2752 p.readBinderArray(null); 2753 fail("Should throw a RuntimeException"); 2754 } catch (RuntimeException e) { 2755 //expected 2756 } 2757 2758 p.setDataPosition(0); 2759 try { 2760 p.readBinderArray(ibinder4); 2761 fail("Should throw a RuntimeException"); 2762 } catch (RuntimeException e) { 2763 //expected 2764 } 2765 2766 p.setDataPosition(0); 2767 p.readBinderArray(ibinder3); 2768 assertNotNull(ibinder3); 2769 for (int i = 0; i < ibinder3.length; i++) { 2770 assertNotNull(ibinder3[i]); 2771 assertEquals(ibinder2[i], ibinder3[i]); 2772 } 2773 p.recycle(); 2774 } 2775 2776 public void testCreateBinderArray() { 2777 Parcel p; 2778 IBinder[] ibinder = {}; 2779 IBinder[] ibinder2 = {new Binder(), new Binder()}; 2780 IBinder[] ibinder3; 2781 IBinder[] ibinder4; 2782 2783 p = Parcel.obtain(); 2784 p.writeBinderArray(null); 2785 p.setDataPosition(0); 2786 ibinder3 = p.createBinderArray(); 2787 assertNull(ibinder3); 2788 p.recycle(); 2789 2790 p = Parcel.obtain(); 2791 p.writeBinderArray(ibinder); 2792 p.setDataPosition(0); 2793 ibinder4 = p.createBinderArray(); 2794 assertNotNull(ibinder4); 2795 assertEquals(0, ibinder4.length); 2796 p.recycle(); 2797 2798 p = Parcel.obtain(); 2799 p.writeBinderArray(ibinder2); 2800 p.setDataPosition(0); 2801 ibinder3 = p.createBinderArray(); 2802 assertNotNull(ibinder3); 2803 for (int i = 0; i < ibinder3.length; i++) { 2804 assertNotNull(ibinder3[i]); 2805 assertEquals(ibinder2[i], ibinder3[i]); 2806 } 2807 p.recycle(); 2808 } 2809 2810 public void testWriteBinderList() { 2811 Parcel p; 2812 ArrayList<IBinder> arrayList = new ArrayList<IBinder>(); 2813 ArrayList<IBinder> arrayList2 = new ArrayList<IBinder>(); 2814 arrayList2.add(new Binder()); 2815 arrayList2.add(new Binder()); 2816 ArrayList<IBinder> arrayList3 = new ArrayList<IBinder>(); 2817 arrayList3.add(new Binder()); 2818 arrayList3.add(new Binder()); 2819 arrayList3.add(new Binder()); 2820 2821 p = Parcel.obtain(); 2822 p.writeBinderList(null); 2823 p.setDataPosition(0); 2824 try { 2825 p.readBinderList(null); 2826 fail("Should throw a RuntimeException"); 2827 } catch (RuntimeException e) { 2828 //expected 2829 } 2830 p.setDataPosition(0); 2831 assertEquals(0, arrayList.size()); 2832 p.readBinderList(arrayList); 2833 assertEquals(0, arrayList.size()); 2834 p.recycle(); 2835 2836 p = Parcel.obtain(); 2837 p.writeBinderList(arrayList2); 2838 p.setDataPosition(0); 2839 assertEquals(0, arrayList.size()); 2840 p.readBinderList(arrayList); 2841 assertEquals(2, arrayList.size()); 2842 assertEquals(arrayList2, arrayList); 2843 p.recycle(); 2844 2845 p = Parcel.obtain(); 2846 p.writeBinderList(arrayList2); 2847 p.setDataPosition(0); 2848 assertEquals(3, arrayList3.size()); 2849 p.readBinderList(arrayList3); 2850 assertEquals(2, arrayList3.size()); 2851 assertEquals(arrayList2, arrayList3); 2852 p.recycle(); 2853 } 2854 2855 public void testCreateBinderArrayList() { 2856 Parcel p; 2857 ArrayList<IBinder> arrayList = new ArrayList<IBinder>(); 2858 ArrayList<IBinder> arrayList2 = new ArrayList<IBinder>(); 2859 arrayList2.add(new Binder()); 2860 arrayList2.add(new Binder()); 2861 ArrayList<IBinder> arrayList3; 2862 ArrayList<IBinder> arrayList4; 2863 2864 p = Parcel.obtain(); 2865 p.writeBinderList(null); 2866 p.setDataPosition(0); 2867 arrayList3 = p.createBinderArrayList(); 2868 assertNull(arrayList3); 2869 p.recycle(); 2870 2871 p = Parcel.obtain(); 2872 p.writeBinderList(arrayList); 2873 p.setDataPosition(0); 2874 arrayList3 = p.createBinderArrayList(); 2875 assertNotNull(arrayList3); 2876 assertEquals(0, arrayList3.size()); 2877 p.recycle(); 2878 2879 p = Parcel.obtain(); 2880 p.writeBinderList(arrayList2); 2881 p.setDataPosition(0); 2882 arrayList4 = p.createBinderArrayList(); 2883 assertNotNull(arrayList4); 2884 assertEquals(arrayList2, arrayList4); 2885 p.recycle(); 2886 } 2887 2888 @SuppressWarnings("unchecked") 2889 public void testWriteMap() { 2890 Parcel p; 2891 MockClassLoader mcl = new MockClassLoader(); 2892 HashMap map = new HashMap(); 2893 HashMap map2 = new HashMap(); 2894 2895 p = Parcel.obtain(); 2896 p.writeMap(null); 2897 p.setDataPosition(0); 2898 assertEquals(0, map2.size()); 2899 p.readMap(map2, mcl); 2900 assertEquals(0, map2.size()); 2901 p.recycle(); 2902 2903 map.put("string", "String"); 2904 map.put("int", Integer.MAX_VALUE); 2905 map.put("boolean", true); 2906 p = Parcel.obtain(); 2907 p.writeMap(map); 2908 p.setDataPosition(0); 2909 assertEquals(0, map2.size()); 2910 p.readMap(map2, mcl); 2911 assertEquals(3, map2.size()); 2912 assertEquals("String", map.get("string")); 2913 assertEquals(Integer.MAX_VALUE, map.get("int")); 2914 assertEquals(true, map.get("boolean")); 2915 p.recycle(); 2916 } 2917 2918 @SuppressWarnings("unchecked") 2919 public void testReadHashMap() { 2920 Parcel p; 2921 MockClassLoader mcl = new MockClassLoader(); 2922 HashMap map = new HashMap(); 2923 HashMap map2; 2924 2925 p = Parcel.obtain(); 2926 p.writeMap(null); 2927 p.setDataPosition(0); 2928 map2 = p.readHashMap(mcl); 2929 assertNull(map2); 2930 p.recycle(); 2931 2932 map.put("string", "String"); 2933 map.put("int", Integer.MAX_VALUE); 2934 map.put("boolean", true); 2935 map2 = null; 2936 p = Parcel.obtain(); 2937 p.writeMap(map); 2938 p.setDataPosition(0); 2939 map2 = p.readHashMap(mcl); 2940 assertNotNull(map2); 2941 assertEquals(3, map2.size()); 2942 assertEquals("String", map.get("string")); 2943 assertEquals(Integer.MAX_VALUE, map.get("int")); 2944 assertEquals(true, map.get("boolean")); 2945 p.recycle(); 2946 } 2947 2948 @SuppressWarnings("unchecked") 2949 public void testReadList() { 2950 Parcel p; 2951 MockClassLoader mcl = new MockClassLoader(); 2952 ArrayList arrayList = new ArrayList(); 2953 2954 p = Parcel.obtain(); 2955 p.writeList(null); 2956 p.setDataPosition(0); 2957 assertEquals(0, arrayList.size()); 2958 p.readList(arrayList, mcl); 2959 assertEquals(0, arrayList.size()); 2960 p.recycle(); 2961 2962 ArrayList arrayList2 = new ArrayList(); 2963 arrayList2.add(Integer.MAX_VALUE); 2964 arrayList2.add(true); 2965 arrayList2.add(Long.MAX_VALUE); 2966 arrayList2.add("String"); 2967 arrayList2.add(Float.MAX_VALUE); 2968 2969 p = Parcel.obtain(); 2970 p.writeList(arrayList2); 2971 p.setDataPosition(0); 2972 assertEquals(0, arrayList.size()); 2973 p.readList(arrayList, mcl); 2974 assertEquals(5, arrayList.size()); 2975 for (int i = 0; i < arrayList.size(); i++) { 2976 assertEquals(arrayList.get(i), arrayList2.get(i)); 2977 } 2978 p.recycle(); 2979 } 2980 2981 public void testBinderDataProtection() { 2982 Parcel p; 2983 IBinder b = new Binder(); 2984 2985 p = Parcel.obtain(); 2986 final int firstIntPos = p.dataPosition(); 2987 p.writeInt(1); 2988 p.writeStrongBinder(b); 2989 final int secondIntPos = p.dataPosition(); 2990 p.writeInt(2); 2991 p.writeStrongBinder(b); 2992 final int thirdIntPos = p.dataPosition(); 2993 p.writeInt(3); 2994 2995 for (int pos = 0; pos <= thirdIntPos; pos++) { 2996 p.setDataPosition(pos); 2997 int value = p.readInt(); 2998 2999 // WARNING: this is using unstable APIs: these positions aren't guaranteed 3000 if (firstIntPos - 4 <= pos && pos <= firstIntPos) continue; 3001 if (secondIntPos - 4 <= pos && pos <= secondIntPos) continue; 3002 if (thirdIntPos - 4 <= pos && pos <= thirdIntPos) continue; 3003 3004 // All other read attempts cross into protected data and will return 0 3005 assertEquals(0, value); 3006 } 3007 3008 p.recycle(); 3009 } 3010 3011 public void testBinderDataProtectionIncrements() { 3012 Parcel p; 3013 IBinder b = new Binder(); 3014 3015 p = Parcel.obtain(); 3016 final int firstIntPos = p.dataPosition(); 3017 p.writeInt(1); 3018 p.writeStrongBinder(b); 3019 final int secondIntPos = p.dataPosition(); 3020 p.writeInt(2); 3021 p.writeStrongBinder(b); 3022 final int thirdIntPos = p.dataPosition(); 3023 p.writeInt(3); 3024 final int end = p.dataPosition(); 3025 3026 p.setDataPosition(0); 3027 int pos; 3028 do { 3029 pos = p.dataPosition(); 3030 int value = p.readInt(); 3031 3032 // WARNING: this is using unstable APIs: these positions aren't guaranteed 3033 if (firstIntPos - 4 <= pos && pos <= firstIntPos) continue; 3034 if (secondIntPos - 4 <= pos && pos <= secondIntPos) continue; 3035 if (thirdIntPos - 4 <= pos && pos <= thirdIntPos) continue; 3036 3037 assertEquals(0, value); 3038 } while(pos < end); 3039 3040 p.recycle(); 3041 } 3042 3043 private class MockClassLoader extends ClassLoader { 3044 public MockClassLoader() { 3045 super(); 3046 } 3047 } 3048 3049 private class MockIInterface implements IInterface { 3050 public Binder binder; 3051 3052 public MockIInterface() { 3053 binder = new Binder(); 3054 } 3055 3056 public IBinder asBinder() { 3057 return binder; 3058 } 3059 } 3060 3061 private static boolean parcelableWithBadCreatorInitializerHasRun; 3062 private static boolean invalidCreatorIntializerHasRun; 3063 3064 /** 3065 * A class that would be Parcelable except that it doesn't have a CREATOR field declared to be 3066 * of the correct type. 3067 */ 3068 @SuppressWarnings("unused") // Referenced via reflection only 3069 private static class ParcelableWithBadCreator implements Parcelable { 3070 3071 static { 3072 ParcelTest.parcelableWithBadCreatorInitializerHasRun = true; 3073 } 3074 3075 private static class InvalidCreator 3076 implements Parcelable.Creator<ParcelableWithBadCreator> { 3077 3078 static { 3079 invalidCreatorIntializerHasRun = true; 3080 } 3081 3082 @Override 3083 public ParcelableWithBadCreator createFromParcel(Parcel source) { 3084 return null; 3085 } 3086 3087 @Override 3088 public ParcelableWithBadCreator[] newArray(int size) { 3089 return new ParcelableWithBadCreator[0]; 3090 } 3091 3092 } 3093 3094 // Invalid declaration: Must be declared as Parcelable.Creator or a subclass. 3095 public static Object CREATOR = new InvalidCreator(); 3096 3097 @Override 3098 public int describeContents() { 3099 return 0; 3100 } 3101 3102 @Override 3103 public void writeToParcel(Parcel dest, int flags) { 3104 3105 } 3106 } 3107 3108 // http://b/1171613 3109 public void testBadStream_invalidCreator() { 3110 Parcel parcel = Parcel.obtain(); 3111 // Create an invalid stream by manipulating the Parcel. 3112 parcel.writeString(getClass().getName() + "$ParcelableWithBadCreator"); 3113 byte[] badData = parcel.marshall(); 3114 parcel.recycle(); 3115 3116 // Now try to read the bad data. 3117 parcel = Parcel.obtain(); 3118 parcel.unmarshall(badData, 0, badData.length); 3119 parcel.setDataPosition(0); 3120 try { 3121 parcel.readParcelable(getClass().getClassLoader()); 3122 fail(); 3123 } catch (BadParcelableException expected) { 3124 } finally { 3125 parcel.recycle(); 3126 } 3127 3128 assertFalse(invalidCreatorIntializerHasRun); 3129 assertFalse(parcelableWithBadCreatorInitializerHasRun); 3130 } 3131 3132 private static boolean doesNotImplementParcelableInitializerHasRun; 3133 3134 /** A class that would be Parcelable except that it does not implement Parcelable. */ 3135 @SuppressWarnings("unused") // Referenced via reflection only 3136 private static class DoesNotImplementParcelable { 3137 3138 static { 3139 doesNotImplementParcelableInitializerHasRun = true; 3140 } 3141 3142 public static Parcelable.Creator<Object> CREATOR = new Parcelable.Creator<Object>() { 3143 @Override 3144 public Object createFromParcel(Parcel source) { 3145 return new DoesNotImplementParcelable(); 3146 } 3147 3148 @Override 3149 public Object[] newArray(int size) { 3150 return new Object[size]; 3151 } 3152 }; 3153 } 3154 3155 // http://b/1171613 3156 public void testBadStream_objectDoesNotImplementParcelable() { 3157 Parcel parcel = Parcel.obtain(); 3158 // Create an invalid stream by manipulating the Parcel. 3159 parcel.writeString(getClass().getName() + "$DoesNotImplementParcelable"); 3160 byte[] badData = parcel.marshall(); 3161 parcel.recycle(); 3162 3163 // Now try to read the bad data. 3164 parcel = Parcel.obtain(); 3165 parcel.unmarshall(badData, 0, badData.length); 3166 parcel.setDataPosition(0); 3167 try { 3168 parcel.readParcelable(getClass().getClassLoader()); 3169 fail(); 3170 } catch (BadParcelableException expected) { 3171 } finally { 3172 parcel.recycle(); 3173 } 3174 3175 assertFalse(doesNotImplementParcelableInitializerHasRun); 3176 } 3177 3178 public static class SimpleParcelable implements Parcelable { 3179 private final int value; 3180 3181 public SimpleParcelable(int value) { 3182 this.value = value; 3183 } 3184 3185 private SimpleParcelable(Parcel in) { 3186 this.value = in.readInt(); 3187 } 3188 3189 public int getValue() { 3190 return value; 3191 } 3192 3193 @Override 3194 public int describeContents() { 3195 return 0; 3196 } 3197 3198 @Override 3199 public void writeToParcel(Parcel out, int flags) { 3200 out.writeInt(value); 3201 } 3202 3203 public static Parcelable.Creator<SimpleParcelable> CREATOR = 3204 new Parcelable.Creator<SimpleParcelable>() { 3205 3206 @Override 3207 public SimpleParcelable createFromParcel(Parcel source) { 3208 return new SimpleParcelable(source); 3209 } 3210 3211 @Override 3212 public SimpleParcelable[] newArray(int size) { 3213 return new SimpleParcelable[size]; 3214 } 3215 }; 3216 } 3217 3218 public void testReadWriteParcellableList() { 3219 Parcel parcel = Parcel.obtain(); 3220 3221 ArrayList<SimpleParcelable> list = new ArrayList<>(); 3222 list.add(new SimpleParcelable(57)); 3223 3224 // Writing a |null| list to a parcel should work, and reading it back 3225 // from a parcel should clear the target list. 3226 parcel.writeParcelableList(null, 0); 3227 parcel.setDataPosition(0); 3228 parcel.readParcelableList(list, SimpleParcelable.class.getClassLoader()); 3229 assertEquals(0, list.size()); 3230 3231 list.clear(); 3232 list.add(new SimpleParcelable(42)); 3233 list.add(new SimpleParcelable(56)); 3234 3235 parcel.setDataPosition(0); 3236 parcel.writeParcelableList(list, 0); 3237 3238 // Populate the list with a value, we will later assert that the 3239 // value has been removed. 3240 list.clear(); 3241 list.add(new SimpleParcelable(100)); 3242 3243 parcel.setDataPosition(0); 3244 parcel.readParcelableList(list, SimpleParcelable.class.getClassLoader()); 3245 3246 assertEquals(2, list.size()); 3247 assertEquals(42, list.get(0).getValue()); 3248 assertEquals(56, list.get(1).getValue()); 3249 } 3250 3251 // http://b/35384981 3252 public void testCreateArrayWithTruncatedParcel() { 3253 Parcel parcel = Parcel.obtain(); 3254 parcel.writeByteArray(new byte[] { 'a', 'b' }); 3255 byte[] marshalled = parcel.marshall(); 3256 3257 // Test that createByteArray returns null with a truncated parcel. 3258 parcel = Parcel.obtain(); 3259 parcel.unmarshall(marshalled, 0, marshalled.length); 3260 parcel.setDataPosition(0); 3261 // Shorten the data size by 2 to remove padding at the end of the array. 3262 parcel.setDataSize(marshalled.length - 2); 3263 assertNull(parcel.createByteArray()); 3264 3265 // Test that readByteArray returns null with a truncated parcel. 3266 parcel = Parcel.obtain(); 3267 parcel.unmarshall(marshalled, 0, marshalled.length); 3268 parcel.setDataSize(marshalled.length - 2); 3269 try { 3270 parcel.readByteArray(new byte[2]); 3271 fail(); 3272 } catch (RuntimeException expected) { 3273 } 3274 } 3275 3276 public void testMaliciousMapWrite() { 3277 class MaliciousMap<K, V> extends HashMap<K, V> { 3278 public int fakeSize = 0; 3279 public boolean armed = false; 3280 3281 class FakeEntrySet extends HashSet<Entry<K, V>> { 3282 public FakeEntrySet(Collection<? extends Entry<K, V>> c) { 3283 super(c); 3284 } 3285 3286 @Override 3287 public int size() { 3288 if (armed) { 3289 // Only return fake size on next call, to mitigate unexpected behavior. 3290 armed = false; 3291 return fakeSize; 3292 } else { 3293 return super.size(); 3294 } 3295 } 3296 } 3297 3298 @Override 3299 public Set<Map.Entry<K, V>> entrySet() { 3300 return new FakeEntrySet(super.entrySet()); 3301 } 3302 } 3303 3304 Parcel parcel = Parcel.obtain(); 3305 3306 // Fake having more Map entries than there really are 3307 MaliciousMap map = new MaliciousMap<String, String>(); 3308 map.fakeSize = 1; 3309 map.armed = true; 3310 try { 3311 parcel.writeMap(map); 3312 fail("Should have thrown a BadParcelableException"); 3313 } catch (BadParcelableException bpe) { 3314 // good 3315 } 3316 3317 // Fake having fewer Map entries than there really are 3318 map = new MaliciousMap<String, String>(); 3319 map.put("key", "value"); 3320 map.fakeSize = 0; 3321 map.armed = true; 3322 try { 3323 parcel.writeMap(map); 3324 fail("Should have thrown a BadParcelableException"); 3325 } catch (BadParcelableException bpe) { 3326 // good 3327 } 3328 } 3329 3330 public static class ParcelExceptionConnection extends AbstractFuture<IParcelExceptionService> 3331 implements ServiceConnection { 3332 @Override 3333 public void onServiceConnected(ComponentName name, IBinder service) { 3334 set(IParcelExceptionService.Stub.asInterface(service)); 3335 } 3336 3337 @Override 3338 public void onServiceDisconnected(ComponentName name) { 3339 } 3340 3341 @Override 3342 public IParcelExceptionService get() throws InterruptedException, ExecutionException { 3343 try { 3344 return get(5, TimeUnit.SECONDS); 3345 } catch (TimeoutException e) { 3346 throw new RuntimeException(e); 3347 } 3348 } 3349 } 3350 3351 public void testExceptionOverwritesObject() throws Exception { 3352 final Intent intent = new Intent(); 3353 intent.setComponent(new ComponentName( 3354 "android.os.cts", "android.os.cts.ParcelExceptionService")); 3355 3356 final ParcelExceptionConnection connection = new ParcelExceptionConnection(); 3357 3358 mContext.startService(intent); 3359 assertTrue(mContext.bindService(intent, connection, 3360 Context.BIND_ABOVE_CLIENT | Context.BIND_EXTERNAL_SERVICE)); 3361 3362 3363 Parcel data = Parcel.obtain(); 3364 Parcel reply = Parcel.obtain(); 3365 data.writeInterfaceToken("android.os.cts.IParcelExceptionService"); 3366 IParcelExceptionService service = connection.get(); 3367 try { 3368 assertTrue("Transaction failed", service.asBinder().transact( 3369 IParcelExceptionService.Stub.TRANSACTION_writeBinderThrowException, data, reply, 3370 0)); 3371 } catch (Exception e) { 3372 fail("Exception caught from transaction: " + e); 3373 } 3374 reply.setDataPosition(0); 3375 assertTrue("Exception should have occurred on service-side", 3376 reply.readExceptionCode() != 0); 3377 assertNull("Binder should have been overwritten by the exception", 3378 reply.readStrongBinder()); 3379 } 3380 3381 public static class ParcelObjectFreeService extends Service { 3382 3383 @Override 3384 public IBinder onBind(Intent intent) { 3385 return new Binder(); 3386 } 3387 3388 @Override 3389 public void onCreate() { 3390 super.onCreate(); 3391 3392 Parcel parcel = Parcel.obtain(); 3393 3394 // Construct parcel with object in it. 3395 parcel.writeInt(1); 3396 final int pos = parcel.dataPosition(); 3397 parcel.writeStrongBinder(new Binder()); 3398 3399 // wipe out the object by setting data size 3400 parcel.setDataSize(pos); 3401 3402 // recycle the parcel. This should not cause a native segfault 3403 parcel.recycle(); 3404 } 3405 3406 public static class Connection extends AbstractFuture<IBinder> 3407 implements ServiceConnection { 3408 3409 @Override 3410 public void onServiceConnected(ComponentName name, IBinder service) { 3411 set(service); 3412 } 3413 3414 @Override 3415 public void onServiceDisconnected(ComponentName name) { 3416 } 3417 3418 @Override 3419 public IBinder get() throws InterruptedException, ExecutionException { 3420 try { 3421 return get(5, TimeUnit.SECONDS); 3422 } catch (TimeoutException e) { 3423 return null; 3424 } 3425 } 3426 } 3427 } 3428 3429 public void testObjectDoubleFree() throws Exception { 3430 3431 final Intent intent = new Intent(); 3432 intent.setComponent(new ComponentName( 3433 "android.os.cts", "android.os.cts.ParcelTest$ParcelObjectFreeService")); 3434 3435 final ParcelObjectFreeService.Connection connection = 3436 new ParcelObjectFreeService.Connection(); 3437 3438 mContext.startService(intent); 3439 assertTrue(mContext.bindService(intent, connection, 3440 Context.BIND_ABOVE_CLIENT | Context.BIND_EXTERNAL_SERVICE)); 3441 3442 assertNotNull("Service should have started without crashing.", connection.get()); 3443 } 3444 3445 @AsbSecurityTest(cveBugId = 140419401) 3446 public void testObjectResize() throws Exception { 3447 Parcel p; 3448 IBinder b1 = new Binder(); 3449 IBinder b2 = new Binder(); 3450 3451 p = Parcel.obtain(); 3452 p.writeStrongBinder(b1); 3453 p.setDataSize(0); 3454 p.writeStrongBinder(b2); 3455 3456 p.setDataPosition(0); 3457 assertEquals("Object in parcel should match the binder written after the resize", b2, 3458 p.readStrongBinder()); 3459 p.recycle(); 3460 3461 p = Parcel.obtain(); 3462 p.writeStrongBinder(b1); 3463 final int secondBinderPos = p.dataPosition(); 3464 p.writeStrongBinder(b1); 3465 p.setDataSize(secondBinderPos); 3466 p.writeStrongBinder(b2); 3467 3468 p.setDataPosition(0); 3469 assertEquals("Object at the start of the parcel parcel should match the first binder", b1, 3470 p.readStrongBinder()); 3471 assertEquals("Object in parcel should match the binder written after the resize", b2, 3472 p.readStrongBinder()); 3473 p.recycle(); 3474 } 3475 } 3476