1 /* 2 * Copyright (C) 2017 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 17 package android.content.cts; 18 19 import android.appsecurity.cts.Utils; 20 import android.platform.test.annotations.SecurityTest; 21 22 import com.android.compatibility.common.tradefed.build.CompatibilityBuildHelper; 23 import com.android.tradefed.build.IBuildInfo; 24 import com.android.tradefed.device.DeviceNotAvailableException; 25 import com.android.tradefed.log.LogUtil; 26 import com.android.tradefed.testtype.DeviceTestCase; 27 import com.android.tradefed.testtype.IBuildReceiver; 28 29 /** 30 * Tests that invalid authorities are cleared from the sync data files on boot. 31 * Otherwise a malicious app can effectively DOS the filesystem and the user can only get out of it 32 * via a factory reset. 33 */ 34 @SecurityTest 35 public class InvalidSyncAuthoritiesHostTest extends DeviceTestCase implements IBuildReceiver { 36 37 private static final String DEVICE_TEST_PACKAGE = "android.content.sync.cts"; 38 private static final String DEVICE_TEST_CLASS = ".InvalidSyncAuthoritiesDeviceTest"; 39 private static final String DEVICE_TEST_APK = "CtsSyncInvalidAccountAuthorityTestCases.apk"; 40 41 private CompatibilityBuildHelper mBuildHelper; 42 43 @Override 44 public void setBuild(IBuildInfo iBuildInfo) { 45 mBuildHelper = new CompatibilityBuildHelper(iBuildInfo); 46 } 47 48 @Override 49 protected void setUp() throws Exception { 50 super.setUp(); 51 getDevice().uninstallPackage(DEVICE_TEST_PACKAGE); 52 53 assertNull(getDevice().installPackage(mBuildHelper.getTestFile(DEVICE_TEST_APK), false, 54 false)); 55 } 56 57 @Override 58 protected void tearDown() throws Exception { 59 super.tearDown(); 60 try { 61 runDeviceTests(DEVICE_TEST_PACKAGE, DEVICE_TEST_CLASS, "removeTestAccount"); 62 } catch (AssertionError exc) { 63 // Test account got left on the device. This test should still pass. 64 LogUtil.CLog.w("removeTestAccount failed: " + exc.getMessage()); 65 } 66 getDevice().uninstallPackage(DEVICE_TEST_PACKAGE); 67 } 68 69 public void testInvalidEntriesClearedOnBoot() throws Exception { 70 runDeviceTests(DEVICE_TEST_PACKAGE, DEVICE_TEST_CLASS, "populateAndTestSyncAutomaticallyBeforeReboot"); 71 getDevice().reboot(); 72 runDeviceTests(DEVICE_TEST_PACKAGE, DEVICE_TEST_CLASS, "testSyncAutomaticallyAfterReboot"); 73 } 74 75 private void runDeviceTests(String packageName, String testClassName, String testMethodName) 76 throws DeviceNotAvailableException { 77 Utils.runDeviceTests(getDevice(), packageName, testClassName, testMethodName); 78 } 79 } 80