Merge changes I3b7913c5,I9c1d6294

* changes:
  Improve error reporting in RateLimitTest
  Add null checks to RateLimitTest#tearDown improves error reporting
This commit is contained in:
Patrick Rohr
2022-03-01 07:36:09 +00:00
committed by Gerrit Code Review

View File

@@ -83,7 +83,8 @@ import java.util.stream.Collectors;
@RunWith(DevSdkIgnoreRunner.class) @RunWith(DevSdkIgnoreRunner.class)
@DevSdkIgnoreRule.IgnoreUpTo(SC_V2) @DevSdkIgnoreRule.IgnoreUpTo(SC_V2)
public class RateLimitTest { public class RateLimitTest {
private static final HashSet<String> sKernelConfig; // cannot be final as it gets initialized inside ensureKernelConfigLoaded().
private static HashSet<String> sKernelConfig;
private static final String TAG = "RateLimitTest"; private static final String TAG = "RateLimitTest";
private static final LinkAddress LOCAL_IP4_ADDR = new LinkAddress("10.0.0.1/8"); private static final LinkAddress LOCAL_IP4_ADDR = new LinkAddress("10.0.0.1/8");
@@ -108,18 +109,21 @@ public class RateLimitTest {
private Network mNetwork; private Network mNetwork;
private DatagramSocket mSocket; private DatagramSocket mSocket;
static { // Note: exceptions thrown in @BeforeClass or @ClassRule methods are not reported correctly.
// This function is called from setUp and loads the kernel config options the first time it is
// invoked. This ensures proper error reporting.
private static synchronized void ensureKernelConfigLoaded() {
if (sKernelConfig != null) return;
final String result = SystemUtil.runShellCommandOrThrow("gzip -cd /proc/config.gz"); final String result = SystemUtil.runShellCommandOrThrow("gzip -cd /proc/config.gz");
sKernelConfig = Arrays.stream(result.split("\\R")).collect( sKernelConfig = Arrays.stream(result.split("\\R")).collect(
Collectors.toCollection(HashSet::new)); Collectors.toCollection(HashSet::new));
// make sure that if for some reason /proc/config.gz returns an empty string, this test // make sure that if for some reason /proc/config.gz returns an empty string, this test
// does not silently fail. // does not silently fail.
assertNotEquals(0, result.length()); assertNotEquals("gzip -cd /proc/config.gz returned an empty string", 0, result.length());
} }
private static void assumeKernelSupport() { private static void assumeKernelSupport() {
// Note: assumptions that fail in @BeforeClass annotated methods are not handled correctly.
assumeTrue(sKernelConfig.contains("CONFIG_NET_CLS_MATCHALL=y")); assumeTrue(sKernelConfig.contains("CONFIG_NET_CLS_MATCHALL=y"));
assumeTrue(sKernelConfig.contains("CONFIG_NET_ACT_POLICE=y")); assumeTrue(sKernelConfig.contains("CONFIG_NET_ACT_POLICE=y"));
assumeTrue(sKernelConfig.contains("CONFIG_NET_ACT_BPF=y")); assumeTrue(sKernelConfig.contains("CONFIG_NET_ACT_BPF=y"));
@@ -127,6 +131,8 @@ public class RateLimitTest {
@Before @Before
public void setUp() throws IOException { public void setUp() throws IOException {
ensureKernelConfigLoaded();
mHandler = new Handler(Looper.getMainLooper()); mHandler = new Handler(Looper.getMainLooper());
runAsShell(MANAGE_TEST_NETWORKS, () -> { runAsShell(MANAGE_TEST_NETWORKS, () -> {
@@ -179,12 +185,14 @@ public class RateLimitTest {
@After @After
public void tearDown() throws IOException { public void tearDown() throws IOException {
// whatever happens, don't leave the device in rate limited state. if (mContext != null) {
ConnectivitySettingsManager.setIngressRateLimitInBytesPerSecond(mContext, -1); // whatever happens, don't leave the device in rate limited state.
mSocket.close(); ConnectivitySettingsManager.setIngressRateLimitInBytesPerSecond(mContext, -1);
mNetworkAgent.unregister(); }
mTunInterface.getFileDescriptor().close(); if (mSocket != null) mSocket.close();
mCm.unregisterNetworkCallback(mNetworkCallback); if (mNetworkAgent != null) mNetworkAgent.unregister();
if (mTunInterface != null) mTunInterface.getFileDescriptor().close();
if (mCm != null) mCm.unregisterNetworkCallback(mNetworkCallback);
} }
private void assertGreaterThan(final String msg, long lhs, long rhs) { private void assertGreaterThan(final String msg, long lhs, long rhs) {