From db6336c0b8d2a69f143978436833c3ff477408ce Mon Sep 17 00:00:00 2001 From: jovanak Date: Tue, 14 May 2019 17:14:13 -0700 Subject: [PATCH] Fixing NetworkWatchlistTest#testGetWatchlistConfigHash for secondary users. Avoids relying on external storage and using the hardcoded /sdcard/ path. Unrooted adb does not have access to non-user 0 sdcard paths, and so the "cp" command fails when the test runs in user 10. Fixes: 121400865 Test: cts-tradefed run cts-dev -m CtsNetTestCases -t android.net.cts.NetworkWatchlistTest#testGetWatchlistConfigHash in user 10. Change-Id: Idc667072bcfbbe159416fa7c7b6b19045b52041d --- .../android/net/cts/NetworkWatchlistTest.java | 45 +++++++++++++------ 1 file changed, 32 insertions(+), 13 deletions(-) diff --git a/tests/cts/net/src/android/net/cts/NetworkWatchlistTest.java b/tests/cts/net/src/android/net/cts/NetworkWatchlistTest.java index e0c03a1e95..8ad7820be6 100644 --- a/tests/cts/net/src/android/net/cts/NetworkWatchlistTest.java +++ b/tests/cts/net/src/android/net/cts/NetworkWatchlistTest.java @@ -24,6 +24,7 @@ import static org.junit.Assume.assumeTrue; import android.content.Context; import android.net.ConnectivityManager; import android.os.FileUtils; +import android.os.ParcelFileDescriptor; import androidx.test.InstrumentationRegistry; import androidx.test.filters.SmallTest; @@ -38,7 +39,7 @@ import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import java.io.File; +import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.Formatter; @@ -50,8 +51,6 @@ public class NetworkWatchlistTest { private static final String TEST_WATCHLIST_XML = "assets/network_watchlist_config_for_test.xml"; private static final String TEST_EMPTY_WATCHLIST_XML = "assets/network_watchlist_config_empty_for_test.xml"; - private static final String SDCARD_CONFIG_PATH = - "/sdcard/network_watchlist_config_for_test.xml"; private static final String TMP_CONFIG_PATH = "/data/local/tmp/network_watchlist_config_for_test.xml"; // Generated from sha256sum network_watchlist_config_for_test.xml @@ -83,8 +82,7 @@ public class NetworkWatchlistTest { } } - private void cleanup() throws Exception { - runCommand("rm " + SDCARD_CONFIG_PATH); + private void cleanup() throws IOException { runCommand("rm " + TMP_CONFIG_PATH); } @@ -116,22 +114,43 @@ public class NetworkWatchlistTest { } private void saveResourceToFile(String res, String filePath) throws IOException { - InputStream in = getClass().getClassLoader().getResourceAsStream(res); - FileUtils.copyToFileOrThrow(in, new File(filePath)); + // App can't access /data/local/tmp directly, so we pipe resource to file through stdin. + ParcelFileDescriptor stdin = pipeFromStdin(filePath); + pipeResourceToFileDescriptor(res, stdin); + } + + /* Pipe stdin to a file in filePath. Returns PFD for stdin. */ + private ParcelFileDescriptor pipeFromStdin(String filePath) { + // Not all devices have symlink for /dev/stdin, so use /proc/self/fd/0 directly. + // /dev/stdin maps to /proc/self/fd/0. + return runRwCommand("cp /proc/self/fd/0 " + filePath)[1]; + } + + private void pipeResourceToFileDescriptor(String res, ParcelFileDescriptor pfd) + throws IOException { + InputStream resStream = getClass().getClassLoader().getResourceAsStream(res); + FileOutputStream fdStream = new ParcelFileDescriptor.AutoCloseOutputStream(pfd); + + FileUtils.copy(resStream, fdStream); + + try { + fdStream.close(); + } catch (IOException e) { + } } private static String runCommand(String command) throws IOException { return SystemUtil.runShellCommand(InstrumentationRegistry.getInstrumentation(), command); } + private static ParcelFileDescriptor[] runRwCommand(String command) { + return InstrumentationRegistry.getInstrumentation() + .getUiAutomation().executeShellCommandRw(command); + } + private void setWatchlistConfig(String watchlistConfigFile) throws Exception { cleanup(); - // Save test watchlist config to sdcard as app can't access /data/local/tmp - saveResourceToFile(watchlistConfigFile, SDCARD_CONFIG_PATH); - // Copy test watchlist config from sdcard to /data/local/tmp as system service - // can't access /sdcard - runCommand("cp " + SDCARD_CONFIG_PATH + " " + TMP_CONFIG_PATH); - // Set test watchlist config to system + saveResourceToFile(watchlistConfigFile, TMP_CONFIG_PATH); final String cmdResult = runCommand( "cmd network_watchlist set-test-config " + TMP_CONFIG_PATH).trim(); assertThat(cmdResult).contains("Success");