Refactor RoutingCoordinator
- This CL puts RoutingCoordinator into staticlibs to make it usable by
both Tethering and ConnectivityService on R.
- This CL also changes the return type of
ConnectivityInternalApi#getRoutingCoordinatorManager() to IBinder so
it has fewer dependencies.
Bug: 350699020
Test: CI
Change-Id: Iba096e940795cd33e61c5141721394ad39e5ce6d
diff --git a/staticlibs/tests/unit/Android.bp b/staticlibs/tests/unit/Android.bp
index 3186033..91f94b5 100644
--- a/staticlibs/tests/unit/Android.bp
+++ b/staticlibs/tests/unit/Android.bp
@@ -27,6 +27,7 @@
"net-utils-device-common-ip",
"net-utils-device-common-struct-base",
"net-utils-device-common-wear",
+ "net-utils-service-connectivity",
],
libs: [
"android.test.runner",
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
new file mode 100644
index 0000000..b04561c
--- /dev/null
+++ b/staticlibs/tests/unit/src/com/android/net/module/util/RoutingCoordinatorServiceTest.kt
@@ -0,0 +1,73 @@
+/*
+ * Copyright (C) 2023 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.net.module.util
+
+import android.net.INetd
+import android.os.Build
+import android.util.Log
+import com.android.testutils.DevSdkIgnoreRule.IgnoreUpTo
+import com.android.testutils.DevSdkIgnoreRunner
+import com.android.testutils.tryTest
+import java.util.concurrent.atomic.AtomicBoolean
+import kotlin.test.assertTrue
+import org.junit.Test
+import org.junit.runner.RunWith
+import org.mockito.ArgumentMatchers.any
+import org.mockito.Mockito.inOrder
+import org.mockito.Mockito.mock
+
+@RunWith(DevSdkIgnoreRunner::class)
+@IgnoreUpTo(Build.VERSION_CODES.TIRAMISU)
+class RoutingCoordinatorServiceTest {
+ val mNetd = mock(INetd::class.java)
+ val mService = RoutingCoordinatorService(mNetd)
+
+ @Test
+ fun testInterfaceForward() {
+ val inOrder = inOrder(mNetd)
+
+ mService.addInterfaceForward("from1", "to1")
+ inOrder.verify(mNetd).ipfwdEnableForwarding(any())
+ inOrder.verify(mNetd).tetherAddForward("from1", "to1")
+ inOrder.verify(mNetd).ipfwdAddInterfaceForward("from1", "to1")
+
+ mService.addInterfaceForward("from2", "to1")
+ inOrder.verify(mNetd).tetherAddForward("from2", "to1")
+ inOrder.verify(mNetd).ipfwdAddInterfaceForward("from2", "to1")
+
+ val hasFailed = AtomicBoolean(false)
+ val prevHandler = Log.setWtfHandler { tag, what, system ->
+ hasFailed.set(true)
+ }
+ tryTest {
+ mService.addInterfaceForward("from2", "to1")
+ assertTrue(hasFailed.get())
+ } cleanup {
+ Log.setWtfHandler(prevHandler)
+ }
+
+ mService.removeInterfaceForward("from1", "to1")
+ inOrder.verify(mNetd).ipfwdRemoveInterfaceForward("from1", "to1")
+ inOrder.verify(mNetd).tetherRemoveForward("from1", "to1")
+
+ mService.removeInterfaceForward("from2", "to1")
+ inOrder.verify(mNetd).ipfwdRemoveInterfaceForward("from2", "to1")
+ inOrder.verify(mNetd).tetherRemoveForward("from2", "to1")
+
+ inOrder.verify(mNetd).ipfwdDisableForwarding(any())
+ }
+}