Add Wi-Fi hotspot > Security Settings page

- Restrict low security type when 6 GHz band is selected
  - Disable "WPA2/WPA3-Personal" security type
  - Disable "WPA2-Personal" security type
  - Disable "None" security type

- Automatically updated security type to WPA3 when 6 GHz band is selected
  - Regenerate password when security type is changed from None

Bug: 245258763
Test: manual test
atest -c WifiTetherSettingsTest
atest -c WifiTetherViewModelTest \
         WifiHotspotSecuritySettingsTest \
         WifiHotspotSecurityViewModelTest \
         WifiHotspotRepositoryTest

Change-Id: I31b08795419baed10dc40b876aeec175f6f41e69
This commit is contained in:
Weng Su
2023-04-10 23:31:50 +08:00
parent 211b544ea8
commit 9f80cd2f77
14 changed files with 1019 additions and 28 deletions

View File

@@ -0,0 +1,173 @@
/*
* Copyright (C) 2023 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.settings.wifi.tether;
import static android.net.wifi.SoftApConfiguration.SECURITY_TYPE_OPEN;
import static android.net.wifi.SoftApConfiguration.SECURITY_TYPE_WPA2_PSK;
import static android.net.wifi.SoftApConfiguration.SECURITY_TYPE_WPA3_SAE;
import static android.net.wifi.SoftApConfiguration.SECURITY_TYPE_WPA3_SAE_TRANSITION;
import static com.android.settings.wifi.tether.WifiHotspotSecurityViewModel.KEY_SECURITY_NONE;
import static com.android.settings.wifi.tether.WifiHotspotSecurityViewModel.KEY_SECURITY_WPA2;
import static com.android.settings.wifi.tether.WifiHotspotSecurityViewModel.KEY_SECURITY_WPA2_WPA3;
import static com.android.settings.wifi.tether.WifiHotspotSecurityViewModel.KEY_SECURITY_WPA3;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import androidx.test.platform.app.InstrumentationRegistry;
import com.android.settingslib.widget.SelectorWithWidgetPreference;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import java.util.HashMap;
import java.util.Map;
public class WifiHotspotSecuritySettingsTest {
@Rule
public final MockitoRule mMockitoRule = MockitoJUnit.rule();
@Mock
WifiHotspotSecurityViewModel mViewModel;
@Mock
SelectorWithWidgetPreference mRadioButtonWpa3;
@Mock
SelectorWithWidgetPreference mRadioButtonWpa2Wpa3;
@Mock
SelectorWithWidgetPreference mRadioButtonWpa2;
@Mock
SelectorWithWidgetPreference mRadioButtonNone;
Map<Integer, WifiHotspotSecurityViewModel.ViewItem> mViewItemMap = new HashMap<>();
WifiHotspotSecurityViewModel.ViewItem mViewItemWpa3 =
new WifiHotspotSecurityViewModel.ViewItem(KEY_SECURITY_WPA3);
WifiHotspotSecurityViewModel.ViewItem mViewItemWpa2Wpa3 =
new WifiHotspotSecurityViewModel.ViewItem(KEY_SECURITY_WPA2_WPA3);
WifiHotspotSecurityViewModel.ViewItem mViewItemWpa2 =
new WifiHotspotSecurityViewModel.ViewItem(KEY_SECURITY_WPA2);
WifiHotspotSecurityViewModel.ViewItem mViewItemNone =
new WifiHotspotSecurityViewModel.ViewItem(KEY_SECURITY_NONE);
WifiHotspotSecuritySettings mSettings;
@Before
public void setUp() {
mViewItemMap.put(SECURITY_TYPE_WPA3_SAE, mViewItemWpa3);
mViewItemMap.put(SECURITY_TYPE_WPA3_SAE_TRANSITION, mViewItemWpa2Wpa3);
mViewItemMap.put(SECURITY_TYPE_WPA2_PSK, mViewItemWpa2);
mViewItemMap.put(SECURITY_TYPE_OPEN, mViewItemNone);
when(mRadioButtonWpa3.getKey()).thenReturn(KEY_SECURITY_WPA3);
when(mRadioButtonWpa2Wpa3.getKey()).thenReturn(KEY_SECURITY_WPA2_WPA3);
when(mRadioButtonWpa2.getKey()).thenReturn(KEY_SECURITY_WPA2);
when(mRadioButtonNone.getKey()).thenReturn(KEY_SECURITY_NONE);
InstrumentationRegistry.getInstrumentation().runOnMainSync(
() -> mSettings = spy(new WifiHotspotSecuritySettings()));
mSettings.mWifiHotspotSecurityViewModel = mViewModel;
when(mSettings.findPreference(KEY_SECURITY_WPA3)).thenReturn(mRadioButtonWpa3);
when(mSettings.findPreference(KEY_SECURITY_WPA2_WPA3)).thenReturn(mRadioButtonWpa2Wpa3);
when(mSettings.findPreference(KEY_SECURITY_WPA2)).thenReturn(mRadioButtonWpa2);
when(mSettings.findPreference(KEY_SECURITY_NONE)).thenReturn(mRadioButtonNone);
}
@Test
public void onViewItemListDataChanged_checkedWpa3_setViewItemCorrectly() {
mViewItemWpa3.mIsChecked = true;
mSettings.onViewItemListDataChanged(mViewItemMap.values().stream().toList());
verify(mRadioButtonWpa3).setChecked(true);
}
@Test
public void onViewItemListDataChanged_checkedWpa2Wpa3_setViewItemCorrectly() {
mViewItemWpa2Wpa3.mIsChecked = true;
mSettings.onViewItemListDataChanged(mViewItemMap.values().stream().toList());
verify(mRadioButtonWpa2Wpa3).setChecked(true);
}
@Test
public void onViewItemListDataChanged_checkedWpa2_setViewItemCorrectly() {
mViewItemWpa2.mIsChecked = true;
mSettings.onViewItemListDataChanged(mViewItemMap.values().stream().toList());
verify(mRadioButtonWpa2).setChecked(true);
}
@Test
public void onViewItemListDataChanged_checkedNone_setViewItemCorrectly() {
mViewItemNone.mIsChecked = true;
mSettings.onViewItemListDataChanged(mViewItemMap.values().stream().toList());
verify(mRadioButtonNone).setChecked(true);
}
@Test
public void onViewItemListDataChanged_enabledWpa3Only_setViewItemCorrectly() {
when(mRadioButtonWpa2Wpa3.isEnabled()).thenReturn(true);
when(mRadioButtonWpa2.isEnabled()).thenReturn(true);
when(mRadioButtonNone.isEnabled()).thenReturn(true);
mViewItemWpa2Wpa3.mIsEnabled = false;
mViewItemWpa2.mIsEnabled = false;
mViewItemNone.mIsEnabled = false;
mSettings.onViewItemListDataChanged(mViewItemMap.values().stream().toList());
verify(mRadioButtonWpa2Wpa3).setEnabled(false);
verify(mRadioButtonWpa2).setEnabled(false);
verify(mRadioButtonNone).setEnabled(false);
}
@Test
public void onRadioButtonClicked_clickedWpa3_setSecurityTypeCorrectly() {
mSettings.onRadioButtonClicked(mRadioButtonWpa3);
verify(mViewModel).handleRadioButtonClicked(KEY_SECURITY_WPA3);
}
@Test
public void onRadioButtonClicked_clickedWpa2Wpa3_setSecurityTypeCorrectly() {
mSettings.onRadioButtonClicked(mRadioButtonWpa2Wpa3);
verify(mViewModel).handleRadioButtonClicked(KEY_SECURITY_WPA2_WPA3);
}
@Test
public void onRadioButtonClicked_clickedWpa2_setSecurityTypeCorrectly() {
mSettings.onRadioButtonClicked(mRadioButtonWpa2);
verify(mViewModel).handleRadioButtonClicked(KEY_SECURITY_WPA2);
}
@Test
public void onRadioButtonClicked_clickedNone_setSecurityTypeCorrectly() {
mSettings.onRadioButtonClicked(mRadioButtonNone);
verify(mViewModel).handleRadioButtonClicked(KEY_SECURITY_NONE);
}
}

View File

@@ -0,0 +1,211 @@
/*
* Copyright (C) 2023 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.settings.wifi.tether;
import static android.net.wifi.SoftApConfiguration.SECURITY_TYPE_OPEN;
import static android.net.wifi.SoftApConfiguration.SECURITY_TYPE_WPA2_PSK;
import static android.net.wifi.SoftApConfiguration.SECURITY_TYPE_WPA3_SAE;
import static android.net.wifi.SoftApConfiguration.SECURITY_TYPE_WPA3_SAE_TRANSITION;
import static com.android.settings.wifi.repository.WifiHotspotRepository.SPEED_2GHZ;
import static com.android.settings.wifi.repository.WifiHotspotRepository.SPEED_2GHZ_5GHZ;
import static com.android.settings.wifi.repository.WifiHotspotRepository.SPEED_5GHZ;
import static com.android.settings.wifi.repository.WifiHotspotRepository.SPEED_6GHZ;
import static com.android.settings.wifi.tether.WifiHotspotSecurityViewModel.KEY_SECURITY_NONE;
import static com.android.settings.wifi.tether.WifiHotspotSecurityViewModel.KEY_SECURITY_WPA2;
import static com.android.settings.wifi.tether.WifiHotspotSecurityViewModel.KEY_SECURITY_WPA2_WPA3;
import static com.android.settings.wifi.tether.WifiHotspotSecurityViewModel.KEY_SECURITY_WPA3;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.Application;
import android.content.Context;
import androidx.lifecycle.MutableLiveData;
import androidx.test.annotation.UiThreadTest;
import androidx.test.core.app.ApplicationProvider;
import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settings.wifi.repository.WifiHotspotRepository;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
@UiThreadTest
public class WifiHotspotSecurityViewModelTest {
@Rule
public final MockitoRule mMockitoRule = MockitoJUnit.rule();
@Spy
Context mContext = ApplicationProvider.getApplicationContext();
@Mock
WifiHotspotRepository mWifiHotspotRepository;
@Mock
MutableLiveData<Integer> mSecurityType;
@Mock
MutableLiveData<Integer> mSpeedType;
WifiHotspotSecurityViewModel mViewModel;
@Before
public void setUp() {
FakeFeatureFactory featureFactory = FakeFeatureFactory.setupForTest();
when(featureFactory.getWifiFeatureProvider().getWifiHotspotRepository())
.thenReturn(mWifiHotspotRepository);
when(mWifiHotspotRepository.getSecurityType()).thenReturn(mSecurityType);
when(mWifiHotspotRepository.getSpeedType()).thenReturn(mSpeedType);
mViewModel = new WifiHotspotSecurityViewModel((Application) mContext);
}
@Test
public void constructor_observeDataAndSetAutoRefresh() {
verify(mSecurityType).observeForever(mViewModel.mSecurityTypeObserver);
verify(mSpeedType).observeForever(mViewModel.mSpeedTypeObserver);
}
@Test
public void onCleared_removeObserverData() {
mViewModel.onCleared();
verify(mSecurityType).removeObserver(mViewModel.mSecurityTypeObserver);
verify(mSpeedType).removeObserver(mViewModel.mSpeedTypeObserver);
}
@Test
public void onSecurityTypeChanged_securityTypeWpa3_setCheckedCorrectly() {
mViewModel.onSecurityTypeChanged(SECURITY_TYPE_WPA3_SAE);
assertItemChecked(true, false, false, false);
}
@Test
public void onSecurityTypeChanged_securityTypeWpa2Wpa3_setCheckedCorrectly() {
mViewModel.onSecurityTypeChanged(SECURITY_TYPE_WPA3_SAE_TRANSITION);
assertItemChecked(false, true, false, false);
}
@Test
public void onSecurityTypeChanged_securityTypeWpa2_setCheckedCorrectly() {
mViewModel.onSecurityTypeChanged(SECURITY_TYPE_WPA2_PSK);
assertItemChecked(false, false, true, false);
}
@Test
public void onSecurityTypeChanged_securityTypeNone_setCheckedCorrectly() {
mViewModel.onSecurityTypeChanged(SECURITY_TYPE_OPEN);
assertItemChecked(false, false, false, true);
}
@Test
public void onSpeedTypeChanged_speed6g_setEnabledCorrectly() {
mViewModel.onSpeedTypeChanged(SPEED_6GHZ);
assertItemEnabled(true, false, false, false);
}
@Test
public void onSpeedTypeChanged_speed2g5g_setEnabledCorrectly() {
mViewModel.onSpeedTypeChanged(SPEED_2GHZ_5GHZ);
assertItemEnabled(true, true, true, true);
}
@Test
public void onSpeedTypeChanged_speed5g_setEnabledCorrectly() {
mViewModel.onSpeedTypeChanged(SPEED_5GHZ);
assertItemEnabled(true, true, true, true);
}
@Test
public void onSpeedTypeChanged_speed2g_setEnabledCorrectly() {
mViewModel.onSpeedTypeChanged(SPEED_2GHZ);
assertItemEnabled(true, true, true, true);
}
@Test
public void handleRadioButtonClicked_keyWpa3_setSecurityTypeCorrectly() {
mViewModel.handleRadioButtonClicked(KEY_SECURITY_WPA3);
verify(mWifiHotspotRepository).setSecurityType(SECURITY_TYPE_WPA3_SAE);
}
@Test
public void handleRadioButtonClicked_keyWpa2Wpa3_setSecurityTypeCorrectly() {
mViewModel.handleRadioButtonClicked(KEY_SECURITY_WPA2_WPA3);
verify(mWifiHotspotRepository).setSecurityType(SECURITY_TYPE_WPA3_SAE_TRANSITION);
}
@Test
public void handleRadioButtonClicked_keyWpa2_setSecurityTypeCorrectly() {
mViewModel.handleRadioButtonClicked(KEY_SECURITY_WPA2);
verify(mWifiHotspotRepository).setSecurityType(SECURITY_TYPE_WPA2_PSK);
}
@Test
public void handleRadioButtonClicked_keyNone_setSecurityTypeCorrectly() {
mViewModel.handleRadioButtonClicked(KEY_SECURITY_NONE);
verify(mWifiHotspotRepository).setSecurityType(SECURITY_TYPE_OPEN);
}
@Test
public void getViewItemListData_shouldNotReturnNull() {
// Reset mViewInfoListData to trigger an update
mViewModel.mViewInfoListData = null;
assertThat(mViewModel.getViewItemListData()).isNotNull();
}
private void assertItemChecked(boolean checkedWpa3, boolean checkedWpa2Wpa3,
boolean checkedWpa2, boolean checkedNone) {
assertThat(mViewModel.mViewItemMap.get(SECURITY_TYPE_WPA3_SAE).mIsChecked)
.isEqualTo(checkedWpa3);
assertThat(mViewModel.mViewItemMap.get(SECURITY_TYPE_WPA3_SAE_TRANSITION).mIsChecked)
.isEqualTo(checkedWpa2Wpa3);
assertThat(mViewModel.mViewItemMap.get(SECURITY_TYPE_WPA2_PSK).mIsChecked)
.isEqualTo(checkedWpa2);
assertThat(mViewModel.mViewItemMap.get(SECURITY_TYPE_OPEN).mIsChecked)
.isEqualTo(checkedNone);
}
private void assertItemEnabled(boolean enabledWpa3, boolean enabledWpa2Wpa3,
boolean enabledWpa2, boolean enabledNone) {
assertThat(mViewModel.mViewItemMap.get(SECURITY_TYPE_WPA3_SAE).mIsEnabled)
.isEqualTo(enabledWpa3);
assertThat(mViewModel.mViewItemMap.get(SECURITY_TYPE_WPA3_SAE_TRANSITION).mIsEnabled)
.isEqualTo(enabledWpa2Wpa3);
assertThat(mViewModel.mViewItemMap.get(SECURITY_TYPE_WPA2_PSK).mIsEnabled)
.isEqualTo(enabledWpa2);
assertThat(mViewModel.mViewItemMap.get(SECURITY_TYPE_OPEN).mIsEnabled)
.isEqualTo(enabledNone);
}
}

View File

@@ -52,6 +52,8 @@ public class WifiTetherViewModelTest {
@Mock
WifiHotspotRepository mWifiHotspotRepository;
@Mock
MutableLiveData<Integer> mSecurityType;
@Mock
MutableLiveData<Integer> mSpeedType;
WifiTetherViewModel mViewModel;
@@ -63,21 +65,18 @@ public class WifiTetherViewModelTest {
FakeFeatureFactory featureFactory = FakeFeatureFactory.setupForTest();
when(featureFactory.getWifiFeatureProvider().getWifiHotspotRepository())
.thenReturn(mWifiHotspotRepository);
when(mWifiHotspotRepository.getSecurityType()).thenReturn(mSecurityType);
when(mWifiHotspotRepository.getSpeedType()).thenReturn(mSpeedType);
mViewModel = new WifiTetherViewModel(mApplication);
}
@Test
public void constructor_setAutoRefreshTrue() {
verify(mWifiHotspotRepository).setAutoRefresh(true);
}
@Test
public void onCleared_setAutoRefreshFalse() {
mViewModel.onCleared();
verify(mWifiHotspotRepository).setAutoRefresh(false);
verify(mSecurityType).removeObserver(mViewModel.mSecurityTypeObserver);
verify(mSpeedType).removeObserver(mViewModel.mSpeedTypeObserver);
}
@Test
@@ -96,6 +95,17 @@ public class WifiTetherViewModelTest {
verify(mWifiHotspotRepository).refresh();
}
@Test
@UiThreadTest
public void getSecuritySummary_returnNotNull() {
mViewModel.mSecuritySummary = null;
mViewModel.getSecuritySummary();
assertThat(mViewModel.mSecuritySummary).isNotNull();
verify(mSecurityType).observeForever(mViewModel.mSecurityTypeObserver);
}
@Test
@UiThreadTest
public void getSpeedSummary_returnNotNull() {
@@ -104,5 +114,6 @@ public class WifiTetherViewModelTest {
mViewModel.getSpeedSummary();
assertThat(mViewModel.mSpeedSummary).isNotNull();
verify(mSpeedType).observeForever(mViewModel.mSpeedTypeObserver);
}
}