From 593054729c48cd5481f0c0c6c0d40842a602242c Mon Sep 17 00:00:00 2001 From: Aaron Huang Date: Wed, 30 Oct 2019 11:06:55 +0800 Subject: [PATCH] Add cts for MacAddress public API Add test for new public APIs Test: atest android.net.cts.MacAddressTest Bug: 139268426 Bug: 135998869 Change-Id: Ib2b73851f621f1648904276ec12ceea02f4b5970 --- .../src/android/net/cts/MacAddressTest.java | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/tests/cts/net/src/android/net/cts/MacAddressTest.java b/tests/cts/net/src/android/net/cts/MacAddressTest.java index af1e76071d..4d25e620d4 100644 --- a/tests/cts/net/src/android/net/cts/MacAddressTest.java +++ b/tests/cts/net/src/android/net/cts/MacAddressTest.java @@ -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); + } }