Create a separate method to get Mac Random Key

To support multiple profile for same network, getKey() may be changed.
Create a new method for random Mac generation to keep backward
compatibility and consistent address for same profile.

Bug: 142035508
Test: atest android.net.wifi
Change-Id: I23a1bbf3a74f13252799a1b36df31f9d3d8ea359
This commit is contained in:
Nate Jiang
2020-09-25 10:23:56 -07:00
parent c25eba3756
commit c550e1f3c5
2 changed files with 93 additions and 0 deletions

View File

@@ -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.
*/

View File

@@ -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