MacAddress: Use SecureRandom and add a 46 bit randomized MAC generator

Use SecureRandom instead of Random since Random is time based and can
increase the chance of generating same MAC address across multiple
devices.

createRandomUnicastAddress should randomize all bits of the address,
except for locally assigned bit and unicast bit. The previous method
that only randomizes NIC and use Google Base OUI is renamed to
createRandomUnicastAddressWithGoogleBase.

Bug: 72450936
Test: runtest frameworks-net
Change-Id: Icda650638c2c1c9fd90d509a87e86347c0e05f2d
This commit is contained in:
Jong Wook Kim
2018-01-31 19:03:19 -08:00
parent 997937820d
commit 93dd5e6964
2 changed files with 34 additions and 6 deletions

View File

@@ -172,7 +172,7 @@ public class MacAddressTest {
final int iterations = 1000;
final String expectedAndroidOui = "da:a1:19";
for (int i = 0; i < iterations; i++) {
MacAddress mac = MacAddress.createRandomUnicastAddress();
MacAddress mac = MacAddress.createRandomUnicastAddressWithGoogleBase();
String stringRepr = mac.toString();
assertTrue(stringRepr + " expected to be a locally assigned address",
@@ -195,6 +195,15 @@ public class MacAddressTest {
assertTrue(stringRepr + " expected to begin with " + expectedLocalOui,
stringRepr.startsWith(expectedLocalOui));
}
for (int i = 0; i < iterations; i++) {
MacAddress mac = MacAddress.createRandomUnicastAddress();
String stringRepr = mac.toString();
assertTrue(stringRepr + " expected to be a locally assigned address",
mac.isLocallyAssigned());
assertEquals(MacAddress.TYPE_UNICAST, mac.getAddressType());
}
}
@Test