Update error handling in BpfNetMaps

Address review comments from aosp/2117045
Throw AssertionError if bpf map init get error
Update tests to avoid getting AssertionError

Bug: 217624062
Test: atest BpfNetMapsTest
Change-Id: I2194b825aa73b88d5334077d17acb9abbd367c69
This commit is contained in:
Motomu Utsumi
2022-06-27 09:24:32 +00:00
parent daced9e4b8
commit 305975ffb0
5 changed files with 47 additions and 52 deletions

View File

@@ -85,24 +85,13 @@ public final class BpfNetMapsTest {
private BpfNetMaps mBpfNetMaps;
@Mock INetd mNetd;
private static final TestBpfMap<U32, U32> sConfigurationMap =
new TestBpfMap<>(U32.class, U32.class);
private final BpfMap<U32, U32> mConfigurationMap = new TestBpfMap<>(U32.class, U32.class);
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
BpfNetMaps.setConfigurationMapForTest(mConfigurationMap);
mBpfNetMaps = new BpfNetMaps(mNetd);
BpfNetMaps.initialize(makeDependencies());
sConfigurationMap.clear();
}
private static BpfNetMaps.Dependencies makeDependencies() {
return new BpfNetMaps.Dependencies() {
@Override
public BpfMap<U32, U32> getConfigurationMap() {
return sConfigurationMap;
}
};
}
@Test
@@ -121,7 +110,7 @@ public final class BpfNetMapsTest {
for (final int chain: enableChains) {
match |= mBpfNetMaps.getMatchByFirewallChain(chain);
}
sConfigurationMap.updateEntry(UID_RULES_CONFIGURATION_KEY, new U32(match));
mConfigurationMap.updateEntry(UID_RULES_CONFIGURATION_KEY, new U32(match));
for (final int chain: FIREWALL_CHAINS) {
final String testCase = "EnabledChains: " + enableChains + " CheckedChain: " + chain;
@@ -187,17 +176,17 @@ public final class BpfNetMapsTest {
expectedMatch |= mBpfNetMaps.getMatchByFirewallChain(chain);
}
assertEquals(0, sConfigurationMap.getValue(UID_RULES_CONFIGURATION_KEY).val);
assertEquals(0, mConfigurationMap.getValue(UID_RULES_CONFIGURATION_KEY).val);
for (final int chain: testChains) {
mBpfNetMaps.setChildChain(chain, true /* enable */);
}
assertEquals(expectedMatch, sConfigurationMap.getValue(UID_RULES_CONFIGURATION_KEY).val);
assertEquals(expectedMatch, mConfigurationMap.getValue(UID_RULES_CONFIGURATION_KEY).val);
for (final int chain: testChains) {
mBpfNetMaps.setChildChain(chain, false /* enable */);
}
assertEquals(0, sConfigurationMap.getValue(UID_RULES_CONFIGURATION_KEY).val);
assertEquals(0, mConfigurationMap.getValue(UID_RULES_CONFIGURATION_KEY).val);
}
private void doTestSetChildChain(final int testChain) throws Exception {
@@ -207,7 +196,7 @@ public final class BpfNetMapsTest {
@Test
@IgnoreUpTo(Build.VERSION_CODES.S_V2)
public void testSetChildChain() throws Exception {
sConfigurationMap.updateEntry(UID_RULES_CONFIGURATION_KEY, new U32(0));
mConfigurationMap.updateEntry(UID_RULES_CONFIGURATION_KEY, new U32(0));
doTestSetChildChain(FIREWALL_CHAIN_DOZABLE);
doTestSetChildChain(FIREWALL_CHAIN_STANDBY);
doTestSetChildChain(FIREWALL_CHAIN_POWERSAVE);
@@ -221,7 +210,7 @@ public final class BpfNetMapsTest {
@Test
@IgnoreUpTo(Build.VERSION_CODES.S_V2)
public void testSetChildChainMultipleChain() throws Exception {
sConfigurationMap.updateEntry(UID_RULES_CONFIGURATION_KEY, new U32(0));
mConfigurationMap.updateEntry(UID_RULES_CONFIGURATION_KEY, new U32(0));
doTestSetChildChain(List.of(
FIREWALL_CHAIN_DOZABLE,
FIREWALL_CHAIN_STANDBY));

View File

@@ -44,6 +44,7 @@ import androidx.test.InstrumentationRegistry;
import androidx.test.filters.SmallTest;
import com.android.frameworks.tests.net.R;
import com.android.server.BpfNetMaps;
import com.android.testutils.DevSdkIgnoreRule;
import com.android.testutils.DevSdkIgnoreRunner;
@@ -74,6 +75,7 @@ public class NetworkStatsFactoryTest extends NetworkStatsBaseTest {
private File mTestProc;
private NetworkStatsFactory mFactory;
@Mock private Context mContext;
@Mock private BpfNetMaps mBpfNetMaps;
@Before
public void setUp() throws Exception {
@@ -84,7 +86,7 @@ public class NetworkStatsFactoryTest extends NetworkStatsBaseTest {
// applications. So in order to have a test support native library, the native code
// related to networkStatsFactory is compiled to a minimal native library and loaded here.
System.loadLibrary("networkstatsfactorytestjni");
mFactory = new NetworkStatsFactory(mContext, mTestProc, false);
mFactory = new NetworkStatsFactory(mContext, mTestProc, false, mBpfNetMaps);
mFactory.updateUnderlyingNetworkInfos(new UnderlyingNetworkInfo[0]);
}