diff options
author | Chiao Cheng <chiaocheng@google.com> | 2013-07-10 13:50:13 -0700 |
---|---|---|
committer | Chiao Cheng <chiaocheng@google.com> | 2013-07-10 16:13:03 -0700 |
commit | d8836bb14138c242daa7d47fa20adff72ec778be (patch) | |
tree | f46691e6f2974810abf4ffb3252950255595d785 | |
parent | aa3e957fe67e1c9eb0728c1f302e209452d3b00e (diff) | |
download | cts-d8836bb14138c242daa7d47fa20adff72ec778be.tar.gz |
Cts test for DumpFileProvider.
Checks that the provider only serves allowed file names.
Bug: 9607306
(cherry picked from commit af89ef6cdc3e323b7fd3c6b16c0d40c88108fd05)
Change-Id: I6f4b4b5f965881b34f19195addc9bd8647a4cf94
-rw-r--r-- | tests/tests/provider/src/android/provider/cts/ContactsContract_DumpFileProviderTest.java | 106 |
1 files changed, 106 insertions, 0 deletions
diff --git a/tests/tests/provider/src/android/provider/cts/ContactsContract_DumpFileProviderTest.java b/tests/tests/provider/src/android/provider/cts/ContactsContract_DumpFileProviderTest.java new file mode 100644 index 00000000000..5c5afaa74e4 --- /dev/null +++ b/tests/tests/provider/src/android/provider/cts/ContactsContract_DumpFileProviderTest.java @@ -0,0 +1,106 @@ +/* + * Copyright (C) 2013 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 android.provider.cts; + +import android.content.ContentResolver; +import android.net.Uri; +import android.test.AndroidTestCase; + +import java.io.FileNotFoundException; + +public class ContactsContract_DumpFileProviderTest extends AndroidTestCase { + + private static final String URI_PREFIX = "content://com.android.contacts.dumpfile/"; + + private static final String[] NOT_ALLOWED_FILES = { + "not_allowed.txt", + "../A-contacts-db.zip", // ".." is not allowed. + "/A-contacts-db.zip", // "/" is not allowed + "-contacts-db.zip", // no name prefix + "asdf-contacts-db.zip"}; + + private static final String[] ALLOWED_FILES = { + "1234567890abcdefABCDEF-contacts-db.zip", + "a-contacts-db.zip", + "0-contacts-db.zip", + "A-contacts-db.zip", + "abcdefabcdefabcdefabcdef-contacts-db.zip"}; + + private ContentResolver mResolver; + + @Override + protected void setUp() throws Exception { + super.setUp(); + mResolver = getContext().getContentResolver(); + } + + public void testOpenFileDescriptor_throwsErrorWithIllegalFileName() { + for (String fileName : NOT_ALLOWED_FILES) { + Uri uri = Uri.parse(URI_PREFIX + fileName); + assertOpenFileDescriptorThrowsError(uri); + } + } + + public void testOpenFileDescriptor_worksWithValidFileName() { + for (String fileName : ALLOWED_FILES) { + final Uri uri = Uri.parse(URI_PREFIX + fileName); + try { + mResolver.openFileDescriptor(uri, "r"); + } catch (FileNotFoundException e) { + + } + } + } + + public void testQuery_throwsErrorWithIllegalFileName() { + for (String fileName : NOT_ALLOWED_FILES) { + final Uri uri = Uri.parse(URI_PREFIX + fileName); + assertQueryThrowsError(uri); + } + } + + public void testQuery_worksWithValidFileName() { + for (String fileName : ALLOWED_FILES) { + final Uri uri = Uri.parse(URI_PREFIX + fileName); + mResolver.query(uri, null, null, null, null); + } + } + + private void assertQueryThrowsError(Uri uri) { + try { + mResolver.query(uri, null, null, null, null); + } catch (IllegalArgumentException e) { + // pass + return; + } + + fail("IllegalArgumentException expected but not thrown."); + } + + private void assertOpenFileDescriptorThrowsError(Uri uri) { + try { + mResolver.openFileDescriptor(uri, "r"); + } catch (IllegalArgumentException e) { + // pass + return; + } catch (FileNotFoundException e) { + + } + + fail("IllegalArgumentException expected but not thrown."); + } +} |