Ensure SpiUnavailableException was thrown because of duplicate SPI

SpiUnavailableException.getSpi() is public API so it should be tested.

Test: CtsNetTestCases:IpSecManagerTest
Change-Id: I68856b80b0ec819b0603dfe0a06ff813be3c5dda
This commit is contained in:
Aaron Huang
2022-03-09 14:11:29 +08:00
parent e6953d54a5
commit 3c6cc0c4e1

View File

@@ -52,6 +52,7 @@ import static android.system.OsConstants.IPPROTO_UDP;
import static com.android.compatibility.common.util.PropertyUtil.getFirstApiLevel; import static com.android.compatibility.common.util.PropertyUtil.getFirstApiLevel;
import static com.android.compatibility.common.util.PropertyUtil.getVendorApiLevel; import static com.android.compatibility.common.util.PropertyUtil.getVendorApiLevel;
import static com.android.testutils.MiscAsserts.assertThrows;
import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
@@ -129,12 +130,11 @@ public class IpSecManagerTest extends IpSecBaseTest {
assertTrue("Failed to allocate specified SPI, " + DROID_SPI, assertTrue("Failed to allocate specified SPI, " + DROID_SPI,
droidSpi.getSpi() == DROID_SPI); droidSpi.getSpi() == DROID_SPI);
try { IpSecManager.SpiUnavailableException expectedException =
mISM.allocateSecurityParameterIndex(addr, DROID_SPI); assertThrows("Duplicate SPI was allowed to be created",
fail("Duplicate SPI was allowed to be created"); IpSecManager.SpiUnavailableException.class,
} catch (IpSecManager.SpiUnavailableException expected) { () -> mISM.allocateSecurityParameterIndex(addr, DROID_SPI));
// This is a success case because we expect a dupe SPI to throw assertEquals(expectedException.getSpi(), droidSpi.getSpi());
}
randomSpi.close(); randomSpi.close();
droidSpi.close(); droidSpi.close();