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 com.android.providers.downloads.permission.tests;
18
19import java.io.FileNotFoundException;
20import java.io.FileOutputStream;
21import java.io.IOException;
22
23import android.content.ContentResolver;
24import android.content.ContentValues;
25import android.content.Intent;
26import android.provider.Downloads;
27import android.test.AndroidTestCase;
28import android.test.suitebuilder.annotation.MediumTest;
29
30/**
31 * Verify that protected Download provider actions require specific permissions.
32 *
33 * TODO: consider adding test where app has ACCESS_DOWNLOAD_MANAGER, but not
34 * ACCESS_DOWNLOAD_MANAGER_ADVANCED
35 */
36public class DownloadProviderPermissionsTest extends AndroidTestCase {
37
38    private ContentResolver mContentResolver;
39
40    @Override
41    protected void setUp() throws Exception {
42        super.setUp();
43        mContentResolver = getContext().getContentResolver();
44    }
45
46    /**
47     * Test that an app cannot access the /cache filesystem
48     * <p>Tests Permission:
49     *   {@link android.Manifest.permission#ACCESS_CACHE_FILESYSTEM}
50     */
51    @MediumTest
52    public void testAccessCacheFilesystem() throws IOException {
53        try {
54            String filePath = "/cache/this-should-not-exist.txt";
55            FileOutputStream strm = new FileOutputStream(filePath);
56            strm.write("Oops!".getBytes());
57            strm.flush();
58            strm.close();
59            fail("Was able to create and write to " + filePath);
60        } catch (SecurityException e) {
61            // expected
62        } catch (FileNotFoundException e) {
63            // also could be expected
64        }
65    }
66
67    /**
68     * Test that an untrusted app cannot write to the download provider
69     * <p>Tests Permission:
70     *   {@link com.android.providers.downloads.Manifest.permission#ACCESS_DOWNLOAD_MANAGER}
71     *   and
72     *   {@link android.Manifest.permission#INTERNET}
73     */
74    @MediumTest
75    public void testWriteDownloadProvider() {
76        try {
77            ContentValues values = new ContentValues();
78            values.put(Downloads.Impl.COLUMN_URI, "foo");
79            mContentResolver.insert(Downloads.Impl.CONTENT_URI, values);
80            fail("write to provider did not throw SecurityException as expected.");
81        } catch (SecurityException e) {
82            // expected
83        }
84    }
85
86    /**
87     * Test that an untrusted app cannot access the download service
88     * <p>Tests Permission:
89     *   {@link com.android.providers.downloads.Manifest.permission#ACCESS_DOWNLOAD_MANAGER}
90     */
91    @MediumTest
92    public void testStartDownloadService() {
93        try {
94            Intent downloadServiceIntent = new Intent();
95            downloadServiceIntent.setClassName("com.android.providers.downloads",
96                    "com.android.providers.downloads.DownloadService");
97            getContext().startService(downloadServiceIntent);
98            fail("starting download service did not throw SecurityException as expected.");
99        } catch (SecurityException e) {
100            // expected
101        }
102    }
103}
104