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

Commit bdb71e01 authored by Jeff Sharkey's avatar Jeff Sharkey
Browse files

Fix StorageManagerIntegrationTest.

OBB files need to be stored on external storage; we no longer support
globally readable files from private app data.

Test: bit FrameworksCoreTests:android.os.storage.StorageManagerIntegrationTest
Bug: 64039580
Change-Id: I491006b691c76f80466da59b1bd728afaec7ed94
parent 1ec536fa
Loading
Loading
Loading
Loading
+13 −41
Original line number Diff line number Diff line
@@ -18,24 +18,21 @@ package android.os.storage;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.Resources.NotFoundException;
import android.os.Environment;
import android.os.SystemClock;
import android.test.InstrumentationTestCase;
import android.util.Log;
import android.os.Environment;
import android.os.FileUtils;
import android.os.storage.OnObbStateChangeListener;
import android.os.storage.StorageManager;

import libcore.io.Streams;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.InputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.StringReader;

public class StorageManagerBaseTest extends InstrumentationTestCase {
@@ -218,29 +215,6 @@ public class StorageManagerBaseTest extends InstrumentationTestCase {

    }

    /**
     * Helper to copy a raw resource file to an actual specified file
     *
     * @param rawResId The raw resource ID of the OBB resource file
     * @param outFile A File representing the file we want to copy the OBB to
     * @throws NotFoundException If the resource file could not be found
     */
    private void copyRawToFile(int rawResId, File outFile) throws NotFoundException {
        Resources res = mContext.getResources();
        InputStream is = null;
        try {
            is = res.openRawResource(rawResId);
        } catch (NotFoundException e) {
            Log.i(LOG_TAG, "Failed to load resource with id: " + rawResId);
            throw e;
        }
        FileUtils.setPermissions(outFile.getPath(), FileUtils.S_IRWXU | FileUtils.S_IRWXG
                | FileUtils.S_IRWXO, -1, -1);
        assertTrue(FileUtils.copyToFile(is, outFile));
        FileUtils.setPermissions(outFile.getPath(), FileUtils.S_IRWXU | FileUtils.S_IRWXG
                | FileUtils.S_IRWXO, -1, -1);
    }

    /**
     * Creates an OBB file (with the given name), into the app's standard files directory
     *
@@ -248,17 +222,15 @@ public class StorageManagerBaseTest extends InstrumentationTestCase {
     * @param rawResId The raw resource ID of the OBB file in the package
     * @return A {@link File} representing the file to write to
     */
    protected File createObbFile(String name, int rawResId) {
        File outFile = null;
        try {
            final File filesDir = mContext.getFilesDir();
            outFile = new File(filesDir, name);
            copyRawToFile(rawResId, outFile);
        } catch (NotFoundException e) {
            if (outFile != null) {
    protected File createObbFile(String name, int rawResId) throws IOException, Resources.NotFoundException {
        final File outFile = new File(mContext.getObbDir(), name);
        outFile.delete();

        try (InputStream in = mContext.getResources().openRawResource(rawResId);
                OutputStream out = new FileOutputStream(outFile)) {
            Streams.copy(in, out);
        }
        }

        return outFile;
    }

+29 −65
Original line number Diff line number Diff line
@@ -16,61 +16,27 @@

package android.os.storage;

import android.content.Context;
import android.os.Environment;
import android.os.ProxyFileDescriptorCallback;
import android.os.ParcelFileDescriptor;
import android.os.ProxyFileDescriptorCallback;
import android.system.ErrnoException;
import android.system.Os;
import android.test.InstrumentationTestCase;
import android.test.suitebuilder.annotation.LargeTest;
import android.test.suitebuilder.annotation.Suppress;
import android.util.Log;

import com.android.frameworks.coretests.R;
import com.android.internal.os.FuseAppLoop;
import java.io.DataInputStream;
import java.io.IOException;
import java.util.concurrent.ThreadFactory;
import java.io.File;
import java.io.FileInputStream;

import junit.framework.AssertionFailedError;
import java.io.File;
import java.util.concurrent.ThreadFactory;

public class StorageManagerIntegrationTest extends StorageManagerBaseTest {

    private static String LOG_TAG = "StorageManagerBaseTest.StorageManagerIntegrationTest";
    protected File mFile = null;

    /**
     * {@inheritDoc}
     */
    @Override
    public void setUp() throws Exception {
        super.setUp();
        mContext = getInstrumentation().getContext();
        mFile = null;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void tearDown() throws Exception {
        if (mFile != null) {
            mFile.delete();
            mFile = null;
        }
        super.tearDown();
    }
    private static String LOG_TAG = "StorageManagerIntegrationTest";

    /**
     * Tests mounting a single OBB file and verifies its contents.
     */
    @LargeTest
    public void testMountSingleObb() {
        mFile = createObbFile(OBB_FILE_1, R.raw.obb_file1);
        String filePath = mFile.getAbsolutePath();
    public void testMountSingleObb() throws Exception {
        final File file = createObbFile(OBB_FILE_1, R.raw.obb_file1);
        String filePath = file.getAbsolutePath();
        mountObb(filePath);
        verifyObb1Contents(filePath);
        unmountObb(filePath, DONT_FORCE);
@@ -80,7 +46,7 @@ public class StorageManagerIntegrationTest extends StorageManagerBaseTest {
     * Tests mounting several OBB files and verifies its contents.
     */
    @LargeTest
    public void testMountMultipleObb() {
    public void testMountMultipleObb() throws Exception {
        File file1 = null;
        File file2 = null;
        File file3 = null;
@@ -120,9 +86,9 @@ public class StorageManagerIntegrationTest extends StorageManagerBaseTest {
     * Tests mounting a single encrypted OBB file and verifies its contents.
     */
    @LargeTest
    public void testMountSingleEncryptedObb() {
        mFile = createObbFile(OBB_FILE_3_ENCRYPTED, R.raw.obb_enc_file100_orig3);
        String filePath = mFile.getAbsolutePath();
    public void testMountSingleEncryptedObb() throws Exception {
        final File file = createObbFile(OBB_FILE_3_ENCRYPTED, R.raw.obb_enc_file100_orig3);
        String filePath = file.getAbsolutePath();
        mountObb(filePath, OBB_FILE_3_PASSWORD, OnObbStateChangeListener.MOUNTED);
        verifyObb3Contents(filePath);
        unmountObb(filePath, DONT_FORCE);
@@ -132,19 +98,17 @@ public class StorageManagerIntegrationTest extends StorageManagerBaseTest {
     * Tests mounting a single encrypted OBB file using an invalid password.
     */
    @LargeTest
    @Suppress  // Failing.
    public void testMountSingleEncryptedObbInvalidPassword() {
        mFile = createObbFile("bad password@$%#@^*(!&)", R.raw.obb_enc_file100_orig3);
        String filePath = mFile.getAbsolutePath();
        mountObb(filePath, OBB_FILE_3_PASSWORD, OnObbStateChangeListener.ERROR_COULD_NOT_MOUNT);
        unmountObb(filePath, DONT_FORCE);
    public void testMountSingleEncryptedObbInvalidPassword() throws Exception {
        final File file = createObbFile("bad password@$%#@^*(!&)", R.raw.obb_enc_file100_orig3);
        String filePath = file.getAbsolutePath();
        mountObb(filePath, OBB_FILE_1_PASSWORD, OnObbStateChangeListener.ERROR_COULD_NOT_MOUNT);
    }

    /**
     * Tests simultaneously mounting 2 encrypted OBBs with different keys and verifies contents.
     */
    @LargeTest
    public void testMountTwoEncryptedObb() {
    public void testMountTwoEncryptedObb() throws Exception {
        File file3 = null;
        File file1 = null;
        try {
@@ -174,9 +138,9 @@ public class StorageManagerIntegrationTest extends StorageManagerBaseTest {
     * Tests that we can not force unmount when a file is currently open on the OBB.
     */
    @LargeTest
    public void testUnmount_DontForce() {
        mFile = createObbFile(OBB_FILE_1, R.raw.obb_file1);
        String obbFilePath = mFile.getAbsolutePath();
    public void testUnmount_DontForce() throws Exception {
        final File file = createObbFile(OBB_FILE_1, R.raw.obb_file1);
        String obbFilePath = file.getAbsolutePath();

        MountingObbThread mountingThread = new MountingObbThread(obbFilePath,
                OBB_FILE_1_CONTENTS_1);
@@ -218,9 +182,9 @@ public class StorageManagerIntegrationTest extends StorageManagerBaseTest {
     * Tests mounting a single OBB that isn't signed.
     */
    @LargeTest
    public void testMountUnsignedObb() {
        mFile = createObbFile(OBB_FILE_2_UNSIGNED, R.raw.obb_file2_nosign);
        String filePath = mFile.getAbsolutePath();
    public void testMountUnsignedObb() throws Exception {
        final File file = createObbFile(OBB_FILE_2_UNSIGNED, R.raw.obb_file2_nosign);
        String filePath = file.getAbsolutePath();
        mountObb(filePath, OBB_FILE_2_UNSIGNED, OnObbStateChangeListener.ERROR_INTERNAL);
    }

@@ -228,9 +192,9 @@ public class StorageManagerIntegrationTest extends StorageManagerBaseTest {
     * Tests mounting a single OBB that is signed with a different package.
     */
    @LargeTest
    public void testMountBadPackageNameObb() {
        mFile = createObbFile(OBB_FILE_3_BAD_PACKAGENAME, R.raw.obb_file3_bad_packagename);
        String filePath = mFile.getAbsolutePath();
    public void testMountBadPackageNameObb() throws Exception {
        final File file = createObbFile(OBB_FILE_3_BAD_PACKAGENAME, R.raw.obb_file3_bad_packagename);
        String filePath = file.getAbsolutePath();
        mountObb(filePath, OBB_FILE_3_BAD_PACKAGENAME,
                OnObbStateChangeListener.ERROR_PERMISSION_DENIED);
    }
@@ -239,9 +203,9 @@ public class StorageManagerIntegrationTest extends StorageManagerBaseTest {
     * Tests remounting a single OBB that has already been mounted.
     */
    @LargeTest
    public void testRemountObb() {
        mFile = createObbFile(OBB_FILE_1, R.raw.obb_file1);
        String filePath = mFile.getAbsolutePath();
    public void testRemountObb() throws Exception {
        final File file = createObbFile(OBB_FILE_1, R.raw.obb_file1);
        String filePath = file.getAbsolutePath();
        mountObb(filePath);
        verifyObb1Contents(filePath);
        mountObb(filePath, null, OnObbStateChangeListener.ERROR_ALREADY_MOUNTED);