1 /*
2  * Copyright (C) 2019 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.appsecurity.cts;
18 
19 import static org.junit.Assert.assertNotNull;
20 
21 import android.platform.test.annotations.AppModeFull;
22 
23 import com.android.ddmlib.Log;
24 import com.android.tradefed.testtype.DeviceJUnit4ClassRunner;
25 
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 
30 /**
31  * Set of tests that verify behavior related to apps with shared user IDs
32  */
33 @RunWith(DeviceJUnit4ClassRunner.class)
34 public class SharedUserIdTest extends BaseAppSecurityTest {
35 
36     // testSharedUidDifferentCerts constants
37     private static final String SHARED_UI_APK = "CtsSharedUidInstall.apk";
38     private static final String SHARED_UI_PKG = "com.android.cts.shareuidinstall";
39     private static final String SHARED_UI_DIFF_CERT_APK = "CtsSharedUidInstallDiffCert.apk";
40     private static final String SHARED_UI_DIFF_CERT_PKG =
41             "com.android.cts.shareuidinstalldiffcert";
42     private static final String LOG_TAG = "SharedUserIdTest";
43     public static final String SHARED_UI_TEST_CLASS =
44             "com.android.cts.shareduidinstallapp.SharedUidPackageTest";
45     public static final String SHARED_UI_TEST_METHOD = "testSelfIsOnlyMemberOfSharedUserIdPackages";
46 
47     @Before
setUp()48     public void setUp() throws Exception {
49         Utils.prepareSingleUser(getDevice());
50         assertNotNull(getBuild());
51     }
52 
53     /**
54      * Test that an app that declares the same shared uid as an existing app, cannot be installed
55      * if it is signed with a different certificate.
56      */
57     @Test
58     @AppModeFull(reason = "Instant applications can't define shared UID")
testSharedUidDifferentCerts()59     public void testSharedUidDifferentCerts() throws Exception {
60         Log.i(LOG_TAG, "installing apks with shared uid, but different certs");
61         try {
62             getDevice().uninstallPackage(SHARED_UI_PKG);
63             getDevice().uninstallPackage(SHARED_UI_DIFF_CERT_PKG);
64 
65             new InstallMultiple().addFile(SHARED_UI_APK).run();
66             runDeviceTests(SHARED_UI_PKG, SHARED_UI_TEST_CLASS, SHARED_UI_TEST_METHOD);
67             new InstallMultiple().addFile(SHARED_UI_DIFF_CERT_APK)
68                     .runExpectingFailure("INSTALL_FAILED_SHARED_USER_INCOMPATIBLE");
69             runDeviceTests(SHARED_UI_PKG, SHARED_UI_TEST_CLASS, SHARED_UI_TEST_METHOD);
70         } finally {
71             getDevice().uninstallPackage(SHARED_UI_PKG);
72             getDevice().uninstallPackage(SHARED_UI_DIFF_CERT_PKG);
73         }
74     }
75 }
76