Cleanup the TetheredClients API

Add comments to getters as requested in API review, and remove the
expirationTime private field that was planned to be replaced with
LinkAddress expiration.

Test: atest TetheringTests
Fixes: 150878126
Change-Id: Iecf65859cdeeaac2fa7b817b4f505c510424ac89
This commit is contained in:
Remi NGUYEN VAN
2020-03-09 15:38:45 +09:00
parent 97eb4abfed
commit dfdf750417
4 changed files with 53 additions and 25 deletions

View File

@@ -64,16 +64,26 @@ public final class TetheredClient implements Parcelable {
dest.writeInt(mTetheringType);
}
/**
* Get the MAC address used to identify the client.
*/
@NonNull
public MacAddress getMacAddress() {
return mMacAddress;
}
/**
* Get information on the list of addresses that are associated with the client.
*/
@NonNull
public List<AddressInfo> getAddresses() {
return new ArrayList<>(mAddresses);
}
/**
* Get the type of tethering used by the client.
* @return one of the {@code TetheringManager#TETHERING_*} constants.
*/
public int getTetheringType() {
return mTetheringType;
}
@@ -115,45 +125,47 @@ public final class TetheredClient implements Parcelable {
private final LinkAddress mAddress;
@Nullable
private final String mHostname;
// TODO: use LinkAddress expiration time once it is supported
private final long mExpirationTime;
/** @hide */
public AddressInfo(@NonNull LinkAddress address, @Nullable String hostname) {
this(address, hostname, 0);
}
/** @hide */
public AddressInfo(@NonNull LinkAddress address, String hostname, long expirationTime) {
this.mAddress = address;
this.mHostname = hostname;
this.mExpirationTime = expirationTime;
}
private AddressInfo(Parcel in) {
this(in.readParcelable(null), in.readString(), in.readLong());
this(in.readParcelable(null), in.readString());
}
@Override
public void writeToParcel(@NonNull Parcel dest, int flags) {
dest.writeParcelable(mAddress, flags);
dest.writeString(mHostname);
dest.writeLong(mExpirationTime);
}
/**
* Get the link address (including prefix length and lifetime) used by the client.
*
* This may be an IPv4 or IPv6 address.
*/
@NonNull
public LinkAddress getAddress() {
return mAddress;
}
/**
* Get the hostname that was advertised by the client when obtaining its address, if any.
*/
@Nullable
public String getHostname() {
return mHostname;
}
/** @hide TODO: use expiration time in LinkAddress */
/**
* Get the expiration time of the address assigned to the client.
* @hide
*/
public long getExpirationTime() {
return mExpirationTime;
return mAddress.getExpirationTime();
}
@Override
@@ -163,7 +175,7 @@ public final class TetheredClient implements Parcelable {
@Override
public int hashCode() {
return Objects.hash(mAddress, mHostname, mExpirationTime);
return Objects.hash(mAddress, mHostname);
}
@Override
@@ -173,8 +185,7 @@ public final class TetheredClient implements Parcelable {
// Use .equals() for addresses as all changes, including address expiry changes,
// should be included.
return other.mAddress.equals(mAddress)
&& Objects.equals(mHostname, other.mHostname)
&& mExpirationTime == other.mExpirationTime;
&& Objects.equals(mHostname, other.mHostname);
}
@NonNull

View File

@@ -24,6 +24,7 @@ import static android.net.util.NetworkConstants.FF;
import static android.net.util.NetworkConstants.RFC7421_PREFIX_LENGTH;
import static android.net.util.NetworkConstants.asByte;
import static android.net.util.TetheringMessageBase.BASE_IPSERVER;
import static android.system.OsConstants.RT_SCOPE_UNIVERSE;
import android.net.INetd;
import android.net.INetworkStackStatusCallback;
@@ -448,7 +449,9 @@ public class IpServer extends StateMachine {
final ArrayList<TetheredClient> leases = new ArrayList<>();
for (DhcpLeaseParcelable lease : leaseParcelables) {
final LinkAddress address = new LinkAddress(
intToInet4AddressHTH(lease.netAddr), lease.prefixLength);
intToInet4AddressHTH(lease.netAddr), lease.prefixLength,
0 /* flags */, RT_SCOPE_UNIVERSE /* as per RFC6724#3.2 */,
lease.expTime /* deprecationTime */, lease.expTime /* expirationTime */);
final MacAddress macAddress;
try {
@@ -460,7 +463,7 @@ public class IpServer extends StateMachine {
}
final TetheredClient.AddressInfo addressInfo = new TetheredClient.AddressInfo(
address, lease.hostname, lease.expTime);
address, lease.hostname);
leases.add(new TetheredClient(
macAddress,
Collections.singletonList(addressInfo),

View File

@@ -20,6 +20,7 @@ import android.net.InetAddresses.parseNumericAddress
import android.net.TetheredClient.AddressInfo
import android.net.TetheringManager.TETHERING_BLUETOOTH
import android.net.TetheringManager.TETHERING_USB
import android.system.OsConstants.RT_SCOPE_UNIVERSE
import androidx.test.filters.SmallTest
import androidx.test.runner.AndroidJUnit4
import com.android.testutils.assertParcelSane
@@ -30,11 +31,19 @@ import kotlin.test.assertNotEquals
private val TEST_MACADDR = MacAddress.fromBytes(byteArrayOf(12, 23, 34, 45, 56, 67))
private val TEST_OTHER_MACADDR = MacAddress.fromBytes(byteArrayOf(23, 34, 45, 56, 67, 78))
private val TEST_ADDR1 = LinkAddress(parseNumericAddress("192.168.113.3"), 24)
private val TEST_ADDR2 = LinkAddress(parseNumericAddress("fe80::1:2:3"), 64)
private val TEST_ADDR1 = makeLinkAddress("192.168.113.3", prefixLength = 24, expTime = 123L)
private val TEST_ADDR2 = makeLinkAddress("fe80::1:2:3", prefixLength = 64, expTime = 456L)
private val TEST_ADDRINFO1 = AddressInfo(TEST_ADDR1, "test_hostname")
private val TEST_ADDRINFO2 = AddressInfo(TEST_ADDR2, null)
private fun makeLinkAddress(addr: String, prefixLength: Int, expTime: Long) = LinkAddress(
parseNumericAddress(addr),
prefixLength,
0 /* flags */,
RT_SCOPE_UNIVERSE,
expTime /* deprecationTime */,
expTime /* expirationTime */)
@RunWith(AndroidJUnit4::class)
@SmallTest
class TetheredClientTest {

View File

@@ -46,23 +46,28 @@ class ConnectedClientsTrackerTest {
private val client1Addr = MacAddress.fromString("01:23:45:67:89:0A")
private val client1 = TetheredClient(client1Addr, listOf(
AddressInfo(LinkAddress("192.168.43.44/32"), null /* hostname */, clock.time + 20)),
makeAddrInfo("192.168.43.44/32", null /* hostname */, clock.time + 20)),
TETHERING_WIFI)
private val wifiClient1 = makeWifiClient(client1Addr)
private val client2Addr = MacAddress.fromString("02:34:56:78:90:AB")
private val client2Exp30AddrInfo = AddressInfo(
LinkAddress("192.168.43.45/32"), "my_hostname", clock.time + 30)
private val client2Exp30AddrInfo = makeAddrInfo(
"192.168.43.45/32", "my_hostname", clock.time + 30)
private val client2 = TetheredClient(client2Addr, listOf(
client2Exp30AddrInfo,
AddressInfo(LinkAddress("2001:db8:12::34/72"), "other_hostname", clock.time + 10)),
makeAddrInfo("2001:db8:12::34/72", "other_hostname", clock.time + 10)),
TETHERING_WIFI)
private val wifiClient2 = makeWifiClient(client2Addr)
private val client3Addr = MacAddress.fromString("03:45:67:89:0A:BC")
private val client3 = TetheredClient(client3Addr,
listOf(AddressInfo(LinkAddress("2001:db8:34::34/72"), "other_other_hostname",
clock.time + 10)),
listOf(makeAddrInfo("2001:db8:34::34/72", "other_other_hostname", clock.time + 10)),
TETHERING_USB)
private fun makeAddrInfo(addr: String, hostname: String?, expTime: Long) =
LinkAddress(addr).let {
AddressInfo(LinkAddress(it.address, it.prefixLength, it.flags, it.scope,
expTime /* deprecationTime */, expTime /* expirationTime */), hostname)
}
@Test
fun testUpdateConnectedClients() {
doReturn(emptyList<TetheredClient>()).`when`(server1).allLeases