diff --git a/wifi/java/android/net/wifi/WifiConfiguration.java b/wifi/java/android/net/wifi/WifiConfiguration.java index 5208bcf84c9d8..7c2556d8cffd4 100644 --- a/wifi/java/android/net/wifi/WifiConfiguration.java +++ b/wifi/java/android/net/wifi/WifiConfiguration.java @@ -2627,6 +2627,25 @@ public class WifiConfiguration implements Parcelable { return key; } + /** + * Get a key for this WifiConfig to generate Persist random Mac Address. + * @hide + */ + public String getMacRandomKey() { + // Passpoint ephemeral networks have their unique identifier set. Return it as is to be + // able to match internally. + if (mPasspointUniqueId != null) { + return mPasspointUniqueId; + } + + String key = getSsidAndSecurityTypeString(); + if (!shared) { + key += "-" + UserHandle.getUserHandleForUid(creatorUid).getIdentifier(); + } + + return key; + } + /** @hide * return the SSID + security type in String format. */ diff --git a/wifi/tests/src/android/net/wifi/WifiConfigurationTest.java b/wifi/tests/src/android/net/wifi/WifiConfigurationTest.java index 54f0c0f53aba8..b82c67b658aa8 100644 --- a/wifi/tests/src/android/net/wifi/WifiConfigurationTest.java +++ b/wifi/tests/src/android/net/wifi/WifiConfigurationTest.java @@ -48,6 +48,7 @@ import org.junit.Test; */ @SmallTest public class WifiConfigurationTest { + private static final String TEST_PASSPOINT_UNIQUE_ID = "uniqueId"; @Before public void setUp() { @@ -390,6 +391,79 @@ public class WifiConfigurationTest { config.getSsidAndSecurityTypeString()); } + /** + * Verifies that getMacRandomKey returns the correct String for networks of + * various different security types, the result should be stable. + */ + @Test + public void testGetMacRandomKeyString() { + WifiConfiguration config = new WifiConfiguration(); + final String mSsid = "TestAP"; + config.SSID = mSsid; + + // Test various combinations + config.allowedKeyManagement.set(KeyMgmt.WPA_PSK); + assertEquals(mSsid + KeyMgmt.strings[KeyMgmt.WPA_PSK], + config.getMacRandomKey()); + + config.allowedKeyManagement.clear(); + config.allowedKeyManagement.set(KeyMgmt.WPA_EAP); + assertEquals(mSsid + KeyMgmt.strings[KeyMgmt.WPA_EAP], + config.getMacRandomKey()); + + config.wepKeys[0] = "TestWep"; + config.allowedKeyManagement.clear(); + assertEquals(mSsid + "WEP", config.getMacRandomKey()); + + // set WEP key and give a valid index. + config.wepKeys[0] = null; + config.wepKeys[2] = "TestWep"; + config.wepTxKeyIndex = 2; + config.allowedKeyManagement.clear(); + assertEquals(mSsid + "WEP", config.getMacRandomKey()); + + // set WEP key but does not give a valid index. + config.wepKeys[0] = null; + config.wepKeys[2] = "TestWep"; + config.wepTxKeyIndex = 0; + config.allowedKeyManagement.clear(); + config.allowedKeyManagement.set(KeyMgmt.OWE); + assertEquals(mSsid + KeyMgmt.strings[KeyMgmt.OWE], config.getMacRandomKey()); + + config.wepKeys[0] = null; + config.wepTxKeyIndex = 0; + config.allowedKeyManagement.clear(); + config.allowedKeyManagement.set(KeyMgmt.OWE); + assertEquals(mSsid + KeyMgmt.strings[KeyMgmt.OWE], config.getMacRandomKey()); + + config.allowedKeyManagement.clear(); + config.allowedKeyManagement.set(KeyMgmt.SAE); + assertEquals(mSsid + KeyMgmt.strings[KeyMgmt.SAE], config.getMacRandomKey()); + + config.allowedKeyManagement.clear(); + config.allowedKeyManagement.set(KeyMgmt.SUITE_B_192); + assertEquals(mSsid + KeyMgmt.strings[KeyMgmt.SUITE_B_192], + config.getMacRandomKey()); + + config.allowedKeyManagement.clear(); + config.allowedKeyManagement.set(KeyMgmt.NONE); + assertEquals(mSsid + KeyMgmt.strings[KeyMgmt.NONE], config.getMacRandomKey()); + + config.allowedKeyManagement.clear(); + config.allowedKeyManagement.set(KeyMgmt.WAPI_PSK); + assertEquals(mSsid + KeyMgmt.strings[KeyMgmt.WAPI_PSK], + config.getMacRandomKey()); + + config.allowedKeyManagement.clear(); + config.allowedKeyManagement.set(KeyMgmt.WAPI_CERT); + assertEquals(mSsid + KeyMgmt.strings[KeyMgmt.WAPI_CERT], + config.getMacRandomKey()); + + config.allowedKeyManagement.clear(); + config.setPasspointUniqueId(TEST_PASSPOINT_UNIQUE_ID); + assertEquals(TEST_PASSPOINT_UNIQUE_ID, config.getMacRandomKey()); + } + /** * Ensure that the {@link NetworkSelectionStatus.DisableReasonInfo}s are populated in * {@link NetworkSelectionStatus#DISABLE_REASON_INFOS} for reason codes from 0 to