Make user explicitly set security type for tether network
This was made implicit which is confusing for the user so this CL adds a preference to make the user manually choose their security type. Test: robotests Fixes: 79435112 Change-Id: Ie78806e8952b52e1b7cd21f0b87c9d064acaff64 Merged-In: Ie78806e8952b52e1b7cd21f0b87c9d064acaff64
This commit is contained in:
committed by
Andrew Sapperstein
parent
99902e1faf
commit
d9bae5a15e
@@ -43,6 +43,6 @@ public class WifiUtilsTest {
|
||||
assertThat(WifiUtils.isHotspotPasswordValid("12345678")).isTrue();
|
||||
assertThat(WifiUtils.isHotspotPasswordValid("1234567890")).isTrue();
|
||||
assertThat(WifiUtils.isHotspotPasswordValid(longPassword)).isFalse();
|
||||
assertThat(WifiUtils.isHotspotPasswordValid("")).isTrue();
|
||||
assertThat(WifiUtils.isHotspotPasswordValid("")).isFalse();
|
||||
}
|
||||
}
|
||||
|
@@ -42,6 +42,8 @@ import org.robolectric.RuntimeEnvironment;
|
||||
@RunWith(SettingsRobolectricTestRunner.class)
|
||||
public class WifiTetherPasswordPreferenceControllerTest {
|
||||
|
||||
private static final String VALID_PASS = "12345678";
|
||||
private static final String VALID_PASS2 = "23456789";
|
||||
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
||||
private Context mContext;
|
||||
@Mock
|
||||
@@ -87,11 +89,13 @@ public class WifiTetherPasswordPreferenceControllerTest {
|
||||
@Test
|
||||
public void changePreference_shouldUpdateValue() {
|
||||
mController.displayPreference(mScreen);
|
||||
mController.onPreferenceChange(mPreference, "1");
|
||||
assertThat(mController.getPassword()).isEqualTo("1");
|
||||
mController.onPreferenceChange(mPreference, VALID_PASS);
|
||||
assertThat(mController.getPasswordValidated(WifiConfiguration.KeyMgmt.WPA2_PSK))
|
||||
.isEqualTo(VALID_PASS);
|
||||
|
||||
mController.onPreferenceChange(mPreference, "0");
|
||||
assertThat(mController.getPassword()).isEqualTo("0");
|
||||
mController.onPreferenceChange(mPreference, VALID_PASS2);
|
||||
assertThat(mController.getPasswordValidated(WifiConfiguration.KeyMgmt.WPA2_PSK))
|
||||
.isEqualTo(VALID_PASS2);
|
||||
|
||||
verify(mListener, times(2)).onTetherConfigUpdated();
|
||||
}
|
||||
@@ -100,62 +104,33 @@ public class WifiTetherPasswordPreferenceControllerTest {
|
||||
public void updateDisplay_shouldUpdateValue() {
|
||||
// Set controller password to anything and verify is set.
|
||||
mController.displayPreference(mScreen);
|
||||
mController.onPreferenceChange(mPreference, "1");
|
||||
assertThat(mController.getPassword()).isEqualTo("1");
|
||||
mController.onPreferenceChange(mPreference, VALID_PASS);
|
||||
assertThat(mController.getPasswordValidated(WifiConfiguration.KeyMgmt.WPA2_PSK))
|
||||
.isEqualTo(VALID_PASS);
|
||||
|
||||
// Create a new config using different password
|
||||
final WifiConfiguration config = new WifiConfiguration();
|
||||
config.preSharedKey = "test_1234";
|
||||
config.preSharedKey = VALID_PASS2;
|
||||
when(mWifiManager.getWifiApConfiguration()).thenReturn(config);
|
||||
|
||||
// Call updateDisplay and verify it's changed.
|
||||
mController.updateDisplay();
|
||||
assertThat(mController.getPassword()).isEqualTo(config.preSharedKey);
|
||||
assertThat(mController.getPasswordValidated(WifiConfiguration.KeyMgmt.WPA2_PSK))
|
||||
.isEqualTo(config.preSharedKey);
|
||||
assertThat(mPreference.getSummary()).isEqualTo(config.preSharedKey);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getSecuritySettingForPassword_returnCorrectType() {
|
||||
// valid wpa2 password
|
||||
mController.displayPreference(mScreen);
|
||||
assertThat(mController.getSecuritySettingForPassword())
|
||||
.isEqualTo(WifiConfiguration.KeyMgmt.WPA2_PSK);
|
||||
|
||||
// password which is empty returns NONE
|
||||
mConfig = new WifiConfiguration();
|
||||
mConfig.SSID = "test_1234";
|
||||
mConfig.preSharedKey = "";
|
||||
when(mContext.getSystemService(Context.WIFI_SERVICE)).thenReturn(mWifiManager);
|
||||
when(mWifiManager.getWifiApConfiguration()).thenReturn(mConfig);
|
||||
mController = new WifiTetherPasswordPreferenceController(mContext, mListener);
|
||||
|
||||
mController.displayPreference(mScreen);
|
||||
assertThat(mController.getSecuritySettingForPassword())
|
||||
.isEqualTo(WifiConfiguration.KeyMgmt.NONE);
|
||||
|
||||
// default for unsupported types is wpa2
|
||||
mConfig = new WifiConfiguration();
|
||||
mConfig.SSID = "test_1234";
|
||||
mConfig.preSharedKey = "short";
|
||||
when(mContext.getSystemService(Context.WIFI_SERVICE)).thenReturn(mWifiManager);
|
||||
when(mWifiManager.getWifiApConfiguration()).thenReturn(mConfig);
|
||||
mController = new WifiTetherPasswordPreferenceController(mContext, mListener);
|
||||
|
||||
mController.displayPreference(mScreen);
|
||||
assertThat(mController.getSecuritySettingForPassword())
|
||||
.isEqualTo(WifiConfiguration.KeyMgmt.WPA2_PSK);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateDisplay_shouldSetInputType() {
|
||||
// Set controller password to anything and verify is set.
|
||||
mController.displayPreference(mScreen);
|
||||
mController.onPreferenceChange(mPreference, "1");
|
||||
assertThat(mController.getPassword()).isEqualTo("1");
|
||||
mController.onPreferenceChange(mPreference, VALID_PASS);
|
||||
assertThat(mController.getPasswordValidated(WifiConfiguration.KeyMgmt.WPA2_PSK))
|
||||
.isEqualTo(VALID_PASS);
|
||||
|
||||
// Create a new config using different password
|
||||
final WifiConfiguration config = new WifiConfiguration();
|
||||
config.preSharedKey = "test_1234";
|
||||
config.preSharedKey = VALID_PASS2;
|
||||
when(mWifiManager.getWifiApConfiguration()).thenReturn(config);
|
||||
|
||||
// Call updateDisplay and verify it's changed.
|
||||
|
@@ -0,0 +1,99 @@
|
||||
package com.android.settings.wifi.tether;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import android.content.Context;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.wifi.WifiConfiguration;
|
||||
import android.net.wifi.WifiManager;
|
||||
import android.support.v7.preference.ListPreference;
|
||||
import android.support.v7.preference.PreferenceScreen;
|
||||
|
||||
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Answers;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
|
||||
@RunWith(SettingsRobolectricTestRunner.class)
|
||||
public class WifiTetherSecurityPreferenceControllerTest {
|
||||
|
||||
private static final String WPA2_PSK = String.valueOf(WifiConfiguration.KeyMgmt.WPA2_PSK);
|
||||
private static final String NONE = String.valueOf(WifiConfiguration.KeyMgmt.NONE);
|
||||
@Mock
|
||||
private WifiTetherBasePreferenceController.OnTetherConfigUpdateListener mListener;
|
||||
private Context mContext;
|
||||
@Mock
|
||||
private ConnectivityManager mConnectivityManager;
|
||||
@Mock
|
||||
private WifiManager mWifiManager;
|
||||
@Mock
|
||||
private PreferenceScreen mScreen;
|
||||
private WifiTetherSecurityPreferenceController mController;
|
||||
private ListPreference mPreference;
|
||||
private WifiConfiguration mConfig;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
mConfig = new WifiConfiguration();
|
||||
mConfig.SSID = "test_1234";
|
||||
mConfig.preSharedKey = "test_password";
|
||||
mConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA2_PSK);
|
||||
mContext = spy(RuntimeEnvironment.application);
|
||||
|
||||
when(mContext.getSystemService(Context.WIFI_SERVICE)).thenReturn(mWifiManager);
|
||||
when(mWifiManager.getWifiApConfiguration()).thenReturn(mConfig);
|
||||
when(mContext.getSystemService(Context.CONNECTIVITY_SERVICE))
|
||||
.thenReturn(mConnectivityManager);
|
||||
when(mConnectivityManager.getTetherableWifiRegexs()).thenReturn(new String[]{"1", "2"});
|
||||
when(mScreen.findPreference(anyString())).thenReturn(mPreference);
|
||||
|
||||
mController = new WifiTetherSecurityPreferenceController(mContext, mListener);
|
||||
mPreference = new ListPreference(RuntimeEnvironment.application);
|
||||
mController.mPreference = mPreference;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onPreferenceChange_securityValueUpdated() {
|
||||
mController.onPreferenceChange(mPreference, WPA2_PSK);
|
||||
assertThat(mController.getSecurityType()).isEqualTo(WifiConfiguration.KeyMgmt.WPA2_PSK);
|
||||
assertThat(mPreference.getSummary()).isEqualTo("WPA2 PSK");
|
||||
|
||||
mController.onPreferenceChange(mPreference, NONE);
|
||||
assertThat(mController.getSecurityType()).isEqualTo(WifiConfiguration.KeyMgmt.NONE);
|
||||
assertThat(mPreference.getSummary()).isEqualTo("None");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateDisplay_preferenceUpdated() {
|
||||
// test defaulting to WPA2 PSK on new config
|
||||
when(mWifiManager.getWifiApConfiguration()).thenReturn(null);
|
||||
mController.updateDisplay();
|
||||
assertThat(mController.getSecurityType()).isEqualTo(WifiConfiguration.KeyMgmt.WPA2_PSK);
|
||||
assertThat(mPreference.getSummary()).isEqualTo("WPA2 PSK");
|
||||
|
||||
// test open tether network
|
||||
when(mWifiManager.getWifiApConfiguration()).thenReturn(mConfig);
|
||||
mConfig.allowedKeyManagement.clear();
|
||||
mConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
|
||||
mController.updateDisplay();
|
||||
assertThat(mController.getSecurityType()).isEqualTo(WifiConfiguration.KeyMgmt.NONE);
|
||||
assertThat(mPreference.getSummary()).isEqualTo("None");
|
||||
|
||||
// test WPA2 PSK tether network
|
||||
mConfig.allowedKeyManagement.clear();
|
||||
mConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA2_PSK);
|
||||
mController.updateDisplay();
|
||||
assertThat(mController.getSecurityType()).isEqualTo(WifiConfiguration.KeyMgmt.WPA2_PSK);
|
||||
assertThat(mPreference.getSummary()).isEqualTo("WPA2 PSK");
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user