Restrict guest user to configure WiFi by QR code
- Don't show WifiDppConfiguratorActivity if the user is a guest. Bug: 224772890 Test: manual test make RunSettingsRoboTests ROBOTEST_FILTER=WifiDppConfiguratorActivityTest Change-Id: I160761edfe2893475676421ba2b59205da8d0224
This commit is contained in:
@@ -17,13 +17,16 @@
|
||||
package com.android.settings.wifi.dpp;
|
||||
|
||||
import android.app.settings.SettingsEnums;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.net.wifi.WifiConfiguration;
|
||||
import android.net.wifi.WifiInfo;
|
||||
import android.net.wifi.WifiManager;
|
||||
import android.os.Bundle;
|
||||
import android.os.UserManager;
|
||||
import android.provider.Settings;
|
||||
import android.util.EventLog;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
@@ -116,6 +119,13 @@ public class WifiDppConfiguratorActivity extends WifiDppBaseActivity implements
|
||||
|
||||
@Override
|
||||
protected void handleIntent(Intent intent) {
|
||||
if (isGuestUser(getApplicationContext())) {
|
||||
Log.e(TAG, "Guest user is not allowed to configure Wi-Fi!");
|
||||
EventLog.writeEvent(0x534e4554, "224772890", -1 /* UID */, "User is a guest");
|
||||
finish();
|
||||
return;
|
||||
}
|
||||
|
||||
String action = intent != null ? intent.getAction() : null;
|
||||
if (action == null) {
|
||||
finish();
|
||||
@@ -185,7 +195,8 @@ public class WifiDppConfiguratorActivity extends WifiDppBaseActivity implements
|
||||
}
|
||||
}
|
||||
|
||||
private void showQrCodeScannerFragment() {
|
||||
@VisibleForTesting
|
||||
void showQrCodeScannerFragment() {
|
||||
WifiDppQrCodeScannerFragment fragment =
|
||||
(WifiDppQrCodeScannerFragment) mFragmentManager.findFragmentByTag(
|
||||
WifiDppUtils.TAG_FRAGMENT_QR_CODE_SCANNER);
|
||||
@@ -384,4 +395,11 @@ public class WifiDppConfiguratorActivity extends WifiDppBaseActivity implements
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private static boolean isGuestUser(Context context) {
|
||||
if (context == null) return false;
|
||||
final UserManager userManager = context.getSystemService(UserManager.class);
|
||||
if (userManager == null) return false;
|
||||
return userManager.isGuestUser();
|
||||
}
|
||||
}
|
||||
|
@@ -16,16 +16,78 @@
|
||||
|
||||
package com.android.settings.wifi.dpp;
|
||||
|
||||
import static org.mockito.Mockito.doNothing;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.UserManager;
|
||||
|
||||
import androidx.test.core.app.ApplicationProvider;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Spy;
|
||||
import org.mockito.junit.MockitoJUnit;
|
||||
import org.mockito.junit.MockitoRule;
|
||||
import org.robolectric.Robolectric;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public class WifiDppConfiguratorActivityTest {
|
||||
|
||||
@Rule
|
||||
public final MockitoRule mMockitoRule = MockitoJUnit.rule();
|
||||
@Spy
|
||||
Context mContext = ApplicationProvider.getApplicationContext();
|
||||
@Mock
|
||||
UserManager mUserManager;
|
||||
|
||||
WifiDppConfiguratorActivity mActivity;
|
||||
Intent mIntent;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
when(mContext.getSystemService(UserManager.class)).thenReturn(mUserManager);
|
||||
when(mUserManager.isGuestUser()).thenReturn(false);
|
||||
|
||||
mIntent = new Intent(WifiDppConfiguratorActivity.ACTION_CONFIGURATOR_QR_CODE_SCANNER);
|
||||
mIntent.putExtra(WifiDppUtils.EXTRA_WIFI_SSID, "GoogleGuest");
|
||||
mIntent.putExtra(WifiDppUtils.EXTRA_WIFI_SECURITY, "WPA");
|
||||
mIntent.putExtra(WifiDppUtils.EXTRA_WIFI_PRE_SHARED_KEY, "\\012345678,");
|
||||
|
||||
mActivity = spy(Robolectric.setupActivity(WifiDppConfiguratorActivity.class));
|
||||
when(mActivity.getApplicationContext()).thenReturn(mContext);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void launchActivity_noIntentAction_shouldNotFatalException() {
|
||||
WifiDppConfiguratorActivity wifiDppConfiguratorActivity =
|
||||
Robolectric.setupActivity(WifiDppConfiguratorActivity.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void handleIntent_isGuestUser_shouldFinish() {
|
||||
when(mUserManager.isGuestUser()).thenReturn(true);
|
||||
|
||||
mActivity.handleIntent(mIntent);
|
||||
|
||||
verify(mActivity).finish();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void handleIntent_notGuestUser_shouldNotFinish() {
|
||||
when(mUserManager.isGuestUser()).thenReturn(false);
|
||||
doNothing().when(mActivity).showQrCodeScannerFragment();
|
||||
|
||||
mActivity.handleIntent(mIntent);
|
||||
|
||||
verify(mActivity, never()).finish();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user