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