From 5aa95553cde83b06890f31d6d0358bd60224c298 Mon Sep 17 00:00:00 2001 From: Quang Anh Luong Date: Thu, 26 Sep 2024 11:52:58 +0900 Subject: [PATCH] Do not auto-downgrade WPA3->Transition mode if password too short Hotspot security is auto-downgraded from WPA3->Transition mode if the band is changed from 6Ghz -> 2.4/5GHz. However, Transition mode requires a password of 8 chars or more, but WPA3 SAE does not. Avoid changing the security type if the current password is less than 8 chars. Flag: EXEMPT minor bugfix Bug: 366452667 Test: atest WifiHotspotRepositoryTest Change-Id: I1abadd59966e170b51899782746b9f14e33e6186 --- .../repository/WifiHotspotRepository.java | 9 ++--- .../repository/WifiHotspotRepositoryTest.java | 33 ++++++++++++++++--- 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/src/com/android/settings/wifi/repository/WifiHotspotRepository.java b/src/com/android/settings/wifi/repository/WifiHotspotRepository.java index 4dc2e9e52f3..b601eb33238 100644 --- a/src/com/android/settings/wifi/repository/WifiHotspotRepository.java +++ b/src/com/android/settings/wifi/repository/WifiHotspotRepository.java @@ -356,10 +356,11 @@ public class WifiHotspotRepository { log("setSpeedType(), setBand(BAND_2GHZ)"); configBuilder.setBand(BAND_2GHZ); } - // Set the security type back to WPA2/WPA3 if we're moving from 6GHz to something else. - if ((config.getBand() & BAND_6GHZ) != 0) { - configBuilder.setPassphrase( - generatePassword(config), SECURITY_TYPE_WPA3_SAE_TRANSITION); + // Set the security type back to WPA2/WPA3 if the password is at least 8 characters and + // we're moving from 6GHz to something else. + String passphrase = generatePassword(config); + if ((passphrase.length() >= 8) && (config.getBand() & BAND_6GHZ) != 0) { + configBuilder.setPassphrase(passphrase, SECURITY_TYPE_WPA3_SAE_TRANSITION); } } setSoftApConfiguration(configBuilder.build()); diff --git a/tests/unit/src/com/android/settings/wifi/repository/WifiHotspotRepositoryTest.java b/tests/unit/src/com/android/settings/wifi/repository/WifiHotspotRepositoryTest.java index 4765d180119..510ce39350d 100644 --- a/tests/unit/src/com/android/settings/wifi/repository/WifiHotspotRepositoryTest.java +++ b/tests/unit/src/com/android/settings/wifi/repository/WifiHotspotRepositoryTest.java @@ -78,6 +78,7 @@ import java.util.Arrays; public class WifiHotspotRepositoryTest { static final String WIFI_SSID = "wifi_ssid"; static final String WIFI_PASSWORD = "wifi_password"; + static final String WIFI_PASSWORD_SHORT = "wifi"; static final int WIFI_5GHZ_BAND_PREFERRED = BAND_2GHZ_5GHZ; static final int WIFI_6GHZ_BAND_PREFERRED = BAND_2GHZ_5GHZ_6GHZ; @@ -477,7 +478,7 @@ public class WifiHotspotRepositoryTest { @Test public void setSpeedType_2g5ghzTo6ghz_setConfigSecurityToWpa3() { - mockConfig(SPEED_2GHZ_5GHZ, SECURITY_TYPE_WPA3_SAE_TRANSITION); + mockConfig(SPEED_2GHZ_5GHZ, SECURITY_TYPE_WPA3_SAE_TRANSITION, WIFI_PASSWORD); mRepository.setSpeedType(SPEED_6GHZ); @@ -497,10 +498,32 @@ public class WifiHotspotRepositoryTest { SparseIntArray channels = mSoftApConfigCaptor.getValue().getChannels(); assertThat(channels.get(BAND_2GHZ, CHANNEL_NOT_FOUND)).isNotEqualTo(CHANNEL_NOT_FOUND); assertThat(channels.get(BAND_2GHZ_5GHZ, CHANNEL_NOT_FOUND)).isNotEqualTo(CHANNEL_NOT_FOUND); + } + + @Test + public void setSpeedType_6ghzTo2g5ghzWith8CharPassphrase_changesSecurityToWpa3Transition() { + mockConfigSpeedType(SPEED_6GHZ); + mRepository.mIsDualBand = true; + + mRepository.setSpeedType(SPEED_2GHZ_5GHZ); + + verify(mWifiManager).setSoftApConfiguration(mSoftApConfigCaptor.capture()); assertThat(mSoftApConfigCaptor.getValue().getSecurityType()) .isEqualTo(SECURITY_TYPE_WPA3_SAE_TRANSITION); } + @Test + public void setSpeedType_6ghzTo2g5ghzWithLessThan8CharPassphrase_doesNotChangeSecurity() { + mockConfig(SECURITY_TYPE_WPA3_SAE, SPEED_6GHZ, WIFI_PASSWORD_SHORT); + mRepository.mIsDualBand = true; + + mRepository.setSpeedType(SPEED_2GHZ_5GHZ); + + verify(mWifiManager).setSoftApConfiguration(mSoftApConfigCaptor.capture()); + assertThat(mSoftApConfigCaptor.getValue().getSecurityType()) + .isEqualTo(SECURITY_TYPE_WPA3_SAE); + } + @Test public void setSpeedType_2ghzTo5ghz_setConfigBandTo5ghzPreferred() { mockConfigSpeedType(SPEED_2GHZ); @@ -784,18 +807,18 @@ public class WifiHotspotRepositoryTest { } private void mockConfigSecurityType(int securityType) { - mockConfig(securityType, SPEED_2GHZ); + mockConfig(securityType, SPEED_2GHZ, + (securityType == SECURITY_TYPE_OPEN) ? null : WIFI_PASSWORD); } private void mockConfigSpeedType(int speedType) { - mockConfig(SECURITY_TYPE_WPA3_SAE, speedType); + mockConfig(SECURITY_TYPE_WPA3_SAE, speedType, WIFI_PASSWORD); } - private void mockConfig(int securityType, int speedType) { + private void mockConfig(int securityType, int speedType, String passphrase) { SoftApConfiguration.Builder configBuilder = new SoftApConfiguration.Builder(); // Security Type doReturn(securityType).when(mSecurityType).getValue(); - String passphrase = (securityType == SECURITY_TYPE_OPEN) ? null : WIFI_PASSWORD; configBuilder.setPassphrase(passphrase, securityType).build(); // Speed Type