Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 594d0eae authored by Automerger Merge Worker's avatar Automerger Merge Worker
Browse files

Merge "Cleanup the TetheredClients API" am: 89711eae am: be7c8955 am:...

Merge "Cleanup the TetheredClients API" am: 89711eae am: be7c8955 am: 1f6a7308 am: 53f2f871 am: 8e0e9d69

Change-Id: I0a1711d8b2b82b688ed70eba9bfebec133c863a9
parents 0f243a9d 8e0e9d69
Loading
Loading
Loading
Loading
+26 −15
Original line number Original line Diff line number Diff line
@@ -64,16 +64,26 @@ public final class TetheredClient implements Parcelable {
        dest.writeInt(mTetheringType);
        dest.writeInt(mTetheringType);
    }
    }


    /**
     * Get the MAC address used to identify the client.
     */
    @NonNull
    @NonNull
    public MacAddress getMacAddress() {
    public MacAddress getMacAddress() {
        return mMacAddress;
        return mMacAddress;
    }
    }


    /**
     * Get information on the list of addresses that are associated with the client.
     */
    @NonNull
    @NonNull
    public List<AddressInfo> getAddresses() {
    public List<AddressInfo> getAddresses() {
        return new ArrayList<>(mAddresses);
        return new ArrayList<>(mAddresses);
    }
    }


    /**
     * Get the type of tethering used by the client.
     * @return one of the {@code TetheringManager#TETHERING_*} constants.
     */
    public int getTetheringType() {
    public int getTetheringType() {
        return mTetheringType;
        return mTetheringType;
    }
    }
@@ -115,45 +125,47 @@ public final class TetheredClient implements Parcelable {
        private final LinkAddress mAddress;
        private final LinkAddress mAddress;
        @Nullable
        @Nullable
        private final String mHostname;
        private final String mHostname;
        // TODO: use LinkAddress expiration time once it is supported
        private final long mExpirationTime;


        /** @hide */
        /** @hide */
        public AddressInfo(@NonNull LinkAddress address, @Nullable String hostname) {
        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.mAddress = address;
            this.mHostname = hostname;
            this.mHostname = hostname;
            this.mExpirationTime = expirationTime;
        }
        }


        private AddressInfo(Parcel in) {
        private AddressInfo(Parcel in) {
            this(in.readParcelable(null),  in.readString(), in.readLong());
            this(in.readParcelable(null),  in.readString());
        }
        }


        @Override
        @Override
        public void writeToParcel(@NonNull Parcel dest, int flags) {
        public void writeToParcel(@NonNull Parcel dest, int flags) {
            dest.writeParcelable(mAddress, flags);
            dest.writeParcelable(mAddress, flags);
            dest.writeString(mHostname);
            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
        @NonNull
        public LinkAddress getAddress() {
        public LinkAddress getAddress() {
            return mAddress;
            return mAddress;
        }
        }


        /**
         * Get the hostname that was advertised by the client when obtaining its address, if any.
         */
        @Nullable
        @Nullable
        public String getHostname() {
        public String getHostname() {
            return mHostname;
            return mHostname;
        }
        }


        /** @hide TODO: use expiration time in LinkAddress */
        /**
         * Get the expiration time of the address assigned to the client.
         * @hide
         */
        public long getExpirationTime() {
        public long getExpirationTime() {
            return mExpirationTime;
            return mAddress.getExpirationTime();
        }
        }


        @Override
        @Override
@@ -163,7 +175,7 @@ public final class TetheredClient implements Parcelable {


        @Override
        @Override
        public int hashCode() {
        public int hashCode() {
            return Objects.hash(mAddress, mHostname, mExpirationTime);
            return Objects.hash(mAddress, mHostname);
        }
        }


        @Override
        @Override
@@ -173,8 +185,7 @@ public final class TetheredClient implements Parcelable {
            // Use .equals() for addresses as all changes, including address expiry changes,
            // Use .equals() for addresses as all changes, including address expiry changes,
            // should be included.
            // should be included.
            return other.mAddress.equals(mAddress)
            return other.mAddress.equals(mAddress)
                    && Objects.equals(mHostname, other.mHostname)
                    && Objects.equals(mHostname, other.mHostname);
                    && mExpirationTime == other.mExpirationTime;
        }
        }


        @NonNull
        @NonNull
+5 −2
Original line number Original line Diff line number Diff line
@@ -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.RFC7421_PREFIX_LENGTH;
import static android.net.util.NetworkConstants.asByte;
import static android.net.util.NetworkConstants.asByte;
import static android.net.util.TetheringMessageBase.BASE_IPSERVER;
import static android.net.util.TetheringMessageBase.BASE_IPSERVER;
import static android.system.OsConstants.RT_SCOPE_UNIVERSE;


import android.net.INetd;
import android.net.INetd;
import android.net.INetworkStackStatusCallback;
import android.net.INetworkStackStatusCallback;
@@ -448,7 +449,9 @@ public class IpServer extends StateMachine {
            final ArrayList<TetheredClient> leases = new ArrayList<>();
            final ArrayList<TetheredClient> leases = new ArrayList<>();
            for (DhcpLeaseParcelable lease : leaseParcelables) {
            for (DhcpLeaseParcelable lease : leaseParcelables) {
                final LinkAddress address = new LinkAddress(
                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;
                final MacAddress macAddress;
                try {
                try {
@@ -460,7 +463,7 @@ public class IpServer extends StateMachine {
                }
                }


                final TetheredClient.AddressInfo addressInfo = new TetheredClient.AddressInfo(
                final TetheredClient.AddressInfo addressInfo = new TetheredClient.AddressInfo(
                        address, lease.hostname, lease.expTime);
                        address, lease.hostname);
                leases.add(new TetheredClient(
                leases.add(new TetheredClient(
                        macAddress,
                        macAddress,
                        Collections.singletonList(addressInfo),
                        Collections.singletonList(addressInfo),
+11 −2
Original line number Original line Diff line number Diff line
@@ -20,6 +20,7 @@ import android.net.InetAddresses.parseNumericAddress
import android.net.TetheredClient.AddressInfo
import android.net.TetheredClient.AddressInfo
import android.net.TetheringManager.TETHERING_BLUETOOTH
import android.net.TetheringManager.TETHERING_BLUETOOTH
import android.net.TetheringManager.TETHERING_USB
import android.net.TetheringManager.TETHERING_USB
import android.system.OsConstants.RT_SCOPE_UNIVERSE
import androidx.test.filters.SmallTest
import androidx.test.filters.SmallTest
import androidx.test.runner.AndroidJUnit4
import androidx.test.runner.AndroidJUnit4
import com.android.testutils.assertParcelSane
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_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_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_ADDR1 = makeLinkAddress("192.168.113.3", prefixLength = 24, expTime = 123L)
private val TEST_ADDR2 = LinkAddress(parseNumericAddress("fe80::1:2:3"), 64)
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_ADDRINFO1 = AddressInfo(TEST_ADDR1, "test_hostname")
private val TEST_ADDRINFO2 = AddressInfo(TEST_ADDR2, null)
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)
@RunWith(AndroidJUnit4::class)
@SmallTest
@SmallTest
class TetheredClientTest {
class TetheredClientTest {
+11 −6
Original line number Original line Diff line number Diff line
@@ -46,23 +46,28 @@ class ConnectedClientsTrackerTest {


    private val client1Addr = MacAddress.fromString("01:23:45:67:89:0A")
    private val client1Addr = MacAddress.fromString("01:23:45:67:89:0A")
    private val client1 = TetheredClient(client1Addr, listOf(
    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)
            TETHERING_WIFI)
    private val wifiClient1 = makeWifiClient(client1Addr)
    private val wifiClient1 = makeWifiClient(client1Addr)
    private val client2Addr = MacAddress.fromString("02:34:56:78:90:AB")
    private val client2Addr = MacAddress.fromString("02:34:56:78:90:AB")
    private val client2Exp30AddrInfo = AddressInfo(
    private val client2Exp30AddrInfo = makeAddrInfo(
            LinkAddress("192.168.43.45/32"), "my_hostname", clock.time + 30)
            "192.168.43.45/32", "my_hostname", clock.time + 30)
    private val client2 = TetheredClient(client2Addr, listOf(
    private val client2 = TetheredClient(client2Addr, listOf(
            client2Exp30AddrInfo,
            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)
            TETHERING_WIFI)
    private val wifiClient2 = makeWifiClient(client2Addr)
    private val wifiClient2 = makeWifiClient(client2Addr)
    private val client3Addr = MacAddress.fromString("03:45:67:89:0A:BC")
    private val client3Addr = MacAddress.fromString("03:45:67:89:0A:BC")
    private val client3 = TetheredClient(client3Addr,
    private val client3 = TetheredClient(client3Addr,
            listOf(AddressInfo(LinkAddress("2001:db8:34::34/72"), "other_other_hostname",
            listOf(makeAddrInfo("2001:db8:34::34/72", "other_other_hostname", clock.time + 10)),
                    clock.time + 10)),
            TETHERING_USB)
            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
    @Test
    fun testUpdateConnectedClients() {
    fun testUpdateConnectedClients() {
        doReturn(emptyList<TetheredClient>()).`when`(server1).allLeases
        doReturn(emptyList<TetheredClient>()).`when`(server1).allLeases