Add a BpfMap#clear method. am: ffb0ccd04b am: 10be22076e

Original change: https://android-review.googlesource.com/c/platform/packages/modules/Connectivity/+/1604974

MUST ONLY BE SUBMITTED BY AUTOMERGER

Change-Id: I1e4d5e6005cdd006ddd814692714fb80a6c46936
This commit is contained in:
Lorenzo Colitti
2021-02-24 12:30:56 +00:00
committed by Automerger Merge Worker
2 changed files with 40 additions and 0 deletions

View File

@@ -226,6 +226,20 @@ public class BpfMap<K extends Struct, V extends Struct> implements AutoCloseable
closeMap(mMapFd);
}
/**
* Clears the map. The map may already be empty.
*
* @throws ErrnoException if the map is already closed, if an error occurred during iteration,
* or if a non-ENOENT error occurred when deleting a key.
*/
public void clear() throws ErrnoException {
K key = getFirstKey();
while (key != null) {
deleteEntry(key); // ignores ENOENT.
key = getFirstKey();
}
}
private static native int closeMap(int fd) throws ErrnoException;
private native int bpfFdGet(String path, int mode) throws ErrnoException, NullPointerException;

View File

@@ -311,6 +311,32 @@ public final class BpfMapTest {
assertNull(mTestMap.getFirstKey());
}
@Test
public void testClear() throws Exception {
// Clear an empty map.
assertTrue(mTestMap.isEmpty());
mTestMap.clear();
// Clear a map with some data in it.
final ArrayMap<TetherDownstream6Key, Tether6Value> resultMap =
new ArrayMap<>(mTestData);
for (int i = 0; i < resultMap.size(); i++) {
mTestMap.insertEntry(resultMap.keyAt(i), resultMap.valueAt(i));
}
assertFalse(mTestMap.isEmpty());
mTestMap.clear();
assertTrue(mTestMap.isEmpty());
// Clearing an already-closed map throws.
mTestMap.close();
try {
mTestMap.clear();
fail("clearing already-closed map should throw");
} catch (ErrnoException expected) {
assertEquals(OsConstants.EBADF, expected.errno);
}
}
@Test
public void testInsertOverflow() throws Exception {
final ArrayMap<TetherDownstream6Key, Tether6Value> testData =