Merge ""02:00:00:00:00:00" invalid for MAC randomization" into pi-dev

This commit is contained in:
Mukesh Agrawal
2018-03-29 22:25:13 +00:00
committed by Android (Google) Code Review
2 changed files with 52 additions and 19 deletions

View File

@@ -25,11 +25,13 @@ import android.net.MacAddress;
import android.net.ProxyInfo;
import android.net.StaticIpConfiguration;
import android.net.Uri;
import android.net.wifi.WifiInfo;
import android.os.Parcel;
import android.os.Parcelable;
import android.os.UserHandle;
import android.text.TextUtils;
import android.util.BackupUtils;
import android.util.Log;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
@@ -76,6 +78,8 @@ public class WifiConfiguration implements Parcelable {
/** {@hide} */
private String mPasspointManagementObjectTree;
/** {@hide} */
private static final int MAXIMUM_RANDOM_MAC_GENERATION_RETRY = 3;
/**
* Recognized key management schemes.
@@ -798,27 +802,37 @@ public class WifiConfiguration implements Parcelable {
* @hide
* Randomized MAC address to use with this particular network
*/
@NonNull
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.
* by verifying that it is non-null, unicast, locally assigned, and not default mac.
* @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();
public static boolean isValidMacAddressForRandomization(MacAddress mac) {
return mac != null && !mac.isMulticastAddress() && mac.isLocallyAssigned()
&& !MacAddress.fromString(WifiInfo.DEFAULT_MAC_ADDRESS).equals(mac);
}
/**
* @hide
* Returns Randomized MAC address to use with the network.
* If it is not set/valid, create a new randomized address.
* If it is not set/valid, creates a new randomized address.
* If it can't generate a valid mac, returns the default MAC.
*/
public MacAddress getOrCreateRandomizedMacAddress() {
if (!isValidMacAddressForRandomization(mRandomizedMacAddress)) {
public @NonNull MacAddress getOrCreateRandomizedMacAddress() {
int randomMacGenerationCount = 0;
while (!isValidMacAddressForRandomization(mRandomizedMacAddress)
&& randomMacGenerationCount < MAXIMUM_RANDOM_MAC_GENERATION_RETRY) {
mRandomizedMacAddress = MacAddress.createRandomUnicastAddress();
randomMacGenerationCount++;
}
if (!isValidMacAddressForRandomization(mRandomizedMacAddress)) {
mRandomizedMacAddress = MacAddress.fromString(WifiInfo.DEFAULT_MAC_ADDRESS);
}
return mRandomizedMacAddress;
}
@@ -828,10 +842,7 @@ public class WifiConfiguration implements Parcelable {
* 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() {
if (mRandomizedMacAddress == null) {
mRandomizedMacAddress = MacAddress.ALL_ZEROS_ADDRESS;
}
public @NonNull MacAddress getRandomizedMacAddress() {
return mRandomizedMacAddress;
}
@@ -839,7 +850,11 @@ public class WifiConfiguration implements Parcelable {
* @hide
* @param mac MacAddress to change into
*/
public void setRandomizedMacAddress(MacAddress mac) {
public void setRandomizedMacAddress(@NonNull MacAddress mac) {
if (mac == null) {
Log.e(TAG, "setRandomizedMacAddress received null MacAddress.");
return;
}
mRandomizedMacAddress = mac;
}
@@ -1532,7 +1547,7 @@ public class WifiConfiguration implements Parcelable {
creatorUid = -1;
shared = true;
dtimInterval = 0;
mRandomizedMacAddress = MacAddress.ALL_ZEROS_ADDRESS;
mRandomizedMacAddress = MacAddress.fromString(WifiInfo.DEFAULT_MAC_ADDRESS);
}
/**

View File

@@ -18,12 +18,14 @@ package android.net.wifi;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
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 android.net.wifi.WifiInfo;
import org.junit.Before;
import org.junit.Test;
@@ -176,22 +178,25 @@ public class WifiConfigurationTest {
@Test
public void testGetOrCreateRandomizedMacAddress_SavesAndReturnsSameAddress() {
WifiConfiguration config = new WifiConfiguration();
assertEquals(MacAddress.ALL_ZEROS_ADDRESS, config.getRandomizedMacAddress());
MacAddress defaultMac = MacAddress.fromString(WifiInfo.DEFAULT_MAC_ADDRESS);
assertEquals(defaultMac, config.getRandomizedMacAddress());
MacAddress firstMacAddress = config.getOrCreateRandomizedMacAddress();
MacAddress secondMacAddress = config.getOrCreateRandomizedMacAddress();
assertNotEquals(defaultMac, firstMacAddress);
assertEquals(firstMacAddress, secondMacAddress);
}
@Test
public void testSetRandomizedMacAddress_ChangesSavedAddress() {
WifiConfiguration config = new WifiConfiguration();
assertEquals(MacAddress.ALL_ZEROS_ADDRESS, config.getRandomizedMacAddress());
MacAddress defaultMac = MacAddress.fromString(WifiInfo.DEFAULT_MAC_ADDRESS);
assertEquals(defaultMac, config.getRandomizedMacAddress());
MacAddress macToChangeInto = MacAddress.createRandomUnicastAddress();
config.setRandomizedMacAddress(macToChangeInto);
MacAddress macAfterChange = config.getOrCreateRandomizedMacAddress();
MacAddress macAfterChange = config.getRandomizedMacAddress();
assertEquals(macToChangeInto, macAfterChange);
}
@@ -200,24 +205,37 @@ public class WifiConfigurationTest {
public void testGetOrCreateRandomizedMacAddress_ReRandomizesInvalidAddress() {
WifiConfiguration config = new WifiConfiguration();
MacAddress defaultMac = MacAddress.fromString(WifiInfo.DEFAULT_MAC_ADDRESS);
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));
assertNotEquals(macAfterChange, null);
config.setRandomizedMacAddress(defaultMac);
macAfterChange = config.getOrCreateRandomizedMacAddress();
assertNotEquals(macAfterChange, defaultMac);
config.setRandomizedMacAddress(macAddressZeroes);
macAfterChange = config.getOrCreateRandomizedMacAddress();
assertFalse(macAfterChange.equals(macAddressZeroes));
assertNotEquals(macAfterChange, macAddressZeroes);
config.setRandomizedMacAddress(macAddressMulticast);
macAfterChange = config.getOrCreateRandomizedMacAddress();
assertFalse(macAfterChange.equals(macAddressMulticast));
assertNotEquals(macAfterChange, macAddressMulticast);
config.setRandomizedMacAddress(macAddressGlobal);
macAfterChange = config.getOrCreateRandomizedMacAddress();
assertFalse(macAfterChange.equals(macAddressGlobal));
assertNotEquals(macAfterChange, macAddressGlobal);
}
@Test
public void testSetRandomizedMacAddress_DoesNothingWhenNull() {
WifiConfiguration config = new WifiConfiguration();
MacAddress defaultMac = MacAddress.fromString(WifiInfo.DEFAULT_MAC_ADDRESS);
config.setRandomizedMacAddress(null);
assertEquals(defaultMac, config.getRandomizedMacAddress());
}
}