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

Commit 78721e8a authored by Sungsoo Lim's avatar Sungsoo Lim
Browse files

Add AdapterServiceBinderTest

Bug: 237467631
Test: atest BluetoothInstrumentationTests:AdapterServiceBinderTest
Change-Id: I0e97d4b031ab015688e00efb83e5aa0404a4e45c
parent 4e0b17f4
Loading
Loading
Loading
Loading
+15 −2
Original line number Diff line number Diff line
@@ -426,6 +426,9 @@ public final class Utils {

    @RequiresPermission(android.Manifest.permission.BLUETOOTH_PRIVILEGED)
    public static void enforceBluetoothPrivilegedPermission(Context context) {
        if (isInstrumentationTestMode()) {
            return;
        }
        context.enforceCallingOrSelfPermission(
                android.Manifest.permission.BLUETOOTH_PRIVILEGED,
                "Need BLUETOOTH PRIVILEGED permission");
@@ -433,6 +436,9 @@ public final class Utils {

    @RequiresPermission(android.Manifest.permission.LOCAL_MAC_ADDRESS)
    public static void enforceLocalMacAddressPermission(Context context) {
        if (isInstrumentationTestMode()) {
            return;
        }
        context.enforceCallingOrSelfPermission(
                android.Manifest.permission.LOCAL_MAC_ADDRESS,
                "Need LOCAL_MAC_ADDRESS permission");
@@ -440,13 +446,14 @@ public final class Utils {

    @RequiresPermission(android.Manifest.permission.DUMP)
    public static void enforceDumpPermission(Context context) {
        if (isInstrumentationTestMode()) {
            return;
        }
        context.enforceCallingOrSelfPermission(
                android.Manifest.permission.DUMP,
                "Need DUMP permission");
    }

    /**
     */
    public static AttributionSource getCallingAttributionSource(Context context) {
        int callingUid = Binder.getCallingUid();
        if (callingUid == android.os.Process.ROOT_UID) {
@@ -481,6 +488,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
@@ -687,6 +697,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"
+12 −6
Original line number Diff line number Diff line
@@ -263,7 +263,8 @@ public class AdapterService extends Service {
    }

    private BluetoothAdapter mAdapter;
    private AdapterProperties mAdapterProperties;
    @VisibleForTesting
    AdapterProperties mAdapterProperties;
    private AdapterState mAdapterStateMachine;
    private BondStateMachine mBondStateMachine;
    private JniCallbacks mJniCallbacks;
@@ -1383,7 +1384,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) {
@@ -1402,7 +1404,8 @@ public class AdapterService extends Service {
        return listenerData.closeServerAndPendingSockets(mHandler);
    }

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

@@ -1573,7 +1576,8 @@ public class AdapterService extends Service {
        }
    }

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

@@ -4105,7 +4109,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;
@@ -5035,7 +5040,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;
+205 −0
Original line number Diff line number Diff line
/*
 * Copyright 2022 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.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.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;

    private AdapterService.AdapterServiceBinder mBinder;
    private AttributionSource mAttributionSource;

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
        mService.mAdapterProperties = mAdapterProperties;
        doReturn(true).when(mService).isAvailable();
        mBinder = new AdapterService.AdapterServiceBinder(mService);
        mAttributionSource = new AttributionSource.Builder(0).build();
    }

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

    @Test
    public void getAddress() {
        mBinder.getAddress(mAttributionSource, SynchronousResultReceiver.get());
        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);
    }
}