diff options
author | Lorenzo Colitti <lorenzo@google.com> | 2020-02-19 19:53:59 +0900 |
---|---|---|
committer | Lorenzo Colitti <lorenzo@google.com> | 2020-02-19 20:34:05 +0900 |
commit | 32336c98689bcfe363390cf8398619dc66fe3107 (patch) | |
tree | 9aba5f9e05c906dc086ea7a827387eeb83176e79 | |
parent | 21907e1cdc3dc9f86c4446d7e111a6637aefcec4 (diff) |
Add a hasMacAddress member to InterfaceParams.
This is a convenience function for callers who would like to know
whether the interface has a MAC address. It is needed because the
constructed object will have a MAC address of 02:00:00:00:00:00
if the interface did not have a MAC address.
Test: added minimal unit test coverage
Change-Id: I422422d032afbbabcf8594def76702bb053f0a96
3 files changed, 24 insertions, 1 deletions
diff --git a/common/moduleutils/src/android/net/util/InterfaceParams.java b/common/moduleutils/src/android/net/util/InterfaceParams.java index 3ba02b5..7e05a8d 100644 --- a/common/moduleutils/src/android/net/util/InterfaceParams.java +++ b/common/moduleutils/src/android/net/util/InterfaceParams.java @@ -38,6 +38,7 @@ import java.net.SocketException; public class InterfaceParams { public final String name; public final int index; + public final boolean hasMacAddress; public final MacAddress macAddr; public final int defaultMtu; @@ -69,7 +70,8 @@ public class InterfaceParams { checkArgument((index > 0), "invalid interface index"); this.name = name; this.index = index; - this.macAddr = (macAddr != null) ? macAddr : MacAddress.fromBytes(new byte[] { + this.hasMacAddress = (macAddr != null); + this.macAddr = hasMacAddress ? macAddr : MacAddress.fromBytes(new byte[] { 0x02, 0x00, 0x00, 0x00, 0x00, 0x00 }); this.defaultMtu = (defaultMtu > IPV6_MIN_MTU) ? defaultMtu : IPV6_MIN_MTU; } diff --git a/tests/integration/src/android/net/ip/IpClientIntegrationTest.java b/tests/integration/src/android/net/ip/IpClientIntegrationTest.java index 4bbee9a..a6543bc 100644 --- a/tests/integration/src/android/net/ip/IpClientIntegrationTest.java +++ b/tests/integration/src/android/net/ip/IpClientIntegrationTest.java @@ -52,6 +52,7 @@ import static junit.framework.Assert.fail; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; @@ -920,6 +921,24 @@ public class IpClientIntegrationTest { } @Test + public void testInterfaceParams() throws Exception { + InterfaceParams params = InterfaceParams.getByName(mIfaceName); + assertNotNull(params); + assertEquals(mIfaceName, params.name); + assertTrue(params.index > 0); + assertNotNull(params.macAddr); + assertTrue(params.hasMacAddress); + + // Sanity check. + params = InterfaceParams.getByName("lo"); + assertNotNull(params); + assertEquals("lo", params.name); + assertTrue(params.index > 0); + assertNotNull(params.macAddr); + assertFalse(params.hasMacAddress); + } + + @Test public void testDhcpInit() throws Exception { startIpClientProvisioning(false /* isDhcpLeaseCacheEnabled */, false /* shouldReplyRapidCommitAck */, false /* isPreconnectionEnabled */, diff --git a/tests/unit/src/android/net/util/InterfaceParamsTest.java b/tests/unit/src/android/net/util/InterfaceParamsTest.java index 5a2b9c6..1be4368 100644 --- a/tests/unit/src/android/net/util/InterfaceParamsTest.java +++ b/tests/unit/src/android/net/util/InterfaceParamsTest.java @@ -17,6 +17,7 @@ package android.net.util; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; @@ -49,6 +50,7 @@ public class InterfaceParamsTest { assertEquals("lo", ifParams.name); assertTrue(ifParams.index > 0); assertNotNull(ifParams.macAddr); + assertFalse(ifParams.hasMacAddress); assertTrue(ifParams.defaultMtu >= NetworkStackConstants.ETHER_MTU); } } |