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
17package android.content;
18
19import android.net.Uri;
20import android.test.AndroidTestCase;
21import android.test.suitebuilder.annotation.LargeTest;
22import android.test.suitebuilder.annotation.MediumTest;
23import android.test.suitebuilder.annotation.SmallTest;
24
25import java.io.InputStream;
26import java.util.Arrays;
27
28/**
29 * Tests reading a MemoryFile-based AssestFile from a ContentProvider running
30 * in a different process.
31 */
32public class MemoryFileProviderTest extends AndroidTestCase {
33
34    // reads from a cross-process AssetFileDescriptor for a MemoryFile
35    @MediumTest
36    public void testRead() throws Exception {
37        ContentResolver resolver = getContext().getContentResolver();
38        Uri uri = Uri.parse("content://android.content.MemoryFileProvider/data/1/blob");
39        byte[] buf = new byte[MemoryFileProvider.TEST_BLOB.length];
40        InputStream in = resolver.openInputStream(uri);
41        assertNotNull(in);
42        int count = in.read(buf);
43        assertEquals(buf.length, count);
44        assertEquals(-1, in.read());
45        in.close();
46        assertTrue(Arrays.equals(MemoryFileProvider.TEST_BLOB, buf));
47    }
48
49    // tests that we don't leak file descriptors or virtual address space
50    @LargeTest
51    public void testClose() throws Exception {
52        ContentResolver resolver = getContext().getContentResolver();
53        // open enough file descriptors that we will crash something if we leak FDs
54        // or address space
55        for (int i = 0; i < 1025; i++) {
56            Uri uri = Uri.parse("content://android.content.MemoryFileProvider/huge");
57            InputStream in = resolver.openInputStream(uri);
58            assertNotNull("Failed to open stream number " + i, in);
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    @SmallTest
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