Merge "MAC Randomization: Store Randomized MAC Address in WifiConfiguration"
This commit is contained in:
committed by
Android (Google) Code Review
commit
1266a4f5d5
@@ -20,6 +20,7 @@ import android.annotation.SystemApi;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.net.IpConfiguration;
|
||||
import android.net.IpConfiguration.ProxySettings;
|
||||
import android.net.MacAddress;
|
||||
import android.net.ProxyInfo;
|
||||
import android.net.StaticIpConfiguration;
|
||||
import android.net.Uri;
|
||||
@@ -852,6 +853,52 @@ public class WifiConfiguration implements Parcelable {
|
||||
@SystemApi
|
||||
public int numAssociation;
|
||||
|
||||
/**
|
||||
* @hide
|
||||
* Randomized MAC address to use with this particular network
|
||||
*/
|
||||
private MacAddress mRandomizedMacAddress;
|
||||
|
||||
/**
|
||||
* @hide
|
||||
* Checks if the given MAC address can be used for Connected Mac Randomization
|
||||
* by verifying that it is non-null, unicast, and locally assigned.
|
||||
* @param mac MacAddress to check
|
||||
* @return true if mac is good to use
|
||||
*/
|
||||
private boolean isValidMacAddressForRandomization(MacAddress mac) {
|
||||
return mac != null && !mac.isMulticastAddress() && mac.isLocallyAssigned();
|
||||
}
|
||||
|
||||
/**
|
||||
* @hide
|
||||
* Returns Randomized MAC address to use with the network.
|
||||
* If it is not set/valid, create a new randomized address.
|
||||
*/
|
||||
public MacAddress getOrCreateRandomizedMacAddress() {
|
||||
if (!isValidMacAddressForRandomization(mRandomizedMacAddress)) {
|
||||
mRandomizedMacAddress = MacAddress.createRandomUnicastAddress();
|
||||
}
|
||||
return mRandomizedMacAddress;
|
||||
}
|
||||
|
||||
/**
|
||||
* @hide
|
||||
* Returns MAC address set to be the local randomized MAC address.
|
||||
* Does not guarantee that the returned address is valid for use.
|
||||
*/
|
||||
public MacAddress getRandomizedMacAddress() {
|
||||
return mRandomizedMacAddress;
|
||||
}
|
||||
|
||||
/**
|
||||
* @hide
|
||||
* @param mac MacAddress to change into
|
||||
*/
|
||||
public void setRandomizedMacAddress(MacAddress mac) {
|
||||
mRandomizedMacAddress = mac;
|
||||
}
|
||||
|
||||
/** @hide
|
||||
* Boost given to RSSI on a home network for the purpose of calculating the score
|
||||
* This adds stickiness to home networks, as defined by:
|
||||
@@ -2124,6 +2171,7 @@ public class WifiConfiguration implements Parcelable {
|
||||
updateTime = source.updateTime;
|
||||
shared = source.shared;
|
||||
recentFailure.setAssociationStatus(source.recentFailure.getAssociationStatus());
|
||||
mRandomizedMacAddress = source.mRandomizedMacAddress;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2191,6 +2239,7 @@ public class WifiConfiguration implements Parcelable {
|
||||
dest.writeInt(shared ? 1 : 0);
|
||||
dest.writeString(mPasspointManagementObjectTree);
|
||||
dest.writeInt(recentFailure.getAssociationStatus());
|
||||
dest.writeParcelable(mRandomizedMacAddress, flags);
|
||||
}
|
||||
|
||||
/** Implement the Parcelable interface {@hide} */
|
||||
@@ -2259,6 +2308,7 @@ public class WifiConfiguration implements Parcelable {
|
||||
config.shared = in.readInt() != 0;
|
||||
config.mPasspointManagementObjectTree = in.readString();
|
||||
config.recentFailure.setAssociationStatus(in.readInt());
|
||||
config.mRandomizedMacAddress = in.readParcelable(null);
|
||||
return config;
|
||||
}
|
||||
|
||||
|
||||
@@ -22,6 +22,7 @@ import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
|
||||
import android.os.Parcel;
|
||||
import android.net.MacAddress;
|
||||
import android.net.wifi.WifiConfiguration.NetworkSelectionStatus;
|
||||
|
||||
import org.junit.Before;
|
||||
@@ -49,6 +50,7 @@ public class WifiConfigurationTest {
|
||||
String cookie = "C O.o |<IE";
|
||||
WifiConfiguration config = new WifiConfiguration();
|
||||
config.setPasspointManagementObjectTree(cookie);
|
||||
MacAddress macBeforeParcel = config.getOrCreateRandomizedMacAddress();
|
||||
Parcel parcelW = Parcel.obtain();
|
||||
config.writeToParcel(parcelW, 0);
|
||||
byte[] bytes = parcelW.marshall();
|
||||
@@ -59,8 +61,9 @@ public class WifiConfigurationTest {
|
||||
parcelR.setDataPosition(0);
|
||||
WifiConfiguration reconfig = WifiConfiguration.CREATOR.createFromParcel(parcelR);
|
||||
|
||||
// lacking a useful config.equals, check one field near the end.
|
||||
// lacking a useful config.equals, check two fields near the end.
|
||||
assertEquals(cookie, reconfig.getMoTree());
|
||||
assertEquals(macBeforeParcel, reconfig.getOrCreateRandomizedMacAddress());
|
||||
|
||||
Parcel parcelWW = Parcel.obtain();
|
||||
reconfig.writeToParcel(parcelWW, 0);
|
||||
@@ -169,4 +172,48 @@ public class WifiConfigurationTest {
|
||||
|
||||
assertFalse(config.isOpenNetwork());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetOrCreateRandomizedMacAddress_SavesAndReturnsSameAddress() {
|
||||
WifiConfiguration config = new WifiConfiguration();
|
||||
MacAddress firstMacAddress = config.getOrCreateRandomizedMacAddress();
|
||||
MacAddress secondMacAddress = config.getOrCreateRandomizedMacAddress();
|
||||
|
||||
assertEquals(firstMacAddress, secondMacAddress);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetRandomizedMacAddress_ChangesSavedAddress() {
|
||||
WifiConfiguration config = new WifiConfiguration();
|
||||
MacAddress macToChangeInto = MacAddress.createRandomUnicastAddress();
|
||||
config.setRandomizedMacAddress(macToChangeInto);
|
||||
MacAddress macAfterChange = config.getOrCreateRandomizedMacAddress();
|
||||
|
||||
assertEquals(macToChangeInto, macAfterChange);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetOrCreateRandomizedMacAddress_ReRandomizesInvalidAddress() {
|
||||
WifiConfiguration config = new WifiConfiguration();
|
||||
|
||||
MacAddress macAddressZeroes = MacAddress.ALL_ZEROS_ADDRESS;
|
||||
MacAddress macAddressMulticast = MacAddress.fromString("03:ff:ff:ff:ff:ff");
|
||||
MacAddress macAddressGlobal = MacAddress.fromString("fc:ff:ff:ff:ff:ff");
|
||||
|
||||
config.setRandomizedMacAddress(null);
|
||||
MacAddress macAfterChange = config.getOrCreateRandomizedMacAddress();
|
||||
assertFalse(macAfterChange.equals(null));
|
||||
|
||||
config.setRandomizedMacAddress(macAddressZeroes);
|
||||
macAfterChange = config.getOrCreateRandomizedMacAddress();
|
||||
assertFalse(macAfterChange.equals(macAddressZeroes));
|
||||
|
||||
config.setRandomizedMacAddress(macAddressMulticast);
|
||||
macAfterChange = config.getOrCreateRandomizedMacAddress();
|
||||
assertFalse(macAfterChange.equals(macAddressMulticast));
|
||||
|
||||
config.setRandomizedMacAddress(macAddressGlobal);
|
||||
macAfterChange = config.getOrCreateRandomizedMacAddress();
|
||||
assertFalse(macAfterChange.equals(macAddressGlobal));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user