DownloadProviderPermissionsTest.java revision 8669ad1117c7502d74ef24e2a7f9df387f5eddd2
18669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot/*
28669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot * Copyright (C) 2009 The Android Open Source Project
38669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot *
48669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot * Licensed under the Apache License, Version 2.0 (the "License");
58669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot * you may not use this file except in compliance with the License.
68669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot * You may obtain a copy of the License at
78669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot *
88669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot *      http://www.apache.org/licenses/LICENSE-2.0
98669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot *
108669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot * Unless required by applicable law or agreed to in writing, software
118669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot * distributed under the License is distributed on an "AS IS" BASIS,
128669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
138669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot * See the License for the specific language governing permissions and
148669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot * limitations under the License.
158669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot */
168669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot
178669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabotpackage com.android.providers.downloads.permission.tests;
188669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot
198669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabotimport java.io.FileNotFoundException;
208669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabotimport java.io.FileOutputStream;
218669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabotimport java.io.IOException;
228669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot
238669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabotimport android.content.ContentResolver;
248669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabotimport android.content.ContentValues;
258669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabotimport android.content.Intent;
268669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabotimport android.provider.Downloads;
278669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabotimport android.test.AndroidTestCase;
288669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabotimport android.test.suitebuilder.annotation.MediumTest;
298669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot
308669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot/**
318669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot * Verify that protected Download provider actions require specific permissions.
328669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot *
338669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot * TODO: consider adding test where app has ACCESS_DOWNLOAD_MANAGER, but not
348669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot * ACCESS_DOWNLOAD_MANAGER_ADVANCED
358669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot */
368669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabotpublic class DownloadProviderPermissionsTest extends AndroidTestCase {
378669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot
388669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot    private ContentResolver mContentResolver;
398669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot
408669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot    @Override
418669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot    protected void setUp() throws Exception {
428669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot        super.setUp();
438669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot        mContentResolver = getContext().getContentResolver();
448669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot    }
458669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot
468669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot    /**
478669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot     * Test that an app cannot access the /cache filesystem
488669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot     * <p>Tests Permission:
498669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot     *   {@link com.android.providers.downloads.Manifest.permission#ACCESS_CACHE_FILESYSTEM}
508669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot     */
518669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot    @MediumTest
528669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot    public void testAccessCacheFilesystem() throws IOException {
538669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot        try {
548669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot            String filePath = "/cache/this-should-not-exist.txt";
558669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot            FileOutputStream strm = new FileOutputStream(filePath);
568669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot            strm.write("Oops!".getBytes());
578669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot            strm.flush();
588669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot            strm.close();
598669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot            fail("Was able to create and write to " + filePath);
608669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot        } catch (SecurityException e) {
618669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot            // expected
628669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot        } catch (FileNotFoundException e) {
638669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot            // also could be expected
648669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot        }
658669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot    }
668669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot
678669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot    /**
688669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot     * Test that an untrusted app cannot read from the download provider
698669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot     * <p>Tests Permission:
708669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot     *   {@link com.android.providers.downloads.Manifest.permission#ACCESS_DOWNLOAD_MANAGER}
718669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot     */
728669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot    @MediumTest
738669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot    public void testReadDownloadProvider() throws IOException {
748669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot        try {
758669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot            mContentResolver.query(Downloads.CONTENT_URI, null, null, null, null);
768669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot            fail("read from provider did not throw SecurityException as expected.");
778669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot        } catch (SecurityException e) {
788669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot            // expected
798669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot        }
808669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot    }
818669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot
828669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot    /**
838669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot     * Test that an untrusted app cannot write to the download provider
848669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot     * <p>Tests Permission:
858669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot     *   {@link com.android.providers.downloads.Manifest.permission#ACCESS_DOWNLOAD_MANAGER}
868669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot     */
878669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot    @MediumTest
888669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot    public void testWriteDownloadProvider() throws IOException {
898669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot        try {
908669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot            ContentValues values = new ContentValues();
918669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot            values.put(Downloads.DESTINATION, "foo");
928669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot            mContentResolver.insert(Downloads.CONTENT_URI, values);
938669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot            fail("write to provider did not throw SecurityException as expected.");
948669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot        } catch (SecurityException e) {
958669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot            // expected
968669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot        }
978669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot    }
988669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot
998669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot    /**
1008669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot     * Test that an untrusted app cannot access the download service
1018669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot     * <p>Tests Permission:
1028669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot     *   {@link com.android.providers.downloads.Manifest.permission#ACCESS_DOWNLOAD_MANAGER}
1038669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot     */
1048669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot    @MediumTest
1058669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot    public void testStartDownloadService() throws IOException {
1068669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot        try {
1078669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot            Intent downloadServiceIntent = new Intent();
1088669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot            downloadServiceIntent.setClassName("com.android.providers.downloads",
1098669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot                    "com.android.providers.downloads.DownloadService");
1108669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot            getContext().startService(downloadServiceIntent);
1118669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot            fail("starting download service did not throw SecurityException as expected.");
1128669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot        } catch (SecurityException e) {
1138669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot            // expected
1148669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot        }
1158669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot    }
1168669ad1117c7502d74ef24e2a7f9df387f5eddd2Brett Chabot}
117