diff options
Diffstat (limited to 'services/tests')
4 files changed, 327 insertions, 1 deletions
diff --git a/services/tests/servicestests/AndroidManifest.xml b/services/tests/servicestests/AndroidManifest.xml index 7848b1d..636dd4d 100644 --- a/services/tests/servicestests/AndroidManifest.xml +++ b/services/tests/servicestests/AndroidManifest.xml @@ -35,7 +35,8 @@ <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.MANAGE_USERS" /> <uses-permission android:name="android.permission.INTERACT_ACROSS_USERS_FULL" /> - + <uses-permission android:name="android.permission.MANAGE_DEVICE_ADMINS" /> + <application> <uses-library android:name="android.test.runner" /> @@ -53,6 +54,15 @@ </intent-filter> </service> + <receiver android:name="com.android.server.devicepolicy.ApplicationRestrictionsTest$AdminReceiver" + android:permission="android.permission.BIND_DEVICE_ADMIN"> + <meta-data android:name="android.app.device_admin" + android:resource="@xml/device_admin_sample" /> + <intent-filter> + <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" /> + </intent-filter> + </receiver> + </application> <instrumentation diff --git a/services/tests/servicestests/res/xml/device_admin_sample.xml b/services/tests/servicestests/res/xml/device_admin_sample.xml new file mode 100644 index 0000000..032debb --- /dev/null +++ b/services/tests/servicestests/res/xml/device_admin_sample.xml @@ -0,0 +1,29 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- Copyright (C) 2014 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. +--> + +<device-admin xmlns:android="http://schemas.android.com/apk/res/android"> + <uses-policies> + <limit-password /> + <watch-login /> + <reset-password /> + <force-lock /> + <wipe-data /> + <expire-password /> + <encrypted-storage /> + <disable-camera /> + <disable-keyguard-features /> + </uses-policies> +</device-admin> diff --git a/services/tests/servicestests/src/com/android/server/devicepolicy/ApplicationRestrictionsTest.java b/services/tests/servicestests/src/com/android/server/devicepolicy/ApplicationRestrictionsTest.java new file mode 100644 index 0000000..8e8e4e6 --- /dev/null +++ b/services/tests/servicestests/src/com/android/server/devicepolicy/ApplicationRestrictionsTest.java @@ -0,0 +1,135 @@ +/* + * Copyright (C) 2014 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 com.android.server.devicepolicy; + +import android.app.admin.DeviceAdminReceiver; +import android.app.admin.DevicePolicyManager; +import android.content.BroadcastReceiver; +import android.content.ComponentName; +import android.content.Context; +import android.content.Intent; +import android.os.Bundle; +import android.os.UserHandle; +import android.provider.Settings; +import android.test.AndroidTestCase; +import android.test.suitebuilder.annotation.SmallTest; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; + +/** + * Tests for application restrictions persisting via profile owner: + * make -j FrameworksServicesTests + * runtest --path frameworks/base/services/tests/servicestests/ \ + * src/com/android/server/devicepolicy/ApplicationRestrictionsTest.java + */ +public class ApplicationRestrictionsTest extends AndroidTestCase { + + static DevicePolicyManager sDpm; + static ComponentName sAdminReceiver; + private static final String RESTRICTED_APP = "com.example.restrictedApp"; + static boolean sAddBack = false; + + public static class AdminReceiver extends DeviceAdminReceiver { + + @Override + public void onDisabled(Context context, Intent intent) { + if (sAddBack) { + sDpm.setActiveAdmin(sAdminReceiver, false); + sAddBack = false; + } + + super.onDisabled(context, intent); + } + } + + @Override + public void setUp() { + final Context context = getContext(); + sAdminReceiver = new ComponentName(mContext.getPackageName(), + AdminReceiver.class.getName()); + sDpm = (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE); + Settings.Secure.putInt(context.getContentResolver(), + Settings.Secure.USER_SETUP_COMPLETE, 0); + sDpm.setProfileOwner(context.getPackageName(), "Test", UserHandle.myUserId()); + Settings.Secure.putInt(context.getContentResolver(), + Settings.Secure.USER_SETUP_COMPLETE, 1); + // Remove the admin if already registered. It's async, so add it back + // when the admin gets a broadcast. Otherwise add it back right away. + if (sDpm.isAdminActive(sAdminReceiver)) { + sAddBack = true; + sDpm.removeActiveAdmin(sAdminReceiver); + } else { + sDpm.setActiveAdmin(sAdminReceiver, false); + } + } + + @Override + public void tearDown() { + Settings.Secure.putInt(getContext().getContentResolver(), + Settings.Secure.USER_SETUP_COMPLETE, 0); + sDpm.removeActiveAdmin(sAdminReceiver); + Settings.Secure.putInt(getContext().getContentResolver(), + Settings.Secure.USER_SETUP_COMPLETE, 1); + } + + public void testSettingRestrictions() { + Bundle restrictions = new Bundle(); + restrictions.putString("KEY_STRING", "Foo"); + assertNotNull(sDpm.getApplicationRestrictions(sAdminReceiver, RESTRICTED_APP)); + sDpm.setApplicationRestrictions(sAdminReceiver, RESTRICTED_APP, restrictions); + Bundle returned = sDpm.getApplicationRestrictions(sAdminReceiver, RESTRICTED_APP); + assertNotNull(returned); + assertEquals(returned.size(), 1); + assertEquals(returned.get("KEY_STRING"), "Foo"); + sDpm.setApplicationRestrictions(sAdminReceiver, RESTRICTED_APP, new Bundle()); + returned = sDpm.getApplicationRestrictions(sAdminReceiver, RESTRICTED_APP); + assertEquals(returned.size(), 0); + } + + public void testRestrictionTypes() { + Bundle restrictions = new Bundle(); + restrictions.putString("KEY_STRING", "Foo"); + restrictions.putInt("KEY_INT", 7); + restrictions.putBoolean("KEY_BOOLEAN", true); + restrictions.putBoolean("KEY_BOOLEAN_2", false); + restrictions.putString("KEY_STRING_2", "Bar"); + restrictions.putStringArray("KEY_STR_ARRAY", new String[] { "Foo", "Bar" }); + sDpm.setApplicationRestrictions(sAdminReceiver, RESTRICTED_APP, restrictions); + Bundle returned = sDpm.getApplicationRestrictions(sAdminReceiver, RESTRICTED_APP); + assertTrue(returned.getBoolean("KEY_BOOLEAN")); + assertFalse(returned.getBoolean("KEY_BOOLEAN_2")); + assertFalse(returned.getBoolean("KEY_BOOLEAN_3")); + assertEquals(returned.getInt("KEY_INT"), 7); + assertTrue(returned.get("KEY_BOOLEAN") instanceof Boolean); + assertTrue(returned.get("KEY_INT") instanceof Integer); + assertEquals(returned.get("KEY_STRING"), "Foo"); + assertEquals(returned.get("KEY_STRING_2"), "Bar"); + assertTrue(returned.getStringArray("KEY_STR_ARRAY") instanceof String[]); + sDpm.setApplicationRestrictions(sAdminReceiver, RESTRICTED_APP, new Bundle()); + } + + public void testTextEscaping() { + String fancyText = "<This contains XML/> <JSON> " + + "{ \"One\": { \"OneOne\": \"11\", \"OneTwo\": \"12\" }, \"Two\": \"2\" } <JSON/>"; + Bundle restrictions = new Bundle(); + restrictions.putString("KEY_FANCY_TEXT", fancyText); + sDpm.setApplicationRestrictions(sAdminReceiver, RESTRICTED_APP, restrictions); + Bundle returned = sDpm.getApplicationRestrictions(sAdminReceiver, RESTRICTED_APP); + assertEquals(returned.getString("KEY_FANCY_TEXT"), fancyText); + } +}
\ No newline at end of file diff --git a/services/tests/servicestests/src/com/android/server/notification/ValidateNotificationPeopleTest.java b/services/tests/servicestests/src/com/android/server/notification/ValidateNotificationPeopleTest.java new file mode 100644 index 0000000..a6fdee9 --- /dev/null +++ b/services/tests/servicestests/src/com/android/server/notification/ValidateNotificationPeopleTest.java @@ -0,0 +1,152 @@ +/* + * Copyright (C) 2014 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 com.android.server.notification; + +import android.app.Notification; +import android.os.Bundle; +import android.test.AndroidTestCase; +import android.test.suitebuilder.annotation.SmallTest; +import android.text.SpannableString; + +import java.util.ArrayList; +import java.util.Arrays; + +public class ValidateNotificationPeopleTest extends AndroidTestCase { + + @SmallTest + public void testNoExtra() throws Exception { + Bundle bundle = new Bundle(); + String[] result = ValidateNotificationPeople.getExtraPeople(bundle); + assertNull("lack of extra should return null", result); + } + + @SmallTest + public void testSingleString() throws Exception { + String[] expected = { "foobar" }; + Bundle bundle = new Bundle(); + bundle.putString(Notification.EXTRA_PEOPLE, expected[0]); + String[] result = ValidateNotificationPeople.getExtraPeople(bundle); + assertStringArrayEquals("string should be in result[0]", expected, result); + } + + @SmallTest + public void testSingleCharArray() throws Exception { + String[] expected = { "foobar" }; + Bundle bundle = new Bundle(); + bundle.putCharArray(Notification.EXTRA_PEOPLE, expected[0].toCharArray()); + String[] result = ValidateNotificationPeople.getExtraPeople(bundle); + assertStringArrayEquals("char[] should be in result[0]", expected, result); + } + + @SmallTest + public void testSingleCharSequence() throws Exception { + String[] expected = { "foobar" }; + Bundle bundle = new Bundle(); + bundle.putCharSequence(Notification.EXTRA_PEOPLE, new SpannableString(expected[0])); + String[] result = ValidateNotificationPeople.getExtraPeople(bundle); + assertStringArrayEquals("charSequence should be in result[0]", expected, result); + } + + @SmallTest + public void testStringArraySingle() throws Exception { + Bundle bundle = new Bundle(); + String[] expected = { "foobar" }; + bundle.putStringArray(Notification.EXTRA_PEOPLE, expected); + String[] result = ValidateNotificationPeople.getExtraPeople(bundle); + assertStringArrayEquals("wrapped string should be in result[0]", expected, result); + } + + @SmallTest + public void testStringArrayMultiple() throws Exception { + Bundle bundle = new Bundle(); + String[] expected = { "foo", "bar", "baz" }; + bundle.putStringArray(Notification.EXTRA_PEOPLE, expected); + String[] result = ValidateNotificationPeople.getExtraPeople(bundle); + assertStringArrayEquals("testStringArrayMultiple", expected, result); + } + + @SmallTest + public void testStringArrayNulls() throws Exception { + Bundle bundle = new Bundle(); + String[] expected = { "foo", null, "baz" }; + bundle.putStringArray(Notification.EXTRA_PEOPLE, expected); + String[] result = ValidateNotificationPeople.getExtraPeople(bundle); + assertStringArrayEquals("testStringArrayNulls", expected, result); + } + + @SmallTest + public void testCharSequenceArrayMultiple() throws Exception { + Bundle bundle = new Bundle(); + String[] expected = { "foo", "bar", "baz" }; + CharSequence[] charSeqArray = new CharSequence[expected.length]; + for (int i = 0; i < expected.length; i++) { + charSeqArray[i] = new SpannableString(expected[i]); + } + bundle.putCharSequenceArray(Notification.EXTRA_PEOPLE, charSeqArray); + String[] result = ValidateNotificationPeople.getExtraPeople(bundle); + assertStringArrayEquals("testCharSequenceArrayMultiple", expected, result); + } + + @SmallTest + public void testMixedCharSequenceArrayList() throws Exception { + Bundle bundle = new Bundle(); + String[] expected = { "foo", "bar", "baz" }; + CharSequence[] charSeqArray = new CharSequence[expected.length]; + for (int i = 0; i < expected.length; i++) { + if (i % 2 == 0) { + charSeqArray[i] = expected[i]; + } else { + charSeqArray[i] = new SpannableString(expected[i]); + } + } + bundle.putCharSequenceArray(Notification.EXTRA_PEOPLE, charSeqArray); + String[] result = ValidateNotificationPeople.getExtraPeople(bundle); + assertStringArrayEquals("testMixedCharSequenceArrayList", expected, result); + } + + @SmallTest + public void testStringArrayList() throws Exception { + Bundle bundle = new Bundle(); + String[] expected = { "foo", null, "baz" }; + final ArrayList<String> stringArrayList = new ArrayList<String>(expected.length); + for (int i = 0; i < expected.length; i++) { + stringArrayList.add(expected[i]); + } + bundle.putStringArrayList(Notification.EXTRA_PEOPLE, stringArrayList); + String[] result = ValidateNotificationPeople.getExtraPeople(bundle); + assertStringArrayEquals("testStringArrayList", expected, result); + } + + @SmallTest + public void testCharSequenceArrayList() throws Exception { + Bundle bundle = new Bundle(); + String[] expected = { "foo", "bar", "baz" }; + final ArrayList<CharSequence> stringArrayList = + new ArrayList<CharSequence>(expected.length); + for (int i = 0; i < expected.length; i++) { + stringArrayList.add(new SpannableString(expected[i])); + } + bundle.putCharSequenceArrayList(Notification.EXTRA_PEOPLE, stringArrayList); + String[] result = ValidateNotificationPeople.getExtraPeople(bundle); + assertStringArrayEquals("testCharSequenceArrayList", expected, result); + } + + private void assertStringArrayEquals(String message, String[] expected, String[] result) { + String expectedString = Arrays.toString(expected); + String resultString = Arrays.toString(result); + assertEquals(message + ": arrays differ", expectedString, resultString); + } +} |