Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit bb8357a1 authored by Android Build Coastguard Worker's avatar Android Build Coastguard Worker
Browse files

Snap for 9459725 from d5d254b1 to tm-qpr2-release

Change-Id: I5f2c70c15843de07f260d94317be20b460ed5ed2
parents de2ac03f d5d254b1
Loading
Loading
Loading
Loading
+6 −2
Original line number Diff line number Diff line
@@ -427,8 +427,6 @@ public final class Utils {
                "Need DUMP permission");
    }

    /**
     */
    public static AttributionSource getCallingAttributionSource(Context context) {
        int callingUid = Binder.getCallingUid();
        if (callingUid == android.os.Process.ROOT_UID) {
@@ -463,6 +461,9 @@ public final class Utils {
    @SuppressLint("AndroidFrameworkRequiresPermission")
    private static boolean checkPermissionForDataDelivery(Context context, String permission,
            AttributionSource attributionSource, String message) {
        if (isInstrumentationTestMode()) {
            return true;
        }
        // STOPSHIP(b/188391719): enable this security enforcement
        // attributionSource.enforceCallingUid();
        AttributionSource currentAttribution = new AttributionSource
@@ -669,6 +670,9 @@ public final class Utils {
    }

    public static boolean checkCallerIsSystemOrActiveOrManagedUser(Context context, String tag) {
        if (isInstrumentationTestMode()) {
            return true;
        }
        final boolean res = checkCallerIsSystemOrActiveOrManagedUser(context);
        if (!res) {
            Log.w(TAG, tag + " - Not allowed for"
+14 −6
Original line number Diff line number Diff line
@@ -262,7 +262,8 @@ public class AdapterService extends Service {
    }

    private BluetoothAdapter mAdapter;
    private AdapterProperties mAdapterProperties;
    @VisibleForTesting
    AdapterProperties mAdapterProperties;
    private AdapterState mAdapterStateMachine;
    private BondStateMachine mBondStateMachine;
    private JniCallbacks mJniCallbacks;
@@ -1361,7 +1362,8 @@ public class AdapterService extends Service {
    }

    @BluetoothAdapter.RfcommListenerResult
    private int stopRfcommListener(ParcelUuid uuid, AttributionSource attributionSource) {
    @VisibleForTesting
    int stopRfcommListener(ParcelUuid uuid, AttributionSource attributionSource) {
        RfcommListenerData listenerData = mBluetoothServerSockets.get(uuid.getUuid());

        if (listenerData == null) {
@@ -1380,7 +1382,8 @@ public class AdapterService extends Service {
        return listenerData.closeServerAndPendingSockets(mHandler);
    }

    private IncomingRfcommSocketInfo retrievePendingSocketForServiceRecord(
    @VisibleForTesting
    IncomingRfcommSocketInfo retrievePendingSocketForServiceRecord(
            ParcelUuid uuid, AttributionSource attributionSource) {
        IncomingRfcommSocketInfo socketInfo = new IncomingRfcommSocketInfo();

@@ -1548,7 +1551,8 @@ public class AdapterService extends Service {
        }
    }

    private boolean isAvailable() {
    @VisibleForTesting
    boolean isAvailable() {
        return !mCleaningUp;
    }

@@ -3864,7 +3868,8 @@ public class AdapterService extends Service {
        return mAdapterProperties.getName().length();
    }

    private static boolean isValidIoCapability(int capability) {
    @VisibleForTesting
    static boolean isValidIoCapability(int capability) {
        if (capability < 0 || capability >= BluetoothAdapter.IO_CAPABILITY_MAX) {
            Log.e(TAG, "Invalid IO capability value - " + capability);
            return false;
@@ -4645,6 +4650,8 @@ public class AdapterService extends Service {
                return BluetoothStatusCodes.ERROR_DISCONNECT_REASON_BAD_PARAMETERS;
            case /*HCI_ERR_PEER_USER*/ 0x13:
                return BluetoothStatusCodes.ERROR_DISCONNECT_REASON_REMOTE_REQUEST;
            case /*HCI_ERR_REMOTE_POWER_OFF*/ 0x15:
                return BluetoothStatusCodes.ERROR_DISCONNECT_REASON_REMOTE_REQUEST;
            case /*HCI_ERR_CONN_CAUSE_LOCAL_HOST*/ 0x16:
                return BluetoothStatusCodes.ERROR_DISCONNECT_REASON_LOCAL_REQUEST;
            case /*HCI_ERR_UNSUPPORTED_REM_FEATURE*/ 0x1A:
@@ -4793,7 +4800,8 @@ public class AdapterService extends Service {
        return mAdapterProperties.isA2dpOffloadEnabled();
    }

    private BluetoothActivityEnergyInfo reportActivityInfo() {
    @VisibleForTesting
    BluetoothActivityEnergyInfo reportActivityInfo() {
        if (mAdapterProperties.getState() != BluetoothAdapter.STATE_ON
                || !mAdapterProperties.isActivityAndEnergyReportingSupported()) {
            return null;
+211 −0
Original line number Diff line number Diff line
/*
 * Copyright 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.bluetooth.btservice;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothClass;
import android.bluetooth.IBluetoothOobDataCallback;
import android.content.AttributionSource;
import android.content.pm.PackageManager;
import android.os.ParcelUuid;

import com.android.bluetooth.x.com.android.modules.utils.SynchronousResultReceiver;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;

import java.io.FileDescriptor;

public class AdapterServiceBinderTest {
    @Mock private AdapterService mService;
    @Mock private AdapterProperties mAdapterProperties;
    @Mock private PackageManager mPackageManager;

    private AdapterService.AdapterServiceBinder mBinder;
    private AttributionSource mAttributionSource;

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
        mService.mAdapterProperties = mAdapterProperties;
        doReturn(true).when(mService).isAvailable();
        doReturn(mPackageManager).when(mService).getPackageManager();
        doReturn(new String[] { "com.android.bluetooth.btservice.test" })
                .when(mPackageManager).getPackagesForUid(anyInt());
        mBinder = new AdapterService.AdapterServiceBinder(mService);
        mAttributionSource = new AttributionSource.Builder(0).build();
    }

    @After
    public void cleaUp() {
        mBinder.cleanup();
    }

    @Test
    public void getAddress() {
        mBinder.getAddress();
        verify(mService.mAdapterProperties).getAddress();
    }

    @Test
    public void dump() {
        FileDescriptor fd = new FileDescriptor();
        String[] args = new String[] { };
        mBinder.dump(fd, args);
        verify(mService).dump(any(), any(), any());

        Mockito.clearInvocations(mService);
        mBinder.cleanup();
        mBinder.dump(fd, args);
        verify(mService, never()).dump(any(), any(), any());
    }

    @Test
    public void generateLocalOobData() {
        int transport = 0;
        IBluetoothOobDataCallback cb = Mockito.mock(IBluetoothOobDataCallback.class);

        mBinder.generateLocalOobData(transport, cb, mAttributionSource,
                SynchronousResultReceiver.get());
        verify(mService).generateLocalOobData(transport, cb);

        Mockito.clearInvocations(mService);
        mBinder.cleanup();
        mBinder.generateLocalOobData(transport, cb, mAttributionSource,
                SynchronousResultReceiver.get());
        verify(mService, never()).generateLocalOobData(transport, cb);
    }

    @Test
    public void getBluetoothClass() {
        mBinder.getBluetoothClass(mAttributionSource, SynchronousResultReceiver.get());
        verify(mService.mAdapterProperties).getBluetoothClass();
    }

    @Test
    public void getIoCapability() {
        mBinder.getIoCapability(mAttributionSource, SynchronousResultReceiver.get());
        verify(mService.mAdapterProperties).getIoCapability();
    }

    @Test
    public void getLeIoCapability() {
        mBinder.getLeIoCapability(mAttributionSource, SynchronousResultReceiver.get());
        verify(mService.mAdapterProperties).getLeIoCapability();
    }

    @Test
    public void getLeMaximumAdvertisingDataLength() {
        mBinder.getLeMaximumAdvertisingDataLength(SynchronousResultReceiver.get());
        verify(mService).getLeMaximumAdvertisingDataLength();
    }

    @Test
    public void getScanMode() {
        mBinder.getScanMode(mAttributionSource, SynchronousResultReceiver.get());
        verify(mService.mAdapterProperties).getScanMode();
    }

    @Test
    public void isA2dpOffloadEnabled() {
        mBinder.isA2dpOffloadEnabled(mAttributionSource, SynchronousResultReceiver.get());
        verify(mService).isA2dpOffloadEnabled();
    }

    @Test
    public void isActivityAndEnergyReportingSupported() {
        mBinder.isActivityAndEnergyReportingSupported(SynchronousResultReceiver.get());
        verify(mService.mAdapterProperties).isActivityAndEnergyReportingSupported();
    }

    @Test
    public void isLe2MPhySupported() {
        mBinder.isLe2MPhySupported(SynchronousResultReceiver.get());
        verify(mService).isLe2MPhySupported();
    }

    @Test
    public void isLeCodedPhySupported() {
        mBinder.isLeCodedPhySupported(SynchronousResultReceiver.get());
        verify(mService).isLeCodedPhySupported();
    }

    @Test
    public void isLeExtendedAdvertisingSupported() {
        mBinder.isLeExtendedAdvertisingSupported(SynchronousResultReceiver.get());
        verify(mService).isLeExtendedAdvertisingSupported();
    }

    @Test
    public void removeActiveDevice() {
        int profiles = BluetoothAdapter.ACTIVE_DEVICE_ALL;
        mBinder.removeActiveDevice(profiles, mAttributionSource, SynchronousResultReceiver.get());
        verify(mService).setActiveDevice(null, profiles);
    }

    @Test
    public void reportActivityInfo() {
        mBinder.reportActivityInfo(mAttributionSource, SynchronousResultReceiver.get());
        verify(mService).reportActivityInfo();
    }

    @Test
    public void retrievePendingSocketForServiceRecord() {
        ParcelUuid uuid = ParcelUuid.fromString("0000110A-0000-1000-8000-00805F9B34FB");
        mBinder.retrievePendingSocketForServiceRecord(uuid, mAttributionSource,
                SynchronousResultReceiver.get());
        verify(mService).retrievePendingSocketForServiceRecord(uuid, mAttributionSource);
    }

    @Test
    public void setBluetoothClass() {
        BluetoothClass btClass = new BluetoothClass(0);
        mBinder.setBluetoothClass(btClass, mAttributionSource, SynchronousResultReceiver.get());
        verify(mService.mAdapterProperties).setBluetoothClass(btClass);
    }

    @Test
    public void setIoCapability() {
        int capability = BluetoothAdapter.IO_CAPABILITY_MAX - 1;
        mBinder.setIoCapability(capability, mAttributionSource, SynchronousResultReceiver.get());
        verify(mService.mAdapterProperties).setIoCapability(capability);
    }

    @Test
    public void setLeIoCapability() {
        int capability = BluetoothAdapter.IO_CAPABILITY_MAX - 1;
        mBinder.setLeIoCapability(capability, mAttributionSource, SynchronousResultReceiver.get());
        verify(mService.mAdapterProperties).setLeIoCapability(capability);
    }

    @Test
    public void stopRfcommListener() {
        ParcelUuid uuid = ParcelUuid.fromString("0000110A-0000-1000-8000-00805F9B34FB");
        mBinder.stopRfcommListener(uuid, mAttributionSource, SynchronousResultReceiver.get());
        verify(mService).stopRfcommListener(uuid, mAttributionSource);
    }
}
+87 −1
Original line number Diff line number Diff line
/*
 * Copyright 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.bluetooth.gatt;

import static com.google.common.truth.Truth.assertThat;

import static org.mockito.Mockito.*;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothGatt;
import android.content.AttributionSource;
import android.content.Context;

import androidx.test.InstrumentationRegistry;
@@ -29,12 +50,20 @@ import org.mockito.MockitoAnnotations;
@SmallTest
@RunWith(AndroidJUnit4.class)
public class GattServiceTest {

    private static final String REMOTE_DEVICE_ADDRESS = "00:00:00:00:00:00";

    private static final int TIMES_UP_AND_DOWN = 3;
    private Context mTargetContext;
    private GattService mService;
    @Mock private GattService.ClientMap mClientMap;
    @Mock private GattService.ScannerMap mScannerMap;

    @Rule public final ServiceTestRule mServiceRule = new ServiceTestRule();

    private BluetoothAdapter mAdapter;
    private AttributionSource mAttributionSource;

    @Mock private AdapterService mAdapterService;

    @Before
@@ -44,9 +73,16 @@ public class GattServiceTest {
        MockitoAnnotations.initMocks(this);
        TestUtils.setAdapterService(mAdapterService);
        doReturn(true).when(mAdapterService).isStartedProfile(anyString());

        mAdapter = BluetoothAdapter.getDefaultAdapter();
        mAttributionSource = mAdapter.getAttributionSource();

        TestUtils.startService(mServiceRule, GattService.class);
        mService = GattService.getGattService();
        Assert.assertNotNull(mService);

        mService.mClientMap = mClientMap;
        mService.mScannerMap = mScannerMap;
    }

    @After
@@ -63,7 +99,7 @@ public class GattServiceTest {

    @Test
    public void testInitialize() {
        Assert.assertNotNull(GattService.getGattService());
        Assert.assertEquals(mService, GattService.getGattService());
    }

    @Test
@@ -93,6 +129,56 @@ public class GattServiceTest {
        Assert.assertEquals(99700000000L, timestampNanos);
    }

    public void emptyClearServices() {
        int serverIf = 1;

        mService.clearServices(serverIf, mAttributionSource);
    }

    @Test
    public void clientReadPhy() {
        int clientIf = 1;
        String address = REMOTE_DEVICE_ADDRESS;

        Integer connId = 1;
        doReturn(connId).when(mClientMap).connIdByAddress(clientIf, address);

        mService.clientReadPhy(clientIf, address, mAttributionSource);
    }

    @Test
    public void clientSetPreferredPhy() {
        int clientIf = 1;
        String address = REMOTE_DEVICE_ADDRESS;
        int txPhy = 2;
        int rxPhy = 1;
        int phyOptions = 3;

        Integer connId = 1;
        doReturn(connId).when(mClientMap).connIdByAddress(clientIf, address);

        mService.clientSetPreferredPhy(clientIf, address, txPhy, rxPhy, phyOptions,
                mAttributionSource);
    }

    @Test
    public void connectionParameterUpdate() {
        int clientIf = 1;
        String address = REMOTE_DEVICE_ADDRESS;

        int connectionPriority = BluetoothGatt.CONNECTION_PRIORITY_HIGH;
        mService.connectionParameterUpdate(clientIf, address, connectionPriority,
                mAttributionSource);

        connectionPriority = BluetoothGatt.CONNECTION_PRIORITY_LOW_POWER;
        mService.connectionParameterUpdate(clientIf, address, connectionPriority,
                mAttributionSource);

        connectionPriority = BluetoothGatt.CONNECTION_PRIORITY_BALANCED;;
        mService.connectionParameterUpdate(clientIf, address, connectionPriority,
                mAttributionSource);
    }

    @Test
    public void testDumpDoesNotCrash() {
        mService.dump(new StringBuilder());
+4 −0
Original line number Diff line number Diff line
@@ -68,6 +68,7 @@ import com.android.bluetooth.vc.VolumeControlService;
import org.junit.After;
import org.junit.Assume;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -1127,6 +1128,7 @@ public class LeAudioServiceTest {
     * Test native interface audio configuration changed message handling
     */
    @Test
    @Ignore("b/258573934")
    public void testMessageFromNativeAudioConfChangedActiveGroup() {
        doReturn(true).when(mNativeInterface).connectLeAudio(any(BluetoothDevice.class));
        connectTestDevice(mSingleDevice, testGroupId);
@@ -1312,6 +1314,7 @@ public class LeAudioServiceTest {
     * Test native interface group status message handling
     */
    @Test
    @Ignore("b/258573934")
    public void testLeadGroupDeviceDisconnects() {
        int groupId = 1;
        /* AUDIO_DIRECTION_OUTPUT_BIT = 0x01 */
@@ -1381,6 +1384,7 @@ public class LeAudioServiceTest {
     * Test native interface group status message handling
     */
    @Test
    @Ignore("b/258573934")
    public void testLeadGroupDeviceReconnects() {
        int groupId = 1;
        /* AUDIO_DIRECTION_OUTPUT_BIT = 0x01 */
Loading