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

Commit 3fecfc6c authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Block guest user from Bluetooth tethering connect/disconnect" into qt-qpr1-dev

parents 191f5244 728920aa
Loading
Loading
Loading
Loading
+13 −0
Original line number Original line Diff line number Diff line
@@ -40,6 +40,7 @@ import com.android.bluetooth.Utils;
import com.android.bluetooth.btservice.AdapterService;
import com.android.bluetooth.btservice.AdapterService;
import com.android.bluetooth.btservice.MetricsLogger;
import com.android.bluetooth.btservice.MetricsLogger;
import com.android.bluetooth.btservice.ProfileService;
import com.android.bluetooth.btservice.ProfileService;
import com.android.internal.annotations.VisibleForTesting;


import java.net.InetAddress;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.ArrayList;
@@ -67,6 +68,9 @@ public class PanService extends ProfileService {
    private String mNapIfaceAddr;
    private String mNapIfaceAddr;
    private boolean mNativeAvailable;
    private boolean mNativeAvailable;


    @VisibleForTesting
    UserManager mUserManager;

    private static final int MESSAGE_CONNECT = 1;
    private static final int MESSAGE_CONNECT = 1;
    private static final int MESSAGE_DISCONNECT = 2;
    private static final int MESSAGE_DISCONNECT = 2;
    private static final int MESSAGE_CONNECT_STATE_CHANGED = 11;
    private static final int MESSAGE_CONNECT_STATE_CHANGED = 11;
@@ -116,6 +120,8 @@ public class PanService extends ProfileService {
        initializeNative();
        initializeNative();
        mNativeAvailable = true;
        mNativeAvailable = true;


        mUserManager = (UserManager) getSystemService(Context.USER_SERVICE);

        mNetworkFactory =
        mNetworkFactory =
                new BluetoothTetheringNetworkFactory(getBaseContext(), getMainLooper(), this);
                new BluetoothTetheringNetworkFactory(getBaseContext(), getMainLooper(), this);
        setPanService(this);
        setPanService(this);
@@ -137,6 +143,9 @@ public class PanService extends ProfileService {
            cleanupNative();
            cleanupNative();
            mNativeAvailable = false;
            mNativeAvailable = false;
        }
        }

        mUserManager = null;

        if (mPanDevices != null) {
        if (mPanDevices != null) {
           int[] desiredStates = {BluetoothProfile.STATE_CONNECTING, BluetoothProfile.STATE_CONNECTED,
           int[] desiredStates = {BluetoothProfile.STATE_CONNECTING, BluetoothProfile.STATE_CONNECTED,
                                  BluetoothProfile.STATE_DISCONNECTING};
                                  BluetoothProfile.STATE_DISCONNECTING};
@@ -319,6 +328,10 @@ public class PanService extends ProfileService {


    public boolean connect(BluetoothDevice device) {
    public boolean connect(BluetoothDevice device) {
        enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
        enforceCallingOrSelfPermission(BLUETOOTH_PERM, "Need BLUETOOTH permission");
        if (mUserManager.isGuestUser()) {
            Log.w(TAG, "Guest user does not have the permission to change the WiFi network");
            return false;
        }
        if (getConnectionState(device) != BluetoothProfile.STATE_DISCONNECTED) {
        if (getConnectionState(device) != BluetoothProfile.STATE_DISCONNECTED) {
            Log.e(TAG, "Pan Device not disconnected: " + device);
            Log.e(TAG, "Pan Device not disconnected: " + device);
            return false;
            return false;
+13 −0
Original line number Original line Diff line number Diff line
@@ -15,8 +15,12 @@
 */
 */
package com.android.bluetooth.pan;
package com.android.bluetooth.pan;


import static org.mockito.Mockito.when;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.content.Context;
import android.os.UserManager;


import androidx.test.InstrumentationRegistry;
import androidx.test.InstrumentationRegistry;
import androidx.test.filters.MediumTest;
import androidx.test.filters.MediumTest;
@@ -47,6 +51,7 @@ public class PanServiceTest {
    @Rule public final ServiceTestRule mServiceRule = new ServiceTestRule();
    @Rule public final ServiceTestRule mServiceRule = new ServiceTestRule();


    @Mock private AdapterService mAdapterService;
    @Mock private AdapterService mAdapterService;
    @Mock private UserManager mMockUserManager;


    @Before
    @Before
    public void setUp() throws Exception {
    public void setUp() throws Exception {
@@ -61,6 +66,7 @@ public class PanServiceTest {
        // Try getting the Bluetooth adapter
        // Try getting the Bluetooth adapter
        mAdapter = BluetoothAdapter.getDefaultAdapter();
        mAdapter = BluetoothAdapter.getDefaultAdapter();
        Assert.assertNotNull(mAdapter);
        Assert.assertNotNull(mAdapter);
        mService.mUserManager = mMockUserManager;
    }
    }


    @After
    @After
@@ -78,4 +84,11 @@ public class PanServiceTest {
    public void testInitialize() {
    public void testInitialize() {
        Assert.assertNotNull(PanService.getPanService());
        Assert.assertNotNull(PanService.getPanService());
    }
    }

    @Test
    public void testGuestUserConnect() {
        BluetoothDevice device = TestUtils.getTestDevice(mAdapter, 0);
        when(mMockUserManager.isGuestUser()).thenReturn(true);
        Assert.assertFalse(mService.connect(device));
    }
}
}