1 /* 2 * Copyright (C) 2009 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.content; 18 19 import android.content.ContentResolver; 20 import android.net.Uri; 21 import android.test.AndroidTestCase; 22 import android.test.suitebuilder.annotation.MediumTest; 23 24 import java.io.InputStream; 25 import java.util.Arrays; 26 27 /** 28 * Tests reading a MemoryFile-based AssestFile from a ContentProvider running 29 * in a different process. 30 */ 31 public class MemoryFileProviderTest extends AndroidTestCase { 32 33 // reads from a cross-process AssetFileDescriptor for a MemoryFile 34 @MediumTest 35 public void testRead() throws Exception { 36 ContentResolver resolver = getContext().getContentResolver(); 37 Uri uri = Uri.parse("content://android.content.MemoryFileProvider/data/1/blob"); 38 byte[] buf = new byte[MemoryFileProvider.TEST_BLOB.length]; 39 InputStream in = resolver.openInputStream(uri); 40 assertNotNull(in); 41 int count = in.read(buf); 42 assertEquals(buf.length, count); 43 assertEquals(-1, in.read()); 44 in.close(); 45 assertTrue(Arrays.equals(MemoryFileProvider.TEST_BLOB, buf)); 46 } 47 48 // tests that we don't leak file descriptors or virtual address space 49 @MediumTest 50 public void testClose() throws Exception { 51 ContentResolver resolver = getContext().getContentResolver(); 52 // open enough file descriptors that we will crash something if we leak FDs 53 // or address space 54 for (int i = 0; i < 1025; i++) { 55 Uri uri = Uri.parse("content://android.content.MemoryFileProvider/huge"); 56 InputStream in = resolver.openInputStream(uri); 57 assertNotNull("Failed to open stream number " + i, in); 58 assertEquals(1000000, in.skip(1000000)); 59 byte[] buf = new byte[MemoryFileProvider.TEST_BLOB.length]; 60 int count = in.read(buf); 61 assertEquals(buf.length, count); 62 assertTrue(Arrays.equals(MemoryFileProvider.TEST_BLOB, buf)); 63 in.close(); 64 } 65 } 66 67 // tests that we haven't broken AssestFileDescriptors for normal files. 68 @MediumTest 69 public void testFile() throws Exception { 70 ContentResolver resolver = getContext().getContentResolver(); 71 Uri uri = Uri.parse("content://android.content.MemoryFileProvider/file"); 72 byte[] buf = new byte[MemoryFileProvider.TEST_BLOB.length]; 73 InputStream in = resolver.openInputStream(uri); 74 assertNotNull(in); 75 int count = in.read(buf); 76 assertEquals(buf.length, count); 77 assertEquals(-1, in.read()); 78 in.close(); 79 assertTrue(Arrays.equals(MemoryFileProvider.TEST_BLOB, buf)); 80 } 81 82 } 83