From d502c991233b62f6fb12c96b6d79f1aef4cb9353 Mon Sep 17 00:00:00 2001 From: Jeff Vander Stoep Date: Fri, 28 Sep 2018 10:36:12 -0700 Subject: [PATCH] Refactor duplicate code into single function Address comments in aosp/763607 Bug: 116053204 Test: atest ProcNetTest Change-Id: Iec8b58b6499a7764b3757d4dd820e1f45a65e814 --- .../src/com/android/cts/net/ProcNetTest.java | 58 +++++++------------ 1 file changed, 22 insertions(+), 36 deletions(-) diff --git a/tests/cts/hostside/src/com/android/cts/net/ProcNetTest.java b/tests/cts/hostside/src/com/android/cts/net/ProcNetTest.java index 1335eb8011..19e61c62a0 100644 --- a/tests/cts/hostside/src/com/android/cts/net/ProcNetTest.java +++ b/tests/cts/hostside/src/com/android/cts/net/ProcNetTest.java @@ -22,16 +22,11 @@ import com.android.tradefed.testtype.DeviceTestCase; import com.android.tradefed.testtype.IBuildReceiver; import com.android.tradefed.testtype.IDeviceTest; -import java.io.File; -import java.io.FileInputStream; -import java.io.InputStreamReader; import java.lang.Integer; import java.lang.String; -import java.util.stream.Collectors; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.zip.GZIPInputStream; +import java.util.Arrays; +import java.util.List; +import java.util.ArrayList; /** * Host-side tests for values in /proc/net. @@ -62,6 +57,7 @@ public class ProcNetTest extends DeviceTestCase implements IBuildReceiver, IDevi private static final int IPV6_WIFI_ROUTER_SOLICITATIONS = -1; private ITestDevice mDevice; private IBuildInfo mBuild; + private String[] mSysctlDirs; /** * {@inheritDoc} @@ -83,8 +79,19 @@ public class ProcNetTest extends DeviceTestCase implements IBuildReceiver, IDevi @Override protected void setUp() throws Exception { super.setUp(); + mSysctlDirs = getSysctlDirs(); } + private String[] getSysctlDirs() throws Exception { + String interfaceDirs[] = mDevice.executeAdbCommand("shell", "ls", "-1", + IPV6_SYSCTL_DIR).split("\n"); + List interfaceDirsList = new ArrayList(Arrays.asList(interfaceDirs)); + interfaceDirsList.remove("all"); + interfaceDirsList.remove("lo"); + return interfaceDirsList.toArray(new String[interfaceDirsList.size()]); + } + + protected void assertLess(String sysctl, int a, int b) { assertTrue("value of " + sysctl + ": expected < " + b + " but was: " + a, a < b); } @@ -113,15 +120,7 @@ public class ProcNetTest extends DeviceTestCase implements IBuildReceiver, IDevi /** * Checks that the sysctls for multinetwork kernel features are present and - * enabled. The necessary kernel commits are: - * - * Mainline Linux: - * e110861 net: add a sysctl to reflect the fwmark on replies - * 1b3c61d net: Use fwmark reflection in PMTU discovery. - * 84f39b0 net: support marking accepting TCP sockets - * - * Common Android tree (e.g., 3.10): - * a03f539 net: ipv6: autoconf routes into per-device tables + * enabled. */ public void testProcSysctls() throws Exception { for (String sysctl : GLOBAL_SYSCTLS) { @@ -129,26 +128,18 @@ public class ProcNetTest extends DeviceTestCase implements IBuildReceiver, IDevi assertEquals(sysctl, 1, value); } - String interfaceDirs[] = mDevice.executeAdbCommand("shell", "ls", "-1", - IPV6_SYSCTL_DIR).split("\n"); - for (String interfaceDir : interfaceDirs) { - if (interfaceDir.equals("all") || interfaceDir.equals("lo")) { - continue; - } + for (String interfaceDir : mSysctlDirs) { String path = IPV6_SYSCTL_DIR + "/" + interfaceDir + "/" + AUTOCONF_SYSCTL; int value = readIntFromPath(path); assertLess(path, value, 0); } } - /** Verify that accept_ra_rt_info_{min,max}_plen exists and is set to the expected value */ + /** + * Verify that accept_ra_rt_info_{min,max}_plen exists and is set to the expected value + */ public void testAcceptRaRtInfoMinMaxPlen() throws Exception { - String interfaceDirs[] = mDevice.executeAdbCommand("shell", "ls", "-1", - IPV6_SYSCTL_DIR).split("\n"); - for (String interfaceDir : interfaceDirs) { - if (interfaceDir.equals("all") || interfaceDir.equals("lo")) { - continue; - } + for (String interfaceDir : mSysctlDirs) { String path = IPV6_SYSCTL_DIR + "/" + interfaceDir + "/" + "accept_ra_rt_info_min_plen"; int value = readIntFromPath(path); assertEquals(path, value, ACCEPT_RA_RT_INFO_MIN_PLEN_VALUE); @@ -163,12 +154,7 @@ public class ProcNetTest extends DeviceTestCase implements IBuildReceiver, IDevi * and verify that router_solicitation_max_interval exists and is in an acceptable interval. */ public void testRouterSolicitations() throws Exception { - String interfaceDirs[] = mDevice.executeAdbCommand("shell", "ls", "-1", - IPV6_SYSCTL_DIR).split("\n"); - for (String interfaceDir : interfaceDirs) { - if (interfaceDir.equals("all") || interfaceDir.equals("lo")) { - continue; - } + for (String interfaceDir : mSysctlDirs) { String path = IPV6_SYSCTL_DIR + "/" + interfaceDir + "/" + "router_solicitations"; int value = readIntFromPath(path); assertEquals(IPV6_WIFI_ROUTER_SOLICITATIONS, value);