diff --git a/service/src/com/android/server/connectivity/ClatCoordinator.java b/service/src/com/android/server/connectivity/ClatCoordinator.java index d87f250773..eb3e7cea7a 100644 --- a/service/src/com/android/server/connectivity/ClatCoordinator.java +++ b/service/src/com/android/server/connectivity/ClatCoordinator.java @@ -37,9 +37,10 @@ import android.os.ServiceSpecificException; import android.system.ErrnoException; import android.util.Log; +import androidx.annotation.RequiresApi; + import com.android.internal.annotations.VisibleForTesting; import com.android.internal.util.IndentingPrintWriter; -import com.android.modules.utils.build.SdkLevel; import com.android.net.module.util.BpfMap; import com.android.net.module.util.IBpfMap; import com.android.net.module.util.InterfaceParams; @@ -59,8 +60,6 @@ import java.net.InetAddress; import java.nio.ByteBuffer; import java.util.Objects; -import androidx.annotation.RequiresApi; - /** * This coordinator is responsible for providing clat relevant functionality. * diff --git a/service/src/com/android/server/connectivity/KeepaliveTracker.java b/service/src/com/android/server/connectivity/KeepaliveTracker.java index b4f74d5e5c..fe5d0cf2c3 100644 --- a/service/src/com/android/server/connectivity/KeepaliveTracker.java +++ b/service/src/com/android/server/connectivity/KeepaliveTracker.java @@ -104,19 +104,21 @@ public class KeepaliveTracker { // Allowed unprivileged keepalive slots per uid. Caller's permission will be enforced if // the number of remaining keepalive slots is less than or equal to the threshold. private final int mAllowedUnprivilegedSlotsForUid; - + private final Dependencies mDependencies; public KeepaliveTracker(Context context, Handler handler) { - this(context, handler, new TcpKeepaliveController(handler)); + this(context, handler, new TcpKeepaliveController(handler), new Dependencies()); } @VisibleForTesting - KeepaliveTracker(Context context, Handler handler, TcpKeepaliveController tcpController) { + KeepaliveTracker(Context context, Handler handler, TcpKeepaliveController tcpController, + Dependencies deps) { mTcpController = tcpController; mContext = context; + mDependencies = deps; - mSupportedKeepalives = KeepaliveResourceUtil.getSupportedKeepalives(context); + mSupportedKeepalives = mDependencies.getSupportedKeepalives(mContext); - final ConnectivityResources res = new ConnectivityResources(mContext); + final ConnectivityResources res = mDependencies.createConnectivityResources(mContext); mReservedPrivilegedSlots = res.get().getInteger( R.integer.config_reservedPrivilegedKeepaliveSlots); mAllowedUnprivilegedSlotsForUid = res.get().getInteger( @@ -725,6 +727,7 @@ public class KeepaliveTracker { srcAddress = InetAddresses.parseNumericAddress(srcAddrString); dstAddress = InetAddresses.parseNumericAddress(dstAddrString); } catch (IllegalArgumentException e) { + Log.e(TAG, "Fail to construct address", e); notifyErrorCallback(cb, ERROR_INVALID_IP_ADDRESS); return null; } @@ -734,6 +737,7 @@ public class KeepaliveTracker { packet = NattKeepalivePacketData.nattKeepalivePacket( srcAddress, srcPort, dstAddress, NATT_PORT); } catch (InvalidPacketException e) { + Log.e(TAG, "Fail to construct keepalive packet", e); notifyErrorCallback(cb, e.getError()); return null; } @@ -866,4 +870,31 @@ public class KeepaliveTracker { } pw.decreaseIndent(); } + + /** + * Dependencies class for testing. + */ + @VisibleForTesting + public static class Dependencies { + /** + * Read supported keepalive count for each transport type from overlay resource. This should + * be used to create a local variable store of resource customization, and set as the + * input for {@link getSupportedKeepalivesForNetworkCapabilities}. + * + * @param context The context to read resource from. + * @return An array of supported keepalive count for each transport type. + */ + @NonNull + public int[] getSupportedKeepalives(@NonNull Context context) { + return KeepaliveResourceUtil.getSupportedKeepalives(context); + } + + /** + * Create a new {@link ConnectivityResources}. + */ + @NonNull + public ConnectivityResources createConnectivityResources(@NonNull Context context) { + return new ConnectivityResources(context); + } + } } diff --git a/tests/unit/java/com/android/server/connectivity/AutomaticOnOffKeepaliveTrackerTest.java b/tests/unit/java/com/android/server/connectivity/AutomaticOnOffKeepaliveTrackerTest.java index 82326585fc..67adcc06e7 100644 --- a/tests/unit/java/com/android/server/connectivity/AutomaticOnOffKeepaliveTrackerTest.java +++ b/tests/unit/java/com/android/server/connectivity/AutomaticOnOffKeepaliveTrackerTest.java @@ -265,7 +265,7 @@ public class AutomaticOnOffKeepaliveTrackerTest { TestKeepaliveTracker(@NonNull final Context context, @NonNull final Handler handler, @NonNull final TcpKeepaliveController tcpController) { - super(context, handler, tcpController); + super(context, handler, tcpController, new Dependencies()); } public void setReturnedKeepaliveInfo(@NonNull final KeepaliveInfo ki) {