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