Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 2de4baf1 authored by Mohammad Samiul Islam's avatar Mohammad Samiul Islam
Browse files

Make ApexManagerTest use resources that are not prebuilt

Currently ApexManagerTest uses a prebuilt apex for testing. This
resource was built in a dev environment and targets R, which is not a
valid version on release builds.

This CL replaces the prebuilt resource with one that gets rebuilt
whenever the test is run.

Bug: 155242557
Test: atest ApexManagerTest
Change-Id: Id6e9e9f688b07da1ad80f44f7486d7266e3a21f3
parent 9c50a64e
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -99,6 +99,7 @@ android_test {
        ":PackageParserTestApp1",
        ":PackageParserTestApp2",
        ":PackageParserTestApp3",
        ":apex.test",
    ],
    resource_zips: [":FrameworksServicesTests_apks_as_resources"],
}
−311 KiB

File deleted.

+22 −20
Original line number Diff line number Diff line
@@ -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;
    }
}