Merge "Add NetIdManagerTest"
am: 4339cf3402
Change-Id: Ifbebb446601fcbbdf91fff48dc66ad173a6ecc7c
diff --git a/services/core/java/com/android/server/NetIdManager.java b/services/core/java/com/android/server/NetIdManager.java
index 11533be..097fb3a 100644
--- a/services/core/java/com/android/server/NetIdManager.java
+++ b/services/core/java/com/android/server/NetIdManager.java
@@ -20,6 +20,7 @@
import android.util.SparseBooleanArray;
import com.android.internal.annotations.GuardedBy;
+import com.android.internal.annotations.VisibleForTesting;
/**
* Class used to reserve and release net IDs.
@@ -38,14 +39,25 @@
@GuardedBy("mNetIdInUse")
private int mLastNetId = MIN_NET_ID - 1;
+ private final int mMaxNetId;
+
+ public NetIdManager() {
+ this(MAX_NET_ID);
+ }
+
+ @VisibleForTesting
+ NetIdManager(int maxNetId) {
+ mMaxNetId = maxNetId;
+ }
+
/**
* Get the first netId that follows the provided lastId and is available.
*/
- private static int getNextAvailableNetIdLocked(
+ private int getNextAvailableNetIdLocked(
int lastId, @NonNull SparseBooleanArray netIdInUse) {
int netId = lastId;
- for (int i = MIN_NET_ID; i <= MAX_NET_ID; i++) {
- netId = netId < MAX_NET_ID ? netId + 1 : MIN_NET_ID;
+ for (int i = MIN_NET_ID; i <= mMaxNetId; i++) {
+ netId = netId < mMaxNetId ? netId + 1 : MIN_NET_ID;
if (!netIdInUse.get(netId)) {
return netId;
}
diff --git a/tests/net/java/com/android/server/NetIdManagerTest.kt b/tests/net/java/com/android/server/NetIdManagerTest.kt
new file mode 100644
index 0000000..045f89f
--- /dev/null
+++ b/tests/net/java/com/android/server/NetIdManagerTest.kt
@@ -0,0 +1,53 @@
+/*
+ * Copyright (C) 2019 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.server
+
+import androidx.test.filters.SmallTest
+import androidx.test.runner.AndroidJUnit4
+import com.android.server.NetIdManager.MIN_NET_ID
+import com.android.testutils.ExceptionUtils.ThrowingRunnable
+import com.android.testutils.assertThrows
+import org.junit.Test
+import org.junit.runner.RunWith
+import kotlin.test.assertEquals
+
+@RunWith(AndroidJUnit4::class)
+@SmallTest
+class NetIdManagerTest {
+ @Test
+ fun testReserveReleaseNetId() {
+ val manager = NetIdManager(MIN_NET_ID + 4)
+ assertEquals(MIN_NET_ID, manager.reserveNetId())
+ assertEquals(MIN_NET_ID + 1, manager.reserveNetId())
+ assertEquals(MIN_NET_ID + 2, manager.reserveNetId())
+ assertEquals(MIN_NET_ID + 3, manager.reserveNetId())
+
+ manager.releaseNetId(MIN_NET_ID + 1)
+ manager.releaseNetId(MIN_NET_ID + 3)
+ // IDs only loop once there is no higher ID available
+ assertEquals(MIN_NET_ID + 4, manager.reserveNetId())
+ assertEquals(MIN_NET_ID + 1, manager.reserveNetId())
+ assertEquals(MIN_NET_ID + 3, manager.reserveNetId())
+ assertThrows(IllegalStateException::class.java, ThrowingRunnable { manager.reserveNetId() })
+ manager.releaseNetId(MIN_NET_ID + 5)
+ // Still no ID available: MIN_NET_ID + 5 was not reserved
+ assertThrows(IllegalStateException::class.java, ThrowingRunnable { manager.reserveNetId() })
+ manager.releaseNetId(MIN_NET_ID + 2)
+ // Throwing an exception still leaves the manager in a working state
+ assertEquals(MIN_NET_ID + 2, manager.reserveNetId())
+ }
+}
\ No newline at end of file