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

Commit 36d2223e authored by Michał Narajowski's avatar Michał Narajowski
Browse files

TBS: Fix unauthorized access response

Previously the TBS access request resulted in no response from the stack
which resulted in ATT Timeout. With this change we respond with
insufficient authorization.

Bug: 263372634
Test: manual and atest BluetoothInstrumentationTests
Tag: #feature
Change-Id: I8e1a8e2ff6322099d8ccca44ddd47b7286703a16
parent 732a57d9
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -1025,7 +1025,7 @@ public class TbsGatt {
            operations.add(op);
            // Send authorization request for each device only for it's first GATT request
            if (operations.size() == 1) {
                mTbsService.getDeviceAuthorization(device);
                mTbsService.onDeviceUnauthorized(device);
            }
        }
    }
+10 −0
Original line number Diff line number Diff line
@@ -139,6 +139,16 @@ public class TbsService extends ProfileService {
        sTbsService = instance;
    }

    public void onDeviceUnauthorized(BluetoothDevice device) {
        if (Utils.isPtsTestMode()) {
            Log.d(TAG, "PTS test: setDeviceAuthorized");
            setDeviceAuthorized(device, true);
            return;
        }
        Log.w(TAG, "onDeviceUnauthorized - authorization notification not implemented yet ");
        setDeviceAuthorized(device, false);
    }

    /**
     * Sets device authorization for TBS.
     *
+78 −4
Original line number Diff line number Diff line
@@ -818,7 +818,7 @@ public class TbsGattTest {
    }

    @Test
    public void testCharacteristicReadUnauthorized() {
    public void testCharacteristicReadAccessRejectedUnauthorized() {
        prepareDefaultService();

        BluetoothGattCharacteristic characteristic =
@@ -837,7 +837,24 @@ public class TbsGattTest {
    }

    @Test
    public void testCharacteristicWriteUnauthorized() {
    public void testCharacteristicReadAccessUnknownUnauthorized() {
        prepareDefaultService();

        BluetoothGattCharacteristic characteristic =
                getCharacteristic(TbsGatt.UUID_BEARER_TECHNOLOGY);

        doReturn(BluetoothDevice.ACCESS_UNKNOWN)
                .when(mMockTbsService)
                .getDeviceAuthorization(any(BluetoothDevice.class));

        mTbsGatt.mGattServerCallback.onCharacteristicReadRequest(
                mFirstDevice, 1, 0, characteristic);

        verify(mMockTbsService).onDeviceUnauthorized(eq(mFirstDevice));
    }

    @Test
    public void testCharacteristicWriteAccessRejectedUnauthorized() {
        prepareDefaultService();

        BluetoothGattCharacteristic characteristic =
@@ -858,7 +875,26 @@ public class TbsGattTest {
    }

    @Test
    public void testDescriptorReadUnauthorized() {
    public void testCharacteristicWriteAccessUnknownUnauthorized() {
        prepareDefaultService();

        BluetoothGattCharacteristic characteristic =
                getCharacteristic(TbsGatt.UUID_CALL_CONTROL_POINT);

        doReturn(BluetoothDevice.ACCESS_UNKNOWN)
                .when(mMockTbsService)
                .getDeviceAuthorization(any(BluetoothDevice.class));

        byte[] value = new byte[] {0x00, /* opcode */ 0x0A, /* argument */ };

        mTbsGatt.mGattServerCallback.onCharacteristicWriteRequest(
                mFirstDevice, 1, characteristic, false, true, 0, value);

        verify(mMockTbsService).onDeviceUnauthorized(eq(mFirstDevice));
    }

    @Test
    public void testDescriptorReadAccessRejectedUnauthorized() {
        prepareDefaultService();

        BluetoothGattDescriptor descriptor =
@@ -878,7 +914,25 @@ public class TbsGattTest {
    }

    @Test
    public void testDescriptorWriteUnauthorized() {
    public void testDescriptorReadAccessUnknownUnauthorized() {
        prepareDefaultService();

        BluetoothGattDescriptor descriptor =
                getCharacteristic(TbsGatt.UUID_BEARER_TECHNOLOGY)
                        .getDescriptor(TbsGatt.UUID_CLIENT_CHARACTERISTIC_CONFIGURATION);
        Assert.assertNotNull(descriptor);

        doReturn(BluetoothDevice.ACCESS_UNKNOWN)
                .when(mMockTbsService)
                .getDeviceAuthorization(any(BluetoothDevice.class));

        mTbsGatt.mGattServerCallback.onDescriptorReadRequest(mFirstDevice, 1, 0, descriptor);

        verify(mMockTbsService).onDeviceUnauthorized(eq(mFirstDevice));
    }

    @Test
    public void testDescriptorWriteAccessRejectedUnauthorized() {
        prepareDefaultService();

        BluetoothGattDescriptor descriptor =
@@ -900,4 +954,24 @@ public class TbsGattTest {
                        eq(BluetoothGatt.GATT_INSUFFICIENT_AUTHORIZATION), eq(0), any());
    }

    @Test
    public void testDescriptorWriteAccessUnknownUnauthorized() {
        prepareDefaultService();

        BluetoothGattDescriptor descriptor =
                getCharacteristic(TbsGatt.UUID_CALL_CONTROL_POINT)
                        .getDescriptor(TbsGatt.UUID_CLIENT_CHARACTERISTIC_CONFIGURATION);
        Assert.assertNotNull(descriptor);

        doReturn(BluetoothDevice.ACCESS_UNKNOWN)
                .when(mMockTbsService)
                .getDeviceAuthorization(any(BluetoothDevice.class));

        byte[] value = new byte[] {0x00, /* opcode */ 0x0A, /* argument */ };

        mTbsGatt.mGattServerCallback.onDescriptorWriteRequest(
                mFirstDevice, 1, descriptor, false, true, 0, value);

        verify(mMockTbsService).onDeviceUnauthorized(eq(mFirstDevice));
    }
}