From 323d22a49770a5ee432c5ed3e7f06b970c3bb596 Mon Sep 17 00:00:00 2001 From: Motomu Utsumi Date: Thu, 10 Jul 2025 17:11:52 +0900 Subject: [PATCH] Pass bluetoothUseRandomAddress via RoutingCoordinator constructor This refactoring changes how the bluetoothUseRandomAddress flag is accessed. PrivateAddressCoordinator is in a library used by multiple mainline modules(Tethering, NetworkStack, CaptivePortal, WiFi) and cannot directly read a Tethering mainline beta flag. To fix this, the flag's value is now passed down through constructors. An alternative solution would be to split the Java library for each module, but with this solution, a single utility class cannot be shared. This commit is a no-op as the passed value is hardcoded to false. Test: TH Flag: EXEMPT refactor Bug: 287177081 Change-Id: I5f2174ee94b55e7a7298b9742221d6e6c3c5cac5 --- .../tethering/TetheringDependencies.java | 5 ++--- .../PrivateAddressCoordinatorTest.java | 16 +++++++++++----- .../networkstack/tethering/TetheringTest.java | 3 ++- .../android/server/ConnectivityService.java | 14 +++++++++++++- .../util/PrivateAddressCoordinator.java | 19 +++++-------------- .../util/RoutingCoordinatorService.java | 17 +++++++++++------ .../util/RoutingCoordinatorServiceTest.kt | 7 ++++++- 7 files changed, 50 insertions(+), 31 deletions(-) diff --git a/Tethering/src/com/android/networkstack/tethering/TetheringDependencies.java b/Tethering/src/com/android/networkstack/tethering/TetheringDependencies.java index 00a7f09079..26f3a6b588 100644 --- a/Tethering/src/com/android/networkstack/tethering/TetheringDependencies.java +++ b/Tethering/src/com/android/networkstack/tethering/TetheringDependencies.java @@ -138,9 +138,8 @@ public abstract class TetheringDependencies { IBinder binder; if (!SdkLevel.isAtLeastS()) { final ConnectivityManager cm = context.getSystemService(ConnectivityManager.class); - binder = - new RoutingCoordinatorService( - getINetd(context, log), cm::getAllNetworks, context); + binder = new RoutingCoordinatorService(getINetd(context, log), cm::getAllNetworks, + context, false /* bluetoothTetheringUseRandomAddress */); } else { binder = ConnectivityInternalApiUtil.getRoutingCoordinator(context); } diff --git a/Tethering/tests/unit/src/com/android/networkstack/tethering/PrivateAddressCoordinatorTest.java b/Tethering/tests/unit/src/com/android/networkstack/tethering/PrivateAddressCoordinatorTest.java index 7d4aa697b6..ad75d6bb20 100644 --- a/Tethering/tests/unit/src/com/android/networkstack/tethering/PrivateAddressCoordinatorTest.java +++ b/Tethering/tests/unit/src/com/android/networkstack/tethering/PrivateAddressCoordinatorTest.java @@ -96,7 +96,13 @@ public final class PrivateAddressCoordinatorTest { mMobileNetwork2, mMobileNetwork3, mMobileNetwork4, mMobileNetwork5, mMobileNetwork6}; private PrivateAddressCoordinator makePrivateAddressCoordinator() { - return spy(new PrivateAddressCoordinator(mConnectivityMgr::getAllNetworks, mDeps)); + return makePrivateAddressCoordinator(false); + } + + private PrivateAddressCoordinator makePrivateAddressCoordinator( + boolean bluetoothTetheringUseRandomAddress) { + return spy(new PrivateAddressCoordinator( + mConnectivityMgr::getAllNetworks, mDeps, bluetoothTetheringUseRandomAddress)); } private void setUpIpServer(IpServer ipServer, int interfaceType) throws Exception { @@ -352,8 +358,8 @@ public final class PrivateAddressCoordinatorTest { @Test public void testBluetoothPrefix_legacy() throws Exception { - when(mDeps.shouldBluetoothUseRandomAddress()).thenReturn(false); - mPrivateAddressCoordinator = makePrivateAddressCoordinator(); + mPrivateAddressCoordinator = + makePrivateAddressCoordinator(false /* bluetoothTetheringUseRandomAddress */); final LinkAddress bluetoothAddress = requestStickyDownstreamAddress(mBluetoothIpServer, CONNECTIVITY_SCOPE_GLOBAL); assertEquals(mLegacyBluetoothAddress, bluetoothAddress); @@ -361,8 +367,8 @@ public final class PrivateAddressCoordinatorTest { @Test public void testBluetoothPrefix_randomized() throws Exception { - when(mDeps.shouldBluetoothUseRandomAddress()).thenReturn(true); - mPrivateAddressCoordinator = makePrivateAddressCoordinator(); + mPrivateAddressCoordinator = + makePrivateAddressCoordinator(true /* bluetoothTetheringUseRandomAddress */); final LinkAddress bluetoothAddress = requestStickyDownstreamAddress(mBluetoothIpServer, CONNECTIVITY_SCOPE_GLOBAL); assertNotEquals(mLegacyBluetoothAddress, bluetoothAddress); diff --git a/Tethering/tests/unit/src/com/android/networkstack/tethering/TetheringTest.java b/Tethering/tests/unit/src/com/android/networkstack/tethering/TetheringTest.java index 5614a35219..e3ef75cff4 100644 --- a/Tethering/tests/unit/src/com/android/networkstack/tethering/TetheringTest.java +++ b/Tethering/tests/unit/src/com/android/networkstack/tethering/TetheringTest.java @@ -544,7 +544,8 @@ public class TetheringTest { RoutingCoordinatorService service = new RoutingCoordinatorService( getINetd(context, log), cm::getAllNetworks, - mPrivateAddressCoordinatorDependencies); + mPrivateAddressCoordinatorDependencies, + false /* bluetoothTetheringUseRandomAddress */); mRoutingCoordinatorManager = spy(new RoutingCoordinatorManager(context, service)); return mRoutingCoordinatorManager; } diff --git a/service/src/com/android/server/ConnectivityService.java b/service/src/com/android/server/ConnectivityService.java index 964d4f5cb5..d2dbbfd499 100644 --- a/service/src/com/android/server/ConnectivityService.java +++ b/service/src/com/android/server/ConnectivityService.java @@ -1991,6 +1991,17 @@ public class ConnectivityService extends IConnectivityManager.Stub @NonNull Consumer consumer) { return new AddressUpdateMonitor(h, log, tag, consumer); } + + /** + * Whether to randomize the tethering prefix for bluetooth as well. Requires T+ because + * in S this code is in the platform. + * This method is intended to be called only a single time. + * The flag value can change at runtime via a server push. + */ + public boolean shouldBluetoothTetheringUseRandomAddress() { + // TODO: add a mainline beta flag to enable the new behaviour. + return SdkLevel.isAtLeastT() && false; + } } public ConnectivityService(Context context) { @@ -2275,7 +2286,8 @@ public class ConnectivityService extends IConnectivityManager.Stub } mRoutingCoordinatorService = - new RoutingCoordinatorService(netd, this::getAllNetworks, mContext); + new RoutingCoordinatorService(netd, this::getAllNetworks, mContext, + mDeps.shouldBluetoothTetheringUseRandomAddress()); mMulticastRoutingCoordinatorService = mDeps.makeMulticastRoutingCoordinatorService(mHandler); diff --git a/staticlibs/device/com/android/net/module/util/PrivateAddressCoordinator.java b/staticlibs/device/com/android/net/module/util/PrivateAddressCoordinator.java index 474005b85c..ca266907f9 100644 --- a/staticlibs/device/com/android/net/module/util/PrivateAddressCoordinator.java +++ b/staticlibs/device/com/android/net/module/util/PrivateAddressCoordinator.java @@ -40,7 +40,6 @@ import android.util.ArrayMap; import androidx.annotation.Nullable; import com.android.internal.annotations.VisibleForTesting; -import com.android.modules.utils.build.SdkLevel; import java.io.PrintWriter; import java.net.Inet4Address; @@ -109,31 +108,23 @@ public class PrivateAddressCoordinator { public boolean isFeatureNotChickenedOut(String featureName) { return DeviceConfigUtils.isTetheringFeatureNotChickenedOut(mContext, featureName); } - - /** - * Whether to randomize the tethering prefix for bluetooth as well. Requires T+ because - * in S this code is in the platform. - */ - public boolean shouldBluetoothUseRandomAddress() { - // TODO: add a mainline beta flag to enable the new behaviour. - return SdkLevel.isAtLeastT() && false; - } } - public PrivateAddressCoordinator(Supplier getAllNetworksSupplier, Context context) { - this(getAllNetworksSupplier, new Dependencies(context)); + public PrivateAddressCoordinator(Supplier getAllNetworksSupplier, Context context, + boolean bluetoothTetheringUseRandomAddress) { + this(getAllNetworksSupplier, new Dependencies(context), bluetoothTetheringUseRandomAddress); } @VisibleForTesting public PrivateAddressCoordinator(Supplier getAllNetworksSupplier, - Dependencies deps) { + Dependencies deps, boolean bluetoothTetheringUseRandomAddress) { mDownstreams = new ArrayMap<>(); mUpstreamPrefixMap = new ArrayMap<>(); mGetAllNetworksSupplier = getAllNetworksSupplier; mDeps = deps; mCachedAddresses = new ArrayMap(); // Reserved static addresses for BLUETOOTH pre-T and WIFI_P2P. - if (!mDeps.shouldBluetoothUseRandomAddress()) { + if (!bluetoothTetheringUseRandomAddress) { mCachedAddresses.put(new AddressKey(TETHERING_BLUETOOTH, CONNECTIVITY_SCOPE_GLOBAL), new LinkAddress(LEGACY_BLUETOOTH_IFACE_ADDRESS)); } diff --git a/staticlibs/device/com/android/net/module/util/RoutingCoordinatorService.java b/staticlibs/device/com/android/net/module/util/RoutingCoordinatorService.java index 51eb47c121..61e830f956 100644 --- a/staticlibs/device/com/android/net/module/util/RoutingCoordinatorService.java +++ b/staticlibs/device/com/android/net/module/util/RoutingCoordinatorService.java @@ -56,18 +56,23 @@ public class RoutingCoordinatorService extends IRoutingCoordinator.Stub { @GuardedBy("mPrivateAddressCoordinatorLock") private final PrivateAddressCoordinator mPrivateAddressCoordinator; + // TODO: Remove bluetoothTetheringUseRandomAddress argument once the beta flag is rolled out public RoutingCoordinatorService(@NonNull INetd netd, - @NonNull Supplier getAllNetworksSupplier, - @NonNull Context context) { - this(netd, getAllNetworksSupplier, new PrivateAddressCoordinator.Dependencies(context)); + @NonNull Supplier getAllNetworksSupplier, + @NonNull Context context, + boolean bluetoothTetheringUseRandomAddress) { + this(netd, getAllNetworksSupplier, new PrivateAddressCoordinator.Dependencies(context), + bluetoothTetheringUseRandomAddress); } @VisibleForTesting public RoutingCoordinatorService(@NonNull INetd netd, - @NonNull Supplier getAllNetworksSupplier, - @NonNull PrivateAddressCoordinator.Dependencies pacDeps) { + @NonNull Supplier getAllNetworksSupplier, + @NonNull PrivateAddressCoordinator.Dependencies pacDeps, + boolean bluetoothTetheringUseRandomAddress) { mNetd = netd; - mPrivateAddressCoordinator = new PrivateAddressCoordinator(getAllNetworksSupplier, pacDeps); + mPrivateAddressCoordinator = new PrivateAddressCoordinator( + getAllNetworksSupplier, pacDeps, bluetoothTetheringUseRandomAddress); } /** diff --git a/staticlibs/tests/unit/src/com/android/net/module/util/RoutingCoordinatorServiceTest.kt b/staticlibs/tests/unit/src/com/android/net/module/util/RoutingCoordinatorServiceTest.kt index 035ce0fe12..43ab47dcd2 100644 --- a/staticlibs/tests/unit/src/com/android/net/module/util/RoutingCoordinatorServiceTest.kt +++ b/staticlibs/tests/unit/src/com/android/net/module/util/RoutingCoordinatorServiceTest.kt @@ -38,7 +38,12 @@ class RoutingCoordinatorServiceTest { val mNetd = mock(INetd::class.java) val mGetAllNetworksSupplier = { emptyArray() } val mContext = mock(Context::class.java) - val mService = RoutingCoordinatorService(mNetd, mGetAllNetworksSupplier, mContext) + val mService = RoutingCoordinatorService( + mNetd, + mGetAllNetworksSupplier, + mContext, + false /* bluetoothTetheringUseRandomAddress */ + ) @Test fun testInterfaceForward() {