Files
frameworks_base/tests/net/java/com/android/server/NetIdManagerTest.kt
Chalard Jean db2934f3cc Move utils from network stack to libs.
This package is using some common utilities from
a library that used to live in the network stack.
A better home for these utilities is frameworks/libs,
so this topic moves the files ther and also changes
the package of some utilities.

See aosp/1350222 and aosp/1350182 for a detailed
description of the specific files that moved.

Test: checkbuild
Change-Id: I76a9b7790f3997e3e6b3c2f75ba6308286457cde
2020-07-29 21:15:48 +09:00

53 lines
2.2 KiB
Kotlin

/*
* 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.assertThrows
import com.android.testutils.ExceptionUtils.ThrowingRunnable
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())
}
}