Merge "Create a new method to get profile key of WifiConfig"

This commit is contained in:
Nate Jiang
2020-11-06 22:41:08 +00:00
committed by Android (Google) Code Review
4 changed files with 195 additions and 0 deletions

View File

@@ -7586,6 +7586,7 @@ package android.net.wifi {
method @Deprecated @NonNull public android.net.IpConfiguration getIpConfiguration();
method @Deprecated @NonNull public android.net.wifi.WifiConfiguration.NetworkSelectionStatus getNetworkSelectionStatus();
method @Deprecated @NonNull public String getPrintableSsid();
method @Deprecated @NonNull public String getProfileKey();
method @Deprecated public int getRecentFailureReason();
method @Deprecated public boolean hasNoInternetAccess();
method @Deprecated public boolean isEphemeral();

View File

@@ -321,6 +321,7 @@ package android.net.wifi {
method @Deprecated @NonNull public android.net.IpConfiguration getIpConfiguration();
method @Deprecated @NonNull public android.net.wifi.WifiConfiguration.NetworkSelectionStatus getNetworkSelectionStatus();
method @Deprecated @NonNull public String getPrintableSsid();
method @Deprecated @NonNull public String getProfileKey();
method @Deprecated public int getRecentFailureReason();
method @Deprecated public boolean hasNoInternetAccess();
method @Deprecated public boolean isEphemeral();

View File

@@ -3216,4 +3216,56 @@ public class WifiConfiguration implements Parcelable {
|| allowedKeyManagement.get(KeyMgmt.WAPI_PSK);
}
/**
* Get a unique key which represent this Wi-Fi configuration profile. If two profiles are for
* the same Wi-Fi network, but from different providers (apps, carriers, or data subscriptions),
* they would have different keys.
* @return a unique key which represent this profile.
* @hide
*/
@SystemApi
@NonNull public String getProfileKey() {
if (mPasspointUniqueId != null) {
return mPasspointUniqueId;
}
String key = SSID + getDefaultSecurityType();
if (!shared) {
key += "-" + UserHandle.getUserHandleForUid(creatorUid).getIdentifier();
}
if (fromWifiNetworkSuggestion) {
key += "_" + creatorName + "-" + carrierId + "-" + subscriptionId;
}
return key;
}
private String getDefaultSecurityType() {
String key;
if (allowedKeyManagement.get(KeyMgmt.WPA_PSK)) {
key = KeyMgmt.strings[KeyMgmt.WPA_PSK];
} else if (allowedKeyManagement.get(KeyMgmt.WPA_EAP)
|| allowedKeyManagement.get(KeyMgmt.IEEE8021X)) {
key = KeyMgmt.strings[KeyMgmt.WPA_EAP];
} else if (wepTxKeyIndex >= 0 && wepTxKeyIndex < wepKeys.length
&& wepKeys[wepTxKeyIndex] != null) {
key = "WEP";
} else if (allowedKeyManagement.get(KeyMgmt.OWE)) {
key = KeyMgmt.strings[KeyMgmt.OWE];
} else if (allowedKeyManagement.get(KeyMgmt.SAE)) {
key = KeyMgmt.strings[KeyMgmt.SAE];
} else if (allowedKeyManagement.get(KeyMgmt.SUITE_B_192)) {
key = KeyMgmt.strings[KeyMgmt.SUITE_B_192];
} else if (allowedKeyManagement.get(KeyMgmt.WAPI_PSK)) {
key = KeyMgmt.strings[KeyMgmt.WAPI_PSK];
} else if (allowedKeyManagement.get(KeyMgmt.WAPI_CERT)) {
key = KeyMgmt.strings[KeyMgmt.WAPI_CERT];
} else if (allowedKeyManagement.get(KeyMgmt.OSEN)) {
key = KeyMgmt.strings[KeyMgmt.OSEN];
} else {
key = KeyMgmt.strings[KeyMgmt.NONE];
}
return key;
}
}

View File

@@ -51,6 +51,9 @@ import org.junit.Test;
@SmallTest
public class WifiConfigurationTest {
private static final String TEST_PASSPOINT_UNIQUE_ID = "uniqueId";
private static final int TEST_CARRIER_ID = 1234;
private static final int TEST_SUB_ID = 3;
private static final String TEST_PACKAGE_NAME = "google.com";
@Before
public void setUp() {
@@ -720,4 +723,142 @@ public class WifiConfigurationTest {
configuration.allowedKeyManagement.set(KeyMgmt.SAE);
configuration.getAuthType();
}
/**
* Verifies that getProfileKey returns the correct String for networks of
* various different security types, the result should be stable.
*/
@Test
public void testGetProfileKeyString() {
WifiConfiguration config = new WifiConfiguration();
final String mSsid = "TestAP";
config.SSID = mSsid;
config.carrierId = TEST_CARRIER_ID;
config.subscriptionId = TEST_SUB_ID;
config.creatorName = TEST_PACKAGE_NAME;
// Test various combinations
config.allowedKeyManagement.set(KeyMgmt.WPA_PSK);
config.fromWifiNetworkSuggestion = false;
assertEquals(createProfileKey(mSsid, KeyMgmt.strings[KeyMgmt.WPA_PSK], TEST_PACKAGE_NAME,
TEST_CARRIER_ID, TEST_SUB_ID, false), config.getProfileKey());
config.fromWifiNetworkSuggestion = true;
assertEquals(createProfileKey(mSsid, KeyMgmt.strings[KeyMgmt.WPA_PSK], TEST_PACKAGE_NAME,
TEST_CARRIER_ID, TEST_SUB_ID, true), config.getProfileKey());
config.allowedKeyManagement.clear();
config.allowedKeyManagement.set(KeyMgmt.WPA_EAP);
config.fromWifiNetworkSuggestion = false;
assertEquals(createProfileKey(mSsid, KeyMgmt.strings[KeyMgmt.WPA_EAP], TEST_PACKAGE_NAME,
TEST_CARRIER_ID, TEST_SUB_ID, false), config.getProfileKey());
config.fromWifiNetworkSuggestion = true;
assertEquals(createProfileKey(mSsid, KeyMgmt.strings[KeyMgmt.WPA_EAP], TEST_PACKAGE_NAME,
TEST_CARRIER_ID, TEST_SUB_ID, true), config.getProfileKey());
config.wepKeys[0] = "TestWep";
config.allowedKeyManagement.clear();
config.fromWifiNetworkSuggestion = false;
assertEquals(createProfileKey(mSsid, "WEP", TEST_PACKAGE_NAME,
TEST_CARRIER_ID, TEST_SUB_ID, false), config.getProfileKey());
config.fromWifiNetworkSuggestion = true;
assertEquals(createProfileKey(mSsid, "WEP", TEST_PACKAGE_NAME,
TEST_CARRIER_ID, TEST_SUB_ID, true), config.getProfileKey());
// set WEP key and give a valid index.
config.wepKeys[0] = null;
config.wepKeys[2] = "TestWep";
config.wepTxKeyIndex = 2;
config.allowedKeyManagement.clear();
config.fromWifiNetworkSuggestion = false;
assertEquals(createProfileKey(mSsid, "WEP", TEST_PACKAGE_NAME,
TEST_CARRIER_ID, TEST_SUB_ID, false), config.getProfileKey());
config.fromWifiNetworkSuggestion = true;
assertEquals(createProfileKey(mSsid, "WEP", TEST_PACKAGE_NAME,
TEST_CARRIER_ID, TEST_SUB_ID, true), config.getProfileKey());
// 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);
config.fromWifiNetworkSuggestion = false;
assertEquals(createProfileKey(mSsid, KeyMgmt.strings[KeyMgmt.OWE], TEST_PACKAGE_NAME,
TEST_CARRIER_ID, TEST_SUB_ID, false), config.getProfileKey());
config.fromWifiNetworkSuggestion = true;
assertEquals(createProfileKey(mSsid, KeyMgmt.strings[KeyMgmt.OWE], TEST_PACKAGE_NAME,
TEST_CARRIER_ID, TEST_SUB_ID, true), config.getProfileKey());
config.wepKeys[0] = null;
config.wepTxKeyIndex = 0;
config.allowedKeyManagement.clear();
config.allowedKeyManagement.set(KeyMgmt.OWE);
config.fromWifiNetworkSuggestion = false;
assertEquals(createProfileKey(mSsid, KeyMgmt.strings[KeyMgmt.OWE], TEST_PACKAGE_NAME,
TEST_CARRIER_ID, TEST_SUB_ID, false), config.getProfileKey());
config.fromWifiNetworkSuggestion = true;
assertEquals(createProfileKey(mSsid, KeyMgmt.strings[KeyMgmt.OWE], TEST_PACKAGE_NAME,
TEST_CARRIER_ID, TEST_SUB_ID, true), config.getProfileKey());
config.allowedKeyManagement.clear();
config.allowedKeyManagement.set(KeyMgmt.SAE);
config.fromWifiNetworkSuggestion = false;
assertEquals(createProfileKey(mSsid, KeyMgmt.strings[KeyMgmt.SAE], TEST_PACKAGE_NAME,
TEST_CARRIER_ID, TEST_SUB_ID, false), config.getProfileKey());
config.fromWifiNetworkSuggestion = true;
assertEquals(createProfileKey(mSsid, KeyMgmt.strings[KeyMgmt.SAE], TEST_PACKAGE_NAME,
TEST_CARRIER_ID, TEST_SUB_ID, true), config.getProfileKey());
config.allowedKeyManagement.clear();
config.allowedKeyManagement.set(KeyMgmt.SUITE_B_192);
config.fromWifiNetworkSuggestion = false;
assertEquals(createProfileKey(mSsid, KeyMgmt.strings[KeyMgmt.SUITE_B_192],
TEST_PACKAGE_NAME, TEST_CARRIER_ID, TEST_SUB_ID, false), config.getProfileKey());
config.fromWifiNetworkSuggestion = true;
assertEquals(createProfileKey(mSsid, KeyMgmt.strings[KeyMgmt.SUITE_B_192],
TEST_PACKAGE_NAME, TEST_CARRIER_ID, TEST_SUB_ID, true), config.getProfileKey());
config.allowedKeyManagement.clear();
config.allowedKeyManagement.set(KeyMgmt.NONE);
config.fromWifiNetworkSuggestion = false;
assertEquals(createProfileKey(mSsid, KeyMgmt.strings[KeyMgmt.NONE], TEST_PACKAGE_NAME,
TEST_CARRIER_ID, TEST_SUB_ID, false), config.getProfileKey());
config.fromWifiNetworkSuggestion = true;
assertEquals(createProfileKey(mSsid, KeyMgmt.strings[KeyMgmt.NONE], TEST_PACKAGE_NAME,
TEST_CARRIER_ID, TEST_SUB_ID, true), config.getProfileKey());
config.allowedKeyManagement.clear();
config.allowedKeyManagement.set(KeyMgmt.WAPI_PSK);
config.fromWifiNetworkSuggestion = false;
assertEquals(createProfileKey(mSsid, KeyMgmt.strings[KeyMgmt.WAPI_PSK], TEST_PACKAGE_NAME,
TEST_CARRIER_ID, TEST_SUB_ID, false), config.getProfileKey());
config.fromWifiNetworkSuggestion = true;
assertEquals(createProfileKey(mSsid, KeyMgmt.strings[KeyMgmt.WAPI_PSK], TEST_PACKAGE_NAME,
TEST_CARRIER_ID, TEST_SUB_ID, true), config.getProfileKey());
config.allowedKeyManagement.clear();
config.allowedKeyManagement.set(KeyMgmt.WAPI_CERT);
config.fromWifiNetworkSuggestion = false;
assertEquals(createProfileKey(mSsid, KeyMgmt.strings[KeyMgmt.WAPI_CERT], TEST_PACKAGE_NAME,
TEST_CARRIER_ID, TEST_SUB_ID, false), config.getProfileKey());
config.fromWifiNetworkSuggestion = true;
assertEquals(createProfileKey(mSsid, KeyMgmt.strings[KeyMgmt.WAPI_CERT], TEST_PACKAGE_NAME,
TEST_CARRIER_ID, TEST_SUB_ID, true), config.getProfileKey());
config.allowedKeyManagement.clear();
config.setPasspointUniqueId(TEST_PASSPOINT_UNIQUE_ID);
assertEquals(TEST_PASSPOINT_UNIQUE_ID, config.getProfileKey());
}
private String createProfileKey(String ssid, String keyMgmt, String providerName,
int carrierId, int subId, boolean isFromSuggestion) {
StringBuilder sb = new StringBuilder();
sb.append(ssid).append(keyMgmt);
if (isFromSuggestion) {
sb.append("_").append(providerName).append('-')
.append(carrierId).append('-').append(subId);
}
return sb.toString();
}
}