diff --git a/services/tests/servicestests/Android.bp b/services/tests/servicestests/Android.bp index b457856e86308..40a17061c7f1b 100644 --- a/services/tests/servicestests/Android.bp +++ b/services/tests/servicestests/Android.bp @@ -99,6 +99,7 @@ android_test { ":PackageParserTestApp1", ":PackageParserTestApp2", ":PackageParserTestApp3", + ":apex.test", ], resource_zips: [":FrameworksServicesTests_apks_as_resources"], } diff --git a/services/tests/servicestests/res/raw/apex_test.apex b/services/tests/servicestests/res/raw/apex_test.apex deleted file mode 100644 index 19b1c5e2e1c67..0000000000000 Binary files a/services/tests/servicestests/res/raw/apex_test.apex and /dev/null differ diff --git a/services/tests/servicestests/src/com/android/server/pm/ApexManagerTest.java b/services/tests/servicestests/src/com/android/server/pm/ApexManagerTest.java index 3718c5c5a5e16..0fbd1b64c379b 100644 --- a/services/tests/servicestests/src/com/android/server/pm/ApexManagerTest.java +++ b/services/tests/servicestests/src/com/android/server/pm/ApexManagerTest.java @@ -36,7 +36,6 @@ import android.apex.ApexSessionParams; import android.apex.IApexService; import android.content.Context; import android.content.pm.PackageInfo; -import android.os.FileUtils; import android.os.RemoteException; import android.platform.test.annotations.Presubmit; @@ -44,7 +43,6 @@ import androidx.test.filters.SmallTest; import androidx.test.platform.app.InstrumentationRegistry; import androidx.test.runner.AndroidJUnit4; -import com.android.frameworks.servicestests.R; import com.android.server.pm.parsing.PackageParser2; import com.android.server.pm.parsing.TestPackageParser2; @@ -52,11 +50,12 @@ import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import java.io.BufferedOutputStream; import java.io.File; -import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; +import java.io.OutputStream; @SmallTest @Presubmit @@ -64,6 +63,7 @@ import java.io.InputStream; public class ApexManagerTest { private static final String TEST_APEX_PKG = "com.android.apex.test"; + private static final String TEST_APEX_FILE_NAME = "apex.test.apex"; private static final int TEST_SESSION_ID = 99999999; private static final int[] TEST_CHILD_SESSION_ID = {8888, 7777}; private ApexManager mApexManager; @@ -274,7 +274,7 @@ public class ApexManagerTest { } private ApexInfo[] createApexInfo(boolean isActive, boolean isFactory) { - File apexFile = copyRawResourceToFile(TEST_APEX_PKG, R.raw.apex_test); + File apexFile = extractResource(TEST_APEX_PKG, TEST_APEX_FILE_NAME); ApexInfo apexInfo = new ApexInfo(); apexInfo.isActive = isActive; apexInfo.isFactory = isFactory; @@ -308,27 +308,29 @@ public class ApexManagerTest { return params; } - /** - * Copies a specified {@code resourceId} to a temp file. Returns a non-null file if the copy - * succeeded - */ - File copyRawResourceToFile(String baseName, int resourceId) { - File outFile; + // Extracts the binary data from a resource and writes it to a temp file + private static File extractResource(String baseName, String fullResourceName) { + File file; try { - outFile = File.createTempFile(baseName, ".apex"); + file = File.createTempFile(baseName, ".apex"); } catch (IOException e) { throw new AssertionError("CreateTempFile IOException" + e); } - - try (InputStream is = mContext.getResources().openRawResource(resourceId); - FileOutputStream os = new FileOutputStream(outFile)) { - assertThat(FileUtils.copy(is, os)).isGreaterThan(0L); - } catch (FileNotFoundException e) { - throw new AssertionError("File not found exception " + e); + try ( + InputStream in = ApexManager.class.getClassLoader() + .getResourceAsStream(fullResourceName); + OutputStream out = new BufferedOutputStream(new FileOutputStream(file))) { + if (in == null) { + throw new IllegalArgumentException("Resource not found: " + fullResourceName); + } + byte[] buf = new byte[65536]; + int chunkSize; + while ((chunkSize = in.read(buf)) != -1) { + out.write(buf, 0, chunkSize); + } + return file; } catch (IOException e) { - throw new AssertionError("IOException" + e); + throw new AssertionError("Exception while converting stream to file" + e); } - - return outFile; } }