1 /* 2 * Copyright (C) 2023 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.car.occupantconnection; 18 19 import static com.android.car.internal.LargeParcelableBase.MAX_DIRECT_PAYLOAD_SIZE; 20 21 import static com.google.common.truth.Truth.assertThat; 22 23 import android.car.test.mocks.JavaMockitoHelper; 24 import android.content.ComponentName; 25 import android.content.Context; 26 import android.content.Intent; 27 import android.content.ServiceConnection; 28 import android.os.Binder; 29 import android.os.IBinder; 30 import android.os.Parcel; 31 import android.os.RemoteException; 32 import android.platform.test.annotations.DisabledOnRavenwood; 33 34 import androidx.test.filters.SmallTest; 35 import androidx.test.platform.app.InstrumentationRegistry; 36 37 import org.junit.Before; 38 import org.junit.Test; 39 40 import java.util.concurrent.CountDownLatch; 41 42 @SmallTest 43 @DisabledOnRavenwood(blockedBy = InstrumentationRegistry.class) 44 public final class PayloadUnitTest { 45 private static final int ARRAY_LENGTH_SMALL = MAX_DIRECT_PAYLOAD_SIZE - 1; 46 private static final int ARRAY_LENGTH_BIG = MAX_DIRECT_PAYLOAD_SIZE + 1; 47 private static final long DEFAULT_TIMEOUT_MS = 10_000; 48 49 private final Context mContext = 50 InstrumentationRegistry.getInstrumentation().getTargetContext(); 51 private final TestServiceConnection mServiceConnection = new TestServiceConnection(); 52 53 private IPayloadTestBinder mBinder; 54 55 @Before setUp()56 public void setUp() throws InterruptedException { 57 Intent intent = new Intent(); 58 intent.setClassName(mContext, PayloadTestBinderService.class.getName()); 59 mContext.bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE); 60 61 JavaMockitoHelper.await(mServiceConnection.latch, DEFAULT_TIMEOUT_MS); 62 } 63 64 @Test testLocalSerializeByteArray()65 public void testLocalSerializeByteArray() { 66 doTestLocalSerializationDeserializationByteArray(ARRAY_LENGTH_SMALL); 67 doTestLocalSerializationDeserializationByteArray(ARRAY_LENGTH_BIG); 68 } 69 70 @Test testRemoteSerializeByteArray()71 public void testRemoteSerializeByteArray() throws RemoteException { 72 doTestRemoteSerializationDeserializationByteArray(ARRAY_LENGTH_SMALL); 73 doTestRemoteSerializationDeserializationByteArray(ARRAY_LENGTH_BIG); 74 } 75 76 @Test testLocalSerializeBinder()77 public void testLocalSerializeBinder() { 78 IBinder origBinder = new Binder(); 79 Payload origPayload = new Payload(origBinder); 80 Parcel dest = Parcel.obtain(); 81 82 origPayload.writeToParcel(dest, 0); 83 dest.setDataPosition(0); 84 Payload newPayload = Payload.CREATOR.createFromParcel(dest); 85 86 assertThat(newPayload.getBinder()).isEqualTo(origBinder); 87 } 88 89 @Test testRemoteSerializeBinder()90 public void testRemoteSerializeBinder() throws RemoteException { 91 IBinder origBinder = new Binder(); 92 Payload origPayload = new Payload(origBinder); 93 Payload newPayload = mBinder.echoPayload(origPayload); 94 95 assertThat(newPayload.getBinder()).isEqualTo(origBinder); 96 } 97 doTestLocalSerializationDeserializationByteArray(int payloadSize)98 private static void doTestLocalSerializationDeserializationByteArray(int payloadSize) { 99 byte[] origArray = createByteArray(payloadSize); 100 Payload origPayload = new Payload(origArray); 101 Parcel dest = Parcel.obtain(); 102 103 origPayload.writeToParcel(dest, 0); 104 dest.setDataPosition(0); 105 Payload newPayload = Payload.CREATOR.createFromParcel(dest); 106 107 assertThat(newPayload.getBytes()).isEqualTo(origArray); 108 } 109 doTestRemoteSerializationDeserializationByteArray(int payloadSize)110 private void doTestRemoteSerializationDeserializationByteArray(int payloadSize) 111 throws RemoteException { 112 byte[] origArray = createByteArray(payloadSize); 113 Payload origPayload = new Payload(origArray); 114 Payload newPayload = mBinder.echoPayload(origPayload); 115 116 assertThat(newPayload.getBytes()).isEqualTo(origArray); 117 } 118 createByteArray(int length)119 private static byte[] createByteArray(int length) { 120 byte[] array = new byte[length]; 121 byte val = 0x7f; 122 for (int i = 0; i < length; i++) { 123 array[i] = val; 124 val++; 125 } 126 return array; 127 } 128 129 private final class TestServiceConnection implements ServiceConnection { 130 public final CountDownLatch latch = new CountDownLatch(1); 131 132 @Override onServiceConnected(ComponentName name, IBinder service)133 public void onServiceConnected(ComponentName name, IBinder service) { 134 mBinder = IPayloadTestBinder.Stub.asInterface(service); 135 latch.countDown(); 136 } 137 138 @Override onServiceDisconnected(ComponentName name)139 public void onServiceDisconnected(ComponentName name) { 140 } 141 } 142 } 143