Allow creating AssetFileDescriptors for MemoryFiles.

This allows content providers to use in-memory data to implement
ContentProvider.openAssetFile(), instead of just normal files
and sockets as before.

To test cross-process use of AssetFileDescriptors for MemoryFiles,
a test content provider and a client for it are added to
AndroidTests.

Fixes http://b/issue?id=1871731
diff --git a/core/java/android/content/res/AssetFileDescriptor.java b/core/java/android/content/res/AssetFileDescriptor.java
index 231e3e2..a37e4e8 100644
--- a/core/java/android/content/res/AssetFileDescriptor.java
+++ b/core/java/android/content/res/AssetFileDescriptor.java
@@ -16,6 +16,7 @@
 
 package android.content.res;
 
+import android.os.MemoryFile;
 import android.os.Parcel;
 import android.os.ParcelFileDescriptor;
 import android.os.Parcelable;
@@ -24,6 +25,8 @@
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.IOException;
+import java.io.InputStream;
+import java.nio.channels.FileChannel;
 
 /**
  * File descriptor of an entry in the AssetManager.  This provides your own
@@ -124,6 +127,13 @@
     }
     
     /**
+     * Checks whether this file descriptor is for a memory file.
+     */
+    private boolean isMemoryFile() throws IOException {
+        return MemoryFile.isMemoryFile(mFd.getFileDescriptor());
+    }
+
+    /**
      * Create and return a new auto-close input stream for this asset.  This
      * will either return a full asset {@link AutoCloseInputStream}, or
      * an underlying {@link ParcelFileDescriptor.AutoCloseInputStream
@@ -132,6 +142,12 @@
      * should only call this once for a particular asset.
      */
     public FileInputStream createInputStream() throws IOException {
+        if (isMemoryFile()) {
+            if (mLength > Integer.MAX_VALUE) {
+                throw new IOException("File length too large for a memory file: " + mLength);
+            }
+            return new AutoCloseMemoryFileInputStream(mFd, (int)mLength);
+        }
         if (mLength < 0) {
             return new ParcelFileDescriptor.AutoCloseInputStream(mFd);
         }
@@ -262,6 +278,66 @@
     }
     
     /**
+     * An input stream that reads from a MemoryFile and closes it when the stream is closed.
+     * This extends FileInputStream just because {@link #createInputStream} returns
+     * a FileInputStream. All the FileInputStream methods are
+     * overridden to use the MemoryFile instead.
+     */
+    private static class AutoCloseMemoryFileInputStream extends FileInputStream {
+        private ParcelFileDescriptor mParcelFd;
+        private MemoryFile mFile;
+        private InputStream mStream;
+
+        public AutoCloseMemoryFileInputStream(ParcelFileDescriptor fd, int length)
+                throws IOException {
+            super(fd.getFileDescriptor());
+            mParcelFd = fd;
+            mFile = new MemoryFile(fd.getFileDescriptor(), length, "r");
+            mStream = mFile.getInputStream();
+        }
+
+        @Override
+        public int available() throws IOException {
+            return mStream.available();
+        }
+
+        @Override
+        public void close() throws IOException {
+            mParcelFd.close();  // must close ParcelFileDescriptor, not just the file descriptor,
+                                // since it could be a subclass of ParcelFileDescriptor.
+                                // E.g. ContentResolver.ParcelFileDescriptorInner.close() releases
+                                // a content provider
+            mFile.close();      // to unmap the memory file from the address space.
+            mStream.close();    // doesn't actually do anything
+        }
+
+        @Override
+        public FileChannel getChannel() {
+            return null;
+        }
+
+        @Override
+        public int read() throws IOException {
+            return mStream.read();
+        }
+
+        @Override
+        public int read(byte[] buffer, int offset, int count) throws IOException {
+            return mStream.read(buffer, offset, count);
+        }
+
+        @Override
+        public int read(byte[] buffer) throws IOException {
+            return mStream.read(buffer);
+        }
+
+        @Override
+        public long skip(long count) throws IOException {
+            return mStream.skip(count);
+        }
+    }
+
+    /**
      * An OutputStream you can create on a ParcelFileDescriptor, which will
      * take care of calling {@link ParcelFileDescriptor#close
      * ParcelFileDescritor.close()} for you when the stream is closed.
@@ -345,4 +421,16 @@
             return new AssetFileDescriptor[size];
         }
     };
+
+    /**
+     * Creates an AssetFileDescriptor from a memory file.
+     *
+     * @hide
+     */
+    public static AssetFileDescriptor fromMemoryFile(MemoryFile memoryFile)
+            throws IOException {
+        ParcelFileDescriptor fd = memoryFile.getParcelFileDescriptor();
+        return new AssetFileDescriptor(fd, 0, memoryFile.length());
+    }
+
 }
diff --git a/core/java/android/os/MemoryFile.java b/core/java/android/os/MemoryFile.java
index 65e83c7..7e4cf8a 100644
--- a/core/java/android/os/MemoryFile.java
+++ b/core/java/android/os/MemoryFile.java
@@ -37,9 +37,14 @@
 {
     private static String TAG = "MemoryFile";
 
+    // mmap(2) protection flags from <sys/mman.h>
+    private static final int PROT_READ = 0x1;
+    private static final int PROT_WRITE = 0x2;
+
     private static native FileDescriptor native_open(String name, int length) throws IOException;
     // returns memory address for ashmem region
-    private static native int native_mmap(FileDescriptor fd, int length) throws IOException;
+    private static native int native_mmap(FileDescriptor fd, int length, int mode)
+            throws IOException;
     private static native void native_munmap(int addr, int length) throws IOException;
     private static native void native_close(FileDescriptor fd);
     private static native int native_read(FileDescriptor fd, int address, byte[] buffer,
@@ -47,14 +52,16 @@
     private static native void native_write(FileDescriptor fd, int address, byte[] buffer,
             int srcOffset, int destOffset, int count, boolean isUnpinned) throws IOException;
     private static native void native_pin(FileDescriptor fd, boolean pin) throws IOException;
+    private static native boolean native_is_ashmem_region(FileDescriptor fd) throws IOException;
 
     private FileDescriptor mFD;        // ashmem file descriptor
     private int mAddress;   // address of ashmem memory
     private int mLength;    // total length of our ashmem region
     private boolean mAllowPurging = false;  // true if our ashmem region is unpinned
+    private final boolean mOwnsRegion;  // false if this is a ref to an existing ashmem region
 
     /**
-     * MemoryFile constructor.
+     * Allocates a new ashmem region. The region is initially not purgable.
      *
      * @param name optional name for the file (can be null).
      * @param length of the memory file in bytes.
@@ -63,11 +70,43 @@
     public MemoryFile(String name, int length) throws IOException {
         mLength = length;
         mFD = native_open(name, length);
-        mAddress = native_mmap(mFD, length);
+        mAddress = native_mmap(mFD, length, PROT_READ | PROT_WRITE);
+        mOwnsRegion = true;
     }
 
     /**
-     * Closes and releases all resources for the memory file.
+     * Creates a reference to an existing memory file. Changes to the original file
+     * will be available through this reference.
+     * Calls to {@link #allowPurging(boolean)} on the returned MemoryFile will fail.
+     *
+     * @param fd File descriptor for an existing memory file, as returned by
+     *        {@link #getFileDescriptor()}. This file descriptor will be closed
+     *        by {@link #close()}.
+     * @param length Length of the memory file in bytes.
+     * @param mode File mode. Currently only "r" for read-only access is supported.
+     * @throws NullPointerException if <code>fd</code> is null.
+     * @throws IOException If <code>fd</code> does not refer to an existing memory file,
+     *         or if the file mode of the existing memory file is more restrictive
+     *         than <code>mode</code>.
+     *
+     * @hide
+     */
+    public MemoryFile(FileDescriptor fd, int length, String mode) throws IOException {
+        if (fd == null) {
+            throw new NullPointerException("File descriptor is null.");
+        }
+        if (!isMemoryFile(fd)) {
+            throw new IllegalArgumentException("Not a memory file.");
+        }
+        mLength = length;
+        mFD = fd;
+        mAddress = native_mmap(mFD, length, modeToProt(mode));
+        mOwnsRegion = false;
+    }
+
+    /**
+     * Closes the memory file. If there are no other open references to the memory
+     * file, it will be deleted.
      */
     public void close() {
         deactivate();
@@ -76,7 +115,14 @@
         }
     }
 
-    private void deactivate() {
+    /**
+     * Unmaps the memory file from the process's memory space, but does not close it.
+     * After this method has been called, read and write operations through this object
+     * will fail, but {@link #getFileDescriptor()} will still return a valid file descriptor.
+     *
+     * @hide
+     */
+    public void deactivate() {
         if (!isDeactivated()) {
             try {
                 native_munmap(mAddress, mLength);
@@ -135,6 +181,9 @@
      * @return previous value of allowPurging
      */
     synchronized public boolean allowPurging(boolean allowPurging) throws IOException {
+        if (!mOwnsRegion) {
+            throw new IOException("Only the owner can make ashmem regions purgable.");
+        }
         boolean oldValue = mAllowPurging;
         if (oldValue != allowPurging) {
             native_pin(mFD, !allowPurging);
@@ -210,6 +259,64 @@
         native_write(mFD, mAddress, buffer, srcOffset, destOffset, count, mAllowPurging);
     }
 
+    /**
+     * Gets a ParcelFileDescriptor for the memory file. See {@link #getFileDescriptor()}
+     * for caveats. This must be here to allow classes outside <code>android.os</code< to
+     * make ParcelFileDescriptors from MemoryFiles, as
+     * {@link ParcelFileDescriptor#ParcelFileDescriptor(FileDescriptor)} is package private.
+     *
+     *
+     * @return The file descriptor owned by this memory file object.
+     *         The file descriptor is not duplicated.
+     * @throws IOException If the memory file has been closed.
+     *
+     * @hide
+     */
+    public ParcelFileDescriptor getParcelFileDescriptor() throws IOException {
+        return new ParcelFileDescriptor(getFileDescriptor());
+    }
+
+    /**
+     * Gets a FileDescriptor for the memory file. Note that this file descriptor
+     * is only safe to pass to {@link #MemoryFile(FileDescriptor,int)}). It
+     * should not be used with file descriptor operations that expect a file descriptor
+     * for a normal file.
+     *
+     * The returned file descriptor is not duplicated.
+     *
+     * @throws IOException If the memory file has been closed.
+     *
+     * @hide
+     */
+    public FileDescriptor getFileDescriptor() throws IOException {
+        return mFD;
+    }
+
+    /**
+     * Checks whether the given file descriptor refers to a memory file.
+     *
+     * @throws IOException If <code>fd</code> is not a valid file descriptor.
+     *
+     * @hide
+     */
+    public static boolean isMemoryFile(FileDescriptor fd) throws IOException {
+        return native_is_ashmem_region(fd);
+    }
+
+    /**
+     * Converts a file mode string to a <code>prot</code> value as expected by
+     * native_mmap().
+     *
+     * @throws IllegalArgumentException if the file mode is invalid.
+     */
+    private static int modeToProt(String mode) {
+        if ("r".equals(mode)) {
+            return PROT_READ;
+        } else {
+            throw new IllegalArgumentException("Unsupported file mode: '" + mode + "'");
+        }
+    }
+
     private class MemoryInputStream extends InputStream {
 
         private int mMark = 0;
diff --git a/core/jni/android_os_MemoryFile.cpp b/core/jni/android_os_MemoryFile.cpp
index 6c16150..8643393 100644
--- a/core/jni/android_os_MemoryFile.cpp
+++ b/core/jni/android_os_MemoryFile.cpp
@@ -39,17 +39,17 @@
 
     if (result < 0) {
         jniThrowException(env, "java/io/IOException", "ashmem_create_region failed");
-	return NULL;
+        return NULL;
     }
 
     return jniCreateFileDescriptor(env, result);
 }
 
 static jint android_os_MemoryFile_mmap(JNIEnv* env, jobject clazz, jobject fileDescriptor,
-        jint length)
+        jint length, jint prot)
 {
     int fd = jniGetFDFromFileDescriptor(env, fileDescriptor);
-    jint result = (jint)mmap(NULL, length, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
+    jint result = (jint)mmap(NULL, length, prot, MAP_SHARED, fd, 0);
     if (!result)
         jniThrowException(env, "java/io/IOException", "mmap failed");
     return result;
@@ -118,14 +118,36 @@
     }
 }
 
+static jboolean android_os_MemoryFile_is_ashmem_region(JNIEnv* env, jobject clazz,
+        jobject fileDescriptor) {
+    int fd = jniGetFDFromFileDescriptor(env, fileDescriptor);
+    // Use ASHMEM_GET_SIZE to find out if the fd refers to an ashmem region.
+    // ASHMEM_GET_SIZE should succeed for all ashmem regions, and the kernel
+    // should return ENOTTY for all other valid file descriptors
+    int result = ashmem_get_size_region(fd);
+    if (result < 0) {
+        if (errno == ENOTTY) {
+            // ENOTTY means that the ioctl does not apply to this object,
+            // i.e., it is not an ashmem region.
+            return JNI_FALSE;
+        }
+        // Some other error, throw exception
+        jniThrowIOException(env, errno);
+        return JNI_FALSE;
+    }
+    return JNI_TRUE;
+}
+
 static const JNINativeMethod methods[] = {
     {"native_open",  "(Ljava/lang/String;I)Ljava/io/FileDescriptor;", (void*)android_os_MemoryFile_open},
-    {"native_mmap",  "(Ljava/io/FileDescriptor;I)I", (void*)android_os_MemoryFile_mmap},
+    {"native_mmap",  "(Ljava/io/FileDescriptor;II)I", (void*)android_os_MemoryFile_mmap},
     {"native_munmap", "(II)V", (void*)android_os_MemoryFile_munmap},
     {"native_close", "(Ljava/io/FileDescriptor;)V", (void*)android_os_MemoryFile_close},
     {"native_read",  "(Ljava/io/FileDescriptor;I[BIIIZ)I", (void*)android_os_MemoryFile_read},
     {"native_write", "(Ljava/io/FileDescriptor;I[BIIIZ)V", (void*)android_os_MemoryFile_write},
     {"native_pin",   "(Ljava/io/FileDescriptor;Z)V", (void*)android_os_MemoryFile_pin},
+    {"native_is_ashmem_region", "(Ljava/io/FileDescriptor;)Z",
+            (void*)android_os_MemoryFile_is_ashmem_region}
 };
 
 static const char* const kClassPathName = "android/os/MemoryFile";
diff --git a/tests/AndroidTests/AndroidManifest.xml b/tests/AndroidTests/AndroidManifest.xml
index 843d844..fd6e6d8 100644
--- a/tests/AndroidTests/AndroidManifest.xml
+++ b/tests/AndroidTests/AndroidManifest.xml
@@ -206,6 +206,12 @@
             <meta-data android:name="com.android.unit_tests.reference" android:resource="@xml/metadata" />
         </provider>
 
+        <!-- Application components used for content tests -->
+        <provider android:name=".content.MemoryFileProvider"
+                android:authorities="com.android.unit_tests.content.MemoryFileProvider"
+                android:process=":MemoryFileProvider">
+        </provider>
+
         <!-- Application components used for os tests -->
 
         <service android:name=".os.MessengerService"
diff --git a/tests/AndroidTests/src/com/android/unit_tests/content/MemoryFileProvider.java b/tests/AndroidTests/src/com/android/unit_tests/content/MemoryFileProvider.java
new file mode 100644
index 0000000..b31ce18
--- /dev/null
+++ b/tests/AndroidTests/src/com/android/unit_tests/content/MemoryFileProvider.java
@@ -0,0 +1,211 @@
+/*
+ * Copyright (C) 2009 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.unit_tests.content;
+
+import android.content.ContentProvider;
+import android.content.ContentValues;
+import android.content.Context;
+import android.content.UriMatcher;
+import android.content.res.AssetFileDescriptor;
+import android.database.Cursor;
+import android.database.sqlite.SQLiteDatabase;
+import android.database.sqlite.SQLiteOpenHelper;
+import android.net.Uri;
+import android.os.MemoryFile;
+import android.os.ParcelFileDescriptor;
+import android.util.Log;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.OutputStream;
+
+/** Simple test provider that runs in the local process. */
+public class MemoryFileProvider extends ContentProvider {
+    private static final String TAG = "MemoryFileProvider";
+
+    private static final String DATA_FILE = "data.bin";
+
+    // some random data
+    public static final byte[] TEST_BLOB = new byte[] {
+        -12,  127, 0, 3, 1, 2, 3, 4, 5, 6, 1, -128, -1, -54, -65, 35,
+        -53, -96, -74, -74, -55, -43, -69, 3, 52, -58,
+        -121, 127, 87, -73, 16, -13, -103, -65, -128, -36,
+        107, 24, 118, -17, 97, 97, -88, 19, -94, -54,
+        53, 43, 44, -27, -124, 28, -74, 26, 35, -36,
+        16, -124, -31, -31, -128, -79, 108, 116, 43, -17 };
+
+    private SQLiteOpenHelper mOpenHelper;
+
+    private static final int DATA_ID_BLOB = 1;
+    private static final int HUGE = 2;
+    private static final int FILE = 3;
+
+    private static final UriMatcher sURLMatcher = new UriMatcher(
+            UriMatcher.NO_MATCH);
+
+    static {
+        sURLMatcher.addURI("*", "data/#/blob", DATA_ID_BLOB);
+        sURLMatcher.addURI("*", "huge", HUGE);
+        sURLMatcher.addURI("*", "file", FILE);
+    }
+
+    private static class DatabaseHelper extends SQLiteOpenHelper {
+        private static final String DATABASE_NAME = "local.db";
+        private static final int DATABASE_VERSION = 1;
+
+        public DatabaseHelper(Context context) {
+            super(context, DATABASE_NAME, null, DATABASE_VERSION);
+        }
+
+        @Override
+        public void onCreate(SQLiteDatabase db) {
+            db.execSQL("CREATE TABLE data (" +
+                       "_id INTEGER PRIMARY KEY," +
+                       "_blob TEXT, " +
+                       "integer INTEGER);");
+
+            // insert alarms
+            ContentValues values = new ContentValues();
+            values.put("_id", 1);
+            values.put("_blob", TEST_BLOB);
+            values.put("integer", 100);
+            db.insert("data", null, values);
+        }
+
+        @Override
+        public void onUpgrade(SQLiteDatabase db, int oldVersion, int currentVersion) {
+            Log.w(TAG, "Upgrading test database from version " +
+                  oldVersion + " to " + currentVersion +
+                  ", which will destroy all old data");
+            db.execSQL("DROP TABLE IF EXISTS data");
+            onCreate(db);
+        }
+    }
+
+
+    public MemoryFileProvider() {
+    }
+
+    @Override
+    public boolean onCreate() {
+        mOpenHelper = new DatabaseHelper(getContext());
+        try {
+            OutputStream out = getContext().openFileOutput(DATA_FILE, Context.MODE_PRIVATE);
+            out.write(TEST_BLOB);
+            out.close();
+        } catch (IOException ex) {
+            ex.printStackTrace();
+        }
+        return true;
+    }
+
+    @Override
+    public Cursor query(Uri url, String[] projectionIn, String selection,
+            String[] selectionArgs, String sort) {
+        throw new UnsupportedOperationException("query not supported");
+    }
+
+    @Override
+    public String getType(Uri url) {
+        int match = sURLMatcher.match(url);
+        switch (match) {
+            case DATA_ID_BLOB:
+                return "application/octet-stream";
+            case FILE:
+                return "application/octet-stream";
+            default:
+                throw new IllegalArgumentException("Unknown URL");
+        }
+    }
+
+    @Override
+    public AssetFileDescriptor openAssetFile(Uri url, String mode) throws FileNotFoundException {
+        int match = sURLMatcher.match(url);
+        switch (match) {
+            case DATA_ID_BLOB:
+                String sql = "SELECT _blob FROM data WHERE _id=" + url.getPathSegments().get(1);
+                return getBlobColumnAsAssetFile(url, mode, sql);
+            case HUGE:
+                try {
+                    MemoryFile memoryFile = new MemoryFile(null, 5000000);
+                    memoryFile.writeBytes(TEST_BLOB, 0, 1000000, TEST_BLOB.length);
+                    memoryFile.deactivate();
+                    return AssetFileDescriptor.fromMemoryFile(memoryFile);
+                } catch (IOException ex) {
+                    throw new FileNotFoundException("Error reading " + url + ":" + ex.toString());
+                }
+            case FILE:
+                File file = getContext().getFileStreamPath(DATA_FILE);
+                ParcelFileDescriptor fd =
+                        ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
+                return new AssetFileDescriptor(fd, 0, AssetFileDescriptor.UNKNOWN_LENGTH);
+            default:
+                throw new FileNotFoundException("No files supported by provider at " + url);
+        }
+    }
+
+    private AssetFileDescriptor getBlobColumnAsAssetFile(Uri url, String mode, String sql)
+            throws FileNotFoundException {
+        if (!"r".equals(mode)) {
+            throw new FileNotFoundException("Mode " + mode + " not supported for " + url);
+        }
+        try {
+            SQLiteDatabase db = mOpenHelper.getReadableDatabase();
+            MemoryFile file = simpleQueryForBlobMemoryFile(db, sql);
+            if (file == null) throw new FileNotFoundException("No such entry: " + url);
+            AssetFileDescriptor afd = AssetFileDescriptor.fromMemoryFile(file);
+            file.deactivate();
+            // need to dup and then close? openFileHelper() doesn't do that though
+            return afd;
+        } catch (IOException ex) {
+            throw new FileNotFoundException("Error reading " + url + ":" + ex.toString());
+        }
+    }
+
+    private MemoryFile simpleQueryForBlobMemoryFile(SQLiteDatabase db, String sql) throws IOException {
+        Cursor cursor = db.rawQuery(sql, null);
+        try {
+            if (!cursor.moveToFirst()) {
+                return null;
+            }
+            byte[] bytes = cursor.getBlob(0);
+            MemoryFile file = new MemoryFile(null, bytes.length);
+            file.writeBytes(bytes, 0, 0, bytes.length);
+            return file;
+        } finally {
+            if (cursor != null) {
+                cursor.close();
+            }
+        }
+    }
+
+    @Override
+    public int update(Uri url, ContentValues values, String where, String[] whereArgs) {
+        throw new UnsupportedOperationException("update not supported");
+    }
+
+    @Override
+    public Uri insert(Uri url, ContentValues initialValues) {
+        throw new UnsupportedOperationException("insert not supported");
+    }
+
+    @Override
+    public int delete(Uri url, String where, String[] whereArgs) {
+        throw new UnsupportedOperationException("delete not supported");
+    }
+}
diff --git a/tests/AndroidTests/src/com/android/unit_tests/content/MemoryFileProviderTest.java b/tests/AndroidTests/src/com/android/unit_tests/content/MemoryFileProviderTest.java
new file mode 100644
index 0000000..2d8190a
--- /dev/null
+++ b/tests/AndroidTests/src/com/android/unit_tests/content/MemoryFileProviderTest.java
@@ -0,0 +1,83 @@
+/*
+ * Copyright (C) 2009 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.unit_tests.content;
+
+import android.content.ContentResolver;
+import android.net.Uri;
+import android.test.AndroidTestCase;
+import android.test.suitebuilder.annotation.MediumTest;
+
+import java.io.InputStream;
+import java.util.Arrays;
+
+/**
+ * Tests reading a MemoryFile-based AssestFile from a ContentProvider running
+ * in a different process.
+ */
+public class MemoryFileProviderTest extends AndroidTestCase {
+
+    // reads from a cross-process AssetFileDescriptor for a MemoryFile
+    @MediumTest
+    public void testRead() throws Exception {
+        ContentResolver resolver = getContext().getContentResolver();
+        Uri uri = Uri.parse("content://com.android.unit_tests.content.MemoryFileProvider/data/1/blob");
+        byte[] buf = new byte[MemoryFileProvider.TEST_BLOB.length];
+        InputStream in = resolver.openInputStream(uri);
+        assertNotNull(in);
+        int count = in.read(buf);
+        assertEquals(buf.length, count);
+        // TODO: MemoryFile throws IndexOutOfBoundsException for this, http://b/issue?id=1881894
+        //assertEquals(-1, in.read());
+        in.close();
+        assertTrue(Arrays.equals(MemoryFileProvider.TEST_BLOB, buf));
+    }
+
+    // tests that we don't leak file descriptors or virtual address space
+    @MediumTest
+    public void testClose() throws Exception {
+        ContentResolver resolver = getContext().getContentResolver();
+        // open enough file descriptors that we will crash something if we leak FDs
+        // or address space
+        for (int i = 0; i < 1025; i++) {
+            Uri uri = Uri.parse("content://com.android.unit_tests.content.MemoryFileProvider/huge");
+            InputStream in = resolver.openInputStream(uri);
+            assertNotNull("Failed to open stream number " + i, in);
+            assertEquals(1000000, in.skip(1000000));
+            byte[] buf = new byte[MemoryFileProvider.TEST_BLOB.length];
+            int count = in.read(buf);
+            assertEquals(buf.length, count);
+            assertTrue(Arrays.equals(MemoryFileProvider.TEST_BLOB, buf));
+            in.close();
+        }
+    }
+
+    // tests that we haven't broken AssestFileDescriptors for normal files.
+    @MediumTest
+    public void testFile() throws Exception {
+        ContentResolver resolver = getContext().getContentResolver();
+        Uri uri = Uri.parse("content://com.android.unit_tests.content.MemoryFileProvider/file");
+        byte[] buf = new byte[MemoryFileProvider.TEST_BLOB.length];
+        InputStream in = resolver.openInputStream(uri);
+        assertNotNull(in);
+        int count = in.read(buf);
+        assertEquals(buf.length, count);
+        assertEquals(-1, in.read());
+        in.close();
+        assertTrue(Arrays.equals(MemoryFileProvider.TEST_BLOB, buf));
+    }
+
+}
diff --git a/tests/AndroidTests/src/com/android/unit_tests/os/MemoryFileTest.java b/tests/AndroidTests/src/com/android/unit_tests/os/MemoryFileTest.java
index 5161f7b..66f2b508 100644
--- a/tests/AndroidTests/src/com/android/unit_tests/os/MemoryFileTest.java
+++ b/tests/AndroidTests/src/com/android/unit_tests/os/MemoryFileTest.java
@@ -17,19 +17,21 @@
 package com.android.unit_tests.os;
 
 import android.os.MemoryFile;
+import android.test.AndroidTestCase;
 import android.test.suitebuilder.annotation.LargeTest;
 import android.test.suitebuilder.annotation.MediumTest;
 import android.test.suitebuilder.annotation.SmallTest;
 
+import java.io.File;
+import java.io.FileDescriptor;
+import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.util.ArrayList;
 import java.util.List;
 
-import junit.framework.TestCase;
-
-public class MemoryFileTest extends TestCase {
+public class MemoryFileTest extends AndroidTestCase {
 
     private void compareBuffers(byte[] buffer1, byte[] buffer2, int length) throws Exception {
         for (int i = 0; i < length; i++) {
@@ -163,6 +165,50 @@
         }
     }
 
+    @SmallTest
+    public void testIsMemoryFile() throws Exception {
+        MemoryFile file = new MemoryFile("MemoryFileTest", 1000000);
+        FileDescriptor fd = file.getFileDescriptor();
+        assertNotNull(fd);
+        assertTrue(fd.valid());
+        assertTrue(MemoryFile.isMemoryFile(fd));
+        file.close();
+
+        assertFalse(MemoryFile.isMemoryFile(FileDescriptor.in));
+        assertFalse(MemoryFile.isMemoryFile(FileDescriptor.out));
+        assertFalse(MemoryFile.isMemoryFile(FileDescriptor.err));
+
+        File tempFile = File.createTempFile("MemoryFileTest",".tmp", getContext().getFilesDir());
+        assertNotNull(file);
+        FileOutputStream out = null;
+        try {
+            out = new FileOutputStream(tempFile);
+            FileDescriptor fileFd = out.getFD();
+            assertNotNull(fileFd);
+            assertFalse(MemoryFile.isMemoryFile(fileFd));
+        } finally {
+            if (out != null) {
+                out.close();
+            }
+            tempFile.delete();
+        }
+    }
+
+    public void testFileDescriptor() throws Exception {
+        MemoryFile file = new MemoryFile("MemoryFileTest", 1000000);
+        MemoryFile ref = new MemoryFile(file.getFileDescriptor(), file.length(), "r");
+        byte[] buffer;
+
+        // write to original, read from reference
+        file.writeBytes(testString, 0, 2000, testString.length);
+        buffer = new byte[testString.length];
+        ref.readBytes(buffer, 2000, 0, testString.length);
+        compareBuffers(testString, buffer, testString.length);
+
+        file.close();
+        ref.close();  // Doesn't actually do anything, since the file descriptor is not dup(2):ed
+    }
+
     private static final byte[] testString = new byte[] {
         3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9, 3, 2, 3, 8, 4, 6, 2, 6, 4, 3, 3, 8, 3, 2, 7, 9, 5, 0, 2, 8, 8, 4, 1, 9, 7, 1, 6, 9, 3, 9, 9, 3, 7, 5, 1, 0, 5, 8, 2, 0, 9, 7, 4, 9, 4, 4, 5, 9, 2, 3, 0, 7, 8, 1, 6, 4,
         0, 6, 2, 8, 6, 2, 0, 8, 9, 9, 8, 6, 2, 8, 0, 3, 4, 8, 2, 5, 3, 4, 2, 1, 1, 7, 0, 6, 7, 9, 8, 2, 1, 4, 8, 0, 8, 6, 5, 1, 3, 2, 8, 2, 3, 0, 6, 6, 4, 7, 0, 9, 3, 8, 4, 4, 6, 0, 9, 5, 5, 0, 5, 8, 2, 2, 3, 1, 7, 2,