Merge "Add cts for MacAddress public API"
am: 0f364ba113
Change-Id: I7e1f870ecc47fda0e3691782389cb2920e76fd13
This commit is contained in:
@@ -20,6 +20,11 @@ import static android.net.MacAddress.TYPE_BROADCAST;
|
||||
import static android.net.MacAddress.TYPE_MULTICAST;
|
||||
import static android.net.MacAddress.TYPE_UNICAST;
|
||||
|
||||
import static com.android.testutils.ParcelUtilsKt.assertParcelSane;
|
||||
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import android.net.MacAddress;
|
||||
@@ -30,6 +35,7 @@ import androidx.test.runner.AndroidJUnit4;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import java.net.Inet6Address;
|
||||
import java.util.Arrays;
|
||||
|
||||
@SmallTest
|
||||
@@ -161,4 +167,57 @@ public class MacAddressTest {
|
||||
} catch (NullPointerException excepted) {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMatches() {
|
||||
// match 4 bytes prefix
|
||||
assertTrue(MacAddress.fromString("aa:bb:cc:dd:ee:11").matches(
|
||||
MacAddress.fromString("aa:bb:cc:dd:00:00"),
|
||||
MacAddress.fromString("ff:ff:ff:ff:00:00")));
|
||||
|
||||
// match bytes 0,1,2 and 5
|
||||
assertTrue(MacAddress.fromString("aa:bb:cc:dd:ee:11").matches(
|
||||
MacAddress.fromString("aa:bb:cc:00:00:11"),
|
||||
MacAddress.fromString("ff:ff:ff:00:00:ff")));
|
||||
|
||||
// match 34 bit prefix
|
||||
assertTrue(MacAddress.fromString("aa:bb:cc:dd:ee:11").matches(
|
||||
MacAddress.fromString("aa:bb:cc:dd:c0:00"),
|
||||
MacAddress.fromString("ff:ff:ff:ff:c0:00")));
|
||||
|
||||
// fail to match 36 bit prefix
|
||||
assertFalse(MacAddress.fromString("aa:bb:cc:dd:ee:11").matches(
|
||||
MacAddress.fromString("aa:bb:cc:dd:40:00"),
|
||||
MacAddress.fromString("ff:ff:ff:ff:f0:00")));
|
||||
|
||||
// match all 6 bytes
|
||||
assertTrue(MacAddress.fromString("aa:bb:cc:dd:ee:11").matches(
|
||||
MacAddress.fromString("aa:bb:cc:dd:ee:11"),
|
||||
MacAddress.fromString("ff:ff:ff:ff:ff:ff")));
|
||||
|
||||
// match none of 6 bytes
|
||||
assertTrue(MacAddress.fromString("aa:bb:cc:dd:ee:11").matches(
|
||||
MacAddress.fromString("00:00:00:00:00:00"),
|
||||
MacAddress.fromString("00:00:00:00:00:00")));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that link-local address generation from MAC is valid.
|
||||
*/
|
||||
@Test
|
||||
public void testLinkLocalFromMacGeneration() {
|
||||
final MacAddress mac = MacAddress.fromString("52:74:f2:b1:a8:7f");
|
||||
final byte[] inet6ll = {(byte) 0xfe, (byte) 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x50,
|
||||
0x74, (byte) 0xf2, (byte) 0xff, (byte) 0xfe, (byte) 0xb1, (byte) 0xa8, 0x7f};
|
||||
final Inet6Address llv6 = mac.getLinkLocalIpv6FromEui48Mac();
|
||||
assertTrue(llv6.isLinkLocalAddress());
|
||||
assertArrayEquals(inet6ll, llv6.getAddress());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParcelMacAddress() {
|
||||
final MacAddress mac = MacAddress.fromString("52:74:f2:b1:a8:7f");
|
||||
|
||||
assertParcelSane(mac, 1);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user